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