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>
55 #include <asm/uv/uv.h>
59 #define EFI_MIN_RESERVE 5120
61 #define EFI_DUMMY_GUID \
62 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
64 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
66 struct efi_memory_map memmap;
68 static struct efi efi_phys __initdata;
69 static efi_system_table_t efi_systab __initdata;
71 static efi_config_table_type_t arch_tables[] __initdata = {
73 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
75 {NULL_GUID, NULL, NULL},
78 u64 efi_setup; /* efi setup_data physical address */
80 static bool disable_runtime __initdata = false;
81 static int __init setup_noefi(char *arg)
83 disable_runtime = true;
86 early_param("noefi", setup_noefi);
89 EXPORT_SYMBOL(add_efi_memmap);
91 static int __init setup_add_efi_memmap(char *arg)
96 early_param("add_efi_memmap", setup_add_efi_memmap);
98 static bool efi_no_storage_paranoia;
100 static int __init setup_storage_paranoia(char *arg)
102 efi_no_storage_paranoia = true;
105 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
107 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
112 spin_lock_irqsave(&rtc_lock, flags);
113 status = efi_call_virt(get_time, tm, tc);
114 spin_unlock_irqrestore(&rtc_lock, flags);
118 static efi_status_t virt_efi_set_time(efi_time_t *tm)
123 spin_lock_irqsave(&rtc_lock, flags);
124 status = efi_call_virt(set_time, tm);
125 spin_unlock_irqrestore(&rtc_lock, flags);
129 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
136 spin_lock_irqsave(&rtc_lock, flags);
137 status = efi_call_virt(get_wakeup_time, 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_virt(set_wakeup_time, enabled, tm);
149 spin_unlock_irqrestore(&rtc_lock, flags);
153 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
156 unsigned long *data_size,
159 return efi_call_virt(get_variable,
164 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
168 return efi_call_virt(get_next_variable,
169 name_size, name, vendor);
172 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
175 unsigned long data_size,
178 return efi_call_virt(set_variable,
183 static efi_status_t virt_efi_query_variable_info(u32 attr,
185 u64 *remaining_space,
186 u64 *max_variable_size)
188 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
189 return EFI_UNSUPPORTED;
191 return efi_call_virt(query_variable_info, attr, storage_space,
192 remaining_space, max_variable_size);
195 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
197 return efi_call_virt(get_next_high_mono_count, count);
200 static void virt_efi_reset_system(int reset_type,
202 unsigned long data_size,
205 efi_call_virt(reset_system, reset_type, status,
209 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
211 unsigned long sg_list)
213 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
214 return EFI_UNSUPPORTED;
216 return efi_call_virt(update_capsule, capsules, count, sg_list);
219 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
224 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
225 return EFI_UNSUPPORTED;
227 return efi_call_virt(query_capsule_caps, capsules, count, max_size,
231 static efi_status_t __init phys_efi_set_virtual_address_map(
232 unsigned long memory_map_size,
233 unsigned long descriptor_size,
234 u32 descriptor_version,
235 efi_memory_desc_t *virtual_map)
239 efi_call_phys_prelog();
240 status = efi_call_phys(efi_phys.set_virtual_address_map,
241 memory_map_size, descriptor_size,
242 descriptor_version, virtual_map);
243 efi_call_phys_epilog();
247 int efi_set_rtc_mmss(const struct timespec *now)
249 unsigned long nowtime = now->tv_sec;
255 status = efi.get_time(&eft, &cap);
256 if (status != EFI_SUCCESS) {
257 pr_err("Oops: efitime: can't read time!\n");
261 rtc_time_to_tm(nowtime, &tm);
262 if (!rtc_valid_tm(&tm)) {
263 eft.year = tm.tm_year + 1900;
264 eft.month = tm.tm_mon + 1;
265 eft.day = tm.tm_mday;
266 eft.minute = tm.tm_min;
267 eft.second = tm.tm_sec;
270 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
275 status = efi.set_time(&eft);
276 if (status != EFI_SUCCESS) {
277 pr_err("Oops: efitime: can't write time!\n");
283 void efi_get_time(struct timespec *now)
289 status = efi.get_time(&eft, &cap);
290 if (status != EFI_SUCCESS)
291 pr_err("Oops: efitime: can't read time!\n");
293 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
294 eft.minute, eft.second);
299 * Tell the kernel about the EFI memory map. This might include
300 * more than the max 128 entries that can fit in the e820 legacy
301 * (zeropage) memory map.
304 static void __init do_add_efi_memmap(void)
308 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
309 efi_memory_desc_t *md = p;
310 unsigned long long start = md->phys_addr;
311 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
315 case EFI_LOADER_CODE:
316 case EFI_LOADER_DATA:
317 case EFI_BOOT_SERVICES_CODE:
318 case EFI_BOOT_SERVICES_DATA:
319 case EFI_CONVENTIONAL_MEMORY:
320 if (md->attribute & EFI_MEMORY_WB)
321 e820_type = E820_RAM;
323 e820_type = E820_RESERVED;
325 case EFI_ACPI_RECLAIM_MEMORY:
326 e820_type = E820_ACPI;
328 case EFI_ACPI_MEMORY_NVS:
329 e820_type = E820_NVS;
331 case EFI_UNUSABLE_MEMORY:
332 e820_type = E820_UNUSABLE;
336 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
337 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
338 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
340 e820_type = E820_RESERVED;
343 e820_add_region(start, size, e820_type);
345 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
348 int __init efi_memblock_x86_reserve_range(void)
350 struct efi_info *e = &boot_params.efi_info;
354 /* Can't handle data above 4GB at this time */
355 if (e->efi_memmap_hi) {
356 pr_err("Memory map is above 4GB, disabling EFI.\n");
359 pmap = e->efi_memmap;
361 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
363 memmap.phys_map = (void *)pmap;
364 memmap.nr_map = e->efi_memmap_size /
366 memmap.desc_size = e->efi_memdesc_size;
367 memmap.desc_version = e->efi_memdesc_version;
369 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
371 efi.memmap = &memmap;
376 static void __init print_efi_memmap(void)
379 efi_memory_desc_t *md;
383 for (p = memmap.map, i = 0;
385 p += memmap.desc_size, i++) {
387 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
388 i, md->type, md->attribute, md->phys_addr,
389 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
390 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
392 #endif /* EFI_DEBUG */
395 void __init efi_reserve_boot_services(void)
399 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
400 efi_memory_desc_t *md = p;
401 u64 start = md->phys_addr;
402 u64 size = md->num_pages << EFI_PAGE_SHIFT;
404 if (md->type != EFI_BOOT_SERVICES_CODE &&
405 md->type != EFI_BOOT_SERVICES_DATA)
407 /* Only reserve where possible:
408 * - Not within any already allocated areas
409 * - Not over any memory area (really needed, if above?)
410 * - Not within any part of the kernel
411 * - Not the bios reserved area
413 if ((start + size > __pa_symbol(_text)
414 && start <= __pa_symbol(_end)) ||
415 !e820_all_mapped(start, start+size, E820_RAM) ||
416 memblock_is_region_reserved(start, size)) {
417 /* Could not reserve, skip it */
419 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
420 start, start+size-1);
422 memblock_reserve(start, size);
426 void __init efi_unmap_memmap(void)
428 clear_bit(EFI_MEMMAP, &efi.flags);
430 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
435 void __init efi_free_boot_services(void)
439 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
440 efi_memory_desc_t *md = p;
441 unsigned long long start = md->phys_addr;
442 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
444 if (md->type != EFI_BOOT_SERVICES_CODE &&
445 md->type != EFI_BOOT_SERVICES_DATA)
448 /* Could not reserve boot area */
452 free_bootmem_late(start, size);
458 static int __init efi_systab_init(void *phys)
460 if (efi_enabled(EFI_64BIT)) {
461 efi_system_table_64_t *systab64;
462 struct efi_setup_data *data = NULL;
466 data = early_memremap(efi_setup, sizeof(*data));
470 systab64 = early_ioremap((unsigned long)phys,
472 if (systab64 == NULL) {
473 pr_err("Couldn't map the system table!\n");
475 early_iounmap(data, sizeof(*data));
479 efi_systab.hdr = systab64->hdr;
480 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
482 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
483 efi_systab.fw_revision = systab64->fw_revision;
484 efi_systab.con_in_handle = systab64->con_in_handle;
485 tmp |= systab64->con_in_handle;
486 efi_systab.con_in = systab64->con_in;
487 tmp |= systab64->con_in;
488 efi_systab.con_out_handle = systab64->con_out_handle;
489 tmp |= systab64->con_out_handle;
490 efi_systab.con_out = systab64->con_out;
491 tmp |= systab64->con_out;
492 efi_systab.stderr_handle = systab64->stderr_handle;
493 tmp |= systab64->stderr_handle;
494 efi_systab.stderr = systab64->stderr;
495 tmp |= systab64->stderr;
496 efi_systab.runtime = data ?
497 (void *)(unsigned long)data->runtime :
498 (void *)(unsigned long)systab64->runtime;
499 tmp |= data ? data->runtime : systab64->runtime;
500 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
501 tmp |= systab64->boottime;
502 efi_systab.nr_tables = systab64->nr_tables;
503 efi_systab.tables = data ? (unsigned long)data->tables :
505 tmp |= data ? data->tables : systab64->tables;
507 early_iounmap(systab64, sizeof(*systab64));
509 early_iounmap(data, sizeof(*data));
512 pr_err("EFI data located above 4GB, disabling EFI.\n");
517 efi_system_table_32_t *systab32;
519 systab32 = early_ioremap((unsigned long)phys,
521 if (systab32 == NULL) {
522 pr_err("Couldn't map the system table!\n");
526 efi_systab.hdr = systab32->hdr;
527 efi_systab.fw_vendor = systab32->fw_vendor;
528 efi_systab.fw_revision = systab32->fw_revision;
529 efi_systab.con_in_handle = systab32->con_in_handle;
530 efi_systab.con_in = systab32->con_in;
531 efi_systab.con_out_handle = systab32->con_out_handle;
532 efi_systab.con_out = systab32->con_out;
533 efi_systab.stderr_handle = systab32->stderr_handle;
534 efi_systab.stderr = systab32->stderr;
535 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
536 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
537 efi_systab.nr_tables = systab32->nr_tables;
538 efi_systab.tables = systab32->tables;
540 early_iounmap(systab32, sizeof(*systab32));
543 efi.systab = &efi_systab;
546 * Verify the EFI Table
548 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
549 pr_err("System table signature incorrect!\n");
552 if ((efi.systab->hdr.revision >> 16) == 0)
553 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
554 efi.systab->hdr.revision >> 16,
555 efi.systab->hdr.revision & 0xffff);
557 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
562 static int __init efi_runtime_init32(void)
564 efi_runtime_services_32_t *runtime;
566 runtime = early_ioremap((unsigned long)efi.systab->runtime,
567 sizeof(efi_runtime_services_32_t));
569 pr_err("Could not map the runtime service table!\n");
574 * We will only need *early* access to the following two
575 * EFI runtime services before set_virtual_address_map
578 efi_phys.set_virtual_address_map =
579 (efi_set_virtual_address_map_t *)
580 (unsigned long)runtime->set_virtual_address_map;
581 early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
586 static int __init efi_runtime_init64(void)
588 efi_runtime_services_64_t *runtime;
590 runtime = early_ioremap((unsigned long)efi.systab->runtime,
591 sizeof(efi_runtime_services_64_t));
593 pr_err("Could not map the runtime service table!\n");
598 * We will only need *early* access to the following two
599 * EFI runtime services before set_virtual_address_map
602 efi_phys.set_virtual_address_map =
603 (efi_set_virtual_address_map_t *)
604 (unsigned long)runtime->set_virtual_address_map;
605 early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
610 static int __init efi_runtime_init(void)
615 * Check out the runtime services table. We need to map
616 * the runtime services table so that we can grab the physical
617 * address of several of the EFI runtime functions, needed to
618 * set the firmware into virtual mode.
620 if (efi_enabled(EFI_64BIT))
621 rv = efi_runtime_init64();
623 rv = efi_runtime_init32();
628 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
633 static int __init efi_memmap_init(void)
635 /* Map the EFI memory map */
636 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
637 memmap.nr_map * memmap.desc_size);
638 if (memmap.map == NULL) {
639 pr_err("Could not map the memory map!\n");
642 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
647 set_bit(EFI_MEMMAP, &efi.flags);
653 * A number of config table entries get remapped to virtual addresses
654 * after entering EFI virtual mode. However, the kexec kernel requires
655 * their physical addresses therefore we pass them via setup_data and
656 * correct those entries to their respective physical addresses here.
658 * Currently only handles smbios which is necessary for some firmware
661 static int __init efi_reuse_config(u64 tables, int nr_tables)
665 struct efi_setup_data *data;
670 if (!efi_enabled(EFI_64BIT))
673 data = early_memremap(efi_setup, sizeof(*data));
682 sz = sizeof(efi_config_table_64_t);
684 p = tablep = early_memremap(tables, nr_tables * sz);
686 pr_err("Could not map Configuration table!\n");
691 for (i = 0; i < efi.systab->nr_tables; i++) {
694 guid = ((efi_config_table_64_t *)p)->guid;
696 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
697 ((efi_config_table_64_t *)p)->table = data->smbios;
700 early_iounmap(tablep, nr_tables * sz);
703 early_iounmap(data, sizeof(*data));
708 void __init efi_init(void)
711 char vendor[100] = "unknown";
716 if (boot_params.efi_info.efi_systab_hi ||
717 boot_params.efi_info.efi_memmap_hi) {
718 pr_info("Table located above 4GB, disabling EFI.\n");
721 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
723 efi_phys.systab = (efi_system_table_t *)
724 (boot_params.efi_info.efi_systab |
725 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
728 if (efi_systab_init(efi_phys.systab))
731 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
733 efi.config_table = (unsigned long)efi.systab->tables;
734 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
735 efi.runtime = (unsigned long)efi.systab->runtime;
738 * Show what we know for posterity
740 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
742 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
746 pr_err("Could not map the firmware vendor!\n");
747 early_iounmap(tmp, 2);
749 pr_info("EFI v%u.%.02u by %s\n",
750 efi.systab->hdr.revision >> 16,
751 efi.systab->hdr.revision & 0xffff, vendor);
753 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
756 if (efi_config_init(arch_tables))
760 * Note: We currently don't support runtime services on an EFI
761 * that doesn't match the kernel 32/64-bit mode.
764 if (!efi_runtime_supported())
765 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
767 if (disable_runtime || efi_runtime_init())
770 if (efi_memmap_init())
773 set_bit(EFI_MEMMAP, &efi.flags);
778 void __init efi_late_init(void)
783 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
787 addr = md->virt_addr;
788 npages = md->num_pages;
790 memrange_efi_to_native(&addr, &npages);
793 set_memory_x(addr, npages);
795 set_memory_nx(addr, npages);
798 void __init runtime_code_page_mkexec(void)
800 efi_memory_desc_t *md;
803 /* Make EFI runtime service code area executable */
804 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
807 if (md->type != EFI_RUNTIME_SERVICES_CODE)
810 efi_set_executable(md, true);
814 void efi_memory_uc(u64 addr, unsigned long size)
816 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
819 npages = round_up(size, page_shift) / page_shift;
820 memrange_efi_to_native(&addr, &npages);
821 set_memory_uc(addr, npages);
824 void __init old_map_region(efi_memory_desc_t *md)
826 u64 start_pfn, end_pfn, end;
830 start_pfn = PFN_DOWN(md->phys_addr);
831 size = md->num_pages << PAGE_SHIFT;
832 end = md->phys_addr + size;
833 end_pfn = PFN_UP(end);
835 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
836 va = __va(md->phys_addr);
838 if (!(md->attribute & EFI_MEMORY_WB))
839 efi_memory_uc((u64)(unsigned long)va, size);
841 va = efi_ioremap(md->phys_addr, size,
842 md->type, md->attribute);
844 md->virt_addr = (u64) (unsigned long) va;
846 pr_err("ioremap of 0x%llX failed!\n",
847 (unsigned long long)md->phys_addr);
850 static void native_runtime_setup(void)
852 efi.get_time = virt_efi_get_time;
853 efi.set_time = virt_efi_set_time;
854 efi.get_wakeup_time = virt_efi_get_wakeup_time;
855 efi.set_wakeup_time = virt_efi_set_wakeup_time;
856 efi.get_variable = virt_efi_get_variable;
857 efi.get_next_variable = virt_efi_get_next_variable;
858 efi.set_variable = virt_efi_set_variable;
859 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
860 efi.reset_system = virt_efi_reset_system;
861 efi.query_variable_info = virt_efi_query_variable_info;
862 efi.update_capsule = virt_efi_update_capsule;
863 efi.query_capsule_caps = virt_efi_query_capsule_caps;
866 /* Merge contiguous regions of the same type and attribute */
867 static void __init efi_merge_regions(void)
870 efi_memory_desc_t *md, *prev_md = NULL;
872 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
881 if (prev_md->type != md->type ||
882 prev_md->attribute != md->attribute) {
887 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
889 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
890 prev_md->num_pages += md->num_pages;
891 md->type = EFI_RESERVED_TYPE;
899 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
904 size = md->num_pages << EFI_PAGE_SHIFT;
905 end = md->phys_addr + size;
906 systab = (u64)(unsigned long)efi_phys.systab;
907 if (md->phys_addr <= systab && systab < end) {
908 systab += md->virt_addr - md->phys_addr;
909 efi.systab = (efi_system_table_t *)(unsigned long)systab;
913 static void __init save_runtime_map(void)
916 efi_memory_desc_t *md;
917 void *tmp, *p, *q = NULL;
920 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
923 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
924 (md->type == EFI_BOOT_SERVICES_CODE) ||
925 (md->type == EFI_BOOT_SERVICES_DATA))
927 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
932 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
936 efi_runtime_map_setup(q, count, memmap.desc_size);
941 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
945 static void *realloc_pages(void *old_memmap, int old_shift)
949 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
954 * A first-time allocation doesn't have anything to copy.
959 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
962 free_pages((unsigned long)old_memmap, old_shift);
967 * Map the efi memory ranges of the runtime services and update new_mmap with
970 static void * __init efi_map_regions(int *count, int *pg_shift)
972 void *p, *new_memmap = NULL;
973 unsigned long left = 0;
974 efi_memory_desc_t *md;
976 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
978 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
980 if (md->type != EFI_BOOT_SERVICES_CODE &&
981 md->type != EFI_BOOT_SERVICES_DATA)
987 get_systab_virt_addr(md);
989 if (left < memmap.desc_size) {
990 new_memmap = realloc_pages(new_memmap, *pg_shift);
994 left += PAGE_SIZE << *pg_shift;
998 memcpy(new_memmap + (*count * memmap.desc_size), md,
1001 left -= memmap.desc_size;
1008 static void __init kexec_enter_virtual_mode(void)
1011 efi_memory_desc_t *md;
1017 * We don't do virtual mode, since we don't do runtime services, on
1020 if (!efi_is_native()) {
1026 * Map efi regions which were passed via setup_data. The virt_addr is a
1027 * fixed addr which was used in first kernel of a kexec boot.
1029 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1031 efi_map_region_fixed(md); /* FIXME: add error handling */
1032 get_systab_virt_addr(md);
1037 BUG_ON(!efi.systab);
1039 efi_sync_low_kernel_mappings();
1042 * Now that EFI is in virtual mode, update the function
1043 * pointers in the runtime service table to the new virtual addresses.
1045 * Call EFI services through wrapper functions.
1047 efi.runtime_version = efi_systab.hdr.revision;
1049 native_runtime_setup();
1051 efi.set_virtual_address_map = NULL;
1053 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1054 runtime_code_page_mkexec();
1056 /* clean DUMMY object */
1057 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1058 EFI_VARIABLE_NON_VOLATILE |
1059 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1060 EFI_VARIABLE_RUNTIME_ACCESS,
1066 * This function will switch the EFI runtime services to virtual mode.
1067 * Essentially, we look through the EFI memmap and map every region that
1068 * has the runtime attribute bit set in its memory descriptor into the
1069 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1071 * The old method which used to update that memory descriptor with the
1072 * virtual address obtained from ioremap() is still supported when the
1073 * kernel is booted with efi=old_map on its command line. Same old
1074 * method enabled the runtime services to be called without having to
1075 * thunk back into physical mode for every invocation.
1077 * The new method does a pagetable switch in a preemption-safe manner
1078 * so that we're in a different address space when calling a runtime
1079 * function. For function arguments passing we do copy the PGDs of the
1080 * kernel page table into ->trampoline_pgd prior to each call.
1082 * Specially for kexec boot, efi runtime maps in previous kernel should
1083 * be passed in via setup_data. In that case runtime ranges will be mapped
1084 * to the same virtual addresses as the first kernel, see
1085 * kexec_enter_virtual_mode().
1087 static void __init __efi_enter_virtual_mode(void)
1089 int count = 0, pg_shift = 0;
1090 void *new_memmap = NULL;
1091 efi_status_t status;
1095 efi_merge_regions();
1096 new_memmap = efi_map_regions(&count, &pg_shift);
1098 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1104 BUG_ON(!efi.systab);
1106 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1109 efi_sync_low_kernel_mappings();
1110 efi_dump_pagetable();
1112 if (efi_is_native()) {
1113 status = phys_efi_set_virtual_address_map(
1114 memmap.desc_size * count,
1116 memmap.desc_version,
1117 (efi_memory_desc_t *)__pa(new_memmap));
1119 status = efi_thunk_set_virtual_address_map(
1120 efi_phys.set_virtual_address_map,
1121 memmap.desc_size * count,
1123 memmap.desc_version,
1124 (efi_memory_desc_t *)__pa(new_memmap));
1127 if (status != EFI_SUCCESS) {
1128 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1130 panic("EFI call to SetVirtualAddressMap() failed!");
1134 * Now that EFI is in virtual mode, update the function
1135 * pointers in the runtime service table to the new virtual addresses.
1137 * Call EFI services through wrapper functions.
1139 efi.runtime_version = efi_systab.hdr.revision;
1141 if (efi_is_native())
1142 native_runtime_setup();
1144 efi_thunk_runtime_setup();
1146 efi.set_virtual_address_map = NULL;
1148 efi_runtime_mkexec();
1151 * We mapped the descriptor array into the EFI pagetable above but we're
1152 * not unmapping it here. Here's why:
1154 * We're copying select PGDs from the kernel page table to the EFI page
1155 * table and when we do so and make changes to those PGDs like unmapping
1156 * stuff from them, those changes appear in the kernel page table and we
1159 * From setup_real_mode():
1162 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1164 * In this particular case, our allocation is in PGD 0 of the EFI page
1165 * table but we've copied that PGD from PGD[272] of the EFI page table:
1167 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1169 * where the direct memory mapping in kernel space is.
1171 * new_memmap's VA comes from that direct mapping and thus clearing it,
1172 * it would get cleared in the kernel page table too.
1174 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1176 free_pages((unsigned long)new_memmap, pg_shift);
1178 /* clean DUMMY object */
1179 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1180 EFI_VARIABLE_NON_VOLATILE |
1181 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1182 EFI_VARIABLE_RUNTIME_ACCESS,
1186 void __init efi_enter_virtual_mode(void)
1189 kexec_enter_virtual_mode();
1191 __efi_enter_virtual_mode();
1195 * Convenience functions to obtain memory types and attributes
1197 u32 efi_mem_type(unsigned long phys_addr)
1199 efi_memory_desc_t *md;
1202 if (!efi_enabled(EFI_MEMMAP))
1205 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1207 if ((md->phys_addr <= phys_addr) &&
1208 (phys_addr < (md->phys_addr +
1209 (md->num_pages << EFI_PAGE_SHIFT))))
1215 u64 efi_mem_attributes(unsigned long phys_addr)
1217 efi_memory_desc_t *md;
1220 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1222 if ((md->phys_addr <= phys_addr) &&
1223 (phys_addr < (md->phys_addr +
1224 (md->num_pages << EFI_PAGE_SHIFT))))
1225 return md->attribute;
1231 * Some firmware implementations refuse to boot if there's insufficient space
1232 * in the variable store. Ensure that we never use more than a safe limit.
1234 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1237 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1239 efi_status_t status;
1240 u64 storage_size, remaining_size, max_size;
1242 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1245 status = efi.query_variable_info(attributes, &storage_size,
1246 &remaining_size, &max_size);
1247 if (status != EFI_SUCCESS)
1251 * We account for that by refusing the write if permitting it would
1252 * reduce the available space to under 5KB. This figure was provided by
1253 * Samsung, so should be safe.
1255 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1256 !efi_no_storage_paranoia) {
1259 * Triggering garbage collection may require that the firmware
1260 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1261 * that by attempting to use more space than is available.
1263 unsigned long dummy_size = remaining_size + 1024;
1264 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1267 return EFI_OUT_OF_RESOURCES;
1269 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1270 EFI_VARIABLE_NON_VOLATILE |
1271 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1272 EFI_VARIABLE_RUNTIME_ACCESS,
1275 if (status == EFI_SUCCESS) {
1277 * This should have failed, so if it didn't make sure
1278 * that we delete it...
1280 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1281 EFI_VARIABLE_NON_VOLATILE |
1282 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1283 EFI_VARIABLE_RUNTIME_ACCESS,
1290 * The runtime code may now have triggered a garbage collection
1291 * run, so check the variable info again
1293 status = efi.query_variable_info(attributes, &storage_size,
1294 &remaining_size, &max_size);
1296 if (status != EFI_SUCCESS)
1300 * There still isn't enough room, so return an error
1302 if (remaining_size - size < EFI_MIN_RESERVE)
1303 return EFI_OUT_OF_RESOURCES;
1308 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1310 static int __init parse_efi_cmdline(char *str)
1315 if (!strncmp(str, "old_map", 7))
1316 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1320 early_param("efi", parse_efi_cmdline);
1322 void __init efi_apply_memmap_quirks(void)
1325 * Once setup is done earlier, unmap the EFI memory map on mismatched
1326 * firmware/kernel architectures since there is no support for runtime
1329 if (!efi_runtime_supported()) {
1330 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1335 * UV doesn't support the new EFI pagetable mapping yet.
1338 set_bit(EFI_OLD_MEMMAP, &efi.flags);