2 * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and
35 * drm(4) should attach as children of this device.
37 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
41 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
53 struct resource *vr_res;
57 struct vga_pci_softc {
58 device_t vga_msi_child; /* Child driver using MSI. */
59 struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
60 struct vga_resource vga_bios;
65 int vga_pci_default_unit = -1;
66 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
67 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
68 &vga_pci_default_unit, -1, "Default VGA-compatible display");
71 vga_pci_probe(device_t dev)
77 switch (pci_get_class(dev)) {
81 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
88 /* Probe default display. */
89 unit = device_get_unit(dev);
90 bdev = device_get_parent(device_get_parent(dev));
91 bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2);
92 if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0)
93 vga_pci_default_unit = unit;
94 if (vga_pci_default_unit == unit)
95 device_set_flags(dev, 1);
97 device_set_desc(dev, "VGA-compatible display");
98 return (BUS_PROBE_GENERIC);
102 vga_pci_attach(device_t dev)
105 bus_generic_probe(dev);
107 /* Always create a drm child for now to make it easier on drm. */
108 device_add_child(dev, "drm", -1);
109 device_add_child(dev, "drmn", -1);
110 bus_generic_attach(dev);
115 vga_pci_suspend(device_t dev)
118 return (bus_generic_suspend(dev));
122 vga_pci_resume(device_t dev)
125 return (bus_generic_resume(dev));
131 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
134 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
138 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
145 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
146 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
149 return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
150 filter, intr, arg, cookiep));
154 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
157 return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
160 static struct vga_resource *
161 lookup_res(struct vga_pci_softc *sc, int rid)
165 if (rid == PCIR_BIOS)
166 return (&sc->vga_bios);
167 bar = PCI_RID2BAR(rid);
168 if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
169 return (&sc->vga_bars[bar]);
173 static struct resource *
174 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
175 u_long start, u_long end, u_long count, u_int flags)
177 struct vga_resource *vr;
183 * For BARs, we cache the resource so that we only allocate it
184 * from the PCI bus once.
186 vr = lookup_res(device_get_softc(dev), *rid);
189 if (vr->vr_res == NULL)
190 vr->vr_res = bus_alloc_resource(dev, type, rid, start,
192 if (vr->vr_res != NULL)
196 return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
200 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
203 struct vga_resource *vr;
210 * For BARs, we release the resource from the PCI bus
211 * when the last child reference goes away.
213 vr = lookup_res(device_get_softc(dev), rid);
216 if (vr->vr_res == NULL)
218 KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
219 if (vr->vr_refs > 1) {
223 KASSERT(vr->vr_refs > 0,
224 ("vga_pci resource reference count underflow"));
225 error = bus_release_resource(dev, type, rid, r);
233 return (bus_release_resource(dev, type, rid, r));
239 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
242 return (pci_read_config(dev, reg, width));
246 vga_pci_write_config(device_t dev, device_t child, int reg,
247 uint32_t val, int width)
250 pci_write_config(dev, reg, val, width);
254 vga_pci_enable_busmaster(device_t dev, device_t child)
257 return (pci_enable_busmaster(dev));
261 vga_pci_disable_busmaster(device_t dev, device_t child)
264 return (pci_disable_busmaster(dev));
268 vga_pci_enable_io(device_t dev, device_t child, int space)
271 device_printf(dev, "child %s requested pci_enable_io\n",
272 device_get_nameunit(child));
273 return (pci_enable_io(dev, space));
277 vga_pci_disable_io(device_t dev, device_t child, int space)
280 device_printf(dev, "child %s requested pci_disable_io\n",
281 device_get_nameunit(child));
282 return (pci_disable_io(dev, space));
286 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
289 return (pci_get_vpd_ident(dev, identptr));
293 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
297 return (pci_get_vpd_readonly(dev, kw, vptr));
301 vga_pci_set_powerstate(device_t dev, device_t child, int state)
304 device_printf(dev, "child %s requested pci_set_powerstate\n",
305 device_get_nameunit(child));
306 return (pci_set_powerstate(dev, state));
310 vga_pci_get_powerstate(device_t dev, device_t child)
313 device_printf(dev, "child %s requested pci_get_powerstate\n",
314 device_get_nameunit(child));
315 return (pci_get_powerstate(dev));
319 vga_pci_assign_interrupt(device_t dev, device_t child)
322 device_printf(dev, "child %s requested pci_assign_interrupt\n",
323 device_get_nameunit(child));
324 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
328 vga_pci_find_extcap(device_t dev, device_t child, int capability,
332 return (pci_find_extcap(dev, capability, capreg));
336 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
338 struct vga_pci_softc *sc;
341 sc = device_get_softc(dev);
342 if (sc->vga_msi_child != NULL)
344 error = pci_alloc_msi(dev, count);
346 sc->vga_msi_child = child;
351 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
353 struct vga_pci_softc *sc;
356 sc = device_get_softc(dev);
357 if (sc->vga_msi_child != NULL)
359 error = pci_alloc_msix(dev, count);
361 sc->vga_msi_child = child;
366 vga_pci_remap_msix(device_t dev, device_t child, int count,
367 const u_int *vectors)
369 struct vga_pci_softc *sc;
371 sc = device_get_softc(dev);
372 if (sc->vga_msi_child != child)
374 return (pci_remap_msix(dev, count, vectors));
378 vga_pci_release_msi(device_t dev, device_t child)
380 struct vga_pci_softc *sc;
383 sc = device_get_softc(dev);
384 if (sc->vga_msi_child != child)
386 error = pci_release_msi(dev);
388 sc->vga_msi_child = NULL;
393 vga_pci_msi_count(device_t dev, device_t child)
396 return (pci_msi_count(dev));
400 vga_pci_msix_count(device_t dev, device_t child)
403 return (pci_msix_count(dev));
407 vga_pci_get_dma_tag(device_t bus, device_t child)
410 return (bus_get_dma_tag(bus));
413 static device_method_t vga_pci_methods[] = {
414 /* Device interface */
415 DEVMETHOD(device_probe, vga_pci_probe),
416 DEVMETHOD(device_attach, vga_pci_attach),
417 DEVMETHOD(device_shutdown, bus_generic_shutdown),
418 DEVMETHOD(device_suspend, vga_pci_suspend),
419 DEVMETHOD(device_resume, vga_pci_resume),
422 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar),
423 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar),
424 DEVMETHOD(bus_setup_intr, vga_pci_setup_intr),
425 DEVMETHOD(bus_teardown_intr, vga_pci_teardown_intr),
426 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource),
427 DEVMETHOD(bus_release_resource, vga_pci_release_resource),
428 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
429 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
430 DEVMETHOD(bus_get_dma_tag, vga_pci_get_dma_tag),
433 DEVMETHOD(pci_read_config, vga_pci_read_config),
434 DEVMETHOD(pci_write_config, vga_pci_write_config),
435 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster),
436 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
437 DEVMETHOD(pci_enable_io, vga_pci_enable_io),
438 DEVMETHOD(pci_disable_io, vga_pci_disable_io),
439 DEVMETHOD(pci_get_vpd_ident, vga_pci_get_vpd_ident),
440 DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly),
441 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate),
442 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate),
443 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt),
444 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap),
445 DEVMETHOD(pci_alloc_msi, vga_pci_alloc_msi),
446 DEVMETHOD(pci_alloc_msix, vga_pci_alloc_msix),
447 DEVMETHOD(pci_remap_msix, vga_pci_remap_msix),
448 DEVMETHOD(pci_release_msi, vga_pci_release_msi),
449 DEVMETHOD(pci_msi_count, vga_pci_msi_count),
450 DEVMETHOD(pci_msix_count, vga_pci_msix_count),
455 static driver_t vga_pci_driver = {
458 sizeof(struct vga_pci_softc),
461 static devclass_t vga_devclass;
463 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);