blob: 7138bc7a265c016359e372d8274f3a4f6f068215 [file] [log] [blame]
Matt Fleming72548e82013-10-04 09:36:56 +01001/*
2 * Copyright (C) 2013 Intel Corporation; author Matt Fleming
3 *
4 * This file is part of the Linux kernel, and is made available under
5 * the terms of the GNU General Public License version 2.
6 */
7
8#include <linux/console.h>
9#include <linux/efi.h>
10#include <linux/font.h>
11#include <linux/io.h>
12#include <linux/kernel.h>
13#include <asm/setup.h>
14
15static const struct font_desc *font;
16static u32 efi_x, efi_y;
Dave Young5f35eb02014-05-01 21:15:48 +080017static void *efi_fb;
18static bool early_efi_keep;
Matt Fleming72548e82013-10-04 09:36:56 +010019
Dave Young5f35eb02014-05-01 21:15:48 +080020/*
21 * efi earlyprintk need use early_ioremap to map the framebuffer.
22 * But early_ioremap is not usable for earlyprintk=efi,keep, ioremap should
23 * be used instead. ioremap will be available after paging_init() which is
24 * earlier than initcall callbacks. Thus adding this early initcall function
25 * early_efi_map_fb to map the whole efi framebuffer.
26 */
27static __init int early_efi_map_fb(void)
Matt Fleming72548e82013-10-04 09:36:56 +010028{
Aaron Mab8b39bf2018-09-13 02:00:08 +080029 u64 base, size;
Dave Young5f35eb02014-05-01 21:15:48 +080030
31 if (!early_efi_keep)
32 return 0;
Matt Fleming72548e82013-10-04 09:36:56 +010033
34 base = boot_params.screen_info.lfb_base;
Aaron Mab8b39bf2018-09-13 02:00:08 +080035 if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
36 base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
Dave Young5f35eb02014-05-01 21:15:48 +080037 size = boot_params.screen_info.lfb_size;
38 efi_fb = ioremap(base, size);
Matt Fleming72548e82013-10-04 09:36:56 +010039
Dave Young5f35eb02014-05-01 21:15:48 +080040 return efi_fb ? 0 : -ENOMEM;
41}
42early_initcall(early_efi_map_fb);
43
44/*
45 * early_efi_map maps efi framebuffer region [start, start + len -1]
46 * In case earlyprintk=efi,keep we have the whole framebuffer mapped already
47 * so just return the offset efi_fb + start.
48 */
Fabian Frederickbd721ea2016-08-02 14:03:33 -070049static __ref void *early_efi_map(unsigned long start, unsigned long len)
Dave Young5f35eb02014-05-01 21:15:48 +080050{
Aaron Mab8b39bf2018-09-13 02:00:08 +080051 u64 base;
Dave Young5f35eb02014-05-01 21:15:48 +080052
53 base = boot_params.screen_info.lfb_base;
Aaron Mab8b39bf2018-09-13 02:00:08 +080054 if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
55 base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
Dave Young5f35eb02014-05-01 21:15:48 +080056
57 if (efi_fb)
58 return (efi_fb + start);
59 else
60 return early_ioremap(base + start, len);
61}
62
Fabian Frederickbd721ea2016-08-02 14:03:33 -070063static __ref void early_efi_unmap(void *addr, unsigned long len)
Dave Young5f35eb02014-05-01 21:15:48 +080064{
65 if (!efi_fb)
66 early_iounmap(addr, len);
67}
68
69static void early_efi_clear_scanline(unsigned int y)
70{
71 unsigned long *dst;
72 u16 len;
73
74 len = boot_params.screen_info.lfb_linelength;
75 dst = early_efi_map(y*len, len);
Matt Fleming72548e82013-10-04 09:36:56 +010076 if (!dst)
77 return;
78
79 memset(dst, 0, len);
Dave Young5f35eb02014-05-01 21:15:48 +080080 early_efi_unmap(dst, len);
Matt Fleming72548e82013-10-04 09:36:56 +010081}
82
Dave Young5f35eb02014-05-01 21:15:48 +080083static void early_efi_scroll_up(void)
Matt Fleming72548e82013-10-04 09:36:56 +010084{
Dave Young5f35eb02014-05-01 21:15:48 +080085 unsigned long *dst, *src;
Matt Fleming72548e82013-10-04 09:36:56 +010086 u16 len;
87 u32 i, height;
88
Matt Fleming72548e82013-10-04 09:36:56 +010089 len = boot_params.screen_info.lfb_linelength;
90 height = boot_params.screen_info.lfb_height;
91
92 for (i = 0; i < height - font->height; i++) {
Dave Young5f35eb02014-05-01 21:15:48 +080093 dst = early_efi_map(i*len, len);
Matt Fleming72548e82013-10-04 09:36:56 +010094 if (!dst)
95 return;
96
Dave Young5f35eb02014-05-01 21:15:48 +080097 src = early_efi_map((i + font->height) * len, len);
Matt Fleming72548e82013-10-04 09:36:56 +010098 if (!src) {
Dave Young5f35eb02014-05-01 21:15:48 +080099 early_efi_unmap(dst, len);
Matt Fleming72548e82013-10-04 09:36:56 +0100100 return;
101 }
102
103 memmove(dst, src, len);
104
Dave Young5f35eb02014-05-01 21:15:48 +0800105 early_efi_unmap(src, len);
106 early_efi_unmap(dst, len);
Matt Fleming72548e82013-10-04 09:36:56 +0100107 }
108}
109
110static void early_efi_write_char(u32 *dst, unsigned char c, unsigned int h)
111{
112 const u32 color_black = 0x00000000;
113 const u32 color_white = 0x00ffffff;
114 const u8 *src;
115 u8 s8;
116 int m;
117
118 src = font->data + c * font->height;
119 s8 = *(src + h);
120
121 for (m = 0; m < 8; m++) {
122 if ((s8 >> (7 - m)) & 1)
123 *dst = color_white;
124 else
125 *dst = color_black;
126 dst++;
127 }
128}
129
Dave Young5f35eb02014-05-01 21:15:48 +0800130static void
Matt Fleming72548e82013-10-04 09:36:56 +0100131early_efi_write(struct console *con, const char *str, unsigned int num)
132{
133 struct screen_info *si;
Matt Fleming72548e82013-10-04 09:36:56 +0100134 unsigned int len;
135 const char *s;
136 void *dst;
137
Matt Fleming72548e82013-10-04 09:36:56 +0100138 si = &boot_params.screen_info;
139 len = si->lfb_linelength;
140
141 while (num) {
142 unsigned int linemax;
143 unsigned int h, count = 0;
144
145 for (s = str; *s && *s != '\n'; s++) {
146 if (count == num)
147 break;
148 count++;
149 }
150
151 linemax = (si->lfb_width - efi_x) / font->width;
152 if (count > linemax)
153 count = linemax;
154
155 for (h = 0; h < font->height; h++) {
156 unsigned int n, x;
157
Dave Young5f35eb02014-05-01 21:15:48 +0800158 dst = early_efi_map((efi_y + h) * len, len);
Matt Fleming72548e82013-10-04 09:36:56 +0100159 if (!dst)
160 return;
161
162 s = str;
163 n = count;
164 x = efi_x;
165
166 while (n-- > 0) {
167 early_efi_write_char(dst + x*4, *s, h);
168 x += font->width;
169 s++;
170 }
171
Dave Young5f35eb02014-05-01 21:15:48 +0800172 early_efi_unmap(dst, len);
Matt Fleming72548e82013-10-04 09:36:56 +0100173 }
174
175 num -= count;
176 efi_x += count * font->width;
177 str += count;
178
179 if (num > 0 && *s == '\n') {
180 efi_x = 0;
181 efi_y += font->height;
182 str++;
183 num--;
184 }
185
YiFei Zhu79c22062018-11-29 18:12:30 +0100186 if (efi_x + font->width > si->lfb_width) {
Matt Fleming72548e82013-10-04 09:36:56 +0100187 efi_x = 0;
188 efi_y += font->height;
189 }
190
Matt Fleming1f3a8ba2013-11-11 16:18:59 +0000191 if (efi_y + font->height > si->lfb_height) {
Matt Fleming72548e82013-10-04 09:36:56 +0100192 u32 i;
193
194 efi_y -= font->height;
195 early_efi_scroll_up();
196
197 for (i = 0; i < font->height; i++)
198 early_efi_clear_scanline(efi_y + i);
199 }
200 }
201}
202
203static __init int early_efi_setup(struct console *con, char *options)
204{
205 struct screen_info *si;
206 u16 xres, yres;
207 u32 i;
208
209 si = &boot_params.screen_info;
210 xres = si->lfb_width;
211 yres = si->lfb_height;
212
213 /*
214 * early_efi_write_char() implicitly assumes a framebuffer with
215 * 32-bits per pixel.
216 */
217 if (si->lfb_depth != 32)
218 return -ENODEV;
219
220 font = get_default_font(xres, yres, -1, -1);
221 if (!font)
222 return -ENODEV;
223
224 efi_y = rounddown(yres, font->height) - font->height;
225 for (i = 0; i < (yres - efi_y) / font->height; i++)
226 early_efi_scroll_up();
227
Dave Young5f35eb02014-05-01 21:15:48 +0800228 /* early_console_register will unset CON_BOOT in case ,keep */
229 if (!(con->flags & CON_BOOT))
230 early_efi_keep = true;
Matt Fleming72548e82013-10-04 09:36:56 +0100231 return 0;
232}
233
234struct console early_efi_console = {
235 .name = "earlyefi",
236 .write = early_efi_write,
237 .setup = early_efi_setup,
238 .flags = CON_PRINTBUFFER,
239 .index = -1,
240};