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