1 // SPDX-License-Identifier: GPL-2.0
3 * Common EFI (Extensible Firmware Interface) support functions
4 * Based on Extensible Firmware Interface Specification version 1.0
6 * Copyright (C) 1999 VA Linux Systems
7 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8 * Copyright (C) 1999-2002 Hewlett-Packard Co.
9 * David Mosberger-Tang <davidm@hpl.hp.com>
10 * Stephane Eranian <eranian@hpl.hp.com>
11 * Copyright (C) 2005-2008 Intel Co.
12 * Fenghua Yu <fenghua.yu@intel.com>
13 * Bibo Mao <bibo.mao@intel.com>
14 * Chandramouli Narayanan <mouli@linux.intel.com>
15 * Huang Ying <ying.huang@intel.com>
16 * Copyright (C) 2013 SuSE Labs
17 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
19 * Copied from efi_32.c to eliminate the duplicated code between EFI
20 * 32/64 support code. --ying 2007-10-26
22 * All EFI Runtime Services are not implemented yet as EFI only
23 * supports physical mode addressing on SoftSDV. This is to be fixed
24 * in a future version. --drummond 1999-07-20
26 * Implemented EFI runtime services and virtual mode calls. --davidm
28 * Goutham Rao: <goutham.rao@intel.com>
29 * Skip non-WB memory and ignore empty memory ranges.
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/efi.h>
37 #include <linux/efi-bgrt.h>
38 #include <linux/export.h>
39 #include <linux/memblock.h>
40 #include <linux/slab.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>
50 #include <asm/e820/api.h>
52 #include <asm/set_memory.h>
53 #include <asm/tlbflush.h>
54 #include <asm/x86_init.h>
55 #include <asm/uv/uv.h>
57 static efi_system_table_t efi_systab __initdata;
58 static u64 efi_systab_phys __initdata;
60 static unsigned long prop_phys = EFI_INVALID_TABLE_ADDR;
61 static unsigned long uga_phys = EFI_INVALID_TABLE_ADDR;
63 static efi_config_table_type_t arch_tables[] __initdata = {
64 {EFI_PROPERTIES_TABLE_GUID, "PROP", &prop_phys},
65 {UGA_IO_PROTOCOL_GUID, "UGA", &uga_phys},
67 {UV_SYSTEM_TABLE_GUID, "UVsystab", &uv_systab_phys},
69 {NULL_GUID, NULL, NULL},
72 static const unsigned long * const efi_tables[] = {
87 #ifdef CONFIG_EFI_RCI2_TABLE
92 u64 efi_setup; /* efi setup_data physical address */
94 static int add_efi_memmap __initdata;
95 static int __init setup_add_efi_memmap(char *arg)
100 early_param("add_efi_memmap", setup_add_efi_memmap);
102 void __init efi_find_mirror(void)
104 efi_memory_desc_t *md;
105 u64 mirror_size = 0, total_size = 0;
107 if (!efi_enabled(EFI_MEMMAP))
110 for_each_efi_memory_desc(md) {
111 unsigned long long start = md->phys_addr;
112 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
115 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
116 memblock_mark_mirror(start, size);
121 pr_info("Memory: %lldM/%lldM mirrored memory\n",
122 mirror_size>>20, total_size>>20);
126 * Tell the kernel about the EFI memory map. This might include
127 * more than the max 128 entries that can fit in the passed in e820
128 * legacy (zeropage) memory map, but the kernel's e820 table can hold
132 static void __init do_add_efi_memmap(void)
134 efi_memory_desc_t *md;
136 if (!efi_enabled(EFI_MEMMAP))
139 for_each_efi_memory_desc(md) {
140 unsigned long long start = md->phys_addr;
141 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
145 case EFI_LOADER_CODE:
146 case EFI_LOADER_DATA:
147 case EFI_BOOT_SERVICES_CODE:
148 case EFI_BOOT_SERVICES_DATA:
149 case EFI_CONVENTIONAL_MEMORY:
150 if (efi_soft_reserve_enabled()
151 && (md->attribute & EFI_MEMORY_SP))
152 e820_type = E820_TYPE_SOFT_RESERVED;
153 else if (md->attribute & EFI_MEMORY_WB)
154 e820_type = E820_TYPE_RAM;
156 e820_type = E820_TYPE_RESERVED;
158 case EFI_ACPI_RECLAIM_MEMORY:
159 e820_type = E820_TYPE_ACPI;
161 case EFI_ACPI_MEMORY_NVS:
162 e820_type = E820_TYPE_NVS;
164 case EFI_UNUSABLE_MEMORY:
165 e820_type = E820_TYPE_UNUSABLE;
167 case EFI_PERSISTENT_MEMORY:
168 e820_type = E820_TYPE_PMEM;
172 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
173 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
174 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
176 e820_type = E820_TYPE_RESERVED;
180 e820__range_add(start, size, e820_type);
182 e820__update_table(e820_table);
186 * Given add_efi_memmap defaults to 0 and there there is no alternative
187 * e820 mechanism for soft-reserved memory, import the full EFI memory
188 * map if soft reservations are present and enabled. Otherwise, the
189 * mechanism to disable the kernel's consideration of EFI_MEMORY_SP is
190 * the efi=nosoftreserve option.
192 static bool do_efi_soft_reserve(void)
194 efi_memory_desc_t *md;
196 if (!efi_enabled(EFI_MEMMAP))
199 if (!efi_soft_reserve_enabled())
202 for_each_efi_memory_desc(md)
203 if (md->type == EFI_CONVENTIONAL_MEMORY &&
204 (md->attribute & EFI_MEMORY_SP))
209 int __init efi_memblock_x86_reserve_range(void)
211 struct efi_info *e = &boot_params.efi_info;
212 struct efi_memory_map_data data;
216 if (efi_enabled(EFI_PARAVIRT))
219 /* Can't handle firmware tables above 4GB on i386 */
220 if (IS_ENABLED(CONFIG_X86_32) && e->efi_memmap_hi > 0) {
221 pr_err("Memory map is above 4GB, disabling EFI.\n");
224 pmap = (phys_addr_t)(e->efi_memmap | ((u64)e->efi_memmap_hi << 32));
226 data.phys_map = pmap;
227 data.size = e->efi_memmap_size;
228 data.desc_size = e->efi_memdesc_size;
229 data.desc_version = e->efi_memdesc_version;
231 rv = efi_memmap_init_early(&data);
235 if (add_efi_memmap || do_efi_soft_reserve())
238 efi_fake_memmap_early();
240 WARN(efi.memmap.desc_version != 1,
241 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
242 efi.memmap.desc_version);
244 memblock_reserve(pmap, efi.memmap.nr_map * efi.memmap.desc_size);
249 #define OVERFLOW_ADDR_SHIFT (64 - EFI_PAGE_SHIFT)
250 #define OVERFLOW_ADDR_MASK (U64_MAX << OVERFLOW_ADDR_SHIFT)
251 #define U64_HIGH_BIT (~(U64_MAX >> 1))
253 static bool __init efi_memmap_entry_valid(const efi_memory_desc_t *md, int i)
255 u64 end = (md->num_pages << EFI_PAGE_SHIFT) + md->phys_addr - 1;
259 if (md->num_pages == 0) {
261 } else if (md->num_pages > EFI_PAGES_MAX ||
262 EFI_PAGES_MAX - md->num_pages <
263 (md->phys_addr >> EFI_PAGE_SHIFT)) {
264 end_hi = (md->num_pages & OVERFLOW_ADDR_MASK)
265 >> OVERFLOW_ADDR_SHIFT;
267 if ((md->phys_addr & U64_HIGH_BIT) && !(end & U64_HIGH_BIT))
273 pr_warn_once(FW_BUG "Invalid EFI memory map entries:\n");
276 pr_warn("mem%02u: %s range=[0x%016llx-0x%llx%016llx] (invalid)\n",
277 i, efi_md_typeattr_format(buf, sizeof(buf), md),
278 md->phys_addr, end_hi, end);
280 pr_warn("mem%02u: %s range=[0x%016llx-0x%016llx] (invalid)\n",
281 i, efi_md_typeattr_format(buf, sizeof(buf), md),
287 static void __init efi_clean_memmap(void)
289 efi_memory_desc_t *out = efi.memmap.map;
290 const efi_memory_desc_t *in = out;
291 const efi_memory_desc_t *end = efi.memmap.map_end;
294 for (i = n_removal = 0; in < end; i++) {
295 if (efi_memmap_entry_valid(in, i)) {
297 memcpy(out, in, efi.memmap.desc_size);
298 out = (void *)out + efi.memmap.desc_size;
302 in = (void *)in + efi.memmap.desc_size;
306 struct efi_memory_map_data data = {
307 .phys_map = efi.memmap.phys_map,
308 .desc_version = efi.memmap.desc_version,
309 .desc_size = efi.memmap.desc_size,
310 .size = efi.memmap.desc_size * (efi.memmap.nr_map - n_removal),
314 pr_warn("Removing %d invalid memory map entries.\n", n_removal);
315 efi_memmap_install(&data);
319 void __init efi_print_memmap(void)
321 efi_memory_desc_t *md;
324 for_each_efi_memory_desc(md) {
327 pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
328 i++, efi_md_typeattr_format(buf, sizeof(buf), md),
330 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
331 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
335 static int __init efi_systab_init(u64 phys)
337 int size = efi_enabled(EFI_64BIT) ? sizeof(efi_system_table_64_t)
338 : sizeof(efi_system_table_32_t);
339 const efi_table_hdr_t *hdr;
344 hdr = p = early_memremap_ro(phys, size);
346 pr_err("Couldn't map the system table!\n");
350 ret = efi_systab_check_header(hdr, 1);
352 early_memunmap(p, size);
356 if (efi_enabled(EFI_64BIT)) {
357 const efi_system_table_64_t *systab64 = p;
359 efi_systab.hdr = systab64->hdr;
360 efi_systab.fw_vendor = systab64->fw_vendor;
361 efi_systab.fw_revision = systab64->fw_revision;
362 efi_systab.con_in_handle = systab64->con_in_handle;
363 efi_systab.con_in = systab64->con_in;
364 efi_systab.con_out_handle = systab64->con_out_handle;
365 efi_systab.con_out = (void *)(unsigned long)systab64->con_out;
366 efi_systab.stderr_handle = systab64->stderr_handle;
367 efi_systab.stderr = systab64->stderr;
368 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
369 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
370 efi_systab.nr_tables = systab64->nr_tables;
371 efi_systab.tables = systab64->tables;
373 over4g = systab64->con_in_handle > U32_MAX ||
374 systab64->con_in > U32_MAX ||
375 systab64->con_out_handle > U32_MAX ||
376 systab64->con_out > U32_MAX ||
377 systab64->stderr_handle > U32_MAX ||
378 systab64->stderr > U32_MAX ||
379 systab64->boottime > U32_MAX;
382 struct efi_setup_data *data;
384 data = early_memremap_ro(efi_setup, sizeof(*data));
386 early_memunmap(p, size);
390 efi_systab.fw_vendor = (unsigned long)data->fw_vendor;
391 efi_systab.runtime = (void *)(unsigned long)data->runtime;
392 efi_systab.tables = (unsigned long)data->tables;
394 over4g |= data->fw_vendor > U32_MAX ||
395 data->runtime > U32_MAX ||
396 data->tables > U32_MAX;
398 early_memunmap(data, sizeof(*data));
400 over4g |= systab64->fw_vendor > U32_MAX ||
401 systab64->runtime > U32_MAX ||
402 systab64->tables > U32_MAX;
405 const efi_system_table_32_t *systab32 = p;
407 efi_systab.hdr = systab32->hdr;
408 efi_systab.fw_vendor = systab32->fw_vendor;
409 efi_systab.fw_revision = systab32->fw_revision;
410 efi_systab.con_in_handle = systab32->con_in_handle;
411 efi_systab.con_in = systab32->con_in;
412 efi_systab.con_out_handle = systab32->con_out_handle;
413 efi_systab.con_out = (void *)(unsigned long)systab32->con_out;
414 efi_systab.stderr_handle = systab32->stderr_handle;
415 efi_systab.stderr = systab32->stderr;
416 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
417 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
418 efi_systab.nr_tables = systab32->nr_tables;
419 efi_systab.tables = systab32->tables;
422 efi_systab_report_header(hdr, efi_systab.fw_vendor);
423 early_memunmap(p, size);
425 if (IS_ENABLED(CONFIG_X86_32) && over4g) {
426 pr_err("EFI data located above 4GB, disabling EFI.\n");
430 efi.systab = &efi_systab;
434 void __init efi_init(void)
436 if (IS_ENABLED(CONFIG_X86_32) &&
437 (boot_params.efi_info.efi_systab_hi ||
438 boot_params.efi_info.efi_memmap_hi)) {
439 pr_info("Table located above 4GB, disabling EFI.\n");
443 efi_systab_phys = boot_params.efi_info.efi_systab |
444 ((__u64)boot_params.efi_info.efi_systab_hi << 32);
446 if (efi_systab_init(efi_systab_phys))
449 efi.config_table = (unsigned long)efi.systab->tables;
450 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
451 efi.runtime = (unsigned long)efi.systab->runtime;
453 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
456 if (efi_config_init(arch_tables))
460 * Note: We currently don't support runtime services on an EFI
461 * that doesn't match the kernel 32/64-bit mode.
464 if (!efi_runtime_supported())
465 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
467 if (!efi_runtime_supported() || efi_runtime_disabled()) {
472 /* Parse the EFI Properties table if it exists */
473 if (prop_phys != EFI_INVALID_TABLE_ADDR) {
474 efi_properties_table_t *tbl;
476 tbl = early_memremap_ro(prop_phys, sizeof(*tbl));
478 pr_err("Could not map Properties table!\n");
480 if (tbl->memory_protection_attribute &
481 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
482 set_bit(EFI_NX_PE_DATA, &efi.flags);
484 early_memunmap(tbl, sizeof(*tbl));
488 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
491 if (efi_enabled(EFI_DBG))
495 #if defined(CONFIG_X86_32) || defined(CONFIG_X86_UV)
497 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
501 addr = md->virt_addr;
502 npages = md->num_pages;
504 memrange_efi_to_native(&addr, &npages);
507 set_memory_x(addr, npages);
509 set_memory_nx(addr, npages);
512 void __init runtime_code_page_mkexec(void)
514 efi_memory_desc_t *md;
516 /* Make EFI runtime service code area executable */
517 for_each_efi_memory_desc(md) {
518 if (md->type != EFI_RUNTIME_SERVICES_CODE)
521 efi_set_executable(md, true);
525 void __init efi_memory_uc(u64 addr, unsigned long size)
527 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
530 npages = round_up(size, page_shift) / page_shift;
531 memrange_efi_to_native(&addr, &npages);
532 set_memory_uc(addr, npages);
535 void __init old_map_region(efi_memory_desc_t *md)
537 u64 start_pfn, end_pfn, end;
541 start_pfn = PFN_DOWN(md->phys_addr);
542 size = md->num_pages << PAGE_SHIFT;
543 end = md->phys_addr + size;
544 end_pfn = PFN_UP(end);
546 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
547 va = __va(md->phys_addr);
549 if (!(md->attribute & EFI_MEMORY_WB))
550 efi_memory_uc((u64)(unsigned long)va, size);
552 va = efi_ioremap(md->phys_addr, size,
553 md->type, md->attribute);
555 md->virt_addr = (u64) (unsigned long) va;
557 pr_err("ioremap of 0x%llX failed!\n",
558 (unsigned long long)md->phys_addr);
563 /* Merge contiguous regions of the same type and attribute */
564 static void __init efi_merge_regions(void)
566 efi_memory_desc_t *md, *prev_md = NULL;
568 for_each_efi_memory_desc(md) {
576 if (prev_md->type != md->type ||
577 prev_md->attribute != md->attribute) {
582 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
584 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
585 prev_md->num_pages += md->num_pages;
586 md->type = EFI_RESERVED_TYPE;
594 static void __init get_systab_virt_addr(efi_memory_desc_t *md)
599 size = md->num_pages << EFI_PAGE_SHIFT;
600 end = md->phys_addr + size;
601 systab = efi_systab_phys;
602 if (md->phys_addr <= systab && systab < end) {
603 systab += md->virt_addr - md->phys_addr;
604 efi.systab = (efi_system_table_t *)(unsigned long)systab;
608 static void *realloc_pages(void *old_memmap, int old_shift)
612 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
617 * A first-time allocation doesn't have anything to copy.
622 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
625 free_pages((unsigned long)old_memmap, old_shift);
630 * Iterate the EFI memory map in reverse order because the regions
631 * will be mapped top-down. The end result is the same as if we had
632 * mapped things forward, but doesn't require us to change the
633 * existing implementation of efi_map_region().
635 static inline void *efi_map_next_entry_reverse(void *entry)
639 return efi.memmap.map_end - efi.memmap.desc_size;
641 entry -= efi.memmap.desc_size;
642 if (entry < efi.memmap.map)
649 * efi_map_next_entry - Return the next EFI memory map descriptor
650 * @entry: Previous EFI memory map descriptor
652 * This is a helper function to iterate over the EFI memory map, which
653 * we do in different orders depending on the current configuration.
655 * To begin traversing the memory map @entry must be %NULL.
657 * Returns %NULL when we reach the end of the memory map.
659 static void *efi_map_next_entry(void *entry)
661 if (!efi_have_uv1_memmap() && efi_enabled(EFI_64BIT)) {
663 * Starting in UEFI v2.5 the EFI_PROPERTIES_TABLE
664 * config table feature requires us to map all entries
665 * in the same order as they appear in the EFI memory
666 * map. That is to say, entry N must have a lower
667 * virtual address than entry N+1. This is because the
668 * firmware toolchain leaves relative references in
669 * the code/data sections, which are split and become
670 * separate EFI memory regions. Mapping things
671 * out-of-order leads to the firmware accessing
672 * unmapped addresses.
674 * Since we need to map things this way whether or not
675 * the kernel actually makes use of
676 * EFI_PROPERTIES_TABLE, let's just switch to this
677 * scheme by default for 64-bit.
679 return efi_map_next_entry_reverse(entry);
684 return efi.memmap.map;
686 entry += efi.memmap.desc_size;
687 if (entry >= efi.memmap.map_end)
693 static bool should_map_region(efi_memory_desc_t *md)
696 * Runtime regions always require runtime mappings (obviously).
698 if (md->attribute & EFI_MEMORY_RUNTIME)
702 * 32-bit EFI doesn't suffer from the bug that requires us to
703 * reserve boot services regions, and mixed mode support
704 * doesn't exist for 32-bit kernels.
706 if (IS_ENABLED(CONFIG_X86_32))
710 * EFI specific purpose memory may be reserved by default
711 * depending on kernel config and boot options.
713 if (md->type == EFI_CONVENTIONAL_MEMORY &&
714 efi_soft_reserve_enabled() &&
715 (md->attribute & EFI_MEMORY_SP))
719 * Map all of RAM so that we can access arguments in the 1:1
720 * mapping when making EFI runtime calls.
722 if (efi_is_mixed()) {
723 if (md->type == EFI_CONVENTIONAL_MEMORY ||
724 md->type == EFI_LOADER_DATA ||
725 md->type == EFI_LOADER_CODE)
730 * Map boot services regions as a workaround for buggy
731 * firmware that accesses them even when they shouldn't.
733 * See efi_{reserve,free}_boot_services().
735 if (md->type == EFI_BOOT_SERVICES_CODE ||
736 md->type == EFI_BOOT_SERVICES_DATA)
743 * Map the efi memory ranges of the runtime services and update new_mmap with
746 static void * __init efi_map_regions(int *count, int *pg_shift)
748 void *p, *new_memmap = NULL;
749 unsigned long left = 0;
750 unsigned long desc_size;
751 efi_memory_desc_t *md;
753 desc_size = efi.memmap.desc_size;
756 while ((p = efi_map_next_entry(p))) {
759 if (!should_map_region(md))
763 get_systab_virt_addr(md);
765 if (left < desc_size) {
766 new_memmap = realloc_pages(new_memmap, *pg_shift);
770 left += PAGE_SIZE << *pg_shift;
774 memcpy(new_memmap + (*count * desc_size), md, desc_size);
783 static void __init kexec_enter_virtual_mode(void)
785 #ifdef CONFIG_KEXEC_CORE
786 efi_memory_desc_t *md;
787 unsigned int num_pages;
792 * We don't do virtual mode, since we don't do runtime services, on
793 * non-native EFI. With the UV1 memmap, we don't do runtime services in
794 * kexec kernel because in the initial boot something else might
795 * have been mapped at these virtual addresses.
797 if (efi_is_mixed() || efi_have_uv1_memmap()) {
799 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
803 if (efi_alloc_page_tables()) {
804 pr_err("Failed to allocate EFI page tables\n");
805 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
810 * Map efi regions which were passed via setup_data. The virt_addr is a
811 * fixed addr which was used in first kernel of a kexec boot.
813 for_each_efi_memory_desc(md) {
814 efi_map_region_fixed(md); /* FIXME: add error handling */
815 get_systab_virt_addr(md);
819 * Unregister the early EFI memmap from efi_init() and install
820 * the new EFI memory map.
824 if (efi_memmap_init_late(efi.memmap.phys_map,
825 efi.memmap.desc_size * efi.memmap.nr_map)) {
826 pr_err("Failed to remap late EFI memory map\n");
827 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
833 num_pages = ALIGN(efi.memmap.nr_map * efi.memmap.desc_size, PAGE_SIZE);
834 num_pages >>= PAGE_SHIFT;
836 if (efi_setup_page_tables(efi.memmap.phys_map, num_pages)) {
837 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
841 efi_sync_low_kernel_mappings();
844 * Now that EFI is in virtual mode, update the function
845 * pointers in the runtime service table to the new virtual addresses.
847 * Call EFI services through wrapper functions.
849 efi.runtime_version = efi_systab.hdr.revision;
851 efi_native_runtime_setup();
856 * This function will switch the EFI runtime services to virtual mode.
857 * Essentially, we look through the EFI memmap and map every region that
858 * has the runtime attribute bit set in its memory descriptor into the
859 * efi_pgd page table.
861 * The old method which used to update that memory descriptor with the
862 * virtual address obtained from ioremap() is still supported when the
863 * kernel is booted on SG1 UV1 hardware. Same old method enabled the
864 * runtime services to be called without having to thunk back into
865 * physical mode for every invocation.
867 * The new method does a pagetable switch in a preemption-safe manner
868 * so that we're in a different address space when calling a runtime
869 * function. For function arguments passing we do copy the PUDs of the
870 * kernel page table into efi_pgd prior to each call.
872 * Specially for kexec boot, efi runtime maps in previous kernel should
873 * be passed in via setup_data. In that case runtime ranges will be mapped
874 * to the same virtual addresses as the first kernel, see
875 * kexec_enter_virtual_mode().
877 static void __init __efi_enter_virtual_mode(void)
879 int count = 0, pg_shift = 0;
880 void *new_memmap = NULL;
886 if (efi_alloc_page_tables()) {
887 pr_err("Failed to allocate EFI page tables\n");
892 new_memmap = efi_map_regions(&count, &pg_shift);
894 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
898 pa = __pa(new_memmap);
901 * Unregister the early EFI memmap from efi_init() and install
902 * the new EFI memory map that we are about to pass to the
903 * firmware via SetVirtualAddressMap().
907 if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
908 pr_err("Failed to remap late EFI memory map\n");
912 if (efi_enabled(EFI_DBG)) {
913 pr_info("EFI runtime memory map:\n");
917 if (WARN_ON(!efi.systab))
920 if (efi_setup_page_tables(pa, 1 << pg_shift))
923 efi_sync_low_kernel_mappings();
925 status = efi_set_virtual_address_map(efi.memmap.desc_size * count,
926 efi.memmap.desc_size,
927 efi.memmap.desc_version,
928 (efi_memory_desc_t *)pa);
929 if (status != EFI_SUCCESS) {
930 pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
935 efi_free_boot_services();
938 * Now that EFI is in virtual mode, update the function
939 * pointers in the runtime service table to the new virtual addresses.
941 * Call EFI services through wrapper functions.
943 efi.runtime_version = efi_systab.hdr.revision;
946 efi_native_runtime_setup();
948 efi_thunk_runtime_setup();
951 * Apply more restrictive page table mapping attributes now that
952 * SVAM() has been called and the firmware has performed all
953 * necessary relocation fixups for the new virtual addresses.
955 efi_runtime_update_mappings();
957 /* clean DUMMY object */
958 efi_delete_dummy_variable();
962 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
965 void __init efi_enter_virtual_mode(void)
967 if (efi_enabled(EFI_PARAVIRT))
971 kexec_enter_virtual_mode();
973 __efi_enter_virtual_mode();
975 efi_dump_pagetable();
978 bool efi_is_table_address(unsigned long phys_addr)
982 if (phys_addr == EFI_INVALID_TABLE_ADDR)
985 for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
986 if (*(efi_tables[i]) == phys_addr)
992 char *efi_systab_show_arch(char *str)
994 if (uga_phys != EFI_INVALID_TABLE_ADDR)
995 str += sprintf(str, "UGA=0x%lx\n", uga_phys);