1 // SPDX-License-Identifier: GPL-2.0
3 * Extensible Firmware Interface
5 * Based on Extensible Firmware Interface Specification version 2.4
7 * Copyright (C) 2013 - 2015 Linaro Ltd.
10 #define pr_fmt(fmt) "efi: " fmt
12 #include <linux/efi.h>
13 #include <linux/fwnode.h>
14 #include <linux/init.h>
15 #include <linux/memblock.h>
16 #include <linux/mm_types.h>
18 #include <linux/of_address.h>
19 #include <linux/of_fdt.h>
20 #include <linux/platform_device.h>
21 #include <linux/screen_info.h>
25 static int __init is_memory(efi_memory_desc_t *md)
27 if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
33 * Translate a EFI virtual address into a physical address: this is necessary,
34 * as some data members of the EFI system table are virtually remapped after
35 * SetVirtualAddressMap() has been called.
37 static phys_addr_t __init efi_to_phys(unsigned long addr)
39 efi_memory_desc_t *md;
41 for_each_efi_memory_desc(md) {
42 if (!(md->attribute & EFI_MEMORY_RUNTIME))
44 if (md->virt_addr == 0)
45 /* no virtual mapping has been installed by the stub */
47 if (md->virt_addr <= addr &&
48 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
49 return md->phys_addr + addr - md->virt_addr;
54 static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
55 static __initdata unsigned long cpu_state_table = EFI_INVALID_TABLE_ADDR;
57 static const efi_config_table_type_t arch_tables[] __initconst = {
58 {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, &screen_info_table},
59 {LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
63 static void __init init_screen_info(void)
65 struct screen_info *si;
67 if (IS_ENABLED(CONFIG_ARM) &&
68 screen_info_table != EFI_INVALID_TABLE_ADDR) {
69 si = early_memremap_ro(screen_info_table, sizeof(*si));
71 pr_err("Could not map screen_info config table\n");
75 early_memunmap(si, sizeof(*si));
77 /* dummycon on ARM needs non-zero values for columns/lines */
78 screen_info.orig_video_cols = 80;
79 screen_info.orig_video_lines = 25;
82 if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
83 memblock_is_map_memory(screen_info.lfb_base))
84 memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
87 static int __init uefi_init(u64 efi_system_table)
89 efi_config_table_t *config_tables;
90 efi_system_table_t *systab;
94 systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t));
96 pr_warn("Unable to map EFI system table.\n");
100 set_bit(EFI_BOOT, &efi.flags);
101 if (IS_ENABLED(CONFIG_64BIT))
102 set_bit(EFI_64BIT, &efi.flags);
104 retval = efi_systab_check_header(&systab->hdr, 2);
108 efi.runtime = systab->runtime;
109 efi.runtime_version = systab->hdr.revision;
111 efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor));
113 table_size = sizeof(efi_config_table_t) * systab->nr_tables;
114 config_tables = early_memremap_ro(efi_to_phys(systab->tables),
116 if (config_tables == NULL) {
117 pr_warn("Unable to map EFI config table array.\n");
121 retval = efi_config_parse_tables(config_tables, systab->nr_tables,
122 IS_ENABLED(CONFIG_ARM) ? arch_tables
125 early_memunmap(config_tables, table_size);
127 early_memunmap(systab, sizeof(efi_system_table_t));
132 * Return true for regions that can be used as System RAM.
134 static __init int is_usable_memory(efi_memory_desc_t *md)
137 case EFI_LOADER_CODE:
138 case EFI_LOADER_DATA:
139 case EFI_ACPI_RECLAIM_MEMORY:
140 case EFI_BOOT_SERVICES_CODE:
141 case EFI_BOOT_SERVICES_DATA:
142 case EFI_CONVENTIONAL_MEMORY:
143 case EFI_PERSISTENT_MEMORY:
145 * Special purpose memory is 'soft reserved', which means it
146 * is set aside initially, but can be hotplugged back in or
147 * be assigned to the dax driver after boot.
149 if (efi_soft_reserve_enabled() &&
150 (md->attribute & EFI_MEMORY_SP))
154 * According to the spec, these regions are no longer reserved
155 * after calling ExitBootServices(). However, we can only use
156 * them as System RAM if they can be mapped writeback cacheable.
158 return (md->attribute & EFI_MEMORY_WB);
165 static __init void reserve_regions(void)
167 efi_memory_desc_t *md;
168 u64 paddr, npages, size;
170 if (efi_enabled(EFI_DBG))
171 pr_info("Processing EFI memory map:\n");
174 * Discard memblocks discovered so far: if there are any at this
175 * point, they originate from memory nodes in the DT, and UEFI
176 * uses its own memory map instead.
179 memblock_remove(0, PHYS_ADDR_MAX);
181 for_each_efi_memory_desc(md) {
182 paddr = md->phys_addr;
183 npages = md->num_pages;
185 if (efi_enabled(EFI_DBG)) {
188 pr_info(" 0x%012llx-0x%012llx %s\n",
189 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
190 efi_md_typeattr_format(buf, sizeof(buf), md));
193 memrange_efi_to_native(&paddr, &npages);
194 size = npages << PAGE_SHIFT;
197 early_init_dt_add_memory_arch(paddr, size);
199 if (!is_usable_memory(md))
200 memblock_mark_nomap(paddr, size);
202 /* keep ACPI reclaim memory intact for kexec etc. */
203 if (md->type == EFI_ACPI_RECLAIM_MEMORY)
204 memblock_reserve(paddr, size);
209 void __init efi_init(void)
211 struct efi_memory_map_data data;
212 u64 efi_system_table;
214 /* Grab UEFI information placed in FDT by stub */
215 efi_system_table = efi_get_fdt_params(&data);
216 if (!efi_system_table)
219 if (efi_memmap_init_early(&data) < 0) {
221 * If we are booting via UEFI, the UEFI memory map is the only
222 * description of memory we have, so there is little point in
223 * proceeding if we cannot access it.
225 panic("Unable to map EFI memory map.\n");
228 WARN(efi.memmap.desc_version != 1,
229 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
230 efi.memmap.desc_version);
232 if (uefi_init(efi_system_table) < 0) {
239 * For memblock manipulation, the cap should come after the memblock_add().
240 * And now, memblock is fully populated, it is time to do capping.
242 early_init_dt_check_for_usable_mem_range();
245 efi_mokvar_table_init();
247 memblock_reserve(data.phys_map & PAGE_MASK,
248 PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
253 /* ARM does not permit early mappings to persist across paging_init() */
256 if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
257 struct efi_arm_entry_state *state;
258 bool dump_state = true;
260 state = early_memremap_ro(cpu_state_table,
261 sizeof(struct efi_arm_entry_state));
263 pr_warn("Unable to map CPU entry state table.\n");
267 if ((state->sctlr_before_ebs & 1) == 0)
268 pr_warn(FW_BUG "EFI stub was entered with MMU and Dcache disabled, please fix your firmware!\n");
269 else if ((state->sctlr_after_ebs & 1) == 0)
270 pr_warn(FW_BUG "ExitBootServices() returned with MMU and Dcache disabled, please fix your firmware!\n");
274 if (dump_state || efi_enabled(EFI_DBG)) {
275 pr_info("CPSR at EFI stub entry : 0x%08x\n", state->cpsr_before_ebs);
276 pr_info("SCTLR at EFI stub entry : 0x%08x\n", state->sctlr_before_ebs);
277 pr_info("CPSR after ExitBootServices() : 0x%08x\n", state->cpsr_after_ebs);
278 pr_info("SCTLR after ExitBootServices(): 0x%08x\n", state->sctlr_after_ebs);
280 early_memunmap(state, sizeof(struct efi_arm_entry_state));