xref: /dragonfly/sys/bus/pci/vga_pci.c (revision 0db87cb7)
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 	int unit;
82 	device_t pcib;
83 	uint16_t config;
84 
85 	/* Check that the given device is a video card */
86 	if ((pci_get_class(dev) != PCIC_DISPLAY &&
87 	    (pci_get_class(dev) != PCIC_OLD ||
88 	     pci_get_subclass(dev) != PCIS_OLD_VGA)))
89 		return (0);
90 
91 	unit = device_get_unit(dev);
92 
93 	if (vga_pci_default_unit >= 0) {
94 		/*
95 		 * The boot display device was determined by a previous
96 		 * call to this function, or the user forced it using
97 		 * the hw.pci.default_vgapci_unit tunable.
98 		 */
99 		return (vga_pci_default_unit == unit);
100 	}
101 
102 	/*
103 	 * The primary video card used as a boot display must have the
104 	 * "I/O" and "Memory Address Space Decoding" bits set in its
105 	 * Command register.
106 	 *
107 	 * Furthermore, if the card is attached to a bridge, instead of
108 	 * the root PCI bus, the bridge must have the "VGA Enable" bit
109 	 * set in its Control register.
110 	 */
111 
112 	pcib = device_get_parent(device_get_parent(dev));
113 	if (device_get_devclass(device_get_parent(pcib)) ==
114 	    devclass_find("pci")) {
115 		/*
116 		 * The parent bridge is a PCI-to-PCI bridge: check the
117 		 * value of the "VGA Enable" bit.
118 		 */
119 		config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
120 		if ((config & PCIB_BCR_VGA_ENABLE) == 0)
121 			return (0);
122 	}
123 
124 	config = pci_read_config(dev, PCIR_COMMAND, 2);
125 	if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
126 		return (0);
127 
128 	/* This video card is the boot display: record its unit number. */
129 	vga_pci_default_unit = unit;
130 	device_set_flags(dev, 1);
131 
132 	return (1);
133 }
134 
135 void *
136 vga_pci_map_bios(device_t dev, size_t *size)
137 {
138 	int rid;
139 	struct resource *res;
140 
141 	if (vga_pci_is_boot_display(dev)) {
142 		/*
143 		 * On x86, the System BIOS copy the default display
144 		 * device's Video BIOS at a fixed location in system
145 		 * memory (0xC0000, 128 kBytes long) at boot time.
146 		 *
147 		 * We use this copy for the default boot device, because
148 		 * the original ROM may not be valid after boot.
149 		 */
150 
151 		*size = VGA_PCI_BIOS_SHADOW_SIZE;
152 		return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
153 	}
154 
155 	rid = PCIR_BIOS;
156 	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
157 	    ~0ul, 1, RF_ACTIVE, -1);
158 	if (res == NULL) {
159 		return (NULL);
160 	}
161 
162 	*size = rman_get_size(res);
163 	return (rman_get_virtual(res));
164 }
165 
166 void
167 vga_pci_unmap_bios(device_t dev, void *bios)
168 {
169 	struct vga_resource *vr;
170 
171 	if (bios == NULL) {
172 		return;
173 	}
174 
175 	if (vga_pci_is_boot_display(dev)) {
176 		/* We mapped the BIOS shadow copy located at 0xC0000. */
177 		pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
178 
179 		return;
180 	}
181 
182 	/*
183 	 * Look up the PCIR_BIOS resource in our softc.  It should match
184 	 * the address we returned previously.
185 	 */
186 	vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
187 	KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
188 	KASSERT(rman_get_virtual(vr->vr_res) == bios,
189 	    ("vga_pci_unmap_bios: mismatch"));
190 	vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
191 	    vr->vr_res);
192 }
193 
194 static int
195 vga_pci_probe(device_t dev)
196 {
197 
198 	switch (pci_get_class(dev)) {
199 	case PCIC_DISPLAY:
200 		break;
201 	case PCIC_OLD:
202 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
203 			return (ENXIO);
204 		break;
205 	default:
206 		return (ENXIO);
207 	}
208 
209 	/* Probe default display. */
210 	vga_pci_is_boot_display(dev);
211 
212 	device_set_desc(dev, "VGA-compatible display");
213 	return (BUS_PROBE_GENERIC);
214 }
215 
216 static int
217 vga_pci_attach(device_t dev)
218 {
219 
220 	bus_generic_probe(dev);
221 
222 	/* Always create a drm child for now to make it easier on drm. */
223 	device_add_child(dev, "drm", -1);
224 	device_add_child(dev, "drmn", -1);
225 	bus_generic_attach(dev);
226 
227 	if (vga_pci_is_boot_display(dev))
228 		device_printf(dev, "Boot video device\n");
229 
230 	return (0);
231 }
232 
233 static int
234 vga_pci_suspend(device_t dev)
235 {
236 
237 	return (bus_generic_suspend(dev));
238 }
239 
240 static int
241 vga_pci_resume(device_t dev)
242 {
243 
244 	return (bus_generic_resume(dev));
245 }
246 
247 /* Bus interface. */
248 
249 static int
250 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
251 {
252 
253 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
254 }
255 
256 static int
257 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
258 {
259 
260 	return (EINVAL);
261 }
262 
263 static int
264 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
265     int flags, driver_intr_t *intr, void *arg, void **cookiep,
266     lwkt_serialize_t serializer, const char *desc)
267 {
268 
269 	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
270 	    intr, arg, cookiep, serializer, desc));
271 }
272 
273 static int
274 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
275     void *cookie)
276 {
277 
278 	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
279 }
280 
281 static struct vga_resource *
282 lookup_res(struct vga_pci_softc *sc, int rid)
283 {
284 	int bar;
285 
286 	if (rid == PCIR_BIOS)
287 		return (&sc->vga_bios);
288 	bar = PCI_RID2BAR(rid);
289 	if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
290 		return (&sc->vga_bars[bar]);
291 	return (NULL);
292 }
293 
294 static struct resource *
295 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
296     u_long start, u_long end, u_long count, u_int flags, int cpuid __unused)
297 {
298 	struct vga_resource *vr;
299 
300 	switch (type) {
301 	case SYS_RES_MEMORY:
302 	case SYS_RES_IOPORT:
303 		/*
304 		 * For BARs, we cache the resource so that we only allocate it
305 		 * from the PCI bus once.
306 		 */
307 		vr = lookup_res(device_get_softc(dev), *rid);
308 		if (vr == NULL)
309 			return (NULL);
310 		if (vr->vr_res == NULL)
311 			vr->vr_res = bus_alloc_resource(dev, type, rid, start,
312 			    end, count, flags);
313 		if (vr->vr_res != NULL)
314 			vr->vr_refs++;
315 		return (vr->vr_res);
316 	}
317 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
318 }
319 
320 static int
321 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
322     struct resource *r)
323 {
324 	struct vga_resource *vr;
325 
326 	switch (type) {
327 	case SYS_RES_MEMORY:
328 	case SYS_RES_IOPORT:
329 		/*
330 		 * Stop caching the resource when refs drops to 0
331 		 */
332 		vr = lookup_res(device_get_softc(dev), rid);
333 		if (vr && vr->vr_refs > 0) {
334 			if (--vr->vr_refs > 0)
335 				return(0);
336 			vr->vr_res = NULL;
337 			/* fall through */
338 		}
339 		/* fall through */
340 		break;
341 	}
342 	return (bus_release_resource(dev, type, rid, r));
343 }
344 
345 /* PCI interface. */
346 
347 static uint32_t
348 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
349 {
350 
351 	return (pci_read_config(dev, reg, width));
352 }
353 
354 static void
355 vga_pci_write_config(device_t dev, device_t child, int reg,
356     uint32_t val, int width)
357 {
358 
359 	pci_write_config(dev, reg, val, width);
360 }
361 
362 static int
363 vga_pci_enable_busmaster(device_t dev, device_t child)
364 {
365 
366 	return (pci_enable_busmaster(dev));
367 }
368 
369 static int
370 vga_pci_disable_busmaster(device_t dev, device_t child)
371 {
372 
373 	return (pci_disable_busmaster(dev));
374 }
375 
376 static int
377 vga_pci_enable_io(device_t dev, device_t child, int space)
378 {
379 
380 	device_printf(dev, "child %s requested pci_enable_io\n",
381 	    device_get_nameunit(child));
382 	return (pci_enable_io(dev, space));
383 }
384 
385 static int
386 vga_pci_disable_io(device_t dev, device_t child, int space)
387 {
388 
389 	device_printf(dev, "child %s requested pci_disable_io\n",
390 	    device_get_nameunit(child));
391 	return (pci_disable_io(dev, space));
392 }
393 
394 static int
395 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
396 {
397 
398 	return (pci_get_vpd_ident(dev, identptr));
399 }
400 
401 static int
402 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
403     const char **vptr)
404 {
405 
406 	return (pci_get_vpd_readonly(dev, kw, vptr));
407 }
408 
409 static int
410 vga_pci_set_powerstate(device_t dev, device_t child, int state)
411 {
412 
413 	device_printf(dev, "child %s requested pci_set_powerstate\n",
414 	    device_get_nameunit(child));
415 	return (pci_set_powerstate(dev, state));
416 }
417 
418 static int
419 vga_pci_get_powerstate(device_t dev, device_t child)
420 {
421 
422 	device_printf(dev, "child %s requested pci_get_powerstate\n",
423 	    device_get_nameunit(child));
424 	return (pci_get_powerstate(dev));
425 }
426 
427 static int
428 vga_pci_assign_interrupt(device_t dev, device_t child)
429 {
430 
431 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
432 	    device_get_nameunit(child));
433 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
434 }
435 
436 static int
437 vga_pci_find_extcap(device_t dev, device_t child, int capability,
438     int *capreg)
439 {
440 
441 	return (pci_find_extcap(dev, capability, capreg));
442 }
443 
444 static int
445 vga_pci_alloc_msi(device_t dev, device_t child, int *rid, int count,
446     int cpuid)
447 {
448 	struct vga_pci_softc *sc;
449 	int error;
450 
451 	sc = device_get_softc(dev);
452 	if (sc->vga_msi_child != NULL)
453 		return (EBUSY);
454 	error = pci_alloc_msi(dev, rid, count, cpuid);
455 	if (error == 0)
456 		sc->vga_msi_child = child;
457 	return (error);
458 }
459 
460 static int
461 vga_pci_release_msi(device_t dev, device_t child)
462 {
463 	struct vga_pci_softc *sc;
464 	int error;
465 
466 	sc = device_get_softc(dev);
467 	if (sc->vga_msi_child != child)
468 		return (ENXIO);
469 	error = pci_release_msi(dev);
470 	if (error == 0)
471 		sc->vga_msi_child = NULL;
472 	return (error);
473 }
474 
475 static int
476 vga_pci_msi_count(device_t dev, device_t child)
477 {
478 
479 	return (pci_msi_count(dev));
480 }
481 
482 static device_method_t vga_pci_methods[] = {
483 	/* Device interface */
484 	DEVMETHOD(device_probe,		vga_pci_probe),
485 	DEVMETHOD(device_attach,	vga_pci_attach),
486 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
487 	DEVMETHOD(device_suspend,	vga_pci_suspend),
488 	DEVMETHOD(device_resume,	vga_pci_resume),
489 
490 	/* Bus interface */
491 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
492 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
493 	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
494 	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
495 
496 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
497 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
498 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
499 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
500 
501 	/* PCI interface */
502 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
503 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
504 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
505 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
506 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
507 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
508 	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
509 	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
510 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
511 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
512 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
513 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
514 	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
515 	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
516 	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
517 
518 	DEVMETHOD_END
519 };
520 
521 static driver_t vga_pci_driver = {
522 	"vgapci",
523 	vga_pci_methods,
524 	sizeof(struct vga_pci_softc),
525 };
526 
527 static devclass_t vga_devclass;
528 
529 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, NULL, NULL);
530