Home | History | Annotate | Line # | Download | only in pci
gffb.c revision 1.5
      1 /*	$NetBSD: gffb.c,v 1.5 2013/10/09 17:18:23 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007, 2012 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * A console driver for nvidia geforce graphics controllers
     30  * tested on macppc only so far
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: gffb.c,v 1.5 2013/10/09 17:18:23 macallan Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/lwp.h>
     42 #include <sys/kauth.h>
     43 #include <sys/atomic.h>
     44 
     45 #include <dev/videomode/videomode.h>
     46 
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcidevs.h>
     50 #include <dev/pci/pciio.h>
     51 #include <dev/pci/gffbreg.h>
     52 
     53 #include <dev/wscons/wsdisplayvar.h>
     54 #include <dev/wscons/wsconsio.h>
     55 #include <dev/wsfont/wsfont.h>
     56 #include <dev/rasops/rasops.h>
     57 #include <dev/wscons/wsdisplay_vconsvar.h>
     58 #include <dev/pci/wsdisplay_pci.h>
     59 #include <dev/wscons/wsdisplay_glyphcachevar.h>
     60 
     61 #include <dev/i2c/i2cvar.h>
     62 
     63 #include "opt_gffb.h"
     64 #include "opt_vcons.h"
     65 
     66 #ifdef GFFB_DEBUG
     67 #define DPRINTF printf
     68 #else
     69 #define DPRINTF while(0) printf
     70 #endif
     71 
     72 struct gffb_softc {
     73 	device_t sc_dev;
     74 
     75 	pci_chipset_tag_t sc_pc;
     76 	pcitag_t sc_pcitag;
     77 
     78 	bus_space_tag_t sc_memt;
     79 	bus_space_tag_t sc_iot;
     80 
     81 	bus_space_handle_t sc_regh, sc_fbh;
     82 	bus_addr_t sc_fb, sc_reg;
     83 	bus_size_t sc_fbsize, sc_regsize;
     84 	uint8_t *sc_fbaddr;
     85 	size_t sc_vramsize;
     86 
     87 	int sc_width, sc_height, sc_depth, sc_stride;
     88 	int sc_locked;
     89 	struct vcons_screen sc_console_screen;
     90 	struct wsscreen_descr sc_defaultscreen_descr;
     91 	const struct wsscreen_descr *sc_screens[1];
     92 	struct wsscreen_list sc_screenlist;
     93 	struct vcons_data vd;
     94 	int sc_mode;
     95 	u_char sc_cmap_red[256];
     96 	u_char sc_cmap_green[256];
     97 	u_char sc_cmap_blue[256];
     98 	int sc_put, sc_current, sc_free;
     99 	uint32_t sc_rop;
    100 	void (*sc_putchar)(void *, int, int, u_int, long);
    101 	kmutex_t sc_lock;
    102 	glyphcache sc_gc;
    103 };
    104 
    105 static int	gffb_match(device_t, cfdata_t, void *);
    106 static void	gffb_attach(device_t, device_t, void *);
    107 
    108 CFATTACH_DECL_NEW(gffb, sizeof(struct gffb_softc),
    109     gffb_match, gffb_attach, NULL, NULL);
    110 
    111 static int	gffb_ioctl(void *, void *, u_long, void *, int,
    112 			     struct lwp *);
    113 static paddr_t	gffb_mmap(void *, void *, off_t, int);
    114 static void	gffb_init_screen(void *, struct vcons_screen *, int, long *);
    115 
    116 static int	gffb_putcmap(struct gffb_softc *, struct wsdisplay_cmap *);
    117 static int 	gffb_getcmap(struct gffb_softc *, struct wsdisplay_cmap *);
    118 static void	gffb_restore_palette(struct gffb_softc *);
    119 static int 	gffb_putpalreg(struct gffb_softc *, uint8_t, uint8_t,
    120 			    uint8_t, uint8_t);
    121 
    122 static void	gffb_init(struct gffb_softc *);
    123 
    124 static void	gffb_make_room(struct gffb_softc *, int);
    125 static void	gffb_sync(struct gffb_softc *);
    126 
    127 static void	gffb_rectfill(struct gffb_softc *, int, int, int, int,
    128 			    uint32_t);
    129 static void	gffb_bitblt(void *, int, int, int, int, int,
    130 			    int, int);
    131 static void	gffb_rop(struct gffb_softc *, int);
    132 
    133 static void	gffb_cursor(void *, int, int, int);
    134 static void	gffb_putchar(void *, int, int, u_int, long);
    135 static void	gffb_copycols(void *, int, int, int, int);
    136 static void	gffb_erasecols(void *, int, int, int, long);
    137 static void	gffb_copyrows(void *, int, int, int);
    138 static void	gffb_eraserows(void *, int, int, long);
    139 
    140 #define GFFB_READ_4(o) bus_space_read_4(sc->sc_memt, sc->sc_regh, (o))
    141 #define GFFB_WRITE_4(o, v) bus_space_write_4(sc->sc_memt, sc->sc_regh, (o), (v))
    142 
    143 struct wsdisplay_accessops gffb_accessops = {
    144 	gffb_ioctl,
    145 	gffb_mmap,
    146 	NULL,	/* alloc_screen */
    147 	NULL,	/* free_screen */
    148 	NULL,	/* show_screen */
    149 	NULL, 	/* load_font */
    150 	NULL,	/* pollc */
    151 	NULL	/* scroll */
    152 };
    153 
    154 static int
    155 gffb_match(device_t parent, cfdata_t match, void *aux)
    156 {
    157 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    158 
    159 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    160 		return 0;
    161 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA)
    162 		return 0;
    163 
    164 	/* only card tested on so far - likely need a list */
    165 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_GEFORCE2MX)
    166 		return 100;
    167 	return (0);
    168 }
    169 
    170 static void
    171 gffb_attach(device_t parent, device_t self, void *aux)
    172 {
    173 	struct gffb_softc	*sc = device_private(self);
    174 	struct pci_attach_args	*pa = aux;
    175 	struct rasops_info	*ri;
    176 	bus_space_tag_t		tag;
    177 	struct wsemuldisplaydev_attach_args aa;
    178 	prop_dictionary_t	dict;
    179 	unsigned long		defattr;
    180 	bool			is_console;
    181 	int			i, j;
    182 	uint8_t			cmap[768];
    183 
    184 	sc->sc_pc = pa->pa_pc;
    185 	sc->sc_pcitag = pa->pa_tag;
    186 	sc->sc_memt = pa->pa_memt;
    187 	sc->sc_iot = pa->pa_iot;
    188 	sc->sc_dev = self;
    189 
    190 	pci_aprint_devinfo(pa, NULL);
    191 
    192 	/* fill in parameters from properties */
    193 	dict = device_properties(self);
    194 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    195 		aprint_error("%s: no width property\n", device_xname(self));
    196 		return;
    197 	}
    198 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    199 		aprint_error("%s: no height property\n", device_xname(self));
    200 		return;
    201 	}
    202 
    203 #ifdef GLYPHCACHE_DEBUG
    204 	/* leave some visible VRAM unused so we can see the glyph cache */
    205 	sc->sc_height -= 300;
    206 #endif
    207 
    208 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    209 		aprint_error("%s: no depth property\n", device_xname(self));
    210 		return;
    211 	}
    212 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride)) {
    213 		aprint_error("%s: no linebytes property\n",
    214 		    device_xname(self));
    215 		return;
    216 	}
    217 
    218 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    219 
    220 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM,
    221 	    BUS_SPACE_MAP_PREFETCHABLE | BUS_SPACE_MAP_LINEAR,
    222 	    &tag, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    223 		aprint_error("%s: failed to map the framebuffer.\n",
    224 		    device_xname(sc->sc_dev));
    225 	}
    226 	sc->sc_fbaddr = bus_space_vaddr(tag, sc->sc_fbh);
    227 
    228 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    229 	    &tag, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    230 		aprint_error("%s: failed to map registers.\n",
    231 		    device_xname(sc->sc_dev));
    232 	}
    233 
    234 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    235 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    236 
    237 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    238 		"default",
    239 		0, 0,
    240 		NULL,
    241 		8, 16,
    242 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    243 		NULL
    244 	};
    245 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    246 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    247 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    248 	sc->sc_locked = 0;
    249 
    250 	sc->sc_vramsize = GFFB_READ_4(GFFB_VRAM) & 0xfff00000;
    251 
    252 	aprint_normal_dev(sc->sc_dev, "%d MB video memory\n",
    253 	    sc->sc_vramsize >> 20);
    254 #ifdef GFFB_DEBUG
    255 	printf("put: %08x\n", GFFB_READ_4(GFFB_FIFO_PUT));
    256 	printf("get: %08x\n", GFFB_READ_4(GFFB_FIFO_GET));
    257 #endif
    258 
    259 	/*
    260 	 * we don't have hardware synchronization so we need a lock to serialize
    261 	 * access to the DMA buffer between normal and kernel output
    262 	 * actually it might be enough to use atomic ops on sc_current, sc_free
    263 	 * etc. but for now we'll play it safe
    264 	 * XXX we will probably deadlock if we take an interrupt while sc_lock
    265 	 * is held and then try to printf()
    266 	 */
    267 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    268 
    269 	/* init engine here */
    270 	gffb_init(sc);
    271 
    272 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    273 	    &gffb_accessops);
    274 	sc->vd.init_screen = gffb_init_screen;
    275 
    276 
    277 	ri = &sc->sc_console_screen.scr_ri;
    278 
    279 	sc->sc_gc.gc_bitblt = gffb_bitblt;
    280 	sc->sc_gc.gc_blitcookie = sc;
    281 	sc->sc_gc.gc_rop = 0xcc;
    282 
    283 	if (is_console) {
    284 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    285 		    &defattr);
    286 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    287 
    288 		gffb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    289 		    ri->ri_devcmap[(defattr >> 16) & 0xf]);
    290 
    291 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    292 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    293 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    294 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    295 
    296 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    297 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
    298 				sc->sc_width,
    299 				ri->ri_font->fontwidth,
    300 				ri->ri_font->fontheight,
    301 				defattr);
    302 
    303 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    304 		    defattr);
    305 		vcons_replay_msgbuf(&sc->sc_console_screen);
    306 	} else {
    307 		/*
    308 		 * since we're not the console we can postpone the rest
    309 		 * until someone actually allocates a screen for us
    310 		 */
    311 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    312 			/* do some minimal setup to avoid weirdnesses later */
    313 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    314 			    &defattr);
    315 		} else
    316 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    317 
    318 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    319 				(0x800000 / sc->sc_stride) - sc->sc_height - 5,
    320 				sc->sc_width,
    321 				ri->ri_font->fontwidth,
    322 				ri->ri_font->fontheight,
    323 				defattr);
    324 	}
    325 
    326 	j = 0;
    327 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    328 	for (i = 0; i < 256; i++) {
    329 		sc->sc_cmap_red[i] = cmap[j];
    330 		sc->sc_cmap_green[i] = cmap[j + 1];
    331 		sc->sc_cmap_blue[i] = cmap[j + 2];
    332 		gffb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    333 		j += 3;
    334 	}
    335 
    336 	/* no suspend/resume support yet */
    337 	pmf_device_register(sc->sc_dev, NULL, NULL);
    338 
    339 	aa.console = is_console;
    340 	aa.scrdata = &sc->sc_screenlist;
    341 	aa.accessops = &gffb_accessops;
    342 	aa.accesscookie = &sc->vd;
    343 
    344 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    345 
    346 #ifdef GFFB_DEBUG
    347 	for (i = 0; i < 40; i++) {
    348 		for (j = 0; j < 40; j++) {
    349 			gffb_rectfill(sc, i * 20, j * 20, 20, 20,
    350 			    (i + j ) & 1 ? 0xe0e0e0e0 : 0x03030303);
    351 		}
    352 	}
    353 
    354 	gffb_rectfill(sc, 0, 800, 1280, 224, 0x92929292);
    355 	gffb_bitblt(sc, 0, 10, 10, 810, 200, 20, 0xcc);
    356 	gffb_bitblt(sc, 0, 10, 10, 840, 300, 20, 0xcc);
    357 	gffb_bitblt(sc, 0, 10, 10, 870, 400, 20, 0xcc);
    358 	gffb_bitblt(sc, 0, 10, 10, 900, 500, 20, 0xcc);
    359 	gffb_bitblt(sc, 0, 10, 10, 930, 600, 20, 0xcc);
    360 	gffb_bitblt(sc, 0, 10, 610, 810, 200, 20, 0xcc);
    361 	gffb_bitblt(sc, 0, 10, 610, 840, 300, 20, 0xcc);
    362 	gffb_bitblt(sc, 0, 10, 610, 870, 400, 20, 0xcc);
    363 	gffb_bitblt(sc, 0, 10, 610, 900, 500, 20, 0xcc);
    364 	gffb_bitblt(sc, 0, 10, 610, 930, 600, 20, 0xcc);
    365 	gffb_sync(sc);
    366 	printf("put %x current %x\n", sc->sc_put, sc->sc_current);
    367 #endif
    368 }
    369 
    370 static int
    371 gffb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    372 	struct lwp *l)
    373 {
    374 	struct vcons_data *vd = v;
    375 	struct gffb_softc *sc = vd->cookie;
    376 	struct wsdisplay_fbinfo *wdf;
    377 	struct vcons_screen *ms = vd->active;
    378 
    379 	switch (cmd) {
    380 	case WSDISPLAYIO_GTYPE:
    381 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    382 		return 0;
    383 
    384 	/* PCI config read/write passthrough. */
    385 	case PCI_IOC_CFGREAD:
    386 	case PCI_IOC_CFGWRITE:
    387 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    388 		    cmd, data, flag, l);
    389 
    390 	case WSDISPLAYIO_GET_BUSID:
    391 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    392 		    sc->sc_pcitag, data);
    393 
    394 	case WSDISPLAYIO_GINFO:
    395 		if (ms == NULL)
    396 			return ENODEV;
    397 		wdf = (void *)data;
    398 		wdf->height = ms->scr_ri.ri_height;
    399 		wdf->width = ms->scr_ri.ri_width;
    400 		wdf->depth = ms->scr_ri.ri_depth;
    401 		wdf->cmsize = 256;
    402 		return 0;
    403 
    404 	case WSDISPLAYIO_GETCMAP:
    405 		return gffb_getcmap(sc,
    406 		    (struct wsdisplay_cmap *)data);
    407 
    408 	case WSDISPLAYIO_PUTCMAP:
    409 		return gffb_putcmap(sc,
    410 		    (struct wsdisplay_cmap *)data);
    411 
    412 	case WSDISPLAYIO_LINEBYTES:
    413 		*(u_int *)data = sc->sc_stride;
    414 		return 0;
    415 
    416 	case WSDISPLAYIO_SMODE: {
    417 		int new_mode = *(int*)data;
    418 		if (new_mode != sc->sc_mode) {
    419 			sc->sc_mode = new_mode;
    420 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    421 				gffb_init(sc);
    422 				gffb_restore_palette(sc);
    423 				glyphcache_wipe(&sc->sc_gc);
    424 				gffb_rectfill(sc, 0, 0, sc->sc_width,
    425 				    sc->sc_height, ms->scr_ri.ri_devcmap[
    426 				    (ms->scr_defattr >> 16) & 0xf]);
    427 				vcons_redraw_screen(ms);
    428 			}
    429 		}
    430 		}
    431 		return 0;
    432 
    433 	case WSDISPLAYIO_GET_EDID: {
    434 		struct wsdisplayio_edid_info *d = data;
    435 		return wsdisplayio_get_edid(sc->sc_dev, d);
    436 	}
    437 
    438 	case WSDISPLAYIO_GET_FBINFO: {
    439 		struct wsdisplayio_fbinfo *fbi = data;
    440 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    441 	}
    442 	}
    443 	return EPASSTHROUGH;
    444 }
    445 
    446 static paddr_t
    447 gffb_mmap(void *v, void *vs, off_t offset, int prot)
    448 {
    449 	struct vcons_data *vd = v;
    450 	struct gffb_softc *sc = vd->cookie;
    451 	paddr_t pa;
    452 
    453 	/* 'regular' framebuffer mmap()ing */
    454 	if (offset < sc->sc_vramsize) {
    455 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset + 0x2000,
    456 		    0, prot, BUS_SPACE_MAP_LINEAR);
    457 		return pa;
    458 	}
    459 
    460 	/*
    461 	 * restrict all other mappings to processes with superuser privileges
    462 	 * or the kernel itself
    463 	 */
    464 	if (kauth_authorize_machdep(kauth_cred_get(),
    465 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    466 	    NULL, NULL, NULL, NULL) != 0) {
    467 		aprint_normal("%s: mmap() rejected.\n",
    468 		    device_xname(sc->sc_dev));
    469 		return -1;
    470 	}
    471 
    472 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    473 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    474 		    BUS_SPACE_MAP_LINEAR);
    475 		return pa;
    476 	}
    477 
    478 	if ((offset >= sc->sc_reg) &&
    479 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    480 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    481 		    BUS_SPACE_MAP_LINEAR);
    482 		return pa;
    483 	}
    484 
    485 #ifdef PCI_MAGIC_IO_RANGE
    486 	/* allow mapping of IO space */
    487 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    488 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    489 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    490 		    0, prot, BUS_SPACE_MAP_LINEAR);
    491 		return pa;
    492 	}
    493 #endif
    494 
    495 	return -1;
    496 }
    497 
    498 static void
    499 gffb_init_screen(void *cookie, struct vcons_screen *scr,
    500     int existing, long *defattr)
    501 {
    502 	struct gffb_softc *sc = cookie;
    503 	struct rasops_info *ri = &scr->scr_ri;
    504 
    505 	ri->ri_depth = sc->sc_depth;
    506 	ri->ri_width = sc->sc_width;
    507 	ri->ri_height = sc->sc_height;
    508 	ri->ri_stride = sc->sc_stride;
    509 	ri->ri_bits = sc->sc_fbaddr + 0x2000;
    510 	ri->ri_flg = RI_CENTER;
    511 	if (sc->sc_depth == 8)
    512 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    513 
    514 	rasops_init(ri, 0, 0);
    515 	ri->ri_caps = WSSCREEN_WSCOLORS;
    516 
    517 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    518 		    sc->sc_width / ri->ri_font->fontwidth);
    519 
    520 	ri->ri_hw = scr;
    521 
    522 	sc->sc_putchar = ri->ri_ops.putchar;
    523 	ri->ri_ops.copyrows = gffb_copyrows;
    524 	ri->ri_ops.copycols = gffb_copycols;
    525 	ri->ri_ops.eraserows = gffb_eraserows;
    526 	ri->ri_ops.erasecols = gffb_erasecols;
    527 	ri->ri_ops.cursor = gffb_cursor;
    528 	ri->ri_ops.putchar = gffb_putchar;
    529 }
    530 
    531 static int
    532 gffb_putcmap(struct gffb_softc *sc, struct wsdisplay_cmap *cm)
    533 {
    534 	u_char *r, *g, *b;
    535 	u_int index = cm->index;
    536 	u_int count = cm->count;
    537 	int i, error;
    538 	u_char rbuf[256], gbuf[256], bbuf[256];
    539 
    540 #ifdef GFFB_DEBUG
    541 	aprint_debug("putcmap: %d %d\n",index, count);
    542 #endif
    543 	if (cm->index >= 256 || cm->count > 256 ||
    544 	    (cm->index + cm->count) > 256)
    545 		return EINVAL;
    546 	error = copyin(cm->red, &rbuf[index], count);
    547 	if (error)
    548 		return error;
    549 	error = copyin(cm->green, &gbuf[index], count);
    550 	if (error)
    551 		return error;
    552 	error = copyin(cm->blue, &bbuf[index], count);
    553 	if (error)
    554 		return error;
    555 
    556 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    557 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    558 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    559 
    560 	r = &sc->sc_cmap_red[index];
    561 	g = &sc->sc_cmap_green[index];
    562 	b = &sc->sc_cmap_blue[index];
    563 
    564 	for (i = 0; i < count; i++) {
    565 		gffb_putpalreg(sc, index, *r, *g, *b);
    566 		index++;
    567 		r++, g++, b++;
    568 	}
    569 	return 0;
    570 }
    571 
    572 static int
    573 gffb_getcmap(struct gffb_softc *sc, struct wsdisplay_cmap *cm)
    574 {
    575 	u_int index = cm->index;
    576 	u_int count = cm->count;
    577 	int error;
    578 
    579 	if (index >= 255 || count > 256 || index + count > 256)
    580 		return EINVAL;
    581 
    582 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    583 	if (error)
    584 		return error;
    585 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    586 	if (error)
    587 		return error;
    588 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    589 	if (error)
    590 		return error;
    591 
    592 	return 0;
    593 }
    594 
    595 static void
    596 gffb_restore_palette(struct gffb_softc *sc)
    597 {
    598 	int i;
    599 
    600 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    601 		gffb_putpalreg(sc, i, sc->sc_cmap_red[i],
    602 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    603 	}
    604 }
    605 
    606 static int
    607 gffb_putpalreg(struct gffb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    608     uint8_t b)
    609 {
    610 	/* port 0 */
    611 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    612 	    GFFB_PDIO0 + GFFB_PEL_IW, idx);
    613 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    614 	    GFFB_PDIO0 + GFFB_PEL_D, r);
    615 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    616 	    GFFB_PDIO0 + GFFB_PEL_D, g);
    617 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    618 	    GFFB_PDIO0 + GFFB_PEL_D, b);
    619 
    620 	/* port 1 */
    621 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    622 	    GFFB_PDIO1 + GFFB_PEL_IW, idx);
    623 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    624 	    GFFB_PDIO1 + GFFB_PEL_D, r);
    625 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    626 	    GFFB_PDIO1 + GFFB_PEL_D, g);
    627 	bus_space_write_1(sc->sc_memt, sc->sc_regh,
    628 	    GFFB_PDIO1 + GFFB_PEL_D, b);
    629 
    630 	return 0;
    631 }
    632 
    633 
    634 static void
    635 gffb_dma_kickoff(struct gffb_softc *sc)
    636 {
    637 	volatile uint8_t scratch;
    638 
    639 	if(sc->sc_current != sc->sc_put) {
    640 		sc->sc_put = sc->sc_current;
    641 		membar_sync();
    642 		scratch = *sc->sc_fbaddr;
    643 		GFFB_WRITE_4(GFFB_FIFO_PUT, sc->sc_put);
    644 		membar_sync();
    645 	}
    646 }
    647 
    648 static void
    649 gffb_dmanext(struct gffb_softc *sc, uint32_t data)
    650 {
    651 	bus_space_write_stream_4(sc->sc_memt, sc->sc_fbh, sc->sc_current, data);
    652 	sc->sc_current += 4;
    653 }
    654 
    655 static void
    656 gffb_dmastart(struct gffb_softc *sc, uint32_t tag, int size)
    657 {
    658 	if(sc->sc_free <= (size << 2))
    659 		gffb_make_room(sc, size);
    660 	gffb_dmanext(sc, ((size) << 18) | (tag));
    661 	sc->sc_free -= ((size + 1) << 2);
    662 }
    663 
    664 /*
    665  * from xf86_video_nv/nv_xaa.c:
    666  * There is a HW race condition with videoram command buffers.
    667  * You can't jump to the location of your put offset.  We write put
    668  * at the jump offset + SKIPS dwords with noop padding in between
    669  * to solve this problem
    670  */
    671 
    672 #define SKIPS  8
    673 
    674 static void
    675 gffb_make_room(struct gffb_softc *sc, int size)
    676 {
    677 	uint32_t get;
    678 
    679 	size = (size + 1) << 2;	/* slots -> offset */
    680 
    681 	while (sc->sc_free < size) {
    682 		get = GFFB_READ_4(GFFB_FIFO_GET);
    683 
    684 		if (sc->sc_put >= get) {
    685 			sc->sc_free = 0x2000 - sc->sc_current;
    686 			if (sc->sc_free < size) {
    687 				gffb_dmanext(sc, 0x20000000);
    688 				if(get <= (SKIPS << 2)) {
    689 					if (sc->sc_put <= (SKIPS << 2)) {
    690 						/* corner case - will be idle */
    691 						GFFB_WRITE_4(GFFB_FIFO_PUT,
    692 						    (SKIPS + 1) << 2);
    693 					}
    694 					do {
    695 						get =GFFB_READ_4(GFFB_FIFO_GET);
    696 					} while (get <= (SKIPS << 2));
    697 				}
    698 				GFFB_WRITE_4(GFFB_FIFO_PUT, SKIPS << 2);
    699 				sc->sc_current = sc->sc_put = (SKIPS << 2);
    700 				sc->sc_free = get - ((SKIPS + 1) << 2);
    701 			}
    702 		} else
    703 			sc->sc_free = get - sc->sc_current - 4;
    704 	}
    705 }
    706 
    707 static void
    708 gffb_sync(struct gffb_softc *sc)
    709 {
    710 	int bail;
    711 	int i;
    712 
    713 	/*
    714 	 * if there are commands in the buffer make sure the chip is actually
    715 	 * trying to run them
    716 	 */
    717 	gffb_dma_kickoff(sc);
    718 
    719 	/* now wait for the command buffer to drain... */
    720 	bail = 100000000;
    721 	while ((GFFB_READ_4(GFFB_FIFO_GET) != sc->sc_put) && (bail > 0)) {
    722 		bail--;
    723 	}
    724 	if (bail == 0) goto crap;
    725 
    726 	/* ... and for the engine to go idle */
    727 	bail = 100000000;
    728 	while((GFFB_READ_4(GFFB_BUSY) != 0) && (bail > 0)) {
    729 		bail--;
    730 	}
    731 	if (bail == 0) goto crap;
    732 	return;
    733 crap:
    734 	/* if we time out fill the buffer with NOPs and cross fingers */
    735 	sc->sc_put = 0;
    736 	sc->sc_current = 0;
    737 	for (i = 0; i < 0x2000; i += 4)
    738 		bus_space_write_stream_4(sc->sc_memt, sc->sc_fbh, i, 0);
    739 	aprint_error_dev(sc->sc_dev, "DMA lockup\n");
    740 }
    741 
    742 static void
    743 gffb_init(struct gffb_softc *sc)
    744 {
    745 	int i;
    746 	uint32_t foo;
    747 
    748 	/* init display start */
    749 	GFFB_WRITE_4(GFFB_CRTC0 + GFFB_DISPLAYSTART, 0x2000);
    750 	GFFB_WRITE_4(GFFB_CRTC1 + GFFB_DISPLAYSTART, 0x2000);
    751 	GFFB_WRITE_4(GFFB_PDIO0 + GFFB_PEL_MASK, 0xff);
    752 	GFFB_WRITE_4(GFFB_PDIO1 + GFFB_PEL_MASK, 0xff);
    753 
    754 	/* DMA stuff. A whole lot of magic number voodoo from xf86-video-nv */
    755 	GFFB_WRITE_4(GFFB_PMC + 0x140, 0);
    756 	GFFB_WRITE_4(GFFB_PMC + 0x200, 0xffff00ff);
    757 	GFFB_WRITE_4(GFFB_PMC + 0x200, 0xffffffff);
    758 	GFFB_WRITE_4(GFFB_PTIMER + 0x800, 8);
    759 	GFFB_WRITE_4(GFFB_PTIMER + 0x840, 3);
    760 	GFFB_WRITE_4(GFFB_PTIMER + 0x500, 0);
    761 	GFFB_WRITE_4(GFFB_PTIMER + 0x400, 0xffffffff);
    762 	for (i = 0; i < 8; i++) {
    763 		GFFB_WRITE_4(GFFB_PMC + 0x240 + (i * 0x10), 0);
    764 		GFFB_WRITE_4(GFFB_PMC + 0x244 + (i * 0x10),
    765 		    sc->sc_vramsize - 1);
    766 	}
    767 
    768 	for (i = 0; i < 8; i++) {
    769 		GFFB_WRITE_4(GFFB_PFB + 0x0240 + (i * 0x10), 0);
    770 		GFFB_WRITE_4(GFFB_PFB + 0x0244 + (i * 0x10),
    771 		    sc->sc_vramsize - 1);
    772 	}
    773 
    774 	GFFB_WRITE_4(GFFB_PRAMIN, 0x80000010);
    775 	GFFB_WRITE_4(GFFB_PRAMIN + 0x04, 0x80011201);
    776 	GFFB_WRITE_4(GFFB_PRAMIN + 0x08, 0x80000011);
    777 	GFFB_WRITE_4(GFFB_PRAMIN + 0x0c, 0x80011202);
    778 	GFFB_WRITE_4(GFFB_PRAMIN + 0x10, 0x80000012);
    779 	GFFB_WRITE_4(GFFB_PRAMIN + 0x14, 0x80011203);
    780 	GFFB_WRITE_4(GFFB_PRAMIN + 0x18, 0x80000013);
    781 	GFFB_WRITE_4(GFFB_PRAMIN + 0x1c, 0x80011204);
    782 	GFFB_WRITE_4(GFFB_PRAMIN + 0x20, 0x80000014);
    783 	GFFB_WRITE_4(GFFB_PRAMIN + 0x24, 0x80011205);
    784 	GFFB_WRITE_4(GFFB_PRAMIN + 0x28, 0x80000015);
    785 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2c, 0x80011206);
    786 	GFFB_WRITE_4(GFFB_PRAMIN + 0x30, 0x80000016);
    787 	GFFB_WRITE_4(GFFB_PRAMIN + 0x34, 0x80011207);
    788 	GFFB_WRITE_4(GFFB_PRAMIN + 0x38, 0x80000017);
    789 	GFFB_WRITE_4(GFFB_PRAMIN + 0x3c, 0x80011208);
    790 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2000, 0x00003000);
    791 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2004, sc->sc_vramsize - 1);
    792 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2008, 0x00000002);
    793 	GFFB_WRITE_4(GFFB_PRAMIN + 0x200c, 0x00000002);
    794 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2010, 0x01008042);	/* different for nv40 */
    795 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2014, 0);
    796 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2018, 0x12001200);
    797 	GFFB_WRITE_4(GFFB_PRAMIN + 0x201c, 0);
    798 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2020, 0x01008043);
    799 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2024, 0);
    800 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2028, 0);
    801 	GFFB_WRITE_4(GFFB_PRAMIN + 0x202c, 0);
    802 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2030, 0x01008044);
    803 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2034, 0x00000002);
    804 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2038, 0);
    805 	GFFB_WRITE_4(GFFB_PRAMIN + 0x203c, 0);
    806 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2040, 0x01008019);
    807 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2044, 0);
    808 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2048, 0);
    809 	GFFB_WRITE_4(GFFB_PRAMIN + 0x204c, 0);
    810 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2050, 0x0100a05c);
    811 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2054, 0);
    812 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2058, 0);
    813 	GFFB_WRITE_4(GFFB_PRAMIN + 0x205c, 0);
    814 	/* XXX 0x0100805f if !WaitVSynvPossible */
    815 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2060, 0x0100809f);
    816 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2064, 0);
    817 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2068, 0x12001200);
    818 	GFFB_WRITE_4(GFFB_PRAMIN + 0x206c, 0);
    819 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2070, 0x0100804a);
    820 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2074, 0x00000002);
    821 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2078, 0);
    822 	GFFB_WRITE_4(GFFB_PRAMIN + 0x207c, 0);
    823 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2080, 0x01018077);
    824 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2084, 0);
    825 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2088, 0x12001200);
    826 	GFFB_WRITE_4(GFFB_PRAMIN + 0x208c, 0);
    827 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2090, 0x00003002);
    828 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2094, 0x00007fff);
    829 	/* command buffer start with some flag in the lower bits */
    830 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2098, 0x00000002);
    831 	GFFB_WRITE_4(GFFB_PRAMIN + 0x209c, 0x00000002);
    832 #if BYTE_ORDER == BIG_ENDIAN
    833 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2010, 0x01088042);
    834 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2020, 0x01088043);
    835 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2030, 0x01088044);
    836 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2040, 0x01088019);
    837 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2050, 0x0108a05c);
    838 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2060, 0x0108809f);
    839 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2070, 0x0108804a);
    840 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2080, 0x01098077);
    841 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2034, 0x00000001);
    842 	GFFB_WRITE_4(GFFB_PRAMIN + 0x2074, 0x00000001);
    843 #endif
    844 
    845 	/* PGRAPH setup */
    846 	GFFB_WRITE_4(GFFB_PFIFO + 0x0500, 0);
    847 	GFFB_WRITE_4(GFFB_PFIFO + 0x0504, 0x00000001);
    848 	GFFB_WRITE_4(GFFB_PFIFO + 0x1200, 0);
    849 	GFFB_WRITE_4(GFFB_PFIFO + 0x1250, 0);
    850 	GFFB_WRITE_4(GFFB_PFIFO + 0x1204, 0x00000100);	/* different on nv40 */
    851 	GFFB_WRITE_4(GFFB_PFIFO + 0x1240, 0);
    852 	GFFB_WRITE_4(GFFB_PFIFO + 0x1244, 0);
    853 	GFFB_WRITE_4(GFFB_PFIFO + 0x122c, 0x00001209);	/* different on nv40 */
    854 	GFFB_WRITE_4(GFFB_PFIFO + 0x1000, 0);
    855 	GFFB_WRITE_4(GFFB_PFIFO + 0x1050, 0);
    856 	GFFB_WRITE_4(GFFB_PFIFO + 0x0210, 0x03000100);
    857 	GFFB_WRITE_4(GFFB_PFIFO + 0x0214, 0x00000110);
    858 	GFFB_WRITE_4(GFFB_PFIFO + 0x0218, 0x00000112);
    859 	GFFB_WRITE_4(GFFB_PFIFO + 0x050c, 0x0000ffff);
    860 	GFFB_WRITE_4(GFFB_PFIFO + 0x1258, 0x0000ffff);
    861 	GFFB_WRITE_4(GFFB_PFIFO + 0x0140, 0);
    862 	GFFB_WRITE_4(GFFB_PFIFO + 0x0100, 0xffffffff);
    863 	GFFB_WRITE_4(GFFB_PFIFO + 0x1054, 0x00000001);
    864 	GFFB_WRITE_4(GFFB_PFIFO + 0x1230, 0);
    865 	GFFB_WRITE_4(GFFB_PFIFO + 0x1280, 0);
    866 #if BYTE_ORDER == BIG_ENDIAN
    867 	GFFB_WRITE_4(GFFB_PFIFO + 0x1224, 0x800f0078);
    868 #else
    869 	GFFB_WRITE_4(GFFB_PFIFO + 0x1224, 0x000f0078);
    870 #endif
    871 	GFFB_WRITE_4(GFFB_PFIFO + 0x1220, 0x00000001);
    872 	GFFB_WRITE_4(GFFB_PFIFO + 0x1200, 0x00000001);
    873 	GFFB_WRITE_4(GFFB_PFIFO + 0x1250, 0x00000001);
    874 	GFFB_WRITE_4(GFFB_PFIFO + 0x1254, 0x00000001);
    875 	GFFB_WRITE_4(GFFB_PFIFO + 0x0500, 0x00000001);
    876 
    877 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0080, 0xFFFFFFFF);
    878 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0080, 0x00000000);
    879 
    880 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0140, 0x00000000);
    881 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0100, 0xFFFFFFFF);
    882 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0144, 0x10010100);
    883 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0714, 0xFFFFFFFF);
    884 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0720, 0x00000001);
    885 	/*
    886 	 * xf86_video_nv does this in two writes,
    887 	 * not sure if they can be combined
    888 	 */
    889 	foo = GFFB_READ_4(GFFB_PGRAPH + 0x0710);
    890 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0710, foo & 0x0007ff00);
    891 	foo = GFFB_READ_4(GFFB_PGRAPH + 0x0710);
    892 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0710, foo | 0x00020100);
    893 
    894 	/* NV_ARCH_10 */
    895 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0084, 0x00118700);
    896 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0088, 0x24E00810);
    897 	GFFB_WRITE_4(GFFB_PGRAPH + 0x008C, 0x55DE0030);
    898 
    899 	for(i = 0; i < 128; i += 4) {
    900 		GFFB_WRITE_4(GFFB_PGRAPH + 0x0B00 + i,
    901 		    GFFB_READ_4(GFFB_PFB + 0x0240 + i));
    902 	}
    903 
    904 	GFFB_WRITE_4(GFFB_PGRAPH + 0x640, 0);
    905 	GFFB_WRITE_4(GFFB_PGRAPH + 0x644, 0);
    906 	GFFB_WRITE_4(GFFB_PGRAPH + 0x684, sc->sc_vramsize - 1);
    907 	GFFB_WRITE_4(GFFB_PGRAPH + 0x688, sc->sc_vramsize - 1);
    908 
    909 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0810, 0x00000000);
    910 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0608, 0xFFFFFFFF);
    911 
    912 	GFFB_WRITE_4(GFFB_PGRAPH + 0x053C, 0);
    913 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0540, 0);
    914 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0544, 0x00007FFF);
    915 	GFFB_WRITE_4(GFFB_PGRAPH + 0x0548, 0x00007FFF);
    916 
    917 	GFFB_WRITE_4(GFFB_CMDSTART, 0x00000002);
    918 	GFFB_WRITE_4(GFFB_FIFO_GET, 0);
    919 	sc->sc_put = 0;
    920 	sc->sc_current = 0;
    921 	sc->sc_free = 0x2000;
    922 
    923 	for(i = 0; i < SKIPS; i++)
    924 		gffb_dmanext(sc, 0);
    925 
    926 	gffb_dmanext(sc, 0x00040000);
    927 	gffb_dmanext(sc, 0x80000010);
    928 	gffb_dmanext(sc, 0x00042000);
    929 	gffb_dmanext(sc, 0x80000011);
    930 	gffb_dmanext(sc, 0x00044000);
    931 	gffb_dmanext(sc, 0x80000012);
    932 	gffb_dmanext(sc, 0x00046000);
    933 	gffb_dmanext(sc, 0x80000013);
    934 	gffb_dmanext(sc, 0x00048000);
    935 	gffb_dmanext(sc, 0x80000014);
    936 	gffb_dmanext(sc, 0x0004A000);
    937 	gffb_dmanext(sc, 0x80000015);
    938 	gffb_dmanext(sc, 0x0004C000);
    939 	gffb_dmanext(sc, 0x80000016);
    940 	gffb_dmanext(sc, 0x0004E000);
    941 	gffb_dmanext(sc, 0x80000017);
    942 	sc->sc_free = 0x2000 - sc->sc_current;
    943 
    944 	gffb_dmastart(sc, SURFACE_FORMAT, 4);
    945 	gffb_dmanext(sc, SURFACE_FORMAT_DEPTH8);
    946 	gffb_dmanext(sc, sc->sc_stride | (sc->sc_stride << 16));
    947 	gffb_dmanext(sc, 0x2000);	/* src offset */
    948 	gffb_dmanext(sc, 0x2000);	/* dst offset */
    949 
    950 	gffb_dmastart(sc, RECT_FORMAT, 1);
    951 	gffb_dmanext(sc, RECT_FORMAT_DEPTH8);
    952 
    953 	gffb_dmastart(sc, PATTERN_FORMAT, 1);
    954 	gffb_dmanext(sc, PATTERN_FORMAT_DEPTH8);
    955 
    956 	gffb_dmastart(sc, PATTERN_COLOR_0, 4);
    957 	gffb_dmanext(sc, 0xffffffff);
    958 	gffb_dmanext(sc, 0xffffffff);
    959 	gffb_dmanext(sc, 0xffffffff);
    960 	gffb_dmanext(sc, 0xffffffff);
    961 
    962 	gffb_dmastart(sc, ROP_SET, 1);
    963 	gffb_dmanext(sc, 0xcc);
    964 	sc->sc_rop = 0xcc;
    965 
    966 	gffb_dma_kickoff(sc);
    967 	gffb_sync(sc);
    968 	DPRINTF("put %x current %x\n", sc->sc_put, sc->sc_current);
    969 
    970 }
    971 
    972 static void
    973 gffb_rop(struct gffb_softc *sc, int rop)
    974 {
    975 	if (rop == sc->sc_rop)
    976 		return;
    977 	sc->sc_rop = rop;
    978 	gffb_dmastart(sc, ROP_SET, 1);
    979 	gffb_dmanext(sc, rop);
    980 }
    981 
    982 static void
    983 gffb_rectfill(struct gffb_softc *sc, int x, int y, int wi, int he,
    984      uint32_t colour)
    985 {
    986 	mutex_enter(&sc->sc_lock);
    987 	gffb_rop(sc, 0xcc);
    988 
    989 	gffb_dmastart(sc, RECT_SOLID_COLOR, 1);
    990 	gffb_dmanext(sc, colour);
    991 
    992 	gffb_dmastart(sc, RECT_SOLID_RECTS(0), 2);
    993 	gffb_dmanext(sc, (x << 16) | y);
    994 	gffb_dmanext(sc, (wi << 16) | he);
    995 	gffb_dma_kickoff(sc);
    996 	mutex_exit(&sc->sc_lock);
    997 }
    998 
    999 static void
   1000 gffb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
   1001     int wi, int he, int rop)
   1002 {
   1003 	struct gffb_softc *sc = cookie;
   1004 
   1005 	mutex_enter(&sc->sc_lock);
   1006 
   1007 	gffb_rop(sc, rop);
   1008 
   1009 	gffb_dmastart(sc, BLIT_POINT_SRC, 3);
   1010 	gffb_dmanext(sc, (ys << 16) | xs);
   1011 	gffb_dmanext(sc, (yd << 16) | xd);
   1012 	gffb_dmanext(sc, (he << 16) | wi);
   1013 	gffb_dma_kickoff(sc);
   1014 	mutex_exit(&sc->sc_lock);
   1015 }
   1016 
   1017 static void
   1018 gffb_cursor(void *cookie, int on, int row, int col)
   1019 {
   1020 	struct rasops_info *ri = cookie;
   1021 	struct vcons_screen *scr = ri->ri_hw;
   1022 	struct gffb_softc *sc = scr->scr_cookie;
   1023 	int x, y, wi, he;
   1024 
   1025 	wi = ri->ri_font->fontwidth;
   1026 	he = ri->ri_font->fontheight;
   1027 
   1028 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
   1029 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1030 		y = ri->ri_crow * he + ri->ri_yorigin;
   1031 		if (ri->ri_flg & RI_CURSOR) {
   1032 			gffb_bitblt(sc, x, y, x, y, wi, he, 0x33);
   1033 			ri->ri_flg &= ~RI_CURSOR;
   1034 		}
   1035 		ri->ri_crow = row;
   1036 		ri->ri_ccol = col;
   1037 		if (on) {
   1038 			x = ri->ri_ccol * wi + ri->ri_xorigin;
   1039 			y = ri->ri_crow * he + ri->ri_yorigin;
   1040 			gffb_bitblt(sc, x, y, x, y, wi, he, 0x33);
   1041 			ri->ri_flg |= RI_CURSOR;
   1042 		}
   1043 	} else {
   1044 		scr->scr_ri.ri_crow = row;
   1045 		scr->scr_ri.ri_ccol = col;
   1046 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
   1047 	}
   1048 
   1049 }
   1050 
   1051 static void
   1052 gffb_putchar(void *cookie, int row, int col, u_int c, long attr)
   1053 {
   1054 	struct rasops_info *ri = cookie;
   1055 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1056 	struct vcons_screen *scr = ri->ri_hw;
   1057 	struct gffb_softc *sc = scr->scr_cookie;
   1058 	int x, y, wi, he, rv = GC_NOPE;
   1059 	uint32_t bg;
   1060 
   1061 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1062 		return;
   1063 
   1064 	if (!CHAR_IN_FONT(c, font))
   1065 		return;
   1066 
   1067 	wi = font->fontwidth;
   1068 	he = font->fontheight;
   1069 
   1070 	x = ri->ri_xorigin + col * wi;
   1071 	y = ri->ri_yorigin + row * he;
   1072 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1073 
   1074 	if (c == 0x20) {
   1075 		gffb_rectfill(sc, x, y, wi, he, bg);
   1076 		return;
   1077 	}
   1078 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1079 	if (rv == GC_OK)
   1080 		return;
   1081 
   1082 	mutex_enter(&sc->sc_lock);
   1083 	gffb_sync(sc);
   1084 	sc->sc_putchar(cookie, row, col, c, attr);
   1085 	membar_sync();
   1086 	mutex_exit(&sc->sc_lock);
   1087 
   1088 	if (rv == GC_ADD) {
   1089 		glyphcache_add(&sc->sc_gc, c, x, y);
   1090 	}
   1091 }
   1092 
   1093 static void
   1094 gffb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1095 {
   1096 	struct rasops_info *ri = cookie;
   1097 	struct vcons_screen *scr = ri->ri_hw;
   1098 	struct gffb_softc *sc = scr->scr_cookie;
   1099 	int32_t xs, xd, y, width, height;
   1100 
   1101 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1102 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1103 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1104 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1105 		width = ri->ri_font->fontwidth * ncols;
   1106 		height = ri->ri_font->fontheight;
   1107 		gffb_bitblt(sc, xs, y, xd, y, width, height, 0xcc);
   1108 	}
   1109 }
   1110 
   1111 static void
   1112 gffb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1113 {
   1114 	struct rasops_info *ri = cookie;
   1115 	struct vcons_screen *scr = ri->ri_hw;
   1116 	struct gffb_softc *sc = scr->scr_cookie;
   1117 	int32_t x, y, width, height, fg, bg, ul;
   1118 
   1119 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1120 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1121 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1122 		width = ri->ri_font->fontwidth * ncols;
   1123 		height = ri->ri_font->fontheight;
   1124 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1125 
   1126 		gffb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1127 	}
   1128 }
   1129 
   1130 static void
   1131 gffb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1132 {
   1133 	struct rasops_info *ri = cookie;
   1134 	struct vcons_screen *scr = ri->ri_hw;
   1135 	struct gffb_softc *sc = scr->scr_cookie;
   1136 	int32_t x, ys, yd, width, height;
   1137 
   1138 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1139 		x = ri->ri_xorigin;
   1140 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1141 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1142 		width = ri->ri_emuwidth;
   1143 		height = ri->ri_font->fontheight * nrows;
   1144 		gffb_bitblt(sc, x, ys, x, yd, width, height, 0xcc);
   1145 	}
   1146 }
   1147 
   1148 static void
   1149 gffb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1150 {
   1151 	struct rasops_info *ri = cookie;
   1152 	struct vcons_screen *scr = ri->ri_hw;
   1153 	struct gffb_softc *sc = scr->scr_cookie;
   1154 	int32_t x, y, width, height, fg, bg, ul;
   1155 
   1156 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1157 		x = ri->ri_xorigin;
   1158 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1159 		width = ri->ri_emuwidth;
   1160 		height = ri->ri_font->fontheight * nrows;
   1161 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1162 
   1163 		gffb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1164 	}
   1165 }
   1166