Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/firmware/efi/earlycon.c
49968 views
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/sysfb.h>
13
#include <linux/string.h>
14
15
#include <asm/early_ioremap.h>
16
17
static const struct console *earlycon_console __initdata;
18
static const struct font_desc *font;
19
static u16 cur_line_y, max_line_y;
20
static u32 efi_x_array[1024];
21
static u32 efi_x, efi_y;
22
static u64 fb_base;
23
static bool fb_wb;
24
static void *efi_fb;
25
26
/*
27
* EFI earlycon needs to use early_memremap() to map the framebuffer.
28
* But early_memremap() is not usable for 'earlycon=efifb keep_bootcon',
29
* memremap() should be used instead. memremap() will be available after
30
* paging_init() which is earlier than initcall callbacks. Thus adding this
31
* early initcall function early_efi_map_fb() to map the whole EFI framebuffer.
32
*/
33
static int __init efi_earlycon_remap_fb(void)
34
{
35
const struct screen_info *si = &sysfb_primary_display.screen;
36
37
/* bail if there is no bootconsole or it was unregistered already */
38
if (!earlycon_console || !console_is_registered(earlycon_console))
39
return 0;
40
41
efi_fb = memremap(fb_base, si->lfb_size, fb_wb ? MEMREMAP_WB : MEMREMAP_WC);
42
43
return efi_fb ? 0 : -ENOMEM;
44
}
45
early_initcall(efi_earlycon_remap_fb);
46
47
static int __init efi_earlycon_unmap_fb(void)
48
{
49
/* unmap the bootconsole fb unless keep_bootcon left it registered */
50
if (efi_fb && !console_is_registered(earlycon_console))
51
memunmap(efi_fb);
52
return 0;
53
}
54
late_initcall(efi_earlycon_unmap_fb);
55
56
static __ref void *efi_earlycon_map(unsigned long start, unsigned long len)
57
{
58
pgprot_t fb_prot;
59
60
if (efi_fb)
61
return efi_fb + start;
62
63
fb_prot = fb_wb ? PAGE_KERNEL : pgprot_writecombine(PAGE_KERNEL);
64
return early_memremap_prot(fb_base + start, len, pgprot_val(fb_prot));
65
}
66
67
static __ref void efi_earlycon_unmap(void *addr, unsigned long len)
68
{
69
if (efi_fb)
70
return;
71
72
early_memunmap(addr, len);
73
}
74
75
static void efi_earlycon_clear_scanline(unsigned int y, const struct screen_info *si)
76
{
77
unsigned long *dst;
78
u16 len;
79
80
len = si->lfb_linelength;
81
dst = efi_earlycon_map(y*len, len);
82
if (!dst)
83
return;
84
85
memset(dst, 0, len);
86
efi_earlycon_unmap(dst, len);
87
}
88
89
static void efi_earlycon_scroll_up(const struct screen_info *si)
90
{
91
unsigned long *dst, *src;
92
u16 maxlen = 0;
93
u16 len;
94
u32 i, height;
95
96
/* Find the cached maximum x coordinate */
97
for (i = 0; i < max_line_y; i++) {
98
if (efi_x_array[i] > maxlen)
99
maxlen = efi_x_array[i];
100
}
101
maxlen *= 4;
102
103
len = si->lfb_linelength;
104
height = si->lfb_height;
105
106
for (i = 0; i < height - font->height; i++) {
107
dst = efi_earlycon_map(i*len, len);
108
if (!dst)
109
return;
110
111
src = efi_earlycon_map((i + font->height) * len, len);
112
if (!src) {
113
efi_earlycon_unmap(dst, len);
114
return;
115
}
116
117
memmove(dst, src, maxlen);
118
119
efi_earlycon_unmap(src, len);
120
efi_earlycon_unmap(dst, len);
121
}
122
}
123
124
static void efi_earlycon_write_char(u32 *dst, unsigned char c, unsigned int h,
125
const struct screen_info *si)
126
{
127
const u32 color_black = 0x00000000;
128
const u32 color_white = 0x00ffffff;
129
const u8 *src;
130
int m, n, bytes;
131
u8 x;
132
133
bytes = BITS_TO_BYTES(font->width);
134
src = font->data + c * font->height * bytes + h * bytes;
135
136
for (m = 0; m < font->width; m++) {
137
n = m % 8;
138
x = *(src + m / 8);
139
if ((x >> (7 - n)) & 1)
140
*dst = color_white;
141
else
142
*dst = color_black;
143
dst++;
144
}
145
}
146
147
static void
148
efi_earlycon_write(struct console *con, const char *str, unsigned int num)
149
{
150
const struct screen_info *si = &sysfb_primary_display.screen;
151
u32 cur_efi_x = efi_x;
152
unsigned int len;
153
const char *s;
154
void *dst;
155
156
len = si->lfb_linelength;
157
158
while (num) {
159
unsigned int linemax = (si->lfb_width - efi_x) / font->width;
160
unsigned int h, count;
161
162
count = strnchrnul(str, num, '\n') - str;
163
if (count > linemax)
164
count = linemax;
165
166
for (h = 0; h < font->height; h++) {
167
unsigned int n, x;
168
169
dst = efi_earlycon_map((efi_y + h) * len, len);
170
if (!dst)
171
return;
172
173
s = str;
174
n = count;
175
x = efi_x;
176
177
while (n-- > 0) {
178
efi_earlycon_write_char(dst + x * 4, *s, h, si);
179
x += font->width;
180
s++;
181
}
182
183
efi_earlycon_unmap(dst, len);
184
}
185
186
num -= count;
187
efi_x += count * font->width;
188
str += count;
189
190
if (num > 0 && *s == '\n') {
191
cur_efi_x = efi_x;
192
efi_x = 0;
193
efi_y += font->height;
194
str++;
195
num--;
196
}
197
198
if (efi_x + font->width > si->lfb_width) {
199
cur_efi_x = efi_x;
200
efi_x = 0;
201
efi_y += font->height;
202
}
203
204
if (efi_y + font->height > si->lfb_height) {
205
u32 i;
206
207
efi_x_array[cur_line_y] = cur_efi_x;
208
cur_line_y = (cur_line_y + 1) % max_line_y;
209
210
efi_y -= font->height;
211
efi_earlycon_scroll_up(si);
212
213
for (i = 0; i < font->height; i++)
214
efi_earlycon_clear_scanline(efi_y + i, si);
215
}
216
}
217
}
218
219
static bool __initdata fb_probed;
220
221
void __init efi_earlycon_reprobe(void)
222
{
223
if (fb_probed)
224
setup_earlycon("efifb");
225
}
226
227
static int __init efi_earlycon_setup(struct earlycon_device *device,
228
const char *opt)
229
{
230
const struct screen_info *si = &sysfb_primary_display.screen;
231
u16 xres, yres;
232
u32 i;
233
234
fb_wb = opt && !strcmp(opt, "ram");
235
236
if (si->orig_video_isVGA != VIDEO_TYPE_EFI) {
237
fb_probed = true;
238
return -ENODEV;
239
}
240
241
fb_base = si->lfb_base;
242
if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)
243
fb_base |= (u64)si->ext_lfb_base << 32;
244
245
xres = si->lfb_width;
246
yres = si->lfb_height;
247
248
/*
249
* efi_earlycon_write_char() implicitly assumes a framebuffer with
250
* 32 bits per pixel.
251
*/
252
if (si->lfb_depth != 32)
253
return -ENODEV;
254
255
font = get_default_font(xres, yres, NULL, NULL);
256
if (!font)
257
return -ENODEV;
258
259
/* Fill the cache with maximum possible value of x coordinate */
260
memset32(efi_x_array, rounddown(xres, font->width), ARRAY_SIZE(efi_x_array));
261
efi_y = rounddown(yres, font->height);
262
263
/* Make sure we have cache for the x coordinate for the full screen */
264
max_line_y = efi_y / font->height + 1;
265
cur_line_y = 0;
266
267
efi_y -= font->height;
268
for (i = 0; i < (yres - efi_y) / font->height; i++)
269
efi_earlycon_scroll_up(si);
270
271
device->con->write = efi_earlycon_write;
272
earlycon_console = device->con;
273
return 0;
274
}
275
EARLYCON_DECLARE(efifb, efi_earlycon_setup);
276
277