Home | History | Annotate | Line # | Download | only in pci
wcfb.c revision 1.17
      1 /*	$NetBSD: wcfb.c,v 1.17 2017/03/24 21:28:03 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.17 2017/03/24 21:28:03 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;
     76 	bus_space_handle_t sc_regh;
     77 	bus_addr_t sc_fb, sc_reg;
     78 	bus_size_t sc_fbsize, sc_regsize;
     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, sc_dpms;
     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, sc_fb8size;
     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 	uint32_t		sub;
    169 
    170 	sc->sc_dev = self;
    171 	sc->putchar = NULL;
    172 	pci_aprint_devinfo(pa, NULL);
    173 
    174 	dict = device_properties(self);
    175 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    176 #ifndef WCFB_DEBUG
    177 	if (!is_console) return;
    178 #endif
    179 	sc->sc_memt = pa->pa_memt;
    180 	sc->sc_iot = pa->pa_iot;
    181 	sc->sc_pc = pa->pa_pc;
    182 	sc->sc_pcitag = pa->pa_tag;
    183 
    184 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
    185 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    186 		aprint_error("%s: failed to map registers.\n",
    187 		    device_xname(sc->sc_dev));
    188 	}
    189 
    190 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
    191 	    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
    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 	sc->sc_fbaddr = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    198 #ifdef DEBUG
    199 	memset(sc->sc_fbaddr, 0, sc->sc_fbsize);
    200 #endif
    201 	sc->sc_fb0off =
    202 	    bus_space_read_4(sc->sc_regt, sc->sc_regh,
    203 	        WC_FB8_ADDR0) - sc->sc_fb;
    204 	sc->sc_fb0 = sc->sc_fbaddr + sc->sc_fb0off;
    205 	sc->sc_fb1off =
    206 	    bus_space_read_4(sc->sc_regt, sc->sc_regh,
    207 	        WC_FB8_ADDR1) - sc->sc_fb;
    208 	sc->sc_fb1 = sc->sc_fbaddr + sc->sc_fb1off;
    209 	sc->sc_fb8size = 2 * (sc->sc_fb1off - sc->sc_fb0off);
    210 
    211 	sub = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_SUBSYS_ID_REG);
    212 	aprint_normal("subsys: %08x\n", sub);
    213 	switch (sub) {
    214 		case WC_XVR1200:
    215 			sc->sc_is_jfb = 1;
    216 			break;
    217 		default:
    218 			sc->sc_is_jfb = 0;
    219 	}
    220 
    221 	reg = bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_RESOLUTION);
    222 	sc->sc_height = (reg >> 16) + 1;
    223 #ifdef WCFB_DEBUG
    224 	sc->sc_height -= 200;
    225 #endif
    226 	sc->sc_width = (reg & 0xffff) + 1;
    227 	sc->sc_stride = 1 <<
    228 	    ((bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_CONFIG) &
    229 	      0x00ff0000) >> 16);
    230 	aprint_normal_dev(self, "%d x %d, %d\n",
    231 	    sc->sc_width, sc->sc_height, sc->sc_stride);
    232 
    233 	if (sc->sc_is_jfb == 0) {
    234 		sc->sc_shadow = kmem_alloc(sc->sc_stride * sc->sc_height,
    235 		    KM_SLEEP);
    236 		if (sc->sc_shadow == NULL) {
    237 			aprint_error_dev(self,
    238 			    "failed to allocate shadow buffer\n");
    239 			return;
    240 		}
    241 	}
    242 
    243 	for (i = 0x40; i < 0x100; i += 16) {
    244 		aprint_normal("%04x:", i);
    245 		for (j = 0; j < 16; j += 4) {
    246 			aprint_normal(" %08x", bus_space_read_4(sc->sc_regt,
    247 			    sc->sc_regh, 0x8000 + i + j));
    248 		}
    249 		aprint_normal("\n");
    250 	}
    251 
    252 	/* make sure video output is on */
    253 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_DPMS_STATE, WC_DPMS_ON);
    254 	sc->sc_dpms = WSDISPLAYIO_VIDEO_ON;
    255 
    256 #if 0
    257 	/* testing & debugging voodoo */
    258 	memset(sc->sc_fb0, 0x01, 0x00100000);
    259 	memset(sc->sc_fb1, 0x00, 0x00100000);
    260 	wcfb_rop_wait(sc);
    261 	wcfb_rop_jfb(sc, 0, 0, 0, 0, 600, 600, WC_ROP_SET, 0xffffffff);
    262 	wcfb_rop_wait(sc);
    263 	delay(4000000);
    264 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_FB8_ADDR1,
    265 	    bus_space_read_4(sc->sc_regt, sc->sc_regh, WC_FB8_ADDR0));
    266 	delay(8000000);
    267 #endif
    268 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    269 		"default",
    270 		0, 0,
    271 		NULL,
    272 		8, 16,
    273 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    274 		NULL
    275 	};
    276 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    277 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    278 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    279 	sc->sc_locked = 0;
    280 
    281 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    282 	    &wcfb_accessops);
    283 	sc->vd.init_screen = wcfb_init_screen;
    284 
    285 	/* init engine here */
    286 #if 0
    287 	wcfb_init(sc);
    288 #endif
    289 
    290 	ri = &sc->sc_console_screen.scr_ri;
    291 
    292 	j = 0;
    293 	for (i = 0; i < 256; i++) {
    294 
    295 		sc->sc_cmap_red[i] = rasops_cmap[j];
    296 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    297 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    298 		wcfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    299 		    rasops_cmap[j + 2]);
    300 		j += 3;
    301 	}
    302 
    303 	if (is_console) {
    304 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    305 		    &defattr);
    306 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    307 
    308 		if (sc->sc_is_jfb) {
    309 			wcfb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    310 				ri->ri_devcmap[(defattr >> 16) & 0xff]);
    311 		} else {
    312 			memset(sc->sc_fb0,
    313 			    ri->ri_devcmap[(defattr >> 16) & 0xff],
    314 			    sc->sc_stride * sc->sc_height);
    315 			memset(sc->sc_fb1,
    316 			    ri->ri_devcmap[(defattr >> 16) & 0xff],
    317 			    sc->sc_stride * sc->sc_height);
    318 		}
    319 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    320 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    321 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    322 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    323 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    324 		    defattr);
    325 		vcons_replay_msgbuf(&sc->sc_console_screen);
    326 	} else {
    327 		/*
    328 		 * since we're not the console we can postpone the rest
    329 		 * until someone actually allocates a screen for us
    330 		 */
    331 		memset(sc->sc_fb0, WS_DEFAULT_BG,
    332 		    sc->sc_stride * sc->sc_height);
    333 		memset(sc->sc_fb1, WS_DEFAULT_BG,
    334 		    sc->sc_stride * sc->sc_height);
    335 		return;
    336 	}
    337 
    338 	aa.console = is_console;
    339 	aa.scrdata = &sc->sc_screenlist;
    340 	aa.accessops = &wcfb_accessops;
    341 	aa.accesscookie = &sc->vd;
    342 
    343 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    344 }
    345 
    346 static int
    347 wcfb_putcmap(struct wcfb_softc *sc, struct wsdisplay_cmap *cm)
    348 {
    349 	u_char *r, *g, *b;
    350 	u_int index = cm->index;
    351 	u_int count = cm->count;
    352 	int i, error;
    353 	u_char rbuf[256], gbuf[256], bbuf[256];
    354 
    355 	if (cm->index >= 256 || cm->count > 256 ||
    356 	    (cm->index + cm->count) > 256)
    357 		return EINVAL;
    358 	error = copyin(cm->red, &rbuf[index], count);
    359 	if (error)
    360 		return error;
    361 	error = copyin(cm->green, &gbuf[index], count);
    362 	if (error)
    363 		return error;
    364 	error = copyin(cm->blue, &bbuf[index], count);
    365 	if (error)
    366 		return error;
    367 
    368 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    369 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    370 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    371 
    372 	r = &sc->sc_cmap_red[index];
    373 	g = &sc->sc_cmap_green[index];
    374 	b = &sc->sc_cmap_blue[index];
    375 
    376 	for (i = 0; i < count; i++) {
    377 		wcfb_putpalreg(sc, index, *r, *g, *b);
    378 		index++;
    379 		r++, g++, b++;
    380 	}
    381 	return 0;
    382 }
    383 
    384 static int
    385 wcfb_getcmap(struct wcfb_softc *sc, struct wsdisplay_cmap *cm)
    386 {
    387 	u_int index = cm->index;
    388 	u_int count = cm->count;
    389 	int error;
    390 
    391 	if (index >= 255 || count > 256 || index + count > 256)
    392 		return EINVAL;
    393 
    394 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    395 	if (error)
    396 		return error;
    397 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    398 	if (error)
    399 		return error;
    400 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    401 	if (error)
    402 		return error;
    403 
    404 	return 0;
    405 }
    406 
    407 static int
    408 wcfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    409     struct lwp *l)
    410 {
    411 	struct vcons_data *vd = v;
    412 	struct wcfb_softc *sc = vd->cookie;
    413 
    414 	switch (cmd) {
    415 	case WSDISPLAYIO_GTYPE:
    416 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    417 		return 0;
    418 
    419 	/* PCI config read/write passthrough. */
    420 	case PCI_IOC_CFGREAD:
    421 	case PCI_IOC_CFGWRITE:
    422 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    423 		    cmd, data, flag, l);
    424 
    425 	case WSDISPLAYIO_GET_BUSID:
    426 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    427 		    sc->sc_pcitag, data);
    428 
    429 	case WSDISPLAYIO_SVIDEO: {
    430 		int new_mode = *(int*)data;
    431 		if (new_mode != sc->sc_dpms) {
    432 			sc->sc_dpms = new_mode;
    433 			bus_space_write_4(sc->sc_regt, sc->sc_regh,
    434 			     WC_DPMS_STATE,
    435 			     (new_mode == WSDISPLAYIO_VIDEO_ON) ?
    436 			      WC_DPMS_ON : WC_DPMS_STANDBY);
    437 		}
    438 		}
    439 		return 0;
    440 
    441 	case WSDISPLAYIO_GVIDEO:
    442 		*(int*)data = sc->sc_dpms;
    443 		return 0;
    444 
    445 	case WSDISPLAYIO_GETCMAP:
    446 		return wcfb_getcmap(sc,
    447 		    (struct wsdisplay_cmap *)data);
    448 
    449 	case WSDISPLAYIO_PUTCMAP:
    450 		return wcfb_putcmap(sc,
    451 		    (struct wsdisplay_cmap *)data);
    452 
    453 	case WSDISPLAYIO_GET_FBINFO: {
    454 		struct wsdisplayio_fbinfo *fbi = data;
    455 
    456 		fbi->fbi_fbsize = sc->sc_fb8size;
    457 		fbi->fbi_fboffset = 0;
    458 		fbi->fbi_width = sc->sc_width;
    459 		fbi->fbi_height = sc->sc_height;
    460 		fbi->fbi_bitsperpixel = 8;
    461 		fbi->fbi_stride = sc->sc_stride;
    462 		fbi->fbi_pixeltype = WSFB_CI;
    463 		fbi->fbi_subtype.fbi_cmapinfo.cmap_entries = 256;
    464 		fbi->fbi_flags = WSFB_VRAM_IS_SPLIT;
    465 		return 0;
    466 		}
    467 	}
    468 	return EPASSTHROUGH;
    469 }
    470 
    471 static paddr_t
    472 wcfb_mmap(void *v, void *vs, off_t offset, int prot)
    473 {
    474 	struct vcons_data *vd = v;
    475 	struct wcfb_softc *sc = vd->cookie;
    476 
    477 	/* XXX in theory the order is not fixed... */
    478 
    479 	if (offset < sc->sc_fb8size)
    480 		return bus_space_mmap(sc->sc_memt, sc->sc_fb + sc->sc_fb0off,
    481 		    offset, prot,
    482 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    483 	/*
    484 	 * restrict all other mappings to processes with superuser privileges
    485 	 * or the kernel itself
    486 	 */
    487 	if (kauth_authorize_machdep(kauth_cred_get(),
    488 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    489 	    NULL, NULL, NULL, NULL) != 0) {
    490 		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
    491 		return -1;
    492 	}
    493 
    494 	/* may want to mmap() registers at some point */
    495 
    496 	return -1;
    497 }
    498 
    499 static void
    500 wcfb_init_screen(void *cookie, struct vcons_screen *scr,
    501     int existing, long *defattr)
    502 {
    503 	struct wcfb_softc *sc = cookie;
    504 	struct rasops_info *ri = &scr->scr_ri;
    505 
    506 	ri->ri_depth = 8;
    507 	ri->ri_width = sc->sc_width;
    508 	ri->ri_height = sc->sc_height;
    509 	ri->ri_stride = sc->sc_stride;
    510 	ri->ri_flg = RI_CENTER /*| RI_FULLCLEAR*/;
    511 
    512 	if (sc->sc_is_jfb) {
    513 		ri->ri_bits = sc->sc_fb0;
    514 	} else {
    515 		ri->ri_bits = sc->sc_shadow;
    516 	}
    517 	if (existing) {
    518 		ri->ri_flg |= RI_CLEAR;
    519 	}
    520 
    521 	rasops_init(ri, 0, 0);
    522 	ri->ri_caps = WSSCREEN_WSCOLORS;
    523 
    524 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    525 		    sc->sc_width / ri->ri_font->fontwidth);
    526 
    527 	ri->ri_hw = scr;
    528 	sc->putchar = ri->ri_ops.putchar;
    529 	sc->copyrows = ri->ri_ops.copyrows;
    530 	sc->eraserows = ri->ri_ops.eraserows;
    531 	sc->copycols = ri->ri_ops.copycols;
    532 	sc->erasecols = ri->ri_ops.erasecols;
    533 
    534 	if (sc->sc_is_jfb) {
    535 		ri->ri_ops.copyrows = wcfb_acc_copyrows;
    536 		ri->ri_ops.copycols = wcfb_acc_copycols;
    537 		ri->ri_ops.eraserows = wcfb_acc_eraserows;
    538 		ri->ri_ops.erasecols = wcfb_acc_erasecols;
    539 		ri->ri_ops.putchar = wcfb_acc_putchar;
    540 		ri->ri_ops.cursor = wcfb_acc_cursor;
    541 	} else {
    542 		ri->ri_ops.copyrows = wcfb_copyrows;
    543 		ri->ri_ops.copycols = wcfb_copycols;
    544 		ri->ri_ops.eraserows = wcfb_eraserows;
    545 		ri->ri_ops.erasecols = wcfb_erasecols;
    546 		ri->ri_ops.putchar = wcfb_putchar;
    547 		ri->ri_ops.cursor = wcfb_cursor;
    548 	}
    549 }
    550 
    551 static void
    552 wcfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    553 {
    554 	struct rasops_info *ri = cookie;
    555 	struct vcons_screen *scr = ri->ri_hw;
    556 	struct wcfb_softc *sc = scr->scr_cookie;
    557 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    558 	    sc->sc_stride + ri->ri_xorigin + col * ri->ri_font->fontwidth;
    559 	uint8_t *from, *to0, *to1;
    560 	int i;
    561 
    562 	sc->putchar(ri, row, col, c, attr);
    563 	from = sc->sc_shadow + offset;
    564 	to0 = sc->sc_fb0 + offset;
    565 	to1 = sc->sc_fb1 + offset;
    566 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    567 		memcpy(to0, from, ri->ri_font->fontwidth);
    568 		memcpy(to1, from, ri->ri_font->fontwidth);
    569 		to0 += sc->sc_stride;
    570 		to1 += sc->sc_stride;
    571 		from += sc->sc_stride;
    572 	}
    573 }
    574 
    575 static void
    576 wcfb_putpalreg(struct wcfb_softc *sc, int i, int r, int g, int b)
    577 {
    578 	uint32_t rgb;
    579 
    580 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_CMAP_INDEX, i);
    581 	rgb = (b << 22) | (g << 12) | (r << 2);
    582 	bus_space_write_4(sc->sc_regt, sc->sc_regh, WC_CMAP_DATA, rgb);
    583 }
    584 
    585 static void
    586 wcfb_cursor(void *cookie, int on, int row, int col)
    587 {
    588 	struct rasops_info *ri = cookie;
    589 	struct vcons_screen *scr = ri->ri_hw;
    590 	struct wcfb_softc *sc = scr->scr_cookie;
    591 	int coffset;
    592 
    593 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    594 
    595 		if (ri->ri_flg & RI_CURSOR) {
    596 			/* remove cursor */
    597 			coffset = ri->ri_ccol + (ri->ri_crow * ri->ri_cols);
    598 #ifdef WSDISPLAY_SCROLLSUPPORT
    599 			coffset += scr->scr_offset_to_zero;
    600 #endif
    601 			wcfb_putchar(cookie, ri->ri_crow,
    602 			    ri->ri_ccol, scr->scr_chars[coffset],
    603 			    scr->scr_attrs[coffset]);
    604 			ri->ri_flg &= ~RI_CURSOR;
    605 		}
    606 		ri->ri_crow = row;
    607 		ri->ri_ccol = col;
    608 		if (on) {
    609 			long attr, revattr;
    610 			coffset = col + (row * ri->ri_cols);
    611 #ifdef WSDISPLAY_SCROLLSUPPORT
    612 			coffset += scr->scr_offset_to_zero;
    613 #endif
    614 			attr = scr->scr_attrs[coffset];
    615 			revattr = attr ^ 0xffff0000;
    616 
    617 			wcfb_putchar(cookie, ri->ri_crow, ri->ri_ccol,
    618 			    scr->scr_chars[coffset], revattr);
    619 			ri->ri_flg |= RI_CURSOR;
    620 		}
    621 	} else {
    622 		ri->ri_crow = row;
    623 		ri->ri_ccol = col;
    624 		ri->ri_flg &= ~RI_CURSOR;
    625 	}
    626 }
    627 
    628 static void
    629 wcfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    630 {
    631 	struct rasops_info *ri = cookie;
    632 	struct vcons_screen *scr = ri->ri_hw;
    633 	struct wcfb_softc *sc = scr->scr_cookie;
    634 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    635 	    sc->sc_stride + ri->ri_xorigin + dstcol * ri->ri_font->fontwidth;
    636 	uint8_t *from, *to0, *to1;
    637 	int i;
    638 
    639 	sc->copycols(ri, row, srccol, dstcol, ncols);
    640 	from = sc->sc_shadow + offset;
    641 	to0 = sc->sc_fb0 + offset;
    642 	to1 = sc->sc_fb1 + offset;
    643 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    644 		memcpy(to0, from, ri->ri_font->fontwidth * ncols);
    645 		memcpy(to1, from, ri->ri_font->fontwidth * ncols);
    646 		to0 += sc->sc_stride;
    647 		to1 += sc->sc_stride;
    648 		from += sc->sc_stride;
    649 	}
    650 }
    651 
    652 static void
    653 wcfb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    654 {
    655 	struct rasops_info *ri = cookie;
    656 	struct vcons_screen *scr = ri->ri_hw;
    657 	struct wcfb_softc *sc = scr->scr_cookie;
    658 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    659 	    sc->sc_stride + ri->ri_xorigin + startcol * ri->ri_font->fontwidth;
    660 	uint8_t *to0, *to1;
    661 	int i;
    662 
    663 	sc->erasecols(ri, row, startcol, ncols, fillattr);
    664 
    665 	to0 = sc->sc_fb0 + offset;
    666 	to1 = sc->sc_fb1 + offset;
    667 	for (i = 0; i < ri->ri_font->fontheight; i++) {
    668 		memset(to0, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    669 		    ri->ri_font->fontwidth * ncols);
    670 		memset(to1, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    671 		    ri->ri_font->fontwidth * ncols);
    672 		to0 += sc->sc_stride;
    673 		to1 += sc->sc_stride;
    674 	}
    675 }
    676 
    677 static void
    678 wcfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    679 {
    680 	struct rasops_info *ri = cookie;
    681 	struct vcons_screen *scr = ri->ri_hw;
    682 	struct wcfb_softc *sc = scr->scr_cookie;
    683 	int offset = (ri->ri_yorigin + dstrow * ri->ri_font->fontheight) *
    684 	    sc->sc_stride + ri->ri_xorigin;
    685 	uint8_t *from, *to0, *to1;
    686 	int i;
    687 
    688 	sc->copyrows(ri, srcrow, dstrow, nrows);
    689 
    690 	from = sc->sc_shadow + offset;
    691 	to0 = sc->sc_fb0 + offset;
    692 	to1 = sc->sc_fb1 + offset;
    693 	for (i = 0; i < ri->ri_font->fontheight * nrows; i++) {
    694 		memcpy(to0, from, ri->ri_emuwidth);
    695 		memcpy(to1, from, ri->ri_emuwidth);
    696 		to0 += sc->sc_stride;
    697 		to1 += sc->sc_stride;
    698 		from += sc->sc_stride;
    699 	}
    700 }
    701 
    702 static void
    703 wcfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    704 {
    705 	struct rasops_info *ri = cookie;
    706 	struct vcons_screen *scr = ri->ri_hw;
    707 	struct wcfb_softc *sc = scr->scr_cookie;
    708 	int offset = (ri->ri_yorigin + row * ri->ri_font->fontheight) *
    709 	    sc->sc_stride + ri->ri_xorigin;
    710 	uint8_t *to0, *to1;
    711 	int i;
    712 
    713 	sc->eraserows(ri, row, nrows, fillattr);
    714 
    715 	to0 = sc->sc_fb0 + offset;
    716 	to1 = sc->sc_fb1 + offset;
    717 	for (i = 0; i < ri->ri_font->fontheight * nrows; i++) {
    718 		memset(to0, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    719 		    ri->ri_emuwidth);
    720 		memset(to1, ri->ri_devcmap[(fillattr >> 16) & 0xff],
    721 		    ri->ri_emuwidth);
    722 		to0 += sc->sc_stride;
    723 		to1 += sc->sc_stride;
    724 	}
    725 }
    726 
    727 static void
    728 wcfb_bitblt(struct wcfb_softc *sc, int sx, int sy, int dx, int dy, int w,
    729 		 int h, uint32_t rop)
    730 {
    731 	wcfb_rop_wait(sc);
    732 	wcfb_rop_jfb(sc, sx, sy, dx, dy, w, h, rop, 0x0f);
    733 }
    734 
    735 static void
    736 wcfb_rectfill(struct wcfb_softc *sc, int x, int y, int w, int h, int bg)
    737 {
    738 	int32_t mask;
    739 
    740 	/* clear everything just in case... */
    741 	wcfb_rop_wait(sc);
    742 	wcfb_rop_jfb(sc, x, y, x, y, w, h, WC_ROP_CLEAR, 0xffffffff);
    743 
    744 	/* pixels to set... */
    745 	mask = 0x0f & bg;
    746 	if (mask != 0) {
    747 		wcfb_rop_wait(sc);
    748 		wcfb_rop_jfb(sc, x, y, x, y, w, h, WC_ROP_SET, mask);
    749 	}
    750 
    751 }
    752 
    753 void
    754 wcfb_rop_common(struct wcfb_softc *sc, bus_addr_t reg, int sx, int sy,
    755     int dx, int dy, int w, int h, uint32_t rop, int32_t planemask)
    756 {
    757 	int dir = 0;
    758 
    759 	/*
    760 	 * Compute rop direction. This only really matters for
    761 	 * screen-to-screen copies.
    762 	 */
    763 	if (sy < dy /* && sy + h > dy */) {
    764 		sy += h - 1;
    765 		dy += h;
    766 		dir |= WC_BLT_DIR_BACKWARDS_Y;
    767 	}
    768 	if (sx < dx /* && sx + w > dx */) {
    769 		sx += w - 1;
    770 		dx += w;
    771 		dir |= WC_BLT_DIR_BACKWARDS_X;
    772 	}
    773 
    774 	/* Which one of those below is your magic number for today? */
    775 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x61000001);
    776 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    777 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x6301c080);
    778 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x80000000);
    779 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, rop);
    780 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, planemask);
    781 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    782 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x64000303);
    783 	/*
    784 	 * This value is a pixel offset within the destination area. It is
    785 	 * probably used to define complex polygon shapes, with the
    786 	 * last pixel in the list being back to (0,0).
    787 	 */
    788 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(0, 0));
    789 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0);
    790 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x00030000);
    791 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x2200010d);
    792 
    793 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x33f01000 | dir);
    794 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(dx, dy));
    795 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(w, h));
    796 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, WCFB_COORDS(sx, sy));
    797 }
    798 
    799 
    800 static void
    801 wcfb_rop_jfb(struct wcfb_softc *sc, int sx, int sy, int dx, int dy,
    802              int w, int h, uint32_t rop, int32_t planemask)
    803 {
    804 	bus_addr_t reg = WC_JFB_ENGINE;
    805 	uint32_t spr, splr;
    806 
    807 #if 0
    808 	/*
    809 	 * Pick the current spr and splr values from the communication
    810 	 * area if possible.
    811 	 */
    812 	if (sc->sc_comm != NULL) {
    813 		spr = sc->sc_comm[IFB_SHARED_TERM8_SPR >> 2];
    814 		splr = sc->sc_comm[IFB_SHARED_TERM8_SPLR >> 2];
    815 	} else
    816 #endif
    817 	{
    818 		/* supposedly sane defaults */
    819 		spr = 0x54ff0303;
    820 		splr = 0x5c0000ff;
    821 	}
    822 
    823 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x00400016);
    824 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5b000002);
    825 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5a000000);
    826 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, spr);
    827 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, splr);
    828 
    829 	wcfb_rop_common(sc, reg, sx, sy, dx, dy, w, h, rop, planemask);
    830 
    831 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5a000001);
    832 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, 0x5b000001);
    833 }
    834 
    835 static int
    836 wcfb_rop_wait(struct wcfb_softc *sc)
    837 {
    838 	int i;
    839 
    840 	for (i = 1000000; i != 0; i--) {
    841 		if (bus_space_read_4(sc->sc_regt, sc->sc_regh,
    842 		    WC_STATUS) & WC_STATUS_DONE)
    843 			break;
    844 		delay(1);
    845 	}
    846 
    847 	return i;
    848 }
    849 
    850 static void
    851 wcfb_acc_putchar(void *cookie, int row, int col, u_int c, long attr)
    852 {
    853 	struct rasops_info *ri = cookie;
    854 	struct vcons_screen *scr = ri->ri_hw;
    855 	struct wcfb_softc *sc = scr->scr_cookie;
    856 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    857 	int x, y, wi, he;
    858 	uint32_t bg;
    859 
    860 	wi = font->fontwidth;
    861 	he = font->fontheight;
    862 	x = ri->ri_xorigin + col * wi;
    863 	y = ri->ri_yorigin + row * he;
    864 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    865 	if (c == 0x20) {
    866 		wcfb_rectfill(sc, x, y, wi, he, bg);
    867 		return;
    868 	}
    869 	/* we wait until the blitter is idle... */
    870 	wcfb_rop_wait(sc);
    871 	/* ... draw the character into buffer 0 ... */
    872 	sc->putchar(ri, row, col, c, attr);
    873 	/* ... and then blit it into buffer 1 */
    874 	wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_COPY);
    875 }
    876 
    877 static void
    878 wcfb_acc_cursor(void *cookie, int on, int row, int col)
    879 {
    880 	struct rasops_info *ri = cookie;
    881 	struct vcons_screen *scr = ri->ri_hw;
    882 	struct wcfb_softc *sc = scr->scr_cookie;
    883 	int x, y, wi, he;
    884 
    885 	wi = ri->ri_font->fontwidth;
    886 	he = ri->ri_font->fontheight;
    887 
    888 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    889 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    890 		y = ri->ri_crow * he + ri->ri_yorigin;
    891 		if (ri->ri_flg & RI_CURSOR) {
    892 			wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_XOR);
    893 			ri->ri_flg &= ~RI_CURSOR;
    894 		}
    895 		ri->ri_crow = row;
    896 		ri->ri_ccol = col;
    897 		if (on) {
    898 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    899 			y = ri->ri_crow * he + ri->ri_yorigin;
    900 			wcfb_bitblt(sc, x, y, x, y, wi, he, WC_ROP_XOR);
    901 			ri->ri_flg |= RI_CURSOR;
    902 		}
    903 	} else {
    904 		scr->scr_ri.ri_crow = row;
    905 		scr->scr_ri.ri_ccol = col;
    906 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    907 	}
    908 
    909 }
    910 
    911 static void
    912 wcfb_acc_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    913 {
    914 	struct rasops_info *ri = cookie;
    915 	struct vcons_screen *scr = ri->ri_hw;
    916 	struct wcfb_softc *sc = scr->scr_cookie;
    917 	int32_t xs, xd, y, width, height;
    918 
    919 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    920 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    921 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    922 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    923 		width = ri->ri_font->fontwidth * ncols;
    924 		height = ri->ri_font->fontheight;
    925 		wcfb_bitblt(sc, xs, y, xd, y, width, height, WC_ROP_COPY);
    926 	}
    927 }
    928 
    929 static void
    930 wcfb_acc_erasecols(void *cookie, int row, int startcol, int ncols,
    931 		long fillattr)
    932 {
    933 	struct rasops_info *ri = cookie;
    934 	struct vcons_screen *scr = ri->ri_hw;
    935 	struct wcfb_softc *sc = scr->scr_cookie;
    936 	int32_t x, y, width, height, fg, bg, ul;
    937 
    938 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    939 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    940 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    941 		width = ri->ri_font->fontwidth * ncols;
    942 		height = ri->ri_font->fontheight;
    943 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    944 
    945 		wcfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    946 	}
    947 }
    948 
    949 static void
    950 wcfb_acc_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    951 {
    952 	struct rasops_info *ri = cookie;
    953 	struct vcons_screen *scr = ri->ri_hw;
    954 	struct wcfb_softc *sc = scr->scr_cookie;
    955 	int32_t x, ys, yd, width, height;
    956 
    957 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    958 		x = ri->ri_xorigin;
    959 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    960 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    961 		width = ri->ri_emuwidth;
    962 		height = ri->ri_font->fontheight * nrows;
    963 		wcfb_bitblt(sc, x, ys, x, yd, width, height, WC_ROP_COPY);
    964 	}
    965 }
    966 
    967 static void
    968 wcfb_acc_eraserows(void *cookie, int row, int nrows, long fillattr)
    969 {
    970 	struct rasops_info *ri = cookie;
    971 	struct vcons_screen *scr = ri->ri_hw;
    972 	struct wcfb_softc *sc = scr->scr_cookie;
    973 	int32_t x, y, width, height, fg, bg, ul;
    974 
    975 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    976 		x = ri->ri_xorigin;
    977 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    978 		width = ri->ri_emuwidth;
    979 		height = ri->ri_font->fontheight * nrows;
    980 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    981 
    982 		wcfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    983 	}
    984 }
    985