efi-init.c 7.2 KB

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