Home | History | Annotate | Line # | Download | only in ic
igsfb.c revision 1.35
      1 /*	$NetBSD: igsfb.c,v 1.35 2006/04/04 23:43:40 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.35 2006/04/04 23:43:40 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 	dc->dc_stride = dc->dc_width;
    291 
    292 	/*
    293 	 * Don't map in all N megs, just the amount we need for the wsscreen.
    294 	 */
    295 	dc->dc_fbsz = dc->dc_width * dc->dc_height; /* XXX: 8bpp specific */
    296 	if (bus_space_map(dc->dc_memt, fbaddr, dc->dc_fbsz,
    297 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    298 			  &dc->dc_fbh) != 0)
    299 	{
    300 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
    301 		printf("unable to map framebuffer\n");
    302 		return (1);
    303 	}
    304 
    305 	igsfb_init_cmap(dc);
    306 
    307 	/*
    308 	 * 1KB for cursor sprite data at the very end of the video memory.
    309 	 */
    310 	croffset = dc->dc_vmemsz - IGS_CURSOR_DATA_SIZE;
    311 	craddr = fbaddr + croffset;
    312 	if (bus_space_map(dc->dc_memt, craddr, IGS_CURSOR_DATA_SIZE,
    313 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
    314 			  &dc->dc_crh) != 0)
    315 	{
    316 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
    317 		bus_space_unmap(dc->dc_memt, dc->dc_fbh, dc->dc_fbsz);
    318 		printf("unable to map cursor sprite region\n");
    319 		return (1);
    320 	}
    321 
    322 	/*
    323 	 * Tell the device where cursor sprite data are located in the
    324 	 * linear space (it takes data offset in 1KB units).
    325 	 */
    326 	croffset >>= 10;	/* bytes -> kilobytes */
    327 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    328 		      IGS_EXT_SPRITE_DATA_LO, croffset & 0xff);
    329 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    330 		      IGS_EXT_SPRITE_DATA_HI, (croffset >> 8) & 0xf);
    331 
    332 	/* init the bit expansion table for cursor sprite data conversion */
    333 	igsfb_init_bit_table(dc);
    334 
    335 	/* XXX: fill dc_cursor and use igsfb_update_cursor() instead? */
    336 	memset(&dc->dc_cursor, 0, sizeof(struct igs_hwcursor));
    337 	memset(bus_space_vaddr(dc->dc_memt, dc->dc_crh),
    338 	       /* transparent */ 0xaa, IGS_CURSOR_DATA_SIZE);
    339 
    340 	curctl = igs_ext_read(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL);
    341 	curctl |= IGS_EXT_SPRITE_64x64;
    342 	curctl &= ~IGS_EXT_SPRITE_VISIBLE;
    343 	igs_ext_write(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL, curctl);
    344 	dc->dc_curenb = 0;
    345 
    346 	/*
    347 	 * Map and init graphic coprocessor for accelerated rasops.
    348 	 */
    349 	if (dc->dc_id >= 0x2000) { /* XXX */
    350 		if (bus_space_map(dc->dc_iot,
    351 				  dc->dc_iobase + IGS_COP_BASE_B, IGS_COP_SIZE,
    352 				  dc->dc_ioflags,
    353 				  &dc->dc_coph) != 0)
    354 		{
    355 			printf("unable to map COP registers\n");
    356 			return (1);
    357 		}
    358 
    359 		/* XXX: hardcoded 8bpp */
    360 		bus_space_write_2(dc->dc_iot, dc->dc_coph,
    361 				  IGS_COP_SRC_MAP_WIDTH_REG,
    362 				  dc->dc_width - 1);
    363 		bus_space_write_2(dc->dc_iot, dc->dc_coph,
    364 				  IGS_COP_DST_MAP_WIDTH_REG,
    365 				  dc->dc_width - 1);
    366 
    367 		bus_space_write_1(dc->dc_iot, dc->dc_coph,
    368 				  IGS_COP_MAP_FMT_REG,
    369 				  IGS_COP_MAP_8BPP);
    370 	}
    371 
    372 	/* make sure screen is not blanked */
    373 	dc->dc_blanked = 0;
    374 	igsfb_blank_screen(dc, dc->dc_blanked);
    375 
    376 	return (0);
    377 }
    378 
    379 
    380 static void
    381 igsfb_init_cmap(dc)
    382 	struct igsfb_devconfig *dc;
    383 {
    384 	bus_space_tag_t iot = dc->dc_iot;
    385 	bus_space_handle_t ioh = dc->dc_ioh;
    386 	const uint8_t *p;
    387 	int i;
    388 
    389 	p = rasops_cmap;	/* "ANSI" color map */
    390 
    391 	/* init software copy */
    392 	for (i = 0; i < IGS_CMAP_SIZE; ++i, p += 3) {
    393 		dc->dc_cmap.r[i] = p[0];
    394 		dc->dc_cmap.g[i] = p[1];
    395 		dc->dc_cmap.b[i] = p[2];
    396 	}
    397 
    398 	/* propagate to the device */
    399 	igsfb_update_cmap(dc, 0, IGS_CMAP_SIZE);
    400 
    401 	/* set overscan color (XXX: use defattr's background?) */
    402 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_RED,   0);
    403 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_GREEN, 0);
    404 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_BLUE,  0);
    405 }
    406 
    407 
    408 static void
    409 igsfb_init_wsdisplay(void *cookie, struct vcons_screen *scr, int existing,
    410     long *defattr)
    411 {
    412 	struct igsfb_devconfig *dc = cookie;
    413 	struct rasops_info *ri = &scr->scr_ri;
    414 	int wsfcookie;
    415 
    416 	if ((scr == &dc->dc_console) && (dc->dc_vd.active != NULL))
    417 		return;
    418 
    419 
    420 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    421 	if (IGSFB_HW_SOFT_BSWAP(dc))
    422 		ri->ri_flg |= RI_BSWAP;
    423 
    424 	ri->ri_depth = dc->dc_depth;
    425 	ri->ri_width = dc->dc_width;
    426 	ri->ri_height = dc->dc_height;
    427 	ri->ri_stride = dc->dc_stride;
    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 	int cursor_busy;
    616 	int turnoff;
    617 
    618 	/* don't permit cursor ioctls if we use sprite for text cursor */
    619 	cursor_busy = !dc->dc_mapped
    620 		&& (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR);
    621 
    622 	switch (cmd) {
    623 
    624 	case WSDISPLAYIO_GTYPE:
    625 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    626 		return (0);
    627 
    628 	case WSDISPLAYIO_GINFO:
    629 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    630 		wsd_fbip->height = dc->dc_height;
    631 		wsd_fbip->width = dc->dc_width;
    632 		wsd_fbip->depth = dc->dc_depth;
    633 		wsd_fbip->cmsize = IGS_CMAP_SIZE;
    634 #undef wsd_fbip
    635 		return (0);
    636 
    637 	case WSDISPLAYIO_LINEBYTES:
    638 		*(int *)data = dc->dc_stride;
    639 		return (0);
    640 
    641 	case WSDISPLAYIO_SMODE:
    642 #define d (*(int *)data)
    643 		if (d != WSDISPLAYIO_MODE_EMUL) {
    644 			dc->dc_mapped = 1;
    645 			/* turn off hardware cursor */
    646 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
    647 				dc->dc_curenb = 0;
    648 				igsfb_update_cursor(dc,
    649 					WSDISPLAY_CURSOR_DOCUR);
    650 			}
    651 		} else {
    652 			dc->dc_mapped = 0;
    653 			/* reinit sprite for text cursor */
    654 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
    655 				igsfb_make_text_cursor(dc, dc->dc_vd.active);
    656 				dc->dc_curenb = 0;
    657 				igsfb_update_cursor(dc,
    658 					  WSDISPLAY_CURSOR_DOSHAPE
    659 					| WSDISPLAY_CURSOR_DOCUR);
    660 			}
    661 			vcons_redraw_screen(vd->active);
    662 		}
    663 		return (0);
    664 
    665 	case WSDISPLAYIO_GVIDEO:
    666 		*(u_int *)data = dc->dc_blanked ?
    667 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    668 		return (0);
    669 
    670 	case WSDISPLAYIO_SVIDEO:
    671 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
    672 		if (dc->dc_blanked != turnoff) {
    673 			dc->dc_blanked = turnoff;
    674 			igsfb_blank_screen(dc, dc->dc_blanked);
    675 		}
    676 		return (0);
    677 
    678 	case WSDISPLAYIO_GETCMAP:
    679 		return (igsfb_get_cmap(dc, (struct wsdisplay_cmap *)data));
    680 
    681 	case WSDISPLAYIO_PUTCMAP:
    682 		return (igsfb_set_cmap(dc, (struct wsdisplay_cmap *)data));
    683 
    684 	case WSDISPLAYIO_GCURMAX:
    685 		((struct wsdisplay_curpos *)data)->x = IGS_CURSOR_MAX_SIZE;
    686 		((struct wsdisplay_curpos *)data)->y = IGS_CURSOR_MAX_SIZE;
    687 		return (0);
    688 
    689 	case WSDISPLAYIO_GCURPOS:
    690 		if (cursor_busy)
    691 			return (EBUSY);
    692 		*(struct wsdisplay_curpos *)data = dc->dc_cursor.cc_pos;
    693 		return (0);
    694 
    695 	case WSDISPLAYIO_SCURPOS:
    696 		if (cursor_busy)
    697 			return (EBUSY);
    698 		igsfb_set_curpos(dc, (struct wsdisplay_curpos *)data);
    699 		return (0);
    700 
    701 	case WSDISPLAYIO_GCURSOR:
    702 		if (cursor_busy)
    703 			return (EBUSY);
    704 		return (igsfb_get_cursor(dc, (struct wsdisplay_cursor *)data));
    705 
    706 	case WSDISPLAYIO_SCURSOR:
    707 		if (cursor_busy)
    708 			return (EBUSY);
    709 		return (igsfb_set_cursor(dc, (struct wsdisplay_cursor *)data));
    710 	}
    711 
    712 	return (EPASSTHROUGH);
    713 }
    714 
    715 
    716 /*
    717  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SVIDEO)
    718  */
    719 static void
    720 igsfb_blank_screen(dc, blank)
    721 	struct igsfb_devconfig *dc;
    722 	int blank;
    723 {
    724 
    725 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    726 		      IGS_EXT_SYNC_CTL,
    727 		      blank ? IGS_EXT_SYNC_H0 | IGS_EXT_SYNC_V0
    728 			    : 0);
    729 }
    730 
    731 
    732 /*
    733  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GETCMAP)
    734  *   Served from the software cmap copy.
    735  */
    736 static int
    737 igsfb_get_cmap(dc, p)
    738 	struct igsfb_devconfig *dc;
    739 	struct wsdisplay_cmap *p;
    740 {
    741 	u_int index, count;
    742 	int err;
    743 
    744 	index = p->index;
    745 	count = p->count;
    746 
    747 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    748 		return (EINVAL);
    749 
    750 	err = copyout(&dc->dc_cmap.r[index], p->red, count);
    751 	if (err)
    752 		return (err);
    753 	err = copyout(&dc->dc_cmap.g[index], p->green, count);
    754 	if (err)
    755 		return (err);
    756 	err = copyout(&dc->dc_cmap.b[index], p->blue, count);
    757 	if (err)
    758 		return (err);
    759 
    760 	return (0);
    761 }
    762 
    763 
    764 /*
    765  * wsdisplay_accessops: ioctl(WSDISPLAYIO_PUTCMAP)
    766  *   Set the software cmap copy and propagate changed range to the device.
    767  */
    768 static int
    769 igsfb_set_cmap(dc, p)
    770 	struct igsfb_devconfig *dc;
    771 	const struct wsdisplay_cmap *p;
    772 {
    773 	u_int index, count;
    774 	uint8_t r[IGS_CMAP_SIZE];
    775 	uint8_t g[IGS_CMAP_SIZE];
    776 	uint8_t b[IGS_CMAP_SIZE];
    777 	int error;
    778 
    779 	index = p->index;
    780 	count = p->count;
    781 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    782 		return (EINVAL);
    783 	error = copyin(p->red, &r[index], count);
    784 	if (error)
    785 		return error;
    786 	error = copyin(p->green, &g[index], count);
    787 	if (error)
    788 		return error;
    789 	error = copyin(p->blue, &b[index], count);
    790 	if (error)
    791 		return error;
    792 
    793 	memcpy(&dc->dc_cmap.r[index], &r[index], count);
    794 	memcpy(&dc->dc_cmap.g[index], &g[index], count);
    795 	memcpy(&dc->dc_cmap.b[index], &b[index], count);
    796 
    797 	/* propagate changes to the device */
    798 	igsfb_update_cmap(dc, index, count);
    799 	return (0);
    800 }
    801 
    802 
    803 /*
    804  * Propagate specified part of the software cmap copy to the device.
    805  */
    806 static void
    807 igsfb_update_cmap(dc, index, count)
    808 	struct igsfb_devconfig *dc;
    809 	u_int index, count;
    810 {
    811 	bus_space_tag_t t;
    812 	bus_space_handle_t h;
    813 	u_int last, i;
    814 
    815 	if (index >= IGS_CMAP_SIZE)
    816 		return;
    817 
    818 	last = index + count;
    819 	if (last > IGS_CMAP_SIZE)
    820 		last = IGS_CMAP_SIZE;
    821 
    822 	t = dc->dc_iot;
    823 	h = dc->dc_ioh;
    824 
    825 	/* start palette writing, index is autoincremented by hardware */
    826 	bus_space_write_1(t, h, IGS_DAC_PEL_WRITE_IDX, index);
    827 
    828 	for (i = index; i < last; ++i) {
    829 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.r[i]);
    830 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.g[i]);
    831 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.b[i]);
    832 	}
    833 }
    834 
    835 
    836 /*
    837  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURPOS)
    838  */
    839 static void
    840 igsfb_set_curpos(dc, curpos)
    841 	struct igsfb_devconfig *dc;
    842 	const struct wsdisplay_curpos *curpos;
    843 {
    844 	struct rasops_info *ri = &dc->dc_vd.active->scr_ri;
    845 	u_int x = curpos->x, y = curpos->y;
    846 
    847 	if (x >= ri->ri_width)
    848 		x = ri->ri_width - 1;
    849 	if (y >= ri->ri_height)
    850 		y = ri->ri_height - 1;
    851 
    852 	dc->dc_cursor.cc_pos.x = x;
    853 	dc->dc_cursor.cc_pos.y = y;
    854 
    855 	/* propagate changes to the device */
    856 	igsfb_update_curpos(dc);
    857 }
    858 
    859 
    860 static void
    861 igsfb_update_curpos(dc)
    862 	struct igsfb_devconfig *dc;
    863 {
    864 	bus_space_tag_t t;
    865 	bus_space_handle_t h;
    866 	int x, xoff, y, yoff;
    867 
    868 	xoff = 0;
    869 	x = dc->dc_cursor.cc_pos.x - dc->dc_cursor.cc_hot.x;
    870 	if (x < 0) {
    871 		xoff = -x;
    872 		x = 0;
    873 	}
    874 
    875 	yoff = 0;
    876 	y = dc->dc_cursor.cc_pos.y - dc->dc_cursor.cc_hot.y;
    877 	if (y < 0) {
    878 		yoff = -y;
    879 		y = 0;
    880 	}
    881 
    882 	t = dc->dc_iot;
    883 	h = dc->dc_ioh;
    884 
    885 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_LO, x & 0xff);
    886 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_HI, (x >> 8) & 0x07);
    887 	igs_ext_write(t, h, IGS_EXT_SPRITE_HPRESET, xoff & 0x3f);
    888 
    889 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_LO, y & 0xff);
    890 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_HI, (y >> 8) & 0x07);
    891 	igs_ext_write(t, h, IGS_EXT_SPRITE_VPRESET, yoff & 0x3f);
    892 }
    893 
    894 
    895 /*
    896  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GCURSOR)
    897  */
    898 static int
    899 igsfb_get_cursor(dc, p)
    900 	struct igsfb_devconfig *dc;
    901 	struct wsdisplay_cursor *p;
    902 {
    903 
    904 	/* XXX: TODO */
    905 	return (0);
    906 }
    907 
    908 
    909 /*
    910  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURSOR)
    911  */
    912 static int
    913 igsfb_set_cursor(dc, p)
    914 	struct igsfb_devconfig *dc;
    915 	const struct wsdisplay_cursor *p;
    916 {
    917 	struct igs_hwcursor *cc;
    918 	struct wsdisplay_curpos pos, hot;
    919 	u_int v, index, count, icount, iwidth;
    920 	uint8_t r[2], g[2], b[2], image[512], mask[512];
    921 	int error;
    922 
    923 	cc = &dc->dc_cursor;
    924 	v = p->which;
    925 	index = count = icount = iwidth = 0;	/* XXX: gcc */
    926 
    927 	/* copy in the new cursor colormap */
    928 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    929 		index = p->cmap.index;
    930 		count = p->cmap.count;
    931 		if (index >= 2 || (index + count) > 2)
    932 			return (EINVAL);
    933 		error = copyin(p->cmap.red, &r[index], count);
    934 		if (error)
    935 			return error;
    936 		error = copyin(p->cmap.green, &g[index], count);
    937 		if (error)
    938 			return error;
    939 		error = copyin(p->cmap.blue, &b[index], count);
    940 		if (error)
    941 			return error;
    942 	}
    943 
    944 	/* verify that the new cursor data are valid */
    945 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    946 		if (p->size.x > IGS_CURSOR_MAX_SIZE
    947 		    || p->size.y > IGS_CURSOR_MAX_SIZE)
    948 			return (EINVAL);
    949 
    950 		iwidth = (p->size.x + 7) >> 3; /* bytes per scan line */
    951 		icount = iwidth * p->size.y;
    952 		error = copyin(p->image, image, icount);
    953 		if (error)
    954 			return error;
    955 		error = copyin(p->mask, mask, icount);
    956 		if (error)
    957 			return error;
    958 	}
    959 
    960 	/* enforce that the position is within screen bounds */
    961 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    962 		struct rasops_info *ri = &dc->dc_vd.active->scr_ri;
    963 
    964 		pos = p->pos;	/* local copy we can write to */
    965 		if (pos.x >= ri->ri_width)
    966 			pos.x = ri->ri_width - 1;
    967 		if (pos.y >= ri->ri_height)
    968 			pos.y = ri->ri_height - 1;
    969 	}
    970 
    971 	/* enforce that the hot spot is within sprite bounds */
    972 	if (v & WSDISPLAY_CURSOR_DOHOT)
    973 		hot = p->hot;	/* local copy we can write to */
    974 
    975 	if (v & (WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOSHAPE)) {
    976 		const struct wsdisplay_curpos *nsize;
    977 		struct wsdisplay_curpos *nhot;
    978 
    979 		nsize = (v & WSDISPLAY_CURSOR_DOSHAPE) ?
    980 			    &p->size : &cc->cc_size;
    981 		nhot = (v & WSDISPLAY_CURSOR_DOHOT) ? &hot : &cc->cc_hot;
    982 
    983 		if (nhot->x >= nsize->x)
    984 			nhot->x = nsize->x - 1;
    985 		if (nhot->y >= nsize->y)
    986 			nhot->y = nsize->y - 1;
    987 	}
    988 
    989 	/* copy data to the driver's cursor info */
    990 	if (v & WSDISPLAY_CURSOR_DOCUR)
    991 		dc->dc_curenb = p->enable;
    992 	if (v & WSDISPLAY_CURSOR_DOPOS)
    993 		cc->cc_pos = pos; /* local copy, possibly corrected */
    994 	if (v & WSDISPLAY_CURSOR_DOHOT)
    995 		cc->cc_hot = hot; /* local copy, possibly corrected */
    996 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    997 		memcpy(&cc->cc_color[index], &r[index], count);
    998 		memcpy(&cc->cc_color[index + 2], &g[index], count);
    999 		memcpy(&cc->cc_color[index + 4], &b[index], count);
   1000 	}
   1001 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
   1002 		u_int trailing_bits;
   1003 
   1004 		memcpy(cc->cc_image, image, icount);
   1005 		memcpy(cc->cc_mask, mask, icount);
   1006 		cc->cc_size = p->size;
   1007 
   1008 		/* clear trailing bits in the "partial" mask bytes */
   1009 		trailing_bits = p->size.x & 0x07;
   1010 		if (trailing_bits != 0) {
   1011 			const u_int cutmask = ~((~0) << trailing_bits);
   1012 			u_char *mp;
   1013 			u_int i;
   1014 
   1015 			mp = cc->cc_mask + iwidth - 1;
   1016 			for (i = 0; i < p->size.y; ++i) {
   1017 				*mp &= cutmask;
   1018 				mp += iwidth;
   1019 			}
   1020 		}
   1021 		igsfb_convert_cursor_data(dc, iwidth, p->size.y);
   1022 	}
   1023 
   1024 	/* propagate changes to the device */
   1025 	igsfb_update_cursor(dc, v);
   1026 
   1027 	return (0);
   1028 }
   1029 
   1030 
   1031 /*
   1032  * Convert incoming 1bpp cursor image/mask into native 2bpp format.
   1033  */
   1034 static void
   1035 igsfb_convert_cursor_data(dc, width, height)
   1036 	struct igsfb_devconfig *dc;
   1037 	u_int width, height;
   1038 {
   1039 	struct igs_hwcursor *cc = &dc->dc_cursor;
   1040 	uint16_t *expand = dc->dc_bexpand;
   1041 	uint8_t *ip, *mp;
   1042 	uint16_t is, ms, *dp;
   1043 	u_int line, i;
   1044 
   1045 	/* init sprite to be all transparent */
   1046 	memset(cc->cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
   1047 
   1048 	/* first scanline */
   1049 	ip = cc->cc_image;
   1050 	mp = cc->cc_mask;
   1051 	dp = cc->cc_sprite;
   1052 
   1053 	for (line = 0; line < height; ++line) {
   1054 		for (i = 0; i < width; ++i) {
   1055 			is = expand[ip[i]]; /* image: 0 -> 00, 1 -> 01 */
   1056 			ms = expand[mp[i]]; /*  mask: 0 -> 00, 1 -> 11 */
   1057 			ms |= (ms << 1);
   1058 			dp[i] = (0xaaaa & ~ms) | (is & ms);
   1059 		}
   1060 
   1061 		/* next scanline */
   1062 		ip += width;
   1063 		mp += width;
   1064 		dp += 8; /* 64 pixels, 2bpp, 8 pixels per short = 8 shorts */
   1065 	}
   1066 }
   1067 
   1068 
   1069 /*
   1070  * Propagate cursor changes to the device.
   1071  * "which" is composed of WSDISPLAY_CURSOR_DO* bits.
   1072  */
   1073 static void
   1074 igsfb_update_cursor(dc, which)
   1075 	struct igsfb_devconfig *dc;
   1076 	u_int which;
   1077 {
   1078 	bus_space_tag_t iot = dc->dc_iot;
   1079 	bus_space_handle_t ioh = dc->dc_ioh;
   1080 	uint8_t curctl;
   1081 
   1082 	curctl = 0;		/* XXX: gcc */
   1083 
   1084 	/*
   1085 	 * We will need to tweak sprite control register for cursor
   1086 	 * visibility and cursor color map manipulation.
   1087 	 */
   1088 	if (which & (WSDISPLAY_CURSOR_DOCUR | WSDISPLAY_CURSOR_DOCMAP)) {
   1089 		curctl = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
   1090 	}
   1091 
   1092 	if (which & WSDISPLAY_CURSOR_DOSHAPE) {
   1093 		uint8_t *dst = bus_space_vaddr(dc->dc_memt, dc->dc_crh);
   1094 
   1095 		/*
   1096 		 * memcpy between spaces of different endianness would
   1097 		 * be ... special.  We cannot be sure if memcpy gonna
   1098 		 * push data in 4-byte chunks, we can't pre-bswap it,
   1099 		 * so do it byte-by-byte to preserve byte ordering.
   1100 		 */
   1101 		if (IGSFB_HW_SOFT_BSWAP(dc)) {
   1102 			uint8_t *src = (uint8_t *)dc->dc_cursor.cc_sprite;
   1103 			int i;
   1104 
   1105 			for (i = 0; i < IGS_CURSOR_DATA_SIZE; ++i)
   1106 				*dst++ = *src++;
   1107 		} else {
   1108 			memcpy(dst, dc->dc_cursor.cc_sprite,
   1109 			       IGS_CURSOR_DATA_SIZE);
   1110 		}
   1111 	}
   1112 
   1113 	if (which & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
   1114 		/* code shared with WSDISPLAYIO_SCURPOS */
   1115 		igsfb_update_curpos(dc);
   1116 	}
   1117 
   1118 	if (which & WSDISPLAY_CURSOR_DOCMAP) {
   1119 		uint8_t *p;
   1120 
   1121 		/* tell DAC we want access to the cursor palette */
   1122 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1123 			      curctl | IGS_EXT_SPRITE_DAC_PEL);
   1124 
   1125 		p = dc->dc_cursor.cc_color;
   1126 
   1127 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 0);
   1128 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[0]);
   1129 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[2]);
   1130 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[4]);
   1131 
   1132 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 1);
   1133 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[1]);
   1134 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[3]);
   1135 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[5]);
   1136 
   1137 		/* restore access to the normal palette */
   1138 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, curctl);
   1139 	}
   1140 
   1141 	if (which & WSDISPLAY_CURSOR_DOCUR) {
   1142 		if ((curctl & IGS_EXT_SPRITE_VISIBLE) == 0
   1143 		    && dc->dc_curenb)
   1144 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1145 				      curctl | IGS_EXT_SPRITE_VISIBLE);
   1146 		else if ((curctl & IGS_EXT_SPRITE_VISIBLE) != 0
   1147 			 && !dc->dc_curenb)
   1148 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1149 				      curctl & ~IGS_EXT_SPRITE_VISIBLE);
   1150 	}
   1151 }
   1152 
   1153 
   1154 /*
   1155  * Accelerated text mode cursor that uses hardware sprite.
   1156  */
   1157 static void
   1158 igsfb_accel_cursor(cookie, on, row, col)
   1159 	void *cookie;
   1160 	int on, row, col;
   1161 {
   1162 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1163 	struct vcons_screen *scr = ri->ri_hw;
   1164 	struct igsfb_devconfig *dc = scr->scr_cookie;
   1165 	struct igs_hwcursor *cc = &dc->dc_cursor;
   1166 	u_int which;
   1167 
   1168 	ri->ri_crow = row;
   1169 	ri->ri_ccol = col;
   1170 
   1171 	which = 0;
   1172 	if (on) {
   1173 		ri->ri_flg |= RI_CURSOR;
   1174 
   1175 		/* only bother to move the cursor if it's visible */
   1176 		cc->cc_pos.x = ri->ri_xorigin
   1177 			+ ri->ri_ccol * ri->ri_font->fontwidth;
   1178 		cc->cc_pos.y = ri->ri_yorigin
   1179 			+ ri->ri_crow * ri->ri_font->fontheight;
   1180 		which |= WSDISPLAY_CURSOR_DOPOS;
   1181 	} else
   1182 		ri->ri_flg &= ~RI_CURSOR;
   1183 
   1184 	if (dc->dc_curenb != on) {
   1185 		dc->dc_curenb = on;
   1186 		which |= WSDISPLAY_CURSOR_DOCUR;
   1187 	}
   1188 
   1189 	/* propagate changes to the device */
   1190 	igsfb_update_cursor(dc, which);
   1191 }
   1192 
   1193 
   1194 
   1195 /*
   1196  * Accelerated raster ops that use graphic coprocessor.
   1197  */
   1198 
   1199 static int
   1200 igsfb_accel_wait(dc)
   1201 	struct igsfb_devconfig *dc;
   1202 {
   1203 	bus_space_tag_t t = dc->dc_iot;
   1204 	bus_space_handle_t h = dc->dc_coph;
   1205 	int timo = 100000;
   1206 	uint8_t reg;
   1207 
   1208 	while (timo--) {
   1209 		reg = bus_space_read_1(t, h, IGS_COP_CTL_REG);
   1210 		if ((reg & IGS_COP_CTL_BUSY) == 0)
   1211 			return (0);
   1212 	}
   1213 
   1214 	return (1);
   1215 }
   1216 
   1217 
   1218 static void
   1219 igsfb_accel_copy(dc, src, dst, width, height)
   1220 	struct igsfb_devconfig *dc;
   1221 	uint32_t src, dst;
   1222 	uint16_t width, height;
   1223 {
   1224 	bus_space_tag_t t = dc->dc_iot;
   1225 	bus_space_handle_t h = dc->dc_coph;
   1226 	uint32_t toend;
   1227 	uint8_t drawcmd;
   1228 
   1229 	drawcmd = IGS_COP_DRAW_ALL;
   1230 	if (dst > src) {
   1231 		toend = dc->dc_vd.active->scr_ri.ri_width * (height - 1) + (width - 1);
   1232 		src += toend;
   1233 		dst += toend;
   1234 		drawcmd |= IGS_COP_OCTANT_X_NEG | IGS_COP_OCTANT_Y_NEG;
   1235 	}
   1236 
   1237 	igsfb_accel_wait(dc);
   1238 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
   1239 
   1240 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
   1241 
   1242 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
   1243 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
   1244 
   1245 	bus_space_write_4(t, h, IGS_COP_SRC_START_REG, src);
   1246 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
   1247 
   1248 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, drawcmd);
   1249 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
   1250 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
   1251 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG,
   1252 			  IGS_COP_OP_PXBLT | IGS_COP_OP_FG_FROM_SRC);
   1253 }
   1254 
   1255 static void
   1256 igsfb_accel_fill(dc, color, dst, width, height)
   1257 	struct igsfb_devconfig *dc;
   1258 	uint32_t color;
   1259 	uint32_t dst;
   1260 	uint16_t width, height;
   1261 {
   1262 	bus_space_tag_t t = dc->dc_iot;
   1263 	bus_space_handle_t h = dc->dc_coph;
   1264 
   1265 	igsfb_accel_wait(dc);
   1266 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
   1267 
   1268 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
   1269 
   1270 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
   1271 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
   1272 
   1273 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
   1274 	bus_space_write_4(t, h, IGS_COP_FG_REG, color);
   1275 
   1276 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, IGS_COP_DRAW_ALL);
   1277 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
   1278 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
   1279 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG, IGS_COP_OP_PXBLT);
   1280 }
   1281 
   1282 
   1283 static void
   1284 igsfb_accel_copyrows(cookie, src, dst, num)
   1285 	void *cookie;
   1286 	int src, dst, num;
   1287 {
   1288 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1289 	struct vcons_screen *scr = ri->ri_hw;
   1290 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1291 	uint32_t srp, dsp;
   1292 	uint16_t width, height;
   1293 
   1294 	width = ri->ri_emuwidth;
   1295 	height = num * ri->ri_font->fontheight;
   1296 
   1297 	srp = ri->ri_xorigin
   1298 		+ ri->ri_width * (ri->ri_yorigin
   1299 				  + src * ri->ri_font->fontheight);
   1300 	dsp = ri->ri_xorigin
   1301 		+ ri->ri_width * (ri->ri_yorigin
   1302 				  + dst * ri->ri_font->fontheight);
   1303 
   1304 	igsfb_accel_copy(dc, srp, dsp, width, height);
   1305 }
   1306 
   1307 
   1308 void
   1309 igsfb_accel_copycols(cookie, row, src, dst, num)
   1310 	void *cookie;
   1311 	int row, src, dst, num;
   1312 {
   1313 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1314 	struct vcons_screen *scr = ri->ri_hw;
   1315 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1316 	uint32_t rowp, srp, dsp;
   1317 	uint16_t width, height;
   1318 
   1319 	width = num * ri->ri_font->fontwidth;
   1320 	height = ri->ri_font->fontheight;
   1321 
   1322 	rowp = ri->ri_xorigin
   1323 		+ ri->ri_width * (ri->ri_yorigin
   1324 				  + row * ri->ri_font->fontheight);
   1325 
   1326 	srp = rowp + src * ri->ri_font->fontwidth;
   1327 	dsp = rowp + dst * ri->ri_font->fontwidth;
   1328 
   1329 	igsfb_accel_copy(dc, srp, dsp, width, height);
   1330 }
   1331 
   1332 
   1333 static void
   1334 igsfb_accel_eraserows(cookie, row, num, attr)
   1335 	void *cookie;
   1336 	int row, num;
   1337 	long attr;
   1338 {
   1339 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1340 	struct vcons_screen *scr = ri->ri_hw;
   1341 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1342 	uint32_t color;
   1343 	uint32_t dsp;
   1344 	uint16_t width, height;
   1345 
   1346 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
   1347 		width = ri->ri_width;
   1348 		height = ri->ri_height;
   1349 		dsp = 0;
   1350 	} else {
   1351 		width = ri->ri_emuwidth;
   1352 		height = num * ri->ri_font->fontheight;
   1353 
   1354 		dsp = ri->ri_xorigin
   1355 			+ ri->ri_width * (ri->ri_yorigin
   1356 					  + row * ri->ri_font->fontheight);
   1357 	}
   1358 
   1359 	/* XXX: we "know" the encoding that rasops' allocattr uses */
   1360 	color = (attr >> 16) & 0xff;
   1361 
   1362 	igsfb_accel_fill(dc, color, dsp, width, height);
   1363 }
   1364 
   1365 
   1366 void
   1367 igsfb_accel_erasecols(cookie, row, col, num, attr)
   1368 	void *cookie;
   1369 	int row, col, num;
   1370 	long attr;
   1371 {
   1372 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1373 	struct vcons_screen *scr = ri->ri_hw;
   1374 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1375 	uint32_t color;
   1376 	uint32_t rowp, dsp;
   1377 	uint16_t width, height;
   1378 
   1379 	width = num * ri->ri_font->fontwidth;
   1380 	height = ri->ri_font->fontheight;
   1381 
   1382 	rowp = ri->ri_xorigin
   1383 		+ ri->ri_width * (ri->ri_yorigin
   1384 				  + row * ri->ri_font->fontheight);
   1385 
   1386 	dsp = rowp + col * ri->ri_font->fontwidth;
   1387 
   1388 	/* XXX: we "know" the encoding that rasops' allocattr uses */
   1389 	color = (attr >> 16) & 0xff;
   1390 
   1391 	igsfb_accel_fill(dc, color, dsp, width, height);
   1392 }
   1393 
   1394 
   1395 /*
   1396  * Not really implemented here, but we need to hook into the one
   1397  * supplied by rasops so that we can synchronize with the COP.
   1398  */
   1399 static void
   1400 igsfb_accel_putchar(cookie, row, col, uc, attr)
   1401 	void *cookie;
   1402 	int row, col;
   1403 	u_int uc;
   1404 	long attr;
   1405 {
   1406 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1407 	struct vcons_screen *scr = ri->ri_hw;
   1408 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
   1409 
   1410 	igsfb_accel_wait(dc);
   1411 	(*dc->dc_ri_putchar)(cookie, row, col, uc, attr);
   1412 }
   1413