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