Home | History | Annotate | Line # | Download | only in ic
igsfb.c revision 1.4
      1 /*	$NetBSD: igsfb.c,v 1.4 2002/07/21 02:56:35 uwe Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002 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  * Only tested on IGA 1682 in Krups JavaStation-NC.
     33  */
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: igsfb.c,v 1.4 2002/07/21 02:56:35 uwe Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/buf.h>
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <machine/bus.h>
     47 
     48 #include <dev/wscons/wsdisplayvar.h>
     49 #include <dev/rasops/rasops.h>
     50 #include <dev/wsfont/wsfont.h>
     51 #include <dev/wscons/wsconsio.h>
     52 
     53 #include <dev/ic/igsfbreg.h>
     54 #include <dev/ic/igsfbvar.h>
     55 
     56 
     57 /*
     58  * wsscreen
     59  */
     60 
     61 /* filled from rasops_info in igsfb_common_init */
     62 static struct wsscreen_descr igsfb_stdscreen = {
     63 	"std",			/* name */
     64 	0, 0,			/* ncols, nrows */
     65 	NULL,			/* textops */
     66 	0, 0,			/* fontwidth, fontheight */
     67 	0			/* capabilities */
     68 };
     69 
     70 static const struct wsscreen_descr *_igsfb_scrlist[] = {
     71 	&igsfb_stdscreen,
     72 };
     73 
     74 static const struct wsscreen_list igsfb_screenlist = {
     75 	sizeof(_igsfb_scrlist) / sizeof(struct wsscreen_descr *),
     76 	_igsfb_scrlist
     77 };
     78 
     79 
     80 /*
     81  * wsdisplay_accessops
     82  */
     83 
     84 static int	igsfb_ioctl(void *, u_long, caddr_t, int, struct proc *);
     85 static paddr_t	igsfb_mmap(void *, off_t, int);
     86 
     87 static int	igsfb_alloc_screen(void *, const struct wsscreen_descr *,
     88 				   void **, int *, int *, long *);
     89 static void	igsfb_free_screen(void *, void *);
     90 static int	igsfb_show_screen(void *, void *, int,
     91 				  void (*) (void *, int, int), void *);
     92 
     93 static const struct wsdisplay_accessops igsfb_accessops = {
     94 	igsfb_ioctl,
     95 	igsfb_mmap,
     96 	igsfb_alloc_screen,
     97 	igsfb_free_screen,
     98 	igsfb_show_screen,
     99 	NULL /* load_font */
    100 };
    101 
    102 
    103 /*
    104  * internal functions
    105  */
    106 static void	igsfb_common_init(struct igsfb_softc *);
    107 static void	igsfb_init_bit_tables(struct igsfb_softc *);
    108 static void	igsfb_blank_screen(struct igsfb_softc *, int);
    109 static int	igsfb_get_cmap(struct igsfb_softc *, struct wsdisplay_cmap *);
    110 static int	igsfb_set_cmap(struct igsfb_softc *, struct wsdisplay_cmap *);
    111 static void	igsfb_update_cmap(struct igsfb_softc *sc, u_int, u_int);
    112 static void	igsfb_set_curpos(struct igsfb_softc *,
    113 				 struct wsdisplay_curpos *);
    114 static void	igsfb_update_curpos(struct igsfb_softc *);
    115 static int	igsfb_get_cursor(struct igsfb_softc *,
    116 				 struct wsdisplay_cursor *);
    117 static int	igsfb_set_cursor(struct igsfb_softc *,
    118 				 struct wsdisplay_cursor *);
    119 static void	igsfb_update_cursor(struct igsfb_softc *, u_int);
    120 static void	igsfb_convert_cursor_data(struct igsfb_softc *, u_int, u_int);
    121 
    122 /*
    123  * bit expanders
    124  */
    125 static u_int16_t igsfb_spread_bits_8(u_int8_t);
    126 
    127 static struct igs_bittab *igsfb_bittab = NULL;
    128 static struct igs_bittab *igsfb_bittab_bswap = NULL;
    129 
    130 
    131 /*
    132  * Enable chip.  This always goes through I/O space because
    133  * uninitialized card only decodes I/O accesses to VDO and VSE.
    134  */
    135 int
    136 igsfb_enable(iot)
    137 	bus_space_tag_t iot;
    138 {
    139 	bus_space_handle_t vdoh;
    140 	bus_space_handle_t vseh;
    141 	bus_space_handle_t regh;
    142 	int ret;
    143 
    144 	ret = bus_space_map(iot, IGS_VDO, 1, 0, &vdoh);
    145 	if (ret != 0) {
    146 		printf("unable to map VDO register\n");
    147 		goto out0;
    148 	}
    149 
    150 	ret = bus_space_map(iot, IGS_VSE, 1, 0, &vseh);
    151 	if (ret != 0) {
    152 		printf("unable to map VSE register\n");
    153 		goto out1;
    154 	}
    155 
    156 	ret = bus_space_map(iot, IGS_REG_BASE, IGS_REG_SIZE, 0, &regh);
    157 	if (ret != 0) {
    158 		printf("unable to map I/O registers\n");
    159 		goto out2;
    160 	}
    161 
    162 	/*
    163 	 * Enable video: start decoding i/o space accesses.
    164 	 */
    165 	bus_space_write_1(iot, vdoh, 0, IGS_VDO_ENABLE | IGS_VDO_SETUP);
    166 	bus_space_write_1(iot, vseh, 0, IGS_VSE_ENABLE);
    167 	bus_space_write_1(iot, vdoh, 0, IGS_VDO_ENABLE);
    168 
    169 	/*
    170 	 * Enable memory: start decoding memory space accesses.
    171 	 * While here, enable coprocessor and select IGS_COP_BASE_B.
    172 	 */
    173 	igs_ext_write(iot, regh, IGS_EXT_BIU_MISC_CTL,
    174 		      (IGS_EXT_BIU_LINEAREN
    175 		       | IGS_EXT_BIU_COPREN | IGS_EXT_BIU_COPASELB));
    176 
    177 	bus_space_unmap(iot, regh, IGS_REG_SIZE);
    178   out2:	bus_space_unmap(iot, vseh, 1);
    179   out1:	bus_space_unmap(iot, vdoh, 1);
    180   out0: return (ret);
    181 }
    182 
    183 
    184 /*
    185  * Finish off the attach.  Bus specific attach method should have
    186  * enabled io and memory accesses and mapped io and cop registers.
    187  */
    188 void
    189 igsfb_common_attach(sc, isconsole)
    190 	struct igsfb_softc *sc;
    191 	int isconsole;
    192 {
    193 	bus_space_handle_t tmph;
    194 	u_int8_t *p;
    195 	int need_bswap;
    196 	char *bswap_msg;
    197 	bus_addr_t fbaddr;
    198 	bus_addr_t craddr;
    199 	off_t croffset;
    200 	struct rasops_info *ri;
    201 	struct wsemuldisplaydev_attach_args waa;
    202 	u_int8_t busctl, curctl;
    203 
    204 	busctl = igs_ext_read(sc->sc_iot, sc->sc_ioh, IGS_EXT_BUS_CTL);
    205 	if (busctl & 0x2)
    206 		sc->sc_vmemsz = 4 << 20;
    207 	else if (busctl & 0x1)
    208 		sc->sc_vmemsz = 2 << 20;
    209 	else
    210 		sc->sc_vmemsz = 1 << 20;
    211 
    212 	/*
    213 	 * Check for endianness mismatch by writing a word at the end
    214 	 * of video memory (off-screen) and reading it back byte-by-byte.
    215 	 */
    216 	if (bus_space_map(sc->sc_memt,
    217 			  sc->sc_memaddr + sc->sc_vmemsz - sizeof(u_int32_t),
    218 			  sizeof(u_int32_t),
    219 			  sc->sc_memflags | BUS_SPACE_MAP_LINEAR,
    220 			  &tmph) != 0)
    221 	{
    222 		printf("unable to map video memory for endianness test\n");
    223 		return;
    224 	}
    225 
    226 	p = bus_space_vaddr(sc->sc_memt, tmph);
    227 #if BYTE_ORDER == BIG_ENDIAN
    228 	*((u_int32_t *)p) = 0x12345678;
    229 #else
    230 	*((u_int32_t *)p) = 0x78563412;
    231 #endif
    232 	if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
    233 		need_bswap = 0;
    234 	else
    235 		need_bswap = 1;
    236 
    237 	bus_space_unmap(sc->sc_memt, tmph, sizeof(u_int32_t));
    238 
    239 	/*
    240 	 * On CyberPro we can use magic bswap bit in linear address.
    241 	 */
    242 	fbaddr = sc->sc_memaddr;
    243 	if (need_bswap)
    244 		if (sc->sc_is2k) {
    245 			fbaddr |= IGS_MEM_BE_SELECT;
    246 			bswap_msg = ", hw bswap";
    247 		} else {
    248 			sc->sc_hwflags |= IGSFB_HW_BSWAP;
    249 			bswap_msg = ", sw bswap"; /* sic! */
    250 		}
    251 	else
    252 		bswap_msg = "";
    253 
    254 	/*
    255 	 * Don't map in all N megs, just the amount we need for wsscreen
    256 	 */
    257 	sc->sc_fbsz = 1024 * 768; /* XXX: 8bpp specific */
    258 	if (bus_space_map(sc->sc_memt, fbaddr, sc->sc_fbsz,
    259 			  sc->sc_memflags | BUS_SPACE_MAP_LINEAR,
    260 			  &sc->sc_fbh) != 0)
    261 	{
    262 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, IGS_REG_SIZE);
    263 		printf("unable to map framebuffer\n");
    264 		return;
    265 	}
    266 
    267 	/*
    268 	 * 1Kb for cursor sprite data at the very end of video memory
    269 	 */
    270 	croffset = sc->sc_vmemsz - IGS_CURSOR_DATA_SIZE;
    271 	craddr = fbaddr + croffset;
    272 	if (bus_space_map(sc->sc_memt, craddr, IGS_CURSOR_DATA_SIZE,
    273 			  sc->sc_memflags | BUS_SPACE_MAP_LINEAR,
    274 			  &sc->sc_crh) != 0)
    275 	{
    276 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, IGS_REG_SIZE);
    277 		bus_space_unmap(sc->sc_memt, sc->sc_fbh, sc->sc_fbsz);
    278 		printf("unable to map cursor sprite region\n");
    279 		return;
    280 	}
    281 
    282 	/*
    283 	 * Tell device where cursor sprite data are located in linear
    284 	 * space (it takes data offset in 1k units).
    285 	 */
    286 	croffset >>= 10;
    287 	igs_ext_write(sc->sc_iot, sc->sc_ioh,
    288 		      IGS_EXT_SPRITE_DATA_LO, croffset & 0xff);
    289 	igs_ext_write(sc->sc_iot, sc->sc_ioh,
    290 		      IGS_EXT_SPRITE_DATA_HI, (croffset >> 8) & 0xf);
    291 
    292 	memset(&sc->sc_cursor, 0, sizeof(struct igs_hwcursor));
    293 	memset(bus_space_vaddr(sc->sc_memt, sc->sc_crh),
    294 	       0xaa, IGS_CURSOR_DATA_SIZE); /* transparent */
    295 
    296 	curctl = igs_ext_read(sc->sc_iot, sc->sc_ioh, IGS_EXT_SPRITE_CTL);
    297 	curctl |= IGS_EXT_SPRITE_64x64;
    298 	curctl &= ~IGS_EXT_SPRITE_VISIBLE;
    299 	igs_ext_write(sc->sc_iot, sc->sc_ioh, IGS_EXT_SPRITE_CTL, curctl);
    300 
    301 	/* bit expanders for cursor sprite data */
    302 	igsfb_init_bit_tables(sc);
    303 
    304 	/* alloc and cross-link raster ops */
    305 	ri = malloc(sizeof(struct rasops_info), M_DEVBUF, M_NOWAIT | M_ZERO);
    306 	if (ri == NULL)
    307 		panic("unable to allocate rasops");
    308 	ri->ri_hw = sc;
    309 	sc->sc_ri = ri;
    310 
    311 	igsfb_common_init(sc);
    312 
    313 	/*
    314 	 * XXX: console attachment needs rethinking
    315 	 */
    316 	if (isconsole) {
    317 		long defattr;
    318 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    319 		wsdisplay_cnattach(&igsfb_stdscreen, ri, 0, 0, defattr);
    320 	}
    321 
    322 
    323 	printf("%s: %dmb%s, %dx%d, %dbpp\n",
    324 	       sc->sc_dev.dv_xname,
    325 	       (u_int32_t)(sc->sc_vmemsz >> 20),
    326 	       bswap_msg,
    327 	       ri->ri_width, ri->ri_height, ri->ri_depth);
    328 
    329 	/* attach wsdisplay */
    330 	waa.console = isconsole;
    331 	waa.scrdata = &igsfb_screenlist;
    332 	waa.accessops = &igsfb_accessops;
    333 	waa.accesscookie = sc;
    334 
    335 	config_found(&sc->sc_dev, &waa, wsemuldisplaydevprint);
    336 }
    337 
    338 
    339 /*
    340  * Helper function for igsfb_init_bit_tables().
    341  */
    342 static u_int16_t
    343 igsfb_spread_bits_8(b)
    344 	u_int8_t b;
    345 {
    346 	u_int16_t s = b;
    347 
    348 	s = ((s & 0x00f0) << 4) | (s & 0x000f);
    349 	s = ((s & 0x0c0c) << 2) | (s & 0x0303);
    350 	s = ((s & 0x2222) << 1) | (s & 0x1111);
    351 	return (s);
    352 }
    353 
    354 
    355 /*
    356  * Cursor sprite data are in 2bpp.  Incoming image/mask are in 1bpp.
    357  * Prebuild tables to expand 1bpp->2bpp with bswapping if neccessary.
    358  */
    359 static void
    360 igsfb_init_bit_tables(sc)
    361 	struct igsfb_softc *sc;
    362 {
    363 	struct igs_bittab *tab;
    364 	u_int i;
    365 
    366 	if (sc->sc_hwflags & IGSFB_HW_BSWAP) {
    367 		if (igsfb_bittab_bswap == NULL) {
    368 			tab = malloc(sizeof(struct igs_bittab),
    369 				     M_DEVBUF, M_NOWAIT);
    370 			for (i = 0; i < 256; ++i) {
    371 				u_int16_t s = igsfb_spread_bits_8(i);
    372 				tab->iexpand[i] = bswap16(s);
    373 				tab->mexpand[i] = bswap16((s << 1) | s);
    374 			}
    375 			igsfb_bittab_bswap = tab;
    376 		}
    377 		sc->sc_bittab = igsfb_bittab_bswap;
    378 	} else {
    379 		if (igsfb_bittab == NULL) {
    380 			tab = malloc(sizeof(struct igs_bittab),
    381 				     M_DEVBUF, M_NOWAIT);
    382 			for (i = 0; i < 256; ++i) {
    383 				u_int16_t s = igsfb_spread_bits_8(i);
    384 				tab->iexpand[i] = s;
    385 				tab->mexpand[i] = (s << 1) | s;
    386 			}
    387 			igsfb_bittab = tab;
    388 		}
    389 		sc->sc_bittab = igsfb_bittab;
    390 	}
    391 }
    392 
    393 /*
    394  * I/O and memory are mapped, video enabled, structures allocated.
    395  */
    396 static void
    397 igsfb_common_init(sc)
    398 	struct igsfb_softc *sc;
    399 {
    400 	bus_space_tag_t iot = sc->sc_iot;
    401 	bus_space_handle_t ioh = sc->sc_ioh;
    402 	struct rasops_info *ri = sc->sc_ri;
    403 	int wsfcookie;
    404 	const u_int8_t *p;
    405 	int i;
    406 
    407 	sc->sc_blanked = 0;
    408 
    409 	ri->ri_flg = RI_CENTER | RI_CLEAR;
    410 	if (sc->sc_hwflags & IGSFB_HW_BSWAP)
    411 	    ri->ri_flg |= RI_BSWAP;
    412 
    413 	ri->ri_depth = 8;
    414 	ri->ri_width = 1024;
    415 	ri->ri_height = 768;
    416 	ri->ri_stride = 1024;
    417 	ri->ri_bits = (u_char *)sc->sc_fbh;
    418 
    419 	/*
    420 	 * Initialize wsfont related stuff.
    421 	 */
    422 	wsfont_init();
    423 
    424 	/* prefer gallant that is identical to the one the prom uses */
    425 	wsfcookie = wsfont_find("Gallant", 12, 22, 0,
    426 				WSDISPLAY_FONTORDER_L2R,
    427 				WSDISPLAY_FONTORDER_L2R);
    428 	if (wsfcookie <= 0) {
    429 #ifdef DIAGNOSTIC
    430 		printf("%s: unable to find font Gallant 12x22\n",
    431 		       sc->sc_dev.dv_xname);
    432 #endif
    433 		/* any font at all? */
    434 		wsfcookie = wsfont_find(NULL, 0, 0, 0,
    435 					WSDISPLAY_FONTORDER_L2R,
    436 					WSDISPLAY_FONTORDER_L2R);
    437 	}
    438 
    439 	if (wsfcookie <= 0) {
    440 		printf("%s: unable to find any fonts\n", sc->sc_dev.dv_xname);
    441 		return;
    442 	}
    443 
    444 	if (wsfont_lock(wsfcookie, &ri->ri_font) != 0) {
    445 		printf("%s: unable to lock font\n", sc->sc_dev.dv_xname);
    446 		return;
    447 	}
    448 	ri->ri_wsfcookie = wsfcookie;
    449 
    450 
    451 	/*
    452 	 * Initialize colormap related stuff.
    453 	 */
    454 
    455 	/* ANSI color map */
    456 	p = rasops_cmap;
    457 	for (i = 0; i < IGS_CMAP_SIZE; ++i, p += 3) { /* software copy */
    458 		sc->sc_cmap.r[i] = p[0];
    459 		sc->sc_cmap.g[i] = p[1];
    460 		sc->sc_cmap.b[i] = p[2];
    461 	}
    462 	igsfb_update_cmap(sc, 0, IGS_CMAP_SIZE);
    463 
    464 	/* set overscan color r/g/b (XXX: use defattr's rgb?) */
    465 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_RED,   0);
    466 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_GREEN, 0);
    467 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_BLUE,  0);
    468 
    469 
    470 	/* TODO: compute term size based on font dimensions? */
    471 	rasops_init(ri, 34, 80);
    472 
    473 	igsfb_stdscreen.nrows = ri->ri_rows;
    474 	igsfb_stdscreen.ncols = ri->ri_cols;
    475 	igsfb_stdscreen.textops = &ri->ri_ops;
    476 	igsfb_stdscreen.capabilities = ri->ri_caps;
    477 }
    478 
    479 
    480 /*
    481  * wsdisplay_accessops: mmap()
    482  *   XXX: allow mmapping i/o mapped i/o regs if INSECURE???
    483  */
    484 static paddr_t
    485 igsfb_mmap(v, offset, prot)
    486 	void *v;
    487 	off_t offset;
    488 	int prot;
    489 {
    490 	struct igsfb_softc *sc = v;
    491 
    492 	if (offset >= sc->sc_memsz || offset < 0)
    493 		return (-1);
    494 
    495 	return (bus_space_mmap(sc->sc_memt, sc->sc_memaddr, offset, prot,
    496 			       sc->sc_memflags | BUS_SPACE_MAP_LINEAR));
    497 }
    498 
    499 
    500 /*
    501  * wsdisplay_accessops: ioctl()
    502  */
    503 static int
    504 igsfb_ioctl(v, cmd, data, flag, p)
    505 	void *v;
    506 	u_long cmd;
    507 	caddr_t data;
    508 	int flag;
    509 	struct proc *p;
    510 {
    511 	struct igsfb_softc *sc = v;
    512 	struct rasops_info *ri = sc->sc_ri;
    513 	int turnoff;
    514 
    515 	switch (cmd) {
    516 
    517 	case WSDISPLAYIO_GTYPE:
    518 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    519 		return (0);
    520 
    521 	case WSDISPLAYIO_GINFO:
    522 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    523 		wsd_fbip->height = ri->ri_height;
    524 		wsd_fbip->width = ri->ri_width;
    525 		wsd_fbip->depth = ri->ri_depth;
    526 		wsd_fbip->cmsize = IGS_CMAP_SIZE;
    527 #undef wsd_fbip
    528 		return (0);
    529 
    530 	case WSDISPLAYIO_GVIDEO:
    531 		*(u_int *)data = sc->sc_blanked ?
    532 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    533 		return (0);
    534 
    535 	case WSDISPLAYIO_SVIDEO:
    536 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
    537 		if (sc->sc_blanked != turnoff) {
    538 			sc->sc_blanked = turnoff;
    539 			igsfb_blank_screen(sc, sc->sc_blanked);
    540 		}
    541 		return (0);
    542 
    543 	case WSDISPLAYIO_GETCMAP:
    544 		return (igsfb_get_cmap(sc, (struct wsdisplay_cmap *)data));
    545 
    546 	case WSDISPLAYIO_PUTCMAP:
    547 		return (igsfb_set_cmap(sc, (struct wsdisplay_cmap *)data));
    548 
    549 	case WSDISPLAYIO_GCURMAX:
    550 		((struct wsdisplay_curpos *)data)->x = IGS_CURSOR_MAX_SIZE;
    551 		((struct wsdisplay_curpos *)data)->y = IGS_CURSOR_MAX_SIZE;
    552 		return (0);
    553 
    554 	case WSDISPLAYIO_GCURPOS:
    555 		*(struct wsdisplay_curpos *)data = sc->sc_cursor.cc_pos;
    556 		return (0);
    557 
    558 	case WSDISPLAYIO_SCURPOS:
    559 		igsfb_set_curpos(sc, (struct wsdisplay_curpos *)data);
    560 		return (0);
    561 
    562 	case WSDISPLAYIO_GCURSOR:
    563 		return (igsfb_get_cursor(sc, (struct wsdisplay_cursor *)data));
    564 
    565 	case WSDISPLAYIO_SCURSOR:
    566 		return (igsfb_set_cursor(sc, (struct wsdisplay_cursor *)data));
    567 	}
    568 
    569 	return (EPASSTHROUGH);
    570 }
    571 
    572 
    573 /*
    574  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SVIDEO)
    575  */
    576 static void
    577 igsfb_blank_screen(sc, blank)
    578 	struct igsfb_softc *sc;
    579 	int blank;
    580 {
    581 
    582 	igs_ext_write(sc->sc_iot, sc->sc_ioh,
    583 		      IGS_EXT_SYNC_CTL,
    584 		      blank ? IGS_EXT_SYNC_H0 | IGS_EXT_SYNC_V0
    585 			    : 0);
    586 }
    587 
    588 
    589 /*
    590  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GETCMAP)
    591  *   Served from software cmap copy.
    592  */
    593 static int
    594 igsfb_get_cmap(sc, p)
    595 	struct igsfb_softc *sc;
    596 	struct wsdisplay_cmap *p;
    597 {
    598 	u_int index = p->index, count = p->count;
    599 
    600 	if (index >= IGS_CMAP_SIZE || (index + count) > IGS_CMAP_SIZE)
    601 		return (EINVAL);
    602 
    603 	if (!uvm_useracc(p->red, count, B_WRITE) ||
    604 	    !uvm_useracc(p->green, count, B_WRITE) ||
    605 	    !uvm_useracc(p->blue, count, B_WRITE))
    606 		return (EFAULT);
    607 
    608 	copyout(&sc->sc_cmap.r[index], p->red, count);
    609 	copyout(&sc->sc_cmap.g[index], p->green, count);
    610 	copyout(&sc->sc_cmap.b[index], p->blue, count);
    611 
    612 	return (0);
    613 }
    614 
    615 
    616 /*
    617  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SETCMAP)
    618  *   Set software cmap copy and propagate changed range to device.
    619  */
    620 static int
    621 igsfb_set_cmap(sc, p)
    622 	struct igsfb_softc *sc;
    623 	struct wsdisplay_cmap *p;
    624 {
    625 	u_int index = p->index, count = p->count;
    626 
    627 	if (index >= IGS_CMAP_SIZE || (index + count) > IGS_CMAP_SIZE)
    628 		return (EINVAL);
    629 
    630 	if (!uvm_useracc(p->red, count, B_READ) ||
    631 	    !uvm_useracc(p->green, count, B_READ) ||
    632 	    !uvm_useracc(p->blue, count, B_READ))
    633 		return (EFAULT);
    634 
    635 	copyin(p->red, &sc->sc_cmap.r[index], count);
    636 	copyin(p->green, &sc->sc_cmap.g[index], count);
    637 	copyin(p->blue, &sc->sc_cmap.b[index], count);
    638 
    639 	igsfb_update_cmap(sc, p->index, p->count);
    640 
    641 	return (0);
    642 }
    643 
    644 
    645 /*
    646  * Propagate specified part of the software cmap copy to device.
    647  */
    648 static void
    649 igsfb_update_cmap(sc, index, count)
    650 	struct igsfb_softc *sc;
    651 	u_int index, count;
    652 {
    653 	bus_space_tag_t t;
    654 	bus_space_handle_t h;
    655 	u_int last, i;
    656 
    657 	if (index >= IGS_CMAP_SIZE)
    658 		return;
    659 
    660 	last = index + count;
    661 	if (last > IGS_CMAP_SIZE)
    662 		last = IGS_CMAP_SIZE;
    663 
    664 	t = sc->sc_iot;
    665 	h = sc->sc_ioh;
    666 
    667 	/* start palette writing, index is autoincremented by hardware */
    668 	bus_space_write_1(t, h, IGS_DAC_PEL_WRITE_IDX, index);
    669 
    670 	for (i = index; i < last; ++i) {
    671 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, sc->sc_cmap.r[i]);
    672 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, sc->sc_cmap.g[i]);
    673 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, sc->sc_cmap.b[i]);
    674 	}
    675 }
    676 
    677 
    678 /*
    679  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURPOS)
    680  */
    681 static void
    682 igsfb_set_curpos(sc, curpos)
    683 	struct igsfb_softc *sc;
    684 	struct wsdisplay_curpos *curpos;
    685 {
    686 	struct rasops_info *ri = sc->sc_ri;
    687 	int x = curpos->x, y = curpos->y;
    688 
    689 	if (y < 0)
    690 		y = 0;
    691 	else if (y > ri->ri_height)
    692 		y = ri->ri_height;
    693 	if (x < 0)
    694 		x = 0;
    695 	else if (x > ri->ri_width)
    696 		x = ri->ri_width;
    697 	sc->sc_cursor.cc_pos.x = x;
    698 	sc->sc_cursor.cc_pos.y = y;
    699 
    700 	igsfb_update_curpos(sc);
    701 }
    702 
    703 
    704 static void
    705 igsfb_update_curpos(sc)
    706 	struct igsfb_softc *sc;
    707 {
    708 	bus_space_tag_t t;
    709 	bus_space_handle_t h;
    710 	int x, xoff, y, yoff;
    711 
    712 	xoff = 0;
    713 	x = sc->sc_cursor.cc_pos.x - sc->sc_cursor.cc_hot.x;
    714 	if (x < 0) {
    715 		x = 0;
    716 		xoff = -x;
    717 	}
    718 
    719 	yoff = 0;
    720 	y = sc->sc_cursor.cc_pos.y - sc->sc_cursor.cc_hot.y;
    721 	if (y < 0) {
    722 		y = 0;
    723 		yoff = -y;
    724 	}
    725 
    726 	t = sc->sc_iot;
    727 	h = sc->sc_ioh;
    728 
    729 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_LO, x & 0xff);
    730 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_HI, (x >> 8) & 0x07);
    731 	igs_ext_write(t, h, IGS_EXT_SPRITE_HPRESET, xoff & 0x3f);
    732 
    733 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_LO, y & 0xff);
    734 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_HI, (y >> 8) & 0x07);
    735 	igs_ext_write(t, h, IGS_EXT_SPRITE_VPRESET, yoff & 0x3f);
    736 }
    737 
    738 
    739 /*
    740  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GCURSOR)
    741  */
    742 static int
    743 igsfb_get_cursor(sc, p)
    744 	struct igsfb_softc *sc;
    745 	struct wsdisplay_cursor *p;
    746 {
    747 
    748 	/* XXX: TODO */
    749 	return (0);
    750 }
    751 
    752 
    753 /*
    754  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURSOR)
    755  */
    756 static int
    757 igsfb_set_cursor(sc, p)
    758 	struct igsfb_softc *sc;
    759 	struct wsdisplay_cursor *p;
    760 {
    761 	struct igs_hwcursor *cc;
    762 	u_int v, index, count, icount, iwidth;
    763 
    764 	cc = &sc->sc_cursor;
    765 	v = p->which;
    766 
    767 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    768 		index = p->cmap.index;
    769 		count = p->cmap.count;
    770 		if (index >= 2 || (index + count) > 2)
    771 			return (EINVAL);
    772 		if (!uvm_useracc(p->cmap.red, count, B_READ)
    773 		    || !uvm_useracc(p->cmap.green, count, B_READ)
    774 		    || !uvm_useracc(p->cmap.blue, count, B_READ))
    775 			return (EFAULT);
    776 	}
    777 
    778 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    779 		if (p->size.x > IGS_CURSOR_MAX_SIZE
    780 		    || p->size.y > IGS_CURSOR_MAX_SIZE)
    781 			return (EINVAL);
    782 
    783 		iwidth = (p->size.x + 7) >> 3; /* bytes per scan line */
    784 		icount = iwidth * p->size.y;
    785 		if (!uvm_useracc(p->image, icount, B_READ)
    786 		    || !uvm_useracc(p->mask, icount, B_READ))
    787 			return (EFAULT);
    788 	}
    789 
    790 	/* XXX: verify that hot is within size, pos within screen? */
    791 
    792 	/* arguments verified, do the processing */
    793 
    794 	if (v & WSDISPLAY_CURSOR_DOCUR)
    795 		sc->sc_curenb = p->enable;
    796 
    797 	if (v & WSDISPLAY_CURSOR_DOPOS)
    798 		cc->cc_pos = p->pos;
    799 
    800 	if (v & WSDISPLAY_CURSOR_DOHOT)
    801 		cc->cc_hot = p->hot;
    802 
    803 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    804 		copyin(p->cmap.red, &cc->cc_color[index], count);
    805 		copyin(p->cmap.green, &cc->cc_color[index + 2], count);
    806 		copyin(p->cmap.blue, &cc->cc_color[index + 4], count);
    807 	}
    808 
    809 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    810 		u_int trailing_bits;
    811 
    812 		copyin(p->image, cc->cc_image, icount);
    813 		copyin(p->mask, cc->cc_mask, icount);
    814 		cc->cc_size = p->size;
    815 
    816 		/* clear trailing bits in the "partial" mask bytes */
    817 		trailing_bits = p->size.x & 0x07;
    818 		if (trailing_bits != 0) {
    819 			const u_int cutmask = ~((~0) << trailing_bits);
    820 			u_char *mp;
    821 			u_int i;
    822 
    823 			mp = cc->cc_mask + iwidth - 1;
    824 			for (i = 0; i < p->size.y; ++i) {
    825 				*mp &= cutmask;
    826 				mp += iwidth;
    827 			}
    828 		}
    829 		igsfb_convert_cursor_data(sc, iwidth, p->size.y);
    830 	}
    831 
    832 	igsfb_update_cursor(sc, v);
    833 	return (0);
    834 }
    835 
    836 
    837 /*
    838  * Convert incoming 1bpp cursor image/mask into native 2bpp format.
    839  */
    840 static void
    841 igsfb_convert_cursor_data(sc, w, h)
    842 	struct igsfb_softc *sc;
    843 	u_int w, h;
    844 {
    845 	struct igs_hwcursor *cc = &sc->sc_cursor;
    846 	struct igs_bittab *btab = sc->sc_bittab;
    847 	u_int8_t *ip, *mp;
    848 	u_int16_t *dp;
    849 	u_int line, i;
    850 
    851 	/* init sprite to be all transparent */
    852 	memset(cc->cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
    853 
    854 	/* first scanline */
    855 	ip = cc->cc_image;
    856 	mp = cc->cc_mask;
    857 	dp = cc->cc_sprite;
    858 
    859 	for (line = 0; line < h; ++line) {
    860 		for (i = 0; i < w; ++i) {
    861 			u_int16_t is = btab->iexpand[ip[i]];
    862 			u_int16_t ms = btab->mexpand[mp[i]];
    863 
    864 			/* NB: tables are pre-bswapped if needed */
    865 			dp[i] = (0xaaaa & ~ms) | (is & ms);
    866 		}
    867 
    868 		/* next scanline */
    869 		ip += w;
    870 		mp += w;
    871 		dp += 8;	/* 2bpp, 8 pixels per short = 8 shorts */
    872 	}
    873 }
    874 
    875 
    876 /*
    877  * Propagate cursor changes to device.
    878  * "which" is composed from WSDISPLAY_CURSOR_DO* bits.
    879  */
    880 static void
    881 igsfb_update_cursor(sc, which)
    882 	struct igsfb_softc *sc;
    883 	u_int which;
    884 {
    885 	bus_space_tag_t iot = sc->sc_iot;
    886 	bus_space_handle_t ioh = sc->sc_ioh;
    887 	u_int8_t curctl;
    888 
    889 	/*
    890 	 * We will need to tweak sprite control register for cursor
    891 	 * visibility and cursor color map manipualtion.
    892 	 */
    893 	if (which & (WSDISPLAY_CURSOR_DOCUR | WSDISPLAY_CURSOR_DOCMAP)) {
    894 		curctl = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
    895 	}
    896 
    897 	if (which & WSDISPLAY_CURSOR_DOSHAPE) {
    898 		u_int8_t *dst = bus_space_vaddr(sc->sc_memt, sc->sc_crh);
    899 
    900 		/*
    901 		 * memcpy between spaces of different endianness would
    902 		 * be ... special.  We cannot be sure if memset gonna
    903 		 * push data in 4-byte chunks, we can't pre-bswap it,
    904 		 * so do it byte-by-byte to preserve byte ordering.
    905 		 */
    906 		if (sc->sc_hwflags & IGSFB_HW_BSWAP) {
    907 			u_int8_t *src = (u_int8_t *)sc->sc_cursor.cc_sprite;
    908 			int i;
    909 
    910 			for (i = 0; i < 1024; ++i)
    911 				*dst++ = *src++;
    912 		} else {
    913 			memcpy(dst, sc->sc_cursor.cc_sprite, 1024);
    914 		}
    915 	}
    916 
    917 	if (which & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
    918 		/* code shared with WSDISPLAYIO_SCURPOS */
    919 		igsfb_update_curpos(sc);
    920 	}
    921 
    922 	if (which & WSDISPLAY_CURSOR_DOCMAP) {
    923 		u_int8_t *p;
    924 
    925 		/* tell DAC we want access to the cursor palette */
    926 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
    927 			      curctl | IGS_EXT_SPRITE_DAC_PEL);
    928 
    929 		p = sc->sc_cursor.cc_color;
    930 
    931 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 0);
    932 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[0]);
    933 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[2]);
    934 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[4]);
    935 
    936 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 1);
    937 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[1]);
    938 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[3]);
    939 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[5]);
    940 
    941 		/* restore access to normal palette */
    942 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, curctl);
    943 	}
    944 
    945 	if (which & WSDISPLAY_CURSOR_DOCUR) {
    946 		if ((curctl & IGS_EXT_SPRITE_VISIBLE) == 0
    947 		    && sc->sc_curenb)
    948 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
    949 				      curctl | IGS_EXT_SPRITE_VISIBLE);
    950 		else if ((curctl & IGS_EXT_SPRITE_VISIBLE) != 0
    951 			 && !sc->sc_curenb)
    952 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
    953 				      curctl & ~IGS_EXT_SPRITE_VISIBLE);
    954 	}
    955 }
    956 
    957 
    958 /*
    959  * wsdisplay_accessops: alloc_screen()
    960  */
    961 static int
    962 igsfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    963 	void *v;
    964 	const struct wsscreen_descr *type;
    965 	void **cookiep;
    966 	int *curxp, *curyp;
    967 	long *attrp;
    968 {
    969 
    970 	/* TODO */
    971 	return (ENOMEM);
    972 }
    973 
    974 
    975 /*
    976  * wsdisplay_accessops: free_screen()
    977  */
    978 static void
    979 igsfb_free_screen(v, cookie)
    980 	void *v;
    981 	void *cookie;
    982 {
    983 
    984 	/* TODO */
    985 	return;
    986 }
    987 
    988 
    989 /*
    990  * wsdisplay_accessops: show_screen()
    991  */
    992 static int
    993 igsfb_show_screen(v, cookie, waitok, cb, cbarg)
    994 	void *v;
    995 	void *cookie;
    996 	int waitok;
    997 	void (*cb)(void *, int, int);
    998 	void *cbarg;
    999 {
   1000 
   1001 	return (0);
   1002 }
   1003