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_virt2(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_virt1(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_virt3(get_wakeup_time,
138 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_virt2(set_wakeup_time,
151 spin_unlock_irqrestore(&rtc_lock, flags);
155 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
158 unsigned long *data_size,
161 return efi_call_virt5(get_variable,
166 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
170 return efi_call_virt3(get_next_variable,
171 name_size, name, vendor);
174 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
177 unsigned long data_size,
180 return efi_call_virt5(set_variable,
185 static efi_status_t virt_efi_query_variable_info(u32 attr,
187 u64 *remaining_space,
188 u64 *max_variable_size)
190 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
191 return EFI_UNSUPPORTED;
193 return efi_call_virt4(query_variable_info, attr, storage_space,
194 remaining_space, max_variable_size);
197 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
199 return efi_call_virt1(get_next_high_mono_count, count);
202 static void virt_efi_reset_system(int reset_type,
204 unsigned long data_size,
207 efi_call_virt4(reset_system, reset_type, status,
211 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
213 unsigned long sg_list)
215 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
216 return EFI_UNSUPPORTED;
218 return efi_call_virt3(update_capsule, capsules, count, sg_list);
221 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
226 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
227 return EFI_UNSUPPORTED;
229 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
233 static efi_status_t __init phys_efi_set_virtual_address_map(
234 unsigned long memory_map_size,
235 unsigned long descriptor_size,
236 u32 descriptor_version,
237 efi_memory_desc_t *virtual_map)
241 efi_call_phys_prelog();
242 status = efi_call_phys4(efi_phys.set_virtual_address_map,
243 memory_map_size, descriptor_size,
244 descriptor_version, virtual_map);
245 efi_call_phys_epilog();
249 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
255 spin_lock_irqsave(&rtc_lock, flags);
256 efi_call_phys_prelog();
257 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
259 efi_call_phys_epilog();
260 spin_unlock_irqrestore(&rtc_lock, flags);
264 int efi_set_rtc_mmss(const struct timespec *now)
266 unsigned long nowtime = now->tv_sec;
272 status = efi.get_time(&eft, &cap);
273 if (status != EFI_SUCCESS) {
274 pr_err("Oops: efitime: can't read time!\n");
278 rtc_time_to_tm(nowtime, &tm);
279 if (!rtc_valid_tm(&tm)) {
280 eft.year = tm.tm_year + 1900;
281 eft.month = tm.tm_mon + 1;
282 eft.day = tm.tm_mday;
283 eft.minute = tm.tm_min;
284 eft.second = tm.tm_sec;
287 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
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, range=[0x%016llx-0x%016llx) (%lluMB)\n",
405 i, md->type, md->attribute, md->phys_addr,
406 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
407 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
409 #endif /* EFI_DEBUG */
412 void __init efi_reserve_boot_services(void)
416 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
417 efi_memory_desc_t *md = p;
418 u64 start = md->phys_addr;
419 u64 size = md->num_pages << EFI_PAGE_SHIFT;
421 if (md->type != EFI_BOOT_SERVICES_CODE &&
422 md->type != EFI_BOOT_SERVICES_DATA)
424 /* Only reserve where possible:
425 * - Not within any already allocated areas
426 * - Not over any memory area (really needed, if above?)
427 * - Not within any part of the kernel
428 * - Not the bios reserved area
430 if ((start + size > __pa_symbol(_text)
431 && start <= __pa_symbol(_end)) ||
432 !e820_all_mapped(start, start+size, E820_RAM) ||
433 memblock_is_region_reserved(start, size)) {
434 /* Could not reserve, skip it */
436 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
437 start, start+size-1);
439 memblock_reserve(start, size);
443 void __init efi_unmap_memmap(void)
445 clear_bit(EFI_MEMMAP, &efi.flags);
447 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
452 void __init efi_free_boot_services(void)
456 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
457 efi_memory_desc_t *md = p;
458 unsigned long long start = md->phys_addr;
459 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
461 if (md->type != EFI_BOOT_SERVICES_CODE &&
462 md->type != EFI_BOOT_SERVICES_DATA)
465 /* Could not reserve boot area */
469 free_bootmem_late(start, size);
475 static int __init efi_systab_init(void *phys)
477 if (efi_enabled(EFI_64BIT)) {
478 efi_system_table_64_t *systab64;
479 struct efi_setup_data *data = NULL;
483 data = early_memremap(efi_setup, sizeof(*data));
487 systab64 = early_ioremap((unsigned long)phys,
489 if (systab64 == NULL) {
490 pr_err("Couldn't map the system table!\n");
492 early_iounmap(data, sizeof(*data));
496 efi_systab.hdr = systab64->hdr;
497 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
499 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
500 efi_systab.fw_revision = systab64->fw_revision;
501 efi_systab.con_in_handle = systab64->con_in_handle;
502 tmp |= systab64->con_in_handle;
503 efi_systab.con_in = systab64->con_in;
504 tmp |= systab64->con_in;
505 efi_systab.con_out_handle = systab64->con_out_handle;
506 tmp |= systab64->con_out_handle;
507 efi_systab.con_out = systab64->con_out;
508 tmp |= systab64->con_out;
509 efi_systab.stderr_handle = systab64->stderr_handle;
510 tmp |= systab64->stderr_handle;
511 efi_systab.stderr = systab64->stderr;
512 tmp |= systab64->stderr;
513 efi_systab.runtime = data ?
514 (void *)(unsigned long)data->runtime :
515 (void *)(unsigned long)systab64->runtime;
516 tmp |= data ? data->runtime : systab64->runtime;
517 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
518 tmp |= systab64->boottime;
519 efi_systab.nr_tables = systab64->nr_tables;
520 efi_systab.tables = data ? (unsigned long)data->tables :
522 tmp |= data ? data->tables : systab64->tables;
524 early_iounmap(systab64, sizeof(*systab64));
526 early_iounmap(data, sizeof(*data));
529 pr_err("EFI data located above 4GB, disabling EFI.\n");
534 efi_system_table_32_t *systab32;
536 systab32 = early_ioremap((unsigned long)phys,
538 if (systab32 == NULL) {
539 pr_err("Couldn't map the system table!\n");
543 efi_systab.hdr = systab32->hdr;
544 efi_systab.fw_vendor = systab32->fw_vendor;
545 efi_systab.fw_revision = systab32->fw_revision;
546 efi_systab.con_in_handle = systab32->con_in_handle;
547 efi_systab.con_in = systab32->con_in;
548 efi_systab.con_out_handle = systab32->con_out_handle;
549 efi_systab.con_out = systab32->con_out;
550 efi_systab.stderr_handle = systab32->stderr_handle;
551 efi_systab.stderr = systab32->stderr;
552 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
553 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
554 efi_systab.nr_tables = systab32->nr_tables;
555 efi_systab.tables = systab32->tables;
557 early_iounmap(systab32, sizeof(*systab32));
560 efi.systab = &efi_systab;
563 * Verify the EFI Table
565 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
566 pr_err("System table signature incorrect!\n");
569 if ((efi.systab->hdr.revision >> 16) == 0)
570 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
571 efi.systab->hdr.revision >> 16,
572 efi.systab->hdr.revision & 0xffff);
574 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
579 static int __init efi_runtime_init32(void)
581 efi_runtime_services_32_t *runtime;
583 runtime = early_ioremap((unsigned long)efi.systab->runtime,
584 sizeof(efi_runtime_services_32_t));
586 pr_err("Could not map the runtime service table!\n");
591 * We will only need *early* access to the following two
592 * EFI runtime services before set_virtual_address_map
595 efi_phys.get_time = (efi_get_time_t *)
596 (unsigned long)runtime->get_time;
597 efi_phys.set_virtual_address_map =
598 (efi_set_virtual_address_map_t *)
599 (unsigned long)runtime->set_virtual_address_map;
601 * Make efi_get_time can be called before entering
604 efi.get_time = phys_efi_get_time;
605 early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
610 static int __init efi_runtime_init64(void)
612 efi_runtime_services_64_t *runtime;
614 runtime = early_ioremap((unsigned long)efi.systab->runtime,
615 sizeof(efi_runtime_services_64_t));
617 pr_err("Could not map the runtime service table!\n");
622 * We will only need *early* access to the following two
623 * EFI runtime services before set_virtual_address_map
626 efi_phys.get_time = (efi_get_time_t *)
627 (unsigned long)runtime->get_time;
628 efi_phys.set_virtual_address_map =
629 (efi_set_virtual_address_map_t *)
630 (unsigned long)runtime->set_virtual_address_map;
632 * Make efi_get_time can be called before entering
635 efi.get_time = phys_efi_get_time;
636 early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
641 static int __init efi_runtime_init(void)
646 * Check out the runtime services table. We need to map
647 * the runtime services table so that we can grab the physical
648 * address of several of the EFI runtime functions, needed to
649 * set the firmware into virtual mode.
651 if (efi_enabled(EFI_64BIT))
652 rv = efi_runtime_init64();
654 rv = efi_runtime_init32();
659 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
664 static int __init efi_memmap_init(void)
666 /* Map the EFI memory map */
667 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
668 memmap.nr_map * memmap.desc_size);
669 if (memmap.map == NULL) {
670 pr_err("Could not map the memory map!\n");
673 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
678 set_bit(EFI_MEMMAP, &efi.flags);
684 * A number of config table entries get remapped to virtual addresses
685 * after entering EFI virtual mode. However, the kexec kernel requires
686 * their physical addresses therefore we pass them via setup_data and
687 * correct those entries to their respective physical addresses here.
689 * Currently only handles smbios which is necessary for some firmware
692 static int __init efi_reuse_config(u64 tables, int nr_tables)
696 struct efi_setup_data *data;
701 if (!efi_enabled(EFI_64BIT))
704 data = early_memremap(efi_setup, sizeof(*data));
713 sz = sizeof(efi_config_table_64_t);
715 p = tablep = early_memremap(tables, nr_tables * sz);
717 pr_err("Could not map Configuration table!\n");
722 for (i = 0; i < efi.systab->nr_tables; i++) {
725 guid = ((efi_config_table_64_t *)p)->guid;
727 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
728 ((efi_config_table_64_t *)p)->table = data->smbios;
731 early_iounmap(tablep, nr_tables * sz);
734 early_iounmap(data, sizeof(*data));
739 void __init efi_init(void)
742 char vendor[100] = "unknown";
747 if (boot_params.efi_info.efi_systab_hi ||
748 boot_params.efi_info.efi_memmap_hi) {
749 pr_info("Table located above 4GB, disabling EFI.\n");
752 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
754 efi_phys.systab = (efi_system_table_t *)
755 (boot_params.efi_info.efi_systab |
756 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
759 if (efi_systab_init(efi_phys.systab))
762 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
764 efi.config_table = (unsigned long)efi.systab->tables;
765 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
766 efi.runtime = (unsigned long)efi.systab->runtime;
769 * Show what we know for posterity
771 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
773 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
777 pr_err("Could not map the firmware vendor!\n");
778 early_iounmap(tmp, 2);
780 pr_info("EFI v%u.%.02u by %s\n",
781 efi.systab->hdr.revision >> 16,
782 efi.systab->hdr.revision & 0xffff, vendor);
784 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
787 if (efi_config_init(arch_tables))
791 * Note: We currently don't support runtime services on an EFI
792 * that doesn't match the kernel 32/64-bit mode.
795 if (!efi_runtime_supported())
796 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
798 if (disable_runtime || efi_runtime_init())
801 if (efi_memmap_init())
804 set_bit(EFI_MEMMAP, &efi.flags);
809 void __init efi_late_init(void)
814 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
818 addr = md->virt_addr;
819 npages = md->num_pages;
821 memrange_efi_to_native(&addr, &npages);
824 set_memory_x(addr, npages);
826 set_memory_nx(addr, npages);
829 void __init runtime_code_page_mkexec(void)
831 efi_memory_desc_t *md;
834 /* Make EFI runtime service code area executable */
835 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
838 if (md->type != EFI_RUNTIME_SERVICES_CODE)
841 efi_set_executable(md, true);
845 void efi_memory_uc(u64 addr, unsigned long size)
847 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
850 npages = round_up(size, page_shift) / page_shift;
851 memrange_efi_to_native(&addr, &npages);
852 set_memory_uc(addr, npages);
855 void __init old_map_region(efi_memory_desc_t *md)
857 u64 start_pfn, end_pfn, end;
861 start_pfn = PFN_DOWN(md->phys_addr);
862 size = md->num_pages << PAGE_SHIFT;
863 end = md->phys_addr + size;
864 end_pfn = PFN_UP(end);
866 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
867 va = __va(md->phys_addr);
869 if (!(md->attribute & EFI_MEMORY_WB))
870 efi_memory_uc((u64)(unsigned long)va, size);
872 va = efi_ioremap(md->phys_addr, size,
873 md->type, md->attribute);
875 md->virt_addr = (u64) (unsigned long) va;
877 pr_err("ioremap of 0x%llX failed!\n",
878 (unsigned long long)md->phys_addr);
881 static void native_runtime_setup(void)
883 efi.get_time = virt_efi_get_time;
884 efi.set_time = virt_efi_set_time;
885 efi.get_wakeup_time = virt_efi_get_wakeup_time;
886 efi.set_wakeup_time = virt_efi_set_wakeup_time;
887 efi.get_variable = virt_efi_get_variable;
888 efi.get_next_variable = virt_efi_get_next_variable;
889 efi.set_variable = virt_efi_set_variable;
890 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
891 efi.reset_system = virt_efi_reset_system;
892 efi.query_variable_info = virt_efi_query_variable_info;
893 efi.update_capsule = virt_efi_update_capsule;
894 efi.query_capsule_caps = virt_efi_query_capsule_caps;
897 /* Merge contiguous regions of the same type and attribute */
898 static void __init efi_merge_regions(void)
901 efi_memory_desc_t *md, *prev_md = NULL;
903 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
912 if (prev_md->type != md->type ||
913 prev_md->attribute != md->attribute) {
918 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
920 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
921 prev_md->num_pages += md->num_pages;
922 md->type = EFI_RESERVED_TYPE;
930 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
935 size = md->num_pages << EFI_PAGE_SHIFT;
936 end = md->phys_addr + size;
937 systab = (u64)(unsigned long)efi_phys.systab;
938 if (md->phys_addr <= systab && systab < end) {
939 systab += md->virt_addr - md->phys_addr;
940 efi.systab = (efi_system_table_t *)(unsigned long)systab;
944 static void __init save_runtime_map(void)
947 efi_memory_desc_t *md;
948 void *tmp, *p, *q = NULL;
951 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
954 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
955 (md->type == EFI_BOOT_SERVICES_CODE) ||
956 (md->type == EFI_BOOT_SERVICES_DATA))
958 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
963 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
967 efi_runtime_map_setup(q, count, memmap.desc_size);
972 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
976 static void *realloc_pages(void *old_memmap, int old_shift)
980 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
985 * A first-time allocation doesn't have anything to copy.
990 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
993 free_pages((unsigned long)old_memmap, old_shift);
998 * Map the efi memory ranges of the runtime services and update new_mmap with
1001 static void * __init efi_map_regions(int *count, int *pg_shift)
1003 void *p, *new_memmap = NULL;
1004 unsigned long left = 0;
1005 efi_memory_desc_t *md;
1007 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1009 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
1010 #ifdef CONFIG_X86_64
1011 if (md->type != EFI_BOOT_SERVICES_CODE &&
1012 md->type != EFI_BOOT_SERVICES_DATA)
1018 get_systab_virt_addr(md);
1020 if (left < memmap.desc_size) {
1021 new_memmap = realloc_pages(new_memmap, *pg_shift);
1025 left += PAGE_SIZE << *pg_shift;
1029 memcpy(new_memmap + (*count * memmap.desc_size), md,
1032 left -= memmap.desc_size;
1039 static void __init kexec_enter_virtual_mode(void)
1042 efi_memory_desc_t *md;
1048 * We don't do virtual mode, since we don't do runtime services, on
1051 if (!efi_is_native()) {
1057 * Map efi regions which were passed via setup_data. The virt_addr is a
1058 * fixed addr which was used in first kernel of a kexec boot.
1060 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1062 efi_map_region_fixed(md); /* FIXME: add error handling */
1063 get_systab_virt_addr(md);
1068 BUG_ON(!efi.systab);
1070 efi_sync_low_kernel_mappings();
1073 * Now that EFI is in virtual mode, update the function
1074 * pointers in the runtime service table to the new virtual addresses.
1076 * Call EFI services through wrapper functions.
1078 efi.runtime_version = efi_systab.hdr.revision;
1080 native_runtime_setup();
1082 efi.set_virtual_address_map = NULL;
1084 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1085 runtime_code_page_mkexec();
1087 /* clean DUMMY object */
1088 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1089 EFI_VARIABLE_NON_VOLATILE |
1090 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1091 EFI_VARIABLE_RUNTIME_ACCESS,
1097 * This function will switch the EFI runtime services to virtual mode.
1098 * Essentially, we look through the EFI memmap and map every region that
1099 * has the runtime attribute bit set in its memory descriptor into the
1100 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1102 * The old method which used to update that memory descriptor with the
1103 * virtual address obtained from ioremap() is still supported when the
1104 * kernel is booted with efi=old_map on its command line. Same old
1105 * method enabled the runtime services to be called without having to
1106 * thunk back into physical mode for every invocation.
1108 * The new method does a pagetable switch in a preemption-safe manner
1109 * so that we're in a different address space when calling a runtime
1110 * function. For function arguments passing we do copy the PGDs of the
1111 * kernel page table into ->trampoline_pgd prior to each call.
1113 * Specially for kexec boot, efi runtime maps in previous kernel should
1114 * be passed in via setup_data. In that case runtime ranges will be mapped
1115 * to the same virtual addresses as the first kernel, see
1116 * kexec_enter_virtual_mode().
1118 static void __init __efi_enter_virtual_mode(void)
1120 int count = 0, pg_shift = 0;
1121 void *new_memmap = NULL;
1122 efi_status_t status;
1126 efi_merge_regions();
1127 new_memmap = efi_map_regions(&count, &pg_shift);
1129 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1135 BUG_ON(!efi.systab);
1137 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1140 efi_sync_low_kernel_mappings();
1141 efi_dump_pagetable();
1143 if (efi_is_native()) {
1144 status = phys_efi_set_virtual_address_map(
1145 memmap.desc_size * count,
1147 memmap.desc_version,
1148 (efi_memory_desc_t *)__pa(new_memmap));
1150 status = efi_thunk_set_virtual_address_map(
1151 efi_phys.set_virtual_address_map,
1152 memmap.desc_size * count,
1154 memmap.desc_version,
1155 (efi_memory_desc_t *)__pa(new_memmap));
1158 if (status != EFI_SUCCESS) {
1159 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1161 panic("EFI call to SetVirtualAddressMap() failed!");
1165 * Now that EFI is in virtual mode, update the function
1166 * pointers in the runtime service table to the new virtual addresses.
1168 * Call EFI services through wrapper functions.
1170 efi.runtime_version = efi_systab.hdr.revision;
1172 if (efi_is_native())
1173 native_runtime_setup();
1175 efi_thunk_runtime_setup();
1177 efi.set_virtual_address_map = NULL;
1179 efi_runtime_mkexec();
1182 * We mapped the descriptor array into the EFI pagetable above but we're
1183 * not unmapping it here. Here's why:
1185 * We're copying select PGDs from the kernel page table to the EFI page
1186 * table and when we do so and make changes to those PGDs like unmapping
1187 * stuff from them, those changes appear in the kernel page table and we
1190 * From setup_real_mode():
1193 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1195 * In this particular case, our allocation is in PGD 0 of the EFI page
1196 * table but we've copied that PGD from PGD[272] of the EFI page table:
1198 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1200 * where the direct memory mapping in kernel space is.
1202 * new_memmap's VA comes from that direct mapping and thus clearing it,
1203 * it would get cleared in the kernel page table too.
1205 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1207 free_pages((unsigned long)new_memmap, pg_shift);
1209 /* clean DUMMY object */
1210 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1211 EFI_VARIABLE_NON_VOLATILE |
1212 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1213 EFI_VARIABLE_RUNTIME_ACCESS,
1217 void __init efi_enter_virtual_mode(void)
1220 kexec_enter_virtual_mode();
1222 __efi_enter_virtual_mode();
1226 * Convenience functions to obtain memory types and attributes
1228 u32 efi_mem_type(unsigned long phys_addr)
1230 efi_memory_desc_t *md;
1233 if (!efi_enabled(EFI_MEMMAP))
1236 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1238 if ((md->phys_addr <= phys_addr) &&
1239 (phys_addr < (md->phys_addr +
1240 (md->num_pages << EFI_PAGE_SHIFT))))
1246 u64 efi_mem_attributes(unsigned long phys_addr)
1248 efi_memory_desc_t *md;
1251 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1253 if ((md->phys_addr <= phys_addr) &&
1254 (phys_addr < (md->phys_addr +
1255 (md->num_pages << EFI_PAGE_SHIFT))))
1256 return md->attribute;
1262 * Some firmware implementations refuse to boot if there's insufficient space
1263 * in the variable store. Ensure that we never use more than a safe limit.
1265 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1268 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1270 efi_status_t status;
1271 u64 storage_size, remaining_size, max_size;
1273 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1276 status = efi.query_variable_info(attributes, &storage_size,
1277 &remaining_size, &max_size);
1278 if (status != EFI_SUCCESS)
1282 * We account for that by refusing the write if permitting it would
1283 * reduce the available space to under 5KB. This figure was provided by
1284 * Samsung, so should be safe.
1286 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1287 !efi_no_storage_paranoia) {
1290 * Triggering garbage collection may require that the firmware
1291 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1292 * that by attempting to use more space than is available.
1294 unsigned long dummy_size = remaining_size + 1024;
1295 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1298 return EFI_OUT_OF_RESOURCES;
1300 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1301 EFI_VARIABLE_NON_VOLATILE |
1302 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1303 EFI_VARIABLE_RUNTIME_ACCESS,
1306 if (status == EFI_SUCCESS) {
1308 * This should have failed, so if it didn't make sure
1309 * that we delete it...
1311 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1312 EFI_VARIABLE_NON_VOLATILE |
1313 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1314 EFI_VARIABLE_RUNTIME_ACCESS,
1321 * The runtime code may now have triggered a garbage collection
1322 * run, so check the variable info again
1324 status = efi.query_variable_info(attributes, &storage_size,
1325 &remaining_size, &max_size);
1327 if (status != EFI_SUCCESS)
1331 * There still isn't enough room, so return an error
1333 if (remaining_size - size < EFI_MIN_RESERVE)
1334 return EFI_OUT_OF_RESOURCES;
1339 EXPORT_SYMBOL_GPL(efi_query_variable_store);
1341 static int __init parse_efi_cmdline(char *str)
1346 if (!strncmp(str, "old_map", 7))
1347 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1351 early_param("efi", parse_efi_cmdline);
1353 void __init efi_apply_memmap_quirks(void)
1356 * Once setup is done earlier, unmap the EFI memory map on mismatched
1357 * firmware/kernel architectures since there is no support for runtime
1360 if (!efi_runtime_supported()) {
1361 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1366 * UV doesn't support the new EFI pagetable mapping yet.
1369 set_bit(EFI_OLD_MEMMAP, &efi.flags);