Merge tag 'kvm-x86-misc-6.9' of https://github.com/kvm-x86/linux into HEAD
[linux-2.6-block.git] / drivers / firmware / efi / efi-init.c
CommitLineData
4febfb8d 1// SPDX-License-Identifier: GPL-2.0
e5bc22a4
AB
2/*
3 * Extensible Firmware Interface
4 *
5 * Based on Extensible Firmware Interface Specification version 2.4
6 *
7 * Copyright (C) 2013 - 2015 Linaro Ltd.
e5bc22a4
AB
8 */
9
801820be
AB
10#define pr_fmt(fmt) "efi: " fmt
11
e5bc22a4 12#include <linux/efi.h>
64c8a0cd 13#include <linux/fwnode.h>
e5bc22a4
AB
14#include <linux/init.h>
15#include <linux/memblock.h>
16#include <linux/mm_types.h>
17#include <linux/of.h>
64c8a0cd 18#include <linux/of_address.h>
e5bc22a4 19#include <linux/of_fdt.h>
e3271c96 20#include <linux/platform_device.h>
801820be 21#include <linux/screen_info.h>
e5bc22a4
AB
22
23#include <asm/efi.h>
24
732ea9db
AB
25unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
26
cb82cce7 27static int __init is_memory(efi_memory_desc_t *md)
e5bc22a4 28{
cb82cce7 29 if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
e5bc22a4
AB
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 */
8819ba39 39static phys_addr_t __init efi_to_phys(unsigned long addr)
e5bc22a4
AB
40{
41 efi_memory_desc_t *md;
42
884f4f66 43 for_each_efi_memory_desc(md) {
e5bc22a4
AB
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
69e377b2 56extern __weak const efi_config_table_type_t efi_arch_tables[];
801820be 57
b8466fe8
AB
58/*
59 * x86 defines its own screen_info and uses it even without EFI,
60 * everything else can get it from here.
61 */
62#if !defined(CONFIG_X86) && (defined(CONFIG_SYSFB) || defined(CONFIG_EFI_EARLYCON))
63struct screen_info screen_info __section(".data");
64EXPORT_SYMBOL_GPL(screen_info);
65#endif
66
801820be
AB
67static void __init init_screen_info(void)
68{
732ea9db
AB
69 struct screen_info *si;
70
71 if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
72 si = early_memremap(screen_info_table, sizeof(*si));
73 if (!si) {
74 pr_err("Could not map screen_info config table\n");
75 return;
76 }
77 screen_info = *si;
78 memset(si, 0, sizeof(*si));
79 early_memunmap(si, sizeof(*si));
80
81 if (memblock_is_map_memory(screen_info.lfb_base))
82 memblock_mark_nomap(screen_info.lfb_base,
83 screen_info.lfb_size);
8b3a149d
AB
84
85 if (IS_ENABLED(CONFIG_EFI_EARLYCON))
86 efi_earlycon_reprobe();
732ea9db 87 }
801820be
AB
88}
89
8819ba39 90static int __init uefi_init(u64 efi_system_table)
e5bc22a4 91{
06c0bd93 92 efi_config_table_t *config_tables;
8819ba39 93 efi_system_table_t *systab;
f7d92489 94 size_t table_size;
14fb4209 95 int retval;
e5bc22a4 96
8819ba39
AB
97 systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t));
98 if (systab == NULL) {
e5bc22a4
AB
99 pr_warn("Unable to map EFI system table.\n");
100 return -ENOMEM;
101 }
102
103 set_bit(EFI_BOOT, &efi.flags);
f7d92489
AB
104 if (IS_ENABLED(CONFIG_64BIT))
105 set_bit(EFI_64BIT, &efi.flags);
e5bc22a4 106
234fa51d 107 retval = efi_systab_check_header(&systab->hdr);
14fb4209 108 if (retval)
e5bc22a4 109 goto out;
e5bc22a4 110
8819ba39
AB
111 efi.runtime = systab->runtime;
112 efi.runtime_version = systab->hdr.revision;
e5bc22a4 113
8819ba39 114 efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor));
e5bc22a4 115
8819ba39
AB
116 table_size = sizeof(efi_config_table_t) * systab->nr_tables;
117 config_tables = early_memremap_ro(efi_to_phys(systab->tables),
2eec5ded 118 table_size);
e5bc22a4
AB
119 if (config_tables == NULL) {
120 pr_warn("Unable to map EFI config table array.\n");
121 retval = -ENOMEM;
122 goto out;
123 }
8819ba39 124 retval = efi_config_parse_tables(config_tables, systab->nr_tables,
69e377b2 125 efi_arch_tables);
e5bc22a4
AB
126
127 early_memunmap(config_tables, table_size);
128out:
8819ba39 129 early_memunmap(systab, sizeof(efi_system_table_t));
e5bc22a4
AB
130 return retval;
131}
132
133/*
cb82cce7 134 * Return true for regions that can be used as System RAM.
e5bc22a4 135 */
cb82cce7 136static __init int is_usable_memory(efi_memory_desc_t *md)
e5bc22a4
AB
137{
138 switch (md->type) {
139 case EFI_LOADER_CODE:
140 case EFI_LOADER_DATA:
f56ab9a5 141 case EFI_ACPI_RECLAIM_MEMORY:
e5bc22a4
AB
142 case EFI_BOOT_SERVICES_CODE:
143 case EFI_BOOT_SERVICES_DATA:
144 case EFI_CONVENTIONAL_MEMORY:
145 case EFI_PERSISTENT_MEMORY:
cb82cce7
AB
146 /*
147 * According to the spec, these regions are no longer reserved
148 * after calling ExitBootServices(). However, we can only use
149 * them as System RAM if they can be mapped writeback cacheable.
150 */
151 return (md->attribute & EFI_MEMORY_WB);
e5bc22a4
AB
152 default:
153 break;
154 }
cb82cce7 155 return false;
e5bc22a4
AB
156}
157
158static __init void reserve_regions(void)
159{
160 efi_memory_desc_t *md;
161 u64 paddr, npages, size;
162
163 if (efi_enabled(EFI_DBG))
164 pr_info("Processing EFI memory map:\n");
165
500899c2
AB
166 /*
167 * Discard memblocks discovered so far: if there are any at this
168 * point, they originate from memory nodes in the DT, and UEFI
169 * uses its own memory map instead.
170 */
171 memblock_dump_all();
d7dc899a 172 memblock_remove(0, PHYS_ADDR_MAX);
500899c2 173
884f4f66 174 for_each_efi_memory_desc(md) {
e5bc22a4
AB
175 paddr = md->phys_addr;
176 npages = md->num_pages;
177
178 if (efi_enabled(EFI_DBG)) {
179 char buf[64];
180
cb82cce7 181 pr_info(" 0x%012llx-0x%012llx %s\n",
e5bc22a4 182 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
cb82cce7 183 efi_md_typeattr_format(buf, sizeof(buf), md));
e5bc22a4
AB
184 }
185
186 memrange_efi_to_native(&paddr, &npages);
187 size = npages << PAGE_SHIFT;
188
cb82cce7 189 if (is_memory(md)) {
0bcff59e
AB
190 /*
191 * Special purpose memory is 'soft reserved', which
192 * means it is set aside initially. Don't add a memblock
193 * for it now so that it can be hotplugged back in or
194 * be assigned to the dax driver after boot.
195 */
196 if (efi_soft_reserve_enabled() &&
197 (md->attribute & EFI_MEMORY_SP))
198 continue;
199
e5bc22a4
AB
200 early_init_dt_add_memory_arch(paddr, size);
201
cb82cce7
AB
202 if (!is_usable_memory(md))
203 memblock_mark_nomap(paddr, size);
f56ab9a5
AB
204
205 /* keep ACPI reclaim memory intact for kexec etc. */
206 if (md->type == EFI_ACPI_RECLAIM_MEMORY)
207 memblock_reserve(paddr, size);
cb82cce7 208 }
e5bc22a4 209 }
e5bc22a4
AB
210}
211
212void __init efi_init(void)
213{
9479c7ce 214 struct efi_memory_map_data data;
3b2e4b4c 215 u64 efi_system_table;
e5bc22a4
AB
216
217 /* Grab UEFI information placed in FDT by stub */
3b2e4b4c
AB
218 efi_system_table = efi_get_fdt_params(&data);
219 if (!efi_system_table)
e5bc22a4
AB
220 return;
221
9479c7ce 222 if (efi_memmap_init_early(&data) < 0) {
e5bc22a4
AB
223 /*
224 * If we are booting via UEFI, the UEFI memory map is the only
225 * description of memory we have, so there is little point in
226 * proceeding if we cannot access it.
227 */
228 panic("Unable to map EFI memory map.\n");
229 }
e5bc22a4 230
0d054ad9
AB
231 WARN(efi.memmap.desc_version != 1,
232 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
233 efi.memmap.desc_version);
234
3b2e4b4c 235 if (uefi_init(efi_system_table) < 0) {
0709a008 236 efi_memmap_unmap();
e5bc22a4 237 return;
0709a008 238 }
e5bc22a4
AB
239
240 reserve_regions();
b398123b
PL
241 /*
242 * For memblock manipulation, the cap should come after the memblock_add().
243 * And now, memblock is fully populated, it is time to do capping.
244 */
245 early_init_dt_check_for_usable_mem_range();
6365a193 246 efi_find_mirror();
2ead3084 247 efi_esrt_init();
58c90902 248 efi_mokvar_table_init();
7cc8cbcf 249
3b2e4b4c
AB
250 memblock_reserve(data.phys_map & PAGE_MASK,
251 PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
801820be 252
b8466fe8
AB
253 if (IS_ENABLED(CONFIG_X86) ||
254 IS_ENABLED(CONFIG_SYSFB) ||
255 IS_ENABLED(CONFIG_EFI_EARLYCON))
256 init_screen_info();
e5bc22a4 257}