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