Home | History | Annotate | Line # | Download | only in ic
igsfb.c revision 1.8
      1 /*	$NetBSD: igsfb.c,v 1.8 2003/05/10 01:51:56 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.8 2003/05/10 01:51:56 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_tables(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 			       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 				 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 				 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 bit expanders for cursor sprite data */
    307 	igsfb_init_bit_tables(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_tables().
    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 tables to expand 1bpp->2bpp, with bswapping if neccessary.
    437  */
    438 static void
    439 igsfb_init_bit_tables(dc)
    440 	struct igsfb_devconfig *dc;
    441 {
    442 	struct igs_bittab *tab = &dc->dc_bittab;
    443 	int need_bswap = IGSFB_HW_SOFT_BSWAP(dc);
    444 	u_int i;
    445 
    446 	for (i = 0; i < 256; ++i) {
    447 		u_int16_t s = igsfb_spread_bits_8(i);
    448 
    449 		if (need_bswap)
    450 			s = bswap16(s);
    451 
    452 		tab->iexpand[i] = s;
    453 		tab->mexpand[i] = (s << 1) | s;	/* invariant over bswap16(s) */
    454 	}
    455 }
    456 
    457 
    458 /*
    459  * wsdisplay_accessops: mmap()
    460  *   XXX: allow mmapping i/o mapped i/o regs if INSECURE???
    461  */
    462 static paddr_t
    463 igsfb_mmap(v, offset, prot)
    464 	void *v;
    465 	off_t offset;
    466 	int prot;
    467 {
    468 	struct igsfb_devconfig *dc = v;
    469 
    470 	if (offset >= dc->dc_memsz || offset < 0)
    471 		return (-1);
    472 
    473 	return (bus_space_mmap(dc->dc_memt, dc->dc_memaddr, offset, prot,
    474 			       dc->dc_memflags | BUS_SPACE_MAP_LINEAR));
    475 }
    476 
    477 
    478 /*
    479  * wsdisplay_accessops: ioctl()
    480  */
    481 static int
    482 igsfb_ioctl(v, cmd, data, flag, p)
    483 	void *v;
    484 	u_long cmd;
    485 	caddr_t data;
    486 	int flag;
    487 	struct proc *p;
    488 {
    489 	struct igsfb_devconfig *dc = v;
    490 	struct rasops_info *ri;
    491 	int turnoff;
    492 
    493 	switch (cmd) {
    494 
    495 	case WSDISPLAYIO_GTYPE:
    496 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    497 		return (0);
    498 
    499 	case WSDISPLAYIO_GINFO:
    500 		ri = &dc->dc_ri;
    501 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    502 		wsd_fbip->height = ri->ri_height;
    503 		wsd_fbip->width = ri->ri_width;
    504 		wsd_fbip->depth = ri->ri_depth;
    505 		wsd_fbip->cmsize = IGS_CMAP_SIZE;
    506 #undef wsd_fbip
    507 		return (0);
    508 
    509 	case WSDISPLAYIO_GVIDEO:
    510 		*(u_int *)data = dc->dc_blanked ?
    511 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
    512 		return (0);
    513 
    514 	case WSDISPLAYIO_SVIDEO:
    515 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
    516 		if (dc->dc_blanked != turnoff) {
    517 			dc->dc_blanked = turnoff;
    518 			igsfb_blank_screen(dc, dc->dc_blanked);
    519 		}
    520 		return (0);
    521 
    522 	case WSDISPLAYIO_GETCMAP:
    523 		return (igsfb_get_cmap(dc, (struct wsdisplay_cmap *)data));
    524 
    525 	case WSDISPLAYIO_PUTCMAP:
    526 		return (igsfb_set_cmap(dc, (struct wsdisplay_cmap *)data));
    527 
    528 	case WSDISPLAYIO_GCURMAX:
    529 		((struct wsdisplay_curpos *)data)->x = IGS_CURSOR_MAX_SIZE;
    530 		((struct wsdisplay_curpos *)data)->y = IGS_CURSOR_MAX_SIZE;
    531 		return (0);
    532 
    533 	case WSDISPLAYIO_GCURPOS:
    534 		*(struct wsdisplay_curpos *)data = dc->dc_cursor.cc_pos;
    535 		return (0);
    536 
    537 	case WSDISPLAYIO_SCURPOS:
    538 		igsfb_set_curpos(dc, (struct wsdisplay_curpos *)data);
    539 		return (0);
    540 
    541 	case WSDISPLAYIO_GCURSOR:
    542 		return (igsfb_get_cursor(dc, (struct wsdisplay_cursor *)data));
    543 
    544 	case WSDISPLAYIO_SCURSOR:
    545 		return (igsfb_set_cursor(dc, (struct wsdisplay_cursor *)data));
    546 	}
    547 
    548 	return (EPASSTHROUGH);
    549 }
    550 
    551 
    552 /*
    553  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SVIDEO)
    554  */
    555 static void
    556 igsfb_blank_screen(dc, blank)
    557 	struct igsfb_devconfig *dc;
    558 	int blank;
    559 {
    560 
    561 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
    562 		      IGS_EXT_SYNC_CTL,
    563 		      blank ? IGS_EXT_SYNC_H0 | IGS_EXT_SYNC_V0
    564 			    : 0);
    565 }
    566 
    567 
    568 /*
    569  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GETCMAP)
    570  *   Served from the software cmap copy.
    571  */
    572 static int
    573 igsfb_get_cmap(dc, p)
    574 	struct igsfb_devconfig *dc;
    575 	struct wsdisplay_cmap *p;
    576 {
    577 	u_int index = p->index, count = p->count;
    578 
    579 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    580 		return (EINVAL);
    581 
    582 	if (!uvm_useracc(p->red, count, B_WRITE) ||
    583 	    !uvm_useracc(p->green, count, B_WRITE) ||
    584 	    !uvm_useracc(p->blue, count, B_WRITE))
    585 		return (EFAULT);
    586 
    587 	copyout(&dc->dc_cmap.r[index], p->red, count);
    588 	copyout(&dc->dc_cmap.g[index], p->green, count);
    589 	copyout(&dc->dc_cmap.b[index], p->blue, count);
    590 
    591 	return (0);
    592 }
    593 
    594 
    595 /*
    596  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SETCMAP)
    597  *   Set the software cmap copy and propagate changed range to the device.
    598  */
    599 static int
    600 igsfb_set_cmap(dc, p)
    601 	struct igsfb_devconfig *dc;
    602 	struct wsdisplay_cmap *p;
    603 {
    604 	u_int index = p->index, count = p->count;
    605 
    606 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
    607 		return (EINVAL);
    608 
    609 	if (!uvm_useracc(p->red, count, B_READ) ||
    610 	    !uvm_useracc(p->green, count, B_READ) ||
    611 	    !uvm_useracc(p->blue, count, B_READ))
    612 		return (EFAULT);
    613 
    614 	copyin(p->red, &dc->dc_cmap.r[index], count);
    615 	copyin(p->green, &dc->dc_cmap.g[index], count);
    616 	copyin(p->blue, &dc->dc_cmap.b[index], count);
    617 
    618 	igsfb_update_cmap(dc, p->index, p->count);
    619 
    620 	return (0);
    621 }
    622 
    623 
    624 /*
    625  * Propagate specified part of the software cmap copy to the device.
    626  */
    627 static void
    628 igsfb_update_cmap(dc, index, count)
    629 	struct igsfb_devconfig *dc;
    630 	u_int index, count;
    631 {
    632 	bus_space_tag_t t;
    633 	bus_space_handle_t h;
    634 	u_int last, i;
    635 
    636 	if (index >= IGS_CMAP_SIZE)
    637 		return;
    638 
    639 	last = index + count;
    640 	if (last > IGS_CMAP_SIZE)
    641 		last = IGS_CMAP_SIZE;
    642 
    643 	t = dc->dc_iot;
    644 	h = dc->dc_ioh;
    645 
    646 	/* start palette writing, index is autoincremented by hardware */
    647 	bus_space_write_1(t, h, IGS_DAC_PEL_WRITE_IDX, index);
    648 
    649 	for (i = index; i < last; ++i) {
    650 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.r[i]);
    651 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.g[i]);
    652 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.b[i]);
    653 	}
    654 }
    655 
    656 
    657 /*
    658  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURPOS)
    659  */
    660 static void
    661 igsfb_set_curpos(dc, curpos)
    662 	struct igsfb_devconfig *dc;
    663 	struct wsdisplay_curpos *curpos;
    664 {
    665 	struct rasops_info *ri = &dc->dc_ri;
    666 	int x = curpos->x, y = curpos->y;
    667 
    668 	if (y < 0)
    669 		y = 0;
    670 	else if (y > ri->ri_height)
    671 		y = ri->ri_height;
    672 	if (x < 0)
    673 		x = 0;
    674 	else if (x > ri->ri_width)
    675 		x = ri->ri_width;
    676 	dc->dc_cursor.cc_pos.x = x;
    677 	dc->dc_cursor.cc_pos.y = y;
    678 
    679 	igsfb_update_curpos(dc);
    680 }
    681 
    682 
    683 static void
    684 igsfb_update_curpos(dc)
    685 	struct igsfb_devconfig *dc;
    686 {
    687 	bus_space_tag_t t;
    688 	bus_space_handle_t h;
    689 	int x, xoff, y, yoff;
    690 
    691 	xoff = 0;
    692 	x = dc->dc_cursor.cc_pos.x - dc->dc_cursor.cc_hot.x;
    693 	if (x < 0) {
    694 		xoff = -x;
    695 		x = 0;
    696 	}
    697 
    698 	yoff = 0;
    699 	y = dc->dc_cursor.cc_pos.y - dc->dc_cursor.cc_hot.y;
    700 	if (y < 0) {
    701 		yoff = -y;
    702 		y = 0;
    703 	}
    704 
    705 	t = dc->dc_iot;
    706 	h = dc->dc_ioh;
    707 
    708 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_LO, x & 0xff);
    709 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_HI, (x >> 8) & 0x07);
    710 	igs_ext_write(t, h, IGS_EXT_SPRITE_HPRESET, xoff & 0x3f);
    711 
    712 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_LO, y & 0xff);
    713 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_HI, (y >> 8) & 0x07);
    714 	igs_ext_write(t, h, IGS_EXT_SPRITE_VPRESET, yoff & 0x3f);
    715 }
    716 
    717 
    718 /*
    719  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GCURSOR)
    720  */
    721 static int
    722 igsfb_get_cursor(dc, p)
    723 	struct igsfb_devconfig *dc;
    724 	struct wsdisplay_cursor *p;
    725 {
    726 
    727 	/* XXX: TODO */
    728 	return (0);
    729 }
    730 
    731 
    732 /*
    733  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURSOR)
    734  */
    735 static int
    736 igsfb_set_cursor(dc, p)
    737 	struct igsfb_devconfig *dc;
    738 	struct wsdisplay_cursor *p;
    739 {
    740 	struct igs_hwcursor *cc;
    741 	u_int v, index, count, icount, iwidth;
    742 
    743 	cc = &dc->dc_cursor;
    744 	v = p->which;
    745 
    746 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    747 		index = p->cmap.index;
    748 		count = p->cmap.count;
    749 		if (index >= 2 || (index + count) > 2)
    750 			return (EINVAL);
    751 		if (!uvm_useracc(p->cmap.red, count, B_READ)
    752 		    || !uvm_useracc(p->cmap.green, count, B_READ)
    753 		    || !uvm_useracc(p->cmap.blue, count, B_READ))
    754 			return (EFAULT);
    755 	}
    756 
    757 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    758 		if (p->size.x > IGS_CURSOR_MAX_SIZE
    759 		    || p->size.y > IGS_CURSOR_MAX_SIZE)
    760 			return (EINVAL);
    761 
    762 		iwidth = (p->size.x + 7) >> 3; /* bytes per scan line */
    763 		icount = iwidth * p->size.y;
    764 		if (!uvm_useracc(p->image, icount, B_READ)
    765 		    || !uvm_useracc(p->mask, icount, B_READ))
    766 			return (EFAULT);
    767 	}
    768 
    769 	/* XXX: verify that hot is within size, pos within screen? */
    770 
    771 	/* arguments verified, do the processing */
    772 
    773 	if (v & WSDISPLAY_CURSOR_DOCUR)
    774 		dc->dc_curenb = p->enable;
    775 
    776 	if (v & WSDISPLAY_CURSOR_DOPOS)
    777 		cc->cc_pos = p->pos;
    778 
    779 	if (v & WSDISPLAY_CURSOR_DOHOT)
    780 		cc->cc_hot = p->hot;
    781 
    782 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
    783 		copyin(p->cmap.red, &cc->cc_color[index], count);
    784 		copyin(p->cmap.green, &cc->cc_color[index + 2], count);
    785 		copyin(p->cmap.blue, &cc->cc_color[index + 4], count);
    786 	}
    787 
    788 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
    789 		u_int trailing_bits;
    790 
    791 		copyin(p->image, cc->cc_image, icount);
    792 		copyin(p->mask, cc->cc_mask, icount);
    793 		cc->cc_size = p->size;
    794 
    795 		/* clear trailing bits in the "partial" mask bytes */
    796 		trailing_bits = p->size.x & 0x07;
    797 		if (trailing_bits != 0) {
    798 			const u_int cutmask = ~((~0) << trailing_bits);
    799 			u_char *mp;
    800 			u_int i;
    801 
    802 			mp = cc->cc_mask + iwidth - 1;
    803 			for (i = 0; i < p->size.y; ++i) {
    804 				*mp &= cutmask;
    805 				mp += iwidth;
    806 			}
    807 		}
    808 		igsfb_convert_cursor_data(dc, iwidth, p->size.y);
    809 	}
    810 
    811 	igsfb_update_cursor(dc, v);
    812 	return (0);
    813 }
    814 
    815 
    816 /*
    817  * Convert incoming 1bpp cursor image/mask into native 2bpp format.
    818  */
    819 static void
    820 igsfb_convert_cursor_data(dc, w, h)
    821 	struct igsfb_devconfig *dc;
    822 	u_int w, h;
    823 {
    824 	struct igs_hwcursor *cc = &dc->dc_cursor;
    825 	struct igs_bittab *btab = &dc->dc_bittab;
    826 	u_int8_t *ip, *mp;
    827 	u_int16_t *dp;
    828 	u_int line, i;
    829 
    830 	/* init sprite to be all transparent */
    831 	memset(cc->cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
    832 
    833 	/* first scanline */
    834 	ip = cc->cc_image;
    835 	mp = cc->cc_mask;
    836 	dp = cc->cc_sprite;
    837 
    838 	for (line = 0; line < h; ++line) {
    839 		for (i = 0; i < w; ++i) {
    840 			/* NB: tables are pre-bswapped if needed */
    841 			u_int16_t is = btab->iexpand[ip[i]];
    842 			u_int16_t ms = btab->iexpand[mp[i]]; /* NB: _I_ */
    843 
    844 			ms |= (ms << 1);
    845 			dp[i] = (0xaaaa & ~ms) | (is & ms);
    846 		}
    847 
    848 		/* next scanline */
    849 		ip += w;
    850 		mp += w;
    851 		dp += 8;	/* 2bpp, 8 pixels per short = 8 shorts */
    852 	}
    853 }
    854 
    855 
    856 /*
    857  * Propagate cursor changes to the device.
    858  * "which" is composed of WSDISPLAY_CURSOR_DO* bits.
    859  */
    860 static void
    861 igsfb_update_cursor(dc, which)
    862 	struct igsfb_devconfig *dc;
    863 	u_int which;
    864 {
    865 	bus_space_tag_t iot = dc->dc_iot;
    866 	bus_space_handle_t ioh = dc->dc_ioh;
    867 	u_int8_t curctl;
    868 
    869 	/*
    870 	 * We will need to tweak sprite control register for cursor
    871 	 * visibility and cursor color map manipualtion.
    872 	 */
    873 	if (which & (WSDISPLAY_CURSOR_DOCUR | WSDISPLAY_CURSOR_DOCMAP)) {
    874 		curctl = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
    875 	}
    876 
    877 	if (which & WSDISPLAY_CURSOR_DOSHAPE) {
    878 		u_int8_t *dst = bus_space_vaddr(dc->dc_memt, dc->dc_crh);
    879 
    880 		/*
    881 		 * memcpy between spaces of different endianness would
    882 		 * be ... special.  We cannot be sure if memcpy gonna
    883 		 * push data in 4-byte chunks, we can't pre-bswap it,
    884 		 * so do it byte-by-byte to preserve byte ordering.
    885 		 */
    886 		if (IGSFB_HW_SOFT_BSWAP(dc)) {
    887 			u_int8_t *src = (u_int8_t *)dc->dc_cursor.cc_sprite;
    888 			int i;
    889 
    890 			for (i = 0; i < IGS_CURSOR_DATA_SIZE; ++i)
    891 				*dst++ = *src++;
    892 		} else {
    893 			memcpy(dst, dc->dc_cursor.cc_sprite,
    894 			       IGS_CURSOR_DATA_SIZE);
    895 		}
    896 	}
    897 
    898 	if (which & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
    899 		/* code shared with WSDISPLAYIO_SCURPOS */
    900 		igsfb_update_curpos(dc);
    901 	}
    902 
    903 	if (which & WSDISPLAY_CURSOR_DOCMAP) {
    904 		u_int8_t *p;
    905 
    906 		/* tell DAC we want access to the cursor palette */
    907 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
    908 			      curctl | IGS_EXT_SPRITE_DAC_PEL);
    909 
    910 		p = dc->dc_cursor.cc_color;
    911 
    912 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 0);
    913 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[0]);
    914 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[2]);
    915 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[4]);
    916 
    917 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 1);
    918 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[1]);
    919 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[3]);
    920 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[5]);
    921 
    922 		/* restore access to the normal palette */
    923 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, curctl);
    924 	}
    925 
    926 	if (which & WSDISPLAY_CURSOR_DOCUR) {
    927 		if ((curctl & IGS_EXT_SPRITE_VISIBLE) == 0
    928 		    && dc->dc_curenb)
    929 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
    930 				      curctl | IGS_EXT_SPRITE_VISIBLE);
    931 		else if ((curctl & IGS_EXT_SPRITE_VISIBLE) != 0
    932 			 && !dc->dc_curenb)
    933 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
    934 				      curctl & ~IGS_EXT_SPRITE_VISIBLE);
    935 	}
    936 }
    937 
    938 
    939 /*
    940  * wsdisplay_accessops: alloc_screen()
    941  */
    942 static int
    943 igsfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    944 	void *v;
    945 	const struct wsscreen_descr *type;
    946 	void **cookiep;
    947 	int *curxp, *curyp;
    948 	long *attrp;
    949 {
    950 	struct igsfb_devconfig *dc = v;
    951 	struct rasops_info *ri = &dc->dc_ri;
    952 
    953 	if (dc->dc_nscreens > 0) /* only do single screen for now */
    954 		return (ENOMEM);
    955 
    956 	dc->dc_nscreens = 1;
    957 
    958 	*cookiep = ri;		/* emulcookie for igsgfb_stdscreen.textops */
    959 	*curxp = *curyp = 0;	/* cursor position */
    960 	(*ri->ri_ops.allocattr)(ri,
    961 				WSCOL_BLACK, /* fg */
    962 				WSCOL_BLACK, /* bg */
    963 				0,           /* wsattr */
    964 				attrp);
    965 	return (0);
    966 }
    967 
    968 
    969 /*
    970  * wsdisplay_accessops: free_screen()
    971  */
    972 static void
    973 igsfb_free_screen(v, cookie)
    974 	void *v;
    975 	void *cookie;
    976 {
    977 
    978 	/* XXX */
    979 	return;
    980 }
    981 
    982 
    983 /*
    984  * wsdisplay_accessops: show_screen()
    985  */
    986 static int
    987 igsfb_show_screen(v, cookie, waitok, cb, cbarg)
    988 	void *v;
    989 	void *cookie;
    990 	int waitok;
    991 	void (*cb)(void *, int, int);
    992 	void *cbarg;
    993 {
    994 
    995 	/* XXX */
    996 	return (0);
    997 }
    998