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