arm-init.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Extensible Firmware Interface
  3. *
  4. * Based on Extensible Firmware Interface Specification version 2.4
  5. *
  6. * Copyright (C) 2013 - 2015 Linaro Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #define pr_fmt(fmt) "efi: " fmt
  14. #include <linux/efi.h>
  15. #include <linux/init.h>
  16. #include <linux/memblock.h>
  17. #include <linux/mm_types.h>
  18. #include <linux/of.h>
  19. #include <linux/of_fdt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/screen_info.h>
  22. #include <asm/efi.h>
  23. u64 efi_system_table;
  24. static int __init is_memory(efi_memory_desc_t *md)
  25. {
  26. if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
  27. return 1;
  28. return 0;
  29. }
  30. /*
  31. * Translate a EFI virtual address into a physical address: this is necessary,
  32. * as some data members of the EFI system table are virtually remapped after
  33. * SetVirtualAddressMap() has been called.
  34. */
  35. static phys_addr_t efi_to_phys(unsigned long addr)
  36. {
  37. efi_memory_desc_t *md;
  38. for_each_efi_memory_desc(md) {
  39. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  40. continue;
  41. if (md->virt_addr == 0)
  42. /* no virtual mapping has been installed by the stub */
  43. break;
  44. if (md->virt_addr <= addr &&
  45. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  46. return md->phys_addr + addr - md->virt_addr;
  47. }
  48. return addr;
  49. }
  50. static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
  51. static __initdata efi_config_table_type_t arch_tables[] = {
  52. {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, NULL, &screen_info_table},
  53. {NULL_GUID, NULL, NULL}
  54. };
  55. static void __init init_screen_info(void)
  56. {
  57. struct screen_info *si;
  58. if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
  59. si = early_memremap_ro(screen_info_table, sizeof(*si));
  60. if (!si) {
  61. pr_err("Could not map screen_info config table\n");
  62. return;
  63. }
  64. screen_info = *si;
  65. early_memunmap(si, sizeof(*si));
  66. /* dummycon on ARM needs non-zero values for columns/lines */
  67. screen_info.orig_video_cols = 80;
  68. screen_info.orig_video_lines = 25;
  69. }
  70. if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
  71. memblock_is_map_memory(screen_info.lfb_base))
  72. memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
  73. }
  74. static int __init uefi_init(void)
  75. {
  76. efi_char16_t *c16;
  77. void *config_tables;
  78. size_t table_size;
  79. char vendor[100] = "unknown";
  80. int i, retval;
  81. efi.systab = early_memremap_ro(efi_system_table,
  82. sizeof(efi_system_table_t));
  83. if (efi.systab == NULL) {
  84. pr_warn("Unable to map EFI system table.\n");
  85. return -ENOMEM;
  86. }
  87. set_bit(EFI_BOOT, &efi.flags);
  88. if (IS_ENABLED(CONFIG_64BIT))
  89. set_bit(EFI_64BIT, &efi.flags);
  90. /*
  91. * Verify the EFI Table
  92. */
  93. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  94. pr_err("System table signature incorrect\n");
  95. retval = -EINVAL;
  96. goto out;
  97. }
  98. if ((efi.systab->hdr.revision >> 16) < 2)
  99. pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
  100. efi.systab->hdr.revision >> 16,
  101. efi.systab->hdr.revision & 0xffff);
  102. efi.runtime_version = efi.systab->hdr.revision;
  103. /* Show what we know for posterity */
  104. c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
  105. sizeof(vendor) * sizeof(efi_char16_t));
  106. if (c16) {
  107. for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
  108. vendor[i] = c16[i];
  109. vendor[i] = '\0';
  110. early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
  111. }
  112. pr_info("EFI v%u.%.02u by %s\n",
  113. efi.systab->hdr.revision >> 16,
  114. efi.systab->hdr.revision & 0xffff, vendor);
  115. table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
  116. config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
  117. table_size);
  118. if (config_tables == NULL) {
  119. pr_warn("Unable to map EFI config table array.\n");
  120. retval = -ENOMEM;
  121. goto out;
  122. }
  123. retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
  124. sizeof(efi_config_table_t),
  125. arch_tables);
  126. early_memunmap(config_tables, table_size);
  127. out:
  128. early_memunmap(efi.systab, sizeof(efi_system_table_t));
  129. return retval;
  130. }
  131. /*
  132. * Return true for regions that can be used as System RAM.
  133. */
  134. static __init int is_usable_memory(efi_memory_desc_t *md)
  135. {
  136. switch (md->type) {
  137. case EFI_LOADER_CODE:
  138. case EFI_LOADER_DATA:
  139. case EFI_BOOT_SERVICES_CODE:
  140. case EFI_BOOT_SERVICES_DATA:
  141. case EFI_CONVENTIONAL_MEMORY:
  142. case EFI_PERSISTENT_MEMORY:
  143. /*
  144. * According to the spec, these regions are no longer reserved
  145. * after calling ExitBootServices(). However, we can only use
  146. * them as System RAM if they can be mapped writeback cacheable.
  147. */
  148. return (md->attribute & EFI_MEMORY_WB);
  149. default:
  150. break;
  151. }
  152. return false;
  153. }
  154. static __init void reserve_regions(void)
  155. {
  156. efi_memory_desc_t *md;
  157. u64 paddr, npages, size;
  158. if (efi_enabled(EFI_DBG))
  159. pr_info("Processing EFI memory map:\n");
  160. /*
  161. * Discard memblocks discovered so far: if there are any at this
  162. * point, they originate from memory nodes in the DT, and UEFI
  163. * uses its own memory map instead.
  164. */
  165. memblock_dump_all();
  166. memblock_remove(0, (phys_addr_t)ULLONG_MAX);
  167. for_each_efi_memory_desc(md) {
  168. paddr = md->phys_addr;
  169. npages = md->num_pages;
  170. if (efi_enabled(EFI_DBG)) {
  171. char buf[64];
  172. pr_info(" 0x%012llx-0x%012llx %s\n",
  173. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  174. efi_md_typeattr_format(buf, sizeof(buf), md));
  175. }
  176. memrange_efi_to_native(&paddr, &npages);
  177. size = npages << PAGE_SHIFT;
  178. if (is_memory(md)) {
  179. early_init_dt_add_memory_arch(paddr, size);
  180. if (!is_usable_memory(md))
  181. memblock_mark_nomap(paddr, size);
  182. }
  183. }
  184. }
  185. void __init efi_init(void)
  186. {
  187. struct efi_memory_map_data data;
  188. struct efi_fdt_params params;
  189. /* Grab UEFI information placed in FDT by stub */
  190. if (!efi_get_fdt_params(&params))
  191. return;
  192. efi_system_table = params.system_table;
  193. data.desc_version = params.desc_ver;
  194. data.desc_size = params.desc_size;
  195. data.size = params.mmap_size;
  196. data.phys_map = params.mmap;
  197. if (efi_memmap_init_early(&data) < 0) {
  198. /*
  199. * If we are booting via UEFI, the UEFI memory map is the only
  200. * description of memory we have, so there is little point in
  201. * proceeding if we cannot access it.
  202. */
  203. panic("Unable to map EFI memory map.\n");
  204. }
  205. WARN(efi.memmap.desc_version != 1,
  206. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  207. efi.memmap.desc_version);
  208. if (uefi_init() < 0) {
  209. efi_memmap_unmap();
  210. return;
  211. }
  212. reserve_regions();
  213. efi_memattr_init();
  214. efi_esrt_init();
  215. efi_memmap_unmap();
  216. memblock_reserve(params.mmap & PAGE_MASK,
  217. PAGE_ALIGN(params.mmap_size +
  218. (params.mmap & ~PAGE_MASK)));
  219. init_screen_info();
  220. }
  221. static int __init register_gop_device(void)
  222. {
  223. void *pd;
  224. if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  225. return 0;
  226. pd = platform_device_register_data(NULL, "efi-framebuffer", 0,
  227. &screen_info, sizeof(screen_info));
  228. return PTR_ERR_OR_ZERO(pd);
  229. }
  230. subsys_initcall(register_gop_device);