Merge remote-tracking branch 'tip/x86/urgent' into efi-for-mingo
[deliverable/linux.git] / arch / x86 / platform / efi / efi.c
CommitLineData
5b83683f
HY
1/*
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>
d2f7cbe7
BP
15 * Copyright (C) 2013 SuSE Labs
16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
5b83683f
HY
17 *
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
e3cb3f5a
OJ
31#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
5b83683f
HY
33#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/efi.h>
2223af38 36#include <linux/efi-bgrt.h>
69c60c88 37#include <linux/export.h>
5b83683f 38#include <linux/bootmem.h>
0d01ff25 39#include <linux/slab.h>
a9ce6bc1 40#include <linux/memblock.h>
5b83683f
HY
41#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>
a2172e25
HY
51#include <asm/cacheflush.h>
52#include <asm/tlbflush.h>
7bd867df 53#include <asm/x86_init.h>
3195ef59 54#include <asm/rtc.h>
a5d90c92 55#include <asm/uv/uv.h>
5b83683f 56
f4fccac0 57#define EFI_DEBUG
5b83683f 58
f8b84043
MG
59#define EFI_MIN_RESERVE 5120
60
61#define EFI_DUMMY_GUID \
62 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
63
64static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
31ff2f20 65
5b83683f
HY
66struct efi_memory_map memmap;
67
ecaea42e 68static struct efi efi_phys __initdata;
5b83683f
HY
69static efi_system_table_t efi_systab __initdata;
70
9b7d2049 71static efi_config_table_type_t arch_tables[] __initdata = {
272686bf
LL
72#ifdef CONFIG_X86_UV
73 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
74#endif
722da9d2 75 {NULL_GUID, NULL, NULL},
272686bf
LL
76};
77
1fec0533 78u64 efi_setup; /* efi setup_data physical address */
926172d4 79
9b7d2049 80static bool disable_runtime __initdata = false;
8b2cb7a8
HY
81static int __init setup_noefi(char *arg)
82{
fb834c7a 83 disable_runtime = true;
8b2cb7a8
HY
84 return 0;
85}
86early_param("noefi", setup_noefi);
87
200001eb
PJ
88int add_efi_memmap;
89EXPORT_SYMBOL(add_efi_memmap);
90
91static int __init setup_add_efi_memmap(char *arg)
92{
93 add_efi_memmap = 1;
94 return 0;
95}
96early_param("add_efi_memmap", setup_add_efi_memmap);
97
8c58bf3e
RW
98static bool efi_no_storage_paranoia;
99
100static int __init setup_storage_paranoia(char *arg)
101{
102 efi_no_storage_paranoia = true;
103 return 0;
104}
105early_param("efi_no_storage_paranoia", setup_storage_paranoia);
106
5b83683f
HY
107static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
108{
ef68c8f8
JB
109 unsigned long flags;
110 efi_status_t status;
111
112 spin_lock_irqsave(&rtc_lock, flags);
113 status = efi_call_virt2(get_time, tm, tc);
114 spin_unlock_irqrestore(&rtc_lock, flags);
115 return status;
5b83683f
HY
116}
117
118static efi_status_t virt_efi_set_time(efi_time_t *tm)
119{
ef68c8f8
JB
120 unsigned long flags;
121 efi_status_t status;
122
123 spin_lock_irqsave(&rtc_lock, flags);
124 status = efi_call_virt1(set_time, tm);
125 spin_unlock_irqrestore(&rtc_lock, flags);
126 return status;
5b83683f
HY
127}
128
129static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
130 efi_bool_t *pending,
131 efi_time_t *tm)
132{
ef68c8f8
JB
133 unsigned long flags;
134 efi_status_t status;
135
136 spin_lock_irqsave(&rtc_lock, flags);
137 status = efi_call_virt3(get_wakeup_time,
138 enabled, pending, tm);
139 spin_unlock_irqrestore(&rtc_lock, flags);
140 return status;
5b83683f
HY
141}
142
143static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
144{
ef68c8f8
JB
145 unsigned long flags;
146 efi_status_t status;
147
148 spin_lock_irqsave(&rtc_lock, flags);
149 status = efi_call_virt2(set_wakeup_time,
150 enabled, tm);
151 spin_unlock_irqrestore(&rtc_lock, flags);
152 return status;
5b83683f
HY
153}
154
155static efi_status_t virt_efi_get_variable(efi_char16_t *name,
156 efi_guid_t *vendor,
157 u32 *attr,
158 unsigned long *data_size,
159 void *data)
160{
161 return efi_call_virt5(get_variable,
162 name, vendor, attr,
163 data_size, data);
164}
165
166static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
167 efi_char16_t *name,
168 efi_guid_t *vendor)
169{
f8b84043
MG
170 return efi_call_virt3(get_next_variable,
171 name_size, name, vendor);
5b83683f
HY
172}
173
174static efi_status_t virt_efi_set_variable(efi_char16_t *name,
175 efi_guid_t *vendor,
f7a2d73f 176 u32 attr,
5b83683f
HY
177 unsigned long data_size,
178 void *data)
179{
f8b84043
MG
180 return efi_call_virt5(set_variable,
181 name, vendor, attr,
182 data_size, data);
5b83683f
HY
183}
184
3b370237
MG
185static efi_status_t virt_efi_query_variable_info(u32 attr,
186 u64 *storage_space,
187 u64 *remaining_space,
188 u64 *max_variable_size)
189{
190 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
191 return EFI_UNSUPPORTED;
192
193 return efi_call_virt4(query_variable_info, attr, storage_space,
194 remaining_space, max_variable_size);
195}
196
5b83683f
HY
197static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
198{
199 return efi_call_virt1(get_next_high_mono_count, count);
200}
201
202static void virt_efi_reset_system(int reset_type,
203 efi_status_t status,
204 unsigned long data_size,
205 efi_char16_t *data)
206{
207 efi_call_virt4(reset_system, reset_type, status,
208 data_size, data);
209}
210
3b370237
MG
211static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
212 unsigned long count,
213 unsigned long sg_list)
214{
215 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
216 return EFI_UNSUPPORTED;
217
218 return efi_call_virt3(update_capsule, capsules, count, sg_list);
219}
220
221static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
222 unsigned long count,
223 u64 *max_size,
224 int *reset_type)
225{
226 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
227 return EFI_UNSUPPORTED;
228
229 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
230 reset_type);
231}
232
5b83683f
HY
233static efi_status_t __init phys_efi_set_virtual_address_map(
234 unsigned long memory_map_size,
235 unsigned long descriptor_size,
236 u32 descriptor_version,
237 efi_memory_desc_t *virtual_map)
238{
239 efi_status_t status;
240
241 efi_call_phys_prelog();
242 status = efi_call_phys4(efi_phys.set_virtual_address_map,
243 memory_map_size, descriptor_size,
244 descriptor_version, virtual_map);
245 efi_call_phys_epilog();
246 return status;
247}
248
11520e5e
LT
249static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
250 efi_time_cap_t *tc)
251{
252 unsigned long flags;
253 efi_status_t status;
254
255 spin_lock_irqsave(&rtc_lock, flags);
256 efi_call_phys_prelog();
257 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
258 virt_to_phys(tc));
259 efi_call_phys_epilog();
260 spin_unlock_irqrestore(&rtc_lock, flags);
261 return status;
262}
263
3565184e 264int efi_set_rtc_mmss(const struct timespec *now)
5b83683f 265{
3565184e 266 unsigned long nowtime = now->tv_sec;
9b7d2049
JP
267 efi_status_t status;
268 efi_time_t eft;
269 efi_time_cap_t cap;
3195ef59 270 struct rtc_time tm;
5b83683f
HY
271
272 status = efi.get_time(&eft, &cap);
273 if (status != EFI_SUCCESS) {
e3cb3f5a 274 pr_err("Oops: efitime: can't read time!\n");
5b83683f
HY
275 return -1;
276 }
277
3195ef59
PB
278 rtc_time_to_tm(nowtime, &tm);
279 if (!rtc_valid_tm(&tm)) {
280 eft.year = tm.tm_year + 1900;
281 eft.month = tm.tm_mon + 1;
282 eft.day = tm.tm_mday;
283 eft.minute = tm.tm_min;
284 eft.second = tm.tm_sec;
285 eft.nanosecond = 0;
286 } else {
9b7d2049
JP
287 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
288 __func__, nowtime);
3195ef59
PB
289 return -1;
290 }
5b83683f
HY
291
292 status = efi.set_time(&eft);
293 if (status != EFI_SUCCESS) {
e3cb3f5a 294 pr_err("Oops: efitime: can't write time!\n");
5b83683f
HY
295 return -1;
296 }
297 return 0;
298}
299
3565184e 300void efi_get_time(struct timespec *now)
5b83683f
HY
301{
302 efi_status_t status;
303 efi_time_t eft;
304 efi_time_cap_t cap;
305
306 status = efi.get_time(&eft, &cap);
307 if (status != EFI_SUCCESS)
e3cb3f5a 308 pr_err("Oops: efitime: can't read time!\n");
5b83683f 309
3565184e
DV
310 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
311 eft.minute, eft.second);
312 now->tv_nsec = 0;
5b83683f
HY
313}
314
69c91893
PJ
315/*
316 * Tell the kernel about the EFI memory map. This might include
317 * more than the max 128 entries that can fit in the e820 legacy
318 * (zeropage) memory map.
319 */
320
200001eb 321static void __init do_add_efi_memmap(void)
69c91893
PJ
322{
323 void *p;
324
325 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
326 efi_memory_desc_t *md = p;
327 unsigned long long start = md->phys_addr;
328 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
329 int e820_type;
330
e2a71476
CW
331 switch (md->type) {
332 case EFI_LOADER_CODE:
333 case EFI_LOADER_DATA:
334 case EFI_BOOT_SERVICES_CODE:
335 case EFI_BOOT_SERVICES_DATA:
336 case EFI_CONVENTIONAL_MEMORY:
337 if (md->attribute & EFI_MEMORY_WB)
338 e820_type = E820_RAM;
339 else
340 e820_type = E820_RESERVED;
341 break;
342 case EFI_ACPI_RECLAIM_MEMORY:
343 e820_type = E820_ACPI;
344 break;
345 case EFI_ACPI_MEMORY_NVS:
346 e820_type = E820_NVS;
347 break;
348 case EFI_UNUSABLE_MEMORY:
349 e820_type = E820_UNUSABLE;
350 break;
351 default:
352 /*
353 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
354 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
355 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
356 */
69c91893 357 e820_type = E820_RESERVED;
e2a71476
CW
358 break;
359 }
d0be6bde 360 e820_add_region(start, size, e820_type);
69c91893
PJ
361 }
362 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
363}
364
1adbfa35 365int __init efi_memblock_x86_reserve_range(void)
ecacf09f 366{
15b9c359 367 struct efi_info *e = &boot_params.efi_info;
ecacf09f
HY
368 unsigned long pmap;
369
05486fa7 370#ifdef CONFIG_X86_32
1adbfa35 371 /* Can't handle data above 4GB at this time */
15b9c359 372 if (e->efi_memmap_hi) {
1adbfa35
OJ
373 pr_err("Memory map is above 4GB, disabling EFI.\n");
374 return -EINVAL;
375 }
15b9c359 376 pmap = e->efi_memmap;
05486fa7 377#else
15b9c359 378 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
ecacf09f 379#endif
15b9c359
BP
380 memmap.phys_map = (void *)pmap;
381 memmap.nr_map = e->efi_memmap_size /
382 e->efi_memdesc_size;
383 memmap.desc_size = e->efi_memdesc_size;
384 memmap.desc_version = e->efi_memdesc_version;
385
24aa0788 386 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
1adbfa35 387
258f6fd7
LL
388 efi.memmap = &memmap;
389
1adbfa35 390 return 0;
ecacf09f
HY
391}
392
5b83683f
HY
393static void __init print_efi_memmap(void)
394{
f4fccac0 395#ifdef EFI_DEBUG
5b83683f
HY
396 efi_memory_desc_t *md;
397 void *p;
398 int i;
399
400 for (p = memmap.map, i = 0;
401 p < memmap.map_end;
402 p += memmap.desc_size, i++) {
403 md = p;
9b7d2049 404 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
5b83683f
HY
405 i, md->type, md->attribute, md->phys_addr,
406 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
407 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
408 }
5b83683f 409#endif /* EFI_DEBUG */
f4fccac0 410}
5b83683f 411
916f676f
MG
412void __init efi_reserve_boot_services(void)
413{
414 void *p;
415
416 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
417 efi_memory_desc_t *md = p;
7d68dc3f
ML
418 u64 start = md->phys_addr;
419 u64 size = md->num_pages << EFI_PAGE_SHIFT;
916f676f
MG
420
421 if (md->type != EFI_BOOT_SERVICES_CODE &&
422 md->type != EFI_BOOT_SERVICES_DATA)
423 continue;
7d68dc3f
ML
424 /* Only reserve where possible:
425 * - Not within any already allocated areas
426 * - Not over any memory area (really needed, if above?)
427 * - Not within any part of the kernel
428 * - Not the bios reserved area
429 */
a7f84f03 430 if ((start + size > __pa_symbol(_text)
fc8d7826 431 && start <= __pa_symbol(_end)) ||
7d68dc3f 432 !e820_all_mapped(start, start+size, E820_RAM) ||
bf61549a 433 memblock_is_region_reserved(start, size)) {
7d68dc3f
ML
434 /* Could not reserve, skip it */
435 md->num_pages = 0;
9b7d2049
JP
436 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
437 start, start+size-1);
7d68dc3f 438 } else
24aa0788 439 memblock_reserve(start, size);
916f676f
MG
440 }
441}
442
5189c2a7 443void __init efi_unmap_memmap(void)
78510792 444{
3e909599 445 clear_bit(EFI_MEMMAP, &efi.flags);
78510792
JT
446 if (memmap.map) {
447 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
448 memmap.map = NULL;
449 }
450}
451
452void __init efi_free_boot_services(void)
916f676f
MG
453{
454 void *p;
455
5189c2a7 456 if (!efi_is_native())
78510792
JT
457 return;
458
916f676f
MG
459 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
460 efi_memory_desc_t *md = p;
461 unsigned long long start = md->phys_addr;
462 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
463
464 if (md->type != EFI_BOOT_SERVICES_CODE &&
465 md->type != EFI_BOOT_SERVICES_DATA)
466 continue;
467
7d68dc3f
ML
468 /* Could not reserve boot area */
469 if (!size)
470 continue;
471
916f676f
MG
472 free_bootmem_late(start, size);
473 }
78510792
JT
474
475 efi_unmap_memmap();
916f676f
MG
476}
477
140bf275 478static int __init efi_systab_init(void *phys)
5b83683f 479{
83e68189 480 if (efi_enabled(EFI_64BIT)) {
1adbfa35 481 efi_system_table_64_t *systab64;
1fec0533 482 struct efi_setup_data *data = NULL;
1adbfa35
OJ
483 u64 tmp = 0;
484
1fec0533
DY
485 if (efi_setup) {
486 data = early_memremap(efi_setup, sizeof(*data));
487 if (!data)
488 return -ENOMEM;
489 }
1adbfa35
OJ
490 systab64 = early_ioremap((unsigned long)phys,
491 sizeof(*systab64));
492 if (systab64 == NULL) {
493 pr_err("Couldn't map the system table!\n");
1fec0533
DY
494 if (data)
495 early_iounmap(data, sizeof(*data));
1adbfa35
OJ
496 return -ENOMEM;
497 }
498
499 efi_systab.hdr = systab64->hdr;
1fec0533
DY
500 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
501 systab64->fw_vendor;
502 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
1adbfa35
OJ
503 efi_systab.fw_revision = systab64->fw_revision;
504 efi_systab.con_in_handle = systab64->con_in_handle;
505 tmp |= systab64->con_in_handle;
506 efi_systab.con_in = systab64->con_in;
507 tmp |= systab64->con_in;
508 efi_systab.con_out_handle = systab64->con_out_handle;
509 tmp |= systab64->con_out_handle;
510 efi_systab.con_out = systab64->con_out;
511 tmp |= systab64->con_out;
512 efi_systab.stderr_handle = systab64->stderr_handle;
513 tmp |= systab64->stderr_handle;
514 efi_systab.stderr = systab64->stderr;
515 tmp |= systab64->stderr;
1fec0533
DY
516 efi_systab.runtime = data ?
517 (void *)(unsigned long)data->runtime :
518 (void *)(unsigned long)systab64->runtime;
519 tmp |= data ? data->runtime : systab64->runtime;
1adbfa35
OJ
520 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
521 tmp |= systab64->boottime;
522 efi_systab.nr_tables = systab64->nr_tables;
1fec0533
DY
523 efi_systab.tables = data ? (unsigned long)data->tables :
524 systab64->tables;
525 tmp |= data ? data->tables : systab64->tables;
1adbfa35
OJ
526
527 early_iounmap(systab64, sizeof(*systab64));
1fec0533
DY
528 if (data)
529 early_iounmap(data, sizeof(*data));
1adbfa35
OJ
530#ifdef CONFIG_X86_32
531 if (tmp >> 32) {
532 pr_err("EFI data located above 4GB, disabling EFI.\n");
533 return -EINVAL;
534 }
535#endif
536 } else {
537 efi_system_table_32_t *systab32;
538
539 systab32 = early_ioremap((unsigned long)phys,
540 sizeof(*systab32));
541 if (systab32 == NULL) {
542 pr_err("Couldn't map the system table!\n");
543 return -ENOMEM;
544 }
545
546 efi_systab.hdr = systab32->hdr;
547 efi_systab.fw_vendor = systab32->fw_vendor;
548 efi_systab.fw_revision = systab32->fw_revision;
549 efi_systab.con_in_handle = systab32->con_in_handle;
550 efi_systab.con_in = systab32->con_in;
551 efi_systab.con_out_handle = systab32->con_out_handle;
552 efi_systab.con_out = systab32->con_out;
553 efi_systab.stderr_handle = systab32->stderr_handle;
554 efi_systab.stderr = systab32->stderr;
555 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
556 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
557 efi_systab.nr_tables = systab32->nr_tables;
558 efi_systab.tables = systab32->tables;
559
560 early_iounmap(systab32, sizeof(*systab32));
140bf275 561 }
1adbfa35 562
5b83683f
HY
563 efi.systab = &efi_systab;
564
565 /*
566 * Verify the EFI Table
567 */
140bf275 568 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
e3cb3f5a 569 pr_err("System table signature incorrect!\n");
140bf275
OJ
570 return -EINVAL;
571 }
5b83683f 572 if ((efi.systab->hdr.revision >> 16) == 0)
9b7d2049 573 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
5b83683f
HY
574 efi.systab->hdr.revision >> 16,
575 efi.systab->hdr.revision & 0xffff);
140bf275 576
0f8093a9
MF
577 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
578
140bf275 579 return 0;
83e7ee66 580}
5b83683f 581
140bf275 582static int __init efi_runtime_init(void)
83e7ee66
OJ
583{
584 efi_runtime_services_t *runtime;
5b83683f
HY
585
586 /*
587 * Check out the runtime services table. We need to map
588 * the runtime services table so that we can grab the physical
589 * address of several of the EFI runtime functions, needed to
590 * set the firmware into virtual mode.
591 */
beacfaac
HY
592 runtime = early_ioremap((unsigned long)efi.systab->runtime,
593 sizeof(efi_runtime_services_t));
140bf275 594 if (!runtime) {
e3cb3f5a 595 pr_err("Could not map the runtime service table!\n");
140bf275
OJ
596 return -ENOMEM;
597 }
598 /*
599 * We will only need *early* access to the following
11520e5e 600 * two EFI runtime services before set_virtual_address_map
140bf275
OJ
601 * is invoked.
602 */
11520e5e 603 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
140bf275
OJ
604 efi_phys.set_virtual_address_map =
605 (efi_set_virtual_address_map_t *)
606 runtime->set_virtual_address_map;
11520e5e
LT
607 /*
608 * Make efi_get_time can be called before entering
609 * virtual mode.
610 */
611 efi.get_time = phys_efi_get_time;
beacfaac 612 early_iounmap(runtime, sizeof(efi_runtime_services_t));
140bf275 613
0f8093a9
MF
614 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
615
140bf275 616 return 0;
83e7ee66 617}
5b83683f 618
140bf275 619static int __init efi_memmap_init(void)
83e7ee66 620{
5b83683f 621 /* Map the EFI memory map */
beacfaac
HY
622 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
623 memmap.nr_map * memmap.desc_size);
140bf275 624 if (memmap.map == NULL) {
e3cb3f5a 625 pr_err("Could not map the memory map!\n");
140bf275
OJ
626 return -ENOMEM;
627 }
5b83683f 628 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
175e438f 629
200001eb
PJ
630 if (add_efi_memmap)
631 do_add_efi_memmap();
140bf275 632
0f8093a9
MF
633 set_bit(EFI_MEMMAP, &efi.flags);
634
140bf275 635 return 0;
83e7ee66
OJ
636}
637
1fec0533
DY
638/*
639 * A number of config table entries get remapped to virtual addresses
640 * after entering EFI virtual mode. However, the kexec kernel requires
641 * their physical addresses therefore we pass them via setup_data and
642 * correct those entries to their respective physical addresses here.
643 *
644 * Currently only handles smbios which is necessary for some firmware
645 * implementation.
646 */
647static int __init efi_reuse_config(u64 tables, int nr_tables)
648{
649 int i, sz, ret = 0;
650 void *p, *tablep;
651 struct efi_setup_data *data;
652
653 if (!efi_setup)
654 return 0;
655
656 if (!efi_enabled(EFI_64BIT))
657 return 0;
658
659 data = early_memremap(efi_setup, sizeof(*data));
660 if (!data) {
661 ret = -ENOMEM;
662 goto out;
663 }
664
665 if (!data->smbios)
666 goto out_memremap;
667
668 sz = sizeof(efi_config_table_64_t);
669
670 p = tablep = early_memremap(tables, nr_tables * sz);
671 if (!p) {
672 pr_err("Could not map Configuration table!\n");
673 ret = -ENOMEM;
674 goto out_memremap;
675 }
676
677 for (i = 0; i < efi.systab->nr_tables; i++) {
678 efi_guid_t guid;
679
680 guid = ((efi_config_table_64_t *)p)->guid;
681
682 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
683 ((efi_config_table_64_t *)p)->table = data->smbios;
684 p += sz;
685 }
686 early_iounmap(tablep, nr_tables * sz);
687
688out_memremap:
689 early_iounmap(data, sizeof(*data));
690out:
691 return ret;
692}
693
83e7ee66
OJ
694void __init efi_init(void)
695{
696 efi_char16_t *c16;
697 char vendor[100] = "unknown";
698 int i = 0;
699 void *tmp;
700
701#ifdef CONFIG_X86_32
1adbfa35
OJ
702 if (boot_params.efi_info.efi_systab_hi ||
703 boot_params.efi_info.efi_memmap_hi) {
704 pr_info("Table located above 4GB, disabling EFI.\n");
1adbfa35
OJ
705 return;
706 }
83e7ee66
OJ
707 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
708#else
709 efi_phys.systab = (efi_system_table_t *)
1adbfa35
OJ
710 (boot_params.efi_info.efi_systab |
711 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
83e7ee66
OJ
712#endif
713
83e68189 714 if (efi_systab_init(efi_phys.systab))
140bf275 715 return;
83e68189 716
3e909599 717 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
83e7ee66 718
a0998eb1
DY
719 efi.config_table = (unsigned long)efi.systab->tables;
720 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
721 efi.runtime = (unsigned long)efi.systab->runtime;
722
83e7ee66
OJ
723 /*
724 * Show what we know for posterity
725 */
726 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
727 if (c16) {
728 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
729 vendor[i] = *c16++;
730 vendor[i] = '\0';
731 } else
e3cb3f5a 732 pr_err("Could not map the firmware vendor!\n");
83e7ee66
OJ
733 early_iounmap(tmp, 2);
734
e3cb3f5a
OJ
735 pr_info("EFI v%u.%.02u by %s\n",
736 efi.systab->hdr.revision >> 16,
737 efi.systab->hdr.revision & 0xffff, vendor);
83e7ee66 738
1fec0533
DY
739 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
740 return;
741
272686bf 742 if (efi_config_init(arch_tables))
140bf275 743 return;
83e68189 744
1adbfa35
OJ
745 /*
746 * Note: We currently don't support runtime services on an EFI
747 * that doesn't match the kernel 32/64-bit mode.
748 */
749
5189c2a7 750 if (!efi_is_native())
1adbfa35 751 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
83e68189 752 else {
fb834c7a 753 if (disable_runtime || efi_runtime_init())
83e68189 754 return;
140bf275 755 }
83e68189 756 if (efi_memmap_init())
140bf275 757 return;
83e68189 758
3e909599 759 set_bit(EFI_MEMMAP, &efi.flags);
83e68189 760
5b83683f 761 print_efi_memmap();
5b83683f
HY
762}
763
2223af38
JT
764void __init efi_late_init(void)
765{
766 efi_bgrt_init();
767}
768
9cd2b07c
MG
769void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
770{
771 u64 addr, npages;
772
773 addr = md->virt_addr;
774 npages = md->num_pages;
775
776 memrange_efi_to_native(&addr, &npages);
777
778 if (executable)
779 set_memory_x(addr, npages);
780 else
781 set_memory_nx(addr, npages);
782}
783
c55d016f 784void __init runtime_code_page_mkexec(void)
a2172e25
HY
785{
786 efi_memory_desc_t *md;
a2172e25
HY
787 void *p;
788
a2172e25
HY
789 /* Make EFI runtime service code area executable */
790 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
791 md = p;
1c083eb2
HY
792
793 if (md->type != EFI_RUNTIME_SERVICES_CODE)
794 continue;
795
9cd2b07c 796 efi_set_executable(md, true);
a2172e25 797 }
a2172e25 798}
a2172e25 799
3e8fa263
MF
800void efi_memory_uc(u64 addr, unsigned long size)
801{
802 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
803 u64 npages;
804
805 npages = round_up(size, page_shift) / page_shift;
806 memrange_efi_to_native(&addr, &npages);
807 set_memory_uc(addr, npages);
808}
809
d2f7cbe7
BP
810void __init old_map_region(efi_memory_desc_t *md)
811{
812 u64 start_pfn, end_pfn, end;
813 unsigned long size;
814 void *va;
815
816 start_pfn = PFN_DOWN(md->phys_addr);
817 size = md->num_pages << PAGE_SHIFT;
818 end = md->phys_addr + size;
819 end_pfn = PFN_UP(end);
820
821 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
822 va = __va(md->phys_addr);
823
824 if (!(md->attribute & EFI_MEMORY_WB))
825 efi_memory_uc((u64)(unsigned long)va, size);
826 } else
827 va = efi_ioremap(md->phys_addr, size,
828 md->type, md->attribute);
829
830 md->virt_addr = (u64) (unsigned long) va;
831 if (!va)
832 pr_err("ioremap of 0x%llX failed!\n",
833 (unsigned long long)md->phys_addr);
834}
835
481f75c0
DY
836/* Merge contiguous regions of the same type and attribute */
837static void __init efi_merge_regions(void)
5b83683f 838{
481f75c0 839 void *p;
202f9d0a 840 efi_memory_desc_t *md, *prev_md = NULL;
202f9d0a 841
202f9d0a
MG
842 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
843 u64 prev_size;
844 md = p;
845
846 if (!prev_md) {
847 prev_md = md;
848 continue;
849 }
850
851 if (prev_md->type != md->type ||
852 prev_md->attribute != md->attribute) {
853 prev_md = md;
854 continue;
855 }
856
857 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
858
859 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
860 prev_md->num_pages += md->num_pages;
861 md->type = EFI_RESERVED_TYPE;
862 md->attribute = 0;
863 continue;
864 }
865 prev_md = md;
481f75c0
DY
866 }
867}
868
869static void __init get_systab_virt_addr(efi_memory_desc_t *md)
870{
871 unsigned long size;
872 u64 end, systab;
d2f7cbe7 873
481f75c0
DY
874 size = md->num_pages << EFI_PAGE_SHIFT;
875 end = md->phys_addr + size;
876 systab = (u64)(unsigned long)efi_phys.systab;
877 if (md->phys_addr <= systab && systab < end) {
878 systab += md->virt_addr - md->phys_addr;
879 efi.systab = (efi_system_table_t *)(unsigned long)systab;
202f9d0a 880 }
481f75c0
DY
881}
882
fabb37c7 883static void __init save_runtime_map(void)
926172d4 884{
fabb37c7 885#ifdef CONFIG_KEXEC
926172d4
DY
886 efi_memory_desc_t *md;
887 void *tmp, *p, *q = NULL;
888 int count = 0;
889
890 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
891 md = p;
892
893 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
894 (md->type == EFI_BOOT_SERVICES_CODE) ||
895 (md->type == EFI_BOOT_SERVICES_DATA))
896 continue;
897 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
898 if (!tmp)
899 goto out;
900 q = tmp;
901
902 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
903 count++;
904 }
905
518548ab 906 efi_runtime_map_setup(q, count, memmap.desc_size);
fabb37c7 907 return;
926172d4 908
926172d4
DY
909out:
910 kfree(q);
fabb37c7
BP
911 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
912#endif
1fec0533
DY
913}
914
b7b898ae
BP
915static void *realloc_pages(void *old_memmap, int old_shift)
916{
917 void *ret;
918
919 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
920 if (!ret)
921 goto out;
922
923 /*
924 * A first-time allocation doesn't have anything to copy.
925 */
926 if (!old_memmap)
927 return ret;
928
929 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
930
931out:
932 free_pages((unsigned long)old_memmap, old_shift);
933 return ret;
934}
935
481f75c0 936/*
b7b898ae
BP
937 * Map the efi memory ranges of the runtime services and update new_mmap with
938 * virtual addresses.
481f75c0 939 */
b7b898ae 940static void * __init efi_map_regions(int *count, int *pg_shift)
481f75c0 941{
b7b898ae
BP
942 void *p, *new_memmap = NULL;
943 unsigned long left = 0;
481f75c0 944 efi_memory_desc_t *md;
202f9d0a 945
5b83683f
HY
946 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
947 md = p;
70087011
JB
948 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
949#ifdef CONFIG_X86_64
950 if (md->type != EFI_BOOT_SERVICES_CODE &&
951 md->type != EFI_BOOT_SERVICES_DATA)
952#endif
953 continue;
954 }
1c083eb2 955
d2f7cbe7 956 efi_map_region(md);
481f75c0
DY
957 get_systab_virt_addr(md);
958
b7b898ae
BP
959 if (left < memmap.desc_size) {
960 new_memmap = realloc_pages(new_memmap, *pg_shift);
961 if (!new_memmap)
962 return NULL;
963
964 left += PAGE_SIZE << *pg_shift;
965 (*pg_shift)++;
966 }
967
481f75c0
DY
968 memcpy(new_memmap + (*count * memmap.desc_size), md,
969 memmap.desc_size);
b7b898ae
BP
970
971 left -= memmap.desc_size;
481f75c0
DY
972 (*count)++;
973 }
d2f7cbe7 974
481f75c0 975 return new_memmap;
481f75c0
DY
976}
977
fabb37c7
BP
978static void __init kexec_enter_virtual_mode(void)
979{
980#ifdef CONFIG_KEXEC
981 efi_memory_desc_t *md;
982 void *p;
983
984 efi.systab = NULL;
985
986 /*
987 * We don't do virtual mode, since we don't do runtime services, on
988 * non-native EFI
989 */
990 if (!efi_is_native()) {
991 efi_unmap_memmap();
992 return;
993 }
994
995 /*
996 * Map efi regions which were passed via setup_data. The virt_addr is a
997 * fixed addr which was used in first kernel of a kexec boot.
998 */
999 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1000 md = p;
1001 efi_map_region_fixed(md); /* FIXME: add error handling */
1002 get_systab_virt_addr(md);
1003 }
1004
1005 save_runtime_map();
1006
1007 BUG_ON(!efi.systab);
1008
1009 efi_sync_low_kernel_mappings();
1010
1011 /*
1012 * Now that EFI is in virtual mode, update the function
1013 * pointers in the runtime service table to the new virtual addresses.
1014 *
1015 * Call EFI services through wrapper functions.
1016 */
1017 efi.runtime_version = efi_systab.hdr.revision;
1018 efi.get_time = virt_efi_get_time;
1019 efi.set_time = virt_efi_set_time;
1020 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1021 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1022 efi.get_variable = virt_efi_get_variable;
1023 efi.get_next_variable = virt_efi_get_next_variable;
1024 efi.set_variable = virt_efi_set_variable;
1025 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1026 efi.reset_system = virt_efi_reset_system;
1027 efi.set_virtual_address_map = NULL;
1028 efi.query_variable_info = virt_efi_query_variable_info;
1029 efi.update_capsule = virt_efi_update_capsule;
1030 efi.query_capsule_caps = virt_efi_query_capsule_caps;
1031
1032 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1033 runtime_code_page_mkexec();
1034
1035 /* clean DUMMY object */
1036 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1037 EFI_VARIABLE_NON_VOLATILE |
1038 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1039 EFI_VARIABLE_RUNTIME_ACCESS,
1040 0, NULL);
1041#endif
1042}
1043
481f75c0
DY
1044/*
1045 * This function will switch the EFI runtime services to virtual mode.
1046 * Essentially, we look through the EFI memmap and map every region that
1047 * has the runtime attribute bit set in its memory descriptor into the
1048 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1049 *
1050 * The old method which used to update that memory descriptor with the
1051 * virtual address obtained from ioremap() is still supported when the
1052 * kernel is booted with efi=old_map on its command line. Same old
1053 * method enabled the runtime services to be called without having to
1054 * thunk back into physical mode for every invocation.
1055 *
1056 * The new method does a pagetable switch in a preemption-safe manner
1057 * so that we're in a different address space when calling a runtime
1058 * function. For function arguments passing we do copy the PGDs of the
1059 * kernel page table into ->trampoline_pgd prior to each call.
1fec0533
DY
1060 *
1061 * Specially for kexec boot, efi runtime maps in previous kernel should
1062 * be passed in via setup_data. In that case runtime ranges will be mapped
fabb37c7
BP
1063 * to the same virtual addresses as the first kernel, see
1064 * kexec_enter_virtual_mode().
481f75c0 1065 */
fabb37c7 1066static void __init __efi_enter_virtual_mode(void)
481f75c0 1067{
fabb37c7 1068 int count = 0, pg_shift = 0;
481f75c0 1069 void *new_memmap = NULL;
b7b898ae 1070 efi_status_t status;
1c083eb2 1071
481f75c0 1072 efi.systab = NULL;
d2f7cbe7 1073
481f75c0
DY
1074 /*
1075 * We don't do virtual mode, since we don't do runtime services, on
1076 * non-native EFI
1077 */
1078 if (!efi_is_native()) {
1079 efi_unmap_memmap();
1080 return;
1081 }
d2f7cbe7 1082
fabb37c7
BP
1083 efi_merge_regions();
1084 new_memmap = efi_map_regions(&count, &pg_shift);
1085 if (!new_memmap) {
1086 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1087 return;
b7b898ae 1088 }
926172d4 1089
fabb37c7
BP
1090 save_runtime_map();
1091
5b83683f
HY
1092 BUG_ON(!efi.systab);
1093
fabb37c7
BP
1094 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1095 return;
b7b898ae 1096
d2f7cbe7 1097 efi_sync_low_kernel_mappings();
11cc8512 1098 efi_dump_pagetable();
d2f7cbe7 1099
fabb37c7 1100 status = phys_efi_set_virtual_address_map(
1fec0533
DY
1101 memmap.desc_size * count,
1102 memmap.desc_size,
1103 memmap.desc_version,
1104 (efi_memory_desc_t *)__pa(new_memmap));
1105
fabb37c7
BP
1106 if (status != EFI_SUCCESS) {
1107 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1108 status);
1109 panic("EFI call to SetVirtualAddressMap() failed!");
5b83683f
HY
1110 }
1111
1112 /*
1113 * Now that EFI is in virtual mode, update the function
1114 * pointers in the runtime service table to the new virtual addresses.
1115 *
1116 * Call EFI services through wrapper functions.
1117 */
712ba9e9 1118 efi.runtime_version = efi_systab.hdr.revision;
5b83683f
HY
1119 efi.get_time = virt_efi_get_time;
1120 efi.set_time = virt_efi_set_time;
1121 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1122 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1123 efi.get_variable = virt_efi_get_variable;
1124 efi.get_next_variable = virt_efi_get_next_variable;
1125 efi.set_variable = virt_efi_set_variable;
1126 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1127 efi.reset_system = virt_efi_reset_system;
2b5e8ef3 1128 efi.set_virtual_address_map = NULL;
3b370237
MG
1129 efi.query_variable_info = virt_efi_query_variable_info;
1130 efi.update_capsule = virt_efi_update_capsule;
1131 efi.query_capsule_caps = virt_efi_query_capsule_caps;
d2f7cbe7 1132
c55d016f 1133 efi_runtime_mkexec();
1adbfa35 1134
b7b898ae
BP
1135 /*
1136 * We mapped the descriptor array into the EFI pagetable above but we're
1137 * not unmapping it here. Here's why:
1138 *
1139 * We're copying select PGDs from the kernel page table to the EFI page
1140 * table and when we do so and make changes to those PGDs like unmapping
1141 * stuff from them, those changes appear in the kernel page table and we
1142 * go boom.
1143 *
1144 * From setup_real_mode():
1145 *
1146 * ...
1147 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1148 *
1149 * In this particular case, our allocation is in PGD 0 of the EFI page
1150 * table but we've copied that PGD from PGD[272] of the EFI page table:
1151 *
1152 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1153 *
1154 * where the direct memory mapping in kernel space is.
1155 *
1156 * new_memmap's VA comes from that direct mapping and thus clearing it,
1157 * it would get cleared in the kernel page table too.
1158 *
1159 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1160 */
fabb37c7 1161 free_pages((unsigned long)new_memmap, pg_shift);
f8b84043
MG
1162
1163 /* clean DUMMY object */
1164 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1165 EFI_VARIABLE_NON_VOLATILE |
1166 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1167 EFI_VARIABLE_RUNTIME_ACCESS,
1168 0, NULL);
5b83683f
HY
1169}
1170
fabb37c7
BP
1171void __init efi_enter_virtual_mode(void)
1172{
1173 if (efi_setup)
1174 kexec_enter_virtual_mode();
1175 else
1176 __efi_enter_virtual_mode();
1177}
1178
5b83683f
HY
1179/*
1180 * Convenience functions to obtain memory types and attributes
1181 */
1182u32 efi_mem_type(unsigned long phys_addr)
1183{
1184 efi_memory_desc_t *md;
1185 void *p;
1186
83e68189
MF
1187 if (!efi_enabled(EFI_MEMMAP))
1188 return 0;
1189
5b83683f
HY
1190 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1191 md = p;
1192 if ((md->phys_addr <= phys_addr) &&
1193 (phys_addr < (md->phys_addr +
1194 (md->num_pages << EFI_PAGE_SHIFT))))
1195 return md->type;
1196 }
1197 return 0;
1198}
1199
1200u64 efi_mem_attributes(unsigned long phys_addr)
1201{
1202 efi_memory_desc_t *md;
1203 void *p;
1204
1205 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1206 md = p;
1207 if ((md->phys_addr <= phys_addr) &&
1208 (phys_addr < (md->phys_addr +
1209 (md->num_pages << EFI_PAGE_SHIFT))))
1210 return md->attribute;
1211 }
1212 return 0;
1213}
a6e4d5a0
MF
1214
1215/*
5db80c65
MX
1216 * Some firmware implementations refuse to boot if there's insufficient space
1217 * in the variable store. Ensure that we never use more than a safe limit.
a6e4d5a0
MF
1218 *
1219 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1220 * store.
1221 */
1222efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1223{
1224 efi_status_t status;
1225 u64 storage_size, remaining_size, max_size;
1226
f8b84043
MG
1227 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1228 return 0;
1229
a6e4d5a0
MF
1230 status = efi.query_variable_info(attributes, &storage_size,
1231 &remaining_size, &max_size);
1232 if (status != EFI_SUCCESS)
1233 return status;
1234
31ff2f20 1235 /*
5db80c65
MX
1236 * We account for that by refusing the write if permitting it would
1237 * reduce the available space to under 5KB. This figure was provided by
1238 * Samsung, so should be safe.
31ff2f20 1239 */
f8b84043
MG
1240 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1241 !efi_no_storage_paranoia) {
1242
1243 /*
1244 * Triggering garbage collection may require that the firmware
1245 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1246 * that by attempting to use more space than is available.
1247 */
1248 unsigned long dummy_size = remaining_size + 1024;
b8cb62f8
BH
1249 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1250
1251 if (!dummy)
1252 return EFI_OUT_OF_RESOURCES;
f8b84043
MG
1253
1254 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1255 EFI_VARIABLE_NON_VOLATILE |
1256 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1257 EFI_VARIABLE_RUNTIME_ACCESS,
1258 dummy_size, dummy);
1259
1260 if (status == EFI_SUCCESS) {
1261 /*
1262 * This should have failed, so if it didn't make sure
1263 * that we delete it...
1264 */
1265 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1266 EFI_VARIABLE_NON_VOLATILE |
1267 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1268 EFI_VARIABLE_RUNTIME_ACCESS,
1269 0, dummy);
1270 }
7791c842 1271
b8cb62f8
BH
1272 kfree(dummy);
1273
f8b84043
MG
1274 /*
1275 * The runtime code may now have triggered a garbage collection
1276 * run, so check the variable info again
1277 */
1278 status = efi.query_variable_info(attributes, &storage_size,
1279 &remaining_size, &max_size);
8c58bf3e 1280
f8b84043
MG
1281 if (status != EFI_SUCCESS)
1282 return status;
1283
1284 /*
1285 * There still isn't enough room, so return an error
1286 */
1287 if (remaining_size - size < EFI_MIN_RESERVE)
1288 return EFI_OUT_OF_RESOURCES;
1289 }
a6e4d5a0
MF
1290
1291 return EFI_SUCCESS;
1292}
3668011d 1293EXPORT_SYMBOL_GPL(efi_query_variable_store);
d2f7cbe7
BP
1294
1295static int __init parse_efi_cmdline(char *str)
1296{
1297 if (*str == '=')
1298 str++;
1299
1300 if (!strncmp(str, "old_map", 7))
0f8093a9 1301 set_bit(EFI_OLD_MEMMAP, &efi.flags);
d2f7cbe7
BP
1302
1303 return 0;
1304}
1305early_param("efi", parse_efi_cmdline);
a5d90c92
BP
1306
1307void __init efi_apply_memmap_quirks(void)
1308{
1309 /*
1310 * Once setup is done earlier, unmap the EFI memory map on mismatched
1311 * firmware/kernel architectures since there is no support for runtime
1312 * services.
1313 */
1314 if (!efi_is_native()) {
1315 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1316 efi_unmap_memmap();
1317 }
1318
1319 /*
1320 * UV doesn't support the new EFI pagetable mapping yet.
1321 */
1322 if (is_uv_system())
4fd69331 1323 set_bit(EFI_OLD_MEMMAP, &efi.flags);
a5d90c92 1324}
This page took 0.506978 seconds and 5 git commands to generate.