Merge tag 'trace-v4.21' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux-2.6-microblaze.git] / arch / x86 / platform / efi / early_printk.c
1 /*
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
15 static const struct font_desc *font;
16 static u32 efi_x, efi_y;
17 static void *efi_fb;
18 static bool early_efi_keep;
19
20 /*
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  */
27 static __init int early_efi_map_fb(void)
28 {
29         u64 base, size;
30
31         if (!early_efi_keep)
32                 return 0;
33
34         base = boot_params.screen_info.lfb_base;
35         if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
36                 base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
37         size = boot_params.screen_info.lfb_size;
38         efi_fb = ioremap(base, size);
39
40         return efi_fb ? 0 : -ENOMEM;
41 }
42 early_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  */
49 static __ref void *early_efi_map(unsigned long start, unsigned long len)
50 {
51         u64 base;
52
53         base = boot_params.screen_info.lfb_base;
54         if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
55                 base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
56
57         if (efi_fb)
58                 return (efi_fb + start);
59         else
60                 return early_ioremap(base + start, len);
61 }
62
63 static __ref void early_efi_unmap(void *addr, unsigned long len)
64 {
65         if (!efi_fb)
66                 early_iounmap(addr, len);
67 }
68
69 static 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);
76         if (!dst)
77                 return;
78
79         memset(dst, 0, len);
80         early_efi_unmap(dst, len);
81 }
82
83 static void early_efi_scroll_up(void)
84 {
85         unsigned long *dst, *src;
86         u16 len;
87         u32 i, height;
88
89         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++) {
93                 dst = early_efi_map(i*len, len);
94                 if (!dst)
95                         return;
96
97                 src = early_efi_map((i + font->height) * len, len);
98                 if (!src) {
99                         early_efi_unmap(dst, len);
100                         return;
101                 }
102
103                 memmove(dst, src, len);
104
105                 early_efi_unmap(src, len);
106                 early_efi_unmap(dst, len);
107         }
108 }
109
110 static 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
130 static void
131 early_efi_write(struct console *con, const char *str, unsigned int num)
132 {
133         struct screen_info *si;
134         unsigned int len;
135         const char *s;
136         void *dst;
137
138         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
158                         dst = early_efi_map((efi_y + h) * len, len);
159                         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
172                         early_efi_unmap(dst, len);
173                 }
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
186                 if (efi_x + font->width > si->lfb_width) {
187                         efi_x = 0;
188                         efi_y += font->height;
189                 }
190
191                 if (efi_y + font->height > si->lfb_height) {
192                         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
203 static __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
228         /* early_console_register will unset CON_BOOT in case ,keep */
229         if (!(con->flags & CON_BOOT))
230                 early_efi_keep = true;
231         return 0;
232 }
233
234 struct 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 };