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