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 unsigned long x86_efi_facility;
72 static __initdata efi_config_table_type_t arch_tables[] = {
74 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
76 {NULL_GUID, NULL, NULL},
79 u64 efi_setup; /* efi setup_data physical address */
82 * Returns 1 if 'facility' is enabled, 0 otherwise.
84 int efi_enabled(int facility)
86 return test_bit(facility, &x86_efi_facility) != 0;
88 EXPORT_SYMBOL(efi_enabled);
90 static bool __initdata disable_runtime = false;
91 static int __init setup_noefi(char *arg)
93 disable_runtime = true;
96 early_param("noefi", setup_noefi);
99 EXPORT_SYMBOL(add_efi_memmap);
101 static int __init setup_add_efi_memmap(char *arg)
106 early_param("add_efi_memmap", setup_add_efi_memmap);
108 static bool efi_no_storage_paranoia;
110 static int __init setup_storage_paranoia(char *arg)
112 efi_no_storage_paranoia = true;
115 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
117 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
122 spin_lock_irqsave(&rtc_lock, flags);
123 status = efi_call_virt2(get_time, tm, tc);
124 spin_unlock_irqrestore(&rtc_lock, flags);
128 static efi_status_t virt_efi_set_time(efi_time_t *tm)
133 spin_lock_irqsave(&rtc_lock, flags);
134 status = efi_call_virt1(set_time, tm);
135 spin_unlock_irqrestore(&rtc_lock, flags);
139 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
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);
153 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
158 spin_lock_irqsave(&rtc_lock, flags);
159 status = efi_call_virt2(set_wakeup_time,
161 spin_unlock_irqrestore(&rtc_lock, flags);
165 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
168 unsigned long *data_size,
171 return efi_call_virt5(get_variable,
176 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
180 return efi_call_virt3(get_next_variable,
181 name_size, name, vendor);
184 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
187 unsigned long data_size,
190 return efi_call_virt5(set_variable,
195 static efi_status_t virt_efi_query_variable_info(u32 attr,
197 u64 *remaining_space,
198 u64 *max_variable_size)
200 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
201 return EFI_UNSUPPORTED;
203 return efi_call_virt4(query_variable_info, attr, storage_space,
204 remaining_space, max_variable_size);
207 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
209 return efi_call_virt1(get_next_high_mono_count, count);
212 static void virt_efi_reset_system(int reset_type,
214 unsigned long data_size,
217 efi_call_virt4(reset_system, reset_type, status,
221 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
223 unsigned long sg_list)
225 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
226 return EFI_UNSUPPORTED;
228 return efi_call_virt3(update_capsule, capsules, count, sg_list);
231 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
236 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
237 return EFI_UNSUPPORTED;
239 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
243 static 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)
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();
259 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
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),
269 efi_call_phys_epilog();
270 spin_unlock_irqrestore(&rtc_lock, flags);
274 int efi_set_rtc_mmss(const struct timespec *now)
276 unsigned long nowtime = now->tv_sec;
282 status = efi.get_time(&eft, &cap);
283 if (status != EFI_SUCCESS) {
284 pr_err("Oops: efitime: can't read time!\n");
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;
298 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
299 __FUNCTION__, nowtime);
303 status = efi.set_time(&eft);
304 if (status != EFI_SUCCESS) {
305 pr_err("Oops: efitime: can't write time!\n");
311 void efi_get_time(struct timespec *now)
317 status = efi.get_time(&eft, &cap);
318 if (status != EFI_SUCCESS)
319 pr_err("Oops: efitime: can't read time!\n");
321 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
322 eft.minute, eft.second);
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.
332 static void __init do_add_efi_memmap(void)
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;
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;
351 e820_type = E820_RESERVED;
353 case EFI_ACPI_RECLAIM_MEMORY:
354 e820_type = E820_ACPI;
356 case EFI_ACPI_MEMORY_NVS:
357 e820_type = E820_NVS;
359 case EFI_UNUSABLE_MEMORY:
360 e820_type = E820_UNUSABLE;
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
368 e820_type = E820_RESERVED;
371 e820_add_region(start, size, e820_type);
373 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
376 int __init efi_memblock_x86_reserve_range(void)
378 struct efi_info *e = &boot_params.efi_info;
382 /* Can't handle data above 4GB at this time */
383 if (e->efi_memmap_hi) {
384 pr_err("Memory map is above 4GB, disabling EFI.\n");
387 pmap = e->efi_memmap;
389 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
391 memmap.phys_map = (void *)pmap;
392 memmap.nr_map = e->efi_memmap_size /
394 memmap.desc_size = e->efi_memdesc_size;
395 memmap.desc_version = e->efi_memdesc_version;
397 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
399 efi.memmap = &memmap;
404 static void __init print_efi_memmap(void)
407 efi_memory_desc_t *md;
411 for (p = memmap.map, i = 0;
413 p += memmap.desc_size, i++) {
415 pr_info("mem%02u: type=%u, attr=0x%llx, "
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)));
421 #endif /* EFI_DEBUG */
424 void __init efi_reserve_boot_services(void)
428 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
429 efi_memory_desc_t *md = p;
430 u64 start = md->phys_addr;
431 u64 size = md->num_pages << EFI_PAGE_SHIFT;
433 if (md->type != EFI_BOOT_SERVICES_CODE &&
434 md->type != EFI_BOOT_SERVICES_DATA)
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
442 if ((start + size > __pa_symbol(_text)
443 && start <= __pa_symbol(_end)) ||
444 !e820_all_mapped(start, start+size, E820_RAM) ||
445 memblock_is_region_reserved(start, size)) {
446 /* Could not reserve, skip it */
448 memblock_dbg("Could not reserve boot range "
449 "[0x%010llx-0x%010llx]\n",
450 start, start+size-1);
452 memblock_reserve(start, size);
456 void __init efi_unmap_memmap(void)
458 clear_bit(EFI_MEMMAP, &x86_efi_facility);
460 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
465 void __init efi_free_boot_services(void)
469 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
470 efi_memory_desc_t *md = p;
471 unsigned long long start = md->phys_addr;
472 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
474 if (md->type != EFI_BOOT_SERVICES_CODE &&
475 md->type != EFI_BOOT_SERVICES_DATA)
478 /* Could not reserve boot area */
482 free_bootmem_late(start, size);
488 static int __init efi_systab_init(void *phys)
490 if (efi_enabled(EFI_64BIT)) {
491 efi_system_table_64_t *systab64;
492 struct efi_setup_data *data = NULL;
496 data = early_memremap(efi_setup, sizeof(*data));
500 systab64 = early_ioremap((unsigned long)phys,
502 if (systab64 == NULL) {
503 pr_err("Couldn't map the system table!\n");
505 early_iounmap(data, sizeof(*data));
509 efi_systab.hdr = systab64->hdr;
510 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
512 tmp |= data ? data->fw_vendor : 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 = data ?
527 (void *)(unsigned long)data->runtime :
528 (void *)(unsigned long)systab64->runtime;
529 tmp |= data ? data->runtime : systab64->runtime;
530 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
531 tmp |= systab64->boottime;
532 efi_systab.nr_tables = systab64->nr_tables;
533 efi_systab.tables = data ? (unsigned long)data->tables :
535 tmp |= data ? data->tables : systab64->tables;
537 early_iounmap(systab64, sizeof(*systab64));
539 early_iounmap(data, sizeof(*data));
542 pr_err("EFI data located above 4GB, disabling EFI.\n");
547 efi_system_table_32_t *systab32;
549 systab32 = early_ioremap((unsigned long)phys,
551 if (systab32 == NULL) {
552 pr_err("Couldn't map the system table!\n");
556 efi_systab.hdr = systab32->hdr;
557 efi_systab.fw_vendor = systab32->fw_vendor;
558 efi_systab.fw_revision = systab32->fw_revision;
559 efi_systab.con_in_handle = systab32->con_in_handle;
560 efi_systab.con_in = systab32->con_in;
561 efi_systab.con_out_handle = systab32->con_out_handle;
562 efi_systab.con_out = systab32->con_out;
563 efi_systab.stderr_handle = systab32->stderr_handle;
564 efi_systab.stderr = systab32->stderr;
565 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
566 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
567 efi_systab.nr_tables = systab32->nr_tables;
568 efi_systab.tables = systab32->tables;
570 early_iounmap(systab32, sizeof(*systab32));
573 efi.systab = &efi_systab;
576 * Verify the EFI Table
578 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
579 pr_err("System table signature incorrect!\n");
582 if ((efi.systab->hdr.revision >> 16) == 0)
583 pr_err("Warning: System table version "
584 "%d.%02d, expected 1.00 or greater!\n",
585 efi.systab->hdr.revision >> 16,
586 efi.systab->hdr.revision & 0xffff);
591 static int __init efi_runtime_init(void)
593 efi_runtime_services_t *runtime;
596 * Check out the runtime services table. We need to map
597 * the runtime services table so that we can grab the physical
598 * address of several of the EFI runtime functions, needed to
599 * set the firmware into virtual mode.
601 runtime = early_ioremap((unsigned long)efi.systab->runtime,
602 sizeof(efi_runtime_services_t));
604 pr_err("Could not map the runtime service table!\n");
608 * We will only need *early* access to the following
609 * two EFI runtime services before set_virtual_address_map
612 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
613 efi_phys.set_virtual_address_map =
614 (efi_set_virtual_address_map_t *)
615 runtime->set_virtual_address_map;
617 * Make efi_get_time can be called before entering
620 efi.get_time = phys_efi_get_time;
621 early_iounmap(runtime, sizeof(efi_runtime_services_t));
626 static int __init efi_memmap_init(void)
628 /* Map the EFI memory map */
629 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
630 memmap.nr_map * memmap.desc_size);
631 if (memmap.map == NULL) {
632 pr_err("Could not map the memory map!\n");
635 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
644 * A number of config table entries get remapped to virtual addresses
645 * after entering EFI virtual mode. However, the kexec kernel requires
646 * their physical addresses therefore we pass them via setup_data and
647 * correct those entries to their respective physical addresses here.
649 * Currently only handles smbios which is necessary for some firmware
652 static int __init efi_reuse_config(u64 tables, int nr_tables)
656 struct efi_setup_data *data;
661 if (!efi_enabled(EFI_64BIT))
664 data = early_memremap(efi_setup, sizeof(*data));
673 sz = sizeof(efi_config_table_64_t);
675 p = tablep = early_memremap(tables, nr_tables * sz);
677 pr_err("Could not map Configuration table!\n");
682 for (i = 0; i < efi.systab->nr_tables; i++) {
685 guid = ((efi_config_table_64_t *)p)->guid;
687 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
688 ((efi_config_table_64_t *)p)->table = data->smbios;
691 early_iounmap(tablep, nr_tables * sz);
694 early_iounmap(data, sizeof(*data));
699 void __init efi_init(void)
702 char vendor[100] = "unknown";
707 if (boot_params.efi_info.efi_systab_hi ||
708 boot_params.efi_info.efi_memmap_hi) {
709 pr_info("Table located above 4GB, disabling EFI.\n");
712 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
714 efi_phys.systab = (efi_system_table_t *)
715 (boot_params.efi_info.efi_systab |
716 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
719 if (efi_systab_init(efi_phys.systab))
722 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
724 efi.config_table = (unsigned long)efi.systab->tables;
725 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
726 efi.runtime = (unsigned long)efi.systab->runtime;
729 * Show what we know for posterity
731 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
733 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
737 pr_err("Could not map the firmware vendor!\n");
738 early_iounmap(tmp, 2);
740 pr_info("EFI v%u.%.02u by %s\n",
741 efi.systab->hdr.revision >> 16,
742 efi.systab->hdr.revision & 0xffff, vendor);
744 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
747 if (efi_config_init(arch_tables))
750 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
753 * Note: We currently don't support runtime services on an EFI
754 * that doesn't match the kernel 32/64-bit mode.
757 if (!efi_is_native())
758 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
760 if (disable_runtime || efi_runtime_init())
762 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
764 if (efi_memmap_init())
767 set_bit(EFI_MEMMAP, &x86_efi_facility);
772 void __init efi_late_init(void)
777 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
781 addr = md->virt_addr;
782 npages = md->num_pages;
784 memrange_efi_to_native(&addr, &npages);
787 set_memory_x(addr, npages);
789 set_memory_nx(addr, npages);
792 void __init runtime_code_page_mkexec(void)
794 efi_memory_desc_t *md;
797 /* Make EFI runtime service code area executable */
798 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
801 if (md->type != EFI_RUNTIME_SERVICES_CODE)
804 efi_set_executable(md, true);
808 void efi_memory_uc(u64 addr, unsigned long size)
810 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
813 npages = round_up(size, page_shift) / page_shift;
814 memrange_efi_to_native(&addr, &npages);
815 set_memory_uc(addr, npages);
818 void __init old_map_region(efi_memory_desc_t *md)
820 u64 start_pfn, end_pfn, end;
824 start_pfn = PFN_DOWN(md->phys_addr);
825 size = md->num_pages << PAGE_SHIFT;
826 end = md->phys_addr + size;
827 end_pfn = PFN_UP(end);
829 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
830 va = __va(md->phys_addr);
832 if (!(md->attribute & EFI_MEMORY_WB))
833 efi_memory_uc((u64)(unsigned long)va, size);
835 va = efi_ioremap(md->phys_addr, size,
836 md->type, md->attribute);
838 md->virt_addr = (u64) (unsigned long) va;
840 pr_err("ioremap of 0x%llX failed!\n",
841 (unsigned long long)md->phys_addr);
844 /* Merge contiguous regions of the same type and attribute */
845 static void __init efi_merge_regions(void)
848 efi_memory_desc_t *md, *prev_md = NULL;
850 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
859 if (prev_md->type != md->type ||
860 prev_md->attribute != md->attribute) {
865 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
867 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
868 prev_md->num_pages += md->num_pages;
869 md->type = EFI_RESERVED_TYPE;
877 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
882 size = md->num_pages << EFI_PAGE_SHIFT;
883 end = md->phys_addr + size;
884 systab = (u64)(unsigned long)efi_phys.systab;
885 if (md->phys_addr <= systab && systab < end) {
886 systab += md->virt_addr - md->phys_addr;
887 efi.systab = (efi_system_table_t *)(unsigned long)systab;
891 static int __init save_runtime_map(void)
893 efi_memory_desc_t *md;
894 void *tmp, *p, *q = NULL;
897 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
900 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
901 (md->type == EFI_BOOT_SERVICES_CODE) ||
902 (md->type == EFI_BOOT_SERVICES_DATA))
904 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
909 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
913 efi_runtime_map_setup(q, count, memmap.desc_size);
922 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
923 * addr which was used in first kernel of a kexec boot.
925 static void __init efi_map_regions_fixed(void)
928 efi_memory_desc_t *md;
930 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
932 efi_map_region_fixed(md); /* FIXME: add error handling */
933 get_systab_virt_addr(md);
939 * Map efi memory ranges for runtime serivce and update new_memmap with virtual
942 static void * __init efi_map_regions(int *count)
944 efi_memory_desc_t *md;
945 void *p, *tmp, *new_memmap = NULL;
947 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
949 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
951 if (md->type != EFI_BOOT_SERVICES_CODE &&
952 md->type != EFI_BOOT_SERVICES_DATA)
958 get_systab_virt_addr(md);
960 tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size,
965 memcpy(new_memmap + (*count * memmap.desc_size), md,
977 * This function will switch the EFI runtime services to virtual mode.
978 * Essentially, we look through the EFI memmap and map every region that
979 * has the runtime attribute bit set in its memory descriptor into the
980 * ->trampoline_pgd page table using a top-down VA allocation scheme.
982 * The old method which used to update that memory descriptor with the
983 * virtual address obtained from ioremap() is still supported when the
984 * kernel is booted with efi=old_map on its command line. Same old
985 * method enabled the runtime services to be called without having to
986 * thunk back into physical mode for every invocation.
988 * The new method does a pagetable switch in a preemption-safe manner
989 * so that we're in a different address space when calling a runtime
990 * function. For function arguments passing we do copy the PGDs of the
991 * kernel page table into ->trampoline_pgd prior to each call.
993 * Specially for kexec boot, efi runtime maps in previous kernel should
994 * be passed in via setup_data. In that case runtime ranges will be mapped
995 * to the same virtual addresses as the first kernel.
997 void __init efi_enter_virtual_mode(void)
1000 void *new_memmap = NULL;
1006 efi_map_regions_fixed();
1008 efi_merge_regions();
1009 new_memmap = efi_map_regions(&count);
1011 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1016 err = save_runtime_map();
1018 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1020 BUG_ON(!efi.systab);
1022 efi_setup_page_tables();
1023 efi_sync_low_kernel_mappings();
1026 status = phys_efi_set_virtual_address_map(
1027 memmap.desc_size * count,
1029 memmap.desc_version,
1030 (efi_memory_desc_t *)__pa(new_memmap));
1032 if (status != EFI_SUCCESS) {
1033 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1035 panic("EFI call to SetVirtualAddressMap() failed!");
1040 * Now that EFI is in virtual mode, update the function
1041 * pointers in the runtime service table to the new virtual addresses.
1043 * Call EFI services through wrapper functions.
1045 efi.runtime_version = efi_systab.hdr.revision;
1046 efi.get_time = virt_efi_get_time;
1047 efi.set_time = virt_efi_set_time;
1048 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1049 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1050 efi.get_variable = virt_efi_get_variable;
1051 efi.get_next_variable = virt_efi_get_next_variable;
1052 efi.set_variable = virt_efi_set_variable;
1053 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1054 efi.reset_system = virt_efi_reset_system;
1055 efi.set_virtual_address_map = NULL;
1056 efi.query_variable_info = virt_efi_query_variable_info;
1057 efi.update_capsule = virt_efi_update_capsule;
1058 efi.query_capsule_caps = virt_efi_query_capsule_caps;
1060 efi_runtime_mkexec();
1064 /* clean DUMMY object */
1065 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1066 EFI_VARIABLE_NON_VOLATILE |
1067 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1068 EFI_VARIABLE_RUNTIME_ACCESS,
1073 * Convenience functions to obtain memory types and attributes
1075 u32 efi_mem_type(unsigned long phys_addr)
1077 efi_memory_desc_t *md;
1080 if (!efi_enabled(EFI_MEMMAP))
1083 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1085 if ((md->phys_addr <= phys_addr) &&
1086 (phys_addr < (md->phys_addr +
1087 (md->num_pages << EFI_PAGE_SHIFT))))
1093 u64 efi_mem_attributes(unsigned long phys_addr)
1095 efi_memory_desc_t *md;
1098 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1100 if ((md->phys_addr <= phys_addr) &&
1101 (phys_addr < (md->phys_addr +
1102 (md->num_pages << EFI_PAGE_SHIFT))))
1103 return md->attribute;
1109 * Some firmware has serious problems when using more than 50% of the EFI
1110 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1111 * we never use more than this safe limit.
1113 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1116 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1118 efi_status_t status;
1119 u64 storage_size, remaining_size, max_size;
1121 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1124 status = efi.query_variable_info(attributes, &storage_size,
1125 &remaining_size, &max_size);
1126 if (status != EFI_SUCCESS)
1130 * Some firmware implementations refuse to boot if there's insufficient
1131 * space in the variable store. We account for that by refusing the
1132 * write if permitting it would reduce the available space to under
1133 * 5KB. This figure was provided by Samsung, so should be safe.
1135 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1136 !efi_no_storage_paranoia) {
1139 * Triggering garbage collection may require that the firmware
1140 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1141 * that by attempting to use more space than is available.
1143 unsigned long dummy_size = remaining_size + 1024;
1144 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1147 return EFI_OUT_OF_RESOURCES;
1149 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1150 EFI_VARIABLE_NON_VOLATILE |
1151 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1152 EFI_VARIABLE_RUNTIME_ACCESS,
1155 if (status == EFI_SUCCESS) {
1157 * This should have failed, so if it didn't make sure
1158 * that we delete it...
1160 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1161 EFI_VARIABLE_NON_VOLATILE |
1162 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1163 EFI_VARIABLE_RUNTIME_ACCESS,
1170 * The runtime code may now have triggered a garbage collection
1171 * run, so check the variable info again
1173 status = efi.query_variable_info(attributes, &storage_size,
1174 &remaining_size, &max_size);
1176 if (status != EFI_SUCCESS)
1180 * There still isn't enough room, so return an error
1182 if (remaining_size - size < EFI_MIN_RESERVE)
1183 return EFI_OUT_OF_RESOURCES;
1188 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1190 static int __init parse_efi_cmdline(char *str)
1195 if (!strncmp(str, "old_map", 7))
1196 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1200 early_param("efi", parse_efi_cmdline);