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