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