efi-init.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Extensible Firmware Interface
  4. *
  5. * Based on Extensible Firmware Interface Specification version 2.4
  6. *
  7. * Copyright (C) 2013 - 2015 Linaro Ltd.
  8. */
  9. #define pr_fmt(fmt) "efi: " fmt
  10. #include <linux/efi.h>
  11. #include <linux/fwnode.h>
  12. #include <linux/init.h>
  13. #include <linux/memblock.h>
  14. #include <linux/mm_types.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/screen_info.h>
  20. #include <asm/efi.h>
  21. unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
  22. static int __init is_memory(efi_memory_desc_t *md)
  23. {
  24. if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
  25. return 1;
  26. return 0;
  27. }
  28. /*
  29. * Translate a EFI virtual address into a physical address: this is necessary,
  30. * as some data members of the EFI system table are virtually remapped after
  31. * SetVirtualAddressMap() has been called.
  32. */
  33. static phys_addr_t __init efi_to_phys(unsigned long addr)
  34. {
  35. efi_memory_desc_t *md;
  36. for_each_efi_memory_desc(md) {
  37. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  38. continue;
  39. if (md->virt_addr == 0)
  40. /* no virtual mapping has been installed by the stub */
  41. break;
  42. if (md->virt_addr <= addr &&
  43. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  44. return md->phys_addr + addr - md->virt_addr;
  45. }
  46. return addr;
  47. }
  48. extern __weak const efi_config_table_type_t efi_arch_tables[];
  49. /*
  50. * x86 defines its own screen_info and uses it even without EFI,
  51. * everything else can get it from here.
  52. */
  53. #if !defined(CONFIG_X86) && (defined(CONFIG_SYSFB) || defined(CONFIG_EFI_EARLYCON))
  54. struct screen_info screen_info __section(".data");
  55. EXPORT_SYMBOL_GPL(screen_info);
  56. #endif
  57. static void __init init_screen_info(void)
  58. {
  59. struct screen_info *si;
  60. if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
  61. si = early_memremap(screen_info_table, sizeof(*si));
  62. if (!si) {
  63. pr_err("Could not map screen_info config table\n");
  64. return;
  65. }
  66. screen_info = *si;
  67. memset(si, 0, sizeof(*si));
  68. early_memunmap(si, sizeof(*si));
  69. if (memblock_is_map_memory(screen_info.lfb_base))
  70. memblock_mark_nomap(screen_info.lfb_base,
  71. screen_info.lfb_size);
  72. if (IS_ENABLED(CONFIG_EFI_EARLYCON))
  73. efi_earlycon_reprobe();
  74. }
  75. }
  76. static int __init uefi_init(u64 efi_system_table)
  77. {
  78. efi_config_table_t *config_tables;
  79. efi_system_table_t *systab;
  80. size_t table_size;
  81. int retval;
  82. systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t));
  83. if (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. retval = efi_systab_check_header(&systab->hdr);
  91. if (retval)
  92. goto out;
  93. efi.runtime = systab->runtime;
  94. efi.runtime_version = systab->hdr.revision;
  95. efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor));
  96. table_size = sizeof(efi_config_table_t) * systab->nr_tables;
  97. config_tables = early_memremap_ro(efi_to_phys(systab->tables),
  98. table_size);
  99. if (config_tables == NULL) {
  100. pr_warn("Unable to map EFI config table array.\n");
  101. retval = -ENOMEM;
  102. goto out;
  103. }
  104. retval = efi_config_parse_tables(config_tables, systab->nr_tables,
  105. efi_arch_tables);
  106. early_memunmap(config_tables, table_size);
  107. out:
  108. early_memunmap(systab, sizeof(efi_system_table_t));
  109. return retval;
  110. }
  111. /*
  112. * Return true for regions that can be used as System RAM.
  113. */
  114. static __init int is_usable_memory(efi_memory_desc_t *md)
  115. {
  116. switch (md->type) {
  117. case EFI_LOADER_CODE:
  118. case EFI_LOADER_DATA:
  119. case EFI_ACPI_RECLAIM_MEMORY:
  120. case EFI_BOOT_SERVICES_CODE:
  121. case EFI_BOOT_SERVICES_DATA:
  122. case EFI_CONVENTIONAL_MEMORY:
  123. case EFI_PERSISTENT_MEMORY:
  124. /*
  125. * According to the spec, these regions are no longer reserved
  126. * after calling ExitBootServices(). However, we can only use
  127. * them as System RAM if they can be mapped writeback cacheable.
  128. */
  129. return (md->attribute & EFI_MEMORY_WB);
  130. default:
  131. break;
  132. }
  133. return false;
  134. }
  135. static __init void reserve_regions(void)
  136. {
  137. efi_memory_desc_t *md;
  138. u64 paddr, npages, size;
  139. if (efi_enabled(EFI_DBG))
  140. pr_info("Processing EFI memory map:\n");
  141. /*
  142. * Discard memblocks discovered so far: if there are any at this
  143. * point, they originate from memory nodes in the DT, and UEFI
  144. * uses its own memory map instead.
  145. */
  146. memblock_dump_all();
  147. memblock_remove(0, PHYS_ADDR_MAX);
  148. for_each_efi_memory_desc(md) {
  149. paddr = md->phys_addr;
  150. npages = md->num_pages;
  151. if (efi_enabled(EFI_DBG)) {
  152. char buf[64];
  153. pr_info(" 0x%012llx-0x%012llx %s\n",
  154. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  155. efi_md_typeattr_format(buf, sizeof(buf), md));
  156. }
  157. memrange_efi_to_native(&paddr, &npages);
  158. size = npages << PAGE_SHIFT;
  159. if (is_memory(md)) {
  160. /*
  161. * Special purpose memory is 'soft reserved', which
  162. * means it is set aside initially. Don't add a memblock
  163. * for it now so that it can be hotplugged back in or
  164. * be assigned to the dax driver after boot.
  165. */
  166. if (efi_soft_reserve_enabled() &&
  167. (md->attribute & EFI_MEMORY_SP))
  168. continue;
  169. early_init_dt_add_memory_arch(paddr, size);
  170. if (!is_usable_memory(md))
  171. memblock_mark_nomap(paddr, size);
  172. /* keep ACPI reclaim memory intact for kexec etc. */
  173. if (md->type == EFI_ACPI_RECLAIM_MEMORY)
  174. memblock_reserve(paddr, size);
  175. }
  176. }
  177. }
  178. void __init efi_init(void)
  179. {
  180. struct efi_memory_map_data data;
  181. u64 efi_system_table;
  182. /* Grab UEFI information placed in FDT by stub */
  183. efi_system_table = efi_get_fdt_params(&data);
  184. if (!efi_system_table)
  185. return;
  186. if (efi_memmap_init_early(&data) < 0) {
  187. /*
  188. * If we are booting via UEFI, the UEFI memory map is the only
  189. * description of memory we have, so there is little point in
  190. * proceeding if we cannot access it.
  191. */
  192. panic("Unable to map EFI memory map.\n");
  193. }
  194. WARN(efi.memmap.desc_version != 1,
  195. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  196. efi.memmap.desc_version);
  197. if (uefi_init(efi_system_table) < 0) {
  198. efi_memmap_unmap();
  199. return;
  200. }
  201. reserve_regions();
  202. /*
  203. * For memblock manipulation, the cap should come after the memblock_add().
  204. * And now, memblock is fully populated, it is time to do capping.
  205. */
  206. early_init_dt_check_for_usable_mem_range();
  207. efi_find_mirror();
  208. efi_esrt_init();
  209. efi_mokvar_table_init();
  210. memblock_reserve(data.phys_map & PAGE_MASK,
  211. PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
  212. if (IS_ENABLED(CONFIG_X86) ||
  213. IS_ENABLED(CONFIG_SYSFB) ||
  214. IS_ENABLED(CONFIG_EFI_EARLYCON))
  215. init_screen_info();
  216. }