Home | History | Annotate | Line # | Download | only in wsfb
genfb.c revision 1.82
      1 /*	$NetBSD: genfb.c,v 1.82 2021/04/24 23:37:00 thorpej 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.82 2021/04/24 23:37:00 thorpej 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 	    CFARG_IATTR, "wsemuldisplaydev",
    382 	    CFARG_EOL);
    383 
    384 	return 0;
    385 }
    386 
    387 static int
    388 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    389 	struct lwp *l)
    390 {
    391 	struct vcons_data *vd = v;
    392 	struct genfb_softc *sc = vd->cookie;
    393 	struct wsdisplay_fbinfo *wdf;
    394 	struct vcons_screen *ms = vd->active;
    395 	struct wsdisplay_param *param;
    396 	int new_mode, error, val, ret;
    397 
    398 	switch (cmd) {
    399 		case WSDISPLAYIO_GINFO:
    400 			if (ms == NULL)
    401 				return ENODEV;
    402 			wdf = (void *)data;
    403 			wdf->height = ms->scr_ri.ri_height;
    404 			wdf->width = ms->scr_ri.ri_width;
    405 			wdf->depth = ms->scr_ri.ri_depth;
    406 			wdf->cmsize = 256;
    407 			return 0;
    408 
    409 		case WSDISPLAYIO_GETCMAP:
    410 			return genfb_getcmap(sc,
    411 			    (struct wsdisplay_cmap *)data);
    412 
    413 		case WSDISPLAYIO_PUTCMAP:
    414 			return genfb_putcmap(sc,
    415 			    (struct wsdisplay_cmap *)data);
    416 
    417 		case WSDISPLAYIO_LINEBYTES:
    418 			*(u_int *)data = sc->sc_stride;
    419 			return 0;
    420 
    421 		case WSDISPLAYIO_SMODE:
    422 			new_mode = *(int *)data;
    423 
    424 			/* notify the bus backend */
    425 			error = 0;
    426 			if (sc->sc_ops.genfb_ioctl)
    427 				error = sc->sc_ops.genfb_ioctl(sc, vs,
    428 					    cmd, data, flag, l);
    429 			if (error && error != EPASSTHROUGH)
    430 				return error;
    431 
    432 			if (new_mode != sc->sc_mode) {
    433 				sc->sc_mode = new_mode;
    434 				if (sc->sc_modecb != NULL)
    435 					sc->sc_modecb->gmc_setmode(sc,
    436 					    sc->sc_mode);
    437 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    438 					genfb_restore_palette(sc);
    439 					vcons_redraw_screen(ms);
    440 				}
    441 			}
    442 			return 0;
    443 
    444 		case WSDISPLAYIO_SSPLASH:
    445 #if defined(SPLASHSCREEN)
    446 			if(*(int *)data == 1) {
    447 				SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    448 				splash_render(&sc->sc_splash,
    449 						SPLASH_F_CENTER|SPLASH_F_FILL);
    450 			} else {
    451 				SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    452 				genfb_init_palette(sc);
    453 			}
    454 			vcons_redraw_screen(ms);
    455 			return 0;
    456 #else
    457 			return ENODEV;
    458 #endif
    459 		case WSDISPLAYIO_GETPARAM:
    460 			param = (struct wsdisplay_param *)data;
    461 			switch (param->param) {
    462 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    463 				if (sc->sc_brightness == NULL)
    464 					return EPASSTHROUGH;
    465 				param->min = 0;
    466 				param->max = 255;
    467 				return sc->sc_brightness->gpc_get_parameter(
    468 				    sc->sc_brightness->gpc_cookie,
    469 				    &param->curval);
    470 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    471 				if (sc->sc_backlight == NULL)
    472 					return EPASSTHROUGH;
    473 				param->min = 0;
    474 				param->max = 1;
    475 				return sc->sc_backlight->gpc_get_parameter(
    476 				    sc->sc_backlight->gpc_cookie,
    477 				    &param->curval);
    478 			}
    479 			return EPASSTHROUGH;
    480 
    481 		case WSDISPLAYIO_SETPARAM:
    482 			param = (struct wsdisplay_param *)data;
    483 			switch (param->param) {
    484 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    485 				if (sc->sc_brightness == NULL)
    486 					return EPASSTHROUGH;
    487 				val = param->curval;
    488 				if (val < 0) val = 0;
    489 				if (val > 255) val = 255;
    490 				return sc->sc_brightness->gpc_set_parameter(
    491 				    sc->sc_brightness->gpc_cookie, val);
    492 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    493 				if (sc->sc_backlight == NULL)
    494 					return EPASSTHROUGH;
    495 				val = param->curval;
    496 				if (val < 0) val = 0;
    497 				if (val > 1) val = 1;
    498 				return sc->sc_backlight->gpc_set_parameter(
    499 				    sc->sc_backlight->gpc_cookie, val);
    500 			}
    501 			return EPASSTHROUGH;
    502 	}
    503 	ret = EPASSTHROUGH;
    504 	if (sc->sc_ops.genfb_ioctl)
    505 		ret = sc->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l);
    506 	if (ret != EPASSTHROUGH)
    507 		return ret;
    508 	/*
    509 	 * XXX
    510 	 * handle these only if there either is no ioctl() handler or it didn't
    511 	 * know how to deal with them. This allows bus frontends to override
    512 	 * them but still provides fallback implementations
    513 	 */
    514 	switch (cmd) {
    515 		case WSDISPLAYIO_GET_EDID: {
    516 			struct wsdisplayio_edid_info *d = data;
    517 			return wsdisplayio_get_edid(sc->sc_dev, d);
    518 		}
    519 
    520 		case WSDISPLAYIO_GET_FBINFO: {
    521 			struct wsdisplayio_fbinfo *fbi = data;
    522 			return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    523 		}
    524 	}
    525 	return EPASSTHROUGH;
    526 }
    527 
    528 static paddr_t
    529 genfb_mmap(void *v, void *vs, off_t offset, int prot)
    530 {
    531 	struct vcons_data *vd = v;
    532 	struct genfb_softc *sc = vd->cookie;
    533 
    534 	if (sc->sc_ops.genfb_mmap)
    535 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
    536 
    537 	return -1;
    538 }
    539 
    540 static void
    541 genfb_pollc(void *v, int on)
    542 {
    543 	struct vcons_data *vd = v;
    544 	struct genfb_softc *sc = vd->cookie;
    545 
    546 	if (sc == NULL)
    547 		return;
    548 
    549 	if (on)
    550 		genfb_enable_polling(sc->sc_dev);
    551 	else
    552 		genfb_disable_polling(sc->sc_dev);
    553 }
    554 
    555 static void
    556 genfb_init_screen(void *cookie, struct vcons_screen *scr,
    557     int existing, long *defattr)
    558 {
    559 	struct genfb_softc *sc = cookie;
    560 	struct rasops_info *ri = &scr->scr_ri;
    561 	int wantcols;
    562 	bool is_bgr, is_swapped;
    563 
    564 	ri->ri_depth = sc->sc_depth;
    565 	ri->ri_width = sc->sc_width;
    566 	ri->ri_height = sc->sc_height;
    567 	ri->ri_stride = sc->sc_stride;
    568 	ri->ri_flg = RI_CENTER;
    569 	if (sc->sc_want_clear)
    570 		ri->ri_flg |= RI_FULLCLEAR;
    571 
    572 	scr->scr_flags |= VCONS_LOADFONT;
    573 
    574 	if (sc->sc_shadowfb != NULL) {
    575 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
    576 		ri->ri_bits = (char *)sc->sc_shadowfb;
    577 	} else {
    578 		ri->ri_bits = (char *)sc->sc_fbaddr;
    579 		scr->scr_flags |= VCONS_DONT_READ;
    580 	}
    581 
    582 	if (existing && sc->sc_want_clear)
    583 		ri->ri_flg |= RI_CLEAR;
    584 
    585 	switch (ri->ri_depth) {
    586 	case 32:
    587 	case 24:
    588 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    589 		ri->ri_flg |= RI_ENABLE_ALPHA;
    590 
    591 		is_bgr = false;
    592 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    593 		    "is_bgr", &is_bgr);
    594 
    595 		is_swapped = false;
    596 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    597 		    "is_swapped", &is_swapped);
    598 
    599 		if (is_bgr) {
    600 			/* someone requested BGR */
    601 			ri->ri_rpos = 0;
    602 			ri->ri_gpos = 8;
    603 			ri->ri_bpos = 16;
    604 		} else if (is_swapped) {
    605 			/* byte-swapped, must be 32 bpp */
    606 			KASSERT(ri->ri_depth == 32);
    607 			ri->ri_rpos = 8;
    608 			ri->ri_gpos = 16;
    609 			ri->ri_bpos = 24;
    610 		} else {
    611 			/* assume RGB */
    612 			ri->ri_rpos = 16;
    613 			ri->ri_gpos = 8;
    614 			ri->ri_bpos = 0;
    615 		}
    616 		break;
    617 
    618 	case 16:
    619 	case 15:
    620 		ri->ri_flg |= RI_ENABLE_ALPHA;
    621 		break;
    622 
    623 	case 8:
    624 		if (sc->sc_cmcb != NULL)
    625 			ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
    626 		break;
    627 
    628 	case 2:
    629 		ri->ri_flg |= RI_ENABLE_ALPHA;
    630 		break;
    631 
    632 	default:
    633 		break;
    634 	}
    635 
    636 	wantcols = genfb_calc_cols(sc);
    637 
    638 	rasops_init(ri, 0, wantcols);
    639 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    640 		  WSSCREEN_RESIZE;
    641 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    642 		    sc->sc_width / ri->ri_font->fontwidth);
    643 
    644 	ri->ri_hw = scr;
    645 #if GENFB_GLYPHCACHE > 0
    646 	sc->sc_putchar = ri->ri_ops.putchar;
    647 	ri->ri_ops.putchar = genfb_putchar;
    648 #endif
    649 #ifdef GENFB_DISABLE_TEXT
    650 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
    651 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    652 #endif
    653 }
    654 
    655 /* Returns the width of the display in millimeters, or 0 if not known. */
    656 static int
    657 genfb_calc_hsize(struct genfb_softc *sc)
    658 {
    659 	device_t dev = sc->sc_dev;
    660 	prop_dictionary_t dict = device_properties(dev);
    661 	prop_data_t edid_data;
    662 	struct edid_info *edid;
    663 	const char *edid_ptr;
    664 	int hsize;
    665 
    666 	edid_data = prop_dictionary_get(dict, "EDID");
    667 	if (edid_data == NULL || prop_data_size(edid_data) < 128)
    668 		return 0;
    669 
    670 	edid = kmem_alloc(sizeof(*edid), KM_SLEEP);
    671 
    672 	edid_ptr = prop_data_value(edid_data);
    673 	if (edid_parse(__UNCONST(edid_ptr), edid) == 0)
    674 		hsize = (int)edid->edid_max_hsize * 10;
    675 	else
    676 		hsize = 0;
    677 
    678 	kmem_free(edid, sizeof(*edid));
    679 
    680 	return hsize;
    681 }
    682 
    683 /* Return the minimum number of character columns based on DPI */
    684 static int
    685 genfb_calc_cols(struct genfb_softc *sc)
    686 {
    687 	const int hsize = genfb_calc_hsize(sc);
    688 
    689 	return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM);
    690 }
    691 
    692 static int
    693 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    694 {
    695 	u_char *r, *g, *b;
    696 	u_int index = cm->index;
    697 	u_int count = cm->count;
    698 	int i, error;
    699 	u_char rbuf[256], gbuf[256], bbuf[256];
    700 
    701 #ifdef GENFB_DEBUG
    702 	aprint_debug("putcmap: %d %d\n",index, count);
    703 #endif
    704 	if (index >= 256 || count > 256 || index + count > 256)
    705 		return EINVAL;
    706 
    707 	error = copyin(cm->red, &rbuf[index], count);
    708 	if (error)
    709 		return error;
    710 	error = copyin(cm->green, &gbuf[index], count);
    711 	if (error)
    712 		return error;
    713 	error = copyin(cm->blue, &bbuf[index], count);
    714 	if (error)
    715 		return error;
    716 
    717 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    718 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    719 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    720 
    721 	r = &sc->sc_cmap_red[index];
    722 	g = &sc->sc_cmap_green[index];
    723 	b = &sc->sc_cmap_blue[index];
    724 
    725 	for (i = 0; i < count; i++) {
    726 		genfb_putpalreg(sc, index, *r, *g, *b);
    727 		index++;
    728 		r++, g++, b++;
    729 	}
    730 	return 0;
    731 }
    732 
    733 static int
    734 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    735 {
    736 	u_int index = cm->index;
    737 	u_int count = cm->count;
    738 	int error;
    739 
    740 	if (index >= 256 || count > 256 || index + count > 256)
    741 		return EINVAL;
    742 
    743 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    744 	if (error)
    745 		return error;
    746 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    747 	if (error)
    748 		return error;
    749 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    750 	if (error)
    751 		return error;
    752 
    753 	return 0;
    754 }
    755 
    756 void
    757 genfb_restore_palette(struct genfb_softc *sc)
    758 {
    759 	int i;
    760 
    761 	if (sc->sc_depth <= 8) {
    762 		for (i = 0; i < (1 << sc->sc_depth); i++) {
    763 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    764 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    765 		}
    766 	}
    767 }
    768 
    769 static void
    770 genfb_init_palette(struct genfb_softc *sc)
    771 {
    772 	int i, j, tmp;
    773 
    774 	if (sc->sc_depth == 8) {
    775 		/* generate an r3g3b2 colour map */
    776 		for (i = 0; i < 256; i++) {
    777 			tmp = i & 0xe0;
    778 			/*
    779 			 * replicate bits so 0xe0 maps to a red value of 0xff
    780 			 * in order to make white look actually white
    781 			 */
    782 			tmp |= (tmp >> 3) | (tmp >> 6);
    783 			sc->sc_cmap_red[i] = tmp;
    784 
    785 			tmp = (i & 0x1c) << 3;
    786 			tmp |= (tmp >> 3) | (tmp >> 6);
    787 			sc->sc_cmap_green[i] = tmp;
    788 
    789 			tmp = (i & 0x03) << 6;
    790 			tmp |= tmp >> 2;
    791 			tmp |= tmp >> 4;
    792 			sc->sc_cmap_blue[i] = tmp;
    793 
    794 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    795 				       sc->sc_cmap_green[i],
    796 				       sc->sc_cmap_blue[i]);
    797 		}
    798 	} else {
    799 		/* steal rasops' ANSI cmap */
    800 		j = 0;
    801 		for (i = 0; i < 256; i++) {
    802 			sc->sc_cmap_red[i] = rasops_cmap[j];
    803 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    804 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    805 			j += 3;
    806 		}
    807 	}
    808 }
    809 
    810 static int
    811 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    812     uint8_t b)
    813 {
    814 
    815 	if (sc->sc_cmcb) {
    816 
    817 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
    818 		    idx, r, g, b);
    819 	}
    820 	return 0;
    821 }
    822 
    823 void
    824 genfb_cnattach(void)
    825 {
    826 	genfb_cnattach_called = 1;
    827 }
    828 
    829 void
    830 genfb_disable(void)
    831 {
    832 	genfb_enabled = 0;
    833 }
    834 
    835 int
    836 genfb_is_console(void)
    837 {
    838 	return genfb_cnattach_called;
    839 }
    840 
    841 int
    842 genfb_is_enabled(void)
    843 {
    844 	return genfb_enabled;
    845 }
    846 
    847 int
    848 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
    849 {
    850 	struct genfb_softc *sc = genfb_softc;
    851 
    852 	if (sc && sc->sc_ops.genfb_borrow)
    853 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
    854 	return 0;
    855 }
    856 
    857 static void
    858 genfb_brightness_up(device_t dev)
    859 {
    860 	struct genfb_softc *sc = device_private(dev);
    861 
    862 	KASSERT(sc->sc_brightness != NULL &&
    863 		sc->sc_brightness->gpc_upd_parameter != NULL);
    864 
    865 	(void)sc->sc_brightness->gpc_upd_parameter(
    866 	    sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
    867 }
    868 
    869 static void
    870 genfb_brightness_down(device_t dev)
    871 {
    872 	struct genfb_softc *sc = device_private(dev);
    873 
    874 	KASSERT(sc->sc_brightness != NULL &&
    875 		sc->sc_brightness->gpc_upd_parameter != NULL);
    876 
    877 	(void)sc->sc_brightness->gpc_upd_parameter(
    878 	    sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
    879 }
    880 
    881 void
    882 genfb_enable_polling(device_t dev)
    883 {
    884 	struct genfb_softc *sc = device_private(dev);
    885 
    886 	if (sc->sc_console_screen.scr_vd) {
    887 		SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    888 		vcons_hard_switch(&sc->sc_console_screen);
    889 		vcons_enable_polling(&sc->vd);
    890 		if (sc->sc_ops.genfb_enable_polling)
    891 			(*sc->sc_ops.genfb_enable_polling)(sc);
    892 	}
    893 }
    894 
    895 void
    896 genfb_disable_polling(device_t dev)
    897 {
    898 	struct genfb_softc *sc = device_private(dev);
    899 
    900 	if (sc->sc_console_screen.scr_vd) {
    901 		if (sc->sc_ops.genfb_disable_polling)
    902 			(*sc->sc_ops.genfb_disable_polling)(sc);
    903 		vcons_disable_polling(&sc->vd);
    904 	}
    905 }
    906 
    907 #if GENFB_GLYPHCACHE > 0
    908 #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024)
    909 
    910 static inline int
    911 attr2idx(long attr)
    912 {
    913 	if ((attr & 0xf0f00ff8) != 0)
    914 		return -1;
    915 
    916 	return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
    917 }
    918 
    919 static int
    920 genfb_setup_glyphcache(struct genfb_softc *sc, long defattr)
    921 {
    922 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri,
    923 			  *cri = &sc->sc_cache_ri;
    924 	gc_bucket *b;
    925 	int i, usedcells = 0, idx, j;
    926 
    927 	sc->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP);
    928 
    929 	/*
    930 	 * now we build a mutant rasops_info for the cache - same pixel type
    931 	 * and such as the real fb, but only one character per line for
    932 	 * simplicity and locality
    933 	 */
    934 	memcpy(cri, ri, sizeof(struct rasops_info));
    935 	cri->ri_ops.putchar = sc->sc_putchar;
    936 	cri->ri_width = ri->ri_font->fontwidth;
    937 	cri->ri_stride = ri->ri_xscale;
    938 	cri->ri_bits = sc->sc_cache;
    939 	cri->ri_hwbits = NULL;
    940 	cri->ri_origbits = sc->sc_cache;
    941 	cri->ri_cols = 1;
    942 	cri->ri_rows = GLYPHCACHESIZE /
    943 	    (cri->ri_stride * cri->ri_font->fontheight);
    944 	cri->ri_xorigin = 0;
    945 	cri->ri_yorigin = 0;
    946 	cri->ri_xscale = ri->ri_xscale;
    947 	cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale;
    948 
    949 	printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride);
    950 	printf("cells: %d\n", cri->ri_rows);
    951 	sc->sc_nbuckets = uimin(256, cri->ri_rows / 223);
    952 	sc->sc_buckets = kmem_alloc(sizeof(gc_bucket) * sc->sc_nbuckets, KM_SLEEP);
    953 	printf("buckets: %d\n", sc->sc_nbuckets);
    954 	for (i = 0; i < sc->sc_nbuckets; i++) {
    955 		b = &sc->sc_buckets[i];
    956 		b->gb_firstcell = usedcells;
    957 		b->gb_numcells = uimin(223, cri->ri_rows - usedcells);
    958 		usedcells += 223;
    959 		b->gb_usedcells = 0;
    960 		b->gb_index = -1;
    961 		for (j = 0; j < 223; j++) b->gb_map[j] = -1;
    962 	}
    963 
    964 	/* initialize the attribute map... */
    965 	for (i = 0; i < 256; i++) {
    966 		sc->sc_attrmap[i] = -1;
    967 	}
    968 
    969 	/* first bucket goes to default attr */
    970 	idx = attr2idx(defattr);
    971 	printf("defattr %08lx idx %x\n", defattr, idx);
    972 
    973 	if (idx >= 0) {
    974 		sc->sc_attrmap[idx] = 0;
    975 		sc->sc_buckets[0].gb_index = idx;
    976 	}
    977 
    978 	return 0;
    979 }
    980 
    981 static void
    982 genfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    983 {
    984 	struct rasops_info *ri = cookie;
    985 	struct vcons_screen *scr = ri->ri_hw;
    986 	struct genfb_softc *sc = scr->scr_cookie;
    987 	uint8_t *src, *dst;
    988 	gc_bucket *b;
    989 	int i, idx, bi, cell;
    990 
    991 	attr &= ~WSATTR_USERMASK;
    992 
    993 	idx = attr2idx(attr);
    994 	if (c < 33 || c > 255 || idx < 0) goto nope;
    995 
    996 	/* look for a bucket with the right attribute */
    997 	bi = sc->sc_attrmap[idx];
    998 	if (bi == -1) {
    999 		/* nope, see if there's an empty one left */
   1000 		bi = 1;
   1001 		while ((bi < sc->sc_nbuckets) &&
   1002 		       (sc->sc_buckets[bi].gb_index != -1)) {
   1003 			bi++;
   1004 		}
   1005 		if (bi < sc->sc_nbuckets) {
   1006 			/* found one -> grab it */
   1007 			sc->sc_attrmap[idx] = bi;
   1008 			b = &sc->sc_buckets[bi];
   1009 			b->gb_index = idx;
   1010 			b->gb_usedcells = 0;
   1011 			/* make sure this doesn't get evicted right away */
   1012 			b->gb_lastread = time_uptime;
   1013 		} else {
   1014 			/*
   1015 			 * still nothing
   1016 			 * steal the least recently read bucket
   1017 			 */
   1018 			time_t moo = time_uptime;
   1019 			int oldest = 1;
   1020 
   1021 			for (i = 1; i < sc->sc_nbuckets; i++) {
   1022 				if (sc->sc_buckets[i].gb_lastread < moo) {
   1023 					oldest = i;
   1024 					moo = sc->sc_buckets[i].gb_lastread;
   1025 				}
   1026 			}
   1027 
   1028 			/* if we end up here all buckets must be in use */
   1029 			b = &sc->sc_buckets[oldest];
   1030 			sc->sc_attrmap[b->gb_index] = -1;
   1031 			b->gb_index = idx;
   1032 			b->gb_usedcells = 0;
   1033 			sc->sc_attrmap[idx] = oldest;
   1034 			/* now scrub it */
   1035 			for (i = 0; i < 223; i++)
   1036 				b->gb_map[i] = -1;
   1037 			/* and set the time stamp */
   1038 			b->gb_lastread = time_uptime;
   1039 		}
   1040 	} else {
   1041 		/* found one */
   1042 		b = &sc->sc_buckets[bi];
   1043 	}
   1044 
   1045 	/* see if there's room in the bucket */
   1046 	if (b->gb_usedcells >= b->gb_numcells) goto nope;
   1047 
   1048 	cell = b->gb_map[c - 33];
   1049 	if (cell == -1) {
   1050 		if (b->gb_usedcells >= b->gb_numcells)
   1051 			goto nope;
   1052 		cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
   1053 		b->gb_map[c - 33] = cell;
   1054 		cell += b->gb_firstcell;
   1055 		sc->sc_putchar(&sc->sc_cache_ri, cell, 0, c, attr);
   1056 
   1057 	} else
   1058 		cell += b->gb_firstcell;
   1059 
   1060 	src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale;
   1061 	dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
   1062 	for (i = 0; i < ri->ri_font->fontheight; i++) {
   1063 		memcpy(dst, src, ri->ri_xscale);
   1064 		src += ri->ri_xscale;
   1065 		dst += ri->ri_stride;
   1066 	}
   1067 	b->gb_lastread = time_uptime;
   1068 	return;
   1069 nope:
   1070 	sc->sc_putchar(cookie, row, col, c, attr);
   1071 }
   1072 
   1073 #endif
   1074