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>
47 #include <linux/acpi.h>
49 #include <asm/setup.h>
52 #include <asm/cacheflush.h>
53 #include <asm/tlbflush.h>
54 #include <asm/x86_init.h>
56 #include <asm/uv/uv.h>
60 #define EFI_MIN_RESERVE 5120
62 #define EFI_DUMMY_GUID \
63 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
65 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
67 struct efi_memory_map memmap;
69 static struct efi efi_phys __initdata;
70 static efi_system_table_t efi_systab __initdata;
72 static efi_config_table_type_t arch_tables[] __initdata = {
74 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
76 {NULL_GUID, NULL, NULL},
79 u64 efi_setup; /* efi setup_data physical address */
81 static bool disable_runtime __initdata = false;
82 static int __init setup_noefi(char *arg)
84 disable_runtime = true;
87 early_param("noefi", setup_noefi);
90 EXPORT_SYMBOL(add_efi_memmap);
92 static int __init setup_add_efi_memmap(char *arg)
97 early_param("add_efi_memmap", setup_add_efi_memmap);
99 static bool efi_no_storage_paranoia;
101 static int __init setup_storage_paranoia(char *arg)
103 efi_no_storage_paranoia = true;
106 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
108 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
113 spin_lock_irqsave(&rtc_lock, flags);
114 status = efi_call_virt(get_time, tm, tc);
115 spin_unlock_irqrestore(&rtc_lock, flags);
119 static efi_status_t virt_efi_set_time(efi_time_t *tm)
124 spin_lock_irqsave(&rtc_lock, flags);
125 status = efi_call_virt(set_time, tm);
126 spin_unlock_irqrestore(&rtc_lock, flags);
130 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
137 spin_lock_irqsave(&rtc_lock, flags);
138 status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
139 spin_unlock_irqrestore(&rtc_lock, flags);
143 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
148 spin_lock_irqsave(&rtc_lock, flags);
149 status = efi_call_virt(set_wakeup_time, enabled, tm);
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_virt(get_variable,
165 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
169 return efi_call_virt(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_virt(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_virt(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_virt(get_next_high_mono_count, count);
201 static void virt_efi_reset_system(int reset_type,
203 unsigned long data_size,
206 __efi_call_virt(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_virt(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_virt(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)
241 efi_call_phys_prelog();
243 /* Disable interrupts around EFI calls: */
244 local_irq_save(flags);
245 status = efi_call_phys(efi_phys.set_virtual_address_map,
246 memory_map_size, descriptor_size,
247 descriptor_version, virtual_map);
248 local_irq_restore(flags);
250 efi_call_phys_epilog();
255 int efi_set_rtc_mmss(const struct timespec *now)
257 unsigned long nowtime = now->tv_sec;
263 status = efi.get_time(&eft, &cap);
264 if (status != EFI_SUCCESS) {
265 pr_err("Oops: efitime: can't read time!\n");
269 rtc_time_to_tm(nowtime, &tm);
270 if (!rtc_valid_tm(&tm)) {
271 eft.year = tm.tm_year + 1900;
272 eft.month = tm.tm_mon + 1;
273 eft.day = tm.tm_mday;
274 eft.minute = tm.tm_min;
275 eft.second = tm.tm_sec;
278 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
283 status = efi.set_time(&eft);
284 if (status != EFI_SUCCESS) {
285 pr_err("Oops: efitime: can't write time!\n");
291 void efi_get_time(struct timespec *now)
297 status = efi.get_time(&eft, &cap);
298 if (status != EFI_SUCCESS)
299 pr_err("Oops: efitime: can't read time!\n");
301 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
302 eft.minute, eft.second);
307 * Tell the kernel about the EFI memory map. This might include
308 * more than the max 128 entries that can fit in the e820 legacy
309 * (zeropage) memory map.
312 static void __init do_add_efi_memmap(void)
316 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
317 efi_memory_desc_t *md = p;
318 unsigned long long start = md->phys_addr;
319 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
323 case EFI_LOADER_CODE:
324 case EFI_LOADER_DATA:
325 case EFI_BOOT_SERVICES_CODE:
326 case EFI_BOOT_SERVICES_DATA:
327 case EFI_CONVENTIONAL_MEMORY:
328 if (md->attribute & EFI_MEMORY_WB)
329 e820_type = E820_RAM;
331 e820_type = E820_RESERVED;
333 case EFI_ACPI_RECLAIM_MEMORY:
334 e820_type = E820_ACPI;
336 case EFI_ACPI_MEMORY_NVS:
337 e820_type = E820_NVS;
339 case EFI_UNUSABLE_MEMORY:
340 e820_type = E820_UNUSABLE;
344 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
345 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
346 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
348 e820_type = E820_RESERVED;
351 e820_add_region(start, size, e820_type);
353 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
356 int __init efi_memblock_x86_reserve_range(void)
358 struct efi_info *e = &boot_params.efi_info;
362 /* Can't handle data above 4GB at this time */
363 if (e->efi_memmap_hi) {
364 pr_err("Memory map is above 4GB, disabling EFI.\n");
367 pmap = e->efi_memmap;
369 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
371 memmap.phys_map = (void *)pmap;
372 memmap.nr_map = e->efi_memmap_size /
374 memmap.desc_size = e->efi_memdesc_size;
375 memmap.desc_version = e->efi_memdesc_version;
377 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
379 efi.memmap = &memmap;
384 static void __init print_efi_memmap(void)
387 efi_memory_desc_t *md;
391 for (p = memmap.map, i = 0;
393 p += memmap.desc_size, i++) {
395 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
396 i, md->type, md->attribute, md->phys_addr,
397 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
398 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
400 #endif /* EFI_DEBUG */
403 void __init efi_reserve_boot_services(void)
407 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
408 efi_memory_desc_t *md = p;
409 u64 start = md->phys_addr;
410 u64 size = md->num_pages << EFI_PAGE_SHIFT;
412 if (md->type != EFI_BOOT_SERVICES_CODE &&
413 md->type != EFI_BOOT_SERVICES_DATA)
415 /* Only reserve where possible:
416 * - Not within any already allocated areas
417 * - Not over any memory area (really needed, if above?)
418 * - Not within any part of the kernel
419 * - Not the bios reserved area
421 if ((start + size > __pa_symbol(_text)
422 && start <= __pa_symbol(_end)) ||
423 !e820_all_mapped(start, start+size, E820_RAM) ||
424 memblock_is_region_reserved(start, size)) {
425 /* Could not reserve, skip it */
427 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
428 start, start+size-1);
430 memblock_reserve(start, size);
434 void __init efi_unmap_memmap(void)
436 clear_bit(EFI_MEMMAP, &efi.flags);
438 early_memunmap(memmap.map, memmap.nr_map * memmap.desc_size);
443 void __init efi_free_boot_services(void)
447 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
448 efi_memory_desc_t *md = p;
449 unsigned long long start = md->phys_addr;
450 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
452 if (md->type != EFI_BOOT_SERVICES_CODE &&
453 md->type != EFI_BOOT_SERVICES_DATA)
456 /* Could not reserve boot area */
460 free_bootmem_late(start, size);
466 static int __init efi_systab_init(void *phys)
468 if (efi_enabled(EFI_64BIT)) {
469 efi_system_table_64_t *systab64;
470 struct efi_setup_data *data = NULL;
474 data = early_memremap(efi_setup, sizeof(*data));
478 systab64 = early_memremap((unsigned long)phys,
480 if (systab64 == NULL) {
481 pr_err("Couldn't map the system table!\n");
483 early_memunmap(data, sizeof(*data));
487 efi_systab.hdr = systab64->hdr;
488 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
490 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
491 efi_systab.fw_revision = systab64->fw_revision;
492 efi_systab.con_in_handle = systab64->con_in_handle;
493 tmp |= systab64->con_in_handle;
494 efi_systab.con_in = systab64->con_in;
495 tmp |= systab64->con_in;
496 efi_systab.con_out_handle = systab64->con_out_handle;
497 tmp |= systab64->con_out_handle;
498 efi_systab.con_out = systab64->con_out;
499 tmp |= systab64->con_out;
500 efi_systab.stderr_handle = systab64->stderr_handle;
501 tmp |= systab64->stderr_handle;
502 efi_systab.stderr = systab64->stderr;
503 tmp |= systab64->stderr;
504 efi_systab.runtime = data ?
505 (void *)(unsigned long)data->runtime :
506 (void *)(unsigned long)systab64->runtime;
507 tmp |= data ? data->runtime : systab64->runtime;
508 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
509 tmp |= systab64->boottime;
510 efi_systab.nr_tables = systab64->nr_tables;
511 efi_systab.tables = data ? (unsigned long)data->tables :
513 tmp |= data ? data->tables : systab64->tables;
515 early_memunmap(systab64, sizeof(*systab64));
517 early_memunmap(data, sizeof(*data));
520 pr_err("EFI data located above 4GB, disabling EFI.\n");
525 efi_system_table_32_t *systab32;
527 systab32 = early_memremap((unsigned long)phys,
529 if (systab32 == NULL) {
530 pr_err("Couldn't map the system table!\n");
534 efi_systab.hdr = systab32->hdr;
535 efi_systab.fw_vendor = systab32->fw_vendor;
536 efi_systab.fw_revision = systab32->fw_revision;
537 efi_systab.con_in_handle = systab32->con_in_handle;
538 efi_systab.con_in = systab32->con_in;
539 efi_systab.con_out_handle = systab32->con_out_handle;
540 efi_systab.con_out = systab32->con_out;
541 efi_systab.stderr_handle = systab32->stderr_handle;
542 efi_systab.stderr = systab32->stderr;
543 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
544 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
545 efi_systab.nr_tables = systab32->nr_tables;
546 efi_systab.tables = systab32->tables;
548 early_memunmap(systab32, sizeof(*systab32));
551 efi.systab = &efi_systab;
554 * Verify the EFI Table
556 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
557 pr_err("System table signature incorrect!\n");
560 if ((efi.systab->hdr.revision >> 16) == 0)
561 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
562 efi.systab->hdr.revision >> 16,
563 efi.systab->hdr.revision & 0xffff);
565 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
570 static int __init efi_runtime_init32(void)
572 efi_runtime_services_32_t *runtime;
574 runtime = early_memremap((unsigned long)efi.systab->runtime,
575 sizeof(efi_runtime_services_32_t));
577 pr_err("Could not map the runtime service table!\n");
582 * We will only need *early* access to the following two
583 * EFI runtime services before set_virtual_address_map
586 efi_phys.set_virtual_address_map =
587 (efi_set_virtual_address_map_t *)
588 (unsigned long)runtime->set_virtual_address_map;
589 early_memunmap(runtime, sizeof(efi_runtime_services_32_t));
594 static int __init efi_runtime_init64(void)
596 efi_runtime_services_64_t *runtime;
598 runtime = early_memremap((unsigned long)efi.systab->runtime,
599 sizeof(efi_runtime_services_64_t));
601 pr_err("Could not map the runtime service table!\n");
606 * We will only need *early* access to the following two
607 * EFI runtime services before set_virtual_address_map
610 efi_phys.set_virtual_address_map =
611 (efi_set_virtual_address_map_t *)
612 (unsigned long)runtime->set_virtual_address_map;
613 early_memunmap(runtime, sizeof(efi_runtime_services_64_t));
618 static int __init efi_runtime_init(void)
623 * Check out the runtime services table. We need to map
624 * the runtime services table so that we can grab the physical
625 * address of several of the EFI runtime functions, needed to
626 * set the firmware into virtual mode.
628 if (efi_enabled(EFI_64BIT))
629 rv = efi_runtime_init64();
631 rv = efi_runtime_init32();
636 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
641 static int __init efi_memmap_init(void)
643 /* Map the EFI memory map */
644 memmap.map = early_memremap((unsigned long)memmap.phys_map,
645 memmap.nr_map * memmap.desc_size);
646 if (memmap.map == NULL) {
647 pr_err("Could not map the memory map!\n");
650 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
655 set_bit(EFI_MEMMAP, &efi.flags);
661 * A number of config table entries get remapped to virtual addresses
662 * after entering EFI virtual mode. However, the kexec kernel requires
663 * their physical addresses therefore we pass them via setup_data and
664 * correct those entries to their respective physical addresses here.
666 * Currently only handles smbios which is necessary for some firmware
669 static int __init efi_reuse_config(u64 tables, int nr_tables)
673 struct efi_setup_data *data;
678 if (!efi_enabled(EFI_64BIT))
681 data = early_memremap(efi_setup, sizeof(*data));
690 sz = sizeof(efi_config_table_64_t);
692 p = tablep = early_memremap(tables, nr_tables * sz);
694 pr_err("Could not map Configuration table!\n");
699 for (i = 0; i < efi.systab->nr_tables; i++) {
702 guid = ((efi_config_table_64_t *)p)->guid;
704 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
705 ((efi_config_table_64_t *)p)->table = data->smbios;
708 early_iounmap(tablep, nr_tables * sz);
711 early_iounmap(data, sizeof(*data));
716 void __init efi_init(void)
719 char vendor[100] = "unknown";
723 if (boot_params.efi_info.efi_systab_hi ||
724 boot_params.efi_info.efi_memmap_hi) {
725 pr_info("Table located above 4GB, disabling EFI.\n");
728 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
730 efi_phys.systab = (efi_system_table_t *)
731 (boot_params.efi_info.efi_systab |
732 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
735 if (efi_systab_init(efi_phys.systab))
738 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
740 efi.config_table = (unsigned long)efi.systab->tables;
741 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
742 efi.runtime = (unsigned long)efi.systab->runtime;
745 * Show what we know for posterity
747 c16 = early_memremap(efi.systab->fw_vendor,
748 sizeof(vendor) * sizeof(efi_char16_t));
750 for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
753 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
755 pr_err("Could not map the firmware vendor!\n");
758 pr_info("EFI v%u.%.02u by %s\n",
759 efi.systab->hdr.revision >> 16,
760 efi.systab->hdr.revision & 0xffff, vendor);
762 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
765 if (efi_config_init(arch_tables))
769 * Note: We currently don't support runtime services on an EFI
770 * that doesn't match the kernel 32/64-bit mode.
773 if (!efi_runtime_supported())
774 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
776 if (disable_runtime || efi_runtime_init())
779 if (efi_memmap_init())
782 set_bit(EFI_MEMMAP, &efi.flags);
787 void __init efi_late_init(void)
792 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
796 addr = md->virt_addr;
797 npages = md->num_pages;
799 memrange_efi_to_native(&addr, &npages);
802 set_memory_x(addr, npages);
804 set_memory_nx(addr, npages);
807 void __init runtime_code_page_mkexec(void)
809 efi_memory_desc_t *md;
812 /* Make EFI runtime service code area executable */
813 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
816 if (md->type != EFI_RUNTIME_SERVICES_CODE)
819 efi_set_executable(md, true);
823 void efi_memory_uc(u64 addr, unsigned long size)
825 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
828 npages = round_up(size, page_shift) / page_shift;
829 memrange_efi_to_native(&addr, &npages);
830 set_memory_uc(addr, npages);
833 void __init old_map_region(efi_memory_desc_t *md)
835 u64 start_pfn, end_pfn, end;
839 start_pfn = PFN_DOWN(md->phys_addr);
840 size = md->num_pages << PAGE_SHIFT;
841 end = md->phys_addr + size;
842 end_pfn = PFN_UP(end);
844 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
845 va = __va(md->phys_addr);
847 if (!(md->attribute & EFI_MEMORY_WB))
848 efi_memory_uc((u64)(unsigned long)va, size);
850 va = efi_ioremap(md->phys_addr, size,
851 md->type, md->attribute);
853 md->virt_addr = (u64) (unsigned long) va;
855 pr_err("ioremap of 0x%llX failed!\n",
856 (unsigned long long)md->phys_addr);
859 static void native_runtime_setup(void)
861 efi.get_time = virt_efi_get_time;
862 efi.set_time = virt_efi_set_time;
863 efi.get_wakeup_time = virt_efi_get_wakeup_time;
864 efi.set_wakeup_time = virt_efi_set_wakeup_time;
865 efi.get_variable = virt_efi_get_variable;
866 efi.get_next_variable = virt_efi_get_next_variable;
867 efi.set_variable = virt_efi_set_variable;
868 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
869 efi.reset_system = virt_efi_reset_system;
870 efi.query_variable_info = virt_efi_query_variable_info;
871 efi.update_capsule = virt_efi_update_capsule;
872 efi.query_capsule_caps = virt_efi_query_capsule_caps;
875 /* Merge contiguous regions of the same type and attribute */
876 static void __init efi_merge_regions(void)
879 efi_memory_desc_t *md, *prev_md = NULL;
881 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
890 if (prev_md->type != md->type ||
891 prev_md->attribute != md->attribute) {
896 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
898 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
899 prev_md->num_pages += md->num_pages;
900 md->type = EFI_RESERVED_TYPE;
908 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
913 size = md->num_pages << EFI_PAGE_SHIFT;
914 end = md->phys_addr + size;
915 systab = (u64)(unsigned long)efi_phys.systab;
916 if (md->phys_addr <= systab && systab < end) {
917 systab += md->virt_addr - md->phys_addr;
918 efi.systab = (efi_system_table_t *)(unsigned long)systab;
922 static void __init save_runtime_map(void)
925 efi_memory_desc_t *md;
926 void *tmp, *p, *q = NULL;
929 if (efi_enabled(EFI_OLD_MEMMAP))
932 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
935 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
936 (md->type == EFI_BOOT_SERVICES_CODE) ||
937 (md->type == EFI_BOOT_SERVICES_DATA))
939 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
944 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
948 efi_runtime_map_setup(q, count, memmap.desc_size);
953 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
957 static void *realloc_pages(void *old_memmap, int old_shift)
961 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
966 * A first-time allocation doesn't have anything to copy.
971 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
974 free_pages((unsigned long)old_memmap, old_shift);
979 * Iterate the EFI memory map in reverse order because the regions
980 * will be mapped top-down. The end result is the same as if we had
981 * mapped things forward, but doesn't require us to change the
982 * existing implementation of efi_map_region().
984 static inline void *efi_map_next_entry_reverse(void *entry)
988 return memmap.map_end - memmap.desc_size;
990 entry -= memmap.desc_size;
991 if (entry < memmap.map)
998 * efi_map_next_entry - Return the next EFI memory map descriptor
999 * @entry: Previous EFI memory map descriptor
1001 * This is a helper function to iterate over the EFI memory map, which
1002 * we do in different orders depending on the current configuration.
1004 * To begin traversing the memory map @entry must be %NULL.
1006 * Returns %NULL when we reach the end of the memory map.
1008 static void *efi_map_next_entry(void *entry)
1010 if (!efi_enabled(EFI_OLD_MEMMAP) && efi_enabled(EFI_64BIT)) {
1012 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
1013 * config table feature requires us to map all entries
1014 * in the same order as they appear in the EFI memory
1015 * map. That is to say, entry N must have a lower
1016 * virtual address than entry N+1. This is because the
1017 * firmware toolchain leaves relative references in
1018 * the code/data sections, which are split and become
1019 * separate EFI memory regions. Mapping things
1020 * out-of-order leads to the firmware accessing
1021 * unmapped addresses.
1023 * Since we need to map things this way whether or not
1024 * the kernel actually makes use of
1025 * EFI_PROPERTIES_TABLE, let's just switch to this
1026 * scheme by default for 64-bit.
1028 return efi_map_next_entry_reverse(entry);
1035 entry += memmap.desc_size;
1036 if (entry >= memmap.map_end)
1043 * Map the efi memory ranges of the runtime services and update new_mmap with
1044 * virtual addresses.
1046 static void * __init efi_map_regions(int *count, int *pg_shift)
1048 void *p, *new_memmap = NULL;
1049 unsigned long left = 0;
1050 efi_memory_desc_t *md;
1053 while ((p = efi_map_next_entry(p))) {
1055 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
1056 #ifdef CONFIG_X86_64
1057 if (md->type != EFI_BOOT_SERVICES_CODE &&
1058 md->type != EFI_BOOT_SERVICES_DATA)
1064 get_systab_virt_addr(md);
1066 if (left < memmap.desc_size) {
1067 new_memmap = realloc_pages(new_memmap, *pg_shift);
1071 left += PAGE_SIZE << *pg_shift;
1075 memcpy(new_memmap + (*count * memmap.desc_size), md,
1078 left -= memmap.desc_size;
1085 static void __init kexec_enter_virtual_mode(void)
1088 efi_memory_desc_t *md;
1094 * We don't do virtual mode, since we don't do runtime services, on
1097 if (!efi_is_native()) {
1103 * Map efi regions which were passed via setup_data. The virt_addr is a
1104 * fixed addr which was used in first kernel of a kexec boot.
1106 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1108 efi_map_region_fixed(md); /* FIXME: add error handling */
1109 get_systab_virt_addr(md);
1114 BUG_ON(!efi.systab);
1116 efi_sync_low_kernel_mappings();
1119 * Now that EFI is in virtual mode, update the function
1120 * pointers in the runtime service table to the new virtual addresses.
1122 * Call EFI services through wrapper functions.
1124 efi.runtime_version = efi_systab.hdr.revision;
1126 native_runtime_setup();
1128 efi.set_virtual_address_map = NULL;
1130 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1131 runtime_code_page_mkexec();
1133 /* clean DUMMY object */
1134 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1135 EFI_VARIABLE_NON_VOLATILE |
1136 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1137 EFI_VARIABLE_RUNTIME_ACCESS,
1143 * This function will switch the EFI runtime services to virtual mode.
1144 * Essentially, we look through the EFI memmap and map every region that
1145 * has the runtime attribute bit set in its memory descriptor into the
1146 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1148 * The old method which used to update that memory descriptor with the
1149 * virtual address obtained from ioremap() is still supported when the
1150 * kernel is booted with efi=old_map on its command line. Same old
1151 * method enabled the runtime services to be called without having to
1152 * thunk back into physical mode for every invocation.
1154 * The new method does a pagetable switch in a preemption-safe manner
1155 * so that we're in a different address space when calling a runtime
1156 * function. For function arguments passing we do copy the PGDs of the
1157 * kernel page table into ->trampoline_pgd prior to each call.
1159 * Specially for kexec boot, efi runtime maps in previous kernel should
1160 * be passed in via setup_data. In that case runtime ranges will be mapped
1161 * to the same virtual addresses as the first kernel, see
1162 * kexec_enter_virtual_mode().
1164 static void __init __efi_enter_virtual_mode(void)
1166 int count = 0, pg_shift = 0;
1167 void *new_memmap = NULL;
1168 efi_status_t status;
1172 efi_merge_regions();
1173 new_memmap = efi_map_regions(&count, &pg_shift);
1175 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1181 BUG_ON(!efi.systab);
1183 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1186 efi_sync_low_kernel_mappings();
1187 efi_dump_pagetable();
1189 if (efi_is_native()) {
1190 status = phys_efi_set_virtual_address_map(
1191 memmap.desc_size * count,
1193 memmap.desc_version,
1194 (efi_memory_desc_t *)__pa(new_memmap));
1196 status = efi_thunk_set_virtual_address_map(
1197 efi_phys.set_virtual_address_map,
1198 memmap.desc_size * count,
1200 memmap.desc_version,
1201 (efi_memory_desc_t *)__pa(new_memmap));
1204 if (status != EFI_SUCCESS) {
1205 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1207 panic("EFI call to SetVirtualAddressMap() failed!");
1211 * Now that EFI is in virtual mode, update the function
1212 * pointers in the runtime service table to the new virtual addresses.
1214 * Call EFI services through wrapper functions.
1216 efi.runtime_version = efi_systab.hdr.revision;
1218 if (efi_is_native())
1219 native_runtime_setup();
1221 efi_thunk_runtime_setup();
1223 efi.set_virtual_address_map = NULL;
1225 efi_runtime_mkexec();
1228 * We mapped the descriptor array into the EFI pagetable above but we're
1229 * not unmapping it here. Here's why:
1231 * We're copying select PGDs from the kernel page table to the EFI page
1232 * table and when we do so and make changes to those PGDs like unmapping
1233 * stuff from them, those changes appear in the kernel page table and we
1236 * From setup_real_mode():
1239 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1241 * In this particular case, our allocation is in PGD 0 of the EFI page
1242 * table but we've copied that PGD from PGD[272] of the EFI page table:
1244 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1246 * where the direct memory mapping in kernel space is.
1248 * new_memmap's VA comes from that direct mapping and thus clearing it,
1249 * it would get cleared in the kernel page table too.
1251 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1253 free_pages((unsigned long)new_memmap, pg_shift);
1255 /* clean DUMMY object */
1256 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1257 EFI_VARIABLE_NON_VOLATILE |
1258 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1259 EFI_VARIABLE_RUNTIME_ACCESS,
1263 void __init efi_enter_virtual_mode(void)
1266 kexec_enter_virtual_mode();
1268 __efi_enter_virtual_mode();
1272 * Convenience functions to obtain memory types and attributes
1274 u32 efi_mem_type(unsigned long phys_addr)
1276 efi_memory_desc_t *md;
1279 if (!efi_enabled(EFI_MEMMAP))
1282 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1284 if ((md->phys_addr <= phys_addr) &&
1285 (phys_addr < (md->phys_addr +
1286 (md->num_pages << EFI_PAGE_SHIFT))))
1292 u64 efi_mem_attributes(unsigned long phys_addr)
1294 efi_memory_desc_t *md;
1297 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1299 if ((md->phys_addr <= phys_addr) &&
1300 (phys_addr < (md->phys_addr +
1301 (md->num_pages << EFI_PAGE_SHIFT))))
1302 return md->attribute;
1308 * Some firmware implementations refuse to boot if there's insufficient space
1309 * in the variable store. Ensure that we never use more than a safe limit.
1311 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1314 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1316 efi_status_t status;
1317 u64 storage_size, remaining_size, max_size;
1319 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1322 status = efi.query_variable_info(attributes, &storage_size,
1323 &remaining_size, &max_size);
1324 if (status != EFI_SUCCESS)
1328 * We account for that by refusing the write if permitting it would
1329 * reduce the available space to under 5KB. This figure was provided by
1330 * Samsung, so should be safe.
1332 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1333 !efi_no_storage_paranoia) {
1336 * Triggering garbage collection may require that the firmware
1337 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1338 * that by attempting to use more space than is available.
1340 unsigned long dummy_size = remaining_size + 1024;
1341 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1344 return EFI_OUT_OF_RESOURCES;
1346 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1347 EFI_VARIABLE_NON_VOLATILE |
1348 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1349 EFI_VARIABLE_RUNTIME_ACCESS,
1352 if (status == EFI_SUCCESS) {
1354 * This should have failed, so if it didn't make sure
1355 * that we delete it...
1357 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1358 EFI_VARIABLE_NON_VOLATILE |
1359 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1360 EFI_VARIABLE_RUNTIME_ACCESS,
1367 * The runtime code may now have triggered a garbage collection
1368 * run, so check the variable info again
1370 status = efi.query_variable_info(attributes, &storage_size,
1371 &remaining_size, &max_size);
1373 if (status != EFI_SUCCESS)
1377 * There still isn't enough room, so return an error
1379 if (remaining_size - size < EFI_MIN_RESERVE)
1380 return EFI_OUT_OF_RESOURCES;
1385 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1387 static int __init parse_efi_cmdline(char *str)
1392 if (!strncmp(str, "old_map", 7))
1393 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1397 early_param("efi", parse_efi_cmdline);
1399 void __init efi_apply_memmap_quirks(void)
1402 * Once setup is done earlier, unmap the EFI memory map on mismatched
1403 * firmware/kernel architectures since there is no support for runtime
1406 if (!efi_runtime_supported()) {
1407 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1412 * UV doesn't support the new EFI pagetable mapping yet.
1415 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1419 * For most modern platforms the preferred method of powering off is via
1420 * ACPI. However, there are some that are known to require the use of
1421 * EFI runtime services and for which ACPI does not work at all.
1423 * Using EFI is a last resort, to be used only if no other option
1426 bool efi_reboot_required(void)
1428 if (!acpi_gbl_reduced_hardware)
1431 efi_reboot_quirk_mode = EFI_RESET_WARM;
1435 bool efi_poweroff_required(void)
1437 return !!acpi_gbl_reduced_hardware;