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