blob: dde46cd78b8f35a821391b4b0c7934f9f67e5521 [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>
50#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010051#include <asm/cacheflush.h>
52#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080053#include <asm/x86_init.h>
Prarit Bhargava3195ef52013-02-14 12:02:54 -050054#include <asm/rtc.h>
Borislav Petkova5d90c92014-03-04 17:02:17 +010055#include <asm/uv/uv.h>
Huang, Ying5b836832008-01-30 13:31:19 +010056
Borislav Petkovf4fccac2013-10-31 17:24:59 +010057#define EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +010058
Harvey Harrisonecaea422008-02-13 13:26:13 -080059static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010060static efi_system_table_t efi_systab __initdata;
61
Joe Perches9b7d2042014-01-03 16:08:48 -080062static efi_config_table_type_t arch_tables[] __initdata = {
Leif Lindholm272686b2013-09-05 11:34:54 +010063#ifdef CONFIG_X86_UV
64 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
65#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010066 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010067};
68
Dave Young1fec0532013-12-20 18:02:19 +080069u64 efi_setup; /* efi setup_data physical address */
Dave Young926172d2013-12-20 18:02:18 +080070
Mathias Krause60920682014-09-07 19:42:15 +020071static int add_efi_memmap __initdata;
Paul Jackson200001e2008-06-25 05:44:46 -070072static int __init setup_add_efi_memmap(char *arg)
73{
74 add_efi_memmap = 1;
75 return 0;
76}
77early_param("add_efi_memmap", setup_add_efi_memmap);
78
Huang, Ying5b836832008-01-30 13:31:19 +010079static efi_status_t __init phys_efi_set_virtual_address_map(
80 unsigned long memory_map_size,
81 unsigned long descriptor_size,
82 u32 descriptor_version,
83 efi_memory_desc_t *virtual_map)
84{
85 efi_status_t status;
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010086 unsigned long flags;
Ingo Molnar744937b2015-03-03 07:48:50 +010087 pgd_t *save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +010088
Ingo Molnar744937b2015-03-03 07:48:50 +010089 save_pgd = efi_call_phys_prolog();
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010090
91 /* Disable interrupts around EFI calls: */
92 local_irq_save(flags);
Matt Fleming62fa6e62014-03-27 15:10:39 -070093 status = efi_call_phys(efi_phys.set_virtual_address_map,
94 memory_map_size, descriptor_size,
95 descriptor_version, virtual_map);
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010096 local_irq_restore(flags);
97
Ingo Molnar744937b2015-03-03 07:48:50 +010098 efi_call_phys_epilog(save_pgd);
Ingo Molnar23a0d4e2015-03-03 07:34:33 +010099
Huang, Ying5b836832008-01-30 13:31:19 +0100100 return status;
101}
102
David Vrabel35651842013-05-13 18:56:06 +0100103void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100104{
105 efi_status_t status;
106 efi_time_t eft;
107 efi_time_cap_t cap;
108
109 status = efi.get_time(&eft, &cap);
110 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800111 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100112
David Vrabel35651842013-05-13 18:56:06 +0100113 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
114 eft.minute, eft.second);
115 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100116}
117
Tony Luckb05b9f52015-06-24 16:58:15 -0700118void __init efi_find_mirror(void)
119{
Matt Fleming78ce2482016-04-25 21:06:38 +0100120 efi_memory_desc_t *md;
Tony Luckb05b9f52015-06-24 16:58:15 -0700121 u64 mirror_size = 0, total_size = 0;
122
Matt Fleming78ce2482016-04-25 21:06:38 +0100123 for_each_efi_memory_desc(md) {
Tony Luckb05b9f52015-06-24 16:58:15 -0700124 unsigned long long start = md->phys_addr;
125 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
126
127 total_size += size;
128 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
129 memblock_mark_mirror(start, size);
130 mirror_size += size;
131 }
132 }
133 if (mirror_size)
134 pr_info("Memory: %lldM/%lldM mirrored memory\n",
135 mirror_size>>20, total_size>>20);
136}
137
Paul Jackson69c91892008-05-14 08:15:58 -0700138/*
139 * Tell the kernel about the EFI memory map. This might include
140 * more than the max 128 entries that can fit in the e820 legacy
141 * (zeropage) memory map.
142 */
143
Paul Jackson200001e2008-06-25 05:44:46 -0700144static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700145{
Matt Fleming78ce2482016-04-25 21:06:38 +0100146 efi_memory_desc_t *md;
Paul Jackson69c91892008-05-14 08:15:58 -0700147
Matt Fleming78ce2482016-04-25 21:06:38 +0100148 for_each_efi_memory_desc(md) {
Paul Jackson69c91892008-05-14 08:15:58 -0700149 unsigned long long start = md->phys_addr;
150 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
151 int e820_type;
152
Cliff Wickmane2a71472009-06-16 16:43:40 -0500153 switch (md->type) {
154 case EFI_LOADER_CODE:
155 case EFI_LOADER_DATA:
156 case EFI_BOOT_SERVICES_CODE:
157 case EFI_BOOT_SERVICES_DATA:
158 case EFI_CONVENTIONAL_MEMORY:
159 if (md->attribute & EFI_MEMORY_WB)
160 e820_type = E820_RAM;
161 else
162 e820_type = E820_RESERVED;
163 break;
164 case EFI_ACPI_RECLAIM_MEMORY:
165 e820_type = E820_ACPI;
166 break;
167 case EFI_ACPI_MEMORY_NVS:
168 e820_type = E820_NVS;
169 break;
170 case EFI_UNUSABLE_MEMORY:
171 e820_type = E820_UNUSABLE;
172 break;
Dan Williamsad5fb872015-04-03 12:05:28 -0400173 case EFI_PERSISTENT_MEMORY:
174 e820_type = E820_PMEM;
175 break;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500176 default:
177 /*
178 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
179 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
180 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
181 */
Paul Jackson69c91892008-05-14 08:15:58 -0700182 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500183 break;
184 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700185 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700186 }
187 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
188}
189
Olof Johansson1adbfa32012-02-12 13:24:29 -0800190int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800191{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100192 struct efi_info *e = &boot_params.efi_info;
Ard Biesheuvel44511fb2015-10-23 11:48:16 +0200193 phys_addr_t pmap;
Huang, Yingecacf092008-06-02 14:26:21 +0800194
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200195 if (efi_enabled(EFI_PARAVIRT))
196 return 0;
197
Paul Jackson05486fa2008-06-22 07:22:02 -0700198#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800199 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100200 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800201 pr_err("Memory map is above 4GB, disabling EFI.\n");
202 return -EINVAL;
203 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100204 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700205#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100206 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800207#endif
Matt Fleming884f4f62016-04-25 21:06:39 +0100208 efi.memmap.phys_map = pmap;
209 efi.memmap.nr_map = e->efi_memmap_size /
Borislav Petkov15b9c352013-03-01 17:07:44 +0100210 e->efi_memdesc_size;
Matt Fleming884f4f62016-04-25 21:06:39 +0100211 efi.memmap.desc_size = e->efi_memdesc_size;
212 efi.memmap.desc_version = e->efi_memdesc_version;
Borislav Petkov15b9c352013-03-01 17:07:44 +0100213
Ard Biesheuvel0d054ad2016-04-25 21:06:40 +0100214 WARN(efi.memmap.desc_version != 1,
215 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
216 efi.memmap.desc_version);
217
Matt Fleming884f4f62016-04-25 21:06:39 +0100218 memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100219
Olof Johansson1adbfa32012-02-12 13:24:29 -0800220 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800221}
222
Taku Izumi0bbea1c2015-09-30 19:20:00 +0900223void __init efi_print_memmap(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100224{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100225#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100226 efi_memory_desc_t *md;
Matt Fleming78ce2482016-04-25 21:06:38 +0100227 int i = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100228
Matt Fleming78ce2482016-04-25 21:06:38 +0100229 for_each_efi_memory_desc(md) {
Laszlo Ersekace1d122014-09-03 13:32:21 +0200230 char buf[64];
231
Robert Elliott1e82b942016-02-01 22:07:05 +0000232 pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
Matt Fleming78ce2482016-04-25 21:06:38 +0100233 i++, efi_md_typeattr_format(buf, sizeof(buf), md),
Laszlo Ersekace1d122014-09-03 13:32:21 +0200234 md->phys_addr,
Robert Elliott1e82b942016-02-01 22:07:05 +0000235 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
Huang, Ying5b836832008-01-30 13:31:19 +0100236 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
237 }
Huang, Ying5b836832008-01-30 13:31:19 +0100238#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100239}
Huang, Ying5b836832008-01-30 13:31:19 +0100240
Olof Johansson5189c2a2012-10-24 10:00:44 -0700241void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700242{
Matt Fleming884f4f62016-04-25 21:06:39 +0100243 unsigned long size;
244
Matt Fleming3e909592014-01-15 13:21:22 +0000245 clear_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming884f4f62016-04-25 21:06:39 +0100246
247 size = efi.memmap.nr_map * efi.memmap.desc_size;
248 if (efi.memmap.map) {
249 early_memunmap(efi.memmap.map, size);
250 efi.memmap.map = NULL;
Josh Triplett78510792012-09-28 17:55:44 -0700251 }
252}
253
Olof Johansson140bf272012-02-12 13:24:28 -0800254static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100255{
Matt Fleming83e68182012-11-14 09:42:35 +0000256 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800257 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800258 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800259 u64 tmp = 0;
260
Dave Young1fec0532013-12-20 18:02:19 +0800261 if (efi_setup) {
262 data = early_memremap(efi_setup, sizeof(*data));
263 if (!data)
264 return -ENOMEM;
265 }
Daniel Kiperabc93f82014-06-30 19:52:56 +0200266 systab64 = early_memremap((unsigned long)phys,
Olof Johansson1adbfa32012-02-12 13:24:29 -0800267 sizeof(*systab64));
268 if (systab64 == NULL) {
269 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800270 if (data)
Daniel Kiperabc93f82014-06-30 19:52:56 +0200271 early_memunmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800272 return -ENOMEM;
273 }
274
275 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800276 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
277 systab64->fw_vendor;
278 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800279 efi_systab.fw_revision = systab64->fw_revision;
280 efi_systab.con_in_handle = systab64->con_in_handle;
281 tmp |= systab64->con_in_handle;
282 efi_systab.con_in = systab64->con_in;
283 tmp |= systab64->con_in;
284 efi_systab.con_out_handle = systab64->con_out_handle;
285 tmp |= systab64->con_out_handle;
286 efi_systab.con_out = systab64->con_out;
287 tmp |= systab64->con_out;
288 efi_systab.stderr_handle = systab64->stderr_handle;
289 tmp |= systab64->stderr_handle;
290 efi_systab.stderr = systab64->stderr;
291 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800292 efi_systab.runtime = data ?
293 (void *)(unsigned long)data->runtime :
294 (void *)(unsigned long)systab64->runtime;
295 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800296 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
297 tmp |= systab64->boottime;
298 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800299 efi_systab.tables = data ? (unsigned long)data->tables :
300 systab64->tables;
301 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800302
Daniel Kiperabc93f82014-06-30 19:52:56 +0200303 early_memunmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800304 if (data)
Daniel Kiperabc93f82014-06-30 19:52:56 +0200305 early_memunmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800306#ifdef CONFIG_X86_32
307 if (tmp >> 32) {
308 pr_err("EFI data located above 4GB, disabling EFI.\n");
309 return -EINVAL;
310 }
311#endif
312 } else {
313 efi_system_table_32_t *systab32;
314
Daniel Kiperabc93f82014-06-30 19:52:56 +0200315 systab32 = early_memremap((unsigned long)phys,
Olof Johansson1adbfa32012-02-12 13:24:29 -0800316 sizeof(*systab32));
317 if (systab32 == NULL) {
318 pr_err("Couldn't map the system table!\n");
319 return -ENOMEM;
320 }
321
322 efi_systab.hdr = systab32->hdr;
323 efi_systab.fw_vendor = systab32->fw_vendor;
324 efi_systab.fw_revision = systab32->fw_revision;
325 efi_systab.con_in_handle = systab32->con_in_handle;
326 efi_systab.con_in = systab32->con_in;
327 efi_systab.con_out_handle = systab32->con_out_handle;
328 efi_systab.con_out = systab32->con_out;
329 efi_systab.stderr_handle = systab32->stderr_handle;
330 efi_systab.stderr = systab32->stderr;
331 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
332 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
333 efi_systab.nr_tables = systab32->nr_tables;
334 efi_systab.tables = systab32->tables;
335
Daniel Kiperabc93f82014-06-30 19:52:56 +0200336 early_memunmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800337 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800338
Huang, Ying5b836832008-01-30 13:31:19 +0100339 efi.systab = &efi_systab;
340
341 /*
342 * Verify the EFI Table
343 */
Olof Johansson140bf272012-02-12 13:24:28 -0800344 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800345 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800346 return -EINVAL;
347 }
Huang, Ying5b836832008-01-30 13:31:19 +0100348 if ((efi.systab->hdr.revision >> 16) == 0)
Joe Perches9b7d2042014-01-03 16:08:48 -0800349 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100350 efi.systab->hdr.revision >> 16,
351 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800352
353 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800354}
Huang, Ying5b836832008-01-30 13:31:19 +0100355
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000356static int __init efi_runtime_init32(void)
357{
358 efi_runtime_services_32_t *runtime;
359
Daniel Kiperabc93f82014-06-30 19:52:56 +0200360 runtime = early_memremap((unsigned long)efi.systab->runtime,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000361 sizeof(efi_runtime_services_32_t));
362 if (!runtime) {
363 pr_err("Could not map the runtime service table!\n");
364 return -ENOMEM;
365 }
366
367 /*
Mathias Krause0ce46052014-09-07 19:42:16 +0200368 * We will only need *early* access to the SetVirtualAddressMap
369 * EFI runtime service. All other runtime services will be called
370 * via the virtual mapping.
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000371 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000372 efi_phys.set_virtual_address_map =
373 (efi_set_virtual_address_map_t *)
374 (unsigned long)runtime->set_virtual_address_map;
Daniel Kiperabc93f82014-06-30 19:52:56 +0200375 early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000376
377 return 0;
378}
379
380static int __init efi_runtime_init64(void)
381{
382 efi_runtime_services_64_t *runtime;
383
Daniel Kiperabc93f82014-06-30 19:52:56 +0200384 runtime = early_memremap((unsigned long)efi.systab->runtime,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000385 sizeof(efi_runtime_services_64_t));
386 if (!runtime) {
387 pr_err("Could not map the runtime service table!\n");
388 return -ENOMEM;
389 }
390
391 /*
Mathias Krause0ce46052014-09-07 19:42:16 +0200392 * We will only need *early* access to the SetVirtualAddressMap
393 * EFI runtime service. All other runtime services will be called
394 * via the virtual mapping.
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000395 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000396 efi_phys.set_virtual_address_map =
397 (efi_set_virtual_address_map_t *)
398 (unsigned long)runtime->set_virtual_address_map;
Daniel Kiperabc93f82014-06-30 19:52:56 +0200399 early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000400
401 return 0;
402}
403
Olof Johansson140bf272012-02-12 13:24:28 -0800404static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800405{
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000406 int rv;
Huang, Ying5b836832008-01-30 13:31:19 +0100407
408 /*
409 * Check out the runtime services table. We need to map
410 * the runtime services table so that we can grab the physical
411 * address of several of the EFI runtime functions, needed to
412 * set the firmware into virtual mode.
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200413 *
414 * When EFI_PARAVIRT is in force then we could not map runtime
415 * service memory region because we do not have direct access to it.
416 * However, runtime services are available through proxy functions
417 * (e.g. in case of Xen dom0 EFI implementation they call special
418 * hypercall which executes relevant EFI functions) and that is why
419 * they are always enabled.
Huang, Ying5b836832008-01-30 13:31:19 +0100420 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000421
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200422 if (!efi_enabled(EFI_PARAVIRT)) {
423 if (efi_enabled(EFI_64BIT))
424 rv = efi_runtime_init64();
425 else
426 rv = efi_runtime_init32();
427
428 if (rv)
429 return rv;
430 }
Olof Johansson140bf272012-02-12 13:24:28 -0800431
Matt Fleming0f8093a2014-01-15 13:36:33 +0000432 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
433
Olof Johansson140bf272012-02-12 13:24:28 -0800434 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800435}
Huang, Ying5b836832008-01-30 13:31:19 +0100436
Olof Johansson140bf272012-02-12 13:24:28 -0800437static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800438{
Matt Fleming884f4f62016-04-25 21:06:39 +0100439 unsigned long addr, size;
440
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200441 if (efi_enabled(EFI_PARAVIRT))
442 return 0;
443
Huang, Ying5b836832008-01-30 13:31:19 +0100444 /* Map the EFI memory map */
Matt Fleming884f4f62016-04-25 21:06:39 +0100445 size = efi.memmap.nr_map * efi.memmap.desc_size;
446 addr = (unsigned long)efi.memmap.phys_map;
447
448 efi.memmap.map = early_memremap(addr, size);
449 if (efi.memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800450 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800451 return -ENOMEM;
452 }
Matt Fleming884f4f62016-04-25 21:06:39 +0100453
454 efi.memmap.map_end = efi.memmap.map + size;
Russ Anderson175e4382008-10-02 17:32:06 -0500455
Paul Jackson200001e2008-06-25 05:44:46 -0700456 if (add_efi_memmap)
457 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800458
Matt Fleming0f8093a2014-01-15 13:36:33 +0000459 set_bit(EFI_MEMMAP, &efi.flags);
460
Olof Johansson140bf272012-02-12 13:24:28 -0800461 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800462}
463
464void __init efi_init(void)
465{
466 efi_char16_t *c16;
467 char vendor[100] = "unknown";
468 int i = 0;
469 void *tmp;
470
471#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800472 if (boot_params.efi_info.efi_systab_hi ||
473 boot_params.efi_info.efi_memmap_hi) {
474 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800475 return;
476 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800477 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
478#else
479 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800480 (boot_params.efi_info.efi_systab |
481 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800482#endif
483
Matt Fleming83e68182012-11-14 09:42:35 +0000484 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800485 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000486
Dave Younga0998eb2013-12-20 18:02:17 +0800487 efi.config_table = (unsigned long)efi.systab->tables;
488 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
489 efi.runtime = (unsigned long)efi.systab->runtime;
490
Olof Johansson83e7ee62012-02-12 13:24:25 -0800491 /*
492 * Show what we know for posterity
493 */
Daniel Kiperabc93f82014-06-30 19:52:56 +0200494 c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800495 if (c16) {
496 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
497 vendor[i] = *c16++;
498 vendor[i] = '\0';
499 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800500 pr_err("Could not map the firmware vendor!\n");
Daniel Kiperabc93f82014-06-30 19:52:56 +0200501 early_memunmap(tmp, 2);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800502
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800503 pr_info("EFI v%u.%.02u by %s\n",
504 efi.systab->hdr.revision >> 16,
505 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800506
Dave Young1fec0532013-12-20 18:02:19 +0800507 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
508 return;
509
Leif Lindholm272686b2013-09-05 11:34:54 +0100510 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800511 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000512
Olof Johansson1adbfa32012-02-12 13:24:29 -0800513 /*
514 * Note: We currently don't support runtime services on an EFI
515 * that doesn't match the kernel 32/64-bit mode.
516 */
517
Matt Fleming7d453ee2014-01-10 18:52:06 +0000518 if (!efi_runtime_supported())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800519 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000520 else {
Dave Youngb2e0a542014-08-14 17:15:26 +0800521 if (efi_runtime_disabled() || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000522 return;
Olof Johansson140bf272012-02-12 13:24:28 -0800523 }
Matt Fleming83e68182012-11-14 09:42:35 +0000524 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800525 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000526
Borislav Petkovfed6cef2015-02-05 11:44:41 +0100527 if (efi_enabled(EFI_DBG))
Taku Izumi0bbea1c2015-09-30 19:20:00 +0900528 efi_print_memmap();
Peter Jones0bb54902015-04-28 18:44:31 -0400529
530 efi_esrt_init();
Huang, Ying5b836832008-01-30 13:31:19 +0100531}
532
Josh Triplett2223af32012-09-28 17:57:05 -0700533void __init efi_late_init(void)
534{
535 efi_bgrt_init();
536}
537
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400538void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
539{
540 u64 addr, npages;
541
542 addr = md->virt_addr;
543 npages = md->num_pages;
544
545 memrange_efi_to_native(&addr, &npages);
546
547 if (executable)
548 set_memory_x(addr, npages);
549 else
550 set_memory_nx(addr, npages);
551}
552
Borislav Petkovc55d0162014-02-14 08:24:24 +0100553void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100554{
555 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100556
Huang, Yinga2172e22008-01-30 13:33:55 +0100557 /* Make EFI runtime service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +0100558 for_each_efi_memory_desc(md) {
Huang, Ying1c083eb2008-02-04 16:48:06 +0100559 if (md->type != EFI_RUNTIME_SERVICES_CODE)
560 continue;
561
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400562 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100563 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100564}
Huang, Yinga2172e22008-01-30 13:33:55 +0100565
Mathias Krause4e78eb052014-09-07 19:42:17 +0200566void __init efi_memory_uc(u64 addr, unsigned long size)
Matt Fleming3e8fa262012-10-19 13:25:46 +0100567{
568 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
569 u64 npages;
570
571 npages = round_up(size, page_shift) / page_shift;
572 memrange_efi_to_native(&addr, &npages);
573 set_memory_uc(addr, npages);
574}
575
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100576void __init old_map_region(efi_memory_desc_t *md)
577{
578 u64 start_pfn, end_pfn, end;
579 unsigned long size;
580 void *va;
581
582 start_pfn = PFN_DOWN(md->phys_addr);
583 size = md->num_pages << PAGE_SHIFT;
584 end = md->phys_addr + size;
585 end_pfn = PFN_UP(end);
586
587 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
588 va = __va(md->phys_addr);
589
590 if (!(md->attribute & EFI_MEMORY_WB))
591 efi_memory_uc((u64)(unsigned long)va, size);
592 } else
593 va = efi_ioremap(md->phys_addr, size,
594 md->type, md->attribute);
595
596 md->virt_addr = (u64) (unsigned long) va;
597 if (!va)
598 pr_err("ioremap of 0x%llX failed!\n",
599 (unsigned long long)md->phys_addr);
600}
601
Dave Young481f75c2013-12-20 18:02:16 +0800602/* Merge contiguous regions of the same type and attribute */
603static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100604{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400605 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100606
Matt Fleming78ce2482016-04-25 21:06:38 +0100607 for_each_efi_memory_desc(md) {
Matthew Garrett202f9d02011-05-05 15:19:44 -0400608 u64 prev_size;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400609
610 if (!prev_md) {
611 prev_md = md;
612 continue;
613 }
614
615 if (prev_md->type != md->type ||
616 prev_md->attribute != md->attribute) {
617 prev_md = md;
618 continue;
619 }
620
621 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
622
623 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
624 prev_md->num_pages += md->num_pages;
625 md->type = EFI_RESERVED_TYPE;
626 md->attribute = 0;
627 continue;
628 }
629 prev_md = md;
630 }
Dave Young481f75c2013-12-20 18:02:16 +0800631}
632
633static void __init get_systab_virt_addr(efi_memory_desc_t *md)
634{
635 unsigned long size;
636 u64 end, systab;
637
638 size = md->num_pages << EFI_PAGE_SHIFT;
639 end = md->phys_addr + size;
640 systab = (u64)(unsigned long)efi_phys.systab;
641 if (md->phys_addr <= systab && systab < end) {
642 systab += md->virt_addr - md->phys_addr;
643 efi.systab = (efi_system_table_t *)(unsigned long)systab;
644 }
645}
646
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100647static void __init save_runtime_map(void)
Dave Young926172d2013-12-20 18:02:18 +0800648{
Dave Young2965faa2015-09-09 15:38:55 -0700649#ifdef CONFIG_KEXEC_CORE
Matt Fleming884f4f62016-04-25 21:06:39 +0100650 unsigned long desc_size;
Dave Young926172d2013-12-20 18:02:18 +0800651 efi_memory_desc_t *md;
Matt Fleming78ce2482016-04-25 21:06:38 +0100652 void *tmp, *q = NULL;
Dave Young926172d2013-12-20 18:02:18 +0800653 int count = 0;
654
Dave Younga3530e82014-05-30 11:20:10 +0800655 if (efi_enabled(EFI_OLD_MEMMAP))
656 return;
657
Matt Fleming884f4f62016-04-25 21:06:39 +0100658 desc_size = efi.memmap.desc_size;
659
Matt Fleming78ce2482016-04-25 21:06:38 +0100660 for_each_efi_memory_desc(md) {
Dave Young926172d2013-12-20 18:02:18 +0800661 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
662 (md->type == EFI_BOOT_SERVICES_CODE) ||
663 (md->type == EFI_BOOT_SERVICES_DATA))
664 continue;
Matt Fleming884f4f62016-04-25 21:06:39 +0100665 tmp = krealloc(q, (count + 1) * desc_size, GFP_KERNEL);
Dave Young926172d2013-12-20 18:02:18 +0800666 if (!tmp)
667 goto out;
668 q = tmp;
669
Matt Fleming884f4f62016-04-25 21:06:39 +0100670 memcpy(q + count * desc_size, md, desc_size);
Dave Young926172d2013-12-20 18:02:18 +0800671 count++;
672 }
673
Matt Fleming884f4f62016-04-25 21:06:39 +0100674 efi_runtime_map_setup(q, count, desc_size);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100675 return;
Dave Young926172d2013-12-20 18:02:18 +0800676
Dave Young926172d2013-12-20 18:02:18 +0800677out:
678 kfree(q);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100679 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
680#endif
Dave Young1fec0532013-12-20 18:02:19 +0800681}
682
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100683static void *realloc_pages(void *old_memmap, int old_shift)
Dave Young481f75c2013-12-20 18:02:16 +0800684{
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100685 void *ret;
686
687 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
688 if (!ret)
689 goto out;
690
691 /*
692 * A first-time allocation doesn't have anything to copy.
693 */
694 if (!old_memmap)
695 return ret;
696
697 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
698
699out:
700 free_pages((unsigned long)old_memmap, old_shift);
701 return ret;
702}
703
704/*
Matt Fleminga5caa202015-09-25 23:02:18 +0100705 * Iterate the EFI memory map in reverse order because the regions
706 * will be mapped top-down. The end result is the same as if we had
707 * mapped things forward, but doesn't require us to change the
708 * existing implementation of efi_map_region().
709 */
710static inline void *efi_map_next_entry_reverse(void *entry)
711{
712 /* Initial call */
713 if (!entry)
Matt Fleming884f4f62016-04-25 21:06:39 +0100714 return efi.memmap.map_end - efi.memmap.desc_size;
Matt Fleminga5caa202015-09-25 23:02:18 +0100715
Matt Fleming884f4f62016-04-25 21:06:39 +0100716 entry -= efi.memmap.desc_size;
717 if (entry < efi.memmap.map)
Matt Fleminga5caa202015-09-25 23:02:18 +0100718 return NULL;
719
720 return entry;
721}
722
723/*
724 * efi_map_next_entry - Return the next EFI memory map descriptor
725 * @entry: Previous EFI memory map descriptor
726 *
727 * This is a helper function to iterate over the EFI memory map, which
728 * we do in different orders depending on the current configuration.
729 *
730 * To begin traversing the memory map @entry must be %NULL.
731 *
732 * Returns %NULL when we reach the end of the memory map.
733 */
734static void *efi_map_next_entry(void *entry)
735{
736 if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
737 /*
738 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
739 * config table feature requires us to map all entries
740 * in the same order as they appear in the EFI memory
741 * map. That is to say, entry N must have a lower
742 * virtual address than entry N+1. This is because the
743 * firmware toolchain leaves relative references in
744 * the code/data sections, which are split and become
745 * separate EFI memory regions. Mapping things
746 * out-of-order leads to the firmware accessing
747 * unmapped addresses.
748 *
749 * Since we need to map things this way whether or not
750 * the kernel actually makes use of
751 * EFI_PROPERTIES_TABLE, let's just switch to this
752 * scheme by default for 64-bit.
753 */
754 return efi_map_next_entry_reverse(entry);
755 }
756
757 /* Initial call */
758 if (!entry)
Matt Fleming884f4f62016-04-25 21:06:39 +0100759 return efi.memmap.map;
Matt Fleminga5caa202015-09-25 23:02:18 +0100760
Matt Fleming884f4f62016-04-25 21:06:39 +0100761 entry += efi.memmap.desc_size;
762 if (entry >= efi.memmap.map_end)
Matt Fleminga5caa202015-09-25 23:02:18 +0100763 return NULL;
764
765 return entry;
766}
767
768/*
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100769 * Map the efi memory ranges of the runtime services and update new_mmap with
770 * virtual addresses.
771 */
772static void * __init efi_map_regions(int *count, int *pg_shift)
773{
774 void *p, *new_memmap = NULL;
775 unsigned long left = 0;
Matt Fleming884f4f62016-04-25 21:06:39 +0100776 unsigned long desc_size;
Dave Young481f75c2013-12-20 18:02:16 +0800777 efi_memory_desc_t *md;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400778
Matt Fleming884f4f62016-04-25 21:06:39 +0100779 desc_size = efi.memmap.desc_size;
780
Matt Fleminga5caa202015-09-25 23:02:18 +0100781 p = NULL;
782 while ((p = efi_map_next_entry(p))) {
Huang, Ying5b836832008-01-30 13:31:19 +0100783 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700784 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
785#ifdef CONFIG_X86_64
786 if (md->type != EFI_BOOT_SERVICES_CODE &&
787 md->type != EFI_BOOT_SERVICES_DATA)
788#endif
789 continue;
790 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100791
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100792 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800793 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100794
Matt Fleming884f4f62016-04-25 21:06:39 +0100795 if (left < desc_size) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100796 new_memmap = realloc_pages(new_memmap, *pg_shift);
797 if (!new_memmap)
798 return NULL;
799
800 left += PAGE_SIZE << *pg_shift;
801 (*pg_shift)++;
802 }
803
Matt Fleming884f4f62016-04-25 21:06:39 +0100804 memcpy(new_memmap + (*count * desc_size), md, desc_size);
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100805
Matt Fleming884f4f62016-04-25 21:06:39 +0100806 left -= desc_size;
Dave Young481f75c2013-12-20 18:02:16 +0800807 (*count)++;
808 }
809
810 return new_memmap;
Dave Young481f75c2013-12-20 18:02:16 +0800811}
812
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100813static void __init kexec_enter_virtual_mode(void)
814{
Dave Young2965faa2015-09-09 15:38:55 -0700815#ifdef CONFIG_KEXEC_CORE
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100816 efi_memory_desc_t *md;
Matt Fleming753b11e2016-01-21 14:11:59 +0000817 unsigned int num_pages;
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100818
819 efi.systab = NULL;
820
821 /*
822 * We don't do virtual mode, since we don't do runtime services, on
823 * non-native EFI
824 */
825 if (!efi_is_native()) {
826 efi_unmap_memmap();
Dave Younga5a750a2014-08-14 17:15:31 +0800827 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100828 return;
829 }
830
Matt Fleming753b11e2016-01-21 14:11:59 +0000831 if (efi_alloc_page_tables()) {
832 pr_err("Failed to allocate EFI page tables\n");
833 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
834 return;
835 }
836
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100837 /*
838 * Map efi regions which were passed via setup_data. The virt_addr is a
839 * fixed addr which was used in first kernel of a kexec boot.
840 */
Matt Fleming78ce2482016-04-25 21:06:38 +0100841 for_each_efi_memory_desc(md) {
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100842 efi_map_region_fixed(md); /* FIXME: add error handling */
843 get_systab_virt_addr(md);
844 }
845
846 save_runtime_map();
847
848 BUG_ON(!efi.systab);
849
Matt Fleming884f4f62016-04-25 21:06:39 +0100850 num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
Matt Fleming753b11e2016-01-21 14:11:59 +0000851 num_pages >>= PAGE_SHIFT;
852
Matt Fleming884f4f62016-04-25 21:06:39 +0100853 if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
Matt Fleming753b11e2016-01-21 14:11:59 +0000854 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
855 return;
856 }
857
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100858 efi_sync_low_kernel_mappings();
859
860 /*
861 * Now that EFI is in virtual mode, update the function
862 * pointers in the runtime service table to the new virtual addresses.
863 *
864 * Call EFI services through wrapper functions.
865 */
866 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming994448f2014-03-05 18:15:37 +0000867
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200868 efi_native_runtime_setup();
Matt Fleming994448f2014-03-05 18:15:37 +0000869
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100870 efi.set_virtual_address_map = NULL;
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100871
872 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
873 runtime_code_page_mkexec();
874
875 /* clean DUMMY object */
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700876 efi_delete_dummy_variable();
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100877#endif
878}
879
Dave Young481f75c2013-12-20 18:02:16 +0800880/*
881 * This function will switch the EFI runtime services to virtual mode.
882 * Essentially, we look through the EFI memmap and map every region that
883 * has the runtime attribute bit set in its memory descriptor into the
Matt Fleming67a91082015-11-27 21:09:34 +0000884 * efi_pgd page table.
Dave Young481f75c2013-12-20 18:02:16 +0800885 *
886 * The old method which used to update that memory descriptor with the
887 * virtual address obtained from ioremap() is still supported when the
888 * kernel is booted with efi=old_map on its command line. Same old
889 * method enabled the runtime services to be called without having to
890 * thunk back into physical mode for every invocation.
891 *
892 * The new method does a pagetable switch in a preemption-safe manner
893 * so that we're in a different address space when calling a runtime
Matt Fleming67a91082015-11-27 21:09:34 +0000894 * function. For function arguments passing we do copy the PUDs of the
895 * kernel page table into efi_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +0800896 *
897 * Specially for kexec boot, efi runtime maps in previous kernel should
898 * be passed in via setup_data. In that case runtime ranges will be mapped
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100899 * to the same virtual addresses as the first kernel, see
900 * kexec_enter_virtual_mode().
Dave Young481f75c2013-12-20 18:02:16 +0800901 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100902static void __init __efi_enter_virtual_mode(void)
Dave Young481f75c2013-12-20 18:02:16 +0800903{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100904 int count = 0, pg_shift = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800905 void *new_memmap = NULL;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100906 efi_status_t status;
Dave Young481f75c2013-12-20 18:02:16 +0800907
908 efi.systab = NULL;
909
Matt Fleming67a91082015-11-27 21:09:34 +0000910 if (efi_alloc_page_tables()) {
911 pr_err("Failed to allocate EFI page tables\n");
912 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
913 return;
914 }
915
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100916 efi_merge_regions();
917 new_memmap = efi_map_regions(&count, &pg_shift);
918 if (!new_memmap) {
919 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
Dave Younga5a750a2014-08-14 17:15:31 +0800920 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100921 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100922 }
Dave Young926172d2013-12-20 18:02:18 +0800923
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100924 save_runtime_map();
925
Huang, Ying5b836832008-01-30 13:31:19 +0100926 BUG_ON(!efi.systab);
927
Dave Younga5a750a2014-08-14 17:15:31 +0800928 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift)) {
929 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100930 return;
Dave Younga5a750a2014-08-14 17:15:31 +0800931 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100932
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100933 efi_sync_low_kernel_mappings();
934
Matt Fleming994448f2014-03-05 18:15:37 +0000935 if (efi_is_native()) {
936 status = phys_efi_set_virtual_address_map(
Matt Fleming884f4f62016-04-25 21:06:39 +0100937 efi.memmap.desc_size * count,
938 efi.memmap.desc_size,
939 efi.memmap.desc_version,
Matt Fleming994448f2014-03-05 18:15:37 +0000940 (efi_memory_desc_t *)__pa(new_memmap));
941 } else {
942 status = efi_thunk_set_virtual_address_map(
943 efi_phys.set_virtual_address_map,
Matt Fleming884f4f62016-04-25 21:06:39 +0100944 efi.memmap.desc_size * count,
945 efi.memmap.desc_size,
946 efi.memmap.desc_version,
Matt Fleming994448f2014-03-05 18:15:37 +0000947 (efi_memory_desc_t *)__pa(new_memmap));
948 }
Huang, Ying5b836832008-01-30 13:31:19 +0100949
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100950 if (status != EFI_SUCCESS) {
951 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
952 status);
953 panic("EFI call to SetVirtualAddressMap() failed!");
Huang, Ying5b836832008-01-30 13:31:19 +0100954 }
955
956 /*
957 * Now that EFI is in virtual mode, update the function
958 * pointers in the runtime service table to the new virtual addresses.
959 *
960 * Call EFI services through wrapper functions.
961 */
Matt Fleming712ba9e2013-01-25 10:07:25 +0000962 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000963
964 if (efi_is_native())
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200965 efi_native_runtime_setup();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000966 else
967 efi_thunk_runtime_setup();
968
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400969 efi.set_virtual_address_map = NULL;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100970
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000971 /*
972 * Apply more restrictive page table mapping attributes now that
973 * SVAM() has been called and the firmware has performed all
974 * necessary relocation fixups for the new virtual addresses.
975 */
976 efi_runtime_update_mappings();
977 efi_dump_pagetable();
Olof Johansson1adbfa32012-02-12 13:24:29 -0800978
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100979 /*
Matt Fleming67a91082015-11-27 21:09:34 +0000980 * We mapped the descriptor array into the EFI pagetable above
981 * but we're not unmapping it here because if we're running in
982 * EFI mixed mode we need all of memory to be accessible when
983 * we pass parameters to the EFI runtime services in the
984 * thunking code.
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100985 *
986 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
987 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100988 free_pages((unsigned long)new_memmap, pg_shift);
Matthew Garrettf8b84042013-06-01 16:06:20 -0400989
990 /* clean DUMMY object */
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700991 efi_delete_dummy_variable();
Huang, Ying5b836832008-01-30 13:31:19 +0100992}
993
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100994void __init efi_enter_virtual_mode(void)
995{
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200996 if (efi_enabled(EFI_PARAVIRT))
997 return;
998
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100999 if (efi_setup)
1000 kexec_enter_virtual_mode();
1001 else
1002 __efi_enter_virtual_mode();
1003}
1004
Huang, Ying5b836832008-01-30 13:31:19 +01001005/*
1006 * Convenience functions to obtain memory types and attributes
1007 */
1008u32 efi_mem_type(unsigned long phys_addr)
1009{
1010 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +01001011
Matt Fleming83e68182012-11-14 09:42:35 +00001012 if (!efi_enabled(EFI_MEMMAP))
1013 return 0;
1014
Matt Fleming78ce2482016-04-25 21:06:38 +01001015 for_each_efi_memory_desc(md) {
Huang, Ying5b836832008-01-30 13:31:19 +01001016 if ((md->phys_addr <= phys_addr) &&
1017 (phys_addr < (md->phys_addr +
1018 (md->num_pages << EFI_PAGE_SHIFT))))
1019 return md->type;
1020 }
1021 return 0;
1022}
1023
Dave Young5ae36832014-08-14 17:15:28 +08001024static int __init arch_parse_efi_cmdline(char *str)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001025{
Ricardo Neri9115c752015-07-15 19:36:03 -07001026 if (!str) {
1027 pr_warn("need at least one option\n");
1028 return -EINVAL;
1029 }
1030
Dave Young6ccc72b82014-08-14 17:15:27 +08001031 if (parse_option_str(str, "old_map"))
1032 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001033
1034 return 0;
1035}
Dave Young5ae36832014-08-14 17:15:28 +08001036early_param("efi", arch_parse_efi_cmdline);