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