xref: /netbsd-src/sys/dev/ic/igsfb.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: igsfb.c,v 1.56 2017/01/25 17:31:55 jakllsch 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.56 2017/01/25 17:31:55 jakllsch 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 
43 #include <sys/bus.h>
44 
45 #include <dev/wscons/wsdisplayvar.h>
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wsfont/wsfont.h>
48 #include <dev/rasops/rasops.h>
49 
50 #include <dev/wscons/wsdisplay_vconsvar.h>
51 
52 #include <dev/ic/igsfbreg.h>
53 #include <dev/ic/igsfbvar.h>
54 
55 
56 struct igsfb_devconfig igsfb_console_dc = {
57 	.dc_mmap = NULL,
58 	.dc_modestring = "",
59 };
60 
61 /*
62  * wsscreen
63  */
64 
65 /* filled from rasops_info in igsfb_init_wsdisplay */
66 static struct wsscreen_descr igsfb_stdscreen = {
67 	.name = "std",
68 };
69 
70 static const struct wsscreen_descr *_igsfb_scrlist[] = {
71 	&igsfb_stdscreen,
72 };
73 
74 static const struct wsscreen_list igsfb_screenlist = {
75 	.nscreens = sizeof(_igsfb_scrlist) / sizeof(_igsfb_scrlist[0]),
76 	.screens = _igsfb_scrlist,
77 };
78 
79 
80 /*
81  * wsdisplay_accessops
82  */
83 
84 static int	igsfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
85 static paddr_t	igsfb_mmap(void *, void *, off_t, int);
86 
87 static struct wsdisplay_accessops igsfb_accessops = {
88 	.ioctl = igsfb_ioctl,
89 	.mmap = igsfb_mmap,
90 };
91 
92 
93 /*
94  * acceleration
95  */
96 static int	igsfb_make_text_cursor(struct igsfb_devconfig *,
97 				       struct vcons_screen *);
98 static void	igsfb_accel_cursor(void *, int, int, int);
99 
100 static int	igsfb_accel_wait(struct igsfb_devconfig *);
101 static void	igsfb_accel_fill(struct igsfb_devconfig *,
102 				 uint32_t, uint32_t, uint16_t, uint16_t);
103 static void	igsfb_accel_copy(struct igsfb_devconfig *,
104 				 uint32_t, uint32_t, uint16_t, uint16_t);
105 
106 static void	igsfb_accel_copycols(void *, int, int, int, int);
107 static void	igsfb_accel_erasecols(void *, int, int, int, long);
108 static void	igsfb_accel_copyrows(void *, int, int, int);
109 static void	igsfb_accel_eraserows(void *, int, int, long);
110 static void	igsfb_accel_putchar(void *, int, int, u_int, long);
111 
112 
113 /*
114  * internal functions
115  */
116 static int	igsfb_init_video(struct igsfb_devconfig *);
117 static void	igsfb_init_cmap(struct igsfb_devconfig *);
118 static uint16_t	igsfb_spread_bits_8(uint8_t);
119 static void	igsfb_init_bit_table(struct igsfb_devconfig *);
120 static void	igsfb_init_wsdisplay(void *, struct vcons_screen *, int,
121 				     long *);
122 
123 
124 static void	igsfb_blank_screen(struct igsfb_devconfig *, int);
125 static int	igsfb_get_cmap(struct igsfb_devconfig *,
126 			       struct wsdisplay_cmap *);
127 static int	igsfb_set_cmap(struct igsfb_devconfig *,
128 			       const struct wsdisplay_cmap *);
129 static void	igsfb_update_cmap(struct igsfb_devconfig *, u_int, u_int);
130 static void	igsfb_set_curpos(struct igsfb_devconfig *,
131 				 const struct wsdisplay_curpos *);
132 static void	igsfb_update_curpos(struct igsfb_devconfig *);
133 static int	igsfb_get_cursor(struct igsfb_devconfig *,
134 				 struct wsdisplay_cursor *);
135 static int	igsfb_set_cursor(struct igsfb_devconfig *,
136 				 const struct wsdisplay_cursor *);
137 static void	igsfb_update_cursor(struct igsfb_devconfig *, u_int);
138 static void	igsfb_convert_cursor_data(struct igsfb_devconfig *,
139 					  u_int, u_int);
140 
141 
142 int
143 igsfb_cnattach_subr(struct igsfb_devconfig *dc)
144 {
145 	struct rasops_info *ri;
146 	long defattr;
147 
148 	KASSERT(dc == &igsfb_console_dc);
149 
150 	igsfb_init_video(dc);
151 	dc->dc_vd.active = NULL;
152 	igsfb_init_wsdisplay(dc, &dc->dc_console, 1, &defattr);
153 
154 	ri = &dc->dc_console.scr_ri;
155 	ri->ri_hw = &dc->dc_console;
156 	dc->dc_console.scr_cookie = dc;
157 
158 	(*ri->ri_ops.allocattr)(ri,
159 				WS_DEFAULT_FG, /* fg */
160 				WS_DEFAULT_BG, /* bg */
161 				0,           /* wsattrs */
162 				&defattr);
163 
164 	wsdisplay_cnattach(&igsfb_stdscreen,
165 			   ri,   /* emulcookie */
166 			   0, 0, /* cursor position */
167 			   defattr);
168 	return 0;
169 }
170 
171 
172 /*
173  * Finish off the attach.  Bus specific attach method should have
174  * enabled io and memory accesses and mapped io (and cop?) registers.
175  */
176 void
177 igsfb_attach_subr(struct igsfb_softc *sc, int isconsole)
178 {
179 	struct igsfb_devconfig *dc = sc->sc_dc;
180 	struct wsemuldisplaydev_attach_args waa;
181 	struct rasops_info *ri;
182 	long defattr;
183 
184 	KASSERT(dc != NULL);
185 
186 	if (!isconsole) {
187 		igsfb_init_video(dc);
188 	}
189 
190 	vcons_init(&dc->dc_vd, dc, &igsfb_stdscreen, &igsfb_accessops);
191 	dc->dc_vd.init_screen = igsfb_init_wsdisplay;
192 
193 	vcons_init_screen(&dc->dc_vd, &dc->dc_console, 1, &defattr);
194 	dc->dc_console.scr_flags |= VCONS_SCREEN_IS_STATIC;
195 
196 	aprint_normal("%s: %dMB, %s%dx%d, %dbpp\n",
197 	       device_xname(sc->sc_dev),
198 	       (uint32_t)(dc->dc_vmemsz >> 20),
199 	       (dc->dc_hwflags & IGSFB_HW_BSWAP)
200 		   ? (dc->dc_hwflags & IGSFB_HW_BE_SELECT)
201 		       ? "hardware bswap, " : "software bswap, "
202 		   : "",
203 	       dc->dc_width, dc->dc_height, dc->dc_depth);
204 	aprint_normal("%s: using %dbpp for X\n", device_xname(sc->sc_dev),
205 	       dc->dc_maxdepth);
206 	ri = &dc->dc_console.scr_ri;
207 	ri->ri_ops.eraserows(ri, 0, ri->ri_rows, defattr);
208 
209 	if (isconsole)
210 		vcons_replay_msgbuf(&dc->dc_console);
211 
212 	/* attach wsdisplay */
213 	waa.console = isconsole;
214 	waa.scrdata = &igsfb_screenlist;
215 	waa.accessops = &igsfb_accessops;
216 	waa.accesscookie = &dc->dc_vd;
217 
218 	config_found(sc->sc_dev, &waa, wsemuldisplaydevprint);
219 }
220 
221 
222 static int
223 igsfb_init_video(struct igsfb_devconfig *dc)
224 {
225 	bus_space_handle_t tmph;
226 	uint8_t *p;
227 	int need_bswap;
228 	bus_addr_t fbaddr, craddr;
229 	off_t croffset;
230 	uint8_t busctl, curctl;
231 	void *va;
232 
233 	/* Total amount of video memory. */
234 	busctl = igs_ext_read(dc->dc_iot, dc->dc_ioh, IGS_EXT_BUS_CTL);
235 	if (busctl & 0x2)
236 		dc->dc_vmemsz = 4;
237 	else if (busctl & 0x1)
238 		dc->dc_vmemsz = 2;
239 	else
240 		dc->dc_vmemsz = 1;
241 	dc->dc_vmemsz <<= 20;	/* megabytes -> bytes */
242 
243 	/*
244 	 * Check for endianness mismatch by writing a word at the end of
245 	 * the video memory (off-screen) and reading it back byte-by-byte.
246 	 */
247 	if (bus_space_map(dc->dc_memt,
248 			  dc->dc_memaddr + dc->dc_vmemsz - sizeof(uint32_t),
249 			  sizeof(uint32_t),
250 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
251 			  &tmph) != 0)
252 	{
253 		printf("unable to map video memory for endianness test\n");
254 		return 1;
255 	}
256 
257 	p = bus_space_vaddr(dc->dc_memt, tmph);
258 #if BYTE_ORDER == BIG_ENDIAN
259 	*((uint32_t *)p) = 0x12345678;
260 #else
261 	*((uint32_t *)p) = 0x78563412;
262 #endif
263 	if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
264 		need_bswap = 0;
265 	else
266 		need_bswap = 1;
267 
268 	bus_space_unmap(dc->dc_memt, tmph, sizeof(uint32_t));
269 
270 	/*
271 	 * On CyberPro we can use magic bswap bit in linear address.
272 	 */
273 	fbaddr = dc->dc_memaddr;
274 	if (need_bswap) {
275 		dc->dc_hwflags |= IGSFB_HW_BSWAP;
276 		if (dc->dc_id >= 0x2000) {
277 			dc->dc_hwflags |= IGSFB_HW_BE_SELECT;
278 			fbaddr |= IGS_MEM_BE_SELECT;
279 		}
280 	}
281 
282 	/*
283 	 * Map graphic coprocessor for mode setting and accelerated rasops.
284 	 */
285 	if (dc->dc_id >= 0x2000) { /* XXX */
286 		if (bus_space_map(dc->dc_iot,
287 				  dc->dc_iobase + IGS_COP_BASE_B, IGS_COP_SIZE,
288 				  dc->dc_ioflags,
289 				  &dc->dc_coph) != 0)
290 		{
291 			printf("unable to map COP registers\n");
292 			return 1;
293 		}
294 	}
295 
296 	igsfb_hw_setup(dc);
297 
298 	/*
299 	 * Don't map in all N megs, just the amount we need for the wsscreen.
300 	 */
301 	dc->dc_fbsz = dc->dc_stride * dc->dc_height;
302 	if (bus_space_map(dc->dc_memt, fbaddr, dc->dc_fbsz,
303 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
304 			  &dc->dc_fbh) != 0)
305 	{
306 		if (dc->dc_id >= 0x2000) { /* XXX */
307 			bus_space_unmap(dc->dc_iot, dc->dc_coph, IGS_COP_SIZE);
308 		}
309 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
310 		printf("unable to map framebuffer\n");
311 		return 1;
312 	}
313 
314 	igsfb_init_cmap(dc);
315 
316 	/*
317 	 * 1KB for cursor sprite data at the very end of the video memory.
318 	 */
319 	croffset = dc->dc_vmemsz - IGS_CURSOR_DATA_SIZE;
320 	craddr = fbaddr + croffset;
321 	if (bus_space_map(dc->dc_memt, craddr, IGS_CURSOR_DATA_SIZE,
322 			  dc->dc_memflags | BUS_SPACE_MAP_LINEAR,
323 			  &dc->dc_crh) != 0)
324 	{
325 		if (dc->dc_id >= 0x2000) { /* XXX */
326 			bus_space_unmap(dc->dc_iot, dc->dc_coph, IGS_COP_SIZE);
327 		}
328 		bus_space_unmap(dc->dc_iot, dc->dc_ioh, IGS_REG_SIZE);
329 		bus_space_unmap(dc->dc_memt, dc->dc_fbh, dc->dc_fbsz);
330 		printf("unable to map cursor sprite region\n");
331 		return 1;
332 	}
333 
334 	/*
335 	 * Tell the device where cursor sprite data are located in the
336 	 * linear space (it takes data offset in 1KB units).
337 	 */
338 	croffset >>= 10;	/* bytes -> kilobytes */
339 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
340 		      IGS_EXT_SPRITE_DATA_LO, croffset & 0xff);
341 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
342 		      IGS_EXT_SPRITE_DATA_HI, (croffset >> 8) & 0xf);
343 
344 	/* init the bit expansion table for cursor sprite data conversion */
345 	igsfb_init_bit_table(dc);
346 
347 	/* XXX: fill dc_cursor and use igsfb_update_cursor() instead? */
348 	memset(&dc->dc_cursor, 0, sizeof(struct igs_hwcursor));
349 	va = bus_space_vaddr(dc->dc_memt, dc->dc_crh);
350 	memset(va, /* transparent */ 0xaa, IGS_CURSOR_DATA_SIZE);
351 
352 	curctl = igs_ext_read(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL);
353 	curctl |= IGS_EXT_SPRITE_64x64;
354 	curctl &= ~IGS_EXT_SPRITE_VISIBLE;
355 	igs_ext_write(dc->dc_iot, dc->dc_ioh, IGS_EXT_SPRITE_CTL, curctl);
356 	dc->dc_curenb = 0;
357 
358 	/*
359 	 * Init graphic coprocessor for accelerated rasops.
360 	 */
361 	if (dc->dc_id >= 0x2000) { /* XXX */
362 		bus_space_write_2(dc->dc_iot, dc->dc_coph,
363 				  IGS_COP_SRC_MAP_WIDTH_REG,
364 				  dc->dc_width - 1);
365 		bus_space_write_2(dc->dc_iot, dc->dc_coph,
366 				  IGS_COP_DST_MAP_WIDTH_REG,
367 				  dc->dc_width - 1);
368 
369 		bus_space_write_1(dc->dc_iot, dc->dc_coph,
370 				  IGS_COP_MAP_FMT_REG,
371 				  howmany(dc->dc_depth, NBBY) - 1);
372 	}
373 
374 	/* make sure screen is not blanked */
375 	dc->dc_blanked = 0;
376 	igsfb_blank_screen(dc, dc->dc_blanked);
377 
378 	return 0;
379 }
380 
381 
382 static void
383 igsfb_init_cmap(struct igsfb_devconfig *dc)
384 {
385 	bus_space_tag_t iot = dc->dc_iot;
386 	bus_space_handle_t ioh = dc->dc_ioh;
387 	const uint8_t *p;
388 	int i;
389 
390 	p = rasops_cmap;	/* "ANSI" color map */
391 
392 	/* init software copy */
393 	for (i = 0; i < IGS_CMAP_SIZE; ++i, p += 3) {
394 		dc->dc_cmap.r[i] = p[0];
395 		dc->dc_cmap.g[i] = p[1];
396 		dc->dc_cmap.b[i] = p[2];
397 	}
398 
399 	/* propagate to the device */
400 	igsfb_update_cmap(dc, 0, IGS_CMAP_SIZE);
401 
402 	/* set overscan color */
403 	p = &rasops_cmap[WSDISPLAY_BORDER_COLOR * 3];
404 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_RED,   p[0]);
405 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_GREEN, p[1]);
406 	igs_ext_write(iot, ioh, IGS_EXT_OVERSCAN_BLUE,  p[2]);
407 }
408 
409 
410 static void
411 igsfb_init_wsdisplay(void *cookie, struct vcons_screen *scr, int existing,
412 		     long *defattr)
413 {
414 	struct igsfb_devconfig *dc = cookie;
415 	struct rasops_info *ri = &scr->scr_ri;
416 	int wsfcookie;
417 
418 	if (scr == &dc->dc_console) {
419 		if (ri->ri_flg == 0) {
420 			/* first time, need to set RI_NO_AUTO */
421 			ri->ri_flg |= RI_NO_AUTO;
422 		} else {
423 			/* clear it on 2nd run */
424 			ri->ri_flg &= ~RI_NO_AUTO;
425 		}
426 	}
427 	ri->ri_flg |= RI_CENTER | RI_FULLCLEAR;
428 
429 	if (IGSFB_HW_SOFT_BSWAP(dc))
430 		ri->ri_flg |= RI_BSWAP;
431 
432 	ri->ri_depth = dc->dc_depth;
433 	ri->ri_width = dc->dc_width;
434 	ri->ri_height = dc->dc_height;
435 	ri->ri_stride = dc->dc_stride;
436 	ri->ri_bits = bus_space_vaddr(dc->dc_memt, dc->dc_fbh);
437 
438 	/*
439 	 * Initialize wsfont related stuff.
440 	 */
441 	wsfont_init();
442 
443 	/* prefer gallant that is identical to the one the prom uses */
444 	wsfcookie = wsfont_find("Gallant", 12, 22, 0,
445 				WSDISPLAY_FONTORDER_L2R,
446 				WSDISPLAY_FONTORDER_L2R, WSFONT_FIND_BITMAP);
447 	if (wsfcookie <= 0) {
448 #ifdef DIAGNOSTIC
449 		printf("unable to find font Gallant 12x22\n");
450 #endif
451 		wsfcookie = wsfont_find(NULL, 0, 0, 0, /* any font at all? */
452 					WSDISPLAY_FONTORDER_L2R,
453 					WSDISPLAY_FONTORDER_L2R,
454 					WSFONT_FIND_BITMAP);
455 	}
456 
457 	if (wsfcookie <= 0) {
458 		printf("unable to find any fonts\n");
459 		return;
460 	}
461 
462 	if (wsfont_lock(wsfcookie, &ri->ri_font) != 0) {
463 		printf("unable to lock font\n");
464 		return;
465 	}
466 	ri->ri_wsfcookie = wsfcookie;
467 
468 
469 	/* XXX: TODO: compute term size based on font dimensions? */
470 	rasops_init(ri, 0, 0);
471 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
472 	    ri->ri_width / ri->ri_font->fontwidth);
473 
474 
475 	/* use the sprite for the text mode cursor */
476 	igsfb_make_text_cursor(dc, scr);
477 
478 	/* the cursor is "busy" while we are in the text mode */
479 	dc->dc_hwflags |= IGSFB_HW_TEXT_CURSOR;
480 
481 	/* propagate sprite data to the device */
482 	igsfb_update_cursor(dc, WSDISPLAY_CURSOR_DOSHAPE);
483 
484 	/* accelerated text cursor */
485 	ri->ri_ops.cursor = igsfb_accel_cursor;
486 
487 	if (dc->dc_id >= 0x2000) { /* XXX */
488 		/* accelerated erase/copy */
489 		ri->ri_ops.copycols = igsfb_accel_copycols;
490 		ri->ri_ops.erasecols = igsfb_accel_erasecols;
491 		ri->ri_ops.copyrows = igsfb_accel_copyrows;
492 		ri->ri_ops.eraserows = igsfb_accel_eraserows;
493 
494 		/* putchar hook to sync with the cop */
495 		dc->dc_ri_putchar = ri->ri_ops.putchar;
496 		ri->ri_ops.putchar = igsfb_accel_putchar;
497 	}
498 
499 	igsfb_stdscreen.nrows = ri->ri_rows;
500 	igsfb_stdscreen.ncols = ri->ri_cols;
501 	igsfb_stdscreen.textops = &ri->ri_ops;
502 	igsfb_stdscreen.capabilities = ri->ri_caps;
503 }
504 
505 
506 /*
507  * Init cursor data in dc_cursor for the accelerated text cursor.
508  */
509 static int
510 igsfb_make_text_cursor(struct igsfb_devconfig *dc, struct vcons_screen *scr)
511 {
512 	struct rasops_info *ri = &scr->scr_ri;
513 	struct wsdisplay_font *f = ri->ri_font;
514 	uint16_t cc_scan[8];	/* one sprite scanline */
515 	uint16_t s;
516 	int w, i;
517 
518 	KASSERT(f->fontwidth <= IGS_CURSOR_MAX_SIZE);
519 	KASSERT(f->fontheight <= IGS_CURSOR_MAX_SIZE);
520 
521 	w = f->fontwidth;
522 	for (i = 0; i < f->stride; ++i) {
523 		if (w >= 8) {
524 			s = 0xffff; /* all inverted */
525 			w -= 8;
526 		} else {
527 			/* first w pixels inverted, the rest is transparent */
528 			s = ~(0x5555 << (w * 2));
529 			s = htole16(s);
530 			w = 0;
531 		}
532 		cc_scan[i] = s;
533 	}
534 
535 	dc->dc_cursor.cc_size.x = f->fontwidth;
536 	dc->dc_cursor.cc_size.y = f->fontheight;
537 
538 	dc->dc_cursor.cc_hot.x = 0;
539 	dc->dc_cursor.cc_hot.y = 0;
540 
541 	/* init sprite array to be all transparent */
542 	memset(dc->dc_cursor.cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
543 
544 	for (i = 0; i < f->fontheight; ++i)
545 		memcpy(&dc->dc_cursor.cc_sprite[i * 8],
546 		       cc_scan, f->stride * sizeof(uint16_t));
547 
548 	return 0;
549 }
550 
551 
552 /*
553  * Spread a byte (abcd.efgh) into two (0a0b.0c0d 0e0f.0g0h).
554  * Helper function for igsfb_init_bit_table().
555  */
556 static uint16_t
557 igsfb_spread_bits_8(uint8_t b)
558 {
559 	uint16_t s = b;
560 
561 	s = ((s & 0x00f0) << 4) | (s & 0x000f);
562 	s = ((s & 0x0c0c) << 2) | (s & 0x0303);
563 	s = ((s & 0x2222) << 1) | (s & 0x1111);
564 	return s;
565 }
566 
567 
568 /*
569  * Cursor sprite data are in 2bpp.  Incoming image/mask are in 1bpp.
570  * Prebuild the table to expand 1bpp->2bpp, with bswapping if necessary.
571  */
572 static void
573 igsfb_init_bit_table(struct igsfb_devconfig *dc)
574 {
575 	uint16_t *expand = dc->dc_bexpand;
576 	uint16_t s;
577 	u_int i;
578 
579 	for (i = 0; i < 256; ++i) {
580 		s = igsfb_spread_bits_8(i);
581 		expand[i] = htole16(s);
582 	}
583 }
584 
585 
586 /*
587  * wsdisplay_accessops: mmap()
588  *   XXX: security considerations for allowing mmapping i/o mapped i/o regs?
589  */
590 static paddr_t
591 igsfb_mmap(void *v, void *vs, off_t offset, int prot)
592 {
593 	struct vcons_data *vd = v;
594 	struct igsfb_devconfig *dc = vd->cookie;
595 
596 	if (offset < dc->dc_memsz && offset >= 0)
597 		return bus_space_mmap(dc->dc_memt, dc->dc_memaddr, offset,
598 		    prot, dc->dc_memflags | BUS_SPACE_MAP_LINEAR);
599 	if (dc->dc_mmap)
600 		return dc->dc_mmap(v, vs, offset, prot);
601 	return -1;
602 }
603 
604 
605 /*
606  * wsdisplay_accessops: ioctl()
607  */
608 static int
609 igsfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
610 	struct lwp *l)
611 {
612 	struct vcons_data *vd = v;
613 	struct igsfb_devconfig *dc = vd->cookie;
614 	int cursor_busy;
615 	int turnoff;
616 
617 	/* don't permit cursor ioctls if we use sprite for text cursor */
618 	cursor_busy = !dc->dc_mapped
619 		&& (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR);
620 
621 	switch (cmd) {
622 
623 	case WSDISPLAYIO_GTYPE:
624 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
625 		return 0;
626 
627 	case WSDISPLAYIO_GINFO:
628 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
629 		wsd_fbip->height = dc->dc_height;
630 		wsd_fbip->width = dc->dc_width;
631 		wsd_fbip->depth = dc->dc_maxdepth;
632 		wsd_fbip->cmsize = IGS_CMAP_SIZE;
633 #undef wsd_fbip
634 		return 0;
635 
636 	case WSDISPLAYIO_LINEBYTES:
637 		*(int *)data = dc->dc_width * howmany(dc->dc_maxdepth, NBBY);
638 		return 0;
639 
640 	case WSDISPLAYIO_SMODE:
641 #define d (*(int *)data)
642 		if (d != WSDISPLAYIO_MODE_EMUL) {
643 			dc->dc_mapped = 1;
644 			/* turn off hardware cursor */
645 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
646 				dc->dc_curenb = 0;
647 				igsfb_update_cursor(dc,
648 					WSDISPLAY_CURSOR_DOCUR);
649 			}
650 			if ((dc->dc_mode != NULL) && (dc->dc_maxdepth != 8))
651 				igsfb_set_mode(dc, dc->dc_mode,
652 				    dc->dc_maxdepth);
653 		} else {
654 			dc->dc_mapped = 0;
655 			if ((dc->dc_mode != NULL) && (dc->dc_maxdepth != 8))
656 				igsfb_set_mode(dc, dc->dc_mode, 8);
657 			igsfb_init_cmap(dc);
658 			/* reinit sprite for text cursor */
659 			if (dc->dc_hwflags & IGSFB_HW_TEXT_CURSOR) {
660 				igsfb_make_text_cursor(dc, dc->dc_vd.active);
661 				dc->dc_curenb = 0;
662 				igsfb_update_cursor(dc,
663 					  WSDISPLAY_CURSOR_DOSHAPE
664 					| WSDISPLAY_CURSOR_DOCUR);
665 			}
666 			vcons_redraw_screen(vd->active);
667 		}
668 		return 0;
669 
670 	case WSDISPLAYIO_GVIDEO:
671 		*(u_int *)data = dc->dc_blanked ?
672 		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
673 		return 0;
674 
675 	case WSDISPLAYIO_SVIDEO:
676 		turnoff = (*(u_int *)data == WSDISPLAYIO_VIDEO_OFF);
677 		if (dc->dc_blanked != turnoff) {
678 			dc->dc_blanked = turnoff;
679 			igsfb_blank_screen(dc, dc->dc_blanked);
680 		}
681 		return 0;
682 
683 	case WSDISPLAYIO_GETCMAP:
684 		return igsfb_get_cmap(dc, (struct wsdisplay_cmap *)data);
685 
686 	case WSDISPLAYIO_PUTCMAP:
687 		return igsfb_set_cmap(dc, (struct wsdisplay_cmap *)data);
688 
689 	case WSDISPLAYIO_GCURMAX:
690 		((struct wsdisplay_curpos *)data)->x = IGS_CURSOR_MAX_SIZE;
691 		((struct wsdisplay_curpos *)data)->y = IGS_CURSOR_MAX_SIZE;
692 		return 0;
693 
694 	case WSDISPLAYIO_GCURPOS:
695 		if (cursor_busy)
696 			return EBUSY;
697 		*(struct wsdisplay_curpos *)data = dc->dc_cursor.cc_pos;
698 		return 0;
699 
700 	case WSDISPLAYIO_SCURPOS:
701 		if (cursor_busy)
702 			return EBUSY;
703 		igsfb_set_curpos(dc, (struct wsdisplay_curpos *)data);
704 		return 0;
705 
706 	case WSDISPLAYIO_GCURSOR:
707 		if (cursor_busy)
708 			return EBUSY;
709 		return igsfb_get_cursor(dc, (struct wsdisplay_cursor *)data);
710 
711 	case WSDISPLAYIO_SCURSOR:
712 		if (cursor_busy)
713 			return EBUSY;
714 		return igsfb_set_cursor(dc, (struct wsdisplay_cursor *)data);
715 	}
716 
717 	return EPASSTHROUGH;
718 }
719 
720 
721 /*
722  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SVIDEO)
723  */
724 static void
725 igsfb_blank_screen(struct igsfb_devconfig *dc, int blank)
726 {
727 
728 	igs_ext_write(dc->dc_iot, dc->dc_ioh,
729 		      IGS_EXT_SYNC_CTL,
730 		      blank ? IGS_EXT_SYNC_H0 | IGS_EXT_SYNC_V0
731 			    : 0);
732 }
733 
734 
735 /*
736  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GETCMAP)
737  *   Served from the software cmap copy.
738  */
739 static int
740 igsfb_get_cmap(struct igsfb_devconfig *dc, struct wsdisplay_cmap *p)
741 {
742 	u_int index, count;
743 	int err;
744 
745 	index = p->index;
746 	count = p->count;
747 
748 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
749 		return EINVAL;
750 
751 	err = copyout(&dc->dc_cmap.r[index], p->red, count);
752 	if (err)
753 		return err;
754 	err = copyout(&dc->dc_cmap.g[index], p->green, count);
755 	if (err)
756 		return err;
757 	err = copyout(&dc->dc_cmap.b[index], p->blue, count);
758 	if (err)
759 		return err;
760 
761 	return 0;
762 }
763 
764 
765 /*
766  * wsdisplay_accessops: ioctl(WSDISPLAYIO_PUTCMAP)
767  *   Set the software cmap copy and propagate changed range to the device.
768  */
769 static int
770 igsfb_set_cmap(struct igsfb_devconfig *dc, const struct wsdisplay_cmap *p)
771 {
772 	u_int index, count;
773 	uint8_t r[IGS_CMAP_SIZE];
774 	uint8_t g[IGS_CMAP_SIZE];
775 	uint8_t b[IGS_CMAP_SIZE];
776 	int error;
777 
778 	index = p->index;
779 	count = p->count;
780 	if (index >= IGS_CMAP_SIZE || count > IGS_CMAP_SIZE - index)
781 		return EINVAL;
782 	error = copyin(p->red, &r[index], count);
783 	if (error)
784 		return error;
785 	error = copyin(p->green, &g[index], count);
786 	if (error)
787 		return error;
788 	error = copyin(p->blue, &b[index], count);
789 	if (error)
790 		return error;
791 
792 	memcpy(&dc->dc_cmap.r[index], &r[index], count);
793 	memcpy(&dc->dc_cmap.g[index], &g[index], count);
794 	memcpy(&dc->dc_cmap.b[index], &b[index], count);
795 
796 	/* propagate changes to the device */
797 	igsfb_update_cmap(dc, index, count);
798 	return 0;
799 }
800 
801 
802 /*
803  * Propagate specified part of the software cmap copy to the device.
804  */
805 static void
806 igsfb_update_cmap(struct igsfb_devconfig *dc, u_int index, u_int count)
807 {
808 	bus_space_tag_t t;
809 	bus_space_handle_t h;
810 	u_int last, i;
811 
812 	if (index >= IGS_CMAP_SIZE)
813 		return;
814 
815 	last = index + count;
816 	if (last > IGS_CMAP_SIZE)
817 		last = IGS_CMAP_SIZE;
818 
819 	t = dc->dc_iot;
820 	h = dc->dc_ioh;
821 
822 	/* start palette writing, index is autoincremented by hardware */
823 	bus_space_write_1(t, h, IGS_DAC_PEL_WRITE_IDX, index);
824 
825 	for (i = index; i < last; ++i) {
826 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.r[i]);
827 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.g[i]);
828 		bus_space_write_1(t, h, IGS_DAC_PEL_DATA, dc->dc_cmap.b[i]);
829 	}
830 }
831 
832 
833 /*
834  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURPOS)
835  */
836 static void
837 igsfb_set_curpos(struct igsfb_devconfig *dc,
838 		 const struct wsdisplay_curpos *curpos)
839 {
840 	struct rasops_info *ri = &dc->dc_vd.active->scr_ri;
841 	u_int x = curpos->x, y = curpos->y;
842 
843 	if (x >= ri->ri_width)
844 		x = ri->ri_width - 1;
845 	if (y >= ri->ri_height)
846 		y = ri->ri_height - 1;
847 
848 	dc->dc_cursor.cc_pos.x = x;
849 	dc->dc_cursor.cc_pos.y = y;
850 
851 	/* propagate changes to the device */
852 	igsfb_update_curpos(dc);
853 }
854 
855 
856 static void
857 igsfb_update_curpos(struct igsfb_devconfig *dc)
858 {
859 	bus_space_tag_t t;
860 	bus_space_handle_t h;
861 	int x, xoff, y, yoff;
862 
863 	xoff = 0;
864 	x = dc->dc_cursor.cc_pos.x - dc->dc_cursor.cc_hot.x;
865 	if (x < 0) {
866 		xoff = -x;
867 		x = 0;
868 	}
869 
870 	yoff = 0;
871 	y = dc->dc_cursor.cc_pos.y - dc->dc_cursor.cc_hot.y;
872 	if (y < 0) {
873 		yoff = -y;
874 		y = 0;
875 	}
876 
877 	t = dc->dc_iot;
878 	h = dc->dc_ioh;
879 
880 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_LO, x & 0xff);
881 	igs_ext_write(t, h, IGS_EXT_SPRITE_HSTART_HI, (x >> 8) & 0x07);
882 	igs_ext_write(t, h, IGS_EXT_SPRITE_HPRESET, xoff & 0x3f);
883 
884 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_LO, y & 0xff);
885 	igs_ext_write(t, h, IGS_EXT_SPRITE_VSTART_HI, (y >> 8) & 0x07);
886 	igs_ext_write(t, h, IGS_EXT_SPRITE_VPRESET, yoff & 0x3f);
887 }
888 
889 
890 /*
891  * wsdisplay_accessops: ioctl(WSDISPLAYIO_GCURSOR)
892  */
893 static int
894 igsfb_get_cursor(struct igsfb_devconfig *dc, struct wsdisplay_cursor *p)
895 {
896 
897 	/* XXX: TODO */
898 	return 0;
899 }
900 
901 
902 /*
903  * wsdisplay_accessops: ioctl(WSDISPLAYIO_SCURSOR)
904  */
905 static int
906 igsfb_set_cursor(struct igsfb_devconfig *dc, const struct wsdisplay_cursor *p)
907 {
908 	struct igs_hwcursor *cc;
909 	struct wsdisplay_curpos pos, hot;
910 	u_int v, index, count, icount, iwidth;
911 	uint8_t r[2], g[2], b[2], image[512], mask[512];
912 	int error;
913 
914 	cc = &dc->dc_cursor;
915 	v = p->which;
916 	index = count = icount = iwidth = 0;	/* XXX: gcc */
917 	pos.x = pos.y = 0;			/* XXX: gcc */
918 
919 	/* copy in the new cursor colormap */
920 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
921 		index = p->cmap.index;
922 		count = p->cmap.count;
923 		if (index >= 2 || (index + count) > 2)
924 			return EINVAL;
925 		error = copyin(p->cmap.red, &r[index], count);
926 		if (error)
927 			return error;
928 		error = copyin(p->cmap.green, &g[index], count);
929 		if (error)
930 			return error;
931 		error = copyin(p->cmap.blue, &b[index], count);
932 		if (error)
933 			return error;
934 	}
935 
936 	/* verify that the new cursor data are valid */
937 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
938 		if (p->size.x > IGS_CURSOR_MAX_SIZE
939 		    || p->size.y > IGS_CURSOR_MAX_SIZE)
940 			return EINVAL;
941 
942 		iwidth = (p->size.x + 7) >> 3; /* bytes per scan line */
943 		icount = iwidth * p->size.y;
944 		error = copyin(p->image, image, icount);
945 		if (error)
946 			return error;
947 		error = copyin(p->mask, mask, icount);
948 		if (error)
949 			return error;
950 	}
951 
952 	/* enforce that the position is within screen bounds */
953 	if (v & WSDISPLAY_CURSOR_DOPOS) {
954 		struct rasops_info *ri = &dc->dc_vd.active->scr_ri;
955 
956 		pos = p->pos;	/* local copy we can write to */
957 		if (pos.x >= ri->ri_width)
958 			pos.x = ri->ri_width - 1;
959 		if (pos.y >= ri->ri_height)
960 			pos.y = ri->ri_height - 1;
961 	}
962 
963 	/* enforce that the hot spot is within sprite bounds */
964 	if (v & WSDISPLAY_CURSOR_DOHOT)
965 		hot = p->hot;	/* local copy we can write to */
966 
967 	if (v & (WSDISPLAY_CURSOR_DOHOT | WSDISPLAY_CURSOR_DOSHAPE)) {
968 		const struct wsdisplay_curpos *nsize;
969 		struct wsdisplay_curpos *nhot;
970 
971 		nsize = (v & WSDISPLAY_CURSOR_DOSHAPE) ?
972 			    &p->size : &cc->cc_size;
973 		nhot = (v & WSDISPLAY_CURSOR_DOHOT) ? &hot : &cc->cc_hot;
974 
975 		if (nhot->x >= nsize->x)
976 			nhot->x = nsize->x - 1;
977 		if (nhot->y >= nsize->y)
978 			nhot->y = nsize->y - 1;
979 	}
980 
981 	/* copy data to the driver's cursor info */
982 	if (v & WSDISPLAY_CURSOR_DOCUR)
983 		dc->dc_curenb = p->enable;
984 	if (v & WSDISPLAY_CURSOR_DOPOS)
985 		cc->cc_pos = pos; /* local copy, possibly corrected */
986 	if (v & WSDISPLAY_CURSOR_DOHOT)
987 		cc->cc_hot = hot; /* local copy, possibly corrected */
988 	if (v & WSDISPLAY_CURSOR_DOCMAP) {
989 		memcpy(&cc->cc_color[index], &r[index], count);
990 		memcpy(&cc->cc_color[index + 2], &g[index], count);
991 		memcpy(&cc->cc_color[index + 4], &b[index], count);
992 	}
993 	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
994 		u_int trailing_bits;
995 
996 		memcpy(cc->cc_image, image, icount);
997 		memcpy(cc->cc_mask, mask, icount);
998 		cc->cc_size = p->size;
999 
1000 		/* clear trailing bits in the "partial" mask bytes */
1001 		trailing_bits = p->size.x & 0x07;
1002 		if (trailing_bits != 0) {
1003 			const u_int cutmask = ~((~0) << trailing_bits);
1004 			u_char *mp;
1005 			u_int i;
1006 
1007 			mp = cc->cc_mask + iwidth - 1;
1008 			for (i = 0; i < p->size.y; ++i) {
1009 				*mp &= cutmask;
1010 				mp += iwidth;
1011 			}
1012 		}
1013 		igsfb_convert_cursor_data(dc, iwidth, p->size.y);
1014 	}
1015 
1016 	/* propagate changes to the device */
1017 	igsfb_update_cursor(dc, v);
1018 
1019 	return 0;
1020 }
1021 
1022 
1023 /*
1024  * Convert incoming 1bpp cursor image/mask into native 2bpp format.
1025  */
1026 static void
1027 igsfb_convert_cursor_data(struct igsfb_devconfig *dc,
1028 			  u_int width, u_int height)
1029 {
1030 	struct igs_hwcursor *cc = &dc->dc_cursor;
1031 	uint16_t *expand = dc->dc_bexpand;
1032 	uint8_t *ip, *mp;
1033 	uint16_t is, ms, *dp;
1034 	u_int line, i;
1035 
1036 	/* init sprite to be all transparent */
1037 	memset(cc->cc_sprite, 0xaa, IGS_CURSOR_DATA_SIZE);
1038 
1039 	/* first scanline */
1040 	ip = cc->cc_image;
1041 	mp = cc->cc_mask;
1042 	dp = cc->cc_sprite;
1043 
1044 	for (line = 0; line < height; ++line) {
1045 		for (i = 0; i < width; ++i) {
1046 			is = expand[ip[i]]; /* image: 0 -> 00, 1 -> 01 */
1047 			ms = expand[mp[i]]; /*  mask: 0 -> 00, 1 -> 11 */
1048 			ms |= (ms << 1);
1049 			dp[i] = (0xaaaa & ~ms) | (is & ms);
1050 		}
1051 
1052 		/* next scanline */
1053 		ip += width;
1054 		mp += width;
1055 		dp += 8; /* 64 pixels, 2bpp, 8 pixels per short = 8 shorts */
1056 	}
1057 }
1058 
1059 
1060 /*
1061  * Propagate cursor changes to the device.
1062  * "which" is composed of WSDISPLAY_CURSOR_DO* bits.
1063  */
1064 static void
1065 igsfb_update_cursor(struct igsfb_devconfig *dc, u_int which)
1066 {
1067 	bus_space_tag_t iot = dc->dc_iot;
1068 	bus_space_handle_t ioh = dc->dc_ioh;
1069 	uint8_t curctl;
1070 
1071 	curctl = 0;		/* XXX: gcc */
1072 
1073 	/*
1074 	 * We will need to tweak sprite control register for cursor
1075 	 * visibility and cursor color map manipulation.
1076 	 */
1077 	if (which & (WSDISPLAY_CURSOR_DOCUR | WSDISPLAY_CURSOR_DOCMAP)) {
1078 		curctl = igs_ext_read(iot, ioh, IGS_EXT_SPRITE_CTL);
1079 	}
1080 
1081 	if (which & WSDISPLAY_CURSOR_DOSHAPE) {
1082 		uint8_t *dst = bus_space_vaddr(dc->dc_memt, dc->dc_crh);
1083 
1084 		/*
1085 		 * memcpy between spaces of different endianness would
1086 		 * be ... special.  We cannot be sure if memcpy gonna
1087 		 * push data in 4-byte chunks, we can't pre-bswap it,
1088 		 * so do it byte-by-byte to preserve byte ordering.
1089 		 */
1090 		if (IGSFB_HW_SOFT_BSWAP(dc)) {
1091 			uint8_t *src = (uint8_t *)dc->dc_cursor.cc_sprite;
1092 			int i;
1093 
1094 			for (i = 0; i < IGS_CURSOR_DATA_SIZE; ++i)
1095 				*dst++ = *src++;
1096 		} else {
1097 			memcpy(dst, dc->dc_cursor.cc_sprite,
1098 			       IGS_CURSOR_DATA_SIZE);
1099 		}
1100 	}
1101 
1102 	if (which & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
1103 		/* code shared with WSDISPLAYIO_SCURPOS */
1104 		igsfb_update_curpos(dc);
1105 	}
1106 
1107 	if (which & WSDISPLAY_CURSOR_DOCMAP) {
1108 		uint8_t *p;
1109 
1110 		/* tell DAC we want access to the cursor palette */
1111 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
1112 			      curctl | IGS_EXT_SPRITE_DAC_PEL);
1113 
1114 		p = dc->dc_cursor.cc_color;
1115 
1116 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 0);
1117 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[0]);
1118 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[2]);
1119 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[4]);
1120 
1121 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_WRITE_IDX, 1);
1122 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[1]);
1123 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[3]);
1124 		bus_space_write_1(iot, ioh, IGS_DAC_PEL_DATA, p[5]);
1125 
1126 		/* restore access to the normal palette */
1127 		igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL, curctl);
1128 	}
1129 
1130 	if (which & WSDISPLAY_CURSOR_DOCUR) {
1131 		if ((curctl & IGS_EXT_SPRITE_VISIBLE) == 0
1132 		    && dc->dc_curenb)
1133 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
1134 				      curctl | IGS_EXT_SPRITE_VISIBLE);
1135 		else if ((curctl & IGS_EXT_SPRITE_VISIBLE) != 0
1136 			 && !dc->dc_curenb)
1137 			igs_ext_write(iot, ioh, IGS_EXT_SPRITE_CTL,
1138 				      curctl & ~IGS_EXT_SPRITE_VISIBLE);
1139 	}
1140 }
1141 
1142 
1143 /*
1144  * Accelerated text mode cursor that uses hardware sprite.
1145  */
1146 static void
1147 igsfb_accel_cursor(void *cookie, int on, int row, int col)
1148 {
1149 	struct rasops_info *ri = (struct rasops_info *)cookie;
1150 	struct vcons_screen *scr = ri->ri_hw;
1151 	struct igsfb_devconfig *dc = scr->scr_cookie;
1152 	struct igs_hwcursor *cc = &dc->dc_cursor;
1153 	u_int which;
1154 
1155 	ri->ri_crow = row;
1156 	ri->ri_ccol = col;
1157 
1158 	which = 0;
1159 	if (on) {
1160 		ri->ri_flg |= RI_CURSOR;
1161 
1162 		/* only bother to move the cursor if it's visible */
1163 		cc->cc_pos.x = ri->ri_xorigin
1164 			+ ri->ri_ccol * ri->ri_font->fontwidth;
1165 		cc->cc_pos.y = ri->ri_yorigin
1166 			+ ri->ri_crow * ri->ri_font->fontheight;
1167 		which |= WSDISPLAY_CURSOR_DOPOS;
1168 	} else
1169 		ri->ri_flg &= ~RI_CURSOR;
1170 
1171 	if (dc->dc_curenb != on) {
1172 		dc->dc_curenb = on;
1173 		which |= WSDISPLAY_CURSOR_DOCUR;
1174 	}
1175 
1176 	/* propagate changes to the device */
1177 	igsfb_update_cursor(dc, which);
1178 }
1179 
1180 
1181 
1182 /*
1183  * Accelerated raster ops that use graphic coprocessor.
1184  */
1185 
1186 static int
1187 igsfb_accel_wait(struct igsfb_devconfig *dc)
1188 {
1189 	bus_space_tag_t t = dc->dc_iot;
1190 	bus_space_handle_t h = dc->dc_coph;
1191 	int timo = 100000;
1192 	uint8_t reg;
1193 
1194 	bus_space_write_1(t, h, IGS_COP_MAP_FMT_REG,
1195 	    howmany(dc->dc_depth, NBBY) - 1);
1196 	while (timo--) {
1197 		reg = bus_space_read_1(t, h, IGS_COP_CTL_REG);
1198 		if ((reg & IGS_COP_CTL_BUSY) == 0)
1199 			return 0;
1200 	}
1201 
1202 	return 1;
1203 }
1204 
1205 
1206 static void
1207 igsfb_accel_copy(struct igsfb_devconfig *dc, uint32_t src, uint32_t dst,
1208 		 uint16_t width, uint16_t height)
1209 {
1210 	bus_space_tag_t t = dc->dc_iot;
1211 	bus_space_handle_t h = dc->dc_coph;
1212 	uint32_t toend;
1213 	uint8_t drawcmd;
1214 
1215 	drawcmd = IGS_COP_DRAW_ALL;
1216 	if (dst > src) {
1217 		toend = dc->dc_vd.active->scr_ri.ri_width * (height - 1) + (width - 1);
1218 		src += toend;
1219 		dst += toend;
1220 		drawcmd |= IGS_COP_OCTANT_X_NEG | IGS_COP_OCTANT_Y_NEG;
1221 	}
1222 
1223 	igsfb_accel_wait(dc);
1224 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
1225 
1226 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
1227 
1228 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
1229 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
1230 
1231 	bus_space_write_4(t, h, IGS_COP_SRC_START_REG, src);
1232 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
1233 
1234 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, drawcmd);
1235 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
1236 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
1237 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG,
1238 			  IGS_COP_OP_PXBLT | IGS_COP_OP_FG_FROM_SRC);
1239 }
1240 
1241 
1242 static void
1243 igsfb_accel_fill(struct igsfb_devconfig *dc, uint32_t color, uint32_t dst,
1244 		 uint16_t width, uint16_t height)
1245 {
1246 	bus_space_tag_t t = dc->dc_iot;
1247 	bus_space_handle_t h = dc->dc_coph;
1248 
1249 	igsfb_accel_wait(dc);
1250 	bus_space_write_1(t, h, IGS_COP_CTL_REG, 0);
1251 
1252 	bus_space_write_1(t, h, IGS_COP_FG_MIX_REG, IGS_COP_MIX_S);
1253 
1254 	bus_space_write_2(t, h, IGS_COP_WIDTH_REG, width - 1);
1255 	bus_space_write_2(t, h, IGS_COP_HEIGHT_REG, height - 1);
1256 
1257 	bus_space_write_4(t, h, IGS_COP_DST_START_REG, dst);
1258 	bus_space_write_4(t, h, IGS_COP_FG_REG, color);
1259 
1260 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_0_REG, IGS_COP_DRAW_ALL);
1261 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_1_REG, IGS_COP_PPM_FIXED_FG);
1262 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_2_REG, 0);
1263 	bus_space_write_1(t, h, IGS_COP_PIXEL_OP_3_REG, IGS_COP_OP_PXBLT);
1264 }
1265 
1266 
1267 static void
1268 igsfb_accel_copyrows(void *cookie, int src, int dst, int num)
1269 {
1270 	struct rasops_info *ri = (struct rasops_info *)cookie;
1271 	struct vcons_screen *scr = ri->ri_hw;
1272 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
1273 	uint32_t srp, dsp;
1274 	uint16_t width, height;
1275 
1276 	width = ri->ri_emuwidth;
1277 	height = num * ri->ri_font->fontheight;
1278 
1279 	srp = ri->ri_xorigin
1280 		+ ri->ri_width * (ri->ri_yorigin
1281 				  + src * ri->ri_font->fontheight);
1282 	dsp = ri->ri_xorigin
1283 		+ ri->ri_width * (ri->ri_yorigin
1284 				  + dst * ri->ri_font->fontheight);
1285 
1286 	igsfb_accel_copy(dc, srp, dsp, width, height);
1287 }
1288 
1289 
1290 static void
1291 igsfb_accel_copycols(void *cookie, int row, int src, int dst, int num)
1292 {
1293 	struct rasops_info *ri = (struct rasops_info *)cookie;
1294 	struct vcons_screen *scr = ri->ri_hw;
1295 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
1296 	uint32_t rowp, srp, dsp;
1297 	uint16_t width, height;
1298 
1299 	width = num * ri->ri_font->fontwidth;
1300 	height = ri->ri_font->fontheight;
1301 
1302 	rowp = ri->ri_xorigin
1303 		+ ri->ri_width * (ri->ri_yorigin
1304 				  + row * ri->ri_font->fontheight);
1305 
1306 	srp = rowp + src * ri->ri_font->fontwidth;
1307 	dsp = rowp + dst * ri->ri_font->fontwidth;
1308 
1309 	igsfb_accel_copy(dc, srp, dsp, width, height);
1310 }
1311 
1312 
1313 static void
1314 igsfb_accel_eraserows(void *cookie, int row, int num, long attr)
1315 {
1316 	struct rasops_info *ri = (struct rasops_info *)cookie;
1317 	struct vcons_screen *scr = ri->ri_hw;
1318 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
1319 	uint32_t color;
1320 	uint32_t dsp;
1321 	uint16_t width, height;
1322 
1323 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
1324 		width = ri->ri_width;
1325 		height = ri->ri_height;
1326 		dsp = 0;
1327 	} else {
1328 		width = ri->ri_emuwidth;
1329 		height = num * ri->ri_font->fontheight;
1330 
1331 		dsp = ri->ri_xorigin
1332 			+ ri->ri_width * (ri->ri_yorigin
1333 					  + row * ri->ri_font->fontheight);
1334 	}
1335 
1336 	/* XXX: we "know" the encoding that rasops' allocattr uses */
1337 	color = (attr >> 16) & 0xff;
1338 
1339 	igsfb_accel_fill(dc, color, dsp, width, height);
1340 }
1341 
1342 
1343 static void
1344 igsfb_accel_erasecols(void *cookie, int row, int col, int num, long attr)
1345 {
1346 	struct rasops_info *ri = (struct rasops_info *)cookie;
1347 	struct vcons_screen *scr = ri->ri_hw;
1348 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
1349 	uint32_t color;
1350 	uint32_t rowp, dsp;
1351 	uint16_t width, height;
1352 
1353 	width = num * ri->ri_font->fontwidth;
1354 	height = ri->ri_font->fontheight;
1355 
1356 	rowp = ri->ri_xorigin
1357 		+ ri->ri_width * (ri->ri_yorigin
1358 				  + row * ri->ri_font->fontheight);
1359 
1360 	dsp = rowp + col * ri->ri_font->fontwidth;
1361 
1362 	/* XXX: we "know" the encoding that rasops' allocattr uses */
1363 	color = (attr >> 16) & 0xff;
1364 
1365 	igsfb_accel_fill(dc, color, dsp, width, height);
1366 }
1367 
1368 
1369 /*
1370  * Not really implemented here, but we need to hook into the one
1371  * supplied by rasops so that we can synchronize with the COP.
1372  */
1373 static void
1374 igsfb_accel_putchar(void *cookie, int row, int col, u_int uc, long attr)
1375 {
1376 	struct rasops_info *ri = (struct rasops_info *)cookie;
1377 	struct vcons_screen *scr = ri->ri_hw;
1378 	struct igsfb_devconfig *dc = (struct igsfb_devconfig *)scr->scr_cookie;
1379 
1380 	igsfb_accel_wait(dc);
1381 	(*dc->dc_ri_putchar)(cookie, row, col, uc, attr);
1382 }
1383