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