Linux 5.1.15
[linux/fpc-iii.git] / drivers / firmware / efi / earlycon.c
blobc9a0efca17b01b9d3959cd055f019683eeceae5d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2013 Intel Corporation; author Matt Fleming
4 */
6 #include <linux/console.h>
7 #include <linux/efi.h>
8 #include <linux/font.h>
9 #include <linux/io.h>
10 #include <linux/kernel.h>
11 #include <linux/serial_core.h>
12 #include <linux/screen_info.h>
14 #include <asm/early_ioremap.h>
16 static const struct font_desc *font;
17 static u32 efi_x, efi_y;
18 static u64 fb_base;
19 static pgprot_t fb_prot;
21 static __ref void *efi_earlycon_map(unsigned long start, unsigned long len)
23 return early_memremap_prot(fb_base + start, len, pgprot_val(fb_prot));
26 static __ref void efi_earlycon_unmap(void *addr, unsigned long len)
28 early_memunmap(addr, len);
31 static void efi_earlycon_clear_scanline(unsigned int y)
33 unsigned long *dst;
34 u16 len;
36 len = screen_info.lfb_linelength;
37 dst = efi_earlycon_map(y*len, len);
38 if (!dst)
39 return;
41 memset(dst, 0, len);
42 efi_earlycon_unmap(dst, len);
45 static void efi_earlycon_scroll_up(void)
47 unsigned long *dst, *src;
48 u16 len;
49 u32 i, height;
51 len = screen_info.lfb_linelength;
52 height = screen_info.lfb_height;
54 for (i = 0; i < height - font->height; i++) {
55 dst = efi_earlycon_map(i*len, len);
56 if (!dst)
57 return;
59 src = efi_earlycon_map((i + font->height) * len, len);
60 if (!src) {
61 efi_earlycon_unmap(dst, len);
62 return;
65 memmove(dst, src, len);
67 efi_earlycon_unmap(src, len);
68 efi_earlycon_unmap(dst, len);
72 static void efi_earlycon_write_char(u32 *dst, unsigned char c, unsigned int h)
74 const u32 color_black = 0x00000000;
75 const u32 color_white = 0x00ffffff;
76 const u8 *src;
77 u8 s8;
78 int m;
80 src = font->data + c * font->height;
81 s8 = *(src + h);
83 for (m = 0; m < 8; m++) {
84 if ((s8 >> (7 - m)) & 1)
85 *dst = color_white;
86 else
87 *dst = color_black;
88 dst++;
92 static void
93 efi_earlycon_write(struct console *con, const char *str, unsigned int num)
95 struct screen_info *si;
96 unsigned int len;
97 const char *s;
98 void *dst;
100 si = &screen_info;
101 len = si->lfb_linelength;
103 while (num) {
104 unsigned int linemax;
105 unsigned int h, count = 0;
107 for (s = str; *s && *s != '\n'; s++) {
108 if (count == num)
109 break;
110 count++;
113 linemax = (si->lfb_width - efi_x) / font->width;
114 if (count > linemax)
115 count = linemax;
117 for (h = 0; h < font->height; h++) {
118 unsigned int n, x;
120 dst = efi_earlycon_map((efi_y + h) * len, len);
121 if (!dst)
122 return;
124 s = str;
125 n = count;
126 x = efi_x;
128 while (n-- > 0) {
129 efi_earlycon_write_char(dst + x*4, *s, h);
130 x += font->width;
131 s++;
134 efi_earlycon_unmap(dst, len);
137 num -= count;
138 efi_x += count * font->width;
139 str += count;
141 if (num > 0 && *s == '\n') {
142 efi_x = 0;
143 efi_y += font->height;
144 str++;
145 num--;
148 if (efi_x + font->width > si->lfb_width) {
149 efi_x = 0;
150 efi_y += font->height;
153 if (efi_y + font->height > si->lfb_height) {
154 u32 i;
156 efi_y -= font->height;
157 efi_earlycon_scroll_up();
159 for (i = 0; i < font->height; i++)
160 efi_earlycon_clear_scanline(efi_y + i);
165 static int __init efi_earlycon_setup(struct earlycon_device *device,
166 const char *opt)
168 struct screen_info *si;
169 u16 xres, yres;
170 u32 i;
172 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
173 return -ENODEV;
175 fb_base = screen_info.lfb_base;
176 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
177 fb_base |= (u64)screen_info.ext_lfb_base << 32;
179 if (opt && !strcmp(opt, "ram"))
180 fb_prot = PAGE_KERNEL;
181 else
182 fb_prot = pgprot_writecombine(PAGE_KERNEL);
184 si = &screen_info;
185 xres = si->lfb_width;
186 yres = si->lfb_height;
189 * efi_earlycon_write_char() implicitly assumes a framebuffer with
190 * 32 bits per pixel.
192 if (si->lfb_depth != 32)
193 return -ENODEV;
195 font = get_default_font(xres, yres, -1, -1);
196 if (!font)
197 return -ENODEV;
199 efi_y = rounddown(yres, font->height) - font->height;
200 for (i = 0; i < (yres - efi_y) / font->height; i++)
201 efi_earlycon_scroll_up();
203 device->con->write = efi_earlycon_write;
204 return 0;
206 EARLYCON_DECLARE(efifb, efi_earlycon_setup);