Home | History | Annotate | Line # | Download | only in wsfb
genfb.c revision 1.84
      1 /*	$NetBSD: genfb.c,v 1.84 2021/08/30 22:47:25 jmcneill 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.84 2021/08/30 22:47:25 jmcneill 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(sc->sc_dev, &aa, wsemuldisplaydevprint,
    381 	    CFARGS(.iattr = "wsemuldisplaydev"));
    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, is_10bit;
    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_flg |= RI_ENABLE_ALPHA;
    588 
    589 		is_bgr = false;
    590 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    591 		    "is_bgr", &is_bgr);
    592 
    593 		is_swapped = false;
    594 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    595 		    "is_swapped", &is_swapped);
    596 
    597 		is_10bit = false;
    598 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    599 		    "is_10bit", &is_10bit);
    600 
    601 		const int bits = is_10bit ? 10 : 8;
    602 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = bits;
    603 
    604 		if (is_bgr) {
    605 			/* someone requested BGR */
    606 			ri->ri_rpos = bits * 0;
    607 			ri->ri_gpos = bits * 1;
    608 			ri->ri_bpos = bits * 2;
    609 		} else if (is_swapped) {
    610 			/* byte-swapped, must be 32 bpp */
    611 			KASSERT(ri->ri_depth == 32 && bits == 8);
    612 			ri->ri_rpos = 8;
    613 			ri->ri_gpos = 16;
    614 			ri->ri_bpos = 24;
    615 		} else {
    616 			/* assume RGB */
    617 			ri->ri_rpos = bits * 2;
    618 			ri->ri_gpos = bits * 1;
    619 			ri->ri_bpos = bits * 0;
    620 		}
    621 		break;
    622 
    623 	case 16:
    624 	case 15:
    625 		ri->ri_flg |= RI_ENABLE_ALPHA;
    626 		break;
    627 
    628 	case 8:
    629 		if (sc->sc_cmcb != NULL)
    630 			ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
    631 		break;
    632 
    633 	case 2:
    634 		ri->ri_flg |= RI_ENABLE_ALPHA;
    635 		break;
    636 
    637 	default:
    638 		break;
    639 	}
    640 
    641 	wantcols = genfb_calc_cols(sc);
    642 
    643 	rasops_init(ri, 0, wantcols);
    644 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    645 		  WSSCREEN_RESIZE;
    646 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    647 		    sc->sc_width / ri->ri_font->fontwidth);
    648 
    649 	ri->ri_hw = scr;
    650 #if GENFB_GLYPHCACHE > 0
    651 	sc->sc_putchar = ri->ri_ops.putchar;
    652 	ri->ri_ops.putchar = genfb_putchar;
    653 #endif
    654 #ifdef GENFB_DISABLE_TEXT
    655 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
    656 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    657 #endif
    658 }
    659 
    660 /* Returns the width of the display in millimeters, or 0 if not known. */
    661 static int
    662 genfb_calc_hsize(struct genfb_softc *sc)
    663 {
    664 	device_t dev = sc->sc_dev;
    665 	prop_dictionary_t dict = device_properties(dev);
    666 	prop_data_t edid_data;
    667 	struct edid_info *edid;
    668 	const char *edid_ptr;
    669 	int hsize;
    670 
    671 	edid_data = prop_dictionary_get(dict, "EDID");
    672 	if (edid_data == NULL || prop_data_size(edid_data) < 128)
    673 		return 0;
    674 
    675 	edid = kmem_alloc(sizeof(*edid), KM_SLEEP);
    676 
    677 	edid_ptr = prop_data_value(edid_data);
    678 	if (edid_parse(__UNCONST(edid_ptr), edid) == 0)
    679 		hsize = (int)edid->edid_max_hsize * 10;
    680 	else
    681 		hsize = 0;
    682 
    683 	kmem_free(edid, sizeof(*edid));
    684 
    685 	return hsize;
    686 }
    687 
    688 /* Return the minimum number of character columns based on DPI */
    689 static int
    690 genfb_calc_cols(struct genfb_softc *sc)
    691 {
    692 	const int hsize = genfb_calc_hsize(sc);
    693 
    694 	return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM);
    695 }
    696 
    697 static int
    698 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    699 {
    700 	u_char *r, *g, *b;
    701 	u_int index = cm->index;
    702 	u_int count = cm->count;
    703 	int i, error;
    704 	u_char rbuf[256], gbuf[256], bbuf[256];
    705 
    706 #ifdef GENFB_DEBUG
    707 	aprint_debug("putcmap: %d %d\n",index, count);
    708 #endif
    709 	if (index >= 256 || count > 256 || index + count > 256)
    710 		return EINVAL;
    711 
    712 	error = copyin(cm->red, &rbuf[index], count);
    713 	if (error)
    714 		return error;
    715 	error = copyin(cm->green, &gbuf[index], count);
    716 	if (error)
    717 		return error;
    718 	error = copyin(cm->blue, &bbuf[index], count);
    719 	if (error)
    720 		return error;
    721 
    722 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    723 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    724 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    725 
    726 	r = &sc->sc_cmap_red[index];
    727 	g = &sc->sc_cmap_green[index];
    728 	b = &sc->sc_cmap_blue[index];
    729 
    730 	for (i = 0; i < count; i++) {
    731 		genfb_putpalreg(sc, index, *r, *g, *b);
    732 		index++;
    733 		r++, g++, b++;
    734 	}
    735 	return 0;
    736 }
    737 
    738 static int
    739 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    740 {
    741 	u_int index = cm->index;
    742 	u_int count = cm->count;
    743 	int error;
    744 
    745 	if (index >= 256 || count > 256 || index + count > 256)
    746 		return EINVAL;
    747 
    748 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    749 	if (error)
    750 		return error;
    751 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    752 	if (error)
    753 		return error;
    754 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    755 	if (error)
    756 		return error;
    757 
    758 	return 0;
    759 }
    760 
    761 void
    762 genfb_restore_palette(struct genfb_softc *sc)
    763 {
    764 	int i;
    765 
    766 	if (sc->sc_depth <= 8) {
    767 		for (i = 0; i < (1 << sc->sc_depth); i++) {
    768 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    769 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    770 		}
    771 	}
    772 }
    773 
    774 static void
    775 genfb_init_palette(struct genfb_softc *sc)
    776 {
    777 	int i, j, tmp;
    778 
    779 	if (sc->sc_depth == 8) {
    780 		/* generate an r3g3b2 colour map */
    781 		for (i = 0; i < 256; i++) {
    782 			tmp = i & 0xe0;
    783 			/*
    784 			 * replicate bits so 0xe0 maps to a red value of 0xff
    785 			 * in order to make white look actually white
    786 			 */
    787 			tmp |= (tmp >> 3) | (tmp >> 6);
    788 			sc->sc_cmap_red[i] = tmp;
    789 
    790 			tmp = (i & 0x1c) << 3;
    791 			tmp |= (tmp >> 3) | (tmp >> 6);
    792 			sc->sc_cmap_green[i] = tmp;
    793 
    794 			tmp = (i & 0x03) << 6;
    795 			tmp |= tmp >> 2;
    796 			tmp |= tmp >> 4;
    797 			sc->sc_cmap_blue[i] = tmp;
    798 
    799 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    800 				       sc->sc_cmap_green[i],
    801 				       sc->sc_cmap_blue[i]);
    802 		}
    803 	} else {
    804 		/* steal rasops' ANSI cmap */
    805 		j = 0;
    806 		for (i = 0; i < 256; i++) {
    807 			sc->sc_cmap_red[i] = rasops_cmap[j];
    808 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    809 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    810 			j += 3;
    811 		}
    812 	}
    813 }
    814 
    815 static int
    816 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    817     uint8_t b)
    818 {
    819 
    820 	if (sc->sc_cmcb) {
    821 
    822 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
    823 		    idx, r, g, b);
    824 	}
    825 	return 0;
    826 }
    827 
    828 void
    829 genfb_cnattach(void)
    830 {
    831 	genfb_cnattach_called = 1;
    832 }
    833 
    834 void
    835 genfb_disable(void)
    836 {
    837 	genfb_enabled = 0;
    838 }
    839 
    840 int
    841 genfb_is_console(void)
    842 {
    843 	return genfb_cnattach_called;
    844 }
    845 
    846 int
    847 genfb_is_enabled(void)
    848 {
    849 	return genfb_enabled;
    850 }
    851 
    852 int
    853 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
    854 {
    855 	struct genfb_softc *sc = genfb_softc;
    856 
    857 	if (sc && sc->sc_ops.genfb_borrow)
    858 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
    859 	return 0;
    860 }
    861 
    862 static void
    863 genfb_brightness_up(device_t dev)
    864 {
    865 	struct genfb_softc *sc = device_private(dev);
    866 
    867 	KASSERT(sc->sc_brightness != NULL &&
    868 		sc->sc_brightness->gpc_upd_parameter != NULL);
    869 
    870 	(void)sc->sc_brightness->gpc_upd_parameter(
    871 	    sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
    872 }
    873 
    874 static void
    875 genfb_brightness_down(device_t dev)
    876 {
    877 	struct genfb_softc *sc = device_private(dev);
    878 
    879 	KASSERT(sc->sc_brightness != NULL &&
    880 		sc->sc_brightness->gpc_upd_parameter != NULL);
    881 
    882 	(void)sc->sc_brightness->gpc_upd_parameter(
    883 	    sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
    884 }
    885 
    886 void
    887 genfb_enable_polling(device_t dev)
    888 {
    889 	struct genfb_softc *sc = device_private(dev);
    890 
    891 	if (sc->sc_console_screen.scr_vd) {
    892 		SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    893 		vcons_hard_switch(&sc->sc_console_screen);
    894 		vcons_enable_polling(&sc->vd);
    895 		if (sc->sc_ops.genfb_enable_polling)
    896 			(*sc->sc_ops.genfb_enable_polling)(sc);
    897 	}
    898 }
    899 
    900 void
    901 genfb_disable_polling(device_t dev)
    902 {
    903 	struct genfb_softc *sc = device_private(dev);
    904 
    905 	if (sc->sc_console_screen.scr_vd) {
    906 		if (sc->sc_ops.genfb_disable_polling)
    907 			(*sc->sc_ops.genfb_disable_polling)(sc);
    908 		vcons_disable_polling(&sc->vd);
    909 	}
    910 }
    911 
    912 #if GENFB_GLYPHCACHE > 0
    913 #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024)
    914 
    915 static inline int
    916 attr2idx(long attr)
    917 {
    918 	if ((attr & 0xf0f00ff8) != 0)
    919 		return -1;
    920 
    921 	return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
    922 }
    923 
    924 static int
    925 genfb_setup_glyphcache(struct genfb_softc *sc, long defattr)
    926 {
    927 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri,
    928 			  *cri = &sc->sc_cache_ri;
    929 	gc_bucket *b;
    930 	int i, usedcells = 0, idx, j;
    931 
    932 	sc->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP);
    933 
    934 	/*
    935 	 * now we build a mutant rasops_info for the cache - same pixel type
    936 	 * and such as the real fb, but only one character per line for
    937 	 * simplicity and locality
    938 	 */
    939 	memcpy(cri, ri, sizeof(struct rasops_info));
    940 	cri->ri_ops.putchar = sc->sc_putchar;
    941 	cri->ri_width = ri->ri_font->fontwidth;
    942 	cri->ri_stride = ri->ri_xscale;
    943 	cri->ri_bits = sc->sc_cache;
    944 	cri->ri_hwbits = NULL;
    945 	cri->ri_origbits = sc->sc_cache;
    946 	cri->ri_cols = 1;
    947 	cri->ri_rows = GLYPHCACHESIZE /
    948 	    (cri->ri_stride * cri->ri_font->fontheight);
    949 	cri->ri_xorigin = 0;
    950 	cri->ri_yorigin = 0;
    951 	cri->ri_xscale = ri->ri_xscale;
    952 	cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale;
    953 
    954 	printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride);
    955 	printf("cells: %d\n", cri->ri_rows);
    956 	sc->sc_nbuckets = uimin(256, cri->ri_rows / 223);
    957 	sc->sc_buckets = kmem_alloc(sizeof(gc_bucket) * sc->sc_nbuckets, KM_SLEEP);
    958 	printf("buckets: %d\n", sc->sc_nbuckets);
    959 	for (i = 0; i < sc->sc_nbuckets; i++) {
    960 		b = &sc->sc_buckets[i];
    961 		b->gb_firstcell = usedcells;
    962 		b->gb_numcells = uimin(223, cri->ri_rows - usedcells);
    963 		usedcells += 223;
    964 		b->gb_usedcells = 0;
    965 		b->gb_index = -1;
    966 		for (j = 0; j < 223; j++) b->gb_map[j] = -1;
    967 	}
    968 
    969 	/* initialize the attribute map... */
    970 	for (i = 0; i < 256; i++) {
    971 		sc->sc_attrmap[i] = -1;
    972 	}
    973 
    974 	/* first bucket goes to default attr */
    975 	idx = attr2idx(defattr);
    976 	printf("defattr %08lx idx %x\n", defattr, idx);
    977 
    978 	if (idx >= 0) {
    979 		sc->sc_attrmap[idx] = 0;
    980 		sc->sc_buckets[0].gb_index = idx;
    981 	}
    982 
    983 	return 0;
    984 }
    985 
    986 static void
    987 genfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    988 {
    989 	struct rasops_info *ri = cookie;
    990 	struct vcons_screen *scr = ri->ri_hw;
    991 	struct genfb_softc *sc = scr->scr_cookie;
    992 	uint8_t *src, *dst;
    993 	gc_bucket *b;
    994 	int i, idx, bi, cell;
    995 
    996 	attr &= ~WSATTR_USERMASK;
    997 
    998 	idx = attr2idx(attr);
    999 	if (c < 33 || c > 255 || idx < 0) goto nope;
   1000 
   1001 	/* look for a bucket with the right attribute */
   1002 	bi = sc->sc_attrmap[idx];
   1003 	if (bi == -1) {
   1004 		/* nope, see if there's an empty one left */
   1005 		bi = 1;
   1006 		while ((bi < sc->sc_nbuckets) &&
   1007 		       (sc->sc_buckets[bi].gb_index != -1)) {
   1008 			bi++;
   1009 		}
   1010 		if (bi < sc->sc_nbuckets) {
   1011 			/* found one -> grab it */
   1012 			sc->sc_attrmap[idx] = bi;
   1013 			b = &sc->sc_buckets[bi];
   1014 			b->gb_index = idx;
   1015 			b->gb_usedcells = 0;
   1016 			/* make sure this doesn't get evicted right away */
   1017 			b->gb_lastread = time_uptime;
   1018 		} else {
   1019 			/*
   1020 			 * still nothing
   1021 			 * steal the least recently read bucket
   1022 			 */
   1023 			time_t moo = time_uptime;
   1024 			int oldest = 1;
   1025 
   1026 			for (i = 1; i < sc->sc_nbuckets; i++) {
   1027 				if (sc->sc_buckets[i].gb_lastread < moo) {
   1028 					oldest = i;
   1029 					moo = sc->sc_buckets[i].gb_lastread;
   1030 				}
   1031 			}
   1032 
   1033 			/* if we end up here all buckets must be in use */
   1034 			b = &sc->sc_buckets[oldest];
   1035 			sc->sc_attrmap[b->gb_index] = -1;
   1036 			b->gb_index = idx;
   1037 			b->gb_usedcells = 0;
   1038 			sc->sc_attrmap[idx] = oldest;
   1039 			/* now scrub it */
   1040 			for (i = 0; i < 223; i++)
   1041 				b->gb_map[i] = -1;
   1042 			/* and set the time stamp */
   1043 			b->gb_lastread = time_uptime;
   1044 		}
   1045 	} else {
   1046 		/* found one */
   1047 		b = &sc->sc_buckets[bi];
   1048 	}
   1049 
   1050 	/* see if there's room in the bucket */
   1051 	if (b->gb_usedcells >= b->gb_numcells) goto nope;
   1052 
   1053 	cell = b->gb_map[c - 33];
   1054 	if (cell == -1) {
   1055 		if (b->gb_usedcells >= b->gb_numcells)
   1056 			goto nope;
   1057 		cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
   1058 		b->gb_map[c - 33] = cell;
   1059 		cell += b->gb_firstcell;
   1060 		sc->sc_putchar(&sc->sc_cache_ri, cell, 0, c, attr);
   1061 
   1062 	} else
   1063 		cell += b->gb_firstcell;
   1064 
   1065 	src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale;
   1066 	dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
   1067 	for (i = 0; i < ri->ri_font->fontheight; i++) {
   1068 		memcpy(dst, src, ri->ri_xscale);
   1069 		src += ri->ri_xscale;
   1070 		dst += ri->ri_stride;
   1071 	}
   1072 	b->gb_lastread = time_uptime;
   1073 	return;
   1074 nope:
   1075 	sc->sc_putchar(cookie, row, col, c, attr);
   1076 }
   1077 
   1078 #endif
   1079