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>
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
19 * All EFI Runtime Services are not implemented yet as EFI only
20 * supports physical mode addressing on SoftSDV. This is to be fixed
21 * in a future version. --drummond 1999-07-20
23 * Implemented EFI runtime services and virtual mode calls. --davidm
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/efi.h>
34 #include <linux/efi-bgrt.h>
35 #include <linux/export.h>
36 #include <linux/bootmem.h>
37 #include <linux/memblock.h>
38 #include <linux/spinlock.h>
39 #include <linux/uaccess.h>
40 #include <linux/time.h>
42 #include <linux/reboot.h>
43 #include <linux/bcd.h>
44 #include <linux/ucs2_string.h>
46 #include <asm/setup.h>
49 #include <asm/cacheflush.h>
50 #include <asm/tlbflush.h>
51 #include <asm/x86_init.h>
57 * There's some additional metadata associated with each
58 * variable. Intel's reference implementation is 60 bytes - bump that
59 * to account for potential alignment constraints
61 #define VAR_METADATA_SIZE 64
63 struct efi __read_mostly efi = {
64 .mps = EFI_INVALID_TABLE_ADDR,
65 .acpi = EFI_INVALID_TABLE_ADDR,
66 .acpi20 = EFI_INVALID_TABLE_ADDR,
67 .smbios = EFI_INVALID_TABLE_ADDR,
68 .sal_systab = EFI_INVALID_TABLE_ADDR,
69 .boot_info = EFI_INVALID_TABLE_ADDR,
70 .hcdp = EFI_INVALID_TABLE_ADDR,
71 .uga = EFI_INVALID_TABLE_ADDR,
72 .uv_systab = EFI_INVALID_TABLE_ADDR,
76 struct efi_memory_map memmap;
78 static struct efi efi_phys __initdata;
79 static efi_system_table_t efi_systab __initdata;
81 static u64 efi_var_store_size;
82 static u64 efi_var_remaining_size;
83 static u64 efi_var_max_var_size;
84 static u64 boot_used_size;
85 static u64 boot_var_size;
86 static u64 active_size;
88 unsigned long x86_efi_facility;
91 * Returns 1 if 'facility' is enabled, 0 otherwise.
93 int efi_enabled(int facility)
95 return test_bit(facility, &x86_efi_facility) != 0;
97 EXPORT_SYMBOL(efi_enabled);
99 static bool __initdata disable_runtime = false;
100 static int __init setup_noefi(char *arg)
102 disable_runtime = true;
105 early_param("noefi", setup_noefi);
108 EXPORT_SYMBOL(add_efi_memmap);
110 static int __init setup_add_efi_memmap(char *arg)
115 early_param("add_efi_memmap", setup_add_efi_memmap);
117 static bool efi_no_storage_paranoia;
119 static int __init setup_storage_paranoia(char *arg)
121 efi_no_storage_paranoia = true;
124 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
127 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
132 spin_lock_irqsave(&rtc_lock, flags);
133 status = efi_call_virt2(get_time, tm, tc);
134 spin_unlock_irqrestore(&rtc_lock, flags);
138 static efi_status_t virt_efi_set_time(efi_time_t *tm)
143 spin_lock_irqsave(&rtc_lock, flags);
144 status = efi_call_virt1(set_time, tm);
145 spin_unlock_irqrestore(&rtc_lock, flags);
149 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
156 spin_lock_irqsave(&rtc_lock, flags);
157 status = efi_call_virt3(get_wakeup_time,
158 enabled, pending, tm);
159 spin_unlock_irqrestore(&rtc_lock, flags);
163 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
168 spin_lock_irqsave(&rtc_lock, flags);
169 status = efi_call_virt2(set_wakeup_time,
171 spin_unlock_irqrestore(&rtc_lock, flags);
175 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
178 unsigned long *data_size,
181 return efi_call_virt5(get_variable,
186 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
191 static bool finished = false;
194 status = efi_call_virt3(get_next_variable,
195 name_size, name, vendor);
197 if (status == EFI_NOT_FOUND) {
199 if (var_size < boot_used_size) {
200 boot_var_size = boot_used_size - var_size;
201 active_size += boot_var_size;
203 printk(KERN_WARNING FW_BUG "efi: Inconsistent initial sizes\n");
207 if (boot_used_size && !finished) {
213 s = virt_efi_get_variable(name, vendor, &attr, &size, NULL);
215 if (s != EFI_BUFFER_TOO_SMALL || !size)
218 tmp = kmalloc(size, GFP_ATOMIC);
223 s = virt_efi_get_variable(name, vendor, &attr, &size, tmp);
225 if (s == EFI_SUCCESS && (attr & EFI_VARIABLE_NON_VOLATILE)) {
227 var_size += ucs2_strsize(name, 1024);
229 active_size += VAR_METADATA_SIZE;
230 active_size += ucs2_strsize(name, 1024);
239 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
242 unsigned long data_size,
247 unsigned long orig_size = 0;
249 status = virt_efi_get_variable(name, vendor, &orig_attr, &orig_size,
252 if (status != EFI_BUFFER_TOO_SMALL)
255 status = efi_call_virt5(set_variable,
259 if (status == EFI_SUCCESS) {
261 active_size -= orig_size;
262 active_size -= ucs2_strsize(name, 1024);
263 active_size -= VAR_METADATA_SIZE;
266 active_size += data_size;
267 active_size += ucs2_strsize(name, 1024);
268 active_size += VAR_METADATA_SIZE;
275 static efi_status_t virt_efi_query_variable_info(u32 attr,
277 u64 *remaining_space,
278 u64 *max_variable_size)
280 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
281 return EFI_UNSUPPORTED;
283 return efi_call_virt4(query_variable_info, attr, storage_space,
284 remaining_space, max_variable_size);
287 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
289 return efi_call_virt1(get_next_high_mono_count, count);
292 static void virt_efi_reset_system(int reset_type,
294 unsigned long data_size,
297 efi_call_virt4(reset_system, reset_type, status,
301 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
303 unsigned long sg_list)
305 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
306 return EFI_UNSUPPORTED;
308 return efi_call_virt3(update_capsule, capsules, count, sg_list);
311 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
316 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
317 return EFI_UNSUPPORTED;
319 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
323 static efi_status_t __init phys_efi_set_virtual_address_map(
324 unsigned long memory_map_size,
325 unsigned long descriptor_size,
326 u32 descriptor_version,
327 efi_memory_desc_t *virtual_map)
331 efi_call_phys_prelog();
332 status = efi_call_phys4(efi_phys.set_virtual_address_map,
333 memory_map_size, descriptor_size,
334 descriptor_version, virtual_map);
335 efi_call_phys_epilog();
339 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
345 spin_lock_irqsave(&rtc_lock, flags);
346 efi_call_phys_prelog();
347 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
349 efi_call_phys_epilog();
350 spin_unlock_irqrestore(&rtc_lock, flags);
354 int efi_set_rtc_mmss(unsigned long nowtime)
361 status = efi.get_time(&eft, &cap);
362 if (status != EFI_SUCCESS) {
363 pr_err("Oops: efitime: can't read time!\n");
367 rtc_time_to_tm(nowtime, &tm);
368 if (!rtc_valid_tm(&tm)) {
369 eft.year = tm.tm_year + 1900;
370 eft.month = tm.tm_mon + 1;
371 eft.day = tm.tm_mday;
372 eft.minute = tm.tm_min;
373 eft.second = tm.tm_sec;
377 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
378 __FUNCTION__, nowtime);
382 status = efi.set_time(&eft);
383 if (status != EFI_SUCCESS) {
384 pr_err("Oops: efitime: can't write time!\n");
390 unsigned long efi_get_time(void)
396 status = efi.get_time(&eft, &cap);
397 if (status != EFI_SUCCESS)
398 pr_err("Oops: efitime: can't read time!\n");
400 return mktime(eft.year, eft.month, eft.day, eft.hour,
401 eft.minute, eft.second);
405 * Tell the kernel about the EFI memory map. This might include
406 * more than the max 128 entries that can fit in the e820 legacy
407 * (zeropage) memory map.
410 static void __init do_add_efi_memmap(void)
414 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
415 efi_memory_desc_t *md = p;
416 unsigned long long start = md->phys_addr;
417 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
421 case EFI_LOADER_CODE:
422 case EFI_LOADER_DATA:
423 case EFI_BOOT_SERVICES_CODE:
424 case EFI_BOOT_SERVICES_DATA:
425 case EFI_CONVENTIONAL_MEMORY:
426 if (md->attribute & EFI_MEMORY_WB)
427 e820_type = E820_RAM;
429 e820_type = E820_RESERVED;
431 case EFI_ACPI_RECLAIM_MEMORY:
432 e820_type = E820_ACPI;
434 case EFI_ACPI_MEMORY_NVS:
435 e820_type = E820_NVS;
437 case EFI_UNUSABLE_MEMORY:
438 e820_type = E820_UNUSABLE;
442 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
443 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
444 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
446 e820_type = E820_RESERVED;
449 e820_add_region(start, size, e820_type);
451 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
454 int __init efi_memblock_x86_reserve_range(void)
459 /* Can't handle data above 4GB at this time */
460 if (boot_params.efi_info.efi_memmap_hi) {
461 pr_err("Memory map is above 4GB, disabling EFI.\n");
464 pmap = boot_params.efi_info.efi_memmap;
466 pmap = (boot_params.efi_info.efi_memmap |
467 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
469 memmap.phys_map = (void *)pmap;
470 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
471 boot_params.efi_info.efi_memdesc_size;
472 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
473 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
474 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
480 static void __init print_efi_memmap(void)
482 efi_memory_desc_t *md;
486 for (p = memmap.map, i = 0;
488 p += memmap.desc_size, i++) {
490 pr_info("mem%02u: type=%u, attr=0x%llx, "
491 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
492 i, md->type, md->attribute, md->phys_addr,
493 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
494 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
497 #endif /* EFI_DEBUG */
499 void __init efi_reserve_boot_services(void)
503 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
504 efi_memory_desc_t *md = p;
505 u64 start = md->phys_addr;
506 u64 size = md->num_pages << EFI_PAGE_SHIFT;
508 if (md->type != EFI_BOOT_SERVICES_CODE &&
509 md->type != EFI_BOOT_SERVICES_DATA)
511 /* Only reserve where possible:
512 * - Not within any already allocated areas
513 * - Not over any memory area (really needed, if above?)
514 * - Not within any part of the kernel
515 * - Not the bios reserved area
517 if ((start+size >= __pa_symbol(_text)
518 && start <= __pa_symbol(_end)) ||
519 !e820_all_mapped(start, start+size, E820_RAM) ||
520 memblock_is_region_reserved(start, size)) {
521 /* Could not reserve, skip it */
523 memblock_dbg("Could not reserve boot range "
524 "[0x%010llx-0x%010llx]\n",
525 start, start+size-1);
527 memblock_reserve(start, size);
531 void __init efi_unmap_memmap(void)
533 clear_bit(EFI_MEMMAP, &x86_efi_facility);
535 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
540 void __init efi_free_boot_services(void)
544 if (!efi_is_native())
547 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
548 efi_memory_desc_t *md = p;
549 unsigned long long start = md->phys_addr;
550 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
552 if (md->type != EFI_BOOT_SERVICES_CODE &&
553 md->type != EFI_BOOT_SERVICES_DATA)
556 /* Could not reserve boot area */
560 free_bootmem_late(start, size);
566 static int __init efi_systab_init(void *phys)
568 if (efi_enabled(EFI_64BIT)) {
569 efi_system_table_64_t *systab64;
572 systab64 = early_ioremap((unsigned long)phys,
574 if (systab64 == NULL) {
575 pr_err("Couldn't map the system table!\n");
579 efi_systab.hdr = systab64->hdr;
580 efi_systab.fw_vendor = systab64->fw_vendor;
581 tmp |= systab64->fw_vendor;
582 efi_systab.fw_revision = systab64->fw_revision;
583 efi_systab.con_in_handle = systab64->con_in_handle;
584 tmp |= systab64->con_in_handle;
585 efi_systab.con_in = systab64->con_in;
586 tmp |= systab64->con_in;
587 efi_systab.con_out_handle = systab64->con_out_handle;
588 tmp |= systab64->con_out_handle;
589 efi_systab.con_out = systab64->con_out;
590 tmp |= systab64->con_out;
591 efi_systab.stderr_handle = systab64->stderr_handle;
592 tmp |= systab64->stderr_handle;
593 efi_systab.stderr = systab64->stderr;
594 tmp |= systab64->stderr;
595 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
596 tmp |= systab64->runtime;
597 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
598 tmp |= systab64->boottime;
599 efi_systab.nr_tables = systab64->nr_tables;
600 efi_systab.tables = systab64->tables;
601 tmp |= systab64->tables;
603 early_iounmap(systab64, sizeof(*systab64));
606 pr_err("EFI data located above 4GB, disabling EFI.\n");
611 efi_system_table_32_t *systab32;
613 systab32 = early_ioremap((unsigned long)phys,
615 if (systab32 == NULL) {
616 pr_err("Couldn't map the system table!\n");
620 efi_systab.hdr = systab32->hdr;
621 efi_systab.fw_vendor = systab32->fw_vendor;
622 efi_systab.fw_revision = systab32->fw_revision;
623 efi_systab.con_in_handle = systab32->con_in_handle;
624 efi_systab.con_in = systab32->con_in;
625 efi_systab.con_out_handle = systab32->con_out_handle;
626 efi_systab.con_out = systab32->con_out;
627 efi_systab.stderr_handle = systab32->stderr_handle;
628 efi_systab.stderr = systab32->stderr;
629 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
630 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
631 efi_systab.nr_tables = systab32->nr_tables;
632 efi_systab.tables = systab32->tables;
634 early_iounmap(systab32, sizeof(*systab32));
637 efi.systab = &efi_systab;
640 * Verify the EFI Table
642 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
643 pr_err("System table signature incorrect!\n");
646 if ((efi.systab->hdr.revision >> 16) == 0)
647 pr_err("Warning: System table version "
648 "%d.%02d, expected 1.00 or greater!\n",
649 efi.systab->hdr.revision >> 16,
650 efi.systab->hdr.revision & 0xffff);
655 static int __init efi_config_init(u64 tables, int nr_tables)
657 void *config_tables, *tablep;
660 if (efi_enabled(EFI_64BIT))
661 sz = sizeof(efi_config_table_64_t);
663 sz = sizeof(efi_config_table_32_t);
666 * Let's see what config tables the firmware passed to us.
668 config_tables = early_ioremap(tables, nr_tables * sz);
669 if (config_tables == NULL) {
670 pr_err("Could not map Configuration table!\n");
674 tablep = config_tables;
676 for (i = 0; i < efi.systab->nr_tables; i++) {
680 if (efi_enabled(EFI_64BIT)) {
682 guid = ((efi_config_table_64_t *)tablep)->guid;
683 table64 = ((efi_config_table_64_t *)tablep)->table;
688 pr_err("Table located above 4GB, disabling EFI.\n");
689 early_iounmap(config_tables,
690 efi.systab->nr_tables * sz);
695 guid = ((efi_config_table_32_t *)tablep)->guid;
696 table = ((efi_config_table_32_t *)tablep)->table;
698 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
700 pr_cont(" MPS=0x%lx ", table);
701 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
703 pr_cont(" ACPI 2.0=0x%lx ", table);
704 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
706 pr_cont(" ACPI=0x%lx ", table);
707 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
709 pr_cont(" SMBIOS=0x%lx ", table);
711 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
712 efi.uv_systab = table;
713 pr_cont(" UVsystab=0x%lx ", table);
715 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
717 pr_cont(" HCDP=0x%lx ", table);
718 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
720 pr_cont(" UGA=0x%lx ", table);
725 early_iounmap(config_tables, efi.systab->nr_tables * sz);
729 static int __init efi_runtime_init(void)
731 efi_runtime_services_t *runtime;
734 * Check out the runtime services table. We need to map
735 * the runtime services table so that we can grab the physical
736 * address of several of the EFI runtime functions, needed to
737 * set the firmware into virtual mode.
739 runtime = early_ioremap((unsigned long)efi.systab->runtime,
740 sizeof(efi_runtime_services_t));
742 pr_err("Could not map the runtime service table!\n");
746 * We will only need *early* access to the following
747 * two EFI runtime services before set_virtual_address_map
750 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
751 efi_phys.set_virtual_address_map =
752 (efi_set_virtual_address_map_t *)
753 runtime->set_virtual_address_map;
755 * Make efi_get_time can be called before entering
758 efi.get_time = phys_efi_get_time;
759 early_iounmap(runtime, sizeof(efi_runtime_services_t));
764 static int __init efi_memmap_init(void)
766 /* Map the EFI memory map */
767 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
768 memmap.nr_map * memmap.desc_size);
769 if (memmap.map == NULL) {
770 pr_err("Could not map the memory map!\n");
773 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
781 void __init efi_init(void)
784 char vendor[100] = "unknown";
787 struct setup_data *data;
788 struct efi_var_bootdata *efi_var_data;
792 if (boot_params.efi_info.efi_systab_hi ||
793 boot_params.efi_info.efi_memmap_hi) {
794 pr_info("Table located above 4GB, disabling EFI.\n");
797 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
799 efi_phys.systab = (efi_system_table_t *)
800 (boot_params.efi_info.efi_systab |
801 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
804 if (efi_systab_init(efi_phys.systab))
807 pa_data = boot_params.hdr.setup_data;
809 data = early_ioremap(pa_data, sizeof(*efi_var_data));
810 if (data->type == SETUP_EFI_VARS) {
811 efi_var_data = (struct efi_var_bootdata *)data;
813 efi_var_store_size = efi_var_data->store_size;
814 efi_var_remaining_size = efi_var_data->remaining_size;
815 efi_var_max_var_size = efi_var_data->max_var_size;
817 pa_data = data->next;
818 early_iounmap(data, sizeof(*efi_var_data));
821 boot_used_size = efi_var_store_size - efi_var_remaining_size;
823 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
826 * Show what we know for posterity
828 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
830 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
834 pr_err("Could not map the firmware vendor!\n");
835 early_iounmap(tmp, 2);
837 pr_info("EFI v%u.%.02u by %s\n",
838 efi.systab->hdr.revision >> 16,
839 efi.systab->hdr.revision & 0xffff, vendor);
841 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
844 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
847 * Note: We currently don't support runtime services on an EFI
848 * that doesn't match the kernel 32/64-bit mode.
851 if (!efi_is_native())
852 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
854 if (disable_runtime || efi_runtime_init())
856 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
859 if (efi_memmap_init())
862 set_bit(EFI_MEMMAP, &x86_efi_facility);
865 if (efi_is_native()) {
866 x86_platform.get_wallclock = efi_get_time;
867 x86_platform.set_wallclock = efi_set_rtc_mmss;
876 void __init efi_late_init(void)
881 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
885 addr = md->virt_addr;
886 npages = md->num_pages;
888 memrange_efi_to_native(&addr, &npages);
891 set_memory_x(addr, npages);
893 set_memory_nx(addr, npages);
896 static void __init runtime_code_page_mkexec(void)
898 efi_memory_desc_t *md;
901 /* Make EFI runtime service code area executable */
902 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
905 if (md->type != EFI_RUNTIME_SERVICES_CODE)
908 efi_set_executable(md, true);
913 * We can't ioremap data in EFI boot services RAM, because we've already mapped
914 * it as RAM. So, look it up in the existing EFI memory map instead. Only
915 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
917 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
920 if (WARN_ON(!memmap.map))
922 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
923 efi_memory_desc_t *md = p;
924 u64 size = md->num_pages << EFI_PAGE_SHIFT;
925 u64 end = md->phys_addr + size;
926 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
927 md->type != EFI_BOOT_SERVICES_CODE &&
928 md->type != EFI_BOOT_SERVICES_DATA)
932 if (phys_addr >= md->phys_addr && phys_addr < end) {
933 phys_addr += md->virt_addr - md->phys_addr;
934 return (__force void __iomem *)(unsigned long)phys_addr;
940 void efi_memory_uc(u64 addr, unsigned long size)
942 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
945 npages = round_up(size, page_shift) / page_shift;
946 memrange_efi_to_native(&addr, &npages);
947 set_memory_uc(addr, npages);
951 * This function will switch the EFI runtime services to virtual mode.
952 * Essentially, look through the EFI memmap and map every region that
953 * has the runtime attribute bit set in its memory descriptor and update
954 * that memory descriptor with the virtual address obtained from ioremap().
955 * This enables the runtime services to be called without having to
956 * thunk back into physical mode for every invocation.
958 void __init efi_enter_virtual_mode(void)
960 efi_memory_desc_t *md, *prev_md = NULL;
963 u64 end, systab, start_pfn, end_pfn;
964 void *p, *va, *new_memmap = NULL;
970 * We don't do virtual mode, since we don't do runtime services, on
974 if (!efi_is_native()) {
979 /* Merge contiguous regions of the same type and attribute */
980 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
989 if (prev_md->type != md->type ||
990 prev_md->attribute != md->attribute) {
995 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
997 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
998 prev_md->num_pages += md->num_pages;
999 md->type = EFI_RESERVED_TYPE;
1006 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1008 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
1009 md->type != EFI_BOOT_SERVICES_CODE &&
1010 md->type != EFI_BOOT_SERVICES_DATA)
1013 size = md->num_pages << EFI_PAGE_SHIFT;
1014 end = md->phys_addr + size;
1016 start_pfn = PFN_DOWN(md->phys_addr);
1017 end_pfn = PFN_UP(end);
1018 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
1019 va = __va(md->phys_addr);
1021 if (!(md->attribute & EFI_MEMORY_WB))
1022 efi_memory_uc((u64)(unsigned long)va, size);
1024 va = efi_ioremap(md->phys_addr, size,
1025 md->type, md->attribute);
1027 md->virt_addr = (u64) (unsigned long) va;
1030 pr_err("ioremap of 0x%llX failed!\n",
1031 (unsigned long long)md->phys_addr);
1035 systab = (u64) (unsigned long) efi_phys.systab;
1036 if (md->phys_addr <= systab && systab < end) {
1037 systab += md->virt_addr - md->phys_addr;
1038 efi.systab = (efi_system_table_t *) (unsigned long) systab;
1040 new_memmap = krealloc(new_memmap,
1041 (count + 1) * memmap.desc_size,
1043 memcpy(new_memmap + (count * memmap.desc_size), md,
1048 BUG_ON(!efi.systab);
1050 status = phys_efi_set_virtual_address_map(
1051 memmap.desc_size * count,
1053 memmap.desc_version,
1054 (efi_memory_desc_t *)__pa(new_memmap));
1056 if (status != EFI_SUCCESS) {
1057 pr_alert("Unable to switch EFI into virtual mode "
1058 "(status=%lx)!\n", status);
1059 panic("EFI call to SetVirtualAddressMap() failed!");
1063 * Now that EFI is in virtual mode, update the function
1064 * pointers in the runtime service table to the new virtual addresses.
1066 * Call EFI services through wrapper functions.
1068 efi.runtime_version = efi_systab.hdr.revision;
1069 efi.get_time = virt_efi_get_time;
1070 efi.set_time = virt_efi_set_time;
1071 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1072 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1073 efi.get_variable = virt_efi_get_variable;
1074 efi.get_next_variable = virt_efi_get_next_variable;
1075 efi.set_variable = virt_efi_set_variable;
1076 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1077 efi.reset_system = virt_efi_reset_system;
1078 efi.set_virtual_address_map = NULL;
1079 efi.query_variable_info = virt_efi_query_variable_info;
1080 efi.update_capsule = virt_efi_update_capsule;
1081 efi.query_capsule_caps = virt_efi_query_capsule_caps;
1082 if (__supported_pte_mask & _PAGE_NX)
1083 runtime_code_page_mkexec();
1089 * Convenience functions to obtain memory types and attributes
1091 u32 efi_mem_type(unsigned long phys_addr)
1093 efi_memory_desc_t *md;
1096 if (!efi_enabled(EFI_MEMMAP))
1099 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1101 if ((md->phys_addr <= phys_addr) &&
1102 (phys_addr < (md->phys_addr +
1103 (md->num_pages << EFI_PAGE_SHIFT))))
1109 u64 efi_mem_attributes(unsigned long phys_addr)
1111 efi_memory_desc_t *md;
1114 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1116 if ((md->phys_addr <= phys_addr) &&
1117 (phys_addr < (md->phys_addr +
1118 (md->num_pages << EFI_PAGE_SHIFT))))
1119 return md->attribute;
1125 * Some firmware has serious problems when using more than 50% of the EFI
1126 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1127 * we never use more than this safe limit.
1129 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1132 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1134 efi_status_t status;
1135 u64 storage_size, remaining_size, max_size;
1137 status = efi.query_variable_info(attributes, &storage_size,
1138 &remaining_size, &max_size);
1139 if (status != EFI_SUCCESS)
1142 if (!max_size && remaining_size > size)
1143 printk_once(KERN_ERR FW_BUG "Broken EFI implementation"
1144 " is returning MaxVariableSize=0\n");
1146 * Some firmware implementations refuse to boot if there's insufficient
1147 * space in the variable store. We account for that by refusing the
1148 * write if permitting it would reduce the available space to under
1149 * 50%. However, some firmware won't reclaim variable space until
1150 * after the used (not merely the actively used) space drops below
1151 * a threshold. We can approximate that case with the value calculated
1152 * above. If both the firmware and our calculations indicate that the
1153 * available space would drop below 50%, refuse the write.
1156 if (!storage_size || size > remaining_size ||
1157 (max_size && size > max_size))
1158 return EFI_OUT_OF_RESOURCES;
1160 if (!efi_no_storage_paranoia &&
1161 ((active_size + size + VAR_METADATA_SIZE > storage_size / 2) &&
1162 (remaining_size - size < storage_size / 2)))
1163 return EFI_OUT_OF_RESOURCES;
1167 EXPORT_SYMBOL_GPL(efi_query_variable_store);