ARM: dma-api: fix max_pfn off-by-one error in __dma_supported()
[linux/fpc-iii.git] / arch / x86 / platform / efi / efi_64.c
blobfa8506e76bbeba3eb7b2efc75ff96767d41f6b6b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
15 * - mouli 06/14/2007.
19 #define pr_fmt(fmt) "efi: " fmt
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/mm.h>
24 #include <linux/types.h>
25 #include <linux/spinlock.h>
26 #include <linux/memblock.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>
32 #include <linux/io.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>
40 #include <asm/page.h>
41 #include <asm/e820/api.h>
42 #include <asm/pgtable.h>
43 #include <asm/tlbflush.h>
44 #include <asm/proto.h>
45 #include <asm/efi.h>
46 #include <asm/cacheflush.h>
47 #include <asm/fixmap.h>
48 #include <asm/realmode.h>
49 #include <asm/time.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 EXPORT_SYMBOL_GPL(efi_mm);
63 * We need our own copy of the higher levels of the page tables
64 * because we want to avoid inserting EFI region mappings (EFI_VA_END
65 * to EFI_VA_START) into the standard kernel page tables. Everything
66 * else can be shared, see efi_sync_low_kernel_mappings().
68 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
69 * allocation.
71 int __init efi_alloc_page_tables(void)
73 pgd_t *pgd, *efi_pgd;
74 p4d_t *p4d;
75 pud_t *pud;
76 gfp_t gfp_mask;
78 if (efi_have_uv1_memmap())
79 return 0;
81 gfp_mask = GFP_KERNEL | __GFP_ZERO;
82 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
83 if (!efi_pgd)
84 return -ENOMEM;
86 pgd = efi_pgd + pgd_index(EFI_VA_END);
87 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
88 if (!p4d) {
89 free_page((unsigned long)efi_pgd);
90 return -ENOMEM;
93 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
94 if (!pud) {
95 if (pgtable_l5_enabled())
96 free_page((unsigned long) pgd_page_vaddr(*pgd));
97 free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
98 return -ENOMEM;
101 efi_mm.pgd = efi_pgd;
102 mm_init_cpumask(&efi_mm);
103 init_new_context(NULL, &efi_mm);
105 return 0;
109 * Add low kernel mappings for passing arguments to EFI functions.
111 void efi_sync_low_kernel_mappings(void)
113 unsigned num_entries;
114 pgd_t *pgd_k, *pgd_efi;
115 p4d_t *p4d_k, *p4d_efi;
116 pud_t *pud_k, *pud_efi;
117 pgd_t *efi_pgd = efi_mm.pgd;
119 if (efi_have_uv1_memmap())
120 return;
123 * We can share all PGD entries apart from the one entry that
124 * covers the EFI runtime mapping space.
126 * Make sure the EFI runtime region mappings are guaranteed to
127 * only span a single PGD entry and that the entry also maps
128 * other important kernel regions.
130 MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
131 MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
132 (EFI_VA_END & PGDIR_MASK));
134 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
135 pgd_k = pgd_offset_k(PAGE_OFFSET);
137 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
138 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
141 * As with PGDs, we share all P4D entries apart from the one entry
142 * that covers the EFI runtime mapping space.
144 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
145 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
147 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
148 pgd_k = pgd_offset_k(EFI_VA_END);
149 p4d_efi = p4d_offset(pgd_efi, 0);
150 p4d_k = p4d_offset(pgd_k, 0);
152 num_entries = p4d_index(EFI_VA_END);
153 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
156 * We share all the PUD entries apart from those that map the
157 * EFI regions. Copy around them.
159 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
160 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
162 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
163 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
164 pud_efi = pud_offset(p4d_efi, 0);
165 pud_k = pud_offset(p4d_k, 0);
167 num_entries = pud_index(EFI_VA_END);
168 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
170 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
171 pud_k = pud_offset(p4d_k, EFI_VA_START);
173 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
174 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
178 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
180 static inline phys_addr_t
181 virt_to_phys_or_null_size(void *va, unsigned long size)
183 bool bad_size;
185 if (!va)
186 return 0;
188 if (virt_addr_valid(va))
189 return virt_to_phys(va);
192 * A fully aligned variable on the stack is guaranteed not to
193 * cross a page bounary. Try to catch strings on the stack by
194 * checking that 'size' is a power of two.
196 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
198 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
200 return slow_virt_to_phys(va);
203 #define virt_to_phys_or_null(addr) \
204 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
206 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
208 unsigned long pfn, text, pf;
209 struct page *page;
210 unsigned npages;
211 pgd_t *pgd = efi_mm.pgd;
213 if (efi_have_uv1_memmap())
214 return 0;
217 * It can happen that the physical address of new_memmap lands in memory
218 * which is not mapped in the EFI page table. Therefore we need to go
219 * and ident-map those pages containing the map before calling
220 * phys_efi_set_virtual_address_map().
222 pfn = pa_memmap >> PAGE_SHIFT;
223 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
224 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
225 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
226 return 1;
230 * Certain firmware versions are way too sentimential and still believe
231 * they are exclusive and unquestionable owners of the first physical page,
232 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
233 * (but then write-access it later during SetVirtualAddressMap()).
235 * Create a 1:1 mapping for this page, to avoid triple faults during early
236 * boot with such firmware. We are free to hand this page to the BIOS,
237 * as trim_bios_range() will reserve the first page and isolate it away
238 * from memory allocators anyway.
240 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
241 pr_err("Failed to create 1:1 mapping for the first page!\n");
242 return 1;
246 * When making calls to the firmware everything needs to be 1:1
247 * mapped and addressable with 32-bit pointers. Map the kernel
248 * text and allocate a new stack because we can't rely on the
249 * stack pointer being < 4GB.
251 if (!efi_is_mixed())
252 return 0;
254 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
255 if (!page) {
256 pr_err("Unable to allocate EFI runtime stack < 4GB\n");
257 return 1;
260 efi_scratch.phys_stack = page_to_phys(page + 1); /* stack grows down */
262 npages = (__end_rodata_aligned - _text) >> PAGE_SHIFT;
263 text = __pa(_text);
264 pfn = text >> PAGE_SHIFT;
266 pf = _PAGE_ENC;
267 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
268 pr_err("Failed to map kernel text 1:1\n");
269 return 1;
272 return 0;
275 static void __init __map_region(efi_memory_desc_t *md, u64 va)
277 unsigned long flags = _PAGE_RW;
278 unsigned long pfn;
279 pgd_t *pgd = efi_mm.pgd;
282 * EFI_RUNTIME_SERVICES_CODE regions typically cover PE/COFF
283 * executable images in memory that consist of both R-X and
284 * RW- sections, so we cannot apply read-only or non-exec
285 * permissions just yet. However, modern EFI systems provide
286 * a memory attributes table that describes those sections
287 * with the appropriate restricted permissions, which are
288 * applied in efi_runtime_update_mappings() below. All other
289 * regions can be mapped non-executable at this point, with
290 * the exception of boot services code regions, but those will
291 * be unmapped again entirely in efi_free_boot_services().
293 if (md->type != EFI_BOOT_SERVICES_CODE &&
294 md->type != EFI_RUNTIME_SERVICES_CODE)
295 flags |= _PAGE_NX;
297 if (!(md->attribute & EFI_MEMORY_WB))
298 flags |= _PAGE_PCD;
300 if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
301 flags |= _PAGE_ENC;
303 pfn = md->phys_addr >> PAGE_SHIFT;
304 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
305 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
306 md->phys_addr, va);
309 void __init efi_map_region(efi_memory_desc_t *md)
311 unsigned long size = md->num_pages << PAGE_SHIFT;
312 u64 pa = md->phys_addr;
314 if (efi_have_uv1_memmap())
315 return old_map_region(md);
318 * Make sure the 1:1 mappings are present as a catch-all for b0rked
319 * firmware which doesn't update all internal pointers after switching
320 * to virtual mode and would otherwise crap on us.
322 __map_region(md, md->phys_addr);
325 * Enforce the 1:1 mapping as the default virtual address when
326 * booting in EFI mixed mode, because even though we may be
327 * running a 64-bit kernel, the firmware may only be 32-bit.
329 if (efi_is_mixed()) {
330 md->virt_addr = md->phys_addr;
331 return;
334 efi_va -= size;
336 /* Is PA 2M-aligned? */
337 if (!(pa & (PMD_SIZE - 1))) {
338 efi_va &= PMD_MASK;
339 } else {
340 u64 pa_offset = pa & (PMD_SIZE - 1);
341 u64 prev_va = efi_va;
343 /* get us the same offset within this 2M page */
344 efi_va = (efi_va & PMD_MASK) + pa_offset;
346 if (efi_va > prev_va)
347 efi_va -= PMD_SIZE;
350 if (efi_va < EFI_VA_END) {
351 pr_warn(FW_WARN "VA address range overflow!\n");
352 return;
355 /* Do the VA map */
356 __map_region(md, efi_va);
357 md->virt_addr = efi_va;
361 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
362 * md->virt_addr is the original virtual address which had been mapped in kexec
363 * 1st kernel.
365 void __init efi_map_region_fixed(efi_memory_desc_t *md)
367 __map_region(md, md->phys_addr);
368 __map_region(md, md->virt_addr);
371 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
373 efi_setup = phys_addr + sizeof(struct setup_data);
376 static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
378 unsigned long pfn;
379 pgd_t *pgd = efi_mm.pgd;
380 int err1, err2;
382 /* Update the 1:1 mapping */
383 pfn = md->phys_addr >> PAGE_SHIFT;
384 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
385 if (err1) {
386 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
387 md->phys_addr, md->virt_addr);
390 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
391 if (err2) {
392 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
393 md->phys_addr, md->virt_addr);
396 return err1 || err2;
399 static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
401 unsigned long pf = 0;
403 if (md->attribute & EFI_MEMORY_XP)
404 pf |= _PAGE_NX;
406 if (!(md->attribute & EFI_MEMORY_RO))
407 pf |= _PAGE_RW;
409 if (sev_active())
410 pf |= _PAGE_ENC;
412 return efi_update_mappings(md, pf);
415 void __init efi_runtime_update_mappings(void)
417 efi_memory_desc_t *md;
419 if (efi_have_uv1_memmap()) {
420 if (__supported_pte_mask & _PAGE_NX)
421 runtime_code_page_mkexec();
422 return;
426 * Use the EFI Memory Attribute Table for mapping permissions if it
427 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
429 if (efi_enabled(EFI_MEM_ATTR)) {
430 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
431 return;
435 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
436 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
437 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
438 * published by the firmware. Even if we find a buggy implementation of
439 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
440 * EFI_PROPERTIES_TABLE, because of the same reason.
443 if (!efi_enabled(EFI_NX_PE_DATA))
444 return;
446 for_each_efi_memory_desc(md) {
447 unsigned long pf = 0;
449 if (!(md->attribute & EFI_MEMORY_RUNTIME))
450 continue;
452 if (!(md->attribute & EFI_MEMORY_WB))
453 pf |= _PAGE_PCD;
455 if ((md->attribute & EFI_MEMORY_XP) ||
456 (md->type == EFI_RUNTIME_SERVICES_DATA))
457 pf |= _PAGE_NX;
459 if (!(md->attribute & EFI_MEMORY_RO) &&
460 (md->type != EFI_RUNTIME_SERVICES_CODE))
461 pf |= _PAGE_RW;
463 if (sev_active())
464 pf |= _PAGE_ENC;
466 efi_update_mappings(md, pf);
470 void __init efi_dump_pagetable(void)
472 #ifdef CONFIG_EFI_PGT_DUMP
473 if (efi_have_uv1_memmap())
474 ptdump_walk_pgd_level(NULL, &init_mm);
475 else
476 ptdump_walk_pgd_level(NULL, &efi_mm);
477 #endif
481 * Makes the calling thread switch to/from efi_mm context. Can be used
482 * in a kernel thread and user context. Preemption needs to remain disabled
483 * while the EFI-mm is borrowed. mmgrab()/mmdrop() is not used because the mm
484 * can not change under us.
485 * It should be ensured that there are no concurent calls to this function.
487 void efi_switch_mm(struct mm_struct *mm)
489 efi_scratch.prev_mm = current->active_mm;
490 current->active_mm = mm;
491 switch_mm(efi_scratch.prev_mm, mm, NULL);
494 static DEFINE_SPINLOCK(efi_runtime_lock);
497 * DS and ES contain user values. We need to save them.
498 * The 32-bit EFI code needs a valid DS, ES, and SS. There's no
499 * need to save the old SS: __KERNEL_DS is always acceptable.
501 #define __efi_thunk(func, ...) \
502 ({ \
503 efi_runtime_services_32_t *__rt; \
504 unsigned short __ds, __es; \
505 efi_status_t ____s; \
507 __rt = (void *)(unsigned long)efi.systab->mixed_mode.runtime; \
509 savesegment(ds, __ds); \
510 savesegment(es, __es); \
512 loadsegment(ss, __KERNEL_DS); \
513 loadsegment(ds, __KERNEL_DS); \
514 loadsegment(es, __KERNEL_DS); \
516 ____s = efi64_thunk(__rt->func, __VA_ARGS__); \
518 loadsegment(ds, __ds); \
519 loadsegment(es, __es); \
521 ____s ^= (____s & BIT(31)) | (____s & BIT_ULL(31)) << 32; \
522 ____s; \
526 * Switch to the EFI page tables early so that we can access the 1:1
527 * runtime services mappings which are not mapped in any other page
528 * tables.
530 * Also, disable interrupts because the IDT points to 64-bit handlers,
531 * which aren't going to function correctly when we switch to 32-bit.
533 #define efi_thunk(func...) \
534 ({ \
535 efi_status_t __s; \
537 arch_efi_call_virt_setup(); \
539 __s = __efi_thunk(func); \
541 arch_efi_call_virt_teardown(); \
543 __s; \
546 static efi_status_t __init __no_sanitize_address
547 efi_thunk_set_virtual_address_map(unsigned long memory_map_size,
548 unsigned long descriptor_size,
549 u32 descriptor_version,
550 efi_memory_desc_t *virtual_map)
552 efi_status_t status;
553 unsigned long flags;
555 efi_sync_low_kernel_mappings();
556 local_irq_save(flags);
558 efi_switch_mm(&efi_mm);
560 status = __efi_thunk(set_virtual_address_map, memory_map_size,
561 descriptor_size, descriptor_version, virtual_map);
563 efi_switch_mm(efi_scratch.prev_mm);
564 local_irq_restore(flags);
566 return status;
569 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
571 efi_status_t status;
572 u32 phys_tm, phys_tc;
573 unsigned long flags;
575 spin_lock(&rtc_lock);
576 spin_lock_irqsave(&efi_runtime_lock, flags);
578 phys_tm = virt_to_phys_or_null(tm);
579 phys_tc = virt_to_phys_or_null(tc);
581 status = efi_thunk(get_time, phys_tm, phys_tc);
583 spin_unlock_irqrestore(&efi_runtime_lock, flags);
584 spin_unlock(&rtc_lock);
586 return status;
589 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
591 efi_status_t status;
592 u32 phys_tm;
593 unsigned long flags;
595 spin_lock(&rtc_lock);
596 spin_lock_irqsave(&efi_runtime_lock, flags);
598 phys_tm = virt_to_phys_or_null(tm);
600 status = efi_thunk(set_time, phys_tm);
602 spin_unlock_irqrestore(&efi_runtime_lock, flags);
603 spin_unlock(&rtc_lock);
605 return status;
608 static efi_status_t
609 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
610 efi_time_t *tm)
612 efi_status_t status;
613 u32 phys_enabled, phys_pending, phys_tm;
614 unsigned long flags;
616 spin_lock(&rtc_lock);
617 spin_lock_irqsave(&efi_runtime_lock, flags);
619 phys_enabled = virt_to_phys_or_null(enabled);
620 phys_pending = virt_to_phys_or_null(pending);
621 phys_tm = virt_to_phys_or_null(tm);
623 status = efi_thunk(get_wakeup_time, phys_enabled,
624 phys_pending, phys_tm);
626 spin_unlock_irqrestore(&efi_runtime_lock, flags);
627 spin_unlock(&rtc_lock);
629 return status;
632 static efi_status_t
633 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
635 efi_status_t status;
636 u32 phys_tm;
637 unsigned long flags;
639 spin_lock(&rtc_lock);
640 spin_lock_irqsave(&efi_runtime_lock, flags);
642 phys_tm = virt_to_phys_or_null(tm);
644 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
646 spin_unlock_irqrestore(&efi_runtime_lock, flags);
647 spin_unlock(&rtc_lock);
649 return status;
652 static unsigned long efi_name_size(efi_char16_t *name)
654 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
657 static efi_status_t
658 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
659 u32 *attr, unsigned long *data_size, void *data)
661 efi_status_t status;
662 u32 phys_name, phys_vendor, phys_attr;
663 u32 phys_data_size, phys_data;
664 unsigned long flags;
666 spin_lock_irqsave(&efi_runtime_lock, flags);
668 phys_data_size = virt_to_phys_or_null(data_size);
669 phys_vendor = virt_to_phys_or_null(vendor);
670 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
671 phys_attr = virt_to_phys_or_null(attr);
672 phys_data = virt_to_phys_or_null_size(data, *data_size);
674 status = efi_thunk(get_variable, phys_name, phys_vendor,
675 phys_attr, phys_data_size, phys_data);
677 spin_unlock_irqrestore(&efi_runtime_lock, flags);
679 return status;
682 static efi_status_t
683 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
684 u32 attr, unsigned long data_size, void *data)
686 u32 phys_name, phys_vendor, phys_data;
687 efi_status_t status;
688 unsigned long flags;
690 spin_lock_irqsave(&efi_runtime_lock, flags);
692 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
693 phys_vendor = virt_to_phys_or_null(vendor);
694 phys_data = virt_to_phys_or_null_size(data, data_size);
696 /* If data_size is > sizeof(u32) we've got problems */
697 status = efi_thunk(set_variable, phys_name, phys_vendor,
698 attr, data_size, phys_data);
700 spin_unlock_irqrestore(&efi_runtime_lock, flags);
702 return status;
705 static efi_status_t
706 efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
707 u32 attr, unsigned long data_size,
708 void *data)
710 u32 phys_name, phys_vendor, phys_data;
711 efi_status_t status;
712 unsigned long flags;
714 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
715 return EFI_NOT_READY;
717 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
718 phys_vendor = virt_to_phys_or_null(vendor);
719 phys_data = virt_to_phys_or_null_size(data, data_size);
721 /* If data_size is > sizeof(u32) we've got problems */
722 status = efi_thunk(set_variable, phys_name, phys_vendor,
723 attr, data_size, phys_data);
725 spin_unlock_irqrestore(&efi_runtime_lock, flags);
727 return status;
730 static efi_status_t
731 efi_thunk_get_next_variable(unsigned long *name_size,
732 efi_char16_t *name,
733 efi_guid_t *vendor)
735 efi_status_t status;
736 u32 phys_name_size, phys_name, phys_vendor;
737 unsigned long flags;
739 spin_lock_irqsave(&efi_runtime_lock, flags);
741 phys_name_size = virt_to_phys_or_null(name_size);
742 phys_vendor = virt_to_phys_or_null(vendor);
743 phys_name = virt_to_phys_or_null_size(name, *name_size);
745 status = efi_thunk(get_next_variable, phys_name_size,
746 phys_name, phys_vendor);
748 spin_unlock_irqrestore(&efi_runtime_lock, flags);
750 return status;
753 static efi_status_t
754 efi_thunk_get_next_high_mono_count(u32 *count)
756 efi_status_t status;
757 u32 phys_count;
758 unsigned long flags;
760 spin_lock_irqsave(&efi_runtime_lock, flags);
762 phys_count = virt_to_phys_or_null(count);
763 status = efi_thunk(get_next_high_mono_count, phys_count);
765 spin_unlock_irqrestore(&efi_runtime_lock, flags);
767 return status;
770 static void
771 efi_thunk_reset_system(int reset_type, efi_status_t status,
772 unsigned long data_size, efi_char16_t *data)
774 u32 phys_data;
775 unsigned long flags;
777 spin_lock_irqsave(&efi_runtime_lock, flags);
779 phys_data = virt_to_phys_or_null_size(data, data_size);
781 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
783 spin_unlock_irqrestore(&efi_runtime_lock, flags);
786 static efi_status_t
787 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
788 unsigned long count, unsigned long sg_list)
791 * To properly support this function we would need to repackage
792 * 'capsules' because the firmware doesn't understand 64-bit
793 * pointers.
795 return EFI_UNSUPPORTED;
798 static efi_status_t
799 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
800 u64 *remaining_space,
801 u64 *max_variable_size)
803 efi_status_t status;
804 u32 phys_storage, phys_remaining, phys_max;
805 unsigned long flags;
807 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
808 return EFI_UNSUPPORTED;
810 spin_lock_irqsave(&efi_runtime_lock, flags);
812 phys_storage = virt_to_phys_or_null(storage_space);
813 phys_remaining = virt_to_phys_or_null(remaining_space);
814 phys_max = virt_to_phys_or_null(max_variable_size);
816 status = efi_thunk(query_variable_info, attr, phys_storage,
817 phys_remaining, phys_max);
819 spin_unlock_irqrestore(&efi_runtime_lock, flags);
821 return status;
824 static efi_status_t
825 efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space,
826 u64 *remaining_space,
827 u64 *max_variable_size)
829 efi_status_t status;
830 u32 phys_storage, phys_remaining, phys_max;
831 unsigned long flags;
833 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
834 return EFI_UNSUPPORTED;
836 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
837 return EFI_NOT_READY;
839 phys_storage = virt_to_phys_or_null(storage_space);
840 phys_remaining = virt_to_phys_or_null(remaining_space);
841 phys_max = virt_to_phys_or_null(max_variable_size);
843 status = efi_thunk(query_variable_info, attr, phys_storage,
844 phys_remaining, phys_max);
846 spin_unlock_irqrestore(&efi_runtime_lock, flags);
848 return status;
851 static efi_status_t
852 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
853 unsigned long count, u64 *max_size,
854 int *reset_type)
857 * To properly support this function we would need to repackage
858 * 'capsules' because the firmware doesn't understand 64-bit
859 * pointers.
861 return EFI_UNSUPPORTED;
864 void __init efi_thunk_runtime_setup(void)
866 if (!IS_ENABLED(CONFIG_EFI_MIXED))
867 return;
869 efi.get_time = efi_thunk_get_time;
870 efi.set_time = efi_thunk_set_time;
871 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
872 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
873 efi.get_variable = efi_thunk_get_variable;
874 efi.get_next_variable = efi_thunk_get_next_variable;
875 efi.set_variable = efi_thunk_set_variable;
876 efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking;
877 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
878 efi.reset_system = efi_thunk_reset_system;
879 efi.query_variable_info = efi_thunk_query_variable_info;
880 efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking;
881 efi.update_capsule = efi_thunk_update_capsule;
882 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
885 efi_status_t __init __no_sanitize_address
886 efi_set_virtual_address_map(unsigned long memory_map_size,
887 unsigned long descriptor_size,
888 u32 descriptor_version,
889 efi_memory_desc_t *virtual_map)
891 efi_status_t status;
892 unsigned long flags;
893 pgd_t *save_pgd = NULL;
895 if (efi_is_mixed())
896 return efi_thunk_set_virtual_address_map(memory_map_size,
897 descriptor_size,
898 descriptor_version,
899 virtual_map);
901 if (efi_have_uv1_memmap()) {
902 save_pgd = efi_uv1_memmap_phys_prolog();
903 if (!save_pgd)
904 return EFI_ABORTED;
905 } else {
906 efi_switch_mm(&efi_mm);
909 kernel_fpu_begin();
911 /* Disable interrupts around EFI calls: */
912 local_irq_save(flags);
913 status = efi_call(efi.systab->runtime->set_virtual_address_map,
914 memory_map_size, descriptor_size,
915 descriptor_version, virtual_map);
916 local_irq_restore(flags);
918 kernel_fpu_end();
920 if (save_pgd)
921 efi_uv1_memmap_phys_epilog(save_pgd);
922 else
923 efi_switch_mm(efi_scratch.prev_mm);
925 return status;