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