xref: /freebsd/sys/dev/pci/vga_pci.c (revision 361e428888e630eb708c72cf31579a25ba5d4f03)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
32  * drm(4) should attach as children of this device.
33  *
34  * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
35  * in or rename it.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/rman.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 
46 #if defined(__amd64__) || defined(__i386__)
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 #endif
50 
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcivar.h>
53 
54 #include <compat/x86bios/x86bios.h> /* To re-POST the card. */
55 
56 struct vga_resource {
57 	struct resource	*vr_res;
58 	int	vr_refs;
59 };
60 
61 struct vga_pci_softc {
62 	device_t	vga_msi_child;	/* Child driver using MSI. */
63 	struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
64 	struct vga_resource vga_bios;
65 };
66 
67 SYSCTL_DECL(_hw_pci);
68 
69 static struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
70 static struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
71     int type, int *rid, u_long start, u_long end, u_long count, u_int flags);
72 static int	vga_pci_release_resource(device_t dev, device_t child, int type,
73     int rid, struct resource *r);
74 
75 int vga_pci_default_unit = -1;
76 SYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
77     &vga_pci_default_unit, -1, "Default VGA-compatible display");
78 
79 int
80 vga_pci_is_boot_display(device_t dev)
81 {
82 	int unit;
83 	device_t pcib;
84 	uint16_t config;
85 
86 	/* Check that the given device is a video card */
87 	if ((pci_get_class(dev) != PCIC_DISPLAY &&
88 	    (pci_get_class(dev) != PCIC_OLD ||
89 	     pci_get_subclass(dev) != PCIS_OLD_VGA)))
90 		return (0);
91 
92 	unit = device_get_unit(dev);
93 
94 	if (vga_pci_default_unit >= 0) {
95 		/*
96 		 * The boot display device was determined by a previous
97 		 * call to this function, or the user forced it using
98 		 * the hw.pci.default_vgapci_unit tunable.
99 		 */
100 		return (vga_pci_default_unit == unit);
101 	}
102 
103 	/*
104 	 * The primary video card used as a boot display must have the
105 	 * "I/O" and "Memory Address Space Decoding" bits set in its
106 	 * Command register.
107 	 *
108 	 * Furthermore, if the card is attached to a bridge, instead of
109 	 * the root PCI bus, the bridge must have the "VGA Enable" bit
110 	 * set in its Control register.
111 	 */
112 
113 	pcib = device_get_parent(device_get_parent(dev));
114 	if (device_get_devclass(device_get_parent(pcib)) ==
115 	    devclass_find("pci")) {
116 		/*
117 		 * The parent bridge is a PCI-to-PCI bridge: check the
118 		 * value of the "VGA Enable" bit.
119 		 */
120 		config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
121 		if ((config & PCIB_BCR_VGA_ENABLE) == 0)
122 			return (0);
123 	}
124 
125 	config = pci_read_config(dev, PCIR_COMMAND, 2);
126 	if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
127 		return (0);
128 
129 	/*
130 	 * Disable interrupts until a chipset driver is loaded for
131 	 * this PCI device. Else unhandled display adapter interrupts
132 	 * might freeze the CPU.
133 	 */
134 	pci_write_config(dev, PCIR_COMMAND, config | PCIM_CMD_INTxDIS, 2);
135 
136 	/* This video card is the boot display: record its unit number. */
137 	vga_pci_default_unit = unit;
138 	device_set_flags(dev, 1);
139 
140 	return (1);
141 }
142 
143 void *
144 vga_pci_map_bios(device_t dev, size_t *size)
145 {
146 	int rid;
147 	struct resource *res;
148 
149 #if defined(__amd64__) || defined(__i386__)
150 	if (vga_pci_is_boot_display(dev)) {
151 		/*
152 		 * On x86, the System BIOS copy the default display
153 		 * device's Video BIOS at a fixed location in system
154 		 * memory (0xC0000, 128 kBytes long) at boot time.
155 		 *
156 		 * We use this copy for the default boot device, because
157 		 * the original ROM may not be valid after boot.
158 		 */
159 
160 		*size = VGA_PCI_BIOS_SHADOW_SIZE;
161 		return (pmap_mapbios(VGA_PCI_BIOS_SHADOW_ADDR, *size));
162 	}
163 #endif
164 
165 	rid = PCIR_BIOS;
166 	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
167 	    ~0ul, 1, RF_ACTIVE);
168 	if (res == NULL) {
169 		return (NULL);
170 	}
171 
172 	*size = rman_get_size(res);
173 	return (rman_get_virtual(res));
174 }
175 
176 void
177 vga_pci_unmap_bios(device_t dev, void *bios)
178 {
179 	struct vga_resource *vr;
180 
181 	if (bios == NULL) {
182 		return;
183 	}
184 
185 #if defined(__amd64__) || defined(__i386__)
186 	if (vga_pci_is_boot_display(dev)) {
187 		/* We mapped the BIOS shadow copy located at 0xC0000. */
188 		pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
189 
190 		return;
191 	}
192 #endif
193 
194 	/*
195 	 * Look up the PCIR_BIOS resource in our softc.  It should match
196 	 * the address we returned previously.
197 	 */
198 	vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
199 	KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
200 	KASSERT(rman_get_virtual(vr->vr_res) == bios,
201 	    ("vga_pci_unmap_bios: mismatch"));
202 	vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
203 	    vr->vr_res);
204 }
205 
206 int
207 vga_pci_repost(device_t dev)
208 {
209 #if defined(__amd64__) || (defined(__i386__) && !defined(PC98))
210 	x86regs_t regs;
211 
212 	if (!vga_pci_is_boot_display(dev))
213 		return (EINVAL);
214 
215 	if (x86bios_get_orm(VGA_PCI_BIOS_SHADOW_ADDR) == NULL)
216 		return (ENOTSUP);
217 
218 	x86bios_init_regs(&regs);
219 
220 	regs.R_AH = pci_get_bus(dev);
221 	regs.R_AL = (pci_get_slot(dev) << 3) | (pci_get_function(dev) & 0x07);
222 	regs.R_DL = 0x80;
223 
224 	device_printf(dev, "REPOSTing\n");
225 	x86bios_call(&regs, X86BIOS_PHYSTOSEG(VGA_PCI_BIOS_SHADOW_ADDR + 3),
226 	    X86BIOS_PHYSTOOFF(VGA_PCI_BIOS_SHADOW_ADDR + 3));
227 
228 	x86bios_get_intr(0x10);
229 
230 	return (0);
231 #else
232 	return (ENOTSUP);
233 #endif
234 }
235 
236 static int
237 vga_pci_probe(device_t dev)
238 {
239 
240 	switch (pci_get_class(dev)) {
241 	case PCIC_DISPLAY:
242 		break;
243 	case PCIC_OLD:
244 		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
245 			return (ENXIO);
246 		break;
247 	default:
248 		return (ENXIO);
249 	}
250 
251 	/* Probe default display. */
252 	vga_pci_is_boot_display(dev);
253 
254 	device_set_desc(dev, "VGA-compatible display");
255 	return (BUS_PROBE_GENERIC);
256 }
257 
258 static int
259 vga_pci_attach(device_t dev)
260 {
261 
262 	bus_generic_probe(dev);
263 
264 	/* Always create a drm child for now to make it easier on drm. */
265 	device_add_child(dev, "drm", -1);
266 	device_add_child(dev, "drmn", -1);
267 	bus_generic_attach(dev);
268 
269 	if (vga_pci_is_boot_display(dev))
270 		device_printf(dev, "Boot video device\n");
271 
272 	return (0);
273 }
274 
275 static int
276 vga_pci_suspend(device_t dev)
277 {
278 
279 	return (bus_generic_suspend(dev));
280 }
281 
282 static int
283 vga_pci_resume(device_t dev)
284 {
285 
286 	return (bus_generic_resume(dev));
287 }
288 
289 /* Bus interface. */
290 
291 static int
292 vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
293 {
294 
295 	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
296 }
297 
298 static int
299 vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
300 {
301 
302 	return (EINVAL);
303 }
304 
305 static int
306 vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
307     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
308     void **cookiep)
309 {
310 	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
311 	    filter, intr, arg, cookiep));
312 }
313 
314 static int
315 vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
316     void *cookie)
317 {
318 	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
319 }
320 
321 static struct vga_resource *
322 lookup_res(struct vga_pci_softc *sc, int rid)
323 {
324 	int bar;
325 
326 	if (rid == PCIR_BIOS)
327 		return (&sc->vga_bios);
328 	bar = PCI_RID2BAR(rid);
329 	if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
330 		return (&sc->vga_bars[bar]);
331 	return (NULL);
332 }
333 
334 static struct resource *
335 vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
336     u_long start, u_long end, u_long count, u_int flags)
337 {
338 	struct vga_resource *vr;
339 
340 	switch (type) {
341 	case SYS_RES_MEMORY:
342 	case SYS_RES_IOPORT:
343 		/*
344 		 * For BARs, we cache the resource so that we only allocate it
345 		 * from the PCI bus once.
346 		 */
347 		vr = lookup_res(device_get_softc(dev), *rid);
348 		if (vr == NULL)
349 			return (NULL);
350 		if (vr->vr_res == NULL)
351 			vr->vr_res = bus_alloc_resource(dev, type, rid, start,
352 			    end, count, flags);
353 		if (vr->vr_res != NULL)
354 			vr->vr_refs++;
355 		return (vr->vr_res);
356 	}
357 	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
358 }
359 
360 static int
361 vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
362     struct resource *r)
363 {
364 	struct vga_resource *vr;
365 	int error;
366 
367 	switch (type) {
368 	case SYS_RES_MEMORY:
369 	case SYS_RES_IOPORT:
370 		/*
371 		 * For BARs, we release the resource from the PCI bus
372 		 * when the last child reference goes away.
373 		 */
374 		vr = lookup_res(device_get_softc(dev), rid);
375 		if (vr == NULL)
376 			return (EINVAL);
377 		if (vr->vr_res == NULL)
378 			return (EINVAL);
379 		KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
380 		if (vr->vr_refs > 1) {
381 			vr->vr_refs--;
382 			return (0);
383 		}
384 		KASSERT(vr->vr_refs > 0,
385 		    ("vga_pci resource reference count underflow"));
386 		error = bus_release_resource(dev, type, rid, r);
387 		if (error == 0) {
388 			vr->vr_res = NULL;
389 			vr->vr_refs = 0;
390 		}
391 		return (error);
392 	}
393 
394 	return (bus_release_resource(dev, type, rid, r));
395 }
396 
397 /* PCI interface. */
398 
399 static uint32_t
400 vga_pci_read_config(device_t dev, device_t child, int reg, int width)
401 {
402 
403 	return (pci_read_config(dev, reg, width));
404 }
405 
406 static void
407 vga_pci_write_config(device_t dev, device_t child, int reg,
408     uint32_t val, int width)
409 {
410 
411 	pci_write_config(dev, reg, val, width);
412 }
413 
414 static int
415 vga_pci_enable_busmaster(device_t dev, device_t child)
416 {
417 
418 	return (pci_enable_busmaster(dev));
419 }
420 
421 static int
422 vga_pci_disable_busmaster(device_t dev, device_t child)
423 {
424 
425 	return (pci_disable_busmaster(dev));
426 }
427 
428 static int
429 vga_pci_enable_io(device_t dev, device_t child, int space)
430 {
431 
432 	device_printf(dev, "child %s requested pci_enable_io\n",
433 	    device_get_nameunit(child));
434 	return (pci_enable_io(dev, space));
435 }
436 
437 static int
438 vga_pci_disable_io(device_t dev, device_t child, int space)
439 {
440 
441 	device_printf(dev, "child %s requested pci_disable_io\n",
442 	    device_get_nameunit(child));
443 	return (pci_disable_io(dev, space));
444 }
445 
446 static int
447 vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
448 {
449 
450 	return (pci_get_vpd_ident(dev, identptr));
451 }
452 
453 static int
454 vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
455     const char **vptr)
456 {
457 
458 	return (pci_get_vpd_readonly(dev, kw, vptr));
459 }
460 
461 static int
462 vga_pci_set_powerstate(device_t dev, device_t child, int state)
463 {
464 
465 	device_printf(dev, "child %s requested pci_set_powerstate\n",
466 	    device_get_nameunit(child));
467 	return (pci_set_powerstate(dev, state));
468 }
469 
470 static int
471 vga_pci_get_powerstate(device_t dev, device_t child)
472 {
473 
474 	device_printf(dev, "child %s requested pci_get_powerstate\n",
475 	    device_get_nameunit(child));
476 	return (pci_get_powerstate(dev));
477 }
478 
479 static int
480 vga_pci_assign_interrupt(device_t dev, device_t child)
481 {
482 
483 	device_printf(dev, "child %s requested pci_assign_interrupt\n",
484 	    device_get_nameunit(child));
485 	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
486 }
487 
488 static int
489 vga_pci_find_cap(device_t dev, device_t child, int capability,
490     int *capreg)
491 {
492 
493 	return (pci_find_cap(dev, capability, capreg));
494 }
495 
496 static int
497 vga_pci_find_extcap(device_t dev, device_t child, int capability,
498     int *capreg)
499 {
500 
501 	return (pci_find_extcap(dev, capability, capreg));
502 }
503 
504 static int
505 vga_pci_find_htcap(device_t dev, device_t child, int capability,
506     int *capreg)
507 {
508 
509 	return (pci_find_htcap(dev, capability, capreg));
510 }
511 
512 static int
513 vga_pci_alloc_msi(device_t dev, device_t child, int *count)
514 {
515 	struct vga_pci_softc *sc;
516 	int error;
517 
518 	sc = device_get_softc(dev);
519 	if (sc->vga_msi_child != NULL)
520 		return (EBUSY);
521 	error = pci_alloc_msi(dev, count);
522 	if (error == 0)
523 		sc->vga_msi_child = child;
524 	return (error);
525 }
526 
527 static int
528 vga_pci_alloc_msix(device_t dev, device_t child, int *count)
529 {
530 	struct vga_pci_softc *sc;
531 	int error;
532 
533 	sc = device_get_softc(dev);
534 	if (sc->vga_msi_child != NULL)
535 		return (EBUSY);
536 	error = pci_alloc_msix(dev, count);
537 	if (error == 0)
538 		sc->vga_msi_child = child;
539 	return (error);
540 }
541 
542 static int
543 vga_pci_remap_msix(device_t dev, device_t child, int count,
544     const u_int *vectors)
545 {
546 	struct vga_pci_softc *sc;
547 
548 	sc = device_get_softc(dev);
549 	if (sc->vga_msi_child != child)
550 		return (ENXIO);
551 	return (pci_remap_msix(dev, count, vectors));
552 }
553 
554 static int
555 vga_pci_release_msi(device_t dev, device_t child)
556 {
557 	struct vga_pci_softc *sc;
558 	int error;
559 
560 	sc = device_get_softc(dev);
561 	if (sc->vga_msi_child != child)
562 		return (ENXIO);
563 	error = pci_release_msi(dev);
564 	if (error == 0)
565 		sc->vga_msi_child = NULL;
566 	return (error);
567 }
568 
569 static int
570 vga_pci_msi_count(device_t dev, device_t child)
571 {
572 
573 	return (pci_msi_count(dev));
574 }
575 
576 static int
577 vga_pci_msix_count(device_t dev, device_t child)
578 {
579 
580 	return (pci_msix_count(dev));
581 }
582 
583 static bus_dma_tag_t
584 vga_pci_get_dma_tag(device_t bus, device_t child)
585 {
586 
587 	return (bus_get_dma_tag(bus));
588 }
589 
590 static device_method_t vga_pci_methods[] = {
591 	/* Device interface */
592 	DEVMETHOD(device_probe,		vga_pci_probe),
593 	DEVMETHOD(device_attach,	vga_pci_attach),
594 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
595 	DEVMETHOD(device_suspend,	vga_pci_suspend),
596 	DEVMETHOD(device_resume,	vga_pci_resume),
597 
598 	/* Bus interface */
599 	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
600 	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
601 	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
602 	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
603 	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
604 	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
605 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
606 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
607 	DEVMETHOD(bus_get_dma_tag,	vga_pci_get_dma_tag),
608 
609 	/* PCI interface */
610 	DEVMETHOD(pci_read_config,	vga_pci_read_config),
611 	DEVMETHOD(pci_write_config,	vga_pci_write_config),
612 	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
613 	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
614 	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
615 	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
616 	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
617 	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
618 	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
619 	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
620 	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
621 	DEVMETHOD(pci_find_cap,		vga_pci_find_cap),
622 	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
623 	DEVMETHOD(pci_find_htcap,	vga_pci_find_htcap),
624 	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
625 	DEVMETHOD(pci_alloc_msix,	vga_pci_alloc_msix),
626 	DEVMETHOD(pci_remap_msix,	vga_pci_remap_msix),
627 	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
628 	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
629 	DEVMETHOD(pci_msix_count,	vga_pci_msix_count),
630 
631 	{ 0, 0 }
632 };
633 
634 static driver_t vga_pci_driver = {
635 	"vgapci",
636 	vga_pci_methods,
637 	sizeof(struct vga_pci_softc),
638 };
639 
640 static devclass_t vga_devclass;
641 
642 DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
643 MODULE_DEPEND(vgapci, x86bios, 1, 1, 1);
644