xref: /netbsd-src/sys/dev/pci/genfb_pci.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /*	$NetBSD: genfb_pci.c,v 1.23 2009/08/24 11:32:49 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 Michael Lorenz
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: genfb_pci.c,v 1.23 2009/08/24 11:32:49 jmcneill Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/mutex.h>
38 #include <sys/ioctl.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/kauth.h>
42 
43 #include <dev/pci/pcidevs.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pciio.h>
47 
48 #include <dev/wsfb/genfbvar.h>
49 
50 #include <dev/pci/genfb_pcivar.h>
51 
52 #include "opt_wsfb.h"
53 #include "opt_genfb.h"
54 
55 #ifdef GENFB_PCI_DEBUG
56 # define DPRINTF printf
57 #else
58 # define DPRINTF while (0) printf
59 #endif
60 
61 static int	pci_genfb_match(device_t, cfdata_t, void *);
62 static void	pci_genfb_attach(device_t, device_t, void *);
63 static int	pci_genfb_ioctl(void *, void *, u_long, void *, int,
64 		    struct lwp *);
65 static paddr_t	pci_genfb_mmap(void *, void *, off_t, int);
66 static int	pci_genfb_borrow(void *, bus_addr_t, bus_space_handle_t *);
67 static int	pci_genfb_drm_print(void *, const char *);
68 
69 CFATTACH_DECL(genfb_pci, sizeof(struct pci_genfb_softc),
70     pci_genfb_match, pci_genfb_attach, NULL, NULL);
71 
72 static int
73 pci_genfb_match(device_t parent, cfdata_t match, void *aux)
74 {
75 	struct pci_attach_args *pa = aux;
76 	int matchlvl = 1;
77 
78 	if (!genfb_is_enabled())
79 		return 0;	/* explicitly disabled by MD code */
80 
81 	if (genfb_is_console())
82 		matchlvl = 5;	/* beat VGA */
83 
84 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
85 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
86 		return matchlvl;
87 
88 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
89 		return matchlvl;
90 
91 	return 0;
92 }
93 
94 static void
95 pci_genfb_attach(device_t parent, device_t self, void *aux)
96 {
97 	struct pci_genfb_softc *sc = device_private(self);
98 	struct pci_attach_args *pa = aux;
99 	struct genfb_ops ops;
100 	int idx, bar, type;
101 	char devinfo[256];
102 
103 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
104 	aprint_naive("\n");
105 	aprint_normal(": %s\n", devinfo);
106 
107 	sc->sc_memt = pa->pa_memt;
108 	sc->sc_iot = pa->pa_iot;
109 	sc->sc_pc = pa->pa_pc;
110 	sc->sc_pcitag = pa->pa_tag;
111 	sc->sc_want_wsfb = 0;
112 
113 	genfb_init(&sc->sc_gen);
114 
115 	/* firmware / MD code responsible for restoring the display */
116 	if (sc->sc_gen.sc_pmfcb == NULL)
117 		pmf_device_register(self, NULL, NULL);
118 	else
119 		pmf_device_register(self,
120 		    sc->sc_gen.sc_pmfcb->gpc_suspend,
121 		    sc->sc_gen.sc_pmfcb->gpc_resume);
122 
123 	if ((sc->sc_gen.sc_width == 0) || (sc->sc_gen.sc_fbsize == 0)) {
124 		aprint_debug_dev(self, "not configured by firmware\n");
125 		return;
126 	}
127 
128 	if (bus_space_map(sc->sc_memt, sc->sc_gen.sc_fboffset,
129 	    sc->sc_gen.sc_fbsize, BUS_SPACE_MAP_LINEAR, &sc->sc_memh) != 0) {
130 
131 		aprint_error_dev(self, "unable to map the framebuffer\n");
132 		return;
133 	}
134 	sc->sc_gen.sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_memh);
135 
136 	/* mmap()able bus ranges */
137 	idx = 0;
138 	bar = 0x10;
139 	while (bar < 0x30) {
140 
141 		type = pci_mapreg_type(sc->sc_pc, sc->sc_pcitag, bar);
142 		if ((type == PCI_MAPREG_TYPE_MEM) ||
143 		    (type == PCI_MAPREG_TYPE_ROM)) {
144 
145 			pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, bar, type,
146 			    &sc->sc_ranges[idx].offset,
147 			    &sc->sc_ranges[idx].size,
148 			    &sc->sc_ranges[idx].flags);
149 			idx++;
150 		}
151 		sc->sc_bars[(bar - 0x10) >> 2] =
152 		    pci_conf_read(sc->sc_pc, sc->sc_pcitag, bar);
153 		bar += 4;
154 	}
155 	sc->sc_ranges_used = idx;
156 
157 	ops.genfb_ioctl = pci_genfb_ioctl;
158 	ops.genfb_mmap = pci_genfb_mmap;
159 	ops.genfb_borrow = pci_genfb_borrow;
160 
161 	if (genfb_attach(&sc->sc_gen, &ops) == 0) {
162 
163 		/* now try to attach a DRM */
164 		config_found_ia(self, "drm", aux, pci_genfb_drm_print);
165 	}
166 }
167 
168 static int
169 pci_genfb_drm_print(void *aux, const char *pnp)
170 {
171 	if (pnp)
172 		aprint_normal("drm at %s", pnp);
173 	return (UNCONF);
174 }
175 
176 
177 static int
178 pci_genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
179     struct lwp *l)
180 {
181 	struct pci_genfb_softc *sc = v;
182 
183 	switch (cmd) {
184 		case WSDISPLAYIO_GTYPE:
185 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
186 			return 0;
187 
188 		/* PCI config read/write passthrough. */
189 		case PCI_IOC_CFGREAD:
190 		case PCI_IOC_CFGWRITE:
191 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
192 			    cmd, data, flag, l));
193 		case WSDISPLAYIO_SMODE:
194 			{
195 				int new_mode = *(int*)data, i;
196 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
197 					for (i = 0; i < 9; i++)
198 						pci_conf_write(sc->sc_pc,
199 						     sc->sc_pcitag,
200 						     0x10 + (i << 2),
201 						     sc->sc_bars[i]);
202 				}
203 			}
204 			return 0;
205 	}
206 
207 	return EPASSTHROUGH;
208 }
209 
210 static paddr_t
211 pci_genfb_mmap(void *v, void *vs, off_t offset, int prot)
212 {
213 	struct pci_genfb_softc *sc = v;
214 	struct range *r;
215 	int i;
216 
217 	if (offset == 0)
218 		sc->sc_want_wsfb = 1;
219 
220 	/*
221 	 * regular fb mapping at 0
222 	 * since some Sun firmware likes to put PCI resources low enough
223 	 * to collide with the wsfb mapping we only allow it after asking
224 	 * for offset 0
225 	 */
226 	DPRINTF("%s: %08x limit %08x\n", __func__, (uint32_t)offset,
227 	    (uint32_t)sc->sc_gen.sc_fbsize);
228 	if ((offset >= 0) && (offset < sc->sc_gen.sc_fbsize) &&
229 	    (sc->sc_want_wsfb == 1)) {
230 
231 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
232 		   offset, prot, BUS_SPACE_MAP_LINEAR);
233 	}
234 
235 	/*
236 	 * restrict all other mappings to processes with superuser privileges
237 	 * or the kernel itself
238 	 */
239 	if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
240 	    NULL) != 0) {
241 		aprint_normal_dev(&sc->sc_gen.sc_dev, "mmap() rejected.\n");
242 		return -1;
243 	}
244 
245 #ifdef WSFB_FAKE_VGA_FB
246 	if ((offset >= 0xa0000) && (offset < 0xbffff)) {
247 
248 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
249 		   offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
250 	}
251 #endif
252 
253 	/*
254 	 * XXX this should be generalized, let's just
255 	 * #define PCI_IOAREA_PADDR
256 	 * #define PCI_IOAREA_OFFSET
257 	 * #define PCI_IOAREA_SIZE
258 	 * somewhere in a MD header and compile this code only if all are
259 	 * present
260 	 */
261 #ifdef PCI_MAGIC_IO_RANGE
262 	/* allow to map our IO space */
263 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
264 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
265 		return bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
266 		    0, prot, BUS_SPACE_MAP_LINEAR);
267 	}
268 #endif
269 
270 	/* allow to mmap() our BARs */
271 	/* maybe the ROM BAR too? */
272 	for (i = 0; i < sc->sc_ranges_used; i++) {
273 
274 		r = &sc->sc_ranges[i];
275 		if ((offset >= r->offset) && (offset < (r->offset + r->size))) {
276 			return bus_space_mmap(sc->sc_memt, offset, 0, prot,
277 			    r->flags);
278 		}
279 	}
280 
281 	return -1;
282 }
283 
284 int
285 pci_genfb_borrow(void *opaque, bus_addr_t addr, bus_space_handle_t *hdlp)
286 {
287 	struct pci_genfb_softc *sc = opaque;
288 
289 	if (sc == NULL)
290 		return 0;
291 	if (!sc->sc_gen.sc_fboffset)
292 		return 0;
293 	if (sc->sc_gen.sc_fboffset != addr)
294 		return 0;
295 	*hdlp = sc->sc_memh;
296 	return 1;
297 }
298