xref: /freebsd/sys/dev/pci/vga_pci.c (revision e7d939bda22b07be6b68ba38835c9167212fd56e)
18e8e46ccSJohn Baldwin /*-
28e8e46ccSJohn Baldwin  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
38e8e46ccSJohn Baldwin  * All rights reserved.
48e8e46ccSJohn Baldwin  *
58e8e46ccSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
68e8e46ccSJohn Baldwin  * modification, are permitted provided that the following conditions
78e8e46ccSJohn Baldwin  * are met:
88e8e46ccSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
98e8e46ccSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
108e8e46ccSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
118e8e46ccSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
128e8e46ccSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
138e8e46ccSJohn Baldwin  *
148e8e46ccSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158e8e46ccSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168e8e46ccSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178e8e46ccSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188e8e46ccSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198e8e46ccSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208e8e46ccSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218e8e46ccSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228e8e46ccSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238e8e46ccSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248e8e46ccSJohn Baldwin  * SUCH DAMAGE.
258e8e46ccSJohn Baldwin  */
268e8e46ccSJohn Baldwin 
278e8e46ccSJohn Baldwin #include <sys/cdefs.h>
288e8e46ccSJohn Baldwin __FBSDID("$FreeBSD$");
298e8e46ccSJohn Baldwin 
308e8e46ccSJohn Baldwin /*
318e8e46ccSJohn Baldwin  * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
328e8e46ccSJohn Baldwin  * drm(4) should attach as children of this device.
338e8e46ccSJohn Baldwin  *
348e8e46ccSJohn Baldwin  * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
358e8e46ccSJohn Baldwin  * in or rename it.
368e8e46ccSJohn Baldwin  */
378e8e46ccSJohn Baldwin 
388e8e46ccSJohn Baldwin #include <sys/param.h>
398e8e46ccSJohn Baldwin #include <sys/bus.h>
408e8e46ccSJohn Baldwin #include <sys/kernel.h>
418e8e46ccSJohn Baldwin #include <sys/module.h>
42e8b145c2SJohn Baldwin #include <sys/rman.h>
433219f535SJung-uk Kim #include <sys/sysctl.h>
44e8b145c2SJohn Baldwin #include <sys/systm.h>
458e8e46ccSJohn Baldwin 
46*e7d939bdSMarcel Moolenaar #if defined(__amd64__) || defined(__i386__)
47509a8ff6SJean-Sébastien Pédron #include <vm/vm.h>
48509a8ff6SJean-Sébastien Pédron #include <vm/pmap.h>
49509a8ff6SJean-Sébastien Pédron #endif
50509a8ff6SJean-Sébastien Pédron 
518e8e46ccSJohn Baldwin #include <dev/pci/pcireg.h>
528e8e46ccSJohn Baldwin #include <dev/pci/pcivar.h>
538e8e46ccSJohn Baldwin 
54e8b145c2SJohn Baldwin struct vga_resource {
55e8b145c2SJohn Baldwin 	struct resource	*vr_res;
56e8b145c2SJohn Baldwin 	int	vr_refs;
57e8b145c2SJohn Baldwin };
58e8b145c2SJohn Baldwin 
59e7e2941bSJohn Baldwin struct vga_pci_softc {
60e7e2941bSJohn Baldwin 	device_t	vga_msi_child;	/* Child driver using MSI. */
619f905dafSJohn Baldwin 	struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
629f905dafSJohn Baldwin 	struct vga_resource vga_bios;
63e7e2941bSJohn Baldwin };
64e7e2941bSJohn Baldwin 
653219f535SJung-uk Kim SYSCTL_DECL(_hw_pci);
663219f535SJung-uk Kim 
67a38326daSJean-Sébastien Pédron static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
68a38326daSJean-Sébastien Pédron static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
69a38326daSJean-Sébastien Pédron     int type, int *rid, u_long start, u_long end, u_long count, u_int flags);
70a38326daSJean-Sébastien Pédron static int	vga_pci_release_resource(device_t dev, device_t child, int type,
71a38326daSJean-Sébastien Pédron     int rid, struct resource *r);
72a38326daSJean-Sébastien Pédron 
733219f535SJung-uk Kim int vga_pci_default_unit = -1;
742259d74cSJung-uk Kim SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
753219f535SJung-uk Kim     &vga_pci_default_unit, -1, "Default VGA-compatible display");
763219f535SJung-uk Kim 
77509a8ff6SJean-Sébastien Pédron int
78509a8ff6SJean-Sébastien Pédron vga_pci_is_boot_display(device_t dev)
79509a8ff6SJean-Sébastien Pédron {
802b22d5ccSJean-Sébastien Pédron 	int unit;
812b22d5ccSJean-Sébastien Pédron 	device_t pcib;
822b22d5ccSJean-Sébastien Pédron 	uint16_t config;
832b22d5ccSJean-Sébastien Pédron 
842b22d5ccSJean-Sébastien Pédron 	/* Check that the given device is a video card */
852b22d5ccSJean-Sébastien Pédron 	if ((pci_get_class(dev) != PCIC_DISPLAY &&
862b22d5ccSJean-Sébastien Pédron 	    (pci_get_class(dev) != PCIC_OLD ||
872b22d5ccSJean-Sébastien Pédron 	     pci_get_subclass(dev) != PCIS_OLD_VGA)))
882b22d5ccSJean-Sébastien Pédron 		return (0);
892b22d5ccSJean-Sébastien Pédron 
902b22d5ccSJean-Sébastien Pédron 	unit = device_get_unit(dev);
912b22d5ccSJean-Sébastien Pédron 
922b22d5ccSJean-Sébastien Pédron 	if (vga_pci_default_unit >= 0) {
932b22d5ccSJean-Sébastien Pédron 		/*
942b22d5ccSJean-Sébastien Pédron 		 * The boot display device was determined by a previous
952b22d5ccSJean-Sébastien Pédron 		 * call to this function, or the user forced it using
962b22d5ccSJean-Sébastien Pédron 		 * the hw.pci.default_vgapci_unit tunable.
972b22d5ccSJean-Sébastien Pédron 		 */
982b22d5ccSJean-Sébastien Pédron 		return (vga_pci_default_unit == unit);
992b22d5ccSJean-Sébastien Pédron 	}
100509a8ff6SJean-Sébastien Pédron 
101509a8ff6SJean-Sébastien Pédron 	/*
1022b22d5ccSJean-Sébastien Pédron 	 * The primary video card used as a boot display must have the
1032b22d5ccSJean-Sébastien Pédron 	 * "I/O" and "Memory Address Space Decoding" bits set in its
1042b22d5ccSJean-Sébastien Pédron 	 * Command register.
1052b22d5ccSJean-Sébastien Pédron 	 *
1062b22d5ccSJean-Sébastien Pédron 	 * Furthermore, if the card is attached to a bridge, instead of
1072b22d5ccSJean-Sébastien Pédron 	 * the root PCI bus, the bridge must have the "VGA Enable" bit
1082b22d5ccSJean-Sébastien Pédron 	 * set in its Control register.
109509a8ff6SJean-Sébastien Pédron 	 */
1102b22d5ccSJean-Sébastien Pédron 
1112b22d5ccSJean-Sébastien Pédron 	pcib = device_get_parent(device_get_parent(dev));
1122b22d5ccSJean-Sébastien Pédron 	if (device_get_devclass(device_get_parent(pcib)) ==
1132b22d5ccSJean-Sébastien Pédron 	    devclass_find("pci")) {
1142b22d5ccSJean-Sébastien Pédron 		/*
1152b22d5ccSJean-Sébastien Pédron 		 * The parent bridge is a PCI-to-PCI bridge: check the
1162b22d5ccSJean-Sébastien Pédron 		 * value of the "VGA Enable" bit.
1172b22d5ccSJean-Sébastien Pédron 		 */
1182b22d5ccSJean-Sébastien Pédron 		config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
1192b22d5ccSJean-Sébastien Pédron 		if ((config & PCIB_BCR_VGA_ENABLE) == 0)
1202b22d5ccSJean-Sébastien Pédron 			return (0);
1212b22d5ccSJean-Sébastien Pédron 	}
1222b22d5ccSJean-Sébastien Pédron 
1232b22d5ccSJean-Sébastien Pédron 	config = pci_read_config(dev, PCIR_COMMAND, 2);
1242b22d5ccSJean-Sébastien Pédron 	if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
1252b22d5ccSJean-Sébastien Pédron 		return (0);
1262b22d5ccSJean-Sébastien Pédron 
1272b22d5ccSJean-Sébastien Pédron 	/* This video card is the boot display: record its unit number. */
1282b22d5ccSJean-Sébastien Pédron 	vga_pci_default_unit = unit;
1292b22d5ccSJean-Sébastien Pédron 	device_set_flags(dev, 1);
1302b22d5ccSJean-Sébastien Pédron 
1312b22d5ccSJean-Sébastien Pédron 	return (1);
132509a8ff6SJean-Sébastien Pédron }
133509a8ff6SJean-Sébastien Pédron 
134509a8ff6SJean-Sébastien Pédron void *
135509a8ff6SJean-Sébastien Pédron vga_pci_map_bios(device_t dev, size_t *size)
136509a8ff6SJean-Sébastien Pédron {
137509a8ff6SJean-Sébastien Pédron 	int rid;
138509a8ff6SJean-Sébastien Pédron 	struct resource *res;
139509a8ff6SJean-Sébastien Pédron 
140*e7d939bdSMarcel Moolenaar #if defined(__amd64__) || defined(__i386__)
141509a8ff6SJean-Sébastien Pédron 	if (vga_pci_is_boot_display(dev)) {
142509a8ff6SJean-Sébastien Pédron 		/*
143509a8ff6SJean-Sébastien Pédron 		 * On x86, the System BIOS copy the default display
144509a8ff6SJean-Sébastien Pédron 		 * device's Video BIOS at a fixed location in system
145509a8ff6SJean-Sébastien Pédron 		 * memory (0xC0000, 128 kBytes long) at boot time.
146509a8ff6SJean-Sébastien Pédron 		 *
147509a8ff6SJean-Sébastien Pédron 		 * We use this copy for the default boot device, because
148509a8ff6SJean-Sébastien Pédron 		 * the original ROM may not be valid after boot.
149509a8ff6SJean-Sébastien Pédron 		 */
150509a8ff6SJean-Sébastien Pédron 
151509a8ff6SJean-Sébastien Pédron 		*size = VGA_PCI_BIOS_SHADOW_SIZE;
152509a8ff6SJean-Sébastien Pédron 		return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
153509a8ff6SJean-Sébastien Pédron 	}
154509a8ff6SJean-Sébastien Pédron #endif
155509a8ff6SJean-Sébastien Pédron 
156509a8ff6SJean-Sébastien Pédron 	rid = PCIR_BIOS;
157a38326daSJean-Sébastien Pédron 	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
158a38326daSJean-Sébastien Pédron 	    ~0ul, 1, RF_ACTIVE);
159509a8ff6SJean-Sébastien Pédron 	if (res == NULL) {
160509a8ff6SJean-Sébastien Pédron 		return (NULL);
161509a8ff6SJean-Sébastien Pédron 	}
162509a8ff6SJean-Sébastien Pédron 
163509a8ff6SJean-Sébastien Pédron 	*size = rman_get_size(res);
164509a8ff6SJean-Sébastien Pédron 	return (rman_get_virtual(res));
165509a8ff6SJean-Sébastien Pédron }
166509a8ff6SJean-Sébastien Pédron 
167509a8ff6SJean-Sébastien Pédron void
168509a8ff6SJean-Sébastien Pédron vga_pci_unmap_bios(device_t dev, void *bios)
169509a8ff6SJean-Sébastien Pédron {
170a38326daSJean-Sébastien Pédron 	struct vga_resource *vr;
171509a8ff6SJean-Sébastien Pédron 
172509a8ff6SJean-Sébastien Pédron 	if (bios == NULL) {
173509a8ff6SJean-Sébastien Pédron 		return;
174509a8ff6SJean-Sébastien Pédron 	}
175509a8ff6SJean-Sébastien Pédron 
176*e7d939bdSMarcel Moolenaar #if defined(__amd64__) || defined(__i386__)
177509a8ff6SJean-Sébastien Pédron 	if (vga_pci_is_boot_display(dev)) {
178509a8ff6SJean-Sébastien Pédron 		/* We mapped the BIOS shadow copy located at 0xC0000. */
179509a8ff6SJean-Sébastien Pédron 		pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
180509a8ff6SJean-Sébastien Pédron 
181509a8ff6SJean-Sébastien Pédron 		return;
182509a8ff6SJean-Sébastien Pédron 	}
183509a8ff6SJean-Sébastien Pédron #endif
184509a8ff6SJean-Sébastien Pédron 
185509a8ff6SJean-Sébastien Pédron 	/*
186a38326daSJean-Sébastien Pédron 	 * Look up the PCIR_BIOS resource in our softc.  It should match
187a38326daSJean-Sébastien Pédron 	 * the address we returned previously.
188509a8ff6SJean-Sébastien Pédron 	 */
189a38326daSJean-Sébastien Pédron 	vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
190a38326daSJean-Sébastien Pédron 	KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
191a38326daSJean-Sébastien Pédron 	KASSERT(rman_get_virtual(vr->vr_res) == bios,
192a38326daSJean-Sébastien Pédron 	    ("vga_pci_unmap_bios: mismatch"));
193a38326daSJean-Sébastien Pédron 	vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
194a38326daSJean-Sébastien Pédron 	    vr->vr_res);
195509a8ff6SJean-Sébastien Pédron }
196509a8ff6SJean-Sébastien Pédron 
1978e8e46ccSJohn Baldwin static int
1988e8e46ccSJohn Baldwin vga_pci_probe(device_t dev)
1998e8e46ccSJohn Baldwin {
2008e8e46ccSJohn Baldwin 
2018e8e46ccSJohn Baldwin 	switch (pci_get_class(dev)) {
2028e8e46ccSJohn Baldwin 	case PCIC_DISPLAY:
2038e8e46ccSJohn Baldwin 		break;
2048e8e46ccSJohn Baldwin 	case PCIC_OLD:
2058e8e46ccSJohn Baldwin 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
2068e8e46ccSJohn Baldwin 			return (ENXIO);
2078e8e46ccSJohn Baldwin 		break;
2088e8e46ccSJohn Baldwin 	default:
2098e8e46ccSJohn Baldwin 		return (ENXIO);
2108e8e46ccSJohn Baldwin 	}
2113219f535SJung-uk Kim 
2123219f535SJung-uk Kim 	/* Probe default display. */
2132b22d5ccSJean-Sébastien Pédron 	vga_pci_is_boot_display(dev);
2143219f535SJung-uk Kim 
2158e8e46ccSJohn Baldwin 	device_set_desc(dev, "VGA-compatible display");
216be7ccc4bSJohn Baldwin 	return (BUS_PROBE_GENERIC);
2178e8e46ccSJohn Baldwin }
2188e8e46ccSJohn Baldwin 
2198e8e46ccSJohn Baldwin static int
2208e8e46ccSJohn Baldwin vga_pci_attach(device_t dev)
2218e8e46ccSJohn Baldwin {
2228e8e46ccSJohn Baldwin 
2238e8e46ccSJohn Baldwin 	bus_generic_probe(dev);
2248e8e46ccSJohn Baldwin 
2258e8e46ccSJohn Baldwin 	/* Always create a drm child for now to make it easier on drm. */
2268e8e46ccSJohn Baldwin 	device_add_child(dev, "drm", -1);
2273c216b73SKonstantin Belousov 	device_add_child(dev, "drmn", -1);
2288e8e46ccSJohn Baldwin 	bus_generic_attach(dev);
2292b22d5ccSJean-Sébastien Pédron 
2302b22d5ccSJean-Sébastien Pédron 	if (vga_pci_is_boot_display(dev))
2312b22d5ccSJean-Sébastien Pédron 		device_printf(dev, "Boot video device\n");
2322b22d5ccSJean-Sébastien Pédron 
2338e8e46ccSJohn Baldwin 	return (0);
2348e8e46ccSJohn Baldwin }
2358e8e46ccSJohn Baldwin 
2368e8e46ccSJohn Baldwin static int
2378e8e46ccSJohn Baldwin vga_pci_suspend(device_t dev)
2388e8e46ccSJohn Baldwin {
2392259d74cSJung-uk Kim 
240b66e2b8eSJung-uk Kim 	return (bus_generic_suspend(dev));
2418e8e46ccSJohn Baldwin }
2428e8e46ccSJohn Baldwin 
2438e8e46ccSJohn Baldwin static int
2448e8e46ccSJohn Baldwin vga_pci_resume(device_t dev)
2458e8e46ccSJohn Baldwin {
2468e8e46ccSJohn Baldwin 
2478e8e46ccSJohn Baldwin 	return (bus_generic_resume(dev));
2488e8e46ccSJohn Baldwin }
2498e8e46ccSJohn Baldwin 
2508e8e46ccSJohn Baldwin /* Bus interface. */
2518e8e46ccSJohn Baldwin 
2528e8e46ccSJohn Baldwin static int
2538e8e46ccSJohn Baldwin vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
2548e8e46ccSJohn Baldwin {
2558e8e46ccSJohn Baldwin 
2568e8e46ccSJohn Baldwin 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
2578e8e46ccSJohn Baldwin }
2588e8e46ccSJohn Baldwin 
2598e8e46ccSJohn Baldwin static int
2608e8e46ccSJohn Baldwin vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
2618e8e46ccSJohn Baldwin {
2628e8e46ccSJohn Baldwin 
2638e8e46ccSJohn Baldwin 	return (EINVAL);
2648e8e46ccSJohn Baldwin }
2658e8e46ccSJohn Baldwin 
2666be0323dSRobert Noland static int
2676be0323dSRobert Noland vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
2686be0323dSRobert Noland     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
2696be0323dSRobert Noland     void **cookiep)
2706be0323dSRobert Noland {
2716be0323dSRobert Noland 	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
2726be0323dSRobert Noland 	    filter, intr, arg, cookiep));
2736be0323dSRobert Noland }
2746be0323dSRobert Noland 
2756be0323dSRobert Noland static int
2766be0323dSRobert Noland vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
2776be0323dSRobert Noland     void *cookie)
2786be0323dSRobert Noland {
2796be0323dSRobert Noland 	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
2806be0323dSRobert Noland }
2816be0323dSRobert Noland 
2829f905dafSJohn Baldwin static struct vga_resource *
2839f905dafSJohn Baldwin lookup_res(struct vga_pci_softc *sc, int rid)
2849f905dafSJohn Baldwin {
2859f905dafSJohn Baldwin 	int bar;
2869f905dafSJohn Baldwin 
2879f905dafSJohn Baldwin 	if (rid == PCIR_BIOS)
2889f905dafSJohn Baldwin 		return (&sc->vga_bios);
2899f905dafSJohn Baldwin 	bar = PCI_RID2BAR(rid);
2909f905dafSJohn Baldwin 	if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
2919f905dafSJohn Baldwin 		return (&sc->vga_bars[bar]);
2929f905dafSJohn Baldwin 	return (NULL);
2939f905dafSJohn Baldwin }
2949f905dafSJohn Baldwin 
2958e8e46ccSJohn Baldwin static struct resource *
2968e8e46ccSJohn Baldwin vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
2978e8e46ccSJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
2988e8e46ccSJohn Baldwin {
2999f905dafSJohn Baldwin 	struct vga_resource *vr;
3008e8e46ccSJohn Baldwin 
301e8b145c2SJohn Baldwin 	switch (type) {
302e8b145c2SJohn Baldwin 	case SYS_RES_MEMORY:
303e8b145c2SJohn Baldwin 	case SYS_RES_IOPORT:
304e8b145c2SJohn Baldwin 		/*
305e8b145c2SJohn Baldwin 		 * For BARs, we cache the resource so that we only allocate it
306e8b145c2SJohn Baldwin 		 * from the PCI bus once.
307e8b145c2SJohn Baldwin 		 */
3089f905dafSJohn Baldwin 		vr = lookup_res(device_get_softc(dev), *rid);
3099f905dafSJohn Baldwin 		if (vr == NULL)
310e8b145c2SJohn Baldwin 			return (NULL);
3119f905dafSJohn Baldwin 		if (vr->vr_res == NULL)
3129f905dafSJohn Baldwin 			vr->vr_res = bus_alloc_resource(dev, type, rid, start,
3139f905dafSJohn Baldwin 			    end, count, flags);
3149f905dafSJohn Baldwin 		if (vr->vr_res != NULL)
3159f905dafSJohn Baldwin 			vr->vr_refs++;
3169f905dafSJohn Baldwin 		return (vr->vr_res);
317e8b145c2SJohn Baldwin 	}
3188e8e46ccSJohn Baldwin 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
3198e8e46ccSJohn Baldwin }
3208e8e46ccSJohn Baldwin 
3218e8e46ccSJohn Baldwin static int
3228e8e46ccSJohn Baldwin vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
3238e8e46ccSJohn Baldwin     struct resource *r)
3248e8e46ccSJohn Baldwin {
3259f905dafSJohn Baldwin 	struct vga_resource *vr;
3269f905dafSJohn Baldwin 	int error;
327e8b145c2SJohn Baldwin 
328e8b145c2SJohn Baldwin 	switch (type) {
329e8b145c2SJohn Baldwin 	case SYS_RES_MEMORY:
330e8b145c2SJohn Baldwin 	case SYS_RES_IOPORT:
331e8b145c2SJohn Baldwin 		/*
332e8b145c2SJohn Baldwin 		 * For BARs, we release the resource from the PCI bus
333e8b145c2SJohn Baldwin 		 * when the last child reference goes away.
334e8b145c2SJohn Baldwin 		 */
3359f905dafSJohn Baldwin 		vr = lookup_res(device_get_softc(dev), rid);
3369f905dafSJohn Baldwin 		if (vr == NULL)
337e8b145c2SJohn Baldwin 			return (EINVAL);
3389f905dafSJohn Baldwin 		if (vr->vr_res == NULL)
339e8b145c2SJohn Baldwin 			return (EINVAL);
3409f905dafSJohn Baldwin 		KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
3419f905dafSJohn Baldwin 		if (vr->vr_refs > 1) {
3429f905dafSJohn Baldwin 			vr->vr_refs--;
343e8b145c2SJohn Baldwin 			return (0);
344e8b145c2SJohn Baldwin 		}
3459f905dafSJohn Baldwin 		KASSERT(vr->vr_refs > 0,
346e8b145c2SJohn Baldwin 		    ("vga_pci resource reference count underflow"));
347e8b145c2SJohn Baldwin 		error = bus_release_resource(dev, type, rid, r);
348e8b145c2SJohn Baldwin 		if (error == 0) {
3499f905dafSJohn Baldwin 			vr->vr_res = NULL;
3509f905dafSJohn Baldwin 			vr->vr_refs = 0;
351e8b145c2SJohn Baldwin 		}
352e8b145c2SJohn Baldwin 		return (error);
353e8b145c2SJohn Baldwin 	}
3548e8e46ccSJohn Baldwin 
3558e8e46ccSJohn Baldwin 	return (bus_release_resource(dev, type, rid, r));
3568e8e46ccSJohn Baldwin }
3578e8e46ccSJohn Baldwin 
3588e8e46ccSJohn Baldwin /* PCI interface. */
3598e8e46ccSJohn Baldwin 
3608e8e46ccSJohn Baldwin static uint32_t
3618e8e46ccSJohn Baldwin vga_pci_read_config(device_t dev, device_t child, int reg, int width)
3628e8e46ccSJohn Baldwin {
3638e8e46ccSJohn Baldwin 
3648e8e46ccSJohn Baldwin 	return (pci_read_config(dev, reg, width));
3658e8e46ccSJohn Baldwin }
3668e8e46ccSJohn Baldwin 
3678e8e46ccSJohn Baldwin static void
3688e8e46ccSJohn Baldwin vga_pci_write_config(device_t dev, device_t child, int reg,
3698e8e46ccSJohn Baldwin     uint32_t val, int width)
3708e8e46ccSJohn Baldwin {
3718e8e46ccSJohn Baldwin 
3728e8e46ccSJohn Baldwin 	pci_write_config(dev, reg, val, width);
3738e8e46ccSJohn Baldwin }
3748e8e46ccSJohn Baldwin 
3758e8e46ccSJohn Baldwin static int
3768e8e46ccSJohn Baldwin vga_pci_enable_busmaster(device_t dev, device_t child)
3778e8e46ccSJohn Baldwin {
3788e8e46ccSJohn Baldwin 
3798e8e46ccSJohn Baldwin 	return (pci_enable_busmaster(dev));
3808e8e46ccSJohn Baldwin }
3818e8e46ccSJohn Baldwin 
3828e8e46ccSJohn Baldwin static int
3838e8e46ccSJohn Baldwin vga_pci_disable_busmaster(device_t dev, device_t child)
3848e8e46ccSJohn Baldwin {
3858e8e46ccSJohn Baldwin 
3868e8e46ccSJohn Baldwin 	return (pci_disable_busmaster(dev));
3878e8e46ccSJohn Baldwin }
3888e8e46ccSJohn Baldwin 
3898e8e46ccSJohn Baldwin static int
3908e8e46ccSJohn Baldwin vga_pci_enable_io(device_t dev, device_t child, int space)
3918e8e46ccSJohn Baldwin {
3928e8e46ccSJohn Baldwin 
3938e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_enable_io\n",
3948e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
3958e8e46ccSJohn Baldwin 	return (pci_enable_io(dev, space));
3968e8e46ccSJohn Baldwin }
3978e8e46ccSJohn Baldwin 
3988e8e46ccSJohn Baldwin static int
3998e8e46ccSJohn Baldwin vga_pci_disable_io(device_t dev, device_t child, int space)
4008e8e46ccSJohn Baldwin {
4018e8e46ccSJohn Baldwin 
4028e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_disable_io\n",
4038e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
4048e8e46ccSJohn Baldwin 	return (pci_disable_io(dev, space));
4058e8e46ccSJohn Baldwin }
4068e8e46ccSJohn Baldwin 
4078e8e46ccSJohn Baldwin static int
408e7e2941bSJohn Baldwin vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
409e7e2941bSJohn Baldwin {
410e7e2941bSJohn Baldwin 
411e7e2941bSJohn Baldwin 	return (pci_get_vpd_ident(dev, identptr));
412e7e2941bSJohn Baldwin }
413e7e2941bSJohn Baldwin 
414e7e2941bSJohn Baldwin static int
415e7e2941bSJohn Baldwin vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
416e7e2941bSJohn Baldwin     const char **vptr)
417e7e2941bSJohn Baldwin {
418e7e2941bSJohn Baldwin 
419e7e2941bSJohn Baldwin 	return (pci_get_vpd_readonly(dev, kw, vptr));
420e7e2941bSJohn Baldwin }
421e7e2941bSJohn Baldwin 
422e7e2941bSJohn Baldwin static int
4238e8e46ccSJohn Baldwin vga_pci_set_powerstate(device_t dev, device_t child, int state)
4248e8e46ccSJohn Baldwin {
4258e8e46ccSJohn Baldwin 
4268e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_set_powerstate\n",
4278e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
4288e8e46ccSJohn Baldwin 	return (pci_set_powerstate(dev, state));
4298e8e46ccSJohn Baldwin }
4308e8e46ccSJohn Baldwin 
4318e8e46ccSJohn Baldwin static int
4328e8e46ccSJohn Baldwin vga_pci_get_powerstate(device_t dev, device_t child)
4338e8e46ccSJohn Baldwin {
4348e8e46ccSJohn Baldwin 
4358e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_get_powerstate\n",
4368e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
4378e8e46ccSJohn Baldwin 	return (pci_get_powerstate(dev));
4388e8e46ccSJohn Baldwin }
4398e8e46ccSJohn Baldwin 
4408e8e46ccSJohn Baldwin static int
4418e8e46ccSJohn Baldwin vga_pci_assign_interrupt(device_t dev, device_t child)
4428e8e46ccSJohn Baldwin {
4438e8e46ccSJohn Baldwin 
4448e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
4458e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
4468e8e46ccSJohn Baldwin 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
4478e8e46ccSJohn Baldwin }
4488e8e46ccSJohn Baldwin 
4498e8e46ccSJohn Baldwin static int
450c668000bSJohn Baldwin vga_pci_find_cap(device_t dev, device_t child, int capability,
451c668000bSJohn Baldwin     int *capreg)
452c668000bSJohn Baldwin {
453c668000bSJohn Baldwin 
454c668000bSJohn Baldwin 	return (pci_find_cap(dev, capability, capreg));
455c668000bSJohn Baldwin }
456c668000bSJohn Baldwin 
457c668000bSJohn Baldwin static int
4588e8e46ccSJohn Baldwin vga_pci_find_extcap(device_t dev, device_t child, int capability,
4598e8e46ccSJohn Baldwin     int *capreg)
4608e8e46ccSJohn Baldwin {
4618e8e46ccSJohn Baldwin 
4628e8e46ccSJohn Baldwin 	return (pci_find_extcap(dev, capability, capreg));
4638e8e46ccSJohn Baldwin }
4648e8e46ccSJohn Baldwin 
465e7e2941bSJohn Baldwin static int
466c668000bSJohn Baldwin vga_pci_find_htcap(device_t dev, device_t child, int capability,
467c668000bSJohn Baldwin     int *capreg)
468c668000bSJohn Baldwin {
469c668000bSJohn Baldwin 
470c668000bSJohn Baldwin 	return (pci_find_htcap(dev, capability, capreg));
471c668000bSJohn Baldwin }
472c668000bSJohn Baldwin 
473c668000bSJohn Baldwin static int
474e7e2941bSJohn Baldwin vga_pci_alloc_msi(device_t dev, device_t child, int *count)
475e7e2941bSJohn Baldwin {
476e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
477e7e2941bSJohn Baldwin 	int error;
478e7e2941bSJohn Baldwin 
479e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
480e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != NULL)
481e7e2941bSJohn Baldwin 		return (EBUSY);
482e7e2941bSJohn Baldwin 	error = pci_alloc_msi(dev, count);
483e7e2941bSJohn Baldwin 	if (error == 0)
484e7e2941bSJohn Baldwin 		sc->vga_msi_child = child;
485e7e2941bSJohn Baldwin 	return (error);
486e7e2941bSJohn Baldwin }
487e7e2941bSJohn Baldwin 
488e7e2941bSJohn Baldwin static int
489e7e2941bSJohn Baldwin vga_pci_alloc_msix(device_t dev, device_t child, int *count)
490e7e2941bSJohn Baldwin {
491e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
492e7e2941bSJohn Baldwin 	int error;
493e7e2941bSJohn Baldwin 
494e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
495e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != NULL)
496e7e2941bSJohn Baldwin 		return (EBUSY);
497e7e2941bSJohn Baldwin 	error = pci_alloc_msix(dev, count);
498e7e2941bSJohn Baldwin 	if (error == 0)
499e7e2941bSJohn Baldwin 		sc->vga_msi_child = child;
500e7e2941bSJohn Baldwin 	return (error);
501e7e2941bSJohn Baldwin }
502e7e2941bSJohn Baldwin 
503e7e2941bSJohn Baldwin static int
504e7e2941bSJohn Baldwin vga_pci_remap_msix(device_t dev, device_t child, int count,
505e7e2941bSJohn Baldwin     const u_int *vectors)
506e7e2941bSJohn Baldwin {
507e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
508e7e2941bSJohn Baldwin 
509e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
510e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != child)
511e7e2941bSJohn Baldwin 		return (ENXIO);
512e7e2941bSJohn Baldwin 	return (pci_remap_msix(dev, count, vectors));
513e7e2941bSJohn Baldwin }
514e7e2941bSJohn Baldwin 
515e7e2941bSJohn Baldwin static int
516e7e2941bSJohn Baldwin vga_pci_release_msi(device_t dev, device_t child)
517e7e2941bSJohn Baldwin {
518e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
519e7e2941bSJohn Baldwin 	int error;
520e7e2941bSJohn Baldwin 
521e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
522e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != child)
523e7e2941bSJohn Baldwin 		return (ENXIO);
524e7e2941bSJohn Baldwin 	error = pci_release_msi(dev);
525e7e2941bSJohn Baldwin 	if (error == 0)
526e7e2941bSJohn Baldwin 		sc->vga_msi_child = NULL;
527e7e2941bSJohn Baldwin 	return (error);
528e7e2941bSJohn Baldwin }
529e7e2941bSJohn Baldwin 
530e7e2941bSJohn Baldwin static int
531e7e2941bSJohn Baldwin vga_pci_msi_count(device_t dev, device_t child)
532e7e2941bSJohn Baldwin {
533e7e2941bSJohn Baldwin 
534e7e2941bSJohn Baldwin 	return (pci_msi_count(dev));
535e7e2941bSJohn Baldwin }
536e7e2941bSJohn Baldwin 
537e7e2941bSJohn Baldwin static int
538e7e2941bSJohn Baldwin vga_pci_msix_count(device_t dev, device_t child)
539e7e2941bSJohn Baldwin {
540e7e2941bSJohn Baldwin 
541e7e2941bSJohn Baldwin 	return (pci_msix_count(dev));
542e7e2941bSJohn Baldwin }
543e7e2941bSJohn Baldwin 
5445c818b67SKonstantin Belousov static bus_dma_tag_t
5455c818b67SKonstantin Belousov vga_pci_get_dma_tag(device_t bus, device_t child)
5465c818b67SKonstantin Belousov {
5475c818b67SKonstantin Belousov 
5485c818b67SKonstantin Belousov 	return (bus_get_dma_tag(bus));
5495c818b67SKonstantin Belousov }
5505c818b67SKonstantin Belousov 
5518e8e46ccSJohn Baldwin static device_method_t vga_pci_methods[] = {
5528e8e46ccSJohn Baldwin 	/* Device interface */
5538e8e46ccSJohn Baldwin 	DEVMETHOD(device_probe,		vga_pci_probe),
5548e8e46ccSJohn Baldwin 	DEVMETHOD(device_attach,	vga_pci_attach),
5558e8e46ccSJohn Baldwin 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
5568e8e46ccSJohn Baldwin 	DEVMETHOD(device_suspend,	vga_pci_suspend),
5578e8e46ccSJohn Baldwin 	DEVMETHOD(device_resume,	vga_pci_resume),
5588e8e46ccSJohn Baldwin 
5598e8e46ccSJohn Baldwin 	/* Bus interface */
5608e8e46ccSJohn Baldwin 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
5618e8e46ccSJohn Baldwin 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
5626be0323dSRobert Noland 	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
5636be0323dSRobert Noland 	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
5648e8e46ccSJohn Baldwin 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
5658e8e46ccSJohn Baldwin 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
5668e8e46ccSJohn Baldwin 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
5678e8e46ccSJohn Baldwin 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
5685c818b67SKonstantin Belousov 	DEVMETHOD(bus_get_dma_tag,	vga_pci_get_dma_tag),
5698e8e46ccSJohn Baldwin 
5708e8e46ccSJohn Baldwin 	/* PCI interface */
5718e8e46ccSJohn Baldwin 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
5728e8e46ccSJohn Baldwin 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
5738e8e46ccSJohn Baldwin 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
5748e8e46ccSJohn Baldwin 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
5758e8e46ccSJohn Baldwin 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
5768e8e46ccSJohn Baldwin 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
577e7e2941bSJohn Baldwin 	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
578e7e2941bSJohn Baldwin 	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
5798e8e46ccSJohn Baldwin 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
5808e8e46ccSJohn Baldwin 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
5818e8e46ccSJohn Baldwin 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
582c668000bSJohn Baldwin 	DEVMETHOD(pci_find_cap,		vga_pci_find_cap),
5838e8e46ccSJohn Baldwin 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
584c668000bSJohn Baldwin 	DEVMETHOD(pci_find_htcap,	vga_pci_find_htcap),
585e7e2941bSJohn Baldwin 	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
586e7e2941bSJohn Baldwin 	DEVMETHOD(pci_alloc_msix,	vga_pci_alloc_msix),
587e7e2941bSJohn Baldwin 	DEVMETHOD(pci_remap_msix,	vga_pci_remap_msix),
588e7e2941bSJohn Baldwin 	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
589e7e2941bSJohn Baldwin 	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
590e7e2941bSJohn Baldwin 	DEVMETHOD(pci_msix_count,	vga_pci_msix_count),
5918e8e46ccSJohn Baldwin 
5928e8e46ccSJohn Baldwin 	{ 0, 0 }
5938e8e46ccSJohn Baldwin };
5948e8e46ccSJohn Baldwin 
5958e8e46ccSJohn Baldwin static driver_t vga_pci_driver = {
5968e8e46ccSJohn Baldwin 	"vgapci",
5978e8e46ccSJohn Baldwin 	vga_pci_methods,
598e7e2941bSJohn Baldwin 	sizeof(struct vga_pci_softc),
5998e8e46ccSJohn Baldwin };
6008e8e46ccSJohn Baldwin 
6018e8e46ccSJohn Baldwin static devclass_t vga_devclass;
6028e8e46ccSJohn Baldwin 
6038e8e46ccSJohn Baldwin DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
604