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