xref: /freebsd/sys/dev/pci/vga_pci.c (revision a38326da8079a160caad57acca615f33a6fa6558)
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  * 3. Neither the name of the author nor the names of any co-contributors
148e8e46ccSJohn Baldwin  *    may be used to endorse or promote products derived from this software
158e8e46ccSJohn Baldwin  *    without specific prior written permission.
168e8e46ccSJohn Baldwin  *
178e8e46ccSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188e8e46ccSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198e8e46ccSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208e8e46ccSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
218e8e46ccSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228e8e46ccSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238e8e46ccSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248e8e46ccSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258e8e46ccSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268e8e46ccSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278e8e46ccSJohn Baldwin  * SUCH DAMAGE.
288e8e46ccSJohn Baldwin  */
298e8e46ccSJohn Baldwin 
308e8e46ccSJohn Baldwin #include <sys/cdefs.h>
318e8e46ccSJohn Baldwin __FBSDID("$FreeBSD$");
328e8e46ccSJohn Baldwin 
338e8e46ccSJohn Baldwin /*
348e8e46ccSJohn Baldwin  * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
358e8e46ccSJohn Baldwin  * drm(4) should attach as children of this device.
368e8e46ccSJohn Baldwin  *
378e8e46ccSJohn Baldwin  * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
388e8e46ccSJohn Baldwin  * in or rename it.
398e8e46ccSJohn Baldwin  */
408e8e46ccSJohn Baldwin 
418e8e46ccSJohn Baldwin #include <sys/param.h>
428e8e46ccSJohn Baldwin #include <sys/bus.h>
438e8e46ccSJohn Baldwin #include <sys/kernel.h>
448e8e46ccSJohn Baldwin #include <sys/module.h>
45e8b145c2SJohn Baldwin #include <sys/rman.h>
463219f535SJung-uk Kim #include <sys/sysctl.h>
47e8b145c2SJohn Baldwin #include <sys/systm.h>
488e8e46ccSJohn Baldwin 
49509a8ff6SJean-Sébastien Pédron #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
50509a8ff6SJean-Sébastien Pédron #include <vm/vm.h>
51509a8ff6SJean-Sébastien Pédron #include <vm/pmap.h>
52509a8ff6SJean-Sébastien Pédron #endif
53509a8ff6SJean-Sébastien Pédron 
548e8e46ccSJohn Baldwin #include <dev/pci/pcireg.h>
558e8e46ccSJohn Baldwin #include <dev/pci/pcivar.h>
568e8e46ccSJohn Baldwin 
57e8b145c2SJohn Baldwin struct vga_resource {
58e8b145c2SJohn Baldwin 	struct resource	*vr_res;
59e8b145c2SJohn Baldwin 	int	vr_refs;
60e8b145c2SJohn Baldwin };
61e8b145c2SJohn Baldwin 
62e7e2941bSJohn Baldwin struct vga_pci_softc {
63e7e2941bSJohn Baldwin 	device_t	vga_msi_child;	/* Child driver using MSI. */
649f905dafSJohn Baldwin 	struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
659f905dafSJohn Baldwin 	struct vga_resource vga_bios;
66e7e2941bSJohn Baldwin };
67e7e2941bSJohn Baldwin 
683219f535SJung-uk Kim SYSCTL_DECL(_hw_pci);
693219f535SJung-uk Kim 
70*a38326daSJean-Sébastien Pédron static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
71*a38326daSJean-Sébastien Pédron static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
72*a38326daSJean-Sébastien Pédron     int type, int *rid, u_long start, u_long end, u_long count, u_int flags);
73*a38326daSJean-Sébastien Pédron static int	vga_pci_release_resource(device_t dev, device_t child, int type,
74*a38326daSJean-Sébastien Pédron     int rid, struct resource *r);
75*a38326daSJean-Sébastien Pédron 
763219f535SJung-uk Kim int vga_pci_default_unit = -1;
773219f535SJung-uk Kim TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
782259d74cSJung-uk Kim SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
793219f535SJung-uk Kim     &vga_pci_default_unit, -1, "Default VGA-compatible display");
803219f535SJung-uk Kim 
81509a8ff6SJean-Sébastien Pédron int
82509a8ff6SJean-Sébastien Pédron vga_pci_is_boot_display(device_t dev)
83509a8ff6SJean-Sébastien Pédron {
84509a8ff6SJean-Sébastien Pédron 
85509a8ff6SJean-Sébastien Pédron 	/*
86509a8ff6SJean-Sébastien Pédron 	 * Return true if the given device is the default display used
87509a8ff6SJean-Sébastien Pédron 	 * at boot time.
88509a8ff6SJean-Sébastien Pédron 	 */
89509a8ff6SJean-Sébastien Pédron 	return (
90509a8ff6SJean-Sébastien Pédron 	    (pci_get_class(dev) == PCIC_DISPLAY ||
91509a8ff6SJean-Sébastien Pédron 	     (pci_get_class(dev) == PCIC_OLD &&
92509a8ff6SJean-Sébastien Pédron 	      pci_get_subclass(dev) == PCIS_OLD_VGA)) &&
93509a8ff6SJean-Sébastien Pédron 	    device_get_unit(dev) == vga_pci_default_unit);
94509a8ff6SJean-Sébastien Pédron }
95509a8ff6SJean-Sébastien Pédron 
96509a8ff6SJean-Sébastien Pédron void *
97509a8ff6SJean-Sébastien Pédron vga_pci_map_bios(device_t dev, size_t *size)
98509a8ff6SJean-Sébastien Pédron {
99509a8ff6SJean-Sébastien Pédron 	int rid;
100509a8ff6SJean-Sébastien Pédron 	struct resource *res;
101509a8ff6SJean-Sébastien Pédron 
102509a8ff6SJean-Sébastien Pédron #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
103509a8ff6SJean-Sébastien Pédron 	if (vga_pci_is_boot_display(dev)) {
104509a8ff6SJean-Sébastien Pédron 		/*
105509a8ff6SJean-Sébastien Pédron 		 * On x86, the System BIOS copy the default display
106509a8ff6SJean-Sébastien Pédron 		 * device's Video BIOS at a fixed location in system
107509a8ff6SJean-Sébastien Pédron 		 * memory (0xC0000, 128 kBytes long) at boot time.
108509a8ff6SJean-Sébastien Pédron 		 *
109509a8ff6SJean-Sébastien Pédron 		 * We use this copy for the default boot device, because
110509a8ff6SJean-Sébastien Pédron 		 * the original ROM may not be valid after boot.
111509a8ff6SJean-Sébastien Pédron 		 */
112509a8ff6SJean-Sébastien Pédron 
113509a8ff6SJean-Sébastien Pédron 		*size = VGA_PCI_BIOS_SHADOW_SIZE;
114509a8ff6SJean-Sébastien Pédron 		return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
115509a8ff6SJean-Sébastien Pédron 	}
116509a8ff6SJean-Sébastien Pédron #endif
117509a8ff6SJean-Sébastien Pédron 
118509a8ff6SJean-Sébastien Pédron 	rid = PCIR_BIOS;
119*a38326daSJean-Sébastien Pédron 	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
120*a38326daSJean-Sébastien Pédron 	    ~0ul, 1, RF_ACTIVE);
121509a8ff6SJean-Sébastien Pédron 	if (res == NULL) {
122509a8ff6SJean-Sébastien Pédron 		return (NULL);
123509a8ff6SJean-Sébastien Pédron 	}
124509a8ff6SJean-Sébastien Pédron 
125509a8ff6SJean-Sébastien Pédron 	*size = rman_get_size(res);
126509a8ff6SJean-Sébastien Pédron 	return (rman_get_virtual(res));
127509a8ff6SJean-Sébastien Pédron }
128509a8ff6SJean-Sébastien Pédron 
129509a8ff6SJean-Sébastien Pédron void
130509a8ff6SJean-Sébastien Pédron vga_pci_unmap_bios(device_t dev, void *bios)
131509a8ff6SJean-Sébastien Pédron {
132*a38326daSJean-Sébastien Pédron 	struct vga_resource *vr;
133509a8ff6SJean-Sébastien Pédron 
134509a8ff6SJean-Sébastien Pédron 	if (bios == NULL) {
135509a8ff6SJean-Sébastien Pédron 		return;
136509a8ff6SJean-Sébastien Pédron 	}
137509a8ff6SJean-Sébastien Pédron 
138509a8ff6SJean-Sébastien Pédron #if defined(__amd64__) || defined(__i386__) || defined(__ia64__)
139509a8ff6SJean-Sébastien Pédron 	if (vga_pci_is_boot_display(dev)) {
140509a8ff6SJean-Sébastien Pédron 		/* We mapped the BIOS shadow copy located at 0xC0000. */
141509a8ff6SJean-Sébastien Pédron 		pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
142509a8ff6SJean-Sébastien Pédron 
143509a8ff6SJean-Sébastien Pédron 		return;
144509a8ff6SJean-Sébastien Pédron 	}
145509a8ff6SJean-Sébastien Pédron #endif
146509a8ff6SJean-Sébastien Pédron 
147509a8ff6SJean-Sébastien Pédron 	/*
148*a38326daSJean-Sébastien Pédron 	 * Look up the PCIR_BIOS resource in our softc.  It should match
149*a38326daSJean-Sébastien Pédron 	 * the address we returned previously.
150509a8ff6SJean-Sébastien Pédron 	 */
151*a38326daSJean-Sébastien Pédron 	vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
152*a38326daSJean-Sébastien Pédron 	KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
153*a38326daSJean-Sébastien Pédron 	KASSERT(rman_get_virtual(vr->vr_res) == bios,
154*a38326daSJean-Sébastien Pédron 	    ("vga_pci_unmap_bios: mismatch"));
155*a38326daSJean-Sébastien Pédron 	vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
156*a38326daSJean-Sébastien Pédron 	    vr->vr_res);
157509a8ff6SJean-Sébastien Pédron }
158509a8ff6SJean-Sébastien Pédron 
1598e8e46ccSJohn Baldwin static int
1608e8e46ccSJohn Baldwin vga_pci_probe(device_t dev)
1618e8e46ccSJohn Baldwin {
1623219f535SJung-uk Kim 	device_t bdev;
1633219f535SJung-uk Kim 	int unit;
1643219f535SJung-uk Kim 	uint16_t bctl;
1658e8e46ccSJohn Baldwin 
1668e8e46ccSJohn Baldwin 	switch (pci_get_class(dev)) {
1678e8e46ccSJohn Baldwin 	case PCIC_DISPLAY:
1688e8e46ccSJohn Baldwin 		break;
1698e8e46ccSJohn Baldwin 	case PCIC_OLD:
1708e8e46ccSJohn Baldwin 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
1718e8e46ccSJohn Baldwin 			return (ENXIO);
1728e8e46ccSJohn Baldwin 		break;
1738e8e46ccSJohn Baldwin 	default:
1748e8e46ccSJohn Baldwin 		return (ENXIO);
1758e8e46ccSJohn Baldwin 	}
1763219f535SJung-uk Kim 
1773219f535SJung-uk Kim 	/* Probe default display. */
1783219f535SJung-uk Kim 	unit = device_get_unit(dev);
1793219f535SJung-uk Kim 	bdev = device_get_parent(device_get_parent(dev));
1803219f535SJung-uk Kim 	bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2);
1813219f535SJung-uk Kim 	if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0)
1823219f535SJung-uk Kim 		vga_pci_default_unit = unit;
1833219f535SJung-uk Kim 	if (vga_pci_default_unit == unit)
1843219f535SJung-uk Kim 		device_set_flags(dev, 1);
1853219f535SJung-uk Kim 
1868e8e46ccSJohn Baldwin 	device_set_desc(dev, "VGA-compatible display");
187be7ccc4bSJohn Baldwin 	return (BUS_PROBE_GENERIC);
1888e8e46ccSJohn Baldwin }
1898e8e46ccSJohn Baldwin 
1908e8e46ccSJohn Baldwin static int
1918e8e46ccSJohn Baldwin vga_pci_attach(device_t dev)
1928e8e46ccSJohn Baldwin {
1938e8e46ccSJohn Baldwin 
1948e8e46ccSJohn Baldwin 	bus_generic_probe(dev);
1958e8e46ccSJohn Baldwin 
1968e8e46ccSJohn Baldwin 	/* Always create a drm child for now to make it easier on drm. */
1978e8e46ccSJohn Baldwin 	device_add_child(dev, "drm", -1);
1983c216b73SKonstantin Belousov 	device_add_child(dev, "drmn", -1);
1998e8e46ccSJohn Baldwin 	bus_generic_attach(dev);
2008e8e46ccSJohn Baldwin 	return (0);
2018e8e46ccSJohn Baldwin }
2028e8e46ccSJohn Baldwin 
2038e8e46ccSJohn Baldwin static int
2048e8e46ccSJohn Baldwin vga_pci_suspend(device_t dev)
2058e8e46ccSJohn Baldwin {
2062259d74cSJung-uk Kim 
207b66e2b8eSJung-uk Kim 	return (bus_generic_suspend(dev));
2088e8e46ccSJohn Baldwin }
2098e8e46ccSJohn Baldwin 
2108e8e46ccSJohn Baldwin static int
2118e8e46ccSJohn Baldwin vga_pci_resume(device_t dev)
2128e8e46ccSJohn Baldwin {
2138e8e46ccSJohn Baldwin 
2148e8e46ccSJohn Baldwin 	return (bus_generic_resume(dev));
2158e8e46ccSJohn Baldwin }
2168e8e46ccSJohn Baldwin 
2178e8e46ccSJohn Baldwin /* Bus interface. */
2188e8e46ccSJohn Baldwin 
2198e8e46ccSJohn Baldwin static int
2208e8e46ccSJohn Baldwin vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
2218e8e46ccSJohn Baldwin {
2228e8e46ccSJohn Baldwin 
2238e8e46ccSJohn Baldwin 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
2248e8e46ccSJohn Baldwin }
2258e8e46ccSJohn Baldwin 
2268e8e46ccSJohn Baldwin static int
2278e8e46ccSJohn Baldwin vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
2288e8e46ccSJohn Baldwin {
2298e8e46ccSJohn Baldwin 
2308e8e46ccSJohn Baldwin 	return (EINVAL);
2318e8e46ccSJohn Baldwin }
2328e8e46ccSJohn Baldwin 
2336be0323dSRobert Noland static int
2346be0323dSRobert Noland vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
2356be0323dSRobert Noland     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
2366be0323dSRobert Noland     void **cookiep)
2376be0323dSRobert Noland {
2386be0323dSRobert Noland 	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
2396be0323dSRobert Noland 	    filter, intr, arg, cookiep));
2406be0323dSRobert Noland }
2416be0323dSRobert Noland 
2426be0323dSRobert Noland static int
2436be0323dSRobert Noland vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
2446be0323dSRobert Noland     void *cookie)
2456be0323dSRobert Noland {
2466be0323dSRobert Noland 	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
2476be0323dSRobert Noland }
2486be0323dSRobert Noland 
2499f905dafSJohn Baldwin static struct vga_resource *
2509f905dafSJohn Baldwin lookup_res(struct vga_pci_softc *sc, int rid)
2519f905dafSJohn Baldwin {
2529f905dafSJohn Baldwin 	int bar;
2539f905dafSJohn Baldwin 
2549f905dafSJohn Baldwin 	if (rid == PCIR_BIOS)
2559f905dafSJohn Baldwin 		return (&sc->vga_bios);
2569f905dafSJohn Baldwin 	bar = PCI_RID2BAR(rid);
2579f905dafSJohn Baldwin 	if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
2589f905dafSJohn Baldwin 		return (&sc->vga_bars[bar]);
2599f905dafSJohn Baldwin 	return (NULL);
2609f905dafSJohn Baldwin }
2619f905dafSJohn Baldwin 
2628e8e46ccSJohn Baldwin static struct resource *
2638e8e46ccSJohn Baldwin vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
2648e8e46ccSJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
2658e8e46ccSJohn Baldwin {
2669f905dafSJohn Baldwin 	struct vga_resource *vr;
2678e8e46ccSJohn Baldwin 
268e8b145c2SJohn Baldwin 	switch (type) {
269e8b145c2SJohn Baldwin 	case SYS_RES_MEMORY:
270e8b145c2SJohn Baldwin 	case SYS_RES_IOPORT:
271e8b145c2SJohn Baldwin 		/*
272e8b145c2SJohn Baldwin 		 * For BARs, we cache the resource so that we only allocate it
273e8b145c2SJohn Baldwin 		 * from the PCI bus once.
274e8b145c2SJohn Baldwin 		 */
2759f905dafSJohn Baldwin 		vr = lookup_res(device_get_softc(dev), *rid);
2769f905dafSJohn Baldwin 		if (vr == NULL)
277e8b145c2SJohn Baldwin 			return (NULL);
2789f905dafSJohn Baldwin 		if (vr->vr_res == NULL)
2799f905dafSJohn Baldwin 			vr->vr_res = bus_alloc_resource(dev, type, rid, start,
2809f905dafSJohn Baldwin 			    end, count, flags);
2819f905dafSJohn Baldwin 		if (vr->vr_res != NULL)
2829f905dafSJohn Baldwin 			vr->vr_refs++;
2839f905dafSJohn Baldwin 		return (vr->vr_res);
284e8b145c2SJohn Baldwin 	}
2858e8e46ccSJohn Baldwin 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
2868e8e46ccSJohn Baldwin }
2878e8e46ccSJohn Baldwin 
2888e8e46ccSJohn Baldwin static int
2898e8e46ccSJohn Baldwin vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
2908e8e46ccSJohn Baldwin     struct resource *r)
2918e8e46ccSJohn Baldwin {
2929f905dafSJohn Baldwin 	struct vga_resource *vr;
2939f905dafSJohn Baldwin 	int error;
294e8b145c2SJohn Baldwin 
295e8b145c2SJohn Baldwin 	switch (type) {
296e8b145c2SJohn Baldwin 	case SYS_RES_MEMORY:
297e8b145c2SJohn Baldwin 	case SYS_RES_IOPORT:
298e8b145c2SJohn Baldwin 		/*
299e8b145c2SJohn Baldwin 		 * For BARs, we release the resource from the PCI bus
300e8b145c2SJohn Baldwin 		 * when the last child reference goes away.
301e8b145c2SJohn Baldwin 		 */
3029f905dafSJohn Baldwin 		vr = lookup_res(device_get_softc(dev), rid);
3039f905dafSJohn Baldwin 		if (vr == NULL)
304e8b145c2SJohn Baldwin 			return (EINVAL);
3059f905dafSJohn Baldwin 		if (vr->vr_res == NULL)
306e8b145c2SJohn Baldwin 			return (EINVAL);
3079f905dafSJohn Baldwin 		KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
3089f905dafSJohn Baldwin 		if (vr->vr_refs > 1) {
3099f905dafSJohn Baldwin 			vr->vr_refs--;
310e8b145c2SJohn Baldwin 			return (0);
311e8b145c2SJohn Baldwin 		}
3129f905dafSJohn Baldwin 		KASSERT(vr->vr_refs > 0,
313e8b145c2SJohn Baldwin 		    ("vga_pci resource reference count underflow"));
314e8b145c2SJohn Baldwin 		error = bus_release_resource(dev, type, rid, r);
315e8b145c2SJohn Baldwin 		if (error == 0) {
3169f905dafSJohn Baldwin 			vr->vr_res = NULL;
3179f905dafSJohn Baldwin 			vr->vr_refs = 0;
318e8b145c2SJohn Baldwin 		}
319e8b145c2SJohn Baldwin 		return (error);
320e8b145c2SJohn Baldwin 	}
3218e8e46ccSJohn Baldwin 
3228e8e46ccSJohn Baldwin 	return (bus_release_resource(dev, type, rid, r));
3238e8e46ccSJohn Baldwin }
3248e8e46ccSJohn Baldwin 
3258e8e46ccSJohn Baldwin /* PCI interface. */
3268e8e46ccSJohn Baldwin 
3278e8e46ccSJohn Baldwin static uint32_t
3288e8e46ccSJohn Baldwin vga_pci_read_config(device_t dev, device_t child, int reg, int width)
3298e8e46ccSJohn Baldwin {
3308e8e46ccSJohn Baldwin 
3318e8e46ccSJohn Baldwin 	return (pci_read_config(dev, reg, width));
3328e8e46ccSJohn Baldwin }
3338e8e46ccSJohn Baldwin 
3348e8e46ccSJohn Baldwin static void
3358e8e46ccSJohn Baldwin vga_pci_write_config(device_t dev, device_t child, int reg,
3368e8e46ccSJohn Baldwin     uint32_t val, int width)
3378e8e46ccSJohn Baldwin {
3388e8e46ccSJohn Baldwin 
3398e8e46ccSJohn Baldwin 	pci_write_config(dev, reg, val, width);
3408e8e46ccSJohn Baldwin }
3418e8e46ccSJohn Baldwin 
3428e8e46ccSJohn Baldwin static int
3438e8e46ccSJohn Baldwin vga_pci_enable_busmaster(device_t dev, device_t child)
3448e8e46ccSJohn Baldwin {
3458e8e46ccSJohn Baldwin 
3468e8e46ccSJohn Baldwin 	return (pci_enable_busmaster(dev));
3478e8e46ccSJohn Baldwin }
3488e8e46ccSJohn Baldwin 
3498e8e46ccSJohn Baldwin static int
3508e8e46ccSJohn Baldwin vga_pci_disable_busmaster(device_t dev, device_t child)
3518e8e46ccSJohn Baldwin {
3528e8e46ccSJohn Baldwin 
3538e8e46ccSJohn Baldwin 	return (pci_disable_busmaster(dev));
3548e8e46ccSJohn Baldwin }
3558e8e46ccSJohn Baldwin 
3568e8e46ccSJohn Baldwin static int
3578e8e46ccSJohn Baldwin vga_pci_enable_io(device_t dev, device_t child, int space)
3588e8e46ccSJohn Baldwin {
3598e8e46ccSJohn Baldwin 
3608e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_enable_io\n",
3618e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
3628e8e46ccSJohn Baldwin 	return (pci_enable_io(dev, space));
3638e8e46ccSJohn Baldwin }
3648e8e46ccSJohn Baldwin 
3658e8e46ccSJohn Baldwin static int
3668e8e46ccSJohn Baldwin vga_pci_disable_io(device_t dev, device_t child, int space)
3678e8e46ccSJohn Baldwin {
3688e8e46ccSJohn Baldwin 
3698e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_disable_io\n",
3708e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
3718e8e46ccSJohn Baldwin 	return (pci_disable_io(dev, space));
3728e8e46ccSJohn Baldwin }
3738e8e46ccSJohn Baldwin 
3748e8e46ccSJohn Baldwin static int
375e7e2941bSJohn Baldwin vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
376e7e2941bSJohn Baldwin {
377e7e2941bSJohn Baldwin 
378e7e2941bSJohn Baldwin 	return (pci_get_vpd_ident(dev, identptr));
379e7e2941bSJohn Baldwin }
380e7e2941bSJohn Baldwin 
381e7e2941bSJohn Baldwin static int
382e7e2941bSJohn Baldwin vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
383e7e2941bSJohn Baldwin     const char **vptr)
384e7e2941bSJohn Baldwin {
385e7e2941bSJohn Baldwin 
386e7e2941bSJohn Baldwin 	return (pci_get_vpd_readonly(dev, kw, vptr));
387e7e2941bSJohn Baldwin }
388e7e2941bSJohn Baldwin 
389e7e2941bSJohn Baldwin static int
3908e8e46ccSJohn Baldwin vga_pci_set_powerstate(device_t dev, device_t child, int state)
3918e8e46ccSJohn Baldwin {
3928e8e46ccSJohn Baldwin 
3938e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_set_powerstate\n",
3948e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
3958e8e46ccSJohn Baldwin 	return (pci_set_powerstate(dev, state));
3968e8e46ccSJohn Baldwin }
3978e8e46ccSJohn Baldwin 
3988e8e46ccSJohn Baldwin static int
3998e8e46ccSJohn Baldwin vga_pci_get_powerstate(device_t dev, device_t child)
4008e8e46ccSJohn Baldwin {
4018e8e46ccSJohn Baldwin 
4028e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_get_powerstate\n",
4038e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
4048e8e46ccSJohn Baldwin 	return (pci_get_powerstate(dev));
4058e8e46ccSJohn Baldwin }
4068e8e46ccSJohn Baldwin 
4078e8e46ccSJohn Baldwin static int
4088e8e46ccSJohn Baldwin vga_pci_assign_interrupt(device_t dev, device_t child)
4098e8e46ccSJohn Baldwin {
4108e8e46ccSJohn Baldwin 
4118e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
4128e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
4138e8e46ccSJohn Baldwin 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
4148e8e46ccSJohn Baldwin }
4158e8e46ccSJohn Baldwin 
4168e8e46ccSJohn Baldwin static int
417c668000bSJohn Baldwin vga_pci_find_cap(device_t dev, device_t child, int capability,
418c668000bSJohn Baldwin     int *capreg)
419c668000bSJohn Baldwin {
420c668000bSJohn Baldwin 
421c668000bSJohn Baldwin 	return (pci_find_cap(dev, capability, capreg));
422c668000bSJohn Baldwin }
423c668000bSJohn Baldwin 
424c668000bSJohn Baldwin static int
4258e8e46ccSJohn Baldwin vga_pci_find_extcap(device_t dev, device_t child, int capability,
4268e8e46ccSJohn Baldwin     int *capreg)
4278e8e46ccSJohn Baldwin {
4288e8e46ccSJohn Baldwin 
4298e8e46ccSJohn Baldwin 	return (pci_find_extcap(dev, capability, capreg));
4308e8e46ccSJohn Baldwin }
4318e8e46ccSJohn Baldwin 
432e7e2941bSJohn Baldwin static int
433c668000bSJohn Baldwin vga_pci_find_htcap(device_t dev, device_t child, int capability,
434c668000bSJohn Baldwin     int *capreg)
435c668000bSJohn Baldwin {
436c668000bSJohn Baldwin 
437c668000bSJohn Baldwin 	return (pci_find_htcap(dev, capability, capreg));
438c668000bSJohn Baldwin }
439c668000bSJohn Baldwin 
440c668000bSJohn Baldwin static int
441e7e2941bSJohn Baldwin vga_pci_alloc_msi(device_t dev, device_t child, int *count)
442e7e2941bSJohn Baldwin {
443e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
444e7e2941bSJohn Baldwin 	int error;
445e7e2941bSJohn Baldwin 
446e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
447e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != NULL)
448e7e2941bSJohn Baldwin 		return (EBUSY);
449e7e2941bSJohn Baldwin 	error = pci_alloc_msi(dev, count);
450e7e2941bSJohn Baldwin 	if (error == 0)
451e7e2941bSJohn Baldwin 		sc->vga_msi_child = child;
452e7e2941bSJohn Baldwin 	return (error);
453e7e2941bSJohn Baldwin }
454e7e2941bSJohn Baldwin 
455e7e2941bSJohn Baldwin static int
456e7e2941bSJohn Baldwin vga_pci_alloc_msix(device_t dev, device_t child, int *count)
457e7e2941bSJohn Baldwin {
458e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
459e7e2941bSJohn Baldwin 	int error;
460e7e2941bSJohn Baldwin 
461e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
462e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != NULL)
463e7e2941bSJohn Baldwin 		return (EBUSY);
464e7e2941bSJohn Baldwin 	error = pci_alloc_msix(dev, count);
465e7e2941bSJohn Baldwin 	if (error == 0)
466e7e2941bSJohn Baldwin 		sc->vga_msi_child = child;
467e7e2941bSJohn Baldwin 	return (error);
468e7e2941bSJohn Baldwin }
469e7e2941bSJohn Baldwin 
470e7e2941bSJohn Baldwin static int
471e7e2941bSJohn Baldwin vga_pci_remap_msix(device_t dev, device_t child, int count,
472e7e2941bSJohn Baldwin     const u_int *vectors)
473e7e2941bSJohn Baldwin {
474e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
475e7e2941bSJohn Baldwin 
476e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
477e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != child)
478e7e2941bSJohn Baldwin 		return (ENXIO);
479e7e2941bSJohn Baldwin 	return (pci_remap_msix(dev, count, vectors));
480e7e2941bSJohn Baldwin }
481e7e2941bSJohn Baldwin 
482e7e2941bSJohn Baldwin static int
483e7e2941bSJohn Baldwin vga_pci_release_msi(device_t dev, device_t child)
484e7e2941bSJohn Baldwin {
485e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
486e7e2941bSJohn Baldwin 	int error;
487e7e2941bSJohn Baldwin 
488e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
489e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != child)
490e7e2941bSJohn Baldwin 		return (ENXIO);
491e7e2941bSJohn Baldwin 	error = pci_release_msi(dev);
492e7e2941bSJohn Baldwin 	if (error == 0)
493e7e2941bSJohn Baldwin 		sc->vga_msi_child = NULL;
494e7e2941bSJohn Baldwin 	return (error);
495e7e2941bSJohn Baldwin }
496e7e2941bSJohn Baldwin 
497e7e2941bSJohn Baldwin static int
498e7e2941bSJohn Baldwin vga_pci_msi_count(device_t dev, device_t child)
499e7e2941bSJohn Baldwin {
500e7e2941bSJohn Baldwin 
501e7e2941bSJohn Baldwin 	return (pci_msi_count(dev));
502e7e2941bSJohn Baldwin }
503e7e2941bSJohn Baldwin 
504e7e2941bSJohn Baldwin static int
505e7e2941bSJohn Baldwin vga_pci_msix_count(device_t dev, device_t child)
506e7e2941bSJohn Baldwin {
507e7e2941bSJohn Baldwin 
508e7e2941bSJohn Baldwin 	return (pci_msix_count(dev));
509e7e2941bSJohn Baldwin }
510e7e2941bSJohn Baldwin 
5115c818b67SKonstantin Belousov static bus_dma_tag_t
5125c818b67SKonstantin Belousov vga_pci_get_dma_tag(device_t bus, device_t child)
5135c818b67SKonstantin Belousov {
5145c818b67SKonstantin Belousov 
5155c818b67SKonstantin Belousov 	return (bus_get_dma_tag(bus));
5165c818b67SKonstantin Belousov }
5175c818b67SKonstantin Belousov 
5188e8e46ccSJohn Baldwin static device_method_t vga_pci_methods[] = {
5198e8e46ccSJohn Baldwin 	/* Device interface */
5208e8e46ccSJohn Baldwin 	DEVMETHOD(device_probe,		vga_pci_probe),
5218e8e46ccSJohn Baldwin 	DEVMETHOD(device_attach,	vga_pci_attach),
5228e8e46ccSJohn Baldwin 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
5238e8e46ccSJohn Baldwin 	DEVMETHOD(device_suspend,	vga_pci_suspend),
5248e8e46ccSJohn Baldwin 	DEVMETHOD(device_resume,	vga_pci_resume),
5258e8e46ccSJohn Baldwin 
5268e8e46ccSJohn Baldwin 	/* Bus interface */
5278e8e46ccSJohn Baldwin 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
5288e8e46ccSJohn Baldwin 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
5296be0323dSRobert Noland 	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
5306be0323dSRobert Noland 	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
5318e8e46ccSJohn Baldwin 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
5328e8e46ccSJohn Baldwin 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
5338e8e46ccSJohn Baldwin 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
5348e8e46ccSJohn Baldwin 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
5355c818b67SKonstantin Belousov 	DEVMETHOD(bus_get_dma_tag,	vga_pci_get_dma_tag),
5368e8e46ccSJohn Baldwin 
5378e8e46ccSJohn Baldwin 	/* PCI interface */
5388e8e46ccSJohn Baldwin 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
5398e8e46ccSJohn Baldwin 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
5408e8e46ccSJohn Baldwin 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
5418e8e46ccSJohn Baldwin 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
5428e8e46ccSJohn Baldwin 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
5438e8e46ccSJohn Baldwin 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
544e7e2941bSJohn Baldwin 	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
545e7e2941bSJohn Baldwin 	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
5468e8e46ccSJohn Baldwin 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
5478e8e46ccSJohn Baldwin 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
5488e8e46ccSJohn Baldwin 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
549c668000bSJohn Baldwin 	DEVMETHOD(pci_find_cap,		vga_pci_find_cap),
5508e8e46ccSJohn Baldwin 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
551c668000bSJohn Baldwin 	DEVMETHOD(pci_find_htcap,	vga_pci_find_htcap),
552e7e2941bSJohn Baldwin 	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
553e7e2941bSJohn Baldwin 	DEVMETHOD(pci_alloc_msix,	vga_pci_alloc_msix),
554e7e2941bSJohn Baldwin 	DEVMETHOD(pci_remap_msix,	vga_pci_remap_msix),
555e7e2941bSJohn Baldwin 	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
556e7e2941bSJohn Baldwin 	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
557e7e2941bSJohn Baldwin 	DEVMETHOD(pci_msix_count,	vga_pci_msix_count),
5588e8e46ccSJohn Baldwin 
5598e8e46ccSJohn Baldwin 	{ 0, 0 }
5608e8e46ccSJohn Baldwin };
5618e8e46ccSJohn Baldwin 
5628e8e46ccSJohn Baldwin static driver_t vga_pci_driver = {
5638e8e46ccSJohn Baldwin 	"vgapci",
5648e8e46ccSJohn Baldwin 	vga_pci_methods,
565e7e2941bSJohn Baldwin 	sizeof(struct vga_pci_softc),
5668e8e46ccSJohn Baldwin };
5678e8e46ccSJohn Baldwin 
5688e8e46ccSJohn Baldwin static devclass_t vga_devclass;
5698e8e46ccSJohn Baldwin 
5708e8e46ccSJohn Baldwin DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
571