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. */ 599f905dafSJohn Baldwin struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1]; 609f905dafSJohn Baldwin struct vga_resource vga_bios; 61e7e2941bSJohn Baldwin }; 62e7e2941bSJohn Baldwin 633219f535SJung-uk Kim SYSCTL_DECL(_hw_pci); 643219f535SJung-uk Kim 653219f535SJung-uk Kim int vga_pci_default_unit = -1; 663219f535SJung-uk Kim TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit); 672259d74cSJung-uk Kim SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN, 683219f535SJung-uk Kim &vga_pci_default_unit, -1, "Default VGA-compatible display"); 693219f535SJung-uk Kim 708e8e46ccSJohn Baldwin static int 718e8e46ccSJohn Baldwin vga_pci_probe(device_t dev) 728e8e46ccSJohn Baldwin { 733219f535SJung-uk Kim device_t bdev; 743219f535SJung-uk Kim int unit; 753219f535SJung-uk Kim uint16_t bctl; 768e8e46ccSJohn Baldwin 778e8e46ccSJohn Baldwin switch (pci_get_class(dev)) { 788e8e46ccSJohn Baldwin case PCIC_DISPLAY: 798e8e46ccSJohn Baldwin break; 808e8e46ccSJohn Baldwin case PCIC_OLD: 818e8e46ccSJohn Baldwin if (pci_get_subclass(dev) != PCIS_OLD_VGA) 828e8e46ccSJohn Baldwin return (ENXIO); 838e8e46ccSJohn Baldwin break; 848e8e46ccSJohn Baldwin default: 858e8e46ccSJohn Baldwin return (ENXIO); 868e8e46ccSJohn Baldwin } 873219f535SJung-uk Kim 883219f535SJung-uk Kim /* Probe default display. */ 893219f535SJung-uk Kim unit = device_get_unit(dev); 903219f535SJung-uk Kim bdev = device_get_parent(device_get_parent(dev)); 913219f535SJung-uk Kim bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2); 923219f535SJung-uk Kim if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0) 933219f535SJung-uk Kim vga_pci_default_unit = unit; 943219f535SJung-uk Kim if (vga_pci_default_unit == unit) 953219f535SJung-uk Kim device_set_flags(dev, 1); 963219f535SJung-uk Kim 978e8e46ccSJohn Baldwin device_set_desc(dev, "VGA-compatible display"); 98be7ccc4bSJohn Baldwin return (BUS_PROBE_GENERIC); 998e8e46ccSJohn Baldwin } 1008e8e46ccSJohn Baldwin 1018e8e46ccSJohn Baldwin static int 1028e8e46ccSJohn Baldwin vga_pci_attach(device_t dev) 1038e8e46ccSJohn Baldwin { 1048e8e46ccSJohn Baldwin 1058e8e46ccSJohn Baldwin bus_generic_probe(dev); 1068e8e46ccSJohn Baldwin 1078e8e46ccSJohn Baldwin /* Always create a drm child for now to make it easier on drm. */ 1088e8e46ccSJohn Baldwin device_add_child(dev, "drm", -1); 1093c216b73SKonstantin Belousov device_add_child(dev, "drmn", -1); 1108e8e46ccSJohn Baldwin bus_generic_attach(dev); 1118e8e46ccSJohn Baldwin return (0); 1128e8e46ccSJohn Baldwin } 1138e8e46ccSJohn Baldwin 1148e8e46ccSJohn Baldwin static int 1158e8e46ccSJohn Baldwin vga_pci_suspend(device_t dev) 1168e8e46ccSJohn Baldwin { 1172259d74cSJung-uk Kim 118b66e2b8eSJung-uk Kim return (bus_generic_suspend(dev)); 1198e8e46ccSJohn Baldwin } 1208e8e46ccSJohn Baldwin 1218e8e46ccSJohn Baldwin static int 1228e8e46ccSJohn Baldwin vga_pci_resume(device_t dev) 1238e8e46ccSJohn Baldwin { 1248e8e46ccSJohn Baldwin 1258e8e46ccSJohn Baldwin return (bus_generic_resume(dev)); 1268e8e46ccSJohn Baldwin } 1278e8e46ccSJohn Baldwin 1288e8e46ccSJohn Baldwin /* Bus interface. */ 1298e8e46ccSJohn Baldwin 1308e8e46ccSJohn Baldwin static int 1318e8e46ccSJohn Baldwin vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1328e8e46ccSJohn Baldwin { 1338e8e46ccSJohn Baldwin 1348e8e46ccSJohn Baldwin return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result)); 1358e8e46ccSJohn Baldwin } 1368e8e46ccSJohn Baldwin 1378e8e46ccSJohn Baldwin static int 1388e8e46ccSJohn Baldwin vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1398e8e46ccSJohn Baldwin { 1408e8e46ccSJohn Baldwin 1418e8e46ccSJohn Baldwin return (EINVAL); 1428e8e46ccSJohn Baldwin } 1438e8e46ccSJohn Baldwin 1446be0323dSRobert Noland static int 1456be0323dSRobert Noland vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 1466be0323dSRobert Noland int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 1476be0323dSRobert Noland void **cookiep) 1486be0323dSRobert Noland { 1496be0323dSRobert Noland return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags, 1506be0323dSRobert Noland filter, intr, arg, cookiep)); 1516be0323dSRobert Noland } 1526be0323dSRobert Noland 1536be0323dSRobert Noland static int 1546be0323dSRobert Noland vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq, 1556be0323dSRobert Noland void *cookie) 1566be0323dSRobert Noland { 1576be0323dSRobert Noland return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie)); 1586be0323dSRobert Noland } 1596be0323dSRobert Noland 1609f905dafSJohn Baldwin static struct vga_resource * 1619f905dafSJohn Baldwin lookup_res(struct vga_pci_softc *sc, int rid) 1629f905dafSJohn Baldwin { 1639f905dafSJohn Baldwin int bar; 1649f905dafSJohn Baldwin 1659f905dafSJohn Baldwin if (rid == PCIR_BIOS) 1669f905dafSJohn Baldwin return (&sc->vga_bios); 1679f905dafSJohn Baldwin bar = PCI_RID2BAR(rid); 1689f905dafSJohn Baldwin if (bar >= 0 && bar <= PCIR_MAX_BAR_0) 1699f905dafSJohn Baldwin return (&sc->vga_bars[bar]); 1709f905dafSJohn Baldwin return (NULL); 1719f905dafSJohn Baldwin } 1729f905dafSJohn Baldwin 1738e8e46ccSJohn Baldwin static struct resource * 1748e8e46ccSJohn Baldwin vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, 1758e8e46ccSJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 1768e8e46ccSJohn Baldwin { 1779f905dafSJohn Baldwin struct vga_resource *vr; 1788e8e46ccSJohn Baldwin 179e8b145c2SJohn Baldwin switch (type) { 180e8b145c2SJohn Baldwin case SYS_RES_MEMORY: 181e8b145c2SJohn Baldwin case SYS_RES_IOPORT: 182e8b145c2SJohn Baldwin /* 183e8b145c2SJohn Baldwin * For BARs, we cache the resource so that we only allocate it 184e8b145c2SJohn Baldwin * from the PCI bus once. 185e8b145c2SJohn Baldwin */ 1869f905dafSJohn Baldwin vr = lookup_res(device_get_softc(dev), *rid); 1879f905dafSJohn Baldwin if (vr == NULL) 188e8b145c2SJohn Baldwin return (NULL); 1899f905dafSJohn Baldwin if (vr->vr_res == NULL) 1909f905dafSJohn Baldwin vr->vr_res = bus_alloc_resource(dev, type, rid, start, 1919f905dafSJohn Baldwin end, count, flags); 1929f905dafSJohn Baldwin if (vr->vr_res != NULL) 1939f905dafSJohn Baldwin vr->vr_refs++; 1949f905dafSJohn Baldwin return (vr->vr_res); 195e8b145c2SJohn Baldwin } 1968e8e46ccSJohn Baldwin return (bus_alloc_resource(dev, type, rid, start, end, count, flags)); 1978e8e46ccSJohn Baldwin } 1988e8e46ccSJohn Baldwin 1998e8e46ccSJohn Baldwin static int 2008e8e46ccSJohn Baldwin vga_pci_release_resource(device_t dev, device_t child, int type, int rid, 2018e8e46ccSJohn Baldwin struct resource *r) 2028e8e46ccSJohn Baldwin { 2039f905dafSJohn Baldwin struct vga_resource *vr; 2049f905dafSJohn Baldwin int error; 205e8b145c2SJohn Baldwin 206e8b145c2SJohn Baldwin switch (type) { 207e8b145c2SJohn Baldwin case SYS_RES_MEMORY: 208e8b145c2SJohn Baldwin case SYS_RES_IOPORT: 209e8b145c2SJohn Baldwin /* 210e8b145c2SJohn Baldwin * For BARs, we release the resource from the PCI bus 211e8b145c2SJohn Baldwin * when the last child reference goes away. 212e8b145c2SJohn Baldwin */ 2139f905dafSJohn Baldwin vr = lookup_res(device_get_softc(dev), rid); 2149f905dafSJohn Baldwin if (vr == NULL) 215e8b145c2SJohn Baldwin return (EINVAL); 2169f905dafSJohn Baldwin if (vr->vr_res == NULL) 217e8b145c2SJohn Baldwin return (EINVAL); 2189f905dafSJohn Baldwin KASSERT(vr->vr_res == r, ("vga_pci resource mismatch")); 2199f905dafSJohn Baldwin if (vr->vr_refs > 1) { 2209f905dafSJohn Baldwin vr->vr_refs--; 221e8b145c2SJohn Baldwin return (0); 222e8b145c2SJohn Baldwin } 2239f905dafSJohn Baldwin KASSERT(vr->vr_refs > 0, 224e8b145c2SJohn Baldwin ("vga_pci resource reference count underflow")); 225e8b145c2SJohn Baldwin error = bus_release_resource(dev, type, rid, r); 226e8b145c2SJohn Baldwin if (error == 0) { 2279f905dafSJohn Baldwin vr->vr_res = NULL; 2289f905dafSJohn Baldwin vr->vr_refs = 0; 229e8b145c2SJohn Baldwin } 230e8b145c2SJohn Baldwin return (error); 231e8b145c2SJohn Baldwin } 2328e8e46ccSJohn Baldwin 2338e8e46ccSJohn Baldwin return (bus_release_resource(dev, type, rid, r)); 2348e8e46ccSJohn Baldwin } 2358e8e46ccSJohn Baldwin 2368e8e46ccSJohn Baldwin /* PCI interface. */ 2378e8e46ccSJohn Baldwin 2388e8e46ccSJohn Baldwin static uint32_t 2398e8e46ccSJohn Baldwin vga_pci_read_config(device_t dev, device_t child, int reg, int width) 2408e8e46ccSJohn Baldwin { 2418e8e46ccSJohn Baldwin 2428e8e46ccSJohn Baldwin return (pci_read_config(dev, reg, width)); 2438e8e46ccSJohn Baldwin } 2448e8e46ccSJohn Baldwin 2458e8e46ccSJohn Baldwin static void 2468e8e46ccSJohn Baldwin vga_pci_write_config(device_t dev, device_t child, int reg, 2478e8e46ccSJohn Baldwin uint32_t val, int width) 2488e8e46ccSJohn Baldwin { 2498e8e46ccSJohn Baldwin 2508e8e46ccSJohn Baldwin pci_write_config(dev, reg, val, width); 2518e8e46ccSJohn Baldwin } 2528e8e46ccSJohn Baldwin 2538e8e46ccSJohn Baldwin static int 2548e8e46ccSJohn Baldwin vga_pci_enable_busmaster(device_t dev, device_t child) 2558e8e46ccSJohn Baldwin { 2568e8e46ccSJohn Baldwin 2578e8e46ccSJohn Baldwin return (pci_enable_busmaster(dev)); 2588e8e46ccSJohn Baldwin } 2598e8e46ccSJohn Baldwin 2608e8e46ccSJohn Baldwin static int 2618e8e46ccSJohn Baldwin vga_pci_disable_busmaster(device_t dev, device_t child) 2628e8e46ccSJohn Baldwin { 2638e8e46ccSJohn Baldwin 2648e8e46ccSJohn Baldwin return (pci_disable_busmaster(dev)); 2658e8e46ccSJohn Baldwin } 2668e8e46ccSJohn Baldwin 2678e8e46ccSJohn Baldwin static int 2688e8e46ccSJohn Baldwin vga_pci_enable_io(device_t dev, device_t child, int space) 2698e8e46ccSJohn Baldwin { 2708e8e46ccSJohn Baldwin 2718e8e46ccSJohn Baldwin device_printf(dev, "child %s requested pci_enable_io\n", 2728e8e46ccSJohn Baldwin device_get_nameunit(child)); 2738e8e46ccSJohn Baldwin return (pci_enable_io(dev, space)); 2748e8e46ccSJohn Baldwin } 2758e8e46ccSJohn Baldwin 2768e8e46ccSJohn Baldwin static int 2778e8e46ccSJohn Baldwin vga_pci_disable_io(device_t dev, device_t child, int space) 2788e8e46ccSJohn Baldwin { 2798e8e46ccSJohn Baldwin 2808e8e46ccSJohn Baldwin device_printf(dev, "child %s requested pci_disable_io\n", 2818e8e46ccSJohn Baldwin device_get_nameunit(child)); 2828e8e46ccSJohn Baldwin return (pci_disable_io(dev, space)); 2838e8e46ccSJohn Baldwin } 2848e8e46ccSJohn Baldwin 2858e8e46ccSJohn Baldwin static int 286e7e2941bSJohn Baldwin vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr) 287e7e2941bSJohn Baldwin { 288e7e2941bSJohn Baldwin 289e7e2941bSJohn Baldwin return (pci_get_vpd_ident(dev, identptr)); 290e7e2941bSJohn Baldwin } 291e7e2941bSJohn Baldwin 292e7e2941bSJohn Baldwin static int 293e7e2941bSJohn Baldwin vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw, 294e7e2941bSJohn Baldwin const char **vptr) 295e7e2941bSJohn Baldwin { 296e7e2941bSJohn Baldwin 297e7e2941bSJohn Baldwin return (pci_get_vpd_readonly(dev, kw, vptr)); 298e7e2941bSJohn Baldwin } 299e7e2941bSJohn Baldwin 300e7e2941bSJohn Baldwin static int 3018e8e46ccSJohn Baldwin vga_pci_set_powerstate(device_t dev, device_t child, int state) 3028e8e46ccSJohn Baldwin { 3038e8e46ccSJohn Baldwin 3048e8e46ccSJohn Baldwin device_printf(dev, "child %s requested pci_set_powerstate\n", 3058e8e46ccSJohn Baldwin device_get_nameunit(child)); 3068e8e46ccSJohn Baldwin return (pci_set_powerstate(dev, state)); 3078e8e46ccSJohn Baldwin } 3088e8e46ccSJohn Baldwin 3098e8e46ccSJohn Baldwin static int 3108e8e46ccSJohn Baldwin vga_pci_get_powerstate(device_t dev, device_t child) 3118e8e46ccSJohn Baldwin { 3128e8e46ccSJohn Baldwin 3138e8e46ccSJohn Baldwin device_printf(dev, "child %s requested pci_get_powerstate\n", 3148e8e46ccSJohn Baldwin device_get_nameunit(child)); 3158e8e46ccSJohn Baldwin return (pci_get_powerstate(dev)); 3168e8e46ccSJohn Baldwin } 3178e8e46ccSJohn Baldwin 3188e8e46ccSJohn Baldwin static int 3198e8e46ccSJohn Baldwin vga_pci_assign_interrupt(device_t dev, device_t child) 3208e8e46ccSJohn Baldwin { 3218e8e46ccSJohn Baldwin 3228e8e46ccSJohn Baldwin device_printf(dev, "child %s requested pci_assign_interrupt\n", 3238e8e46ccSJohn Baldwin device_get_nameunit(child)); 3248e8e46ccSJohn Baldwin return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev)); 3258e8e46ccSJohn Baldwin } 3268e8e46ccSJohn Baldwin 3278e8e46ccSJohn Baldwin static int 328c668000bSJohn Baldwin vga_pci_find_cap(device_t dev, device_t child, int capability, 329c668000bSJohn Baldwin int *capreg) 330c668000bSJohn Baldwin { 331c668000bSJohn Baldwin 332c668000bSJohn Baldwin return (pci_find_cap(dev, capability, capreg)); 333c668000bSJohn Baldwin } 334c668000bSJohn Baldwin 335c668000bSJohn Baldwin static int 3368e8e46ccSJohn Baldwin vga_pci_find_extcap(device_t dev, device_t child, int capability, 3378e8e46ccSJohn Baldwin int *capreg) 3388e8e46ccSJohn Baldwin { 3398e8e46ccSJohn Baldwin 3408e8e46ccSJohn Baldwin return (pci_find_extcap(dev, capability, capreg)); 3418e8e46ccSJohn Baldwin } 3428e8e46ccSJohn Baldwin 343e7e2941bSJohn Baldwin static int 344c668000bSJohn Baldwin vga_pci_find_htcap(device_t dev, device_t child, int capability, 345c668000bSJohn Baldwin int *capreg) 346c668000bSJohn Baldwin { 347c668000bSJohn Baldwin 348c668000bSJohn Baldwin return (pci_find_htcap(dev, capability, capreg)); 349c668000bSJohn Baldwin } 350c668000bSJohn Baldwin 351c668000bSJohn Baldwin static int 352e7e2941bSJohn Baldwin vga_pci_alloc_msi(device_t dev, device_t child, int *count) 353e7e2941bSJohn Baldwin { 354e7e2941bSJohn Baldwin struct vga_pci_softc *sc; 355e7e2941bSJohn Baldwin int error; 356e7e2941bSJohn Baldwin 357e7e2941bSJohn Baldwin sc = device_get_softc(dev); 358e7e2941bSJohn Baldwin if (sc->vga_msi_child != NULL) 359e7e2941bSJohn Baldwin return (EBUSY); 360e7e2941bSJohn Baldwin error = pci_alloc_msi(dev, count); 361e7e2941bSJohn Baldwin if (error == 0) 362e7e2941bSJohn Baldwin sc->vga_msi_child = child; 363e7e2941bSJohn Baldwin return (error); 364e7e2941bSJohn Baldwin } 365e7e2941bSJohn Baldwin 366e7e2941bSJohn Baldwin static int 367e7e2941bSJohn Baldwin vga_pci_alloc_msix(device_t dev, device_t child, int *count) 368e7e2941bSJohn Baldwin { 369e7e2941bSJohn Baldwin struct vga_pci_softc *sc; 370e7e2941bSJohn Baldwin int error; 371e7e2941bSJohn Baldwin 372e7e2941bSJohn Baldwin sc = device_get_softc(dev); 373e7e2941bSJohn Baldwin if (sc->vga_msi_child != NULL) 374e7e2941bSJohn Baldwin return (EBUSY); 375e7e2941bSJohn Baldwin error = pci_alloc_msix(dev, count); 376e7e2941bSJohn Baldwin if (error == 0) 377e7e2941bSJohn Baldwin sc->vga_msi_child = child; 378e7e2941bSJohn Baldwin return (error); 379e7e2941bSJohn Baldwin } 380e7e2941bSJohn Baldwin 381e7e2941bSJohn Baldwin static int 382e7e2941bSJohn Baldwin vga_pci_remap_msix(device_t dev, device_t child, int count, 383e7e2941bSJohn Baldwin const u_int *vectors) 384e7e2941bSJohn Baldwin { 385e7e2941bSJohn Baldwin struct vga_pci_softc *sc; 386e7e2941bSJohn Baldwin 387e7e2941bSJohn Baldwin sc = device_get_softc(dev); 388e7e2941bSJohn Baldwin if (sc->vga_msi_child != child) 389e7e2941bSJohn Baldwin return (ENXIO); 390e7e2941bSJohn Baldwin return (pci_remap_msix(dev, count, vectors)); 391e7e2941bSJohn Baldwin } 392e7e2941bSJohn Baldwin 393e7e2941bSJohn Baldwin static int 394e7e2941bSJohn Baldwin vga_pci_release_msi(device_t dev, device_t child) 395e7e2941bSJohn Baldwin { 396e7e2941bSJohn Baldwin struct vga_pci_softc *sc; 397e7e2941bSJohn Baldwin int error; 398e7e2941bSJohn Baldwin 399e7e2941bSJohn Baldwin sc = device_get_softc(dev); 400e7e2941bSJohn Baldwin if (sc->vga_msi_child != child) 401e7e2941bSJohn Baldwin return (ENXIO); 402e7e2941bSJohn Baldwin error = pci_release_msi(dev); 403e7e2941bSJohn Baldwin if (error == 0) 404e7e2941bSJohn Baldwin sc->vga_msi_child = NULL; 405e7e2941bSJohn Baldwin return (error); 406e7e2941bSJohn Baldwin } 407e7e2941bSJohn Baldwin 408e7e2941bSJohn Baldwin static int 409e7e2941bSJohn Baldwin vga_pci_msi_count(device_t dev, device_t child) 410e7e2941bSJohn Baldwin { 411e7e2941bSJohn Baldwin 412e7e2941bSJohn Baldwin return (pci_msi_count(dev)); 413e7e2941bSJohn Baldwin } 414e7e2941bSJohn Baldwin 415e7e2941bSJohn Baldwin static int 416e7e2941bSJohn Baldwin vga_pci_msix_count(device_t dev, device_t child) 417e7e2941bSJohn Baldwin { 418e7e2941bSJohn Baldwin 419e7e2941bSJohn Baldwin return (pci_msix_count(dev)); 420e7e2941bSJohn Baldwin } 421e7e2941bSJohn Baldwin 422*5c818b67SKonstantin Belousov static bus_dma_tag_t 423*5c818b67SKonstantin Belousov vga_pci_get_dma_tag(device_t bus, device_t child) 424*5c818b67SKonstantin Belousov { 425*5c818b67SKonstantin Belousov 426*5c818b67SKonstantin Belousov return (bus_get_dma_tag(bus)); 427*5c818b67SKonstantin Belousov } 428*5c818b67SKonstantin Belousov 4298e8e46ccSJohn Baldwin static device_method_t vga_pci_methods[] = { 4308e8e46ccSJohn Baldwin /* Device interface */ 4318e8e46ccSJohn Baldwin DEVMETHOD(device_probe, vga_pci_probe), 4328e8e46ccSJohn Baldwin DEVMETHOD(device_attach, vga_pci_attach), 4338e8e46ccSJohn Baldwin DEVMETHOD(device_shutdown, bus_generic_shutdown), 4348e8e46ccSJohn Baldwin DEVMETHOD(device_suspend, vga_pci_suspend), 4358e8e46ccSJohn Baldwin DEVMETHOD(device_resume, vga_pci_resume), 4368e8e46ccSJohn Baldwin 4378e8e46ccSJohn Baldwin /* Bus interface */ 4388e8e46ccSJohn Baldwin DEVMETHOD(bus_read_ivar, vga_pci_read_ivar), 4398e8e46ccSJohn Baldwin DEVMETHOD(bus_write_ivar, vga_pci_write_ivar), 4406be0323dSRobert Noland DEVMETHOD(bus_setup_intr, vga_pci_setup_intr), 4416be0323dSRobert Noland DEVMETHOD(bus_teardown_intr, vga_pci_teardown_intr), 4428e8e46ccSJohn Baldwin DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource), 4438e8e46ccSJohn Baldwin DEVMETHOD(bus_release_resource, vga_pci_release_resource), 4448e8e46ccSJohn Baldwin DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 4458e8e46ccSJohn Baldwin DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 446*5c818b67SKonstantin Belousov DEVMETHOD(bus_get_dma_tag, vga_pci_get_dma_tag), 4478e8e46ccSJohn Baldwin 4488e8e46ccSJohn Baldwin /* PCI interface */ 4498e8e46ccSJohn Baldwin DEVMETHOD(pci_read_config, vga_pci_read_config), 4508e8e46ccSJohn Baldwin DEVMETHOD(pci_write_config, vga_pci_write_config), 4518e8e46ccSJohn Baldwin DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster), 4528e8e46ccSJohn Baldwin DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster), 4538e8e46ccSJohn Baldwin DEVMETHOD(pci_enable_io, vga_pci_enable_io), 4548e8e46ccSJohn Baldwin DEVMETHOD(pci_disable_io, vga_pci_disable_io), 455e7e2941bSJohn Baldwin DEVMETHOD(pci_get_vpd_ident, vga_pci_get_vpd_ident), 456e7e2941bSJohn Baldwin DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly), 4578e8e46ccSJohn Baldwin DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate), 4588e8e46ccSJohn Baldwin DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate), 4598e8e46ccSJohn Baldwin DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt), 460c668000bSJohn Baldwin DEVMETHOD(pci_find_cap, vga_pci_find_cap), 4618e8e46ccSJohn Baldwin DEVMETHOD(pci_find_extcap, vga_pci_find_extcap), 462c668000bSJohn Baldwin DEVMETHOD(pci_find_htcap, vga_pci_find_htcap), 463e7e2941bSJohn Baldwin DEVMETHOD(pci_alloc_msi, vga_pci_alloc_msi), 464e7e2941bSJohn Baldwin DEVMETHOD(pci_alloc_msix, vga_pci_alloc_msix), 465e7e2941bSJohn Baldwin DEVMETHOD(pci_remap_msix, vga_pci_remap_msix), 466e7e2941bSJohn Baldwin DEVMETHOD(pci_release_msi, vga_pci_release_msi), 467e7e2941bSJohn Baldwin DEVMETHOD(pci_msi_count, vga_pci_msi_count), 468e7e2941bSJohn Baldwin DEVMETHOD(pci_msix_count, vga_pci_msix_count), 4698e8e46ccSJohn Baldwin 4708e8e46ccSJohn Baldwin { 0, 0 } 4718e8e46ccSJohn Baldwin }; 4728e8e46ccSJohn Baldwin 4738e8e46ccSJohn Baldwin static driver_t vga_pci_driver = { 4748e8e46ccSJohn Baldwin "vgapci", 4758e8e46ccSJohn Baldwin vga_pci_methods, 476e7e2941bSJohn Baldwin sizeof(struct vga_pci_softc), 4778e8e46ccSJohn Baldwin }; 4788e8e46ccSJohn Baldwin 4798e8e46ccSJohn Baldwin static devclass_t vga_devclass; 4808e8e46ccSJohn Baldwin 4818e8e46ccSJohn Baldwin DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0); 482