Home | History | Annotate | Line # | Download | only in ic
igsfb.c revision 1.24
      1 /*	$NetBSD: igsfb.c,v 1.24 2005/09/30 23:16:26 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.24 2005/09/30 23:16:26 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/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 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    461 	    ri->ri_width / ri->ri_font->fontwidth);
    462 
    463 
    464 	/* use the sprite for the text mode cursor */
    465 	igsfb_make_text_cursor(dc);
    466 
    467 	/* the cursor is "busy" while we are in the text mode */
    468 	dc->dc_hwflags |= IGSFB_HW_TEXT_CURSOR;
    469 
    470 	/* propagate sprite data to the device */
    471 	igsfb_update_cursor(dc, WSDISPLAY_CURSOR_DOSHAPE);
    472 
    473 	/* accelerated text cursor */
    474 	ri->ri_ops.cursor = igsfb_accel_cursor;
    475 
    476 	if (dc->dc_id >= 0x2000) { /* XXX */
    477 	    /* accelerated erase/copy */
    478 	    ri->ri_ops.copycols = igsfb_accel_copycols;
    479 	    ri->ri_ops.erasecols = igsfb_accel_erasecols;
    480 	    ri->ri_ops.copyrows = igsfb_accel_copyrows;
    481 	    ri->ri_ops.eraserows = igsfb_accel_eraserows;
    482 
    483 	    /* putchar hook to sync with the cop */
    484 	    dc->dc_ri_putchar = ri->ri_ops.putchar;
    485 	    ri->ri_ops.putchar = igsfb_accel_putchar;
    486 	}
    487 
    488 	igsfb_stdscreen.nrows = ri->ri_rows;
    489 	igsfb_stdscreen.ncols = ri->ri_cols;
    490 	igsfb_stdscreen.textops = &ri->ri_ops;
    491 	igsfb_stdscreen.capabilities = ri->ri_caps;
    492 }
    493 
    494 
    495 /*
    496  * Init cursor data in dc_cursor for the accelerated text cursor.
    497  */
    498 static int
    499 igsfb_make_text_cursor(dc)
    500 	struct igsfb_devconfig *dc;
    501 {
    502 	struct wsdisplay_font *f = dc->dc_ri.ri_font;
    503 	uint16_t cc_scan[8];	/* one sprite scanline */
    504 	uint16_t s;
    505 	int w, i;
    506 
    507 	KASSERT(f->fontwidth <= IGS_CURSOR_MAX_SIZE);
    508 	KASSERT(f->fontheight <= IGS_CURSOR_MAX_SIZE);
    509 
    510 	w = f->fontwidth;
    511 	for (i = 0; i < f->stride; ++i) {
    512 		if (w >= 8) {
    513 			s = 0xffff; /* all inverted */
    514 			w -= 8;
    515 		} else {
    516 			/* first w pixels inverted, the rest is transparent */
    517 			s = ~(0x5555 << (w * 2));
    518 			if (IGSFB_HW_SOFT_BSWAP(dc))
    519 				s = bswap16(s);
    520 			w = 0;
    521 		}
    522 		cc_scan[i] = s;
    523 	}
    524 
    525 	dc->dc_cursor.cc_size.x = f->fontwidth;
    526 	dc->dc_cursor.cc_size.y = f->fontheight;
    527 
    528 	dc->dc_cursor.cc_hot.x = 0;
    529 	dc->dc_cursor.cc_hot.y = 0;
    530 
    531 	/* init sprite array to be all transparent */
    532 	memset(dc->dc_cursor.cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
    533 
    534 	for (i = 0; i < f->fontheight; ++i)
    535 		memcpy(&dc->dc_cursor.cc_sprite[i * 8],
    536 		       cc_scan, f->stride * sizeof(uint16_t));
    537 
    538 	return (0);
    539 }
    540 
    541 
    542 /*
    543  * Spread a byte (abcd.efgh) into two (0a0b.0c0d 0e0f.0g0h).
    544  * Helper function for igsfb_init_bit_table().
    545  */
    546 static uint16_t
    547 igsfb_spread_bits_8(b)
    548 	uint8_t b;
    549 {
    550 	uint16_t s = b;
    551 
    552 	s = ((s & 0x00f0) << 4) | (s & 0x000f);
    553 	s = ((s & 0x0c0c) << 2) | (s & 0x0303);
    554 	s = ((s & 0x2222) << 1) | (s & 0x1111);
    555 	return (s);
    556 }
    557 
    558 
    559 /*
    560  * Cursor sprite data are in 2bpp.  Incoming image/mask are in 1bpp.
    561  * Prebuild the table to expand 1bpp->2bpp, with bswapping if necessary.
    562  */
    563 static void
    564 igsfb_init_bit_table(dc)
    565 	struct igsfb_devconfig *dc;
    566 {
    567 	uint16_t *expand = dc->dc_bexpand;
    568 	int need_bswap = IGSFB_HW_SOFT_BSWAP(dc);
    569 	uint16_t s;
    570 	u_int i;
    571 
    572 	for (i = 0; i < 256; ++i) {
    573 		s = igsfb_spread_bits_8(i);
    574 		expand[i] = need_bswap ? bswap16(s) : s;
    575 	}
    576 }
    577 
    578 
    579 /*
    580  * wsdisplay_accessops: mmap()
    581  *   XXX: allow mmapping i/o mapped i/o regs if INSECURE???
    582  */
    583 static paddr_t
    584 igsfb_mmap(v, offset, prot)
    585 	void *v;
    586 	off_t offset;
    587 	int prot;
    588 {
    589 	struct igsfb_devconfig *dc = v;
    590 
    591 	if (offset >= dc->dc_memsz || offset < 0)
    592 		return (-1);
    593 
    594 	return (bus_space_mmap(dc->dc_memt, dc->dc_memaddr, offset, prot,
    595 			       dc->dc_memflags | BUS_SPACE_MAP_LINEAR));
    596 }
    597 
    598 
    599 /*
    600  * wsdisplay_accessops: ioctl()
    601  */
    602 static int
    603 igsfb_ioctl(v, cmd, data, flag, p)
    604 	void *v;
    605 	u_long cmd;
    606 	caddr_t data;
    607 	int flag;
    608 	struct proc *p;
    609 {
    610 	struct igsfb_devconfig *dc = v;
    611 	struct rasops_info *ri;
    612 	int cursor_busy;
    613 	int turnoff;
    614 
    615 	/* don't permit cursor ioctls if we use sprite for text cursor */
    616 	cursor_busy = !dc->dc_mapped
    617 		&& (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR);
    618 
    619 	switch (cmd) {
    620 
    621 	case WSDISPLAYIO_GTYPE:
    622 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    623 		return (0);
    624 
    625 	case WSDISPLAYIO_GINFO:
    626 		ri = &dc->dc_ri;
    627 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    628 		wsd_fbip->height = ri->ri_height;
    629 		wsd_fbip->width = ri->ri_width;
    630 		wsd_fbip->depth = ri->ri_depth;
    631 		wsd_fbip->cmsize = IGS_CMAP_SIZE;
    632 #undef wsd_fbip
    633 		return (0);
    634 
    635 	case WSDISPLAYIO_LINEBYTES:
    636 		ri = &dc->dc_ri;
    637 		*(int *)data = ri->ri_stride;
    638 		return (0);
    639 
    640 	case WSDISPLAYIO_SMODE:
    641 #define d (*(int *)data)
    642 		if (d != WSDISPLAYIO_MODE_EMUL)
    643 			dc->dc_mapped = 1;
    644 			/* turn off hardware cursor */
    645 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
    646 				dc->dc_curenb = 0;
    647 				igsfb_update_cursor(dc,
    648 					WSDISPLAY_CURSOR_DOCUR);
    649 			}
    650 
    651 		else {
    652 			dc->dc_mapped = 0;
    653 			/* reinit sprite for text cursor */
    654 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
    655 				igsfb_make_text_cursor(dc);
    656 				dc->dc_curenb = 0;
    657 				igsfb_update_cursor(dc,
    658 					  WSDISPLAY_CURSOR_DOSHAPE
    659 					| WSDISPLAY_CURSOR_DOCUR);
    660 			}
    661 		}
    662 		return (0);
    663 
    664 	case WSDISPLAYIO_GVIDEO:
    665 		*(u_int *)data = dc->dc_blanked ?
    666 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    667 		return (0);
    668 
    669 	case WSDISPLAYIO_SVIDEO:
    670 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
    671 		if (dc->dc_blanked != turnoff) {
    672 			dc->dc_blanked = turnoff;
    673 			igsfb_blank_screen(dc, dc->dc_blanked);
    674 		}
    675 		return (0);
    676 
    677 	case WSDISPLAYIO_GETCMAP:
    678 		return (igsfb_get_cmap(dc, (struct wsdisplay_cmap *)data));
    679 
    680 	case WSDISPLAYIO_PUTCMAP:
    681 		return (igsfb_set_cmap(dc, (struct wsdisplay_cmap *)data));
    682 
    683 	case WSDISPLAYIO_GCURMAX:
    684 		((struct wsdisplay_curpos *)data)->x = IGS_CURSOR_MAX_SIZE;
    685 		((struct wsdisplay_curpos *)data)->y = IGS_CURSOR_MAX_SIZE;
    686 		return (0);
    687 
    688 	case WSDISPLAYIO_GCURPOS:
    689 		if (cursor_busy)
    690 			return (EBUSY);
    691 		*(struct wsdisplay_curpos *)data = dc->dc_cursor.cc_pos;
    692 		return (0);
    693 
    694 	case WSDISPLAYIO_SCURPOS:
    695 		if (cursor_busy)
    696 			return (EBUSY);
    697 		igsfb_set_curpos(dc, (struct wsdisplay_curpos *)data);
    698 		return (0);
    699 
    700 	case WSDISPLAYIO_GCURSOR:
    701 		if (cursor_busy)
    702 			return (EBUSY);
    703 		return (igsfb_get_cursor(dc, (struct wsdisplay_cursor *)data));
    704 
    705 	case WSDISPLAYIO_SCURSOR:
    706 		if (cursor_busy)
    707 			return (EBUSY);
    708 		return (igsfb_set_cursor(dc, (struct wsdisplay_cursor *)data));
    709 	}
    710 
    711 	return (EPASSTHROUGH);
    712 }
    713 
    714 
    715 /*
    716  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SVIDEO)
    717  */
    718 static void
    719 igsfb_blank_screen(dc, blank)
    720 	struct igsfb_devconfig *dc;
    721 	int blank;
    722 {
    723 
    724 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    725 		      IGS_EXT_SYNC_CTL,
    726 		      blank ? IGS_EXT_SYNC_H0 | IGS_EXT_SYNC_V0
    727 			    : 0);
    728 }
    729 
    730 
    731 /*
    732  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GETCMAP)
    733  *   Served from the software cmap copy.
    734  */
    735 static int
    736 igsfb_get_cmap(dc, p)
    737 	struct igsfb_devconfig *dc;
    738 	struct wsdisplay_cmap *p;
    739 {
    740 	u_int index, count;
    741 	int err;
    742 
    743 	index = p->index;
    744 	count = p->count;
    745 
    746 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    747 		return (EINVAL);
    748 
    749 	err = copyout(&dc->dc_cmap.r[index], p->red, count);
    750 	if (err)
    751 		return (err);
    752 	err = copyout(&dc->dc_cmap.g[index], p->green, count);
    753 	if (err)
    754 		return (err);
    755 	err = copyout(&dc->dc_cmap.b[index], p->blue, count);
    756 	if (err)
    757 		return (err);
    758 
    759 	return (0);
    760 }
    761 
    762 
    763 /*
    764  * wsdisplay_accessops: ioctl(WSDISPLAYIO_PUTCMAP)
    765  *   Set the software cmap copy and propagate changed range to the device.
    766  */
    767 static int
    768 igsfb_set_cmap(dc, p)
    769 	struct igsfb_devconfig *dc;
    770 	const struct wsdisplay_cmap *p;
    771 {
    772 	u_int index, count;
    773 	uint8_t r[IGS_CMAP_SIZE];
    774 	uint8_t g[IGS_CMAP_SIZE];
    775 	uint8_t b[IGS_CMAP_SIZE];
    776 	int error;
    777 
    778 	index = p->index;
    779 	count = p->count;
    780 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    781 		return (EINVAL);
    782 	error = copyin(p->red, &r[index], count);
    783 	if (error)
    784 		return error;
    785 	error = copyin(p->green, &g[index], count);
    786 	if (error)
    787 		return error;
    788 	error = copyin(p->blue, &b[index], count);
    789 	if (error)
    790 		return error;
    791 
    792 	memcpy(&dc->dc_cmap.r[index], &r[index], count);
    793 	memcpy(&dc->dc_cmap.g[index], &g[index], count);
    794 	memcpy(&dc->dc_cmap.b[index], &b[index], count);
    795 
    796 	/* propagate changes to the device */
    797 	igsfb_update_cmap(dc, index, count);
    798 	return (0);
    799 }
    800 
    801 
    802 /*
    803  * Propagate specified part of the software cmap copy to the device.
    804  */
    805 static void
    806 igsfb_update_cmap(dc, index, count)
    807 	struct igsfb_devconfig *dc;
    808 	u_int index, count;
    809 {
    810 	bus_space_tag_t t;
    811 	bus_space_handle_t h;
    812 	u_int last, i;
    813 
    814 	if (index >= IGS_CMAP_SIZE)
    815 		return;
    816 
    817 	last = index + count;
    818 	if (last > IGS_CMAP_SIZE)
    819 		last = IGS_CMAP_SIZE;
    820 
    821 	t = dc->dc_iot;
    822 	h = dc->dc_ioh;
    823 
    824 	/* start palette writing, index is autoincremented by hardware */
    825 	bus_space_write_1(t, h, IGS_DAC_PEL_WRITE_IDX, index);
    826 
    827 	for (i = index; i < last; ++i) {
    828 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.r[i]);
    829 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.g[i]);
    830 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.b[i]);
    831 	}
    832 }
    833 
    834 
    835 /*
    836  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURPOS)
    837  */
    838 static void
    839 igsfb_set_curpos(dc, curpos)
    840 	struct igsfb_devconfig *dc;
    841 	const struct wsdisplay_curpos *curpos;
    842 {
    843 	struct rasops_info *ri = &dc->dc_ri;
    844 	u_int x = curpos->x, y = curpos->y;
    845 
    846 	if (x >= ri->ri_width)
    847 		x = ri->ri_width - 1;
    848 	if (y >= ri->ri_height)
    849 		y = ri->ri_height - 1;
    850 
    851 	dc->dc_cursor.cc_pos.x = x;
    852 	dc->dc_cursor.cc_pos.y = y;
    853 
    854 	/* propagate changes to the device */
    855 	igsfb_update_curpos(dc);
    856 }
    857 
    858 
    859 static void
    860 igsfb_update_curpos(dc)
    861 	struct igsfb_devconfig *dc;
    862 {
    863 	bus_space_tag_t t;
    864 	bus_space_handle_t h;
    865 	int x, xoff, y, yoff;
    866 
    867 	xoff = 0;
    868 	x = dc->dc_cursor.cc_pos.x - dc->dc_cursor.cc_hot.x;
    869 	if (x < 0) {
    870 		xoff = -x;
    871 		x = 0;
    872 	}
    873 
    874 	yoff = 0;
    875 	y = dc->dc_cursor.cc_pos.y - dc->dc_cursor.cc_hot.y;
    876 	if (y < 0) {
    877 		yoff = -y;
    878 		y = 0;
    879 	}
    880 
    881 	t = dc->dc_iot;
    882 	h = dc->dc_ioh;
    883 
    884 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_LO, x & 0xff);
    885 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_HI, (x >> 8) & 0x07);
    886 	igs_ext_write(t, h, IGS_EXT_SPRITE_HPRESET, xoff & 0x3f);
    887 
    888 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_LO, y & 0xff);
    889 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_HI, (y >> 8) & 0x07);
    890 	igs_ext_write(t, h, IGS_EXT_SPRITE_VPRESET, yoff & 0x3f);
    891 }
    892 
    893 
    894 /*
    895  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GCURSOR)
    896  */
    897 static int
    898 igsfb_get_cursor(dc, p)
    899 	struct igsfb_devconfig *dc;
    900 	struct wsdisplay_cursor *p;
    901 {
    902 
    903 	/* XXX: TODO */
    904 	return (0);
    905 }
    906 
    907 
    908 /*
    909  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURSOR)
    910  */
    911 static int
    912 igsfb_set_cursor(dc, p)
    913 	struct igsfb_devconfig *dc;
    914 	const struct wsdisplay_cursor *p;
    915 {
    916 	struct igs_hwcursor *cc;
    917 	struct wsdisplay_curpos pos, hot;
    918 	u_int v, index, count, icount, iwidth;
    919 	uint8_t r[2], g[2], b[2], image[512], mask[512];
    920 	int error;
    921 
    922 	cc = &dc->dc_cursor;
    923 	v = p->which;
    924 	index = count = icount = iwidth = 0;	/* XXX: gcc */
    925 
    926 	/* copy in the new cursor colormap */
    927 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    928 		index = p->cmap.index;
    929 		count = p->cmap.count;
    930 		if (index >= 2 || (index + count) > 2)
    931 			return (EINVAL);
    932 		error = copyin(p->cmap.red, &r[index], count);
    933 		if (error)
    934 			return error;
    935 		error = copyin(p->cmap.green, &g[index], count);
    936 		if (error)
    937 			return error;
    938 		error = copyin(p->cmap.blue, &b[index], count);
    939 		if (error)
    940 			return error;
    941 	}
    942 
    943 	/* verify that the new cursor data are valid */
    944 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    945 		if (p->size.x > IGS_CURSOR_MAX_SIZE
    946 		    || p->size.y > IGS_CURSOR_MAX_SIZE)
    947 			return (EINVAL);
    948 
    949 		iwidth = (p->size.x + 7) >> 3; /* bytes per scan line */
    950 		icount = iwidth * p->size.y;
    951 		error = copyin(p->image, image, icount);
    952 		if (error)
    953 			return error;
    954 		error = copyin(p->mask, mask, icount);
    955 		if (error)
    956 			return error;
    957 	}
    958 
    959 	/* enforce that the position is within screen bounds */
    960 	if (v & WSDISPLAY_CURSOR_DOPOS) {
    961 		struct rasops_info *ri = &dc->dc_ri;
    962 
    963 		pos = p->pos;	/* local copy we can write to */
    964 		if (pos.x >= (ri->ri_width + 64))
    965 			pos.x = ri->ri_width +63;
    966 		if (pos.y >= (ri->ri_height + 64))
    967 			pos.y = ri->ri_height + 63;
    968 	}
    969 
    970 	/* enforce that the hot spot is within sprite bounds */
    971 	if (v & WSDISPLAY_CURSOR_DOHOT)
    972 		hot = p->hot;	/* local copy we can write to */
    973 
    974 	if (v & (WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOSHAPE)) {
    975 		const struct wsdisplay_curpos *nsize;
    976 		struct wsdisplay_curpos *nhot;
    977 
    978 		nsize = (v & WSDISPLAY_CURSOR_DOSHAPE) ?
    979 			    &p->size : &cc->cc_size;
    980 		nhot = (v & WSDISPLAY_CURSOR_DOHOT) ? &hot : &cc->cc_hot;
    981 
    982 		if (nhot->x >= nsize->x)
    983 			nhot->x = nsize->x - 1;
    984 		if (nhot->y >= nsize->y)
    985 			nhot->y = nsize->y - 1;
    986 	}
    987 
    988 	/* copy data to the driver's cursor info */
    989 	if (v & WSDISPLAY_CURSOR_DOCUR)
    990 		dc->dc_curenb = p->enable;
    991 	if (v & WSDISPLAY_CURSOR_DOPOS)
    992 		cc->cc_pos = pos; /* local copy, possibly corrected */
    993 	if (v & WSDISPLAY_CURSOR_DOHOT)
    994 		cc->cc_hot = hot; /* local copy, possibly corrected */
    995 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    996 		memcpy(&cc->cc_color[index], &r[index], count);
    997 		memcpy(&cc->cc_color[index + 2], &g[index], count);
    998 		memcpy(&cc->cc_color[index + 4], &b[index], count);
    999 	}
   1000 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
   1001 		u_int trailing_bits;
   1002 
   1003 		memcpy(cc->cc_image, image, icount);
   1004 		memcpy(cc->cc_mask, mask, icount);
   1005 		cc->cc_size = p->size;
   1006 
   1007 		/* clear trailing bits in the "partial" mask bytes */
   1008 		trailing_bits = p->size.x & 0x07;
   1009 		if (trailing_bits != 0) {
   1010 			const u_int cutmask = ~((~0) << trailing_bits);
   1011 			u_char *mp;
   1012 			u_int i;
   1013 
   1014 			mp = cc->cc_mask + iwidth - 1;
   1015 			for (i = 0; i < p->size.y; ++i) {
   1016 				*mp &= cutmask;
   1017 				mp += iwidth;
   1018 			}
   1019 		}
   1020 		igsfb_convert_cursor_data(dc, iwidth, p->size.y);
   1021 	}
   1022 
   1023 	/* propagate changes to the device */
   1024 	igsfb_update_cursor(dc, v);
   1025 
   1026 	return (0);
   1027 }
   1028 
   1029 
   1030 /*
   1031  * Convert incoming 1bpp cursor image/mask into native 2bpp format.
   1032  */
   1033 static void
   1034 igsfb_convert_cursor_data(dc, width, height)
   1035 	struct igsfb_devconfig *dc;
   1036 	u_int width, height;
   1037 {
   1038 	struct igs_hwcursor *cc = &dc->dc_cursor;
   1039 	uint16_t *expand = dc->dc_bexpand;
   1040 	uint8_t *ip, *mp;
   1041 	uint16_t is, ms, *dp;
   1042 	u_int line, i;
   1043 
   1044 	/* init sprite to be all transparent */
   1045 	memset(cc->cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
   1046 
   1047 	/* first scanline */
   1048 	ip = cc->cc_image;
   1049 	mp = cc->cc_mask;
   1050 	dp = cc->cc_sprite;
   1051 
   1052 	for (line = 0; line < height; ++line) {
   1053 		for (i = 0; i < width; ++i) {
   1054 			is = expand[ip[i]]; /* image: 0 -> 00, 1 -> 01 */
   1055 			ms = expand[mp[i]]; /*  mask: 0 -> 00, 1 -> 11 */
   1056 			ms |= (ms << 1);
   1057 			dp[i] = (0xaaaa & ~ms) | (is & ms);
   1058 		}
   1059 
   1060 		/* next scanline */
   1061 		ip += width;
   1062 		mp += width;
   1063 		dp += 8; /* 64 pixels, 2bpp, 8 pixels per short = 8 shorts */
   1064 	}
   1065 }
   1066 
   1067 
   1068 /*
   1069  * Propagate cursor changes to the device.
   1070  * "which" is composed of WSDISPLAY_CURSOR_DO* bits.
   1071  */
   1072 static void
   1073 igsfb_update_cursor(dc, which)
   1074 	struct igsfb_devconfig *dc;
   1075 	u_int which;
   1076 {
   1077 	bus_space_tag_t iot = dc->dc_iot;
   1078 	bus_space_handle_t ioh = dc->dc_ioh;
   1079 	uint8_t curctl;
   1080 
   1081 	curctl = 0;		/* XXX: gcc */
   1082 
   1083 	/*
   1084 	 * We will need to tweak sprite control register for cursor
   1085 	 * visibility and cursor color map manipulation.
   1086 	 */
   1087 	if (which & (WSDISPLAY_CURSOR_DOCUR | WSDISPLAY_CURSOR_DOCMAP)) {
   1088 		curctl = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
   1089 	}
   1090 
   1091 	if (which & WSDISPLAY_CURSOR_DOSHAPE) {
   1092 		uint8_t *dst = bus_space_vaddr(dc->dc_memt, dc->dc_crh);
   1093 
   1094 		/*
   1095 		 * memcpy between spaces of different endianness would
   1096 		 * be ... special.  We cannot be sure if memcpy gonna
   1097 		 * push data in 4-byte chunks, we can't pre-bswap it,
   1098 		 * so do it byte-by-byte to preserve byte ordering.
   1099 		 */
   1100 		if (IGSFB_HW_SOFT_BSWAP(dc)) {
   1101 			uint8_t *src = (uint8_t *)dc->dc_cursor.cc_sprite;
   1102 			int i;
   1103 
   1104 			for (i = 0; i < IGS_CURSOR_DATA_SIZE; ++i)
   1105 				*dst++ = *src++;
   1106 		} else {
   1107 			memcpy(dst, dc->dc_cursor.cc_sprite,
   1108 			       IGS_CURSOR_DATA_SIZE);
   1109 		}
   1110 	}
   1111 
   1112 	if (which & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
   1113 		/* code shared with WSDISPLAYIO_SCURPOS */
   1114 		igsfb_update_curpos(dc);
   1115 	}
   1116 
   1117 	if (which & WSDISPLAY_CURSOR_DOCMAP) {
   1118 		uint8_t *p;
   1119 
   1120 		/* tell DAC we want access to the cursor palette */
   1121 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1122 			      curctl | IGS_EXT_SPRITE_DAC_PEL);
   1123 
   1124 		p = dc->dc_cursor.cc_color;
   1125 
   1126 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 0);
   1127 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[0]);
   1128 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[2]);
   1129 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[4]);
   1130 
   1131 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 1);
   1132 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[1]);
   1133 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[3]);
   1134 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[5]);
   1135 
   1136 		/* restore access to the normal palette */
   1137 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, curctl);
   1138 	}
   1139 
   1140 	if (which & WSDISPLAY_CURSOR_DOCUR) {
   1141 		if ((curctl & IGS_EXT_SPRITE_VISIBLE) == 0
   1142 		    && dc->dc_curenb)
   1143 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1144 				      curctl | IGS_EXT_SPRITE_VISIBLE);
   1145 		else if ((curctl & IGS_EXT_SPRITE_VISIBLE) != 0
   1146 			 && !dc->dc_curenb)
   1147 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
   1148 				      curctl & ~IGS_EXT_SPRITE_VISIBLE);
   1149 	}
   1150 }
   1151 
   1152 
   1153 /*
   1154  * wsdisplay_accessops: alloc_screen()
   1155  */
   1156 static int
   1157 igsfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
   1158 	void *v;
   1159 	const struct wsscreen_descr *type;
   1160 	void **cookiep;
   1161 	int *curxp, *curyp;
   1162 	long *attrp;
   1163 {
   1164 	struct igsfb_devconfig *dc = v;
   1165 	struct rasops_info *ri = &dc->dc_ri;
   1166 
   1167 	if (dc->dc_nscreens > 0) /* only do single screen for now */
   1168 		return (ENOMEM);
   1169 
   1170 	dc->dc_nscreens = 1;
   1171 
   1172 	*cookiep = ri;		/* emulcookie for igsgfb_stdscreen.textops */
   1173 	*curxp = *curyp = 0;	/* cursor position */
   1174 	(*ri->ri_ops.allocattr)(ri,
   1175 				WSCOL_BLACK, /* fg */
   1176 				WSCOL_BLACK, /* bg */
   1177 				0,           /* wsattr */
   1178 				attrp);
   1179 	return (0);
   1180 }
   1181 
   1182 
   1183 /*
   1184  * wsdisplay_accessops: free_screen()
   1185  */
   1186 static void
   1187 igsfb_free_screen(v, cookie)
   1188 	void *v;
   1189 	void *cookie;
   1190 {
   1191 
   1192 	/* XXX */
   1193 	return;
   1194 }
   1195 
   1196 
   1197 /*
   1198  * wsdisplay_accessops: show_screen()
   1199  */
   1200 static int
   1201 igsfb_show_screen(v, cookie, waitok, cb, cbarg)
   1202 	void *v;
   1203 	void *cookie;
   1204 	int waitok;
   1205 	void (*cb)(void *, int, int);
   1206 	void *cbarg;
   1207 {
   1208 
   1209 	/* XXX */
   1210 	return (0);
   1211 }
   1212 
   1213 
   1214 
   1215 /*
   1216  * Accelerated text mode cursor that uses hardware sprite.
   1217  */
   1218 static void
   1219 igsfb_accel_cursor(cookie, on, row, col)
   1220 	void *cookie;
   1221 	int on, row, col;
   1222 {
   1223 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1224 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)ri->ri_hw;
   1225 	struct igs_hwcursor *cc = &dc->dc_cursor;
   1226 	u_int which;
   1227 
   1228 	ri->ri_crow = row;
   1229 	ri->ri_ccol = col;
   1230 
   1231 	which = 0;
   1232 	if (on) {
   1233 		ri->ri_flg |= RI_CURSOR;
   1234 
   1235 		/* only bother to move the cursor if it's visible */
   1236 		cc->cc_pos.x = ri->ri_xorigin
   1237 			+ ri->ri_ccol * ri->ri_font->fontwidth;
   1238 		cc->cc_pos.y = ri->ri_yorigin
   1239 			+ ri->ri_crow * ri->ri_font->fontheight;
   1240 		which |= WSDISPLAY_CURSOR_DOPOS;
   1241 	} else
   1242 		ri->ri_flg &= ~RI_CURSOR;
   1243 
   1244 	if (dc->dc_curenb != on) {
   1245 		dc->dc_curenb = on;
   1246 		which |= WSDISPLAY_CURSOR_DOCUR;
   1247 	}
   1248 
   1249 	/* propagate changes to the device */
   1250 	igsfb_update_cursor(dc, which);
   1251 }
   1252 
   1253 
   1254 
   1255 /*
   1256  * Accelerated raster ops that use graphic coprocessor.
   1257  */
   1258 
   1259 static int
   1260 igsfb_accel_wait(dc)
   1261 	struct igsfb_devconfig *dc;
   1262 {
   1263 	bus_space_tag_t t = dc->dc_iot;
   1264 	bus_space_handle_t h = dc->dc_coph;
   1265 	int timo = 100000;
   1266 	uint8_t reg;
   1267 
   1268 	while (timo--) {
   1269 		reg = bus_space_read_1(t, h, IGS_COP_CTL_REG);
   1270 		if ((reg & IGS_COP_CTL_BUSY) == 0)
   1271 			return (0);
   1272 	}
   1273 
   1274 	return (1);
   1275 }
   1276 
   1277 
   1278 static void
   1279 igsfb_accel_copy(dc, src, dst, width, height)
   1280 	struct igsfb_devconfig *dc;
   1281 	uint32_t src, dst;
   1282 	uint16_t width, height;
   1283 {
   1284 	bus_space_tag_t t = dc->dc_iot;
   1285 	bus_space_handle_t h = dc->dc_coph;
   1286 	uint32_t toend;
   1287 	uint8_t drawcmd;
   1288 
   1289 	drawcmd = IGS_COP_DRAW_ALL;
   1290 	if (dst > src) {
   1291 		toend = dc->dc_ri.ri_width * (height - 1) + (width - 1);
   1292 		src += toend;
   1293 		dst += toend;
   1294 		drawcmd |= IGS_COP_OCTANT_X_NEG | IGS_COP_OCTANT_Y_NEG;
   1295 	}
   1296 
   1297 	igsfb_accel_wait(dc);
   1298 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
   1299 
   1300 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
   1301 
   1302 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
   1303 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
   1304 
   1305 	bus_space_write_4(t, h, IGS_COP_SRC_START_REG, src);
   1306 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
   1307 
   1308 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, drawcmd);
   1309 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
   1310 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
   1311 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG,
   1312 			  IGS_COP_OP_PXBLT | IGS_COP_OP_FG_FROM_SRC);
   1313 }
   1314 
   1315 static void
   1316 igsfb_accel_fill(dc, color, dst, width, height)
   1317 	struct igsfb_devconfig *dc;
   1318 	uint32_t color;
   1319 	uint32_t dst;
   1320 	uint16_t width, height;
   1321 {
   1322 	bus_space_tag_t t = dc->dc_iot;
   1323 	bus_space_handle_t h = dc->dc_coph;
   1324 
   1325 	igsfb_accel_wait(dc);
   1326 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
   1327 
   1328 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
   1329 
   1330 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
   1331 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
   1332 
   1333 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
   1334 	bus_space_write_4(t, h, IGS_COP_FG_REG, color);
   1335 
   1336 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, IGS_COP_DRAW_ALL);
   1337 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
   1338 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
   1339 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG, IGS_COP_OP_PXBLT);
   1340 }
   1341 
   1342 
   1343 static void
   1344 igsfb_accel_copyrows(cookie, src, dst, num)
   1345 	void *cookie;
   1346 	int src, dst, num;
   1347 {
   1348 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1349 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)ri->ri_hw;
   1350 	uint32_t srp, dsp;
   1351 	uint16_t width, height;
   1352 
   1353 	width = ri->ri_emuwidth;
   1354 	height = num * ri->ri_font->fontheight;
   1355 
   1356 	srp = ri->ri_xorigin
   1357 		+ ri->ri_width * (ri->ri_yorigin
   1358 				  + src * ri->ri_font->fontheight);
   1359 	dsp = ri->ri_xorigin
   1360 		+ ri->ri_width * (ri->ri_yorigin
   1361 				  + dst * ri->ri_font->fontheight);
   1362 
   1363 	igsfb_accel_copy(dc, srp, dsp, width, height);
   1364 }
   1365 
   1366 
   1367 void
   1368 igsfb_accel_copycols(cookie, row, src, dst, num)
   1369 	void *cookie;
   1370 	int row, src, dst, num;
   1371 {
   1372 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1373 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)ri->ri_hw;
   1374 	uint32_t rowp, srp, dsp;
   1375 	uint16_t width, height;
   1376 
   1377 	width = num * ri->ri_font->fontwidth;
   1378 	height = ri->ri_font->fontheight;
   1379 
   1380 	rowp = ri->ri_xorigin
   1381 		+ ri->ri_width * (ri->ri_yorigin
   1382 				  + row * ri->ri_font->fontheight);
   1383 
   1384 	srp = rowp + src * ri->ri_font->fontwidth;
   1385 	dsp = rowp + dst * ri->ri_font->fontwidth;
   1386 
   1387 	igsfb_accel_copy(dc, srp, dsp, width, height);
   1388 }
   1389 
   1390 
   1391 static void
   1392 igsfb_accel_eraserows(cookie, row, num, attr)
   1393 	void *cookie;
   1394 	int row, num;
   1395 	long attr;
   1396 {
   1397 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1398 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)ri->ri_hw;
   1399 	uint32_t color;
   1400 	uint32_t dsp;
   1401 	uint16_t width, height;
   1402 
   1403 	width = ri->ri_emuwidth;
   1404 	height = num * ri->ri_font->fontheight;
   1405 
   1406 	dsp = ri->ri_xorigin
   1407 		+ ri->ri_width * (ri->ri_yorigin
   1408 				  + row * ri->ri_font->fontheight);
   1409 
   1410 	/* XXX: we "know" the encoding that rasops' allocattr uses */
   1411 	color = (attr >> 16) & 0xff;
   1412 
   1413 	igsfb_accel_fill(dc, color, dsp, width, height);
   1414 }
   1415 
   1416 
   1417 void
   1418 igsfb_accel_erasecols(cookie, row, col, num, attr)
   1419 	void *cookie;
   1420 	int row, col, num;
   1421 	long attr;
   1422 {
   1423 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1424 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)ri->ri_hw;
   1425 	uint32_t color;
   1426 	uint32_t rowp, dsp;
   1427 	uint16_t width, height;
   1428 
   1429 	width = num * ri->ri_font->fontwidth;
   1430 	height = ri->ri_font->fontheight;
   1431 
   1432 	rowp = ri->ri_xorigin
   1433 		+ ri->ri_width * (ri->ri_yorigin
   1434 				  + row * ri->ri_font->fontheight);
   1435 
   1436 	dsp = rowp + col * ri->ri_font->fontwidth;
   1437 
   1438 	/* XXX: we "know" the encoding that rasops' allocattr uses */
   1439 	color = (attr >> 16) & 0xff;
   1440 
   1441 	igsfb_accel_fill(dc, color, dsp, width, height);
   1442 }
   1443 
   1444 
   1445 /*
   1446  * Not really implemented here, but we need to hook into the one
   1447  * supplied by rasops so that we can synchronize with the COP.
   1448  */
   1449 static void
   1450 igsfb_accel_putchar(cookie, row, col, uc, attr)
   1451 	void *cookie;
   1452 	int row, col;
   1453 	u_int uc;
   1454 	long attr;
   1455 {
   1456 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1457 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)ri->ri_hw;
   1458 
   1459 	igsfb_accel_wait(dc);
   1460 	(*dc->dc_ri_putchar)(cookie, row, col, uc, attr);
   1461 }
   1462