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 27#include <sys/cdefs.h>
| 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 27#include <sys/cdefs.h>
|
150 desc = agp_via_match(dev); 151 if (desc) { 152 device_set_desc(dev, desc); 153 return BUS_PROBE_DEFAULT; 154 } 155 156 return ENXIO; 157} 158 159static int 160agp_via_attach(device_t dev) 161{ 162 struct agp_via_softc *sc = device_get_softc(dev); 163 struct agp_gatt *gatt; 164 int error; 165 u_int32_t agpsel; 166 u_int32_t capid; 167 168 sc->regs = via_v2_regs; 169 170 /* Look at the capability register to see if we handle AGP3 */ 171 capid = pci_read_config(dev, agp_find_caps(dev) + AGP_CAPID, 4); 172 if (((capid >> 20) & 0x0f) >= 3) { 173 agpsel = pci_read_config(dev, AGP_VIA_AGPSEL, 1); 174 if ((agpsel & (1 << 1)) == 0) 175 sc->regs = via_v3_regs; 176 } 177 178 error = agp_generic_attach(dev); 179 if (error) 180 return error; 181 182 sc->initial_aperture = AGP_GET_APERTURE(dev); 183 184 for (;;) { 185 gatt = agp_alloc_gatt(dev); 186 if (gatt) 187 break; 188 189 /* 190 * Probably contigmalloc failure. Try reducing the 191 * aperture so that the gatt size reduces. 192 */ 193 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) { 194 agp_generic_detach(dev); 195 return ENOMEM; 196 } 197 } 198 sc->gatt = gatt; 199 200 if (sc->regs == via_v2_regs) { 201 /* Install the gatt. */ 202 pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical | 3, 4); 203 204 /* Enable the aperture. */ 205 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 206 } else { 207 u_int32_t gartctrl; 208 209 /* Install the gatt. */ 210 pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical, 4); 211 212 /* Enable the aperture. */ 213 gartctrl = pci_read_config(dev, sc->regs[REG_GARTCTRL], 4); 214 pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl | (3 << 7), 4); 215 } 216 217 device_printf(dev, "aperture size is %dM\n", 218 sc->initial_aperture / 1024 / 1024); 219 220 return 0; 221} 222 223static int 224agp_via_detach(device_t dev) 225{ 226 struct agp_via_softc *sc = device_get_softc(dev); 227 228 agp_free_cdev(dev); 229 230 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0, 4); 231 pci_write_config(dev, sc->regs[REG_ATTBASE], 0, 4); 232 AGP_SET_APERTURE(dev, sc->initial_aperture); 233 agp_free_gatt(sc->gatt); 234 agp_free_res(dev); 235 236 return 0; 237} 238 239static u_int32_t 240agp_via_get_aperture(device_t dev) 241{ 242 struct agp_via_softc *sc = device_get_softc(dev); 243 u_int32_t apsize; 244 245 if (sc->regs == via_v2_regs) { 246 apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 1); 247 248 /* 249 * The size is determined by the number of low bits of 250 * register APBASE which are forced to zero. The low 20 bits 251 * are always forced to zero and each zero bit in the apsize 252 * field just read forces the corresponding bit in the 27:20 253 * to be zero. We calculate the aperture size accordingly. 254 */ 255 return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1; 256 } else { 257 apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 2) & 0xfff; 258 switch (apsize) { 259 case 0x800: 260 return 0x80000000; 261 case 0xc00: 262 return 0x40000000; 263 case 0xe00: 264 return 0x20000000; 265 case 0xf00: 266 return 0x10000000; 267 case 0xf20: 268 return 0x08000000; 269 case 0xf30: 270 return 0x04000000; 271 case 0xf38: 272 return 0x02000000; 273 case 0xf3c: 274 return 0x01000000; 275 case 0xf3e: 276 return 0x00800000; 277 case 0xf3f: 278 return 0x00400000; 279 default: 280 device_printf(dev, "Invalid aperture setting 0x%x\n", 281 pci_read_config(dev, sc->regs[REG_APSIZE], 2)); 282 return 0; 283 } 284 } 285} 286 287static int 288agp_via_set_aperture(device_t dev, u_int32_t aperture) 289{ 290 struct agp_via_softc *sc = device_get_softc(dev); 291 u_int32_t apsize, key, val; 292 293 if (sc->regs == via_v2_regs) { 294 /* 295 * Reverse the magic from get_aperture. 296 */ 297 apsize = ((aperture - 1) >> 20) ^ 0xff; 298 299 /* 300 * Double check for sanity. 301 */ 302 if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture) 303 return EINVAL; 304 305 pci_write_config(dev, sc->regs[REG_APSIZE], apsize, 1); 306 } else { 307 switch (aperture) { 308 case 0x80000000: 309 key = 0x800; 310 break; 311 case 0x40000000: 312 key = 0xc00; 313 break; 314 case 0x20000000: 315 key = 0xe00; 316 break; 317 case 0x10000000: 318 key = 0xf00; 319 break; 320 case 0x08000000: 321 key = 0xf20; 322 break; 323 case 0x04000000: 324 key = 0xf30; 325 break; 326 case 0x02000000: 327 key = 0xf38; 328 break; 329 case 0x01000000: 330 key = 0xf3c; 331 break; 332 case 0x00800000: 333 key = 0xf3e; 334 break; 335 case 0x00400000: 336 key = 0xf3f; 337 break; 338 default: 339 device_printf(dev, "Invalid aperture size (%dMb)\n", 340 aperture / 1024 / 1024); 341 return EINVAL; 342 } 343 val = pci_read_config(dev, sc->regs[REG_APSIZE], 2); 344 pci_write_config(dev, sc->regs[REG_APSIZE], 345 ((val & ~0xfff) | key), 2); 346 } 347 return 0; 348} 349 350static int 351agp_via_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical) 352{ 353 struct agp_via_softc *sc = device_get_softc(dev); 354 355 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 356 return EINVAL; 357 358 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 359 return 0; 360} 361 362static int 363agp_via_unbind_page(device_t dev, vm_offset_t offset) 364{ 365 struct agp_via_softc *sc = device_get_softc(dev); 366 367 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 368 return EINVAL; 369 370 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 371 return 0; 372} 373 374static void 375agp_via_flush_tlb(device_t dev) 376{ 377 struct agp_via_softc *sc = device_get_softc(dev); 378 u_int32_t gartctrl; 379 380 if (sc->regs == via_v2_regs) { 381 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x8f, 4); 382 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 383 } else { 384 gartctrl = pci_read_config(dev, sc->regs[REG_GARTCTRL], 4); 385 pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl & 386 ~(1 << 7), 4); 387 pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl, 4); 388 } 389 390} 391 392static device_method_t agp_via_methods[] = { 393 /* Device interface */ 394 DEVMETHOD(device_probe, agp_via_probe), 395 DEVMETHOD(device_attach, agp_via_attach), 396 DEVMETHOD(device_detach, agp_via_detach), 397 DEVMETHOD(device_shutdown, bus_generic_shutdown), 398 DEVMETHOD(device_suspend, bus_generic_suspend), 399 DEVMETHOD(device_resume, bus_generic_resume), 400 401 /* AGP interface */ 402 DEVMETHOD(agp_get_aperture, agp_via_get_aperture), 403 DEVMETHOD(agp_set_aperture, agp_via_set_aperture), 404 DEVMETHOD(agp_bind_page, agp_via_bind_page), 405 DEVMETHOD(agp_unbind_page, agp_via_unbind_page), 406 DEVMETHOD(agp_flush_tlb, agp_via_flush_tlb), 407 DEVMETHOD(agp_enable, agp_generic_enable), 408 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 409 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 410 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 411 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 412 413 { 0, 0 } 414}; 415 416static driver_t agp_via_driver = { 417 "agp", 418 agp_via_methods, 419 sizeof(struct agp_via_softc), 420}; 421 422static devclass_t agp_devclass; 423 424DRIVER_MODULE(agp_via, hostb, agp_via_driver, agp_devclass, 0, 0); 425MODULE_DEPEND(agp_via, agp, 1, 1, 1); 426MODULE_DEPEND(agp_via, pci, 1, 1, 1);
| 152 desc = agp_via_match(dev); 153 if (desc) { 154 device_set_desc(dev, desc); 155 return BUS_PROBE_DEFAULT; 156 } 157 158 return ENXIO; 159} 160 161static int 162agp_via_attach(device_t dev) 163{ 164 struct agp_via_softc *sc = device_get_softc(dev); 165 struct agp_gatt *gatt; 166 int error; 167 u_int32_t agpsel; 168 u_int32_t capid; 169 170 sc->regs = via_v2_regs; 171 172 /* Look at the capability register to see if we handle AGP3 */ 173 capid = pci_read_config(dev, agp_find_caps(dev) + AGP_CAPID, 4); 174 if (((capid >> 20) & 0x0f) >= 3) { 175 agpsel = pci_read_config(dev, AGP_VIA_AGPSEL, 1); 176 if ((agpsel & (1 << 1)) == 0) 177 sc->regs = via_v3_regs; 178 } 179 180 error = agp_generic_attach(dev); 181 if (error) 182 return error; 183 184 sc->initial_aperture = AGP_GET_APERTURE(dev); 185 186 for (;;) { 187 gatt = agp_alloc_gatt(dev); 188 if (gatt) 189 break; 190 191 /* 192 * Probably contigmalloc failure. Try reducing the 193 * aperture so that the gatt size reduces. 194 */ 195 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) { 196 agp_generic_detach(dev); 197 return ENOMEM; 198 } 199 } 200 sc->gatt = gatt; 201 202 if (sc->regs == via_v2_regs) { 203 /* Install the gatt. */ 204 pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical | 3, 4); 205 206 /* Enable the aperture. */ 207 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 208 } else { 209 u_int32_t gartctrl; 210 211 /* Install the gatt. */ 212 pci_write_config(dev, sc->regs[REG_ATTBASE], gatt->ag_physical, 4); 213 214 /* Enable the aperture. */ 215 gartctrl = pci_read_config(dev, sc->regs[REG_GARTCTRL], 4); 216 pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl | (3 << 7), 4); 217 } 218 219 device_printf(dev, "aperture size is %dM\n", 220 sc->initial_aperture / 1024 / 1024); 221 222 return 0; 223} 224 225static int 226agp_via_detach(device_t dev) 227{ 228 struct agp_via_softc *sc = device_get_softc(dev); 229 230 agp_free_cdev(dev); 231 232 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0, 4); 233 pci_write_config(dev, sc->regs[REG_ATTBASE], 0, 4); 234 AGP_SET_APERTURE(dev, sc->initial_aperture); 235 agp_free_gatt(sc->gatt); 236 agp_free_res(dev); 237 238 return 0; 239} 240 241static u_int32_t 242agp_via_get_aperture(device_t dev) 243{ 244 struct agp_via_softc *sc = device_get_softc(dev); 245 u_int32_t apsize; 246 247 if (sc->regs == via_v2_regs) { 248 apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 1); 249 250 /* 251 * The size is determined by the number of low bits of 252 * register APBASE which are forced to zero. The low 20 bits 253 * are always forced to zero and each zero bit in the apsize 254 * field just read forces the corresponding bit in the 27:20 255 * to be zero. We calculate the aperture size accordingly. 256 */ 257 return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1; 258 } else { 259 apsize = pci_read_config(dev, sc->regs[REG_APSIZE], 2) & 0xfff; 260 switch (apsize) { 261 case 0x800: 262 return 0x80000000; 263 case 0xc00: 264 return 0x40000000; 265 case 0xe00: 266 return 0x20000000; 267 case 0xf00: 268 return 0x10000000; 269 case 0xf20: 270 return 0x08000000; 271 case 0xf30: 272 return 0x04000000; 273 case 0xf38: 274 return 0x02000000; 275 case 0xf3c: 276 return 0x01000000; 277 case 0xf3e: 278 return 0x00800000; 279 case 0xf3f: 280 return 0x00400000; 281 default: 282 device_printf(dev, "Invalid aperture setting 0x%x\n", 283 pci_read_config(dev, sc->regs[REG_APSIZE], 2)); 284 return 0; 285 } 286 } 287} 288 289static int 290agp_via_set_aperture(device_t dev, u_int32_t aperture) 291{ 292 struct agp_via_softc *sc = device_get_softc(dev); 293 u_int32_t apsize, key, val; 294 295 if (sc->regs == via_v2_regs) { 296 /* 297 * Reverse the magic from get_aperture. 298 */ 299 apsize = ((aperture - 1) >> 20) ^ 0xff; 300 301 /* 302 * Double check for sanity. 303 */ 304 if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture) 305 return EINVAL; 306 307 pci_write_config(dev, sc->regs[REG_APSIZE], apsize, 1); 308 } else { 309 switch (aperture) { 310 case 0x80000000: 311 key = 0x800; 312 break; 313 case 0x40000000: 314 key = 0xc00; 315 break; 316 case 0x20000000: 317 key = 0xe00; 318 break; 319 case 0x10000000: 320 key = 0xf00; 321 break; 322 case 0x08000000: 323 key = 0xf20; 324 break; 325 case 0x04000000: 326 key = 0xf30; 327 break; 328 case 0x02000000: 329 key = 0xf38; 330 break; 331 case 0x01000000: 332 key = 0xf3c; 333 break; 334 case 0x00800000: 335 key = 0xf3e; 336 break; 337 case 0x00400000: 338 key = 0xf3f; 339 break; 340 default: 341 device_printf(dev, "Invalid aperture size (%dMb)\n", 342 aperture / 1024 / 1024); 343 return EINVAL; 344 } 345 val = pci_read_config(dev, sc->regs[REG_APSIZE], 2); 346 pci_write_config(dev, sc->regs[REG_APSIZE], 347 ((val & ~0xfff) | key), 2); 348 } 349 return 0; 350} 351 352static int 353agp_via_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical) 354{ 355 struct agp_via_softc *sc = device_get_softc(dev); 356 357 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 358 return EINVAL; 359 360 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical; 361 return 0; 362} 363 364static int 365agp_via_unbind_page(device_t dev, vm_offset_t offset) 366{ 367 struct agp_via_softc *sc = device_get_softc(dev); 368 369 if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) 370 return EINVAL; 371 372 sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0; 373 return 0; 374} 375 376static void 377agp_via_flush_tlb(device_t dev) 378{ 379 struct agp_via_softc *sc = device_get_softc(dev); 380 u_int32_t gartctrl; 381 382 if (sc->regs == via_v2_regs) { 383 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x8f, 4); 384 pci_write_config(dev, sc->regs[REG_GARTCTRL], 0x0f, 4); 385 } else { 386 gartctrl = pci_read_config(dev, sc->regs[REG_GARTCTRL], 4); 387 pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl & 388 ~(1 << 7), 4); 389 pci_write_config(dev, sc->regs[REG_GARTCTRL], gartctrl, 4); 390 } 391 392} 393 394static device_method_t agp_via_methods[] = { 395 /* Device interface */ 396 DEVMETHOD(device_probe, agp_via_probe), 397 DEVMETHOD(device_attach, agp_via_attach), 398 DEVMETHOD(device_detach, agp_via_detach), 399 DEVMETHOD(device_shutdown, bus_generic_shutdown), 400 DEVMETHOD(device_suspend, bus_generic_suspend), 401 DEVMETHOD(device_resume, bus_generic_resume), 402 403 /* AGP interface */ 404 DEVMETHOD(agp_get_aperture, agp_via_get_aperture), 405 DEVMETHOD(agp_set_aperture, agp_via_set_aperture), 406 DEVMETHOD(agp_bind_page, agp_via_bind_page), 407 DEVMETHOD(agp_unbind_page, agp_via_unbind_page), 408 DEVMETHOD(agp_flush_tlb, agp_via_flush_tlb), 409 DEVMETHOD(agp_enable, agp_generic_enable), 410 DEVMETHOD(agp_alloc_memory, agp_generic_alloc_memory), 411 DEVMETHOD(agp_free_memory, agp_generic_free_memory), 412 DEVMETHOD(agp_bind_memory, agp_generic_bind_memory), 413 DEVMETHOD(agp_unbind_memory, agp_generic_unbind_memory), 414 415 { 0, 0 } 416}; 417 418static driver_t agp_via_driver = { 419 "agp", 420 agp_via_methods, 421 sizeof(struct agp_via_softc), 422}; 423 424static devclass_t agp_devclass; 425 426DRIVER_MODULE(agp_via, hostb, agp_via_driver, agp_devclass, 0, 0); 427MODULE_DEPEND(agp_via, agp, 1, 1, 1); 428MODULE_DEPEND(agp_via, pci, 1, 1, 1);
|