|
Added
Link Here
|
| 1 |
/*- |
| 2 |
* Copyright (c) 2004 Eric Anholt |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 |
* SUCH DAMAGE. |
| 25 |
* |
| 26 |
* Based on reading the Linux 2.6.8.1 driver by Dave Jones. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <sys/cdefs.h> |
| 30 |
__FBSDID("$FreeBSD$"); |
| 31 |
|
| 32 |
#include "opt_bus.h" |
| 33 |
|
| 34 |
#include <sys/param.h> |
| 35 |
#include <sys/systm.h> |
| 36 |
#include <sys/malloc.h> |
| 37 |
#include <sys/kernel.h> |
| 38 |
#include <sys/module.h> |
| 39 |
#include <sys/bus.h> |
| 40 |
#include <sys/lock.h> |
| 41 |
#include <sys/mutex.h> |
| 42 |
#include <sys/proc.h> |
| 43 |
|
| 44 |
#include <dev/pci/pcivar.h> |
| 45 |
#include <dev/pci/pcireg.h> |
| 46 |
#include <pci/agppriv.h> |
| 47 |
#include <pci/agpreg.h> |
| 48 |
|
| 49 |
#include <vm/vm.h> |
| 50 |
#include <vm/vm_object.h> |
| 51 |
#include <vm/pmap.h> |
| 52 |
#include <machine/bus.h> |
| 53 |
#include <machine/resource.h> |
| 54 |
#include <sys/rman.h> |
| 55 |
|
| 56 |
MALLOC_DECLARE(M_AGP); |
| 57 |
|
| 58 |
#define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off) |
| 59 |
#define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v) |
| 60 |
|
| 61 |
struct agp_ati_softc { |
| 62 |
struct agp_softc agp; |
| 63 |
struct resource *regs; /* memory mapped control registers */ |
| 64 |
bus_space_tag_t bst; /* bus_space tag */ |
| 65 |
bus_space_handle_t bsh; /* bus_space handle */ |
| 66 |
u_int32_t initial_aperture; /* aperture size at startup */ |
| 67 |
char is_rs300; |
| 68 |
|
| 69 |
/* The GATT */ |
| 70 |
u_int32_t ag_entries; |
| 71 |
u_int32_t *ag_virtual; /* virtual address of gatt */ |
| 72 |
u_int32_t *ag_vdir; /* virtual address of page dir */ |
| 73 |
vm_offset_t ag_pdir; /* physical address of page dir */ |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
static const char* |
| 78 |
agp_ati_match(device_t dev) |
| 79 |
{ |
| 80 |
if (pci_get_class(dev) != PCIC_BRIDGE |
| 81 |
|| pci_get_subclass(dev) != PCIS_BRIDGE_HOST) |
| 82 |
return NULL; |
| 83 |
|
| 84 |
if (agp_find_caps(dev) == 0) |
| 85 |
return NULL; |
| 86 |
|
| 87 |
switch (pci_get_devid(dev)) { |
| 88 |
case 0xcab01002: |
| 89 |
return ("ATI RS100 AGP bridge"); |
| 90 |
case 0xcab21002: |
| 91 |
return ("ATI RS200 AGP bridge"); |
| 92 |
case 0xcab31002: |
| 93 |
return ("ATI RS250 AGP bridge"); |
| 94 |
case 0x58301002: |
| 95 |
return ("ATI RS300_100 AGP bridge"); |
| 96 |
case 0x58311002: |
| 97 |
return ("ATI RS300_133 AGP bridge"); |
| 98 |
case 0x58321002: |
| 99 |
return ("ATI RS300_166 AGP bridge"); |
| 100 |
case 0x58331002: |
| 101 |
return ("ATI RS300_200 AGP bridge"); |
| 102 |
}; |
| 103 |
|
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
static int |
| 108 |
agp_ati_probe(device_t dev) |
| 109 |
{ |
| 110 |
const char *desc; |
| 111 |
|
| 112 |
desc = agp_ati_match(dev); |
| 113 |
if (desc) { |
| 114 |
device_verbose(dev); |
| 115 |
device_set_desc(dev, desc); |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
return ENXIO; |
| 120 |
} |
| 121 |
|
| 122 |
static int |
| 123 |
agp_ati_alloc_gatt(device_t dev) |
| 124 |
{ |
| 125 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 126 |
u_int32_t apsize = AGP_GET_APERTURE(dev); |
| 127 |
u_int32_t entries = apsize >> AGP_PAGE_SHIFT; |
| 128 |
int i; |
| 129 |
|
| 130 |
/* Alloc the GATT -- pointers to pages of AGP memory */ |
| 131 |
sc->ag_entries = entries; |
| 132 |
sc->ag_virtual = malloc(entries * sizeof(u_int32_t), M_AGP, |
| 133 |
M_NOWAIT | M_ZERO); |
| 134 |
if (sc->ag_virtual == NULL) { |
| 135 |
if (bootverbose) |
| 136 |
device_printf(dev, "aperture allocation failed\n"); |
| 137 |
return ENOMEM; |
| 138 |
} |
| 139 |
|
| 140 |
/* Alloc the page directory -- pointers to each page of the GATT */ |
| 141 |
sc->ag_vdir = malloc(AGP_PAGE_SIZE, M_AGP, M_NOWAIT | M_ZERO); |
| 142 |
if (sc->ag_vdir == NULL) { |
| 143 |
if (bootverbose) |
| 144 |
device_printf(dev, "pagedir allocation failed\n"); |
| 145 |
free(sc->ag_virtual, M_AGP); |
| 146 |
return ENOMEM; |
| 147 |
} |
| 148 |
sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir); |
| 149 |
|
| 150 |
/* Fill in the pagedir's pointers to GATT pages */ |
| 151 |
for (i = 0; i < sc->ag_entries / 1024; i++) { |
| 152 |
vm_offset_t va; |
| 153 |
vm_offset_t pa; |
| 154 |
|
| 155 |
va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE; |
| 156 |
pa = vtophys(va); |
| 157 |
sc->ag_vdir[i] = pa | 1; |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* Make sure the chipset can see everything. |
| 162 |
*/ |
| 163 |
agp_flush_cache(); |
| 164 |
|
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static int |
| 170 |
agp_ati_attach(device_t dev) |
| 171 |
{ |
| 172 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 173 |
int error, rid; |
| 174 |
u_int32_t temp; |
| 175 |
|
| 176 |
error = agp_generic_attach(dev); |
| 177 |
if (error) |
| 178 |
return error; |
| 179 |
|
| 180 |
switch (pci_get_devid(dev)) { |
| 181 |
case 0xcab01002: /* ATI RS100 AGP bridge */ |
| 182 |
case 0xcab21002: /*ATI RS200 AGP bridge */ |
| 183 |
case 0xcab31002: /* ATI RS250 AGP bridge */ |
| 184 |
sc->is_rs300 = 0; |
| 185 |
case 0x58301002: /* ATI RS300_100 AGP bridge */ |
| 186 |
case 0x58311002: /* ATI RS300_133 AGP bridge */ |
| 187 |
case 0x58321002: /* ATI RS300_166 AGP bridge */ |
| 188 |
case 0x58331002: /* ATI RS300_200 AGP bridge */ |
| 189 |
sc->is_rs300 = 1; |
| 190 |
default: |
| 191 |
/* Unknown chipset */ |
| 192 |
return EINVAL; |
| 193 |
}; |
| 194 |
|
| 195 |
rid = ATI_GART_MMADDR; |
| 196 |
sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); |
| 197 |
if (!sc->regs) { |
| 198 |
agp_generic_detach(dev); |
| 199 |
return ENOMEM; |
| 200 |
} |
| 201 |
|
| 202 |
sc->bst = rman_get_bustag(sc->regs); |
| 203 |
sc->bsh = rman_get_bushandle(sc->regs); |
| 204 |
|
| 205 |
sc->initial_aperture = AGP_GET_APERTURE(dev); |
| 206 |
|
| 207 |
for (;;) { |
| 208 |
if (agp_ati_alloc_gatt(dev) == 0) |
| 209 |
break; |
| 210 |
|
| 211 |
/* |
| 212 |
* Probably contigmalloc failure. Try reducing the |
| 213 |
* aperture so that the gatt size reduces. |
| 214 |
*/ |
| 215 |
if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) |
| 216 |
return ENOMEM; |
| 217 |
} |
| 218 |
|
| 219 |
if (sc->is_rs300) |
| 220 |
pci_write_config(dev, ATI_RS300_IG_AGPMODE, 0x20000, 4); |
| 221 |
else |
| 222 |
pci_write_config(dev, ATI_RS100_IG_AGPMODE, 0x20000, 4); |
| 223 |
|
| 224 |
WRITE4(ATI_GART_FEATURE_ID, 0x00060000); |
| 225 |
|
| 226 |
temp = pci_read_config(dev, 4, 4); /* XXX: Magic reg# */ |
| 227 |
pci_write_config(dev, 4, temp | (1 << 14), 4); |
| 228 |
|
| 229 |
WRITE4(ATI_GART_BASE, sc->ag_pdir); |
| 230 |
|
| 231 |
AGP_FLUSH_TLB(dev); |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
static int |
| 237 |
agp_ati_detach(device_t dev) |
| 238 |
{ |
| 239 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 240 |
int error; |
| 241 |
|
| 242 |
error = agp_generic_detach(dev); |
| 243 |
if (error) |
| 244 |
return error; |
| 245 |
|
| 246 |
/* Clear the GATT base */ |
| 247 |
WRITE4(ATI_GART_BASE, 0); |
| 248 |
|
| 249 |
/* Put the aperture back the way it started. */ |
| 250 |
AGP_SET_APERTURE(dev, sc->initial_aperture); |
| 251 |
|
| 252 |
free(sc->ag_vdir, M_AGP); |
| 253 |
free(sc->ag_virtual, M_AGP); |
| 254 |
|
| 255 |
bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs); |
| 256 |
|
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
static u_int32_t |
| 261 |
agp_ati_get_aperture(device_t dev) |
| 262 |
{ |
| 263 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 264 |
int size_value; |
| 265 |
|
| 266 |
if (sc->is_rs300) |
| 267 |
size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4); |
| 268 |
else |
| 269 |
size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4); |
| 270 |
|
| 271 |
size_value = (size_value & 0x0000000e) >> 1; |
| 272 |
size_value = (32 * 1024 * 1024) << size_value; |
| 273 |
|
| 274 |
return size_value; |
| 275 |
} |
| 276 |
|
| 277 |
static int |
| 278 |
agp_ati_set_aperture(device_t dev, u_int32_t aperture) |
| 279 |
{ |
| 280 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 281 |
int size_value; |
| 282 |
|
| 283 |
if (sc->is_rs300) |
| 284 |
size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4); |
| 285 |
else |
| 286 |
size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4); |
| 287 |
|
| 288 |
size_value &= ~0x0000000e; |
| 289 |
size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1; |
| 290 |
|
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
static int |
| 295 |
agp_ati_bind_page(device_t dev, int offset, vm_offset_t physical) |
| 296 |
{ |
| 297 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 298 |
|
| 299 |
if (offset < 0 || offset >= (sc->ag_entries << AGP_PAGE_SHIFT)) |
| 300 |
return EINVAL; |
| 301 |
|
| 302 |
sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; |
| 303 |
|
| 304 |
/* invalidate the cache */ |
| 305 |
AGP_FLUSH_TLB(dev); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
static int |
| 310 |
agp_ati_unbind_page(device_t dev, int offset) |
| 311 |
{ |
| 312 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 313 |
|
| 314 |
if (offset < 0 || offset >= (sc->ag_entries << AGP_PAGE_SHIFT)) |
| 315 |
return EINVAL; |
| 316 |
|
| 317 |
sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; |
| 318 |
AGP_FLUSH_TLB(dev); /* XXX: Shouldn't other drivers be doing this? */ |
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
static void |
| 323 |
agp_ati_flush_tlb(device_t dev) |
| 324 |
{ |
| 325 |
struct agp_ati_softc *sc = device_get_softc(dev); |
| 326 |
|
| 327 |
/* Set the cache invalidate bit and wait for the chipset to clear */ |
| 328 |
WRITE4(ATI_GART_CACHE_CNTRL, 1); |
| 329 |
} |
| 330 |
|
| 331 |
static device_method_t agp_ati_methods[] = { |
| 332 |
/* Device interface */ |
| 333 |
DEVMETHOD(device_probe, agp_ati_probe), |
| 334 |
DEVMETHOD(device_attach, agp_ati_attach), |
| 335 |
DEVMETHOD(device_detach, agp_ati_detach), |
| 336 |
DEVMETHOD(device_shutdown, bus_generic_shutdown), |
| 337 |
DEVMETHOD(device_suspend, bus_generic_suspend), |
| 338 |
DEVMETHOD(device_resume, bus_generic_resume), |
| 339 |
|
| 340 |
/* AGP interface */ |
| 341 |
DEVMETHOD(agp_get_aperture, agp_ati_get_aperture), |
| 342 |
DEVMETHOD(agp_set_aperture, agp_ati_set_aperture), |
| 343 |
DEVMETHOD(agp_bind_page, agp_ati_bind_page), |
| 344 |
DEVMETHOD(agp_unbind_page, agp_ati_unbind_page), |
| 345 |
DEVMETHOD(agp_flush_tlb, agp_ati_flush_tlb), |
| 346 |
DEVMETHOD(agp_enable, agp_generic_enable), |
| 347 |
DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), |
| 348 |
DEVMETHOD(agp_free_memory, agp_generic_free_memory), |
| 349 |
DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), |
| 350 |
DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), |
| 351 |
|
| 352 |
{ 0, 0 } |
| 353 |
}; |
| 354 |
|
| 355 |
static driver_t agp_ati_driver = { |
| 356 |
"agp", |
| 357 |
agp_ati_methods, |
| 358 |
sizeof(struct agp_ati_softc), |
| 359 |
}; |
| 360 |
|
| 361 |
static devclass_t agp_devclass; |
| 362 |
|
| 363 |
DRIVER_MODULE(agp_ati, pci, agp_ati_driver, agp_devclass, 0, 0); |
| 364 |
MODULE_DEPEND(agp_ati, agp, 1, 1, 1); |
| 365 |
MODULE_DEPEND(agp_ati, pci, 1, 1, 1); |