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