2 * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and
35 * drm(4) should attach as children of this device.
37 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
41 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
49 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
58 struct resource *vr_res;
62 struct 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;
70 int vga_pci_default_unit = -1;
71 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
72 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
73 &vga_pci_default_unit, -1, "Default VGA-compatible display");
76 vga_pci_is_boot_display(device_t dev)
80 * Return true if the given device is the default display used
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);
92 vga_pci_map_bios(device_t dev, size_t *size)
97 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
98 if (vga_pci_is_boot_display(dev)) {
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.
104 * We use this copy for the default boot device, because
105 * the original ROM may not be valid after boot.
108 *size = VGA_PCI_BIOS_SHADOW_SIZE;
109 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
114 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
119 *size = rman_get_size(res);
120 return (rman_get_virtual(res));
124 vga_pci_unmap_bios(device_t dev, void *bios)
127 struct resource *res;
133 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
134 if (vga_pci_is_boot_display(dev)) {
135 /* We mapped the BIOS shadow copy located at 0xC0000. */
136 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
143 * FIXME: We returned only the virtual address of the resource
144 * to the caller. Now, to get the resource struct back, we
145 * allocate it again: the struct exists once in memory in
146 * device softc. Therefore, we release twice now to release the
147 * reference we just obtained to get the structure back and the
148 * caller's reference.
152 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
155 ("%s: Can't get BIOS resource back", __func__));
156 KASSERT(bios == rman_get_virtual(res),
157 ("%s: Given BIOS address doesn't match "
158 "resource virtual address", __func__));
160 bus_release_resource(dev, SYS_RES_MEMORY, rid, bios);
161 bus_release_resource(dev, SYS_RES_MEMORY, rid, bios);
165 vga_pci_probe(device_t dev)
171 switch (pci_get_class(dev)) {
175 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
182 /* Probe default display. */
183 unit = device_get_unit(dev);
184 bdev = device_get_parent(device_get_parent(dev));
185 bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2);
186 if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0)
187 vga_pci_default_unit = unit;
188 if (vga_pci_default_unit == unit)
189 device_set_flags(dev, 1);
191 device_set_desc(dev, "VGA-compatible display");
192 return (BUS_PROBE_GENERIC);
196 vga_pci_attach(device_t dev)
199 bus_generic_probe(dev);
201 /* Always create a drm child for now to make it easier on drm. */
202 device_add_child(dev, "drm", -1);
203 device_add_child(dev, "drmn", -1);
204 bus_generic_attach(dev);
209 vga_pci_suspend(device_t dev)
212 return (bus_generic_suspend(dev));
216 vga_pci_resume(device_t dev)
219 return (bus_generic_resume(dev));
225 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
228 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
232 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
239 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
240 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
243 return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
244 filter, intr, arg, cookiep));
248 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
251 return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
254 static struct vga_resource *
255 lookup_res(struct vga_pci_softc *sc, int rid)
259 if (rid == PCIR_BIOS)
260 return (&sc->vga_bios);
261 bar = PCI_RID2BAR(rid);
262 if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
263 return (&sc->vga_bars[bar]);
267 static struct resource *
268 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
269 u_long start, u_long end, u_long count, u_int flags)
271 struct vga_resource *vr;
277 * For BARs, we cache the resource so that we only allocate it
278 * from the PCI bus once.
280 vr = lookup_res(device_get_softc(dev), *rid);
283 if (vr->vr_res == NULL)
284 vr->vr_res = bus_alloc_resource(dev, type, rid, start,
286 if (vr->vr_res != NULL)
290 return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
294 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
297 struct vga_resource *vr;
304 * For BARs, we release the resource from the PCI bus
305 * when the last child reference goes away.
307 vr = lookup_res(device_get_softc(dev), rid);
310 if (vr->vr_res == NULL)
312 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
313 if (vr->vr_refs > 1) {
317 KASSERT(vr->vr_refs > 0,
318 ("vga_pci resource reference count underflow"));
319 error = bus_release_resource(dev, type, rid, r);
327 return (bus_release_resource(dev, type, rid, r));
333 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
336 return (pci_read_config(dev, reg, width));
340 vga_pci_write_config(device_t dev, device_t child, int reg,
341 uint32_t val, int width)
344 pci_write_config(dev, reg, val, width);
348 vga_pci_enable_busmaster(device_t dev, device_t child)
351 return (pci_enable_busmaster(dev));
355 vga_pci_disable_busmaster(device_t dev, device_t child)
358 return (pci_disable_busmaster(dev));
362 vga_pci_enable_io(device_t dev, device_t child, int space)
365 device_printf(dev, "child %s requested pci_enable_io\n",
366 device_get_nameunit(child));
367 return (pci_enable_io(dev, space));
371 vga_pci_disable_io(device_t dev, device_t child, int space)
374 device_printf(dev, "child %s requested pci_disable_io\n",
375 device_get_nameunit(child));
376 return (pci_disable_io(dev, space));
380 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
383 return (pci_get_vpd_ident(dev, identptr));
387 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
391 return (pci_get_vpd_readonly(dev, kw, vptr));
395 vga_pci_set_powerstate(device_t dev, device_t child, int state)
398 device_printf(dev, "child %s requested pci_set_powerstate\n",
399 device_get_nameunit(child));
400 return (pci_set_powerstate(dev, state));
404 vga_pci_get_powerstate(device_t dev, device_t child)
407 device_printf(dev, "child %s requested pci_get_powerstate\n",
408 device_get_nameunit(child));
409 return (pci_get_powerstate(dev));
413 vga_pci_assign_interrupt(device_t dev, device_t child)
416 device_printf(dev, "child %s requested pci_assign_interrupt\n",
417 device_get_nameunit(child));
418 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
422 vga_pci_find_cap(device_t dev, device_t child, int capability,
426 return (pci_find_cap(dev, capability, capreg));
430 vga_pci_find_extcap(device_t dev, device_t child, int capability,
434 return (pci_find_extcap(dev, capability, capreg));
438 vga_pci_find_htcap(device_t dev, device_t child, int capability,
442 return (pci_find_htcap(dev, capability, capreg));
446 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
448 struct vga_pci_softc *sc;
451 sc = device_get_softc(dev);
452 if (sc->vga_msi_child != NULL)
454 error = pci_alloc_msi(dev, count);
456 sc->vga_msi_child = child;
461 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
463 struct vga_pci_softc *sc;
466 sc = device_get_softc(dev);
467 if (sc->vga_msi_child != NULL)
469 error = pci_alloc_msix(dev, count);
471 sc->vga_msi_child = child;
476 vga_pci_remap_msix(device_t dev, device_t child, int count,
477 const u_int *vectors)
479 struct vga_pci_softc *sc;
481 sc = device_get_softc(dev);
482 if (sc->vga_msi_child != child)
484 return (pci_remap_msix(dev, count, vectors));
488 vga_pci_release_msi(device_t dev, device_t child)
490 struct vga_pci_softc *sc;
493 sc = device_get_softc(dev);
494 if (sc->vga_msi_child != child)
496 error = pci_release_msi(dev);
498 sc->vga_msi_child = NULL;
503 vga_pci_msi_count(device_t dev, device_t child)
506 return (pci_msi_count(dev));
510 vga_pci_msix_count(device_t dev, device_t child)
513 return (pci_msix_count(dev));
517 vga_pci_get_dma_tag(device_t bus, device_t child)
520 return (bus_get_dma_tag(bus));
523 static device_method_t vga_pci_methods[] = {
524 /* Device interface */
525 DEVMETHOD(device_probe, vga_pci_probe),
526 DEVMETHOD(device_attach, vga_pci_attach),
527 DEVMETHOD(device_shutdown, bus_generic_shutdown),
528 DEVMETHOD(device_suspend, vga_pci_suspend),
529 DEVMETHOD(device_resume, vga_pci_resume),
532 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar),
533 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar),
534 DEVMETHOD(bus_setup_intr, vga_pci_setup_intr),
535 DEVMETHOD(bus_teardown_intr, vga_pci_teardown_intr),
536 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource),
537 DEVMETHOD(bus_release_resource, vga_pci_release_resource),
538 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
539 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
540 DEVMETHOD(bus_get_dma_tag, vga_pci_get_dma_tag),
543 DEVMETHOD(pci_read_config, vga_pci_read_config),
544 DEVMETHOD(pci_write_config, vga_pci_write_config),
545 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster),
546 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
547 DEVMETHOD(pci_enable_io, vga_pci_enable_io),
548 DEVMETHOD(pci_disable_io, vga_pci_disable_io),
549 DEVMETHOD(pci_get_vpd_ident, vga_pci_get_vpd_ident),
550 DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly),
551 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate),
552 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate),
553 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt),
554 DEVMETHOD(pci_find_cap, vga_pci_find_cap),
555 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap),
556 DEVMETHOD(pci_find_htcap, vga_pci_find_htcap),
557 DEVMETHOD(pci_alloc_msi, vga_pci_alloc_msi),
558 DEVMETHOD(pci_alloc_msix, vga_pci_alloc_msix),
559 DEVMETHOD(pci_remap_msix, vga_pci_remap_msix),
560 DEVMETHOD(pci_release_msi, vga_pci_release_msi),
561 DEVMETHOD(pci_msi_count, vga_pci_msi_count),
562 DEVMETHOD(pci_msix_count, vga_pci_msix_count),
567 static driver_t vga_pci_driver = {
570 sizeof(struct vga_pci_softc),
573 static devclass_t vga_devclass;
575 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);