xref: /linux/drivers/firmware/efi/earlycon.c (revision 6c8c1406a6d6a3f2e61ac590f5c0994231bc6be7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2013 Intel Corporation; author Matt Fleming
4  */
5 
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>
13 
14 #include <asm/early_ioremap.h>
15 
16 static const struct console *earlycon_console __initdata;
17 static const struct font_desc *font;
18 static u32 efi_x, efi_y;
19 static u64 fb_base;
20 static bool fb_wb;
21 static void *efi_fb;
22 
23 /*
24  * EFI earlycon needs to use early_memremap() to map the framebuffer.
25  * But early_memremap() is not usable for 'earlycon=efifb keep_bootcon',
26  * memremap() should be used instead. memremap() will be available after
27  * paging_init() which is earlier than initcall callbacks. Thus adding this
28  * early initcall function early_efi_map_fb() to map the whole EFI framebuffer.
29  */
30 static int __init efi_earlycon_remap_fb(void)
31 {
32 	/* bail if there is no bootconsole or it has been disabled already */
33 	if (!earlycon_console || !(earlycon_console->flags & CON_ENABLED))
34 		return 0;
35 
36 	efi_fb = memremap(fb_base, screen_info.lfb_size,
37 			  fb_wb ? MEMREMAP_WB : MEMREMAP_WC);
38 
39 	return efi_fb ? 0 : -ENOMEM;
40 }
41 early_initcall(efi_earlycon_remap_fb);
42 
43 static int __init efi_earlycon_unmap_fb(void)
44 {
45 	/* unmap the bootconsole fb unless keep_bootcon has left it enabled */
46 	if (efi_fb && !(earlycon_console->flags & CON_ENABLED))
47 		memunmap(efi_fb);
48 	return 0;
49 }
50 late_initcall(efi_earlycon_unmap_fb);
51 
52 static __ref void *efi_earlycon_map(unsigned long start, unsigned long len)
53 {
54 	pgprot_t fb_prot;
55 
56 	if (efi_fb)
57 		return efi_fb + start;
58 
59 	fb_prot = fb_wb ? PAGE_KERNEL : pgprot_writecombine(PAGE_KERNEL);
60 	return early_memremap_prot(fb_base + start, len, pgprot_val(fb_prot));
61 }
62 
63 static __ref void efi_earlycon_unmap(void *addr, unsigned long len)
64 {
65 	if (efi_fb)
66 		return;
67 
68 	early_memunmap(addr, len);
69 }
70 
71 static void efi_earlycon_clear_scanline(unsigned int y)
72 {
73 	unsigned long *dst;
74 	u16 len;
75 
76 	len = screen_info.lfb_linelength;
77 	dst = efi_earlycon_map(y*len, len);
78 	if (!dst)
79 		return;
80 
81 	memset(dst, 0, len);
82 	efi_earlycon_unmap(dst, len);
83 }
84 
85 static void efi_earlycon_scroll_up(void)
86 {
87 	unsigned long *dst, *src;
88 	u16 len;
89 	u32 i, height;
90 
91 	len = screen_info.lfb_linelength;
92 	height = screen_info.lfb_height;
93 
94 	for (i = 0; i < height - font->height; i++) {
95 		dst = efi_earlycon_map(i*len, len);
96 		if (!dst)
97 			return;
98 
99 		src = efi_earlycon_map((i + font->height) * len, len);
100 		if (!src) {
101 			efi_earlycon_unmap(dst, len);
102 			return;
103 		}
104 
105 		memmove(dst, src, len);
106 
107 		efi_earlycon_unmap(src, len);
108 		efi_earlycon_unmap(dst, len);
109 	}
110 }
111 
112 static void efi_earlycon_write_char(u32 *dst, unsigned char c, unsigned int h)
113 {
114 	const u32 color_black = 0x00000000;
115 	const u32 color_white = 0x00ffffff;
116 	const u8 *src;
117 	int m, n, bytes;
118 	u8 x;
119 
120 	bytes = BITS_TO_BYTES(font->width);
121 	src = font->data + c * font->height * bytes + h * bytes;
122 
123 	for (m = 0; m < font->width; m++) {
124 		n = m % 8;
125 		x = *(src + m / 8);
126 		if ((x >> (7 - n)) & 1)
127 			*dst = color_white;
128 		else
129 			*dst = color_black;
130 		dst++;
131 	}
132 }
133 
134 static void
135 efi_earlycon_write(struct console *con, const char *str, unsigned int num)
136 {
137 	struct screen_info *si;
138 	unsigned int len;
139 	const char *s;
140 	void *dst;
141 
142 	si = &screen_info;
143 	len = si->lfb_linelength;
144 
145 	while (num) {
146 		unsigned int linemax;
147 		unsigned int h, count = 0;
148 
149 		for (s = str; *s && *s != '\n'; s++) {
150 			if (count == num)
151 				break;
152 			count++;
153 		}
154 
155 		linemax = (si->lfb_width - efi_x) / font->width;
156 		if (count > linemax)
157 			count = linemax;
158 
159 		for (h = 0; h < font->height; h++) {
160 			unsigned int n, x;
161 
162 			dst = efi_earlycon_map((efi_y + h) * len, len);
163 			if (!dst)
164 				return;
165 
166 			s = str;
167 			n = count;
168 			x = efi_x;
169 
170 			while (n-- > 0) {
171 				efi_earlycon_write_char(dst + x*4, *s, h);
172 				x += font->width;
173 				s++;
174 			}
175 
176 			efi_earlycon_unmap(dst, len);
177 		}
178 
179 		num -= count;
180 		efi_x += count * font->width;
181 		str += count;
182 
183 		if (num > 0 && *s == '\n') {
184 			efi_x = 0;
185 			efi_y += font->height;
186 			str++;
187 			num--;
188 		}
189 
190 		if (efi_x + font->width > si->lfb_width) {
191 			efi_x = 0;
192 			efi_y += font->height;
193 		}
194 
195 		if (efi_y + font->height > si->lfb_height) {
196 			u32 i;
197 
198 			efi_y -= font->height;
199 			efi_earlycon_scroll_up();
200 
201 			for (i = 0; i < font->height; i++)
202 				efi_earlycon_clear_scanline(efi_y + i);
203 		}
204 	}
205 }
206 
207 static int __init efi_earlycon_setup(struct earlycon_device *device,
208 				     const char *opt)
209 {
210 	struct screen_info *si;
211 	u16 xres, yres;
212 	u32 i;
213 
214 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
215 		return -ENODEV;
216 
217 	fb_base = screen_info.lfb_base;
218 	if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
219 		fb_base |= (u64)screen_info.ext_lfb_base << 32;
220 
221 	fb_wb = opt && !strcmp(opt, "ram");
222 
223 	si = &screen_info;
224 	xres = si->lfb_width;
225 	yres = si->lfb_height;
226 
227 	/*
228 	 * efi_earlycon_write_char() implicitly assumes a framebuffer with
229 	 * 32 bits per pixel.
230 	 */
231 	if (si->lfb_depth != 32)
232 		return -ENODEV;
233 
234 	font = get_default_font(xres, yres, -1, -1);
235 	if (!font)
236 		return -ENODEV;
237 
238 	efi_y = rounddown(yres, font->height) - font->height;
239 	for (i = 0; i < (yres - efi_y) / font->height; i++)
240 		efi_earlycon_scroll_up();
241 
242 	device->con->write = efi_earlycon_write;
243 	earlycon_console = device->con;
244 	return 0;
245 }
246 EARLYCON_DECLARE(efifb, efi_earlycon_setup);
247