2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
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 * Copyright (C) 2013 SuSE Labs
16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
18 * Copied from efi_32.c to eliminate the duplicated code between EFI
19 * 32/64 support code. --ying 2007-10-26
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
25 * Implemented EFI runtime services and virtual mode calls. --davidm
27 * Goutham Rao: <goutham.rao@intel.com>
28 * Skip non-WB memory and ignore empty memory ranges.
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/efi.h>
36 #include <linux/efi-bgrt.h>
37 #include <linux/export.h>
38 #include <linux/bootmem.h>
39 #include <linux/slab.h>
40 #include <linux/memblock.h>
41 #include <linux/spinlock.h>
42 #include <linux/uaccess.h>
43 #include <linux/time.h>
45 #include <linux/reboot.h>
46 #include <linux/bcd.h>
48 #include <asm/setup.h>
51 #include <asm/cacheflush.h>
52 #include <asm/tlbflush.h>
53 #include <asm/x86_init.h>
58 #define EFI_MIN_RESERVE 5120
60 #define EFI_DUMMY_GUID \
61 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
63 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
65 struct efi_memory_map memmap;
67 static struct efi efi_phys __initdata;
68 static efi_system_table_t efi_systab __initdata;
70 static __initdata efi_config_table_type_t arch_tables[] = {
72 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
74 {NULL_GUID, NULL, NULL},
77 u64 efi_setup; /* efi setup_data physical address */
79 static bool __initdata disable_runtime = false;
80 static int __init setup_noefi(char *arg)
82 disable_runtime = true;
85 early_param("noefi", setup_noefi);
88 EXPORT_SYMBOL(add_efi_memmap);
90 static int __init setup_add_efi_memmap(char *arg)
95 early_param("add_efi_memmap", setup_add_efi_memmap);
97 static bool efi_no_storage_paranoia;
99 static int __init setup_storage_paranoia(char *arg)
101 efi_no_storage_paranoia = true;
104 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
106 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
111 spin_lock_irqsave(&rtc_lock, flags);
112 status = efi_call_virt2(get_time, tm, tc);
113 spin_unlock_irqrestore(&rtc_lock, flags);
117 static efi_status_t virt_efi_set_time(efi_time_t *tm)
122 spin_lock_irqsave(&rtc_lock, flags);
123 status = efi_call_virt1(set_time, tm);
124 spin_unlock_irqrestore(&rtc_lock, flags);
128 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
135 spin_lock_irqsave(&rtc_lock, flags);
136 status = efi_call_virt3(get_wakeup_time,
137 enabled, pending, tm);
138 spin_unlock_irqrestore(&rtc_lock, flags);
142 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
147 spin_lock_irqsave(&rtc_lock, flags);
148 status = efi_call_virt2(set_wakeup_time,
150 spin_unlock_irqrestore(&rtc_lock, flags);
154 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
157 unsigned long *data_size,
160 return efi_call_virt5(get_variable,
165 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
169 return efi_call_virt3(get_next_variable,
170 name_size, name, vendor);
173 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
176 unsigned long data_size,
179 return efi_call_virt5(set_variable,
184 static efi_status_t virt_efi_query_variable_info(u32 attr,
186 u64 *remaining_space,
187 u64 *max_variable_size)
189 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
190 return EFI_UNSUPPORTED;
192 return efi_call_virt4(query_variable_info, attr, storage_space,
193 remaining_space, max_variable_size);
196 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
198 return efi_call_virt1(get_next_high_mono_count, count);
201 static void virt_efi_reset_system(int reset_type,
203 unsigned long data_size,
206 efi_call_virt4(reset_system, reset_type, status,
210 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
212 unsigned long sg_list)
214 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
215 return EFI_UNSUPPORTED;
217 return efi_call_virt3(update_capsule, capsules, count, sg_list);
220 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
225 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
226 return EFI_UNSUPPORTED;
228 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
232 static efi_status_t __init phys_efi_set_virtual_address_map(
233 unsigned long memory_map_size,
234 unsigned long descriptor_size,
235 u32 descriptor_version,
236 efi_memory_desc_t *virtual_map)
240 efi_call_phys_prelog();
241 status = efi_call_phys4(efi_phys.set_virtual_address_map,
242 memory_map_size, descriptor_size,
243 descriptor_version, virtual_map);
244 efi_call_phys_epilog();
248 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
254 spin_lock_irqsave(&rtc_lock, flags);
255 efi_call_phys_prelog();
256 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
258 efi_call_phys_epilog();
259 spin_unlock_irqrestore(&rtc_lock, flags);
263 int efi_set_rtc_mmss(const struct timespec *now)
265 unsigned long nowtime = now->tv_sec;
271 status = efi.get_time(&eft, &cap);
272 if (status != EFI_SUCCESS) {
273 pr_err("Oops: efitime: can't read time!\n");
277 rtc_time_to_tm(nowtime, &tm);
278 if (!rtc_valid_tm(&tm)) {
279 eft.year = tm.tm_year + 1900;
280 eft.month = tm.tm_mon + 1;
281 eft.day = tm.tm_mday;
282 eft.minute = tm.tm_min;
283 eft.second = tm.tm_sec;
287 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
288 __FUNCTION__, nowtime);
292 status = efi.set_time(&eft);
293 if (status != EFI_SUCCESS) {
294 pr_err("Oops: efitime: can't write time!\n");
300 void efi_get_time(struct timespec *now)
306 status = efi.get_time(&eft, &cap);
307 if (status != EFI_SUCCESS)
308 pr_err("Oops: efitime: can't read time!\n");
310 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
311 eft.minute, eft.second);
316 * Tell the kernel about the EFI memory map. This might include
317 * more than the max 128 entries that can fit in the e820 legacy
318 * (zeropage) memory map.
321 static void __init do_add_efi_memmap(void)
325 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
326 efi_memory_desc_t *md = p;
327 unsigned long long start = md->phys_addr;
328 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
332 case EFI_LOADER_CODE:
333 case EFI_LOADER_DATA:
334 case EFI_BOOT_SERVICES_CODE:
335 case EFI_BOOT_SERVICES_DATA:
336 case EFI_CONVENTIONAL_MEMORY:
337 if (md->attribute & EFI_MEMORY_WB)
338 e820_type = E820_RAM;
340 e820_type = E820_RESERVED;
342 case EFI_ACPI_RECLAIM_MEMORY:
343 e820_type = E820_ACPI;
345 case EFI_ACPI_MEMORY_NVS:
346 e820_type = E820_NVS;
348 case EFI_UNUSABLE_MEMORY:
349 e820_type = E820_UNUSABLE;
353 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
354 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
355 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
357 e820_type = E820_RESERVED;
360 e820_add_region(start, size, e820_type);
362 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
365 int __init efi_memblock_x86_reserve_range(void)
367 struct efi_info *e = &boot_params.efi_info;
371 /* Can't handle data above 4GB at this time */
372 if (e->efi_memmap_hi) {
373 pr_err("Memory map is above 4GB, disabling EFI.\n");
376 pmap = e->efi_memmap;
378 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
380 memmap.phys_map = (void *)pmap;
381 memmap.nr_map = e->efi_memmap_size /
383 memmap.desc_size = e->efi_memdesc_size;
384 memmap.desc_version = e->efi_memdesc_version;
386 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
388 efi.memmap = &memmap;
393 static void __init print_efi_memmap(void)
396 efi_memory_desc_t *md;
400 for (p = memmap.map, i = 0;
402 p += memmap.desc_size, i++) {
404 pr_info("mem%02u: type=%u, attr=0x%llx, "
405 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
406 i, md->type, md->attribute, md->phys_addr,
407 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
408 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
410 #endif /* EFI_DEBUG */
413 void __init efi_reserve_boot_services(void)
417 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
418 efi_memory_desc_t *md = p;
419 u64 start = md->phys_addr;
420 u64 size = md->num_pages << EFI_PAGE_SHIFT;
422 if (md->type != EFI_BOOT_SERVICES_CODE &&
423 md->type != EFI_BOOT_SERVICES_DATA)
425 /* Only reserve where possible:
426 * - Not within any already allocated areas
427 * - Not over any memory area (really needed, if above?)
428 * - Not within any part of the kernel
429 * - Not the bios reserved area
431 if ((start + size > __pa_symbol(_text)
432 && start <= __pa_symbol(_end)) ||
433 !e820_all_mapped(start, start+size, E820_RAM) ||
434 memblock_is_region_reserved(start, size)) {
435 /* Could not reserve, skip it */
437 memblock_dbg("Could not reserve boot range "
438 "[0x%010llx-0x%010llx]\n",
439 start, start+size-1);
441 memblock_reserve(start, size);
445 void __init efi_unmap_memmap(void)
447 clear_bit(EFI_MEMMAP, &efi.flags);
449 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
454 void __init efi_free_boot_services(void)
458 if (!efi_is_native())
461 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
462 efi_memory_desc_t *md = p;
463 unsigned long long start = md->phys_addr;
464 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
466 if (md->type != EFI_BOOT_SERVICES_CODE &&
467 md->type != EFI_BOOT_SERVICES_DATA)
470 /* Could not reserve boot area */
474 free_bootmem_late(start, size);
480 static int __init efi_systab_init(void *phys)
482 if (efi_enabled(EFI_64BIT)) {
483 efi_system_table_64_t *systab64;
484 struct efi_setup_data *data = NULL;
488 data = early_memremap(efi_setup, sizeof(*data));
492 systab64 = early_ioremap((unsigned long)phys,
494 if (systab64 == NULL) {
495 pr_err("Couldn't map the system table!\n");
497 early_iounmap(data, sizeof(*data));
501 efi_systab.hdr = systab64->hdr;
502 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
504 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
505 efi_systab.fw_revision = systab64->fw_revision;
506 efi_systab.con_in_handle = systab64->con_in_handle;
507 tmp |= systab64->con_in_handle;
508 efi_systab.con_in = systab64->con_in;
509 tmp |= systab64->con_in;
510 efi_systab.con_out_handle = systab64->con_out_handle;
511 tmp |= systab64->con_out_handle;
512 efi_systab.con_out = systab64->con_out;
513 tmp |= systab64->con_out;
514 efi_systab.stderr_handle = systab64->stderr_handle;
515 tmp |= systab64->stderr_handle;
516 efi_systab.stderr = systab64->stderr;
517 tmp |= systab64->stderr;
518 efi_systab.runtime = data ?
519 (void *)(unsigned long)data->runtime :
520 (void *)(unsigned long)systab64->runtime;
521 tmp |= data ? data->runtime : systab64->runtime;
522 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
523 tmp |= systab64->boottime;
524 efi_systab.nr_tables = systab64->nr_tables;
525 efi_systab.tables = data ? (unsigned long)data->tables :
527 tmp |= data ? data->tables : systab64->tables;
529 early_iounmap(systab64, sizeof(*systab64));
531 early_iounmap(data, sizeof(*data));
534 pr_err("EFI data located above 4GB, disabling EFI.\n");
539 efi_system_table_32_t *systab32;
541 systab32 = early_ioremap((unsigned long)phys,
543 if (systab32 == NULL) {
544 pr_err("Couldn't map the system table!\n");
548 efi_systab.hdr = systab32->hdr;
549 efi_systab.fw_vendor = systab32->fw_vendor;
550 efi_systab.fw_revision = systab32->fw_revision;
551 efi_systab.con_in_handle = systab32->con_in_handle;
552 efi_systab.con_in = systab32->con_in;
553 efi_systab.con_out_handle = systab32->con_out_handle;
554 efi_systab.con_out = systab32->con_out;
555 efi_systab.stderr_handle = systab32->stderr_handle;
556 efi_systab.stderr = systab32->stderr;
557 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
558 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
559 efi_systab.nr_tables = systab32->nr_tables;
560 efi_systab.tables = systab32->tables;
562 early_iounmap(systab32, sizeof(*systab32));
565 efi.systab = &efi_systab;
568 * Verify the EFI Table
570 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
571 pr_err("System table signature incorrect!\n");
574 if ((efi.systab->hdr.revision >> 16) == 0)
575 pr_err("Warning: System table version "
576 "%d.%02d, expected 1.00 or greater!\n",
577 efi.systab->hdr.revision >> 16,
578 efi.systab->hdr.revision & 0xffff);
580 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
585 static int __init efi_runtime_init(void)
587 efi_runtime_services_t *runtime;
590 * Check out the runtime services table. We need to map
591 * the runtime services table so that we can grab the physical
592 * address of several of the EFI runtime functions, needed to
593 * set the firmware into virtual mode.
595 runtime = early_ioremap((unsigned long)efi.systab->runtime,
596 sizeof(efi_runtime_services_t));
598 pr_err("Could not map the runtime service table!\n");
602 * We will only need *early* access to the following
603 * two EFI runtime services before set_virtual_address_map
606 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
607 efi_phys.set_virtual_address_map =
608 (efi_set_virtual_address_map_t *)
609 runtime->set_virtual_address_map;
611 * Make efi_get_time can be called before entering
614 efi.get_time = phys_efi_get_time;
615 early_iounmap(runtime, sizeof(efi_runtime_services_t));
617 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
622 static int __init efi_memmap_init(void)
624 /* Map the EFI memory map */
625 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
626 memmap.nr_map * memmap.desc_size);
627 if (memmap.map == NULL) {
628 pr_err("Could not map the memory map!\n");
631 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
636 set_bit(EFI_MEMMAP, &efi.flags);
642 * A number of config table entries get remapped to virtual addresses
643 * after entering EFI virtual mode. However, the kexec kernel requires
644 * their physical addresses therefore we pass them via setup_data and
645 * correct those entries to their respective physical addresses here.
647 * Currently only handles smbios which is necessary for some firmware
650 static int __init efi_reuse_config(u64 tables, int nr_tables)
654 struct efi_setup_data *data;
659 if (!efi_enabled(EFI_64BIT))
662 data = early_memremap(efi_setup, sizeof(*data));
671 sz = sizeof(efi_config_table_64_t);
673 p = tablep = early_memremap(tables, nr_tables * sz);
675 pr_err("Could not map Configuration table!\n");
680 for (i = 0; i < efi.systab->nr_tables; i++) {
683 guid = ((efi_config_table_64_t *)p)->guid;
685 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
686 ((efi_config_table_64_t *)p)->table = data->smbios;
689 early_iounmap(tablep, nr_tables * sz);
692 early_iounmap(data, sizeof(*data));
697 void __init efi_init(void)
700 char vendor[100] = "unknown";
705 if (boot_params.efi_info.efi_systab_hi ||
706 boot_params.efi_info.efi_memmap_hi) {
707 pr_info("Table located above 4GB, disabling EFI.\n");
710 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
712 efi_phys.systab = (efi_system_table_t *)
713 (boot_params.efi_info.efi_systab |
714 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
717 if (efi_systab_init(efi_phys.systab))
720 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
722 efi.config_table = (unsigned long)efi.systab->tables;
723 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
724 efi.runtime = (unsigned long)efi.systab->runtime;
727 * Show what we know for posterity
729 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
731 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
735 pr_err("Could not map the firmware vendor!\n");
736 early_iounmap(tmp, 2);
738 pr_info("EFI v%u.%.02u by %s\n",
739 efi.systab->hdr.revision >> 16,
740 efi.systab->hdr.revision & 0xffff, vendor);
742 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
745 if (efi_config_init(arch_tables))
749 * Note: We currently don't support runtime services on an EFI
750 * that doesn't match the kernel 32/64-bit mode.
753 if (!efi_is_native())
754 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
756 if (disable_runtime || efi_runtime_init())
759 if (efi_memmap_init())
762 set_bit(EFI_MEMMAP, &efi.flags);
767 void __init efi_late_init(void)
772 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
776 addr = md->virt_addr;
777 npages = md->num_pages;
779 memrange_efi_to_native(&addr, &npages);
782 set_memory_x(addr, npages);
784 set_memory_nx(addr, npages);
787 void __init runtime_code_page_mkexec(void)
789 efi_memory_desc_t *md;
792 /* Make EFI runtime service code area executable */
793 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
796 if (md->type != EFI_RUNTIME_SERVICES_CODE)
799 efi_set_executable(md, true);
803 void efi_memory_uc(u64 addr, unsigned long size)
805 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
808 npages = round_up(size, page_shift) / page_shift;
809 memrange_efi_to_native(&addr, &npages);
810 set_memory_uc(addr, npages);
813 void __init old_map_region(efi_memory_desc_t *md)
815 u64 start_pfn, end_pfn, end;
819 start_pfn = PFN_DOWN(md->phys_addr);
820 size = md->num_pages << PAGE_SHIFT;
821 end = md->phys_addr + size;
822 end_pfn = PFN_UP(end);
824 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
825 va = __va(md->phys_addr);
827 if (!(md->attribute & EFI_MEMORY_WB))
828 efi_memory_uc((u64)(unsigned long)va, size);
830 va = efi_ioremap(md->phys_addr, size,
831 md->type, md->attribute);
833 md->virt_addr = (u64) (unsigned long) va;
835 pr_err("ioremap of 0x%llX failed!\n",
836 (unsigned long long)md->phys_addr);
839 /* Merge contiguous regions of the same type and attribute */
840 static void __init efi_merge_regions(void)
843 efi_memory_desc_t *md, *prev_md = NULL;
845 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
854 if (prev_md->type != md->type ||
855 prev_md->attribute != md->attribute) {
860 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
862 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
863 prev_md->num_pages += md->num_pages;
864 md->type = EFI_RESERVED_TYPE;
872 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
877 size = md->num_pages << EFI_PAGE_SHIFT;
878 end = md->phys_addr + size;
879 systab = (u64)(unsigned long)efi_phys.systab;
880 if (md->phys_addr <= systab && systab < end) {
881 systab += md->virt_addr - md->phys_addr;
882 efi.systab = (efi_system_table_t *)(unsigned long)systab;
886 static int __init save_runtime_map(void)
888 efi_memory_desc_t *md;
889 void *tmp, *p, *q = NULL;
892 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
895 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
896 (md->type == EFI_BOOT_SERVICES_CODE) ||
897 (md->type == EFI_BOOT_SERVICES_DATA))
899 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
904 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
908 efi_runtime_map_setup(q, count, memmap.desc_size);
917 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
918 * addr which was used in first kernel of a kexec boot.
920 static void __init efi_map_regions_fixed(void)
923 efi_memory_desc_t *md;
925 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
927 efi_map_region_fixed(md); /* FIXME: add error handling */
928 get_systab_virt_addr(md);
934 * Map efi memory ranges for runtime serivce and update new_memmap with virtual
937 static void * __init efi_map_regions(int *count)
939 efi_memory_desc_t *md;
940 void *p, *tmp, *new_memmap = NULL;
942 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
944 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
946 if (md->type != EFI_BOOT_SERVICES_CODE &&
947 md->type != EFI_BOOT_SERVICES_DATA)
953 get_systab_virt_addr(md);
955 tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size,
960 memcpy(new_memmap + (*count * memmap.desc_size), md,
972 * This function will switch the EFI runtime services to virtual mode.
973 * Essentially, we look through the EFI memmap and map every region that
974 * has the runtime attribute bit set in its memory descriptor into the
975 * ->trampoline_pgd page table using a top-down VA allocation scheme.
977 * The old method which used to update that memory descriptor with the
978 * virtual address obtained from ioremap() is still supported when the
979 * kernel is booted with efi=old_map on its command line. Same old
980 * method enabled the runtime services to be called without having to
981 * thunk back into physical mode for every invocation.
983 * The new method does a pagetable switch in a preemption-safe manner
984 * so that we're in a different address space when calling a runtime
985 * function. For function arguments passing we do copy the PGDs of the
986 * kernel page table into ->trampoline_pgd prior to each call.
988 * Specially for kexec boot, efi runtime maps in previous kernel should
989 * be passed in via setup_data. In that case runtime ranges will be mapped
990 * to the same virtual addresses as the first kernel.
992 void __init efi_enter_virtual_mode(void)
995 void *new_memmap = NULL;
1001 * We don't do virtual mode, since we don't do runtime services, on
1004 if (!efi_is_native()) {
1010 efi_map_regions_fixed();
1012 efi_merge_regions();
1013 new_memmap = efi_map_regions(&count);
1015 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1020 err = save_runtime_map();
1022 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1024 BUG_ON(!efi.systab);
1026 efi_setup_page_tables();
1027 efi_sync_low_kernel_mappings();
1030 status = phys_efi_set_virtual_address_map(
1031 memmap.desc_size * count,
1033 memmap.desc_version,
1034 (efi_memory_desc_t *)__pa(new_memmap));
1036 if (status != EFI_SUCCESS) {
1037 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1039 panic("EFI call to SetVirtualAddressMap() failed!");
1044 * Now that EFI is in virtual mode, update the function
1045 * pointers in the runtime service table to the new virtual addresses.
1047 * Call EFI services through wrapper functions.
1049 efi.runtime_version = efi_systab.hdr.revision;
1050 efi.get_time = virt_efi_get_time;
1051 efi.set_time = virt_efi_set_time;
1052 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1053 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1054 efi.get_variable = virt_efi_get_variable;
1055 efi.get_next_variable = virt_efi_get_next_variable;
1056 efi.set_variable = virt_efi_set_variable;
1057 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1058 efi.reset_system = virt_efi_reset_system;
1059 efi.set_virtual_address_map = NULL;
1060 efi.query_variable_info = virt_efi_query_variable_info;
1061 efi.update_capsule = virt_efi_update_capsule;
1062 efi.query_capsule_caps = virt_efi_query_capsule_caps;
1064 efi_runtime_mkexec();
1068 /* clean DUMMY object */
1069 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1070 EFI_VARIABLE_NON_VOLATILE |
1071 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1072 EFI_VARIABLE_RUNTIME_ACCESS,
1077 * Convenience functions to obtain memory types and attributes
1079 u32 efi_mem_type(unsigned long phys_addr)
1081 efi_memory_desc_t *md;
1084 if (!efi_enabled(EFI_MEMMAP))
1087 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1089 if ((md->phys_addr <= phys_addr) &&
1090 (phys_addr < (md->phys_addr +
1091 (md->num_pages << EFI_PAGE_SHIFT))))
1097 u64 efi_mem_attributes(unsigned long phys_addr)
1099 efi_memory_desc_t *md;
1102 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1104 if ((md->phys_addr <= phys_addr) &&
1105 (phys_addr < (md->phys_addr +
1106 (md->num_pages << EFI_PAGE_SHIFT))))
1107 return md->attribute;
1113 * Some firmware has serious problems when using more than 50% of the EFI
1114 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1115 * we never use more than this safe limit.
1117 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1120 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1122 efi_status_t status;
1123 u64 storage_size, remaining_size, max_size;
1125 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1128 status = efi.query_variable_info(attributes, &storage_size,
1129 &remaining_size, &max_size);
1130 if (status != EFI_SUCCESS)
1134 * Some firmware implementations refuse to boot if there's insufficient
1135 * space in the variable store. We account for that by refusing the
1136 * write if permitting it would reduce the available space to under
1137 * 5KB. This figure was provided by Samsung, so should be safe.
1139 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1140 !efi_no_storage_paranoia) {
1143 * Triggering garbage collection may require that the firmware
1144 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1145 * that by attempting to use more space than is available.
1147 unsigned long dummy_size = remaining_size + 1024;
1148 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1151 return EFI_OUT_OF_RESOURCES;
1153 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1154 EFI_VARIABLE_NON_VOLATILE |
1155 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1156 EFI_VARIABLE_RUNTIME_ACCESS,
1159 if (status == EFI_SUCCESS) {
1161 * This should have failed, so if it didn't make sure
1162 * that we delete it...
1164 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1165 EFI_VARIABLE_NON_VOLATILE |
1166 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1167 EFI_VARIABLE_RUNTIME_ACCESS,
1174 * The runtime code may now have triggered a garbage collection
1175 * run, so check the variable info again
1177 status = efi.query_variable_info(attributes, &storage_size,
1178 &remaining_size, &max_size);
1180 if (status != EFI_SUCCESS)
1184 * There still isn't enough room, so return an error
1186 if (remaining_size - size < EFI_MIN_RESERVE)
1187 return EFI_OUT_OF_RESOURCES;
1192 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1194 static int __init parse_efi_cmdline(char *str)
1199 if (!strncmp(str, "old_map", 7))
1200 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1204 early_param("efi", parse_efi_cmdline);