early_printk.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #include <linux/console.h>
  8. #include <linux/efi.h>
  9. #include <linux/font.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <asm/setup.h>
  13. static const struct font_desc *font;
  14. static u32 efi_x, efi_y;
  15. static void *efi_fb;
  16. static bool early_efi_keep;
  17. /*
  18. * efi earlyprintk need use early_ioremap to map the framebuffer.
  19. * But early_ioremap is not usable for earlyprintk=efi,keep, ioremap should
  20. * be used instead. ioremap will be available after paging_init() which is
  21. * earlier than initcall callbacks. Thus adding this early initcall function
  22. * early_efi_map_fb to map the whole efi framebuffer.
  23. */
  24. static __init int early_efi_map_fb(void)
  25. {
  26. u64 base, size;
  27. if (!early_efi_keep)
  28. return 0;
  29. base = boot_params.screen_info.lfb_base;
  30. if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  31. base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
  32. size = boot_params.screen_info.lfb_size;
  33. efi_fb = ioremap(base, size);
  34. return efi_fb ? 0 : -ENOMEM;
  35. }
  36. early_initcall(early_efi_map_fb);
  37. /*
  38. * early_efi_map maps efi framebuffer region [start, start + len -1]
  39. * In case earlyprintk=efi,keep we have the whole framebuffer mapped already
  40. * so just return the offset efi_fb + start.
  41. */
  42. static __ref void *early_efi_map(unsigned long start, unsigned long len)
  43. {
  44. u64 base;
  45. base = boot_params.screen_info.lfb_base;
  46. if (boot_params.screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
  47. base |= (u64)boot_params.screen_info.ext_lfb_base << 32;
  48. if (efi_fb)
  49. return (efi_fb + start);
  50. else
  51. return early_ioremap(base + start, len);
  52. }
  53. static __ref void early_efi_unmap(void *addr, unsigned long len)
  54. {
  55. if (!efi_fb)
  56. early_iounmap(addr, len);
  57. }
  58. static void early_efi_clear_scanline(unsigned int y)
  59. {
  60. unsigned long *dst;
  61. u16 len;
  62. len = boot_params.screen_info.lfb_linelength;
  63. dst = early_efi_map(y*len, len);
  64. if (!dst)
  65. return;
  66. memset(dst, 0, len);
  67. early_efi_unmap(dst, len);
  68. }
  69. static void early_efi_scroll_up(void)
  70. {
  71. unsigned long *dst, *src;
  72. u16 len;
  73. u32 i, height;
  74. len = boot_params.screen_info.lfb_linelength;
  75. height = boot_params.screen_info.lfb_height;
  76. for (i = 0; i < height - font->height; i++) {
  77. dst = early_efi_map(i*len, len);
  78. if (!dst)
  79. return;
  80. src = early_efi_map((i + font->height) * len, len);
  81. if (!src) {
  82. early_efi_unmap(dst, len);
  83. return;
  84. }
  85. memmove(dst, src, len);
  86. early_efi_unmap(src, len);
  87. early_efi_unmap(dst, len);
  88. }
  89. }
  90. static void early_efi_write_char(u32 *dst, unsigned char c, unsigned int h)
  91. {
  92. const u32 color_black = 0x00000000;
  93. const u32 color_white = 0x00ffffff;
  94. const u8 *src;
  95. u8 s8;
  96. int m;
  97. src = font->data + c * font->height;
  98. s8 = *(src + h);
  99. for (m = 0; m < 8; m++) {
  100. if ((s8 >> (7 - m)) & 1)
  101. *dst = color_white;
  102. else
  103. *dst = color_black;
  104. dst++;
  105. }
  106. }
  107. static void
  108. early_efi_write(struct console *con, const char *str, unsigned int num)
  109. {
  110. struct screen_info *si;
  111. unsigned int len;
  112. const char *s;
  113. void *dst;
  114. si = &boot_params.screen_info;
  115. len = si->lfb_linelength;
  116. while (num) {
  117. unsigned int linemax;
  118. unsigned int h, count = 0;
  119. for (s = str; *s && *s != '\n'; s++) {
  120. if (count == num)
  121. break;
  122. count++;
  123. }
  124. linemax = (si->lfb_width - efi_x) / font->width;
  125. if (count > linemax)
  126. count = linemax;
  127. for (h = 0; h < font->height; h++) {
  128. unsigned int n, x;
  129. dst = early_efi_map((efi_y + h) * len, len);
  130. if (!dst)
  131. return;
  132. s = str;
  133. n = count;
  134. x = efi_x;
  135. while (n-- > 0) {
  136. early_efi_write_char(dst + x*4, *s, h);
  137. x += font->width;
  138. s++;
  139. }
  140. early_efi_unmap(dst, len);
  141. }
  142. num -= count;
  143. efi_x += count * font->width;
  144. str += count;
  145. if (num > 0 && *s == '\n') {
  146. efi_x = 0;
  147. efi_y += font->height;
  148. str++;
  149. num--;
  150. }
  151. if (efi_x + font->width > si->lfb_width) {
  152. efi_x = 0;
  153. efi_y += font->height;
  154. }
  155. if (efi_y + font->height > si->lfb_height) {
  156. u32 i;
  157. efi_y -= font->height;
  158. early_efi_scroll_up();
  159. for (i = 0; i < font->height; i++)
  160. early_efi_clear_scanline(efi_y + i);
  161. }
  162. }
  163. }
  164. static __init int early_efi_setup(struct console *con, char *options)
  165. {
  166. struct screen_info *si;
  167. u16 xres, yres;
  168. u32 i;
  169. si = &boot_params.screen_info;
  170. xres = si->lfb_width;
  171. yres = si->lfb_height;
  172. /*
  173. * early_efi_write_char() implicitly assumes a framebuffer with
  174. * 32-bits per pixel.
  175. */
  176. if (si->lfb_depth != 32)
  177. return -ENODEV;
  178. font = get_default_font(xres, yres, -1, -1);
  179. if (!font)
  180. return -ENODEV;
  181. efi_y = rounddown(yres, font->height) - font->height;
  182. for (i = 0; i < (yres - efi_y) / font->height; i++)
  183. early_efi_scroll_up();
  184. /* early_console_register will unset CON_BOOT in case ,keep */
  185. if (!(con->flags & CON_BOOT))
  186. early_efi_keep = true;
  187. return 0;
  188. }
  189. struct console early_efi_console = {
  190. .name = "earlyefi",
  191. .write = early_efi_write,
  192. .setup = early_efi_setup,
  193. .flags = CON_PRINTBUFFER,
  194. .index = -1,
  195. };