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