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