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