x86/efi: Fix off-by-one bug in EFI Boot Services reservation
[linux-2.6-block.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>
5b83683f 55
f4fccac0 56#define EFI_DEBUG
5b83683f 57
f8b84043
MG
58#define EFI_MIN_RESERVE 5120
59
60#define EFI_DUMMY_GUID \
61 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
62
63static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
31ff2f20 64
5b83683f
HY
65struct efi_memory_map memmap;
66
ecaea42e 67static struct efi efi_phys __initdata;
5b83683f
HY
68static efi_system_table_t efi_systab __initdata;
69
83e68189
MF
70unsigned long x86_efi_facility;
71
272686bf
LL
72static __initdata efi_config_table_type_t arch_tables[] = {
73#ifdef CONFIG_X86_UV
74 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
75#endif
722da9d2 76 {NULL_GUID, NULL, NULL},
272686bf
LL
77};
78
83e68189
MF
79/*
80 * Returns 1 if 'facility' is enabled, 0 otherwise.
81 */
82int efi_enabled(int facility)
83{
84 return test_bit(facility, &x86_efi_facility) != 0;
5189c2a7 85}
83e68189 86EXPORT_SYMBOL(efi_enabled);
5189c2a7 87
058e7b58 88static bool __initdata disable_runtime = false;
8b2cb7a8
HY
89static int __init setup_noefi(char *arg)
90{
fb834c7a 91 disable_runtime = true;
8b2cb7a8
HY
92 return 0;
93}
94early_param("noefi", setup_noefi);
95
200001eb
PJ
96int add_efi_memmap;
97EXPORT_SYMBOL(add_efi_memmap);
98
99static int __init setup_add_efi_memmap(char *arg)
100{
101 add_efi_memmap = 1;
102 return 0;
103}
104early_param("add_efi_memmap", setup_add_efi_memmap);
105
8c58bf3e
RW
106static bool efi_no_storage_paranoia;
107
108static int __init setup_storage_paranoia(char *arg)
109{
110 efi_no_storage_paranoia = true;
111 return 0;
112}
113early_param("efi_no_storage_paranoia", setup_storage_paranoia);
114
200001eb 115
5b83683f
HY
116static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
117{
ef68c8f8
JB
118 unsigned long flags;
119 efi_status_t status;
120
121 spin_lock_irqsave(&rtc_lock, flags);
122 status = efi_call_virt2(get_time, tm, tc);
123 spin_unlock_irqrestore(&rtc_lock, flags);
124 return status;
5b83683f
HY
125}
126
127static efi_status_t virt_efi_set_time(efi_time_t *tm)
128{
ef68c8f8
JB
129 unsigned long flags;
130 efi_status_t status;
131
132 spin_lock_irqsave(&rtc_lock, flags);
133 status = efi_call_virt1(set_time, tm);
134 spin_unlock_irqrestore(&rtc_lock, flags);
135 return status;
5b83683f
HY
136}
137
138static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
139 efi_bool_t *pending,
140 efi_time_t *tm)
141{
ef68c8f8
JB
142 unsigned long flags;
143 efi_status_t status;
144
145 spin_lock_irqsave(&rtc_lock, flags);
146 status = efi_call_virt3(get_wakeup_time,
147 enabled, pending, tm);
148 spin_unlock_irqrestore(&rtc_lock, flags);
149 return status;
5b83683f
HY
150}
151
152static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
153{
ef68c8f8
JB
154 unsigned long flags;
155 efi_status_t status;
156
157 spin_lock_irqsave(&rtc_lock, flags);
158 status = efi_call_virt2(set_wakeup_time,
159 enabled, tm);
160 spin_unlock_irqrestore(&rtc_lock, flags);
161 return status;
5b83683f
HY
162}
163
164static efi_status_t virt_efi_get_variable(efi_char16_t *name,
165 efi_guid_t *vendor,
166 u32 *attr,
167 unsigned long *data_size,
168 void *data)
169{
170 return efi_call_virt5(get_variable,
171 name, vendor, attr,
172 data_size, data);
173}
174
175static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
176 efi_char16_t *name,
177 efi_guid_t *vendor)
178{
f8b84043
MG
179 return efi_call_virt3(get_next_variable,
180 name_size, name, vendor);
5b83683f
HY
181}
182
183static efi_status_t virt_efi_set_variable(efi_char16_t *name,
184 efi_guid_t *vendor,
f7a2d73f 185 u32 attr,
5b83683f
HY
186 unsigned long data_size,
187 void *data)
188{
f8b84043
MG
189 return efi_call_virt5(set_variable,
190 name, vendor, attr,
191 data_size, data);
5b83683f
HY
192}
193
3b370237
MG
194static efi_status_t virt_efi_query_variable_info(u32 attr,
195 u64 *storage_space,
196 u64 *remaining_space,
197 u64 *max_variable_size)
198{
199 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
200 return EFI_UNSUPPORTED;
201
202 return efi_call_virt4(query_variable_info, attr, storage_space,
203 remaining_space, max_variable_size);
204}
205
5b83683f
HY
206static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
207{
208 return efi_call_virt1(get_next_high_mono_count, count);
209}
210
211static void virt_efi_reset_system(int reset_type,
212 efi_status_t status,
213 unsigned long data_size,
214 efi_char16_t *data)
215{
216 efi_call_virt4(reset_system, reset_type, status,
217 data_size, data);
218}
219
3b370237
MG
220static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
221 unsigned long count,
222 unsigned long sg_list)
223{
224 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
225 return EFI_UNSUPPORTED;
226
227 return efi_call_virt3(update_capsule, capsules, count, sg_list);
228}
229
230static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
231 unsigned long count,
232 u64 *max_size,
233 int *reset_type)
234{
235 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
236 return EFI_UNSUPPORTED;
237
238 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
239 reset_type);
240}
241
5b83683f
HY
242static efi_status_t __init phys_efi_set_virtual_address_map(
243 unsigned long memory_map_size,
244 unsigned long descriptor_size,
245 u32 descriptor_version,
246 efi_memory_desc_t *virtual_map)
247{
248 efi_status_t status;
249
250 efi_call_phys_prelog();
251 status = efi_call_phys4(efi_phys.set_virtual_address_map,
252 memory_map_size, descriptor_size,
253 descriptor_version, virtual_map);
254 efi_call_phys_epilog();
255 return status;
256}
257
11520e5e
LT
258static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
259 efi_time_cap_t *tc)
260{
261 unsigned long flags;
262 efi_status_t status;
263
264 spin_lock_irqsave(&rtc_lock, flags);
265 efi_call_phys_prelog();
266 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
267 virt_to_phys(tc));
268 efi_call_phys_epilog();
269 spin_unlock_irqrestore(&rtc_lock, flags);
270 return status;
271}
272
3565184e 273int efi_set_rtc_mmss(const struct timespec *now)
5b83683f 274{
3565184e 275 unsigned long nowtime = now->tv_sec;
5b83683f
HY
276 efi_status_t status;
277 efi_time_t eft;
278 efi_time_cap_t cap;
3195ef59 279 struct rtc_time tm;
5b83683f
HY
280
281 status = efi.get_time(&eft, &cap);
282 if (status != EFI_SUCCESS) {
e3cb3f5a 283 pr_err("Oops: efitime: can't read time!\n");
5b83683f
HY
284 return -1;
285 }
286
3195ef59
PB
287 rtc_time_to_tm(nowtime, &tm);
288 if (!rtc_valid_tm(&tm)) {
289 eft.year = tm.tm_year + 1900;
290 eft.month = tm.tm_mon + 1;
291 eft.day = tm.tm_mday;
292 eft.minute = tm.tm_min;
293 eft.second = tm.tm_sec;
294 eft.nanosecond = 0;
295 } else {
296 printk(KERN_ERR
297 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
298 __FUNCTION__, nowtime);
299 return -1;
300 }
5b83683f
HY
301
302 status = efi.set_time(&eft);
303 if (status != EFI_SUCCESS) {
e3cb3f5a 304 pr_err("Oops: efitime: can't write time!\n");
5b83683f
HY
305 return -1;
306 }
307 return 0;
308}
309
3565184e 310void efi_get_time(struct timespec *now)
5b83683f
HY
311{
312 efi_status_t status;
313 efi_time_t eft;
314 efi_time_cap_t cap;
315
316 status = efi.get_time(&eft, &cap);
317 if (status != EFI_SUCCESS)
e3cb3f5a 318 pr_err("Oops: efitime: can't read time!\n");
5b83683f 319
3565184e
DV
320 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
321 eft.minute, eft.second);
322 now->tv_nsec = 0;
5b83683f
HY
323}
324
69c91893
PJ
325/*
326 * Tell the kernel about the EFI memory map. This might include
327 * more than the max 128 entries that can fit in the e820 legacy
328 * (zeropage) memory map.
329 */
330
200001eb 331static void __init do_add_efi_memmap(void)
69c91893
PJ
332{
333 void *p;
334
335 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
336 efi_memory_desc_t *md = p;
337 unsigned long long start = md->phys_addr;
338 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
339 int e820_type;
340
e2a71476
CW
341 switch (md->type) {
342 case EFI_LOADER_CODE:
343 case EFI_LOADER_DATA:
344 case EFI_BOOT_SERVICES_CODE:
345 case EFI_BOOT_SERVICES_DATA:
346 case EFI_CONVENTIONAL_MEMORY:
347 if (md->attribute & EFI_MEMORY_WB)
348 e820_type = E820_RAM;
349 else
350 e820_type = E820_RESERVED;
351 break;
352 case EFI_ACPI_RECLAIM_MEMORY:
353 e820_type = E820_ACPI;
354 break;
355 case EFI_ACPI_MEMORY_NVS:
356 e820_type = E820_NVS;
357 break;
358 case EFI_UNUSABLE_MEMORY:
359 e820_type = E820_UNUSABLE;
360 break;
361 default:
362 /*
363 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
364 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
365 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
366 */
69c91893 367 e820_type = E820_RESERVED;
e2a71476
CW
368 break;
369 }
d0be6bde 370 e820_add_region(start, size, e820_type);
69c91893
PJ
371 }
372 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
373}
374
1adbfa35 375int __init efi_memblock_x86_reserve_range(void)
ecacf09f 376{
15b9c359 377 struct efi_info *e = &boot_params.efi_info;
ecacf09f
HY
378 unsigned long pmap;
379
05486fa7 380#ifdef CONFIG_X86_32
1adbfa35 381 /* Can't handle data above 4GB at this time */
15b9c359 382 if (e->efi_memmap_hi) {
1adbfa35
OJ
383 pr_err("Memory map is above 4GB, disabling EFI.\n");
384 return -EINVAL;
385 }
15b9c359 386 pmap = e->efi_memmap;
05486fa7 387#else
15b9c359 388 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
ecacf09f 389#endif
15b9c359
BP
390 memmap.phys_map = (void *)pmap;
391 memmap.nr_map = e->efi_memmap_size /
392 e->efi_memdesc_size;
393 memmap.desc_size = e->efi_memdesc_size;
394 memmap.desc_version = e->efi_memdesc_version;
395
24aa0788 396 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
1adbfa35 397
258f6fd7
LL
398 efi.memmap = &memmap;
399
1adbfa35 400 return 0;
ecacf09f
HY
401}
402
5b83683f
HY
403static void __init print_efi_memmap(void)
404{
f4fccac0 405#ifdef EFI_DEBUG
5b83683f
HY
406 efi_memory_desc_t *md;
407 void *p;
408 int i;
409
410 for (p = memmap.map, i = 0;
411 p < memmap.map_end;
412 p += memmap.desc_size, i++) {
413 md = p;
e3cb3f5a 414 pr_info("mem%02u: type=%u, attr=0x%llx, "
5b83683f
HY
415 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
416 i, md->type, md->attribute, md->phys_addr,
417 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
418 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
419 }
5b83683f 420#endif /* EFI_DEBUG */
f4fccac0 421}
5b83683f 422
916f676f
MG
423void __init efi_reserve_boot_services(void)
424{
425 void *p;
426
427 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
428 efi_memory_desc_t *md = p;
7d68dc3f
ML
429 u64 start = md->phys_addr;
430 u64 size = md->num_pages << EFI_PAGE_SHIFT;
916f676f
MG
431
432 if (md->type != EFI_BOOT_SERVICES_CODE &&
433 md->type != EFI_BOOT_SERVICES_DATA)
434 continue;
7d68dc3f
ML
435 /* Only reserve where possible:
436 * - Not within any already allocated areas
437 * - Not over any memory area (really needed, if above?)
438 * - Not within any part of the kernel
439 * - Not the bios reserved area
440 */
a7f84f03 441 if ((start + size > __pa_symbol(_text)
fc8d7826 442 && start <= __pa_symbol(_end)) ||
7d68dc3f 443 !e820_all_mapped(start, start+size, E820_RAM) ||
bf61549a 444 memblock_is_region_reserved(start, size)) {
7d68dc3f
ML
445 /* Could not reserve, skip it */
446 md->num_pages = 0;
e3cb3f5a 447 memblock_dbg("Could not reserve boot range "
7d68dc3f
ML
448 "[0x%010llx-0x%010llx]\n",
449 start, start+size-1);
450 } else
24aa0788 451 memblock_reserve(start, size);
916f676f
MG
452 }
453}
454
5189c2a7 455void __init efi_unmap_memmap(void)
78510792 456{
83e68189 457 clear_bit(EFI_MEMMAP, &x86_efi_facility);
78510792
JT
458 if (memmap.map) {
459 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
460 memmap.map = NULL;
461 }
462}
463
464void __init efi_free_boot_services(void)
916f676f
MG
465{
466 void *p;
467
5189c2a7 468 if (!efi_is_native())
78510792
JT
469 return;
470
916f676f
MG
471 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
472 efi_memory_desc_t *md = p;
473 unsigned long long start = md->phys_addr;
474 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
475
476 if (md->type != EFI_BOOT_SERVICES_CODE &&
477 md->type != EFI_BOOT_SERVICES_DATA)
478 continue;
479
7d68dc3f
ML
480 /* Could not reserve boot area */
481 if (!size)
482 continue;
483
916f676f
MG
484 free_bootmem_late(start, size);
485 }
78510792
JT
486
487 efi_unmap_memmap();
916f676f
MG
488}
489
140bf275 490static int __init efi_systab_init(void *phys)
5b83683f 491{
83e68189 492 if (efi_enabled(EFI_64BIT)) {
1adbfa35
OJ
493 efi_system_table_64_t *systab64;
494 u64 tmp = 0;
495
496 systab64 = early_ioremap((unsigned long)phys,
497 sizeof(*systab64));
498 if (systab64 == NULL) {
499 pr_err("Couldn't map the system table!\n");
500 return -ENOMEM;
501 }
502
503 efi_systab.hdr = systab64->hdr;
504 efi_systab.fw_vendor = systab64->fw_vendor;
505 tmp |= systab64->fw_vendor;
506 efi_systab.fw_revision = systab64->fw_revision;
507 efi_systab.con_in_handle = systab64->con_in_handle;
508 tmp |= systab64->con_in_handle;
509 efi_systab.con_in = systab64->con_in;
510 tmp |= systab64->con_in;
511 efi_systab.con_out_handle = systab64->con_out_handle;
512 tmp |= systab64->con_out_handle;
513 efi_systab.con_out = systab64->con_out;
514 tmp |= systab64->con_out;
515 efi_systab.stderr_handle = systab64->stderr_handle;
516 tmp |= systab64->stderr_handle;
517 efi_systab.stderr = systab64->stderr;
518 tmp |= systab64->stderr;
519 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
520 tmp |= systab64->runtime;
521 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
522 tmp |= systab64->boottime;
523 efi_systab.nr_tables = systab64->nr_tables;
524 efi_systab.tables = systab64->tables;
525 tmp |= systab64->tables;
526
527 early_iounmap(systab64, sizeof(*systab64));
528#ifdef CONFIG_X86_32
529 if (tmp >> 32) {
530 pr_err("EFI data located above 4GB, disabling EFI.\n");
531 return -EINVAL;
532 }
533#endif
534 } else {
535 efi_system_table_32_t *systab32;
536
537 systab32 = early_ioremap((unsigned long)phys,
538 sizeof(*systab32));
539 if (systab32 == NULL) {
540 pr_err("Couldn't map the system table!\n");
541 return -ENOMEM;
542 }
543
544 efi_systab.hdr = systab32->hdr;
545 efi_systab.fw_vendor = systab32->fw_vendor;
546 efi_systab.fw_revision = systab32->fw_revision;
547 efi_systab.con_in_handle = systab32->con_in_handle;
548 efi_systab.con_in = systab32->con_in;
549 efi_systab.con_out_handle = systab32->con_out_handle;
550 efi_systab.con_out = systab32->con_out;
551 efi_systab.stderr_handle = systab32->stderr_handle;
552 efi_systab.stderr = systab32->stderr;
553 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
554 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
555 efi_systab.nr_tables = systab32->nr_tables;
556 efi_systab.tables = systab32->tables;
557
558 early_iounmap(systab32, sizeof(*systab32));
140bf275 559 }
1adbfa35 560
5b83683f
HY
561 efi.systab = &efi_systab;
562
563 /*
564 * Verify the EFI Table
565 */
140bf275 566 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
e3cb3f5a 567 pr_err("System table signature incorrect!\n");
140bf275
OJ
568 return -EINVAL;
569 }
5b83683f 570 if ((efi.systab->hdr.revision >> 16) == 0)
e3cb3f5a 571 pr_err("Warning: System table version "
5b83683f
HY
572 "%d.%02d, expected 1.00 or greater!\n",
573 efi.systab->hdr.revision >> 16,
574 efi.systab->hdr.revision & 0xffff);
140bf275
OJ
575
576 return 0;
83e7ee66 577}
5b83683f 578
140bf275 579static int __init efi_runtime_init(void)
83e7ee66
OJ
580{
581 efi_runtime_services_t *runtime;
5b83683f
HY
582
583 /*
584 * Check out the runtime services table. We need to map
585 * the runtime services table so that we can grab the physical
586 * address of several of the EFI runtime functions, needed to
587 * set the firmware into virtual mode.
588 */
beacfaac
HY
589 runtime = early_ioremap((unsigned long)efi.systab->runtime,
590 sizeof(efi_runtime_services_t));
140bf275 591 if (!runtime) {
e3cb3f5a 592 pr_err("Could not map the runtime service table!\n");
140bf275
OJ
593 return -ENOMEM;
594 }
595 /*
596 * We will only need *early* access to the following
11520e5e 597 * two EFI runtime services before set_virtual_address_map
140bf275
OJ
598 * is invoked.
599 */
11520e5e 600 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
140bf275
OJ
601 efi_phys.set_virtual_address_map =
602 (efi_set_virtual_address_map_t *)
603 runtime->set_virtual_address_map;
11520e5e
LT
604 /*
605 * Make efi_get_time can be called before entering
606 * virtual mode.
607 */
608 efi.get_time = phys_efi_get_time;
beacfaac 609 early_iounmap(runtime, sizeof(efi_runtime_services_t));
140bf275
OJ
610
611 return 0;
83e7ee66 612}
5b83683f 613
140bf275 614static int __init efi_memmap_init(void)
83e7ee66 615{
5b83683f 616 /* Map the EFI memory map */
beacfaac
HY
617 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
618 memmap.nr_map * memmap.desc_size);
140bf275 619 if (memmap.map == NULL) {
e3cb3f5a 620 pr_err("Could not map the memory map!\n");
140bf275
OJ
621 return -ENOMEM;
622 }
5b83683f 623 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
175e438f 624
200001eb
PJ
625 if (add_efi_memmap)
626 do_add_efi_memmap();
140bf275
OJ
627
628 return 0;
83e7ee66
OJ
629}
630
631void __init efi_init(void)
632{
633 efi_char16_t *c16;
634 char vendor[100] = "unknown";
635 int i = 0;
636 void *tmp;
637
638#ifdef CONFIG_X86_32
1adbfa35
OJ
639 if (boot_params.efi_info.efi_systab_hi ||
640 boot_params.efi_info.efi_memmap_hi) {
641 pr_info("Table located above 4GB, disabling EFI.\n");
1adbfa35
OJ
642 return;
643 }
83e7ee66
OJ
644 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
645#else
646 efi_phys.systab = (efi_system_table_t *)
1adbfa35
OJ
647 (boot_params.efi_info.efi_systab |
648 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
83e7ee66
OJ
649#endif
650
83e68189 651 if (efi_systab_init(efi_phys.systab))
140bf275 652 return;
83e68189
MF
653
654 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
83e7ee66
OJ
655
656 /*
657 * Show what we know for posterity
658 */
659 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
660 if (c16) {
661 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
662 vendor[i] = *c16++;
663 vendor[i] = '\0';
664 } else
e3cb3f5a 665 pr_err("Could not map the firmware vendor!\n");
83e7ee66
OJ
666 early_iounmap(tmp, 2);
667
e3cb3f5a
OJ
668 pr_info("EFI v%u.%.02u by %s\n",
669 efi.systab->hdr.revision >> 16,
670 efi.systab->hdr.revision & 0xffff, vendor);
83e7ee66 671
272686bf 672 if (efi_config_init(arch_tables))
140bf275 673 return;
83e68189
MF
674
675 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
83e7ee66 676
1adbfa35
OJ
677 /*
678 * Note: We currently don't support runtime services on an EFI
679 * that doesn't match the kernel 32/64-bit mode.
680 */
681
5189c2a7 682 if (!efi_is_native())
1adbfa35 683 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
83e68189 684 else {
fb834c7a 685 if (disable_runtime || efi_runtime_init())
83e68189
MF
686 return;
687 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
140bf275 688 }
83e7ee66 689
83e68189 690 if (efi_memmap_init())
140bf275 691 return;
83e68189
MF
692
693 set_bit(EFI_MEMMAP, &x86_efi_facility);
694
11520e5e 695#ifdef CONFIG_X86_32
5189c2a7 696 if (efi_is_native()) {
1adbfa35
OJ
697 x86_platform.get_wallclock = efi_get_time;
698 x86_platform.set_wallclock = efi_set_rtc_mmss;
699 }
11520e5e 700#endif
5b83683f 701 print_efi_memmap();
5b83683f
HY
702}
703
2223af38
JT
704void __init efi_late_init(void)
705{
706 efi_bgrt_init();
707}
708
9cd2b07c
MG
709void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
710{
711 u64 addr, npages;
712
713 addr = md->virt_addr;
714 npages = md->num_pages;
715
716 memrange_efi_to_native(&addr, &npages);
717
718 if (executable)
719 set_memory_x(addr, npages);
720 else
721 set_memory_nx(addr, npages);
722}
723
a2172e25
HY
724static void __init runtime_code_page_mkexec(void)
725{
726 efi_memory_desc_t *md;
a2172e25
HY
727 void *p;
728
a2172e25
HY
729 /* Make EFI runtime service code area executable */
730 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
731 md = p;
1c083eb2
HY
732
733 if (md->type != EFI_RUNTIME_SERVICES_CODE)
734 continue;
735
9cd2b07c 736 efi_set_executable(md, true);
a2172e25 737 }
a2172e25 738}
a2172e25 739
3e8fa263
MF
740void efi_memory_uc(u64 addr, unsigned long size)
741{
742 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
743 u64 npages;
744
745 npages = round_up(size, page_shift) / page_shift;
746 memrange_efi_to_native(&addr, &npages);
747 set_memory_uc(addr, npages);
748}
749
d2f7cbe7
BP
750void __init old_map_region(efi_memory_desc_t *md)
751{
752 u64 start_pfn, end_pfn, end;
753 unsigned long size;
754 void *va;
755
756 start_pfn = PFN_DOWN(md->phys_addr);
757 size = md->num_pages << PAGE_SHIFT;
758 end = md->phys_addr + size;
759 end_pfn = PFN_UP(end);
760
761 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
762 va = __va(md->phys_addr);
763
764 if (!(md->attribute & EFI_MEMORY_WB))
765 efi_memory_uc((u64)(unsigned long)va, size);
766 } else
767 va = efi_ioremap(md->phys_addr, size,
768 md->type, md->attribute);
769
770 md->virt_addr = (u64) (unsigned long) va;
771 if (!va)
772 pr_err("ioremap of 0x%llX failed!\n",
773 (unsigned long long)md->phys_addr);
774}
775
5b83683f
HY
776/*
777 * This function will switch the EFI runtime services to virtual mode.
d2f7cbe7
BP
778 * Essentially, we look through the EFI memmap and map every region that
779 * has the runtime attribute bit set in its memory descriptor into the
780 * ->trampoline_pgd page table using a top-down VA allocation scheme.
781 *
782 * The old method which used to update that memory descriptor with the
783 * virtual address obtained from ioremap() is still supported when the
784 * kernel is booted with efi=old_map on its command line. Same old
785 * method enabled the runtime services to be called without having to
5b83683f 786 * thunk back into physical mode for every invocation.
d2f7cbe7
BP
787 *
788 * The new method does a pagetable switch in a preemption-safe manner
789 * so that we're in a different address space when calling a runtime
790 * function. For function arguments passing we do copy the PGDs of the
791 * kernel page table into ->trampoline_pgd prior to each call.
5b83683f
HY
792 */
793void __init efi_enter_virtual_mode(void)
794{
202f9d0a 795 efi_memory_desc_t *md, *prev_md = NULL;
d2f7cbe7 796 void *p, *new_memmap = NULL;
1c083eb2 797 unsigned long size;
d2f7cbe7
BP
798 efi_status_t status;
799 u64 end, systab;
7cb00b72 800 int count = 0;
5b83683f
HY
801
802 efi.systab = NULL;
202f9d0a 803
1adbfa35
OJ
804 /*
805 * We don't do virtual mode, since we don't do runtime services, on
806 * non-native EFI
807 */
5189c2a7 808 if (!efi_is_native()) {
78510792
JT
809 efi_unmap_memmap();
810 return;
811 }
1adbfa35 812
202f9d0a
MG
813 /* Merge contiguous regions of the same type and attribute */
814 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
815 u64 prev_size;
816 md = p;
817
818 if (!prev_md) {
819 prev_md = md;
820 continue;
821 }
822
823 if (prev_md->type != md->type ||
824 prev_md->attribute != md->attribute) {
825 prev_md = md;
826 continue;
827 }
828
829 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
830
831 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
832 prev_md->num_pages += md->num_pages;
833 md->type = EFI_RESERVED_TYPE;
834 md->attribute = 0;
835 continue;
836 }
837 prev_md = md;
d2f7cbe7 838
202f9d0a
MG
839 }
840
5b83683f
HY
841 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
842 md = p;
70087011
JB
843 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
844#ifdef CONFIG_X86_64
845 if (md->type != EFI_BOOT_SERVICES_CODE &&
846 md->type != EFI_BOOT_SERVICES_DATA)
847#endif
848 continue;
849 }
1c083eb2 850
d2f7cbe7
BP
851 efi_map_region(md);
852
1c083eb2
HY
853 size = md->num_pages << EFI_PAGE_SHIFT;
854 end = md->phys_addr + size;
855
1c083eb2
HY
856 systab = (u64) (unsigned long) efi_phys.systab;
857 if (md->phys_addr <= systab && systab < end) {
858 systab += md->virt_addr - md->phys_addr;
d2f7cbe7 859
1c083eb2
HY
860 efi.systab = (efi_system_table_t *) (unsigned long) systab;
861 }
d2f7cbe7 862
7cb00b72
MG
863 new_memmap = krealloc(new_memmap,
864 (count + 1) * memmap.desc_size,
865 GFP_KERNEL);
ee411430
BP
866 if (!new_memmap)
867 goto err_out;
868
7cb00b72
MG
869 memcpy(new_memmap + (count * memmap.desc_size), md,
870 memmap.desc_size);
871 count++;
5b83683f
HY
872 }
873
874 BUG_ON(!efi.systab);
875
d2f7cbe7
BP
876 efi_setup_page_tables();
877 efi_sync_low_kernel_mappings();
878
5b83683f 879 status = phys_efi_set_virtual_address_map(
7cb00b72 880 memmap.desc_size * count,
5b83683f
HY
881 memmap.desc_size,
882 memmap.desc_version,
7cb00b72 883 (efi_memory_desc_t *)__pa(new_memmap));
5b83683f
HY
884
885 if (status != EFI_SUCCESS) {
e3cb3f5a
OJ
886 pr_alert("Unable to switch EFI into virtual mode "
887 "(status=%lx)!\n", status);
5b83683f
HY
888 panic("EFI call to SetVirtualAddressMap() failed!");
889 }
890
891 /*
892 * Now that EFI is in virtual mode, update the function
893 * pointers in the runtime service table to the new virtual addresses.
894 *
895 * Call EFI services through wrapper functions.
896 */
712ba9e9 897 efi.runtime_version = efi_systab.hdr.revision;
5b83683f
HY
898 efi.get_time = virt_efi_get_time;
899 efi.set_time = virt_efi_set_time;
900 efi.get_wakeup_time = virt_efi_get_wakeup_time;
901 efi.set_wakeup_time = virt_efi_set_wakeup_time;
902 efi.get_variable = virt_efi_get_variable;
903 efi.get_next_variable = virt_efi_get_next_variable;
904 efi.set_variable = virt_efi_set_variable;
905 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
906 efi.reset_system = virt_efi_reset_system;
2b5e8ef3 907 efi.set_virtual_address_map = NULL;
3b370237
MG
908 efi.query_variable_info = virt_efi_query_variable_info;
909 efi.update_capsule = virt_efi_update_capsule;
910 efi.query_capsule_caps = virt_efi_query_capsule_caps;
d2f7cbe7
BP
911
912 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
4de0d4a6 913 runtime_code_page_mkexec();
1adbfa35 914
7cb00b72 915 kfree(new_memmap);
f8b84043
MG
916
917 /* clean DUMMY object */
918 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
919 EFI_VARIABLE_NON_VOLATILE |
920 EFI_VARIABLE_BOOTSERVICE_ACCESS |
921 EFI_VARIABLE_RUNTIME_ACCESS,
922 0, NULL);
ee411430
BP
923
924 return;
925
926 err_out:
927 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
5b83683f
HY
928}
929
930/*
931 * Convenience functions to obtain memory types and attributes
932 */
933u32 efi_mem_type(unsigned long phys_addr)
934{
935 efi_memory_desc_t *md;
936 void *p;
937
83e68189
MF
938 if (!efi_enabled(EFI_MEMMAP))
939 return 0;
940
5b83683f
HY
941 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
942 md = p;
943 if ((md->phys_addr <= phys_addr) &&
944 (phys_addr < (md->phys_addr +
945 (md->num_pages << EFI_PAGE_SHIFT))))
946 return md->type;
947 }
948 return 0;
949}
950
951u64 efi_mem_attributes(unsigned long phys_addr)
952{
953 efi_memory_desc_t *md;
954 void *p;
955
956 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
957 md = p;
958 if ((md->phys_addr <= phys_addr) &&
959 (phys_addr < (md->phys_addr +
960 (md->num_pages << EFI_PAGE_SHIFT))))
961 return md->attribute;
962 }
963 return 0;
964}
a6e4d5a0
MF
965
966/*
967 * Some firmware has serious problems when using more than 50% of the EFI
968 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
969 * we never use more than this safe limit.
970 *
971 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
972 * store.
973 */
974efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
975{
976 efi_status_t status;
977 u64 storage_size, remaining_size, max_size;
978
f8b84043
MG
979 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
980 return 0;
981
a6e4d5a0
MF
982 status = efi.query_variable_info(attributes, &storage_size,
983 &remaining_size, &max_size);
984 if (status != EFI_SUCCESS)
985 return status;
986
31ff2f20
MG
987 /*
988 * Some firmware implementations refuse to boot if there's insufficient
989 * space in the variable store. We account for that by refusing the
990 * write if permitting it would reduce the available space to under
f8b84043 991 * 5KB. This figure was provided by Samsung, so should be safe.
31ff2f20 992 */
f8b84043
MG
993 if ((remaining_size - size < EFI_MIN_RESERVE) &&
994 !efi_no_storage_paranoia) {
995
996 /*
997 * Triggering garbage collection may require that the firmware
998 * generate a real EFI_OUT_OF_RESOURCES error. We can force
999 * that by attempting to use more space than is available.
1000 */
1001 unsigned long dummy_size = remaining_size + 1024;
b8cb62f8
BH
1002 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1003
1004 if (!dummy)
1005 return EFI_OUT_OF_RESOURCES;
f8b84043
MG
1006
1007 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1008 EFI_VARIABLE_NON_VOLATILE |
1009 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1010 EFI_VARIABLE_RUNTIME_ACCESS,
1011 dummy_size, dummy);
1012
1013 if (status == EFI_SUCCESS) {
1014 /*
1015 * This should have failed, so if it didn't make sure
1016 * that we delete it...
1017 */
1018 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1019 EFI_VARIABLE_NON_VOLATILE |
1020 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1021 EFI_VARIABLE_RUNTIME_ACCESS,
1022 0, dummy);
1023 }
7791c842 1024
b8cb62f8
BH
1025 kfree(dummy);
1026
f8b84043
MG
1027 /*
1028 * The runtime code may now have triggered a garbage collection
1029 * run, so check the variable info again
1030 */
1031 status = efi.query_variable_info(attributes, &storage_size,
1032 &remaining_size, &max_size);
8c58bf3e 1033
f8b84043
MG
1034 if (status != EFI_SUCCESS)
1035 return status;
1036
1037 /*
1038 * There still isn't enough room, so return an error
1039 */
1040 if (remaining_size - size < EFI_MIN_RESERVE)
1041 return EFI_OUT_OF_RESOURCES;
1042 }
a6e4d5a0
MF
1043
1044 return EFI_SUCCESS;
1045}
3668011d 1046EXPORT_SYMBOL_GPL(efi_query_variable_store);
d2f7cbe7
BP
1047
1048static int __init parse_efi_cmdline(char *str)
1049{
1050 if (*str == '=')
1051 str++;
1052
1053 if (!strncmp(str, "old_map", 7))
1054 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1055
1056 return 0;
1057}
1058early_param("efi", parse_efi_cmdline);