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