genfb.c revision 1.30 1 /* $NetBSD: genfb.c,v 1.30 2010/08/31 02:49:17 macallan 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.30 2010/08/31 02:49:17 macallan 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 #ifdef GENFB_SHADOWFB
197 sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
198 if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
199 memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
200 #endif
201
202 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
203 &genfb_accessops);
204 sc->vd.init_screen = genfb_init_screen;
205
206 /* Do not print anything between this point and the screen
207 * clear operation below. Otherwise it will be lost. */
208
209 ri = &sc->sc_console_screen.scr_ri;
210
211 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
212 &defattr);
213 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
214
215 #ifdef SPLASHSCREEN
216 /*
217 * If system isn't going to go multiuser, or user has requested to see
218 * boot text, don't render splash screen immediately
219 */
220 if (DISABLESPLASH)
221 #endif
222 vcons_redraw_screen(&sc->sc_console_screen);
223
224 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
225 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
226 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
227 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
228 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
229 defattr);
230
231 /* Clear the whole screen to bring it to a known state. */
232 if (sc->sc_want_clear)
233 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
234
235 j = 0;
236 for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
237 #ifndef SPLASHSCREEN
238 sc->sc_cmap_red[i] = rasops_cmap[j];
239 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
240 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
241 genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
242 rasops_cmap[j + 2]);
243 j += 3;
244 #else
245 if(i >= SPLASH_CMAP_OFFSET &&
246 i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
247 sc->sc_cmap_red[i] = _splash_header_data_cmap[j][0];
248 sc->sc_cmap_green[i] = _splash_header_data_cmap[j][1];
249 sc->sc_cmap_blue[i] = _splash_header_data_cmap[j][2];
250 } else {
251 sc->sc_cmap_red[i] = rasops_cmap[j];
252 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
253 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
254 genfb_putpalreg(sc, i, rasops_cmap[j],
255 rasops_cmap[j + 1],
256 rasops_cmap[j + 2]);
257 }
258 j += 3;
259 #endif
260 }
261
262 #ifdef SPLASHSCREEN
263 sc->sc_splash.si_depth = sc->sc_depth;
264 sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_bits;
265 sc->sc_splash.si_hwbits = sc->sc_fbaddr;
266 sc->sc_splash.si_width = sc->sc_width;
267 sc->sc_splash.si_height = sc->sc_height;
268 sc->sc_splash.si_stride = sc->sc_stride;
269 sc->sc_splash.si_fillrect = NULL;
270 if (!DISABLESPLASH)
271 splash_render(&sc->sc_splash, SPLASH_F_CENTER|SPLASH_F_FILL);
272 #ifdef SPLASHSCREEN_PROGRESS
273 sc->sc_progress.sp_top = (sc->sc_height / 8) * 7;
274 sc->sc_progress.sp_width = (sc->sc_width / 4) * 3;
275 sc->sc_progress.sp_left = (sc->sc_width / 8) * 7;
276 sc->sc_progress.sp_height = 20;
277 sc->sc_progress.sp_state = -1;
278 sc->sc_progress.sp_si = &sc->sc_splash;
279 splash_progress_init(&sc->sc_progress);
280 #endif
281 #else
282 vcons_replay_msgbuf(&sc->sc_console_screen);
283 #endif
284
285 if (genfb_softc == NULL)
286 genfb_softc = sc;
287
288 aa.console = console;
289 aa.scrdata = &sc->sc_screenlist;
290 aa.accessops = &genfb_accessops;
291 aa.accesscookie = &sc->vd;
292
293 #ifdef GENFB_DISABLE_TEXT
294 if (!DISABLESPLASH)
295 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
296 #endif
297
298 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
299
300 return 0;
301 }
302
303 static int
304 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
305 struct lwp *l)
306 {
307 struct vcons_data *vd = v;
308 struct genfb_softc *sc = vd->cookie;
309 struct wsdisplay_fbinfo *wdf;
310 struct vcons_screen *ms = vd->active;
311 int new_mode, error;
312
313 switch (cmd) {
314 case WSDISPLAYIO_GINFO:
315 if (ms == NULL)
316 return ENODEV;
317 wdf = (void *)data;
318 wdf->height = ms->scr_ri.ri_height;
319 wdf->width = ms->scr_ri.ri_width;
320 wdf->depth = ms->scr_ri.ri_depth;
321 wdf->cmsize = 256;
322 return 0;
323
324 case WSDISPLAYIO_GETCMAP:
325 return genfb_getcmap(sc,
326 (struct wsdisplay_cmap *)data);
327
328 case WSDISPLAYIO_PUTCMAP:
329 return genfb_putcmap(sc,
330 (struct wsdisplay_cmap *)data);
331
332 case WSDISPLAYIO_LINEBYTES:
333 *(u_int *)data = sc->sc_stride;
334 return 0;
335
336 case WSDISPLAYIO_SMODE:
337 new_mode = *(int *)data;
338
339 /* notify the bus backend */
340 error = 0;
341 if (sc->sc_ops.genfb_ioctl)
342 error = sc->sc_ops.genfb_ioctl(sc, vs,
343 cmd, data, flag, l);
344 if (error)
345 return error;
346
347 if (new_mode != sc->sc_mode) {
348 sc->sc_mode = new_mode;
349 if (new_mode == WSDISPLAYIO_MODE_EMUL) {
350 genfb_restore_palette(sc);
351 vcons_redraw_screen(ms);
352 }
353 }
354 return 0;
355 case WSDISPLAYIO_SSPLASH:
356 #if defined(SPLASHSCREEN)
357 if(*(int *)data == 1) {
358 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
359 splash_render(&sc->sc_splash,
360 SPLASH_F_CENTER|SPLASH_F_FILL);
361 #if defined(SPLASHSCREEN_PROGRESS)
362 sc->sc_progress.sp_running = 1;
363 #endif
364 } else {
365 SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
366 #if defined(SPLASHSCREEN_PROGRESS)
367 sc->sc_progress.sp_running = 0;
368 #endif
369 }
370 vcons_redraw_screen(ms);
371 return 0;
372 #else
373 return ENODEV;
374 #endif
375 case WSDISPLAYIO_SPROGRESS:
376 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
377 sc->sc_progress.sp_force = 1;
378 splash_progress_update(&sc->sc_progress);
379 sc->sc_progress.sp_force = 0;
380 vcons_redraw_screen(ms);
381 return 0;
382 #else
383 return ENODEV;
384 #endif
385 default:
386 if (sc->sc_ops.genfb_ioctl)
387 return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
388 data, flag, l);
389 }
390 return EPASSTHROUGH;
391 }
392
393 static paddr_t
394 genfb_mmap(void *v, void *vs, off_t offset, int prot)
395 {
396 struct vcons_data *vd = v;
397 struct genfb_softc *sc = vd->cookie;
398
399 if (sc->sc_ops.genfb_mmap)
400 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
401
402 return -1;
403 }
404
405 static void
406 genfb_init_screen(void *cookie, struct vcons_screen *scr,
407 int existing, long *defattr)
408 {
409 struct genfb_softc *sc = cookie;
410 struct rasops_info *ri = &scr->scr_ri;
411
412 ri->ri_depth = sc->sc_depth;
413 ri->ri_width = sc->sc_width;
414 ri->ri_height = sc->sc_height;
415 ri->ri_stride = sc->sc_stride;
416 ri->ri_flg = RI_CENTER;
417 if (sc->sc_want_clear)
418 ri->ri_flg |= RI_FULLCLEAR;
419
420 #ifdef GENFB_SHADOWFB
421 if (sc->sc_shadowfb != NULL) {
422
423 ri->ri_hwbits = (char *)sc->sc_fbaddr;
424 ri->ri_bits = (char *)sc->sc_shadowfb;
425 } else
426 #endif
427 {
428 ri->ri_bits = (char *)sc->sc_fbaddr;
429 scr->scr_flags |= VCONS_DONT_READ;
430 }
431
432 if (existing && sc->sc_want_clear) {
433 ri->ri_flg |= RI_CLEAR;
434 }
435
436 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
437 ri->ri_caps = WSSCREEN_WSCOLORS;
438
439 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
440 sc->sc_width / ri->ri_font->fontwidth);
441
442 /* TODO: actually center output */
443 ri->ri_hw = scr;
444
445 #ifdef GENFB_DISABLE_TEXT
446 if (scr == &sc->sc_console_screen && !DISABLESPLASH)
447 SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
448 #endif
449 }
450
451 static int
452 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
453 {
454 u_char *r, *g, *b;
455 u_int index = cm->index;
456 u_int count = cm->count;
457 int i, error;
458 u_char rbuf[256], gbuf[256], bbuf[256];
459
460 #ifdef GENFB_DEBUG
461 aprint_debug("putcmap: %d %d\n",index, count);
462 #endif
463 if (cm->index >= 256 || cm->count > 256 ||
464 (cm->index + cm->count) > 256)
465 return EINVAL;
466 error = copyin(cm->red, &rbuf[index], count);
467 if (error)
468 return error;
469 error = copyin(cm->green, &gbuf[index], count);
470 if (error)
471 return error;
472 error = copyin(cm->blue, &bbuf[index], count);
473 if (error)
474 return error;
475
476 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
477 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
478 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
479
480 r = &sc->sc_cmap_red[index];
481 g = &sc->sc_cmap_green[index];
482 b = &sc->sc_cmap_blue[index];
483
484 for (i = 0; i < count; i++) {
485 genfb_putpalreg(sc, index, *r, *g, *b);
486 index++;
487 r++, g++, b++;
488 }
489 return 0;
490 }
491
492 static int
493 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
494 {
495 u_int index = cm->index;
496 u_int count = cm->count;
497 int error;
498
499 if (index >= 255 || count > 256 || index + count > 256)
500 return EINVAL;
501
502 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
503 if (error)
504 return error;
505 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
506 if (error)
507 return error;
508 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
509 if (error)
510 return error;
511
512 return 0;
513 }
514
515 void
516 genfb_restore_palette(struct genfb_softc *sc)
517 {
518 int i;
519
520 if (sc->sc_depth <= 8) {
521 for (i = 0; i < (1 << sc->sc_depth); i++) {
522 genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
523 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
524 }
525 }
526 }
527
528 static int
529 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
530 uint8_t b)
531 {
532
533 if (sc->sc_cmcb) {
534
535 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
536 idx, r, g, b);
537 }
538 return 0;
539 }
540
541 void
542 genfb_cnattach(void)
543 {
544 genfb_cnattach_called = 1;
545 }
546
547 void
548 genfb_disable(void)
549 {
550 genfb_enabled = 0;
551 }
552
553 int
554 genfb_is_console(void)
555 {
556 return genfb_cnattach_called;
557 }
558
559 int
560 genfb_is_enabled(void)
561 {
562 return genfb_enabled;
563 }
564
565 int
566 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
567 {
568 struct genfb_softc *sc = genfb_softc;
569
570 if (sc && sc->sc_ops.genfb_borrow)
571 return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
572 return 0;
573 }
574