xref: /dragonfly/sys/bus/pci/vga_pci.c (revision 926deccb)
1 /*-
2  * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/dev/pci/vga_pci.c,v 1.5.8.1 2009/04/15 03:14:26 kensmith Exp $
30  */
31 
32 /*
33  * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
34  * drm(4) should attach as children of this device.
35  *
36  * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
37  * in or rename it.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 
48 #include <machine/pmap.h>
49 
50 #include <bus/pci/pcireg.h>
51 #include <bus/pci/pcivar.h>
52 
53 SYSCTL_DECL(_hw_pci);
54 
55 int vga_pci_default_unit = -1;
56 TUNABLE_INT("hw.pci.default_vgapci_unit", &vga_pci_default_unit);
57 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RD,
58     &vga_pci_default_unit, -1, "Default VGA-compatible display");
59 
60 int
61 vga_pci_is_boot_display(device_t dev)
62 {
63 
64 	/*
65 	 * Return true if the given device is the default display used
66 	 * at boot time.
67 	 */
68 
69 	return (
70 	    (pci_get_class(dev) == PCIC_DISPLAY ||
71 	     (pci_get_class(dev) == PCIC_OLD &&
72 	      pci_get_subclass(dev) == PCIS_OLD_VGA)) &&
73 	    device_get_unit(dev) == vga_pci_default_unit);
74 }
75 
76 void *
77 vga_pci_map_bios(device_t dev, size_t *size)
78 {
79 	int rid;
80 	struct resource *res;
81 
82 	if (vga_pci_is_boot_display(dev)) {
83 		/*
84 		 * On x86, the System BIOS copy the default display
85 		 * device's Video BIOS at a fixed location in system
86 		 * memory (0xC0000, 128 kBytes long) at boot time.
87 		 *
88 		 * We use this copy for the default boot device, because
89 		 * the original ROM may not be valid after boot.
90 		 */
91 
92 		*size = VGA_PCI_BIOS_SHADOW_SIZE;
93 		return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
94 	}
95 
96 	rid = PCIR_BIOS;
97 	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
98 	if (res == NULL) {
99 		return (NULL);
100 	}
101 
102 	*size = rman_get_size(res);
103 	return (rman_get_virtual(res));
104 }
105 
106 void
107 vga_pci_unmap_bios(device_t dev, void *bios)
108 {
109 	int rid;
110 	struct resource *res;
111 
112 	if (bios == NULL) {
113 		return;
114 	}
115 
116 	if (vga_pci_is_boot_display(dev)) {
117 		/* We mapped the BIOS shadow copy located at 0xC0000. */
118 		pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
119 
120 		return;
121 	}
122 
123 	/*
124 	 * FIXME: We returned only the virtual address of the resource
125 	 * to the caller. Now, to get the resource struct back, we
126 	 * allocate it again: the struct exists once in memory in
127 	 * device softc. Therefore, we release twice now to release the
128 	 * reference we just obtained to get the structure back and the
129 	 * caller's reference.
130 	 */
131 
132 	rid = PCIR_BIOS;
133 	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
134 
135 	KASSERT(res != NULL,
136 	    ("%s: Can't get BIOS resource back", __func__));
137 	KASSERT(bios == rman_get_virtual(res),
138 	    ("%s: Given BIOS address doesn't match "
139 	     "resource virtual address", __func__));
140 
141 	bus_release_resource(dev, SYS_RES_MEMORY, rid, bios);
142 	bus_release_resource(dev, SYS_RES_MEMORY, rid, bios);
143 }
144 
145 static int
146 vga_pci_probe(device_t dev)
147 {
148 	device_t bdev;
149 	int unit;
150 	uint16_t bctl;
151 
152 	switch (pci_get_class(dev)) {
153 	case PCIC_DISPLAY:
154 		break;
155 	case PCIC_OLD:
156 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
157 			return (ENXIO);
158 		break;
159 	default:
160 		return (ENXIO);
161 	}
162 
163 	/* Probe default display. */
164 	unit = device_get_unit(dev);
165 	bdev = device_get_parent(device_get_parent(dev));
166 	bctl = pci_read_config(bdev, PCIR_BRIDGECTL_1, 2);
167 	if (vga_pci_default_unit < 0 && (bctl & PCIB_BCR_VGA_ENABLE) != 0)
168 		vga_pci_default_unit = unit;
169 	if (vga_pci_default_unit == unit)
170 		device_set_flags(dev, 1);
171 
172 	device_set_desc(dev, "VGA-compatible display");
173 	return (BUS_PROBE_GENERIC);
174 }
175 
176 static int
177 vga_pci_attach(device_t dev)
178 {
179 
180 	bus_generic_probe(dev);
181 
182 	/* Always create a drm child for now to make it easier on drm. */
183 	device_add_child(dev, "drm", -1);
184 	device_add_child(dev, "drmn", -1);
185 	bus_generic_attach(dev);
186 	return (0);
187 }
188 
189 static int
190 vga_pci_suspend(device_t dev)
191 {
192 
193 	return (bus_generic_suspend(dev));
194 }
195 
196 static int
197 vga_pci_resume(device_t dev)
198 {
199 
200 	return (bus_generic_resume(dev));
201 }
202 
203 /* Bus interface. */
204 
205 static int
206 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
207 {
208 
209 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
210 }
211 
212 static int
213 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
214 {
215 
216 	return (EINVAL);
217 }
218 
219 static struct resource *
220 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
221     u_long start, u_long end, u_long count, u_int flags, int cpuid __unused)
222 {
223 
224 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
225 }
226 
227 static int
228 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
229     struct resource *r)
230 {
231 
232 	return (bus_release_resource(dev, type, rid, r));
233 }
234 
235 /* PCI interface. */
236 
237 static uint32_t
238 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
239 {
240 
241 	return (pci_read_config(dev, reg, width));
242 }
243 
244 static void
245 vga_pci_write_config(device_t dev, device_t child, int reg,
246     uint32_t val, int width)
247 {
248 
249 	pci_write_config(dev, reg, val, width);
250 }
251 
252 static int
253 vga_pci_enable_busmaster(device_t dev, device_t child)
254 {
255 
256 	device_printf(dev, "child %s requested pci_enable_busmaster\n",
257 	    device_get_nameunit(child));
258 	return (pci_enable_busmaster(dev));
259 }
260 
261 static int
262 vga_pci_disable_busmaster(device_t dev, device_t child)
263 {
264 
265 	device_printf(dev, "child %s requested pci_disable_busmaster\n",
266 	    device_get_nameunit(child));
267 	return (pci_disable_busmaster(dev));
268 }
269 
270 static int
271 vga_pci_enable_io(device_t dev, device_t child, int space)
272 {
273 
274 	device_printf(dev, "child %s requested pci_enable_io\n",
275 	    device_get_nameunit(child));
276 	return (pci_enable_io(dev, space));
277 }
278 
279 static int
280 vga_pci_disable_io(device_t dev, device_t child, int space)
281 {
282 
283 	device_printf(dev, "child %s requested pci_disable_io\n",
284 	    device_get_nameunit(child));
285 	return (pci_disable_io(dev, space));
286 }
287 
288 static int
289 vga_pci_set_powerstate(device_t dev, device_t child, int state)
290 {
291 
292 	device_printf(dev, "child %s requested pci_set_powerstate\n",
293 	    device_get_nameunit(child));
294 	return (pci_set_powerstate(dev, state));
295 }
296 
297 static int
298 vga_pci_get_powerstate(device_t dev, device_t child)
299 {
300 
301 	device_printf(dev, "child %s requested pci_get_powerstate\n",
302 	    device_get_nameunit(child));
303 	return (pci_get_powerstate(dev));
304 }
305 
306 static int
307 vga_pci_assign_interrupt(device_t dev, device_t child)
308 {
309 
310 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
311 	    device_get_nameunit(child));
312 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
313 }
314 
315 static int
316 vga_pci_find_extcap(device_t dev, device_t child, int capability,
317     int *capreg)
318 {
319 
320 	return (pci_find_extcap(dev, capability, capreg));
321 }
322 
323 static device_method_t vga_pci_methods[] = {
324 	/* Device interface */
325 	DEVMETHOD(device_probe,		vga_pci_probe),
326 	DEVMETHOD(device_attach,	vga_pci_attach),
327 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
328 	DEVMETHOD(device_suspend,	vga_pci_suspend),
329 	DEVMETHOD(device_resume,	vga_pci_resume),
330 
331 	/* Bus interface */
332 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
333 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
334 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
335 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
336 
337 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
338 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
339 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
340 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
341 
342 	/* PCI interface */
343 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
344 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
345 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
346 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
347 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
348 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
349 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
350 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
351 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
352 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
353 
354 	DEVMETHOD_END
355 };
356 
357 static driver_t vga_pci_driver = {
358 	"vgapci",
359 	vga_pci_methods,
360 	1,
361 };
362 
363 static devclass_t vga_devclass;
364 
365 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, NULL, NULL);
366