earlycon.c 6.1 KB

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