source: mainline/kernel/genarch/src/fb/fb.c@ 86ffa27f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 86ffa27f was b366a6f4, checked in by Martin Decky <martin@…>, 14 years ago

automatic kernel console lockout

  • kernel automatically relinquishes the access to the kernel console when the uspace maps the respective physical memory area
  • kernel output before uspace initialization is currently broken on Ski (no physical memory area), but this is pending further unification
  • kernel console devices are now independent (there is no system-wide "silent" variable), thus on multiple devices the kernel console and uspace output might be usable at the same time
  • Property mode set to 100644
File size: 16.8 KB
Line 
1/*
2 * Copyright (c) 2008 Martin Decky
3 * Copyright (c) 2006 Ondrej Palkovsky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - 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 * - 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/** @addtogroup genarch
31 * @{
32 */
33/** @file
34 */
35
36#include <genarch/fb/font-8x16.h>
37#include <genarch/fb/logo-196x66.h>
38#include <genarch/fb/visuals.h>
39#include <genarch/fb/fb.h>
40#include <console/chardev.h>
41#include <console/console.h>
42#include <sysinfo/sysinfo.h>
43#include <mm/page.h>
44#include <mm/slab.h>
45#include <align.h>
46#include <panic.h>
47#include <memstr.h>
48#include <config.h>
49#include <bitops.h>
50#include <print.h>
51#include <str.h>
52#include <ddi/ddi.h>
53#include <typedefs.h>
54#include <byteorder.h>
55
56#define BG_COLOR 0x000080
57#define FG_COLOR 0xffff00
58#define INV_COLOR 0xaaaaaa
59
60#define RED(x, bits) (((x) >> (8 + 8 + 8 - (bits))) & ((1 << (bits)) - 1))
61#define GREEN(x, bits) (((x) >> (8 + 8 - (bits))) & ((1 << (bits)) - 1))
62#define BLUE(x, bits) (((x) >> (8 - (bits))) & ((1 << (bits)) - 1))
63
64#define COL2X(col) ((col) * FONT_WIDTH)
65#define ROW2Y(row) ((row) * FONT_SCANLINES)
66
67#define X2COL(x) ((x) / FONT_WIDTH)
68#define Y2ROW(y) ((y) / FONT_SCANLINES)
69
70#define FB_POS(instance, x, y) \
71 ((y) * (instance)->scanline + (x) * (instance)->pixelbytes)
72
73#define BB_POS(instance, col, row) \
74 ((row) * (instance)->cols + (col))
75
76#define GLYPH_POS(instance, glyph, y) \
77 ((glyph) * (instance)->glyphbytes + (y) * (instance)->glyphscanline)
78
79typedef void (* rgb_conv_t)(void *, uint32_t);
80
81typedef struct {
82 SPINLOCK_DECLARE(lock);
83
84 parea_t parea;
85
86 uint8_t *addr;
87 uint16_t *backbuf;
88 uint8_t *glyphs;
89 uint8_t *bgscan;
90
91 rgb_conv_t rgb_conv;
92
93 unsigned int xres;
94 unsigned int yres;
95
96 unsigned int ylogo;
97 unsigned int ytrim;
98 unsigned int rowtrim;
99
100 unsigned int scanline;
101 unsigned int glyphscanline;
102
103 unsigned int pixelbytes;
104 unsigned int glyphbytes;
105 unsigned int bgscanbytes;
106
107 unsigned int cols;
108 unsigned int rows;
109
110 unsigned int position;
111} fb_instance_t;
112
113static void fb_putchar(outdev_t *dev, wchar_t ch);
114static void fb_redraw_internal(fb_instance_t *instance);
115static void fb_redraw(outdev_t *dev);
116
117static outdev_operations_t fbdev_ops = {
118 .write = fb_putchar,
119 .redraw = fb_redraw
120};
121
122/*
123 * RGB conversion functions.
124 *
125 * These functions write an RGB value to some memory in some predefined format.
126 * The naming convention corresponds to the format created by these functions.
127 * The functions use the so called network order (i.e. big endian) with respect
128 * to their names.
129 */
130
131static void rgb_0888(void *dst, uint32_t rgb)
132{
133 *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
134 (RED(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (BLUE(rgb, 8)));
135}
136
137static void bgr_0888(void *dst, uint32_t rgb)
138{
139 *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
140 (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (RED(rgb, 8)));
141}
142
143static void rgb_8880(void *dst, uint32_t rgb)
144{
145 *((uint32_t *) dst) = host2uint32_t_be((RED(rgb, 8) << 24) |
146 (GREEN(rgb, 8) << 16) | (BLUE(rgb, 8) << 8) | 0);
147}
148
149static void bgr_8880(void *dst, uint32_t rgb)
150{
151 *((uint32_t *) dst) = host2uint32_t_be((BLUE(rgb, 8) << 24) |
152 (GREEN(rgb, 8) << 16) | (RED(rgb, 8) << 8) | 0);
153}
154
155static void rgb_888(void *dst, uint32_t rgb)
156{
157 ((uint8_t *) dst)[0] = RED(rgb, 8);
158 ((uint8_t *) dst)[1] = GREEN(rgb, 8);
159 ((uint8_t *) dst)[2] = BLUE(rgb, 8);
160}
161
162static void bgr_888(void *dst, uint32_t rgb)
163{
164 ((uint8_t *) dst)[0] = BLUE(rgb, 8);
165 ((uint8_t *) dst)[1] = GREEN(rgb, 8);
166 ((uint8_t *) dst)[2] = RED(rgb, 8);
167}
168
169static void rgb_555_be(void *dst, uint32_t rgb)
170{
171 *((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 10 |
172 GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
173}
174
175static void rgb_555_le(void *dst, uint32_t rgb)
176{
177 *((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 10 |
178 GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
179}
180
181static void rgb_565_be(void *dst, uint32_t rgb)
182{
183 *((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 11 |
184 GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
185}
186
187static void rgb_565_le(void *dst, uint32_t rgb)
188{
189 *((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 11 |
190 GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
191}
192
193/** BGR 3:2:3
194 *
195 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
196 * will most likely use a color palette. The color appearance
197 * will be pretty random and depend on the default installed
198 * palette. This could be fixed by supporting custom palette
199 * and setting it to simulate the 8-bit truecolor.
200 *
201 * Currently we set the palette on the ia32, amd64, ppc32 and sparc64 port.
202 *
203 * Note that the byte is being inverted by this function. The reason is
204 * that we would like to use a color palette where the white color code
205 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
206 * use these codes for black and white and prevent to set codes
207 * 0 and 255 to other colors.
208 *
209 */
210static void bgr_323(void *dst, uint32_t rgb)
211{
212 *((uint8_t *) dst)
213 = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
214}
215
216/** Hide logo and refresh screen
217 *
218 */
219static void logo_hide(fb_instance_t *instance)
220{
221 instance->ylogo = 0;
222 instance->ytrim = instance->yres;
223 instance->rowtrim = instance->rows;
224
225 if ((!instance->parea.mapped) || (console_override))
226 fb_redraw_internal(instance);
227}
228
229/** Draw character at given position
230 *
231 */
232static void glyph_draw(fb_instance_t *instance, uint16_t glyph,
233 unsigned int col, unsigned int row, bool overlay)
234{
235 unsigned int x = COL2X(col);
236 unsigned int y = ROW2Y(row);
237 unsigned int yd;
238
239 if (y >= instance->ytrim)
240 logo_hide(instance);
241
242 if (!overlay)
243 instance->backbuf[BB_POS(instance, col, row)] = glyph;
244
245 if ((!instance->parea.mapped) || (console_override)) {
246 for (yd = 0; yd < FONT_SCANLINES; yd++)
247 memcpy(&instance->addr[FB_POS(instance, x, y + yd + instance->ylogo)],
248 &instance->glyphs[GLYPH_POS(instance, glyph, yd)],
249 instance->glyphscanline);
250 }
251}
252
253/** Scroll screen down by one row
254 *
255 *
256 */
257static void screen_scroll(fb_instance_t *instance)
258{
259 if (instance->ylogo > 0) {
260 logo_hide(instance);
261 return;
262 }
263
264 if ((!instance->parea.mapped) || (console_override)) {
265 unsigned int row;
266
267 for (row = 0; row < instance->rows; row++) {
268 unsigned int y = ROW2Y(row);
269 unsigned int yd;
270
271 for (yd = 0; yd < FONT_SCANLINES; yd++) {
272 unsigned int x;
273 unsigned int col;
274
275 for (col = 0, x = 0; col < instance->cols;
276 col++, x += FONT_WIDTH) {
277 uint16_t glyph;
278
279 if (row < instance->rows - 1) {
280 if (instance->backbuf[BB_POS(instance, col, row)] ==
281 instance->backbuf[BB_POS(instance, col, row + 1)])
282 continue;
283
284 glyph = instance->backbuf[BB_POS(instance, col, row + 1)];
285 } else
286 glyph = 0;
287
288 memcpy(&instance->addr[FB_POS(instance, x, y + yd)],
289 &instance->glyphs[GLYPH_POS(instance, glyph, yd)],
290 instance->glyphscanline);
291 }
292 }
293 }
294 }
295
296 memmove(instance->backbuf, &instance->backbuf[BB_POS(instance, 0, 1)],
297 instance->cols * (instance->rows - 1) * sizeof(uint16_t));
298 memsetw(&instance->backbuf[BB_POS(instance, 0, instance->rows - 1)],
299 instance->cols, 0);
300}
301
302static void cursor_put(fb_instance_t *instance)
303{
304 unsigned int col = instance->position % instance->cols;
305 unsigned int row = instance->position / instance->cols;
306
307 glyph_draw(instance, fb_font_glyph(U_CURSOR), col, row, true);
308}
309
310static void cursor_remove(fb_instance_t *instance)
311{
312 unsigned int col = instance->position % instance->cols;
313 unsigned int row = instance->position / instance->cols;
314
315 glyph_draw(instance, instance->backbuf[BB_POS(instance, col, row)],
316 col, row, true);
317}
318
319/** Render glyphs
320 *
321 * Convert glyphs from device independent font
322 * description to current visual representation.
323 *
324 */
325static void glyphs_render(fb_instance_t *instance)
326{
327 /* Prerender glyphs */
328 uint16_t glyph;
329
330 for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
331 uint32_t fg_color;
332
333 if (glyph == FONT_GLYPHS - 1)
334 fg_color = INV_COLOR;
335 else
336 fg_color = FG_COLOR;
337
338 unsigned int y;
339
340 for (y = 0; y < FONT_SCANLINES; y++) {
341 unsigned int x;
342
343 for (x = 0; x < FONT_WIDTH; x++) {
344 void *dst =
345 &instance->glyphs[GLYPH_POS(instance, glyph, y) +
346 x * instance->pixelbytes];
347 uint32_t rgb = (fb_font[glyph][y] &
348 (1 << (7 - x))) ? fg_color : BG_COLOR;
349 instance->rgb_conv(dst, rgb);
350 }
351 }
352 }
353
354 /* Prerender background scanline */
355 unsigned int x;
356
357 for (x = 0; x < instance->xres; x++)
358 instance->rgb_conv(&instance->bgscan[x * instance->pixelbytes], BG_COLOR);
359}
360
361/** Print character to screen
362 *
363 * Emulate basic terminal commands.
364 *
365 */
366static void fb_putchar(outdev_t *dev, wchar_t ch)
367{
368 fb_instance_t *instance = (fb_instance_t *) dev->data;
369 spinlock_lock(&instance->lock);
370
371 switch (ch) {
372 case '\n':
373 cursor_remove(instance);
374 instance->position += instance->cols;
375 instance->position -= instance->position % instance->cols;
376 break;
377 case '\r':
378 cursor_remove(instance);
379 instance->position -= instance->position % instance->cols;
380 break;
381 case '\b':
382 cursor_remove(instance);
383 if (instance->position % instance->cols)
384 instance->position--;
385 break;
386 case '\t':
387 cursor_remove(instance);
388 do {
389 glyph_draw(instance, fb_font_glyph(' '),
390 instance->position % instance->cols,
391 instance->position / instance->cols, false);
392 instance->position++;
393 } while ((instance->position % 8)
394 && (instance->position < instance->cols * instance->rows));
395 break;
396 default:
397 glyph_draw(instance, fb_font_glyph(ch),
398 instance->position % instance->cols,
399 instance->position / instance->cols, false);
400 instance->position++;
401 }
402
403 if (instance->position >= instance->cols * instance->rows) {
404 instance->position -= instance->cols;
405 screen_scroll(instance);
406 }
407
408 cursor_put(instance);
409
410 spinlock_unlock(&instance->lock);
411}
412
413static void fb_redraw_internal(fb_instance_t *instance)
414{
415 if (instance->ylogo > 0) {
416 unsigned int y;
417
418 for (y = 0; y < LOGO_HEIGHT; y++) {
419 unsigned int x;
420
421 for (x = 0; x < instance->xres; x++)
422 instance->rgb_conv(&instance->addr[FB_POS(instance, x, y)],
423 (x < LOGO_WIDTH) ?
424 fb_logo[y * LOGO_WIDTH + x] :
425 LOGO_COLOR);
426 }
427 }
428
429 unsigned int row;
430
431 for (row = 0; row < instance->rowtrim; row++) {
432 unsigned int y = instance->ylogo + ROW2Y(row);
433 unsigned int yd;
434
435 for (yd = 0; yd < FONT_SCANLINES; yd++) {
436 unsigned int x;
437 unsigned int col;
438
439 for (col = 0, x = 0; col < instance->cols;
440 col++, x += FONT_WIDTH) {
441 uint16_t glyph =
442 instance->backbuf[BB_POS(instance, col, row)];
443 void *dst = &instance->addr[FB_POS(instance, x, y + yd)];
444 void *src = &instance->glyphs[GLYPH_POS(instance, glyph, yd)];
445 memcpy(dst, src, instance->glyphscanline);
446 }
447 }
448 }
449
450 if (COL2X(instance->cols) < instance->xres) {
451 unsigned int y;
452 unsigned int size =
453 (instance->xres - COL2X(instance->cols)) * instance->pixelbytes;
454
455 for (y = instance->ylogo; y < instance->yres; y++)
456 memcpy(&instance->addr[FB_POS(instance, COL2X(instance->cols), y)],
457 instance->bgscan, size);
458 }
459
460 if (ROW2Y(instance->rowtrim) + instance->ylogo < instance->yres) {
461 unsigned int y;
462
463 for (y = ROW2Y(instance->rowtrim) + instance->ylogo;
464 y < instance->yres; y++)
465 memcpy(&instance->addr[FB_POS(instance, 0, y)],
466 instance->bgscan, instance->bgscanbytes);
467 }
468}
469
470/** Refresh the screen
471 *
472 */
473static void fb_redraw(outdev_t *dev)
474{
475 fb_instance_t *instance = (fb_instance_t *) dev->data;
476
477 spinlock_lock(&instance->lock);
478 fb_redraw_internal(instance);
479 spinlock_unlock(&instance->lock);
480}
481
482/** Initialize framebuffer as a output character device
483 *
484 */
485outdev_t *fb_init(fb_properties_t *props)
486{
487 ASSERT(props);
488 ASSERT(props->x > 0);
489 ASSERT(props->y > 0);
490 ASSERT(props->scan > 0);
491
492 rgb_conv_t rgb_conv;
493 unsigned int pixelbytes;
494
495 switch (props->visual) {
496 case VISUAL_INDIRECT_8:
497 rgb_conv = bgr_323;
498 pixelbytes = 1;
499 break;
500 case VISUAL_RGB_5_5_5_LE:
501 rgb_conv = rgb_555_le;
502 pixelbytes = 2;
503 break;
504 case VISUAL_RGB_5_5_5_BE:
505 rgb_conv = rgb_555_be;
506 pixelbytes = 2;
507 break;
508 case VISUAL_RGB_5_6_5_LE:
509 rgb_conv = rgb_565_le;
510 pixelbytes = 2;
511 break;
512 case VISUAL_RGB_5_6_5_BE:
513 rgb_conv = rgb_565_be;
514 pixelbytes = 2;
515 break;
516 case VISUAL_RGB_8_8_8:
517 rgb_conv = rgb_888;
518 pixelbytes = 3;
519 break;
520 case VISUAL_BGR_8_8_8:
521 rgb_conv = bgr_888;
522 pixelbytes = 3;
523 break;
524 case VISUAL_RGB_8_8_8_0:
525 rgb_conv = rgb_8880;
526 pixelbytes = 4;
527 break;
528 case VISUAL_RGB_0_8_8_8:
529 rgb_conv = rgb_0888;
530 pixelbytes = 4;
531 break;
532 case VISUAL_BGR_0_8_8_8:
533 rgb_conv = bgr_0888;
534 pixelbytes = 4;
535 break;
536 case VISUAL_BGR_8_8_8_0:
537 rgb_conv = bgr_8880;
538 pixelbytes = 4;
539 break;
540 default:
541 LOG("Unsupported visual.");
542 return NULL;
543 }
544
545 outdev_t *fbdev = malloc(sizeof(outdev_t), FRAME_ATOMIC);
546 if (!fbdev)
547 return NULL;
548
549 fb_instance_t *instance = malloc(sizeof(fb_instance_t), FRAME_ATOMIC);
550 if (!instance) {
551 free(fbdev);
552 return NULL;
553 }
554
555 outdev_initialize("fbdev", fbdev, &fbdev_ops);
556 fbdev->data = instance;
557
558 spinlock_initialize(&instance->lock, "*fb.instance.lock");
559
560 instance->rgb_conv = rgb_conv;
561 instance->pixelbytes = pixelbytes;
562 instance->xres = props->x;
563 instance->yres = props->y;
564 instance->scanline = props->scan;
565 instance->position = 0;
566
567 instance->cols = X2COL(instance->xres);
568 instance->rows = Y2ROW(instance->yres);
569
570 if (instance->yres > LOGO_HEIGHT) {
571 instance->ylogo = LOGO_HEIGHT;
572 instance->rowtrim = instance->rows - Y2ROW(instance->ylogo);
573 if (instance->ylogo % FONT_SCANLINES > 0)
574 instance->rowtrim--;
575 instance->ytrim = ROW2Y(instance->rowtrim);
576 } else {
577 instance->ylogo = 0;
578 instance->ytrim = instance->yres;
579 instance->rowtrim = instance->rows;
580 }
581
582 instance->glyphscanline = FONT_WIDTH * instance->pixelbytes;
583 instance->glyphbytes = ROW2Y(instance->glyphscanline);
584 instance->bgscanbytes = instance->xres * instance->pixelbytes;
585
586 size_t fbsize = instance->scanline * instance->yres;
587 size_t bbsize = instance->cols * instance->rows * sizeof(uint16_t);
588 size_t glyphsize = FONT_GLYPHS * instance->glyphbytes;
589
590 instance->addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
591 if (!instance->addr) {
592 LOG("Unable to map framebuffer.");
593 free(instance);
594 free(fbdev);
595 return NULL;
596 }
597
598 instance->backbuf = (uint16_t *) malloc(bbsize, 0);
599 if (!instance->backbuf) {
600 LOG("Unable to allocate backbuffer.");
601 free(instance);
602 free(fbdev);
603 return NULL;
604 }
605
606 instance->glyphs = (uint8_t *) malloc(glyphsize, 0);
607 if (!instance->glyphs) {
608 LOG("Unable to allocate glyphs.");
609 free(instance->backbuf);
610 free(instance);
611 free(fbdev);
612 return NULL;
613 }
614
615 instance->bgscan = malloc(instance->bgscanbytes, 0);
616 if (!instance->bgscan) {
617 LOG("Unable to allocate background pixel.");
618 free(instance->glyphs);
619 free(instance->backbuf);
620 free(instance);
621 free(fbdev);
622 return NULL;
623 }
624
625 memsetw(instance->backbuf, instance->cols * instance->rows, 0);
626 glyphs_render(instance);
627
628 link_initialize(&instance->parea.link);
629 instance->parea.pbase = props->addr;
630 instance->parea.frames = SIZE2FRAMES(fbsize);
631 instance->parea.unpriv = false;
632 instance->parea.mapped = false;
633 ddi_parea_register(&instance->parea);
634
635 if (!fb_exported) {
636 /*
637 * We export the kernel framebuffer for uspace usage.
638 * This is used in the case the uspace framebuffer
639 * driver is not self-sufficient.
640 */
641 sysinfo_set_item_val("fb", NULL, true);
642 sysinfo_set_item_val("fb.kind", NULL, 1);
643 sysinfo_set_item_val("fb.width", NULL, instance->xres);
644 sysinfo_set_item_val("fb.height", NULL, instance->yres);
645 sysinfo_set_item_val("fb.scanline", NULL, instance->scanline);
646 sysinfo_set_item_val("fb.visual", NULL, props->visual);
647 sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
648
649 fb_exported = true;
650 }
651
652 fb_redraw(fbdev);
653 return fbdev;
654}
655
656/** @}
657 */
Note: See TracBrowser for help on using the repository browser.