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