1 /*- 2 * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org> 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 * 3. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and 35 * drm(4) should attach as children of this device. 36 * 37 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver 38 * in or rename it. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/bus.h> 43 #include <sys/kernel.h> 44 #include <sys/module.h> 45 #include <sys/rman.h> 46 #include <sys/sysctl.h> 47 #include <sys/systm.h> 48 49 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__) 50 #include <vm/vm.h> 51 #include <vm/pmap.h> 52 #endif 53 54 #include <dev/pci/pcireg.h> 55 #include <dev/pci/pcivar.h> 56 57 struct vga_resource { 58 struct resource *vr_res; 59 int vr_refs; 60 }; 61 62 struct vga_pci_softc { 63 device_t vga_msi_child; /* Child driver using MSI. */ 64 struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1]; 65 struct vga_resource vga_bios; 66 }; 67 68 SYSCTL_DECL(_hw_pci); 69 70 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid); 71 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child, 72 int type, int *rid, u_long start, u_long end, u_long count, u_int flags); 73 static int vga_pci_release_resource(device_t dev, device_t child, int type, 74 int rid, struct resource *r); 75 76 int vga_pci_default_unit = -1; 77 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit); 78 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN, 79 &vga_pci_default_unit, -1, "Default VGA-compatible display"); 80 81 int 82 vga_pci_is_boot_display(device_t dev) 83 { 84 int unit; 85 device_t pcib; 86 uint16_t config; 87 88 /* Check that the given device is a video card */ 89 if ((pci_get_class(dev) != PCIC_DISPLAY && 90 (pci_get_class(dev) != PCIC_OLD || 91 pci_get_subclass(dev) != PCIS_OLD_VGA))) 92 return (0); 93 94 unit = device_get_unit(dev); 95 96 if (vga_pci_default_unit >= 0) { 97 /* 98 * The boot display device was determined by a previous 99 * call to this function, or the user forced it using 100 * the hw.pci.default_vgapci_unit tunable. 101 */ 102 return (vga_pci_default_unit == unit); 103 } 104 105 /* 106 * The primary video card used as a boot display must have the 107 * "I/O" and "Memory Address Space Decoding" bits set in its 108 * Command register. 109 * 110 * Furthermore, if the card is attached to a bridge, instead of 111 * the root PCI bus, the bridge must have the "VGA Enable" bit 112 * set in its Control register. 113 */ 114 115 pcib = device_get_parent(device_get_parent(dev)); 116 if (device_get_devclass(device_get_parent(pcib)) == 117 devclass_find("pci")) { 118 /* 119 * The parent bridge is a PCI-to-PCI bridge: check the 120 * value of the "VGA Enable" bit. 121 */ 122 config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2); 123 if ((config & PCIB_BCR_VGA_ENABLE) == 0) 124 return (0); 125 } 126 127 config = pci_read_config(dev, PCIR_COMMAND, 2); 128 if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0) 129 return (0); 130 131 /* This video card is the boot display: record its unit number. */ 132 vga_pci_default_unit = unit; 133 device_set_flags(dev, 1); 134 135 return (1); 136 } 137 138 void * 139 vga_pci_map_bios(device_t dev, size_t *size) 140 { 141 int rid; 142 struct resource *res; 143 144 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__) 145 if (vga_pci_is_boot_display(dev)) { 146 /* 147 * On x86, the System BIOS copy the default display 148 * device's Video BIOS at a fixed location in system 149 * memory (0xC0000, 128 kBytes long) at boot time. 150 * 151 * We use this copy for the default boot device, because 152 * the original ROM may not be valid after boot. 153 */ 154 155 *size = VGA_PCI_BIOS_SHADOW_SIZE; 156 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size)); 157 } 158 #endif 159 160 rid = PCIR_BIOS; 161 res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul, 162 ~0ul, 1, RF_ACTIVE); 163 if (res == NULL) { 164 return (NULL); 165 } 166 167 *size = rman_get_size(res); 168 return (rman_get_virtual(res)); 169 } 170 171 void 172 vga_pci_unmap_bios(device_t dev, void *bios) 173 { 174 struct vga_resource *vr; 175 176 if (bios == NULL) { 177 return; 178 } 179 180 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__) 181 if (vga_pci_is_boot_display(dev)) { 182 /* We mapped the BIOS shadow copy located at 0xC0000. */ 183 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE); 184 185 return; 186 } 187 #endif 188 189 /* 190 * Look up the PCIR_BIOS resource in our softc. It should match 191 * the address we returned previously. 192 */ 193 vr = lookup_res(device_get_softc(dev), PCIR_BIOS); 194 KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped")); 195 KASSERT(rman_get_virtual(vr->vr_res) == bios, 196 ("vga_pci_unmap_bios: mismatch")); 197 vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS, 198 vr->vr_res); 199 } 200 201 static int 202 vga_pci_probe(device_t dev) 203 { 204 205 switch (pci_get_class(dev)) { 206 case PCIC_DISPLAY: 207 break; 208 case PCIC_OLD: 209 if (pci_get_subclass(dev) != PCIS_OLD_VGA) 210 return (ENXIO); 211 break; 212 default: 213 return (ENXIO); 214 } 215 216 /* Probe default display. */ 217 vga_pci_is_boot_display(dev); 218 219 device_set_desc(dev, "VGA-compatible display"); 220 return (BUS_PROBE_GENERIC); 221 } 222 223 static int 224 vga_pci_attach(device_t dev) 225 { 226 227 bus_generic_probe(dev); 228 229 /* Always create a drm child for now to make it easier on drm. */ 230 device_add_child(dev, "drm", -1); 231 device_add_child(dev, "drmn", -1); 232 bus_generic_attach(dev); 233 234 if (vga_pci_is_boot_display(dev)) 235 device_printf(dev, "Boot video device\n"); 236 237 return (0); 238 } 239 240 static int 241 vga_pci_suspend(device_t dev) 242 { 243 244 return (bus_generic_suspend(dev)); 245 } 246 247 static int 248 vga_pci_resume(device_t dev) 249 { 250 251 return (bus_generic_resume(dev)); 252 } 253 254 /* Bus interface. */ 255 256 static int 257 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 258 { 259 260 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result)); 261 } 262 263 static int 264 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 265 { 266 267 return (EINVAL); 268 } 269 270 static int 271 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 272 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 273 void **cookiep) 274 { 275 return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags, 276 filter, intr, arg, cookiep)); 277 } 278 279 static int 280 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 281 void *cookie) 282 { 283 return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie)); 284 } 285 286 static struct vga_resource * 287 lookup_res(struct vga_pci_softc *sc, int rid) 288 { 289 int bar; 290 291 if (rid == PCIR_BIOS) 292 return (&sc->vga_bios); 293 bar = PCI_RID2BAR(rid); 294 if (bar >= 0 && bar <= PCIR_MAX_BAR_0) 295 return (&sc->vga_bars[bar]); 296 return (NULL); 297 } 298 299 static struct resource * 300 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 301 u_long start, u_long end, u_long count, u_int flags) 302 { 303 struct vga_resource *vr; 304 305 switch (type) { 306 case SYS_RES_MEMORY: 307 case SYS_RES_IOPORT: 308 /* 309 * For BARs, we cache the resource so that we only allocate it 310 * from the PCI bus once. 311 */ 312 vr = lookup_res(device_get_softc(dev), *rid); 313 if (vr == NULL) 314 return (NULL); 315 if (vr->vr_res == NULL) 316 vr->vr_res = bus_alloc_resource(dev, type, rid, start, 317 end, count, flags); 318 if (vr->vr_res != NULL) 319 vr->vr_refs++; 320 return (vr->vr_res); 321 } 322 return (bus_alloc_resource(dev, type, rid, start, end, count, flags)); 323 } 324 325 static int 326 vga_pci_release_resource(device_t dev, device_t child, int type, int rid, 327 struct resource *r) 328 { 329 struct vga_resource *vr; 330 int error; 331 332 switch (type) { 333 case SYS_RES_MEMORY: 334 case SYS_RES_IOPORT: 335 /* 336 * For BARs, we release the resource from the PCI bus 337 * when the last child reference goes away. 338 */ 339 vr = lookup_res(device_get_softc(dev), rid); 340 if (vr == NULL) 341 return (EINVAL); 342 if (vr->vr_res == NULL) 343 return (EINVAL); 344 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch")); 345 if (vr->vr_refs > 1) { 346 vr->vr_refs--; 347 return (0); 348 } 349 KASSERT(vr->vr_refs > 0, 350 ("vga_pci resource reference count underflow")); 351 error = bus_release_resource(dev, type, rid, r); 352 if (error == 0) { 353 vr->vr_res = NULL; 354 vr->vr_refs = 0; 355 } 356 return (error); 357 } 358 359 return (bus_release_resource(dev, type, rid, r)); 360 } 361 362 /* PCI interface. */ 363 364 static uint32_t 365 vga_pci_read_config(device_t dev, device_t child, int reg, int width) 366 { 367 368 return (pci_read_config(dev, reg, width)); 369 } 370 371 static void 372 vga_pci_write_config(device_t dev, device_t child, int reg, 373 uint32_t val, int width) 374 { 375 376 pci_write_config(dev, reg, val, width); 377 } 378 379 static int 380 vga_pci_enable_busmaster(device_t dev, device_t child) 381 { 382 383 return (pci_enable_busmaster(dev)); 384 } 385 386 static int 387 vga_pci_disable_busmaster(device_t dev, device_t child) 388 { 389 390 return (pci_disable_busmaster(dev)); 391 } 392 393 static int 394 vga_pci_enable_io(device_t dev, device_t child, int space) 395 { 396 397 device_printf(dev, "child %s requested pci_enable_io\n", 398 device_get_nameunit(child)); 399 return (pci_enable_io(dev, space)); 400 } 401 402 static int 403 vga_pci_disable_io(device_t dev, device_t child, int space) 404 { 405 406 device_printf(dev, "child %s requested pci_disable_io\n", 407 device_get_nameunit(child)); 408 return (pci_disable_io(dev, space)); 409 } 410 411 static int 412 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr) 413 { 414 415 return (pci_get_vpd_ident(dev, identptr)); 416 } 417 418 static int 419 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw, 420 const char **vptr) 421 { 422 423 return (pci_get_vpd_readonly(dev, kw, vptr)); 424 } 425 426 static int 427 vga_pci_set_powerstate(device_t dev, device_t child, int state) 428 { 429 430 device_printf(dev, "child %s requested pci_set_powerstate\n", 431 device_get_nameunit(child)); 432 return (pci_set_powerstate(dev, state)); 433 } 434 435 static int 436 vga_pci_get_powerstate(device_t dev, device_t child) 437 { 438 439 device_printf(dev, "child %s requested pci_get_powerstate\n", 440 device_get_nameunit(child)); 441 return (pci_get_powerstate(dev)); 442 } 443 444 static int 445 vga_pci_assign_interrupt(device_t dev, device_t child) 446 { 447 448 device_printf(dev, "child %s requested pci_assign_interrupt\n", 449 device_get_nameunit(child)); 450 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev)); 451 } 452 453 static int 454 vga_pci_find_cap(device_t dev, device_t child, int capability, 455 int *capreg) 456 { 457 458 return (pci_find_cap(dev, capability, capreg)); 459 } 460 461 static int 462 vga_pci_find_extcap(device_t dev, device_t child, int capability, 463 int *capreg) 464 { 465 466 return (pci_find_extcap(dev, capability, capreg)); 467 } 468 469 static int 470 vga_pci_find_htcap(device_t dev, device_t child, int capability, 471 int *capreg) 472 { 473 474 return (pci_find_htcap(dev, capability, capreg)); 475 } 476 477 static int 478 vga_pci_alloc_msi(device_t dev, device_t child, int *count) 479 { 480 struct vga_pci_softc *sc; 481 int error; 482 483 sc = device_get_softc(dev); 484 if (sc->vga_msi_child != NULL) 485 return (EBUSY); 486 error = pci_alloc_msi(dev, count); 487 if (error == 0) 488 sc->vga_msi_child = child; 489 return (error); 490 } 491 492 static int 493 vga_pci_alloc_msix(device_t dev, device_t child, int *count) 494 { 495 struct vga_pci_softc *sc; 496 int error; 497 498 sc = device_get_softc(dev); 499 if (sc->vga_msi_child != NULL) 500 return (EBUSY); 501 error = pci_alloc_msix(dev, count); 502 if (error == 0) 503 sc->vga_msi_child = child; 504 return (error); 505 } 506 507 static int 508 vga_pci_remap_msix(device_t dev, device_t child, int count, 509 const u_int *vectors) 510 { 511 struct vga_pci_softc *sc; 512 513 sc = device_get_softc(dev); 514 if (sc->vga_msi_child != child) 515 return (ENXIO); 516 return (pci_remap_msix(dev, count, vectors)); 517 } 518 519 static int 520 vga_pci_release_msi(device_t dev, device_t child) 521 { 522 struct vga_pci_softc *sc; 523 int error; 524 525 sc = device_get_softc(dev); 526 if (sc->vga_msi_child != child) 527 return (ENXIO); 528 error = pci_release_msi(dev); 529 if (error == 0) 530 sc->vga_msi_child = NULL; 531 return (error); 532 } 533 534 static int 535 vga_pci_msi_count(device_t dev, device_t child) 536 { 537 538 return (pci_msi_count(dev)); 539 } 540 541 static int 542 vga_pci_msix_count(device_t dev, device_t child) 543 { 544 545 return (pci_msix_count(dev)); 546 } 547 548 static bus_dma_tag_t 549 vga_pci_get_dma_tag(device_t bus, device_t child) 550 { 551 552 return (bus_get_dma_tag(bus)); 553 } 554 555 static device_method_t vga_pci_methods[] = { 556 /* Device interface */ 557 DEVMETHOD(device_probe, vga_pci_probe), 558 DEVMETHOD(device_attach, vga_pci_attach), 559 DEVMETHOD(device_shutdown, bus_generic_shutdown), 560 DEVMETHOD(device_suspend, vga_pci_suspend), 561 DEVMETHOD(device_resume, vga_pci_resume), 562 563 /* Bus interface */ 564 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar), 565 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar), 566 DEVMETHOD(bus_setup_intr, vga_pci_setup_intr), 567 DEVMETHOD(bus_teardown_intr, vga_pci_teardown_intr), 568 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource), 569 DEVMETHOD(bus_release_resource, vga_pci_release_resource), 570 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 571 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 572 DEVMETHOD(bus_get_dma_tag, vga_pci_get_dma_tag), 573 574 /* PCI interface */ 575 DEVMETHOD(pci_read_config, vga_pci_read_config), 576 DEVMETHOD(pci_write_config, vga_pci_write_config), 577 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster), 578 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster), 579 DEVMETHOD(pci_enable_io, vga_pci_enable_io), 580 DEVMETHOD(pci_disable_io, vga_pci_disable_io), 581 DEVMETHOD(pci_get_vpd_ident, vga_pci_get_vpd_ident), 582 DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly), 583 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate), 584 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate), 585 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt), 586 DEVMETHOD(pci_find_cap, vga_pci_find_cap), 587 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap), 588 DEVMETHOD(pci_find_htcap, vga_pci_find_htcap), 589 DEVMETHOD(pci_alloc_msi, vga_pci_alloc_msi), 590 DEVMETHOD(pci_alloc_msix, vga_pci_alloc_msix), 591 DEVMETHOD(pci_remap_msix, vga_pci_remap_msix), 592 DEVMETHOD(pci_release_msi, vga_pci_release_msi), 593 DEVMETHOD(pci_msi_count, vga_pci_msi_count), 594 DEVMETHOD(pci_msix_count, vga_pci_msix_count), 595 596 { 0, 0 } 597 }; 598 599 static driver_t vga_pci_driver = { 600 "vgapci", 601 vga_pci_methods, 602 sizeof(struct vga_pci_softc), 603 }; 604 605 static devclass_t vga_devclass; 606 607 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0); 608