1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and 33 * drm(4) should attach as children of this device. 34 * 35 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver 36 * in or rename it. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/bus.h> 41 #include <sys/kernel.h> 42 #include <sys/module.h> 43 #include <sys/rman.h> 44 #include <sys/sysctl.h> 45 #include <sys/systm.h> 46 47 #if defined(__amd64__) || defined(__i386__) 48 #include <vm/vm.h> 49 #include <vm/pmap.h> 50 #endif 51 52 #include <dev/pci/pcireg.h> 53 #include <dev/pci/pcivar.h> 54 55 #include <compat/x86bios/x86bios.h> /* To re-POST the card. */ 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, rman_res_t start, rman_res_t end, rman_res_t count, 73 u_int flags); 74 static int vga_pci_release_resource(device_t dev, device_t child, int type, 75 int rid, struct resource *r); 76 77 int vga_pci_default_unit = -1; 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 /* 132 * Disable interrupts until a chipset driver is loaded for 133 * this PCI device. Else unhandled display adapter interrupts 134 * might freeze the CPU. 135 */ 136 pci_write_config(dev, PCIR_COMMAND, config | PCIM_CMD_INTxDIS, 2); 137 138 /* This video card is the boot display: record its unit number. */ 139 vga_pci_default_unit = unit; 140 device_set_flags(dev, 1); 141 142 return (1); 143 } 144 145 void * 146 vga_pci_map_bios(device_t dev, size_t *size) 147 { 148 int rid; 149 struct resource *res; 150 151 #if defined(__amd64__) || defined(__i386__) 152 if (vga_pci_is_boot_display(dev)) { 153 /* 154 * On x86, the System BIOS copy the default display 155 * device's Video BIOS at a fixed location in system 156 * memory (0xC0000, 128 kBytes long) at boot time. 157 * 158 * We use this copy for the default boot device, because 159 * the original ROM may not be valid after boot. 160 */ 161 162 *size = VGA_PCI_BIOS_SHADOW_SIZE; 163 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size)); 164 } 165 #endif 166 167 rid = PCIR_BIOS; 168 res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0, 169 ~0, 1, RF_ACTIVE); 170 if (res == NULL) { 171 return (NULL); 172 } 173 174 *size = rman_get_size(res); 175 return (rman_get_virtual(res)); 176 } 177 178 void 179 vga_pci_unmap_bios(device_t dev, void *bios) 180 { 181 struct vga_resource *vr; 182 183 if (bios == NULL) { 184 return; 185 } 186 187 #if defined(__amd64__) || defined(__i386__) 188 if (vga_pci_is_boot_display(dev)) { 189 /* We mapped the BIOS shadow copy located at 0xC0000. */ 190 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE); 191 192 return; 193 } 194 #endif 195 196 /* 197 * Look up the PCIR_BIOS resource in our softc. It should match 198 * the address we returned previously. 199 */ 200 vr = lookup_res(device_get_softc(dev), PCIR_BIOS); 201 KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped")); 202 KASSERT(rman_get_virtual(vr->vr_res) == bios, 203 ("vga_pci_unmap_bios: mismatch")); 204 vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS, 205 vr->vr_res); 206 } 207 208 int 209 vga_pci_repost(device_t dev) 210 { 211 #if defined(__amd64__) || defined(__i386__) 212 x86regs_t regs; 213 214 if (!vga_pci_is_boot_display(dev)) 215 return (EINVAL); 216 217 if (x86bios_get_orm(VGA_PCI_BIOS_SHADOW_ADDR) == NULL) 218 return (ENOTSUP); 219 220 x86bios_init_regs(®s); 221 222 regs.R_AH = pci_get_bus(dev); 223 regs.R_AL = (pci_get_slot(dev) << 3) | (pci_get_function(dev) & 0x07); 224 regs.R_DL = 0x80; 225 226 device_printf(dev, "REPOSTing\n"); 227 x86bios_call(®s, X86BIOS_PHYSTOSEG(VGA_PCI_BIOS_SHADOW_ADDR + 3), 228 X86BIOS_PHYSTOOFF(VGA_PCI_BIOS_SHADOW_ADDR + 3)); 229 230 x86bios_get_intr(0x10); 231 232 return (0); 233 #else 234 return (ENOTSUP); 235 #endif 236 } 237 238 static int 239 vga_pci_probe(device_t dev) 240 { 241 242 switch (pci_get_class(dev)) { 243 case PCIC_DISPLAY: 244 break; 245 case PCIC_OLD: 246 if (pci_get_subclass(dev) != PCIS_OLD_VGA) 247 return (ENXIO); 248 break; 249 default: 250 return (ENXIO); 251 } 252 253 /* Probe default display. */ 254 vga_pci_is_boot_display(dev); 255 256 device_set_desc(dev, "VGA-compatible display"); 257 return (BUS_PROBE_GENERIC); 258 } 259 260 static int 261 vga_pci_attach(device_t dev) 262 { 263 264 bus_generic_probe(dev); 265 266 /* Always create a drm child for now to make it easier on drm. */ 267 device_add_child(dev, "drm", -1); 268 device_add_child(dev, "drmn", -1); 269 bus_generic_attach(dev); 270 271 if (vga_pci_is_boot_display(dev)) 272 device_printf(dev, "Boot video device\n"); 273 274 return (0); 275 } 276 277 static int 278 vga_pci_suspend(device_t dev) 279 { 280 281 return (bus_generic_suspend(dev)); 282 } 283 284 static int 285 vga_pci_detach(device_t dev) 286 { 287 int error; 288 289 error = bus_generic_detach(dev); 290 if (error == 0) 291 error = device_delete_children(dev); 292 return (error); 293 } 294 295 static int 296 vga_pci_resume(device_t dev) 297 { 298 299 return (bus_generic_resume(dev)); 300 } 301 302 /* Bus interface. */ 303 304 static int 305 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 306 { 307 308 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result)); 309 } 310 311 static int 312 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 313 { 314 315 return (EINVAL); 316 } 317 318 static int 319 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 320 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 321 void **cookiep) 322 { 323 return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags, 324 filter, intr, arg, cookiep)); 325 } 326 327 static int 328 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 329 void *cookie) 330 { 331 return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie)); 332 } 333 334 static struct vga_resource * 335 lookup_res(struct vga_pci_softc *sc, int rid) 336 { 337 int bar; 338 339 if (rid == PCIR_BIOS) 340 return (&sc->vga_bios); 341 bar = PCI_RID2BAR(rid); 342 if (bar >= 0 && bar <= PCIR_MAX_BAR_0) 343 return (&sc->vga_bars[bar]); 344 return (NULL); 345 } 346 347 static struct resource * 348 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 349 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 350 { 351 struct vga_resource *vr; 352 353 switch (type) { 354 case SYS_RES_MEMORY: 355 case SYS_RES_IOPORT: 356 /* 357 * For BARs, we cache the resource so that we only allocate it 358 * from the PCI bus once. 359 */ 360 vr = lookup_res(device_get_softc(dev), *rid); 361 if (vr == NULL) 362 return (NULL); 363 if (vr->vr_res == NULL) 364 vr->vr_res = bus_alloc_resource(dev, type, rid, start, 365 end, count, flags); 366 if (vr->vr_res != NULL) 367 vr->vr_refs++; 368 return (vr->vr_res); 369 } 370 return (bus_alloc_resource(dev, type, rid, start, end, count, flags)); 371 } 372 373 static int 374 vga_pci_release_resource(device_t dev, device_t child, int type, int rid, 375 struct resource *r) 376 { 377 struct vga_resource *vr; 378 int error; 379 380 switch (type) { 381 case SYS_RES_MEMORY: 382 case SYS_RES_IOPORT: 383 /* 384 * For BARs, we release the resource from the PCI bus 385 * when the last child reference goes away. 386 */ 387 vr = lookup_res(device_get_softc(dev), rid); 388 if (vr == NULL) 389 return (EINVAL); 390 if (vr->vr_res == NULL) 391 return (EINVAL); 392 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch")); 393 if (vr->vr_refs > 1) { 394 vr->vr_refs--; 395 return (0); 396 } 397 KASSERT(vr->vr_refs > 0, 398 ("vga_pci resource reference count underflow")); 399 error = bus_release_resource(dev, type, rid, r); 400 if (error == 0) { 401 vr->vr_res = NULL; 402 vr->vr_refs = 0; 403 } 404 return (error); 405 } 406 407 return (bus_release_resource(dev, type, rid, r)); 408 } 409 410 /* PCI interface. */ 411 412 static uint32_t 413 vga_pci_read_config(device_t dev, device_t child, int reg, int width) 414 { 415 416 return (pci_read_config(dev, reg, width)); 417 } 418 419 static void 420 vga_pci_write_config(device_t dev, device_t child, int reg, 421 uint32_t val, int width) 422 { 423 424 pci_write_config(dev, reg, val, width); 425 } 426 427 static int 428 vga_pci_enable_busmaster(device_t dev, device_t child) 429 { 430 431 return (pci_enable_busmaster(dev)); 432 } 433 434 static int 435 vga_pci_disable_busmaster(device_t dev, device_t child) 436 { 437 438 return (pci_disable_busmaster(dev)); 439 } 440 441 static int 442 vga_pci_enable_io(device_t dev, device_t child, int space) 443 { 444 445 device_printf(dev, "child %s requested pci_enable_io\n", 446 device_get_nameunit(child)); 447 return (pci_enable_io(dev, space)); 448 } 449 450 static int 451 vga_pci_disable_io(device_t dev, device_t child, int space) 452 { 453 454 device_printf(dev, "child %s requested pci_disable_io\n", 455 device_get_nameunit(child)); 456 return (pci_disable_io(dev, space)); 457 } 458 459 static int 460 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr) 461 { 462 463 return (pci_get_vpd_ident(dev, identptr)); 464 } 465 466 static int 467 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw, 468 const char **vptr) 469 { 470 471 return (pci_get_vpd_readonly(dev, kw, vptr)); 472 } 473 474 static int 475 vga_pci_set_powerstate(device_t dev, device_t child, int state) 476 { 477 478 device_printf(dev, "child %s requested pci_set_powerstate\n", 479 device_get_nameunit(child)); 480 return (pci_set_powerstate(dev, state)); 481 } 482 483 static int 484 vga_pci_get_powerstate(device_t dev, device_t child) 485 { 486 487 device_printf(dev, "child %s requested pci_get_powerstate\n", 488 device_get_nameunit(child)); 489 return (pci_get_powerstate(dev)); 490 } 491 492 static int 493 vga_pci_assign_interrupt(device_t dev, device_t child) 494 { 495 496 device_printf(dev, "child %s requested pci_assign_interrupt\n", 497 device_get_nameunit(child)); 498 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev)); 499 } 500 501 static int 502 vga_pci_find_cap(device_t dev, device_t child, int capability, 503 int *capreg) 504 { 505 506 return (pci_find_cap(dev, capability, capreg)); 507 } 508 509 static int 510 vga_pci_find_next_cap(device_t dev, device_t child, int capability, 511 int start, int *capreg) 512 { 513 514 return (pci_find_next_cap(dev, capability, start, capreg)); 515 } 516 517 static int 518 vga_pci_find_extcap(device_t dev, device_t child, int capability, 519 int *capreg) 520 { 521 522 return (pci_find_extcap(dev, capability, capreg)); 523 } 524 525 static int 526 vga_pci_find_next_extcap(device_t dev, device_t child, int capability, 527 int start, int *capreg) 528 { 529 530 return (pci_find_next_extcap(dev, capability, start, capreg)); 531 } 532 533 static int 534 vga_pci_find_htcap(device_t dev, device_t child, int capability, 535 int *capreg) 536 { 537 538 return (pci_find_htcap(dev, capability, capreg)); 539 } 540 541 static int 542 vga_pci_find_next_htcap(device_t dev, device_t child, int capability, 543 int start, int *capreg) 544 { 545 546 return (pci_find_next_htcap(dev, capability, start, capreg)); 547 } 548 549 static int 550 vga_pci_alloc_msi(device_t dev, device_t child, int *count) 551 { 552 struct vga_pci_softc *sc; 553 int error; 554 555 sc = device_get_softc(dev); 556 if (sc->vga_msi_child != NULL) 557 return (EBUSY); 558 error = pci_alloc_msi(dev, count); 559 if (error == 0) 560 sc->vga_msi_child = child; 561 return (error); 562 } 563 564 static int 565 vga_pci_alloc_msix(device_t dev, device_t child, int *count) 566 { 567 struct vga_pci_softc *sc; 568 int error; 569 570 sc = device_get_softc(dev); 571 if (sc->vga_msi_child != NULL) 572 return (EBUSY); 573 error = pci_alloc_msix(dev, count); 574 if (error == 0) 575 sc->vga_msi_child = child; 576 return (error); 577 } 578 579 static int 580 vga_pci_remap_msix(device_t dev, device_t child, int count, 581 const u_int *vectors) 582 { 583 struct vga_pci_softc *sc; 584 585 sc = device_get_softc(dev); 586 if (sc->vga_msi_child != child) 587 return (ENXIO); 588 return (pci_remap_msix(dev, count, vectors)); 589 } 590 591 static int 592 vga_pci_release_msi(device_t dev, device_t child) 593 { 594 struct vga_pci_softc *sc; 595 int error; 596 597 sc = device_get_softc(dev); 598 if (sc->vga_msi_child != child) 599 return (ENXIO); 600 error = pci_release_msi(dev); 601 if (error == 0) 602 sc->vga_msi_child = NULL; 603 return (error); 604 } 605 606 static int 607 vga_pci_msi_count(device_t dev, device_t child) 608 { 609 610 return (pci_msi_count(dev)); 611 } 612 613 static int 614 vga_pci_msix_count(device_t dev, device_t child) 615 { 616 617 return (pci_msix_count(dev)); 618 } 619 620 static bus_dma_tag_t 621 vga_pci_get_dma_tag(device_t bus, device_t child) 622 { 623 624 return (bus_get_dma_tag(bus)); 625 } 626 627 static device_method_t vga_pci_methods[] = { 628 /* Device interface */ 629 DEVMETHOD(device_probe, vga_pci_probe), 630 DEVMETHOD(device_attach, vga_pci_attach), 631 DEVMETHOD(device_shutdown, bus_generic_shutdown), 632 DEVMETHOD(device_suspend, vga_pci_suspend), 633 DEVMETHOD(device_detach, vga_pci_detach), 634 DEVMETHOD(device_resume, vga_pci_resume), 635 636 /* Bus interface */ 637 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar), 638 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar), 639 DEVMETHOD(bus_setup_intr, vga_pci_setup_intr), 640 DEVMETHOD(bus_teardown_intr, vga_pci_teardown_intr), 641 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource), 642 DEVMETHOD(bus_release_resource, vga_pci_release_resource), 643 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 644 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 645 DEVMETHOD(bus_get_dma_tag, vga_pci_get_dma_tag), 646 647 /* PCI interface */ 648 DEVMETHOD(pci_read_config, vga_pci_read_config), 649 DEVMETHOD(pci_write_config, vga_pci_write_config), 650 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster), 651 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster), 652 DEVMETHOD(pci_enable_io, vga_pci_enable_io), 653 DEVMETHOD(pci_disable_io, vga_pci_disable_io), 654 DEVMETHOD(pci_get_vpd_ident, vga_pci_get_vpd_ident), 655 DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly), 656 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate), 657 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate), 658 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt), 659 DEVMETHOD(pci_find_cap, vga_pci_find_cap), 660 DEVMETHOD(pci_find_next_cap, vga_pci_find_next_cap), 661 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap), 662 DEVMETHOD(pci_find_next_extcap, vga_pci_find_next_extcap), 663 DEVMETHOD(pci_find_htcap, vga_pci_find_htcap), 664 DEVMETHOD(pci_find_next_htcap, vga_pci_find_next_htcap), 665 DEVMETHOD(pci_alloc_msi, vga_pci_alloc_msi), 666 DEVMETHOD(pci_alloc_msix, vga_pci_alloc_msix), 667 DEVMETHOD(pci_remap_msix, vga_pci_remap_msix), 668 DEVMETHOD(pci_release_msi, vga_pci_release_msi), 669 DEVMETHOD(pci_msi_count, vga_pci_msi_count), 670 DEVMETHOD(pci_msix_count, vga_pci_msix_count), 671 672 { 0, 0 } 673 }; 674 675 static driver_t vga_pci_driver = { 676 "vgapci", 677 vga_pci_methods, 678 sizeof(struct vga_pci_softc), 679 }; 680 681 static devclass_t vga_devclass; 682 683 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0); 684 MODULE_DEPEND(vgapci, x86bios, 1, 1, 1); 685