xref: /netbsd/sys/arch/macppc/dev/ofb.c (revision beecddb6)
1 /*	$NetBSD: ofb.c,v 1.72 2021/08/07 16:18:57 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: ofb.c,v 1.72 2021/08/07 16:18:57 thorpej Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/buf.h>
35 #include <sys/conf.h>
36 #include <sys/device.h>
37 #include <sys/ioctl.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/systm.h>
41 #include <sys/kauth.h>
42 #include <sys/lwp.h>
43 
44 #include <dev/pci/pcidevs.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pciio.h>
48 
49 #include <dev/wscons/wsconsio.h>
50 #include <dev/wscons/wsdisplayvar.h>
51 #include <dev/rasops/rasops.h>
52 #include <dev/wsfont/wsfont.h>
53 
54 #include <dev/ofw/openfirm.h>
55 #include <dev/ofw/ofw_pci.h>
56 
57 #include <sys/bus.h>
58 #include <machine/autoconf.h>
59 #include <machine/grfioctl.h>
60 
61 #include <dev/wscons/wsdisplay_vconsvar.h>
62 
63 #include <powerpc/oea/ofw_rasconsvar.h>
64 
65 struct ofb_softc {
66 	device_t sc_dev;
67 
68 	pci_chipset_tag_t sc_pc;
69 	pcitag_t sc_pcitag;
70 	bus_space_tag_t sc_memt;
71 	bus_space_tag_t sc_iot;
72 
73 	u_int32_t sc_addrs[30];		/* "assigned-addresses" storage */
74 	u_char sc_cmap_red[256];
75 	u_char sc_cmap_green[256];
76 	u_char sc_cmap_blue[256];
77 
78 	int sc_node, sc_ih, sc_mode;
79 	paddr_t sc_fbaddr;
80 	uint32_t sc_fbsize;
81 
82 	struct vcons_data vd;
83 };
84 
85 static int	ofbmatch(device_t, cfdata_t, void *);
86 static void	ofbattach(device_t, device_t, void *);
87 
88 CFATTACH_DECL_NEW(ofb, sizeof(struct ofb_softc),
89     ofbmatch, ofbattach, NULL, NULL);
90 
91 const struct wsscreen_descr *_ofb_scrlist[] = {
92 	&rascons_stdscreen,
93 	/* XXX other formats, graphics screen? */
94 };
95 
96 struct wsscreen_list ofb_screenlist = {
97 	sizeof(_ofb_scrlist) / sizeof(struct wsscreen_descr *), _ofb_scrlist
98 };
99 
100 static int	ofb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
101 static paddr_t	ofb_mmap(void *, void *, off_t, int);
102 
103 static void	ofb_init_screen(void *, struct vcons_screen *, int, long *);
104 
105 struct wsdisplay_accessops ofb_accessops = {
106 	ofb_ioctl,
107 	ofb_mmap,
108 	NULL,		/* vcons_alloc_screen */
109 	NULL,		/* vcons_free_screen */
110 	NULL,		/* vcons_show_screen */
111 	NULL		/* load_font */
112 };
113 
114 static void	ofb_putpalreg(struct ofb_softc *, int, uint8_t, uint8_t,
115     uint8_t);
116 
117 static int	ofb_getcmap(struct ofb_softc *, struct wsdisplay_cmap *);
118 static int	ofb_putcmap(struct ofb_softc *, struct wsdisplay_cmap *);
119 static void	ofb_init_cmap(struct ofb_softc *);
120 static int	ofb_drm_print(void *, const char *);
121 
122 extern const u_char rasops_cmap[768];
123 
124 extern int console_node;
125 extern int console_instance;
126 
127 static int
ofbmatch(device_t parent,cfdata_t match,void * aux)128 ofbmatch(device_t parent, cfdata_t match, void *aux)
129 {
130 	struct pci_attach_args *pa = aux;
131 
132 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
133 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_CONTROL)
134 		return 1;
135 
136 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY)
137 		return 1;
138 
139 	return 0;
140 }
141 
142 static void
ofbattach(device_t parent,device_t self,void * aux)143 ofbattach(device_t parent, device_t self, void *aux)
144 {
145 	struct ofb_softc *sc = device_private(self);
146 	struct pci_attach_args *pa = aux;
147 	struct wsemuldisplaydev_attach_args a;
148 	struct rasops_info *ri = &rascons_console_screen.scr_ri;
149 	long defattr;
150 	int console, node, sub;
151 	char devinfo[256];
152 
153 	sc->sc_dev = self;
154 
155 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
156 	printf(": %s\n", devinfo);
157 
158 	if (console_node == 0)
159 		return;
160 
161 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
162 	console = (node == console_node);
163 	if (!console) {
164 		/* check if any of the childs matches */
165 		sub = OF_child(node);
166 		while ((sub != 0) && (sub != console_node)) {
167 			sub = OF_peer(sub);
168 		}
169 		if (sub == console_node) {
170 			console = true;
171 		}
172 	}
173 
174 	sc->sc_memt = pa->pa_memt;
175 	sc->sc_iot = pa->pa_iot;
176 	sc->sc_pc = pa->pa_pc;
177 	sc->sc_pcitag = pa->pa_tag;
178 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
179 
180 	if (!console)
181 		return;
182 
183 	vcons_init(&sc->vd, sc, &rascons_stdscreen, &ofb_accessops);
184 	sc->vd.init_screen = ofb_init_screen;
185 
186 	sc->sc_node = console_node;
187 
188 	sc->sc_ih = console_instance;
189 	vcons_init_screen(&sc->vd, &rascons_console_screen, 1, &defattr);
190 	rascons_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
191 
192 	printf("%s: %d x %d, %dbpp\n", device_xname(self),
193 	       ri->ri_width, ri->ri_height, ri->ri_depth);
194 
195 	sc->sc_fbaddr = 0;
196 	if (OF_getprop(sc->sc_node, "address", &sc->sc_fbaddr, 4) != 4)
197 		OF_interpret("frame-buffer-adr", 0, 1, &sc->sc_fbaddr);
198 	if (sc->sc_fbaddr == 0) {
199 		printf("%s: Unable to find the framebuffer address.\n",
200 		    device_xname(sc->sc_dev));
201 		return;
202 	}
203 	sc->sc_fbsize = round_page(ri->ri_stride * ri->ri_height);
204 
205 	/* XXX */
206 	if (OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
207 	    sizeof(sc->sc_addrs)) == -1) {
208 		sc->sc_node = OF_parent(sc->sc_node);
209 		OF_getprop(sc->sc_node, "assigned-addresses", sc->sc_addrs,
210 		    sizeof(sc->sc_addrs));
211 	}
212 
213 	ofb_init_cmap(sc);
214 
215 	a.console = console;
216 	a.scrdata = &ofb_screenlist;
217 	a.accessops = &ofb_accessops;
218 	a.accesscookie = &sc->vd;
219 
220 	config_found(self, &a, wsemuldisplaydevprint,
221 	    CFARGS(.iattr = "wsemuldisplaydev"));
222 
223 	config_found(self, aux, ofb_drm_print,
224 	    CFARGS(.iattr = "drm"));
225 }
226 
227 static int
ofb_drm_print(void * aux,const char * pnp)228 ofb_drm_print(void *aux, const char *pnp)
229 {
230 	if (pnp)
231 		aprint_normal("direct rendering for %s", pnp);
232 	return (UNSUPP);
233 }
234 
235 static void
ofb_init_screen(void * cookie,struct vcons_screen * scr,int existing,long * defattr)236 ofb_init_screen(void *cookie, struct vcons_screen *scr,
237     int existing, long *defattr)
238 {
239 	struct ofb_softc *sc = cookie;
240 	struct rasops_info *ri = &scr->scr_ri;
241 
242 	if (scr != &rascons_console_screen) {
243 		rascons_init_rasops(sc->sc_node, ri);
244 	}
245 }
246 
247 static int
ofb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)248 ofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
249 {
250 	struct vcons_data *vd = v;
251 	struct ofb_softc *sc = vd->cookie;
252 	struct wsdisplay_fbinfo *wdf;
253 	struct vcons_screen *ms = vd->active;
254 	struct grfinfo *gm;
255 
256 	switch (cmd) {
257 	case WSDISPLAYIO_GTYPE:
258 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;	/* XXX ? */
259 		return 0;
260 
261 	case WSDISPLAYIO_GINFO:
262 		/* we won't get here without any screen anyway */
263 		if (ms != NULL) {
264 			wdf = (void *)data;
265 			wdf->height = ms->scr_ri.ri_height;
266 			wdf->width = ms->scr_ri.ri_width;
267 			wdf->depth = ms->scr_ri.ri_depth;
268 			wdf->cmsize = 256;
269 			return 0;
270 		} else
271 			return ENODEV;
272 
273 	case WSDISPLAYIO_GETCMAP:
274 		return ofb_getcmap(sc, (struct wsdisplay_cmap *)data);
275 
276 	case WSDISPLAYIO_PUTCMAP:
277 		return ofb_putcmap(sc, (struct wsdisplay_cmap *)data);
278 
279 	/* XXX There are no way to know framebuffer pa from a user program. */
280 	case GRFIOCGINFO:
281 		if (ms != NULL) {
282 			gm = (void *)data;
283 			memset(gm, 0, sizeof(struct grfinfo));
284 			gm->gd_fbaddr = (void *)sc->sc_fbaddr;
285 			gm->gd_fbrowbytes = ms->scr_ri.ri_stride;
286 			return 0;
287 		} else
288 			return ENODEV;
289 	case WSDISPLAYIO_LINEBYTES:
290 		{
291 			*(int *)data = ms->scr_ri.ri_stride;
292 			return 0;
293 		}
294 	case WSDISPLAYIO_SMODE:
295 		{
296 			int new_mode = *(int*)data;
297 			if (new_mode != sc->sc_mode)
298 			{
299 				sc->sc_mode = new_mode;
300 				if (new_mode == WSDISPLAYIO_MODE_EMUL)
301 				{
302 					ofb_init_cmap(sc);
303 					vcons_redraw_screen(ms);
304 				}
305 			}
306 		}
307 		return 0;
308 	/* PCI config read/write passthrough. */
309 	case PCI_IOC_CFGREAD:
310 	case PCI_IOC_CFGWRITE:
311 		return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
312 		    cmd, data, flag, l));
313 	}
314 	return EPASSTHROUGH;
315 }
316 
317 static paddr_t
ofb_mmap(void * v,void * vs,off_t offset,int prot)318 ofb_mmap(void *v, void *vs, off_t offset, int prot)
319 {
320 	struct vcons_data *vd = v;
321 	struct ofb_softc *sc = vd->cookie;
322 	u_int32_t *ap = sc->sc_addrs;
323 	int i;
324 
325 	if (vd->active == NULL) {
326 		printf("%s: no active screen.\n", device_xname(sc->sc_dev));
327 		return -1;
328 	}
329 
330 	/* framebuffer at offset 0 */
331 	if ((offset >= 0) && (offset < sc->sc_fbsize))
332 		return bus_space_mmap(sc->sc_memt, sc->sc_fbaddr, offset, prot,
333 		    BUS_SPACE_MAP_LINEAR);
334 
335 	/*
336 	 * restrict all other mappings to processes with superuser privileges
337 	 * or the kernel itself
338 	 */
339 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
340 	    NULL, NULL, NULL, NULL) != 0) {
341 		printf("%s: mmap() rejected.\n", device_xname(sc->sc_dev));
342 		return -1;
343 	}
344 
345 	/* let them mmap() 0xa0000 - 0xbffff if it's not covered above */
346 #ifdef OFB_FAKE_VGA_FB
347 	if (offset >=0xa0000 && offset < 0xbffff)
348 		return sc->sc_fbaddr + offset - 0xa0000;
349 #endif
350 
351 	/* allow to map our IO space */
352 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
353 		return bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
354 		    BUS_SPACE_MAP_LINEAR);
355 	}
356 
357 	for (i = 0; i < 6; i++) {
358 		switch (ap[0] & OFW_PCI_PHYS_HI_SPACEMASK) {
359 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
360 			if (offset >= ap[2] && offset < ap[2] + ap[4])
361 				return bus_space_mmap(sc->sc_memt, offset, 0,
362 				    prot, BUS_SPACE_MAP_LINEAR);
363 		}
364 		ap += 5;
365 	}
366 	return -1;
367 }
368 
369 static int
ofb_getcmap(struct ofb_softc * sc,struct wsdisplay_cmap * cm)370 ofb_getcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
371 {
372 	u_int index = cm->index;
373 	u_int count = cm->count;
374 	int error;
375 
376 	if (index >= 256 || count > 256 || index + count > 256)
377 		return EINVAL;
378 
379 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
380 	if (error)
381 		return error;
382 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
383 	if (error)
384 		return error;
385 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
386 	if (error)
387 		return error;
388 
389 	return 0;
390 }
391 
392 int
ofb_putcmap(struct ofb_softc * sc,struct wsdisplay_cmap * cm)393 ofb_putcmap(struct ofb_softc *sc, struct wsdisplay_cmap *cm)
394 {
395 	u_int index = cm->index;
396 	u_int count = cm->count;
397 	int i, error;
398 	u_char rbuf[256], gbuf[256], bbuf[256];
399 	u_char *r, *g, *b;
400 
401 	if (cm->index >= 256 || cm->count > 256 ||
402 	    (cm->index + cm->count) > 256)
403 		return EINVAL;
404 	error = copyin(cm->red, &rbuf[index], count);
405 	if (error)
406 		return error;
407 	error = copyin(cm->green, &gbuf[index], count);
408 	if (error)
409 		return error;
410 	error = copyin(cm->blue, &bbuf[index], count);
411 	if (error)
412 		return error;
413 
414 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
415 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
416 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
417 
418 	r = &sc->sc_cmap_red[index];
419 	g = &sc->sc_cmap_green[index];
420 	b = &sc->sc_cmap_blue[index];
421 	for (i = 0; i < count; i++) {
422 		OF_call_method_1("color!", sc->sc_ih, 4, *r, *g, *b, index);
423 		r++, g++, b++, index++;
424 	}
425 	return 0;
426 }
427 
428 static void
ofb_putpalreg(struct ofb_softc * sc,int idx,uint8_t r,uint8_t g,uint8_t b)429 ofb_putpalreg(struct ofb_softc *sc, int idx, uint8_t r, uint8_t g, uint8_t b)
430 {
431 	if ((idx<0) || (idx > 255))
432 		return;
433 	if (sc != NULL) {
434 		OF_call_method_1("color!", sc->sc_ih, 4, r, g, b, idx);
435 		sc->sc_cmap_red[idx] = r;
436 		sc->sc_cmap_green[idx] = g;
437 		sc->sc_cmap_blue[idx] = b;
438 	} else {
439 		OF_call_method_1("color!", console_instance, 4, r, g, b, idx);
440 	}
441 }
442 
443 static void
ofb_init_cmap(struct ofb_softc * sc)444 ofb_init_cmap(struct ofb_softc *sc)
445 {
446 	int i;
447 
448 	/* mess with the palette only when we're running in 8 bit */
449 	if (rascons_console_screen.scr_ri.ri_depth == 8) {
450 		for (i = 0; i < 256; i++) {
451 			ofb_putpalreg(sc, i, rasops_cmap[(i * 3) + 0],
452 			     rasops_cmap[(i * 3) + 1],
453 			     rasops_cmap[(i * 3) + 2]);
454 		}
455 	}
456 }
457