Home | History | Annotate | Line # | Download | only in ic
igsfb.c revision 1.27
      1 /*	$NetBSD: igsfb.c,v 1.27 2006/02/23 08:01:59 macallan Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2003 Valeriy E. Ushakov
      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  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Integraphics Systems IGA 168x and CyberPro series.
     32  */
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: igsfb.c,v 1.27 2006/02/23 08:01:59 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/ioctl.h>
     42 #include <sys/buf.h>
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <machine/bus.h>
     46 
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wsfont/wsfont.h>
     50 #include <dev/rasops/rasops.h>
     51 
     52 #include <dev/wscons/wsdisplay_vconsvar.h>
     53 
     54 #include <dev/ic/igsfbreg.h>
     55 #include <dev/ic/igsfbvar.h>
     56 
     57 #include "opt_wsemul.h"
     58 
     59 struct igsfb_devconfig igsfb_console_dc;
     60 
     61 /*
     62  * wsscreen
     63  */
     64 
     65 /* filled from rasops_info in igsfb_common_init */
     66 static struct wsscreen_descr igsfb_stdscreen = {
     67 	"std",			/* name */
     68 	0, 0,			/* ncols, nrows */
     69 	NULL,			/* textops */
     70 	0, 0,			/* fontwidth, fontheight */
     71 	0			/* capabilities */
     72 };
     73 
     74 static const struct wsscreen_descr *_igsfb_scrlist[] = {
     75 	&igsfb_stdscreen,
     76 };
     77 
     78 static const struct wsscreen_list igsfb_screenlist = {
     79 	sizeof(_igsfb_scrlist) / sizeof(struct wsscreen_descr *),
     80 	_igsfb_scrlist
     81 };
     82 
     83 
     84 /*
     85  * wsdisplay_accessops
     86  */
     87 
     88 static int	igsfb_ioctl(void *, u_long, caddr_t, int, struct lwp *);
     89 static paddr_t	igsfb_mmap(void *, off_t, int);
     90 
     91 static struct wsdisplay_accessops igsfb_accessops = {
     92 	igsfb_ioctl,
     93 	igsfb_mmap,
     94 	NULL,
     95 	NULL,
     96 	NULL,
     97 	NULL,			/* load_font */
     98 	NULL,			/* pollc */
     99 	NULL,			/* getwschar */
    100 	NULL			/* putwschar */
    101 };
    102 
    103 
    104 /*
    105  * acceleration
    106  */
    107 static int	igsfb_make_text_cursor(struct igsfb_devconfig *,
    108     struct vcons_screen *);
    109 static void	igsfb_accel_cursor(void *, int, int, int);
    110 
    111 static int	igsfb_accel_wait(struct igsfb_devconfig *);
    112 static void	igsfb_accel_fill(struct igsfb_devconfig *,
    113 				 uint32_t, uint32_t, uint16_t, uint16_t);
    114 static void	igsfb_accel_copy(struct igsfb_devconfig *,
    115 				 uint32_t, uint32_t, uint16_t, uint16_t);
    116 
    117 static void	igsfb_accel_copycols(void *, int, int, int, int);
    118 static void	igsfb_accel_erasecols(void *, int, int, int, long);
    119 static void	igsfb_accel_copyrows(void *, int, int, int);
    120 static void	igsfb_accel_eraserows(void *, int, int, long);
    121 static void	igsfb_accel_putchar(void *, int, int, u_int, long);
    122 
    123 
    124 /*
    125  * internal functions
    126  */
    127 static int	igsfb_init_video(struct igsfb_devconfig *);
    128 static void	igsfb_init_cmap(struct igsfb_devconfig *);
    129 static uint16_t	igsfb_spread_bits_8(uint8_t);
    130 static void	igsfb_init_bit_table(struct igsfb_devconfig *);
    131 static void	igsfb_init_wsdisplay(void *, struct vcons_screen *, int,
    132     long *);
    133 
    134 
    135 static void	igsfb_blank_screen(struct igsfb_devconfig *, int);
    136 static int	igsfb_get_cmap(struct igsfb_devconfig *,
    137 			       struct wsdisplay_cmap *);
    138 static int	igsfb_set_cmap(struct igsfb_devconfig *,
    139 			       const struct wsdisplay_cmap *);
    140 static void	igsfb_update_cmap(struct igsfb_devconfig *, u_int, u_int);
    141 static void	igsfb_set_curpos(struct igsfb_devconfig *,
    142 				 const struct wsdisplay_curpos *);
    143 static void	igsfb_update_curpos(struct igsfb_devconfig *);
    144 static int	igsfb_get_cursor(struct igsfb_devconfig *,
    145 				 struct wsdisplay_cursor *);
    146 static int	igsfb_set_cursor(struct igsfb_devconfig *,
    147 				 const struct wsdisplay_cursor *);
    148 static void	igsfb_update_cursor(struct igsfb_devconfig *, u_int);
    149 static void	igsfb_convert_cursor_data(struct igsfb_devconfig *,
    150 					  u_int, u_int);
    151 
    152 
    153 int
    154 igsfb_cnattach_subr(dc)
    155 	struct igsfb_devconfig *dc;
    156 {
    157 	struct rasops_info *ri;
    158 	long defattr;
    159 
    160 	KASSERT(dc == &igsfb_console_dc);
    161 
    162 	igsfb_init_video(dc);
    163 	dc->dc_vd.active = NULL;
    164 	igsfb_init_wsdisplay(dc, &dc->dc_console, 1, &defattr);
    165 
    166 	ri = &dc->dc_console.scr_ri;
    167 	ri->ri_hw = &dc->dc_console;
    168 	dc->dc_console.scr_cookie = dc;
    169 
    170 	(*ri->ri_ops.allocattr)(ri,
    171 				WS_DEFAULT_FG, /* fg */
    172 				WS_DEFAULT_BG, /* bg */
    173 				0,           /* wsattrs */
    174 				&defattr);
    175 
    176 	wsdisplay_cnattach(&igsfb_stdscreen,
    177 			   ri,   /* emulcookie */
    178 			   0, 0, /* cursor position */
    179 			   defattr);
    180 	return (0);
    181 }
    182 
    183 
    184 /*
    185  * Finish off the attach.  Bus specific attach method should have
    186  * enabled io and memory accesses and mapped io (and cop?) registers.
    187  */
    188 void
    189 igsfb_attach_subr(sc, isconsole)
    190 	struct igsfb_softc *sc;
    191 	int isconsole;
    192 {
    193 	struct igsfb_devconfig *dc = sc->sc_dc;
    194 	struct wsemuldisplaydev_attach_args waa;
    195 	struct rasops_info *ri;
    196 	long defattr;
    197 
    198 	KASSERT(dc != NULL);
    199 
    200 	if (!isconsole) {
    201 		igsfb_init_video(dc);
    202 	}
    203 
    204 	vcons_init(&dc->dc_vd, dc, &igsfb_stdscreen, &igsfb_accessops);
    205 	dc->dc_vd.init_screen = igsfb_init_wsdisplay;
    206 
    207 	vcons_init_screen(&dc->dc_vd, &dc->dc_console, 1, &defattr);
    208 	dc->dc_console.scr_flags |= VCONS_SCREEN_IS_STATIC;
    209 
    210 	printf("%s: %dMB, %s%dx%d, %dbpp\n",
    211 	       sc->sc_dev.dv_xname,
    212 	       (uint32_t)(dc->dc_vmemsz >> 20),
    213 	       (dc->dc_hwflags & IGSFB_HW_BSWAP)
    214 		   ? (dc->dc_hwflags & IGSFB_HW_BE_SELECT)
    215 		       ? "hardware bswap, " : "software bswap, "
    216 		   : "",
    217 	       dc->dc_width, dc->dc_height, dc->dc_depth);
    218 
    219 	ri = &dc->dc_console.scr_ri;
    220 	ri->ri_ops.eraserows(ri, 0, ri->ri_rows, defattr);
    221 
    222 	/* attach wsdisplay */
    223 	waa.console = isconsole;
    224 	waa.scrdata = &igsfb_screenlist;
    225 	waa.accessops = &igsfb_accessops;
    226 	waa.accesscookie = &dc->dc_vd;
    227 
    228 	config_found(&sc->sc_dev, &waa, wsemuldisplaydevprint);
    229 }
    230 
    231 
    232 static int
    233 igsfb_init_video(dc)
    234 	struct igsfb_devconfig *dc;
    235 {
    236 	bus_space_handle_t tmph;
    237 	uint8_t *p;
    238 	int need_bswap;
    239 	bus_addr_t fbaddr, craddr;
    240 	off_t croffset;
    241 	uint8_t busctl, curctl;
    242 
    243 	/* Total amount of video memory. */
    244 	busctl = igs_ext_read(dc->dc_iot, dc->dc_ioh, IGS_EXT_BUS_CTL);
    245 	if (busctl & 0x2)
    246 		dc->dc_vmemsz = 4;
    247 	else if (busctl & 0x1)
    248 		dc->dc_vmemsz = 2;
    249 	else
    250 		dc->dc_vmemsz = 1;
    251 	dc->dc_vmemsz <<= 20;	/* megabytes -> bytes */
    252 
    253 	/*
    254 	 * Check for endianness mismatch by writing a word at the end of
    255 	 * the video memory (off-screen) and reading it back byte-by-byte.
    256 	 */
    257 	if (bus_space_map(dc->dc_memt,
    258 			  dc->dc_memaddr + dc->dc_vmemsz - sizeof(uint32_t),
    259 			  sizeof(uint32_t),
    260 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    261 			  &tmph) != 0)
    262 	{
    263 		printf("unable to map video memory for endianness test\n");
    264 		return (1);
    265 	}
    266 
    267 	p = bus_space_vaddr(dc->dc_memt, tmph);
    268 #if BYTE_ORDER == BIG_ENDIAN
    269 	*((uint32_t *)p) = 0x12345678;
    270 #else
    271 	*((uint32_t *)p) = 0x78563412;
    272 #endif
    273 	if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
    274 		need_bswap = 0;
    275 	else
    276 		need_bswap = 1;
    277 
    278 	bus_space_unmap(dc->dc_memt, tmph, sizeof(uint32_t));
    279 
    280 	/*
    281 	 * On CyberPro we can use magic bswap bit in linear address.
    282 	 */
    283 	fbaddr = dc->dc_memaddr;
    284 	if (need_bswap) {
    285 		dc->dc_hwflags |= IGSFB_HW_BSWAP;
    286 		if (dc->dc_id >= 0x2000) {
    287 			dc->dc_hwflags |= IGSFB_HW_BE_SELECT;
    288 			fbaddr |= IGS_MEM_BE_SELECT;
    289 		}
    290 	}
    291 
    292 	/*
    293 	 * XXX: TODO: make it possible to select the desired video mode.
    294 	 * For now - hardcode to 1024x768/8bpp.  This is what Krups OFW uses.
    295 	 */
    296 	igsfb_hw_setup(dc);
    297 	delay(10000);		/* XXX: uwe */
    298 
    299 	dc->dc_width = 1024;
    300 	dc->dc_height = 768;
    301 	dc->dc_depth = 8;
    302 
    303 	/*
    304 	 * Don't map in all N megs, just the amount we need for the wsscreen.
    305 	 */
    306 	dc->dc_fbsz = dc->dc_width * dc->dc_height; /* XXX: 8bpp specific */
    307 	if (bus_space_map(dc->dc_memt, fbaddr, dc->dc_fbsz,
    308 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    309 			  &dc->dc_fbh) != 0)
    310 	{
    311 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
    312 		printf("unable to map framebuffer\n");
    313 		return (1);
    314 	}
    315 
    316 	igsfb_init_cmap(dc);
    317 
    318 	/*
    319 	 * 1KB for cursor sprite data at the very end of the video memory.
    320 	 */
    321 	croffset = dc->dc_vmemsz - IGS_CURSOR_DATA_SIZE;
    322 	craddr = fbaddr + croffset;
    323 	if (bus_space_map(dc->dc_memt, craddr, IGS_CURSOR_DATA_SIZE,
    324 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    325 			  &dc->dc_crh) != 0)
    326 	{
    327 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
    328 		bus_space_unmap(dc->dc_memt, dc->dc_fbh, dc->dc_fbsz);
    329 		printf("unable to map cursor sprite region\n");
    330 		return (1);
    331 	}
    332 
    333 	/*
    334 	 * Tell the device where cursor sprite data are located in the
    335 	 * linear space (it takes data offset in 1KB units).
    336 	 */
    337 	croffset >>= 10;	/* bytes -> kilobytes */
    338 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    339 		      IGS_EXT_SPRITE_DATA_LO, croffset & 0xff);
    340 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    341 		      IGS_EXT_SPRITE_DATA_HI, (croffset >> 8) & 0xf);
    342 
    343 	/* init the bit expansion table for cursor sprite data conversion */
    344 	igsfb_init_bit_table(dc);
    345 
    346 	/* XXX: fill dc_cursor and use igsfb_update_cursor() instead? */
    347 	memset(&dc->dc_cursor, 0, sizeof(struct igs_hwcursor));
    348 	memset(bus_space_vaddr(dc->dc_memt, dc->dc_crh),
    349 	       /* transparent */ 0xaa, IGS_CURSOR_DATA_SIZE);
    350 
    351 	curctl = igs_ext_read(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL);
    352 	curctl |= IGS_EXT_SPRITE_64x64;
    353 	curctl &= ~IGS_EXT_SPRITE_VISIBLE;
    354 	igs_ext_write(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL, curctl);
    355 	dc->dc_curenb = 0;
    356 
    357 	/*
    358 	 * Map and init graphic coprocessor for accelerated rasops.
    359 	 */
    360 	if (dc->dc_id >= 0x2000) { /* XXX */
    361 		if (bus_space_map(dc->dc_iot,
    362 				  dc->dc_iobase + IGS_COP_BASE_B, IGS_COP_SIZE,
    363 				  dc->dc_ioflags,
    364 				  &dc->dc_coph) != 0)
    365 		{
    366 			printf("unable to map COP registers\n");
    367 			return (1);
    368 		}
    369 
    370 		/* XXX: hardcoded 8bpp */
    371 		bus_space_write_2(dc->dc_iot, dc->dc_coph,
    372 				  IGS_COP_SRC_MAP_WIDTH_REG,
    373 				  dc->dc_width - 1);
    374 		bus_space_write_2(dc->dc_iot, dc->dc_coph,
    375 				  IGS_COP_DST_MAP_WIDTH_REG,
    376 				  dc->dc_width - 1);
    377 
    378 		bus_space_write_1(dc->dc_iot, dc->dc_coph,
    379 				  IGS_COP_MAP_FMT_REG,
    380 				  IGS_COP_MAP_8BPP);
    381 	}
    382 
    383 	/* make sure screen is not blanked */
    384 	dc->dc_blanked = 0;
    385 	igsfb_blank_screen(dc, dc->dc_blanked);
    386 
    387 	return (0);
    388 }
    389 
    390 
    391 static void
    392 igsfb_init_cmap(dc)
    393 	struct igsfb_devconfig *dc;
    394 {
    395 	bus_space_tag_t iot = dc->dc_iot;
    396 	bus_space_handle_t ioh = dc->dc_ioh;
    397 	const uint8_t *p;
    398 	int i;
    399 
    400 	p = rasops_cmap;	/* "ANSI" color map */
    401 
    402 	/* init software copy */
    403 	for (i = 0; i < IGS_CMAP_SIZE; ++i, p += 3) {
    404 		dc->dc_cmap.r[i] = p[0];
    405 		dc->dc_cmap.g[i] = p[1];
    406 		dc->dc_cmap.b[i] = p[2];
    407 	}
    408 
    409 	/* propagate to the device */
    410 	igsfb_update_cmap(dc, 0, IGS_CMAP_SIZE);
    411 
    412 	/* set overscan color (XXX: use defattr's background?) */
    413 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_RED,   0);
    414 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_GREEN, 0);
    415 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_BLUE,  0);
    416 }
    417 
    418 
    419 static void
    420 igsfb_init_wsdisplay(void *cookie, struct vcons_screen *scr, int existing,
    421     long *defattr)
    422 {
    423 	struct igsfb_devconfig *dc = cookie;
    424 	struct rasops_info *ri = &scr->scr_ri;
    425 	int wsfcookie;
    426 
    427 	if ((scr == &dc->dc_console) && (dc->dc_vd.active != NULL))
    428 		return;
    429 
    430 
    431 	ri->ri_flg = RI_CENTER;
    432 	if (IGSFB_HW_SOFT_BSWAP(dc))
    433 	    ri->ri_flg |= RI_BSWAP;
    434 
    435 	ri->ri_depth = dc->dc_depth;
    436 	ri->ri_width = dc->dc_width;
    437 	ri->ri_height = dc->dc_height;
    438 
    439 	ri->ri_stride = dc->dc_width; /* XXX: 8bpp specific */
    440 	ri->ri_bits = (u_char *)dc->dc_fbh;
    441 
    442 	/*
    443 	 * Initialize wsfont related stuff.
    444 	 */
    445 	wsfont_init();
    446 
    447 	/* prefer gallant that is identical to the one the prom uses */
    448 	wsfcookie = wsfont_find("Gallant", 12, 22, 0,
    449 				WSDISPLAY_FONTORDER_L2R,
    450 				WSDISPLAY_FONTORDER_L2R);
    451 	if (wsfcookie <= 0) {
    452 #ifdef DIAGNOSTIC
    453 		printf("unable to find font Gallant 12x22\n");
    454 #endif
    455 		wsfcookie = wsfont_find(NULL, 0, 0, 0, /* any font at all? */
    456 					WSDISPLAY_FONTORDER_L2R,
    457 					WSDISPLAY_FONTORDER_L2R);
    458 	}
    459 
    460 	if (wsfcookie <= 0) {
    461 		printf("unable to find any fonts\n");
    462 		return;
    463 	}
    464 
    465 	if (wsfont_lock(wsfcookie, &ri->ri_font) != 0) {
    466 		printf("unable to lock font\n");
    467 		return;
    468 	}
    469 	ri->ri_wsfcookie = wsfcookie;
    470 
    471 
    472 	/* XXX: TODO: compute term size based on font dimensions? */
    473 	rasops_init(ri, 34, 80);
    474 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    475 	    ri->ri_width / ri->ri_font->fontwidth);
    476 
    477 
    478 	/* use the sprite for the text mode cursor */
    479 	igsfb_make_text_cursor(dc, scr);
    480 
    481 	/* the cursor is "busy" while we are in the text mode */
    482 	dc->dc_hwflags |= IGSFB_HW_TEXT_CURSOR;
    483 
    484 	/* propagate sprite data to the device */
    485 	igsfb_update_cursor(dc, WSDISPLAY_CURSOR_DOSHAPE);
    486 
    487 	/* accelerated text cursor */
    488 	ri->ri_ops.cursor = igsfb_accel_cursor;
    489 
    490 	if (dc->dc_id >= 0x2000) { /* XXX */
    491 	    /* accelerated erase/copy */
    492 	    ri->ri_ops.copycols = igsfb_accel_copycols;
    493 	    ri->ri_ops.erasecols = igsfb_accel_erasecols;
    494 	    ri->ri_ops.copyrows = igsfb_accel_copyrows;
    495 	    ri->ri_ops.eraserows = igsfb_accel_eraserows;
    496 
    497 	    /* putchar hook to sync with the cop */
    498 	    dc->dc_ri_putchar = ri->ri_ops.putchar;
    499 	    ri->ri_ops.putchar = igsfb_accel_putchar;
    500 	}
    501 
    502 	igsfb_stdscreen.nrows = ri->ri_rows;
    503 	igsfb_stdscreen.ncols = ri->ri_cols;
    504 	igsfb_stdscreen.textops = &ri->ri_ops;
    505 	igsfb_stdscreen.capabilities = ri->ri_caps;
    506 }
    507 
    508 
    509 /*
    510  * Init cursor data in dc_cursor for the accelerated text cursor.
    511  */
    512 static int
    513 igsfb_make_text_cursor(dc, scr)
    514 	struct igsfb_devconfig *dc;
    515 	struct vcons_screen *scr;
    516 {
    517 	struct rasops_info *ri = &scr->scr_ri;
    518 	struct wsdisplay_font *f = ri->ri_font;
    519 	uint16_t cc_scan[8];	/* one sprite scanline */
    520 	uint16_t s;
    521 	int w, i;
    522 
    523 	KASSERT(f->fontwidth <= IGS_CURSOR_MAX_SIZE);
    524 	KASSERT(f->fontheight <= IGS_CURSOR_MAX_SIZE);
    525 
    526 	w = f->fontwidth;
    527 	for (i = 0; i < f->stride; ++i) {
    528 		if (w >= 8) {
    529 			s = 0xffff; /* all inverted */
    530 			w -= 8;
    531 		} else {
    532 			/* first w pixels inverted, the rest is transparent */
    533 			s = ~(0x5555 << (w * 2));
    534 			if (IGSFB_HW_SOFT_BSWAP(dc))
    535 				s = bswap16(s);
    536 			w = 0;
    537 		}
    538 		cc_scan[i] = s;
    539 	}
    540 
    541 	dc->dc_cursor.cc_size.x = f->fontwidth;
    542 	dc->dc_cursor.cc_size.y = f->fontheight;
    543 
    544 	dc->dc_cursor.cc_hot.x = 0;
    545 	dc->dc_cursor.cc_hot.y = 0;
    546 
    547 	/* init sprite array to be all transparent */
    548 	memset(dc->dc_cursor.cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
    549 
    550 	for (i = 0; i < f->fontheight; ++i)
    551 		memcpy(&dc->dc_cursor.cc_sprite[i * 8],
    552 		       cc_scan, f->stride * sizeof(uint16_t));
    553 
    554 	return (0);
    555 }
    556 
    557 
    558 /*
    559  * Spread a byte (abcd.efgh) into two (0a0b.0c0d 0e0f.0g0h).
    560  * Helper function for igsfb_init_bit_table().
    561  */
    562 static uint16_t
    563 igsfb_spread_bits_8(b)
    564 	uint8_t b;
    565 {
    566 	uint16_t s = b;
    567 
    568 	s = ((s & 0x00f0) << 4) | (s & 0x000f);
    569 	s = ((s & 0x0c0c) << 2) | (s & 0x0303);
    570 	s = ((s & 0x2222) << 1) | (s & 0x1111);
    571 	return (s);
    572 }
    573 
    574 
    575 /*
    576  * Cursor sprite data are in 2bpp.  Incoming image/mask are in 1bpp.
    577  * Prebuild the table to expand 1bpp->2bpp, with bswapping if necessary.
    578  */
    579 static void
    580 igsfb_init_bit_table(dc)
    581 	struct igsfb_devconfig *dc;
    582 {
    583 	uint16_t *expand = dc->dc_bexpand;
    584 	int need_bswap = IGSFB_HW_SOFT_BSWAP(dc);
    585 	uint16_t s;
    586 	u_int i;
    587 
    588 	for (i = 0; i < 256; ++i) {
    589 		s = igsfb_spread_bits_8(i);
    590 		expand[i] = need_bswap ? bswap16(s) : s;
    591 	}
    592 }
    593 
    594 
    595 /*
    596  * wsdisplay_accessops: mmap()
    597  *   XXX: allow mmapping i/o mapped i/o regs if INSECURE???
    598  */
    599 static paddr_t
    600 igsfb_mmap(v, offset, prot)
    601 	void *v;
    602 	off_t offset;
    603 	int prot;
    604 {
    605 	struct vcons_data *vd = v;
    606 	struct igsfb_devconfig *dc = vd->cookie;
    607 
    608 	if (offset >= dc->dc_memsz || offset < 0)
    609 		return (-1);
    610 
    611 	return (bus_space_mmap(dc->dc_memt, dc->dc_memaddr, offset, prot,
    612 			       dc->dc_memflags | BUS_SPACE_MAP_LINEAR));
    613 }
    614 
    615 
    616 /*
    617  * wsdisplay_accessops: ioctl()
    618  */
    619 static int
    620 igsfb_ioctl(v, cmd, data, flag, l)
    621 	void *v;
    622 	u_long cmd;
    623 	caddr_t data;
    624 	int flag;
    625 	struct lwp *l;
    626 {
    627 	struct vcons_data *vd = v;
    628 	struct igsfb_devconfig *dc = vd->cookie;
    629 	struct rasops_info *ri;
    630 	int cursor_busy;
    631 	int turnoff;
    632 
    633 	/* don't permit cursor ioctls if we use sprite for text cursor */
    634 	cursor_busy = !dc->dc_mapped
    635 		&& (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR);
    636 
    637 	switch (cmd) {
    638 
    639 	case WSDISPLAYIO_GTYPE:
    640 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    641 		return (0);
    642 
    643 	case WSDISPLAYIO_GINFO:
    644 		ri = &dc->dc_vd.active->scr_ri;
    645 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    646 		wsd_fbip->height = ri->ri_height;
    647 		wsd_fbip->width = ri->ri_width;
    648 		wsd_fbip->depth = ri->ri_depth;
    649 		wsd_fbip->cmsize = IGS_CMAP_SIZE;
    650 #undef wsd_fbip
    651 		return (0);
    652 
    653 	case WSDISPLAYIO_LINEBYTES:
    654 		ri = &dc->dc_vd.active->scr_ri;
    655 		*(int *)data = ri->ri_stride;
    656 		return (0);
    657 
    658 	case WSDISPLAYIO_SMODE:
    659 #define d (*(int *)data)
    660 		if (d != WSDISPLAYIO_MODE_EMUL) {
    661 			dc->dc_mapped = 1;
    662 			/* turn off hardware cursor */
    663 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
    664 				dc->dc_curenb = 0;
    665 				igsfb_update_cursor(dc,
    666 					WSDISPLAY_CURSOR_DOCUR);
    667 			}
    668 		} else {
    669 			dc->dc_mapped = 0;
    670 			/* reinit sprite for text cursor */
    671 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
    672 				igsfb_make_text_cursor(dc, dc->dc_vd.active);
    673 				dc->dc_curenb = 0;
    674 				igsfb_update_cursor(dc,
    675 					  WSDISPLAY_CURSOR_DOSHAPE
    676 					| WSDISPLAY_CURSOR_DOCUR);
    677 			}
    678 			vcons_redraw_screen(vd->active);
    679 		}
    680 		return (0);
    681 
    682 	case WSDISPLAYIO_GVIDEO:
    683 		*(u_int *)data = dc->dc_blanked ?
    684 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    685 		return (0);
    686 
    687 	case WSDISPLAYIO_SVIDEO:
    688 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
    689 		if (dc->dc_blanked != turnoff) {
    690 			dc->dc_blanked = turnoff;
    691 			igsfb_blank_screen(dc, dc->dc_blanked);
    692 		}
    693 		return (0);
    694 
    695 	case WSDISPLAYIO_GETCMAP:
    696 		return (igsfb_get_cmap(dc, (struct wsdisplay_cmap *)data));
    697 
    698 	case WSDISPLAYIO_PUTCMAP:
    699 		return (igsfb_set_cmap(dc, (struct wsdisplay_cmap *)data));
    700 
    701 	case WSDISPLAYIO_GCURMAX:
    702 		((struct wsdisplay_curpos *)data)->x = IGS_CURSOR_MAX_SIZE;
    703 		((struct wsdisplay_curpos *)data)->y = IGS_CURSOR_MAX_SIZE;
    704 		return (0);
    705 
    706 	case WSDISPLAYIO_GCURPOS:
    707 		if (cursor_busy)
    708 			return (EBUSY);
    709 		*(struct wsdisplay_curpos *)data = dc->dc_cursor.cc_pos;
    710 		return (0);
    711 
    712 	case WSDISPLAYIO_SCURPOS:
    713 		if (cursor_busy)
    714 			return (EBUSY);
    715 		igsfb_set_curpos(dc, (struct wsdisplay_curpos *)data);
    716 		return (0);
    717 
    718 	case WSDISPLAYIO_GCURSOR:
    719 		if (cursor_busy)
    720 			return (EBUSY);
    721 		return (igsfb_get_cursor(dc, (struct wsdisplay_cursor *)data));
    722 
    723 	case WSDISPLAYIO_SCURSOR:
    724 		if (cursor_busy)
    725 			return (EBUSY);
    726 		return (igsfb_set_cursor(dc, (struct wsdisplay_cursor *)data));
    727 	}
    728 
    729 	return (EPASSTHROUGH);
    730 }
    731 
    732 
    733 /*
    734  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SVIDEO)
    735  */
    736 static void
    737 igsfb_blank_screen(dc, blank)
    738 	struct igsfb_devconfig *dc;
    739 	int blank;
    740 {
    741 
    742 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    743 		      IGS_EXT_SYNC_CTL,
    744 		      blank ? IGS_EXT_SYNC_H0 | IGS_EXT_SYNC_V0
    745 			    : 0);
    746 }
    747 
    748 
    749 /*
    750  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GETCMAP)
    751  *   Served from the software cmap copy.
    752  */
    753 static int
    754 igsfb_get_cmap(dc, p)
    755 	struct igsfb_devconfig *dc;
    756 	struct wsdisplay_cmap *p;
    757 {
    758 	u_int index, count;
    759 	int err;
    760 
    761 	index = p->index;
    762 	count = p->count;
    763 
    764 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    765 		return (EINVAL);
    766 
    767 	err = copyout(&dc->dc_cmap.r[index], p->red, count);
    768 	if (err)
    769 		return (err);
    770 	err = copyout(&dc->dc_cmap.g[index], p->green, count);
    771 	if (err)
    772 		return (err);
    773 	err = copyout(&dc->dc_cmap.b[index], p->blue, count);
    774 	if (err)
    775 		return (err);
    776 
    777 	return (0);
    778 }
    779 
    780 
    781 /*
    782  * wsdisplay_accessops: ioctl(WSDISPLAYIO_PUTCMAP)
    783  *   Set the software cmap copy and propagate changed range to the device.
    784  */
    785 static int
    786 igsfb_set_cmap(dc, p)
    787 	struct igsfb_devconfig *dc;
    788 	const struct wsdisplay_cmap *p;
    789 {
    790 	u_int index, count;
    791 	uint8_t r[IGS_CMAP_SIZE];
    792 	uint8_t g[IGS_CMAP_SIZE];
    793 	uint8_t b[IGS_CMAP_SIZE];
    794 	int error;
    795 
    796 	index = p->index;
    797 	count = p->count;
    798 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    799 		return (EINVAL);
    800 	error = copyin(p->red, &r[index], count);
    801 	if (error)
    802 		return error;
    803 	error = copyin(p->green, &g[index], count);
    804 	if (error)
    805 		return error;
    806 	error = copyin(p->blue, &b[index], count);
    807 	if (error)
    808 		return error;
    809 
    810 	memcpy(&dc->dc_cmap.r[index], &r[index], count);
    811 	memcpy(&dc->dc_cmap.g[index], &g[index], count);
    812 	memcpy(&dc->dc_cmap.b[index], &b[index], count);
    813 
    814 	/* propagate changes to the device */
    815 	igsfb_update_cmap(dc, index, count);
    816 	return (0);
    817 }
    818 
    819 
    820 /*
    821  * Propagate specified part of the software cmap copy to the device.
    822  */
    823 static void
    824 igsfb_update_cmap(dc, index, count)
    825 	struct igsfb_devconfig *dc;
    826 	u_int index, count;
    827 {
    828 	bus_space_tag_t t;
    829 	bus_space_handle_t h;
    830 	u_int last, i;
    831 
    832 	if (index >= IGS_CMAP_SIZE)
    833 		return;
    834 
    835 	last = index + count;
    836 	if (last > IGS_CMAP_SIZE)
    837 		last = IGS_CMAP_SIZE;
    838 
    839 	t = dc->dc_iot;
    840 	h = dc->dc_ioh;
    841 
    842 	/* start palette writing, index is autoincremented by hardware */
    843 	bus_space_write_1(t, h, IGS_DAC_PEL_WRITE_IDX, index);
    844 
    845 	for (i = index; i < last; ++i) {
    846 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.r[i]);
    847 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.g[i]);
    848 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.b[i]);
    849 	}
    850 }
    851 
    852 
    853 /*
    854  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURPOS)
    855  */
    856 static void
    857 igsfb_set_curpos(dc, curpos)
    858 	struct igsfb_devconfig *dc;
    859 	const struct wsdisplay_curpos *curpos;
    860 {
    861 	struct rasops_info *ri = &dc->dc_vd.active->scr_ri;
    862 	u_int x = curpos->x, y = curpos->y;
    863 
    864 	if (x >= ri->ri_width)
    865 		x = ri->ri_width - 1;
    866 	if (y >= ri->ri_height)
    867 		y = ri->ri_height - 1;
    868 
    869 	dc->dc_cursor.cc_pos.x = x;
    870 	dc->dc_cursor.cc_pos.y = y;
    871 
    872 	/* propagate changes to the device */
    873 	igsfb_update_curpos(dc);
    874 }
    875 
    876 
    877 static void
    878 igsfb_update_curpos(dc)
    879 	struct igsfb_devconfig *dc;
    880 {
    881 	bus_space_tag_t t;
    882 	bus_space_handle_t h;
    883 	int x, xoff, y, yoff;
    884 
    885 	xoff = 0;
    886 	x = dc->dc_cursor.cc_pos.x - dc->dc_cursor.cc_hot.x;
    887 	if (x < 0) {
    888 		xoff = -x;
    889 		x = 0;
    890 	}
    891 
    892 	yoff = 0;
    893 	y = dc->dc_cursor.cc_pos.y - dc->dc_cursor.cc_hot.y;
    894 	if (y < 0) {
    895 		yoff = -y;
    896 		y = 0;
    897 	}
    898 
    899 	t = dc->dc_iot;
    900 	h = dc->dc_ioh;
    901 
    902 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_LO, x & 0xff);
    903 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_HI, (x >> 8) & 0x07);
    904 	igs_ext_write(t, h, IGS_EXT_SPRITE_HPRESET, xoff & 0x3f);
    905 
    906 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_LO, y & 0xff);
    907 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_HI, (y >> 8) & 0x07);
    908 	igs_ext_write(t, h, IGS_EXT_SPRITE_VPRESET, yoff & 0x3f);
    909 }
    910 
    911 
    912 /*
    913  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GCURSOR)
    914  */
    915 static int
    916 igsfb_get_cursor(dc, p)
    917 	struct igsfb_devconfig *dc;
    918 	struct wsdisplay_cursor *p;
    919 {
    920 
    921 	/* XXX: TODO */
    922 	return (0);
    923 }
    924 
    925 
    926 /*
    927  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURSOR)
    928  */
    929 static int
    930 igsfb_set_cursor(dc, p)
    931 	struct igsfb_devconfig *dc;
    932 	const struct wsdisplay_cursor *p;
    933 {
    934 	struct igs_hwcursor *cc;
    935 	struct wsdisplay_curpos pos, hot;
    936 	u_int v, index, count, icount, iwidth;
    937 	uint8_t r[2], g[2], b[2], image[512], mask[512];
    938 	int error;
    939 
    940 	cc = &dc->dc_cursor;
    941 	v = p->which;
    942 	index = count = icount = iwidth = 0;	/* XXX: gcc */
    943 
    944 	/* copy in the new cursor colormap */
    945 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    946 		index = p->cmap.index;
    947 		count = p->cmap.count;
    948 		if (index >= 2 || (index + count) > 2)
    949 			return (EINVAL);
    950 		error = copyin(p->cmap.red, &r[index], count);
    951 		if (error)
    952 			return error;
    953 		error = copyin(p->cmap.green, &g[index], count);
    954 		if (error)
    955 			return error;
    956 		error = copyin(p->cmap.blue, &b[index], count);
    957 		if (error)
    958 			return error;
    959 	}
    960 
    961 	/* verify that the new cursor data are valid */
    962 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    963 		if (p->size.x > IGS_CURSOR_MAX_SIZE
    964 		    || p->size.y > IGS_CURSOR_MAX_SIZE)
    965 			return (EINVAL);
    966 
    967 		iwidth = (p->size.x + 7) >> 3; /* bytes per scan line */
    968 		icount = iwidth * p->size.y;
    969 		error = copyin(p->image, image, icount);
    970 		if (error)
    971 			return error;
    972 		error = copyin(p->mask, mask, icount);
    973 		if (error)
    974 			return error;
    975 	}
    976 
    977 	/* enforce that the position is within screen bounds */
    978 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    979 		struct rasops_info *ri = &dc->dc_vd.active->scr_ri;
    980 
    981 		pos = p->pos;	/* local copy we can write to */
    982 		if (pos.x >= ri->ri_width)
    983 			pos.x = ri->ri_width - 1;
    984 		if (pos.y >= ri->ri_height)
    985 			pos.y = ri->ri_height - 1;
    986 	}
    987 
    988 	/* enforce that the hot spot is within sprite bounds */
    989 	if (v & WSDISPLAY_CURSOR_DOHOT)
    990 		hot = p->hot;	/* local copy we can write to */
    991 
    992 	if (v & (WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOSHAPE)) {
    993 		const struct wsdisplay_curpos *nsize;
    994 		struct wsdisplay_curpos *nhot;
    995 
    996 		nsize = (v & WSDISPLAY_CURSOR_DOSHAPE) ?
    997 			    &p->size : &cc->cc_size;
    998 		nhot = (v & WSDISPLAY_CURSOR_DOHOT) ? &hot : &cc->cc_hot;
    999 
   1000 		if (nhot->x >= nsize->x)
   1001 			nhot->x = nsize->x - 1;
   1002 		if (nhot->y >= nsize->y)
   1003 			nhot->y = nsize->y - 1;
   1004 	}
   1005 
   1006 	/* copy data to the driver's cursor info */
   1007 	if (v & WSDISPLAY_CURSOR_DOCUR)
   1008 		dc->dc_curenb = p->enable;
   1009 	if (v & WSDISPLAY_CURSOR_DOPOS)
   1010 		cc->cc_pos = pos; /* local copy, possibly corrected */
   1011 	if (v & WSDISPLAY_CURSOR_DOHOT)
   1012 		cc->cc_hot = hot; /* local copy, possibly corrected */
   1013 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
   1014 		memcpy(&cc->cc_color[index], &r[index], count);
   1015 		memcpy(&cc->cc_color[index + 2], &g[index], count);
   1016 		memcpy(&cc->cc_color[index + 4], &b[index], count);
   1017 	}
   1018 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
   1019 		u_int trailing_bits;
   1020 
   1021 		memcpy(cc->cc_image, image, icount);
   1022 		memcpy(cc->cc_mask, mask, icount);
   1023 		cc->cc_size = p->size;
   1024 
   1025 		/* clear trailing bits in the "partial" mask bytes */
   1026 		trailing_bits = p->size.x & 0x07;
   1027 		if (trailing_bits != 0) {
   1028 			const u_int cutmask = ~((~0) << trailing_bits);
   1029 			u_char *mp;
   1030 			u_int i;
   1031 
   1032 			mp = cc->cc_mask + iwidth - 1;
   1033 			for (i = 0; i < p->size.y; ++i) {
   1034 				*mp &= cutmask;
   1035 				mp += iwidth;
   1036 			}
   1037 		}
   1038 		igsfb_convert_cursor_data(dc, iwidth, p->size.y);
   1039 	}
   1040 
   1041 	/* propagate changes to the device */
   1042 	igsfb_update_cursor(dc, v);
   1043 
   1044 	return (0);
   1045 }
   1046 
   1047 
   1048 /*
   1049  * Convert incoming 1bpp cursor image/mask into native 2bpp format.
   1050  */
   1051 static void
   1052 igsfb_convert_cursor_data(dc, width, height)
   1053 	struct igsfb_devconfig *dc;
   1054 	u_int width, height;
   1055 {
   1056 	struct igs_hwcursor *cc = &dc->dc_cursor;
   1057 	uint16_t *expand = dc->dc_bexpand;
   1058 	uint8_t *ip, *mp;
   1059 	uint16_t is, ms, *dp;
   1060 	u_int line, i;
   1061 
   1062 	/* init sprite to be all transparent */
   1063 	memset(cc->cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
   1064 
   1065 	/* first scanline */
   1066 	ip = cc->cc_image;
   1067 	mp = cc->cc_mask;
   1068 	dp = cc->cc_sprite;
   1069 
   1070 	for (line = 0; line < height; ++line) {
   1071 		for (i = 0; i < width; ++i) {
   1072 			is = expand[ip[i]]; /* image: 0 -> 00, 1 -> 01 */
   1073 			ms = expand[mp[i]]; /*  mask: 0 -> 00, 1 -> 11 */
   1074 			ms |= (ms << 1);
   1075 			dp[i] = (0xaaaa & ~ms) | (is & ms);
   1076 		}
   1077 
   1078 		/* next scanline */
   1079 		ip += width;
   1080 		mp += width;
   1081 		dp += 8; /* 64 pixels, 2bpp, 8 pixels per short = 8 shorts */
   1082 	}
   1083 }
   1084 
   1085 
   1086 /*
   1087  * Propagate cursor changes to the device.
   1088  * "which" is composed of WSDISPLAY_CURSOR_DO* bits.
   1089  */
   1090 static void
   1091 igsfb_update_cursor(dc, which)
   1092 	struct igsfb_devconfig *dc;
   1093 	u_int which;
   1094 {
   1095 	bus_space_tag_t iot = dc->dc_iot;
   1096 	bus_space_handle_t ioh = dc->dc_ioh;
   1097 	uint8_t curctl;
   1098 
   1099 	curctl = 0;		/* XXX: gcc */
   1100 
   1101 	/*
   1102 	 * We will need to tweak sprite control register for cursor
   1103 	 * visibility and cursor color map manipulation.
   1104 	 */
   1105 	if (which & (WSDISPLAY_CURSOR_DOCUR | WSDISPLAY_CURSOR_DOCMAP)) {
   1106 		curctl = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
   1107 	}
   1108 
   1109 	if (which & WSDISPLAY_CURSOR_DOSHAPE) {
   1110 		uint8_t *dst = bus_space_vaddr(dc->dc_memt, dc->dc_crh);
   1111 
   1112 		/*
   1113 		 * memcpy between spaces of different endianness would
   1114 		 * be ... special.  We cannot be sure if memcpy gonna
   1115 		 * push data in 4-byte chunks, we can't pre-bswap it,
   1116 		 * so do it byte-by-byte to preserve byte ordering.
   1117 		 */
   1118 		if (IGSFB_HW_SOFT_BSWAP(dc)) {
   1119 			uint8_t *src = (uint8_t *)dc->dc_cursor.cc_sprite;
   1120 			int i;
   1121 
   1122 			for (i = 0; i < IGS_CURSOR_DATA_SIZE; ++i)
   1123 				*dst++ = *src++;
   1124 		} else {
   1125 			memcpy(dst, dc->dc_cursor.cc_sprite,
   1126 			       IGS_CURSOR_DATA_SIZE);
   1127 		}
   1128 	}
   1129 
   1130 	if (which & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
   1131 		/* code shared with WSDISPLAYIO_SCURPOS */
   1132 		igsfb_update_curpos(dc);
   1133 	}
   1134 
   1135 	if (which & WSDISPLAY_CURSOR_DOCMAP) {
   1136 		uint8_t *p;
   1137 
   1138 		/* tell DAC we want access to the cursor palette */
   1139 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1140 			      curctl | IGS_EXT_SPRITE_DAC_PEL);
   1141 
   1142 		p = dc->dc_cursor.cc_color;
   1143 
   1144 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 0);
   1145 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[0]);
   1146 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[2]);
   1147 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[4]);
   1148 
   1149 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 1);
   1150 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[1]);
   1151 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[3]);
   1152 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[5]);
   1153 
   1154 		/* restore access to the normal palette */
   1155 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, curctl);
   1156 	}
   1157 
   1158 	if (which & WSDISPLAY_CURSOR_DOCUR) {
   1159 		if ((curctl & IGS_EXT_SPRITE_VISIBLE) == 0
   1160 		    && dc->dc_curenb)
   1161 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1162 				      curctl | IGS_EXT_SPRITE_VISIBLE);
   1163 		else if ((curctl & IGS_EXT_SPRITE_VISIBLE) != 0
   1164 			 && !dc->dc_curenb)
   1165 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1166 				      curctl & ~IGS_EXT_SPRITE_VISIBLE);
   1167 	}
   1168 }
   1169 
   1170 
   1171 /*
   1172  * Accelerated text mode cursor that uses hardware sprite.
   1173  */
   1174 static void
   1175 igsfb_accel_cursor(cookie, on, row, col)
   1176 	void *cookie;
   1177 	int on, row, col;
   1178 {
   1179 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1180 	struct vcons_screen *scr = ri->ri_hw;
   1181 	struct igsfb_devconfig *dc = scr->scr_cookie;
   1182 	struct igs_hwcursor *cc = &dc->dc_cursor;
   1183 	u_int which;
   1184 
   1185 	ri->ri_crow = row;
   1186 	ri->ri_ccol = col;
   1187 
   1188 	which = 0;
   1189 	if (on) {
   1190 		ri->ri_flg |= RI_CURSOR;
   1191 
   1192 		/* only bother to move the cursor if it's visible */
   1193 		cc->cc_pos.x = ri->ri_xorigin
   1194 			+ ri->ri_ccol * ri->ri_font->fontwidth;
   1195 		cc->cc_pos.y = ri->ri_yorigin
   1196 			+ ri->ri_crow * ri->ri_font->fontheight;
   1197 		which |= WSDISPLAY_CURSOR_DOPOS;
   1198 	} else
   1199 		ri->ri_flg &= ~RI_CURSOR;
   1200 
   1201 	if (dc->dc_curenb != on) {
   1202 		dc->dc_curenb = on;
   1203 		which |= WSDISPLAY_CURSOR_DOCUR;
   1204 	}
   1205 
   1206 	/* propagate changes to the device */
   1207 	igsfb_update_cursor(dc, which);
   1208 }
   1209 
   1210 
   1211 
   1212 /*
   1213  * Accelerated raster ops that use graphic coprocessor.
   1214  */
   1215 
   1216 static int
   1217 igsfb_accel_wait(dc)
   1218 	struct igsfb_devconfig *dc;
   1219 {
   1220 	bus_space_tag_t t = dc->dc_iot;
   1221 	bus_space_handle_t h = dc->dc_coph;
   1222 	int timo = 100000;
   1223 	uint8_t reg;
   1224 
   1225 	while (timo--) {
   1226 		reg = bus_space_read_1(t, h, IGS_COP_CTL_REG);
   1227 		if ((reg & IGS_COP_CTL_BUSY) == 0)
   1228 			return (0);
   1229 	}
   1230 
   1231 	return (1);
   1232 }
   1233 
   1234 
   1235 static void
   1236 igsfb_accel_copy(dc, src, dst, width, height)
   1237 	struct igsfb_devconfig *dc;
   1238 	uint32_t src, dst;
   1239 	uint16_t width, height;
   1240 {
   1241 	bus_space_tag_t t = dc->dc_iot;
   1242 	bus_space_handle_t h = dc->dc_coph;
   1243 	uint32_t toend;
   1244 	uint8_t drawcmd;
   1245 
   1246 	drawcmd = IGS_COP_DRAW_ALL;
   1247 	if (dst > src) {
   1248 		toend = dc->dc_vd.active->scr_ri.ri_width * (height - 1) + (width - 1);
   1249 		src += toend;
   1250 		dst += toend;
   1251 		drawcmd |= IGS_COP_OCTANT_X_NEG | IGS_COP_OCTANT_Y_NEG;
   1252 	}
   1253 
   1254 	igsfb_accel_wait(dc);
   1255 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
   1256 
   1257 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
   1258 
   1259 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
   1260 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
   1261 
   1262 	bus_space_write_4(t, h, IGS_COP_SRC_START_REG, src);
   1263 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
   1264 
   1265 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, drawcmd);
   1266 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
   1267 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
   1268 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG,
   1269 			  IGS_COP_OP_PXBLT | IGS_COP_OP_FG_FROM_SRC);
   1270 }
   1271 
   1272 static void
   1273 igsfb_accel_fill(dc, color, dst, width, height)
   1274 	struct igsfb_devconfig *dc;
   1275 	uint32_t color;
   1276 	uint32_t dst;
   1277 	uint16_t width, height;
   1278 {
   1279 	bus_space_tag_t t = dc->dc_iot;
   1280 	bus_space_handle_t h = dc->dc_coph;
   1281 
   1282 	igsfb_accel_wait(dc);
   1283 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
   1284 
   1285 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
   1286 
   1287 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
   1288 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
   1289 
   1290 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
   1291 	bus_space_write_4(t, h, IGS_COP_FG_REG, color);
   1292 
   1293 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, IGS_COP_DRAW_ALL);
   1294 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
   1295 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
   1296 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG, IGS_COP_OP_PXBLT);
   1297 }
   1298 
   1299 
   1300 static void
   1301 igsfb_accel_copyrows(cookie, src, dst, num)
   1302 	void *cookie;
   1303 	int src, dst, num;
   1304 {
   1305 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1306 	struct vcons_screen *scr = ri->ri_hw;
   1307 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1308 	uint32_t srp, dsp;
   1309 	uint16_t width, height;
   1310 
   1311 	width = ri->ri_emuwidth;
   1312 	height = num * ri->ri_font->fontheight;
   1313 
   1314 	srp = ri->ri_xorigin
   1315 		+ ri->ri_width * (ri->ri_yorigin
   1316 				  + src * ri->ri_font->fontheight);
   1317 	dsp = ri->ri_xorigin
   1318 		+ ri->ri_width * (ri->ri_yorigin
   1319 				  + dst * ri->ri_font->fontheight);
   1320 
   1321 	igsfb_accel_copy(dc, srp, dsp, width, height);
   1322 }
   1323 
   1324 
   1325 void
   1326 igsfb_accel_copycols(cookie, row, src, dst, num)
   1327 	void *cookie;
   1328 	int row, src, dst, num;
   1329 {
   1330 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1331 	struct vcons_screen *scr = ri->ri_hw;
   1332 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1333 	uint32_t rowp, srp, dsp;
   1334 	uint16_t width, height;
   1335 
   1336 	width = num * ri->ri_font->fontwidth;
   1337 	height = ri->ri_font->fontheight;
   1338 
   1339 	rowp = ri->ri_xorigin
   1340 		+ ri->ri_width * (ri->ri_yorigin
   1341 				  + row * ri->ri_font->fontheight);
   1342 
   1343 	srp = rowp + src * ri->ri_font->fontwidth;
   1344 	dsp = rowp + dst * ri->ri_font->fontwidth;
   1345 
   1346 	igsfb_accel_copy(dc, srp, dsp, width, height);
   1347 }
   1348 
   1349 
   1350 static void
   1351 igsfb_accel_eraserows(cookie, row, num, attr)
   1352 	void *cookie;
   1353 	int row, num;
   1354 	long attr;
   1355 {
   1356 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1357 	struct vcons_screen *scr = ri->ri_hw;
   1358 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1359 	uint32_t color;
   1360 	uint32_t dsp;
   1361 	uint16_t width, height;
   1362 
   1363 	width = ri->ri_emuwidth;
   1364 	height = num * ri->ri_font->fontheight;
   1365 
   1366 	dsp = ri->ri_xorigin
   1367 		+ ri->ri_width * (ri->ri_yorigin
   1368 				  + row * ri->ri_font->fontheight);
   1369 
   1370 	/* XXX: we "know" the encoding that rasops' allocattr uses */
   1371 	color = (attr >> 16) & 0xff;
   1372 
   1373 	igsfb_accel_fill(dc, color, dsp, width, height);
   1374 }
   1375 
   1376 
   1377 void
   1378 igsfb_accel_erasecols(cookie, row, col, num, attr)
   1379 	void *cookie;
   1380 	int row, col, num;
   1381 	long attr;
   1382 {
   1383 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1384 	struct vcons_screen *scr = ri->ri_hw;
   1385 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1386 	uint32_t color;
   1387 	uint32_t rowp, dsp;
   1388 	uint16_t width, height;
   1389 
   1390 	width = num * ri->ri_font->fontwidth;
   1391 	height = ri->ri_font->fontheight;
   1392 
   1393 	rowp = ri->ri_xorigin
   1394 		+ ri->ri_width * (ri->ri_yorigin
   1395 				  + row * ri->ri_font->fontheight);
   1396 
   1397 	dsp = rowp + col * ri->ri_font->fontwidth;
   1398 
   1399 	/* XXX: we "know" the encoding that rasops' allocattr uses */
   1400 	color = (attr >> 16) & 0xff;
   1401 
   1402 	igsfb_accel_fill(dc, color, dsp, width, height);
   1403 }
   1404 
   1405 
   1406 /*
   1407  * Not really implemented here, but we need to hook into the one
   1408  * supplied by rasops so that we can synchronize with the COP.
   1409  */
   1410 static void
   1411 igsfb_accel_putchar(cookie, row, col, uc, attr)
   1412 	void *cookie;
   1413 	int row, col;
   1414 	u_int uc;
   1415 	long attr;
   1416 {
   1417 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1418 	struct vcons_screen *scr = ri->ri_hw;
   1419 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1420 
   1421 	igsfb_accel_wait(dc);
   1422 	(*dc->dc_ri_putchar)(cookie, row, col, uc, attr);
   1423 }
   1424