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