xref: /netbsd-src/sys/dev/wsfb/genfb.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: genfb.c,v 1.81 2021/01/27 22:42:53 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  *
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.c,v 1.81 2021/01/27 22:42:53 macallan 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/kmem.h>
42 #include <sys/reboot.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsdisplayvar.h>
48 #include <dev/rasops/rasops.h>
49 #include <dev/wsfont/wsfont.h>
50 
51 #include <dev/wscons/wsdisplay_vconsvar.h>
52 
53 #include <dev/wsfb/genfbvar.h>
54 
55 #include <dev/videomode/videomode.h>
56 #include <dev/videomode/edidvar.h>
57 
58 #ifdef GENFB_DISABLE_TEXT
59 #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \
60 		AB_VERBOSE | AB_DEBUG) )
61 #endif
62 
63 #ifdef _KERNEL_OPT
64 #include "opt_genfb.h"
65 #include "opt_wsfb.h"
66 #include "opt_rasops.h"
67 #endif
68 
69 #ifdef GENFB_DEBUG
70 #define GPRINTF panic
71 #else
72 #define GPRINTF aprint_debug
73 #endif
74 
75 #define GENFB_BRIGHTNESS_STEP 15
76 #define	GENFB_CHAR_WIDTH_MM 3
77 
78 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
79 static paddr_t	genfb_mmap(void *, void *, off_t, int);
80 static void	genfb_pollc(void *, int);
81 
82 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
83 static int	genfb_calc_hsize(struct genfb_softc *);
84 static int	genfb_calc_cols(struct genfb_softc *);
85 
86 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
87 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
88 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
89 			    uint8_t, uint8_t);
90 static void	genfb_init_palette(struct genfb_softc *);
91 
92 static void	genfb_brightness_up(device_t);
93 static void	genfb_brightness_down(device_t);
94 
95 #if GENFB_GLYPHCACHE > 0
96 static int	genfb_setup_glyphcache(struct genfb_softc *, long);
97 static void	genfb_putchar(void *, int, int, u_int, long);
98 #endif
99 
100 extern const u_char rasops_cmap[768];
101 
102 static int genfb_cnattach_called = 0;
103 static int genfb_enabled = 1;
104 
105 static struct genfb_softc *genfb_softc = NULL;
106 
107 void
108 genfb_init(struct genfb_softc *sc)
109 {
110 	prop_dictionary_t dict;
111 	uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
112 	uint64_t fboffset;
113 	bool console;
114 
115 	dict = device_properties(sc->sc_dev);
116 #ifdef GENFB_DEBUG
117 	printf("%s", prop_dictionary_externalize(dict));
118 #endif
119 	prop_dictionary_get_bool(dict, "is_console", &console);
120 
121 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
122 		GPRINTF("no width property\n");
123 		return;
124 	}
125 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
126 		GPRINTF("no height property\n");
127 		return;
128 	}
129 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
130 		GPRINTF("no depth property\n");
131 		return;
132 	}
133 
134 	if (!prop_dictionary_get_uint64(dict, "address", &fboffset)) {
135 		GPRINTF("no address property\n");
136 		return;
137 	}
138 
139 	sc->sc_fboffset = (bus_addr_t)fboffset;
140 
141 	sc->sc_fbaddr = NULL;
142 	if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
143 		sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
144 	}
145 
146 	sc->sc_shadowfb = NULL;
147 	if (!prop_dictionary_get_bool(dict, "enable_shadowfb",
148 	    &sc->sc_enable_shadowfb))
149 #ifdef GENFB_SHADOWFB
150 		sc->sc_enable_shadowfb = true;
151 #else
152 		sc->sc_enable_shadowfb = false;
153 #endif
154 
155 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
156 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
157 
158 	/*
159 	 * deal with a bug in the Raptor firmware which always sets
160 	 * stride = width even when depth != 8
161 	 */
162 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
163 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
164 
165 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
166 
167 	/* optional colour map callback */
168 	sc->sc_cmcb = NULL;
169 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
170 		if (cmap_cb != 0)
171 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
172 	}
173 
174 	/* optional pmf callback */
175 	sc->sc_pmfcb = NULL;
176 	if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
177 		if (pmf_cb != 0)
178 			sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
179 	}
180 
181 	/* optional mode callback */
182 	sc->sc_modecb = NULL;
183 	if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
184 		if (mode_cb != 0)
185 			sc->sc_modecb = (void *)(vaddr_t)mode_cb;
186 	}
187 
188 	/* optional backlight control callback */
189 	sc->sc_backlight = NULL;
190 	if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
191 		if (bl_cb != 0) {
192 			sc->sc_backlight = (void *)(vaddr_t)bl_cb;
193 			aprint_naive_dev(sc->sc_dev,
194 			    "enabling backlight control\n");
195 		}
196 	}
197 
198 	/* optional brightness control callback */
199 	sc->sc_brightness = NULL;
200 	if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
201 		if (br_cb != 0) {
202 			sc->sc_brightness = (void *)(vaddr_t)br_cb;
203 			aprint_naive_dev(sc->sc_dev,
204 			    "enabling brightness control\n");
205 			if (console &&
206 			    sc->sc_brightness->gpc_upd_parameter != NULL) {
207 				pmf_event_register(sc->sc_dev,
208 				    PMFE_DISPLAY_BRIGHTNESS_UP,
209 				    genfb_brightness_up, TRUE);
210 				pmf_event_register(sc->sc_dev,
211 				    PMFE_DISPLAY_BRIGHTNESS_DOWN,
212 				    genfb_brightness_down, TRUE);
213 			}
214 		}
215 	}
216 }
217 
218 int
219 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
220 {
221 	struct wsemuldisplaydev_attach_args aa;
222 	prop_dictionary_t dict;
223 	struct rasops_info *ri;
224 	paddr_t fb_phys;
225 	uint16_t crow;
226 	long defattr;
227 	bool console;
228 #ifdef SPLASHSCREEN
229 	int i, j;
230 	int error = ENXIO;
231 #endif
232 
233 	dict = device_properties(sc->sc_dev);
234 	prop_dictionary_get_bool(dict, "is_console", &console);
235 
236 	if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
237 		crow = 0;
238 	if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
239 	    == false)
240 		sc->sc_want_clear = true;
241 
242 	fb_phys = (paddr_t)sc->sc_fboffset;
243 	if (fb_phys == 0) {
244 		KASSERT(sc->sc_fbaddr != NULL);
245 		(void)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_fbaddr,
246 		    &fb_phys);
247 	}
248 
249 	aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
250 	    "stride %d\n",
251 	    fb_phys ? (void *)(intptr_t)fb_phys : sc->sc_fbaddr,
252 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
253 
254 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
255 		"default",
256 		0, 0,
257 		NULL,
258 		8, 16,
259 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
260 		  WSSCREEN_RESIZE,
261 		NULL
262 	};
263 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
264 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
265 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
266 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
267 	if (sc->sc_modecb != NULL)
268 		sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
269 
270 	sc->sc_accessops.ioctl = genfb_ioctl;
271 	sc->sc_accessops.mmap = genfb_mmap;
272 	sc->sc_accessops.pollc = genfb_pollc;
273 
274 	if (sc->sc_enable_shadowfb) {
275 		sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
276 		if (sc->sc_want_clear == false)
277 			memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
278 		aprint_verbose_dev(sc->sc_dev,
279 		    "shadow framebuffer enabled, size %zu KB\n",
280 		    sc->sc_fbsize >> 10);
281 	}
282 
283 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
284 	    &sc->sc_accessops);
285 	sc->vd.init_screen = genfb_init_screen;
286 
287 	/* Do not print anything between this point and the screen
288 	 * clear operation below.  Otherwise it will be lost. */
289 
290 	ri = &sc->sc_console_screen.scr_ri;
291 
292 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
293 	    &defattr);
294 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
295 
296 #if GENFB_GLYPHCACHE > 0
297 	genfb_setup_glyphcache(sc, defattr);
298 #endif
299 
300 #ifdef SPLASHSCREEN
301 /*
302  * If system isn't going to go multiuser, or user has requested to see
303  * boot text, don't render splash screen immediately
304  */
305 	if (DISABLESPLASH)
306 #endif
307 		vcons_redraw_screen(&sc->sc_console_screen);
308 
309 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
310 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
311 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
312 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
313 
314 	if (crow >= ri->ri_rows) {
315 		crow = 0;
316 		sc->sc_want_clear = 1;
317 	}
318 
319 	if (console)
320 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
321 		    defattr);
322 
323 	/* Clear the whole screen to bring it to a known state. */
324 	if (sc->sc_want_clear)
325 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
326 
327 #ifdef SPLASHSCREEN
328 	j = 0;
329 	for (i = 0; i < uimin(1 << sc->sc_depth, 256); i++) {
330 		if (i >= SPLASH_CMAP_OFFSET &&
331 		    i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
332 			splash_get_cmap(i,
333 			    &sc->sc_cmap_red[i],
334 			    &sc->sc_cmap_green[i],
335 			    &sc->sc_cmap_blue[i]);
336 		} else {
337 			sc->sc_cmap_red[i] = rasops_cmap[j];
338 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
339 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
340 		}
341 		j += 3;
342 	}
343 	genfb_restore_palette(sc);
344 
345 	sc->sc_splash.si_depth = sc->sc_depth;
346 	sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_origbits;
347 	sc->sc_splash.si_hwbits = sc->sc_fbaddr;
348 	sc->sc_splash.si_width = sc->sc_width;
349 	sc->sc_splash.si_height = sc->sc_height;
350 	sc->sc_splash.si_stride = sc->sc_stride;
351 	sc->sc_splash.si_fillrect = NULL;
352 	if (!DISABLESPLASH) {
353 		error = splash_render(&sc->sc_splash,
354 		    SPLASH_F_CENTER|SPLASH_F_FILL);
355 		if (error) {
356 			SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
357 			genfb_init_palette(sc);
358 			vcons_replay_msgbuf(&sc->sc_console_screen);
359 		}
360 	}
361 #else
362 	genfb_init_palette(sc);
363 	if (console && (boothowto & (AB_SILENT|AB_QUIET)) == 0)
364 		vcons_replay_msgbuf(&sc->sc_console_screen);
365 #endif
366 
367 	if (genfb_softc == NULL)
368 		genfb_softc = sc;
369 
370 	aa.console = console;
371 	aa.scrdata = &sc->sc_screenlist;
372 	aa.accessops = &sc->sc_accessops;
373 	aa.accesscookie = &sc->vd;
374 
375 #ifdef GENFB_DISABLE_TEXT
376 	if (!DISABLESPLASH && error == 0)
377 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
378 #endif
379 
380 	config_found_ia(sc->sc_dev, "wsemuldisplaydev", &aa,
381 	    wsemuldisplaydevprint);
382 
383 	return 0;
384 }
385 
386 static int
387 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
388 	struct lwp *l)
389 {
390 	struct vcons_data *vd = v;
391 	struct genfb_softc *sc = vd->cookie;
392 	struct wsdisplay_fbinfo *wdf;
393 	struct vcons_screen *ms = vd->active;
394 	struct wsdisplay_param *param;
395 	int new_mode, error, val, ret;
396 
397 	switch (cmd) {
398 		case WSDISPLAYIO_GINFO:
399 			if (ms == NULL)
400 				return ENODEV;
401 			wdf = (void *)data;
402 			wdf->height = ms->scr_ri.ri_height;
403 			wdf->width = ms->scr_ri.ri_width;
404 			wdf->depth = ms->scr_ri.ri_depth;
405 			wdf->cmsize = 256;
406 			return 0;
407 
408 		case WSDISPLAYIO_GETCMAP:
409 			return genfb_getcmap(sc,
410 			    (struct wsdisplay_cmap *)data);
411 
412 		case WSDISPLAYIO_PUTCMAP:
413 			return genfb_putcmap(sc,
414 			    (struct wsdisplay_cmap *)data);
415 
416 		case WSDISPLAYIO_LINEBYTES:
417 			*(u_int *)data = sc->sc_stride;
418 			return 0;
419 
420 		case WSDISPLAYIO_SMODE:
421 			new_mode = *(int *)data;
422 
423 			/* notify the bus backend */
424 			error = 0;
425 			if (sc->sc_ops.genfb_ioctl)
426 				error = sc->sc_ops.genfb_ioctl(sc, vs,
427 					    cmd, data, flag, l);
428 			if (error && error != EPASSTHROUGH)
429 				return error;
430 
431 			if (new_mode != sc->sc_mode) {
432 				sc->sc_mode = new_mode;
433 				if (sc->sc_modecb != NULL)
434 					sc->sc_modecb->gmc_setmode(sc,
435 					    sc->sc_mode);
436 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
437 					genfb_restore_palette(sc);
438 					vcons_redraw_screen(ms);
439 				}
440 			}
441 			return 0;
442 
443 		case WSDISPLAYIO_SSPLASH:
444 #if defined(SPLASHSCREEN)
445 			if(*(int *)data == 1) {
446 				SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
447 				splash_render(&sc->sc_splash,
448 						SPLASH_F_CENTER|SPLASH_F_FILL);
449 			} else {
450 				SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
451 				genfb_init_palette(sc);
452 			}
453 			vcons_redraw_screen(ms);
454 			return 0;
455 #else
456 			return ENODEV;
457 #endif
458 		case WSDISPLAYIO_GETPARAM:
459 			param = (struct wsdisplay_param *)data;
460 			switch (param->param) {
461 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
462 				if (sc->sc_brightness == NULL)
463 					return EPASSTHROUGH;
464 				param->min = 0;
465 				param->max = 255;
466 				return sc->sc_brightness->gpc_get_parameter(
467 				    sc->sc_brightness->gpc_cookie,
468 				    &param->curval);
469 			case WSDISPLAYIO_PARAM_BACKLIGHT:
470 				if (sc->sc_backlight == NULL)
471 					return EPASSTHROUGH;
472 				param->min = 0;
473 				param->max = 1;
474 				return sc->sc_backlight->gpc_get_parameter(
475 				    sc->sc_backlight->gpc_cookie,
476 				    &param->curval);
477 			}
478 			return EPASSTHROUGH;
479 
480 		case WSDISPLAYIO_SETPARAM:
481 			param = (struct wsdisplay_param *)data;
482 			switch (param->param) {
483 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
484 				if (sc->sc_brightness == NULL)
485 					return EPASSTHROUGH;
486 				val = param->curval;
487 				if (val < 0) val = 0;
488 				if (val > 255) val = 255;
489 				return sc->sc_brightness->gpc_set_parameter(
490 				    sc->sc_brightness->gpc_cookie, val);
491 			case WSDISPLAYIO_PARAM_BACKLIGHT:
492 				if (sc->sc_backlight == NULL)
493 					return EPASSTHROUGH;
494 				val = param->curval;
495 				if (val < 0) val = 0;
496 				if (val > 1) val = 1;
497 				return sc->sc_backlight->gpc_set_parameter(
498 				    sc->sc_backlight->gpc_cookie, val);
499 			}
500 			return EPASSTHROUGH;
501 	}
502 	ret = EPASSTHROUGH;
503 	if (sc->sc_ops.genfb_ioctl)
504 		ret = sc->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l);
505 	if (ret != EPASSTHROUGH)
506 		return ret;
507 	/*
508 	 * XXX
509 	 * handle these only if there either is no ioctl() handler or it didn't
510 	 * know how to deal with them. This allows bus frontends to override
511 	 * them but still provides fallback implementations
512 	 */
513 	switch (cmd) {
514 		case WSDISPLAYIO_GET_EDID: {
515 			struct wsdisplayio_edid_info *d = data;
516 			return wsdisplayio_get_edid(sc->sc_dev, d);
517 		}
518 
519 		case WSDISPLAYIO_GET_FBINFO: {
520 			struct wsdisplayio_fbinfo *fbi = data;
521 			return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
522 		}
523 	}
524 	return EPASSTHROUGH;
525 }
526 
527 static paddr_t
528 genfb_mmap(void *v, void *vs, off_t offset, int prot)
529 {
530 	struct vcons_data *vd = v;
531 	struct genfb_softc *sc = vd->cookie;
532 
533 	if (sc->sc_ops.genfb_mmap)
534 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
535 
536 	return -1;
537 }
538 
539 static void
540 genfb_pollc(void *v, int on)
541 {
542 	struct vcons_data *vd = v;
543 	struct genfb_softc *sc = vd->cookie;
544 
545 	if (sc == NULL)
546 		return;
547 
548 	if (on)
549 		genfb_enable_polling(sc->sc_dev);
550 	else
551 		genfb_disable_polling(sc->sc_dev);
552 }
553 
554 static void
555 genfb_init_screen(void *cookie, struct vcons_screen *scr,
556     int existing, long *defattr)
557 {
558 	struct genfb_softc *sc = cookie;
559 	struct rasops_info *ri = &scr->scr_ri;
560 	int wantcols;
561 	bool is_bgr, is_swapped;
562 
563 	ri->ri_depth = sc->sc_depth;
564 	ri->ri_width = sc->sc_width;
565 	ri->ri_height = sc->sc_height;
566 	ri->ri_stride = sc->sc_stride;
567 	ri->ri_flg = RI_CENTER;
568 	if (sc->sc_want_clear)
569 		ri->ri_flg |= RI_FULLCLEAR;
570 
571 	scr->scr_flags |= VCONS_LOADFONT;
572 
573 	if (sc->sc_shadowfb != NULL) {
574 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
575 		ri->ri_bits = (char *)sc->sc_shadowfb;
576 	} else {
577 		ri->ri_bits = (char *)sc->sc_fbaddr;
578 		scr->scr_flags |= VCONS_DONT_READ;
579 	}
580 
581 	if (existing && sc->sc_want_clear)
582 		ri->ri_flg |= RI_CLEAR;
583 
584 	switch (ri->ri_depth) {
585 	case 32:
586 	case 24:
587 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
588 		ri->ri_flg |= RI_ENABLE_ALPHA;
589 
590 		is_bgr = false;
591 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
592 		    "is_bgr", &is_bgr);
593 
594 		is_swapped = false;
595 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
596 		    "is_swapped", &is_swapped);
597 
598 		if (is_bgr) {
599 			/* someone requested BGR */
600 			ri->ri_rpos = 0;
601 			ri->ri_gpos = 8;
602 			ri->ri_bpos = 16;
603 		} else if (is_swapped) {
604 			/* byte-swapped, must be 32 bpp */
605 			KASSERT(ri->ri_depth == 32);
606 			ri->ri_rpos = 8;
607 			ri->ri_gpos = 16;
608 			ri->ri_bpos = 24;
609 		} else {
610 			/* assume RGB */
611 			ri->ri_rpos = 16;
612 			ri->ri_gpos = 8;
613 			ri->ri_bpos = 0;
614 		}
615 		break;
616 
617 	case 16:
618 	case 15:
619 		ri->ri_flg |= RI_ENABLE_ALPHA;
620 		break;
621 
622 	case 8:
623 		if (sc->sc_cmcb != NULL)
624 			ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
625 		break;
626 
627 	case 2:
628 		ri->ri_flg |= RI_ENABLE_ALPHA;
629 		break;
630 
631 	default:
632 		break;
633 	}
634 
635 	wantcols = genfb_calc_cols(sc);
636 
637 	rasops_init(ri, 0, wantcols);
638 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
639 		  WSSCREEN_RESIZE;
640 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
641 		    sc->sc_width / ri->ri_font->fontwidth);
642 
643 	ri->ri_hw = scr;
644 #if GENFB_GLYPHCACHE > 0
645 	sc->sc_putchar = ri->ri_ops.putchar;
646 	ri->ri_ops.putchar = genfb_putchar;
647 #endif
648 #ifdef GENFB_DISABLE_TEXT
649 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
650 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
651 #endif
652 }
653 
654 /* Returns the width of the display in millimeters, or 0 if not known. */
655 static int
656 genfb_calc_hsize(struct genfb_softc *sc)
657 {
658 	device_t dev = sc->sc_dev;
659 	prop_dictionary_t dict = device_properties(dev);
660 	prop_data_t edid_data;
661 	struct edid_info *edid;
662 	const char *edid_ptr;
663 	int hsize;
664 
665 	edid_data = prop_dictionary_get(dict, "EDID");
666 	if (edid_data == NULL || prop_data_size(edid_data) < 128)
667 		return 0;
668 
669 	edid = kmem_alloc(sizeof(*edid), KM_SLEEP);
670 
671 	edid_ptr = prop_data_value(edid_data);
672 	if (edid_parse(__UNCONST(edid_ptr), edid) == 0)
673 		hsize = (int)edid->edid_max_hsize * 10;
674 	else
675 		hsize = 0;
676 
677 	kmem_free(edid, sizeof(*edid));
678 
679 	return hsize;
680 }
681 
682 /* Return the minimum number of character columns based on DPI */
683 static int
684 genfb_calc_cols(struct genfb_softc *sc)
685 {
686 	const int hsize = genfb_calc_hsize(sc);
687 
688 	return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM);
689 }
690 
691 static int
692 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
693 {
694 	u_char *r, *g, *b;
695 	u_int index = cm->index;
696 	u_int count = cm->count;
697 	int i, error;
698 	u_char rbuf[256], gbuf[256], bbuf[256];
699 
700 #ifdef GENFB_DEBUG
701 	aprint_debug("putcmap: %d %d\n",index, count);
702 #endif
703 	if (index >= 256 || count > 256 || index + count > 256)
704 		return EINVAL;
705 
706 	error = copyin(cm->red, &rbuf[index], count);
707 	if (error)
708 		return error;
709 	error = copyin(cm->green, &gbuf[index], count);
710 	if (error)
711 		return error;
712 	error = copyin(cm->blue, &bbuf[index], count);
713 	if (error)
714 		return error;
715 
716 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
717 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
718 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
719 
720 	r = &sc->sc_cmap_red[index];
721 	g = &sc->sc_cmap_green[index];
722 	b = &sc->sc_cmap_blue[index];
723 
724 	for (i = 0; i < count; i++) {
725 		genfb_putpalreg(sc, index, *r, *g, *b);
726 		index++;
727 		r++, g++, b++;
728 	}
729 	return 0;
730 }
731 
732 static int
733 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
734 {
735 	u_int index = cm->index;
736 	u_int count = cm->count;
737 	int error;
738 
739 	if (index >= 256 || count > 256 || index + count > 256)
740 		return EINVAL;
741 
742 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
743 	if (error)
744 		return error;
745 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
746 	if (error)
747 		return error;
748 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
749 	if (error)
750 		return error;
751 
752 	return 0;
753 }
754 
755 void
756 genfb_restore_palette(struct genfb_softc *sc)
757 {
758 	int i;
759 
760 	if (sc->sc_depth <= 8) {
761 		for (i = 0; i < (1 << sc->sc_depth); i++) {
762 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
763 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
764 		}
765 	}
766 }
767 
768 static void
769 genfb_init_palette(struct genfb_softc *sc)
770 {
771 	int i, j, tmp;
772 
773 	if (sc->sc_depth == 8) {
774 		/* generate an r3g3b2 colour map */
775 		for (i = 0; i < 256; i++) {
776 			tmp = i & 0xe0;
777 			/*
778 			 * replicate bits so 0xe0 maps to a red value of 0xff
779 			 * in order to make white look actually white
780 			 */
781 			tmp |= (tmp >> 3) | (tmp >> 6);
782 			sc->sc_cmap_red[i] = tmp;
783 
784 			tmp = (i & 0x1c) << 3;
785 			tmp |= (tmp >> 3) | (tmp >> 6);
786 			sc->sc_cmap_green[i] = tmp;
787 
788 			tmp = (i & 0x03) << 6;
789 			tmp |= tmp >> 2;
790 			tmp |= tmp >> 4;
791 			sc->sc_cmap_blue[i] = tmp;
792 
793 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
794 				       sc->sc_cmap_green[i],
795 				       sc->sc_cmap_blue[i]);
796 		}
797 	} else {
798 		/* steal rasops' ANSI cmap */
799 		j = 0;
800 		for (i = 0; i < 256; i++) {
801 			sc->sc_cmap_red[i] = rasops_cmap[j];
802 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
803 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
804 			j += 3;
805 		}
806 	}
807 }
808 
809 static int
810 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
811     uint8_t b)
812 {
813 
814 	if (sc->sc_cmcb) {
815 
816 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
817 		    idx, r, g, b);
818 	}
819 	return 0;
820 }
821 
822 void
823 genfb_cnattach(void)
824 {
825 	genfb_cnattach_called = 1;
826 }
827 
828 void
829 genfb_disable(void)
830 {
831 	genfb_enabled = 0;
832 }
833 
834 int
835 genfb_is_console(void)
836 {
837 	return genfb_cnattach_called;
838 }
839 
840 int
841 genfb_is_enabled(void)
842 {
843 	return genfb_enabled;
844 }
845 
846 int
847 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
848 {
849 	struct genfb_softc *sc = genfb_softc;
850 
851 	if (sc && sc->sc_ops.genfb_borrow)
852 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
853 	return 0;
854 }
855 
856 static void
857 genfb_brightness_up(device_t dev)
858 {
859 	struct genfb_softc *sc = device_private(dev);
860 
861 	KASSERT(sc->sc_brightness != NULL &&
862 		sc->sc_brightness->gpc_upd_parameter != NULL);
863 
864 	(void)sc->sc_brightness->gpc_upd_parameter(
865 	    sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
866 }
867 
868 static void
869 genfb_brightness_down(device_t dev)
870 {
871 	struct genfb_softc *sc = device_private(dev);
872 
873 	KASSERT(sc->sc_brightness != NULL &&
874 		sc->sc_brightness->gpc_upd_parameter != NULL);
875 
876 	(void)sc->sc_brightness->gpc_upd_parameter(
877 	    sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
878 }
879 
880 void
881 genfb_enable_polling(device_t dev)
882 {
883 	struct genfb_softc *sc = device_private(dev);
884 
885 	if (sc->sc_console_screen.scr_vd) {
886 		SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
887 		vcons_hard_switch(&sc->sc_console_screen);
888 		vcons_enable_polling(&sc->vd);
889 		if (sc->sc_ops.genfb_enable_polling)
890 			(*sc->sc_ops.genfb_enable_polling)(sc);
891 	}
892 }
893 
894 void
895 genfb_disable_polling(device_t dev)
896 {
897 	struct genfb_softc *sc = device_private(dev);
898 
899 	if (sc->sc_console_screen.scr_vd) {
900 		if (sc->sc_ops.genfb_disable_polling)
901 			(*sc->sc_ops.genfb_disable_polling)(sc);
902 		vcons_disable_polling(&sc->vd);
903 	}
904 }
905 
906 #if GENFB_GLYPHCACHE > 0
907 #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024)
908 
909 static inline int
910 attr2idx(long attr)
911 {
912 	if ((attr & 0xf0f00ff8) != 0)
913 		return -1;
914 
915 	return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
916 }
917 
918 static int
919 genfb_setup_glyphcache(struct genfb_softc *sc, long defattr)
920 {
921 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri,
922 			  *cri = &sc->sc_cache_ri;
923 	gc_bucket *b;
924 	int i, usedcells = 0, idx, j;
925 
926 	sc->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP);
927 
928 	/*
929 	 * now we build a mutant rasops_info for the cache - same pixel type
930 	 * and such as the real fb, but only one character per line for
931 	 * simplicity and locality
932 	 */
933 	memcpy(cri, ri, sizeof(struct rasops_info));
934 	cri->ri_ops.putchar = sc->sc_putchar;
935 	cri->ri_width = ri->ri_font->fontwidth;
936 	cri->ri_stride = ri->ri_xscale;
937 	cri->ri_bits = sc->sc_cache;
938 	cri->ri_hwbits = NULL;
939 	cri->ri_origbits = sc->sc_cache;
940 	cri->ri_cols = 1;
941 	cri->ri_rows = GLYPHCACHESIZE /
942 	    (cri->ri_stride * cri->ri_font->fontheight);
943 	cri->ri_xorigin = 0;
944 	cri->ri_yorigin = 0;
945 	cri->ri_xscale = ri->ri_xscale;
946 	cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale;
947 
948 	printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride);
949 	printf("cells: %d\n", cri->ri_rows);
950 	sc->sc_nbuckets = uimin(256, cri->ri_rows / 223);
951 	sc->sc_buckets = kmem_alloc(sizeof(gc_bucket) * sc->sc_nbuckets, KM_SLEEP);
952 	printf("buckets: %d\n", sc->sc_nbuckets);
953 	for (i = 0; i < sc->sc_nbuckets; i++) {
954 		b = &sc->sc_buckets[i];
955 		b->gb_firstcell = usedcells;
956 		b->gb_numcells = uimin(223, cri->ri_rows - usedcells);
957 		usedcells += 223;
958 		b->gb_usedcells = 0;
959 		b->gb_index = -1;
960 		for (j = 0; j < 223; j++) b->gb_map[j] = -1;
961 	}
962 
963 	/* initialize the attribute map... */
964 	for (i = 0; i < 256; i++) {
965 		sc->sc_attrmap[i] = -1;
966 	}
967 
968 	/* first bucket goes to default attr */
969 	idx = attr2idx(defattr);
970 	printf("defattr %08lx idx %x\n", defattr, idx);
971 
972 	if (idx >= 0) {
973 		sc->sc_attrmap[idx] = 0;
974 		sc->sc_buckets[0].gb_index = idx;
975 	}
976 
977 	return 0;
978 }
979 
980 static void
981 genfb_putchar(void *cookie, int row, int col, u_int c, long attr)
982 {
983 	struct rasops_info *ri = cookie;
984 	struct vcons_screen *scr = ri->ri_hw;
985 	struct genfb_softc *sc = scr->scr_cookie;
986 	uint8_t *src, *dst;
987 	gc_bucket *b;
988 	int i, idx, bi, cell;
989 
990 	attr &= ~WSATTR_USERMASK;
991 
992 	idx = attr2idx(attr);
993 	if (c < 33 || c > 255 || idx < 0) goto nope;
994 
995 	/* look for a bucket with the right attribute */
996 	bi = sc->sc_attrmap[idx];
997 	if (bi == -1) {
998 		/* nope, see if there's an empty one left */
999 		bi = 1;
1000 		while ((bi < sc->sc_nbuckets) &&
1001 		       (sc->sc_buckets[bi].gb_index != -1)) {
1002 			bi++;
1003 		}
1004 		if (bi < sc->sc_nbuckets) {
1005 			/* found one -> grab it */
1006 			sc->sc_attrmap[idx] = bi;
1007 			b = &sc->sc_buckets[bi];
1008 			b->gb_index = idx;
1009 			b->gb_usedcells = 0;
1010 			/* make sure this doesn't get evicted right away */
1011 			b->gb_lastread = time_uptime;
1012 		} else {
1013 			/*
1014 			 * still nothing
1015 			 * steal the least recently read bucket
1016 			 */
1017 			time_t moo = time_uptime;
1018 			int oldest = 1;
1019 
1020 			for (i = 1; i < sc->sc_nbuckets; i++) {
1021 				if (sc->sc_buckets[i].gb_lastread < moo) {
1022 					oldest = i;
1023 					moo = sc->sc_buckets[i].gb_lastread;
1024 				}
1025 			}
1026 
1027 			/* if we end up here all buckets must be in use */
1028 			b = &sc->sc_buckets[oldest];
1029 			sc->sc_attrmap[b->gb_index] = -1;
1030 			b->gb_index = idx;
1031 			b->gb_usedcells = 0;
1032 			sc->sc_attrmap[idx] = oldest;
1033 			/* now scrub it */
1034 			for (i = 0; i < 223; i++)
1035 				b->gb_map[i] = -1;
1036 			/* and set the time stamp */
1037 			b->gb_lastread = time_uptime;
1038 		}
1039 	} else {
1040 		/* found one */
1041 		b = &sc->sc_buckets[bi];
1042 	}
1043 
1044 	/* see if there's room in the bucket */
1045 	if (b->gb_usedcells >= b->gb_numcells) goto nope;
1046 
1047 	cell = b->gb_map[c - 33];
1048 	if (cell == -1) {
1049 		if (b->gb_usedcells >= b->gb_numcells)
1050 			goto nope;
1051 		cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
1052 		b->gb_map[c - 33] = cell;
1053 		cell += b->gb_firstcell;
1054 		sc->sc_putchar(&sc->sc_cache_ri, cell, 0, c, attr);
1055 
1056 	} else
1057 		cell += b->gb_firstcell;
1058 
1059 	src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale;
1060 	dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
1061 	for (i = 0; i < ri->ri_font->fontheight; i++) {
1062 		memcpy(dst, src, ri->ri_xscale);
1063 		src += ri->ri_xscale;
1064 		dst += ri->ri_stride;
1065 	}
1066 	b->gb_lastread = time_uptime;
1067 	return;
1068 nope:
1069 	sc->sc_putchar(cookie, row, col, c, attr);
1070 }
1071 
1072 #endif
1073