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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and
32 * drm(4) should attach as children of this device.
34 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
38 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
46 #if defined(__amd64__) || defined(__i386__)
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
54 #include <compat/x86bios/x86bios.h> /* To re-POST the card. */
57 struct resource *vr_res;
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;
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, rman_res_t start, rman_res_t end, rman_res_t count,
73 static int vga_pci_release_resource(device_t dev, device_t child, int type,
74 int rid, struct resource *r);
76 int vga_pci_default_unit = -1;
77 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
78 &vga_pci_default_unit, -1, "Default VGA-compatible display");
81 vga_pci_is_boot_display(device_t dev)
87 /* Check that the given device is a video card */
88 if ((pci_get_class(dev) != PCIC_DISPLAY &&
89 (pci_get_class(dev) != PCIC_OLD ||
90 pci_get_subclass(dev) != PCIS_OLD_VGA)))
93 unit = device_get_unit(dev);
95 if (vga_pci_default_unit >= 0) {
97 * The boot display device was determined by a previous
98 * call to this function, or the user forced it using
99 * the hw.pci.default_vgapci_unit tunable.
101 return (vga_pci_default_unit == unit);
105 * The primary video card used as a boot display must have the
106 * "I/O" and "Memory Address Space Decoding" bits set in its
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.
114 pcib = device_get_parent(device_get_parent(dev));
115 if (device_get_devclass(device_get_parent(pcib)) ==
116 devclass_find("pci")) {
118 * The parent bridge is a PCI-to-PCI bridge: check the
119 * value of the "VGA Enable" bit.
121 config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
122 if ((config & PCIB_BCR_VGA_ENABLE) == 0)
126 config = pci_read_config(dev, PCIR_COMMAND, 2);
127 if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
131 * Disable interrupts until a chipset driver is loaded for
132 * this PCI device. Else unhandled display adapter interrupts
133 * might freeze the CPU.
135 pci_write_config(dev, PCIR_COMMAND, config | PCIM_CMD_INTxDIS, 2);
137 /* This video card is the boot display: record its unit number. */
138 vga_pci_default_unit = unit;
139 device_set_flags(dev, 1);
145 vga_pci_map_bios(device_t dev, size_t *size)
148 struct resource *res;
150 #if defined(__amd64__) || defined(__i386__)
151 if (vga_pci_is_boot_display(dev)) {
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.
157 * We use this copy for the default boot device, because
158 * the original ROM may not be valid after boot.
161 *size = VGA_PCI_BIOS_SHADOW_SIZE;
162 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
167 res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0,
173 *size = rman_get_size(res);
174 return (rman_get_virtual(res));
178 vga_pci_unmap_bios(device_t dev, void *bios)
180 struct vga_resource *vr;
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);
196 * Look up the PCIR_BIOS resource in our softc. It should match
197 * the address we returned previously.
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,
208 vga_pci_repost(device_t dev)
210 #if defined(__amd64__) || defined(__i386__)
213 if (!vga_pci_is_boot_display(dev))
216 if (x86bios_get_orm(VGA_PCI_BIOS_SHADOW_ADDR) == NULL)
219 x86bios_init_regs(®s);
221 regs.R_AH = pci_get_bus(dev);
222 regs.R_AL = (pci_get_slot(dev) << 3) | (pci_get_function(dev) & 0x07);
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));
229 x86bios_get_intr(0x10);
238 vga_pci_probe(device_t dev)
241 switch (pci_get_class(dev)) {
245 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
252 /* Probe default display. */
253 vga_pci_is_boot_display(dev);
255 device_set_desc(dev, "VGA-compatible display");
256 return (BUS_PROBE_GENERIC);
260 vga_pci_attach(device_t dev)
263 bus_generic_probe(dev);
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);
270 if (vga_pci_is_boot_display(dev))
271 device_printf(dev, "Boot video device\n");
277 vga_pci_suspend(device_t dev)
280 return (bus_generic_suspend(dev));
284 vga_pci_resume(device_t dev)
287 return (bus_generic_resume(dev));
293 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
296 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
300 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
307 vga_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,
311 return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
312 filter, intr, arg, cookiep));
316 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
319 return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
322 static struct vga_resource *
323 lookup_res(struct vga_pci_softc *sc, int rid)
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]);
335 static struct resource *
336 vga_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)
339 struct vga_resource *vr;
345 * For BARs, we cache the resource so that we only allocate it
346 * from the PCI bus once.
348 vr = lookup_res(device_get_softc(dev), *rid);
351 if (vr->vr_res == NULL)
352 vr->vr_res = bus_alloc_resource(dev, type, rid, start,
354 if (vr->vr_res != NULL)
358 return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
362 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
365 struct vga_resource *vr;
372 * For BARs, we release the resource from the PCI bus
373 * when the last child reference goes away.
375 vr = lookup_res(device_get_softc(dev), rid);
378 if (vr->vr_res == NULL)
380 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
381 if (vr->vr_refs > 1) {
385 KASSERT(vr->vr_refs > 0,
386 ("vga_pci resource reference count underflow"));
387 error = bus_release_resource(dev, type, rid, r);
395 return (bus_release_resource(dev, type, rid, r));
401 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
404 return (pci_read_config(dev, reg, width));
408 vga_pci_write_config(device_t dev, device_t child, int reg,
409 uint32_t val, int width)
412 pci_write_config(dev, reg, val, width);
416 vga_pci_enable_busmaster(device_t dev, device_t child)
419 return (pci_enable_busmaster(dev));
423 vga_pci_disable_busmaster(device_t dev, device_t child)
426 return (pci_disable_busmaster(dev));
430 vga_pci_enable_io(device_t dev, device_t child, int space)
433 device_printf(dev, "child %s requested pci_enable_io\n",
434 device_get_nameunit(child));
435 return (pci_enable_io(dev, space));
439 vga_pci_disable_io(device_t dev, device_t child, int space)
442 device_printf(dev, "child %s requested pci_disable_io\n",
443 device_get_nameunit(child));
444 return (pci_disable_io(dev, space));
448 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
451 return (pci_get_vpd_ident(dev, identptr));
455 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
459 return (pci_get_vpd_readonly(dev, kw, vptr));
463 vga_pci_set_powerstate(device_t dev, device_t child, int state)
466 device_printf(dev, "child %s requested pci_set_powerstate\n",
467 device_get_nameunit(child));
468 return (pci_set_powerstate(dev, state));
472 vga_pci_get_powerstate(device_t dev, device_t child)
475 device_printf(dev, "child %s requested pci_get_powerstate\n",
476 device_get_nameunit(child));
477 return (pci_get_powerstate(dev));
481 vga_pci_assign_interrupt(device_t dev, device_t child)
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));
490 vga_pci_find_cap(device_t dev, device_t child, int capability,
494 return (pci_find_cap(dev, capability, capreg));
498 vga_pci_find_extcap(device_t dev, device_t child, int capability,
502 return (pci_find_extcap(dev, capability, capreg));
506 vga_pci_find_htcap(device_t dev, device_t child, int capability,
510 return (pci_find_htcap(dev, capability, capreg));
514 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
516 struct vga_pci_softc *sc;
519 sc = device_get_softc(dev);
520 if (sc->vga_msi_child != NULL)
522 error = pci_alloc_msi(dev, count);
524 sc->vga_msi_child = child;
529 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
531 struct vga_pci_softc *sc;
534 sc = device_get_softc(dev);
535 if (sc->vga_msi_child != NULL)
537 error = pci_alloc_msix(dev, count);
539 sc->vga_msi_child = child;
544 vga_pci_remap_msix(device_t dev, device_t child, int count,
545 const u_int *vectors)
547 struct vga_pci_softc *sc;
549 sc = device_get_softc(dev);
550 if (sc->vga_msi_child != child)
552 return (pci_remap_msix(dev, count, vectors));
556 vga_pci_release_msi(device_t dev, device_t child)
558 struct vga_pci_softc *sc;
561 sc = device_get_softc(dev);
562 if (sc->vga_msi_child != child)
564 error = pci_release_msi(dev);
566 sc->vga_msi_child = NULL;
571 vga_pci_msi_count(device_t dev, device_t child)
574 return (pci_msi_count(dev));
578 vga_pci_msix_count(device_t dev, device_t child)
581 return (pci_msix_count(dev));
585 vga_pci_get_dma_tag(device_t bus, device_t child)
588 return (bus_get_dma_tag(bus));
591 static 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),
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),
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),
635 static driver_t vga_pci_driver = {
638 sizeof(struct vga_pci_softc),
641 static devclass_t vga_devclass;
643 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
644 MODULE_DEPEND(vgapci, x86bios, 1, 1, 1);