Home | History | Annotate | Line # | Download | only in pci
wcfb.c revision 1.12
      1 /*	$NetBSD: wcfb.c,v 1.12 2012/10/09 21:59:19 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 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: wcfb.c,v 1.12 2012/10/09 21:59:19 macallan Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/proc.h>
     37 #include <sys/mutex.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 #include <sys/kauth.h>
     42 #include <sys/kmem.h>
     43 
     44 #include <dev/pci/pcidevs.h>
     45 #include <dev/pci/pcireg.h>
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pciio.h>
     48 #include <dev/pci/wcfbreg.h>
     49 
     50 #include <dev/wscons/wsdisplayvar.h>
     51 #include <dev/wscons/wsconsio.h>
     52 #include <dev/wsfont/wsfont.h>
     53 #include <dev/rasops/rasops.h>
     54 #include <dev/wscons/wsdisplay_vconsvar.h>
     55 #include <dev/pci/wsdisplay_pci.h>
     56 
     57 #include "opt_wsfb.h"
     58 #include "opt_wsdisplay_compat.h"
     59 
     60 #ifdef WCFB_DEBUG
     61 # define DPRINTF printf
     62 #else
     63 # define DPRINTF while (0) printf
     64 #endif
     65 
     66 static int	wcfb_match(device_t, cfdata_t, void *);
     67 static void	wcfb_attach(device_t, device_t, void *);
     68 static int	wcfb_ioctl(void *, void *, u_long, void *, int,
     69 		    struct lwp *);
     70 static paddr_t	wcfb_mmap(void *, void *, off_t, int);
     71 
     72 struct wcfb_softc {
     73 	device_t sc_dev;
     74 
     75 	pci_chipset_tag_t sc_pc;
     76 	pcitag_t sc_pcitag;
     77 
     78 	bus_space_tag_t sc_memt;
     79 	bus_space_tag_t sc_regt, sc_wtft;
     80 	bus_space_tag_t sc_iot;
     81 
     82 	bus_space_handle_t sc_fbh, sc_wtfh;
     83 	bus_space_handle_t sc_regh;
     84 	bus_addr_t sc_fb, sc_reg, sc_wtf;
     85 	bus_size_t sc_fbsize, sc_regsize, sc_wtfsize;
     86 
     87 	int sc_width, sc_height, sc_stride;
     88 	int sc_locked;
     89 	uint8_t *sc_fbaddr, *sc_fb0, *sc_fb1, *sc_shadow;
     90 	struct vcons_screen sc_console_screen;
     91 	struct wsscreen_descr sc_defaultscreen_descr;
     92 	const struct wsscreen_descr *sc_screens[1];
     93 	struct wsscreen_list sc_screenlist;
     94 	struct vcons_data vd;
     95 	int sc_mode;
     96 	u_char sc_cmap_red[256];
     97 	u_char sc_cmap_green[256];
     98 	u_char sc_cmap_blue[256];
     99 	uint32_t sc_fb0off, sc_fb1off;
    100 
    101 	void (*copycols)(void *, int, int, int, int);
    102 	void (*erasecols)(void *, int, int, int, long);
    103 	void (*copyrows)(void *, int, int, int);
    104 	void (*eraserows)(void *, int, int, long);
    105 	void (*putchar)(void *, int, int, u_int, long);
    106 	void (*cursor)(void *, int, int, int);
    107 	int sc_is_jfb;
    108 };
    109 
    110 static void	wcfb_init_screen(void *, struct vcons_screen *, int, long *);
    111 
    112 CFATTACH_DECL_NEW(wcfb, sizeof(struct wcfb_softc),
    113     wcfb_match, wcfb_attach, NULL, NULL);
    114 
    115 struct wsdisplay_accessops wcfb_accessops = {
    116 	wcfb_ioctl,
    117 	wcfb_mmap,
    118 	NULL,	/* alloc_screen */
    119 	NULL,	/* free_screen */
    120 	NULL,	/* show_screen */
    121 	NULL, 	/* load_font */
    122 	NULL,	/* pollc */
    123 	NULL	/* scroll */
    124 };
    125 
    126 static void	wcfb_putchar(void *, int, int, u_int, long);
    127 static void	wcfb_cursor(void *, int, int, int);
    128 static void	wcfb_copycols(void *, int, int, int, int);
    129 static void	wcfb_erasecols(void *, int, int, int, long);
    130 static void	wcfb_copyrows(void *, int, int, int);
    131 static void	wcfb_eraserows(void *, int, int, long);
    132 
    133 static void	wcfb_acc_putchar(void *, int, int, u_int, long);
    134 static void	wcfb_acc_cursor(void *, int, int, int);
    135 static void	wcfb_acc_copycols(void *, int, int, int, int);
    136 static void	wcfb_acc_erasecols(void *, int, int, int, long);
    137 static void	wcfb_acc_copyrows(void *, int, int, int);
    138 static void	wcfb_acc_eraserows(void *, int, int, long);
    139 
    140 static void 	wcfb_putpalreg(struct wcfb_softc *, int, int, int, int);
    141 
    142 static void	wcfb_bitblt(struct wcfb_softc *, int, int, int, int, int,
    143 			int, uint32_t);
    144 static void	wcfb_rectfill(struct wcfb_softc *, int, int, int, int, int);
    145 static void	wcfb_rop_common(struct wcfb_softc *, bus_addr_t, int, int, int,
    146 			int, int, int, uint32_t, int32_t);
    147 static void	wcfb_rop_jfb(struct wcfb_softc *, int, int, int, int, int, int,
    148 			uint32_t, int32_t);
    149 static int	wcfb_rop_wait(struct wcfb_softc *);
    150 
    151 static int
    152 wcfb_match(device_t parent, cfdata_t match, void *aux)
    153 {
    154 	struct pci_attach_args *pa = aux;
    155 
    156 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3DLABS &&
    157 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_WILDCAT5110)
    158 		return 100;
    159 
    160 	return 0;
    161 }
    162 
    163 static void
    164 wcfb_attach(device_t parent, device_t self, void *aux)
    165 {
    166 	struct wcfb_softc *sc = device_private(self);
    167 	struct pci_attach_args *pa = aux;
    168 	struct rasops_info	*ri;
    169 	prop_dictionary_t	dict;
    170 	struct wsemuldisplaydev_attach_args aa;
    171 	int 			i, j;
    172 	uint32_t		reg;
    173 	unsigned long		defattr;
    174 	bool			is_console = 0;
    175 	void 			*wtf;
    176 	uint32_t		sub;
    177 
    178 	sc->sc_dev = self;
    179 	sc->putchar = NULL;
    180 	pci_aprint_devinfo(pa, NULL);
    181 
    182 	dict = device_properties(self);
    183 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    184 #ifndef WCFB_DEBUG
    185 	if (!is_console) return;
    186 #endif
    187 	sc->sc_memt = pa->pa_memt;
    188 	sc->sc_iot = pa->pa_iot;
    189 	sc->sc_pc = pa->pa_pc;
    190 	sc->sc_pcitag = pa->pa_tag;
    191 
    192 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
    193 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    194 		aprint_error("%s: failed to map registers.\n",
    195 		    device_xname(sc->sc_dev));
    196 	}
    197 
    198 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR,
    199 	    &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    200 		aprint_error("%s: failed to map framebuffer.\n",
    201 		    device_xname(sc->sc_dev));
    202 	}
    203 
    204 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_MEM, BUS_SPACE_MAP_LINEAR,
    205 	    &sc->sc_wtft, &sc->sc_wtfh, &sc->sc_wtf, &sc->sc_wtfsize)) {
    206 		aprint_error("%s: failed to map wtf.\n",
    207 		    device_xname(sc->sc_dev));
    208 	}
    209 	wtf = bus_space_vaddr(sc->sc_wtft, sc->sc_wtfh);
    210 	memset(wtf, 0, 0x100000);
    211 
    212 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    213 
    214 	sc->sc_fb0off =
    215 	    bus_space_read_4(sc->sc_regt, sc->sc_regh,
    216 	        WC_FB8_ADDR0) - sc->sc_fb;
    217 	sc->sc_fb0 = sc->sc_fbaddr + sc->sc_fb0off;
    218 	sc->sc_fb1off =
    219 	    bus_space_read_4(sc->sc_regt, sc->sc_regh,
    220 	        WC_FB8_ADDR1) - sc->sc_fb;
    221 	sc->sc_fb1 = sc->sc_fbaddr + sc->sc_fb1off;
    222 
    223 	sub = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_SUBSYS_ID_REG);
    224 	printf("subsys: %08x\n", sub);
    225 	switch (sub) {
    226 		case WC_XVR1200:
    227 			sc->sc_is_jfb = 1;
    228 			break;
    229 		default:
    230 			sc->sc_is_jfb = 0;
    231 	}
    232 
    233 	reg = bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_RESOLUTION);
    234 	sc->sc_height = (reg >> 16) + 1;
    235 #ifdef WCFB_DEBUG
    236 	sc->sc_height -= 200;
    237 #endif
    238 	sc->sc_width = (reg & 0xffff) + 1;
    239 	sc->sc_stride = 1 <<
    240 	    ((bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_CONFIG) &
    241 	      0x00ff0000) >> 16);
    242 	printf("%s: %d x %d, %d\n", device_xname(sc->sc_dev),
    243 	    sc->sc_width, sc->sc_height, sc->sc_stride);
    244 
    245 	if (sc->sc_is_jfb == 0) {
    246 		sc->sc_shadow = kmem_alloc(sc->sc_stride * sc->sc_height,
    247 		    KM_SLEEP);
    248 		if (sc->sc_shadow == NULL) {
    249 			printf("%s: failed to allocate shadow buffer\n",
    250 			    device_xname(self));
    251 			return;
    252 		}
    253 	}
    254 
    255 	for (i = 0x40; i < 0x100; i += 16) {
    256 		printf("%04x:", i);
    257 		for (j = 0; j < 16; j += 4) {
    258 			printf(" %08x", bus_space_read_4(sc->sc_regt,
    259 			    sc->sc_regh, 0x8000 + i + j));
    260 		}
    261 		printf("\n");
    262 	}
    263 
    264 	/* make sure video output is on */
    265 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_DPMS_STATE, WC_DPMS_ON);
    266 
    267 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    268 		"default",
    269 		0, 0,
    270 		NULL,
    271 		8, 16,
    272 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    273 		NULL
    274 	};
    275 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    276 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    277 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    278 	sc->sc_locked = 0;
    279 
    280 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    281 	    &wcfb_accessops);
    282 	sc->vd.init_screen = wcfb_init_screen;
    283 
    284 	/* init engine here */
    285 #if 0
    286 	wcfb_init(sc);
    287 #endif
    288 
    289 	ri = &sc->sc_console_screen.scr_ri;
    290 
    291 	j = 0;
    292 	for (i = 0; i < 256; i++) {
    293 
    294 		sc->sc_cmap_red[i] = rasops_cmap[j];
    295 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    296 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    297 		wcfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    298 		    rasops_cmap[j + 2]);
    299 		j += 3;
    300 	}
    301 
    302 	if (is_console) {
    303 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    304 		    &defattr);
    305 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    306 
    307 		if (sc->sc_is_jfb) {
    308 			wcfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    309 				ri->ri_devcmap[(defattr >> 16) & 0xff]);
    310 		} else {
    311 			memset(sc->sc_fb0,
    312 			    ri->ri_devcmap[(defattr >> 16) & 0xff],
    313 			    sc->sc_stride * sc->sc_height);
    314 			memset(sc->sc_fb1,
    315 			    ri->ri_devcmap[(defattr >> 16) & 0xff],
    316 			    sc->sc_stride * sc->sc_height);
    317 		}
    318 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    319 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    320 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    321 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    322 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    323 		    defattr);
    324 		vcons_replay_msgbuf(&sc->sc_console_screen);
    325 	} else {
    326 		/*
    327 		 * since we're not the console we can postpone the rest
    328 		 * until someone actually allocates a screen for us
    329 		 */
    330 		memset(sc->sc_fb0, WS_DEFAULT_BG,
    331 		    sc->sc_stride * sc->sc_height);
    332 		memset(sc->sc_fb1, WS_DEFAULT_BG,
    333 		    sc->sc_stride * sc->sc_height);
    334 		return;
    335 	}
    336 
    337 	aa.console = is_console;
    338 	aa.scrdata = &sc->sc_screenlist;
    339 	aa.accessops = &wcfb_accessops;
    340 	aa.accesscookie = &sc->vd;
    341 
    342 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    343 }
    344 
    345 static int
    346 wcfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    347     struct lwp *l)
    348 {
    349 	struct wcfb_softc *sc = v;
    350 
    351 	switch (cmd) {
    352 	case WSDISPLAYIO_GTYPE:
    353 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    354 		return 0;
    355 
    356 	/* PCI config read/write passthrough. */
    357 	case PCI_IOC_CFGREAD:
    358 	case PCI_IOC_CFGWRITE:
    359 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    360 		    cmd, data, flag, l);
    361 
    362 	case WSDISPLAYIO_GET_BUSID:
    363 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    364 		    sc->sc_pcitag, data);
    365 
    366 	case WSDISPLAYIO_SMODE: {
    367 		/*int new_mode = *(int*)data, i;*/
    368 		}
    369 		return 0;
    370 	}
    371 
    372 	return EPASSTHROUGH;
    373 }
    374 
    375 static paddr_t
    376 wcfb_mmap(void *v, void *vs, off_t offset, int prot)
    377 {
    378 	struct wcfb_softc *sc = v;
    379 
    380 	/* no point in allowing a wsfb map if we can't provide one */
    381 	/*
    382 	 * restrict all other mappings to processes with superuser privileges
    383 	 * or the kernel itself
    384 	 */
    385 	if (kauth_authorize_machdep(kauth_cred_get(),
    386 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    387 	    NULL, NULL, NULL, NULL) != 0) {
    388 		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
    389 		return -1;
    390 	}
    391 
    392 #ifdef WSFB_FAKE_VGA_FB
    393 	if ((offset >= 0xa0000) && (offset < 0xbffff)) {
    394 
    395 		return bus_space_mmap(sc->sc_memt, sc->sc_gen.sc_fboffset,
    396 		   offset - 0xa0000, prot, BUS_SPACE_MAP_LINEAR);
    397 	}
    398 #endif
    399 
    400 	return -1;
    401 }
    402 
    403 static void
    404 wcfb_init_screen(void *cookie, struct vcons_screen *scr,
    405     int existing, long *defattr)
    406 {
    407 	struct wcfb_softc *sc = cookie;
    408 	struct rasops_info *ri = &scr->scr_ri;
    409 
    410 	ri->ri_depth = 8;
    411 	ri->ri_width = sc->sc_width;
    412 	ri->ri_height = sc->sc_height;
    413 	ri->ri_stride = sc->sc_stride;
    414 	ri->ri_flg = RI_CENTER /*| RI_FULLCLEAR*/;
    415 
    416 	if (sc->sc_is_jfb) {
    417 		ri->ri_bits = sc->sc_fb0;
    418 	} else {
    419 		ri->ri_bits = sc->sc_shadow;
    420 	}
    421 	if (existing) {
    422 		ri->ri_flg |= RI_CLEAR;
    423 	}
    424 
    425 	rasops_init(ri, 0, 0);
    426 	ri->ri_caps = WSSCREEN_WSCOLORS;
    427 
    428 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    429 		    sc->sc_width / ri->ri_font->fontwidth);
    430 
    431 	ri->ri_hw = scr;
    432 	sc->putchar = ri->ri_ops.putchar;
    433 	sc->copyrows = ri->ri_ops.copyrows;
    434 	sc->eraserows = ri->ri_ops.eraserows;
    435 	sc->copycols = ri->ri_ops.copycols;
    436 	sc->erasecols = ri->ri_ops.erasecols;
    437 
    438 	if (sc->sc_is_jfb) {
    439 		ri->ri_ops.copyrows = wcfb_acc_copyrows;
    440 		ri->ri_ops.copycols = wcfb_acc_copycols;
    441 		ri->ri_ops.eraserows = wcfb_acc_eraserows;
    442 		ri->ri_ops.erasecols = wcfb_acc_erasecols;
    443 		ri->ri_ops.putchar = wcfb_acc_putchar;
    444 		ri->ri_ops.cursor = wcfb_acc_cursor;
    445 	} else {
    446 		ri->ri_ops.copyrows = wcfb_copyrows;
    447 		ri->ri_ops.copycols = wcfb_copycols;
    448 		ri->ri_ops.eraserows = wcfb_eraserows;
    449 		ri->ri_ops.erasecols = wcfb_erasecols;
    450 		ri->ri_ops.putchar = wcfb_putchar;
    451 		ri->ri_ops.cursor = wcfb_cursor;
    452 	}
    453 }
    454 
    455 static void
    456 wcfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    457 {
    458 	struct rasops_info *ri = cookie;
    459 	struct vcons_screen *scr = ri->ri_hw;
    460 	struct wcfb_softc *sc = scr->scr_cookie;
    461 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    462 	    sc->sc_stride + ri->ri_xorigin + col * ri->ri_font->fontwidth;
    463 	uint8_t *from, *to0, *to1;
    464 	int i;
    465 
    466 	sc->putchar(ri, row, col, c, attr);
    467 	from = sc->sc_shadow + offset;
    468 	to0 = sc->sc_fb0 + offset;
    469 	to1 = sc->sc_fb1 + offset;
    470 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    471 		memcpy(to0, from, ri->ri_font->fontwidth);
    472 		memcpy(to1, from, ri->ri_font->fontwidth);
    473 		to0 += sc->sc_stride;
    474 		to1 += sc->sc_stride;
    475 		from += sc->sc_stride;
    476 	}
    477 }
    478 
    479 static void
    480 wcfb_putpalreg(struct wcfb_softc *sc, int i, int r, int g, int b)
    481 {
    482 	uint32_t rgb;
    483 
    484 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_CMAP_INDEX, i);
    485 	rgb = (b << 22) | (g << 12) | (r << 2);
    486 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_CMAP_DATA, rgb);
    487 }
    488 
    489 static void
    490 wcfb_cursor(void *cookie, int on, int row, int col)
    491 {
    492 	struct rasops_info *ri = cookie;
    493 	struct vcons_screen *scr = ri->ri_hw;
    494 	struct wcfb_softc *sc = scr->scr_cookie;
    495 	int coffset;
    496 
    497 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    498 
    499 		if (ri->ri_flg & RI_CURSOR) {
    500 			/* remove cursor */
    501 			coffset = ri->ri_ccol + (ri->ri_crow * ri->ri_cols);
    502 #ifdef WSDISPLAY_SCROLLSUPPORT
    503 			coffset += scr->scr_offset_to_zero;
    504 #endif
    505 			wcfb_putchar(cookie, ri->ri_crow,
    506 			    ri->ri_ccol, scr->scr_chars[coffset],
    507 			    scr->scr_attrs[coffset]);
    508 			ri->ri_flg &= ~RI_CURSOR;
    509 		}
    510 		ri->ri_crow = row;
    511 		ri->ri_ccol = col;
    512 		if (on) {
    513 			long attr, revattr;
    514 			coffset = col + (row * ri->ri_cols);
    515 #ifdef WSDISPLAY_SCROLLSUPPORT
    516 			coffset += scr->scr_offset_to_zero;
    517 #endif
    518 			attr = scr->scr_attrs[coffset];
    519 			revattr = attr ^ 0xffff0000;
    520 
    521 			wcfb_putchar(cookie, ri->ri_crow, ri->ri_ccol,
    522 			    scr->scr_chars[coffset], revattr);
    523 			ri->ri_flg |= RI_CURSOR;
    524 		}
    525 	} else {
    526 		ri->ri_crow = row;
    527 		ri->ri_ccol = col;
    528 		ri->ri_flg &= ~RI_CURSOR;
    529 	}
    530 }
    531 
    532 static void
    533 wcfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    534 {
    535 	struct rasops_info *ri = cookie;
    536 	struct vcons_screen *scr = ri->ri_hw;
    537 	struct wcfb_softc *sc = scr->scr_cookie;
    538 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    539 	    sc->sc_stride + ri->ri_xorigin + dstcol * ri->ri_font->fontwidth;
    540 	uint8_t *from, *to0, *to1;
    541 	int i;
    542 
    543 	sc->copycols(ri, row, srccol, dstcol, ncols);
    544 	from = sc->sc_shadow + offset;
    545 	to0 = sc->sc_fb0 + offset;
    546 	to1 = sc->sc_fb1 + offset;
    547 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    548 		memcpy(to0, from, ri->ri_font->fontwidth * ncols);
    549 		memcpy(to1, from, ri->ri_font->fontwidth * ncols);
    550 		to0 += sc->sc_stride;
    551 		to1 += sc->sc_stride;
    552 		from += sc->sc_stride;
    553 	}
    554 }
    555 
    556 static void
    557 wcfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    558 {
    559 	struct rasops_info *ri = cookie;
    560 	struct vcons_screen *scr = ri->ri_hw;
    561 	struct wcfb_softc *sc = scr->scr_cookie;
    562 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    563 	    sc->sc_stride + ri->ri_xorigin + startcol * ri->ri_font->fontwidth;
    564 	uint8_t *to0, *to1;
    565 	int i;
    566 
    567 	sc->erasecols(ri, row, startcol, ncols, fillattr);
    568 
    569 	to0 = sc->sc_fb0 + offset;
    570 	to1 = sc->sc_fb1 + offset;
    571 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    572 		memset(to0, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    573 		    ri->ri_font->fontwidth * ncols);
    574 		memset(to1, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    575 		    ri->ri_font->fontwidth * ncols);
    576 		to0 += sc->sc_stride;
    577 		to1 += sc->sc_stride;
    578 	}
    579 }
    580 
    581 static void
    582 wcfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    583 {
    584 	struct rasops_info *ri = cookie;
    585 	struct vcons_screen *scr = ri->ri_hw;
    586 	struct wcfb_softc *sc = scr->scr_cookie;
    587 	int offset = (ri->ri_yorigin + dstrow * ri->ri_font->fontheight) *
    588 	    sc->sc_stride + ri->ri_xorigin;
    589 	uint8_t *from, *to0, *to1;
    590 	int i;
    591 
    592 	sc->copyrows(ri, srcrow, dstrow, nrows);
    593 
    594 	from = sc->sc_shadow + offset;
    595 	to0 = sc->sc_fb0 + offset;
    596 	to1 = sc->sc_fb1 + offset;
    597 	for (i = 0; i < ri->ri_font->fontheight * nrows; i++) {
    598 		memcpy(to0, from, ri->ri_emuwidth);
    599 		memcpy(to1, from, ri->ri_emuwidth);
    600 		to0 += sc->sc_stride;
    601 		to1 += sc->sc_stride;
    602 		from += sc->sc_stride;
    603 	}
    604 }
    605 
    606 static void
    607 wcfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    608 {
    609 	struct rasops_info *ri = cookie;
    610 	struct vcons_screen *scr = ri->ri_hw;
    611 	struct wcfb_softc *sc = scr->scr_cookie;
    612 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    613 	    sc->sc_stride + ri->ri_xorigin;
    614 	uint8_t *to0, *to1;
    615 	int i;
    616 
    617 	sc->eraserows(ri, row, nrows, fillattr);
    618 
    619 	to0 = sc->sc_fb0 + offset;
    620 	to1 = sc->sc_fb1 + offset;
    621 	for (i = 0; i < ri->ri_font->fontheight * nrows; i++) {
    622 		memset(to0, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    623 		    ri->ri_emuwidth);
    624 		memset(to1, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    625 		    ri->ri_emuwidth);
    626 		to0 += sc->sc_stride;
    627 		to1 += sc->sc_stride;
    628 	}
    629 }
    630 
    631 static void
    632 wcfb_bitblt(struct wcfb_softc *sc, int sx, int sy, int dx, int dy, int w,
    633 		 int h, uint32_t rop)
    634 {
    635 	wcfb_rop_wait(sc);
    636 	wcfb_rop_jfb(sc, sx, sy, dx, dy, w, h, rop, 0xff);
    637 }
    638 
    639 static void
    640 wcfb_rectfill(struct wcfb_softc *sc, int x, int y, int w, int h, int bg)
    641 {
    642 	int32_t mask;
    643 
    644 	/* pixels to set... */
    645 	mask = 0xff & bg;
    646 	if (mask != 0) {
    647 		wcfb_rop_wait(sc);
    648 		wcfb_rop_jfb(sc, x, y, x, y, w, h, WC_ROP_SET, mask);
    649 	}
    650 
    651 	/* pixels to clear... */
    652 	mask = 0xff & ~bg;
    653 	if (mask != 0) {
    654 		wcfb_rop_wait(sc);
    655 		wcfb_rop_jfb(sc, x, y, x, y, w, h, WC_ROP_CLEAR, mask);
    656 	}
    657 }
    658 
    659 void
    660 wcfb_rop_common(struct wcfb_softc *sc, bus_addr_t reg, int sx, int sy,
    661     int dx, int dy, int w, int h, uint32_t rop, int32_t planemask)
    662 {
    663 	int dir = 0;
    664 
    665 	/*
    666 	 * Compute rop direction. This only really matters for
    667 	 * screen-to-screen copies.
    668 	 */
    669 	if (sy < dy /* && sy + h > dy */) {
    670 		sy += h - 1;
    671 		dy += h;
    672 		dir |= WC_BLT_DIR_BACKWARDS_Y;
    673 	}
    674 	if (sx < dx /* && sx + w > dx */) {
    675 		sx += w - 1;
    676 		dx += w;
    677 		dir |= WC_BLT_DIR_BACKWARDS_X;
    678 	}
    679 
    680 	/* Which one of those below is your magic number for today? */
    681 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x61000001);
    682 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    683 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x6301c080);
    684 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x80000000);
    685 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, rop);
    686 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, planemask);
    687 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    688 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x64000303);
    689 	/*
    690 	 * This value is a pixel offset within the destination area. It is
    691 	 * probably used to define complex polygon shapes, with the
    692 	 * last pixel in the list being back to (0,0).
    693 	 */
    694 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(0, 0));
    695 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    696 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x00030000);
    697 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x2200010d);
    698 
    699 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x33f01000 | dir);
    700 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(dx, dy));
    701 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(w, h));
    702 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(sx, sy));
    703 }
    704 
    705 
    706 static void
    707 wcfb_rop_jfb(struct wcfb_softc *sc, int sx, int sy, int dx, int dy,
    708              int w, int h, uint32_t rop, int32_t planemask)
    709 {
    710 	bus_addr_t reg = WC_JFB_ENGINE;
    711 	uint32_t spr, splr;
    712 
    713 #if 0
    714 	/*
    715 	 * Pick the current spr and splr values from the communication
    716 	 * area if possible.
    717 	 */
    718 	if (sc->sc_comm != NULL) {
    719 		spr = sc->sc_comm[IFB_SHARED_TERM8_SPR >> 2];
    720 		splr = sc->sc_comm[IFB_SHARED_TERM8_SPLR >> 2];
    721 	} else
    722 #endif
    723 	{
    724 		/* supposedly sane defaults */
    725 		spr = 0x54ff0303;
    726 		splr = 0x5c0000ff;
    727 	}
    728 
    729 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x00400016);
    730 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5b000002);
    731 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5a000000);
    732 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, spr);
    733 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, splr);
    734 
    735 	wcfb_rop_common(sc, reg, sx, sy, dx, dy, w, h, rop, planemask);
    736 
    737 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5a000001);
    738 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5b000001);
    739 }
    740 
    741 static int
    742 wcfb_rop_wait(struct wcfb_softc *sc)
    743 {
    744 	int i;
    745 
    746 	for (i = 1000000; i != 0; i--) {
    747 		if (bus_space_read_4(sc->sc_regt, sc->sc_regh,
    748 		    WC_STATUS) & WC_STATUS_DONE)
    749 			break;
    750 		delay(1);
    751 	}
    752 
    753 	return i;
    754 }
    755 
    756 static void
    757 wcfb_acc_putchar(void *cookie, int row, int col, u_int c, long attr)
    758 {
    759 	struct rasops_info *ri = cookie;
    760 	struct vcons_screen *scr = ri->ri_hw;
    761 	struct wcfb_softc *sc = scr->scr_cookie;
    762 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    763 	int x, y, wi, he;
    764 	uint32_t bg;
    765 
    766 	wi = font->fontwidth;
    767 	he = font->fontheight;
    768 	x = ri->ri_xorigin + col * wi;
    769 	y = ri->ri_yorigin + row * he;
    770 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    771 	if (c == 0x20) {
    772 		wcfb_rectfill(sc, x, y, wi, he, bg);
    773 		return;
    774 	}
    775 	/* we wait until the blitter is idle... */
    776 	wcfb_rop_wait(sc);
    777 	/* ... draw the character into buffer 0 ... */
    778 	sc->putchar(ri, row, col, c, attr);
    779 	/* ... and then blit it into buffer 1 */
    780 	wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_COPY);
    781 }
    782 
    783 static void
    784 wcfb_acc_cursor(void *cookie, int on, int row, int col)
    785 {
    786 	struct rasops_info *ri = cookie;
    787 	struct vcons_screen *scr = ri->ri_hw;
    788 	struct wcfb_softc *sc = scr->scr_cookie;
    789 	int x, y, wi, he;
    790 
    791 	wi = ri->ri_font->fontwidth;
    792 	he = ri->ri_font->fontheight;
    793 
    794 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    795 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    796 		y = ri->ri_crow * he + ri->ri_yorigin;
    797 		if (ri->ri_flg & RI_CURSOR) {
    798 			wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_XOR);
    799 			ri->ri_flg &= ~RI_CURSOR;
    800 		}
    801 		ri->ri_crow = row;
    802 		ri->ri_ccol = col;
    803 		if (on) {
    804 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    805 			y = ri->ri_crow * he + ri->ri_yorigin;
    806 			wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_XOR);
    807 			ri->ri_flg |= RI_CURSOR;
    808 		}
    809 	} else {
    810 		scr->scr_ri.ri_crow = row;
    811 		scr->scr_ri.ri_ccol = col;
    812 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    813 	}
    814 
    815 }
    816 
    817 static void
    818 wcfb_acc_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    819 {
    820 	struct rasops_info *ri = cookie;
    821 	struct vcons_screen *scr = ri->ri_hw;
    822 	struct wcfb_softc *sc = scr->scr_cookie;
    823 	int32_t xs, xd, y, width, height;
    824 
    825 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    826 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    827 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    828 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    829 		width = ri->ri_font->fontwidth * ncols;
    830 		height = ri->ri_font->fontheight;
    831 		wcfb_bitblt(sc, xs, y, xd, y, width, height, WC_ROP_COPY);
    832 	}
    833 }
    834 
    835 static void
    836 wcfb_acc_erasecols(void *cookie, int row, int startcol, int ncols,
    837 		long fillattr)
    838 {
    839 	struct rasops_info *ri = cookie;
    840 	struct vcons_screen *scr = ri->ri_hw;
    841 	struct wcfb_softc *sc = scr->scr_cookie;
    842 	int32_t x, y, width, height, fg, bg, ul;
    843 
    844 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    845 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    846 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    847 		width = ri->ri_font->fontwidth * ncols;
    848 		height = ri->ri_font->fontheight;
    849 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    850 
    851 		wcfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    852 	}
    853 }
    854 
    855 static void
    856 wcfb_acc_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    857 {
    858 	struct rasops_info *ri = cookie;
    859 	struct vcons_screen *scr = ri->ri_hw;
    860 	struct wcfb_softc *sc = scr->scr_cookie;
    861 	int32_t x, ys, yd, width, height;
    862 
    863 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    864 		x = ri->ri_xorigin;
    865 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    866 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    867 		width = ri->ri_emuwidth;
    868 		height = ri->ri_font->fontheight * nrows;
    869 		wcfb_bitblt(sc, x, ys, x, yd, width, height, WC_ROP_COPY);
    870 	}
    871 }
    872 
    873 static void
    874 wcfb_acc_eraserows(void *cookie, int row, int nrows, long fillattr)
    875 {
    876 	struct rasops_info *ri = cookie;
    877 	struct vcons_screen *scr = ri->ri_hw;
    878 	struct wcfb_softc *sc = scr->scr_cookie;
    879 	int32_t x, y, width, height, fg, bg, ul;
    880 
    881 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    882 		x = ri->ri_xorigin;
    883 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    884 		width = ri->ri_emuwidth;
    885 		height = ri->ri_font->fontheight * nrows;
    886 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    887 
    888 		wcfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    889 	}
    890 }
    891