vga_pci.c revision 268351
1254721Semaste/*-
2254721Semaste * Copyright (c) 2005 John Baldwin <jhb@FreeBSD.org>
3254721Semaste * All rights reserved.
4254721Semaste *
5254721Semaste * Redistribution and use in source and binary forms, with or without
6254721Semaste * modification, are permitted provided that the following conditions
7254721Semaste * are met:
8254721Semaste * 1. Redistributions of source code must retain the above copyright
9254721Semaste *    notice, this list of conditions and the following disclaimer.
10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer in the
12254721Semaste *    documentation and/or other materials provided with the distribution.
13254721Semaste *
14254721Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17254721Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24254721Semaste * SUCH DAMAGE.
25254721Semaste */
26254721Semaste
27254721Semaste#include <sys/cdefs.h>
28254721Semaste__FBSDID("$FreeBSD: head/sys/dev/pci/vga_pci.c 268351 2014-07-07 00:27:09Z marcel $");
29254721Semaste
30254721Semaste/*
31254721Semaste * Simple driver for PCI VGA display devices.  Drivers such as agp(4) and
32254721Semaste * drm(4) should attach as children of this device.
33254721Semaste *
34254721Semaste * XXX: The vgapci name is a hack until we somehow merge the isa vga driver
35254721Semaste * in or rename it.
36254721Semaste */
37254721Semaste
38254721Semaste#include <sys/param.h>
39254721Semaste#include <sys/bus.h>
40254721Semaste#include <sys/kernel.h>
41254721Semaste#include <sys/module.h>
42254721Semaste#include <sys/rman.h>
43254721Semaste#include <sys/sysctl.h>
44254721Semaste#include <sys/systm.h>
45254721Semaste
46254721Semaste#if defined(__amd64__) || defined(__i386__)
47254721Semaste#include <vm/vm.h>
48254721Semaste#include <vm/pmap.h>
49254721Semaste#endif
50254721Semaste
51254721Semaste#include <dev/pci/pcireg.h>
52254721Semaste#include <dev/pci/pcivar.h>
53254721Semaste
54254721Semastestruct vga_resource {
55254721Semaste	struct resource	*vr_res;
56254721Semaste	int	vr_refs;
57254721Semaste};
58254721Semaste
59254721Semastestruct vga_pci_softc {
60254721Semaste	device_t	vga_msi_child;	/* Child driver using MSI. */
61254721Semaste	struct vga_resource vga_bars[PCIR_MAX_BAR_0 + 1];
62254721Semaste	struct vga_resource vga_bios;
63254721Semaste};
64254721Semaste
65254721SemasteSYSCTL_DECL(_hw_pci);
66254721Semaste
67254721Semastestatic struct vga_resource *lookup_res(struct vga_pci_softc *sc, int rid);
68254721Semastestatic struct resource *vga_pci_alloc_resource(device_t dev, device_t child,
69254721Semaste    int type, int *rid, u_long start, u_long end, u_long count, u_int flags);
70254721Semastestatic int	vga_pci_release_resource(device_t dev, device_t child, int type,
71254721Semaste    int rid, struct resource *r);
72254721Semaste
73254721Semasteint vga_pci_default_unit = -1;
74254721SemasteSYSCTL_INT(_hw_pci, OID_AUTO, default_vgapci_unit, CTLFLAG_RDTUN,
75254721Semaste    &vga_pci_default_unit, -1, "Default VGA-compatible display");
76254721Semaste
77254721Semasteint
78254721Semastevga_pci_is_boot_display(device_t dev)
79254721Semaste{
80254721Semaste	int unit;
81254721Semaste	device_t pcib;
82254721Semaste	uint16_t config;
83254721Semaste
84254721Semaste	/* Check that the given device is a video card */
85254721Semaste	if ((pci_get_class(dev) != PCIC_DISPLAY &&
86254721Semaste	    (pci_get_class(dev) != PCIC_OLD ||
87254721Semaste	     pci_get_subclass(dev) != PCIS_OLD_VGA)))
88254721Semaste		return (0);
89254721Semaste
90	unit = device_get_unit(dev);
91
92	if (vga_pci_default_unit >= 0) {
93		/*
94		 * The boot display device was determined by a previous
95		 * call to this function, or the user forced it using
96		 * the hw.pci.default_vgapci_unit tunable.
97		 */
98		return (vga_pci_default_unit == unit);
99	}
100
101	/*
102	 * The primary video card used as a boot display must have the
103	 * "I/O" and "Memory Address Space Decoding" bits set in its
104	 * Command register.
105	 *
106	 * Furthermore, if the card is attached to a bridge, instead of
107	 * the root PCI bus, the bridge must have the "VGA Enable" bit
108	 * set in its Control register.
109	 */
110
111	pcib = device_get_parent(device_get_parent(dev));
112	if (device_get_devclass(device_get_parent(pcib)) ==
113	    devclass_find("pci")) {
114		/*
115		 * The parent bridge is a PCI-to-PCI bridge: check the
116		 * value of the "VGA Enable" bit.
117		 */
118		config = pci_read_config(pcib, PCIR_BRIDGECTL_1, 2);
119		if ((config & PCIB_BCR_VGA_ENABLE) == 0)
120			return (0);
121	}
122
123	config = pci_read_config(dev, PCIR_COMMAND, 2);
124	if ((config & (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN)) == 0)
125		return (0);
126
127	/* This video card is the boot display: record its unit number. */
128	vga_pci_default_unit = unit;
129	device_set_flags(dev, 1);
130
131	return (1);
132}
133
134void *
135vga_pci_map_bios(device_t dev, size_t *size)
136{
137	int rid;
138	struct resource *res;
139
140#if defined(__amd64__) || defined(__i386__)
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#endif
155
156	rid = PCIR_BIOS;
157	res = vga_pci_alloc_resource(dev, NULL, SYS_RES_MEMORY, &rid, 0ul,
158	    ~0ul, 1, RF_ACTIVE);
159	if (res == NULL) {
160		return (NULL);
161	}
162
163	*size = rman_get_size(res);
164	return (rman_get_virtual(res));
165}
166
167void
168vga_pci_unmap_bios(device_t dev, void *bios)
169{
170	struct vga_resource *vr;
171
172	if (bios == NULL) {
173		return;
174	}
175
176#if defined(__amd64__) || defined(__i386__)
177	if (vga_pci_is_boot_display(dev)) {
178		/* We mapped the BIOS shadow copy located at 0xC0000. */
179		pmap_unmapdev((vm_offset_t)bios, VGA_PCI_BIOS_SHADOW_SIZE);
180
181		return;
182	}
183#endif
184
185	/*
186	 * Look up the PCIR_BIOS resource in our softc.  It should match
187	 * the address we returned previously.
188	 */
189	vr = lookup_res(device_get_softc(dev), PCIR_BIOS);
190	KASSERT(vr->vr_res != NULL, ("vga_pci_unmap_bios: bios not mapped"));
191	KASSERT(rman_get_virtual(vr->vr_res) == bios,
192	    ("vga_pci_unmap_bios: mismatch"));
193	vga_pci_release_resource(dev, NULL, SYS_RES_MEMORY, PCIR_BIOS,
194	    vr->vr_res);
195}
196
197static int
198vga_pci_probe(device_t dev)
199{
200
201	switch (pci_get_class(dev)) {
202	case PCIC_DISPLAY:
203		break;
204	case PCIC_OLD:
205		if (pci_get_subclass(dev) != PCIS_OLD_VGA)
206			return (ENXIO);
207		break;
208	default:
209		return (ENXIO);
210	}
211
212	/* Probe default display. */
213	vga_pci_is_boot_display(dev);
214
215	device_set_desc(dev, "VGA-compatible display");
216	return (BUS_PROBE_GENERIC);
217}
218
219static int
220vga_pci_attach(device_t dev)
221{
222
223	bus_generic_probe(dev);
224
225	/* Always create a drm child for now to make it easier on drm. */
226	device_add_child(dev, "drm", -1);
227	device_add_child(dev, "drmn", -1);
228	bus_generic_attach(dev);
229
230	if (vga_pci_is_boot_display(dev))
231		device_printf(dev, "Boot video device\n");
232
233	return (0);
234}
235
236static int
237vga_pci_suspend(device_t dev)
238{
239
240	return (bus_generic_suspend(dev));
241}
242
243static int
244vga_pci_resume(device_t dev)
245{
246
247	return (bus_generic_resume(dev));
248}
249
250/* Bus interface. */
251
252static int
253vga_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
254{
255
256	return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
257}
258
259static int
260vga_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
261{
262
263	return (EINVAL);
264}
265
266static int
267vga_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
268    int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
269    void **cookiep)
270{
271	return (BUS_SETUP_INTR(device_get_parent(dev), dev, irq, flags,
272	    filter, intr, arg, cookiep));
273}
274
275static int
276vga_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
277    void *cookie)
278{
279	return (BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie));
280}
281
282static struct vga_resource *
283lookup_res(struct vga_pci_softc *sc, int rid)
284{
285	int bar;
286
287	if (rid == PCIR_BIOS)
288		return (&sc->vga_bios);
289	bar = PCI_RID2BAR(rid);
290	if (bar >= 0 && bar <= PCIR_MAX_BAR_0)
291		return (&sc->vga_bars[bar]);
292	return (NULL);
293}
294
295static struct resource *
296vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
297    u_long start, u_long end, u_long count, u_int flags)
298{
299	struct vga_resource *vr;
300
301	switch (type) {
302	case SYS_RES_MEMORY:
303	case SYS_RES_IOPORT:
304		/*
305		 * For BARs, we cache the resource so that we only allocate it
306		 * from the PCI bus once.
307		 */
308		vr = lookup_res(device_get_softc(dev), *rid);
309		if (vr == NULL)
310			return (NULL);
311		if (vr->vr_res == NULL)
312			vr->vr_res = bus_alloc_resource(dev, type, rid, start,
313			    end, count, flags);
314		if (vr->vr_res != NULL)
315			vr->vr_refs++;
316		return (vr->vr_res);
317	}
318	return (bus_alloc_resource(dev, type, rid, start, end, count, flags));
319}
320
321static int
322vga_pci_release_resource(device_t dev, device_t child, int type, int rid,
323    struct resource *r)
324{
325	struct vga_resource *vr;
326	int error;
327
328	switch (type) {
329	case SYS_RES_MEMORY:
330	case SYS_RES_IOPORT:
331		/*
332		 * For BARs, we release the resource from the PCI bus
333		 * when the last child reference goes away.
334		 */
335		vr = lookup_res(device_get_softc(dev), rid);
336		if (vr == NULL)
337			return (EINVAL);
338		if (vr->vr_res == NULL)
339			return (EINVAL);
340		KASSERT(vr->vr_res == r, ("vga_pci resource mismatch"));
341		if (vr->vr_refs > 1) {
342			vr->vr_refs--;
343			return (0);
344		}
345		KASSERT(vr->vr_refs > 0,
346		    ("vga_pci resource reference count underflow"));
347		error = bus_release_resource(dev, type, rid, r);
348		if (error == 0) {
349			vr->vr_res = NULL;
350			vr->vr_refs = 0;
351		}
352		return (error);
353	}
354
355	return (bus_release_resource(dev, type, rid, r));
356}
357
358/* PCI interface. */
359
360static uint32_t
361vga_pci_read_config(device_t dev, device_t child, int reg, int width)
362{
363
364	return (pci_read_config(dev, reg, width));
365}
366
367static void
368vga_pci_write_config(device_t dev, device_t child, int reg,
369    uint32_t val, int width)
370{
371
372	pci_write_config(dev, reg, val, width);
373}
374
375static int
376vga_pci_enable_busmaster(device_t dev, device_t child)
377{
378
379	return (pci_enable_busmaster(dev));
380}
381
382static int
383vga_pci_disable_busmaster(device_t dev, device_t child)
384{
385
386	return (pci_disable_busmaster(dev));
387}
388
389static int
390vga_pci_enable_io(device_t dev, device_t child, int space)
391{
392
393	device_printf(dev, "child %s requested pci_enable_io\n",
394	    device_get_nameunit(child));
395	return (pci_enable_io(dev, space));
396}
397
398static int
399vga_pci_disable_io(device_t dev, device_t child, int space)
400{
401
402	device_printf(dev, "child %s requested pci_disable_io\n",
403	    device_get_nameunit(child));
404	return (pci_disable_io(dev, space));
405}
406
407static int
408vga_pci_get_vpd_ident(device_t dev, device_t child, const char **identptr)
409{
410
411	return (pci_get_vpd_ident(dev, identptr));
412}
413
414static int
415vga_pci_get_vpd_readonly(device_t dev, device_t child, const char *kw,
416    const char **vptr)
417{
418
419	return (pci_get_vpd_readonly(dev, kw, vptr));
420}
421
422static int
423vga_pci_set_powerstate(device_t dev, device_t child, int state)
424{
425
426	device_printf(dev, "child %s requested pci_set_powerstate\n",
427	    device_get_nameunit(child));
428	return (pci_set_powerstate(dev, state));
429}
430
431static int
432vga_pci_get_powerstate(device_t dev, device_t child)
433{
434
435	device_printf(dev, "child %s requested pci_get_powerstate\n",
436	    device_get_nameunit(child));
437	return (pci_get_powerstate(dev));
438}
439
440static int
441vga_pci_assign_interrupt(device_t dev, device_t child)
442{
443
444	device_printf(dev, "child %s requested pci_assign_interrupt\n",
445	    device_get_nameunit(child));
446	return (PCI_ASSIGN_INTERRUPT(device_get_parent(dev), dev));
447}
448
449static int
450vga_pci_find_cap(device_t dev, device_t child, int capability,
451    int *capreg)
452{
453
454	return (pci_find_cap(dev, capability, capreg));
455}
456
457static int
458vga_pci_find_extcap(device_t dev, device_t child, int capability,
459    int *capreg)
460{
461
462	return (pci_find_extcap(dev, capability, capreg));
463}
464
465static int
466vga_pci_find_htcap(device_t dev, device_t child, int capability,
467    int *capreg)
468{
469
470	return (pci_find_htcap(dev, capability, capreg));
471}
472
473static int
474vga_pci_alloc_msi(device_t dev, device_t child, int *count)
475{
476	struct vga_pci_softc *sc;
477	int error;
478
479	sc = device_get_softc(dev);
480	if (sc->vga_msi_child != NULL)
481		return (EBUSY);
482	error = pci_alloc_msi(dev, count);
483	if (error == 0)
484		sc->vga_msi_child = child;
485	return (error);
486}
487
488static int
489vga_pci_alloc_msix(device_t dev, device_t child, int *count)
490{
491	struct vga_pci_softc *sc;
492	int error;
493
494	sc = device_get_softc(dev);
495	if (sc->vga_msi_child != NULL)
496		return (EBUSY);
497	error = pci_alloc_msix(dev, count);
498	if (error == 0)
499		sc->vga_msi_child = child;
500	return (error);
501}
502
503static int
504vga_pci_remap_msix(device_t dev, device_t child, int count,
505    const u_int *vectors)
506{
507	struct vga_pci_softc *sc;
508
509	sc = device_get_softc(dev);
510	if (sc->vga_msi_child != child)
511		return (ENXIO);
512	return (pci_remap_msix(dev, count, vectors));
513}
514
515static int
516vga_pci_release_msi(device_t dev, device_t child)
517{
518	struct vga_pci_softc *sc;
519	int error;
520
521	sc = device_get_softc(dev);
522	if (sc->vga_msi_child != child)
523		return (ENXIO);
524	error = pci_release_msi(dev);
525	if (error == 0)
526		sc->vga_msi_child = NULL;
527	return (error);
528}
529
530static int
531vga_pci_msi_count(device_t dev, device_t child)
532{
533
534	return (pci_msi_count(dev));
535}
536
537static int
538vga_pci_msix_count(device_t dev, device_t child)
539{
540
541	return (pci_msix_count(dev));
542}
543
544static bus_dma_tag_t
545vga_pci_get_dma_tag(device_t bus, device_t child)
546{
547
548	return (bus_get_dma_tag(bus));
549}
550
551static device_method_t vga_pci_methods[] = {
552	/* Device interface */
553	DEVMETHOD(device_probe,		vga_pci_probe),
554	DEVMETHOD(device_attach,	vga_pci_attach),
555	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
556	DEVMETHOD(device_suspend,	vga_pci_suspend),
557	DEVMETHOD(device_resume,	vga_pci_resume),
558
559	/* Bus interface */
560	DEVMETHOD(bus_read_ivar,	vga_pci_read_ivar),
561	DEVMETHOD(bus_write_ivar,	vga_pci_write_ivar),
562	DEVMETHOD(bus_setup_intr,	vga_pci_setup_intr),
563	DEVMETHOD(bus_teardown_intr,	vga_pci_teardown_intr),
564	DEVMETHOD(bus_alloc_resource,	vga_pci_alloc_resource),
565	DEVMETHOD(bus_release_resource,	vga_pci_release_resource),
566	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
567	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
568	DEVMETHOD(bus_get_dma_tag,	vga_pci_get_dma_tag),
569
570	/* PCI interface */
571	DEVMETHOD(pci_read_config,	vga_pci_read_config),
572	DEVMETHOD(pci_write_config,	vga_pci_write_config),
573	DEVMETHOD(pci_enable_busmaster,	vga_pci_enable_busmaster),
574	DEVMETHOD(pci_disable_busmaster, vga_pci_disable_busmaster),
575	DEVMETHOD(pci_enable_io,	vga_pci_enable_io),
576	DEVMETHOD(pci_disable_io,	vga_pci_disable_io),
577	DEVMETHOD(pci_get_vpd_ident,	vga_pci_get_vpd_ident),
578	DEVMETHOD(pci_get_vpd_readonly,	vga_pci_get_vpd_readonly),
579	DEVMETHOD(pci_get_powerstate,	vga_pci_get_powerstate),
580	DEVMETHOD(pci_set_powerstate,	vga_pci_set_powerstate),
581	DEVMETHOD(pci_assign_interrupt,	vga_pci_assign_interrupt),
582	DEVMETHOD(pci_find_cap,		vga_pci_find_cap),
583	DEVMETHOD(pci_find_extcap,	vga_pci_find_extcap),
584	DEVMETHOD(pci_find_htcap,	vga_pci_find_htcap),
585	DEVMETHOD(pci_alloc_msi,	vga_pci_alloc_msi),
586	DEVMETHOD(pci_alloc_msix,	vga_pci_alloc_msix),
587	DEVMETHOD(pci_remap_msix,	vga_pci_remap_msix),
588	DEVMETHOD(pci_release_msi,	vga_pci_release_msi),
589	DEVMETHOD(pci_msi_count,	vga_pci_msi_count),
590	DEVMETHOD(pci_msix_count,	vga_pci_msix_count),
591
592	{ 0, 0 }
593};
594
595static driver_t vga_pci_driver = {
596	"vgapci",
597	vga_pci_methods,
598	sizeof(struct vga_pci_softc),
599};
600
601static devclass_t vga_devclass;
602
603DRIVER_MODULE(vgapci, pci, vga_pci_driver, vga_devclass, 0, 0);
604