Home | History | Annotate | Line # | Download | only in ic
ssdfb.c revision 1.14
      1 /* $NetBSD: ssdfb.c,v 1.14 2021/07/30 13:44:09 tnn Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tobias Nygren.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ssdfb.c,v 1.14 2021/07/30 13:44:09 tnn Exp $");
     34 
     35 #include "opt_ddb.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/kernel.h>
     39 #include <sys/conf.h>
     40 #include <sys/condvar.h>
     41 #include <sys/kmem.h>
     42 #include <sys/kthread.h>
     43 
     44 #include <uvm/uvm_device.h>
     45 #include <uvm/uvm_extern.h>
     46 
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/rasops/rasops.h>
     49 #include <dev/ic/ssdfbvar.h>
     50 
     51 #if defined(DDB)
     52 #include <machine/db_machdep.h>
     53 #include <ddb/db_extern.h>
     54 #endif
     55 
     56 /* userland interface */
     57 static int	ssdfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     58 static paddr_t	ssdfb_mmap(void *, void *, off_t, int);
     59 
     60 /* wscons screen management */
     61 static int	ssdfb_alloc_screen(void *, const struct wsscreen_descr *,
     62 				   void **, int *, int *, long *);
     63 static void	ssdfb_free_screen(void *, void *);
     64 static int	ssdfb_show_screen(void *, void *, int,
     65 				  void (*cb) (void *, int, int), void *);
     66 
     67 /* rasops hooks */
     68 static void	ssdfb_putchar(void *, int, int, u_int, long);
     69 static void	ssdfb_copycols(void *, int, int, int, int);
     70 static void	ssdfb_erasecols(void *, int, int, int, long);
     71 static void	ssdfb_copyrows(void *, int, int, int);
     72 static void	ssdfb_eraserows(void *, int, int, long);
     73 static void	ssdfb_cursor(void *, int, int, int);
     74 
     75 /* hardware interface */
     76 static int	ssdfb_init_ssd1306(struct ssdfb_softc *);
     77 static int	ssdfb_init_ssd1322(struct ssdfb_softc *);
     78 static int	ssdfb_set_contrast(struct ssdfb_softc *, uint8_t, bool);
     79 static int	ssdfb_set_display_on(struct ssdfb_softc *, bool, bool);
     80 static int	ssdfb_set_mode(struct ssdfb_softc *, u_int);
     81 
     82 /* frame buffer damage tracking and synchronization */
     83 static void	ssdfb_udv_attach(struct ssdfb_softc *sc);
     84 static bool	ssdfb_is_modified(struct ssdfb_softc *sc);
     85 static bool	ssdfb_clear_modify(struct ssdfb_softc *sc);
     86 static void	ssdfb_damage(struct ssdfb_softc *);
     87 static void	ssdfb_thread(void *);
     88 static void	ssdfb_set_usepoll(struct ssdfb_softc *, bool);
     89 static int	ssdfb_sync(struct ssdfb_softc *, bool);
     90 static int	ssdfb_sync_ssd1306(struct ssdfb_softc *, bool);
     91 static int	ssdfb_sync_ssd1322(struct ssdfb_softc *, bool);
     92 static uint64_t	ssdfb_transpose_block(uint8_t *, size_t);
     93 
     94 /* misc helpers */
     95 static const struct ssdfb_product *
     96 		ssdfb_lookup_product(ssdfb_product_id_t);
     97 static int	ssdfb_pick_font(int *, struct wsdisplay_font **);
     98 static void	ssdfb_clear_screen(struct ssdfb_softc *);
     99 #if defined(DDB)
    100 static void	ssdfb_ddb_trap_callback(int);
    101 #endif
    102 
    103 static const char *ssdfb_controller_names[] = {
    104 	[SSDFB_CONTROLLER_UNKNOWN] =	"unknown",
    105 	[SSDFB_CONTROLLER_SSD1306] =	"Solomon Systech SSD1306",
    106 	[SSDFB_CONTROLLER_SH1106] =	"Sino Wealth SH1106",
    107 	[SSDFB_CONTROLLER_SSD1322] =	"Solomon Systech SSD1322"
    108 };
    109 
    110 /*
    111  * Display module assemblies supported by this driver.
    112  */
    113 static const struct ssdfb_product ssdfb_products[] = {
    114 	{
    115 		.p_product_id =		SSDFB_PRODUCT_SSD1306_GENERIC,
    116 		.p_controller_id =	SSDFB_CONTROLLER_SSD1306,
    117 		.p_name =		"generic",
    118 		.p_width =		128,
    119 		.p_height =		64,
    120 		.p_bits_per_pixel =	1,
    121 		.p_panel_shift =	0,
    122 		.p_fosc =		0x8,
    123 		.p_fosc_div =		0,
    124 		.p_precharge =		0x1,
    125 		.p_discharge =		0xf,
    126 		.p_compin_cfg =		SSDFB_COM_PINS_A1_MASK
    127 					| SSDFB_COM_PINS_ALTERNATIVE_MASK,
    128 		.p_vcomh_deselect_level = SSD1306_VCOMH_DESELECT_LEVEL_0_77_VCC,
    129 		.p_default_contrast =	0x7f,
    130 		.p_multiplex_ratio =	0x3f,
    131 		.p_init =		ssdfb_init_ssd1306,
    132 		.p_sync =		ssdfb_sync_ssd1306
    133 	},
    134 	{
    135 		.p_product_id =		SSDFB_PRODUCT_SH1106_GENERIC,
    136 		.p_controller_id =	SSDFB_CONTROLLER_SH1106,
    137 		.p_name =		"generic",
    138 		.p_width =		128,
    139 		.p_height =		64,
    140 		.p_bits_per_pixel =	1,
    141 		.p_panel_shift =	2,
    142 		.p_fosc =		0x5,
    143 		.p_fosc_div =		0,
    144 		.p_precharge =		0x2,
    145 		.p_discharge =		0x2,
    146 		.p_compin_cfg =		SSDFB_COM_PINS_A1_MASK
    147 					| SSDFB_COM_PINS_ALTERNATIVE_MASK,
    148 		.p_vcomh_deselect_level = SH1106_VCOMH_DESELECT_LEVEL_DEFAULT,
    149 		.p_default_contrast =	0x80,
    150 		.p_multiplex_ratio =	0x3f,
    151 		.p_init =		ssdfb_init_ssd1306,
    152 		.p_sync =		ssdfb_sync_ssd1306
    153 	},
    154 	{
    155 		.p_product_id =		SSDFB_PRODUCT_ADAFRUIT_938,
    156 		.p_controller_id =	SSDFB_CONTROLLER_SSD1306,
    157 		.p_name =		"Adafruit Industries, LLC product 938",
    158 		.p_width =		128,
    159 		.p_height =		64,
    160 		.p_bits_per_pixel =	1,
    161 		.p_panel_shift =	0,
    162 		.p_fosc =		0x8,
    163 		.p_fosc_div =		0,
    164 		.p_precharge =		0x1,
    165 		.p_discharge =		0xf,
    166 		.p_compin_cfg =		0x12,
    167 		.p_vcomh_deselect_level = 0x40,
    168 		.p_default_contrast =	0x8f,
    169 		.p_multiplex_ratio =	0x3f,
    170 		.p_init =		ssdfb_init_ssd1306,
    171 		.p_sync =		ssdfb_sync_ssd1306
    172 	},
    173 	{
    174 		.p_product_id =		SSDFB_PRODUCT_ADAFRUIT_931,
    175 		.p_controller_id =	SSDFB_CONTROLLER_SSD1306,
    176 		.p_name =		"Adafruit Industries, LLC product 931",
    177 		.p_width =		128,
    178 		.p_height =		32,
    179 		.p_bits_per_pixel =	1,
    180 		.p_panel_shift =	0,
    181 		.p_fosc =		0x8,
    182 		.p_fosc_div =		0,
    183 		.p_precharge =		0x1,
    184 		.p_discharge =		0xf,
    185 		.p_compin_cfg =		0x2,
    186 		.p_vcomh_deselect_level = 0x40,
    187 		.p_default_contrast =	0x8f,
    188 		.p_multiplex_ratio =	0x1f,
    189 		.p_init =		ssdfb_init_ssd1306,
    190 		.p_sync =		ssdfb_sync_ssd1306
    191 	},
    192 	{
    193 		.p_product_id =		SSDFB_PRODUCT_SSD1322_GENERIC,
    194 		.p_controller_id =	SSDFB_CONTROLLER_SSD1322,
    195 		.p_name =		"generic",
    196 		.p_width =		256,
    197 		.p_height =		64,
    198 		.p_bits_per_pixel =	4,
    199 		.p_panel_shift =	28,
    200 		.p_vcomh_deselect_level = SSD1322_DEFAULT_VCOMH,
    201 		.p_fosc =		SSD1322_DEFAULT_FREQUENCY,
    202 		.p_fosc_div =		SSD1322_DEFAULT_DIVIDER,
    203 		.p_default_contrast =	SSD1322_DEFAULT_CONTRAST_CURRENT,
    204 		.p_multiplex_ratio =	0x3f,
    205 		.p_init =		ssdfb_init_ssd1322,
    206 		.p_sync =		ssdfb_sync_ssd1322
    207 	}
    208 };
    209 
    210 static const struct wsdisplay_accessops ssdfb_accessops = {
    211 	.ioctl =	ssdfb_ioctl,
    212 	.mmap =		ssdfb_mmap,
    213 	.alloc_screen =	ssdfb_alloc_screen,
    214 	.free_screen =	ssdfb_free_screen,
    215 	.show_screen =	ssdfb_show_screen
    216 };
    217 
    218 #define SSDFB_CMD1(c) do { cmd[0] = (c); error = sc->sc_cmd(sc->sc_cookie, cmd, 1, usepoll); } while(0)
    219 #define SSDFB_CMD2(c, a) do { cmd[0] = (c); cmd[1] = (a); error = sc->sc_cmd(sc->sc_cookie, cmd, 2, usepoll); } while(0)
    220 #define SSDFB_CMD3(c, a, b) do { cmd[0] = (c); cmd[1] = (a); cmd[2] = (b); error = sc->sc_cmd(sc->sc_cookie, cmd, 3, usepoll); } while(0)
    221 
    222 void
    223 ssdfb_attach(struct ssdfb_softc *sc, int flags)
    224 {
    225 	struct wsemuldisplaydev_attach_args aa;
    226 	struct rasops_info *ri = &sc->sc_ri;
    227 	int error = 0;
    228 	long defattr;
    229 	const struct ssdfb_product *p;
    230 
    231 	p = ssdfb_lookup_product(flags & SSDFB_ATTACH_FLAG_PRODUCT_MASK);
    232 	if (p == NULL) {
    233 		aprint_error(": unknown display assembly\n");
    234 		return;
    235 	}
    236 	sc->sc_p = p;
    237 
    238 	aprint_naive("\n");
    239 	aprint_normal(": %s (%s)\n",
    240 	    ssdfb_controller_names[p->p_controller_id],
    241 	    p->p_name);
    242 
    243 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    244 	sc->sc_is_console = flags & SSDFB_ATTACH_FLAG_CONSOLE ? true : false;
    245 	sc->sc_inverse = flags & SSDFB_ATTACH_FLAG_INVERSE ? true : false;
    246 	sc->sc_upsidedown = flags & SSDFB_ATTACH_FLAG_UPSIDEDOWN ? true : false;
    247 	sc->sc_backoff = 1;
    248 	sc->sc_contrast = sc->sc_p->p_default_contrast;
    249 	sc->sc_gddram_len = sc->sc_p->p_width * sc->sc_p->p_height
    250 	    * sc->sc_p->p_bits_per_pixel / 8;
    251 	sc->sc_gddram = kmem_alloc(sc->sc_gddram_len, KM_SLEEP);
    252 	if (sc->sc_gddram == NULL)
    253 		goto out;
    254 
    255 	aprint_normal_dev(sc->sc_dev, "%dx%d%s\n", sc->sc_p->p_width,
    256 	    sc->sc_p->p_height, sc->sc_is_console ? ", console" : "");
    257 
    258 	/*
    259 	 * Initialize rasops. The native depth is 1-bit monochrome and we
    260 	 * support this in text emul mode via rasops1. But modern Xorg
    261 	 * userland has many rendering glitches when running with 1-bit depth
    262 	 * so to better support this use case we instead declare ourselves as
    263 	 * an 8-bit display with a two entry constant color map.
    264 	 */
    265 	error = ssdfb_pick_font(&sc->sc_fontcookie, &sc->sc_font);
    266 	if (error) {
    267 		aprint_error_dev(sc->sc_dev, "no font\n");
    268 		goto out;
    269 	}
    270 #ifdef SSDFB_USE_NATIVE_DEPTH
    271 	ri->ri_depth =	sc->sc_p->p_bits_per_pixel;
    272 #else
    273 	ri->ri_depth =	8;
    274 #endif
    275 	ri->ri_font =	sc->sc_font;
    276 	ri->ri_width =	sc->sc_p->p_width;
    277 	ri->ri_height =	sc->sc_p->p_height;
    278 	ri->ri_stride =	ri->ri_width * ri->ri_depth / 8;
    279 	ri->ri_hw =	sc;
    280 	ri->ri_flg =	RI_FULLCLEAR | RI_FORCEMONO;
    281 	sc->sc_ri_bits_len = round_page(ri->ri_stride * ri->ri_height);
    282 	ri->ri_bits	= (u_char *)uvm_km_alloc(kernel_map, sc->sc_ri_bits_len,
    283 						 0, UVM_KMF_WIRED);
    284 	if (ri->ri_bits == NULL)
    285 		goto out;
    286 
    287 	error = rasops_init(ri,
    288 	    sc->sc_p->p_height / sc->sc_font->fontheight,
    289 	    sc->sc_p->p_width  / sc->sc_font->fontwidth);
    290 	if (error)
    291 		goto out;
    292 
    293 	ri->ri_caps &= ~WSSCREEN_WSCOLORS;
    294 
    295 	/*
    296 	 * Save original emul ops & insert our damage notification hooks.
    297 	 */
    298 	sc->sc_orig_riops =	ri->ri_ops;
    299 	ri->ri_ops.putchar =	ssdfb_putchar;
    300 	ri->ri_ops.copycols =	ssdfb_copycols;
    301 	ri->ri_ops.erasecols =	ssdfb_erasecols;
    302 	ri->ri_ops.copyrows =	ssdfb_copyrows;
    303 	ri->ri_ops.eraserows =	ssdfb_eraserows;
    304 	ri->ri_ops.cursor =	ssdfb_cursor;
    305 
    306 	/*
    307 	 * Set up the screen.
    308 	 */
    309 	sc->sc_screen_descr = (struct wsscreen_descr){
    310 		.name =		"default",
    311 		.ncols =	ri->ri_cols,
    312 		.nrows =	ri->ri_rows,
    313 		.textops =	&ri->ri_ops,
    314 		.fontwidth =	ri->ri_font->fontwidth,
    315 		.fontheight =	ri->ri_font->fontheight,
    316 		.capabilities =	ri->ri_caps
    317 	};
    318 	sc->sc_screens[0] = &sc->sc_screen_descr;
    319 	sc->sc_screenlist = (struct wsscreen_list){
    320 		.nscreens =	1,
    321 		.screens =	sc->sc_screens
    322 	};
    323 
    324 	/*
    325 	 * Initialize hardware.
    326 	 */
    327 	error = p->p_init(sc);
    328 	if (error)
    329 		goto out;
    330 
    331 	if (sc->sc_is_console)
    332 		ssdfb_set_usepoll(sc, true);
    333 
    334 	mutex_init(&sc->sc_cond_mtx, MUTEX_DEFAULT, IPL_SCHED);
    335 	cv_init(&sc->sc_cond, "ssdfb");
    336 	error = kthread_create(PRI_SOFTCLOCK, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE,
    337 	    NULL, ssdfb_thread, sc,  &sc->sc_thread, "%s",
    338 	    device_xname(sc->sc_dev));
    339 	if (error) {
    340 		cv_destroy(&sc->sc_cond);
    341 		mutex_destroy(&sc->sc_cond_mtx);
    342 		goto out;
    343 	}
    344 
    345 	/*
    346 	 * Attach wsdisplay.
    347 	 */
    348 	if (sc->sc_is_console) {
    349 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    350 		wsdisplay_cnattach(&sc->sc_screen_descr, ri, 0, 0, defattr);
    351 #if defined(DDB)
    352 		db_trap_callback = ssdfb_ddb_trap_callback;
    353 #endif
    354 	}
    355 	aa = (struct wsemuldisplaydev_attach_args){
    356 		.console =	sc->sc_is_console,
    357 		.scrdata =	&sc->sc_screenlist,
    358 		.accessops =	&ssdfb_accessops,
    359 		.accesscookie =	sc
    360 	};
    361 	sc->sc_wsdisplay =
    362 	    config_found(sc->sc_dev, &aa, wsemuldisplaydevprint, CFARG_EOL);
    363 
    364 	return;
    365 out:
    366 	aprint_error_dev(sc->sc_dev, "attach failed: %d\n", error);
    367 	if (sc->sc_gddram != NULL)
    368 		kmem_free(sc->sc_gddram, sc->sc_gddram_len);
    369 	if (ri->ri_bits != NULL)
    370 		uvm_km_free(kernel_map, (vaddr_t)ri->ri_bits, sc->sc_ri_bits_len,
    371 		    UVM_KMF_WIRED);
    372 	if (sc->sc_fontcookie > 0)
    373 		(void) wsfont_unlock(sc->sc_fontcookie);
    374 }
    375 
    376 int
    377 ssdfb_detach(struct ssdfb_softc *sc)
    378 {
    379 	mutex_enter(&sc->sc_cond_mtx);
    380 	sc->sc_detaching = true;
    381 	cv_broadcast(&sc->sc_cond);
    382 	mutex_exit(&sc->sc_cond_mtx);
    383 	kthread_join(sc->sc_thread);
    384 
    385 	if (sc->sc_uobj != NULL) {
    386 		rw_enter(sc->sc_uobj->vmobjlock, RW_WRITER);
    387 		sc->sc_uobj->uo_refs--;
    388 		rw_exit(sc->sc_uobj->vmobjlock);
    389 	}
    390 	config_detach(sc->sc_wsdisplay, DETACH_FORCE);
    391 
    392 	cv_destroy(&sc->sc_cond);
    393 	mutex_destroy(&sc->sc_cond_mtx);
    394 	uvm_km_free(kernel_map, (vaddr_t)sc->sc_ri.ri_bits, sc->sc_ri_bits_len,
    395 	    UVM_KMF_WIRED);
    396 	kmem_free(sc->sc_gddram, sc->sc_gddram_len);
    397 	(void) wsfont_unlock(sc->sc_fontcookie);
    398 	return 0;
    399 }
    400 
    401 static int
    402 ssdfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    403 {
    404 	struct ssdfb_softc *sc = v;
    405 	struct wsdisplay_param *wdp;
    406 	struct wsdisplay_cmap *wc;
    407 	u_char cmap[16];
    408 	int cmaplen = 1 << sc->sc_p->p_bits_per_pixel;
    409 	int i;
    410 	struct wsdisplayio_fbinfo *fbi;
    411 	int error;
    412 
    413 	switch (cmd) {
    414 	case WSDISPLAYIO_GTYPE:
    415 		 *(u_int *)data = WSDISPLAY_TYPE_SSDFB;
    416 		return 0;
    417 	case WSDISPLAYIO_GINFO:
    418 		*(struct wsdisplay_fbinfo *)data = (struct wsdisplay_fbinfo){
    419 			.width =	sc->sc_ri.ri_width,
    420 			.height =	sc->sc_ri.ri_height,
    421 			.depth =	sc->sc_ri.ri_depth,
    422 			.cmsize =	cmaplen
    423 		};
    424 		return 0;
    425 	case WSDISPLAYIO_GET_FBINFO:
    426 		fbi = (struct wsdisplayio_fbinfo *)data;
    427 		error = wsdisplayio_get_fbinfo(&sc->sc_ri, fbi);
    428 		fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = cmaplen;
    429 		/* fbi->fbi_pixeltype = WSFB_GREYSCALE */;
    430 		return error;
    431 	case WSDISPLAYIO_LINEBYTES:
    432 		*(u_int *)data = sc->sc_ri.ri_stride;
    433 		return 0;
    434 	case WSDISPLAYIO_GETPARAM:
    435 		wdp = (struct wsdisplay_param *)data;
    436 		if (wdp->param != WSDISPLAYIO_PARAM_CONTRAST)
    437 			return EINVAL;
    438 		wdp->min = 0;
    439 		wdp->max = 0xff;
    440 		wdp->curval = sc->sc_contrast;
    441 		return 0;
    442 	case WSDISPLAYIO_SETPARAM:
    443 		wdp = (struct wsdisplay_param *)data;
    444 		if (wdp->param != WSDISPLAYIO_PARAM_CONTRAST)
    445 			return EINVAL;
    446 		if (wdp->curval < 0 || wdp->curval > 0xff)
    447 			return EINVAL;
    448 		return ssdfb_set_contrast(sc, wdp->curval, sc->sc_usepoll);
    449 	case WSDISPLAYIO_GMODE:
    450 		*(u_int *)data = sc->sc_mode;
    451 		return 0;
    452 	case WSDISPLAYIO_SMODE:
    453 		return ssdfb_set_mode(sc, *(u_int *)data);
    454 	case WSDISPLAYIO_GVIDEO:
    455 		*(u_int *)data = sc->sc_display_on
    456 			? WSDISPLAYIO_VIDEO_ON
    457 			: WSDISPLAYIO_VIDEO_OFF;
    458 		return 0;
    459 	case WSDISPLAYIO_SVIDEO:
    460 		switch (*(u_int *)data) {
    461 		case WSDISPLAYIO_VIDEO_ON:
    462 		case WSDISPLAYIO_VIDEO_OFF:
    463 			break;
    464 		default:
    465 			return EINVAL;
    466 		}
    467 		return ssdfb_set_display_on(sc,
    468 		    *(u_int *)data == WSDISPLAYIO_VIDEO_ON ? true : false,
    469 		    sc->sc_usepoll);
    470 #if 0 /* don't let userland mess with polling yet */
    471 	case WSDISPLAYIO_SET_POLLING:
    472 		switch (*(u_int *)data) {
    473 		case 0:
    474 		case 1:
    475 			break;
    476 		default:
    477 			return EINVAL;
    478 		}
    479 		mutex_enter(&sc->sc_cond_mtx);
    480 		ssdfb_set_usepoll(sc, *(u_int *)data ? true : false);
    481 		cv_broadcast(&sc->sc_cond);
    482 		mutex_exit(&sc->sc_cond_mtx);
    483 		return 0;
    484 #endif
    485 	case WSDISPLAYIO_GETCMAP:
    486 		wc = (struct wsdisplay_cmap *)data;
    487 		if (wc->index >= cmaplen ||
    488 		    wc->count > cmaplen - wc->index)
    489 			return EINVAL;
    490 		for(i = 0; i < cmaplen; i++) {
    491 			cmap[i] = 255 * i / (cmaplen - 1);
    492 		}
    493 		error = copyout(&cmap[wc->index], wc->red, wc->count);
    494 		if (error)
    495 			return error;
    496 		error = copyout(&cmap[wc->index], wc->green, wc->count);
    497 		if (error)
    498 			return error;
    499 		error = copyout(&cmap[wc->index], wc->blue, wc->count);
    500 		return error;
    501 	case WSDISPLAYIO_PUTCMAP:
    502 		return ENODEV;
    503 	}
    504 
    505 	return EPASSTHROUGH;
    506 }
    507 
    508 static paddr_t
    509 ssdfb_mmap(void *v, void *vs, off_t off, int prot)
    510 {
    511 	struct ssdfb_softc *sc = (struct ssdfb_softc *)v;
    512 	struct rasops_info *ri = &sc->sc_ri;
    513 	vaddr_t va_base = (vaddr_t)ri->ri_bits;
    514 	paddr_t pa;
    515 
    516 	if (off < 0 || off >= sc->sc_ri_bits_len || (off & PAGE_MASK) != 0)
    517 		return -1;
    518 
    519 	if (!pmap_extract(pmap_kernel(), va_base + off, &pa))
    520 		return -1;
    521 
    522 	return atop(pa);
    523 }
    524 
    525 static int
    526 ssdfb_alloc_screen(void *v, const struct wsscreen_descr *descr, void **cookiep,
    527 		   int *curxp, int *curyp, long *attrp)
    528 {
    529 	struct ssdfb_softc *sc = v;
    530 	struct rasops_info *ri = &sc->sc_ri;
    531 
    532 	if (sc->sc_nscreens > 0)
    533 		return ENOMEM;
    534 
    535 	ri->ri_ops.allocattr(ri, 0, 0, 0, attrp);
    536 	*cookiep = &sc->sc_ri;
    537 	*curxp = 0;
    538 	*curyp = 0;
    539 	sc->sc_nscreens++;
    540 
    541 	return 0;
    542 }
    543 
    544 static void
    545 ssdfb_free_screen(void *v, void *cookie)
    546 {
    547 	struct ssdfb_softc *sc = v;
    548 
    549 	if (sc->sc_is_console)
    550 		panic("ssdfb_free_screen: is console");
    551 
    552 	sc->sc_nscreens--;
    553 }
    554 
    555 static int
    556 ssdfb_show_screen(void *v, void *cookie, int waitok,
    557 		  void (*cb) (void *, int, int), void *cb_arg)
    558 {
    559 	return 0;
    560 }
    561 
    562 static void
    563 ssdfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    564 {
    565 	struct rasops_info *ri = (struct rasops_info *)cookie;
    566 	struct ssdfb_softc *sc = ri->ri_hw;
    567 
    568 	sc->sc_orig_riops.putchar(cookie, row, col, c, attr);
    569 	ssdfb_damage(sc);
    570 }
    571 
    572 static void
    573 ssdfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    574 {
    575 	struct rasops_info *ri = (struct rasops_info *)cookie;
    576 	struct ssdfb_softc *sc = ri->ri_hw;
    577 
    578 	sc->sc_orig_riops.copycols(cookie, row, srccol, dstcol, ncols);
    579 	ssdfb_damage(sc);
    580 }
    581 
    582 static void
    583 ssdfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    584 {
    585 	struct rasops_info *ri = (struct rasops_info *)cookie;
    586 	struct ssdfb_softc *sc = ri->ri_hw;
    587 
    588 	sc->sc_orig_riops.erasecols(cookie, row, startcol, ncols, fillattr);
    589 	ssdfb_damage(sc);
    590 }
    591 
    592 static void
    593 ssdfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    594 {
    595 	struct rasops_info *ri = (struct rasops_info *)cookie;
    596 	struct ssdfb_softc *sc = ri->ri_hw;
    597 
    598 	sc->sc_orig_riops.copyrows(cookie, srcrow, dstrow, nrows);
    599 	ssdfb_damage(sc);
    600 }
    601 
    602 static void
    603 ssdfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    604 {
    605 	struct rasops_info *ri = (struct rasops_info *)cookie;
    606 	struct ssdfb_softc *sc = ri->ri_hw;
    607 
    608 	sc->sc_orig_riops.eraserows(cookie, row, nrows, fillattr);
    609 	ssdfb_damage(sc);
    610 }
    611 
    612 static void
    613 ssdfb_cursor(void *cookie, int on, int row, int col)
    614 {
    615 	struct rasops_info *ri = (struct rasops_info *)cookie;
    616 	struct ssdfb_softc *sc = ri->ri_hw;
    617 
    618 	sc->sc_orig_riops.cursor(cookie, on, row, col);
    619 	ssdfb_damage(sc);
    620 }
    621 
    622 static int
    623 ssdfb_init_ssd1306(struct ssdfb_softc *sc)
    624 {
    625 	int error;
    626 	uint8_t cmd[2];
    627 	bool usepoll = true;
    628 
    629 	/*
    630 	 * Enter sleep.
    631 	 */
    632 	SSDFB_CMD1(SSDFB_CMD_SET_DISPLAY_OFF);
    633 	if (error)
    634 		return error;
    635 	SSDFB_CMD1(SSDFB_CMD_DEACTIVATE_SCROLL);
    636 	if (error)
    637 		return error;
    638 	SSDFB_CMD1(SSDFB_CMD_ENTIRE_DISPLAY_OFF);
    639 	if (error)
    640 		return error;
    641 
    642 	/*
    643 	 * Configure physical display panel layout.
    644 	 */
    645 	SSDFB_CMD2(SSDFB_CMD_SET_MULTIPLEX_RATIO, sc->sc_p->p_multiplex_ratio);
    646 	if (error)
    647 		return error;
    648 	SSDFB_CMD2(SSDFB_CMD_SET_DISPLAY_OFFSET, 0);
    649 	if (error)
    650 		return error;
    651 	SSDFB_CMD1(SSDFB_CMD_SET_DISPLAY_START_LINE_BASE + 0x00);
    652 	if (error)
    653 		return error;
    654 	SSDFB_CMD2(SSDFB_CMD_SET_COM_PINS_HARDWARE_CFG, sc->sc_p->p_compin_cfg);
    655 	if (error)
    656 		return error;
    657 	if (sc->sc_upsidedown) {
    658 		SSDFB_CMD1(SSDFB_CMD_SET_SEGMENT_REMAP_REVERSE);
    659 		if (error)
    660 			return error;
    661 		SSDFB_CMD1(SSDFB_CMD_SET_COM_OUTPUT_DIRECTION_REMAP);
    662 		if (error)
    663 			return error;
    664 	} else {
    665 		SSDFB_CMD1(SSDFB_CMD_SET_SEGMENT_REMAP_NORMAL);
    666 		if (error)
    667 			return error;
    668 		SSDFB_CMD1(SSDFB_CMD_SET_COM_OUTPUT_DIRECTION_NORMAL);
    669 		if (error)
    670 			return error;
    671 	}
    672 	SSDFB_CMD1(SSDFB_CMD_SET_NORMAL_DISPLAY + (uint8_t)sc->sc_inverse);
    673 	if (error)
    674 		return error;
    675 
    676 	/*
    677 	 * Configure timing characteristics.
    678 	 */
    679 	SSDFB_CMD2(SSDFB_CMD_SET_DISPLAY_CLOCK_RATIO,
    680 	    __SHIFTIN(sc->sc_p->p_fosc, SSDFB_DISPLAY_CLOCK_OSCILLATOR_MASK) |
    681 	    __SHIFTIN(sc->sc_p->p_fosc_div, SSDFB_DISPLAY_CLOCK_DIVIDER_MASK));
    682 	if (error)
    683 		return error;
    684 	SSDFB_CMD2(SSDFB_CMD_SET_CONTRAST_CONTROL, sc->sc_contrast);
    685 	if (error)
    686 		return error;
    687 	SSDFB_CMD2(SSDFB_CMD_SET_PRECHARGE_PERIOD,
    688 	    __SHIFTIN(sc->sc_p->p_precharge, SSDFB_PRECHARGE_MASK) |
    689 	    __SHIFTIN(sc->sc_p->p_discharge, SSDFB_DISCHARGE_MASK));
    690 	if (error)
    691 		return error;
    692 	SSDFB_CMD2(SSDFB_CMD_SET_VCOMH_DESELECT_LEVEL,
    693 	    sc->sc_p->p_vcomh_deselect_level);
    694 	if (error)
    695 		return error;
    696 
    697 	/*
    698 	 * Start charge pumps.
    699 	 */
    700 	if (sc->sc_p->p_controller_id == SSDFB_CONTROLLER_SH1106) {
    701 		SSDFB_CMD1(SH1106_CMD_SET_CHARGE_PUMP_7V4);
    702 		if (error)
    703 			return error;
    704 		SSDFB_CMD2(SH1106_CMD_SET_DC_DC, SH1106_DC_DC_ON);
    705 		if (error)
    706 			return error;
    707 	} else {
    708 		SSDFB_CMD2(SSD1306_CMD_SET_CHARGE_PUMP,
    709 		    SSD1306_CHARGE_PUMP_ENABLE);
    710 		if (error)
    711 			return error;
    712 	}
    713 
    714 	ssdfb_clear_screen(sc);
    715 	error = sc->sc_p->p_sync(sc, usepoll);
    716 	if (error)
    717 		return error;
    718 	error = ssdfb_set_display_on(sc, true, usepoll);
    719 
    720 	return error;
    721 }
    722 
    723 static int
    724 ssdfb_init_ssd1322(struct ssdfb_softc *sc)
    725 {
    726 	int error;
    727 	uint8_t cmd[3];
    728 	bool usepoll = true;
    729 	uint8_t remap;
    730 	uint8_t dualcom;
    731 
    732 	/*
    733 	 * Enter sleep.
    734 	 */
    735 	SSDFB_CMD2(SSD1322_CMD_SET_COMMAND_LOCK, SSD1322_COMMAND_UNLOCK_MAGIC);
    736 	if (error)
    737 		return error;
    738 	SSDFB_CMD1(SSD1322_CMD_SET_SLEEP_MODE_ON);
    739 	if (error)
    740 		return error;
    741 
    742 	/*
    743 	 * Start charge pumps.
    744 	 */
    745 	SSDFB_CMD2(SSD1322_CMD_FUNCTION_SELECTION,
    746 	    SSD1322_FUNCTION_SELECTION_INTERNAL_VDD);
    747 	if (error)
    748 		return error;
    749 	SSDFB_CMD2(SSD1322_CMD_SET_VCOMH, sc->sc_p->p_vcomh_deselect_level);
    750 	if (error)
    751 		return error;
    752 	SSDFB_CMD2(SSD1322_CMD_SET_PRE_CHARGE_VOLTAGE_LEVEL,
    753 	    SSD1322_DEFAULT_PRE_CHARGE_VOLTAGE_LEVEL);
    754 	if (error)
    755 		return error;
    756 	SSDFB_CMD2(SSD1322_CMD_SET_GPIO,
    757 	    SSD1322_GPIO0_DISABLED | SSD1322_GPIO1_DISABLED);
    758 	if (error)
    759 		return error;
    760 
    761 	/*
    762 	 * Configure timing characteristics.
    763 	 */
    764 	SSDFB_CMD2(SSD1322_CMD_SET_FRONT_CLOCK_DIVIDER,
    765 	   __SHIFTIN(sc->sc_p->p_fosc, SSD1322_FREQUENCY_MASK) |
    766 	   __SHIFTIN(sc->sc_p->p_fosc_div, SSD1322_DIVIDER_MASK));
    767 	if (error)
    768 		return error;
    769 	SSDFB_CMD2(SSD1322_CMD_SET_PHASE_LENGTH,
    770 	   __SHIFTIN(SSD1322_DEFAULT_PHASE_2,
    771 	    SSD1322_PHASE_LENGTH_PHASE_2_MASK) |
    772 	    __SHIFTIN(SSD1322_DEFAULT_PHASE_1,
    773 	    SSD1322_PHASE_LENGTH_PHASE_1_MASK));
    774 	if (error)
    775 		return error;
    776 	SSDFB_CMD2(SSD1322_CMD_SET_SECOND_PRECHARGE_PERIOD,
    777 	    SSD1322_DEFAULT_SECOND_PRECHARGE);
    778 	if (error)
    779 		return error;
    780 
    781 	/*
    782 	 * Configure physical display panel layout.
    783 	 */
    784 	SSDFB_CMD2(SSD1322_CMD_SET_MUX_RATIO, sc->sc_p->p_multiplex_ratio);
    785 	if (error)
    786 		return error;
    787 	if (sc->sc_upsidedown)
    788 		remap = 0x10;
    789 	else
    790 		remap = 0x2;
    791 	dualcom = 0x1;
    792 	if (sc->sc_p->p_multiplex_ratio <= 63)
    793 		dualcom |= 0x10;
    794 	SSDFB_CMD3(SSD1322_CMD_SET_REMAP_AND_DUAL_COM_LINE_MODE, remap, dualcom);
    795 	if (error)
    796 		return error;
    797 
    798 	/*
    799 	 * Contrast settings.
    800 	 */
    801 	SSDFB_CMD1(SSD1322_CMD_SET_DEFAULT_GRAY_SCALE_TABLE);
    802 	if (error)
    803 		return error;
    804 	SSDFB_CMD3(SSD1322_CMD_DISPLAY_ENHANCEMENT_A,
    805 	    SSD1322_DISPLAY_ENHANCEMENT_A_MAGIC1,
    806 	    SSD1322_DISPLAY_ENHANCEMENT_A_MAGIC2);
    807 	if (error)
    808 		return error;
    809 	SSDFB_CMD3(SSD1322_CMD_DISPLAY_ENHANCEMENT_B,
    810 	    SSD1322_DISPLAY_ENHANCEMENT_B_MAGIC1,
    811 	    SSD1322_DISPLAY_ENHANCEMENT_B_MAGIC2);
    812 	if (error)
    813 		return error;
    814 	SSDFB_CMD2(SSD1322_CMD_SET_CONTRAST_CURRENT,
    815 	    sc->sc_contrast);
    816 	if (error)
    817 		return error;
    818 	SSDFB_CMD2(SSD1322_CMD_MASTER_CONTRAST_CURRENT_CONTROL,
    819 	    SSD1322_DEFAULT_MASTER_CONTRAST_CURRENT_CONTROL);
    820 	if (error)
    821 		return error;
    822 
    823 	/*
    824 	 * Reset display engine state.
    825 	 */
    826 	SSDFB_CMD2(SSD1322_CMD_SET_DISPLAY_OFFSET, 0x00);
    827 	if (error)
    828 		return error;
    829 	SSDFB_CMD2(SSD1322_CMD_SET_DISPLAY_START_LINE, 0x00);
    830 	if (error)
    831 		return error;
    832 	SSDFB_CMD1(SSD1322_CMD_NORMAL_DISPLAY + (uint8_t)sc->sc_inverse);
    833 	if (error)
    834 		return error;
    835 	SSDFB_CMD1(SSD1322_CMD_EXIT_PARTIAL_DISPLAY);
    836 	if (error)
    837 		return error;
    838 
    839 	ssdfb_clear_screen(sc);
    840 	error = ssdfb_sync(sc, usepoll);
    841 	if (error)
    842 		return error;
    843 
    844 	error = ssdfb_set_display_on(sc, true, usepoll);
    845 
    846 	return error;
    847 }
    848 
    849 static int
    850 ssdfb_set_contrast(struct ssdfb_softc *sc, uint8_t value, bool usepoll)
    851 {
    852 	uint8_t cmd[2];
    853 
    854 	switch (sc->sc_p->p_controller_id) {
    855 	case SSDFB_CONTROLLER_SSD1322:
    856 		cmd[0] = SSD1322_CMD_SET_CONTRAST_CURRENT;
    857 		break;
    858 	default:
    859 		cmd[0] = SSDFB_CMD_SET_CONTRAST_CONTROL;
    860 	}
    861 	cmd[1] = sc->sc_contrast = value;
    862 
    863 	return sc->sc_cmd(sc->sc_cookie, cmd, sizeof(cmd), usepoll);
    864 }
    865 
    866 static int
    867 ssdfb_set_display_on(struct ssdfb_softc *sc, bool value, bool usepoll)
    868 {
    869 	uint8_t cmd[1];
    870 	int error;
    871 	sc->sc_display_on = value;
    872 
    873 	SSDFB_CMD1(value ? SSDFB_CMD_SET_DISPLAY_ON : SSDFB_CMD_SET_DISPLAY_OFF);
    874 
    875 	return error;
    876 }
    877 
    878 static int
    879 ssdfb_set_mode(struct ssdfb_softc *sc, u_int mode)
    880 {
    881 	switch (mode) {
    882 	case WSDISPLAYIO_MODE_EMUL:
    883 	case WSDISPLAYIO_MODE_DUMBFB:
    884 		break;
    885 	default:
    886 		return EINVAL;
    887 	}
    888 	if (mode == sc->sc_mode)
    889 		return 0;
    890 	mutex_enter(&sc->sc_cond_mtx);
    891 	sc->sc_mode = mode;
    892 	cv_broadcast(&sc->sc_cond);
    893 	mutex_exit(&sc->sc_cond_mtx);
    894 	ssdfb_clear_screen(sc);
    895 	ssdfb_damage(sc);
    896 
    897 	return 0;
    898 }
    899 
    900 static void
    901 ssdfb_damage(struct ssdfb_softc *sc)
    902 {
    903 	int s;
    904 
    905 	if (sc->sc_usepoll) {
    906 		(void) ssdfb_sync(sc, true);
    907 	} else {
    908 		/*
    909 		 * kernel code isn't permitted to call us via kprintf at
    910 		 * splhigh. In case misbehaving code calls us anyway we can't
    911 		 * safely take the mutex so we skip the damage notification.
    912 		 */
    913 		if (sc->sc_is_console) {
    914 			s = splhigh();
    915 			splx(s);
    916 			if (s == IPL_HIGH)
    917 				return;
    918 		}
    919 		mutex_enter(&sc->sc_cond_mtx);
    920 		sc->sc_modified = true;
    921 		cv_broadcast(&sc->sc_cond);
    922 		mutex_exit(&sc->sc_cond_mtx);
    923 	}
    924 }
    925 
    926 static void
    927 ssdfb_udv_attach(struct ssdfb_softc *sc)
    928 {
    929 	extern const struct cdevsw wsdisplay_cdevsw;
    930 	dev_t dev;
    931 #define WSDISPLAYMINOR(unit, screen)	(((unit) << 8) | (screen))
    932 	dev = makedev(cdevsw_lookup_major(&wsdisplay_cdevsw),
    933 	    WSDISPLAYMINOR(device_unit(sc->sc_wsdisplay), 0));
    934 	sc->sc_uobj = udv_attach(dev, VM_PROT_READ|VM_PROT_WRITE, 0,
    935 	    sc->sc_ri_bits_len);
    936 }
    937 
    938 static bool
    939 ssdfb_is_modified(struct ssdfb_softc *sc)
    940 {
    941 	vaddr_t va, va_end;
    942 
    943 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)
    944 		return sc->sc_modified;
    945 
    946 	if (sc->sc_uobj == NULL)
    947 		return false;
    948 
    949 	va = (vaddr_t)sc->sc_ri.ri_bits;
    950 	va_end = va + sc->sc_ri_bits_len;
    951 	while (va < va_end) {
    952 		if (pmap_is_modified(uvm_pageratop(va)))
    953 			return true;
    954 		va += PAGE_SIZE;
    955 	}
    956 
    957 	return false;
    958 }
    959 
    960 static bool
    961 ssdfb_clear_modify(struct ssdfb_softc *sc)
    962 {
    963 	vaddr_t va, va_end;
    964 	bool ret;
    965 
    966 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    967 		mutex_enter(&sc->sc_cond_mtx);
    968 		ret = sc->sc_modified;
    969 		sc->sc_modified = false;
    970 		mutex_exit(&sc->sc_cond_mtx);
    971 		return ret;
    972 	}
    973 
    974 	if (sc->sc_uobj == NULL)
    975 		return false;
    976 
    977 	va = (vaddr_t)sc->sc_ri.ri_bits;
    978 	va_end = va + sc->sc_ri_bits_len;
    979 	ret = false;
    980 	while (va < va_end) {
    981 		if (pmap_clear_modify(uvm_pageratop(va)))
    982 			ret = true;
    983 		va += PAGE_SIZE;
    984 	}
    985 
    986 	return ret;
    987 }
    988 
    989 static void
    990 ssdfb_thread(void *arg)
    991 {
    992 	struct ssdfb_softc *sc = (struct ssdfb_softc *)arg;
    993 	int error;
    994 
    995 	mutex_enter(&sc->sc_cond_mtx);
    996 
    997 	if (sc->sc_usepoll)
    998 		ssdfb_set_usepoll(sc, false);
    999 
   1000 	while(!sc->sc_detaching) {
   1001 		if (sc->sc_mode == WSDISPLAYIO_MODE_DUMBFB &&
   1002 		    sc->sc_uobj == NULL) {
   1003 			mutex_exit(&sc->sc_cond_mtx);
   1004 			ssdfb_udv_attach(sc);
   1005 			mutex_enter(&sc->sc_cond_mtx);
   1006 		}
   1007 		if (!ssdfb_is_modified(sc)) {
   1008 			if (cv_timedwait(&sc->sc_cond, &sc->sc_cond_mtx,
   1009 			    sc->sc_mode == WSDISPLAYIO_MODE_EMUL
   1010 			    ? 0 : sc->sc_backoff) == EWOULDBLOCK
   1011 			    && sc->sc_backoff < mstohz(200)) {
   1012 				sc->sc_backoff <<= 1;
   1013 			}
   1014 			continue;
   1015 		}
   1016 		sc->sc_backoff = 1;
   1017 		mutex_exit(&sc->sc_cond_mtx);
   1018 		(void) ssdfb_clear_modify(sc);
   1019 		if (!sc->sc_usepoll) {
   1020 			error = ssdfb_sync(sc, false);
   1021 			if (error)
   1022 				device_printf(sc->sc_dev,
   1023 				    "ssdfb_sync: error %d\n",
   1024 				    error);
   1025 		}
   1026 		mutex_enter(&sc->sc_cond_mtx);
   1027 	}
   1028 
   1029 	mutex_exit(&sc->sc_cond_mtx);
   1030 	kthread_exit(0);
   1031 }
   1032 
   1033 static void
   1034 ssdfb_set_usepoll(struct ssdfb_softc *sc, bool enable)
   1035 {
   1036 	sc->sc_usepoll = enable;
   1037 }
   1038 
   1039 static int
   1040 ssdfb_sync(struct ssdfb_softc *sc, bool usepoll)
   1041 {
   1042 	return sc->sc_p->p_sync(sc, usepoll);
   1043 }
   1044 
   1045 static int
   1046 ssdfb_sync_ssd1306(struct ssdfb_softc *sc, bool usepoll)
   1047 {
   1048 	struct rasops_info *ri = &sc->sc_ri;
   1049 	int block_size = 8;
   1050 	int ri_block_stride = ri->ri_stride * block_size;
   1051 	int height_in_blocks = sc->sc_p->p_height / block_size;
   1052 	int width_in_blocks  = sc->sc_p->p_width / block_size;
   1053 	int ri_block_step = block_size * ri->ri_depth / 8;
   1054 	int x, y;
   1055 	union ssdfb_block *blockp;
   1056 	uint64_t raw_block;
   1057 	uint8_t *src;
   1058 	int x1, x2, y1, y2;
   1059 
   1060 	/*
   1061 	 * Transfer rasops bitmap into gddram shadow buffer while keeping track
   1062 	 * of the bounding box of the dirty region we scribbled over.
   1063 	 */
   1064 	x1 = width_in_blocks;
   1065 	x2 = -1;
   1066 	y1 = height_in_blocks;
   1067 	y2 = -1;
   1068 	for (y = 0; y < height_in_blocks; y++) {
   1069 		src = &ri->ri_bits[y * ri_block_stride];
   1070 		blockp = &sc->sc_gddram[y * width_in_blocks];
   1071 		for (x = 0; x < width_in_blocks; x++) {
   1072 			raw_block = ssdfb_transpose_block(src, ri->ri_stride);
   1073 			if (raw_block != blockp->raw) {
   1074 				blockp->raw = raw_block;
   1075 				if (x1 > x)
   1076 					x1 = x;
   1077 				if (x2 < x)
   1078 					x2 = x;
   1079 				if (y1 > y)
   1080 					y1 = y;
   1081 				if (y2 < y)
   1082 					y2 = y;
   1083 			}
   1084 			src += ri_block_step;
   1085 			blockp++;
   1086 		}
   1087 	}
   1088 	if (x2 != -1)
   1089 		return sc->sc_transfer_rect(sc->sc_cookie,
   1090 		    x1 * block_size + sc->sc_p->p_panel_shift,
   1091 		    (x2 + 1) * block_size - 1 + sc->sc_p->p_panel_shift,
   1092 		    y1,
   1093 		    y2,
   1094 		    &sc->sc_gddram[y1 * width_in_blocks + x1].col[0],
   1095 		    sc->sc_p->p_width,
   1096 		    usepoll);
   1097 
   1098 	return 0;
   1099 }
   1100 
   1101 static int
   1102 ssdfb_sync_ssd1322(struct ssdfb_softc *sc, bool usepoll)
   1103 {
   1104 	struct rasops_info *ri = &sc->sc_ri;
   1105 	int block_size_w = 4;
   1106 	int width = sc->sc_p->p_width;
   1107 	int height = sc->sc_p->p_height;
   1108 	int width_in_blocks = width / block_size_w;
   1109 	int x, y;
   1110 	uint16_t *blockp;
   1111 	uint16_t raw_block;
   1112 	uint16_t *src;
   1113 	uint32_t *src32;
   1114 	int x1, x2, y1, y2;
   1115 
   1116 	/*
   1117 	 * Transfer rasops bitmap into gddram shadow buffer while keeping track
   1118 	 * of the bounding box of the dirty region we scribbled over.
   1119 	 */
   1120 	x1 = sc->sc_p->p_width;
   1121 	x2 = -1;
   1122 	y1 = sc->sc_p->p_height;
   1123 	y2 = -1;
   1124 	blockp = (uint16_t*)sc->sc_gddram;
   1125 	for (y = 0; y < height; y++) {
   1126 		src = (uint16_t*)&ri->ri_bits[y * ri->ri_stride];
   1127 		src32 = (uint32_t*)src;
   1128 		for (x = 0; x < width_in_blocks; x++) {
   1129 #if _BYTE_ORDER == _LITTLE_ENDIAN
   1130 #  ifdef SSDFB_USE_NATIVE_DEPTH
   1131 			raw_block =
   1132 			    ((*src << 12) & 0xf000) |
   1133 			    ((*src << 4) & 0x0f00) |
   1134 			    ((*src >> 4) & 0x00f0) |
   1135 			    ((*src >> 12) & 0x000f);
   1136 			src++;
   1137 #  else
   1138 			raw_block =
   1139 			    ((*src32 <<  8) & 0x0f00) |
   1140 			    ((*src32 <<  4) & 0xf000) |
   1141 			    ((*src32 >> 16) & 0x000f) |
   1142 			    ((*src32 >> 20) & 0x00f0);
   1143 #  endif
   1144 			src32++;
   1145 #else
   1146 #  error please add big endian host support here
   1147 #endif
   1148 			if (raw_block != *blockp) {
   1149 				*blockp = raw_block;
   1150 				if (x1 > x)
   1151 					x1 = x;
   1152 				if (x2 < x)
   1153 					x2 = x;
   1154 				if (y1 > y)
   1155 					y1 = y;
   1156 				if (y2 < y)
   1157 					y2 = y;
   1158 			}
   1159 			blockp++;
   1160 		}
   1161 	}
   1162 
   1163 	blockp = (uint16_t*)sc->sc_gddram;
   1164 	if (x2 != -1)
   1165 		return sc->sc_transfer_rect(sc->sc_cookie,
   1166 		    x1 + sc->sc_p->p_panel_shift,
   1167 		    x2 + sc->sc_p->p_panel_shift,
   1168 		    y1,
   1169 		    y2,
   1170 		    (uint8_t*)&blockp[y1 * width_in_blocks + x1],
   1171 		    width * sc->sc_p->p_bits_per_pixel / 8,
   1172 		    usepoll);
   1173 	return 0;
   1174 }
   1175 
   1176 static uint64_t
   1177 ssdfb_transpose_block(uint8_t *src, size_t src_stride)
   1178 {
   1179 	uint64_t x = 0;
   1180 #ifdef SSDFB_USE_NATIVE_DEPTH
   1181 	uint64_t t;
   1182 	int i;
   1183 
   1184 	/*
   1185 	 * collect the 8x8 block.
   1186 	 */
   1187 	for (i = 0; i < 8; i++) {
   1188 		x >>= 8;
   1189 		x |= (uint64_t)src[i * src_stride] << 56;
   1190 	}
   1191 
   1192 	/*
   1193 	 * Transpose it into gddram layout.
   1194 	 * Post-transpose bswap is the same as pre-transpose bit order reversal.
   1195 	 * We do this to match rasops1 bit order.
   1196 	 */
   1197 	t = (x ^ (x >> 28)) & 0x00000000F0F0F0F0ULL;
   1198 	x = x ^ t ^ (t << 28);
   1199 	t = (x ^ (x >> 14)) & 0x0000CCCC0000CCCCULL;
   1200 	x = x ^ t ^ (t << 14);
   1201 	t = (x ^ (x >>  7)) & 0x00AA00AA00AA00AAULL;
   1202 	x = x ^ t ^ (t <<  7);
   1203 	x = bswap64(x);
   1204 #else
   1205 	int m, n;
   1206 
   1207 	for (m = 0; m < 8; m++) {
   1208 		for (n = 0; n < 8; n++) {
   1209 			x >>= 1;
   1210 			x |= src[n * src_stride + m] ? (1ULL << 63) : 0;
   1211 		}
   1212 	}
   1213 #endif
   1214 	return htole64(x);
   1215 }
   1216 
   1217 static const struct ssdfb_product *
   1218 ssdfb_lookup_product(ssdfb_product_id_t id)
   1219 {
   1220 	int i;
   1221 
   1222 	for (i = 0; i < __arraycount(ssdfb_products); i++) {
   1223 		if (ssdfb_products[i].p_product_id == id)
   1224 			return &ssdfb_products[i];
   1225 	}
   1226 
   1227 	return NULL;
   1228 }
   1229 
   1230 static int
   1231 ssdfb_pick_font(int *cookiep, struct wsdisplay_font **fontp)
   1232 {
   1233 	int error;
   1234 	int c;
   1235 	struct wsdisplay_font *f;
   1236 	int i;
   1237 	uint8_t d[4][2] = {{5, 8}, {8, 8}, {8, 10} ,{8, 16}};
   1238 
   1239 	/*
   1240 	 * Try to find fonts in order of increasing size.
   1241 	 */
   1242 	wsfont_init();
   1243 	for(i = 0; i < __arraycount(d); i++) {
   1244 		c = wsfont_find(NULL, d[i][0], d[i][1], 0,
   1245 		    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
   1246 		    WSFONT_FIND_BITMAP);
   1247 		if (c > 0)
   1248 			break;
   1249 	}
   1250 	if (c <= 0)
   1251 		return ENOENT;
   1252 	error = wsfont_lock(c, &f);
   1253 	if (error)
   1254 		return error;
   1255 	*cookiep = c;
   1256 	*fontp = f;
   1257 
   1258 	return 0;
   1259 }
   1260 
   1261 static void
   1262 ssdfb_clear_screen(struct ssdfb_softc *sc)
   1263 {
   1264 	struct rasops_info *ri = &sc->sc_ri;
   1265 
   1266 	memset(sc->sc_gddram, 0xff, sc->sc_gddram_len);
   1267 	memset(ri->ri_bits, 0, sc->sc_ri_bits_len);
   1268 }
   1269 
   1270 #if defined(DDB)
   1271 static void
   1272 ssdfb_ddb_trap_callback(int enable)
   1273 {
   1274 	extern struct cfdriver ssdfb_cd;
   1275 	struct ssdfb_softc *sc;
   1276 	int i;
   1277 
   1278 	for (i = 0; i < ssdfb_cd.cd_ndevs; i++) {
   1279 		sc = device_lookup_private(&ssdfb_cd, i);
   1280 		if (sc != NULL && sc->sc_is_console) {
   1281 			ssdfb_set_usepoll(sc, (bool)enable);
   1282 		}
   1283         }
   1284 }
   1285 #endif
   1286