Home | History | Annotate | Line # | Download | only in wsfb
genfb.c revision 1.88
      1  1.88  riastrad /*	$NetBSD: genfb.c,v 1.88 2022/07/17 13:10:04 riastradh Exp $ */
      2   1.1  macallan 
      3   1.1  macallan /*-
      4   1.1  macallan  * Copyright (c) 2007 Michael Lorenz
      5   1.1  macallan  * All rights reserved.
      6   1.1  macallan  *
      7   1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8   1.1  macallan  * modification, are permitted provided that the following conditions
      9   1.1  macallan  * are met:
     10   1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11   1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12   1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15   1.1  macallan  *
     16   1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  macallan  */
     28   1.1  macallan 
     29   1.1  macallan #include <sys/cdefs.h>
     30  1.88  riastrad __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.88 2022/07/17 13:10:04 riastradh Exp $");
     31   1.1  macallan 
     32   1.1  macallan #include <sys/param.h>
     33   1.1  macallan #include <sys/systm.h>
     34   1.1  macallan #include <sys/kernel.h>
     35   1.1  macallan #include <sys/device.h>
     36   1.1  macallan #include <sys/proc.h>
     37   1.1  macallan #include <sys/mutex.h>
     38   1.1  macallan #include <sys/ioctl.h>
     39   1.1  macallan #include <sys/kernel.h>
     40   1.1  macallan #include <sys/systm.h>
     41  1.23  jmcneill #include <sys/kmem.h>
     42  1.71    martin #include <sys/reboot.h>
     43   1.1  macallan 
     44  1.79  jmcneill #include <uvm/uvm_extern.h>
     45  1.79  jmcneill 
     46   1.1  macallan #include <dev/wscons/wsconsio.h>
     47   1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     48   1.1  macallan #include <dev/rasops/rasops.h>
     49   1.1  macallan #include <dev/wsfont/wsfont.h>
     50   1.1  macallan 
     51   1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     52   1.1  macallan 
     53   1.1  macallan #include <dev/wsfb/genfbvar.h>
     54   1.1  macallan 
     55  1.65  jmcneill #include <dev/videomode/videomode.h>
     56  1.65  jmcneill #include <dev/videomode/edidvar.h>
     57  1.65  jmcneill 
     58  1.29     ahoka #ifdef GENFB_DISABLE_TEXT
     59  1.29     ahoka #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \
     60  1.29     ahoka 		AB_VERBOSE | AB_DEBUG) )
     61  1.29     ahoka #endif
     62  1.29     ahoka 
     63  1.53  riastrad #ifdef _KERNEL_OPT
     64   1.1  macallan #include "opt_genfb.h"
     65   1.1  macallan #include "opt_wsfb.h"
     66  1.65  jmcneill #include "opt_rasops.h"
     67  1.53  riastrad #endif
     68   1.1  macallan 
     69  1.11  macallan #ifdef GENFB_DEBUG
     70  1.11  macallan #define GPRINTF panic
     71  1.11  macallan #else
     72  1.49  jmcneill #define GPRINTF aprint_debug
     73  1.11  macallan #endif
     74  1.11  macallan 
     75  1.42  macallan #define GENFB_BRIGHTNESS_STEP 15
     76  1.65  jmcneill #define	GENFB_CHAR_WIDTH_MM 3
     77  1.42  macallan 
     78   1.1  macallan static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     79   1.1  macallan static paddr_t	genfb_mmap(void *, void *, off_t, int);
     80  1.52     skrll static void	genfb_pollc(void *, int);
     81  1.52     skrll 
     82   1.1  macallan static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
     83  1.65  jmcneill static int	genfb_calc_hsize(struct genfb_softc *);
     84  1.85  jmcneill static int	genfb_calc_cols(struct genfb_softc *, struct rasops_info *);
     85   1.1  macallan 
     86   1.1  macallan static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     87   1.1  macallan static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     88   1.1  macallan static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
     89   1.1  macallan 			    uint8_t, uint8_t);
     90  1.45  macallan static void	genfb_init_palette(struct genfb_softc *);
     91   1.1  macallan 
     92  1.31  macallan static void	genfb_brightness_up(device_t);
     93  1.31  macallan static void	genfb_brightness_down(device_t);
     94  1.31  macallan 
     95  1.81  macallan #if GENFB_GLYPHCACHE > 0
     96  1.81  macallan static int	genfb_setup_glyphcache(struct genfb_softc *, long);
     97  1.81  macallan static void	genfb_putchar(void *, int, int, u_int, long);
     98  1.81  macallan #endif
     99  1.81  macallan 
    100   1.1  macallan extern const u_char rasops_cmap[768];
    101   1.1  macallan 
    102  1.18  jmcneill static int genfb_cnattach_called = 0;
    103  1.20  jmcneill static int genfb_enabled = 1;
    104  1.18  jmcneill 
    105  1.19  jmcneill static struct genfb_softc *genfb_softc = NULL;
    106  1.19  jmcneill 
    107   1.2  macallan void
    108   1.2  macallan genfb_init(struct genfb_softc *sc)
    109   1.2  macallan {
    110   1.2  macallan 	prop_dictionary_t dict;
    111  1.42  macallan 	uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
    112  1.72   msaitoh 	uint64_t fboffset;
    113  1.31  macallan 	bool console;
    114   1.2  macallan 
    115  1.32  macallan 	dict = device_properties(sc->sc_dev);
    116   1.2  macallan #ifdef GENFB_DEBUG
    117  1.54  riastrad 	printf("%s", prop_dictionary_externalize(dict));
    118   1.2  macallan #endif
    119  1.31  macallan 	prop_dictionary_get_bool(dict, "is_console", &console);
    120  1.31  macallan 
    121  1.11  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    122  1.12  macallan 		GPRINTF("no width property\n");
    123  1.11  macallan 		return;
    124  1.11  macallan 	}
    125  1.11  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    126  1.12  macallan 		GPRINTF("no height property\n");
    127  1.11  macallan 		return;
    128  1.11  macallan 	}
    129  1.11  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    130  1.12  macallan 		GPRINTF("no depth property\n");
    131  1.11  macallan 		return;
    132  1.11  macallan 	}
    133   1.2  macallan 
    134  1.72   msaitoh 	if (!prop_dictionary_get_uint64(dict, "address", &fboffset)) {
    135  1.12  macallan 		GPRINTF("no address property\n");
    136  1.11  macallan 		return;
    137  1.11  macallan 	}
    138  1.11  macallan 
    139  1.72   msaitoh 	sc->sc_fboffset = (bus_addr_t)fboffset;
    140   1.2  macallan 
    141  1.39  macallan 	sc->sc_fbaddr = NULL;
    142  1.39  macallan 	if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
    143  1.40  macallan 		sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
    144  1.39  macallan 	}
    145  1.39  macallan 
    146  1.59    nonaka 	sc->sc_shadowfb = NULL;
    147  1.59    nonaka 	if (!prop_dictionary_get_bool(dict, "enable_shadowfb",
    148  1.59    nonaka 	    &sc->sc_enable_shadowfb))
    149  1.59    nonaka #ifdef GENFB_SHADOWFB
    150  1.59    nonaka 		sc->sc_enable_shadowfb = true;
    151  1.59    nonaka #else
    152  1.59    nonaka 		sc->sc_enable_shadowfb = false;
    153  1.59    nonaka #endif
    154  1.59    nonaka 
    155   1.2  macallan 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
    156   1.2  macallan 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
    157  1.13  macallan 
    158  1.13  macallan 	/*
    159  1.13  macallan 	 * deal with a bug in the Raptor firmware which always sets
    160  1.13  macallan 	 * stride = width even when depth != 8
    161  1.13  macallan 	 */
    162  1.13  macallan 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
    163  1.13  macallan 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    164  1.13  macallan 
    165   1.6  macallan 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
    166   1.4  macallan 
    167   1.4  macallan 	/* optional colour map callback */
    168   1.4  macallan 	sc->sc_cmcb = NULL;
    169   1.4  macallan 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
    170   1.4  macallan 		if (cmap_cb != 0)
    171   1.4  macallan 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
    172   1.4  macallan 	}
    173  1.31  macallan 
    174  1.28  jmcneill 	/* optional pmf callback */
    175  1.28  jmcneill 	sc->sc_pmfcb = NULL;
    176  1.28  jmcneill 	if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
    177  1.28  jmcneill 		if (pmf_cb != 0)
    178  1.28  jmcneill 			sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
    179  1.28  jmcneill 	}
    180  1.31  macallan 
    181  1.34  jmcneill 	/* optional mode callback */
    182  1.34  jmcneill 	sc->sc_modecb = NULL;
    183  1.34  jmcneill 	if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
    184  1.34  jmcneill 		if (mode_cb != 0)
    185  1.34  jmcneill 			sc->sc_modecb = (void *)(vaddr_t)mode_cb;
    186  1.34  jmcneill 	}
    187  1.34  jmcneill 
    188  1.31  macallan 	/* optional backlight control callback */
    189  1.31  macallan 	sc->sc_backlight = NULL;
    190  1.31  macallan 	if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
    191  1.31  macallan 		if (bl_cb != 0) {
    192  1.31  macallan 			sc->sc_backlight = (void *)(vaddr_t)bl_cb;
    193  1.42  macallan 			aprint_naive_dev(sc->sc_dev,
    194  1.31  macallan 			    "enabling backlight control\n");
    195  1.42  macallan 		}
    196  1.42  macallan 	}
    197  1.42  macallan 
    198  1.42  macallan 	/* optional brightness control callback */
    199  1.42  macallan 	sc->sc_brightness = NULL;
    200  1.42  macallan 	if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
    201  1.42  macallan 		if (br_cb != 0) {
    202  1.42  macallan 			sc->sc_brightness = (void *)(vaddr_t)br_cb;
    203  1.42  macallan 			aprint_naive_dev(sc->sc_dev,
    204  1.42  macallan 			    "enabling brightness control\n");
    205  1.42  macallan 			if (console &&
    206  1.42  macallan 			    sc->sc_brightness->gpc_upd_parameter != NULL) {
    207  1.42  macallan 				pmf_event_register(sc->sc_dev,
    208  1.31  macallan 				    PMFE_DISPLAY_BRIGHTNESS_UP,
    209  1.31  macallan 				    genfb_brightness_up, TRUE);
    210  1.42  macallan 				pmf_event_register(sc->sc_dev,
    211  1.31  macallan 				    PMFE_DISPLAY_BRIGHTNESS_DOWN,
    212  1.31  macallan 				    genfb_brightness_down, TRUE);
    213  1.31  macallan 			}
    214  1.31  macallan 		}
    215  1.31  macallan 	}
    216   1.2  macallan }
    217   1.2  macallan 
    218   1.2  macallan int
    219   1.2  macallan genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
    220   1.1  macallan {
    221   1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    222   1.1  macallan 	prop_dictionary_t dict;
    223   1.1  macallan 	struct rasops_info *ri;
    224  1.79  jmcneill 	paddr_t fb_phys;
    225  1.21  jmcneill 	uint16_t crow;
    226   1.1  macallan 	long defattr;
    227   1.7  macallan 	bool console;
    228  1.33  jmcneill #ifdef SPLASHSCREEN
    229  1.45  macallan 	int i, j;
    230  1.33  jmcneill 	int error = ENXIO;
    231  1.33  jmcneill #endif
    232   1.9      jmmv 
    233  1.32  macallan 	dict = device_properties(sc->sc_dev);
    234  1.14       phx 	prop_dictionary_get_bool(dict, "is_console", &console);
    235  1.14       phx 
    236  1.21  jmcneill 	if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
    237  1.21  jmcneill 		crow = 0;
    238  1.21  jmcneill 	if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
    239  1.21  jmcneill 	    == false)
    240  1.21  jmcneill 		sc->sc_want_clear = true;
    241  1.21  jmcneill 
    242  1.79  jmcneill 	fb_phys = (paddr_t)sc->sc_fboffset;
    243  1.79  jmcneill 	if (fb_phys == 0) {
    244  1.79  jmcneill 		KASSERT(sc->sc_fbaddr != NULL);
    245  1.79  jmcneill 		(void)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_fbaddr,
    246  1.79  jmcneill 		    &fb_phys);
    247  1.79  jmcneill 	}
    248  1.79  jmcneill 
    249  1.42  macallan 	aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
    250  1.42  macallan 	    "stride %d\n",
    251  1.79  jmcneill 	    fb_phys ? (void *)(intptr_t)fb_phys : sc->sc_fbaddr,
    252   1.9      jmmv 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
    253   1.9      jmmv 
    254   1.1  macallan 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    255   1.1  macallan 		"default",
    256   1.1  macallan 		0, 0,
    257   1.1  macallan 		NULL,
    258   1.1  macallan 		8, 16,
    259  1.60  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    260  1.60  macallan 		  WSSCREEN_RESIZE,
    261   1.3  macallan 		NULL
    262   1.1  macallan 	};
    263   1.1  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    264   1.1  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    265   1.1  macallan 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
    266   1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    267  1.36       phx 	if (sc->sc_modecb != NULL)
    268  1.34  jmcneill 		sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
    269   1.1  macallan 
    270  1.41  macallan 	sc->sc_accessops.ioctl = genfb_ioctl;
    271  1.41  macallan 	sc->sc_accessops.mmap = genfb_mmap;
    272  1.52     skrll 	sc->sc_accessops.pollc = genfb_pollc;
    273  1.41  macallan 
    274  1.59    nonaka 	if (sc->sc_enable_shadowfb) {
    275  1.59    nonaka 		sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
    276  1.61       chs 		if (sc->sc_want_clear == false)
    277  1.59    nonaka 			memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
    278  1.68       rin 		aprint_verbose_dev(sc->sc_dev,
    279  1.68       rin 		    "shadow framebuffer enabled, size %zu KB\n",
    280  1.68       rin 		    sc->sc_fbsize >> 10);
    281  1.59    nonaka 	}
    282   1.5  macallan 
    283   1.1  macallan 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    284  1.41  macallan 	    &sc->sc_accessops);
    285   1.1  macallan 	sc->vd.init_screen = genfb_init_screen;
    286   1.1  macallan 
    287  1.10      jmmv 	/* Do not print anything between this point and the screen
    288  1.10      jmmv 	 * clear operation below.  Otherwise it will be lost. */
    289  1.10      jmmv 
    290   1.1  macallan 	ri = &sc->sc_console_screen.scr_ri;
    291   1.1  macallan 
    292  1.14       phx 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    293  1.14       phx 	    &defattr);
    294  1.14       phx 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    295  1.14       phx 
    296  1.81  macallan #if GENFB_GLYPHCACHE > 0
    297  1.81  macallan 	genfb_setup_glyphcache(sc, defattr);
    298  1.81  macallan #endif
    299  1.81  macallan 
    300  1.29     ahoka #ifdef SPLASHSCREEN
    301  1.29     ahoka /*
    302  1.29     ahoka  * If system isn't going to go multiuser, or user has requested to see
    303  1.29     ahoka  * boot text, don't render splash screen immediately
    304  1.29     ahoka  */
    305  1.29     ahoka 	if (DISABLESPLASH)
    306  1.29     ahoka #endif
    307  1.29     ahoka 		vcons_redraw_screen(&sc->sc_console_screen);
    308  1.29     ahoka 
    309  1.14       phx 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    310  1.14       phx 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    311  1.14       phx 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    312  1.14       phx 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    313  1.41  macallan 
    314  1.44  macallan 	if (crow >= ri->ri_rows) {
    315  1.44  macallan 		crow = 0;
    316  1.44  macallan 		sc->sc_want_clear = 1;
    317  1.44  macallan 	}
    318  1.44  macallan 
    319  1.41  macallan 	if (console)
    320  1.41  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
    321  1.41  macallan 		    defattr);
    322   1.1  macallan 
    323  1.10      jmmv 	/* Clear the whole screen to bring it to a known state. */
    324  1.21  jmcneill 	if (sc->sc_want_clear)
    325  1.21  jmcneill 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
    326  1.10      jmmv 
    327  1.45  macallan #ifdef SPLASHSCREEN
    328   1.1  macallan 	j = 0;
    329  1.64  riastrad 	for (i = 0; i < uimin(1 << sc->sc_depth, 256); i++) {
    330  1.33  jmcneill 		if (i >= SPLASH_CMAP_OFFSET &&
    331  1.29     ahoka 		    i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
    332  1.33  jmcneill 			splash_get_cmap(i,
    333  1.33  jmcneill 			    &sc->sc_cmap_red[i],
    334  1.33  jmcneill 			    &sc->sc_cmap_green[i],
    335  1.33  jmcneill 			    &sc->sc_cmap_blue[i]);
    336  1.29     ahoka 		} else {
    337  1.29     ahoka 			sc->sc_cmap_red[i] = rasops_cmap[j];
    338  1.29     ahoka 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    339  1.29     ahoka 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    340  1.29     ahoka 		}
    341  1.29     ahoka 		j += 3;
    342   1.1  macallan 	}
    343  1.33  jmcneill 	genfb_restore_palette(sc);
    344   1.1  macallan 
    345  1.29     ahoka 	sc->sc_splash.si_depth = sc->sc_depth;
    346  1.58       nat 	sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_origbits;
    347  1.29     ahoka 	sc->sc_splash.si_hwbits = sc->sc_fbaddr;
    348  1.29     ahoka 	sc->sc_splash.si_width = sc->sc_width;
    349  1.29     ahoka 	sc->sc_splash.si_height = sc->sc_height;
    350  1.29     ahoka 	sc->sc_splash.si_stride = sc->sc_stride;
    351  1.29     ahoka 	sc->sc_splash.si_fillrect = NULL;
    352  1.33  jmcneill 	if (!DISABLESPLASH) {
    353  1.33  jmcneill 		error = splash_render(&sc->sc_splash,
    354  1.33  jmcneill 		    SPLASH_F_CENTER|SPLASH_F_FILL);
    355  1.33  jmcneill 		if (error) {
    356  1.33  jmcneill 			SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    357  1.45  macallan 			genfb_init_palette(sc);
    358  1.33  jmcneill 			vcons_replay_msgbuf(&sc->sc_console_screen);
    359  1.33  jmcneill 		}
    360  1.33  jmcneill 	}
    361  1.29     ahoka #else
    362  1.45  macallan 	genfb_init_palette(sc);
    363  1.71    martin 	if (console && (boothowto & (AB_SILENT|AB_QUIET)) == 0)
    364  1.53  riastrad 		vcons_replay_msgbuf(&sc->sc_console_screen);
    365  1.29     ahoka #endif
    366  1.27  macallan 
    367  1.19  jmcneill 	if (genfb_softc == NULL)
    368  1.19  jmcneill 		genfb_softc = sc;
    369  1.19  jmcneill 
    370   1.1  macallan 	aa.console = console;
    371   1.1  macallan 	aa.scrdata = &sc->sc_screenlist;
    372  1.41  macallan 	aa.accessops = &sc->sc_accessops;
    373   1.1  macallan 	aa.accesscookie = &sc->vd;
    374   1.1  macallan 
    375  1.29     ahoka #ifdef GENFB_DISABLE_TEXT
    376  1.33  jmcneill 	if (!DISABLESPLASH && error == 0)
    377  1.29     ahoka 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    378  1.29     ahoka #endif
    379  1.29     ahoka 
    380  1.82   thorpej 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint,
    381  1.83   thorpej 	    CFARGS(.iattr = "wsemuldisplaydev"));
    382   1.1  macallan 
    383   1.1  macallan 	return 0;
    384   1.1  macallan }
    385   1.1  macallan 
    386   1.1  macallan static int
    387   1.1  macallan genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    388   1.1  macallan 	struct lwp *l)
    389   1.1  macallan {
    390   1.1  macallan 	struct vcons_data *vd = v;
    391   1.1  macallan 	struct genfb_softc *sc = vd->cookie;
    392   1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    393   1.1  macallan 	struct vcons_screen *ms = vd->active;
    394  1.31  macallan 	struct wsdisplay_param *param;
    395  1.56  macallan 	int new_mode, error, val, ret;
    396   1.1  macallan 
    397   1.1  macallan 	switch (cmd) {
    398   1.1  macallan 		case WSDISPLAYIO_GINFO:
    399   1.6  macallan 			if (ms == NULL)
    400   1.6  macallan 				return ENODEV;
    401   1.1  macallan 			wdf = (void *)data;
    402   1.1  macallan 			wdf->height = ms->scr_ri.ri_height;
    403   1.1  macallan 			wdf->width = ms->scr_ri.ri_width;
    404   1.1  macallan 			wdf->depth = ms->scr_ri.ri_depth;
    405   1.1  macallan 			wdf->cmsize = 256;
    406   1.1  macallan 			return 0;
    407   1.1  macallan 
    408   1.1  macallan 		case WSDISPLAYIO_GETCMAP:
    409   1.1  macallan 			return genfb_getcmap(sc,
    410   1.1  macallan 			    (struct wsdisplay_cmap *)data);
    411   1.1  macallan 
    412   1.1  macallan 		case WSDISPLAYIO_PUTCMAP:
    413   1.1  macallan 			return genfb_putcmap(sc,
    414   1.1  macallan 			    (struct wsdisplay_cmap *)data);
    415   1.1  macallan 
    416   1.3  macallan 		case WSDISPLAYIO_LINEBYTES:
    417   1.3  macallan 			*(u_int *)data = sc->sc_stride;
    418   1.3  macallan 			return 0;
    419   1.3  macallan 
    420   1.1  macallan 		case WSDISPLAYIO_SMODE:
    421  1.24  jmcneill 			new_mode = *(int *)data;
    422  1.11  macallan 
    423  1.24  jmcneill 			/* notify the bus backend */
    424  1.24  jmcneill 			error = 0;
    425  1.24  jmcneill 			if (sc->sc_ops.genfb_ioctl)
    426  1.24  jmcneill 				error = sc->sc_ops.genfb_ioctl(sc, vs,
    427  1.11  macallan 					    cmd, data, flag, l);
    428  1.50  jmcneill 			if (error && error != EPASSTHROUGH)
    429  1.24  jmcneill 				return error;
    430  1.11  macallan 
    431  1.24  jmcneill 			if (new_mode != sc->sc_mode) {
    432  1.24  jmcneill 				sc->sc_mode = new_mode;
    433  1.36       phx 				if (sc->sc_modecb != NULL)
    434  1.34  jmcneill 					sc->sc_modecb->gmc_setmode(sc,
    435  1.34  jmcneill 					    sc->sc_mode);
    436  1.24  jmcneill 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    437  1.24  jmcneill 					genfb_restore_palette(sc);
    438  1.24  jmcneill 					vcons_redraw_screen(ms);
    439   1.1  macallan 				}
    440   1.1  macallan 			}
    441   1.1  macallan 			return 0;
    442  1.51  macallan 
    443  1.29     ahoka 		case WSDISPLAYIO_SSPLASH:
    444  1.29     ahoka #if defined(SPLASHSCREEN)
    445  1.29     ahoka 			if(*(int *)data == 1) {
    446  1.29     ahoka 				SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    447  1.29     ahoka 				splash_render(&sc->sc_splash,
    448  1.29     ahoka 						SPLASH_F_CENTER|SPLASH_F_FILL);
    449  1.29     ahoka 			} else {
    450  1.29     ahoka 				SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    451  1.45  macallan 				genfb_init_palette(sc);
    452  1.29     ahoka 			}
    453  1.29     ahoka 			vcons_redraw_screen(ms);
    454  1.29     ahoka 			return 0;
    455  1.29     ahoka #else
    456  1.29     ahoka 			return ENODEV;
    457  1.29     ahoka #endif
    458  1.31  macallan 		case WSDISPLAYIO_GETPARAM:
    459  1.31  macallan 			param = (struct wsdisplay_param *)data;
    460  1.31  macallan 			switch (param->param) {
    461  1.31  macallan 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    462  1.42  macallan 				if (sc->sc_brightness == NULL)
    463  1.42  macallan 					return EPASSTHROUGH;
    464  1.31  macallan 				param->min = 0;
    465  1.31  macallan 				param->max = 255;
    466  1.42  macallan 				return sc->sc_brightness->gpc_get_parameter(
    467  1.42  macallan 				    sc->sc_brightness->gpc_cookie,
    468  1.42  macallan 				    &param->curval);
    469  1.31  macallan 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    470  1.42  macallan 				if (sc->sc_backlight == NULL)
    471  1.42  macallan 					return EPASSTHROUGH;
    472  1.31  macallan 				param->min = 0;
    473  1.31  macallan 				param->max = 1;
    474  1.42  macallan 				return sc->sc_backlight->gpc_get_parameter(
    475  1.42  macallan 				    sc->sc_backlight->gpc_cookie,
    476  1.42  macallan 				    &param->curval);
    477  1.31  macallan 			}
    478  1.31  macallan 			return EPASSTHROUGH;
    479  1.31  macallan 
    480  1.31  macallan 		case WSDISPLAYIO_SETPARAM:
    481  1.31  macallan 			param = (struct wsdisplay_param *)data;
    482  1.31  macallan 			switch (param->param) {
    483  1.31  macallan 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    484  1.42  macallan 				if (sc->sc_brightness == NULL)
    485  1.42  macallan 					return EPASSTHROUGH;
    486  1.42  macallan 				val = param->curval;
    487  1.42  macallan 				if (val < 0) val = 0;
    488  1.42  macallan 				if (val > 255) val = 255;
    489  1.42  macallan 				return sc->sc_brightness->gpc_set_parameter(
    490  1.42  macallan 				    sc->sc_brightness->gpc_cookie, val);
    491  1.31  macallan 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    492  1.42  macallan 				if (sc->sc_backlight == NULL)
    493  1.42  macallan 					return EPASSTHROUGH;
    494  1.42  macallan 				val = param->curval;
    495  1.42  macallan 				if (val < 0) val = 0;
    496  1.42  macallan 				if (val > 1) val = 1;
    497  1.42  macallan 				return sc->sc_backlight->gpc_set_parameter(
    498  1.42  macallan 				    sc->sc_backlight->gpc_cookie, val);
    499  1.31  macallan 			}
    500  1.56  macallan 			return EPASSTHROUGH;
    501  1.56  macallan 	}
    502  1.56  macallan 	ret = EPASSTHROUGH;
    503  1.56  macallan 	if (sc->sc_ops.genfb_ioctl)
    504  1.56  macallan 		ret = sc->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l);
    505  1.56  macallan 	if (ret != EPASSTHROUGH)
    506  1.56  macallan 		return ret;
    507  1.56  macallan 	/*
    508  1.56  macallan 	 * XXX
    509  1.56  macallan 	 * handle these only if there either is no ioctl() handler or it didn't
    510  1.56  macallan 	 * know how to deal with them. This allows bus frontends to override
    511  1.56  macallan 	 * them but still provides fallback implementations
    512  1.56  macallan 	 */
    513  1.56  macallan 	switch (cmd) {
    514  1.42  macallan 		case WSDISPLAYIO_GET_EDID: {
    515  1.42  macallan 			struct wsdisplayio_edid_info *d = data;
    516  1.42  macallan 			return wsdisplayio_get_edid(sc->sc_dev, d);
    517  1.42  macallan 		}
    518  1.51  macallan 
    519  1.51  macallan 		case WSDISPLAYIO_GET_FBINFO: {
    520  1.51  macallan 			struct wsdisplayio_fbinfo *fbi = data;
    521  1.77       rin 			return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    522  1.51  macallan 		}
    523   1.1  macallan 	}
    524   1.1  macallan 	return EPASSTHROUGH;
    525   1.1  macallan }
    526   1.1  macallan 
    527   1.1  macallan static paddr_t
    528   1.1  macallan genfb_mmap(void *v, void *vs, off_t offset, int prot)
    529   1.1  macallan {
    530   1.1  macallan 	struct vcons_data *vd = v;
    531   1.1  macallan 	struct genfb_softc *sc = vd->cookie;
    532   1.1  macallan 
    533   1.1  macallan 	if (sc->sc_ops.genfb_mmap)
    534   1.1  macallan 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
    535   1.1  macallan 
    536   1.1  macallan 	return -1;
    537   1.1  macallan }
    538   1.1  macallan 
    539   1.1  macallan static void
    540  1.52     skrll genfb_pollc(void *v, int on)
    541  1.52     skrll {
    542  1.52     skrll 	struct vcons_data *vd = v;
    543  1.52     skrll 	struct genfb_softc *sc = vd->cookie;
    544  1.52     skrll 
    545  1.52     skrll 	if (sc == NULL)
    546  1.52     skrll 		return;
    547  1.52     skrll 
    548  1.52     skrll 	if (on)
    549  1.52     skrll 		genfb_enable_polling(sc->sc_dev);
    550  1.52     skrll 	else
    551  1.52     skrll 		genfb_disable_polling(sc->sc_dev);
    552  1.52     skrll }
    553  1.52     skrll 
    554  1.52     skrll static void
    555   1.1  macallan genfb_init_screen(void *cookie, struct vcons_screen *scr,
    556   1.1  macallan     int existing, long *defattr)
    557   1.1  macallan {
    558   1.1  macallan 	struct genfb_softc *sc = cookie;
    559   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    560  1.65  jmcneill 	int wantcols;
    561  1.84  jmcneill 	bool is_bgr, is_swapped, is_10bit;
    562   1.1  macallan 
    563   1.1  macallan 	ri->ri_depth = sc->sc_depth;
    564   1.1  macallan 	ri->ri_width = sc->sc_width;
    565   1.1  macallan 	ri->ri_height = sc->sc_height;
    566   1.1  macallan 	ri->ri_stride = sc->sc_stride;
    567  1.21  jmcneill 	ri->ri_flg = RI_CENTER;
    568  1.21  jmcneill 	if (sc->sc_want_clear)
    569  1.21  jmcneill 		ri->ri_flg |= RI_FULLCLEAR;
    570   1.1  macallan 
    571  1.60  macallan 	scr->scr_flags |= VCONS_LOADFONT;
    572  1.60  macallan 
    573   1.5  macallan 	if (sc->sc_shadowfb != NULL) {
    574   1.5  macallan 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
    575   1.5  macallan 		ri->ri_bits = (char *)sc->sc_shadowfb;
    576  1.59    nonaka 	} else {
    577   1.5  macallan 		ri->ri_bits = (char *)sc->sc_fbaddr;
    578  1.30  macallan 		scr->scr_flags |= VCONS_DONT_READ;
    579  1.30  macallan 	}
    580   1.1  macallan 
    581  1.67       rin 	if (existing && sc->sc_want_clear)
    582   1.1  macallan 		ri->ri_flg |= RI_CLEAR;
    583   1.1  macallan 
    584  1.69       rin 	switch (ri->ri_depth) {
    585  1.69       rin 	case 32:
    586  1.69       rin 	case 24:
    587  1.67       rin 		ri->ri_flg |= RI_ENABLE_ALPHA;
    588  1.67       rin 
    589  1.70       rin 		is_bgr = false;
    590  1.48  macallan 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    591  1.48  macallan 		    "is_bgr", &is_bgr);
    592  1.78       rin 
    593  1.78       rin 		is_swapped = false;
    594  1.78       rin 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    595  1.78       rin 		    "is_swapped", &is_swapped);
    596  1.78       rin 
    597  1.84  jmcneill 		is_10bit = false;
    598  1.84  jmcneill 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    599  1.84  jmcneill 		    "is_10bit", &is_10bit);
    600  1.84  jmcneill 
    601  1.84  jmcneill 		const int bits = is_10bit ? 10 : 8;
    602  1.84  jmcneill 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = bits;
    603  1.84  jmcneill 
    604  1.48  macallan 		if (is_bgr) {
    605  1.48  macallan 			/* someone requested BGR */
    606  1.84  jmcneill 			ri->ri_rpos = bits * 0;
    607  1.84  jmcneill 			ri->ri_gpos = bits * 1;
    608  1.84  jmcneill 			ri->ri_bpos = bits * 2;
    609  1.78       rin 		} else if (is_swapped) {
    610  1.78       rin 			/* byte-swapped, must be 32 bpp */
    611  1.88  riastrad 			KASSERT(ri->ri_depth == 32);
    612  1.88  riastrad 			KASSERT(bits == 8);
    613  1.78       rin 			ri->ri_rpos = 8;
    614  1.78       rin 			ri->ri_gpos = 16;
    615  1.78       rin 			ri->ri_bpos = 24;
    616  1.48  macallan 		} else {
    617  1.48  macallan 			/* assume RGB */
    618  1.84  jmcneill 			ri->ri_rpos = bits * 2;
    619  1.84  jmcneill 			ri->ri_gpos = bits * 1;
    620  1.84  jmcneill 			ri->ri_bpos = bits * 0;
    621  1.48  macallan 		}
    622  1.69       rin 		break;
    623  1.69       rin 
    624  1.69       rin 	case 16:
    625  1.69       rin 	case 15:
    626  1.69       rin 		ri->ri_flg |= RI_ENABLE_ALPHA;
    627  1.69       rin 		break;
    628  1.69       rin 
    629  1.69       rin 	case 8:
    630  1.69       rin 		if (sc->sc_cmcb != NULL)
    631  1.69       rin 			ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
    632  1.69       rin 		break;
    633  1.43  macallan 
    634  1.69       rin 	case 2:
    635  1.66       rin 		ri->ri_flg |= RI_ENABLE_ALPHA;
    636  1.69       rin 		break;
    637  1.66       rin 
    638  1.69       rin 	default:
    639  1.69       rin 		break;
    640  1.69       rin 	}
    641  1.45  macallan 
    642  1.85  jmcneill 	wantcols = genfb_calc_cols(sc, ri);
    643  1.45  macallan 
    644  1.65  jmcneill 	rasops_init(ri, 0, wantcols);
    645  1.60  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
    646  1.60  macallan 		  WSSCREEN_RESIZE;
    647   1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    648   1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    649   1.1  macallan 
    650   1.1  macallan 	ri->ri_hw = scr;
    651  1.81  macallan #if GENFB_GLYPHCACHE > 0
    652  1.81  macallan 	sc->sc_putchar = ri->ri_ops.putchar;
    653  1.81  macallan 	ri->ri_ops.putchar = genfb_putchar;
    654  1.81  macallan #endif
    655  1.29     ahoka #ifdef GENFB_DISABLE_TEXT
    656  1.29     ahoka 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
    657  1.29     ahoka 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    658  1.29     ahoka #endif
    659   1.1  macallan }
    660   1.1  macallan 
    661  1.65  jmcneill /* Returns the width of the display in millimeters, or 0 if not known. */
    662  1.65  jmcneill static int
    663  1.65  jmcneill genfb_calc_hsize(struct genfb_softc *sc)
    664  1.65  jmcneill {
    665  1.65  jmcneill 	device_t dev = sc->sc_dev;
    666  1.65  jmcneill 	prop_dictionary_t dict = device_properties(dev);
    667  1.65  jmcneill 	prop_data_t edid_data;
    668  1.73  jdolecek 	struct edid_info *edid;
    669  1.65  jmcneill 	const char *edid_ptr;
    670  1.73  jdolecek 	int hsize;
    671  1.65  jmcneill 
    672  1.65  jmcneill 	edid_data = prop_dictionary_get(dict, "EDID");
    673  1.65  jmcneill 	if (edid_data == NULL || prop_data_size(edid_data) < 128)
    674  1.65  jmcneill 		return 0;
    675  1.65  jmcneill 
    676  1.73  jdolecek 	edid = kmem_alloc(sizeof(*edid), KM_SLEEP);
    677  1.73  jdolecek 
    678  1.74   thorpej 	edid_ptr = prop_data_value(edid_data);
    679  1.73  jdolecek 	if (edid_parse(__UNCONST(edid_ptr), edid) == 0)
    680  1.73  jdolecek 		hsize = (int)edid->edid_max_hsize * 10;
    681  1.73  jdolecek 	else
    682  1.73  jdolecek 		hsize = 0;
    683  1.73  jdolecek 
    684  1.73  jdolecek 	kmem_free(edid, sizeof(*edid));
    685  1.65  jmcneill 
    686  1.73  jdolecek 	return hsize;
    687  1.65  jmcneill }
    688  1.65  jmcneill 
    689  1.65  jmcneill /* Return the minimum number of character columns based on DPI */
    690  1.65  jmcneill static int
    691  1.85  jmcneill genfb_calc_cols(struct genfb_softc *sc, struct rasops_info *ri)
    692  1.65  jmcneill {
    693  1.65  jmcneill 	const int hsize = genfb_calc_hsize(sc);
    694  1.65  jmcneill 
    695  1.85  jmcneill 	if (hsize != 0) {
    696  1.85  jmcneill 		ri->ri_flg |= RI_PREFER_WIDEFONT;
    697  1.85  jmcneill 	}
    698  1.85  jmcneill 
    699  1.65  jmcneill 	return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM);
    700  1.65  jmcneill }
    701  1.65  jmcneill 
    702   1.1  macallan static int
    703   1.1  macallan genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    704   1.1  macallan {
    705   1.1  macallan 	u_char *r, *g, *b;
    706   1.1  macallan 	u_int index = cm->index;
    707   1.1  macallan 	u_int count = cm->count;
    708   1.1  macallan 	int i, error;
    709   1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    710   1.1  macallan 
    711   1.1  macallan #ifdef GENFB_DEBUG
    712   1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    713   1.1  macallan #endif
    714  1.63   mlelstv 	if (index >= 256 || count > 256 || index + count > 256)
    715   1.1  macallan 		return EINVAL;
    716  1.63   mlelstv 
    717   1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    718   1.1  macallan 	if (error)
    719   1.1  macallan 		return error;
    720   1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    721   1.1  macallan 	if (error)
    722   1.1  macallan 		return error;
    723   1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    724   1.1  macallan 	if (error)
    725   1.1  macallan 		return error;
    726   1.1  macallan 
    727   1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    728   1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    729   1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    730   1.1  macallan 
    731   1.1  macallan 	r = &sc->sc_cmap_red[index];
    732   1.1  macallan 	g = &sc->sc_cmap_green[index];
    733   1.1  macallan 	b = &sc->sc_cmap_blue[index];
    734   1.1  macallan 
    735   1.1  macallan 	for (i = 0; i < count; i++) {
    736   1.1  macallan 		genfb_putpalreg(sc, index, *r, *g, *b);
    737   1.1  macallan 		index++;
    738   1.1  macallan 		r++, g++, b++;
    739   1.1  macallan 	}
    740   1.1  macallan 	return 0;
    741   1.1  macallan }
    742   1.1  macallan 
    743   1.1  macallan static int
    744   1.1  macallan genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    745   1.1  macallan {
    746   1.1  macallan 	u_int index = cm->index;
    747   1.1  macallan 	u_int count = cm->count;
    748   1.1  macallan 	int error;
    749   1.1  macallan 
    750  1.63   mlelstv 	if (index >= 256 || count > 256 || index + count > 256)
    751   1.1  macallan 		return EINVAL;
    752   1.1  macallan 
    753   1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    754   1.1  macallan 	if (error)
    755   1.1  macallan 		return error;
    756   1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    757   1.1  macallan 	if (error)
    758   1.1  macallan 		return error;
    759   1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    760   1.1  macallan 	if (error)
    761   1.1  macallan 		return error;
    762   1.1  macallan 
    763   1.1  macallan 	return 0;
    764   1.1  macallan }
    765   1.1  macallan 
    766  1.28  jmcneill void
    767   1.1  macallan genfb_restore_palette(struct genfb_softc *sc)
    768   1.1  macallan {
    769   1.1  macallan 	int i;
    770   1.1  macallan 
    771  1.27  macallan 	if (sc->sc_depth <= 8) {
    772  1.27  macallan 		for (i = 0; i < (1 << sc->sc_depth); i++) {
    773  1.27  macallan 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    774  1.27  macallan 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    775  1.27  macallan 		}
    776   1.1  macallan 	}
    777   1.1  macallan }
    778   1.1  macallan 
    779  1.45  macallan static void
    780  1.45  macallan genfb_init_palette(struct genfb_softc *sc)
    781  1.45  macallan {
    782  1.45  macallan 	int i, j, tmp;
    783  1.45  macallan 
    784  1.45  macallan 	if (sc->sc_depth == 8) {
    785  1.45  macallan 		/* generate an r3g3b2 colour map */
    786  1.45  macallan 		for (i = 0; i < 256; i++) {
    787  1.45  macallan 			tmp = i & 0xe0;
    788  1.45  macallan 			/*
    789  1.45  macallan 			 * replicate bits so 0xe0 maps to a red value of 0xff
    790  1.45  macallan 			 * in order to make white look actually white
    791  1.45  macallan 			 */
    792  1.45  macallan 			tmp |= (tmp >> 3) | (tmp >> 6);
    793  1.45  macallan 			sc->sc_cmap_red[i] = tmp;
    794  1.45  macallan 
    795  1.45  macallan 			tmp = (i & 0x1c) << 3;
    796  1.45  macallan 			tmp |= (tmp >> 3) | (tmp >> 6);
    797  1.45  macallan 			sc->sc_cmap_green[i] = tmp;
    798  1.45  macallan 
    799  1.45  macallan 			tmp = (i & 0x03) << 6;
    800  1.45  macallan 			tmp |= tmp >> 2;
    801  1.45  macallan 			tmp |= tmp >> 4;
    802  1.45  macallan 			sc->sc_cmap_blue[i] = tmp;
    803  1.45  macallan 
    804  1.45  macallan 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    805  1.45  macallan 				       sc->sc_cmap_green[i],
    806  1.45  macallan 				       sc->sc_cmap_blue[i]);
    807  1.45  macallan 		}
    808  1.45  macallan 	} else {
    809  1.45  macallan 		/* steal rasops' ANSI cmap */
    810  1.45  macallan 		j = 0;
    811  1.45  macallan 		for (i = 0; i < 256; i++) {
    812  1.45  macallan 			sc->sc_cmap_red[i] = rasops_cmap[j];
    813  1.45  macallan 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    814  1.45  macallan 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    815  1.45  macallan 			j += 3;
    816  1.45  macallan 		}
    817  1.45  macallan 	}
    818  1.45  macallan }
    819  1.45  macallan 
    820   1.1  macallan static int
    821   1.1  macallan genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    822   1.1  macallan     uint8_t b)
    823   1.1  macallan {
    824   1.1  macallan 
    825   1.4  macallan 	if (sc->sc_cmcb) {
    826   1.4  macallan 
    827   1.4  macallan 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
    828   1.4  macallan 		    idx, r, g, b);
    829   1.4  macallan 	}
    830   1.1  macallan 	return 0;
    831   1.1  macallan }
    832   1.1  macallan 
    833  1.18  jmcneill void
    834  1.18  jmcneill genfb_cnattach(void)
    835  1.18  jmcneill {
    836  1.18  jmcneill 	genfb_cnattach_called = 1;
    837  1.18  jmcneill }
    838  1.18  jmcneill 
    839  1.86   mlelstv int
    840  1.86   mlelstv genfb_cndetach(void)
    841  1.86   mlelstv {
    842  1.86   mlelstv 
    843  1.86   mlelstv 	if (genfb_cnattach_called) {
    844  1.86   mlelstv 		genfb_cnattach_called = 0;
    845  1.86   mlelstv 		return 1;
    846  1.86   mlelstv 	}
    847  1.86   mlelstv 	return 0;
    848  1.86   mlelstv }
    849  1.86   mlelstv 
    850  1.20  jmcneill void
    851  1.20  jmcneill genfb_disable(void)
    852  1.20  jmcneill {
    853  1.20  jmcneill 	genfb_enabled = 0;
    854  1.20  jmcneill }
    855  1.20  jmcneill 
    856  1.18  jmcneill int
    857  1.18  jmcneill genfb_is_console(void)
    858  1.18  jmcneill {
    859  1.18  jmcneill 	return genfb_cnattach_called;
    860  1.18  jmcneill }
    861  1.19  jmcneill 
    862  1.19  jmcneill int
    863  1.20  jmcneill genfb_is_enabled(void)
    864  1.20  jmcneill {
    865  1.20  jmcneill 	return genfb_enabled;
    866  1.20  jmcneill }
    867  1.20  jmcneill 
    868  1.20  jmcneill int
    869  1.19  jmcneill genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
    870  1.19  jmcneill {
    871  1.19  jmcneill 	struct genfb_softc *sc = genfb_softc;
    872  1.19  jmcneill 
    873  1.19  jmcneill 	if (sc && sc->sc_ops.genfb_borrow)
    874  1.19  jmcneill 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
    875  1.19  jmcneill 	return 0;
    876  1.19  jmcneill }
    877  1.31  macallan 
    878  1.31  macallan static void
    879  1.31  macallan genfb_brightness_up(device_t dev)
    880  1.31  macallan {
    881  1.31  macallan 	struct genfb_softc *sc = device_private(dev);
    882  1.31  macallan 
    883  1.88  riastrad 	KASSERT(sc->sc_brightness != NULL);
    884  1.88  riastrad 	KASSERT(sc->sc_brightness->gpc_upd_parameter != NULL);
    885  1.42  macallan 
    886  1.42  macallan 	(void)sc->sc_brightness->gpc_upd_parameter(
    887  1.42  macallan 	    sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
    888  1.31  macallan }
    889  1.31  macallan 
    890  1.31  macallan static void
    891  1.31  macallan genfb_brightness_down(device_t dev)
    892  1.31  macallan {
    893  1.31  macallan 	struct genfb_softc *sc = device_private(dev);
    894  1.31  macallan 
    895  1.88  riastrad 	KASSERT(sc->sc_brightness != NULL);
    896  1.88  riastrad 	KASSERT(sc->sc_brightness->gpc_upd_parameter != NULL);
    897  1.42  macallan 
    898  1.42  macallan 	(void)sc->sc_brightness->gpc_upd_parameter(
    899  1.42  macallan 	    sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
    900  1.31  macallan }
    901  1.35  jmcneill 
    902  1.35  jmcneill void
    903  1.35  jmcneill genfb_enable_polling(device_t dev)
    904  1.35  jmcneill {
    905  1.35  jmcneill 	struct genfb_softc *sc = device_private(dev);
    906  1.35  jmcneill 
    907  1.38  jmcneill 	if (sc->sc_console_screen.scr_vd) {
    908  1.38  jmcneill 		SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    909  1.38  jmcneill 		vcons_hard_switch(&sc->sc_console_screen);
    910  1.38  jmcneill 		vcons_enable_polling(&sc->vd);
    911  1.55  riastrad 		if (sc->sc_ops.genfb_enable_polling)
    912  1.55  riastrad 			(*sc->sc_ops.genfb_enable_polling)(sc);
    913  1.38  jmcneill 	}
    914  1.35  jmcneill }
    915  1.35  jmcneill 
    916  1.35  jmcneill void
    917  1.35  jmcneill genfb_disable_polling(device_t dev)
    918  1.35  jmcneill {
    919  1.35  jmcneill 	struct genfb_softc *sc = device_private(dev);
    920  1.35  jmcneill 
    921  1.38  jmcneill 	if (sc->sc_console_screen.scr_vd) {
    922  1.55  riastrad 		if (sc->sc_ops.genfb_disable_polling)
    923  1.55  riastrad 			(*sc->sc_ops.genfb_disable_polling)(sc);
    924  1.38  jmcneill 		vcons_disable_polling(&sc->vd);
    925  1.38  jmcneill 	}
    926  1.35  jmcneill }
    927  1.81  macallan 
    928  1.81  macallan #if GENFB_GLYPHCACHE > 0
    929  1.81  macallan #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024)
    930  1.81  macallan 
    931  1.81  macallan static inline int
    932  1.81  macallan attr2idx(long attr)
    933  1.81  macallan {
    934  1.81  macallan 	if ((attr & 0xf0f00ff8) != 0)
    935  1.81  macallan 		return -1;
    936  1.81  macallan 
    937  1.81  macallan 	return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
    938  1.81  macallan }
    939  1.81  macallan 
    940  1.81  macallan static int
    941  1.81  macallan genfb_setup_glyphcache(struct genfb_softc *sc, long defattr)
    942  1.81  macallan {
    943  1.81  macallan 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri,
    944  1.81  macallan 			  *cri = &sc->sc_cache_ri;
    945  1.81  macallan 	gc_bucket *b;
    946  1.81  macallan 	int i, usedcells = 0, idx, j;
    947  1.81  macallan 
    948  1.81  macallan 	sc->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP);
    949  1.81  macallan 
    950  1.81  macallan 	/*
    951  1.81  macallan 	 * now we build a mutant rasops_info for the cache - same pixel type
    952  1.81  macallan 	 * and such as the real fb, but only one character per line for
    953  1.81  macallan 	 * simplicity and locality
    954  1.81  macallan 	 */
    955  1.81  macallan 	memcpy(cri, ri, sizeof(struct rasops_info));
    956  1.81  macallan 	cri->ri_ops.putchar = sc->sc_putchar;
    957  1.81  macallan 	cri->ri_width = ri->ri_font->fontwidth;
    958  1.81  macallan 	cri->ri_stride = ri->ri_xscale;
    959  1.81  macallan 	cri->ri_bits = sc->sc_cache;
    960  1.81  macallan 	cri->ri_hwbits = NULL;
    961  1.81  macallan 	cri->ri_origbits = sc->sc_cache;
    962  1.81  macallan 	cri->ri_cols = 1;
    963  1.81  macallan 	cri->ri_rows = GLYPHCACHESIZE /
    964  1.81  macallan 	    (cri->ri_stride * cri->ri_font->fontheight);
    965  1.81  macallan 	cri->ri_xorigin = 0;
    966  1.81  macallan 	cri->ri_yorigin = 0;
    967  1.81  macallan 	cri->ri_xscale = ri->ri_xscale;
    968  1.81  macallan 	cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale;
    969  1.81  macallan 
    970  1.81  macallan 	printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride);
    971  1.81  macallan 	printf("cells: %d\n", cri->ri_rows);
    972  1.81  macallan 	sc->sc_nbuckets = uimin(256, cri->ri_rows / 223);
    973  1.81  macallan 	sc->sc_buckets = kmem_alloc(sizeof(gc_bucket) * sc->sc_nbuckets, KM_SLEEP);
    974  1.81  macallan 	printf("buckets: %d\n", sc->sc_nbuckets);
    975  1.81  macallan 	for (i = 0; i < sc->sc_nbuckets; i++) {
    976  1.81  macallan 		b = &sc->sc_buckets[i];
    977  1.81  macallan 		b->gb_firstcell = usedcells;
    978  1.81  macallan 		b->gb_numcells = uimin(223, cri->ri_rows - usedcells);
    979  1.81  macallan 		usedcells += 223;
    980  1.81  macallan 		b->gb_usedcells = 0;
    981  1.81  macallan 		b->gb_index = -1;
    982  1.81  macallan 		for (j = 0; j < 223; j++) b->gb_map[j] = -1;
    983  1.81  macallan 	}
    984  1.81  macallan 
    985  1.81  macallan 	/* initialize the attribute map... */
    986  1.81  macallan 	for (i = 0; i < 256; i++) {
    987  1.81  macallan 		sc->sc_attrmap[i] = -1;
    988  1.81  macallan 	}
    989  1.81  macallan 
    990  1.81  macallan 	/* first bucket goes to default attr */
    991  1.81  macallan 	idx = attr2idx(defattr);
    992  1.81  macallan 	printf("defattr %08lx idx %x\n", defattr, idx);
    993  1.81  macallan 
    994  1.81  macallan 	if (idx >= 0) {
    995  1.81  macallan 		sc->sc_attrmap[idx] = 0;
    996  1.81  macallan 		sc->sc_buckets[0].gb_index = idx;
    997  1.81  macallan 	}
    998  1.81  macallan 
    999  1.81  macallan 	return 0;
   1000  1.81  macallan }
   1001  1.81  macallan 
   1002  1.81  macallan static void
   1003  1.81  macallan genfb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1004  1.81  macallan {
   1005  1.81  macallan 	struct rasops_info *ri = cookie;
   1006  1.81  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1007  1.81  macallan 	struct genfb_softc *sc = scr->scr_cookie;
   1008  1.87       rin 	uint8_t *src, *dst, *hwdst;
   1009  1.81  macallan 	gc_bucket *b;
   1010  1.81  macallan 	int i, idx, bi, cell;
   1011  1.81  macallan 
   1012  1.81  macallan 	attr &= ~WSATTR_USERMASK;
   1013  1.81  macallan 
   1014  1.81  macallan 	idx = attr2idx(attr);
   1015  1.81  macallan 	if (c < 33 || c > 255 || idx < 0) goto nope;
   1016  1.81  macallan 
   1017  1.81  macallan 	/* look for a bucket with the right attribute */
   1018  1.81  macallan 	bi = sc->sc_attrmap[idx];
   1019  1.81  macallan 	if (bi == -1) {
   1020  1.81  macallan 		/* nope, see if there's an empty one left */
   1021  1.81  macallan 		bi = 1;
   1022  1.81  macallan 		while ((bi < sc->sc_nbuckets) &&
   1023  1.81  macallan 		       (sc->sc_buckets[bi].gb_index != -1)) {
   1024  1.81  macallan 			bi++;
   1025  1.81  macallan 		}
   1026  1.81  macallan 		if (bi < sc->sc_nbuckets) {
   1027  1.81  macallan 			/* found one -> grab it */
   1028  1.81  macallan 			sc->sc_attrmap[idx] = bi;
   1029  1.81  macallan 			b = &sc->sc_buckets[bi];
   1030  1.81  macallan 			b->gb_index = idx;
   1031  1.81  macallan 			b->gb_usedcells = 0;
   1032  1.81  macallan 			/* make sure this doesn't get evicted right away */
   1033  1.81  macallan 			b->gb_lastread = time_uptime;
   1034  1.81  macallan 		} else {
   1035  1.81  macallan 			/*
   1036  1.81  macallan 			 * still nothing
   1037  1.81  macallan 			 * steal the least recently read bucket
   1038  1.81  macallan 			 */
   1039  1.81  macallan 			time_t moo = time_uptime;
   1040  1.81  macallan 			int oldest = 1;
   1041  1.81  macallan 
   1042  1.81  macallan 			for (i = 1; i < sc->sc_nbuckets; i++) {
   1043  1.81  macallan 				if (sc->sc_buckets[i].gb_lastread < moo) {
   1044  1.81  macallan 					oldest = i;
   1045  1.81  macallan 					moo = sc->sc_buckets[i].gb_lastread;
   1046  1.81  macallan 				}
   1047  1.81  macallan 			}
   1048  1.81  macallan 
   1049  1.81  macallan 			/* if we end up here all buckets must be in use */
   1050  1.81  macallan 			b = &sc->sc_buckets[oldest];
   1051  1.81  macallan 			sc->sc_attrmap[b->gb_index] = -1;
   1052  1.81  macallan 			b->gb_index = idx;
   1053  1.81  macallan 			b->gb_usedcells = 0;
   1054  1.81  macallan 			sc->sc_attrmap[idx] = oldest;
   1055  1.81  macallan 			/* now scrub it */
   1056  1.81  macallan 			for (i = 0; i < 223; i++)
   1057  1.81  macallan 				b->gb_map[i] = -1;
   1058  1.81  macallan 			/* and set the time stamp */
   1059  1.81  macallan 			b->gb_lastread = time_uptime;
   1060  1.81  macallan 		}
   1061  1.81  macallan 	} else {
   1062  1.81  macallan 		/* found one */
   1063  1.81  macallan 		b = &sc->sc_buckets[bi];
   1064  1.81  macallan 	}
   1065  1.81  macallan 
   1066  1.81  macallan 	/* see if there's room in the bucket */
   1067  1.81  macallan 	if (b->gb_usedcells >= b->gb_numcells) goto nope;
   1068  1.81  macallan 
   1069  1.81  macallan 	cell = b->gb_map[c - 33];
   1070  1.81  macallan 	if (cell == -1) {
   1071  1.81  macallan 		if (b->gb_usedcells >= b->gb_numcells)
   1072  1.81  macallan 			goto nope;
   1073  1.81  macallan 		cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
   1074  1.81  macallan 		b->gb_map[c - 33] = cell;
   1075  1.81  macallan 		cell += b->gb_firstcell;
   1076  1.81  macallan 		sc->sc_putchar(&sc->sc_cache_ri, cell, 0, c, attr);
   1077  1.81  macallan 
   1078  1.81  macallan 	} else
   1079  1.81  macallan 		cell += b->gb_firstcell;
   1080  1.81  macallan 
   1081  1.81  macallan 	src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale;
   1082  1.81  macallan 	dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
   1083  1.81  macallan 	for (i = 0; i < ri->ri_font->fontheight; i++) {
   1084  1.81  macallan 		memcpy(dst, src, ri->ri_xscale);
   1085  1.81  macallan 		src += ri->ri_xscale;
   1086  1.81  macallan 		dst += ri->ri_stride;
   1087  1.81  macallan 	}
   1088  1.87       rin 	if (ri->ri_hwbits) {
   1089  1.87       rin 		src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale;
   1090  1.87       rin 		hwdst = ri->ri_hwbits
   1091  1.87       rin 		    + row * ri->ri_yscale + col * ri->ri_xscale;
   1092  1.87       rin 		for (i = 0; i < ri->ri_font->fontheight; i++) {
   1093  1.87       rin 			memcpy(hwdst, src, ri->ri_xscale);
   1094  1.87       rin 			src += ri->ri_xscale;
   1095  1.87       rin 			hwdst += ri->ri_stride;
   1096  1.87       rin 		}
   1097  1.87       rin 	}
   1098  1.81  macallan 	b->gb_lastread = time_uptime;
   1099  1.81  macallan 	return;
   1100  1.81  macallan nope:
   1101  1.81  macallan 	sc->sc_putchar(cookie, row, col, c, attr);
   1102  1.81  macallan }
   1103  1.81  macallan 
   1104  1.81  macallan #endif
   1105