xref: /dragonfly/sys/bus/pci/vga_pci.c (revision fa71f50a)
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 struct vga_resource {
54 	struct resource	*vr_res;
55 	int	vr_refs;
56 };
57 
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;
62 };
63 
64 SYSCTL_DECL(_hw_pci);
65 
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,
69     int cpuid);
70 static int	vga_pci_release_resource(device_t dev, device_t child, int type,
71     int rid, struct resource *r);
72 
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");
77 
78 int
79 vga_pci_is_boot_display(device_t dev)
80 {
81 
82 	/*
83 	 * Return true if the given device is the default display used
84 	 * at boot time.
85 	 */
86 	return (
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);
91 }
92 
93 void *
94 vga_pci_map_bios(device_t dev, size_t *size)
95 {
96 	int rid;
97 	struct resource *res;
98 
99 	if (vga_pci_is_boot_display(dev)) {
100 		/*
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.
104 		 *
105 		 * We use this copy for the default boot device, because
106 		 * the original ROM may not be valid after boot.
107 		 */
108 
109 		*size = VGA_PCI_BIOS_SHADOW_SIZE;
110 		return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
111 	}
112 
113 	rid = PCIR_BIOS;
114 	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
115 	    ~0ul, 1, RF_ACTIVE, -1);
116 	if (res == NULL) {
117 		return (NULL);
118 	}
119 
120 	*size = rman_get_size(res);
121 	return (rman_get_virtual(res));
122 }
123 
124 void
125 vga_pci_unmap_bios(device_t dev, void *bios)
126 {
127 	struct vga_resource *vr;
128 
129 	if (bios == NULL) {
130 		return;
131 	}
132 
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);
136 
137 		return;
138 	}
139 
140 	/*
141 	 * Look up the PCIR_BIOS resource in our softc.  It should match
142 	 * the address we returned previously.
143 	 */
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,
149 	    vr->vr_res);
150 }
151 
152 static int
153 vga_pci_probe(device_t dev)
154 {
155 	device_t bdev;
156 	int unit;
157 	uint16_t bctl;
158 
159 	switch (pci_get_class(dev)) {
160 	case PCIC_DISPLAY:
161 		break;
162 	case PCIC_OLD:
163 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
164 			return (ENXIO);
165 		break;
166 	default:
167 		return (ENXIO);
168 	}
169 
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);
178 
179 	device_set_desc(dev, "VGA-compatible display");
180 	return (BUS_PROBE_GENERIC);
181 }
182 
183 static int
184 vga_pci_attach(device_t dev)
185 {
186 
187 	bus_generic_probe(dev);
188 
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);
193 	return (0);
194 }
195 
196 static int
197 vga_pci_suspend(device_t dev)
198 {
199 
200 	return (bus_generic_suspend(dev));
201 }
202 
203 static int
204 vga_pci_resume(device_t dev)
205 {
206 
207 	return (bus_generic_resume(dev));
208 }
209 
210 /* Bus interface. */
211 
212 static int
213 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
214 {
215 
216 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
217 }
218 
219 static int
220 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
221 {
222 
223 	return (EINVAL);
224 }
225 
226 static struct vga_resource *
227 lookup_res(struct vga_pci_softc *sc, int rid)
228 {
229 	int bar;
230 
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]);
236 	return (NULL);
237 }
238 
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)
242 {
243 	struct vga_resource *vr;
244 
245 	switch (type) {
246 	case SYS_RES_MEMORY:
247 	case SYS_RES_IOPORT:
248 		/*
249 		 * For BARs, we cache the resource so that we only allocate it
250 		 * from the PCI bus once.
251 		 */
252 		vr = lookup_res(device_get_softc(dev), *rid);
253 		if (vr == NULL)
254 			return (NULL);
255 		if (vr->vr_res == NULL)
256 			vr->vr_res = bus_alloc_resource(dev, type, rid, start,
257 			    end, count, flags);
258 		if (vr->vr_res != NULL)
259 			vr->vr_refs++;
260 		return (vr->vr_res);
261 	}
262 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
263 }
264 
265 static int
266 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
267     struct resource *r)
268 {
269 	struct vga_resource *vr;
270 
271 	switch (type) {
272 	case SYS_RES_MEMORY:
273 	case SYS_RES_IOPORT:
274 		/*
275 		 * Stop caching the resource when refs drops to 0
276 		 */
277 		vr = lookup_res(device_get_softc(dev), rid);
278 		if (vr && vr->vr_refs > 0) {
279 			if (--vr->vr_refs > 0)
280 				return(0);
281 			vr->vr_res = NULL;
282 			/* fall through */
283 		}
284 		/* fall through */
285 		break;
286 	}
287 	return (bus_release_resource(dev, type, rid, r));
288 }
289 
290 /* PCI interface. */
291 
292 static uint32_t
293 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
294 {
295 
296 	return (pci_read_config(dev, reg, width));
297 }
298 
299 static void
300 vga_pci_write_config(device_t dev, device_t child, int reg,
301     uint32_t val, int width)
302 {
303 
304 	pci_write_config(dev, reg, val, width);
305 }
306 
307 static int
308 vga_pci_enable_busmaster(device_t dev, device_t child)
309 {
310 
311 	return (pci_enable_busmaster(dev));
312 }
313 
314 static int
315 vga_pci_disable_busmaster(device_t dev, device_t child)
316 {
317 
318 	return (pci_disable_busmaster(dev));
319 }
320 
321 static int
322 vga_pci_enable_io(device_t dev, device_t child, int space)
323 {
324 
325 	device_printf(dev, "child %s requested pci_enable_io\n",
326 	    device_get_nameunit(child));
327 	return (pci_enable_io(dev, space));
328 }
329 
330 static int
331 vga_pci_disable_io(device_t dev, device_t child, int space)
332 {
333 
334 	device_printf(dev, "child %s requested pci_disable_io\n",
335 	    device_get_nameunit(child));
336 	return (pci_disable_io(dev, space));
337 }
338 
339 static int
340 vga_pci_set_powerstate(device_t dev, device_t child, int state)
341 {
342 
343 	device_printf(dev, "child %s requested pci_set_powerstate\n",
344 	    device_get_nameunit(child));
345 	return (pci_set_powerstate(dev, state));
346 }
347 
348 static int
349 vga_pci_get_powerstate(device_t dev, device_t child)
350 {
351 
352 	device_printf(dev, "child %s requested pci_get_powerstate\n",
353 	    device_get_nameunit(child));
354 	return (pci_get_powerstate(dev));
355 }
356 
357 static int
358 vga_pci_assign_interrupt(device_t dev, device_t child)
359 {
360 
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));
364 }
365 
366 static int
367 vga_pci_find_extcap(device_t dev, device_t child, int capability,
368     int *capreg)
369 {
370 
371 	return (pci_find_extcap(dev, capability, capreg));
372 }
373 
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),
381 
382 	/* Bus interface */
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),
387 
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),
392 
393 	/* PCI interface */
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),
404 
405 	DEVMETHOD_END
406 };
407 
408 static driver_t vga_pci_driver = {
409 	"vgapci",
410 	vga_pci_methods,
411 	sizeof(struct vga_pci_softc),
412 };
413 
414 static devclass_t vga_devclass;
415 
416 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, NULL, NULL);
417