blob: 7e76a4d8304bc5add30e5f86d16e4f5b423a24f6 [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
4 *
5 * Copyright (C) 1999 VA Linux Systems
6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7 * Copyright (C) 1999-2002 Hewlett-Packard Co.
8 * David Mosberger-Tang <davidm@hpl.hp.com>
9 * Stephane Eranian <eranian@hpl.hp.com>
10 * Copyright (C) 2005-2008 Intel Co.
11 * Fenghua Yu <fenghua.yu@intel.com>
12 * Bibo Mao <bibo.mao@intel.com>
13 * Chandramouli Narayanan <mouli@linux.intel.com>
14 * Huang Ying <ying.huang@intel.com>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010015 * Copyright (C) 2013 SuSE Labs
16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
Huang, Ying5b836832008-01-30 13:31:19 +010017 *
18 * Copied from efi_32.c to eliminate the duplicated code between EFI
19 * 32/64 support code. --ying 2007-10-26
20 *
21 * All EFI Runtime Services are not implemented yet as EFI only
22 * supports physical mode addressing on SoftSDV. This is to be fixed
23 * in a future version. --drummond 1999-07-20
24 *
25 * Implemented EFI runtime services and virtual mode calls. --davidm
26 *
27 * Goutham Rao: <goutham.rao@intel.com>
28 * Skip non-WB memory and ignore empty memory ranges.
29 */
30
Olof Johanssone3cb3f52012-02-12 13:24:26 -080031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Huang, Ying5b836832008-01-30 13:31:19 +010033#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/efi.h>
Josh Triplett2223af32012-09-28 17:57:05 -070036#include <linux/efi-bgrt.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040037#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010038#include <linux/bootmem.h>
David Howells0d01ff22013-04-11 23:51:01 +010039#include <linux/slab.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070040#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010041#include <linux/spinlock.h>
42#include <linux/uaccess.h>
43#include <linux/time.h>
44#include <linux/io.h>
45#include <linux/reboot.h>
46#include <linux/bcd.h>
47
48#include <asm/setup.h>
49#include <asm/efi.h>
Ingo Molnar5520b7e2017-01-27 11:59:46 +010050#include <asm/e820/api.h>
Huang, Ying5b836832008-01-30 13:31:19 +010051#include <asm/time.h>
Laura Abbottd1163652017-05-08 15:58:11 -070052#include <asm/set_memory.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010053#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080054#include <asm/x86_init.h>
Borislav Petkova5d90c92014-03-04 17:02:17 +010055#include <asm/uv/uv.h>
Huang, Ying5b836832008-01-30 13:31:19 +010056
Harvey Harrisonecaea422008-02-13 13:26:13 -080057static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010058static efi_system_table_t efi_systab __initdata;
59
Joe Perches9b7d2042014-01-03 16:08:48 -080060static efi_config_table_type_t arch_tables[] __initdata = {
Leif Lindholm272686b2013-09-05 11:34:54 +010061#ifdef CONFIG_X86_UV
62 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
63#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010064 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010065};
66
Dave Young1fec0532013-12-20 18:02:19 +080067u64 efi_setup; /* efi setup_data physical address */
Dave Young926172d2013-12-20 18:02:18 +080068
Mathias Krause60920682014-09-07 19:42:15 +020069static int add_efi_memmap __initdata;
Paul Jackson200001e2008-06-25 05:44:46 -070070static int __init setup_add_efi_memmap(char *arg)
71{
72 add_efi_memmap = 1;
73 return 0;
74}
75early_param("add_efi_memmap", setup_add_efi_memmap);
76
Huang, Ying5b836832008-01-30 13:31:19 +010077static efi_status_t __init phys_efi_set_virtual_address_map(
78 unsigned long memory_map_size,
79 unsigned long descriptor_size,
80 u32 descriptor_version,
81 efi_memory_desc_t *virtual_map)
82{
83 efi_status_t status;
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010084 unsigned long flags;
Ingo Molnar744937b2015-03-03 07:48:50 +010085 pgd_t *save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +010086
Ingo Molnar744937b2015-03-03 07:48:50 +010087 save_pgd = efi_call_phys_prolog();
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010088
89 /* Disable interrupts around EFI calls: */
90 local_irq_save(flags);
Matt Fleming62fa6e62014-03-27 15:10:39 -070091 status = efi_call_phys(efi_phys.set_virtual_address_map,
92 memory_map_size, descriptor_size,
93 descriptor_version, virtual_map);
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010094 local_irq_restore(flags);
95
Ingo Molnar744937b2015-03-03 07:48:50 +010096 efi_call_phys_epilog(save_pgd);
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010097
Huang, Ying5b836832008-01-30 13:31:19 +010098 return status;
99}
100
Tony Luckb05b9f52015-06-24 16:58:15 -0700101void __init efi_find_mirror(void)
102{
Matt Fleming78ce2482016-04-25 21:06:38 +0100103 efi_memory_desc_t *md;
Tony Luckb05b9f52015-06-24 16:58:15 -0700104 u64 mirror_size = 0, total_size = 0;
105
Matt Fleming78ce2482016-04-25 21:06:38 +0100106 for_each_efi_memory_desc(md) {
Tony Luckb05b9f52015-06-24 16:58:15 -0700107 unsigned long long start = md->phys_addr;
108 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
109
110 total_size += size;
111 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
112 memblock_mark_mirror(start, size);
113 mirror_size += size;
114 }
115 }
116 if (mirror_size)
117 pr_info("Memory: %lldM/%lldM mirrored memory\n",
118 mirror_size>>20, total_size>>20);
119}
120
Paul Jackson69c91892008-05-14 08:15:58 -0700121/*
122 * Tell the kernel about the EFI memory map. This might include
123 * more than the max 128 entries that can fit in the e820 legacy
124 * (zeropage) memory map.
125 */
126
Paul Jackson200001e2008-06-25 05:44:46 -0700127static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700128{
Matt Fleming78ce2482016-04-25 21:06:38 +0100129 efi_memory_desc_t *md;
Paul Jackson69c91892008-05-14 08:15:58 -0700130
Matt Fleming78ce2482016-04-25 21:06:38 +0100131 for_each_efi_memory_desc(md) {
Paul Jackson69c91892008-05-14 08:15:58 -0700132 unsigned long long start = md->phys_addr;
133 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
134 int e820_type;
135
Cliff Wickmane2a71472009-06-16 16:43:40 -0500136 switch (md->type) {
137 case EFI_LOADER_CODE:
138 case EFI_LOADER_DATA:
139 case EFI_BOOT_SERVICES_CODE:
140 case EFI_BOOT_SERVICES_DATA:
141 case EFI_CONVENTIONAL_MEMORY:
142 if (md->attribute & EFI_MEMORY_WB)
Ingo Molnar09821ff2017-01-28 17:09:33 +0100143 e820_type = E820_TYPE_RAM;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500144 else
Ingo Molnar09821ff2017-01-28 17:09:33 +0100145 e820_type = E820_TYPE_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500146 break;
147 case EFI_ACPI_RECLAIM_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100148 e820_type = E820_TYPE_ACPI;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500149 break;
150 case EFI_ACPI_MEMORY_NVS:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100151 e820_type = E820_TYPE_NVS;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500152 break;
153 case EFI_UNUSABLE_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100154 e820_type = E820_TYPE_UNUSABLE;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500155 break;
Dan Williamsad5fb872015-04-03 12:05:28 -0400156 case EFI_PERSISTENT_MEMORY:
Ingo Molnar09821ff2017-01-28 17:09:33 +0100157 e820_type = E820_TYPE_PMEM;
Dan Williamsad5fb872015-04-03 12:05:28 -0400158 break;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500159 default:
160 /*
161 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
162 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
163 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
164 */
Ingo Molnar09821ff2017-01-28 17:09:33 +0100165 e820_type = E820_TYPE_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500166 break;
167 }
Ingo Molnarab6bc042017-01-28 14:19:36 +0100168 e820__range_add(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700169 }
Ingo Molnarf9748fa2017-01-28 18:00:35 +0100170 e820__update_table(e820_table);
Paul Jackson69c91892008-05-14 08:15:58 -0700171}
172
Olof Johansson1adbfa32012-02-12 13:24:29 -0800173int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800174{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100175 struct efi_info *e = &boot_params.efi_info;
Matt Fleming9479c7c2016-02-26 21:22:05 +0000176 struct efi_memory_map_data data;
Ard Biesheuvel44511fb2015-10-23 11:48:16 +0200177 phys_addr_t pmap;
Matt Fleming9479c7c2016-02-26 21:22:05 +0000178 int rv;
Huang, Yingecacf092008-06-02 14:26:21 +0800179
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200180 if (efi_enabled(EFI_PARAVIRT))
181 return 0;
182
Paul Jackson05486fa2008-06-22 07:22:02 -0700183#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800184 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100185 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800186 pr_err("Memory map is above 4GB, disabling EFI.\n");
187 return -EINVAL;
188 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100189 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700190#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100191 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800192#endif
Matt Fleming9479c7c2016-02-26 21:22:05 +0000193 data.phys_map = pmap;
194 data.size = e->efi_memmap_size;
195 data.desc_size = e->efi_memdesc_size;
196 data.desc_version = e->efi_memdesc_version;
197
198 rv = efi_memmap_init_early(&data);
199 if (rv)
200 return rv;
201
202 if (add_efi_memmap)
203 do_add_efi_memmap();
Borislav Petkov15b9c352013-03-01 17:07:44 +0100204
Ard Biesheuvel0d054ad2016-04-25 21:06:40 +0100205 WARN(efi.memmap.desc_version != 1,
206 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
207 efi.memmap.desc_version);
208
Matt Fleming884f4f62016-04-25 21:06:39 +0100209 memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100210
Olof Johansson1adbfa32012-02-12 13:24:29 -0800211 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800212}
213
Peter Jones0100a3e2016-12-12 18:42:28 -0500214#define OVERFLOW_ADDR_SHIFT (64 - EFI_PAGE_SHIFT)
215#define OVERFLOW_ADDR_MASK (U64_MAX << OVERFLOW_ADDR_SHIFT)
216#define U64_HIGH_BIT (~(U64_MAX >> 1))
217
218static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
219{
220 u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
221 u64 end_hi = 0;
222 char buf[64];
223
224 if (md->num_pages == 0) {
225 end = 0;
226 } else if (md->num_pages > EFI_PAGES_MAX ||
227 EFI_PAGES_MAX - md->num_pages <
228 (md->phys_addr >> EFI_PAGE_SHIFT)) {
229 end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
230 >> OVERFLOW_ADDR_SHIFT;
231
232 if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
233 end_hi += 1;
234 } else {
235 return true;
236 }
237
238 pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
239
240 if (end_hi) {
241 pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
242 i, efi_md_typeattr_format(buf, sizeof(buf), md),
243 md->phys_addr, end_hi, end);
244 } else {
245 pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
246 i, efi_md_typeattr_format(buf, sizeof(buf), md),
247 md->phys_addr, end);
248 }
249 return false;
250}
251
252static void __init efi_clean_memmap(void)
253{
254 efi_memory_desc_t *out = efi.memmap.map;
255 const efi_memory_desc_t *in = out;
256 const efi_memory_desc_t *end = efi.memmap.map_end;
257 int i, n_removal;
258
259 for (i = n_removal = 0; in < end; i++) {
260 if (efi_memmap_entry_valid(in, i)) {
261 if (out != in)
262 memcpy(out, in, efi.memmap.desc_size);
263 out = (void *)out + efi.memmap.desc_size;
264 } else {
265 n_removal++;
266 }
267 in = (void *)in + efi.memmap.desc_size;
268 }
269
270 if (n_removal > 0) {
271 u64 size = efi.memmap.nr_map - n_removal;
272
273 pr_warn("Removing %d invalid memory map entries.\n", n_removal);
274 efi_memmap_install(efi.memmap.phys_map, size);
275 }
276}
277
Taku Izumi0bbea1c2015-09-30 19:20:00 +0900278void __init efi_print_memmap(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100279{
280 efi_memory_desc_t *md;
Matt Fleming78ce2482016-04-25 21:06:38 +0100281 int i = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100282
Matt Fleming78ce2482016-04-25 21:06:38 +0100283 for_each_efi_memory_desc(md) {
Laszlo Ersekace1d122014-09-03 13:32:21 +0200284 char buf[64];
285
Robert Elliott1e82b942016-02-01 22:07:05 +0000286 pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
Matt Fleming78ce2482016-04-25 21:06:38 +0100287 i++, efi_md_typeattr_format(buf, sizeof(buf), md),
Laszlo Ersekace1d122014-09-03 13:32:21 +0200288 md->phys_addr,
Robert Elliott1e82b942016-02-01 22:07:05 +0000289 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
Huang, Ying5b836832008-01-30 13:31:19 +0100290 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
291 }
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100292}
Huang, Ying5b836832008-01-30 13:31:19 +0100293
Olof Johansson140bf272012-02-12 13:24:28 -0800294static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100295{
Matt Fleming83e68182012-11-14 09:42:35 +0000296 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800297 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800298 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800299 u64 tmp = 0;
300
Dave Young1fec0532013-12-20 18:02:19 +0800301 if (efi_setup) {
302 data = early_memremap(efi_setup, sizeof(*data));
303 if (!data)
304 return -ENOMEM;
305 }
Daniel Kiperabc93f82014-06-30 19:52:56 +0200306 systab64 = early_memremap((unsigned long)phys,
Olof Johansson1adbfa32012-02-12 13:24:29 -0800307 sizeof(*systab64));
308 if (systab64 == NULL) {
309 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800310 if (data)
Daniel Kiperabc93f82014-06-30 19:52:56 +0200311 early_memunmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800312 return -ENOMEM;
313 }
314
315 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800316 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
317 systab64->fw_vendor;
318 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800319 efi_systab.fw_revision = systab64->fw_revision;
320 efi_systab.con_in_handle = systab64->con_in_handle;
321 tmp |= systab64->con_in_handle;
322 efi_systab.con_in = systab64->con_in;
323 tmp |= systab64->con_in;
324 efi_systab.con_out_handle = systab64->con_out_handle;
325 tmp |= systab64->con_out_handle;
326 efi_systab.con_out = systab64->con_out;
327 tmp |= systab64->con_out;
328 efi_systab.stderr_handle = systab64->stderr_handle;
329 tmp |= systab64->stderr_handle;
330 efi_systab.stderr = systab64->stderr;
331 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800332 efi_systab.runtime = data ?
333 (void *)(unsigned long)data->runtime :
334 (void *)(unsigned long)systab64->runtime;
335 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800336 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
337 tmp |= systab64->boottime;
338 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800339 efi_systab.tables = data ? (unsigned long)data->tables :
340 systab64->tables;
341 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800342
Daniel Kiperabc93f82014-06-30 19:52:56 +0200343 early_memunmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800344 if (data)
Daniel Kiperabc93f82014-06-30 19:52:56 +0200345 early_memunmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800346#ifdef CONFIG_X86_32
347 if (tmp >> 32) {
348 pr_err("EFI data located above 4GB, disabling EFI.\n");
349 return -EINVAL;
350 }
351#endif
352 } else {
353 efi_system_table_32_t *systab32;
354
Daniel Kiperabc93f82014-06-30 19:52:56 +0200355 systab32 = early_memremap((unsigned long)phys,
Olof Johansson1adbfa32012-02-12 13:24:29 -0800356 sizeof(*systab32));
357 if (systab32 == NULL) {
358 pr_err("Couldn't map the system table!\n");
359 return -ENOMEM;
360 }
361
362 efi_systab.hdr = systab32->hdr;
363 efi_systab.fw_vendor = systab32->fw_vendor;
364 efi_systab.fw_revision = systab32->fw_revision;
365 efi_systab.con_in_handle = systab32->con_in_handle;
366 efi_systab.con_in = systab32->con_in;
367 efi_systab.con_out_handle = systab32->con_out_handle;
368 efi_systab.con_out = systab32->con_out;
369 efi_systab.stderr_handle = systab32->stderr_handle;
370 efi_systab.stderr = systab32->stderr;
371 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
372 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
373 efi_systab.nr_tables = systab32->nr_tables;
374 efi_systab.tables = systab32->tables;
375
Daniel Kiperabc93f82014-06-30 19:52:56 +0200376 early_memunmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800377 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800378
Huang, Ying5b836832008-01-30 13:31:19 +0100379 efi.systab = &efi_systab;
380
381 /*
382 * Verify the EFI Table
383 */
Olof Johansson140bf272012-02-12 13:24:28 -0800384 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800385 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800386 return -EINVAL;
387 }
Huang, Ying5b836832008-01-30 13:31:19 +0100388 if ((efi.systab->hdr.revision >> 16) == 0)
Joe Perches9b7d2042014-01-03 16:08:48 -0800389 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100390 efi.systab->hdr.revision >> 16,
391 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800392
393 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800394}
Huang, Ying5b836832008-01-30 13:31:19 +0100395
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000396static int __init efi_runtime_init32(void)
397{
398 efi_runtime_services_32_t *runtime;
399
Daniel Kiperabc93f82014-06-30 19:52:56 +0200400 runtime = early_memremap((unsigned long)efi.systab->runtime,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000401 sizeof(efi_runtime_services_32_t));
402 if (!runtime) {
403 pr_err("Could not map the runtime service table!\n");
404 return -ENOMEM;
405 }
406
407 /*
Mathias Krause0ce46052014-09-07 19:42:16 +0200408 * We will only need *early* access to the SetVirtualAddressMap
409 * EFI runtime service. All other runtime services will be called
410 * via the virtual mapping.
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000411 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000412 efi_phys.set_virtual_address_map =
413 (efi_set_virtual_address_map_t *)
414 (unsigned long)runtime->set_virtual_address_map;
Daniel Kiperabc93f82014-06-30 19:52:56 +0200415 early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000416
417 return 0;
418}
419
420static int __init efi_runtime_init64(void)
421{
422 efi_runtime_services_64_t *runtime;
423
Daniel Kiperabc93f82014-06-30 19:52:56 +0200424 runtime = early_memremap((unsigned long)efi.systab->runtime,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000425 sizeof(efi_runtime_services_64_t));
426 if (!runtime) {
427 pr_err("Could not map the runtime service table!\n");
428 return -ENOMEM;
429 }
430
431 /*
Mathias Krause0ce46052014-09-07 19:42:16 +0200432 * We will only need *early* access to the SetVirtualAddressMap
433 * EFI runtime service. All other runtime services will be called
434 * via the virtual mapping.
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000435 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000436 efi_phys.set_virtual_address_map =
437 (efi_set_virtual_address_map_t *)
438 (unsigned long)runtime->set_virtual_address_map;
Daniel Kiperabc93f82014-06-30 19:52:56 +0200439 early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000440
441 return 0;
442}
443
Olof Johansson140bf272012-02-12 13:24:28 -0800444static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800445{
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000446 int rv;
Huang, Ying5b836832008-01-30 13:31:19 +0100447
448 /*
449 * Check out the runtime services table. We need to map
450 * the runtime services table so that we can grab the physical
451 * address of several of the EFI runtime functions, needed to
452 * set the firmware into virtual mode.
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200453 *
454 * When EFI_PARAVIRT is in force then we could not map runtime
455 * service memory region because we do not have direct access to it.
456 * However, runtime services are available through proxy functions
457 * (e.g. in case of Xen dom0 EFI implementation they call special
458 * hypercall which executes relevant EFI functions) and that is why
459 * they are always enabled.
Huang, Ying5b836832008-01-30 13:31:19 +0100460 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000461
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200462 if (!efi_enabled(EFI_PARAVIRT)) {
463 if (efi_enabled(EFI_64BIT))
464 rv = efi_runtime_init64();
465 else
466 rv = efi_runtime_init32();
467
468 if (rv)
469 return rv;
470 }
Olof Johansson140bf272012-02-12 13:24:28 -0800471
Matt Fleming0f8093a2014-01-15 13:36:33 +0000472 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
473
Olof Johansson140bf272012-02-12 13:24:28 -0800474 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800475}
Huang, Ying5b836832008-01-30 13:31:19 +0100476
Olof Johansson83e7ee62012-02-12 13:24:25 -0800477void __init efi_init(void)
478{
479 efi_char16_t *c16;
480 char vendor[100] = "unknown";
481 int i = 0;
482 void *tmp;
483
484#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800485 if (boot_params.efi_info.efi_systab_hi ||
486 boot_params.efi_info.efi_memmap_hi) {
487 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800488 return;
489 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800490 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
491#else
492 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800493 (boot_params.efi_info.efi_systab |
494 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800495#endif
496
Matt Fleming83e68182012-11-14 09:42:35 +0000497 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800498 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000499
Dave Younga0998eb2013-12-20 18:02:17 +0800500 efi.config_table = (unsigned long)efi.systab->tables;
501 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
502 efi.runtime = (unsigned long)efi.systab->runtime;
503
Olof Johansson83e7ee62012-02-12 13:24:25 -0800504 /*
505 * Show what we know for posterity
506 */
Daniel Kiperabc93f82014-06-30 19:52:56 +0200507 c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800508 if (c16) {
509 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
510 vendor[i] = *c16++;
511 vendor[i] = '\0';
512 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800513 pr_err("Could not map the firmware vendor!\n");
Daniel Kiperabc93f82014-06-30 19:52:56 +0200514 early_memunmap(tmp, 2);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800515
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800516 pr_info("EFI v%u.%.02u by %s\n",
517 efi.systab->hdr.revision >> 16,
518 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800519
Dave Young1fec0532013-12-20 18:02:19 +0800520 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
521 return;
522
Leif Lindholm272686b2013-09-05 11:34:54 +0100523 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800524 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000525
Olof Johansson1adbfa32012-02-12 13:24:29 -0800526 /*
527 * Note: We currently don't support runtime services on an EFI
528 * that doesn't match the kernel 32/64-bit mode.
529 */
530
Matt Fleming7d453ee2014-01-10 18:52:06 +0000531 if (!efi_runtime_supported())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800532 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000533 else {
Matt Fleming9479c7c2016-02-26 21:22:05 +0000534 if (efi_runtime_disabled() || efi_runtime_init()) {
535 efi_memmap_unmap();
Matt Fleming83e68182012-11-14 09:42:35 +0000536 return;
Matt Fleming9479c7c2016-02-26 21:22:05 +0000537 }
Olof Johansson140bf272012-02-12 13:24:28 -0800538 }
Matt Fleming83e68182012-11-14 09:42:35 +0000539
Peter Jones0100a3e2016-12-12 18:42:28 -0500540 efi_clean_memmap();
541
Borislav Petkovfed6cef2015-02-05 11:44:41 +0100542 if (efi_enabled(EFI_DBG))
Taku Izumi0bbea1c2015-09-30 19:20:00 +0900543 efi_print_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100544}
545
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400546void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
547{
548 u64 addr, npages;
549
550 addr = md->virt_addr;
551 npages = md->num_pages;
552
553 memrange_efi_to_native(&addr, &npages);
554
555 if (executable)
556 set_memory_x(addr, npages);
557 else
558 set_memory_nx(addr, npages);
559}
560
Borislav Petkovc55d0162014-02-14 08:24:24 +0100561void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100562{
563 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100564
Huang, Yinga2172e22008-01-30 13:33:55 +0100565 /* Make EFI runtime service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +0100566 for_each_efi_memory_desc(md) {
Huang, Ying1c083eb2008-02-04 16:48:06 +0100567 if (md->type != EFI_RUNTIME_SERVICES_CODE)
568 continue;
569
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400570 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100571 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100572}
Huang, Yinga2172e22008-01-30 13:33:55 +0100573
Mathias Krause4e78eb052014-09-07 19:42:17 +0200574void __init efi_memory_uc(u64 addr, unsigned long size)
Matt Fleming3e8fa262012-10-19 13:25:46 +0100575{
576 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
577 u64 npages;
578
579 npages = round_up(size, page_shift) / page_shift;
580 memrange_efi_to_native(&addr, &npages);
581 set_memory_uc(addr, npages);
582}
583
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100584void __init old_map_region(efi_memory_desc_t *md)
585{
586 u64 start_pfn, end_pfn, end;
587 unsigned long size;
588 void *va;
589
590 start_pfn = PFN_DOWN(md->phys_addr);
591 size = md->num_pages << PAGE_SHIFT;
592 end = md->phys_addr + size;
593 end_pfn = PFN_UP(end);
594
595 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
596 va = __va(md->phys_addr);
597
598 if (!(md->attribute & EFI_MEMORY_WB))
599 efi_memory_uc((u64)(unsigned long)va, size);
600 } else
601 va = efi_ioremap(md->phys_addr, size,
602 md->type, md->attribute);
603
604 md->virt_addr = (u64) (unsigned long) va;
605 if (!va)
606 pr_err("ioremap of 0x%llX failed!\n",
607 (unsigned long long)md->phys_addr);
608}
609
Dave Young481f75c2013-12-20 18:02:16 +0800610/* Merge contiguous regions of the same type and attribute */
611static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100612{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400613 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100614
Matt Fleming78ce2482016-04-25 21:06:38 +0100615 for_each_efi_memory_desc(md) {
Matthew Garrett202f9d02011-05-05 15:19:44 -0400616 u64 prev_size;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400617
618 if (!prev_md) {
619 prev_md = md;
620 continue;
621 }
622
623 if (prev_md->type != md->type ||
624 prev_md->attribute != md->attribute) {
625 prev_md = md;
626 continue;
627 }
628
629 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
630
631 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
632 prev_md->num_pages += md->num_pages;
633 md->type = EFI_RESERVED_TYPE;
634 md->attribute = 0;
635 continue;
636 }
637 prev_md = md;
638 }
Dave Young481f75c2013-12-20 18:02:16 +0800639}
640
641static void __init get_systab_virt_addr(efi_memory_desc_t *md)
642{
643 unsigned long size;
644 u64 end, systab;
645
646 size = md->num_pages << EFI_PAGE_SHIFT;
647 end = md->phys_addr + size;
648 systab = (u64)(unsigned long)efi_phys.systab;
649 if (md->phys_addr <= systab && systab < end) {
650 systab += md->virt_addr - md->phys_addr;
651 efi.systab = (efi_system_table_t *)(unsigned long)systab;
652 }
653}
654
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100655static void *realloc_pages(void *old_memmap, int old_shift)
Dave Young481f75c2013-12-20 18:02:16 +0800656{
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100657 void *ret;
658
659 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
660 if (!ret)
661 goto out;
662
663 /*
664 * A first-time allocation doesn't have anything to copy.
665 */
666 if (!old_memmap)
667 return ret;
668
669 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
670
671out:
672 free_pages((unsigned long)old_memmap, old_shift);
673 return ret;
674}
675
676/*
Matt Fleminga5caa202015-09-25 23:02:18 +0100677 * Iterate the EFI memory map in reverse order because the regions
678 * will be mapped top-down. The end result is the same as if we had
679 * mapped things forward, but doesn't require us to change the
680 * existing implementation of efi_map_region().
681 */
682static inline void *efi_map_next_entry_reverse(void *entry)
683{
684 /* Initial call */
685 if (!entry)
Matt Fleming884f4f62016-04-25 21:06:39 +0100686 return efi.memmap.map_end - efi.memmap.desc_size;
Matt Fleminga5caa202015-09-25 23:02:18 +0100687
Matt Fleming884f4f62016-04-25 21:06:39 +0100688 entry -= efi.memmap.desc_size;
689 if (entry < efi.memmap.map)
Matt Fleminga5caa202015-09-25 23:02:18 +0100690 return NULL;
691
692 return entry;
693}
694
695/*
696 * efi_map_next_entry - Return the next EFI memory map descriptor
697 * @entry: Previous EFI memory map descriptor
698 *
699 * This is a helper function to iterate over the EFI memory map, which
700 * we do in different orders depending on the current configuration.
701 *
702 * To begin traversing the memory map @entry must be %NULL.
703 *
704 * Returns %NULL when we reach the end of the memory map.
705 */
706static void *efi_map_next_entry(void *entry)
707{
708 if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
709 /*
710 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
711 * config table feature requires us to map all entries
712 * in the same order as they appear in the EFI memory
713 * map. That is to say, entry N must have a lower
714 * virtual address than entry N+1. This is because the
715 * firmware toolchain leaves relative references in
716 * the code/data sections, which are split and become
717 * separate EFI memory regions. Mapping things
718 * out-of-order leads to the firmware accessing
719 * unmapped addresses.
720 *
721 * Since we need to map things this way whether or not
722 * the kernel actually makes use of
723 * EFI_PROPERTIES_TABLE, let's just switch to this
724 * scheme by default for 64-bit.
725 */
726 return efi_map_next_entry_reverse(entry);
727 }
728
729 /* Initial call */
730 if (!entry)
Matt Fleming884f4f62016-04-25 21:06:39 +0100731 return efi.memmap.map;
Matt Fleminga5caa202015-09-25 23:02:18 +0100732
Matt Fleming884f4f62016-04-25 21:06:39 +0100733 entry += efi.memmap.desc_size;
734 if (entry >= efi.memmap.map_end)
Matt Fleminga5caa202015-09-25 23:02:18 +0100735 return NULL;
736
737 return entry;
738}
739
Matt Flemingab72a272016-06-20 14:36:51 +0100740static bool should_map_region(efi_memory_desc_t *md)
741{
742 /*
743 * Runtime regions always require runtime mappings (obviously).
744 */
745 if (md->attribute & EFI_MEMORY_RUNTIME)
746 return true;
747
748 /*
749 * 32-bit EFI doesn't suffer from the bug that requires us to
750 * reserve boot services regions, and mixed mode support
751 * doesn't exist for 32-bit kernels.
752 */
753 if (IS_ENABLED(CONFIG_X86_32))
754 return false;
755
756 /*
757 * Map all of RAM so that we can access arguments in the 1:1
758 * mapping when making EFI runtime calls.
759 */
760 if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_is_native()) {
761 if (md->type == EFI_CONVENTIONAL_MEMORY ||
762 md->type == EFI_LOADER_DATA ||
763 md->type == EFI_LOADER_CODE)
764 return true;
765 }
766
767 /*
768 * Map boot services regions as a workaround for buggy
769 * firmware that accesses them even when they shouldn't.
770 *
771 * See efi_{reserve,free}_boot_services().
772 */
773 if (md->type == EFI_BOOT_SERVICES_CODE ||
774 md->type == EFI_BOOT_SERVICES_DATA)
775 return true;
776
777 return false;
778}
779
Matt Fleminga5caa202015-09-25 23:02:18 +0100780/*
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100781 * Map the efi memory ranges of the runtime services and update new_mmap with
782 * virtual addresses.
783 */
784static void * __init efi_map_regions(int *count, int *pg_shift)
785{
786 void *p, *new_memmap = NULL;
787 unsigned long left = 0;
Matt Fleming884f4f62016-04-25 21:06:39 +0100788 unsigned long desc_size;
Dave Young481f75c2013-12-20 18:02:16 +0800789 efi_memory_desc_t *md;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400790
Matt Fleming884f4f62016-04-25 21:06:39 +0100791 desc_size = efi.memmap.desc_size;
792
Matt Fleminga5caa202015-09-25 23:02:18 +0100793 p = NULL;
794 while ((p = efi_map_next_entry(p))) {
Huang, Ying5b836832008-01-30 13:31:19 +0100795 md = p;
Matt Flemingab72a272016-06-20 14:36:51 +0100796
797 if (!should_map_region(md))
798 continue;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100799
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100800 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800801 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100802
Matt Fleming884f4f62016-04-25 21:06:39 +0100803 if (left < desc_size) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100804 new_memmap = realloc_pages(new_memmap, *pg_shift);
805 if (!new_memmap)
806 return NULL;
807
808 left += PAGE_SIZE << *pg_shift;
809 (*pg_shift)++;
810 }
811
Matt Fleming884f4f62016-04-25 21:06:39 +0100812 memcpy(new_memmap + (*count * desc_size), md, desc_size);
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100813
Matt Fleming884f4f62016-04-25 21:06:39 +0100814 left -= desc_size;
Dave Young481f75c2013-12-20 18:02:16 +0800815 (*count)++;
816 }
817
818 return new_memmap;
Dave Young481f75c2013-12-20 18:02:16 +0800819}
820
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100821static void __init kexec_enter_virtual_mode(void)
822{
Dave Young2965faa2015-09-09 15:38:55 -0700823#ifdef CONFIG_KEXEC_CORE
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100824 efi_memory_desc_t *md;
Matt Fleming753b11e2016-01-21 14:11:59 +0000825 unsigned int num_pages;
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100826
827 efi.systab = NULL;
828
829 /*
830 * We don't do virtual mode, since we don't do runtime services, on
831 * non-native EFI
832 */
833 if (!efi_is_native()) {
Matt Fleming9479c7c2016-02-26 21:22:05 +0000834 efi_memmap_unmap();
Dave Younga5a750a2014-08-14 17:15:31 +0800835 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100836 return;
837 }
838
Matt Fleming753b11e2016-01-21 14:11:59 +0000839 if (efi_alloc_page_tables()) {
840 pr_err("Failed to allocate EFI page tables\n");
841 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
842 return;
843 }
844
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100845 /*
846 * Map efi regions which were passed via setup_data. The virt_addr is a
847 * fixed addr which was used in first kernel of a kexec boot.
848 */
Matt Fleming78ce2482016-04-25 21:06:38 +0100849 for_each_efi_memory_desc(md) {
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100850 efi_map_region_fixed(md); /* FIXME: add error handling */
851 get_systab_virt_addr(md);
852 }
853
Matt Flemingdca0f972016-02-27 15:52:50 +0000854 /*
855 * Unregister the early EFI memmap from efi_init() and install
856 * the new EFI memory map.
857 */
858 efi_memmap_unmap();
859
860 if (efi_memmap_init_late(efi.memmap.phys_map,
861 efi.memmap.desc_size * efi.memmap.nr_map)) {
862 pr_err("Failed to remap late EFI memory map\n");
863 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
864 return;
865 }
866
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100867 BUG_ON(!efi.systab);
868
Matt Fleming884f4f62016-04-25 21:06:39 +0100869 num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
Matt Fleming753b11e2016-01-21 14:11:59 +0000870 num_pages >>= PAGE_SHIFT;
871
Matt Fleming884f4f62016-04-25 21:06:39 +0100872 if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
Matt Fleming753b11e2016-01-21 14:11:59 +0000873 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
874 return;
875 }
876
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100877 efi_sync_low_kernel_mappings();
878
879 /*
880 * Now that EFI is in virtual mode, update the function
881 * pointers in the runtime service table to the new virtual addresses.
882 *
883 * Call EFI services through wrapper functions.
884 */
885 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming994448f2014-03-05 18:15:37 +0000886
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200887 efi_native_runtime_setup();
Matt Fleming994448f2014-03-05 18:15:37 +0000888
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100889 efi.set_virtual_address_map = NULL;
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100890
891 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
892 runtime_code_page_mkexec();
893
894 /* clean DUMMY object */
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700895 efi_delete_dummy_variable();
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100896#endif
897}
898
Dave Young481f75c2013-12-20 18:02:16 +0800899/*
900 * This function will switch the EFI runtime services to virtual mode.
901 * Essentially, we look through the EFI memmap and map every region that
902 * has the runtime attribute bit set in its memory descriptor into the
Matt Fleming67a91082015-11-27 21:09:34 +0000903 * efi_pgd page table.
Dave Young481f75c2013-12-20 18:02:16 +0800904 *
905 * The old method which used to update that memory descriptor with the
906 * virtual address obtained from ioremap() is still supported when the
907 * kernel is booted with efi=old_map on its command line. Same old
908 * method enabled the runtime services to be called without having to
909 * thunk back into physical mode for every invocation.
910 *
911 * The new method does a pagetable switch in a preemption-safe manner
912 * so that we're in a different address space when calling a runtime
Matt Fleming67a91082015-11-27 21:09:34 +0000913 * function. For function arguments passing we do copy the PUDs of the
914 * kernel page table into efi_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +0800915 *
916 * Specially for kexec boot, efi runtime maps in previous kernel should
917 * be passed in via setup_data. In that case runtime ranges will be mapped
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100918 * to the same virtual addresses as the first kernel, see
919 * kexec_enter_virtual_mode().
Dave Young481f75c2013-12-20 18:02:16 +0800920 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100921static void __init __efi_enter_virtual_mode(void)
Dave Young481f75c2013-12-20 18:02:16 +0800922{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100923 int count = 0, pg_shift = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800924 void *new_memmap = NULL;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100925 efi_status_t status;
Borislav Petkov02e56902016-11-12 21:04:23 +0000926 unsigned long pa;
Dave Young481f75c2013-12-20 18:02:16 +0800927
928 efi.systab = NULL;
929
Matt Fleming67a91082015-11-27 21:09:34 +0000930 if (efi_alloc_page_tables()) {
931 pr_err("Failed to allocate EFI page tables\n");
932 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
933 return;
934 }
935
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100936 efi_merge_regions();
937 new_memmap = efi_map_regions(&count, &pg_shift);
938 if (!new_memmap) {
939 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
Dave Younga5a750a2014-08-14 17:15:31 +0800940 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100941 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100942 }
Dave Young926172d2013-12-20 18:02:18 +0800943
Matt Flemingdca0f972016-02-27 15:52:50 +0000944 pa = __pa(new_memmap);
945
946 /*
947 * Unregister the early EFI memmap from efi_init() and install
948 * the new EFI memory map that we are about to pass to the
949 * firmware via SetVirtualAddressMap().
950 */
951 efi_memmap_unmap();
952
953 if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
954 pr_err("Failed to remap late EFI memory map\n");
955 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
956 return;
957 }
958
Dave Young22c091d2017-01-31 13:21:41 +0000959 if (efi_enabled(EFI_DBG)) {
960 pr_info("EFI runtime memory map:\n");
961 efi_print_memmap();
962 }
963
Huang, Ying5b836832008-01-30 13:31:19 +0100964 BUG_ON(!efi.systab);
965
Matt Flemingdca0f972016-02-27 15:52:50 +0000966 if (efi_setup_page_tables(pa, 1 << pg_shift)) {
Dave Younga5a750a2014-08-14 17:15:31 +0800967 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100968 return;
Dave Younga5a750a2014-08-14 17:15:31 +0800969 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100970
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100971 efi_sync_low_kernel_mappings();
972
Matt Fleming994448f2014-03-05 18:15:37 +0000973 if (efi_is_native()) {
974 status = phys_efi_set_virtual_address_map(
Matt Fleming884f4f62016-04-25 21:06:39 +0100975 efi.memmap.desc_size * count,
976 efi.memmap.desc_size,
977 efi.memmap.desc_version,
Matt Flemingdca0f972016-02-27 15:52:50 +0000978 (efi_memory_desc_t *)pa);
Matt Fleming994448f2014-03-05 18:15:37 +0000979 } else {
980 status = efi_thunk_set_virtual_address_map(
981 efi_phys.set_virtual_address_map,
Matt Fleming884f4f62016-04-25 21:06:39 +0100982 efi.memmap.desc_size * count,
983 efi.memmap.desc_size,
984 efi.memmap.desc_version,
Matt Flemingdca0f972016-02-27 15:52:50 +0000985 (efi_memory_desc_t *)pa);
Matt Fleming994448f2014-03-05 18:15:37 +0000986 }
Huang, Ying5b836832008-01-30 13:31:19 +0100987
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100988 if (status != EFI_SUCCESS) {
989 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
990 status);
991 panic("EFI call to SetVirtualAddressMap() failed!");
Huang, Ying5b836832008-01-30 13:31:19 +0100992 }
993
994 /*
995 * Now that EFI is in virtual mode, update the function
996 * pointers in the runtime service table to the new virtual addresses.
997 *
998 * Call EFI services through wrapper functions.
999 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001000 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +00001001
1002 if (efi_is_native())
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +02001003 efi_native_runtime_setup();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +00001004 else
1005 efi_thunk_runtime_setup();
1006
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001007 efi.set_virtual_address_map = NULL;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001008
Sai Praneeth6d0cc882016-02-17 12:36:05 +00001009 /*
1010 * Apply more restrictive page table mapping attributes now that
1011 * SVAM() has been called and the firmware has performed all
1012 * necessary relocation fixups for the new virtual addresses.
1013 */
1014 efi_runtime_update_mappings();
1015 efi_dump_pagetable();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001016
Matthew Garrettf8b84042013-06-01 16:06:20 -04001017 /* clean DUMMY object */
Saurabh Tangrieeb9db02014-06-02 05:18:35 -07001018 efi_delete_dummy_variable();
Huang, Ying5b836832008-01-30 13:31:19 +01001019}
1020
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001021void __init efi_enter_virtual_mode(void)
1022{
Daniel Kiper9f27bc52014-06-30 19:52:58 +02001023 if (efi_enabled(EFI_PARAVIRT))
1024 return;
1025
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001026 if (efi_setup)
1027 kexec_enter_virtual_mode();
1028 else
1029 __efi_enter_virtual_mode();
1030}
1031
Huang, Ying5b836832008-01-30 13:31:19 +01001032/*
1033 * Convenience functions to obtain memory types and attributes
1034 */
1035u32 efi_mem_type(unsigned long phys_addr)
1036{
1037 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +01001038
Matt Fleming83e68182012-11-14 09:42:35 +00001039 if (!efi_enabled(EFI_MEMMAP))
1040 return 0;
1041
Matt Fleming78ce2482016-04-25 21:06:38 +01001042 for_each_efi_memory_desc(md) {
Huang, Ying5b836832008-01-30 13:31:19 +01001043 if ((md->phys_addr <= phys_addr) &&
1044 (phys_addr < (md->phys_addr +
1045 (md->num_pages << EFI_PAGE_SHIFT))))
1046 return md->type;
1047 }
1048 return 0;
1049}
1050
Dave Young5ae36832014-08-14 17:15:28 +08001051static int __init arch_parse_efi_cmdline(char *str)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001052{
Ricardo Neri9115c752015-07-15 19:36:03 -07001053 if (!str) {
1054 pr_warn("need at least one option\n");
1055 return -EINVAL;
1056 }
1057
Dave Young6ccc72b82014-08-14 17:15:27 +08001058 if (parse_option_str(str, "old_map"))
1059 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001060
1061 return 0;
1062}
Dave Young5ae36832014-08-14 17:15:28 +08001063early_param("efi", arch_parse_efi_cmdline);