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