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
29 * $FreeBSD: src/sys/dev/pci/vga_pci.c,v 1.5.8.1 2009/04/15 03:14:26 kensmith Exp $
33 * Simple driver for PCI VGA display devices. Drivers such as agp(4) and
34 * drm(4) should attach as children of this device.
36 * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
40 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
48 #include <machine/pmap.h>
50 #include <bus/pci/pcireg.h>
51 #include <bus/pci/pcivar.h>
54 struct resource *vr_res;
58 struct vga_pci_softc {
59 device_t vga_msi_child; /* Child driver using MSI. */
60 struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
61 struct vga_resource vga_bios;
66 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
67 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
68 int type, int *rid, u_long start, u_long end, u_long count, u_int flags,
70 static int vga_pci_release_resource(device_t dev, device_t child, int type,
71 int rid, struct resource *r);
73 int vga_pci_default_unit = -1;
74 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
75 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RD,
76 &vga_pci_default_unit, -1, "Default VGA-compatible display");
79 vga_pci_is_boot_display(device_t dev)
83 * Return true if the given device is the default display used
87 (pci_get_class(dev) == PCIC_DISPLAY ||
88 (pci_get_class(dev) == PCIC_OLD &&
89 pci_get_subclass(dev) == PCIS_OLD_VGA)) &&
90 device_get_unit(dev) == vga_pci_default_unit);
94 vga_pci_map_bios(device_t dev, size_t *size)
99 if (vga_pci_is_boot_display(dev)) {
101 * On x86, the System BIOS copy the default display
102 * device's Video BIOS at a fixed location in system
103 * memory (0xC0000, 128 kBytes long) at boot time.
105 * We use this copy for the default boot device, because
106 * the original ROM may not be valid after boot.
109 *size = VGA_PCI_BIOS_SHADOW_SIZE;
110 return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
114 res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
115 ~0ul, 1, RF_ACTIVE, -1);
120 *size = rman_get_size(res);
121 return (rman_get_virtual(res));
125 vga_pci_unmap_bios(device_t dev, void *bios)
127 struct vga_resource *vr;
133 if (vga_pci_is_boot_display(dev)) {
134 /* We mapped the BIOS shadow copy located at 0xC0000. */
135 pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
141 * Look up the PCIR_BIOS resource in our softc. It should match
142 * the address we returned previously.
144 vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
145 KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
146 KASSERT(rman_get_virtual(vr->vr_res) == bios,
147 ("vga_pci_unmap_bios: mismatch"));
148 vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
153 vga_pci_probe(device_t dev)
159 switch (pci_get_class(dev)) {
163 if (pci_get_subclass(dev) != PCIS_OLD_VGA)
170 /* Probe default display. */
171 unit = device_get_unit(dev);
172 bdev = device_get_parent(device_get_parent(dev));
173 bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2);
174 if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0)
175 vga_pci_default_unit = unit;
176 if (vga_pci_default_unit == unit)
177 device_set_flags(dev, 1);
179 device_set_desc(dev, "VGA-compatible display");
180 return (BUS_PROBE_GENERIC);
184 vga_pci_attach(device_t dev)
187 bus_generic_probe(dev);
189 /* Always create a drm child for now to make it easier on drm. */
190 device_add_child(dev, "drm", -1);
191 device_add_child(dev, "drmn", -1);
192 bus_generic_attach(dev);
197 vga_pci_suspend(device_t dev)
200 return (bus_generic_suspend(dev));
204 vga_pci_resume(device_t dev)
207 return (bus_generic_resume(dev));
213 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
216 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
220 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
226 static struct vga_resource *
227 lookup_res(struct vga_pci_softc *sc, int rid)
231 if (rid == PCIR_BIOS)
232 return (&sc->vga_bios);
233 bar = PCI_RID2BAR(rid);
234 if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
235 return (&sc->vga_bars[bar]);
239 static struct resource *
240 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
241 u_long start, u_long end, u_long count, u_int flags, int cpuid __unused)
243 struct vga_resource *vr;
249 * For BARs, we cache the resource so that we only allocate it
250 * from the PCI bus once.
252 vr = lookup_res(device_get_softc(dev), *rid);
255 if (vr->vr_res == NULL)
256 vr->vr_res = bus_alloc_resource(dev, type, rid, start,
258 if (vr->vr_res != NULL)
262 return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
266 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
269 struct vga_resource *vr;
275 * Stop caching the resource when refs drops to 0
277 vr = lookup_res(device_get_softc(dev), rid);
278 if (vr && vr->vr_refs > 0) {
279 if (--vr->vr_refs > 0)
287 return (bus_release_resource(dev, type, rid, r));
293 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
296 return (pci_read_config(dev, reg, width));
300 vga_pci_write_config(device_t dev, device_t child, int reg,
301 uint32_t val, int width)
304 pci_write_config(dev, reg, val, width);
308 vga_pci_enable_busmaster(device_t dev, device_t child)
311 return (pci_enable_busmaster(dev));
315 vga_pci_disable_busmaster(device_t dev, device_t child)
318 return (pci_disable_busmaster(dev));
322 vga_pci_enable_io(device_t dev, device_t child, int space)
325 device_printf(dev, "child %s requested pci_enable_io\n",
326 device_get_nameunit(child));
327 return (pci_enable_io(dev, space));
331 vga_pci_disable_io(device_t dev, device_t child, int space)
334 device_printf(dev, "child %s requested pci_disable_io\n",
335 device_get_nameunit(child));
336 return (pci_disable_io(dev, space));
340 vga_pci_set_powerstate(device_t dev, device_t child, int state)
343 device_printf(dev, "child %s requested pci_set_powerstate\n",
344 device_get_nameunit(child));
345 return (pci_set_powerstate(dev, state));
349 vga_pci_get_powerstate(device_t dev, device_t child)
352 device_printf(dev, "child %s requested pci_get_powerstate\n",
353 device_get_nameunit(child));
354 return (pci_get_powerstate(dev));
358 vga_pci_assign_interrupt(device_t dev, device_t child)
361 device_printf(dev, "child %s requested pci_assign_interrupt\n",
362 device_get_nameunit(child));
363 return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
367 vga_pci_find_extcap(device_t dev, device_t child, int capability,
371 return (pci_find_extcap(dev, capability, capreg));
374 static device_method_t vga_pci_methods[] = {
375 /* Device interface */
376 DEVMETHOD(device_probe, vga_pci_probe),
377 DEVMETHOD(device_attach, vga_pci_attach),
378 DEVMETHOD(device_shutdown, bus_generic_shutdown),
379 DEVMETHOD(device_suspend, vga_pci_suspend),
380 DEVMETHOD(device_resume, vga_pci_resume),
383 DEVMETHOD(bus_read_ivar, vga_pci_read_ivar),
384 DEVMETHOD(bus_write_ivar, vga_pci_write_ivar),
385 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
386 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
388 DEVMETHOD(bus_alloc_resource, vga_pci_alloc_resource),
389 DEVMETHOD(bus_release_resource, vga_pci_release_resource),
390 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
391 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
394 DEVMETHOD(pci_read_config, vga_pci_read_config),
395 DEVMETHOD(pci_write_config, vga_pci_write_config),
396 DEVMETHOD(pci_enable_busmaster, vga_pci_enable_busmaster),
397 DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
398 DEVMETHOD(pci_enable_io, vga_pci_enable_io),
399 DEVMETHOD(pci_disable_io, vga_pci_disable_io),
400 DEVMETHOD(pci_get_powerstate, vga_pci_get_powerstate),
401 DEVMETHOD(pci_set_powerstate, vga_pci_set_powerstate),
402 DEVMETHOD(pci_assign_interrupt, vga_pci_assign_interrupt),
403 DEVMETHOD(pci_find_extcap, vga_pci_find_extcap),
408 static driver_t vga_pci_driver = {
411 sizeof(struct vga_pci_softc),
414 static devclass_t vga_devclass;
416 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, NULL, NULL);