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