Linux 4.18.10
[linux/fpc-iii.git] / arch / x86 / platform / efi / efi_64.c
blob5f2eb32316073f15307aa28ac8b37a17230ea751
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/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>
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 static void __init early_code_mapping_set_exec(int executable)
62 efi_memory_desc_t *md;
64 if (!(__supported_pte_mask & _PAGE_NX))
65 return;
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;
80 pud_t *pud;
82 int pgd;
83 int n_pgds, i, j;
85 if (!efi_enabled(EFI_OLD_MEMMAP)) {
86 efi_switch_mm(&efi_mm);
87 return NULL;
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);
110 if (!p4d) {
111 pr_err("Failed to allocate p4d table!\n");
112 goto out;
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);
120 if (!pud) {
121 pr_err("Failed to allocate pud table!\n");
122 goto out;
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))
129 break;
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;
141 out:
142 __flush_tlb_all();
144 return save_pgd;
147 void __init efi_call_phys_epilog(pgd_t *save_pgd)
150 * After the lock is released, the original page table is restored.
152 int pgd_idx, i;
153 int nr_pgds;
154 pgd_t *pgd;
155 p4d_t *p4d;
156 pud_t *pud;
158 if (!efi_enabled(EFI_OLD_MEMMAP)) {
159 efi_switch_mm(efi_scratch.prev_mm);
160 return;
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))
170 continue;
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))
177 continue;
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);
187 kfree(save_pgd);
189 __flush_tlb_all();
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
202 * allocation.
204 int __init efi_alloc_page_tables(void)
206 pgd_t *pgd, *efi_pgd;
207 p4d_t *p4d;
208 pud_t *pud;
209 gfp_t gfp_mask;
211 if (efi_enabled(EFI_OLD_MEMMAP))
212 return 0;
214 gfp_mask = GFP_KERNEL | __GFP_ZERO;
215 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
216 if (!efi_pgd)
217 return -ENOMEM;
219 pgd = efi_pgd + pgd_index(EFI_VA_END);
220 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
221 if (!p4d) {
222 free_page((unsigned long)efi_pgd);
223 return -ENOMEM;
226 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
227 if (!pud) {
228 if (pgtable_l5_enabled())
229 free_page((unsigned long) pgd_page_vaddr(*pgd));
230 free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
231 return -ENOMEM;
234 efi_mm.pgd = efi_pgd;
235 mm_init_cpumask(&efi_mm);
236 init_new_context(NULL, &efi_mm);
238 return 0;
242 * Add low kernel mappings for passing arguments to EFI functions.
244 void efi_sync_low_kernel_mappings(void)
246 unsigned num_entries;
247 pgd_t *pgd_k, *pgd_efi;
248 p4d_t *p4d_k, *p4d_efi;
249 pud_t *pud_k, *pud_efi;
250 pgd_t *efi_pgd = efi_mm.pgd;
252 if (efi_enabled(EFI_OLD_MEMMAP))
253 return;
256 * We can share all PGD entries apart from the one entry that
257 * covers the EFI runtime mapping space.
259 * Make sure the EFI runtime region mappings are guaranteed to
260 * only span a single PGD entry and that the entry also maps
261 * other important kernel regions.
263 MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
264 MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
265 (EFI_VA_END & PGDIR_MASK));
267 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
268 pgd_k = pgd_offset_k(PAGE_OFFSET);
270 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
271 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
274 * As with PGDs, we share all P4D entries apart from the one entry
275 * that covers the EFI runtime mapping space.
277 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
278 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
280 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
281 pgd_k = pgd_offset_k(EFI_VA_END);
282 p4d_efi = p4d_offset(pgd_efi, 0);
283 p4d_k = p4d_offset(pgd_k, 0);
285 num_entries = p4d_index(EFI_VA_END);
286 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
289 * We share all the PUD entries apart from those that map the
290 * EFI regions. Copy around them.
292 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
293 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
295 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
296 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
297 pud_efi = pud_offset(p4d_efi, 0);
298 pud_k = pud_offset(p4d_k, 0);
300 num_entries = pud_index(EFI_VA_END);
301 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
303 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
304 pud_k = pud_offset(p4d_k, EFI_VA_START);
306 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
307 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
311 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
313 static inline phys_addr_t
314 virt_to_phys_or_null_size(void *va, unsigned long size)
316 bool bad_size;
318 if (!va)
319 return 0;
321 if (virt_addr_valid(va))
322 return virt_to_phys(va);
325 * A fully aligned variable on the stack is guaranteed not to
326 * cross a page bounary. Try to catch strings on the stack by
327 * checking that 'size' is a power of two.
329 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
331 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
333 return slow_virt_to_phys(va);
336 #define virt_to_phys_or_null(addr) \
337 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
339 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
341 unsigned long pfn, text, pf;
342 struct page *page;
343 unsigned npages;
344 pgd_t *pgd = efi_mm.pgd;
346 if (efi_enabled(EFI_OLD_MEMMAP))
347 return 0;
350 * It can happen that the physical address of new_memmap lands in memory
351 * which is not mapped in the EFI page table. Therefore we need to go
352 * and ident-map those pages containing the map before calling
353 * phys_efi_set_virtual_address_map().
355 pfn = pa_memmap >> PAGE_SHIFT;
356 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
357 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
358 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
359 return 1;
363 * Certain firmware versions are way too sentimential and still believe
364 * they are exclusive and unquestionable owners of the first physical page,
365 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
366 * (but then write-access it later during SetVirtualAddressMap()).
368 * Create a 1:1 mapping for this page, to avoid triple faults during early
369 * boot with such firmware. We are free to hand this page to the BIOS,
370 * as trim_bios_range() will reserve the first page and isolate it away
371 * from memory allocators anyway.
373 pf = _PAGE_RW;
374 if (sev_active())
375 pf |= _PAGE_ENC;
377 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
378 pr_err("Failed to create 1:1 mapping for the first page!\n");
379 return 1;
383 * When making calls to the firmware everything needs to be 1:1
384 * mapped and addressable with 32-bit pointers. Map the kernel
385 * text and allocate a new stack because we can't rely on the
386 * stack pointer being < 4GB.
388 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
389 return 0;
391 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
392 if (!page)
393 panic("Unable to allocate EFI runtime stack < 4GB\n");
395 efi_scratch.phys_stack = virt_to_phys(page_address(page));
396 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
398 npages = (_etext - _text) >> PAGE_SHIFT;
399 text = __pa(_text);
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");
405 return 1;
408 return 0;
411 static void __init __map_region(efi_memory_desc_t *md, u64 va)
413 unsigned long flags = _PAGE_RW;
414 unsigned long pfn;
415 pgd_t *pgd = efi_mm.pgd;
417 if (!(md->attribute & EFI_MEMORY_WB))
418 flags |= _PAGE_PCD;
420 if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
421 flags |= _PAGE_ENC;
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",
426 md->phys_addr, va);
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;
451 return;
454 efi_va -= size;
456 /* Is PA 2M-aligned? */
457 if (!(pa & (PMD_SIZE - 1))) {
458 efi_va &= PMD_MASK;
459 } else {
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)
467 efi_va -= PMD_SIZE;
470 if (efi_va < EFI_VA_END) {
471 pr_warn(FW_WARN "VA address range overflow!\n");
472 return;
475 /* Do the VA map */
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
483 * 1st kernel.
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)
518 unsigned long pfn;
519 pgd_t *pgd = efi_mm.pgd;
520 int err1, err2;
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);
525 if (err1) {
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);
531 if (err2) {
532 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
533 md->phys_addr, md->virt_addr);
536 return err1 || err2;
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)
544 pf |= _PAGE_NX;
546 if (!(md->attribute & EFI_MEMORY_RO))
547 pf |= _PAGE_RW;
549 if (sev_active())
550 pf |= _PAGE_ENC;
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();
562 return;
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);
571 return;
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))
584 return;
586 for_each_efi_memory_desc(md) {
587 unsigned long pf = 0;
589 if (!(md->attribute & EFI_MEMORY_RUNTIME))
590 continue;
592 if (!(md->attribute & EFI_MEMORY_WB))
593 pf |= _PAGE_PCD;
595 if ((md->attribute & EFI_MEMORY_XP) ||
596 (md->type == EFI_RUNTIME_SERVICES_DATA))
597 pf |= _PAGE_NX;
599 if (!(md->attribute & EFI_MEMORY_RO) &&
600 (md->type != EFI_RUNTIME_SERVICES_CODE))
601 pf |= _PAGE_RW;
603 if (sev_active())
604 pf |= _PAGE_ENC;
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);
615 else
616 ptdump_walk_pgd_level(NULL, efi_mm.pgd);
617 #endif
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)
629 task_lock(current);
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 #define runtime_service32(func) \
640 ({ \
641 u32 table = (u32)(unsigned long)efi.systab; \
642 u32 *rt, *___f; \
644 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
645 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
646 *___f; \
650 * Switch to the EFI page tables early so that we can access the 1:1
651 * runtime services mappings which are not mapped in any other page
652 * tables. This function must be called before runtime_service32().
654 * Also, disable interrupts because the IDT points to 64-bit handlers,
655 * which aren't going to function correctly when we switch to 32-bit.
657 #define efi_thunk(f, ...) \
658 ({ \
659 efi_status_t __s; \
660 unsigned long __flags; \
661 u32 __func; \
663 local_irq_save(__flags); \
664 arch_efi_call_virt_setup(); \
666 __func = runtime_service32(f); \
667 __s = efi64_thunk(__func, __VA_ARGS__); \
669 arch_efi_call_virt_teardown(); \
670 local_irq_restore(__flags); \
672 __s; \
675 efi_status_t efi_thunk_set_virtual_address_map(
676 void *phys_set_virtual_address_map,
677 unsigned long memory_map_size,
678 unsigned long descriptor_size,
679 u32 descriptor_version,
680 efi_memory_desc_t *virtual_map)
682 efi_status_t status;
683 unsigned long flags;
684 u32 func;
686 efi_sync_low_kernel_mappings();
687 local_irq_save(flags);
689 efi_switch_mm(&efi_mm);
691 func = (u32)(unsigned long)phys_set_virtual_address_map;
692 status = efi64_thunk(func, memory_map_size, descriptor_size,
693 descriptor_version, virtual_map);
695 efi_switch_mm(efi_scratch.prev_mm);
696 local_irq_restore(flags);
698 return status;
701 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
703 efi_status_t status;
704 u32 phys_tm, phys_tc;
706 spin_lock(&rtc_lock);
708 phys_tm = virt_to_phys_or_null(tm);
709 phys_tc = virt_to_phys_or_null(tc);
711 status = efi_thunk(get_time, phys_tm, phys_tc);
713 spin_unlock(&rtc_lock);
715 return status;
718 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
720 efi_status_t status;
721 u32 phys_tm;
723 spin_lock(&rtc_lock);
725 phys_tm = virt_to_phys_or_null(tm);
727 status = efi_thunk(set_time, phys_tm);
729 spin_unlock(&rtc_lock);
731 return status;
734 static efi_status_t
735 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
736 efi_time_t *tm)
738 efi_status_t status;
739 u32 phys_enabled, phys_pending, phys_tm;
741 spin_lock(&rtc_lock);
743 phys_enabled = virt_to_phys_or_null(enabled);
744 phys_pending = virt_to_phys_or_null(pending);
745 phys_tm = virt_to_phys_or_null(tm);
747 status = efi_thunk(get_wakeup_time, phys_enabled,
748 phys_pending, phys_tm);
750 spin_unlock(&rtc_lock);
752 return status;
755 static efi_status_t
756 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
758 efi_status_t status;
759 u32 phys_tm;
761 spin_lock(&rtc_lock);
763 phys_tm = virt_to_phys_or_null(tm);
765 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
767 spin_unlock(&rtc_lock);
769 return status;
772 static unsigned long efi_name_size(efi_char16_t *name)
774 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
777 static efi_status_t
778 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
779 u32 *attr, unsigned long *data_size, void *data)
781 efi_status_t status;
782 u32 phys_name, phys_vendor, phys_attr;
783 u32 phys_data_size, phys_data;
785 phys_data_size = virt_to_phys_or_null(data_size);
786 phys_vendor = virt_to_phys_or_null(vendor);
787 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
788 phys_attr = virt_to_phys_or_null(attr);
789 phys_data = virt_to_phys_or_null_size(data, *data_size);
791 status = efi_thunk(get_variable, phys_name, phys_vendor,
792 phys_attr, phys_data_size, phys_data);
794 return status;
797 static efi_status_t
798 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
799 u32 attr, unsigned long data_size, void *data)
801 u32 phys_name, phys_vendor, phys_data;
802 efi_status_t status;
804 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
805 phys_vendor = virt_to_phys_or_null(vendor);
806 phys_data = virt_to_phys_or_null_size(data, data_size);
808 /* If data_size is > sizeof(u32) we've got problems */
809 status = efi_thunk(set_variable, phys_name, phys_vendor,
810 attr, data_size, phys_data);
812 return status;
815 static efi_status_t
816 efi_thunk_get_next_variable(unsigned long *name_size,
817 efi_char16_t *name,
818 efi_guid_t *vendor)
820 efi_status_t status;
821 u32 phys_name_size, phys_name, phys_vendor;
823 phys_name_size = virt_to_phys_or_null(name_size);
824 phys_vendor = virt_to_phys_or_null(vendor);
825 phys_name = virt_to_phys_or_null_size(name, *name_size);
827 status = efi_thunk(get_next_variable, phys_name_size,
828 phys_name, phys_vendor);
830 return status;
833 static efi_status_t
834 efi_thunk_get_next_high_mono_count(u32 *count)
836 efi_status_t status;
837 u32 phys_count;
839 phys_count = virt_to_phys_or_null(count);
840 status = efi_thunk(get_next_high_mono_count, phys_count);
842 return status;
845 static void
846 efi_thunk_reset_system(int reset_type, efi_status_t status,
847 unsigned long data_size, efi_char16_t *data)
849 u32 phys_data;
851 phys_data = virt_to_phys_or_null_size(data, data_size);
853 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
856 static efi_status_t
857 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
858 unsigned long count, unsigned long sg_list)
861 * To properly support this function we would need to repackage
862 * 'capsules' because the firmware doesn't understand 64-bit
863 * pointers.
865 return EFI_UNSUPPORTED;
868 static efi_status_t
869 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
870 u64 *remaining_space,
871 u64 *max_variable_size)
873 efi_status_t status;
874 u32 phys_storage, phys_remaining, phys_max;
876 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
877 return EFI_UNSUPPORTED;
879 phys_storage = virt_to_phys_or_null(storage_space);
880 phys_remaining = virt_to_phys_or_null(remaining_space);
881 phys_max = virt_to_phys_or_null(max_variable_size);
883 status = efi_thunk(query_variable_info, attr, phys_storage,
884 phys_remaining, phys_max);
886 return status;
889 static efi_status_t
890 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
891 unsigned long count, u64 *max_size,
892 int *reset_type)
895 * To properly support this function we would need to repackage
896 * 'capsules' because the firmware doesn't understand 64-bit
897 * pointers.
899 return EFI_UNSUPPORTED;
902 void efi_thunk_runtime_setup(void)
904 efi.get_time = efi_thunk_get_time;
905 efi.set_time = efi_thunk_set_time;
906 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
907 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
908 efi.get_variable = efi_thunk_get_variable;
909 efi.get_next_variable = efi_thunk_get_next_variable;
910 efi.set_variable = efi_thunk_set_variable;
911 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
912 efi.reset_system = efi_thunk_reset_system;
913 efi.query_variable_info = efi_thunk_query_variable_info;
914 efi.update_capsule = efi_thunk_update_capsule;
915 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
917 #endif /* CONFIG_EFI_MIXED */