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