Home | History | Annotate | Line # | Download | only in wscons
wsdisplay_vcons.c revision 1.20
      1  1.20  jmcneill /*	$NetBSD: wsdisplay_vcons.c,v 1.20 2011/02/08 12:45:04 jmcneill Exp $ */
      2   1.1  macallan 
      3   1.1  macallan /*-
      4   1.1  macallan  * Copyright (c) 2005, 2006 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.20  jmcneill __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.20 2011/02/08 12:45:04 jmcneill 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/buf.h>
     36   1.1  macallan #include <sys/device.h>
     37   1.1  macallan #include <sys/ioctl.h>
     38   1.1  macallan #include <sys/malloc.h>
     39   1.1  macallan #include <sys/mman.h>
     40   1.1  macallan #include <sys/tty.h>
     41   1.1  macallan #include <sys/conf.h>
     42   1.1  macallan #include <sys/proc.h>
     43   1.1  macallan #include <sys/kthread.h>
     44   1.1  macallan #include <sys/tprintf.h>
     45  1.19  macallan #include <sys/atomic.h>
     46   1.1  macallan 
     47   1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     48   1.1  macallan #include <dev/wscons/wsconsio.h>
     49   1.1  macallan #include <dev/wsfont/wsfont.h>
     50   1.1  macallan #include <dev/rasops/rasops.h>
     51   1.1  macallan 
     52   1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     53   1.1  macallan 
     54  1.14  macallan #include "opt_wsemul.h"
     55  1.14  macallan #include "opt_wsdisplay_compat.h"
     56  1.14  macallan #include "opt_vcons.h"
     57  1.14  macallan 
     58  1.20  jmcneill #if defined(VCONS_DRAW_ASYNC) && defined(VCONS_DRAW_INTR)
     59  1.20  jmcneill #error VCONS_DRAW_ASYNC and VCONS_DRAW_INTR cannot be defined together
     60  1.20  jmcneill #endif
     61  1.20  jmcneill 
     62   1.1  macallan static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
     63   1.1  macallan 	    long *);
     64   1.1  macallan 
     65  1.10  christos static int  vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     66   1.1  macallan static int  vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
     67   1.1  macallan 	    int *, int *, long *);
     68   1.1  macallan static void vcons_free_screen(void *, void *);
     69   1.1  macallan static int  vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
     70   1.1  macallan 	    void *);
     71   1.1  macallan 
     72  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
     73  1.14  macallan static void vcons_scroll(void *, void *, int);
     74  1.14  macallan static void vcons_do_scroll(struct vcons_screen *);
     75  1.14  macallan #endif
     76  1.14  macallan 
     77  1.15     joerg static void vcons_do_switch(void *);
     78   1.1  macallan 
     79   1.1  macallan /* methods that work only on text buffers */
     80   1.1  macallan static void vcons_copycols_buffer(void *, int, int, int, int);
     81   1.1  macallan static void vcons_erasecols_buffer(void *, int, int, int, long);
     82   1.1  macallan static void vcons_copyrows_buffer(void *, int, int, int);
     83   1.1  macallan static void vcons_eraserows_buffer(void *, int, int, long);
     84   1.1  macallan static void vcons_putchar_buffer(void *, int, int, u_int, long);
     85   1.1  macallan 
     86  1.19  macallan #ifdef VCONS_DRAW_ASYNC
     87  1.19  macallan /* methods that work asynchronously */
     88  1.19  macallan static void vcons_copycols_async(void *, int, int, int, int);
     89  1.19  macallan static void vcons_erasecols_async(void *, int, int, int, long);
     90  1.19  macallan static void vcons_copyrows_async(void *, int, int, int);
     91  1.19  macallan static void vcons_eraserows_async(void *, int, int, long);
     92  1.19  macallan static void vcons_putchar_async(void *, int, int, u_int, long);
     93  1.19  macallan static void vcons_cursor_async(void *, int, int, int);
     94  1.19  macallan #endif
     95  1.19  macallan 
     96   1.1  macallan /*
     97   1.1  macallan  * actual wrapper methods which call both the _buffer ones above and the
     98   1.1  macallan  * driver supplied ones to do the drawing
     99   1.1  macallan  */
    100   1.1  macallan static void vcons_copycols(void *, int, int, int, int);
    101   1.1  macallan static void vcons_erasecols(void *, int, int, int, long);
    102   1.1  macallan static void vcons_copyrows(void *, int, int, int);
    103   1.1  macallan static void vcons_eraserows(void *, int, int, long);
    104   1.1  macallan static void vcons_putchar(void *, int, int, u_int, long);
    105   1.1  macallan static void vcons_cursor(void *, int, int, int);
    106   1.1  macallan 
    107  1.17  macallan /*
    108  1.17  macallan  * methods that avoid framebuffer reads
    109  1.17  macallan  */
    110  1.17  macallan static void vcons_copycols_noread(void *, int, int, int, int);
    111  1.17  macallan static void vcons_copyrows_noread(void *, int, int, int);
    112  1.17  macallan 
    113  1.17  macallan 
    114  1.14  macallan /* support for reading/writing text buffers. For wsmoused */
    115   1.6      jmmv static int  vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
    116   1.6      jmmv static int  vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
    117   1.1  macallan 
    118   1.1  macallan static void vcons_lock(struct vcons_screen *);
    119   1.1  macallan static void vcons_unlock(struct vcons_screen *);
    120   1.1  macallan 
    121  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    122  1.14  macallan static void vcons_kthread(void *);
    123  1.14  macallan #endif
    124  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    125  1.20  jmcneill static void vcons_intr(void *);
    126  1.20  jmcneill static void vcons_intr_work(struct work *, void *);
    127  1.20  jmcneill #endif
    128   1.1  macallan 
    129   1.1  macallan int
    130   1.1  macallan vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
    131   1.1  macallan     struct wsdisplay_accessops *ao)
    132   1.1  macallan {
    133   1.2  macallan 
    134   1.2  macallan 	/* zero out everything so we can rely on untouched fields being 0 */
    135   1.3  macallan 	memset(vd, 0, sizeof(struct vcons_data));
    136   1.2  macallan 
    137   1.1  macallan 	vd->cookie = cookie;
    138   1.1  macallan 
    139   1.1  macallan 	vd->init_screen = vcons_dummy_init_screen;
    140   1.1  macallan 	vd->show_screen_cb = NULL;
    141   1.1  macallan 
    142   1.6      jmmv 	/* keep a copy of the accessops that we replace below with our
    143   1.6      jmmv 	 * own wrappers */
    144   1.6      jmmv 	vd->ioctl = ao->ioctl;
    145   1.6      jmmv 
    146   1.6      jmmv 	/* configure the accessops */
    147   1.6      jmmv 	ao->ioctl = vcons_ioctl;
    148   1.1  macallan 	ao->alloc_screen = vcons_alloc_screen;
    149   1.1  macallan 	ao->free_screen = vcons_free_screen;
    150   1.1  macallan 	ao->show_screen = vcons_show_screen;
    151  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    152  1.14  macallan 	ao->scroll = vcons_scroll;
    153  1.14  macallan #endif
    154   1.1  macallan 
    155   1.1  macallan 	LIST_INIT(&vd->screens);
    156   1.1  macallan 	vd->active = NULL;
    157   1.1  macallan 	vd->wanted = NULL;
    158   1.1  macallan 	vd->currenttype = def;
    159  1.11        ad 	callout_init(&vd->switch_callout, 0);
    160  1.15     joerg 	callout_setfunc(&vd->switch_callout, vcons_do_switch, vd);
    161   1.1  macallan 
    162   1.1  macallan 	/*
    163   1.1  macallan 	 * a lock to serialize access to the framebuffer.
    164   1.1  macallan 	 * when switching screens we need to make sure there's no rasops
    165   1.1  macallan 	 * operation in progress
    166   1.1  macallan 	 */
    167   1.1  macallan #ifdef DIAGNOSTIC
    168   1.1  macallan 	vd->switch_poll_count = 0;
    169   1.1  macallan #endif
    170  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    171  1.14  macallan 	kthread_create(PRI_NONE, 0, NULL, vcons_kthread, vd,
    172  1.19  macallan 	    &vd->drawing_thread, "vcons_draw");
    173  1.14  macallan #endif
    174  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    175  1.20  jmcneill 	workqueue_create(&vd->intr_wq, "vcons_draw",
    176  1.20  jmcneill 	    vcons_intr_work, vd, PRI_KTHREAD, IPL_TTY, WQ_MPSAFE);
    177  1.20  jmcneill 	callout_init(&vd->intr, 0);
    178  1.20  jmcneill 	callout_setfunc(&vd->intr, vcons_intr, vd);
    179  1.20  jmcneill 	callout_schedule(&vd->intr, mstohz(33));
    180  1.20  jmcneill #endif
    181   1.1  macallan 	return 0;
    182   1.1  macallan }
    183   1.1  macallan 
    184   1.1  macallan static void
    185   1.1  macallan vcons_lock(struct vcons_screen *scr)
    186   1.1  macallan {
    187   1.1  macallan #ifdef VCONS_PARANOIA
    188   1.1  macallan 	int s;
    189   1.1  macallan 
    190   1.1  macallan 	s = splhigh();
    191   1.1  macallan #endif
    192   1.1  macallan 	SCREEN_BUSY(scr);
    193   1.1  macallan #ifdef VCONS_PARANOIA
    194   1.1  macallan 	splx(s);
    195   1.1  macallan #endif
    196   1.1  macallan }
    197   1.1  macallan 
    198   1.1  macallan static void
    199   1.1  macallan vcons_unlock(struct vcons_screen *scr)
    200   1.1  macallan {
    201   1.1  macallan #ifdef VCONS_PARANOIA
    202   1.1  macallan 	int s;
    203   1.1  macallan 
    204   1.1  macallan 	s = splhigh();
    205   1.1  macallan #endif
    206   1.1  macallan 	SCREEN_IDLE(scr);
    207   1.1  macallan #ifdef VCONS_PARANOIA
    208   1.1  macallan 	splx(s);
    209   1.1  macallan #endif
    210   1.1  macallan }
    211   1.1  macallan 
    212   1.1  macallan static void
    213   1.9  christos vcons_dummy_init_screen(void *cookie,
    214   1.9  christos     struct vcons_screen *scr, int exists,
    215   1.9  christos     long *defattr)
    216   1.1  macallan {
    217   1.1  macallan 
    218   1.1  macallan 	/*
    219   1.1  macallan 	 * default init_screen() method.
    220   1.1  macallan 	 * Needs to be overwritten so we bitch and whine in case anyone ends
    221   1.1  macallan 	 * up in here.
    222   1.1  macallan 	 */
    223   1.1  macallan 	printf("vcons_init_screen: dummy function called. Your driver is "
    224   1.1  macallan 	       "supposed to supply a replacement for proper operation\n");
    225   1.1  macallan }
    226   1.1  macallan 
    227   1.1  macallan int
    228   1.1  macallan vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
    229   1.1  macallan     int existing, long *defattr)
    230   1.1  macallan {
    231   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    232   1.1  macallan 	int cnt, i;
    233   1.1  macallan 
    234   1.1  macallan 	scr->scr_cookie = vd->cookie;
    235   1.4  jmcneill 	scr->scr_vd = scr->scr_origvd = vd;
    236  1.14  macallan 	scr->scr_busy = 0;
    237   1.2  macallan 
    238   1.1  macallan 	/*
    239   1.1  macallan 	 * call the driver-supplied init_screen function which is expected
    240   1.1  macallan 	 * to set up rasops_info, override cursor() and probably others
    241   1.1  macallan 	 */
    242   1.1  macallan 	vd->init_screen(vd->cookie, scr, existing, defattr);
    243   1.1  macallan 
    244   1.1  macallan 	/*
    245   1.1  macallan 	 * save the non virtual console aware rasops and replace them with
    246   1.1  macallan 	 * our wrappers
    247   1.1  macallan 	 */
    248   1.1  macallan 	vd->eraserows = ri->ri_ops.eraserows;
    249   1.1  macallan 	vd->erasecols = ri->ri_ops.erasecols;
    250   1.1  macallan 	vd->putchar   = ri->ri_ops.putchar;
    251   1.1  macallan 	vd->cursor    = ri->ri_ops.cursor;
    252   1.1  macallan 
    253  1.18  macallan 	if (scr->scr_flags & VCONS_NO_COPYCOLS) {
    254  1.19  macallan 		vd->copycols  = vcons_copycols_noread;
    255  1.18  macallan 	} else {
    256  1.19  macallan 		vd->copycols = ri->ri_ops.copycols;
    257  1.18  macallan 	}
    258  1.18  macallan 
    259  1.18  macallan 	if (scr->scr_flags & VCONS_NO_COPYROWS) {
    260  1.19  macallan 		vd->copyrows  = vcons_copyrows_noread;
    261  1.17  macallan 	} else {
    262  1.19  macallan 		vd->copyrows = ri->ri_ops.copyrows;
    263  1.17  macallan 	}
    264  1.17  macallan 
    265  1.19  macallan 	ri->ri_ops.eraserows = vcons_eraserows;
    266  1.19  macallan 	ri->ri_ops.erasecols = vcons_erasecols;
    267  1.19  macallan 	ri->ri_ops.putchar   = vcons_putchar;
    268  1.19  macallan 	ri->ri_ops.cursor    = vcons_cursor;
    269  1.19  macallan 	ri->ri_ops.copycols  = vcons_copycols;
    270  1.19  macallan 	ri->ri_ops.copyrows  = vcons_copyrows;
    271  1.19  macallan 
    272  1.19  macallan 
    273   1.1  macallan 	ri->ri_hw = scr;
    274   1.1  macallan 
    275   1.1  macallan 	/*
    276   1.1  macallan 	 * we allocate both chars and attributes in one chunk, attributes first
    277   1.1  macallan 	 * because they have the (potentially) bigger alignment
    278   1.1  macallan 	 */
    279  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    280  1.14  macallan 	cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols;
    281  1.14  macallan 	scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES;
    282  1.14  macallan 	scr->scr_current_line = 0;
    283  1.14  macallan 	scr->scr_line_wanted = 0;
    284  1.14  macallan 	scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES;
    285  1.14  macallan 	scr->scr_current_offset = scr->scr_offset_to_zero;
    286  1.14  macallan #else
    287   1.1  macallan 	cnt = ri->ri_rows * ri->ri_cols;
    288  1.14  macallan #endif
    289   1.1  macallan 	scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) +
    290   1.1  macallan 	    sizeof(uint16_t)), M_DEVBUF, M_WAITOK);
    291   1.1  macallan 	if (scr->scr_attrs == NULL)
    292   1.1  macallan 		return ENOMEM;
    293   1.1  macallan 
    294   1.1  macallan 	scr->scr_chars = (uint16_t *)&scr->scr_attrs[cnt];
    295   1.1  macallan 
    296   1.1  macallan 	ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
    297   1.1  macallan 	scr->scr_defattr = *defattr;
    298   1.1  macallan 
    299   1.1  macallan 	/*
    300   1.1  macallan 	 * fill the attribute buffer with *defattr, chars with 0x20
    301   1.1  macallan 	 * since we don't know if the driver tries to mimic firmware output or
    302   1.1  macallan 	 * reset everything we do nothing to VRAM here, any driver that feels
    303   1.1  macallan 	 * the need to clear screen or something will have to do it on its own
    304   1.1  macallan 	 * Additional screens will start out in the background anyway so
    305   1.1  macallan 	 * cleaning or not only really affects the initial console screen
    306   1.1  macallan 	 */
    307   1.1  macallan 	for (i = 0; i < cnt; i++) {
    308   1.1  macallan 		scr->scr_attrs[i] = *defattr;
    309   1.1  macallan 		scr->scr_chars[i] = 0x20;
    310   1.1  macallan 	}
    311   1.1  macallan 
    312   1.2  macallan 	if(vd->active == NULL) {
    313   1.2  macallan 		vd->active = scr;
    314   1.2  macallan 		SCREEN_VISIBLE(scr);
    315   1.2  macallan 	}
    316   1.2  macallan 
    317   1.1  macallan 	if (existing) {
    318   1.2  macallan 		SCREEN_VISIBLE(scr);
    319   1.2  macallan 		vd->active = scr;
    320   1.1  macallan 	} else {
    321   1.2  macallan 		SCREEN_INVISIBLE(scr);
    322   1.1  macallan 	}
    323   1.1  macallan 
    324   1.1  macallan 	LIST_INSERT_HEAD(&vd->screens, scr, next);
    325   1.1  macallan 	return 0;
    326   1.1  macallan }
    327   1.1  macallan 
    328   1.1  macallan static void
    329  1.15     joerg vcons_do_switch(void *arg)
    330   1.1  macallan {
    331  1.15     joerg 	struct vcons_data *vd = arg;
    332   1.1  macallan 	struct vcons_screen *scr, *oldscr;
    333   1.1  macallan 
    334   1.1  macallan 	scr = vd->wanted;
    335   1.1  macallan 	if (!scr) {
    336   1.1  macallan 		printf("vcons_switch_screen: disappeared\n");
    337   1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, EIO, 0);
    338   1.1  macallan 		return;
    339   1.1  macallan 	}
    340   1.1  macallan 	oldscr = vd->active; /* can be NULL! */
    341   1.1  macallan 
    342   1.1  macallan 	/*
    343   1.1  macallan 	 * if there's an old, visible screen we mark it invisible and wait
    344   1.1  macallan 	 * until it's not busy so we can safely switch
    345   1.1  macallan 	 */
    346   1.1  macallan 	if (oldscr != NULL) {
    347   1.1  macallan 		SCREEN_INVISIBLE(oldscr);
    348   1.1  macallan 		if (SCREEN_IS_BUSY(oldscr)) {
    349  1.15     joerg 			callout_schedule(&vd->switch_callout, 1);
    350   1.1  macallan #ifdef DIAGNOSTIC
    351   1.1  macallan 			/* bitch if we wait too long */
    352   1.1  macallan 			vd->switch_poll_count++;
    353   1.1  macallan 			if (vd->switch_poll_count > 100) {
    354   1.1  macallan 				panic("vcons: screen still busy");
    355   1.1  macallan 			}
    356   1.1  macallan #endif
    357   1.1  macallan 			return;
    358   1.1  macallan 		}
    359   1.1  macallan 		/* invisible screen -> no visible cursor image */
    360   1.1  macallan 		oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
    361   1.1  macallan #ifdef DIAGNOSTIC
    362   1.1  macallan 		vd->switch_poll_count = 0;
    363   1.1  macallan #endif
    364   1.1  macallan 	}
    365   1.1  macallan 
    366   1.1  macallan 	if (scr == oldscr)
    367   1.1  macallan 		return;
    368   1.1  macallan 
    369   1.1  macallan #ifdef DIAGNOSTIC
    370   1.1  macallan 	if (SCREEN_IS_VISIBLE(scr))
    371  1.14  macallan 		printf("vcons_switch_screen: already active");
    372   1.1  macallan #endif
    373   1.1  macallan 
    374   1.1  macallan #ifdef notyet
    375   1.1  macallan 	if (vd->currenttype != type) {
    376   1.1  macallan 		vcons_set_screentype(vd, type);
    377   1.1  macallan 		vd->currenttype = type;
    378   1.1  macallan 	}
    379   1.1  macallan #endif
    380   1.1  macallan 
    381   1.1  macallan 	SCREEN_VISIBLE(scr);
    382   1.1  macallan 	vd->active = scr;
    383   1.1  macallan 	vd->wanted = NULL;
    384   1.1  macallan 
    385   1.1  macallan 	if (vd->show_screen_cb != NULL)
    386   1.1  macallan 		vd->show_screen_cb(scr);
    387   1.1  macallan 
    388   1.1  macallan 	if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
    389   1.1  macallan 		vcons_redraw_screen(scr);
    390   1.1  macallan 
    391   1.1  macallan 	if (vd->switch_cb)
    392   1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, 0, 0);
    393   1.1  macallan }
    394   1.1  macallan 
    395   1.1  macallan void
    396   1.1  macallan vcons_redraw_screen(struct vcons_screen *scr)
    397   1.1  macallan {
    398   1.1  macallan 	uint16_t *charptr = scr->scr_chars;
    399   1.1  macallan 	long *attrptr = scr->scr_attrs;
    400   1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    401   1.1  macallan 	int i, j, offset;
    402   1.1  macallan 
    403   1.1  macallan 	vcons_lock(scr);
    404   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    405   1.1  macallan 
    406   1.1  macallan 		/*
    407   1.1  macallan 		 * only clear the screen when RI_FULLCLEAR is set since we're
    408   1.1  macallan 		 * going to overwrite every single character cell anyway
    409   1.1  macallan 		 */
    410   1.1  macallan 		if (ri->ri_flg & RI_FULLCLEAR) {
    411   1.1  macallan 			scr->scr_vd->eraserows(ri, 0, ri->ri_rows,
    412   1.1  macallan 			    scr->scr_defattr);
    413   1.1  macallan 		}
    414   1.1  macallan 
    415   1.1  macallan 		/* redraw the screen */
    416  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    417  1.14  macallan 		offset = scr->scr_current_offset;
    418  1.14  macallan #else
    419   1.1  macallan 		offset = 0;
    420  1.14  macallan #endif
    421   1.1  macallan 		for (i = 0; i < ri->ri_rows; i++) {
    422   1.1  macallan 			for (j = 0; j < ri->ri_cols; j++) {
    423   1.1  macallan 				/*
    424   1.1  macallan 				 * no need to use the wrapper function - we
    425   1.1  macallan 				 * don't change any characters or attributes
    426   1.1  macallan 				 * and we already made sure the screen we're
    427   1.1  macallan 				 * working on is visible
    428   1.1  macallan 				 */
    429   1.1  macallan 				scr->scr_vd->putchar(ri, i, j,
    430   1.1  macallan 				    charptr[offset], attrptr[offset]);
    431   1.1  macallan 				offset++;
    432   1.1  macallan 			}
    433   1.1  macallan 		}
    434   1.1  macallan 		ri->ri_flg &= ~RI_CURSOR;
    435   1.1  macallan 		scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
    436   1.1  macallan 	}
    437   1.1  macallan 	vcons_unlock(scr);
    438   1.1  macallan }
    439   1.1  macallan 
    440   1.1  macallan static int
    441  1.10  christos vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    442   1.6      jmmv 	struct lwp *l)
    443   1.6      jmmv {
    444   1.6      jmmv 	struct vcons_data *vd = v;
    445   1.6      jmmv 	int error;
    446   1.6      jmmv 
    447   1.6      jmmv 	switch (cmd) {
    448   1.6      jmmv 	case WSDISPLAYIO_GETWSCHAR:
    449   1.6      jmmv 		error = vcons_getwschar((struct vcons_screen *)vs,
    450   1.6      jmmv 			(struct wsdisplay_char *)data);
    451   1.6      jmmv 		break;
    452   1.6      jmmv 
    453   1.6      jmmv 	case WSDISPLAYIO_PUTWSCHAR:
    454   1.6      jmmv 		error = vcons_putwschar((struct vcons_screen *)vs,
    455   1.6      jmmv 			(struct wsdisplay_char *)data);
    456   1.6      jmmv 		break;
    457   1.6      jmmv 
    458   1.6      jmmv 	default:
    459   1.6      jmmv 		if (vd->ioctl != NULL)
    460   1.6      jmmv 			error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
    461   1.6      jmmv 		else
    462   1.6      jmmv 			error = EPASSTHROUGH;
    463   1.6      jmmv 	}
    464   1.6      jmmv 
    465   1.6      jmmv 	return error;
    466   1.6      jmmv }
    467   1.6      jmmv 
    468   1.6      jmmv static int
    469   1.1  macallan vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    470   1.1  macallan     int *curxp, int *curyp, long *defattrp)
    471   1.1  macallan {
    472   1.1  macallan 	struct vcons_data *vd = v;
    473   1.1  macallan 	struct vcons_screen *scr;
    474   1.1  macallan 	int ret;
    475   1.1  macallan 
    476   1.1  macallan 	scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
    477   1.1  macallan 	if (scr == NULL)
    478   1.1  macallan 		return ENOMEM;
    479   1.1  macallan 
    480   1.1  macallan 	scr->scr_flags = 0;
    481   1.2  macallan 	scr->scr_status = 0;
    482   1.2  macallan 	scr->scr_busy = 0;
    483   1.1  macallan 	scr->scr_type = type;
    484   1.1  macallan 
    485   1.1  macallan 	ret = vcons_init_screen(vd, scr, 0, defattrp);
    486   1.1  macallan 	if (ret != 0) {
    487   1.1  macallan 		free(scr, M_DEVBUF);
    488   1.1  macallan 		return ret;
    489   1.1  macallan 	}
    490   1.1  macallan 
    491   1.1  macallan 	if (vd->active == NULL) {
    492   1.1  macallan 		SCREEN_VISIBLE(scr);
    493   1.1  macallan 		vd->active = scr;
    494   1.1  macallan 		vd->currenttype = type;
    495   1.1  macallan 	}
    496   1.1  macallan 
    497   1.1  macallan 	*cookiep = scr;
    498   1.1  macallan 	*curxp = scr->scr_ri.ri_ccol;
    499   1.1  macallan 	*curyp = scr->scr_ri.ri_crow;
    500   1.1  macallan 	return 0;
    501   1.1  macallan }
    502   1.1  macallan 
    503   1.1  macallan static void
    504   1.1  macallan vcons_free_screen(void *v, void *cookie)
    505   1.1  macallan {
    506   1.1  macallan 	struct vcons_data *vd = v;
    507   1.1  macallan 	struct vcons_screen *scr = cookie;
    508   1.1  macallan 
    509   1.1  macallan 	vcons_lock(scr);
    510   1.1  macallan 	/* there should be no rasops activity here */
    511   1.1  macallan 
    512   1.1  macallan 	LIST_REMOVE(scr, next);
    513   1.1  macallan 
    514   1.1  macallan 	if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
    515   1.1  macallan 		free(scr->scr_attrs, M_DEVBUF);
    516   1.1  macallan 		free(scr, M_DEVBUF);
    517   1.1  macallan 	} else {
    518   1.1  macallan 		/*
    519   1.1  macallan 		 * maybe we should just restore the old rasops_info methods
    520   1.1  macallan 		 * and free the character/attribute buffer here?
    521   1.1  macallan 		 */
    522   1.1  macallan #ifdef VCONS_DEBUG
    523   1.1  macallan 		panic("vcons_free_screen: console");
    524   1.1  macallan #else
    525   1.1  macallan 		printf("vcons_free_screen: console\n");
    526   1.1  macallan #endif
    527   1.1  macallan 	}
    528   1.1  macallan 
    529   1.1  macallan 	if (vd->active == scr)
    530   1.1  macallan 		vd->active = NULL;
    531   1.1  macallan }
    532   1.1  macallan 
    533   1.1  macallan static int
    534   1.9  christos vcons_show_screen(void *v, void *cookie, int waitok,
    535   1.1  macallan     void (*cb)(void *, int, int), void *cb_arg)
    536   1.1  macallan {
    537   1.1  macallan 	struct vcons_data *vd = v;
    538   1.1  macallan 	struct vcons_screen *scr;
    539   1.1  macallan 
    540   1.1  macallan 	scr = cookie;
    541   1.1  macallan 	if (scr == vd->active)
    542   1.1  macallan 		return 0;
    543   1.1  macallan 
    544   1.1  macallan 	vd->wanted = scr;
    545   1.1  macallan 	vd->switch_cb = cb;
    546   1.1  macallan 	vd->switch_cb_arg = cb_arg;
    547   1.1  macallan 	if (cb) {
    548  1.15     joerg 		callout_schedule(&vd->switch_callout, 0);
    549   1.1  macallan 		return EAGAIN;
    550   1.1  macallan 	}
    551   1.1  macallan 
    552   1.1  macallan 	vcons_do_switch(vd);
    553   1.1  macallan 	return 0;
    554   1.1  macallan }
    555   1.1  macallan 
    556   1.1  macallan /* wrappers for rasops_info methods */
    557   1.1  macallan 
    558   1.1  macallan static void
    559   1.1  macallan vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
    560   1.1  macallan {
    561   1.1  macallan 	struct rasops_info *ri = cookie;
    562   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    563   1.1  macallan 	int from = srccol + row * ri->ri_cols;
    564   1.1  macallan 	int to = dstcol + row * ri->ri_cols;
    565   1.1  macallan 
    566  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    567  1.14  macallan 	int offset;
    568  1.14  macallan 	offset = scr->scr_offset_to_zero;
    569  1.14  macallan 
    570  1.14  macallan 	memmove(&scr->scr_attrs[offset + to], &scr->scr_attrs[offset + from],
    571  1.14  macallan 	    ncols * sizeof(long));
    572  1.14  macallan 	memmove(&scr->scr_chars[offset + to], &scr->scr_chars[offset + from],
    573  1.14  macallan 	    ncols * sizeof(uint16_t));
    574  1.14  macallan #else
    575   1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    576   1.1  macallan 	    ncols * sizeof(long));
    577   1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    578   1.1  macallan 	    ncols * sizeof(uint16_t));
    579  1.14  macallan #endif
    580  1.20  jmcneill 
    581  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    582  1.20  jmcneill 	scr->scr_dirty++;
    583  1.20  jmcneill #endif
    584   1.1  macallan }
    585   1.1  macallan 
    586   1.1  macallan static void
    587   1.1  macallan vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    588   1.1  macallan {
    589  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    590   1.1  macallan 	struct rasops_info *ri = cookie;
    591   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    592  1.20  jmcneill #endif
    593   1.1  macallan 
    594   1.1  macallan 	vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
    595   1.1  macallan 
    596  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    597   1.1  macallan 	vcons_lock(scr);
    598   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    599  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    600  1.19  macallan 		struct vcons_data *vd = scr->scr_vd;
    601  1.19  macallan 		if (vd->use_async) {
    602  1.19  macallan 			vcons_copycols_async(cookie, row, srccol, dstcol, ncols);
    603  1.19  macallan 		} else
    604  1.19  macallan #endif
    605  1.19  macallan 			scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
    606   1.1  macallan 	}
    607   1.1  macallan 	vcons_unlock(scr);
    608  1.20  jmcneill #endif
    609   1.1  macallan }
    610   1.1  macallan 
    611   1.1  macallan static void
    612  1.17  macallan vcons_copycols_noread(void *cookie, int row, int srccol, int dstcol, int ncols)
    613  1.17  macallan {
    614  1.17  macallan 	struct rasops_info *ri = cookie;
    615  1.17  macallan 	struct vcons_screen *scr = ri->ri_hw;
    616  1.17  macallan 
    617  1.17  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    618  1.17  macallan 		int pos, c, offset;
    619  1.17  macallan 
    620  1.17  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    621  1.17  macallan 		offset = scr->scr_current_offset;
    622  1.17  macallan #else
    623  1.17  macallan 		offset = 0;
    624  1.17  macallan #endif
    625  1.17  macallan 		pos = ri->ri_cols * row + dstcol + offset;
    626  1.17  macallan 		for (c = dstcol; c < (dstcol + ncols); c++) {
    627  1.17  macallan 			scr->scr_vd->putchar(cookie, row, c,
    628  1.17  macallan 			   scr->scr_chars[pos], scr->scr_attrs[pos]);
    629  1.17  macallan 			pos++;
    630  1.17  macallan 		}
    631  1.17  macallan 	}
    632  1.17  macallan }
    633  1.17  macallan 
    634  1.17  macallan static void
    635   1.1  macallan vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
    636   1.1  macallan {
    637   1.1  macallan 	struct rasops_info *ri = cookie;
    638   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    639   1.1  macallan 	int start = startcol + row * ri->ri_cols;
    640   1.1  macallan 	int end = start + ncols, i;
    641   1.1  macallan 
    642  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    643  1.14  macallan 	int offset;
    644  1.14  macallan 	offset = scr->scr_offset_to_zero;
    645  1.14  macallan 
    646  1.14  macallan 	for (i = start; i < end; i++) {
    647  1.14  macallan 		scr->scr_attrs[offset + i] = fillattr;
    648  1.14  macallan 		scr->scr_chars[offset + i] = 0x20;
    649  1.14  macallan 	}
    650  1.14  macallan #else
    651   1.1  macallan 	for (i = start; i < end; i++) {
    652   1.1  macallan 		scr->scr_attrs[i] = fillattr;
    653   1.1  macallan 		scr->scr_chars[i] = 0x20;
    654   1.1  macallan 	}
    655  1.14  macallan #endif
    656  1.20  jmcneill 
    657  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    658  1.20  jmcneill 	scr->scr_dirty++;
    659  1.20  jmcneill #endif
    660   1.1  macallan }
    661   1.1  macallan 
    662   1.1  macallan static void
    663   1.1  macallan vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    664   1.1  macallan {
    665  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    666   1.1  macallan 	struct rasops_info *ri = cookie;
    667   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    668  1.20  jmcneill #endif
    669   1.1  macallan 
    670   1.1  macallan 	vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
    671   1.1  macallan 
    672  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    673   1.1  macallan 	vcons_lock(scr);
    674   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    675  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    676  1.19  macallan 		struct vcons_data *vd = scr->scr_vd;
    677  1.19  macallan 		if (vd->use_async) {
    678  1.19  macallan 			vcons_erasecols_async(cookie, row, startcol, ncols,
    679  1.19  macallan 			    fillattr);
    680  1.19  macallan 		} else
    681  1.19  macallan #endif
    682  1.19  macallan 			scr->scr_vd->erasecols(cookie, row, startcol, ncols,
    683  1.19  macallan 			    fillattr);
    684   1.1  macallan 	}
    685   1.1  macallan 	vcons_unlock(scr);
    686  1.20  jmcneill #endif
    687   1.1  macallan }
    688   1.1  macallan 
    689   1.1  macallan static void
    690   1.1  macallan vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
    691   1.1  macallan {
    692   1.1  macallan 	struct rasops_info *ri = cookie;
    693   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    694   1.1  macallan 	int from, to, len;
    695   1.1  macallan 
    696  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    697  1.14  macallan 	int offset;
    698  1.14  macallan 	offset = scr->scr_offset_to_zero;
    699  1.14  macallan 
    700  1.14  macallan 	/* do we need to scroll the back buffer? */
    701  1.14  macallan 	if (dstrow == 0) {
    702  1.14  macallan 		from = ri->ri_cols * srcrow;
    703  1.14  macallan 		to = ri->ri_cols * dstrow;
    704  1.14  macallan 
    705  1.14  macallan 		memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    706  1.14  macallan 		    scr->scr_offset_to_zero * sizeof(long));
    707  1.14  macallan 		memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    708  1.14  macallan 		    scr->scr_offset_to_zero * sizeof(uint16_t));
    709  1.14  macallan 	}
    710  1.14  macallan 	from = ri->ri_cols * srcrow + offset;
    711  1.14  macallan 	to = ri->ri_cols * dstrow + offset;
    712  1.14  macallan 	len = ri->ri_cols * nrows;
    713  1.14  macallan 
    714  1.14  macallan #else
    715   1.1  macallan 	from = ri->ri_cols * srcrow;
    716   1.1  macallan 	to = ri->ri_cols * dstrow;
    717   1.1  macallan 	len = ri->ri_cols * nrows;
    718  1.14  macallan #endif
    719   1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    720   1.1  macallan 	    len * sizeof(long));
    721   1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    722   1.1  macallan 	    len * sizeof(uint16_t));
    723  1.20  jmcneill 
    724  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    725  1.20  jmcneill 	scr->scr_dirty++;
    726  1.20  jmcneill #endif
    727   1.1  macallan }
    728   1.1  macallan 
    729   1.1  macallan static void
    730   1.1  macallan vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    731   1.1  macallan {
    732  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    733   1.1  macallan 	struct rasops_info *ri = cookie;
    734   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    735  1.20  jmcneill #endif
    736   1.1  macallan 
    737   1.1  macallan 	vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
    738   1.1  macallan 
    739  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    740   1.1  macallan 	vcons_lock(scr);
    741   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    742  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    743  1.19  macallan 		struct vcons_data *vd = scr->scr_vd;
    744  1.19  macallan 		if (vd->use_async) {
    745  1.19  macallan 			vcons_copyrows_async(cookie, srcrow, dstrow, nrows);
    746  1.19  macallan 		} else
    747  1.19  macallan #endif
    748  1.19  macallan 			scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
    749   1.1  macallan 	}
    750   1.1  macallan 	vcons_unlock(scr);
    751  1.20  jmcneill #endif
    752   1.1  macallan }
    753   1.1  macallan 
    754   1.1  macallan static void
    755  1.17  macallan vcons_copyrows_noread(void *cookie, int srcrow, int dstrow, int nrows)
    756  1.17  macallan {
    757  1.17  macallan 	struct rasops_info *ri = cookie;
    758  1.17  macallan 	struct vcons_screen *scr = ri->ri_hw;
    759  1.17  macallan 
    760  1.17  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    761  1.17  macallan 		int pos, l, c, offset;
    762  1.17  macallan 
    763  1.17  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    764  1.17  macallan 		offset = scr->scr_current_offset;
    765  1.17  macallan #else
    766  1.17  macallan 		offset = 0;
    767  1.17  macallan #endif
    768  1.17  macallan 		pos = ri->ri_cols * dstrow + offset;
    769  1.17  macallan 		for (l = dstrow; l < (dstrow + nrows); l++) {
    770  1.17  macallan 			for (c = 0; c < ri->ri_cols; c++) {
    771  1.17  macallan 				scr->scr_vd->putchar(cookie, l, c,
    772  1.17  macallan 				   scr->scr_chars[pos], scr->scr_attrs[pos]);
    773  1.17  macallan 				pos++;
    774  1.17  macallan 			}
    775  1.17  macallan 		}
    776  1.17  macallan 	}
    777  1.17  macallan }
    778  1.17  macallan 
    779  1.17  macallan static void
    780   1.1  macallan vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
    781   1.1  macallan {
    782   1.1  macallan 	struct rasops_info *ri = cookie;
    783   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    784   1.1  macallan 	int start, end, i;
    785   1.1  macallan 
    786  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    787  1.14  macallan 	int offset;
    788  1.14  macallan 	offset = scr->scr_offset_to_zero;
    789  1.14  macallan 
    790  1.14  macallan 	start = ri->ri_cols * row + offset;
    791  1.14  macallan 	end = ri->ri_cols * (row + nrows) + offset;
    792  1.14  macallan #else
    793   1.1  macallan 	start = ri->ri_cols * row;
    794   1.1  macallan 	end = ri->ri_cols * (row + nrows);
    795  1.14  macallan #endif
    796   1.1  macallan 
    797   1.1  macallan 	for (i = start; i < end; i++) {
    798   1.1  macallan 		scr->scr_attrs[i] = fillattr;
    799   1.1  macallan 		scr->scr_chars[i] = 0x20;
    800   1.1  macallan 	}
    801  1.20  jmcneill 
    802  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    803  1.20  jmcneill 	scr->scr_dirty++;
    804  1.20  jmcneill #endif
    805   1.1  macallan }
    806   1.1  macallan 
    807   1.1  macallan static void
    808   1.1  macallan vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
    809   1.1  macallan {
    810  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    811   1.1  macallan 	struct rasops_info *ri = cookie;
    812   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    813  1.20  jmcneill #endif
    814   1.1  macallan 
    815   1.1  macallan 	vcons_eraserows_buffer(cookie, row, nrows, fillattr);
    816   1.1  macallan 
    817  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    818   1.1  macallan 	vcons_lock(scr);
    819   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    820  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    821  1.19  macallan 		struct vcons_data *vd = scr->scr_vd;
    822  1.19  macallan 		if (vd->use_async) {
    823  1.19  macallan 			vcons_eraserows_async(cookie, row, nrows, fillattr);
    824  1.19  macallan 		} else
    825  1.19  macallan #endif
    826  1.19  macallan 			scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
    827   1.1  macallan 	}
    828   1.1  macallan 	vcons_unlock(scr);
    829  1.20  jmcneill #endif
    830   1.1  macallan }
    831   1.1  macallan 
    832   1.1  macallan static void
    833   1.1  macallan vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
    834   1.1  macallan {
    835   1.1  macallan 	struct rasops_info *ri = cookie;
    836   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    837   1.1  macallan 	int pos;
    838   1.1  macallan 
    839  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    840  1.14  macallan 	int offset;
    841  1.14  macallan 	offset = scr->scr_offset_to_zero;
    842  1.14  macallan 
    843  1.14  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
    844  1.14  macallan 	     (col < ri->ri_cols)) {
    845  1.14  macallan 		pos = col + row * ri->ri_cols;
    846  1.14  macallan 		scr->scr_attrs[pos + offset] = attr;
    847  1.14  macallan 		scr->scr_chars[pos + offset] = c;
    848  1.14  macallan 	}
    849  1.14  macallan #else
    850   1.1  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
    851   1.1  macallan 	     (col < ri->ri_cols)) {
    852   1.1  macallan 		pos = col + row * ri->ri_cols;
    853   1.1  macallan 		scr->scr_attrs[pos] = attr;
    854   1.1  macallan 		scr->scr_chars[pos] = c;
    855   1.1  macallan 	}
    856  1.14  macallan #endif
    857  1.20  jmcneill 
    858  1.20  jmcneill #ifdef VCONS_DRAW_INTR
    859  1.20  jmcneill 	scr->scr_dirty++;
    860  1.20  jmcneill #endif
    861   1.1  macallan }
    862   1.1  macallan 
    863   1.1  macallan static void
    864   1.1  macallan vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
    865   1.1  macallan {
    866  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    867   1.1  macallan 	struct rasops_info *ri = cookie;
    868   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    869  1.20  jmcneill #endif
    870   1.1  macallan 
    871   1.1  macallan 	vcons_putchar_buffer(cookie, row, col, c, attr);
    872   1.1  macallan 
    873  1.20  jmcneill #if !defined(VCONS_DRAW_INTR) || defined(VCONS_INTR_DEBUG)
    874   1.1  macallan 	vcons_lock(scr);
    875   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    876  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    877  1.19  macallan 		struct vcons_data *vd = scr->scr_vd;
    878  1.19  macallan 		if (vd->use_async) {
    879  1.19  macallan 			vcons_putchar_async(cookie, row, col, c, attr);
    880  1.19  macallan 		} else
    881  1.19  macallan #endif
    882  1.19  macallan 			scr->scr_vd->putchar(cookie, row, col, c, attr);
    883   1.1  macallan 	}
    884   1.1  macallan 	vcons_unlock(scr);
    885  1.20  jmcneill #endif
    886   1.1  macallan }
    887   1.1  macallan 
    888   1.1  macallan static void
    889   1.1  macallan vcons_cursor(void *cookie, int on, int row, int col)
    890   1.1  macallan {
    891   1.1  macallan 	struct rasops_info *ri = cookie;
    892   1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    893   1.1  macallan 
    894  1.20  jmcneill 
    895   1.1  macallan 	vcons_lock(scr);
    896  1.20  jmcneill #if defined(VCONS_DRAW_INTR)
    897  1.20  jmcneill 	if (scr->scr_ri.ri_crow != row || scr->scr_ri.ri_ccol != col) {
    898  1.20  jmcneill 		scr->scr_ri.ri_crow = row;
    899  1.20  jmcneill 		scr->scr_ri.ri_ccol = col;
    900  1.20  jmcneill 		scr->scr_dirty++;
    901  1.20  jmcneill 	}
    902  1.20  jmcneill #else
    903   1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    904  1.19  macallan #ifdef VCONS_DRAW_ASYNC
    905  1.19  macallan 		struct vcons_data *vd = scr->scr_vd;
    906  1.19  macallan 		if (vd->use_async) {
    907  1.19  macallan 			vcons_cursor_async(cookie, on, row, col);
    908  1.19  macallan 		} else
    909  1.19  macallan #endif
    910  1.19  macallan 			scr->scr_vd->cursor(cookie, on, row, col);
    911   1.1  macallan 	} else {
    912   1.1  macallan 		scr->scr_ri.ri_crow = row;
    913   1.1  macallan 		scr->scr_ri.ri_ccol = col;
    914   1.1  macallan 	}
    915  1.20  jmcneill #endif
    916   1.1  macallan 	vcons_unlock(scr);
    917   1.1  macallan }
    918   1.1  macallan 
    919   1.1  macallan /* methods to read/write characters via ioctl() */
    920   1.1  macallan 
    921   1.1  macallan static int
    922   1.6      jmmv vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
    923   1.1  macallan {
    924   1.1  macallan 	long attr;
    925   1.6      jmmv 	struct rasops_info *ri;
    926   1.6      jmmv 
    927   1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
    928   1.6      jmmv 
    929  1.13    rumble 	ri = &scr->scr_ri;
    930  1.13    rumble 
    931  1.12       mjf 	if (__predict_false((unsigned int)wsc->col > ri->ri_cols ||
    932  1.12       mjf 	    (unsigned int)wsc->row > ri->ri_rows))
    933  1.12       mjf 			return (EINVAL);
    934   1.1  macallan 
    935  1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
    936  1.14  macallan 	     (wsc->col < ri->ri_cols)) {
    937  1.14  macallan 
    938  1.14  macallan 		ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background,
    939  1.14  macallan 		    wsc->flags, &attr);
    940  1.14  macallan 		vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
    941  1.14  macallan #ifdef VCONS_DEBUG
    942  1.14  macallan 		printf("vcons_putwschar(%d, %d, %x, %lx\n", wsc->row, wsc->col,
    943  1.14  macallan 		    wsc->letter, attr);
    944  1.14  macallan #endif
    945  1.14  macallan 		return 0;
    946  1.14  macallan 	} else
    947  1.14  macallan 		return EINVAL;
    948   1.1  macallan }
    949   1.1  macallan 
    950   1.1  macallan static int
    951   1.6      jmmv vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
    952   1.1  macallan {
    953   1.6      jmmv 	int offset;
    954   1.1  macallan 	long attr;
    955   1.6      jmmv 	struct rasops_info *ri;
    956   1.6      jmmv 
    957   1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
    958   1.6      jmmv 
    959   1.6      jmmv 	ri = &scr->scr_ri;
    960   1.1  macallan 
    961  1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
    962  1.14  macallan 	     (wsc->col < ri->ri_cols)) {
    963  1.14  macallan 
    964  1.14  macallan 		offset = ri->ri_cols * wsc->row + wsc->col;
    965  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    966  1.14  macallan 		offset += scr->scr_offset_to_zero;
    967  1.14  macallan #endif
    968  1.14  macallan 		wsc->letter = scr->scr_chars[offset];
    969  1.14  macallan 		attr = scr->scr_attrs[offset];
    970  1.14  macallan 
    971  1.14  macallan 		/*
    972  1.14  macallan 		 * this is ugly. We need to break up an attribute into colours and
    973  1.14  macallan 		 * flags but there's no rasops method to do that so we must rely on
    974  1.14  macallan 		 * the 'canonical' encoding.
    975  1.14  macallan 		 */
    976  1.14  macallan #ifdef VCONS_DEBUG
    977  1.14  macallan 		printf("vcons_getwschar: %d, %d, %x, %lx\n", wsc->row,
    978  1.14  macallan 		    wsc->col, wsc->letter, attr);
    979  1.14  macallan #endif
    980  1.14  macallan 		wsc->foreground = (attr >> 24) & 0xff;
    981  1.14  macallan 		wsc->background = (attr >> 16) & 0xff;
    982  1.14  macallan 		wsc->flags      = attr & 0xff;
    983  1.14  macallan 		return 0;
    984  1.14  macallan 	} else
    985  1.14  macallan 		return EINVAL;
    986  1.14  macallan }
    987  1.14  macallan 
    988  1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    989  1.14  macallan 
    990  1.14  macallan static void
    991  1.14  macallan vcons_scroll(void *cookie, void *vs, int where)
    992  1.14  macallan {
    993  1.14  macallan 	struct vcons_screen *scr = vs;
    994  1.14  macallan 
    995  1.14  macallan 	if (where == 0) {
    996  1.14  macallan 		scr->scr_line_wanted = 0;
    997  1.14  macallan 	} else {
    998  1.14  macallan 		scr->scr_line_wanted = scr->scr_line_wanted - where;
    999  1.14  macallan 		if (scr->scr_line_wanted < 0)
   1000  1.14  macallan 			scr->scr_line_wanted = 0;
   1001  1.14  macallan 		if (scr->scr_line_wanted > scr->scr_lines_in_buffer)
   1002  1.14  macallan 			scr->scr_line_wanted = scr->scr_lines_in_buffer;
   1003  1.14  macallan 	}
   1004  1.14  macallan 
   1005  1.14  macallan 	if (scr->scr_line_wanted != scr->scr_current_line) {
   1006  1.14  macallan 
   1007  1.14  macallan 		vcons_do_scroll(scr);
   1008  1.14  macallan 	}
   1009  1.14  macallan }
   1010  1.14  macallan 
   1011  1.14  macallan static void
   1012  1.14  macallan vcons_do_scroll(struct vcons_screen *scr)
   1013  1.14  macallan {
   1014  1.14  macallan 	int dist, from, to, num;
   1015  1.14  macallan 	int r_offset, r_start;
   1016  1.14  macallan 	int i, j;
   1017  1.14  macallan 
   1018  1.14  macallan 	if (scr->scr_line_wanted == scr->scr_current_line)
   1019  1.14  macallan 		return;
   1020  1.14  macallan 	dist = scr->scr_line_wanted - scr->scr_current_line;
   1021  1.14  macallan 	scr->scr_current_line = scr->scr_line_wanted;
   1022  1.14  macallan 	scr->scr_current_offset = scr->scr_ri.ri_cols *
   1023  1.14  macallan 	    (scr->scr_lines_in_buffer - scr->scr_current_line);
   1024  1.14  macallan 	if (abs(dist) >= scr->scr_ri.ri_rows) {
   1025  1.14  macallan 		vcons_redraw_screen(scr);
   1026  1.14  macallan 		return;
   1027  1.14  macallan 	}
   1028  1.14  macallan 	/* scroll and redraw only what we really have to */
   1029  1.14  macallan 	if (dist > 0) {
   1030  1.14  macallan 		/* we scroll down */
   1031  1.14  macallan 		from = 0;
   1032  1.14  macallan 		to = dist;
   1033  1.14  macallan 		num = scr->scr_ri.ri_rows - dist;
   1034  1.14  macallan 		/* now the redraw parameters */
   1035  1.14  macallan 		r_offset = scr->scr_current_offset;
   1036  1.14  macallan 		r_start = 0;
   1037  1.14  macallan 	} else {
   1038  1.14  macallan 		/* scrolling up */
   1039  1.14  macallan 		to = 0;
   1040  1.14  macallan 		from = -dist;
   1041  1.14  macallan 		num = scr->scr_ri.ri_rows + dist;
   1042  1.14  macallan 		r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols;
   1043  1.14  macallan 		r_start = num;
   1044  1.14  macallan 	}
   1045  1.14  macallan 	scr->scr_vd->copyrows(scr, from, to, num);
   1046  1.14  macallan 	for (i = 0; i < abs(dist); i++) {
   1047  1.14  macallan 		for (j = 0; j < scr->scr_ri.ri_cols; j++) {
   1048  1.14  macallan 			scr->scr_vd->putchar(scr, i + r_start, j,
   1049  1.14  macallan 			    scr->scr_chars[r_offset],
   1050  1.14  macallan 			    scr->scr_attrs[r_offset]);
   1051  1.14  macallan 			r_offset++;
   1052  1.14  macallan 		}
   1053  1.14  macallan 	}
   1054  1.14  macallan 
   1055  1.14  macallan 	if (scr->scr_line_wanted == 0) {
   1056  1.14  macallan 		/* this was a reset - need to draw the cursor */
   1057  1.14  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
   1058  1.14  macallan 		scr->scr_vd->cursor(scr, 1, scr->scr_ri.ri_crow,
   1059  1.14  macallan 		    scr->scr_ri.ri_ccol);
   1060  1.14  macallan 	}
   1061  1.14  macallan }
   1062  1.14  macallan 
   1063  1.14  macallan #endif /* WSDISPLAY_SCROLLSUPPORT */
   1064  1.14  macallan 
   1065  1.14  macallan /* async drawing using a kernel thread */
   1066  1.14  macallan 
   1067  1.19  macallan #ifdef VCONS_DRAW_ASYNC
   1068  1.19  macallan 
   1069  1.19  macallan static inline uint32_t
   1070  1.19  macallan vcons_words_in_buffer(struct vcons_data *vd)
   1071  1.19  macallan {
   1072  1.19  macallan 	int len = vd->rb_write - vd->rb_read;
   1073  1.19  macallan 
   1074  1.19  macallan 	if (len < 0) len += VCONS_RING_BUFFER_LENGTH;
   1075  1.19  macallan 	if (len < 0) vd->use_async = 0;
   1076  1.19  macallan 	if (len >= VCONS_RING_BUFFER_LENGTH) vd->use_async = 0;
   1077  1.19  macallan 	return (uint32_t)len;
   1078  1.19  macallan }
   1079  1.19  macallan 
   1080  1.19  macallan static inline int
   1081  1.19  macallan vcons_wait_buffer(struct vcons_data *vd, uint32_t words)
   1082  1.19  macallan {
   1083  1.19  macallan 	int bail = 0;
   1084  1.19  macallan 
   1085  1.19  macallan 	mutex_enter(&vd->go_buffer_il);
   1086  1.19  macallan 	while (((VCONS_RING_BUFFER_LENGTH - vcons_words_in_buffer(vd)) < words)
   1087  1.19  macallan 	    && (bail < 3)) {
   1088  1.19  macallan 		if (cv_timedwait(&vd->go_buffer, &vd->go_buffer_il, hz)
   1089  1.19  macallan 		    == EWOULDBLOCK)
   1090  1.19  macallan 			bail++;
   1091  1.19  macallan 	}
   1092  1.19  macallan 	if (bail >= 3) {
   1093  1.19  macallan 		/*
   1094  1.19  macallan 		 * waited too long, something is wrong so fall back to sync
   1095  1.19  macallan 		 * we should probably kill the kthread here and try to empty
   1096  1.19  macallan 		 * the command buffer as well
   1097  1.19  macallan 		 */
   1098  1.19  macallan 		vd->use_async = 0;
   1099  1.19  macallan 	}
   1100  1.19  macallan 	return 0;
   1101  1.19  macallan }
   1102  1.19  macallan 
   1103  1.19  macallan #define VRB_NEXT(idx) ((idx + 1) >= VCONS_RING_BUFFER_LENGTH) ? 0 : idx + 1
   1104  1.19  macallan 
   1105  1.19  macallan static void
   1106  1.19  macallan vcons_copycols_async(void *cookie, int row, int srccol, int dstcol, int ncols)
   1107  1.19  macallan {
   1108  1.19  macallan 	struct rasops_info *ri = cookie;
   1109  1.19  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1110  1.19  macallan 	struct vcons_data *vd = scr->scr_vd;
   1111  1.19  macallan 	int idx;
   1112  1.19  macallan 
   1113  1.19  macallan 	vcons_wait_buffer(vd, 5);
   1114  1.19  macallan 	mutex_enter(&vd->drawing_mutex);
   1115  1.19  macallan 	mutex_exit(&vd->go_buffer_il);
   1116  1.19  macallan 	idx = vd->rb_write;
   1117  1.19  macallan 	vd->rb_buffer[idx] = VCMD_COPYCOLS;
   1118  1.19  macallan 	idx = VRB_NEXT(idx);
   1119  1.19  macallan 	vd->rb_buffer[idx] = row;
   1120  1.19  macallan 	idx = VRB_NEXT(idx);
   1121  1.19  macallan 	vd->rb_buffer[idx] = srccol;
   1122  1.19  macallan 	idx = VRB_NEXT(idx);
   1123  1.19  macallan 	vd->rb_buffer[idx] = dstcol;
   1124  1.19  macallan 	idx = VRB_NEXT(idx);
   1125  1.19  macallan 	vd->rb_buffer[idx] = ncols;
   1126  1.19  macallan 	idx = VRB_NEXT(idx);
   1127  1.19  macallan 	membar_producer();
   1128  1.19  macallan 	vd->rb_write = idx;
   1129  1.19  macallan 	membar_enter();
   1130  1.19  macallan 	mutex_exit(&vd->drawing_mutex);
   1131  1.19  macallan 	cv_signal(&vd->go_draw);
   1132  1.19  macallan }
   1133  1.19  macallan 
   1134  1.19  macallan static void
   1135  1.19  macallan vcons_erasecols_async(void *cookie, int row, int startcol, int ncols,
   1136  1.19  macallan     long fillattr)
   1137  1.19  macallan {
   1138  1.19  macallan 	struct rasops_info *ri = cookie;
   1139  1.19  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1140  1.19  macallan 	struct vcons_data *vd = scr->scr_vd;
   1141  1.19  macallan 	int idx;
   1142  1.19  macallan 
   1143  1.19  macallan 	vcons_wait_buffer(vd, 5);
   1144  1.19  macallan 	mutex_enter(&vd->drawing_mutex);
   1145  1.19  macallan 	mutex_exit(&vd->go_buffer_il);
   1146  1.19  macallan 	idx = vd->rb_write;
   1147  1.19  macallan 	vd->rb_buffer[idx] = VCMD_ERASECOLS;
   1148  1.19  macallan 	idx = VRB_NEXT(idx);
   1149  1.19  macallan 	vd->rb_buffer[idx] = row;
   1150  1.19  macallan 	idx = VRB_NEXT(idx);
   1151  1.19  macallan 	vd->rb_buffer[idx] = startcol;
   1152  1.19  macallan 	idx = VRB_NEXT(idx);
   1153  1.19  macallan 	vd->rb_buffer[idx] = ncols;
   1154  1.19  macallan 	idx = VRB_NEXT(idx);
   1155  1.19  macallan 	/*
   1156  1.19  macallan 	 * XXX all drivers I've seen use 32bit attributes although fillattr is
   1157  1.19  macallan 	 * a 64bit value on LP64
   1158  1.19  macallan 	 */
   1159  1.19  macallan 	vd->rb_buffer[idx] = (uint32_t)fillattr;
   1160  1.19  macallan 	idx = VRB_NEXT(idx);
   1161  1.19  macallan 	membar_producer();
   1162  1.19  macallan 	vd->rb_write = idx;
   1163  1.19  macallan 	membar_enter();
   1164  1.19  macallan 	mutex_exit(&vd->drawing_mutex);
   1165  1.19  macallan 	cv_signal(&vd->go_draw);
   1166  1.19  macallan }
   1167  1.19  macallan 
   1168  1.19  macallan static void
   1169  1.19  macallan vcons_copyrows_async(void *cookie, int srcrow, int dstrow, int nrows)
   1170  1.19  macallan {
   1171  1.19  macallan 	struct rasops_info *ri = cookie;
   1172  1.19  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1173  1.19  macallan 	struct vcons_data *vd = scr->scr_vd;
   1174  1.19  macallan 	int idx;
   1175  1.19  macallan 
   1176  1.19  macallan 	vcons_wait_buffer(vd, 4);
   1177  1.19  macallan 	mutex_enter(&vd->drawing_mutex);
   1178  1.19  macallan 	mutex_exit(&vd->go_buffer_il);
   1179  1.19  macallan 	idx = vd->rb_write;
   1180  1.19  macallan 	vd->rb_buffer[idx] = VCMD_COPYROWS;
   1181  1.19  macallan 	idx = VRB_NEXT(idx);
   1182  1.19  macallan 	vd->rb_buffer[idx] = srcrow;
   1183  1.19  macallan 	idx = VRB_NEXT(idx);
   1184  1.19  macallan 	vd->rb_buffer[idx] = dstrow;
   1185  1.19  macallan 	idx = VRB_NEXT(idx);
   1186  1.19  macallan 	vd->rb_buffer[idx] = nrows;
   1187  1.19  macallan 	idx = VRB_NEXT(idx);
   1188  1.19  macallan 	membar_producer();
   1189  1.19  macallan 	vd->rb_write = idx;
   1190  1.19  macallan 	membar_enter();
   1191  1.19  macallan 	mutex_exit(&vd->drawing_mutex);
   1192  1.19  macallan 	cv_signal(&vd->go_draw);
   1193  1.19  macallan }
   1194  1.19  macallan 
   1195  1.19  macallan static void
   1196  1.19  macallan vcons_eraserows_async(void *cookie, int row, int nrows, long fillattr)
   1197  1.19  macallan {
   1198  1.19  macallan 	struct rasops_info *ri = cookie;
   1199  1.19  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1200  1.19  macallan 	struct vcons_data *vd = scr->scr_vd;
   1201  1.19  macallan 	int idx;
   1202  1.19  macallan 
   1203  1.19  macallan 	vcons_wait_buffer(vd, 4);
   1204  1.19  macallan 	mutex_enter(&vd->drawing_mutex);
   1205  1.19  macallan 	mutex_exit(&vd->go_buffer_il);
   1206  1.19  macallan 	idx = vd->rb_write;
   1207  1.19  macallan 	vd->rb_buffer[idx] = VCMD_ERASEROWS;
   1208  1.19  macallan 	idx = VRB_NEXT(idx);
   1209  1.19  macallan 	vd->rb_buffer[idx] = row;
   1210  1.19  macallan 	idx = VRB_NEXT(idx);
   1211  1.19  macallan 	vd->rb_buffer[idx] = nrows;
   1212  1.19  macallan 	idx = VRB_NEXT(idx);
   1213  1.19  macallan 	vd->rb_buffer[idx] = (uint32_t)fillattr;
   1214  1.19  macallan 	idx = VRB_NEXT(idx);
   1215  1.19  macallan 	membar_producer();
   1216  1.19  macallan 	vd->rb_write = idx;
   1217  1.19  macallan 	membar_enter();
   1218  1.19  macallan 	mutex_exit(&vd->drawing_mutex);
   1219  1.19  macallan 	cv_signal(&vd->go_draw);
   1220  1.19  macallan }
   1221  1.19  macallan 
   1222  1.19  macallan static void
   1223  1.19  macallan vcons_putchar_async(void *cookie, int row, int col, u_int c, long attr)
   1224  1.19  macallan {
   1225  1.19  macallan 	struct rasops_info *ri = cookie;
   1226  1.19  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1227  1.19  macallan 	struct vcons_data *vd = scr->scr_vd;
   1228  1.19  macallan 	int idx;
   1229  1.19  macallan 
   1230  1.19  macallan #ifdef VCONS_ASYNC_DEBUG
   1231  1.19  macallan 	/* mess with the background attribute so we can see if we draw async */
   1232  1.19  macallan 	attr &= 0xff00ffff;
   1233  1.19  macallan 	attr |= (WSCOL_LIGHT_BROWN << 16);
   1234  1.19  macallan #endif
   1235  1.19  macallan 
   1236  1.19  macallan 	vcons_wait_buffer(vd, 5);
   1237  1.19  macallan 	mutex_enter(&vd->drawing_mutex);
   1238  1.19  macallan 	mutex_exit(&vd->go_buffer_il);
   1239  1.19  macallan 	idx = vd->rb_write;
   1240  1.19  macallan 	vd->rb_buffer[idx] = VCMD_PUTCHAR;
   1241  1.19  macallan 	idx = VRB_NEXT(idx);
   1242  1.19  macallan 	vd->rb_buffer[idx] = row;
   1243  1.19  macallan 	idx = VRB_NEXT(idx);
   1244  1.19  macallan 	vd->rb_buffer[idx] = col;
   1245  1.19  macallan 	idx = VRB_NEXT(idx);
   1246  1.19  macallan 	vd->rb_buffer[idx] = c;
   1247  1.19  macallan 	idx = VRB_NEXT(idx);
   1248  1.19  macallan 	vd->rb_buffer[idx] = (uint32_t)attr;
   1249  1.19  macallan 	idx = VRB_NEXT(idx);
   1250  1.19  macallan 	membar_producer();
   1251  1.19  macallan 	vd->rb_write = idx;
   1252  1.19  macallan 	membar_enter();
   1253  1.19  macallan 	mutex_exit(&vd->drawing_mutex);
   1254  1.19  macallan 	cv_signal(&vd->go_draw);
   1255  1.19  macallan }
   1256  1.19  macallan 
   1257  1.19  macallan static void
   1258  1.19  macallan vcons_cursor_async(void *cookie, int on, int row, int col)
   1259  1.19  macallan {
   1260  1.19  macallan 	struct rasops_info *ri = cookie;
   1261  1.19  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1262  1.19  macallan 	struct vcons_data *vd = scr->scr_vd;
   1263  1.19  macallan 	int idx;
   1264  1.19  macallan 
   1265  1.19  macallan 	vcons_wait_buffer(vd, 4);
   1266  1.19  macallan 	mutex_enter(&vd->drawing_mutex);
   1267  1.19  macallan 	mutex_exit(&vd->go_buffer_il);
   1268  1.19  macallan 	idx = vd->rb_write;
   1269  1.19  macallan 	vd->rb_buffer[idx] = VCMD_CURSOR;
   1270  1.19  macallan 	idx = VRB_NEXT(idx);
   1271  1.19  macallan 	vd->rb_buffer[idx] = on;
   1272  1.19  macallan 	idx = VRB_NEXT(idx);
   1273  1.19  macallan 	vd->rb_buffer[idx] = row;
   1274  1.19  macallan 	idx = VRB_NEXT(idx);
   1275  1.19  macallan 	vd->rb_buffer[idx] = col;
   1276  1.19  macallan 	idx = VRB_NEXT(idx);
   1277  1.19  macallan 	membar_producer();
   1278  1.19  macallan 	vd->rb_write = idx;
   1279  1.19  macallan 	membar_enter();
   1280  1.19  macallan 	mutex_exit(&vd->drawing_mutex);
   1281  1.19  macallan 	cv_signal(&vd->go_draw);
   1282  1.19  macallan }
   1283  1.19  macallan 
   1284  1.19  macallan static int
   1285  1.19  macallan vcons_copy_params(struct vcons_data *vd, int len, uint32_t *buf)
   1286  1.19  macallan {
   1287  1.19  macallan 	int idx = vd->rb_read, i;
   1288  1.19  macallan 
   1289  1.19  macallan 	for (i = 0; i < len; i++) {
   1290  1.19  macallan 		buf[i] = vd->rb_buffer[idx];
   1291  1.19  macallan 		idx = VRB_NEXT(idx);
   1292  1.19  macallan 	}
   1293  1.19  macallan 	return idx;
   1294  1.19  macallan }
   1295  1.19  macallan 
   1296  1.19  macallan 
   1297  1.14  macallan static void
   1298  1.19  macallan vcons_process_command(struct vcons_data *vd)
   1299  1.14  macallan {
   1300  1.19  macallan 	/* we take a command out of the buffer, run it and return */
   1301  1.19  macallan 	void *cookie;
   1302  1.19  macallan 	int idx = vd->rb_read;
   1303  1.19  macallan 	uint32_t cmd = vd->rb_buffer[idx];
   1304  1.19  macallan 	uint32_t params[10];
   1305  1.14  macallan 
   1306  1.19  macallan 	KASSERT(vd->active != NULL);
   1307  1.19  macallan 	cookie = &vd->active->scr_ri;
   1308  1.14  macallan 
   1309  1.19  macallan 	switch (cmd) {
   1310  1.19  macallan 		case VCMD_COPYCOLS:
   1311  1.19  macallan 			idx = vcons_copy_params(vd, 5, params);
   1312  1.19  macallan 			vd->rb_read = idx;
   1313  1.19  macallan 			membar_producer();
   1314  1.19  macallan 			vd->copycols(cookie, params[1], params[2], params[3], params[4]);
   1315  1.19  macallan 			break;
   1316  1.19  macallan 		case VCMD_ERASECOLS:
   1317  1.19  macallan 			idx = vcons_copy_params(vd, 5, params);
   1318  1.19  macallan 			vd->rb_read = idx;
   1319  1.19  macallan 			membar_producer();
   1320  1.19  macallan 			vd->erasecols(cookie, params[1], params[2], params[3], params[4]);
   1321  1.19  macallan 			break;
   1322  1.19  macallan 		case VCMD_COPYROWS:
   1323  1.19  macallan 			idx = vcons_copy_params(vd, 4, params);
   1324  1.19  macallan 			vd->rb_read = idx;
   1325  1.19  macallan 			membar_producer();
   1326  1.19  macallan 			vd->copyrows(cookie, params[1], params[2], params[3]);
   1327  1.19  macallan 			break;
   1328  1.19  macallan 		case VCMD_ERASEROWS:
   1329  1.19  macallan 			idx = vcons_copy_params(vd, 4, params);
   1330  1.19  macallan 			vd->rb_read = idx;
   1331  1.19  macallan 			membar_producer();
   1332  1.19  macallan 			vd->eraserows(cookie, params[1], params[2], params[3]);
   1333  1.19  macallan 			break;
   1334  1.19  macallan 		case VCMD_PUTCHAR:
   1335  1.19  macallan 			idx = vcons_copy_params(vd, 5, params);
   1336  1.19  macallan 			vd->rb_read = idx;
   1337  1.19  macallan 			membar_producer();
   1338  1.19  macallan 			vd->putchar(cookie, params[1], params[2], params[3], params[4]);
   1339  1.19  macallan 			break;
   1340  1.19  macallan 		case VCMD_CURSOR:
   1341  1.19  macallan 			idx = vcons_copy_params(vd, 4, params);
   1342  1.19  macallan 			vd->rb_read = idx;
   1343  1.19  macallan 			membar_producer();
   1344  1.19  macallan 			vd->cursor(cookie, params[1], params[2], params[3]);
   1345  1.19  macallan 			break;
   1346  1.19  macallan 		default:
   1347  1.14  macallan 			/*
   1348  1.19  macallan 			 * invalid command, something is wrong so we fall back
   1349  1.19  macallan 			 * to synchronous operations
   1350  1.14  macallan 			 */
   1351  1.19  macallan 			vd->use_async = 0;
   1352  1.19  macallan 			vd->rb_read = 0;
   1353  1.19  macallan 			vd->rb_write = 0;
   1354  1.19  macallan 	}
   1355  1.19  macallan }
   1356  1.19  macallan 
   1357  1.19  macallan static void vcons_cursor(void *, int, int, int);
   1358  1.14  macallan 
   1359  1.19  macallan static void
   1360  1.19  macallan vcons_kthread(void *cookie)
   1361  1.19  macallan {
   1362  1.19  macallan 	struct vcons_data *vd = cookie;
   1363  1.14  macallan 
   1364  1.19  macallan 	/* initialize the synchronization goo */
   1365  1.19  macallan 	cv_init(&vd->go_draw, "go_draw");
   1366  1.19  macallan 	cv_init(&vd->go_buffer, "go_buffer");
   1367  1.19  macallan 	mutex_init(&vd->drawing_mutex, MUTEX_DEFAULT, IPL_NONE);
   1368  1.19  macallan 	mutex_init(&vd->go_draw_il, MUTEX_DEFAULT, IPL_NONE);
   1369  1.19  macallan 	mutex_init(&vd->go_buffer_il, MUTEX_DEFAULT, IPL_NONE);
   1370  1.19  macallan 	vd->rb_read = 1000;
   1371  1.19  macallan 	vd->rb_write = 2;
   1372  1.19  macallan 	printf("%d\n", vcons_words_in_buffer(vd));
   1373  1.19  macallan 	vd->rb_read = 0;
   1374  1.19  macallan 	vd->rb_write = 0;
   1375  1.19  macallan 	printf("%d\n", vcons_words_in_buffer(vd));
   1376  1.19  macallan 	printf("%d %d\n", VRB_NEXT(1), VRB_NEXT(1023));
   1377  1.19  macallan 	/* now we're good to go */
   1378  1.19  macallan 	vd->use_async = 1;
   1379  1.14  macallan 
   1380  1.19  macallan 	while (1) {
   1381  1.14  macallan 
   1382  1.19  macallan 		while (vcons_words_in_buffer(vd) > 0) {
   1383  1.19  macallan 			vcons_process_command(vd);
   1384  1.19  macallan 			cv_signal(&vd->go_buffer);
   1385  1.19  macallan 		}
   1386  1.19  macallan 		/*
   1387  1.19  macallan 		 * We don't really need the interlock here since there is no
   1388  1.19  macallan 		 * need for serializing access to the buffer - we're the only
   1389  1.19  macallan 		 * consumer. All we want is to sleep until someone gives us
   1390  1.19  macallan 		 * something to so.
   1391  1.19  macallan 		 */
   1392  1.19  macallan 		mutex_enter(&vd->go_draw_il);
   1393  1.19  macallan 		cv_timedwait(&vd->go_draw, &vd->go_draw_il, hz);
   1394  1.19  macallan 		mutex_exit(&vd->go_draw_il);
   1395  1.14  macallan 	}
   1396   1.1  macallan }
   1397  1.19  macallan #endif /* VCONS_DRAW_ASYNC */
   1398  1.20  jmcneill 
   1399  1.20  jmcneill #ifdef VCONS_DRAW_INTR
   1400  1.20  jmcneill static void
   1401  1.20  jmcneill vcons_intr(void *cookie)
   1402  1.20  jmcneill {
   1403  1.20  jmcneill 	struct vcons_data *vd = cookie;
   1404  1.20  jmcneill 
   1405  1.20  jmcneill 	workqueue_enqueue(vd->intr_wq, &vd->wk, NULL);
   1406  1.20  jmcneill }
   1407  1.20  jmcneill 
   1408  1.20  jmcneill static void
   1409  1.20  jmcneill vcons_intr_work(struct work *wk, void *cookie)
   1410  1.20  jmcneill {
   1411  1.20  jmcneill 	struct vcons_data *vd = cookie;
   1412  1.20  jmcneill 	struct vcons_screen *scr = vd->active;
   1413  1.20  jmcneill 
   1414  1.20  jmcneill 	if (scr) {
   1415  1.20  jmcneill 		if (!SCREEN_IS_BUSY(scr) && scr->scr_dirty > 0) {
   1416  1.20  jmcneill 			if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
   1417  1.20  jmcneill 				vcons_redraw_screen(scr);
   1418  1.20  jmcneill 			scr->scr_dirty = 0;
   1419  1.20  jmcneill 		}
   1420  1.20  jmcneill 	}
   1421  1.20  jmcneill 
   1422  1.20  jmcneill 	callout_schedule(&vd->intr, mstohz(33));
   1423  1.20  jmcneill }
   1424  1.20  jmcneill #endif /* VCONS_DRAW_INTR */
   1425