GNU Linux-libre 4.4.288-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 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/mm.h>
21 #include <linux/types.h>
22 #include <linux/spinlock.h>
23 #include <linux/bootmem.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/efi.h>
27 #include <linux/uaccess.h>
28 #include <linux/io.h>
29 #include <linux/reboot.h>
30 #include <linux/slab.h>
31
32 #include <asm/setup.h>
33 #include <asm/page.h>
34 #include <asm/e820.h>
35 #include <asm/pgtable.h>
36 #include <asm/tlbflush.h>
37 #include <asm/proto.h>
38 #include <asm/efi.h>
39 #include <asm/cacheflush.h>
40 #include <asm/fixmap.h>
41 #include <asm/realmode.h>
42 #include <asm/time.h>
43 #include <asm/nospec-branch.h>
44
45 /*
46  * We allocate runtime services regions bottom-up, starting from -4G, i.e.
47  * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
48  */
49 static u64 efi_va = EFI_VA_START;
50
51 /*
52  * Scratch space used for switching the pagetable in the EFI stub
53  */
54 struct efi_scratch {
55         u64 r15;
56         u64 prev_cr3;
57         pgd_t *efi_pgt;
58         bool use_pgd;
59         u64 phys_stack;
60 } __packed;
61
62 static void __init early_code_mapping_set_exec(int executable)
63 {
64         efi_memory_desc_t *md;
65         void *p;
66
67         if (!(__supported_pte_mask & _PAGE_NX))
68                 return;
69
70         /* Make EFI service code area executable */
71         for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
72                 md = p;
73                 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
74                     md->type == EFI_BOOT_SERVICES_CODE)
75                         efi_set_executable(md, executable);
76         }
77 }
78
79 pgd_t * __init efi_call_phys_prolog(void)
80 {
81         unsigned long vaddress;
82         pgd_t *save_pgd;
83
84         int pgd;
85         int n_pgds;
86
87         if (!efi_enabled(EFI_OLD_MEMMAP))
88                 return NULL;
89
90         early_code_mapping_set_exec(1);
91
92         n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
93         save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
94
95         for (pgd = 0; pgd < n_pgds; pgd++) {
96                 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
97                 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
98                 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
99         }
100         __flush_tlb_all();
101
102         return save_pgd;
103 }
104
105 void __init efi_call_phys_epilog(pgd_t *save_pgd)
106 {
107         /*
108          * After the lock is released, the original page table is restored.
109          */
110         int pgd_idx;
111         int nr_pgds;
112
113         if (!save_pgd)
114                 return;
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 /*
128  * Add low kernel mappings for passing arguments to EFI functions.
129  */
130 void efi_sync_low_kernel_mappings(void)
131 {
132         unsigned num_pgds;
133         pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
134
135         if (efi_enabled(EFI_OLD_MEMMAP))
136                 return;
137
138         num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET);
139
140         memcpy(pgd + pgd_index(PAGE_OFFSET),
141                 init_mm.pgd + pgd_index(PAGE_OFFSET),
142                 sizeof(pgd_t) * num_pgds);
143 }
144
145 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
146 {
147         unsigned long text;
148         struct page *page;
149         unsigned npages;
150         pgd_t *pgd;
151
152         if (efi_enabled(EFI_OLD_MEMMAP))
153                 return 0;
154
155         efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
156         pgd = __va(efi_scratch.efi_pgt);
157
158         /*
159          * It can happen that the physical address of new_memmap lands in memory
160          * which is not mapped in the EFI page table. Therefore we need to go
161          * and ident-map those pages containing the map before calling
162          * phys_efi_set_virtual_address_map().
163          */
164         if (kernel_map_pages_in_pgd(pgd, pa_memmap, pa_memmap, num_pages, _PAGE_NX)) {
165                 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
166                 return 1;
167         }
168
169         efi_scratch.use_pgd = true;
170
171         /*
172          * When making calls to the firmware everything needs to be 1:1
173          * mapped and addressable with 32-bit pointers. Map the kernel
174          * text and allocate a new stack because we can't rely on the
175          * stack pointer being < 4GB.
176          */
177         if (!IS_ENABLED(CONFIG_EFI_MIXED))
178                 return 0;
179
180         page = alloc_page(GFP_KERNEL|__GFP_DMA32);
181         if (!page)
182                 panic("Unable to allocate EFI runtime stack < 4GB\n");
183
184         efi_scratch.phys_stack = virt_to_phys(page_address(page));
185         efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
186
187         npages = (_end - _text) >> PAGE_SHIFT;
188         text = __pa(_text);
189
190         if (kernel_map_pages_in_pgd(pgd, text >> PAGE_SHIFT, text, npages, 0)) {
191                 pr_err("Failed to map kernel text 1:1\n");
192                 return 1;
193         }
194
195         return 0;
196 }
197
198 void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
199 {
200         pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
201
202         kernel_unmap_pages_in_pgd(pgd, pa_memmap, num_pages);
203 }
204
205 static void __init __map_region(efi_memory_desc_t *md, u64 va)
206 {
207         pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
208         unsigned long pf = 0;
209
210         if (!(md->attribute & EFI_MEMORY_WB))
211                 pf |= _PAGE_PCD;
212
213         if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
214                 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
215                            md->phys_addr, va);
216 }
217
218 void __init efi_map_region(efi_memory_desc_t *md)
219 {
220         unsigned long size = md->num_pages << PAGE_SHIFT;
221         u64 pa = md->phys_addr;
222
223         if (efi_enabled(EFI_OLD_MEMMAP))
224                 return old_map_region(md);
225
226         /*
227          * Make sure the 1:1 mappings are present as a catch-all for b0rked
228          * firmware which doesn't update all internal pointers after switching
229          * to virtual mode and would otherwise crap on us.
230          */
231         __map_region(md, md->phys_addr);
232
233         /*
234          * Enforce the 1:1 mapping as the default virtual address when
235          * booting in EFI mixed mode, because even though we may be
236          * running a 64-bit kernel, the firmware may only be 32-bit.
237          */
238         if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
239                 md->virt_addr = md->phys_addr;
240                 return;
241         }
242
243         efi_va -= size;
244
245         /* Is PA 2M-aligned? */
246         if (!(pa & (PMD_SIZE - 1))) {
247                 efi_va &= PMD_MASK;
248         } else {
249                 u64 pa_offset = pa & (PMD_SIZE - 1);
250                 u64 prev_va = efi_va;
251
252                 /* get us the same offset within this 2M page */
253                 efi_va = (efi_va & PMD_MASK) + pa_offset;
254
255                 if (efi_va > prev_va)
256                         efi_va -= PMD_SIZE;
257         }
258
259         if (efi_va < EFI_VA_END) {
260                 pr_warn(FW_WARN "VA address range overflow!\n");
261                 return;
262         }
263
264         /* Do the VA map */
265         __map_region(md, efi_va);
266         md->virt_addr = efi_va;
267 }
268
269 /*
270  * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
271  * md->virt_addr is the original virtual address which had been mapped in kexec
272  * 1st kernel.
273  */
274 void __init efi_map_region_fixed(efi_memory_desc_t *md)
275 {
276         __map_region(md, md->virt_addr);
277 }
278
279 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
280                                  u32 type, u64 attribute)
281 {
282         unsigned long last_map_pfn;
283
284         if (type == EFI_MEMORY_MAPPED_IO)
285                 return ioremap(phys_addr, size);
286
287         last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
288         if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
289                 unsigned long top = last_map_pfn << PAGE_SHIFT;
290                 efi_ioremap(top, size - (top - phys_addr), type, attribute);
291         }
292
293         if (!(attribute & EFI_MEMORY_WB))
294                 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
295
296         return (void __iomem *)__va(phys_addr);
297 }
298
299 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
300 {
301         efi_setup = phys_addr + sizeof(struct setup_data);
302 }
303
304 void __init efi_runtime_mkexec(void)
305 {
306         if (!efi_enabled(EFI_OLD_MEMMAP))
307                 return;
308
309         if (__supported_pte_mask & _PAGE_NX)
310                 runtime_code_page_mkexec();
311 }
312
313 void __init efi_dump_pagetable(void)
314 {
315 #ifdef CONFIG_EFI_PGT_DUMP
316         pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
317
318         ptdump_walk_pgd_level(NULL, pgd);
319 #endif
320 }
321
322 #ifdef CONFIG_EFI_MIXED
323 extern efi_status_t efi64_thunk(u32, ...);
324
325 #define runtime_service32(func)                                          \
326 ({                                                                       \
327         u32 table = (u32)(unsigned long)efi.systab;                      \
328         u32 *rt, *___f;                                                  \
329                                                                          \
330         rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime));  \
331         ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
332         *___f;                                                           \
333 })
334
335 /*
336  * Switch to the EFI page tables early so that we can access the 1:1
337  * runtime services mappings which are not mapped in any other page
338  * tables. This function must be called before runtime_service32().
339  *
340  * Also, disable interrupts because the IDT points to 64-bit handlers,
341  * which aren't going to function correctly when we switch to 32-bit.
342  */
343 #define efi_thunk(f, ...)                                               \
344 ({                                                                      \
345         efi_status_t __s;                                               \
346         unsigned long flags;                                            \
347         u32 func;                                                       \
348                                                                         \
349         efi_sync_low_kernel_mappings();                                 \
350         local_irq_save(flags);                                          \
351         firmware_restrict_branch_speculation_start();                   \
352                                                                         \
353         efi_scratch.prev_cr3 = read_cr3();                              \
354         write_cr3((unsigned long)efi_scratch.efi_pgt);                  \
355         __flush_tlb_all();                                              \
356                                                                         \
357         func = runtime_service32(f);                                    \
358         __s = efi64_thunk(func, __VA_ARGS__);                   \
359                                                                         \
360         write_cr3(efi_scratch.prev_cr3);                                \
361         __flush_tlb_all();                                              \
362         firmware_restrict_branch_speculation_end();                     \
363         local_irq_restore(flags);                                       \
364                                                                         \
365         __s;                                                            \
366 })
367
368 efi_status_t efi_thunk_set_virtual_address_map(
369         void *phys_set_virtual_address_map,
370         unsigned long memory_map_size,
371         unsigned long descriptor_size,
372         u32 descriptor_version,
373         efi_memory_desc_t *virtual_map)
374 {
375         efi_status_t status;
376         unsigned long flags;
377         u32 func;
378
379         efi_sync_low_kernel_mappings();
380         local_irq_save(flags);
381
382         efi_scratch.prev_cr3 = read_cr3();
383         write_cr3((unsigned long)efi_scratch.efi_pgt);
384         __flush_tlb_all();
385
386         func = (u32)(unsigned long)phys_set_virtual_address_map;
387         status = efi64_thunk(func, memory_map_size, descriptor_size,
388                              descriptor_version, virtual_map);
389
390         write_cr3(efi_scratch.prev_cr3);
391         __flush_tlb_all();
392         local_irq_restore(flags);
393
394         return status;
395 }
396
397 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
398 {
399         efi_status_t status;
400         u32 phys_tm, phys_tc;
401
402         spin_lock(&rtc_lock);
403
404         phys_tm = virt_to_phys(tm);
405         phys_tc = virt_to_phys(tc);
406
407         status = efi_thunk(get_time, phys_tm, phys_tc);
408
409         spin_unlock(&rtc_lock);
410
411         return status;
412 }
413
414 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
415 {
416         efi_status_t status;
417         u32 phys_tm;
418
419         spin_lock(&rtc_lock);
420
421         phys_tm = virt_to_phys(tm);
422
423         status = efi_thunk(set_time, phys_tm);
424
425         spin_unlock(&rtc_lock);
426
427         return status;
428 }
429
430 static efi_status_t
431 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
432                           efi_time_t *tm)
433 {
434         efi_status_t status;
435         u32 phys_enabled, phys_pending, phys_tm;
436
437         spin_lock(&rtc_lock);
438
439         phys_enabled = virt_to_phys(enabled);
440         phys_pending = virt_to_phys(pending);
441         phys_tm = virt_to_phys(tm);
442
443         status = efi_thunk(get_wakeup_time, phys_enabled,
444                              phys_pending, phys_tm);
445
446         spin_unlock(&rtc_lock);
447
448         return status;
449 }
450
451 static efi_status_t
452 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
453 {
454         efi_status_t status;
455         u32 phys_tm;
456
457         spin_lock(&rtc_lock);
458
459         phys_tm = virt_to_phys(tm);
460
461         status = efi_thunk(set_wakeup_time, enabled, phys_tm);
462
463         spin_unlock(&rtc_lock);
464
465         return status;
466 }
467
468
469 static efi_status_t
470 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
471                        u32 *attr, unsigned long *data_size, void *data)
472 {
473         efi_status_t status;
474         u32 phys_name, phys_vendor, phys_attr;
475         u32 phys_data_size, phys_data;
476
477         phys_data_size = virt_to_phys(data_size);
478         phys_vendor = virt_to_phys(vendor);
479         phys_name = virt_to_phys(name);
480         phys_attr = virt_to_phys(attr);
481         phys_data = virt_to_phys(data);
482
483         status = efi_thunk(get_variable, phys_name, phys_vendor,
484                            phys_attr, phys_data_size, phys_data);
485
486         return status;
487 }
488
489 static efi_status_t
490 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
491                        u32 attr, unsigned long data_size, void *data)
492 {
493         u32 phys_name, phys_vendor, phys_data;
494         efi_status_t status;
495
496         phys_name = virt_to_phys(name);
497         phys_vendor = virt_to_phys(vendor);
498         phys_data = virt_to_phys(data);
499
500         /* If data_size is > sizeof(u32) we've got problems */
501         status = efi_thunk(set_variable, phys_name, phys_vendor,
502                            attr, data_size, phys_data);
503
504         return status;
505 }
506
507 static efi_status_t
508 efi_thunk_get_next_variable(unsigned long *name_size,
509                             efi_char16_t *name,
510                             efi_guid_t *vendor)
511 {
512         efi_status_t status;
513         u32 phys_name_size, phys_name, phys_vendor;
514
515         phys_name_size = virt_to_phys(name_size);
516         phys_vendor = virt_to_phys(vendor);
517         phys_name = virt_to_phys(name);
518
519         status = efi_thunk(get_next_variable, phys_name_size,
520                            phys_name, phys_vendor);
521
522         return status;
523 }
524
525 static efi_status_t
526 efi_thunk_get_next_high_mono_count(u32 *count)
527 {
528         efi_status_t status;
529         u32 phys_count;
530
531         phys_count = virt_to_phys(count);
532         status = efi_thunk(get_next_high_mono_count, phys_count);
533
534         return status;
535 }
536
537 static void
538 efi_thunk_reset_system(int reset_type, efi_status_t status,
539                        unsigned long data_size, efi_char16_t *data)
540 {
541         u32 phys_data;
542
543         phys_data = virt_to_phys(data);
544
545         efi_thunk(reset_system, reset_type, status, data_size, phys_data);
546 }
547
548 static efi_status_t
549 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
550                          unsigned long count, unsigned long sg_list)
551 {
552         /*
553          * To properly support this function we would need to repackage
554          * 'capsules' because the firmware doesn't understand 64-bit
555          * pointers.
556          */
557         return EFI_UNSUPPORTED;
558 }
559
560 static efi_status_t
561 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
562                               u64 *remaining_space,
563                               u64 *max_variable_size)
564 {
565         efi_status_t status;
566         u32 phys_storage, phys_remaining, phys_max;
567
568         if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
569                 return EFI_UNSUPPORTED;
570
571         phys_storage = virt_to_phys(storage_space);
572         phys_remaining = virt_to_phys(remaining_space);
573         phys_max = virt_to_phys(max_variable_size);
574
575         status = efi_thunk(query_variable_info, attr, phys_storage,
576                            phys_remaining, phys_max);
577
578         return status;
579 }
580
581 static efi_status_t
582 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
583                              unsigned long count, u64 *max_size,
584                              int *reset_type)
585 {
586         /*
587          * To properly support this function we would need to repackage
588          * 'capsules' because the firmware doesn't understand 64-bit
589          * pointers.
590          */
591         return EFI_UNSUPPORTED;
592 }
593
594 void efi_thunk_runtime_setup(void)
595 {
596         efi.get_time = efi_thunk_get_time;
597         efi.set_time = efi_thunk_set_time;
598         efi.get_wakeup_time = efi_thunk_get_wakeup_time;
599         efi.set_wakeup_time = efi_thunk_set_wakeup_time;
600         efi.get_variable = efi_thunk_get_variable;
601         efi.get_next_variable = efi_thunk_get_next_variable;
602         efi.set_variable = efi_thunk_set_variable;
603         efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
604         efi.reset_system = efi_thunk_reset_system;
605         efi.query_variable_info = efi_thunk_query_variable_info;
606         efi.update_capsule = efi_thunk_update_capsule;
607         efi.query_capsule_caps = efi_thunk_query_capsule_caps;
608 }
609 #endif /* CONFIG_EFI_MIXED */