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