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