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