xref: /netbsd-src/sys/arch/sparc64/dev/gfb.c (revision 1dc652ef5a0bffbd0917f95e0797bad8c6fc8efd)
1 /*	$NetBSD: gfb.c,v 1.12 2023/12/20 05:33:58 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * A console driver for Sun XVR-1000 graphics cards
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: gfb.c,v 1.12 2023/12/20 05:33:58 thorpej 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/lwp.h>
40 #include <sys/kauth.h>
41 #include <sys/kmem.h>
42 
43 #include <uvm/uvm_extern.h>
44 
45 #include <dev/videomode/videomode.h>
46 
47 #include <sys/bus.h>
48 #include <machine/autoconf.h>
49 #include <machine/openfirm.h>
50 
51 #include <dev/wscons/wsdisplayvar.h>
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wsfont/wsfont.h>
54 #include <dev/rasops/rasops.h>
55 #include <dev/wscons/wsdisplay_vconsvar.h>
56 
57 
58 struct gfb_softc {
59 	device_t sc_dev;
60 
61 	bus_space_tag_t sc_memt;
62 
63 	bus_space_handle_t sc_fbh;
64 	bus_addr_t sc_fb_paddr;
65 
66 	int sc_width, sc_height, sc_depth, sc_stride, sc_fblen;
67 	int sc_locked;
68 	void *sc_fbaddr;
69 	struct vcons_screen sc_console_screen;
70 	struct wsscreen_descr sc_defaultscreen_descr;
71 	const struct wsscreen_descr *sc_screens[1];
72 	struct wsscreen_list sc_screenlist;
73 	struct vcons_data vd;
74 	int sc_mode;
75 	int sc_node;
76 	u_char sc_cmap_red[256];
77 	u_char sc_cmap_green[256];
78 	u_char sc_cmap_blue[256];
79 };
80 
81 static int	gfb_match(device_t, cfdata_t, void *);
82 static void	gfb_attach(device_t, device_t, void *);
83 
84 CFATTACH_DECL_NEW(gfb, sizeof(struct gfb_softc),
85     gfb_match, gfb_attach, NULL, NULL);
86 
87 extern const u_char rasops_cmap[768];
88 
89 static int	gfb_ioctl(void *, void *, u_long, void *, int,
90 			     struct lwp *);
91 static paddr_t	gfb_mmap(void *, void *, off_t, int);
92 static void	gfb_init_screen(void *, struct vcons_screen *, int, long *);
93 
94 static int	gfb_putcmap(struct gfb_softc *, struct wsdisplay_cmap *);
95 static int 	gfb_getcmap(struct gfb_softc *, struct wsdisplay_cmap *);
96 static void	gfb_restore_palette(struct gfb_softc *);
97 static int 	gfb_putpalreg(struct gfb_softc *, uint8_t, uint8_t,
98 			    uint8_t, uint8_t);
99 
100 struct wsdisplay_accessops gfb_accessops = {
101 	gfb_ioctl,
102 	gfb_mmap,
103 	NULL,	/* alloc_screen */
104 	NULL,	/* free_screen */
105 	NULL,	/* show_screen */
106 	NULL, 	/* load_font */
107 	NULL,	/* pollc */
108 	NULL	/* scroll */
109 };
110 
111 extern int prom_stdout_node;
112 
113 static int
gfb_match(device_t parent,cfdata_t match,void * aux)114 gfb_match(device_t parent, cfdata_t match, void *aux)
115 {
116 	struct mainbus_attach_args *ma = aux;
117 
118 	if (strcmp(ma->ma_name, "SUNW,gfb") == 0 )
119 		return 100;
120 	return 0;
121 }
122 
123 static void
gfb_attach(device_t parent,device_t self,void * aux)124 gfb_attach(device_t parent, device_t self, void *aux)
125 {
126 	struct gfb_softc	*sc = device_private(self);
127 	struct mainbus_attach_args	*ma = aux;
128 	struct rasops_info	*ri;
129 	struct wsemuldisplaydev_attach_args aa;
130 	unsigned long		defattr;
131 	bool			is_console;
132 	int i, j;
133 
134 	sc->sc_memt = ma->ma_bustag;
135 	sc->sc_dev = self;
136 	sc->sc_node = ma->ma_node;
137 
138 	if (ma->ma_nreg < 7) {
139 		aprint_error("%s: can't find the fb range\n",
140 		    device_xname(self));
141 		return;
142 	}
143 
144 	is_console = (prom_stdout_node == ma->ma_node);
145 
146 	aprint_normal(": Sun XVR-1000%s\n", is_console ? " (console)" : "");
147 
148 	sc->sc_depth = 32;
149 	sc->sc_stride = 0x4000;
150 	sc->sc_height = prom_getpropint(sc->sc_node, "height", 0);
151 	sc->sc_width = prom_getpropint(sc->sc_node, "width", 0);
152 	sc->sc_fblen = sc->sc_stride * sc->sc_height;
153 
154 	if (sc->sc_fblen == 0) {
155 		aprint_error("%s: not set up by firmware, can't continue.\n",
156 		    device_xname(self));
157 		return;
158 	}
159 
160 	sc->sc_locked = 0;
161 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
162 
163 	if (bus_space_map(sc->sc_memt, ma->ma_reg[6].ur_paddr,
164 	    sc->sc_fblen, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
165 	    &sc->sc_fbh)) {
166 		printf(": failed to map the framebuffer\n");
167 		return;
168 	}
169 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
170 	sc->sc_fb_paddr = ma->ma_reg[6].ur_paddr;
171 
172 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
173 		"default",
174 		0, 0,
175 		NULL,
176 		8, 16,
177 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
178 		NULL
179 	};
180 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
181 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
182 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
183 	sc->sc_locked = 0;
184 
185 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
186 	    &gfb_accessops);
187 	sc->vd.init_screen = gfb_init_screen;
188 
189 	ri = &sc->sc_console_screen.scr_ri;
190 
191 	j = 0;
192 	for (i = 0; i < 256; i++) {
193 
194 		sc->sc_cmap_red[i] = rasops_cmap[j];
195 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
196 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
197 		gfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
198 		    rasops_cmap[j + 2]);
199 		j += 3;
200 	}
201 
202 	if (is_console) {
203 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
204 		    &defattr);
205 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
206 
207 #if notyet
208 		gfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
209 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
210 #endif
211 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
212 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
213 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
214 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
215 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
216 		    defattr);
217 		vcons_replay_msgbuf(&sc->sc_console_screen);
218 	} else {
219 		/*
220 		 * since we're not the console we can postpone the rest
221 		 * until someone actually allocates a screen for us
222 		 */
223 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
224 	}
225 
226 	aa.console = is_console;
227 	aa.scrdata = &sc->sc_screenlist;
228 	aa.accessops = &gfb_accessops;
229 	aa.accesscookie = &sc->vd;
230 
231 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARGS_NONE);
232 
233 #ifdef GFB_DEBUG
234 	/*
235 	 * now dump a register range
236 	 * try 1, 2 and 4 since they're only 0x2000 each
237 	 */
238 	bus_space_handle_t regh;
239 
240 	if (bus_space_map(sc->sc_memt, ma->ma_reg[3].ur_paddr,
241 	    0x2000, BUS_SPACE_MAP_LINEAR, &regh) == 0) {
242 		for (i = 0; i < 0x200; i += 32) {
243 			printf("%04x", i);
244 			for (j = 0; j < 32; j += 4) {
245 				printf(" %08x", bus_space_read_4(sc->sc_memt,
246 				    regh, i + j));
247 			}
248 			printf("\n");
249 		}
250 		bus_space_unmap(sc->sc_memt, regh, 0x2000);
251 	}
252 #endif
253 }
254 
255 static int
gfb_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)256 gfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
257 	struct lwp *l)
258 {
259 	struct vcons_data *vd = v;
260 	struct gfb_softc *sc = vd->cookie;
261 	struct wsdisplay_fbinfo *wdf;
262 	struct vcons_screen *ms = vd->active;
263 
264 	switch (cmd) {
265 
266 		case WSDISPLAYIO_GTYPE:
267 			*(u_int *)data = WSDISPLAY_TYPE_XVR1000;
268 			return 0;
269 
270 		case WSDISPLAYIO_GINFO:
271 			if (ms == NULL)
272 				return ENODEV;
273 			wdf = (void *)data;
274 			wdf->height = ms->scr_ri.ri_height;
275 			wdf->width = ms->scr_ri.ri_width;
276 			wdf->depth = ms->scr_ri.ri_depth;
277 			wdf->cmsize = 256;
278 			return 0;
279 
280 		case WSDISPLAYIO_GETCMAP:
281 			return gfb_getcmap(sc,
282 			    (struct wsdisplay_cmap *)data);
283 
284 		case WSDISPLAYIO_PUTCMAP:
285 			return gfb_putcmap(sc,
286 			    (struct wsdisplay_cmap *)data);
287 
288 		case WSDISPLAYIO_LINEBYTES:
289 			*(u_int *)data = sc->sc_stride;
290 			return 0;
291 
292 		case WSDISPLAYIO_SMODE:
293 			{
294 				int new_mode = *(int*)data;
295 
296 				/* notify the bus backend */
297 				if (new_mode != sc->sc_mode) {
298 					sc->sc_mode = new_mode;
299 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
300 						gfb_restore_palette(sc);
301 						vcons_redraw_screen(ms);
302 					}
303 				}
304 			}
305 			return 0;
306 		case WSDISPLAYIO_GET_FBINFO:
307 			{
308 				struct wsdisplayio_fbinfo *fbi = data;
309 				return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
310 			}
311 	}
312 	return EPASSTHROUGH;
313 }
314 
315 static paddr_t
gfb_mmap(void * v,void * vs,off_t offset,int prot)316 gfb_mmap(void *v, void *vs, off_t offset, int prot)
317 {
318 	struct vcons_data *vd = v;
319 	struct gfb_softc *sc = vd->cookie;
320 	paddr_t pa;
321 
322 	/* 'regular' framebuffer mmap()ing */
323 	if (offset < sc->sc_fblen) {
324 		/*
325 		 * XXX
326 		 * BUS_SPACE_MAP_PREFETCHABLE produces artifacts with X, but
327 		 * not with the console code, so don't set it here
328 		 */
329 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb_paddr + offset, 0,
330 		    prot, BUS_SPACE_MAP_LINEAR);
331 		return pa;
332 	}
333 
334 	/*
335 	 * restrict all other mappings to processes with superuser privileges
336 	 * or the kernel itself
337 	 */
338 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
339 	    NULL, NULL, NULL, NULL) != 0) {
340 		aprint_normal("%s: mmap() rejected.\n",
341 		    device_xname(sc->sc_dev));
342 		return -1;
343 	}
344 
345 	/* let userland map other ranges */
346 
347 	return -1;
348 }
349 
350 static void
gfb_init_screen(void * cookie,struct vcons_screen * scr,int existing,long * defattr)351 gfb_init_screen(void *cookie, struct vcons_screen *scr,
352     int existing, long *defattr)
353 {
354 	struct gfb_softc *sc = cookie;
355 	struct rasops_info *ri = &scr->scr_ri;
356 
357 	ri->ri_depth = sc->sc_depth;
358 	ri->ri_width = sc->sc_width;
359 	ri->ri_height = sc->sc_height;
360 	ri->ri_stride = sc->sc_stride;
361 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_ENABLE_ALPHA;
362 
363 	ri->ri_bits = (char *)sc->sc_fbaddr;
364 	scr->scr_flags |= VCONS_DONT_READ;
365 
366 	if (existing) {
367 		ri->ri_flg |= RI_CLEAR;
368 	}
369 
370 	/* explicitly request BGR in case the default changes */
371 	ri->ri_rnum = 8;
372 	ri->ri_gnum = 8;
373 	ri->ri_bnum = 8;
374 	ri->ri_rpos = 0;
375 	ri->ri_gpos = 8;
376 	ri->ri_bpos = 16;
377 
378 	rasops_init(ri, 0, 0);
379 	ri->ri_caps = WSSCREEN_WSCOLORS;
380 
381 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
382 		    sc->sc_width / ri->ri_font->fontwidth);
383 
384 	ri->ri_hw = scr;
385 #if 0
386 	ri->ri_ops.copyrows = gfb_copyrows;
387 	ri->ri_ops.copycols = gfb_copycols;
388 	ri->ri_ops.cursor = gfb_cursor;
389 	ri->ri_ops.eraserows = gfb_eraserows;
390 	ri->ri_ops.erasecols = gfb_erasecols;
391 	ri->ri_ops.putchar = gfb_putchar;
392 #endif
393 }
394 
395 static int
gfb_putcmap(struct gfb_softc * sc,struct wsdisplay_cmap * cm)396 gfb_putcmap(struct gfb_softc *sc, struct wsdisplay_cmap *cm)
397 {
398 	u_char *r, *g, *b;
399 	u_int index = cm->index;
400 	u_int count = cm->count;
401 	int i, error;
402 	u_char rbuf[256], gbuf[256], bbuf[256];
403 
404 #ifdef GFB_DEBUG
405 	aprint_debug("putcmap: %d %d\n",index, count);
406 #endif
407 	if (cm->index >= 256 || cm->count > 256 ||
408 	    (cm->index + cm->count) > 256)
409 		return EINVAL;
410 	error = copyin(cm->red, &rbuf[index], count);
411 	if (error)
412 		return error;
413 	error = copyin(cm->green, &gbuf[index], count);
414 	if (error)
415 		return error;
416 	error = copyin(cm->blue, &bbuf[index], count);
417 	if (error)
418 		return error;
419 
420 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
421 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
422 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
423 
424 	r = &sc->sc_cmap_red[index];
425 	g = &sc->sc_cmap_green[index];
426 	b = &sc->sc_cmap_blue[index];
427 
428 	for (i = 0; i < count; i++) {
429 		gfb_putpalreg(sc, index, *r, *g, *b);
430 		index++;
431 		r++, g++, b++;
432 	}
433 	return 0;
434 }
435 
436 static int
gfb_getcmap(struct gfb_softc * sc,struct wsdisplay_cmap * cm)437 gfb_getcmap(struct gfb_softc *sc, struct wsdisplay_cmap *cm)
438 {
439 	u_int index = cm->index;
440 	u_int count = cm->count;
441 	int error;
442 
443 	if (index >= 255 || count > 256 || index + count > 256)
444 		return EINVAL;
445 
446 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
447 	if (error)
448 		return error;
449 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
450 	if (error)
451 		return error;
452 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
453 	if (error)
454 		return error;
455 
456 	return 0;
457 }
458 
459 static void
gfb_restore_palette(struct gfb_softc * sc)460 gfb_restore_palette(struct gfb_softc *sc)
461 {
462 	int i;
463 
464 	for (i = 0; i < (1 << sc->sc_depth); i++) {
465 		gfb_putpalreg(sc, i, sc->sc_cmap_red[i],
466 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
467 	}
468 }
469 
470 static int
gfb_putpalreg(struct gfb_softc * sc,uint8_t idx,uint8_t r,uint8_t g,uint8_t b)471 gfb_putpalreg(struct gfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
472     uint8_t b)
473 {
474 	return 0;
475 }
476