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