1 // SPDX-License-Identifier: GPL-2.0
3 * x86_64 specific EFI support functions
4 * Based on Extensible Firmware Interface Specification version 1.0
6 * Copyright (C) 2005-2008 Intel Co.
7 * Fenghua Yu <fenghua.yu@intel.com>
8 * Bibo Mao <bibo.mao@intel.com>
9 * Chandramouli Narayanan <mouli@linux.intel.com>
10 * Huang Ying <ying.huang@intel.com>
12 * Code to convert EFI to E820 map has been implemented in elilo bootloader
13 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
14 * is setup appropriately for EFI runtime code.
19 #define pr_fmt(fmt) "efi: " fmt
21 #include <linux/kernel.h>
22 #include <linux/init.h>
24 #include <linux/types.h>
25 #include <linux/spinlock.h>
26 #include <linux/bootmem.h>
27 #include <linux/ioport.h>
28 #include <linux/mc146818rtc.h>
29 #include <linux/efi.h>
30 #include <linux/export.h>
31 #include <linux/uaccess.h>
33 #include <linux/reboot.h>
34 #include <linux/slab.h>
35 #include <linux/ucs2_string.h>
36 #include <linux/mem_encrypt.h>
37 #include <linux/sched/task.h>
39 #include <asm/setup.h>
41 #include <asm/e820/api.h>
42 #include <asm/pgtable.h>
43 #include <asm/tlbflush.h>
44 #include <asm/proto.h>
46 #include <asm/cacheflush.h>
47 #include <asm/fixmap.h>
48 #include <asm/realmode.h>
50 #include <asm/pgalloc.h>
53 * We allocate runtime services regions top-down, starting from -4G, i.e.
54 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
56 static u64 efi_va = EFI_VA_START;
58 struct efi_scratch efi_scratch;
60 static void __init early_code_mapping_set_exec(int executable)
62 efi_memory_desc_t *md;
64 if (!(__supported_pte_mask & _PAGE_NX))
67 /* Make EFI service code area executable */
68 for_each_efi_memory_desc(md) {
69 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
70 md->type == EFI_BOOT_SERVICES_CODE)
71 efi_set_executable(md, executable);
75 pgd_t * __init efi_call_phys_prolog(void)
77 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
78 pgd_t *save_pgd, *pgd_k, *pgd_efi;
79 p4d_t *p4d, *p4d_k, *p4d_efi;
85 if (!efi_enabled(EFI_OLD_MEMMAP)) {
86 efi_switch_mm(&efi_mm);
90 early_code_mapping_set_exec(1);
92 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
93 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
96 * Build 1:1 identity mapping for efi=old_map usage. Note that
97 * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while
98 * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical
99 * address X, the pud_index(X) != pud_index(__va(X)), we can only copy
100 * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping.
101 * This means here we can only reuse the PMD tables of the direct mapping.
103 for (pgd = 0; pgd < n_pgds; pgd++) {
104 addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
105 vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
106 pgd_efi = pgd_offset_k(addr_pgd);
107 save_pgd[pgd] = *pgd_efi;
109 p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
111 pr_err("Failed to allocate p4d table!\n");
115 for (i = 0; i < PTRS_PER_P4D; i++) {
116 addr_p4d = addr_pgd + i * P4D_SIZE;
117 p4d_efi = p4d + p4d_index(addr_p4d);
119 pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
121 pr_err("Failed to allocate pud table!\n");
125 for (j = 0; j < PTRS_PER_PUD; j++) {
126 addr_pud = addr_p4d + j * PUD_SIZE;
128 if (addr_pud > (max_pfn << PAGE_SHIFT))
131 vaddr = (unsigned long)__va(addr_pud);
133 pgd_k = pgd_offset_k(vaddr);
134 p4d_k = p4d_offset(pgd_k, vaddr);
135 pud[j] = *pud_offset(p4d_k, vaddr);
138 pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
147 void __init efi_call_phys_epilog(pgd_t *save_pgd)
150 * After the lock is released, the original page table is restored.
158 if (!efi_enabled(EFI_OLD_MEMMAP)) {
159 efi_switch_mm(efi_scratch.prev_mm);
163 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
165 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
166 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
167 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
169 if (!pgd_present(*pgd))
172 for (i = 0; i < PTRS_PER_P4D; i++) {
173 p4d = p4d_offset(pgd,
174 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
176 if (!p4d_present(*p4d))
179 pud = (pud_t *)p4d_page_vaddr(*p4d);
180 pud_free(&init_mm, pud);
183 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
184 p4d_free(&init_mm, p4d);
190 early_code_mapping_set_exec(0);
193 EXPORT_SYMBOL_GPL(efi_mm);
196 * We need our own copy of the higher levels of the page tables
197 * because we want to avoid inserting EFI region mappings (EFI_VA_END
198 * to EFI_VA_START) into the standard kernel page tables. Everything
199 * else can be shared, see efi_sync_low_kernel_mappings().
201 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
204 int __init efi_alloc_page_tables(void)
206 pgd_t *pgd, *efi_pgd;
211 if (efi_enabled(EFI_OLD_MEMMAP))
214 gfp_mask = GFP_KERNEL | __GFP_ZERO;
215 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
219 pgd = efi_pgd + pgd_index(EFI_VA_END);
220 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
224 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
228 efi_mm.pgd = efi_pgd;
229 mm_init_cpumask(&efi_mm);
230 init_new_context(NULL, &efi_mm);
235 if (pgtable_l5_enabled())
236 free_page((unsigned long)pgd_page_vaddr(*pgd));
238 free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
244 * Add low kernel mappings for passing arguments to EFI functions.
246 void efi_sync_low_kernel_mappings(void)
248 unsigned num_entries;
249 pgd_t *pgd_k, *pgd_efi;
250 p4d_t *p4d_k, *p4d_efi;
251 pud_t *pud_k, *pud_efi;
252 pgd_t *efi_pgd = efi_mm.pgd;
254 if (efi_enabled(EFI_OLD_MEMMAP))
258 * We can share all PGD entries apart from the one entry that
259 * covers the EFI runtime mapping space.
261 * Make sure the EFI runtime region mappings are guaranteed to
262 * only span a single PGD entry and that the entry also maps
263 * other important kernel regions.
265 MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
266 MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
267 (EFI_VA_END & PGDIR_MASK));
269 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
270 pgd_k = pgd_offset_k(PAGE_OFFSET);
272 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
273 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
276 * As with PGDs, we share all P4D entries apart from the one entry
277 * that covers the EFI runtime mapping space.
279 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
280 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
282 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
283 pgd_k = pgd_offset_k(EFI_VA_END);
284 p4d_efi = p4d_offset(pgd_efi, 0);
285 p4d_k = p4d_offset(pgd_k, 0);
287 num_entries = p4d_index(EFI_VA_END);
288 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
291 * We share all the PUD entries apart from those that map the
292 * EFI regions. Copy around them.
294 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
295 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
297 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
298 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
299 pud_efi = pud_offset(p4d_efi, 0);
300 pud_k = pud_offset(p4d_k, 0);
302 num_entries = pud_index(EFI_VA_END);
303 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
305 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
306 pud_k = pud_offset(p4d_k, EFI_VA_START);
308 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
309 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
313 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
315 static inline phys_addr_t
316 virt_to_phys_or_null_size(void *va, unsigned long size)
323 if (virt_addr_valid(va))
324 return virt_to_phys(va);
326 pa = slow_virt_to_phys(va);
328 /* check if the object crosses a page boundary */
329 if (WARN_ON((pa ^ (pa + size - 1)) & PAGE_MASK))
335 #define virt_to_phys_or_null(addr) \
336 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
338 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
340 unsigned long pfn, text, pf;
343 pgd_t *pgd = efi_mm.pgd;
345 if (efi_enabled(EFI_OLD_MEMMAP))
349 * It can happen that the physical address of new_memmap lands in memory
350 * which is not mapped in the EFI page table. Therefore we need to go
351 * and ident-map those pages containing the map before calling
352 * phys_efi_set_virtual_address_map().
354 pfn = pa_memmap >> PAGE_SHIFT;
355 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
356 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
357 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
362 * Certain firmware versions are way too sentimential and still believe
363 * they are exclusive and unquestionable owners of the first physical page,
364 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
365 * (but then write-access it later during SetVirtualAddressMap()).
367 * Create a 1:1 mapping for this page, to avoid triple faults during early
368 * boot with such firmware. We are free to hand this page to the BIOS,
369 * as trim_bios_range() will reserve the first page and isolate it away
370 * from memory allocators anyway.
376 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
377 pr_err("Failed to create 1:1 mapping for the first page!\n");
382 * When making calls to the firmware everything needs to be 1:1
383 * mapped and addressable with 32-bit pointers. Map the kernel
384 * text and allocate a new stack because we can't rely on the
385 * stack pointer being < 4GB.
387 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
390 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
392 pr_err("Unable to allocate EFI runtime stack < 4GB\n");
396 efi_scratch.phys_stack = page_to_phys(page + 1); /* stack grows down */
398 npages = (_etext - _text) >> PAGE_SHIFT;
400 pfn = text >> PAGE_SHIFT;
402 pf = _PAGE_RW | _PAGE_ENC;
403 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
404 pr_err("Failed to map kernel text 1:1\n");
411 static void __init __map_region(efi_memory_desc_t *md, u64 va)
413 unsigned long flags = _PAGE_RW;
415 pgd_t *pgd = efi_mm.pgd;
417 if (!(md->attribute & EFI_MEMORY_WB))
420 if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
423 pfn = md->phys_addr >> PAGE_SHIFT;
424 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
425 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
429 void __init efi_map_region(efi_memory_desc_t *md)
431 unsigned long size = md->num_pages << PAGE_SHIFT;
432 u64 pa = md->phys_addr;
434 if (efi_enabled(EFI_OLD_MEMMAP))
435 return old_map_region(md);
438 * Make sure the 1:1 mappings are present as a catch-all for b0rked
439 * firmware which doesn't update all internal pointers after switching
440 * to virtual mode and would otherwise crap on us.
442 __map_region(md, md->phys_addr);
445 * Enforce the 1:1 mapping as the default virtual address when
446 * booting in EFI mixed mode, because even though we may be
447 * running a 64-bit kernel, the firmware may only be 32-bit.
449 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
450 md->virt_addr = md->phys_addr;
456 /* Is PA 2M-aligned? */
457 if (!(pa & (PMD_SIZE - 1))) {
460 u64 pa_offset = pa & (PMD_SIZE - 1);
461 u64 prev_va = efi_va;
463 /* get us the same offset within this 2M page */
464 efi_va = (efi_va & PMD_MASK) + pa_offset;
466 if (efi_va > prev_va)
470 if (efi_va < EFI_VA_END) {
471 pr_warn(FW_WARN "VA address range overflow!\n");
476 __map_region(md, efi_va);
477 md->virt_addr = efi_va;
481 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
482 * md->virt_addr is the original virtual address which had been mapped in kexec
485 void __init efi_map_region_fixed(efi_memory_desc_t *md)
487 __map_region(md, md->phys_addr);
488 __map_region(md, md->virt_addr);
491 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
492 u32 type, u64 attribute)
494 unsigned long last_map_pfn;
496 if (type == EFI_MEMORY_MAPPED_IO)
497 return ioremap(phys_addr, size);
499 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
500 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
501 unsigned long top = last_map_pfn << PAGE_SHIFT;
502 efi_ioremap(top, size - (top - phys_addr), type, attribute);
505 if (!(attribute & EFI_MEMORY_WB))
506 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
508 return (void __iomem *)__va(phys_addr);
511 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
513 efi_setup = phys_addr + sizeof(struct setup_data);
516 static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
519 pgd_t *pgd = efi_mm.pgd;
522 /* Update the 1:1 mapping */
523 pfn = md->phys_addr >> PAGE_SHIFT;
524 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
526 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
527 md->phys_addr, md->virt_addr);
530 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
532 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
533 md->phys_addr, md->virt_addr);
539 static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
541 unsigned long pf = 0;
543 if (md->attribute & EFI_MEMORY_XP)
546 if (!(md->attribute & EFI_MEMORY_RO))
552 return efi_update_mappings(md, pf);
555 void __init efi_runtime_update_mappings(void)
557 efi_memory_desc_t *md;
559 if (efi_enabled(EFI_OLD_MEMMAP)) {
560 if (__supported_pte_mask & _PAGE_NX)
561 runtime_code_page_mkexec();
566 * Use the EFI Memory Attribute Table for mapping permissions if it
567 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
569 if (efi_enabled(EFI_MEM_ATTR)) {
570 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
575 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
576 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
577 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
578 * published by the firmware. Even if we find a buggy implementation of
579 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
580 * EFI_PROPERTIES_TABLE, because of the same reason.
583 if (!efi_enabled(EFI_NX_PE_DATA))
586 for_each_efi_memory_desc(md) {
587 unsigned long pf = 0;
589 if (!(md->attribute & EFI_MEMORY_RUNTIME))
592 if (!(md->attribute & EFI_MEMORY_WB))
595 if ((md->attribute & EFI_MEMORY_XP) ||
596 (md->type == EFI_RUNTIME_SERVICES_DATA))
599 if (!(md->attribute & EFI_MEMORY_RO) &&
600 (md->type != EFI_RUNTIME_SERVICES_CODE))
606 efi_update_mappings(md, pf);
610 void __init efi_dump_pagetable(void)
612 #ifdef CONFIG_EFI_PGT_DUMP
613 if (efi_enabled(EFI_OLD_MEMMAP))
614 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
616 ptdump_walk_pgd_level(NULL, efi_mm.pgd);
621 * Makes the calling thread switch to/from efi_mm context. Can be used
622 * for SetVirtualAddressMap() i.e. current->active_mm == init_mm as well
623 * as during efi runtime calls i.e current->active_mm == current_mm.
624 * We are not mm_dropping()/mm_grabbing() any mm, because we are not
625 * losing/creating any references.
627 void efi_switch_mm(struct mm_struct *mm)
630 efi_scratch.prev_mm = current->active_mm;
631 current->active_mm = mm;
632 switch_mm(efi_scratch.prev_mm, mm, NULL);
633 task_unlock(current);
636 #ifdef CONFIG_EFI_MIXED
637 extern efi_status_t efi64_thunk(u32, ...);
639 static DEFINE_SPINLOCK(efi_runtime_lock);
641 #define runtime_service32(func) \
643 u32 table = (u32)(unsigned long)efi.systab; \
646 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
647 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
652 * Switch to the EFI page tables early so that we can access the 1:1
653 * runtime services mappings which are not mapped in any other page
654 * tables. This function must be called before runtime_service32().
656 * Also, disable interrupts because the IDT points to 64-bit handlers,
657 * which aren't going to function correctly when we switch to 32-bit.
659 #define efi_thunk(f, ...) \
664 arch_efi_call_virt_setup(); \
666 __func = runtime_service32(f); \
667 __s = efi64_thunk(__func, __VA_ARGS__); \
669 arch_efi_call_virt_teardown(); \
674 efi_status_t efi_thunk_set_virtual_address_map(
675 void *phys_set_virtual_address_map,
676 unsigned long memory_map_size,
677 unsigned long descriptor_size,
678 u32 descriptor_version,
679 efi_memory_desc_t *virtual_map)
685 efi_sync_low_kernel_mappings();
686 local_irq_save(flags);
688 efi_switch_mm(&efi_mm);
690 func = (u32)(unsigned long)phys_set_virtual_address_map;
691 status = efi64_thunk(func, memory_map_size, descriptor_size,
692 descriptor_version, virtual_map);
694 efi_switch_mm(efi_scratch.prev_mm);
695 local_irq_restore(flags);
700 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
703 u32 phys_tm, phys_tc;
706 spin_lock(&rtc_lock);
707 spin_lock_irqsave(&efi_runtime_lock, flags);
709 phys_tm = virt_to_phys_or_null(tm);
710 phys_tc = virt_to_phys_or_null(tc);
712 status = efi_thunk(get_time, phys_tm, phys_tc);
714 spin_unlock_irqrestore(&efi_runtime_lock, flags);
715 spin_unlock(&rtc_lock);
720 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
726 spin_lock(&rtc_lock);
727 spin_lock_irqsave(&efi_runtime_lock, flags);
729 phys_tm = virt_to_phys_or_null(tm);
731 status = efi_thunk(set_time, phys_tm);
733 spin_unlock_irqrestore(&efi_runtime_lock, flags);
734 spin_unlock(&rtc_lock);
740 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
744 u32 phys_enabled, phys_pending, phys_tm;
747 spin_lock(&rtc_lock);
748 spin_lock_irqsave(&efi_runtime_lock, flags);
750 phys_enabled = virt_to_phys_or_null(enabled);
751 phys_pending = virt_to_phys_or_null(pending);
752 phys_tm = virt_to_phys_or_null(tm);
754 status = efi_thunk(get_wakeup_time, phys_enabled,
755 phys_pending, phys_tm);
757 spin_unlock_irqrestore(&efi_runtime_lock, flags);
758 spin_unlock(&rtc_lock);
764 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
770 spin_lock(&rtc_lock);
771 spin_lock_irqsave(&efi_runtime_lock, flags);
773 phys_tm = virt_to_phys_or_null(tm);
775 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
777 spin_unlock_irqrestore(&efi_runtime_lock, flags);
778 spin_unlock(&rtc_lock);
783 static unsigned long efi_name_size(efi_char16_t *name)
785 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
789 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
790 u32 *attr, unsigned long *data_size, void *data)
792 u8 buf[24] __aligned(8);
793 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
795 u32 phys_name, phys_vendor, phys_attr;
796 u32 phys_data_size, phys_data;
799 spin_lock_irqsave(&efi_runtime_lock, flags);
803 phys_data_size = virt_to_phys_or_null(data_size);
804 phys_vendor = virt_to_phys_or_null(vnd);
805 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
806 phys_attr = virt_to_phys_or_null(attr);
807 phys_data = virt_to_phys_or_null_size(data, *data_size);
809 if (!phys_name || (data && !phys_data))
810 status = EFI_INVALID_PARAMETER;
812 status = efi_thunk(get_variable, phys_name, phys_vendor,
813 phys_attr, phys_data_size, phys_data);
815 spin_unlock_irqrestore(&efi_runtime_lock, flags);
821 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
822 u32 attr, unsigned long data_size, void *data)
824 u8 buf[24] __aligned(8);
825 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
826 u32 phys_name, phys_vendor, phys_data;
830 spin_lock_irqsave(&efi_runtime_lock, flags);
834 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
835 phys_vendor = virt_to_phys_or_null(vnd);
836 phys_data = virt_to_phys_or_null_size(data, data_size);
838 if (!phys_name || (data && !phys_data))
839 status = EFI_INVALID_PARAMETER;
841 status = efi_thunk(set_variable, phys_name, phys_vendor,
842 attr, data_size, phys_data);
844 spin_unlock_irqrestore(&efi_runtime_lock, flags);
850 efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
851 u32 attr, unsigned long data_size,
854 u8 buf[24] __aligned(8);
855 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
856 u32 phys_name, phys_vendor, phys_data;
860 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
861 return EFI_NOT_READY;
865 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
866 phys_vendor = virt_to_phys_or_null(vnd);
867 phys_data = virt_to_phys_or_null_size(data, data_size);
869 if (!phys_name || (data && !phys_data))
870 status = EFI_INVALID_PARAMETER;
872 status = efi_thunk(set_variable, phys_name, phys_vendor,
873 attr, data_size, phys_data);
875 spin_unlock_irqrestore(&efi_runtime_lock, flags);
881 efi_thunk_get_next_variable(unsigned long *name_size,
885 u8 buf[24] __aligned(8);
886 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
888 u32 phys_name_size, phys_name, phys_vendor;
891 spin_lock_irqsave(&efi_runtime_lock, flags);
895 phys_name_size = virt_to_phys_or_null(name_size);
896 phys_vendor = virt_to_phys_or_null(vnd);
897 phys_name = virt_to_phys_or_null_size(name, *name_size);
900 status = EFI_INVALID_PARAMETER;
902 status = efi_thunk(get_next_variable, phys_name_size,
903 phys_name, phys_vendor);
905 spin_unlock_irqrestore(&efi_runtime_lock, flags);
912 efi_thunk_get_next_high_mono_count(u32 *count)
918 spin_lock_irqsave(&efi_runtime_lock, flags);
920 phys_count = virt_to_phys_or_null(count);
921 status = efi_thunk(get_next_high_mono_count, phys_count);
923 spin_unlock_irqrestore(&efi_runtime_lock, flags);
929 efi_thunk_reset_system(int reset_type, efi_status_t status,
930 unsigned long data_size, efi_char16_t *data)
935 spin_lock_irqsave(&efi_runtime_lock, flags);
937 phys_data = virt_to_phys_or_null_size(data, data_size);
939 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
941 spin_unlock_irqrestore(&efi_runtime_lock, flags);
945 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
946 unsigned long count, unsigned long sg_list)
949 * To properly support this function we would need to repackage
950 * 'capsules' because the firmware doesn't understand 64-bit
953 return EFI_UNSUPPORTED;
957 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
958 u64 *remaining_space,
959 u64 *max_variable_size)
962 u32 phys_storage, phys_remaining, phys_max;
965 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
966 return EFI_UNSUPPORTED;
968 spin_lock_irqsave(&efi_runtime_lock, flags);
970 phys_storage = virt_to_phys_or_null(storage_space);
971 phys_remaining = virt_to_phys_or_null(remaining_space);
972 phys_max = virt_to_phys_or_null(max_variable_size);
974 status = efi_thunk(query_variable_info, attr, phys_storage,
975 phys_remaining, phys_max);
977 spin_unlock_irqrestore(&efi_runtime_lock, flags);
983 efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space,
984 u64 *remaining_space,
985 u64 *max_variable_size)
988 u32 phys_storage, phys_remaining, phys_max;
991 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
992 return EFI_UNSUPPORTED;
994 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
995 return EFI_NOT_READY;
997 phys_storage = virt_to_phys_or_null(storage_space);
998 phys_remaining = virt_to_phys_or_null(remaining_space);
999 phys_max = virt_to_phys_or_null(max_variable_size);
1001 status = efi_thunk(query_variable_info, attr, phys_storage,
1002 phys_remaining, phys_max);
1004 spin_unlock_irqrestore(&efi_runtime_lock, flags);
1010 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
1011 unsigned long count, u64 *max_size,
1015 * To properly support this function we would need to repackage
1016 * 'capsules' because the firmware doesn't understand 64-bit
1019 return EFI_UNSUPPORTED;
1022 void efi_thunk_runtime_setup(void)
1024 efi.get_time = efi_thunk_get_time;
1025 efi.set_time = efi_thunk_set_time;
1026 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
1027 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
1028 efi.get_variable = efi_thunk_get_variable;
1029 efi.get_next_variable = efi_thunk_get_next_variable;
1030 efi.set_variable = efi_thunk_set_variable;
1031 efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking;
1032 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
1033 efi.reset_system = efi_thunk_reset_system;
1034 efi.query_variable_info = efi_thunk_query_variable_info;
1035 efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking;
1036 efi.update_capsule = efi_thunk_update_capsule;
1037 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
1039 #endif /* CONFIG_EFI_MIXED */