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>
45 #include <asm/setup.h>
48 #include <asm/cacheflush.h>
49 #include <asm/tlbflush.h>
50 #include <asm/x86_init.h>
54 struct efi __read_mostly efi = {
55 .mps = EFI_INVALID_TABLE_ADDR,
56 .acpi = EFI_INVALID_TABLE_ADDR,
57 .acpi20 = EFI_INVALID_TABLE_ADDR,
58 .smbios = EFI_INVALID_TABLE_ADDR,
59 .sal_systab = EFI_INVALID_TABLE_ADDR,
60 .boot_info = EFI_INVALID_TABLE_ADDR,
61 .hcdp = EFI_INVALID_TABLE_ADDR,
62 .uga = EFI_INVALID_TABLE_ADDR,
63 .uv_systab = EFI_INVALID_TABLE_ADDR,
67 struct efi_memory_map memmap;
69 static struct efi efi_phys __initdata;
70 static efi_system_table_t efi_systab __initdata;
72 unsigned long x86_efi_facility;
75 * Returns 1 if 'facility' is enabled, 0 otherwise.
77 int efi_enabled(int facility)
79 return test_bit(facility, &x86_efi_facility) != 0;
81 EXPORT_SYMBOL(efi_enabled);
83 static bool __initdata disable_runtime = false;
84 static int __init setup_noefi(char *arg)
86 disable_runtime = true;
89 early_param("noefi", setup_noefi);
92 EXPORT_SYMBOL(add_efi_memmap);
94 static int __init setup_add_efi_memmap(char *arg)
99 early_param("add_efi_memmap", setup_add_efi_memmap);
102 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
107 spin_lock_irqsave(&rtc_lock, flags);
108 status = efi_call_virt2(get_time, tm, tc);
109 spin_unlock_irqrestore(&rtc_lock, flags);
113 static efi_status_t virt_efi_set_time(efi_time_t *tm)
118 spin_lock_irqsave(&rtc_lock, flags);
119 status = efi_call_virt1(set_time, tm);
120 spin_unlock_irqrestore(&rtc_lock, flags);
124 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
131 spin_lock_irqsave(&rtc_lock, flags);
132 status = efi_call_virt3(get_wakeup_time,
133 enabled, pending, tm);
134 spin_unlock_irqrestore(&rtc_lock, flags);
138 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
143 spin_lock_irqsave(&rtc_lock, flags);
144 status = efi_call_virt2(set_wakeup_time,
146 spin_unlock_irqrestore(&rtc_lock, flags);
150 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
153 unsigned long *data_size,
156 return efi_call_virt5(get_variable,
161 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
165 return efi_call_virt3(get_next_variable,
166 name_size, name, vendor);
169 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
172 unsigned long data_size,
175 return efi_call_virt5(set_variable,
180 static efi_status_t virt_efi_query_variable_info(u32 attr,
182 u64 *remaining_space,
183 u64 *max_variable_size)
185 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
186 return EFI_UNSUPPORTED;
188 return efi_call_virt4(query_variable_info, attr, storage_space,
189 remaining_space, max_variable_size);
192 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
194 return efi_call_virt1(get_next_high_mono_count, count);
197 static void virt_efi_reset_system(int reset_type,
199 unsigned long data_size,
202 efi_call_virt4(reset_system, reset_type, status,
206 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
208 unsigned long sg_list)
210 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
211 return EFI_UNSUPPORTED;
213 return efi_call_virt3(update_capsule, capsules, count, sg_list);
216 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
221 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
222 return EFI_UNSUPPORTED;
224 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
228 static efi_status_t __init phys_efi_set_virtual_address_map(
229 unsigned long memory_map_size,
230 unsigned long descriptor_size,
231 u32 descriptor_version,
232 efi_memory_desc_t *virtual_map)
236 efi_call_phys_prelog();
237 status = efi_call_phys4(efi_phys.set_virtual_address_map,
238 memory_map_size, descriptor_size,
239 descriptor_version, virtual_map);
240 efi_call_phys_epilog();
244 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
250 spin_lock_irqsave(&rtc_lock, flags);
251 efi_call_phys_prelog();
252 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
254 efi_call_phys_epilog();
255 spin_unlock_irqrestore(&rtc_lock, flags);
259 int efi_set_rtc_mmss(unsigned long nowtime)
261 int real_seconds, real_minutes;
266 status = efi.get_time(&eft, &cap);
267 if (status != EFI_SUCCESS) {
268 pr_err("Oops: efitime: can't read time!\n");
272 real_seconds = nowtime % 60;
273 real_minutes = nowtime / 60;
274 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
277 eft.minute = real_minutes;
278 eft.second = real_seconds;
280 status = efi.set_time(&eft);
281 if (status != EFI_SUCCESS) {
282 pr_err("Oops: efitime: can't write time!\n");
288 unsigned long efi_get_time(void)
294 status = efi.get_time(&eft, &cap);
295 if (status != EFI_SUCCESS)
296 pr_err("Oops: efitime: can't read time!\n");
298 return mktime(eft.year, eft.month, eft.day, eft.hour,
299 eft.minute, eft.second);
303 * Tell the kernel about the EFI memory map. This might include
304 * more than the max 128 entries that can fit in the e820 legacy
305 * (zeropage) memory map.
308 static void __init do_add_efi_memmap(void)
312 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
313 efi_memory_desc_t *md = p;
314 unsigned long long start = md->phys_addr;
315 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
319 case EFI_LOADER_CODE:
320 case EFI_LOADER_DATA:
321 case EFI_BOOT_SERVICES_CODE:
322 case EFI_BOOT_SERVICES_DATA:
323 case EFI_CONVENTIONAL_MEMORY:
324 if (md->attribute & EFI_MEMORY_WB)
325 e820_type = E820_RAM;
327 e820_type = E820_RESERVED;
329 case EFI_ACPI_RECLAIM_MEMORY:
330 e820_type = E820_ACPI;
332 case EFI_ACPI_MEMORY_NVS:
333 e820_type = E820_NVS;
335 case EFI_UNUSABLE_MEMORY:
336 e820_type = E820_UNUSABLE;
340 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
341 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
342 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
344 e820_type = E820_RESERVED;
347 e820_add_region(start, size, e820_type);
349 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
352 int __init efi_memblock_x86_reserve_range(void)
354 struct efi_info *e = &boot_params.efi_info;
358 /* Can't handle data above 4GB at this time */
359 if (e->efi_memmap_hi) {
360 pr_err("Memory map is above 4GB, disabling EFI.\n");
363 pmap = e->efi_memmap;
365 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
367 memmap.phys_map = (void *)pmap;
368 memmap.nr_map = e->efi_memmap_size /
370 memmap.desc_size = e->efi_memdesc_size;
371 memmap.desc_version = e->efi_memdesc_version;
373 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
379 static void __init print_efi_memmap(void)
381 efi_memory_desc_t *md;
385 for (p = memmap.map, i = 0;
387 p += memmap.desc_size, i++) {
389 pr_info("mem%02u: type=%u, attr=0x%llx, "
390 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
391 i, md->type, md->attribute, md->phys_addr,
392 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
393 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
396 #endif /* EFI_DEBUG */
398 void __init efi_reserve_boot_services(void)
402 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
403 efi_memory_desc_t *md = p;
404 u64 start = md->phys_addr;
405 u64 size = md->num_pages << EFI_PAGE_SHIFT;
407 if (md->type != EFI_BOOT_SERVICES_CODE &&
408 md->type != EFI_BOOT_SERVICES_DATA)
410 /* Only reserve where possible:
411 * - Not within any already allocated areas
412 * - Not over any memory area (really needed, if above?)
413 * - Not within any part of the kernel
414 * - Not the bios reserved area
416 if ((start+size >= __pa_symbol(_text)
417 && start <= __pa_symbol(_end)) ||
418 !e820_all_mapped(start, start+size, E820_RAM) ||
419 memblock_is_region_reserved(start, size)) {
420 /* Could not reserve, skip it */
422 memblock_dbg("Could not reserve boot range "
423 "[0x%010llx-0x%010llx]\n",
424 start, start+size-1);
426 memblock_reserve(start, size);
430 void __init efi_unmap_memmap(void)
432 clear_bit(EFI_MEMMAP, &x86_efi_facility);
434 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
439 void __init efi_free_boot_services(void)
443 if (!efi_is_native())
446 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
447 efi_memory_desc_t *md = p;
448 unsigned long long start = md->phys_addr;
449 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
451 if (md->type != EFI_BOOT_SERVICES_CODE &&
452 md->type != EFI_BOOT_SERVICES_DATA)
455 /* Could not reserve boot area */
459 free_bootmem_late(start, size);
465 static int __init efi_systab_init(void *phys)
467 if (efi_enabled(EFI_64BIT)) {
468 efi_system_table_64_t *systab64;
471 systab64 = early_ioremap((unsigned long)phys,
473 if (systab64 == NULL) {
474 pr_err("Couldn't map the system table!\n");
478 efi_systab.hdr = systab64->hdr;
479 efi_systab.fw_vendor = systab64->fw_vendor;
480 tmp |= systab64->fw_vendor;
481 efi_systab.fw_revision = systab64->fw_revision;
482 efi_systab.con_in_handle = systab64->con_in_handle;
483 tmp |= systab64->con_in_handle;
484 efi_systab.con_in = systab64->con_in;
485 tmp |= systab64->con_in;
486 efi_systab.con_out_handle = systab64->con_out_handle;
487 tmp |= systab64->con_out_handle;
488 efi_systab.con_out = systab64->con_out;
489 tmp |= systab64->con_out;
490 efi_systab.stderr_handle = systab64->stderr_handle;
491 tmp |= systab64->stderr_handle;
492 efi_systab.stderr = systab64->stderr;
493 tmp |= systab64->stderr;
494 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
495 tmp |= systab64->runtime;
496 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
497 tmp |= systab64->boottime;
498 efi_systab.nr_tables = systab64->nr_tables;
499 efi_systab.tables = systab64->tables;
500 tmp |= systab64->tables;
502 early_iounmap(systab64, sizeof(*systab64));
505 pr_err("EFI data located above 4GB, disabling EFI.\n");
510 efi_system_table_32_t *systab32;
512 systab32 = early_ioremap((unsigned long)phys,
514 if (systab32 == NULL) {
515 pr_err("Couldn't map the system table!\n");
519 efi_systab.hdr = systab32->hdr;
520 efi_systab.fw_vendor = systab32->fw_vendor;
521 efi_systab.fw_revision = systab32->fw_revision;
522 efi_systab.con_in_handle = systab32->con_in_handle;
523 efi_systab.con_in = systab32->con_in;
524 efi_systab.con_out_handle = systab32->con_out_handle;
525 efi_systab.con_out = systab32->con_out;
526 efi_systab.stderr_handle = systab32->stderr_handle;
527 efi_systab.stderr = systab32->stderr;
528 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
529 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
530 efi_systab.nr_tables = systab32->nr_tables;
531 efi_systab.tables = systab32->tables;
533 early_iounmap(systab32, sizeof(*systab32));
536 efi.systab = &efi_systab;
539 * Verify the EFI Table
541 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
542 pr_err("System table signature incorrect!\n");
545 if ((efi.systab->hdr.revision >> 16) == 0)
546 pr_err("Warning: System table version "
547 "%d.%02d, expected 1.00 or greater!\n",
548 efi.systab->hdr.revision >> 16,
549 efi.systab->hdr.revision & 0xffff);
554 static int __init efi_config_init(u64 tables, int nr_tables)
556 void *config_tables, *tablep;
559 if (efi_enabled(EFI_64BIT))
560 sz = sizeof(efi_config_table_64_t);
562 sz = sizeof(efi_config_table_32_t);
565 * Let's see what config tables the firmware passed to us.
567 config_tables = early_ioremap(tables, nr_tables * sz);
568 if (config_tables == NULL) {
569 pr_err("Could not map Configuration table!\n");
573 tablep = config_tables;
575 for (i = 0; i < efi.systab->nr_tables; i++) {
579 if (efi_enabled(EFI_64BIT)) {
581 guid = ((efi_config_table_64_t *)tablep)->guid;
582 table64 = ((efi_config_table_64_t *)tablep)->table;
587 pr_err("Table located above 4GB, disabling EFI.\n");
588 early_iounmap(config_tables,
589 efi.systab->nr_tables * sz);
594 guid = ((efi_config_table_32_t *)tablep)->guid;
595 table = ((efi_config_table_32_t *)tablep)->table;
597 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
599 pr_cont(" MPS=0x%lx ", table);
600 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
602 pr_cont(" ACPI 2.0=0x%lx ", table);
603 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
605 pr_cont(" ACPI=0x%lx ", table);
606 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
608 pr_cont(" SMBIOS=0x%lx ", table);
610 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
611 efi.uv_systab = table;
612 pr_cont(" UVsystab=0x%lx ", table);
614 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
616 pr_cont(" HCDP=0x%lx ", table);
617 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
619 pr_cont(" UGA=0x%lx ", table);
624 early_iounmap(config_tables, efi.systab->nr_tables * sz);
628 static int __init efi_runtime_init(void)
630 efi_runtime_services_t *runtime;
633 * Check out the runtime services table. We need to map
634 * the runtime services table so that we can grab the physical
635 * address of several of the EFI runtime functions, needed to
636 * set the firmware into virtual mode.
638 runtime = early_ioremap((unsigned long)efi.systab->runtime,
639 sizeof(efi_runtime_services_t));
641 pr_err("Could not map the runtime service table!\n");
645 * We will only need *early* access to the following
646 * two EFI runtime services before set_virtual_address_map
649 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
650 efi_phys.set_virtual_address_map =
651 (efi_set_virtual_address_map_t *)
652 runtime->set_virtual_address_map;
654 * Make efi_get_time can be called before entering
657 efi.get_time = phys_efi_get_time;
658 early_iounmap(runtime, sizeof(efi_runtime_services_t));
663 static int __init efi_memmap_init(void)
665 /* Map the EFI memory map */
666 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
667 memmap.nr_map * memmap.desc_size);
668 if (memmap.map == NULL) {
669 pr_err("Could not map the memory map!\n");
672 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
680 void __init efi_init(void)
683 char vendor[100] = "unknown";
688 if (boot_params.efi_info.efi_systab_hi ||
689 boot_params.efi_info.efi_memmap_hi) {
690 pr_info("Table located above 4GB, disabling EFI.\n");
693 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
695 efi_phys.systab = (efi_system_table_t *)
696 (boot_params.efi_info.efi_systab |
697 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
700 if (efi_systab_init(efi_phys.systab))
703 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
706 * Show what we know for posterity
708 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
710 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
714 pr_err("Could not map the firmware vendor!\n");
715 early_iounmap(tmp, 2);
717 pr_info("EFI v%u.%.02u by %s\n",
718 efi.systab->hdr.revision >> 16,
719 efi.systab->hdr.revision & 0xffff, vendor);
721 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
724 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
727 * Note: We currently don't support runtime services on an EFI
728 * that doesn't match the kernel 32/64-bit mode.
731 if (!efi_is_native())
732 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
734 if (disable_runtime || efi_runtime_init())
736 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
739 if (efi_memmap_init())
742 set_bit(EFI_MEMMAP, &x86_efi_facility);
745 if (efi_is_native()) {
746 x86_platform.get_wallclock = efi_get_time;
747 x86_platform.set_wallclock = efi_set_rtc_mmss;
756 void __init efi_late_init(void)
761 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
765 addr = md->virt_addr;
766 npages = md->num_pages;
768 memrange_efi_to_native(&addr, &npages);
771 set_memory_x(addr, npages);
773 set_memory_nx(addr, npages);
776 static void __init runtime_code_page_mkexec(void)
778 efi_memory_desc_t *md;
781 /* Make EFI runtime service code area executable */
782 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
785 if (md->type != EFI_RUNTIME_SERVICES_CODE)
788 efi_set_executable(md, true);
793 * We can't ioremap data in EFI boot services RAM, because we've already mapped
794 * it as RAM. So, look it up in the existing EFI memory map instead. Only
795 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
797 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
800 if (WARN_ON(!memmap.map))
802 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
803 efi_memory_desc_t *md = p;
804 u64 size = md->num_pages << EFI_PAGE_SHIFT;
805 u64 end = md->phys_addr + size;
806 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
807 md->type != EFI_BOOT_SERVICES_CODE &&
808 md->type != EFI_BOOT_SERVICES_DATA)
812 if (phys_addr >= md->phys_addr && phys_addr < end) {
813 phys_addr += md->virt_addr - md->phys_addr;
814 return (__force void __iomem *)(unsigned long)phys_addr;
820 void efi_memory_uc(u64 addr, unsigned long size)
822 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
825 npages = round_up(size, page_shift) / page_shift;
826 memrange_efi_to_native(&addr, &npages);
827 set_memory_uc(addr, npages);
831 * This function will switch the EFI runtime services to virtual mode.
832 * Essentially, look through the EFI memmap and map every region that
833 * has the runtime attribute bit set in its memory descriptor and update
834 * that memory descriptor with the virtual address obtained from ioremap().
835 * This enables the runtime services to be called without having to
836 * thunk back into physical mode for every invocation.
838 void __init efi_enter_virtual_mode(void)
840 efi_memory_desc_t *md, *prev_md = NULL;
843 u64 end, systab, start_pfn, end_pfn;
844 void *p, *va, *new_memmap = NULL;
850 * We don't do virtual mode, since we don't do runtime services, on
854 if (!efi_is_native()) {
859 /* Merge contiguous regions of the same type and attribute */
860 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
869 if (prev_md->type != md->type ||
870 prev_md->attribute != md->attribute) {
875 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
877 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
878 prev_md->num_pages += md->num_pages;
879 md->type = EFI_RESERVED_TYPE;
886 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
888 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
889 md->type != EFI_BOOT_SERVICES_CODE &&
890 md->type != EFI_BOOT_SERVICES_DATA)
893 size = md->num_pages << EFI_PAGE_SHIFT;
894 end = md->phys_addr + size;
896 start_pfn = PFN_DOWN(md->phys_addr);
897 end_pfn = PFN_UP(end);
898 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
899 va = __va(md->phys_addr);
901 if (!(md->attribute & EFI_MEMORY_WB))
902 efi_memory_uc((u64)(unsigned long)va, size);
904 va = efi_ioremap(md->phys_addr, size,
905 md->type, md->attribute);
907 md->virt_addr = (u64) (unsigned long) va;
910 pr_err("ioremap of 0x%llX failed!\n",
911 (unsigned long long)md->phys_addr);
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;
920 new_memmap = krealloc(new_memmap,
921 (count + 1) * memmap.desc_size,
923 memcpy(new_memmap + (count * memmap.desc_size), md,
930 status = phys_efi_set_virtual_address_map(
931 memmap.desc_size * count,
934 (efi_memory_desc_t *)__pa(new_memmap));
936 if (status != EFI_SUCCESS) {
937 pr_alert("Unable to switch EFI into virtual mode "
938 "(status=%lx)!\n", status);
939 panic("EFI call to SetVirtualAddressMap() failed!");
943 * Now that EFI is in virtual mode, update the function
944 * pointers in the runtime service table to the new virtual addresses.
946 * Call EFI services through wrapper functions.
948 efi.runtime_version = efi_systab.hdr.revision;
949 efi.get_time = virt_efi_get_time;
950 efi.set_time = virt_efi_set_time;
951 efi.get_wakeup_time = virt_efi_get_wakeup_time;
952 efi.set_wakeup_time = virt_efi_set_wakeup_time;
953 efi.get_variable = virt_efi_get_variable;
954 efi.get_next_variable = virt_efi_get_next_variable;
955 efi.set_variable = virt_efi_set_variable;
956 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
957 efi.reset_system = virt_efi_reset_system;
958 efi.set_virtual_address_map = NULL;
959 efi.query_variable_info = virt_efi_query_variable_info;
960 efi.update_capsule = virt_efi_update_capsule;
961 efi.query_capsule_caps = virt_efi_query_capsule_caps;
962 if (__supported_pte_mask & _PAGE_NX)
963 runtime_code_page_mkexec();
969 * Convenience functions to obtain memory types and attributes
971 u32 efi_mem_type(unsigned long phys_addr)
973 efi_memory_desc_t *md;
976 if (!efi_enabled(EFI_MEMMAP))
979 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
981 if ((md->phys_addr <= phys_addr) &&
982 (phys_addr < (md->phys_addr +
983 (md->num_pages << EFI_PAGE_SHIFT))))
989 u64 efi_mem_attributes(unsigned long phys_addr)
991 efi_memory_desc_t *md;
994 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
996 if ((md->phys_addr <= phys_addr) &&
997 (phys_addr < (md->phys_addr +
998 (md->num_pages << EFI_PAGE_SHIFT))))
999 return md->attribute;