blob: 8dd3784eb0752ea6ff4cc0d21f249ca084a323f6 [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>
Paul Gortmakercc3ae7b2016-07-13 20:18:58 -040027#include <linux/init.h>
Arnd Bergmann5ab788d2016-05-30 20:57:50 +020028#include <linux/mc146818rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010029#include <linux/efi.h>
30#include <linux/uaccess.h>
31#include <linux/io.h>
32#include <linux/reboot.h>
David Howells0d01ff22013-04-11 23:51:01 +010033#include <linux/slab.h>
Huang, Ying5b836832008-01-30 13:31:19 +010034
35#include <asm/setup.h>
36#include <asm/page.h>
37#include <asm/e820.h>
38#include <asm/pgtable.h>
39#include <asm/tlbflush.h>
Huang, Ying5b836832008-01-30 13:31:19 +010040#include <asm/proto.h>
41#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080042#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090043#include <asm/fixmap.h>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010044#include <asm/realmode.h>
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000045#include <asm/time.h>
Matt Fleming67a91082015-11-27 21:09:34 +000046#include <asm/pgalloc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010047
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010048/*
49 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
50 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
51 */
Mathias Krause8266e312014-09-21 17:26:54 +020052static u64 efi_va = EFI_VA_START;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010053
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000054struct efi_scratch efi_scratch;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010055
Matthew Garrett9cd2b072011-05-05 15:19:43 -040056static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010057{
58 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +010059
Huang, Yinga2172e22008-01-30 13:33:55 +010060 if (!(__supported_pte_mask & _PAGE_NX))
61 return;
62
Matthew Garrett916f6762011-05-25 09:53:13 -040063 /* Make EFI service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +010064 for_each_efi_memory_desc(md) {
Matthew Garrett916f6762011-05-25 09:53:13 -040065 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
66 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040067 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010068 }
69}
70
Ingo Molnar744937b2015-03-03 07:48:50 +010071pgd_t * __init efi_call_phys_prolog(void)
Huang, Ying5b836832008-01-30 13:31:19 +010072{
73 unsigned long vaddress;
Ingo Molnar744937b2015-03-03 07:48:50 +010074 pgd_t *save_pgd;
75
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060076 int pgd;
77 int n_pgds;
Huang, Ying5b836832008-01-30 13:31:19 +010078
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000079 if (!efi_enabled(EFI_OLD_MEMMAP)) {
80 save_pgd = (pgd_t *)read_cr3();
81 write_cr3((unsigned long)efi_scratch.efi_pgt);
82 goto out;
83 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010084
Matthew Garrett9cd2b072011-05-05 15:19:43 -040085 early_code_mapping_set_exec(1);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060086
87 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
88 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
89
90 for (pgd = 0; pgd < n_pgds; pgd++) {
91 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
92 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
93 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
94 }
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000095out:
Huang, Ying5b836832008-01-30 13:31:19 +010096 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +010097
98 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +010099}
100
Ingo Molnar744937b2015-03-03 07:48:50 +0100101void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100102{
103 /*
104 * After the lock is released, the original page table is restored.
105 */
Ingo Molnar744937b2015-03-03 07:48:50 +0100106 int pgd_idx;
107 int nr_pgds;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100108
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000109 if (!efi_enabled(EFI_OLD_MEMMAP)) {
110 write_cr3((unsigned long)save_pgd);
111 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100112 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000113 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100114
Ingo Molnar744937b2015-03-03 07:48:50 +0100115 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
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600120 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100121
Huang, Ying5b836832008-01-30 13:31:19 +0100122 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400123 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100124}
Keith Packarde1ad7832011-12-11 16:12:42 -0800125
Matt Fleming67a91082015-11-27 21:09:34 +0000126static pgd_t *efi_pgd;
127
128/*
129 * We need our own copy of the higher levels of the page tables
130 * because we want to avoid inserting EFI region mappings (EFI_VA_END
131 * to EFI_VA_START) into the standard kernel page tables. Everything
132 * else can be shared, see efi_sync_low_kernel_mappings().
133 */
134int __init efi_alloc_page_tables(void)
135{
136 pgd_t *pgd;
137 pud_t *pud;
138 gfp_t gfp_mask;
139
140 if (efi_enabled(EFI_OLD_MEMMAP))
141 return 0;
142
Michal Hockof58f2302016-06-24 14:48:53 -0700143 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
Matt Fleming67a91082015-11-27 21:09:34 +0000144 efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
145 if (!efi_pgd)
146 return -ENOMEM;
147
148 pgd = efi_pgd + pgd_index(EFI_VA_END);
149
150 pud = pud_alloc_one(NULL, 0);
151 if (!pud) {
152 free_page((unsigned long)efi_pgd);
153 return -ENOMEM;
154 }
155
156 pgd_populate(NULL, pgd, pud);
157
158 return 0;
159}
160
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100161/*
162 * Add low kernel mappings for passing arguments to EFI functions.
163 */
164void efi_sync_low_kernel_mappings(void)
165{
Matt Fleming67a91082015-11-27 21:09:34 +0000166 unsigned num_entries;
167 pgd_t *pgd_k, *pgd_efi;
168 pud_t *pud_k, *pud_efi;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100169
170 if (efi_enabled(EFI_OLD_MEMMAP))
171 return;
172
Matt Fleming67a91082015-11-27 21:09:34 +0000173 /*
174 * We can share all PGD entries apart from the one entry that
175 * covers the EFI runtime mapping space.
176 *
177 * Make sure the EFI runtime region mappings are guaranteed to
178 * only span a single PGD entry and that the entry also maps
179 * other important kernel regions.
180 */
181 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
182 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
183 (EFI_VA_END & PGDIR_MASK));
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100184
Matt Fleming67a91082015-11-27 21:09:34 +0000185 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
186 pgd_k = pgd_offset_k(PAGE_OFFSET);
187
188 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
189 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
190
191 /*
192 * We share all the PUD entries apart from those that map the
193 * EFI regions. Copy around them.
194 */
195 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
196 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
197
198 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
199 pud_efi = pud_offset(pgd_efi, 0);
200
201 pgd_k = pgd_offset_k(EFI_VA_END);
202 pud_k = pud_offset(pgd_k, 0);
203
204 num_entries = pud_index(EFI_VA_END);
205 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
206
207 pud_efi = pud_offset(pgd_efi, EFI_VA_START);
208 pud_k = pud_offset(pgd_k, EFI_VA_START);
209
210 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
211 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100212}
213
Mathias Krause4e78eb052014-09-07 19:42:17 +0200214int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100215{
Matt Flemingedc3b912015-11-27 21:09:31 +0000216 unsigned long pfn, text;
Matt Flemingb61a76f2015-11-27 21:09:32 +0000217 efi_memory_desc_t *md;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000218 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000219 unsigned npages;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100220 pgd_t *pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100221
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100222 if (efi_enabled(EFI_OLD_MEMMAP))
223 return 0;
224
Matt Fleming67a91082015-11-27 21:09:34 +0000225 efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd);
226 pgd = efi_pgd;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100227
228 /*
229 * It can happen that the physical address of new_memmap lands in memory
230 * which is not mapped in the EFI page table. Therefore we need to go
231 * and ident-map those pages containing the map before calling
232 * phys_efi_set_virtual_address_map().
233 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000234 pfn = pa_memmap >> PAGE_SHIFT;
Sai Praneeth15f003d2016-02-17 12:36:04 +0000235 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, _PAGE_NX | _PAGE_RW)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100236 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
237 return 1;
238 }
239
240 efi_scratch.use_pgd = true;
241
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000242 /*
243 * When making calls to the firmware everything needs to be 1:1
244 * mapped and addressable with 32-bit pointers. Map the kernel
245 * text and allocate a new stack because we can't rely on the
246 * stack pointer being < 4GB.
247 */
Matt Fleming12976672016-09-19 13:09:09 +0100248 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
Matt Fleming994448f2014-03-05 18:15:37 +0000249 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000250
Matt Flemingb61a76f2015-11-27 21:09:32 +0000251 /*
252 * Map all of RAM so that we can access arguments in the 1:1
253 * mapping when making EFI runtime calls.
254 */
Matt Fleming78ce2482016-04-25 21:06:38 +0100255 for_each_efi_memory_desc(md) {
Matt Flemingb61a76f2015-11-27 21:09:32 +0000256 if (md->type != EFI_CONVENTIONAL_MEMORY &&
257 md->type != EFI_LOADER_DATA &&
258 md->type != EFI_LOADER_CODE)
259 continue;
260
261 pfn = md->phys_addr >> PAGE_SHIFT;
262 npages = md->num_pages;
263
Sai Praneeth15f003d2016-02-17 12:36:04 +0000264 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, npages, _PAGE_RW)) {
Matt Flemingb61a76f2015-11-27 21:09:32 +0000265 pr_err("Failed to map 1:1 memory\n");
266 return 1;
267 }
268 }
269
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000270 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
271 if (!page)
272 panic("Unable to allocate EFI runtime stack < 4GB\n");
273
274 efi_scratch.phys_stack = virt_to_phys(page_address(page));
275 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
276
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000277 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000278 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000279 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000280
Sai Praneeth15f003d2016-02-17 12:36:04 +0000281 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000282 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000283 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000284 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100285
286 return 0;
287}
288
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100289static void __init __map_region(efi_memory_desc_t *md, u64 va)
290{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000291 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000292 unsigned long pfn;
Matt Fleming67a91082015-11-27 21:09:34 +0000293 pgd_t *pgd = efi_pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100294
295 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000296 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100297
Matt Flemingedc3b912015-11-27 21:09:31 +0000298 pfn = md->phys_addr >> PAGE_SHIFT;
299 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100300 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
301 md->phys_addr, va);
302}
303
304void __init efi_map_region(efi_memory_desc_t *md)
305{
306 unsigned long size = md->num_pages << PAGE_SHIFT;
307 u64 pa = md->phys_addr;
308
309 if (efi_enabled(EFI_OLD_MEMMAP))
310 return old_map_region(md);
311
312 /*
313 * Make sure the 1:1 mappings are present as a catch-all for b0rked
314 * firmware which doesn't update all internal pointers after switching
315 * to virtual mode and would otherwise crap on us.
316 */
317 __map_region(md, md->phys_addr);
318
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000319 /*
320 * Enforce the 1:1 mapping as the default virtual address when
321 * booting in EFI mixed mode, because even though we may be
322 * running a 64-bit kernel, the firmware may only be 32-bit.
323 */
324 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
325 md->virt_addr = md->phys_addr;
326 return;
327 }
328
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100329 efi_va -= size;
330
331 /* Is PA 2M-aligned? */
332 if (!(pa & (PMD_SIZE - 1))) {
333 efi_va &= PMD_MASK;
334 } else {
335 u64 pa_offset = pa & (PMD_SIZE - 1);
336 u64 prev_va = efi_va;
337
338 /* get us the same offset within this 2M page */
339 efi_va = (efi_va & PMD_MASK) + pa_offset;
340
341 if (efi_va > prev_va)
342 efi_va -= PMD_SIZE;
343 }
344
345 if (efi_va < EFI_VA_END) {
346 pr_warn(FW_WARN "VA address range overflow!\n");
347 return;
348 }
349
350 /* Do the VA map */
351 __map_region(md, efi_va);
352 md->virt_addr = efi_va;
353}
354
Dave Young3b266492013-12-20 18:02:14 +0800355/*
356 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
357 * md->virt_addr is the original virtual address which had been mapped in kexec
358 * 1st kernel.
359 */
360void __init efi_map_region_fixed(efi_memory_desc_t *md)
361{
362 __map_region(md, md->virt_addr);
363}
364
Keith Packarde1ad7832011-12-11 16:12:42 -0800365void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100366 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800367{
368 unsigned long last_map_pfn;
369
370 if (type == EFI_MEMORY_MAPPED_IO)
371 return ioremap(phys_addr, size);
372
373 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
374 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
375 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100376 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800377 }
378
Matt Fleming3e8fa262012-10-19 13:25:46 +0100379 if (!(attribute & EFI_MEMORY_WB))
380 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
381
Keith Packarde1ad7832011-12-11 16:12:42 -0800382 return (void __iomem *)__va(phys_addr);
383}
Dave Young1fec0532013-12-20 18:02:19 +0800384
385void __init parse_efi_setup(u64 phys_addr, u32 data_len)
386{
387 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800388}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100389
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000390void __init efi_runtime_update_mappings(void)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100391{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000392 unsigned long pfn;
393 pgd_t *pgd = efi_pgd;
394 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000395
396 if (efi_enabled(EFI_OLD_MEMMAP)) {
397 if (__supported_pte_mask & _PAGE_NX)
398 runtime_code_page_mkexec();
399 return;
400 }
401
402 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100403 return;
404
Matt Fleming78ce2482016-04-25 21:06:38 +0100405 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000406 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000407
408 if (!(md->attribute & EFI_MEMORY_RUNTIME))
409 continue;
410
411 if (!(md->attribute & EFI_MEMORY_WB))
412 pf |= _PAGE_PCD;
413
414 if ((md->attribute & EFI_MEMORY_XP) ||
415 (md->type == EFI_RUNTIME_SERVICES_DATA))
416 pf |= _PAGE_NX;
417
418 if (!(md->attribute & EFI_MEMORY_RO) &&
419 (md->type != EFI_RUNTIME_SERVICES_CODE))
420 pf |= _PAGE_RW;
421
422 /* Update the 1:1 mapping */
423 pfn = md->phys_addr >> PAGE_SHIFT;
424 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf))
425 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
426 md->phys_addr, md->virt_addr);
427
428 if (kernel_map_pages_in_pgd(pgd, pfn, md->virt_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 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100432}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100433
434void __init efi_dump_pagetable(void)
435{
436#ifdef CONFIG_EFI_PGT_DUMP
Matt Fleming67a91082015-11-27 21:09:34 +0000437 ptdump_walk_pgd_level(NULL, efi_pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100438#endif
439}
Matt Fleming994448f2014-03-05 18:15:37 +0000440
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000441#ifdef CONFIG_EFI_MIXED
442extern efi_status_t efi64_thunk(u32, ...);
443
444#define runtime_service32(func) \
445({ \
446 u32 table = (u32)(unsigned long)efi.systab; \
447 u32 *rt, *___f; \
448 \
449 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
450 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
451 *___f; \
452})
453
454/*
455 * Switch to the EFI page tables early so that we can access the 1:1
456 * runtime services mappings which are not mapped in any other page
457 * tables. This function must be called before runtime_service32().
458 *
459 * Also, disable interrupts because the IDT points to 64-bit handlers,
460 * which aren't going to function correctly when we switch to 32-bit.
461 */
462#define efi_thunk(f, ...) \
463({ \
464 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100465 unsigned long __flags; \
466 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000467 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100468 local_irq_save(__flags); \
469 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000470 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100471 __func = runtime_service32(f); \
472 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000473 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100474 arch_efi_call_virt_teardown(); \
475 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000476 \
477 __s; \
478})
479
480efi_status_t efi_thunk_set_virtual_address_map(
481 void *phys_set_virtual_address_map,
482 unsigned long memory_map_size,
483 unsigned long descriptor_size,
484 u32 descriptor_version,
485 efi_memory_desc_t *virtual_map)
486{
487 efi_status_t status;
488 unsigned long flags;
489 u32 func;
490
491 efi_sync_low_kernel_mappings();
492 local_irq_save(flags);
493
494 efi_scratch.prev_cr3 = read_cr3();
495 write_cr3((unsigned long)efi_scratch.efi_pgt);
496 __flush_tlb_all();
497
498 func = (u32)(unsigned long)phys_set_virtual_address_map;
499 status = efi64_thunk(func, memory_map_size, descriptor_size,
500 descriptor_version, virtual_map);
501
502 write_cr3(efi_scratch.prev_cr3);
503 __flush_tlb_all();
504 local_irq_restore(flags);
505
506 return status;
507}
508
509static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
510{
511 efi_status_t status;
512 u32 phys_tm, phys_tc;
513
514 spin_lock(&rtc_lock);
515
516 phys_tm = virt_to_phys(tm);
517 phys_tc = virt_to_phys(tc);
518
519 status = efi_thunk(get_time, phys_tm, phys_tc);
520
521 spin_unlock(&rtc_lock);
522
523 return status;
524}
525
526static efi_status_t efi_thunk_set_time(efi_time_t *tm)
527{
528 efi_status_t status;
529 u32 phys_tm;
530
531 spin_lock(&rtc_lock);
532
533 phys_tm = virt_to_phys(tm);
534
535 status = efi_thunk(set_time, phys_tm);
536
537 spin_unlock(&rtc_lock);
538
539 return status;
540}
541
542static efi_status_t
543efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
544 efi_time_t *tm)
545{
546 efi_status_t status;
547 u32 phys_enabled, phys_pending, phys_tm;
548
549 spin_lock(&rtc_lock);
550
551 phys_enabled = virt_to_phys(enabled);
552 phys_pending = virt_to_phys(pending);
553 phys_tm = virt_to_phys(tm);
554
555 status = efi_thunk(get_wakeup_time, phys_enabled,
556 phys_pending, phys_tm);
557
558 spin_unlock(&rtc_lock);
559
560 return status;
561}
562
563static efi_status_t
564efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
565{
566 efi_status_t status;
567 u32 phys_tm;
568
569 spin_lock(&rtc_lock);
570
571 phys_tm = virt_to_phys(tm);
572
573 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
574
575 spin_unlock(&rtc_lock);
576
577 return status;
578}
579
580
581static efi_status_t
582efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
583 u32 *attr, unsigned long *data_size, void *data)
584{
585 efi_status_t status;
586 u32 phys_name, phys_vendor, phys_attr;
587 u32 phys_data_size, phys_data;
588
589 phys_data_size = virt_to_phys(data_size);
590 phys_vendor = virt_to_phys(vendor);
591 phys_name = virt_to_phys(name);
592 phys_attr = virt_to_phys(attr);
593 phys_data = virt_to_phys(data);
594
595 status = efi_thunk(get_variable, phys_name, phys_vendor,
596 phys_attr, phys_data_size, phys_data);
597
598 return status;
599}
600
601static efi_status_t
602efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
603 u32 attr, unsigned long data_size, void *data)
604{
605 u32 phys_name, phys_vendor, phys_data;
606 efi_status_t status;
607
608 phys_name = virt_to_phys(name);
609 phys_vendor = virt_to_phys(vendor);
610 phys_data = virt_to_phys(data);
611
612 /* If data_size is > sizeof(u32) we've got problems */
613 status = efi_thunk(set_variable, phys_name, phys_vendor,
614 attr, data_size, phys_data);
615
616 return status;
617}
618
619static efi_status_t
620efi_thunk_get_next_variable(unsigned long *name_size,
621 efi_char16_t *name,
622 efi_guid_t *vendor)
623{
624 efi_status_t status;
625 u32 phys_name_size, phys_name, phys_vendor;
626
627 phys_name_size = virt_to_phys(name_size);
628 phys_vendor = virt_to_phys(vendor);
629 phys_name = virt_to_phys(name);
630
631 status = efi_thunk(get_next_variable, phys_name_size,
632 phys_name, phys_vendor);
633
634 return status;
635}
636
637static efi_status_t
638efi_thunk_get_next_high_mono_count(u32 *count)
639{
640 efi_status_t status;
641 u32 phys_count;
642
643 phys_count = virt_to_phys(count);
644 status = efi_thunk(get_next_high_mono_count, phys_count);
645
646 return status;
647}
648
649static void
650efi_thunk_reset_system(int reset_type, efi_status_t status,
651 unsigned long data_size, efi_char16_t *data)
652{
653 u32 phys_data;
654
655 phys_data = virt_to_phys(data);
656
657 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
658}
659
660static efi_status_t
661efi_thunk_update_capsule(efi_capsule_header_t **capsules,
662 unsigned long count, unsigned long sg_list)
663{
664 /*
665 * To properly support this function we would need to repackage
666 * 'capsules' because the firmware doesn't understand 64-bit
667 * pointers.
668 */
669 return EFI_UNSUPPORTED;
670}
671
672static efi_status_t
673efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
674 u64 *remaining_space,
675 u64 *max_variable_size)
676{
677 efi_status_t status;
678 u32 phys_storage, phys_remaining, phys_max;
679
680 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
681 return EFI_UNSUPPORTED;
682
683 phys_storage = virt_to_phys(storage_space);
684 phys_remaining = virt_to_phys(remaining_space);
685 phys_max = virt_to_phys(max_variable_size);
686
Matt Fleming9a110402014-03-16 17:46:46 +0000687 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000688 phys_remaining, phys_max);
689
690 return status;
691}
692
693static efi_status_t
694efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
695 unsigned long count, u64 *max_size,
696 int *reset_type)
697{
698 /*
699 * To properly support this function we would need to repackage
700 * 'capsules' because the firmware doesn't understand 64-bit
701 * pointers.
702 */
703 return EFI_UNSUPPORTED;
704}
705
706void efi_thunk_runtime_setup(void)
707{
708 efi.get_time = efi_thunk_get_time;
709 efi.set_time = efi_thunk_set_time;
710 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
711 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
712 efi.get_variable = efi_thunk_get_variable;
713 efi.get_next_variable = efi_thunk_get_next_variable;
714 efi.set_variable = efi_thunk_set_variable;
715 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
716 efi.reset_system = efi_thunk_reset_system;
717 efi.query_variable_info = efi_thunk_query_variable_info;
718 efi.update_capsule = efi_thunk_update_capsule;
719 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
720}
721#endif /* CONFIG_EFI_MIXED */