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