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_res[PCIR_MAX_BAR_0 + 1];
64 int vga_pci_default_unit = -1;
65 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
66 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
67 &vga_pci_default_unit, -1, "Default VGA-compatible display");
70 vga_pci_probe(device_t dev)
76 switch (pci_get_class(dev)) {
80 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
87 /* Probe default display. */
88 unit = device_get_unit(dev);
89 bdev = device_get_parent(device_get_parent(dev));
90 bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2);
91 if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0)
92 vga_pci_default_unit = unit;
93 if (vga_pci_default_unit == unit)
94 device_set_flags(dev, 1);
96 device_set_desc(dev, "VGA-compatible display");
97 return (BUS_PROBE_GENERIC);
101 vga_pci_attach(device_t dev)
104 bus_generic_probe(dev);
106 /* Always create a drm child for now to make it easier on drm. */
107 device_add_child(dev, "drm", -1);
108 bus_generic_attach(dev);
113 vga_pci_suspend(device_t dev)
116 return (bus_generic_suspend(dev));
120 vga_pci_resume(device_t dev)
123 return (bus_generic_resume(dev));
129 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
132 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
136 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
143 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
144 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
147 return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
148 filter, intr, arg, cookiep));
152 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
155 return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
158 static struct resource *
159 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
160 u_long start, u_long end, u_long count, u_int flags)
162 struct vga_pci_softc *sc;
169 * For BARs, we cache the resource so that we only allocate it
170 * from the PCI bus once.
172 bar = PCI_RID2BAR(*rid);
173 if (bar < 0 || bar > PCIR_MAX_BAR_0)
175 sc = device_get_softc(dev);
176 if (sc->vga_res[bar].vr_res == NULL)
177 sc->vga_res[bar].vr_res = bus_alloc_resource(dev, type,
178 rid, start, end, count, flags);
179 if (sc->vga_res[bar].vr_res != NULL)
180 sc->vga_res[bar].vr_refs++;
181 return (sc->vga_res[bar].vr_res);
183 return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
187 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
190 struct vga_pci_softc *sc;
197 * For BARs, we release the resource from the PCI bus
198 * when the last child reference goes away.
200 bar = PCI_RID2BAR(rid);
201 if (bar < 0 || bar > PCIR_MAX_BAR_0)
203 sc = device_get_softc(dev);
204 if (sc->vga_res[bar].vr_res == NULL)
206 KASSERT(sc->vga_res[bar].vr_res == r,
207 ("vga_pci resource mismatch"));
208 if (sc->vga_res[bar].vr_refs > 1) {
209 sc->vga_res[bar].vr_refs--;
212 KASSERT(sc->vga_res[bar].vr_refs > 0,
213 ("vga_pci resource reference count underflow"));
214 error = bus_release_resource(dev, type, rid, r);
216 sc->vga_res[bar].vr_res = NULL;
217 sc->vga_res[bar].vr_refs = 0;
222 return (bus_release_resource(dev, type, rid, r));
228 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
231 return (pci_read_config(dev, reg, width));
235 vga_pci_write_config(device_t dev, device_t child, int reg,
236 uint32_t val, int width)
239 pci_write_config(dev, reg, val, width);
243 vga_pci_enable_busmaster(device_t dev, device_t child)
246 return (pci_enable_busmaster(dev));
250 vga_pci_disable_busmaster(device_t dev, device_t child)
253 return (pci_disable_busmaster(dev));
257 vga_pci_enable_io(device_t dev, device_t child, int space)
260 device_printf(dev, "child %s requested pci_enable_io\n",
261 device_get_nameunit(child));
262 return (pci_enable_io(dev, space));
266 vga_pci_disable_io(device_t dev, device_t child, int space)
269 device_printf(dev, "child %s requested pci_disable_io\n",
270 device_get_nameunit(child));
271 return (pci_disable_io(dev, space));
275 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
278 return (pci_get_vpd_ident(dev, identptr));
282 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
286 return (pci_get_vpd_readonly(dev, kw, vptr));
290 vga_pci_set_powerstate(device_t dev, device_t child, int state)
293 device_printf(dev, "child %s requested pci_set_powerstate\n",
294 device_get_nameunit(child));
295 return (pci_set_powerstate(dev, state));
299 vga_pci_get_powerstate(device_t dev, device_t child)
302 device_printf(dev, "child %s requested pci_get_powerstate\n",
303 device_get_nameunit(child));
304 return (pci_get_powerstate(dev));
308 vga_pci_assign_interrupt(device_t dev, device_t child)
311 device_printf(dev, "child %s requested pci_assign_interrupt\n",
312 device_get_nameunit(child));
313 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
317 vga_pci_find_extcap(device_t dev, device_t child, int capability,
321 return (pci_find_extcap(dev, capability, capreg));
325 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
327 struct vga_pci_softc *sc;
330 sc = device_get_softc(dev);
331 if (sc->vga_msi_child != NULL)
333 error = pci_alloc_msi(dev, count);
335 sc->vga_msi_child = child;
340 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
342 struct vga_pci_softc *sc;
345 sc = device_get_softc(dev);
346 if (sc->vga_msi_child != NULL)
348 error = pci_alloc_msix(dev, count);
350 sc->vga_msi_child = child;
355 vga_pci_remap_msix(device_t dev, device_t child, int count,
356 const u_int *vectors)
358 struct vga_pci_softc *sc;
360 sc = device_get_softc(dev);
361 if (sc->vga_msi_child != child)
363 return (pci_remap_msix(dev, count, vectors));
367 vga_pci_release_msi(device_t dev, device_t child)
369 struct vga_pci_softc *sc;
372 sc = device_get_softc(dev);
373 if (sc->vga_msi_child != child)
375 error = pci_release_msi(dev);
377 sc->vga_msi_child = NULL;
382 vga_pci_msi_count(device_t dev, device_t child)
385 return (pci_msi_count(dev));
389 vga_pci_msix_count(device_t dev, device_t child)
392 return (pci_msix_count(dev));
395 static device_method_t vga_pci_methods[] = {
396 /* Device interface */
397 DEVMETHOD(device_probe, vga_pci_probe),
398 DEVMETHOD(device_attach, vga_pci_attach),
399 DEVMETHOD(device_shutdown, bus_generic_shutdown),
400 DEVMETHOD(device_suspend, vga_pci_suspend),
401 DEVMETHOD(device_resume, vga_pci_resume),
404 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar),
405 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar),
406 DEVMETHOD(bus_setup_intr, vga_pci_setup_intr),
407 DEVMETHOD(bus_teardown_intr, vga_pci_teardown_intr),
408 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource),
409 DEVMETHOD(bus_release_resource, vga_pci_release_resource),
410 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
411 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
414 DEVMETHOD(pci_read_config, vga_pci_read_config),
415 DEVMETHOD(pci_write_config, vga_pci_write_config),
416 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster),
417 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
418 DEVMETHOD(pci_enable_io, vga_pci_enable_io),
419 DEVMETHOD(pci_disable_io, vga_pci_disable_io),
420 DEVMETHOD(pci_get_vpd_ident, vga_pci_get_vpd_ident),
421 DEVMETHOD(pci_get_vpd_readonly, vga_pci_get_vpd_readonly),
422 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate),
423 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate),
424 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt),
425 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap),
426 DEVMETHOD(pci_alloc_msi, vga_pci_alloc_msi),
427 DEVMETHOD(pci_alloc_msix, vga_pci_alloc_msix),
428 DEVMETHOD(pci_remap_msix, vga_pci_remap_msix),
429 DEVMETHOD(pci_release_msi, vga_pci_release_msi),
430 DEVMETHOD(pci_msi_count, vga_pci_msi_count),
431 DEVMETHOD(pci_msix_count, vga_pci_msix_count),
436 static driver_t vga_pci_driver = {
439 sizeof(struct vga_pci_softc),
442 static devclass_t vga_devclass;
444 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);