2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and
34 * drm(4) should attach as children of this device.
36 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
40 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
48 #if defined(__amd64__) || defined(__i386__)
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcivar.h>
56 #include <compat/x86bios/x86bios.h> /* To re-POST the card. */
59 struct resource *vr_res;
63 struct vga_pci_softc {
64 device_t vga_msi_child; /* Child driver using MSI. */
65 struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
66 struct vga_resource vga_bios;
71 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
72 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
73 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
75 static int vga_pci_release_resource(device_t dev, device_t child, int type,
76 int rid, struct resource *r);
78 int vga_pci_default_unit = -1;
79 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
80 &vga_pci_default_unit, -1, "Default VGA-compatible display");
83 vga_pci_is_boot_display(device_t dev)
89 /* Check that the given device is a video card */
90 if ((pci_get_class(dev) != PCIC_DISPLAY &&
91 (pci_get_class(dev) != PCIC_OLD ||
92 pci_get_subclass(dev) != PCIS_OLD_VGA)))
95 unit = device_get_unit(dev);
97 if (vga_pci_default_unit >= 0) {
99 * The boot display device was determined by a previous
100 * call to this function, or the user forced it using
101 * the hw.pci.default_vgapci_unit tunable.
103 return (vga_pci_default_unit == unit);
107 * The primary video card used as a boot display must have the
108 * "I/O" and "Memory Address Space Decoding" bits set in its
111 * Furthermore, if the card is attached to a bridge, instead of
112 * the root PCI bus, the bridge must have the "VGA Enable" bit
113 * set in its Control register.
116 pcib = device_get_parent(device_get_parent(dev));
117 if (device_get_devclass(device_get_parent(pcib)) ==
118 devclass_find("pci")) {
120 * The parent bridge is a PCI-to-PCI bridge: check the
121 * value of the "VGA Enable" bit.
123 config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
124 if ((config & PCIB_BCR_VGA_ENABLE) == 0)
128 config = pci_read_config(dev, PCIR_COMMAND, 2);
129 if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
133 * Disable interrupts until a chipset driver is loaded for
134 * this PCI device. Else unhandled display adapter interrupts
135 * might freeze the CPU.
137 pci_write_config(dev, PCIR_COMMAND, config | PCIM_CMD_INTxDIS, 2);
139 /* This video card is the boot display: record its unit number. */
140 vga_pci_default_unit = unit;
141 device_set_flags(dev, 1);
147 vga_pci_map_bios(device_t dev, size_t *size)
150 struct resource *res;
152 #if defined(__amd64__) || defined(__i386__)
153 if (vga_pci_is_boot_display(dev)) {
155 * On x86, the System BIOS copy the default display
156 * device's Video BIOS at a fixed location in system
157 * memory (0xC0000, 128 kBytes long) at boot time.
159 * We use this copy for the default boot device, because
160 * the original ROM may not be valid after boot.
163 *size = VGA_PCI_BIOS_SHADOW_SIZE;
164 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
169 res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0,
175 *size = rman_get_size(res);
176 return (rman_get_virtual(res));
180 vga_pci_unmap_bios(device_t dev, void *bios)
182 struct vga_resource *vr;
188 #if defined(__amd64__) || defined(__i386__)
189 if (vga_pci_is_boot_display(dev)) {
190 /* We mapped the BIOS shadow copy located at 0xC0000. */
191 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
198 * Look up the PCIR_BIOS resource in our softc. It should match
199 * the address we returned previously.
201 vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
202 KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
203 KASSERT(rman_get_virtual(vr->vr_res) == bios,
204 ("vga_pci_unmap_bios: mismatch"));
205 vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
210 vga_pci_repost(device_t dev)
212 #if defined(__amd64__) || defined(__i386__)
215 if (!vga_pci_is_boot_display(dev))
218 if (x86bios_get_orm(VGA_PCI_BIOS_SHADOW_ADDR) == NULL)
221 x86bios_init_regs(®s);
223 regs.R_AH = pci_get_bus(dev);
224 regs.R_AL = (pci_get_slot(dev) << 3) | (pci_get_function(dev) & 0x07);
227 device_printf(dev, "REPOSTing\n");
228 x86bios_call(®s, X86BIOS_PHYSTOSEG(VGA_PCI_BIOS_SHADOW_ADDR + 3),
229 X86BIOS_PHYSTOOFF(VGA_PCI_BIOS_SHADOW_ADDR + 3));
231 x86bios_get_intr(0x10);
240 vga_pci_probe(device_t dev)
243 switch (pci_get_class(dev)) {
247 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
254 /* Probe default display. */
255 vga_pci_is_boot_display(dev);
257 device_set_desc(dev, "VGA-compatible display");
258 return (BUS_PROBE_GENERIC);
262 vga_pci_attach(device_t dev)
265 bus_generic_probe(dev);
267 /* Always create a drm child for now to make it easier on drm. */
268 device_add_child(dev, "drm", -1);
269 device_add_child(dev, "drmn", -1);
270 bus_generic_attach(dev);
272 if (vga_pci_is_boot_display(dev))
273 device_printf(dev, "Boot video device\n");
279 vga_pci_suspend(device_t dev)
282 return (bus_generic_suspend(dev));
286 vga_pci_resume(device_t dev)
289 return (bus_generic_resume(dev));
295 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
298 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
302 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
309 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
310 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
313 return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
314 filter, intr, arg, cookiep));
318 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
321 return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
324 static struct vga_resource *
325 lookup_res(struct vga_pci_softc *sc, int rid)
329 if (rid == PCIR_BIOS)
330 return (&sc->vga_bios);
331 bar = PCI_RID2BAR(rid);
332 if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
333 return (&sc->vga_bars[bar]);
337 static struct resource *
338 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
339 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
341 struct vga_resource *vr;
347 * For BARs, we cache the resource so that we only allocate it
348 * from the PCI bus once.
350 vr = lookup_res(device_get_softc(dev), *rid);
353 if (vr->vr_res == NULL)
354 vr->vr_res = bus_alloc_resource(dev, type, rid, start,
356 if (vr->vr_res != NULL)
360 return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
364 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
367 struct vga_resource *vr;
374 * For BARs, we release the resource from the PCI bus
375 * when the last child reference goes away.
377 vr = lookup_res(device_get_softc(dev), rid);
380 if (vr->vr_res == NULL)
382 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
383 if (vr->vr_refs > 1) {
387 KASSERT(vr->vr_refs > 0,
388 ("vga_pci resource reference count underflow"));
389 error = bus_release_resource(dev, type, rid, r);
397 return (bus_release_resource(dev, type, rid, r));
403 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
406 return (pci_read_config(dev, reg, width));
410 vga_pci_write_config(device_t dev, device_t child, int reg,
411 uint32_t val, int width)
414 pci_write_config(dev, reg, val, width);
418 vga_pci_enable_busmaster(device_t dev, device_t child)
421 return (pci_enable_busmaster(dev));
425 vga_pci_disable_busmaster(device_t dev, device_t child)
428 return (pci_disable_busmaster(dev));
432 vga_pci_enable_io(device_t dev, device_t child, int space)
435 device_printf(dev, "child %s requested pci_enable_io\n",
436 device_get_nameunit(child));
437 return (pci_enable_io(dev, space));
441 vga_pci_disable_io(device_t dev, device_t child, int space)
444 device_printf(dev, "child %s requested pci_disable_io\n",
445 device_get_nameunit(child));
446 return (pci_disable_io(dev, space));
450 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
453 return (pci_get_vpd_ident(dev, identptr));
457 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
461 return (pci_get_vpd_readonly(dev, kw, vptr));
465 vga_pci_set_powerstate(device_t dev, device_t child, int state)
468 device_printf(dev, "child %s requested pci_set_powerstate\n",
469 device_get_nameunit(child));
470 return (pci_set_powerstate(dev, state));
474 vga_pci_get_powerstate(device_t dev, device_t child)
477 device_printf(dev, "child %s requested pci_get_powerstate\n",
478 device_get_nameunit(child));
479 return (pci_get_powerstate(dev));
483 vga_pci_assign_interrupt(device_t dev, device_t child)
486 device_printf(dev, "child %s requested pci_assign_interrupt\n",
487 device_get_nameunit(child));
488 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
492 vga_pci_find_cap(device_t dev, device_t child, int capability,
496 return (pci_find_cap(dev, capability, capreg));
500 vga_pci_find_extcap(device_t dev, device_t child, int capability,
504 return (pci_find_extcap(dev, capability, capreg));
508 vga_pci_find_htcap(device_t dev, device_t child, int capability,
512 return (pci_find_htcap(dev, capability, capreg));
516 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
518 struct vga_pci_softc *sc;
521 sc = device_get_softc(dev);
522 if (sc->vga_msi_child != NULL)
524 error = pci_alloc_msi(dev, count);
526 sc->vga_msi_child = child;
531 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
533 struct vga_pci_softc *sc;
536 sc = device_get_softc(dev);
537 if (sc->vga_msi_child != NULL)
539 error = pci_alloc_msix(dev, count);
541 sc->vga_msi_child = child;
546 vga_pci_remap_msix(device_t dev, device_t child, int count,
547 const u_int *vectors)
549 struct vga_pci_softc *sc;
551 sc = device_get_softc(dev);
552 if (sc->vga_msi_child != child)
554 return (pci_remap_msix(dev, count, vectors));
558 vga_pci_release_msi(device_t dev, device_t child)
560 struct vga_pci_softc *sc;
563 sc = device_get_softc(dev);
564 if (sc->vga_msi_child != child)
566 error = pci_release_msi(dev);
568 sc->vga_msi_child = NULL;
573 vga_pci_msi_count(device_t dev, device_t child)
576 return (pci_msi_count(dev));
580 vga_pci_msix_count(device_t dev, device_t child)
583 return (pci_msix_count(dev));
587 vga_pci_get_dma_tag(device_t bus, device_t child)
590 return (bus_get_dma_tag(bus));
593 static device_method_t vga_pci_methods[] = {
594 /* Device interface */
595 DEVMETHOD(device_probe, vga_pci_probe),
596 DEVMETHOD(device_attach, vga_pci_attach),
597 DEVMETHOD(device_shutdown, bus_generic_shutdown),
598 DEVMETHOD(device_suspend, vga_pci_suspend),
599 DEVMETHOD(device_resume, vga_pci_resume),
602 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar),
603 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar),
604 DEVMETHOD(bus_setup_intr, vga_pci_setup_intr),
605 DEVMETHOD(bus_teardown_intr, vga_pci_teardown_intr),
606 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource),
607 DEVMETHOD(bus_release_resource, vga_pci_release_resource),
608 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
609 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
610 DEVMETHOD(bus_get_dma_tag, vga_pci_get_dma_tag),
613 DEVMETHOD(pci_read_config, vga_pci_read_config),
614 DEVMETHOD(pci_write_config, vga_pci_write_config),
615 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster),
616 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
617 DEVMETHOD(pci_enable_io, vga_pci_enable_io),
618 DEVMETHOD(pci_disable_io, vga_pci_disable_io),
619 DEVMETHOD(pci_get_vpd_ident, vga_pci_get_vpd_ident),
620 DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly),
621 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate),
622 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate),
623 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt),
624 DEVMETHOD(pci_find_cap, vga_pci_find_cap),
625 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap),
626 DEVMETHOD(pci_find_htcap, vga_pci_find_htcap),
627 DEVMETHOD(pci_alloc_msi, vga_pci_alloc_msi),
628 DEVMETHOD(pci_alloc_msix, vga_pci_alloc_msix),
629 DEVMETHOD(pci_remap_msix, vga_pci_remap_msix),
630 DEVMETHOD(pci_release_msi, vga_pci_release_msi),
631 DEVMETHOD(pci_msi_count, vga_pci_msi_count),
632 DEVMETHOD(pci_msix_count, vga_pci_msix_count),
637 static driver_t vga_pci_driver = {
640 sizeof(struct vga_pci_softc),
643 static devclass_t vga_devclass;
645 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
646 MODULE_DEPEND(vgapci, x86bios, 1, 1, 1);