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