xref: /freebsd/sys/dev/pci/vga_pci.c (revision 3219f535d98ad7c1a1b731f022c7b9ec4402094c)
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 
498e8e46ccSJohn Baldwin #include <dev/pci/pcireg.h>
508e8e46ccSJohn Baldwin #include <dev/pci/pcivar.h>
518e8e46ccSJohn Baldwin 
52e8b145c2SJohn Baldwin struct vga_resource {
53e8b145c2SJohn Baldwin 	struct resource	*vr_res;
54e8b145c2SJohn Baldwin 	int	vr_refs;
55e8b145c2SJohn Baldwin };
56e8b145c2SJohn Baldwin 
57e7e2941bSJohn Baldwin struct vga_pci_softc {
58e7e2941bSJohn Baldwin 	device_t	vga_msi_child;	/* Child driver using MSI. */
59e8b145c2SJohn Baldwin 	struct vga_resource vga_res[PCIR_MAX_BAR_0 + 1];
60e7e2941bSJohn Baldwin };
61e7e2941bSJohn Baldwin 
623219f535SJung-uk Kim SYSCTL_DECL(_hw_pci);
633219f535SJung-uk Kim 
643219f535SJung-uk Kim int vga_pci_default_unit = -1;
653219f535SJung-uk Kim TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
663219f535SJung-uk Kim SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RD,
673219f535SJung-uk Kim     &vga_pci_default_unit, -1, "Default VGA-compatible display");
683219f535SJung-uk Kim 
698e8e46ccSJohn Baldwin static int
708e8e46ccSJohn Baldwin vga_pci_probe(device_t dev)
718e8e46ccSJohn Baldwin {
723219f535SJung-uk Kim 	device_t bdev;
733219f535SJung-uk Kim 	int unit;
743219f535SJung-uk Kim 	uint16_t bctl;
758e8e46ccSJohn Baldwin 
768e8e46ccSJohn Baldwin 	switch (pci_get_class(dev)) {
778e8e46ccSJohn Baldwin 	case PCIC_DISPLAY:
788e8e46ccSJohn Baldwin 		break;
798e8e46ccSJohn Baldwin 	case PCIC_OLD:
808e8e46ccSJohn Baldwin 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
818e8e46ccSJohn Baldwin 			return (ENXIO);
828e8e46ccSJohn Baldwin 		break;
838e8e46ccSJohn Baldwin 	default:
848e8e46ccSJohn Baldwin 		return (ENXIO);
858e8e46ccSJohn Baldwin 	}
863219f535SJung-uk Kim 
873219f535SJung-uk Kim 	/* Probe default display. */
883219f535SJung-uk Kim 	unit = device_get_unit(dev);
893219f535SJung-uk Kim 	bdev = device_get_parent(device_get_parent(dev));
903219f535SJung-uk Kim 	bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2);
913219f535SJung-uk Kim 	if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0)
923219f535SJung-uk Kim 		vga_pci_default_unit = unit;
933219f535SJung-uk Kim 	if (vga_pci_default_unit == unit)
943219f535SJung-uk Kim 		device_set_flags(dev, 1);
953219f535SJung-uk Kim 
968e8e46ccSJohn Baldwin 	device_set_desc(dev, "VGA-compatible display");
97be7ccc4bSJohn Baldwin 	return (BUS_PROBE_GENERIC);
988e8e46ccSJohn Baldwin }
998e8e46ccSJohn Baldwin 
1008e8e46ccSJohn Baldwin static int
1018e8e46ccSJohn Baldwin vga_pci_attach(device_t dev)
1028e8e46ccSJohn Baldwin {
1038e8e46ccSJohn Baldwin 
1048e8e46ccSJohn Baldwin 	bus_generic_probe(dev);
1058e8e46ccSJohn Baldwin 
1068e8e46ccSJohn Baldwin 	/* Always create a drm child for now to make it easier on drm. */
1078e8e46ccSJohn Baldwin 	device_add_child(dev, "drm", -1);
1088e8e46ccSJohn Baldwin 	bus_generic_attach(dev);
1098e8e46ccSJohn Baldwin 	return (0);
1108e8e46ccSJohn Baldwin }
1118e8e46ccSJohn Baldwin 
1128e8e46ccSJohn Baldwin static int
1138e8e46ccSJohn Baldwin vga_pci_suspend(device_t dev)
1148e8e46ccSJohn Baldwin {
1158e8e46ccSJohn Baldwin 
1168e8e46ccSJohn Baldwin 	return (bus_generic_suspend(dev));
1178e8e46ccSJohn Baldwin }
1188e8e46ccSJohn Baldwin 
1198e8e46ccSJohn Baldwin static int
1208e8e46ccSJohn Baldwin vga_pci_resume(device_t dev)
1218e8e46ccSJohn Baldwin {
1228e8e46ccSJohn Baldwin 
1238e8e46ccSJohn Baldwin 	return (bus_generic_resume(dev));
1248e8e46ccSJohn Baldwin }
1258e8e46ccSJohn Baldwin 
1268e8e46ccSJohn Baldwin /* Bus interface. */
1278e8e46ccSJohn Baldwin 
1288e8e46ccSJohn Baldwin static int
1298e8e46ccSJohn Baldwin vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1308e8e46ccSJohn Baldwin {
1318e8e46ccSJohn Baldwin 
1328e8e46ccSJohn Baldwin 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
1338e8e46ccSJohn Baldwin }
1348e8e46ccSJohn Baldwin 
1358e8e46ccSJohn Baldwin static int
1368e8e46ccSJohn Baldwin vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1378e8e46ccSJohn Baldwin {
1388e8e46ccSJohn Baldwin 
1398e8e46ccSJohn Baldwin 	return (EINVAL);
1408e8e46ccSJohn Baldwin }
1418e8e46ccSJohn Baldwin 
1426be0323dSRobert Noland static int
1436be0323dSRobert Noland vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
1446be0323dSRobert Noland     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
1456be0323dSRobert Noland     void **cookiep)
1466be0323dSRobert Noland {
1476be0323dSRobert Noland 	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
1486be0323dSRobert Noland 	    filter, intr, arg, cookiep));
1496be0323dSRobert Noland }
1506be0323dSRobert Noland 
1516be0323dSRobert Noland static int
1526be0323dSRobert Noland vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
1536be0323dSRobert Noland     void *cookie)
1546be0323dSRobert Noland {
1556be0323dSRobert Noland 	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
1566be0323dSRobert Noland }
1576be0323dSRobert Noland 
1588e8e46ccSJohn Baldwin static struct resource *
1598e8e46ccSJohn Baldwin vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
1608e8e46ccSJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
1618e8e46ccSJohn Baldwin {
162e8b145c2SJohn Baldwin 	struct vga_pci_softc *sc;
163e8b145c2SJohn Baldwin 	int bar;
1648e8e46ccSJohn Baldwin 
165e8b145c2SJohn Baldwin 	switch (type) {
166e8b145c2SJohn Baldwin 	case SYS_RES_MEMORY:
167e8b145c2SJohn Baldwin 	case SYS_RES_IOPORT:
168e8b145c2SJohn Baldwin 		/*
169e8b145c2SJohn Baldwin 		 * For BARs, we cache the resource so that we only allocate it
170e8b145c2SJohn Baldwin 		 * from the PCI bus once.
171e8b145c2SJohn Baldwin 		 */
172e8b145c2SJohn Baldwin 		bar = PCI_RID2BAR(*rid);
173e8b145c2SJohn Baldwin 		if (bar < 0 || bar > PCIR_MAX_BAR_0)
174e8b145c2SJohn Baldwin 			return (NULL);
175e8b145c2SJohn Baldwin 		sc = device_get_softc(dev);
176e8b145c2SJohn Baldwin 		if (sc->vga_res[bar].vr_res == NULL)
177e8b145c2SJohn Baldwin 			sc->vga_res[bar].vr_res = bus_alloc_resource(dev, type,
178e8b145c2SJohn Baldwin 			    rid, start, end, count, flags);
179e8b145c2SJohn Baldwin 		if (sc->vga_res[bar].vr_res != NULL)
180e8b145c2SJohn Baldwin 			sc->vga_res[bar].vr_refs++;
181e8b145c2SJohn Baldwin 		return (sc->vga_res[bar].vr_res);
182e8b145c2SJohn Baldwin 	}
1838e8e46ccSJohn Baldwin 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
1848e8e46ccSJohn Baldwin }
1858e8e46ccSJohn Baldwin 
1868e8e46ccSJohn Baldwin static int
1878e8e46ccSJohn Baldwin vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
1888e8e46ccSJohn Baldwin     struct resource *r)
1898e8e46ccSJohn Baldwin {
190e8b145c2SJohn Baldwin 	struct vga_pci_softc *sc;
191e8b145c2SJohn Baldwin 	int bar, error;
192e8b145c2SJohn Baldwin 
193e8b145c2SJohn Baldwin 	switch (type) {
194e8b145c2SJohn Baldwin 	case SYS_RES_MEMORY:
195e8b145c2SJohn Baldwin 	case SYS_RES_IOPORT:
196e8b145c2SJohn Baldwin 		/*
197e8b145c2SJohn Baldwin 		 * For BARs, we release the resource from the PCI bus
198e8b145c2SJohn Baldwin 		 * when the last child reference goes away.
199e8b145c2SJohn Baldwin 		 */
200e8b145c2SJohn Baldwin 		bar = PCI_RID2BAR(rid);
201e8b145c2SJohn Baldwin 		if (bar < 0 || bar > PCIR_MAX_BAR_0)
202e8b145c2SJohn Baldwin 			return (EINVAL);
203e8b145c2SJohn Baldwin 		sc = device_get_softc(dev);
204e8b145c2SJohn Baldwin 		if (sc->vga_res[bar].vr_res == NULL)
205e8b145c2SJohn Baldwin 			return (EINVAL);
206e8b145c2SJohn Baldwin 		KASSERT(sc->vga_res[bar].vr_res == r,
207e8b145c2SJohn Baldwin 		    ("vga_pci resource mismatch"));
208e8b145c2SJohn Baldwin 		if (sc->vga_res[bar].vr_refs > 1) {
209e8b145c2SJohn Baldwin 			sc->vga_res[bar].vr_refs--;
210e8b145c2SJohn Baldwin 			return (0);
211e8b145c2SJohn Baldwin 		}
212e8b145c2SJohn Baldwin 		KASSERT(sc->vga_res[bar].vr_refs > 0,
213e8b145c2SJohn Baldwin 		    ("vga_pci resource reference count underflow"));
214e8b145c2SJohn Baldwin 		error = bus_release_resource(dev, type, rid, r);
215e8b145c2SJohn Baldwin 		if (error == 0) {
216e8b145c2SJohn Baldwin 			sc->vga_res[bar].vr_res = NULL;
217e8b145c2SJohn Baldwin 			sc->vga_res[bar].vr_refs = 0;
218e8b145c2SJohn Baldwin 		}
219e8b145c2SJohn Baldwin 		return (error);
220e8b145c2SJohn Baldwin 	}
2218e8e46ccSJohn Baldwin 
2228e8e46ccSJohn Baldwin 	return (bus_release_resource(dev, type, rid, r));
2238e8e46ccSJohn Baldwin }
2248e8e46ccSJohn Baldwin 
2258e8e46ccSJohn Baldwin /* PCI interface. */
2268e8e46ccSJohn Baldwin 
2278e8e46ccSJohn Baldwin static uint32_t
2288e8e46ccSJohn Baldwin vga_pci_read_config(device_t dev, device_t child, int reg, int width)
2298e8e46ccSJohn Baldwin {
2308e8e46ccSJohn Baldwin 
2318e8e46ccSJohn Baldwin 	return (pci_read_config(dev, reg, width));
2328e8e46ccSJohn Baldwin }
2338e8e46ccSJohn Baldwin 
2348e8e46ccSJohn Baldwin static void
2358e8e46ccSJohn Baldwin vga_pci_write_config(device_t dev, device_t child, int reg,
2368e8e46ccSJohn Baldwin     uint32_t val, int width)
2378e8e46ccSJohn Baldwin {
2388e8e46ccSJohn Baldwin 
2398e8e46ccSJohn Baldwin 	pci_write_config(dev, reg, val, width);
2408e8e46ccSJohn Baldwin }
2418e8e46ccSJohn Baldwin 
2428e8e46ccSJohn Baldwin static int
2438e8e46ccSJohn Baldwin vga_pci_enable_busmaster(device_t dev, device_t child)
2448e8e46ccSJohn Baldwin {
2458e8e46ccSJohn Baldwin 
2468e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_enable_busmaster\n",
2478e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
2488e8e46ccSJohn Baldwin 	return (pci_enable_busmaster(dev));
2498e8e46ccSJohn Baldwin }
2508e8e46ccSJohn Baldwin 
2518e8e46ccSJohn Baldwin static int
2528e8e46ccSJohn Baldwin vga_pci_disable_busmaster(device_t dev, device_t child)
2538e8e46ccSJohn Baldwin {
2548e8e46ccSJohn Baldwin 
2558e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_disable_busmaster\n",
2568e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
2578e8e46ccSJohn Baldwin 	return (pci_disable_busmaster(dev));
2588e8e46ccSJohn Baldwin }
2598e8e46ccSJohn Baldwin 
2608e8e46ccSJohn Baldwin static int
2618e8e46ccSJohn Baldwin vga_pci_enable_io(device_t dev, device_t child, int space)
2628e8e46ccSJohn Baldwin {
2638e8e46ccSJohn Baldwin 
2648e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_enable_io\n",
2658e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
2668e8e46ccSJohn Baldwin 	return (pci_enable_io(dev, space));
2678e8e46ccSJohn Baldwin }
2688e8e46ccSJohn Baldwin 
2698e8e46ccSJohn Baldwin static int
2708e8e46ccSJohn Baldwin vga_pci_disable_io(device_t dev, device_t child, int space)
2718e8e46ccSJohn Baldwin {
2728e8e46ccSJohn Baldwin 
2738e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_disable_io\n",
2748e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
2758e8e46ccSJohn Baldwin 	return (pci_disable_io(dev, space));
2768e8e46ccSJohn Baldwin }
2778e8e46ccSJohn Baldwin 
2788e8e46ccSJohn Baldwin static int
279e7e2941bSJohn Baldwin vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
280e7e2941bSJohn Baldwin {
281e7e2941bSJohn Baldwin 
282e7e2941bSJohn Baldwin 	return (pci_get_vpd_ident(dev, identptr));
283e7e2941bSJohn Baldwin }
284e7e2941bSJohn Baldwin 
285e7e2941bSJohn Baldwin static int
286e7e2941bSJohn Baldwin vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
287e7e2941bSJohn Baldwin     const char **vptr)
288e7e2941bSJohn Baldwin {
289e7e2941bSJohn Baldwin 
290e7e2941bSJohn Baldwin 	return (pci_get_vpd_readonly(dev, kw, vptr));
291e7e2941bSJohn Baldwin }
292e7e2941bSJohn Baldwin 
293e7e2941bSJohn Baldwin static int
2948e8e46ccSJohn Baldwin vga_pci_set_powerstate(device_t dev, device_t child, int state)
2958e8e46ccSJohn Baldwin {
2968e8e46ccSJohn Baldwin 
2978e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_set_powerstate\n",
2988e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
2998e8e46ccSJohn Baldwin 	return (pci_set_powerstate(dev, state));
3008e8e46ccSJohn Baldwin }
3018e8e46ccSJohn Baldwin 
3028e8e46ccSJohn Baldwin static int
3038e8e46ccSJohn Baldwin vga_pci_get_powerstate(device_t dev, device_t child)
3048e8e46ccSJohn Baldwin {
3058e8e46ccSJohn Baldwin 
3068e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_get_powerstate\n",
3078e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
3088e8e46ccSJohn Baldwin 	return (pci_get_powerstate(dev));
3098e8e46ccSJohn Baldwin }
3108e8e46ccSJohn Baldwin 
3118e8e46ccSJohn Baldwin static int
3128e8e46ccSJohn Baldwin vga_pci_assign_interrupt(device_t dev, device_t child)
3138e8e46ccSJohn Baldwin {
3148e8e46ccSJohn Baldwin 
3158e8e46ccSJohn Baldwin 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
3168e8e46ccSJohn Baldwin 	    device_get_nameunit(child));
3178e8e46ccSJohn Baldwin 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
3188e8e46ccSJohn Baldwin }
3198e8e46ccSJohn Baldwin 
3208e8e46ccSJohn Baldwin static int
3218e8e46ccSJohn Baldwin vga_pci_find_extcap(device_t dev, device_t child, int capability,
3228e8e46ccSJohn Baldwin     int *capreg)
3238e8e46ccSJohn Baldwin {
3248e8e46ccSJohn Baldwin 
3258e8e46ccSJohn Baldwin 	return (pci_find_extcap(dev, capability, capreg));
3268e8e46ccSJohn Baldwin }
3278e8e46ccSJohn Baldwin 
328e7e2941bSJohn Baldwin static int
329e7e2941bSJohn Baldwin vga_pci_alloc_msi(device_t dev, device_t child, int *count)
330e7e2941bSJohn Baldwin {
331e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
332e7e2941bSJohn Baldwin 	int error;
333e7e2941bSJohn Baldwin 
334e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
335e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != NULL)
336e7e2941bSJohn Baldwin 		return (EBUSY);
337e7e2941bSJohn Baldwin 	error = pci_alloc_msi(dev, count);
338e7e2941bSJohn Baldwin 	if (error == 0)
339e7e2941bSJohn Baldwin 		sc->vga_msi_child = child;
340e7e2941bSJohn Baldwin 	return (error);
341e7e2941bSJohn Baldwin }
342e7e2941bSJohn Baldwin 
343e7e2941bSJohn Baldwin static int
344e7e2941bSJohn Baldwin vga_pci_alloc_msix(device_t dev, device_t child, int *count)
345e7e2941bSJohn Baldwin {
346e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
347e7e2941bSJohn Baldwin 	int error;
348e7e2941bSJohn Baldwin 
349e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
350e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != NULL)
351e7e2941bSJohn Baldwin 		return (EBUSY);
352e7e2941bSJohn Baldwin 	error = pci_alloc_msix(dev, count);
353e7e2941bSJohn Baldwin 	if (error == 0)
354e7e2941bSJohn Baldwin 		sc->vga_msi_child = child;
355e7e2941bSJohn Baldwin 	return (error);
356e7e2941bSJohn Baldwin }
357e7e2941bSJohn Baldwin 
358e7e2941bSJohn Baldwin static int
359e7e2941bSJohn Baldwin vga_pci_remap_msix(device_t dev, device_t child, int count,
360e7e2941bSJohn Baldwin     const u_int *vectors)
361e7e2941bSJohn Baldwin {
362e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
363e7e2941bSJohn Baldwin 
364e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
365e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != child)
366e7e2941bSJohn Baldwin 		return (ENXIO);
367e7e2941bSJohn Baldwin 	return (pci_remap_msix(dev, count, vectors));
368e7e2941bSJohn Baldwin }
369e7e2941bSJohn Baldwin 
370e7e2941bSJohn Baldwin static int
371e7e2941bSJohn Baldwin vga_pci_release_msi(device_t dev, device_t child)
372e7e2941bSJohn Baldwin {
373e7e2941bSJohn Baldwin 	struct vga_pci_softc *sc;
374e7e2941bSJohn Baldwin 	int error;
375e7e2941bSJohn Baldwin 
376e7e2941bSJohn Baldwin 	sc = device_get_softc(dev);
377e7e2941bSJohn Baldwin 	if (sc->vga_msi_child != child)
378e7e2941bSJohn Baldwin 		return (ENXIO);
379e7e2941bSJohn Baldwin 	error = pci_release_msi(dev);
380e7e2941bSJohn Baldwin 	if (error == 0)
381e7e2941bSJohn Baldwin 		sc->vga_msi_child = NULL;
382e7e2941bSJohn Baldwin 	return (error);
383e7e2941bSJohn Baldwin }
384e7e2941bSJohn Baldwin 
385e7e2941bSJohn Baldwin static int
386e7e2941bSJohn Baldwin vga_pci_msi_count(device_t dev, device_t child)
387e7e2941bSJohn Baldwin {
388e7e2941bSJohn Baldwin 
389e7e2941bSJohn Baldwin 	return (pci_msi_count(dev));
390e7e2941bSJohn Baldwin }
391e7e2941bSJohn Baldwin 
392e7e2941bSJohn Baldwin static int
393e7e2941bSJohn Baldwin vga_pci_msix_count(device_t dev, device_t child)
394e7e2941bSJohn Baldwin {
395e7e2941bSJohn Baldwin 
396e7e2941bSJohn Baldwin 	return (pci_msix_count(dev));
397e7e2941bSJohn Baldwin }
398e7e2941bSJohn Baldwin 
3998e8e46ccSJohn Baldwin static device_method_t vga_pci_methods[] = {
4008e8e46ccSJohn Baldwin 	/* Device interface */
4018e8e46ccSJohn Baldwin 	DEVMETHOD(device_probe,		vga_pci_probe),
4028e8e46ccSJohn Baldwin 	DEVMETHOD(device_attach,	vga_pci_attach),
4038e8e46ccSJohn Baldwin 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
4048e8e46ccSJohn Baldwin 	DEVMETHOD(device_suspend,	vga_pci_suspend),
4058e8e46ccSJohn Baldwin 	DEVMETHOD(device_resume,	vga_pci_resume),
4068e8e46ccSJohn Baldwin 
4078e8e46ccSJohn Baldwin 	/* Bus interface */
4088e8e46ccSJohn Baldwin 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
4098e8e46ccSJohn Baldwin 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
4106be0323dSRobert Noland 	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
4116be0323dSRobert Noland 	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
4128e8e46ccSJohn Baldwin 
4138e8e46ccSJohn Baldwin 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
4148e8e46ccSJohn Baldwin 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
4158e8e46ccSJohn Baldwin 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
4168e8e46ccSJohn Baldwin 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
4178e8e46ccSJohn Baldwin 
4188e8e46ccSJohn Baldwin 	/* PCI interface */
4198e8e46ccSJohn Baldwin 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
4208e8e46ccSJohn Baldwin 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
4218e8e46ccSJohn Baldwin 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
4228e8e46ccSJohn Baldwin 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
4238e8e46ccSJohn Baldwin 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
4248e8e46ccSJohn Baldwin 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
425e7e2941bSJohn Baldwin 	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
426e7e2941bSJohn Baldwin 	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
4278e8e46ccSJohn Baldwin 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
4288e8e46ccSJohn Baldwin 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
4298e8e46ccSJohn Baldwin 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
4308e8e46ccSJohn Baldwin 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
431e7e2941bSJohn Baldwin 	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
432e7e2941bSJohn Baldwin 	DEVMETHOD(pci_alloc_msix,	vga_pci_alloc_msix),
433e7e2941bSJohn Baldwin 	DEVMETHOD(pci_remap_msix,	vga_pci_remap_msix),
434e7e2941bSJohn Baldwin 	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
435e7e2941bSJohn Baldwin 	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
436e7e2941bSJohn Baldwin 	DEVMETHOD(pci_msix_count,	vga_pci_msix_count),
4378e8e46ccSJohn Baldwin 
4388e8e46ccSJohn Baldwin 	{ 0, 0 }
4398e8e46ccSJohn Baldwin };
4408e8e46ccSJohn Baldwin 
4418e8e46ccSJohn Baldwin static driver_t vga_pci_driver = {
4428e8e46ccSJohn Baldwin 	"vgapci",
4438e8e46ccSJohn Baldwin 	vga_pci_methods,
444e7e2941bSJohn Baldwin 	sizeof(struct vga_pci_softc),
4458e8e46ccSJohn Baldwin };
4468e8e46ccSJohn Baldwin 
4478e8e46ccSJohn Baldwin static devclass_t vga_devclass;
4488e8e46ccSJohn Baldwin 
4498e8e46ccSJohn Baldwin DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
450