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