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