1 /*- 2 * Copyright (c) 2000 Doug Rabson 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 * $FreeBSD$ 27 */ 28 29 #include "opt_bus.h" 30 #include "opt_pci.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/kernel.h> 36 #include <sys/bus.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/proc.h> 40 41 #include <pci/pcivar.h> 42 #include <pci/pcireg.h> 43 #include <pci/agppriv.h> 44 #include <pci/agpreg.h> 45 46 #include <vm/vm.h> 47 #include <vm/vm_object.h> 48 #include <vm/pmap.h> 49 #include <machine/bus.h> 50 #include <machine/resource.h> 51 #include <sys/rman.h> 52 53 MALLOC_DECLARE(M_AGP); 54 55 #define READ2(off) bus_space_read_2(sc->bst, sc->bsh, off) 56 #define READ4(off) bus_space_read_4(sc->bst, sc->bsh, off) 57 #define WRITE2(off,v) bus_space_write_2(sc->bst, sc->bsh, off, v) 58 #define WRITE4(off,v) bus_space_write_4(sc->bst, sc->bsh, off, v) 59 60 struct agp_amd_gatt { 61 u_int32_t ag_entries; 62 u_int32_t *ag_vdir; /* virtual address of page dir */ 63 vm_offset_t ag_pdir; /* physical address of page dir */ 64 u_int32_t *ag_virtual; /* virtual address of gatt */ 65 }; 66 67 struct agp_amd_softc { 68 struct agp_softc agp; 69 struct resource *regs; /* memory mapped control registers */ 70 bus_space_tag_t bst; /* bus_space tag */ 71 bus_space_handle_t bsh; /* bus_space handle */ 72 u_int32_t initial_aperture; /* aperture size at startup */ 73 struct agp_amd_gatt *gatt; 74 }; 75 76 static struct agp_amd_gatt * 77 agp_amd_alloc_gatt(device_t dev) 78 { 79 u_int32_t apsize = AGP_GET_APERTURE(dev); 80 u_int32_t entries = apsize >> AGP_PAGE_SHIFT; 81 struct agp_amd_gatt *gatt; 82 int i, npages; 83 84 if (bootverbose) 85 device_printf(dev, 86 "allocating GATT for aperture of size %dM\n", 87 apsize / (1024*1024)); 88 89 gatt = malloc(sizeof(struct agp_amd_gatt), M_AGP, M_NOWAIT); 90 if (!gatt) 91 return 0; 92 93 /* 94 * The AMD751 uses a page directory to map a non-contiguous 95 * gatt so we don't need to use contigmalloc. 96 */ 97 gatt->ag_entries = entries; 98 gatt->ag_virtual = malloc(entries * sizeof(u_int32_t), 99 M_AGP, M_NOWAIT); 100 if (!gatt->ag_virtual) { 101 if (bootverbose) 102 device_printf(dev, "allocation failed\n"); 103 free(gatt, M_AGP); 104 return 0; 105 } 106 bzero(gatt->ag_virtual, entries * sizeof(u_int32_t)); 107 108 /* 109 * Allocate the page directory. 110 */ 111 gatt->ag_vdir = malloc(AGP_PAGE_SIZE, M_AGP, M_NOWAIT); 112 if (!gatt->ag_vdir) { 113 if (bootverbose) 114 device_printf(dev, 115 "failed to allocate page directory\n"); 116 free(gatt->ag_virtual, M_AGP); 117 free(gatt, M_AGP); 118 return 0; 119 } 120 bzero(gatt->ag_vdir, AGP_PAGE_SIZE); 121 gatt->ag_pdir = vtophys((vm_offset_t) gatt->ag_vdir); 122 gatt->ag_pdir = vtophys(gatt->ag_virtual); 123 124 /* 125 * Map the pages of the GATT into the page directory. 126 */ 127 npages = ((entries * sizeof(u_int32_t) + AGP_PAGE_SIZE - 1) 128 >> AGP_PAGE_SHIFT); 129 for (i = 0; i < npages; i++) { 130 vm_offset_t va; 131 vm_offset_t pa; 132 133 va = ((vm_offset_t) gatt->ag_virtual) + i * AGP_PAGE_SIZE; 134 pa = vtophys(va); 135 gatt->ag_vdir[i] = pa | 1; 136 } 137 138 /* 139 * Make sure the chipset can see everything. 140 */ 141 agp_flush_cache(); 142 143 return gatt; 144 } 145 146 static void 147 agp_amd_free_gatt(struct agp_amd_gatt *gatt) 148 { 149 free(gatt->ag_virtual, M_AGP); 150 free(gatt->ag_vdir, M_AGP); 151 free(gatt, M_AGP); 152 } 153 154 static const char* 155 agp_amd_match(device_t dev) 156 { 157 if (pci_get_class(dev) != PCIC_BRIDGE 158 || pci_get_subclass(dev) != PCIS_BRIDGE_HOST) 159 return NULL; 160 161 if (agp_find_caps(dev) == 0) 162 return NULL; 163 164 switch (pci_get_devid(dev)) { 165 case 0x700e1022: 166 return ("AMD 761 host to AGP bridge"); 167 case 0x70061022: 168 return ("AMD 751 host to AGP bridge"); 169 }; 170 171 return NULL; 172 } 173 174 static int 175 agp_amd_probe(device_t dev) 176 { 177 const char *desc; 178 179 desc = agp_amd_match(dev); 180 if (desc) { 181 device_verbose(dev); 182 device_set_desc(dev, desc); 183 return 0; 184 } 185 186 return ENXIO; 187 } 188 189 static int 190 agp_amd_attach(device_t dev) 191 { 192 struct agp_amd_softc *sc = device_get_softc(dev); 193 struct agp_amd_gatt *gatt; 194 int error, rid; 195 196 error = agp_generic_attach(dev); 197 if (error) 198 return error; 199 200 rid = AGP_AMD751_REGISTERS; 201 sc->regs = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 202 0, ~0, 1, RF_ACTIVE); 203 if (!sc->regs) { 204 agp_generic_detach(dev); 205 return ENOMEM; 206 } 207 208 sc->bst = rman_get_bustag(sc->regs); 209 sc->bsh = rman_get_bushandle(sc->regs); 210 211 sc->initial_aperture = AGP_GET_APERTURE(dev); 212 213 for (;;) { 214 gatt = agp_amd_alloc_gatt(dev); 215 if (gatt) 216 break; 217 218 /* 219 * Probably contigmalloc failure. Try reducing the 220 * aperture so that the gatt size reduces. 221 */ 222 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) 223 return ENOMEM; 224 } 225 sc->gatt = gatt; 226 227 /* Install the gatt. */ 228 WRITE4(AGP_AMD751_ATTBASE, gatt->ag_pdir); 229 230 /* Enable synchronisation between host and agp. */ 231 pci_write_config(dev, 232 AGP_AMD751_MODECTRL, 233 AGP_AMD751_MODECTRL_SYNEN, 1); 234 235 /* Set indexing mode for two-level and enable page dir cache */ 236 pci_write_config(dev, 237 AGP_AMD751_MODECTRL2, 238 AGP_AMD751_MODECTRL2_GPDCE, 1); 239 240 /* Enable the TLB and flush */ 241 WRITE2(AGP_AMD751_STATUS, 242 READ2(AGP_AMD751_STATUS) | AGP_AMD751_STATUS_GCE); 243 AGP_FLUSH_TLB(dev); 244 245 return 0; 246 } 247 248 static int 249 agp_amd_detach(device_t dev) 250 { 251 struct agp_amd_softc *sc = device_get_softc(dev); 252 int error; 253 254 error = agp_generic_detach(dev); 255 if (error) 256 return error; 257 258 /* Disable the TLB.. */ 259 WRITE2(AGP_AMD751_STATUS, 260 READ2(AGP_AMD751_STATUS) & ~AGP_AMD751_STATUS_GCE); 261 262 /* Disable host-agp sync */ 263 pci_write_config(dev, AGP_AMD751_MODECTRL, 0x00, 1); 264 265 /* Clear the GATT base */ 266 WRITE4(AGP_AMD751_ATTBASE, 0); 267 268 /* Put the aperture back the way it started. */ 269 AGP_SET_APERTURE(dev, sc->initial_aperture); 270 271 agp_amd_free_gatt(sc->gatt); 272 273 bus_release_resource(dev, SYS_RES_MEMORY, 274 AGP_AMD751_REGISTERS, sc->regs); 275 276 return 0; 277 } 278 279 static u_int32_t 280 agp_amd_get_aperture(device_t dev) 281 { 282 int vas; 283 284 /* 285 * The aperture size is equal to 32M<<vas. 286 */ 287 vas = (pci_read_config(dev, AGP_AMD751_APCTRL, 1) & 0x06) >> 1; 288 return (32*1024*1024) << vas; 289 } 290 291 static int 292 agp_amd_set_aperture(device_t dev, u_int32_t aperture) 293 { 294 int vas; 295 296 /* 297 * Check for a power of two and make sure its within the 298 * programmable range. 299 */ 300 if (aperture & (aperture - 1) 301 || aperture < 32*1024*1024 302 || aperture > 2U*1024*1024*1024) 303 return EINVAL; 304 305 vas = ffs(aperture / 32*1024*1024) - 1; 306 307 pci_write_config(dev, AGP_AMD751_APCTRL, 308 ((pci_read_config(dev, AGP_AMD751_APCTRL, 1) & ~0x06) 309 | vas << 1), 1); 310 311 return 0; 312 } 313 314 static int 315 agp_amd_bind_page(device_t dev, int offset, vm_offset_t physical) 316 { 317 struct agp_amd_softc *sc = device_get_softc(dev); 318 319 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 320 return EINVAL; 321 322 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1; 323 return 0; 324 } 325 326 static int 327 agp_amd_unbind_page(device_t dev, int offset) 328 { 329 struct agp_amd_softc *sc = device_get_softc(dev); 330 331 if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 332 return EINVAL; 333 334 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 335 return 0; 336 } 337 338 static void 339 agp_amd_flush_tlb(device_t dev) 340 { 341 struct agp_amd_softc *sc = device_get_softc(dev); 342 343 /* Set the cache invalidate bit and wait for the chipset to clear */ 344 WRITE4(AGP_AMD751_TLBCTRL, 1); 345 do { 346 DELAY(1); 347 } while (READ4(AGP_AMD751_TLBCTRL)); 348 } 349 350 static device_method_t agp_amd_methods[] = { 351 /* Device interface */ 352 DEVMETHOD(device_probe, agp_amd_probe), 353 DEVMETHOD(device_attach, agp_amd_attach), 354 DEVMETHOD(device_detach, agp_amd_detach), 355 DEVMETHOD(device_shutdown, bus_generic_shutdown), 356 DEVMETHOD(device_suspend, bus_generic_suspend), 357 DEVMETHOD(device_resume, bus_generic_resume), 358 359 /* AGP interface */ 360 DEVMETHOD(agp_get_aperture, agp_amd_get_aperture), 361 DEVMETHOD(agp_set_aperture, agp_amd_set_aperture), 362 DEVMETHOD(agp_bind_page, agp_amd_bind_page), 363 DEVMETHOD(agp_unbind_page, agp_amd_unbind_page), 364 DEVMETHOD(agp_flush_tlb, agp_amd_flush_tlb), 365 DEVMETHOD(agp_enable, agp_generic_enable), 366 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 367 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 368 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 369 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 370 371 { 0, 0 } 372 }; 373 374 static driver_t agp_amd_driver = { 375 "agp", 376 agp_amd_methods, 377 sizeof(struct agp_amd_softc), 378 }; 379 380 static devclass_t agp_devclass; 381 382 DRIVER_MODULE(agp_amd, pci, agp_amd_driver, agp_devclass, 0, 0); 383