Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2013 Intel Corporation; author Matt Fleming
0004  */
0005 
0006 #include <linux/console.h>
0007 #include <linux/efi.h>
0008 #include <linux/font.h>
0009 #include <linux/io.h>
0010 #include <linux/kernel.h>
0011 #include <linux/serial_core.h>
0012 #include <linux/screen_info.h>
0013 
0014 #include <asm/early_ioremap.h>
0015 
0016 static const struct console *earlycon_console __initdata;
0017 static const struct font_desc *font;
0018 static u32 efi_x, efi_y;
0019 static u64 fb_base;
0020 static bool fb_wb;
0021 static void *efi_fb;
0022 
0023 /*
0024  * EFI earlycon needs to use early_memremap() to map the framebuffer.
0025  * But early_memremap() is not usable for 'earlycon=efifb keep_bootcon',
0026  * memremap() should be used instead. memremap() will be available after
0027  * paging_init() which is earlier than initcall callbacks. Thus adding this
0028  * early initcall function early_efi_map_fb() to map the whole EFI framebuffer.
0029  */
0030 static int __init efi_earlycon_remap_fb(void)
0031 {
0032     /* bail if there is no bootconsole or it has been disabled already */
0033     if (!earlycon_console || !(earlycon_console->flags & CON_ENABLED))
0034         return 0;
0035 
0036     efi_fb = memremap(fb_base, screen_info.lfb_size,
0037               fb_wb ? MEMREMAP_WB : MEMREMAP_WC);
0038 
0039     return efi_fb ? 0 : -ENOMEM;
0040 }
0041 early_initcall(efi_earlycon_remap_fb);
0042 
0043 static int __init efi_earlycon_unmap_fb(void)
0044 {
0045     /* unmap the bootconsole fb unless keep_bootcon has left it enabled */
0046     if (efi_fb && !(earlycon_console->flags & CON_ENABLED))
0047         memunmap(efi_fb);
0048     return 0;
0049 }
0050 late_initcall(efi_earlycon_unmap_fb);
0051 
0052 static __ref void *efi_earlycon_map(unsigned long start, unsigned long len)
0053 {
0054     pgprot_t fb_prot;
0055 
0056     if (efi_fb)
0057         return efi_fb + start;
0058 
0059     fb_prot = fb_wb ? PAGE_KERNEL : pgprot_writecombine(PAGE_KERNEL);
0060     return early_memremap_prot(fb_base + start, len, pgprot_val(fb_prot));
0061 }
0062 
0063 static __ref void efi_earlycon_unmap(void *addr, unsigned long len)
0064 {
0065     if (efi_fb)
0066         return;
0067 
0068     early_memunmap(addr, len);
0069 }
0070 
0071 static void efi_earlycon_clear_scanline(unsigned int y)
0072 {
0073     unsigned long *dst;
0074     u16 len;
0075 
0076     len = screen_info.lfb_linelength;
0077     dst = efi_earlycon_map(y*len, len);
0078     if (!dst)
0079         return;
0080 
0081     memset(dst, 0, len);
0082     efi_earlycon_unmap(dst, len);
0083 }
0084 
0085 static void efi_earlycon_scroll_up(void)
0086 {
0087     unsigned long *dst, *src;
0088     u16 len;
0089     u32 i, height;
0090 
0091     len = screen_info.lfb_linelength;
0092     height = screen_info.lfb_height;
0093 
0094     for (i = 0; i < height - font->height; i++) {
0095         dst = efi_earlycon_map(i*len, len);
0096         if (!dst)
0097             return;
0098 
0099         src = efi_earlycon_map((i + font->height) * len, len);
0100         if (!src) {
0101             efi_earlycon_unmap(dst, len);
0102             return;
0103         }
0104 
0105         memmove(dst, src, len);
0106 
0107         efi_earlycon_unmap(src, len);
0108         efi_earlycon_unmap(dst, len);
0109     }
0110 }
0111 
0112 static void efi_earlycon_write_char(u32 *dst, unsigned char c, unsigned int h)
0113 {
0114     const u32 color_black = 0x00000000;
0115     const u32 color_white = 0x00ffffff;
0116     const u8 *src;
0117     int m, n, bytes;
0118     u8 x;
0119 
0120     bytes = BITS_TO_BYTES(font->width);
0121     src = font->data + c * font->height * bytes + h * bytes;
0122 
0123     for (m = 0; m < font->width; m++) {
0124         n = m % 8;
0125         x = *(src + m / 8);
0126         if ((x >> (7 - n)) & 1)
0127             *dst = color_white;
0128         else
0129             *dst = color_black;
0130         dst++;
0131     }
0132 }
0133 
0134 static void
0135 efi_earlycon_write(struct console *con, const char *str, unsigned int num)
0136 {
0137     struct screen_info *si;
0138     unsigned int len;
0139     const char *s;
0140     void *dst;
0141 
0142     si = &screen_info;
0143     len = si->lfb_linelength;
0144 
0145     while (num) {
0146         unsigned int linemax;
0147         unsigned int h, count = 0;
0148 
0149         for (s = str; *s && *s != '\n'; s++) {
0150             if (count == num)
0151                 break;
0152             count++;
0153         }
0154 
0155         linemax = (si->lfb_width - efi_x) / font->width;
0156         if (count > linemax)
0157             count = linemax;
0158 
0159         for (h = 0; h < font->height; h++) {
0160             unsigned int n, x;
0161 
0162             dst = efi_earlycon_map((efi_y + h) * len, len);
0163             if (!dst)
0164                 return;
0165 
0166             s = str;
0167             n = count;
0168             x = efi_x;
0169 
0170             while (n-- > 0) {
0171                 efi_earlycon_write_char(dst + x*4, *s, h);
0172                 x += font->width;
0173                 s++;
0174             }
0175 
0176             efi_earlycon_unmap(dst, len);
0177         }
0178 
0179         num -= count;
0180         efi_x += count * font->width;
0181         str += count;
0182 
0183         if (num > 0 && *s == '\n') {
0184             efi_x = 0;
0185             efi_y += font->height;
0186             str++;
0187             num--;
0188         }
0189 
0190         if (efi_x + font->width > si->lfb_width) {
0191             efi_x = 0;
0192             efi_y += font->height;
0193         }
0194 
0195         if (efi_y + font->height > si->lfb_height) {
0196             u32 i;
0197 
0198             efi_y -= font->height;
0199             efi_earlycon_scroll_up();
0200 
0201             for (i = 0; i < font->height; i++)
0202                 efi_earlycon_clear_scanline(efi_y + i);
0203         }
0204     }
0205 }
0206 
0207 static int __init efi_earlycon_setup(struct earlycon_device *device,
0208                      const char *opt)
0209 {
0210     struct screen_info *si;
0211     u16 xres, yres;
0212     u32 i;
0213 
0214     if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
0215         return -ENODEV;
0216 
0217     fb_base = screen_info.lfb_base;
0218     if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
0219         fb_base |= (u64)screen_info.ext_lfb_base << 32;
0220 
0221     fb_wb = opt && !strcmp(opt, "ram");
0222 
0223     si = &screen_info;
0224     xres = si->lfb_width;
0225     yres = si->lfb_height;
0226 
0227     /*
0228      * efi_earlycon_write_char() implicitly assumes a framebuffer with
0229      * 32 bits per pixel.
0230      */
0231     if (si->lfb_depth != 32)
0232         return -ENODEV;
0233 
0234     font = get_default_font(xres, yres, -1, -1);
0235     if (!font)
0236         return -ENODEV;
0237 
0238     efi_y = rounddown(yres, font->height) - font->height;
0239     for (i = 0; i < (yres - efi_y) / font->height; i++)
0240         efi_earlycon_scroll_up();
0241 
0242     device->con->write = efi_earlycon_write;
0243     earlycon_console = device->con;
0244     return 0;
0245 }
0246 EARLYCON_DECLARE(efifb, efi_earlycon_setup);