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