source: mainline/kernel/genarch/src/fb/fb.c@ 925fdd7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 925fdd7 was 925fdd7, checked in by Pavel Rimsky <rimskyp@…>, 17 years ago

Fixed the bug when on SunBlade1500 we wrote to an address outside the framebuffer memory. A nasty hack to the 8-bit palette so that on sb1500 we do not have to turn black into white and white into black.

  • Property mode set to 100644
File size: 13.9 KB
RevLine 
[bbf5657]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[bbf5657]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[94d614e]29/** @addtogroup genarch
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[fe050b7]35#include <genarch/fb/font-8x16.h>
[2bc137c2]36#include <genarch/fb/visuals.h>
[fe050b7]37#include <genarch/fb/fb.h>
[bbf5657]38#include <console/chardev.h>
39#include <console/console.h>
[2666daa]40#include <sysinfo/sysinfo.h>
[74d1d66]41#include <mm/slab.h>
[fcbca14f]42#include <panic.h>
[d87c3f3]43#include <memstr.h>
[74d1d66]44#include <config.h>
[6eb96fce]45#include <bitops.h>
46#include <print.h>
[f8ddd17]47#include <ddi/ddi.h>
[730376d]48#include <arch/types.h>
[bbf5657]49
[c2942d8]50#include "helenos.xbm"
51
[f8ddd17]52static parea_t fb_parea; /**< Physical memory area for fb. */
53
[bbf5657]54SPINLOCK_INITIALIZE(fb_lock);
55
[7f1c620]56static uint8_t *fbaddress = NULL;
[bbf5657]57
[7f1c620]58static uint8_t *blankline = NULL;
[730376d]59static uint8_t *dbbuffer = NULL; /* Buffer for fast scrolling console */
60static index_t dboffset;
[74d1d66]61
[a2c4445]62static unsigned int xres = 0;
63static unsigned int yres = 0;
64static unsigned int scanline = 0;
65static unsigned int pixelbytes = 0;
[253f35a1]66#ifdef FB_INVERT_COLORS
67static bool invert_colors = true;
68#else
69static bool invert_colors = false;
70#endif
[bbf5657]71
[a2c4445]72static unsigned int position = 0;
73static unsigned int columns = 0;
74static unsigned int rows = 0;
[bbf5657]75
[a2c4445]76#define COL_WIDTH 8
77#define ROW_BYTES (scanline * FONT_SCANLINES)
78
79#define BGCOLOR 0x000080
80#define FGCOLOR 0xffff00
81#define LOGOCOLOR 0x2020b0
82
[51baa8a]83#define RED(x, bits) ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
[a2c4445]84#define GREEN(x, bits) ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
85#define BLUE(x, bits) ((x >> (8 - bits)) & ((1 << bits) - 1))
86
[6eb96fce]87#define POINTPOS(x, y) ((y) * scanline + (x) * pixelbytes)
[95c7526]88
89/***************************************************************/
90/* Pixel specific fuctions */
[bbf5657]91
[6eb96fce]92static void (*rgb2scr)(void *, int);
93static int (*scr2rgb)(void *);
[fcbca14f]94
[253f35a1]95static inline int COLOR(int color)
96{
97 return invert_colors ? ~color : color;
98}
99
[6eb96fce]100/* Conversion routines between different color representations */
[2bc137c2]101static void rgb_byte0888(void *dst, int rgb)
[bbf5657]102{
[800eaf5]103 *((int *) dst) = rgb;
[95c7526]104}
105
[2bc137c2]106static int byte0888_rgb(void *src)
[fcbca14f]107{
[800eaf5]108 return (*((int *) src)) & 0xffffff;
[fcbca14f]109}
110
[ccb0cbc]111static void bgr_byte0888(void *dst, int rgb)
112{
[51baa8a]113 *((uint32_t *) dst) = BLUE(rgb, 8) << 16 | GREEN(rgb, 8) << 8 |
114 RED(rgb, 8);
[ccb0cbc]115}
116
117static int byte0888_bgr(void *src)
118{
119 int color = *(uint32_t *)(src);
[51baa8a]120 return ((color & 0xff) << 16) | (((color >> 8) & 0xff) << 8) |
121 ((color >> 16) & 0xff);
[ccb0cbc]122}
123
[2bc137c2]124static void rgb_byte888(void *dst, int rgb)
[fcbca14f]125{
[1004b37]126 uint8_t *scr = (uint8_t *) dst;
[d7e3f1ad]127#if defined(FB_INVERT_ENDIAN)
[6eb96fce]128 scr[0] = RED(rgb, 8);
129 scr[1] = GREEN(rgb, 8);
130 scr[2] = BLUE(rgb, 8);
[31282f9]131#else
[6eb96fce]132 scr[2] = RED(rgb, 8);
133 scr[1] = GREEN(rgb, 8);
134 scr[0] = BLUE(rgb, 8);
135#endif
[fcbca14f]136}
137
[2bc137c2]138static int byte888_rgb(void *src)
[95c7526]139{
[1004b37]140 uint8_t *scr = (uint8_t *) src;
[d7e3f1ad]141#if defined(FB_INVERT_ENDIAN)
[6eb96fce]142 return scr[0] << 16 | scr[1] << 8 | scr[2];
[31282f9]143#else
[6eb96fce]144 return scr[2] << 16 | scr[1] << 8 | scr[0];
[31282f9]145#endif
[95c7526]146}
147
[2bc137c2]148/** 16-bit depth (5:5:5) */
149static void rgb_byte555(void *dst, int rgb)
150{
151 /* 5-bit, 5-bits, 5-bits */
[51baa8a]152 *((uint16_t *) dst) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 |
153 BLUE(rgb, 5);
[2bc137c2]154}
155
156/** 16-bit depth (5:5:5) */
157static int byte555_rgb(void *src)
158{
159 int color = *(uint16_t *)(src);
[51baa8a]160 return (((color >> 10) & 0x1f) << (16 + 3)) |
161 (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
[2bc137c2]162}
163
[6eb96fce]164/** 16-bit depth (5:6:5) */
[2bc137c2]165static void rgb_byte565(void *dst, int rgb)
[fcbca14f]166{
[6eb96fce]167 /* 5-bit, 6-bits, 5-bits */
[51baa8a]168 *((uint16_t *) dst) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 |
169 BLUE(rgb, 5);
[fcbca14f]170}
171
[6eb96fce]172/** 16-bit depth (5:6:5) */
[2bc137c2]173static int byte565_rgb(void *src)
[fcbca14f]174{
[7f1c620]175 int color = *(uint16_t *)(src);
[51baa8a]176 return (((color >> 11) & 0x1f) << (16 + 3)) |
177 (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
[fcbca14f]178}
179
[925fdd7]180/** Put pixel - 8-bit depth (color palette/3:2:3, inverted)
[b4fa652]181 *
182 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
183 * will most likely use a color palette. The color appearance
184 * will be pretty random and depend on the default installed
185 * palette. This could be fixed by supporting custom palette
186 * and setting it to simulate the 8-bit truecolor.
[925fdd7]187 *
188 * Currently we set the palette on the sparc64 port.
189 *
190 * Note that the byte is being inverted by this function. The reason is
191 * that we would like to use a color palette where the white color code
192 * is 0 and the black color code is 255, as some machines (SunBlade 1500)
193 * use these codes for black and white and prevent to set codes
194 * 0 and 255 to other colors.
[b4fa652]195 */
[2bc137c2]196static void rgb_byte8(void *dst, int rgb)
[fcbca14f]197{
[925fdd7]198 *((uint8_t *) dst) = 255 - (RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 |
199 BLUE(rgb, 3));
[fcbca14f]200}
201
[b4fa652]202/** Return pixel color - 8-bit depth (color palette/3:2:3)
203 *
[2bc137c2]204 * See the comment for rgb_byte().
[b4fa652]205 */
[2bc137c2]206static int byte8_rgb(void *src)
[fcbca14f]207{
[925fdd7]208 int color = 255 - (*(uint8_t *)src);
[51baa8a]209 return (((color >> 5) & 0x7) << (16 + 5)) |
210 (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
[fcbca14f]211}
212
[6eb96fce]213static void putpixel(unsigned int x, unsigned int y, int color)
[95c7526]214{
[800eaf5]215 (*rgb2scr)(&fbaddress[POINTPOS(x, y)], COLOR(color));
[6eb96fce]216
217 if (dbbuffer) {
218 int dline = (y + dboffset) % yres;
[800eaf5]219 (*rgb2scr)(&dbbuffer[POINTPOS(x, dline)], COLOR(color));
[6eb96fce]220 }
221}
222
223/** Get pixel from viewport */
224static int getpixel(unsigned int x, unsigned int y)
225{
226 if (dbbuffer) {
227 int dline = (y + dboffset) % yres;
[800eaf5]228 return COLOR((*scr2rgb)(&dbbuffer[POINTPOS(x, dline)]));
[6eb96fce]229 }
[800eaf5]230 return COLOR((*scr2rgb)(&fbaddress[POINTPOS(x, y)]));
[bbf5657]231}
232
[95c7526]233
[bbf5657]234/** Fill screen with background color */
235static void clear_screen(void)
236{
[a2c4445]237 unsigned int y;
[bbf5657]238
[6eb96fce]239 for (y = 0; y < yres; y++) {
[800eaf5]240 memcpy(&fbaddress[scanline * y], blankline, xres * pixelbytes);
[6eb96fce]241 if (dbbuffer)
[51baa8a]242 memcpy(&dbbuffer[scanline * y], blankline,
243 xres * pixelbytes);
[6eb96fce]244 }
[bbf5657]245}
246
[a2c4445]247
248/** Scroll screen one row up */
249static void scroll_screen(void)
[bbf5657]250{
[6eb96fce]251 if (dbbuffer) {
[730376d]252 count_t first;
253
[51baa8a]254 /* Clear the last row */
[2bc137c2]255 memcpy(&dbbuffer[dboffset * scanline], blankline, ROW_BYTES);
[6eb96fce]256
257 dboffset = (dboffset + FONT_SCANLINES) % yres;
[730376d]258 first = yres - dboffset;
[51baa8a]259
260 /* Move all rows one row up */
261 if (xres * pixelbytes == scanline) {
262 memcpy(fbaddress, &dbbuffer[dboffset * scanline],
263 first * scanline);
264 memcpy(&fbaddress[first * scanline], dbbuffer,
265 dboffset * scanline);
266 } else {
267 /*
268 * When the scanline is bigger than number of bytes
269 * in the X-resolution, chances are that the
270 * frame buffer memory past the X-resolution is special
271 * in some way. For example, the SUNW,ffb framebuffer
272 * wraps this area around the beginning of the same
273 * line. To avoid troubles, copy only memory as
274 * specified by the resolution.
275 */
[1004b37]276 unsigned int i;
[51baa8a]277
278 for (i = 0; i < first; i++)
279 memcpy(&fbaddress[i * scanline],
280 &dbbuffer[(dboffset + i) * scanline],
281 xres * pixelbytes);
282 for (i = 0; i < dboffset; i++)
283 memcpy(&fbaddress[(first + i) * scanline],
284 &dbbuffer[i * scanline], xres * pixelbytes);
285 }
[6eb96fce]286 } else {
[730376d]287 uint8_t *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
288
[51baa8a]289 if (xres * pixelbytes == scanline) {
290 /* Move all rows one row up */
291 memcpy((void *) fbaddress,
292 (void *) &fbaddress[ROW_BYTES],
293 scanline * yres - ROW_BYTES);
294 /* Clear the last row */
295 memcpy((void *) lastline, (void *) blankline,
296 ROW_BYTES);
297 } else {
298 /*
299 * See the comment in the dbbuffer case.
300 */
[1004b37]301 unsigned int i;
[51baa8a]302
303 /* Move all rows one row up */
304 for (i = 0; i < yres - FONT_SCANLINES; i++)
305 memcpy(&fbaddress[i * scanline],
306 &fbaddress[(i + FONT_SCANLINES) * scanline],
307 xres * pixelbytes);
308 /* Clear the last row */
309 for (i = 0; i < FONT_SCANLINES; i++)
310 memcpy(&lastline[i * scanline],
311 &blankline[i * scanline],
312 xres * pixelbytes);
313 }
[6eb96fce]314 }
[a2c4445]315}
316
317
318static void invert_pixel(unsigned int x, unsigned int y)
319{
[6eb96fce]320 putpixel(x, y, ~getpixel(x, y));
[bbf5657]321}
322
323
324/** Draw one line of glyph at a given position */
[a2c4445]325static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
[bbf5657]326{
[a2c4445]327 unsigned int i;
[bbf5657]328
[a2c4445]329 for (i = 0; i < 8; i++)
330 if (glline & (1 << (7 - i))) {
[6eb96fce]331 putpixel(x + i, y, FGCOLOR);
[bbf5657]332 } else
[6eb96fce]333 putpixel(x + i, y, BGCOLOR);
[bbf5657]334}
335
[95c7526]336/***************************************************************/
337/* Character-console functions */
338
[bbf5657]339/** Draw character at given position */
[7f1c620]340static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
[bbf5657]341{
[a2c4445]342 unsigned int y;
[bbf5657]343
[a2c4445]344 for (y = 0; y < FONT_SCANLINES; y++)
[51baa8a]345 draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y],
346 col * COL_WIDTH, row * FONT_SCANLINES + y);
[bbf5657]347}
348
349/** Invert character at given position */
[a2c4445]350static void invert_char(unsigned int col, unsigned int row)
[bbf5657]351{
[a2c4445]352 unsigned int x;
353 unsigned int y;
[bbf5657]354
[a2c4445]355 for (x = 0; x < COL_WIDTH; x++)
356 for (y = 0; y < FONT_SCANLINES; y++)
[51baa8a]357 invert_pixel(col * COL_WIDTH + x,
358 row * FONT_SCANLINES + y);
[bbf5657]359}
360
361/** Draw character at default position */
362static void draw_char(char chr)
363{
[a2c4445]364 draw_glyph(chr, position % columns, position / columns);
[bbf5657]365}
366
[a2c4445]367static void draw_logo(unsigned int startx, unsigned int starty)
[c2942d8]368{
[a2c4445]369 unsigned int x;
370 unsigned int y;
371 unsigned int byte;
372 unsigned int rowbytes;
[c2942d8]373
[a2c4445]374 rowbytes = (helenos_width - 1) / 8 + 1;
[c2942d8]375
[a2c4445]376 for (y = 0; y < helenos_height; y++)
377 for (x = 0; x < helenos_width; x++) {
378 byte = helenos_bits[rowbytes * y + x / 8];
[c2942d8]379 byte >>= x % 8;
380 if (byte & 1)
[730376d]381 putpixel(startx + x, starty + y,
[51baa8a]382 COLOR(LOGOCOLOR));
[c2942d8]383 }
384}
385
[95c7526]386/***************************************************************/
387/* Stdout specific functions */
388
[bbf5657]389static void invert_cursor(void)
390{
[a2c4445]391 invert_char(position % columns, position / columns);
[bbf5657]392}
393
394/** Print character to screen
395 *
396 * Emulate basic terminal commands
397 */
398static void fb_putchar(chardev_t *dev, char ch)
399{
[22cf454d]400 spinlock_lock(&fb_lock);
[a2c4445]401
402 switch (ch) {
[06e1e95]403 case '\n':
404 invert_cursor();
405 position += columns;
406 position -= position % columns;
407 break;
408 case '\r':
409 invert_cursor();
410 position -= position % columns;
411 break;
412 case '\b':
413 invert_cursor();
414 if (position % columns)
415 position--;
416 break;
417 case '\t':
418 invert_cursor();
419 do {
420 draw_char(' ');
[95c7526]421 position++;
[06e1e95]422 } while ((position % 8) && position < columns * rows);
423 break;
424 default:
425 draw_char(ch);
426 position++;
[bbf5657]427 }
[a2c4445]428
429 if (position >= columns * rows) {
[bbf5657]430 position -= columns;
431 scroll_screen();
432 }
[a2c4445]433
[bbf5657]434 invert_cursor();
[a2c4445]435
[22cf454d]436 spinlock_unlock(&fb_lock);
[bbf5657]437}
438
439static chardev_t framebuffer;
440static chardev_operations_t fb_ops = {
441 .write = fb_putchar,
442};
443
444
445/** Initialize framebuffer as a chardev output device
446 *
[965dc18]447 * @param props Properties of the framebuffer device.
[bbf5657]448 */
[965dc18]449void fb_init(fb_properties_t *props)
[bbf5657]450{
[965dc18]451 switch (props->visual) {
[2bc137c2]452 case VISUAL_INDIRECT_8:
453 rgb2scr = rgb_byte8;
454 scr2rgb = byte8_rgb;
[d6f270f]455 pixelbytes = 1;
456 break;
[2bc137c2]457 case VISUAL_RGB_5_5_5:
458 rgb2scr = rgb_byte555;
459 scr2rgb = byte555_rgb;
460 pixelbytes = 2;
461 break;
462 case VISUAL_RGB_5_6_5:
463 rgb2scr = rgb_byte565;
464 scr2rgb = byte565_rgb;
[d6f270f]465 pixelbytes = 2;
466 break;
[2bc137c2]467 case VISUAL_RGB_8_8_8:
468 rgb2scr = rgb_byte888;
469 scr2rgb = byte888_rgb;
470 pixelbytes = 3;
471 break;
472 case VISUAL_RGB_8_8_8_0:
473 rgb2scr = rgb_byte888;
474 scr2rgb = byte888_rgb;
475 pixelbytes = 4;
[d6f270f]476 break;
[2bc137c2]477 case VISUAL_RGB_0_8_8_8:
478 rgb2scr = rgb_byte0888;
479 scr2rgb = byte0888_rgb;
[d6f270f]480 pixelbytes = 4;
481 break;
[ccb0cbc]482 case VISUAL_BGR_0_8_8_8:
483 rgb2scr = bgr_byte0888;
484 scr2rgb = byte0888_bgr;
485 pixelbytes = 4;
486 break;
[d6f270f]487 default:
[2bc137c2]488 panic("Unsupported visual.\n");
[fcbca14f]489 }
[8424198]490
[20eb5e4d]491 unsigned int fbsize = props->scan * props->y;
[8424198]492
493 /* Map the framebuffer */
[925fdd7]494 fbaddress = (uint8_t *) hw_map((uintptr_t) props->addr,
495 fbsize + props->offset);
[965dc18]496 fbaddress += props->offset;
[8424198]497
[965dc18]498 xres = props->x;
499 yres = props->y;
500 scanline = props->scan;
[bbf5657]501
[965dc18]502 rows = props->y / FONT_SCANLINES;
503 columns = props->x / COL_WIDTH;
[bbf5657]504
[925fdd7]505 fb_parea.pbase = (uintptr_t) props->addr + props->offset;
[f8ddd17]506 fb_parea.vbase = (uintptr_t) fbaddress;
507 fb_parea.frames = SIZE2FRAMES(fbsize);
508 fb_parea.cacheable = false;
509 ddi_parea_register(&fb_parea);
510
[8060a24c]511 sysinfo_set_item_val("fb", NULL, true);
512 sysinfo_set_item_val("fb.kind", NULL, 1);
513 sysinfo_set_item_val("fb.width", NULL, xres);
514 sysinfo_set_item_val("fb.height", NULL, yres);
[965dc18]515 sysinfo_set_item_val("fb.scanline", NULL, props->scan);
516 sysinfo_set_item_val("fb.visual", NULL, props->visual);
517 sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
[20eb5e4d]518 sysinfo_set_item_val("fb.offset", NULL, props->offset);
[253f35a1]519 sysinfo_set_item_val("fb.invert-colors", NULL, invert_colors);
[8060a24c]520
[6eb96fce]521 /* Allocate double buffer */
[730376d]522 unsigned int order = fnzb(SIZE2FRAMES(fbsize) - 1) + 1;
523 dbbuffer = (uint8_t *) frame_alloc(order, FRAME_ATOMIC | FRAME_KA);
[2e9eae2]524 if (!dbbuffer)
[6eb96fce]525 printf("Failed to allocate scroll buffer.\n");
526 dboffset = 0;
527
528 /* Initialized blank line */
[7f1c620]529 blankline = (uint8_t *) malloc(ROW_BYTES, FRAME_ATOMIC);
[e45f81a]530 if (!blankline)
531 panic("Failed to allocate blank line for framebuffer.");
[965dc18]532 unsigned int x, y;
[800eaf5]533 for (y = 0; y < FONT_SCANLINES; y++)
534 for (x = 0; x < xres; x++)
535 (*rgb2scr)(&blankline[POINTPOS(x, y)], COLOR(BGCOLOR));
536
[6eb96fce]537 clear_screen();
538
539 /* Update size of screen to match text area */
540 yres = rows * FONT_SCANLINES;
541
[a2c4445]542 draw_logo(xres - helenos_width, 0);
[bbf5657]543 invert_cursor();
544
545 chardev_initialize("fb", &framebuffer, &fb_ops);
546 stdout = &framebuffer;
547}
[b45c443]548
[94d614e]549/** @}
[b45c443]550 */
Note: See TracBrowser for help on using the repository browser.