efi-init.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. static int __init is_memory(efi_memory_desc_t *md)
  22. {
  23. if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
  24. return 1;
  25. return 0;
  26. }
  27. /*
  28. * Translate a EFI virtual address into a physical address: this is necessary,
  29. * as some data members of the EFI system table are virtually remapped after
  30. * SetVirtualAddressMap() has been called.
  31. */
  32. static phys_addr_t __init efi_to_phys(unsigned long addr)
  33. {
  34. efi_memory_desc_t *md;
  35. for_each_efi_memory_desc(md) {
  36. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  37. continue;
  38. if (md->virt_addr == 0)
  39. /* no virtual mapping has been installed by the stub */
  40. break;
  41. if (md->virt_addr <= addr &&
  42. (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
  43. return md->phys_addr + addr - md->virt_addr;
  44. }
  45. return addr;
  46. }
  47. extern __weak const efi_config_table_type_t efi_arch_tables[];
  48. static void __init init_screen_info(void)
  49. {
  50. if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
  51. memblock_is_map_memory(screen_info.lfb_base))
  52. memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
  53. }
  54. static int __init uefi_init(u64 efi_system_table)
  55. {
  56. efi_config_table_t *config_tables;
  57. efi_system_table_t *systab;
  58. size_t table_size;
  59. int retval;
  60. systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t));
  61. if (systab == NULL) {
  62. pr_warn("Unable to map EFI system table.\n");
  63. return -ENOMEM;
  64. }
  65. set_bit(EFI_BOOT, &efi.flags);
  66. if (IS_ENABLED(CONFIG_64BIT))
  67. set_bit(EFI_64BIT, &efi.flags);
  68. retval = efi_systab_check_header(&systab->hdr, 2);
  69. if (retval)
  70. goto out;
  71. efi.runtime = systab->runtime;
  72. efi.runtime_version = systab->hdr.revision;
  73. efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor));
  74. table_size = sizeof(efi_config_table_t) * systab->nr_tables;
  75. config_tables = early_memremap_ro(efi_to_phys(systab->tables),
  76. table_size);
  77. if (config_tables == NULL) {
  78. pr_warn("Unable to map EFI config table array.\n");
  79. retval = -ENOMEM;
  80. goto out;
  81. }
  82. retval = efi_config_parse_tables(config_tables, systab->nr_tables,
  83. efi_arch_tables);
  84. early_memunmap(config_tables, table_size);
  85. out:
  86. early_memunmap(systab, sizeof(efi_system_table_t));
  87. return retval;
  88. }
  89. /*
  90. * Return true for regions that can be used as System RAM.
  91. */
  92. static __init int is_usable_memory(efi_memory_desc_t *md)
  93. {
  94. switch (md->type) {
  95. case EFI_LOADER_CODE:
  96. case EFI_LOADER_DATA:
  97. case EFI_ACPI_RECLAIM_MEMORY:
  98. case EFI_BOOT_SERVICES_CODE:
  99. case EFI_BOOT_SERVICES_DATA:
  100. case EFI_CONVENTIONAL_MEMORY:
  101. case EFI_PERSISTENT_MEMORY:
  102. /*
  103. * Special purpose memory is 'soft reserved', which means it
  104. * is set aside initially, but can be hotplugged back in or
  105. * be assigned to the dax driver after boot.
  106. */
  107. if (efi_soft_reserve_enabled() &&
  108. (md->attribute & EFI_MEMORY_SP))
  109. return false;
  110. /*
  111. * According to the spec, these regions are no longer reserved
  112. * after calling ExitBootServices(). However, we can only use
  113. * them as System RAM if they can be mapped writeback cacheable.
  114. */
  115. return (md->attribute & EFI_MEMORY_WB);
  116. default:
  117. break;
  118. }
  119. return false;
  120. }
  121. static __init void reserve_regions(void)
  122. {
  123. efi_memory_desc_t *md;
  124. u64 paddr, npages, size;
  125. if (efi_enabled(EFI_DBG))
  126. pr_info("Processing EFI memory map:\n");
  127. /*
  128. * Discard memblocks discovered so far: if there are any at this
  129. * point, they originate from memory nodes in the DT, and UEFI
  130. * uses its own memory map instead.
  131. */
  132. memblock_dump_all();
  133. memblock_remove(0, PHYS_ADDR_MAX);
  134. for_each_efi_memory_desc(md) {
  135. paddr = md->phys_addr;
  136. npages = md->num_pages;
  137. if (efi_enabled(EFI_DBG)) {
  138. char buf[64];
  139. pr_info(" 0x%012llx-0x%012llx %s\n",
  140. paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
  141. efi_md_typeattr_format(buf, sizeof(buf), md));
  142. }
  143. memrange_efi_to_native(&paddr, &npages);
  144. size = npages << PAGE_SHIFT;
  145. if (is_memory(md)) {
  146. early_init_dt_add_memory_arch(paddr, size);
  147. if (!is_usable_memory(md))
  148. memblock_mark_nomap(paddr, size);
  149. /* keep ACPI reclaim memory intact for kexec etc. */
  150. if (md->type == EFI_ACPI_RECLAIM_MEMORY)
  151. memblock_reserve(paddr, size);
  152. }
  153. }
  154. }
  155. void __init efi_init(void)
  156. {
  157. struct efi_memory_map_data data;
  158. u64 efi_system_table;
  159. /* Grab UEFI information placed in FDT by stub */
  160. efi_system_table = efi_get_fdt_params(&data);
  161. if (!efi_system_table)
  162. return;
  163. if (efi_memmap_init_early(&data) < 0) {
  164. /*
  165. * If we are booting via UEFI, the UEFI memory map is the only
  166. * description of memory we have, so there is little point in
  167. * proceeding if we cannot access it.
  168. */
  169. panic("Unable to map EFI memory map.\n");
  170. }
  171. WARN(efi.memmap.desc_version != 1,
  172. "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
  173. efi.memmap.desc_version);
  174. if (uefi_init(efi_system_table) < 0) {
  175. efi_memmap_unmap();
  176. return;
  177. }
  178. reserve_regions();
  179. /*
  180. * For memblock manipulation, the cap should come after the memblock_add().
  181. * And now, memblock is fully populated, it is time to do capping.
  182. */
  183. early_init_dt_check_for_usable_mem_range();
  184. efi_find_mirror();
  185. efi_esrt_init();
  186. efi_mokvar_table_init();
  187. memblock_reserve(data.phys_map & PAGE_MASK,
  188. PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
  189. init_screen_info();
  190. }