genfb.c revision 1.16.4.4 1 /* $NetBSD: genfb.c,v 1.16.4.4 2010/03/11 15:04:10 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.16.4.4 2010/03/11 15:04:10 yamt Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/mutex.h>
38 #include <sys/ioctl.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/kmem.h>
42
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/wscons/wsdisplayvar.h>
45 #include <dev/rasops/rasops.h>
46 #include <dev/wsfont/wsfont.h>
47
48 #include <dev/wscons/wsdisplay_vconsvar.h>
49
50 #include <dev/wsfb/genfbvar.h>
51
52 #ifdef GENFB_DISABLE_TEXT
53 #include <sys/reboot.h>
54 #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \
55 AB_VERBOSE | AB_DEBUG) )
56 #endif
57
58 #include "opt_genfb.h"
59 #include "opt_wsfb.h"
60
61 #ifdef GENFB_DEBUG
62 #define GPRINTF panic
63 #else
64 #define GPRINTF aprint_verbose
65 #endif
66
67 static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
68 static paddr_t genfb_mmap(void *, void *, off_t, int);
69 static void genfb_init_screen(void *, struct vcons_screen *, int, long *);
70
71 static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
72 static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
73 static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
74 uint8_t, uint8_t);
75
76 extern const u_char rasops_cmap[768];
77
78 static int genfb_cnattach_called = 0;
79 static int genfb_enabled = 1;
80
81 struct wsdisplay_accessops genfb_accessops = {
82 genfb_ioctl,
83 genfb_mmap,
84 NULL, /* alloc_screen */
85 NULL, /* free_screen */
86 NULL, /* show_screen */
87 NULL, /* load_font */
88 NULL, /* pollc */
89 NULL /* scroll */
90 };
91
92 static struct genfb_softc *genfb_softc = NULL;
93
94 void
95 genfb_init(struct genfb_softc *sc)
96 {
97 prop_dictionary_t dict;
98 uint64_t cmap_cb, pmf_cb;
99 uint32_t fboffset;
100
101 dict = device_properties(&sc->sc_dev);
102 #ifdef GENFB_DEBUG
103 printf(prop_dictionary_externalize(dict));
104 #endif
105 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
106 GPRINTF("no width property\n");
107 return;
108 }
109 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
110 GPRINTF("no height property\n");
111 return;
112 }
113 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
114 GPRINTF("no depth property\n");
115 return;
116 }
117
118 /* XXX should be a 64bit value */
119 if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
120 GPRINTF("no address property\n");
121 return;
122 }
123
124 sc->sc_fboffset = fboffset;
125
126 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
127 sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
128
129 /*
130 * deal with a bug in the Raptor firmware which always sets
131 * stride = width even when depth != 8
132 */
133 if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
134 sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
135
136 sc->sc_fbsize = sc->sc_height * sc->sc_stride;
137
138 /* optional colour map callback */
139 sc->sc_cmcb = NULL;
140 if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
141 if (cmap_cb != 0)
142 sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
143 }
144 /* optional pmf callback */
145 sc->sc_pmfcb = NULL;
146 if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
147 if (pmf_cb != 0)
148 sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
149 }
150 }
151
152 int
153 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
154 {
155 struct wsemuldisplaydev_attach_args aa;
156 prop_dictionary_t dict;
157 struct rasops_info *ri;
158 uint16_t crow;
159 long defattr;
160 int i, j;
161 bool console;
162
163 dict = device_properties(&sc->sc_dev);
164 prop_dictionary_get_bool(dict, "is_console", &console);
165
166 if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
167 crow = 0;
168 if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
169 == false)
170 sc->sc_want_clear = true;
171
172 /* do not attach when we're not console */
173 if (!console) {
174 aprint_normal_dev(&sc->sc_dev, "no console, unable to continue\n");
175 return -1;
176 }
177
178 aprint_verbose_dev(&sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
179 "stride %d\n",
180 sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
181 sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
182
183 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
184 "default",
185 0, 0,
186 NULL,
187 8, 16,
188 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
189 NULL
190 };
191 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
192 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
193 memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
194 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
195
196 sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
197 if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
198 memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
199
200 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
201 &genfb_accessops);
202 sc->vd.init_screen = genfb_init_screen;
203
204 /* Do not print anything between this point and the screen
205 * clear operation below. Otherwise it will be lost. */
206
207 ri = &sc->sc_console_screen.scr_ri;
208
209 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
210 &defattr);
211 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
212
213 #ifdef SPLASHSCREEN
214 /*
215 * If system isn't going to go multiuser, or user has requested to see
216 * boot text, don't render splash screen immediately
217 */
218 if (DISABLESPLASH)
219 #endif
220 vcons_redraw_screen(&sc->sc_console_screen);
221
222 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
223 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
224 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
225 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
226 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
227 defattr);
228
229 /* Clear the whole screen to bring it to a known state. */
230 if (sc->sc_want_clear)
231 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
232
233 j = 0;
234 for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
235 #ifndef SPLASHSCREEN
236 sc->sc_cmap_red[i] = rasops_cmap[j];
237 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
238 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
239 genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
240 rasops_cmap[j + 2]);
241 j += 3;
242 #else
243 if(i >= SPLASH_CMAP_OFFSET &&
244 i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
245 sc->sc_cmap_red[i] = _splash_header_data_cmap[j][0];
246 sc->sc_cmap_green[i] = _splash_header_data_cmap[j][1];
247 sc->sc_cmap_blue[i] = _splash_header_data_cmap[j][2];
248 } else {
249 sc->sc_cmap_red[i] = rasops_cmap[j];
250 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
251 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
252 genfb_putpalreg(sc, i, rasops_cmap[j],
253 rasops_cmap[j + 1],
254 rasops_cmap[j + 2]);
255 }
256 j += 3;
257 #endif
258 }
259
260 #ifdef SPLASHSCREEN
261 sc->sc_splash.si_depth = sc->sc_depth;
262 sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_bits;
263 sc->sc_splash.si_hwbits = sc->sc_fbaddr;
264 sc->sc_splash.si_width = sc->sc_width;
265 sc->sc_splash.si_height = sc->sc_height;
266 sc->sc_splash.si_stride = sc->sc_stride;
267 sc->sc_splash.si_fillrect = NULL;
268 if (!DISABLESPLASH)
269 splash_render(&sc->sc_splash, SPLASH_F_CENTER|SPLASH_F_FILL);
270 #ifdef SPLASHSCREEN_PROGRESS
271 sc->sc_progress.sp_top = (sc->sc_height / 8) * 7;
272 sc->sc_progress.sp_width = (sc->sc_width / 4) * 3;
273 sc->sc_progress.sp_left = (sc->sc_width / 8) * 7;
274 sc->sc_progress.sp_height = 20;
275 sc->sc_progress.sp_state = -1;
276 sc->sc_progress.sp_si = &sc->sc_splash;
277 splash_progress_init(&sc->sc_progress);
278 #endif
279 #else
280 vcons_replay_msgbuf(&sc->sc_console_screen);
281 #endif
282
283 if (genfb_softc == NULL)
284 genfb_softc = sc;
285
286 aa.console = console;
287 aa.scrdata = &sc->sc_screenlist;
288 aa.accessops = &genfb_accessops;
289 aa.accesscookie = &sc->vd;
290
291 #ifdef GENFB_DISABLE_TEXT
292 if (!DISABLESPLASH)
293 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
294 #endif
295
296 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
297
298 return 0;
299 }
300
301 static int
302 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
303 struct lwp *l)
304 {
305 struct vcons_data *vd = v;
306 struct genfb_softc *sc = vd->cookie;
307 struct wsdisplay_fbinfo *wdf;
308 struct vcons_screen *ms = vd->active;
309 int new_mode, error;
310
311 switch (cmd) {
312 case WSDISPLAYIO_GINFO:
313 if (ms == NULL)
314 return ENODEV;
315 wdf = (void *)data;
316 wdf->height = ms->scr_ri.ri_height;
317 wdf->width = ms->scr_ri.ri_width;
318 wdf->depth = ms->scr_ri.ri_depth;
319 wdf->cmsize = 256;
320 return 0;
321
322 case WSDISPLAYIO_GETCMAP:
323 return genfb_getcmap(sc,
324 (struct wsdisplay_cmap *)data);
325
326 case WSDISPLAYIO_PUTCMAP:
327 return genfb_putcmap(sc,
328 (struct wsdisplay_cmap *)data);
329
330 case WSDISPLAYIO_LINEBYTES:
331 *(u_int *)data = sc->sc_stride;
332 return 0;
333
334 case WSDISPLAYIO_SMODE:
335 new_mode = *(int *)data;
336
337 /* notify the bus backend */
338 error = 0;
339 if (sc->sc_ops.genfb_ioctl)
340 error = sc->sc_ops.genfb_ioctl(sc, vs,
341 cmd, data, flag, l);
342 if (error)
343 return error;
344
345 if (new_mode != sc->sc_mode) {
346 sc->sc_mode = new_mode;
347 if (new_mode == WSDISPLAYIO_MODE_EMUL) {
348 genfb_restore_palette(sc);
349 vcons_redraw_screen(ms);
350 }
351 }
352 return 0;
353 case WSDISPLAYIO_SSPLASH:
354 #if defined(SPLASHSCREEN)
355 if(*(int *)data == 1) {
356 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
357 splash_render(&sc->sc_splash,
358 SPLASH_F_CENTER|SPLASH_F_FILL);
359 #if defined(SPLASHSCREEN_PROGRESS)
360 sc->sc_progress.sp_running = 1;
361 #endif
362 } else {
363 SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
364 #if defined(SPLASHSCREEN_PROGRESS)
365 sc->sc_progress.sp_running = 0;
366 #endif
367 }
368 vcons_redraw_screen(ms);
369 return 0;
370 #else
371 return ENODEV;
372 #endif
373 case WSDISPLAYIO_SPROGRESS:
374 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
375 sc->sc_progress.sp_force = 1;
376 splash_progress_update(&sc->sc_progress);
377 sc->sc_progress.sp_force = 0;
378 vcons_redraw_screen(ms);
379 return 0;
380 #else
381 return ENODEV;
382 #endif
383 default:
384 if (sc->sc_ops.genfb_ioctl)
385 return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
386 data, flag, l);
387 }
388 return EPASSTHROUGH;
389 }
390
391 static paddr_t
392 genfb_mmap(void *v, void *vs, off_t offset, int prot)
393 {
394 struct vcons_data *vd = v;
395 struct genfb_softc *sc = vd->cookie;
396
397 if (sc->sc_ops.genfb_mmap)
398 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
399
400 return -1;
401 }
402
403 static void
404 genfb_init_screen(void *cookie, struct vcons_screen *scr,
405 int existing, long *defattr)
406 {
407 struct genfb_softc *sc = cookie;
408 struct rasops_info *ri = &scr->scr_ri;
409
410 ri->ri_depth = sc->sc_depth;
411 ri->ri_width = sc->sc_width;
412 ri->ri_height = sc->sc_height;
413 ri->ri_stride = sc->sc_stride;
414 ri->ri_flg = RI_CENTER;
415 if (sc->sc_want_clear)
416 ri->ri_flg |= RI_FULLCLEAR;
417
418 if (sc->sc_shadowfb != NULL) {
419
420 ri->ri_hwbits = (char *)sc->sc_fbaddr;
421 ri->ri_bits = (char *)sc->sc_shadowfb;
422 } else
423 ri->ri_bits = (char *)sc->sc_fbaddr;
424
425 if (existing && sc->sc_want_clear) {
426 ri->ri_flg |= RI_CLEAR;
427 }
428
429 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
430 ri->ri_caps = WSSCREEN_WSCOLORS;
431
432 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
433 sc->sc_width / ri->ri_font->fontwidth);
434
435 /* TODO: actually center output */
436 ri->ri_hw = scr;
437
438 #ifdef GENFB_DISABLE_TEXT
439 if (scr == &sc->sc_console_screen && !DISABLESPLASH)
440 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
441 #endif
442 }
443
444 static int
445 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
446 {
447 u_char *r, *g, *b;
448 u_int index = cm->index;
449 u_int count = cm->count;
450 int i, error;
451 u_char rbuf[256], gbuf[256], bbuf[256];
452
453 #ifdef GENFB_DEBUG
454 aprint_debug("putcmap: %d %d\n",index, count);
455 #endif
456 if (cm->index >= 256 || cm->count > 256 ||
457 (cm->index + cm->count) > 256)
458 return EINVAL;
459 error = copyin(cm->red, &rbuf[index], count);
460 if (error)
461 return error;
462 error = copyin(cm->green, &gbuf[index], count);
463 if (error)
464 return error;
465 error = copyin(cm->blue, &bbuf[index], count);
466 if (error)
467 return error;
468
469 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
470 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
471 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
472
473 r = &sc->sc_cmap_red[index];
474 g = &sc->sc_cmap_green[index];
475 b = &sc->sc_cmap_blue[index];
476
477 for (i = 0; i < count; i++) {
478 genfb_putpalreg(sc, index, *r, *g, *b);
479 index++;
480 r++, g++, b++;
481 }
482 return 0;
483 }
484
485 static int
486 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
487 {
488 u_int index = cm->index;
489 u_int count = cm->count;
490 int error;
491
492 if (index >= 255 || count > 256 || index + count > 256)
493 return EINVAL;
494
495 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
496 if (error)
497 return error;
498 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
499 if (error)
500 return error;
501 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
502 if (error)
503 return error;
504
505 return 0;
506 }
507
508 void
509 genfb_restore_palette(struct genfb_softc *sc)
510 {
511 int i;
512
513 if (sc->sc_depth <= 8) {
514 for (i = 0; i < (1 << sc->sc_depth); i++) {
515 genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
516 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
517 }
518 }
519 }
520
521 static int
522 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
523 uint8_t b)
524 {
525
526 if (sc->sc_cmcb) {
527
528 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
529 idx, r, g, b);
530 }
531 return 0;
532 }
533
534 void
535 genfb_cnattach(void)
536 {
537 genfb_cnattach_called = 1;
538 }
539
540 void
541 genfb_disable(void)
542 {
543 genfb_enabled = 0;
544 }
545
546 int
547 genfb_is_console(void)
548 {
549 return genfb_cnattach_called;
550 }
551
552 int
553 genfb_is_enabled(void)
554 {
555 return genfb_enabled;
556 }
557
558 int
559 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
560 {
561 struct genfb_softc *sc = genfb_softc;
562
563 if (sc && sc->sc_ops.genfb_borrow)
564 return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
565 return 0;
566 }
567