GNU Linux-libre 4.9.308-gnu1
[releases.git] / arch / x86 / platform / efi / efi_64.c
1 /*
2  * x86_64 specific EFI support functions
3  * Based on Extensible Firmware Interface Specification version 1.0
4  *
5  * Copyright (C) 2005-2008 Intel Co.
6  *      Fenghua Yu <fenghua.yu@intel.com>
7  *      Bibo Mao <bibo.mao@intel.com>
8  *      Chandramouli Narayanan <mouli@linux.intel.com>
9  *      Huang Ying <ying.huang@intel.com>
10  *
11  * Code to convert EFI to E820 map has been implemented in elilo bootloader
12  * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
13  * is setup appropriately for EFI runtime code.
14  * - mouli 06/14/2007.
15  *
16  */
17
18 #define pr_fmt(fmt) "efi: " fmt
19
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/mm.h>
23 #include <linux/types.h>
24 #include <linux/spinlock.h>
25 #include <linux/bootmem.h>
26 #include <linux/ioport.h>
27 #include <linux/init.h>
28 #include <linux/mc146818rtc.h>
29 #include <linux/efi.h>
30 #include <linux/uaccess.h>
31 #include <linux/io.h>
32 #include <linux/reboot.h>
33 #include <linux/slab.h>
34 #include <linux/ucs2_string.h>
35
36 #include <asm/setup.h>
37 #include <asm/page.h>
38 #include <asm/e820.h>
39 #include <asm/pgtable.h>
40 #include <asm/tlbflush.h>
41 #include <asm/proto.h>
42 #include <asm/efi.h>
43 #include <asm/cacheflush.h>
44 #include <asm/fixmap.h>
45 #include <asm/realmode.h>
46 #include <asm/time.h>
47 #include <asm/pgalloc.h>
48 #include <asm/sections.h>
49
50 /*
51  * We allocate runtime services regions bottom-up, starting from -4G, i.e.
52  * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
53  */
54 static u64 efi_va = EFI_VA_START;
55
56 struct efi_scratch efi_scratch;
57
58 static void __init early_code_mapping_set_exec(int executable)
59 {
60         efi_memory_desc_t *md;
61
62         if (!(__supported_pte_mask & _PAGE_NX))
63                 return;
64
65         /* Make EFI service code area executable */
66         for_each_efi_memory_desc(md) {
67                 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
68                     md->type == EFI_BOOT_SERVICES_CODE)
69                         efi_set_executable(md, executable);
70         }
71 }
72
73 pgd_t * __init efi_call_phys_prolog(void)
74 {
75         unsigned long vaddress;
76         pgd_t *save_pgd;
77
78         int pgd;
79         int n_pgds;
80
81         if (!efi_enabled(EFI_OLD_MEMMAP)) {
82                 save_pgd = (pgd_t *)read_cr3();
83                 write_cr3((unsigned long)efi_scratch.efi_pgt);
84                 goto out;
85         }
86
87         early_code_mapping_set_exec(1);
88
89         n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
90         save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
91
92         for (pgd = 0; pgd < n_pgds; pgd++) {
93                 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
94                 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
95                 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
96         }
97 out:
98         __flush_tlb_all();
99
100         return save_pgd;
101 }
102
103 void __init efi_call_phys_epilog(pgd_t *save_pgd)
104 {
105         /*
106          * After the lock is released, the original page table is restored.
107          */
108         int pgd_idx;
109         int nr_pgds;
110
111         if (!efi_enabled(EFI_OLD_MEMMAP)) {
112                 write_cr3((unsigned long)save_pgd);
113                 __flush_tlb_all();
114                 return;
115         }
116
117         nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
118
119         for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++)
120                 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
121
122         kfree(save_pgd);
123
124         __flush_tlb_all();
125         early_code_mapping_set_exec(0);
126 }
127
128 static pgd_t *efi_pgd;
129
130 /*
131  * We need our own copy of the higher levels of the page tables
132  * because we want to avoid inserting EFI region mappings (EFI_VA_END
133  * to EFI_VA_START) into the standard kernel page tables. Everything
134  * else can be shared, see efi_sync_low_kernel_mappings().
135  */
136 int __init efi_alloc_page_tables(void)
137 {
138         pgd_t *pgd;
139         pud_t *pud;
140         gfp_t gfp_mask;
141
142         if (efi_enabled(EFI_OLD_MEMMAP))
143                 return 0;
144
145         gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
146         efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
147         if (!efi_pgd)
148                 return -ENOMEM;
149
150         pgd = efi_pgd + pgd_index(EFI_VA_END);
151
152         pud = pud_alloc_one(NULL, 0);
153         if (!pud) {
154                 free_page((unsigned long)efi_pgd);
155                 return -ENOMEM;
156         }
157
158         pgd_populate(NULL, pgd, pud);
159
160         return 0;
161 }
162
163 /*
164  * Add low kernel mappings for passing arguments to EFI functions.
165  */
166 void efi_sync_low_kernel_mappings(void)
167 {
168         unsigned num_entries;
169         pgd_t *pgd_k, *pgd_efi;
170         pud_t *pud_k, *pud_efi;
171
172         if (efi_enabled(EFI_OLD_MEMMAP))
173                 return;
174
175         /*
176          * We can share all PGD entries apart from the one entry that
177          * covers the EFI runtime mapping space.
178          *
179          * Make sure the EFI runtime region mappings are guaranteed to
180          * only span a single PGD entry and that the entry also maps
181          * other important kernel regions.
182          */
183         BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
184         BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
185                         (EFI_VA_END & PGDIR_MASK));
186
187         pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
188         pgd_k = pgd_offset_k(PAGE_OFFSET);
189
190         num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
191         memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
192
193         /*
194          * We share all the PUD entries apart from those that map the
195          * EFI regions. Copy around them.
196          */
197         BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
198         BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
199
200         pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
201         pud_efi = pud_offset(pgd_efi, 0);
202
203         pgd_k = pgd_offset_k(EFI_VA_END);
204         pud_k = pud_offset(pgd_k, 0);
205
206         num_entries = pud_index(EFI_VA_END);
207         memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
208
209         pud_efi = pud_offset(pgd_efi, EFI_VA_START);
210         pud_k = pud_offset(pgd_k, EFI_VA_START);
211
212         num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
213         memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
214 }
215
216 /*
217  * Wrapper for slow_virt_to_phys() that handles NULL addresses.
218  */
219 static inline phys_addr_t
220 virt_to_phys_or_null_size(void *va, unsigned long size)
221 {
222         bool bad_size;
223
224         if (!va)
225                 return 0;
226
227         if (virt_addr_valid(va))
228                 return virt_to_phys(va);
229
230         /*
231          * A fully aligned variable on the stack is guaranteed not to
232          * cross a page bounary. Try to catch strings on the stack by
233          * checking that 'size' is a power of two.
234          */
235         bad_size = size > PAGE_SIZE || !is_power_of_2(size);
236
237         WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
238
239         return slow_virt_to_phys(va);
240 }
241
242 #define virt_to_phys_or_null(addr)                              \
243         virt_to_phys_or_null_size((addr), sizeof(*(addr)))
244
245 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
246 {
247         unsigned long pfn, text;
248         struct page *page;
249         unsigned npages;
250         pgd_t *pgd;
251
252         if (efi_enabled(EFI_OLD_MEMMAP))
253                 return 0;
254
255         efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd);
256         pgd = efi_pgd;
257
258         /*
259          * It can happen that the physical address of new_memmap lands in memory
260          * which is not mapped in the EFI page table. Therefore we need to go
261          * and ident-map those pages containing the map before calling
262          * phys_efi_set_virtual_address_map().
263          */
264         pfn = pa_memmap >> PAGE_SHIFT;
265         if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, _PAGE_NX | _PAGE_RW)) {
266                 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
267                 return 1;
268         }
269
270         efi_scratch.use_pgd = true;
271
272         /*
273          * Certain firmware versions are way too sentimential and still believe
274          * they are exclusive and unquestionable owners of the first physical page,
275          * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
276          * (but then write-access it later during SetVirtualAddressMap()).
277          *
278          * Create a 1:1 mapping for this page, to avoid triple faults during early
279          * boot with such firmware. We are free to hand this page to the BIOS,
280          * as trim_bios_range() will reserve the first page and isolate it away
281          * from memory allocators anyway.
282          */
283         if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, _PAGE_RW)) {
284                 pr_err("Failed to create 1:1 mapping for the first page!\n");
285                 return 1;
286         }
287
288         /*
289          * When making calls to the firmware everything needs to be 1:1
290          * mapped and addressable with 32-bit pointers. Map the kernel
291          * text and allocate a new stack because we can't rely on the
292          * stack pointer being < 4GB.
293          */
294         if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
295                 return 0;
296
297         page = alloc_page(GFP_KERNEL|__GFP_DMA32);
298         if (!page)
299                 panic("Unable to allocate EFI runtime stack < 4GB\n");
300
301         efi_scratch.phys_stack = virt_to_phys(page_address(page));
302         efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
303
304         npages = (_etext - _text) >> PAGE_SHIFT;
305         text = __pa(_text);
306         pfn = text >> PAGE_SHIFT;
307
308         if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
309                 pr_err("Failed to map kernel text 1:1\n");
310                 return 1;
311         }
312
313         return 0;
314 }
315
316 static void __init __map_region(efi_memory_desc_t *md, u64 va)
317 {
318         unsigned long flags = _PAGE_RW;
319         unsigned long pfn;
320         pgd_t *pgd = efi_pgd;
321
322         if (!(md->attribute & EFI_MEMORY_WB))
323                 flags |= _PAGE_PCD;
324
325         pfn = md->phys_addr >> PAGE_SHIFT;
326         if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
327                 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
328                            md->phys_addr, va);
329 }
330
331 void __init efi_map_region(efi_memory_desc_t *md)
332 {
333         unsigned long size = md->num_pages << PAGE_SHIFT;
334         u64 pa = md->phys_addr;
335
336         if (efi_enabled(EFI_OLD_MEMMAP))
337                 return old_map_region(md);
338
339         /*
340          * Make sure the 1:1 mappings are present as a catch-all for b0rked
341          * firmware which doesn't update all internal pointers after switching
342          * to virtual mode and would otherwise crap on us.
343          */
344         __map_region(md, md->phys_addr);
345
346         /*
347          * Enforce the 1:1 mapping as the default virtual address when
348          * booting in EFI mixed mode, because even though we may be
349          * running a 64-bit kernel, the firmware may only be 32-bit.
350          */
351         if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
352                 md->virt_addr = md->phys_addr;
353                 return;
354         }
355
356         efi_va -= size;
357
358         /* Is PA 2M-aligned? */
359         if (!(pa & (PMD_SIZE - 1))) {
360                 efi_va &= PMD_MASK;
361         } else {
362                 u64 pa_offset = pa & (PMD_SIZE - 1);
363                 u64 prev_va = efi_va;
364
365                 /* get us the same offset within this 2M page */
366                 efi_va = (efi_va & PMD_MASK) + pa_offset;
367
368                 if (efi_va > prev_va)
369                         efi_va -= PMD_SIZE;
370         }
371
372         if (efi_va < EFI_VA_END) {
373                 pr_warn(FW_WARN "VA address range overflow!\n");
374                 return;
375         }
376
377         /* Do the VA map */
378         __map_region(md, efi_va);
379         md->virt_addr = efi_va;
380 }
381
382 /*
383  * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
384  * md->virt_addr is the original virtual address which had been mapped in kexec
385  * 1st kernel.
386  */
387 void __init efi_map_region_fixed(efi_memory_desc_t *md)
388 {
389         __map_region(md, md->phys_addr);
390         __map_region(md, md->virt_addr);
391 }
392
393 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
394                                  u32 type, u64 attribute)
395 {
396         unsigned long last_map_pfn;
397
398         if (type == EFI_MEMORY_MAPPED_IO)
399                 return ioremap(phys_addr, size);
400
401         last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
402         if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
403                 unsigned long top = last_map_pfn << PAGE_SHIFT;
404                 efi_ioremap(top, size - (top - phys_addr), type, attribute);
405         }
406
407         if (!(attribute & EFI_MEMORY_WB))
408                 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
409
410         return (void __iomem *)__va(phys_addr);
411 }
412
413 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
414 {
415         efi_setup = phys_addr + sizeof(struct setup_data);
416 }
417
418 void __init efi_runtime_update_mappings(void)
419 {
420         unsigned long pfn;
421         pgd_t *pgd = efi_pgd;
422         efi_memory_desc_t *md;
423
424         if (efi_enabled(EFI_OLD_MEMMAP)) {
425                 if (__supported_pte_mask & _PAGE_NX)
426                         runtime_code_page_mkexec();
427                 return;
428         }
429
430         if (!efi_enabled(EFI_NX_PE_DATA))
431                 return;
432
433         for_each_efi_memory_desc(md) {
434                 unsigned long pf = 0;
435
436                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
437                         continue;
438
439                 if (!(md->attribute & EFI_MEMORY_WB))
440                         pf |= _PAGE_PCD;
441
442                 if ((md->attribute & EFI_MEMORY_XP) ||
443                         (md->type == EFI_RUNTIME_SERVICES_DATA))
444                         pf |= _PAGE_NX;
445
446                 if (!(md->attribute & EFI_MEMORY_RO) &&
447                         (md->type != EFI_RUNTIME_SERVICES_CODE))
448                         pf |= _PAGE_RW;
449
450                 /* Update the 1:1 mapping */
451                 pfn = md->phys_addr >> PAGE_SHIFT;
452                 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf))
453                         pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
454                                    md->phys_addr, md->virt_addr);
455
456                 if (kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf))
457                         pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
458                                    md->phys_addr, md->virt_addr);
459         }
460 }
461
462 void __init efi_dump_pagetable(void)
463 {
464 #ifdef CONFIG_EFI_PGT_DUMP
465         ptdump_walk_pgd_level(NULL, efi_pgd);
466 #endif
467 }
468
469 #ifdef CONFIG_EFI_MIXED
470 extern efi_status_t efi64_thunk(u32, ...);
471
472 #define runtime_service32(func)                                          \
473 ({                                                                       \
474         u32 table = (u32)(unsigned long)efi.systab;                      \
475         u32 *rt, *___f;                                                  \
476                                                                          \
477         rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime));  \
478         ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
479         *___f;                                                           \
480 })
481
482 /*
483  * Switch to the EFI page tables early so that we can access the 1:1
484  * runtime services mappings which are not mapped in any other page
485  * tables. This function must be called before runtime_service32().
486  *
487  * Also, disable interrupts because the IDT points to 64-bit handlers,
488  * which aren't going to function correctly when we switch to 32-bit.
489  */
490 #define efi_thunk(f, ...)                                               \
491 ({                                                                      \
492         efi_status_t __s;                                               \
493         unsigned long __flags;                                          \
494         u32 __func;                                                     \
495                                                                         \
496         local_irq_save(__flags);                                        \
497         arch_efi_call_virt_setup();                                     \
498                                                                         \
499         __func = runtime_service32(f);                                  \
500         __s = efi64_thunk(__func, __VA_ARGS__);                         \
501                                                                         \
502         arch_efi_call_virt_teardown();                                  \
503         local_irq_restore(__flags);                                     \
504                                                                         \
505         __s;                                                            \
506 })
507
508 efi_status_t efi_thunk_set_virtual_address_map(
509         void *phys_set_virtual_address_map,
510         unsigned long memory_map_size,
511         unsigned long descriptor_size,
512         u32 descriptor_version,
513         efi_memory_desc_t *virtual_map)
514 {
515         efi_status_t status;
516         unsigned long flags;
517         u32 func;
518
519         efi_sync_low_kernel_mappings();
520         local_irq_save(flags);
521
522         efi_scratch.prev_cr3 = read_cr3();
523         write_cr3((unsigned long)efi_scratch.efi_pgt);
524         __flush_tlb_all();
525
526         func = (u32)(unsigned long)phys_set_virtual_address_map;
527         status = efi64_thunk(func, memory_map_size, descriptor_size,
528                              descriptor_version, virtual_map);
529
530         write_cr3(efi_scratch.prev_cr3);
531         __flush_tlb_all();
532         local_irq_restore(flags);
533
534         return status;
535 }
536
537 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
538 {
539         efi_status_t status;
540         u32 phys_tm, phys_tc;
541
542         spin_lock(&rtc_lock);
543
544         phys_tm = virt_to_phys_or_null(tm);
545         phys_tc = virt_to_phys_or_null(tc);
546
547         status = efi_thunk(get_time, phys_tm, phys_tc);
548
549         spin_unlock(&rtc_lock);
550
551         return status;
552 }
553
554 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
555 {
556         efi_status_t status;
557         u32 phys_tm;
558
559         spin_lock(&rtc_lock);
560
561         phys_tm = virt_to_phys_or_null(tm);
562
563         status = efi_thunk(set_time, phys_tm);
564
565         spin_unlock(&rtc_lock);
566
567         return status;
568 }
569
570 static efi_status_t
571 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
572                           efi_time_t *tm)
573 {
574         efi_status_t status;
575         u32 phys_enabled, phys_pending, phys_tm;
576
577         spin_lock(&rtc_lock);
578
579         phys_enabled = virt_to_phys_or_null(enabled);
580         phys_pending = virt_to_phys_or_null(pending);
581         phys_tm = virt_to_phys_or_null(tm);
582
583         status = efi_thunk(get_wakeup_time, phys_enabled,
584                              phys_pending, phys_tm);
585
586         spin_unlock(&rtc_lock);
587
588         return status;
589 }
590
591 static efi_status_t
592 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
593 {
594         efi_status_t status;
595         u32 phys_tm;
596
597         spin_lock(&rtc_lock);
598
599         phys_tm = virt_to_phys_or_null(tm);
600
601         status = efi_thunk(set_wakeup_time, enabled, phys_tm);
602
603         spin_unlock(&rtc_lock);
604
605         return status;
606 }
607
608 static unsigned long efi_name_size(efi_char16_t *name)
609 {
610         return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
611 }
612
613 static efi_status_t
614 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
615                        u32 *attr, unsigned long *data_size, void *data)
616 {
617         efi_status_t status;
618         u32 phys_name, phys_vendor, phys_attr;
619         u32 phys_data_size, phys_data;
620
621         phys_data_size = virt_to_phys_or_null(data_size);
622         phys_vendor = virt_to_phys_or_null(vendor);
623         phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
624         phys_attr = virt_to_phys_or_null(attr);
625         phys_data = virt_to_phys_or_null_size(data, *data_size);
626
627         status = efi_thunk(get_variable, phys_name, phys_vendor,
628                            phys_attr, phys_data_size, phys_data);
629
630         return status;
631 }
632
633 static efi_status_t
634 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
635                        u32 attr, unsigned long data_size, void *data)
636 {
637         u32 phys_name, phys_vendor, phys_data;
638         efi_status_t status;
639
640         phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
641         phys_vendor = virt_to_phys_or_null(vendor);
642         phys_data = virt_to_phys_or_null_size(data, data_size);
643
644         /* If data_size is > sizeof(u32) we've got problems */
645         status = efi_thunk(set_variable, phys_name, phys_vendor,
646                            attr, data_size, phys_data);
647
648         return status;
649 }
650
651 static efi_status_t
652 efi_thunk_get_next_variable(unsigned long *name_size,
653                             efi_char16_t *name,
654                             efi_guid_t *vendor)
655 {
656         efi_status_t status;
657         u32 phys_name_size, phys_name, phys_vendor;
658
659         phys_name_size = virt_to_phys_or_null(name_size);
660         phys_vendor = virt_to_phys_or_null(vendor);
661         phys_name = virt_to_phys_or_null_size(name, *name_size);
662
663         status = efi_thunk(get_next_variable, phys_name_size,
664                            phys_name, phys_vendor);
665
666         return status;
667 }
668
669 static efi_status_t
670 efi_thunk_get_next_high_mono_count(u32 *count)
671 {
672         efi_status_t status;
673         u32 phys_count;
674
675         phys_count = virt_to_phys_or_null(count);
676         status = efi_thunk(get_next_high_mono_count, phys_count);
677
678         return status;
679 }
680
681 static void
682 efi_thunk_reset_system(int reset_type, efi_status_t status,
683                        unsigned long data_size, efi_char16_t *data)
684 {
685         u32 phys_data;
686
687         phys_data = virt_to_phys_or_null_size(data, data_size);
688
689         efi_thunk(reset_system, reset_type, status, data_size, phys_data);
690 }
691
692 static efi_status_t
693 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
694                          unsigned long count, unsigned long sg_list)
695 {
696         /*
697          * To properly support this function we would need to repackage
698          * 'capsules' because the firmware doesn't understand 64-bit
699          * pointers.
700          */
701         return EFI_UNSUPPORTED;
702 }
703
704 static efi_status_t
705 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
706                               u64 *remaining_space,
707                               u64 *max_variable_size)
708 {
709         efi_status_t status;
710         u32 phys_storage, phys_remaining, phys_max;
711
712         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
713                 return EFI_UNSUPPORTED;
714
715         phys_storage = virt_to_phys_or_null(storage_space);
716         phys_remaining = virt_to_phys_or_null(remaining_space);
717         phys_max = virt_to_phys_or_null(max_variable_size);
718
719         status = efi_thunk(query_variable_info, attr, phys_storage,
720                            phys_remaining, phys_max);
721
722         return status;
723 }
724
725 static efi_status_t
726 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
727                              unsigned long count, u64 *max_size,
728                              int *reset_type)
729 {
730         /*
731          * To properly support this function we would need to repackage
732          * 'capsules' because the firmware doesn't understand 64-bit
733          * pointers.
734          */
735         return EFI_UNSUPPORTED;
736 }
737
738 void efi_thunk_runtime_setup(void)
739 {
740         efi.get_time = efi_thunk_get_time;
741         efi.set_time = efi_thunk_set_time;
742         efi.get_wakeup_time = efi_thunk_get_wakeup_time;
743         efi.set_wakeup_time = efi_thunk_set_wakeup_time;
744         efi.get_variable = efi_thunk_get_variable;
745         efi.get_next_variable = efi_thunk_get_next_variable;
746         efi.set_variable = efi_thunk_set_variable;
747         efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
748         efi.reset_system = efi_thunk_reset_system;
749         efi.query_variable_info = efi_thunk_query_variable_info;
750         efi.update_capsule = efi_thunk_update_capsule;
751         efi.query_capsule_caps = efi_thunk_query_capsule_caps;
752 }
753 #endif /* CONFIG_EFI_MIXED */