blob: 7d627a02ed82d8d6cdb0328d1b78bf3379721f70 [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
Huang, Ying5b836832008-01-30 13:31:19 +010059struct efi_memory_map memmap;
60
Harvey Harrisonecaea422008-02-13 13:26:13 -080061static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010062static efi_system_table_t efi_systab __initdata;
63
Joe Perches9b7d2042014-01-03 16:08:48 -080064static efi_config_table_type_t arch_tables[] __initdata = {
Leif Lindholm272686b2013-09-05 11:34:54 +010065#ifdef CONFIG_X86_UV
66 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
67#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010068 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010069};
70
Dave Young1fec0532013-12-20 18:02:19 +080071u64 efi_setup; /* efi setup_data physical address */
Dave Young926172d2013-12-20 18:02:18 +080072
Joe Perches9b7d2042014-01-03 16:08:48 -080073static bool disable_runtime __initdata = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010074static int __init setup_noefi(char *arg)
75{
Matt Flemingfb834c72013-02-20 20:36:12 +000076 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010077 return 0;
78}
79early_param("noefi", setup_noefi);
80
Paul Jackson200001e2008-06-25 05:44:46 -070081int add_efi_memmap;
82EXPORT_SYMBOL(add_efi_memmap);
83
84static int __init setup_add_efi_memmap(char *arg)
85{
86 add_efi_memmap = 1;
87 return 0;
88}
89early_param("add_efi_memmap", setup_add_efi_memmap);
90
Huang, Ying5b836832008-01-30 13:31:19 +010091static efi_status_t __init phys_efi_set_virtual_address_map(
92 unsigned long memory_map_size,
93 unsigned long descriptor_size,
94 u32 descriptor_version,
95 efi_memory_desc_t *virtual_map)
96{
97 efi_status_t status;
98
99 efi_call_phys_prelog();
Matt Fleming62fa6e62014-03-27 15:10:39 -0700100 status = efi_call_phys(efi_phys.set_virtual_address_map,
101 memory_map_size, descriptor_size,
102 descriptor_version, virtual_map);
Huang, Ying5b836832008-01-30 13:31:19 +0100103 efi_call_phys_epilog();
104 return status;
105}
106
David Vrabel35651842013-05-13 18:56:06 +0100107int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100108{
David Vrabel35651842013-05-13 18:56:06 +0100109 unsigned long nowtime = now->tv_sec;
Joe Perches9b7d2042014-01-03 16:08:48 -0800110 efi_status_t status;
111 efi_time_t eft;
112 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500113 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100114
115 status = efi.get_time(&eft, &cap);
116 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800117 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100118 return -1;
119 }
120
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500121 rtc_time_to_tm(nowtime, &tm);
122 if (!rtc_valid_tm(&tm)) {
123 eft.year = tm.tm_year + 1900;
124 eft.month = tm.tm_mon + 1;
125 eft.day = tm.tm_mday;
126 eft.minute = tm.tm_min;
127 eft.second = tm.tm_sec;
128 eft.nanosecond = 0;
129 } else {
Joe Perches9b7d2042014-01-03 16:08:48 -0800130 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
131 __func__, nowtime);
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500132 return -1;
133 }
Huang, Ying5b836832008-01-30 13:31:19 +0100134
135 status = efi.set_time(&eft);
136 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800137 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100138 return -1;
139 }
140 return 0;
141}
142
David Vrabel35651842013-05-13 18:56:06 +0100143void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100144{
145 efi_status_t status;
146 efi_time_t eft;
147 efi_time_cap_t cap;
148
149 status = efi.get_time(&eft, &cap);
150 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800151 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100152
David Vrabel35651842013-05-13 18:56:06 +0100153 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
154 eft.minute, eft.second);
155 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100156}
157
Paul Jackson69c91892008-05-14 08:15:58 -0700158/*
159 * Tell the kernel about the EFI memory map. This might include
160 * more than the max 128 entries that can fit in the e820 legacy
161 * (zeropage) memory map.
162 */
163
Paul Jackson200001e2008-06-25 05:44:46 -0700164static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700165{
166 void *p;
167
168 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
169 efi_memory_desc_t *md = p;
170 unsigned long long start = md->phys_addr;
171 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
172 int e820_type;
173
Cliff Wickmane2a71472009-06-16 16:43:40 -0500174 switch (md->type) {
175 case EFI_LOADER_CODE:
176 case EFI_LOADER_DATA:
177 case EFI_BOOT_SERVICES_CODE:
178 case EFI_BOOT_SERVICES_DATA:
179 case EFI_CONVENTIONAL_MEMORY:
180 if (md->attribute & EFI_MEMORY_WB)
181 e820_type = E820_RAM;
182 else
183 e820_type = E820_RESERVED;
184 break;
185 case EFI_ACPI_RECLAIM_MEMORY:
186 e820_type = E820_ACPI;
187 break;
188 case EFI_ACPI_MEMORY_NVS:
189 e820_type = E820_NVS;
190 break;
191 case EFI_UNUSABLE_MEMORY:
192 e820_type = E820_UNUSABLE;
193 break;
194 default:
195 /*
196 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
197 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
198 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
199 */
Paul Jackson69c91892008-05-14 08:15:58 -0700200 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500201 break;
202 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700203 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700204 }
205 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
206}
207
Olof Johansson1adbfa32012-02-12 13:24:29 -0800208int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800209{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100210 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800211 unsigned long pmap;
212
Paul Jackson05486fa2008-06-22 07:22:02 -0700213#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800214 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100215 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800216 pr_err("Memory map is above 4GB, disabling EFI.\n");
217 return -EINVAL;
218 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100219 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700220#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100221 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800222#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100223 memmap.phys_map = (void *)pmap;
224 memmap.nr_map = e->efi_memmap_size /
225 e->efi_memdesc_size;
226 memmap.desc_size = e->efi_memdesc_size;
227 memmap.desc_version = e->efi_memdesc_version;
228
Tejun Heo24aa0782011-07-12 11:16:06 +0200229 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800230
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100231 efi.memmap = &memmap;
232
Olof Johansson1adbfa32012-02-12 13:24:29 -0800233 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800234}
235
Huang, Ying5b836832008-01-30 13:31:19 +0100236static void __init print_efi_memmap(void)
237{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100238#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100239 efi_memory_desc_t *md;
240 void *p;
241 int i;
242
243 for (p = memmap.map, i = 0;
244 p < memmap.map_end;
245 p += memmap.desc_size, i++) {
246 md = p;
Joe Perches9b7d2042014-01-03 16:08:48 -0800247 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100248 i, md->type, md->attribute, md->phys_addr,
249 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
250 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
251 }
Huang, Ying5b836832008-01-30 13:31:19 +0100252#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100253}
Huang, Ying5b836832008-01-30 13:31:19 +0100254
Olof Johansson5189c2a2012-10-24 10:00:44 -0700255void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700256{
Matt Fleming3e909592014-01-15 13:21:22 +0000257 clear_bit(EFI_MEMMAP, &efi.flags);
Josh Triplett78510792012-09-28 17:55:44 -0700258 if (memmap.map) {
Daniel Kiperabc93f82014-06-30 19:52:56 +0200259 early_memunmap(memmap.map, memmap.nr_map * memmap.desc_size);
Josh Triplett78510792012-09-28 17:55:44 -0700260 memmap.map = NULL;
261 }
262}
263
Olof Johansson140bf272012-02-12 13:24:28 -0800264static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100265{
Matt Fleming83e68182012-11-14 09:42:35 +0000266 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800267 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800268 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800269 u64 tmp = 0;
270
Dave Young1fec0532013-12-20 18:02:19 +0800271 if (efi_setup) {
272 data = early_memremap(efi_setup, sizeof(*data));
273 if (!data)
274 return -ENOMEM;
275 }
Daniel Kiperabc93f82014-06-30 19:52:56 +0200276 systab64 = early_memremap((unsigned long)phys,
Olof Johansson1adbfa32012-02-12 13:24:29 -0800277 sizeof(*systab64));
278 if (systab64 == NULL) {
279 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800280 if (data)
Daniel Kiperabc93f82014-06-30 19:52:56 +0200281 early_memunmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800282 return -ENOMEM;
283 }
284
285 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800286 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
287 systab64->fw_vendor;
288 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800289 efi_systab.fw_revision = systab64->fw_revision;
290 efi_systab.con_in_handle = systab64->con_in_handle;
291 tmp |= systab64->con_in_handle;
292 efi_systab.con_in = systab64->con_in;
293 tmp |= systab64->con_in;
294 efi_systab.con_out_handle = systab64->con_out_handle;
295 tmp |= systab64->con_out_handle;
296 efi_systab.con_out = systab64->con_out;
297 tmp |= systab64->con_out;
298 efi_systab.stderr_handle = systab64->stderr_handle;
299 tmp |= systab64->stderr_handle;
300 efi_systab.stderr = systab64->stderr;
301 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800302 efi_systab.runtime = data ?
303 (void *)(unsigned long)data->runtime :
304 (void *)(unsigned long)systab64->runtime;
305 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800306 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
307 tmp |= systab64->boottime;
308 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800309 efi_systab.tables = data ? (unsigned long)data->tables :
310 systab64->tables;
311 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800312
Daniel Kiperabc93f82014-06-30 19:52:56 +0200313 early_memunmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800314 if (data)
Daniel Kiperabc93f82014-06-30 19:52:56 +0200315 early_memunmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800316#ifdef CONFIG_X86_32
317 if (tmp >> 32) {
318 pr_err("EFI data located above 4GB, disabling EFI.\n");
319 return -EINVAL;
320 }
321#endif
322 } else {
323 efi_system_table_32_t *systab32;
324
Daniel Kiperabc93f82014-06-30 19:52:56 +0200325 systab32 = early_memremap((unsigned long)phys,
Olof Johansson1adbfa32012-02-12 13:24:29 -0800326 sizeof(*systab32));
327 if (systab32 == NULL) {
328 pr_err("Couldn't map the system table!\n");
329 return -ENOMEM;
330 }
331
332 efi_systab.hdr = systab32->hdr;
333 efi_systab.fw_vendor = systab32->fw_vendor;
334 efi_systab.fw_revision = systab32->fw_revision;
335 efi_systab.con_in_handle = systab32->con_in_handle;
336 efi_systab.con_in = systab32->con_in;
337 efi_systab.con_out_handle = systab32->con_out_handle;
338 efi_systab.con_out = systab32->con_out;
339 efi_systab.stderr_handle = systab32->stderr_handle;
340 efi_systab.stderr = systab32->stderr;
341 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
342 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
343 efi_systab.nr_tables = systab32->nr_tables;
344 efi_systab.tables = systab32->tables;
345
Daniel Kiperabc93f82014-06-30 19:52:56 +0200346 early_memunmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800347 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800348
Huang, Ying5b836832008-01-30 13:31:19 +0100349 efi.systab = &efi_systab;
350
351 /*
352 * Verify the EFI Table
353 */
Olof Johansson140bf272012-02-12 13:24:28 -0800354 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800355 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800356 return -EINVAL;
357 }
Huang, Ying5b836832008-01-30 13:31:19 +0100358 if ((efi.systab->hdr.revision >> 16) == 0)
Joe Perches9b7d2042014-01-03 16:08:48 -0800359 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100360 efi.systab->hdr.revision >> 16,
361 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800362
Matt Fleming0f8093a2014-01-15 13:36:33 +0000363 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
364
Olof Johansson140bf272012-02-12 13:24:28 -0800365 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800366}
Huang, Ying5b836832008-01-30 13:31:19 +0100367
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000368static int __init efi_runtime_init32(void)
369{
370 efi_runtime_services_32_t *runtime;
371
Daniel Kiperabc93f82014-06-30 19:52:56 +0200372 runtime = early_memremap((unsigned long)efi.systab->runtime,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000373 sizeof(efi_runtime_services_32_t));
374 if (!runtime) {
375 pr_err("Could not map the runtime service table!\n");
376 return -ENOMEM;
377 }
378
379 /*
380 * We will only need *early* access to the following two
381 * EFI runtime services before set_virtual_address_map
382 * is invoked.
383 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000384 efi_phys.set_virtual_address_map =
385 (efi_set_virtual_address_map_t *)
386 (unsigned long)runtime->set_virtual_address_map;
Daniel Kiperabc93f82014-06-30 19:52:56 +0200387 early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000388
389 return 0;
390}
391
392static int __init efi_runtime_init64(void)
393{
394 efi_runtime_services_64_t *runtime;
395
Daniel Kiperabc93f82014-06-30 19:52:56 +0200396 runtime = early_memremap((unsigned long)efi.systab->runtime,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000397 sizeof(efi_runtime_services_64_t));
398 if (!runtime) {
399 pr_err("Could not map the runtime service table!\n");
400 return -ENOMEM;
401 }
402
403 /*
404 * We will only need *early* access to the following two
405 * EFI runtime services before set_virtual_address_map
406 * is invoked.
407 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000408 efi_phys.set_virtual_address_map =
409 (efi_set_virtual_address_map_t *)
410 (unsigned long)runtime->set_virtual_address_map;
Daniel Kiperabc93f82014-06-30 19:52:56 +0200411 early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000412
413 return 0;
414}
415
Olof Johansson140bf272012-02-12 13:24:28 -0800416static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800417{
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000418 int rv;
Huang, Ying5b836832008-01-30 13:31:19 +0100419
420 /*
421 * Check out the runtime services table. We need to map
422 * the runtime services table so that we can grab the physical
423 * address of several of the EFI runtime functions, needed to
424 * set the firmware into virtual mode.
425 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000426 if (efi_enabled(EFI_64BIT))
427 rv = efi_runtime_init64();
428 else
429 rv = efi_runtime_init32();
430
431 if (rv)
432 return rv;
Olof Johansson140bf272012-02-12 13:24:28 -0800433
Matt Fleming0f8093a2014-01-15 13:36:33 +0000434 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
435
Olof Johansson140bf272012-02-12 13:24:28 -0800436 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800437}
Huang, Ying5b836832008-01-30 13:31:19 +0100438
Olof Johansson140bf272012-02-12 13:24:28 -0800439static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800440{
Huang, Ying5b836832008-01-30 13:31:19 +0100441 /* Map the EFI memory map */
Daniel Kiperabc93f82014-06-30 19:52:56 +0200442 memmap.map = early_memremap((unsigned long)memmap.phys_map,
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100443 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800444 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800445 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800446 return -ENOMEM;
447 }
Huang, Ying5b836832008-01-30 13:31:19 +0100448 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500449
Paul Jackson200001e2008-06-25 05:44:46 -0700450 if (add_efi_memmap)
451 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800452
Matt Fleming0f8093a2014-01-15 13:36:33 +0000453 set_bit(EFI_MEMMAP, &efi.flags);
454
Olof Johansson140bf272012-02-12 13:24:28 -0800455 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800456}
457
458void __init efi_init(void)
459{
460 efi_char16_t *c16;
461 char vendor[100] = "unknown";
462 int i = 0;
463 void *tmp;
464
465#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800466 if (boot_params.efi_info.efi_systab_hi ||
467 boot_params.efi_info.efi_memmap_hi) {
468 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800469 return;
470 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800471 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
472#else
473 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800474 (boot_params.efi_info.efi_systab |
475 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800476#endif
477
Matt Fleming83e68182012-11-14 09:42:35 +0000478 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800479 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000480
Matt Fleming3e909592014-01-15 13:21:22 +0000481 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800482
Dave Younga0998eb2013-12-20 18:02:17 +0800483 efi.config_table = (unsigned long)efi.systab->tables;
484 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
485 efi.runtime = (unsigned long)efi.systab->runtime;
486
Olof Johansson83e7ee62012-02-12 13:24:25 -0800487 /*
488 * Show what we know for posterity
489 */
Daniel Kiperabc93f82014-06-30 19:52:56 +0200490 c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800491 if (c16) {
492 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
493 vendor[i] = *c16++;
494 vendor[i] = '\0';
495 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800496 pr_err("Could not map the firmware vendor!\n");
Daniel Kiperabc93f82014-06-30 19:52:56 +0200497 early_memunmap(tmp, 2);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800498
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800499 pr_info("EFI v%u.%.02u by %s\n",
500 efi.systab->hdr.revision >> 16,
501 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800502
Dave Young1fec0532013-12-20 18:02:19 +0800503 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
504 return;
505
Leif Lindholm272686b2013-09-05 11:34:54 +0100506 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800507 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000508
Olof Johansson1adbfa32012-02-12 13:24:29 -0800509 /*
510 * Note: We currently don't support runtime services on an EFI
511 * that doesn't match the kernel 32/64-bit mode.
512 */
513
Matt Fleming7d453ee2014-01-10 18:52:06 +0000514 if (!efi_runtime_supported())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800515 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000516 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000517 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000518 return;
Olof Johansson140bf272012-02-12 13:24:28 -0800519 }
Matt Fleming83e68182012-11-14 09:42:35 +0000520 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800521 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000522
Matt Fleming3e909592014-01-15 13:21:22 +0000523 set_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming83e68182012-11-14 09:42:35 +0000524
Huang, Ying5b836832008-01-30 13:31:19 +0100525 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100526}
527
Josh Triplett2223af32012-09-28 17:57:05 -0700528void __init efi_late_init(void)
529{
530 efi_bgrt_init();
531}
532
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400533void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
534{
535 u64 addr, npages;
536
537 addr = md->virt_addr;
538 npages = md->num_pages;
539
540 memrange_efi_to_native(&addr, &npages);
541
542 if (executable)
543 set_memory_x(addr, npages);
544 else
545 set_memory_nx(addr, npages);
546}
547
Borislav Petkovc55d0162014-02-14 08:24:24 +0100548void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100549{
550 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100551 void *p;
552
Huang, Yinga2172e22008-01-30 13:33:55 +0100553 /* Make EFI runtime service code area executable */
554 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
555 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100556
557 if (md->type != EFI_RUNTIME_SERVICES_CODE)
558 continue;
559
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400560 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100561 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100562}
Huang, Yinga2172e22008-01-30 13:33:55 +0100563
Matt Fleming3e8fa262012-10-19 13:25:46 +0100564void efi_memory_uc(u64 addr, unsigned long size)
565{
566 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
567 u64 npages;
568
569 npages = round_up(size, page_shift) / page_shift;
570 memrange_efi_to_native(&addr, &npages);
571 set_memory_uc(addr, npages);
572}
573
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100574void __init old_map_region(efi_memory_desc_t *md)
575{
576 u64 start_pfn, end_pfn, end;
577 unsigned long size;
578 void *va;
579
580 start_pfn = PFN_DOWN(md->phys_addr);
581 size = md->num_pages << PAGE_SHIFT;
582 end = md->phys_addr + size;
583 end_pfn = PFN_UP(end);
584
585 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
586 va = __va(md->phys_addr);
587
588 if (!(md->attribute & EFI_MEMORY_WB))
589 efi_memory_uc((u64)(unsigned long)va, size);
590 } else
591 va = efi_ioremap(md->phys_addr, size,
592 md->type, md->attribute);
593
594 md->virt_addr = (u64) (unsigned long) va;
595 if (!va)
596 pr_err("ioremap of 0x%llX failed!\n",
597 (unsigned long long)md->phys_addr);
598}
599
Dave Young481f75c2013-12-20 18:02:16 +0800600/* Merge contiguous regions of the same type and attribute */
601static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100602{
Dave Young481f75c2013-12-20 18:02:16 +0800603 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400604 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100605
Matthew Garrett202f9d02011-05-05 15:19:44 -0400606 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
607 u64 prev_size;
608 md = p;
609
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{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100649#ifdef CONFIG_KEXEC
Dave Young926172d2013-12-20 18:02:18 +0800650 efi_memory_desc_t *md;
651 void *tmp, *p, *q = NULL;
652 int count = 0;
653
Dave Younga3530e82014-05-30 11:20:10 +0800654 if (efi_enabled(EFI_OLD_MEMMAP))
655 return;
656
Dave Young926172d2013-12-20 18:02:18 +0800657 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
658 md = p;
659
660 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
661 (md->type == EFI_BOOT_SERVICES_CODE) ||
662 (md->type == EFI_BOOT_SERVICES_DATA))
663 continue;
664 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
665 if (!tmp)
666 goto out;
667 q = tmp;
668
669 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
670 count++;
671 }
672
Matt Fleming518548a2013-12-21 16:09:46 +0000673 efi_runtime_map_setup(q, count, memmap.desc_size);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100674 return;
Dave Young926172d2013-12-20 18:02:18 +0800675
Dave Young926172d2013-12-20 18:02:18 +0800676out:
677 kfree(q);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100678 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
679#endif
Dave Young1fec0532013-12-20 18:02:19 +0800680}
681
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100682static void *realloc_pages(void *old_memmap, int old_shift)
Dave Young481f75c2013-12-20 18:02:16 +0800683{
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100684 void *ret;
685
686 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
687 if (!ret)
688 goto out;
689
690 /*
691 * A first-time allocation doesn't have anything to copy.
692 */
693 if (!old_memmap)
694 return ret;
695
696 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
697
698out:
699 free_pages((unsigned long)old_memmap, old_shift);
700 return ret;
701}
702
703/*
704 * Map the efi memory ranges of the runtime services and update new_mmap with
705 * virtual addresses.
706 */
707static void * __init efi_map_regions(int *count, int *pg_shift)
708{
709 void *p, *new_memmap = NULL;
710 unsigned long left = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800711 efi_memory_desc_t *md;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400712
Huang, Ying5b836832008-01-30 13:31:19 +0100713 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
714 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700715 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
716#ifdef CONFIG_X86_64
717 if (md->type != EFI_BOOT_SERVICES_CODE &&
718 md->type != EFI_BOOT_SERVICES_DATA)
719#endif
720 continue;
721 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100722
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100723 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800724 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100725
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100726 if (left < memmap.desc_size) {
727 new_memmap = realloc_pages(new_memmap, *pg_shift);
728 if (!new_memmap)
729 return NULL;
730
731 left += PAGE_SIZE << *pg_shift;
732 (*pg_shift)++;
733 }
734
Dave Young481f75c2013-12-20 18:02:16 +0800735 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400736 memmap.desc_size);
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100737
738 left -= memmap.desc_size;
Dave Young481f75c2013-12-20 18:02:16 +0800739 (*count)++;
740 }
741
742 return new_memmap;
Dave Young481f75c2013-12-20 18:02:16 +0800743}
744
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100745static void __init kexec_enter_virtual_mode(void)
746{
747#ifdef CONFIG_KEXEC
748 efi_memory_desc_t *md;
749 void *p;
750
751 efi.systab = NULL;
752
753 /*
754 * We don't do virtual mode, since we don't do runtime services, on
755 * non-native EFI
756 */
757 if (!efi_is_native()) {
758 efi_unmap_memmap();
759 return;
760 }
761
762 /*
763 * Map efi regions which were passed via setup_data. The virt_addr is a
764 * fixed addr which was used in first kernel of a kexec boot.
765 */
766 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
767 md = p;
768 efi_map_region_fixed(md); /* FIXME: add error handling */
769 get_systab_virt_addr(md);
770 }
771
772 save_runtime_map();
773
774 BUG_ON(!efi.systab);
775
776 efi_sync_low_kernel_mappings();
777
778 /*
779 * Now that EFI is in virtual mode, update the function
780 * pointers in the runtime service table to the new virtual addresses.
781 *
782 * Call EFI services through wrapper functions.
783 */
784 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming994448f2014-03-05 18:15:37 +0000785
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200786 efi_native_runtime_setup();
Matt Fleming994448f2014-03-05 18:15:37 +0000787
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100788 efi.set_virtual_address_map = NULL;
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100789
790 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
791 runtime_code_page_mkexec();
792
793 /* clean DUMMY object */
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700794 efi_delete_dummy_variable();
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100795#endif
796}
797
Dave Young481f75c2013-12-20 18:02:16 +0800798/*
799 * This function will switch the EFI runtime services to virtual mode.
800 * Essentially, we look through the EFI memmap and map every region that
801 * has the runtime attribute bit set in its memory descriptor into the
802 * ->trampoline_pgd page table using a top-down VA allocation scheme.
803 *
804 * The old method which used to update that memory descriptor with the
805 * virtual address obtained from ioremap() is still supported when the
806 * kernel is booted with efi=old_map on its command line. Same old
807 * method enabled the runtime services to be called without having to
808 * thunk back into physical mode for every invocation.
809 *
810 * The new method does a pagetable switch in a preemption-safe manner
811 * so that we're in a different address space when calling a runtime
812 * function. For function arguments passing we do copy the PGDs of the
813 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +0800814 *
815 * Specially for kexec boot, efi runtime maps in previous kernel should
816 * be passed in via setup_data. In that case runtime ranges will be mapped
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100817 * to the same virtual addresses as the first kernel, see
818 * kexec_enter_virtual_mode().
Dave Young481f75c2013-12-20 18:02:16 +0800819 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100820static void __init __efi_enter_virtual_mode(void)
Dave Young481f75c2013-12-20 18:02:16 +0800821{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100822 int count = 0, pg_shift = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800823 void *new_memmap = NULL;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100824 efi_status_t status;
Dave Young481f75c2013-12-20 18:02:16 +0800825
826 efi.systab = NULL;
827
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100828 efi_merge_regions();
829 new_memmap = efi_map_regions(&count, &pg_shift);
830 if (!new_memmap) {
831 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
832 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100833 }
Dave Young926172d2013-12-20 18:02:18 +0800834
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100835 save_runtime_map();
836
Huang, Ying5b836832008-01-30 13:31:19 +0100837 BUG_ON(!efi.systab);
838
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100839 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
840 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100841
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100842 efi_sync_low_kernel_mappings();
Borislav Petkov11cc8512014-01-18 12:48:15 +0100843 efi_dump_pagetable();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100844
Matt Fleming994448f2014-03-05 18:15:37 +0000845 if (efi_is_native()) {
846 status = phys_efi_set_virtual_address_map(
847 memmap.desc_size * count,
848 memmap.desc_size,
849 memmap.desc_version,
850 (efi_memory_desc_t *)__pa(new_memmap));
851 } else {
852 status = efi_thunk_set_virtual_address_map(
853 efi_phys.set_virtual_address_map,
854 memmap.desc_size * count,
855 memmap.desc_size,
856 memmap.desc_version,
857 (efi_memory_desc_t *)__pa(new_memmap));
858 }
Huang, Ying5b836832008-01-30 13:31:19 +0100859
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100860 if (status != EFI_SUCCESS) {
861 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
862 status);
863 panic("EFI call to SetVirtualAddressMap() failed!");
Huang, Ying5b836832008-01-30 13:31:19 +0100864 }
865
866 /*
867 * Now that EFI is in virtual mode, update the function
868 * pointers in the runtime service table to the new virtual addresses.
869 *
870 * Call EFI services through wrapper functions.
871 */
Matt Fleming712ba9e2013-01-25 10:07:25 +0000872 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000873
874 if (efi_is_native())
Ard Biesheuvel022ee6c2014-06-26 12:09:05 +0200875 efi_native_runtime_setup();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000876 else
877 efi_thunk_runtime_setup();
878
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400879 efi.set_virtual_address_map = NULL;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100880
Borislav Petkovc55d0162014-02-14 08:24:24 +0100881 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -0800882
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100883 /*
884 * We mapped the descriptor array into the EFI pagetable above but we're
885 * not unmapping it here. Here's why:
886 *
887 * We're copying select PGDs from the kernel page table to the EFI page
888 * table and when we do so and make changes to those PGDs like unmapping
889 * stuff from them, those changes appear in the kernel page table and we
890 * go boom.
891 *
892 * From setup_real_mode():
893 *
894 * ...
895 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
896 *
897 * In this particular case, our allocation is in PGD 0 of the EFI page
898 * table but we've copied that PGD from PGD[272] of the EFI page table:
899 *
900 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
901 *
902 * where the direct memory mapping in kernel space is.
903 *
904 * new_memmap's VA comes from that direct mapping and thus clearing it,
905 * it would get cleared in the kernel page table too.
906 *
907 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
908 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100909 free_pages((unsigned long)new_memmap, pg_shift);
Matthew Garrettf8b84042013-06-01 16:06:20 -0400910
911 /* clean DUMMY object */
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700912 efi_delete_dummy_variable();
Huang, Ying5b836832008-01-30 13:31:19 +0100913}
914
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100915void __init efi_enter_virtual_mode(void)
916{
917 if (efi_setup)
918 kexec_enter_virtual_mode();
919 else
920 __efi_enter_virtual_mode();
921}
922
Huang, Ying5b836832008-01-30 13:31:19 +0100923/*
924 * Convenience functions to obtain memory types and attributes
925 */
926u32 efi_mem_type(unsigned long phys_addr)
927{
928 efi_memory_desc_t *md;
929 void *p;
930
Matt Fleming83e68182012-11-14 09:42:35 +0000931 if (!efi_enabled(EFI_MEMMAP))
932 return 0;
933
Huang, Ying5b836832008-01-30 13:31:19 +0100934 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
935 md = p;
936 if ((md->phys_addr <= phys_addr) &&
937 (phys_addr < (md->phys_addr +
938 (md->num_pages << EFI_PAGE_SHIFT))))
939 return md->type;
940 }
941 return 0;
942}
943
944u64 efi_mem_attributes(unsigned long phys_addr)
945{
946 efi_memory_desc_t *md;
947 void *p;
948
Daniel Kiper67a9b9c2014-06-30 19:52:57 +0200949 if (!efi_enabled(EFI_MEMMAP))
950 return 0;
951
Huang, Ying5b836832008-01-30 13:31:19 +0100952 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
953 md = p;
954 if ((md->phys_addr <= phys_addr) &&
955 (phys_addr < (md->phys_addr +
956 (md->num_pages << EFI_PAGE_SHIFT))))
957 return md->attribute;
958 }
959 return 0;
960}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000961
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100962static int __init parse_efi_cmdline(char *str)
963{
964 if (*str == '=')
965 str++;
966
967 if (!strncmp(str, "old_map", 7))
Matt Fleming0f8093a2014-01-15 13:36:33 +0000968 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100969
970 return 0;
971}
972early_param("efi", parse_efi_cmdline);