blob: 5cb4301c4dcfcfd1fe7a906c242ec8632c035fc7 [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
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
Matt Fleming26d7f652015-10-25 10:26:35 +000018#define pr_fmt(fmt) "efi: " fmt
19
Huang, Ying5b836832008-01-30 13:31:19 +010020#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/module.h>
28#include <linux/efi.h>
29#include <linux/uaccess.h>
30#include <linux/io.h>
31#include <linux/reboot.h>
David Howells0d01ff22013-04-11 23:51:01 +010032#include <linux/slab.h>
Huang, Ying5b836832008-01-30 13:31:19 +010033
34#include <asm/setup.h>
35#include <asm/page.h>
36#include <asm/e820.h>
37#include <asm/pgtable.h>
38#include <asm/tlbflush.h>
Huang, Ying5b836832008-01-30 13:31:19 +010039#include <asm/proto.h>
40#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080041#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090042#include <asm/fixmap.h>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010043#include <asm/realmode.h>
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000044#include <asm/time.h>
Matt Fleming67a91082015-11-27 21:09:34 +000045#include <asm/pgalloc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010046
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010047/*
48 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
49 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
50 */
Mathias Krause8266e312014-09-21 17:26:54 +020051static u64 efi_va = EFI_VA_START;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010052
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000053struct efi_scratch efi_scratch;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010054
Matthew Garrett9cd2b072011-05-05 15:19:43 -040055static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010056{
57 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +010058
Huang, Yinga2172e22008-01-30 13:33:55 +010059 if (!(__supported_pte_mask & _PAGE_NX))
60 return;
61
Matthew Garrett916f6762011-05-25 09:53:13 -040062 /* Make EFI service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +010063 for_each_efi_memory_desc(md) {
Matthew Garrett916f6762011-05-25 09:53:13 -040064 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
65 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040066 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010067 }
68}
69
Ingo Molnar744937b2015-03-03 07:48:50 +010070pgd_t * __init efi_call_phys_prolog(void)
Huang, Ying5b836832008-01-30 13:31:19 +010071{
72 unsigned long vaddress;
Ingo Molnar744937b2015-03-03 07:48:50 +010073 pgd_t *save_pgd;
74
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060075 int pgd;
76 int n_pgds;
Huang, Ying5b836832008-01-30 13:31:19 +010077
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000078 if (!efi_enabled(EFI_OLD_MEMMAP)) {
79 save_pgd = (pgd_t *)read_cr3();
80 write_cr3((unsigned long)efi_scratch.efi_pgt);
81 goto out;
82 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010083
Matthew Garrett9cd2b072011-05-05 15:19:43 -040084 early_code_mapping_set_exec(1);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060085
86 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
87 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
88
89 for (pgd = 0; pgd < n_pgds; pgd++) {
90 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
91 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
92 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
93 }
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000094out:
Huang, Ying5b836832008-01-30 13:31:19 +010095 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +010096
97 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +010098}
99
Ingo Molnar744937b2015-03-03 07:48:50 +0100100void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100101{
102 /*
103 * After the lock is released, the original page table is restored.
104 */
Ingo Molnar744937b2015-03-03 07:48:50 +0100105 int pgd_idx;
106 int nr_pgds;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100107
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000108 if (!efi_enabled(EFI_OLD_MEMMAP)) {
109 write_cr3((unsigned long)save_pgd);
110 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100111 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000112 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100113
Ingo Molnar744937b2015-03-03 07:48:50 +0100114 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
115
116 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++)
117 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
118
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600119 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100120
Huang, Ying5b836832008-01-30 13:31:19 +0100121 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400122 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100123}
Keith Packarde1ad7832011-12-11 16:12:42 -0800124
Matt Fleming67a91082015-11-27 21:09:34 +0000125static pgd_t *efi_pgd;
126
127/*
128 * We need our own copy of the higher levels of the page tables
129 * because we want to avoid inserting EFI region mappings (EFI_VA_END
130 * to EFI_VA_START) into the standard kernel page tables. Everything
131 * else can be shared, see efi_sync_low_kernel_mappings().
132 */
133int __init efi_alloc_page_tables(void)
134{
135 pgd_t *pgd;
136 pud_t *pud;
137 gfp_t gfp_mask;
138
139 if (efi_enabled(EFI_OLD_MEMMAP))
140 return 0;
141
Michal Hockof58f2302016-06-24 14:48:53 -0700142 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
Matt Fleming67a91082015-11-27 21:09:34 +0000143 efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
144 if (!efi_pgd)
145 return -ENOMEM;
146
147 pgd = efi_pgd + pgd_index(EFI_VA_END);
148
149 pud = pud_alloc_one(NULL, 0);
150 if (!pud) {
151 free_page((unsigned long)efi_pgd);
152 return -ENOMEM;
153 }
154
155 pgd_populate(NULL, pgd, pud);
156
157 return 0;
158}
159
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100160/*
161 * Add low kernel mappings for passing arguments to EFI functions.
162 */
163void efi_sync_low_kernel_mappings(void)
164{
Matt Fleming67a91082015-11-27 21:09:34 +0000165 unsigned num_entries;
166 pgd_t *pgd_k, *pgd_efi;
167 pud_t *pud_k, *pud_efi;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100168
169 if (efi_enabled(EFI_OLD_MEMMAP))
170 return;
171
Matt Fleming67a91082015-11-27 21:09:34 +0000172 /*
173 * We can share all PGD entries apart from the one entry that
174 * covers the EFI runtime mapping space.
175 *
176 * Make sure the EFI runtime region mappings are guaranteed to
177 * only span a single PGD entry and that the entry also maps
178 * other important kernel regions.
179 */
180 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
181 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
182 (EFI_VA_END & PGDIR_MASK));
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100183
Matt Fleming67a91082015-11-27 21:09:34 +0000184 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
185 pgd_k = pgd_offset_k(PAGE_OFFSET);
186
187 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
188 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
189
190 /*
191 * We share all the PUD entries apart from those that map the
192 * EFI regions. Copy around them.
193 */
194 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
195 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
196
197 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
198 pud_efi = pud_offset(pgd_efi, 0);
199
200 pgd_k = pgd_offset_k(EFI_VA_END);
201 pud_k = pud_offset(pgd_k, 0);
202
203 num_entries = pud_index(EFI_VA_END);
204 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
205
206 pud_efi = pud_offset(pgd_efi, EFI_VA_START);
207 pud_k = pud_offset(pgd_k, EFI_VA_START);
208
209 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
210 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100211}
212
Mathias Krause4e78eb052014-09-07 19:42:17 +0200213int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100214{
Matt Flemingedc3b912015-11-27 21:09:31 +0000215 unsigned long pfn, text;
Matt Flemingb61a76f2015-11-27 21:09:32 +0000216 efi_memory_desc_t *md;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000217 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000218 unsigned npages;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100219 pgd_t *pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100220
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100221 if (efi_enabled(EFI_OLD_MEMMAP))
222 return 0;
223
Matt Fleming67a91082015-11-27 21:09:34 +0000224 efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd);
225 pgd = efi_pgd;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100226
227 /*
228 * It can happen that the physical address of new_memmap lands in memory
229 * which is not mapped in the EFI page table. Therefore we need to go
230 * and ident-map those pages containing the map before calling
231 * phys_efi_set_virtual_address_map().
232 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000233 pfn = pa_memmap >> PAGE_SHIFT;
Sai Praneeth15f003d2016-02-17 12:36:04 +0000234 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, _PAGE_NX | _PAGE_RW)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100235 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
236 return 1;
237 }
238
239 efi_scratch.use_pgd = true;
240
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000241 /*
242 * When making calls to the firmware everything needs to be 1:1
243 * mapped and addressable with 32-bit pointers. Map the kernel
244 * text and allocate a new stack because we can't rely on the
245 * stack pointer being < 4GB.
246 */
247 if (!IS_ENABLED(CONFIG_EFI_MIXED))
Matt Fleming994448f2014-03-05 18:15:37 +0000248 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000249
Matt Flemingb61a76f2015-11-27 21:09:32 +0000250 /*
251 * Map all of RAM so that we can access arguments in the 1:1
252 * mapping when making EFI runtime calls.
253 */
Matt Fleming78ce2482016-04-25 21:06:38 +0100254 for_each_efi_memory_desc(md) {
Matt Flemingb61a76f2015-11-27 21:09:32 +0000255 if (md->type != EFI_CONVENTIONAL_MEMORY &&
256 md->type != EFI_LOADER_DATA &&
257 md->type != EFI_LOADER_CODE)
258 continue;
259
260 pfn = md->phys_addr >> PAGE_SHIFT;
261 npages = md->num_pages;
262
Sai Praneeth15f003d2016-02-17 12:36:04 +0000263 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, npages, _PAGE_RW)) {
Matt Flemingb61a76f2015-11-27 21:09:32 +0000264 pr_err("Failed to map 1:1 memory\n");
265 return 1;
266 }
267 }
268
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000269 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
270 if (!page)
271 panic("Unable to allocate EFI runtime stack < 4GB\n");
272
273 efi_scratch.phys_stack = virt_to_phys(page_address(page));
274 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
275
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000276 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000277 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000278 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000279
Sai Praneeth15f003d2016-02-17 12:36:04 +0000280 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000281 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000282 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000283 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100284
285 return 0;
286}
287
Mathias Krause4e78eb052014-09-07 19:42:17 +0200288void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100289{
Matt Fleming67a91082015-11-27 21:09:34 +0000290 kernel_unmap_pages_in_pgd(efi_pgd, pa_memmap, num_pages);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100291}
292
293static void __init __map_region(efi_memory_desc_t *md, u64 va)
294{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000295 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000296 unsigned long pfn;
Matt Fleming67a91082015-11-27 21:09:34 +0000297 pgd_t *pgd = efi_pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100298
299 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000300 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100301
Matt Flemingedc3b912015-11-27 21:09:31 +0000302 pfn = md->phys_addr >> PAGE_SHIFT;
303 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100304 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
305 md->phys_addr, va);
306}
307
308void __init efi_map_region(efi_memory_desc_t *md)
309{
310 unsigned long size = md->num_pages << PAGE_SHIFT;
311 u64 pa = md->phys_addr;
312
313 if (efi_enabled(EFI_OLD_MEMMAP))
314 return old_map_region(md);
315
316 /*
317 * Make sure the 1:1 mappings are present as a catch-all for b0rked
318 * firmware which doesn't update all internal pointers after switching
319 * to virtual mode and would otherwise crap on us.
320 */
321 __map_region(md, md->phys_addr);
322
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000323 /*
324 * Enforce the 1:1 mapping as the default virtual address when
325 * booting in EFI mixed mode, because even though we may be
326 * running a 64-bit kernel, the firmware may only be 32-bit.
327 */
328 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
329 md->virt_addr = md->phys_addr;
330 return;
331 }
332
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100333 efi_va -= size;
334
335 /* Is PA 2M-aligned? */
336 if (!(pa & (PMD_SIZE - 1))) {
337 efi_va &= PMD_MASK;
338 } else {
339 u64 pa_offset = pa & (PMD_SIZE - 1);
340 u64 prev_va = efi_va;
341
342 /* get us the same offset within this 2M page */
343 efi_va = (efi_va & PMD_MASK) + pa_offset;
344
345 if (efi_va > prev_va)
346 efi_va -= PMD_SIZE;
347 }
348
349 if (efi_va < EFI_VA_END) {
350 pr_warn(FW_WARN "VA address range overflow!\n");
351 return;
352 }
353
354 /* Do the VA map */
355 __map_region(md, efi_va);
356 md->virt_addr = efi_va;
357}
358
Dave Young3b266492013-12-20 18:02:14 +0800359/*
360 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
361 * md->virt_addr is the original virtual address which had been mapped in kexec
362 * 1st kernel.
363 */
364void __init efi_map_region_fixed(efi_memory_desc_t *md)
365{
366 __map_region(md, md->virt_addr);
367}
368
Keith Packarde1ad7832011-12-11 16:12:42 -0800369void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100370 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800371{
372 unsigned long last_map_pfn;
373
374 if (type == EFI_MEMORY_MAPPED_IO)
375 return ioremap(phys_addr, size);
376
377 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
378 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
379 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100380 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800381 }
382
Matt Fleming3e8fa262012-10-19 13:25:46 +0100383 if (!(attribute & EFI_MEMORY_WB))
384 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
385
Keith Packarde1ad7832011-12-11 16:12:42 -0800386 return (void __iomem *)__va(phys_addr);
387}
Dave Young1fec0532013-12-20 18:02:19 +0800388
389void __init parse_efi_setup(u64 phys_addr, u32 data_len)
390{
391 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800392}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100393
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000394void __init efi_runtime_update_mappings(void)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100395{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000396 unsigned long pfn;
397 pgd_t *pgd = efi_pgd;
398 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000399
400 if (efi_enabled(EFI_OLD_MEMMAP)) {
401 if (__supported_pte_mask & _PAGE_NX)
402 runtime_code_page_mkexec();
403 return;
404 }
405
406 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100407 return;
408
Matt Fleming78ce2482016-04-25 21:06:38 +0100409 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000410 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000411
412 if (!(md->attribute & EFI_MEMORY_RUNTIME))
413 continue;
414
415 if (!(md->attribute & EFI_MEMORY_WB))
416 pf |= _PAGE_PCD;
417
418 if ((md->attribute & EFI_MEMORY_XP) ||
419 (md->type == EFI_RUNTIME_SERVICES_DATA))
420 pf |= _PAGE_NX;
421
422 if (!(md->attribute & EFI_MEMORY_RO) &&
423 (md->type != EFI_RUNTIME_SERVICES_CODE))
424 pf |= _PAGE_RW;
425
426 /* Update the 1:1 mapping */
427 pfn = md->phys_addr >> PAGE_SHIFT;
428 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf))
429 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
430 md->phys_addr, md->virt_addr);
431
432 if (kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf))
433 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
434 md->phys_addr, md->virt_addr);
435 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100436}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100437
438void __init efi_dump_pagetable(void)
439{
440#ifdef CONFIG_EFI_PGT_DUMP
Matt Fleming67a91082015-11-27 21:09:34 +0000441 ptdump_walk_pgd_level(NULL, efi_pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100442#endif
443}
Matt Fleming994448f2014-03-05 18:15:37 +0000444
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000445#ifdef CONFIG_EFI_MIXED
446extern efi_status_t efi64_thunk(u32, ...);
447
448#define runtime_service32(func) \
449({ \
450 u32 table = (u32)(unsigned long)efi.systab; \
451 u32 *rt, *___f; \
452 \
453 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
454 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
455 *___f; \
456})
457
458/*
459 * Switch to the EFI page tables early so that we can access the 1:1
460 * runtime services mappings which are not mapped in any other page
461 * tables. This function must be called before runtime_service32().
462 *
463 * Also, disable interrupts because the IDT points to 64-bit handlers,
464 * which aren't going to function correctly when we switch to 32-bit.
465 */
466#define efi_thunk(f, ...) \
467({ \
468 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100469 unsigned long __flags; \
470 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000471 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100472 local_irq_save(__flags); \
473 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000474 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100475 __func = runtime_service32(f); \
476 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000477 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100478 arch_efi_call_virt_teardown(); \
479 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000480 \
481 __s; \
482})
483
484efi_status_t efi_thunk_set_virtual_address_map(
485 void *phys_set_virtual_address_map,
486 unsigned long memory_map_size,
487 unsigned long descriptor_size,
488 u32 descriptor_version,
489 efi_memory_desc_t *virtual_map)
490{
491 efi_status_t status;
492 unsigned long flags;
493 u32 func;
494
495 efi_sync_low_kernel_mappings();
496 local_irq_save(flags);
497
498 efi_scratch.prev_cr3 = read_cr3();
499 write_cr3((unsigned long)efi_scratch.efi_pgt);
500 __flush_tlb_all();
501
502 func = (u32)(unsigned long)phys_set_virtual_address_map;
503 status = efi64_thunk(func, memory_map_size, descriptor_size,
504 descriptor_version, virtual_map);
505
506 write_cr3(efi_scratch.prev_cr3);
507 __flush_tlb_all();
508 local_irq_restore(flags);
509
510 return status;
511}
512
513static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
514{
515 efi_status_t status;
516 u32 phys_tm, phys_tc;
517
518 spin_lock(&rtc_lock);
519
520 phys_tm = virt_to_phys(tm);
521 phys_tc = virt_to_phys(tc);
522
523 status = efi_thunk(get_time, phys_tm, phys_tc);
524
525 spin_unlock(&rtc_lock);
526
527 return status;
528}
529
530static efi_status_t efi_thunk_set_time(efi_time_t *tm)
531{
532 efi_status_t status;
533 u32 phys_tm;
534
535 spin_lock(&rtc_lock);
536
537 phys_tm = virt_to_phys(tm);
538
539 status = efi_thunk(set_time, phys_tm);
540
541 spin_unlock(&rtc_lock);
542
543 return status;
544}
545
546static efi_status_t
547efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
548 efi_time_t *tm)
549{
550 efi_status_t status;
551 u32 phys_enabled, phys_pending, phys_tm;
552
553 spin_lock(&rtc_lock);
554
555 phys_enabled = virt_to_phys(enabled);
556 phys_pending = virt_to_phys(pending);
557 phys_tm = virt_to_phys(tm);
558
559 status = efi_thunk(get_wakeup_time, phys_enabled,
560 phys_pending, phys_tm);
561
562 spin_unlock(&rtc_lock);
563
564 return status;
565}
566
567static efi_status_t
568efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
569{
570 efi_status_t status;
571 u32 phys_tm;
572
573 spin_lock(&rtc_lock);
574
575 phys_tm = virt_to_phys(tm);
576
577 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
578
579 spin_unlock(&rtc_lock);
580
581 return status;
582}
583
584
585static efi_status_t
586efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
587 u32 *attr, unsigned long *data_size, void *data)
588{
589 efi_status_t status;
590 u32 phys_name, phys_vendor, phys_attr;
591 u32 phys_data_size, phys_data;
592
593 phys_data_size = virt_to_phys(data_size);
594 phys_vendor = virt_to_phys(vendor);
595 phys_name = virt_to_phys(name);
596 phys_attr = virt_to_phys(attr);
597 phys_data = virt_to_phys(data);
598
599 status = efi_thunk(get_variable, phys_name, phys_vendor,
600 phys_attr, phys_data_size, phys_data);
601
602 return status;
603}
604
605static efi_status_t
606efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
607 u32 attr, unsigned long data_size, void *data)
608{
609 u32 phys_name, phys_vendor, phys_data;
610 efi_status_t status;
611
612 phys_name = virt_to_phys(name);
613 phys_vendor = virt_to_phys(vendor);
614 phys_data = virt_to_phys(data);
615
616 /* If data_size is > sizeof(u32) we've got problems */
617 status = efi_thunk(set_variable, phys_name, phys_vendor,
618 attr, data_size, phys_data);
619
620 return status;
621}
622
623static efi_status_t
624efi_thunk_get_next_variable(unsigned long *name_size,
625 efi_char16_t *name,
626 efi_guid_t *vendor)
627{
628 efi_status_t status;
629 u32 phys_name_size, phys_name, phys_vendor;
630
631 phys_name_size = virt_to_phys(name_size);
632 phys_vendor = virt_to_phys(vendor);
633 phys_name = virt_to_phys(name);
634
635 status = efi_thunk(get_next_variable, phys_name_size,
636 phys_name, phys_vendor);
637
638 return status;
639}
640
641static efi_status_t
642efi_thunk_get_next_high_mono_count(u32 *count)
643{
644 efi_status_t status;
645 u32 phys_count;
646
647 phys_count = virt_to_phys(count);
648 status = efi_thunk(get_next_high_mono_count, phys_count);
649
650 return status;
651}
652
653static void
654efi_thunk_reset_system(int reset_type, efi_status_t status,
655 unsigned long data_size, efi_char16_t *data)
656{
657 u32 phys_data;
658
659 phys_data = virt_to_phys(data);
660
661 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
662}
663
664static efi_status_t
665efi_thunk_update_capsule(efi_capsule_header_t **capsules,
666 unsigned long count, unsigned long sg_list)
667{
668 /*
669 * To properly support this function we would need to repackage
670 * 'capsules' because the firmware doesn't understand 64-bit
671 * pointers.
672 */
673 return EFI_UNSUPPORTED;
674}
675
676static efi_status_t
677efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
678 u64 *remaining_space,
679 u64 *max_variable_size)
680{
681 efi_status_t status;
682 u32 phys_storage, phys_remaining, phys_max;
683
684 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
685 return EFI_UNSUPPORTED;
686
687 phys_storage = virt_to_phys(storage_space);
688 phys_remaining = virt_to_phys(remaining_space);
689 phys_max = virt_to_phys(max_variable_size);
690
Matt Fleming9a110402014-03-16 17:46:46 +0000691 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000692 phys_remaining, phys_max);
693
694 return status;
695}
696
697static efi_status_t
698efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
699 unsigned long count, u64 *max_size,
700 int *reset_type)
701{
702 /*
703 * To properly support this function we would need to repackage
704 * 'capsules' because the firmware doesn't understand 64-bit
705 * pointers.
706 */
707 return EFI_UNSUPPORTED;
708}
709
710void efi_thunk_runtime_setup(void)
711{
712 efi.get_time = efi_thunk_get_time;
713 efi.set_time = efi_thunk_set_time;
714 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
715 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
716 efi.get_variable = efi_thunk_get_variable;
717 efi.get_next_variable = efi_thunk_get_next_variable;
718 efi.set_variable = efi_thunk_set_variable;
719 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
720 efi.reset_system = efi_thunk_reset_system;
721 efi.query_variable_info = efi_thunk_query_variable_info;
722 efi.update_capsule = efi_thunk_update_capsule;
723 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
724}
725#endif /* CONFIG_EFI_MIXED */