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