]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/x86/platform/efi/efi_64.c
Merge remote-tracking branch 'asoc/topic/pcm512x' into asoc-next
[mirror_ubuntu-focal-kernel.git] / arch / x86 / platform / efi / efi_64.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5b83683f
HY
2/*
3 * x86_64 specific EFI support functions
4 * Based on Extensible Firmware Interface Specification version 1.0
5 *
6 * Copyright (C) 2005-2008 Intel Co.
7 * Fenghua Yu <fenghua.yu@intel.com>
8 * Bibo Mao <bibo.mao@intel.com>
9 * Chandramouli Narayanan <mouli@linux.intel.com>
10 * Huang Ying <ying.huang@intel.com>
11 *
12 * Code to convert EFI to E820 map has been implemented in elilo bootloader
13 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
14 * is setup appropriately for EFI runtime code.
15 * - mouli 06/14/2007.
16 *
17 */
18
26d7f65f
MF
19#define pr_fmt(fmt) "efi: " fmt
20
5b83683f
HY
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/mm.h>
24#include <linux/types.h>
25#include <linux/spinlock.h>
26#include <linux/bootmem.h>
27#include <linux/ioport.h>
cc3ae7b0 28#include <linux/init.h>
5ab788d7 29#include <linux/mc146818rtc.h>
5b83683f
HY
30#include <linux/efi.h>
31#include <linux/uaccess.h>
32#include <linux/io.h>
33#include <linux/reboot.h>
0d01ff25 34#include <linux/slab.h>
f6697df3 35#include <linux/ucs2_string.h>
1379edd5 36#include <linux/mem_encrypt.h>
5b83683f
HY
37
38#include <asm/setup.h>
39#include <asm/page.h>
66441bd3 40#include <asm/e820/api.h>
5b83683f
HY
41#include <asm/pgtable.h>
42#include <asm/tlbflush.h>
5b83683f
HY
43#include <asm/proto.h>
44#include <asm/efi.h>
4de0d4a6 45#include <asm/cacheflush.h>
3819cd48 46#include <asm/fixmap.h>
d2f7cbe7 47#include <asm/realmode.h>
4f9dbcfc 48#include <asm/time.h>
67a9108e 49#include <asm/pgalloc.h>
5b83683f 50
d2f7cbe7 51/*
b1d17761 52 * We allocate runtime services regions top-down, starting from -4G, i.e.
d2f7cbe7
BP
53 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
54 */
8266e31e 55static u64 efi_va = EFI_VA_START;
d2f7cbe7 56
c9f2a9a6 57struct efi_scratch efi_scratch;
d2f7cbe7 58
9cd2b07c 59static void __init early_code_mapping_set_exec(int executable)
5b83683f
HY
60{
61 efi_memory_desc_t *md;
5b83683f 62
a2172e25
HY
63 if (!(__supported_pte_mask & _PAGE_NX))
64 return;
65
916f676f 66 /* Make EFI service code area executable */
78ce248f 67 for_each_efi_memory_desc(md) {
916f676f
MG
68 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
69 md->type == EFI_BOOT_SERVICES_CODE)
9cd2b07c 70 efi_set_executable(md, executable);
5b83683f
HY
71 }
72}
73
744937b0 74pgd_t * __init efi_call_phys_prolog(void)
5b83683f 75{
94133e46
BH
76 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
77 pgd_t *save_pgd, *pgd_k, *pgd_efi;
78 p4d_t *p4d, *p4d_k, *p4d_efi;
79 pud_t *pud;
744937b0 80
b8f2c21d 81 int pgd;
94133e46 82 int n_pgds, i, j;
5b83683f 83
c9f2a9a6 84 if (!efi_enabled(EFI_OLD_MEMMAP)) {
6c690ee1 85 save_pgd = (pgd_t *)__read_cr3();
c9f2a9a6
MF
86 write_cr3((unsigned long)efi_scratch.efi_pgt);
87 goto out;
88 }
d2f7cbe7 89
9cd2b07c 90 early_code_mapping_set_exec(1);
b8f2c21d
NZ
91
92 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
20ebc15e 93 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
b8f2c21d 94
94133e46
BH
95 /*
96 * Build 1:1 identity mapping for efi=old_map usage. Note that
97 * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while
98 * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical
99 * address X, the pud_index(X) != pud_index(__va(X)), we can only copy
100 * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping.
101 * This means here we can only reuse the PMD tables of the direct mapping.
102 */
b8f2c21d 103 for (pgd = 0; pgd < n_pgds; pgd++) {
94133e46
BH
104 addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
105 vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
106 pgd_efi = pgd_offset_k(addr_pgd);
107 save_pgd[pgd] = *pgd_efi;
108
109 p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
110 if (!p4d) {
111 pr_err("Failed to allocate p4d table!\n");
112 goto out;
113 }
114
115 for (i = 0; i < PTRS_PER_P4D; i++) {
116 addr_p4d = addr_pgd + i * P4D_SIZE;
117 p4d_efi = p4d + p4d_index(addr_p4d);
118
119 pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
120 if (!pud) {
121 pr_err("Failed to allocate pud table!\n");
122 goto out;
123 }
124
125 for (j = 0; j < PTRS_PER_PUD; j++) {
126 addr_pud = addr_p4d + j * PUD_SIZE;
127
128 if (addr_pud > (max_pfn << PAGE_SHIFT))
129 break;
130
131 vaddr = (unsigned long)__va(addr_pud);
132
133 pgd_k = pgd_offset_k(vaddr);
134 p4d_k = p4d_offset(pgd_k, vaddr);
135 pud[j] = *pud_offset(p4d_k, vaddr);
136 }
137 }
de53c378 138 pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
b8f2c21d 139 }
de53c378 140
c9f2a9a6 141out:
5b83683f 142 __flush_tlb_all();
744937b0
IM
143
144 return save_pgd;
5b83683f
HY
145}
146
744937b0 147void __init efi_call_phys_epilog(pgd_t *save_pgd)
5b83683f
HY
148{
149 /*
150 * After the lock is released, the original page table is restored.
151 */
94133e46 152 int pgd_idx, i;
744937b0 153 int nr_pgds;
94133e46
BH
154 pgd_t *pgd;
155 p4d_t *p4d;
156 pud_t *pud;
d2f7cbe7 157
c9f2a9a6
MF
158 if (!efi_enabled(EFI_OLD_MEMMAP)) {
159 write_cr3((unsigned long)save_pgd);
160 __flush_tlb_all();
d2f7cbe7 161 return;
c9f2a9a6 162 }
d2f7cbe7 163
744937b0
IM
164 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
165
94133e46
BH
166 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
167 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
744937b0
IM
168 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
169
94133e46
BH
170 if (!(pgd_val(*pgd) & _PAGE_PRESENT))
171 continue;
172
173 for (i = 0; i < PTRS_PER_P4D; i++) {
174 p4d = p4d_offset(pgd,
175 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
176
177 if (!(p4d_val(*p4d) & _PAGE_PRESENT))
178 continue;
179
180 pud = (pud_t *)p4d_page_vaddr(*p4d);
181 pud_free(&init_mm, pud);
182 }
183
184 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
185 p4d_free(&init_mm, p4d);
186 }
187
b8f2c21d 188 kfree(save_pgd);
744937b0 189
5b83683f 190 __flush_tlb_all();
9cd2b07c 191 early_code_mapping_set_exec(0);
5b83683f 192}
e1ad783b 193
67a9108e
MF
194static pgd_t *efi_pgd;
195
196/*
197 * We need our own copy of the higher levels of the page tables
198 * because we want to avoid inserting EFI region mappings (EFI_VA_END
199 * to EFI_VA_START) into the standard kernel page tables. Everything
200 * else can be shared, see efi_sync_low_kernel_mappings().
d9e9a641
DH
201 *
202 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
203 * allocation.
67a9108e
MF
204 */
205int __init efi_alloc_page_tables(void)
206{
207 pgd_t *pgd;
e981316f 208 p4d_t *p4d;
67a9108e
MF
209 pud_t *pud;
210 gfp_t gfp_mask;
211
212 if (efi_enabled(EFI_OLD_MEMMAP))
213 return 0;
214
75f296d9 215 gfp_mask = GFP_KERNEL | __GFP_ZERO;
d9e9a641 216 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
67a9108e
MF
217 if (!efi_pgd)
218 return -ENOMEM;
219
220 pgd = efi_pgd + pgd_index(EFI_VA_END);
e981316f
KS
221 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
222 if (!p4d) {
223 free_page((unsigned long)efi_pgd);
224 return -ENOMEM;
225 }
67a9108e 226
e981316f 227 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
67a9108e 228 if (!pud) {
e981316f
KS
229 if (CONFIG_PGTABLE_LEVELS > 4)
230 free_page((unsigned long) pgd_page_vaddr(*pgd));
67a9108e
MF
231 free_page((unsigned long)efi_pgd);
232 return -ENOMEM;
233 }
234
67a9108e
MF
235 return 0;
236}
237
d2f7cbe7
BP
238/*
239 * Add low kernel mappings for passing arguments to EFI functions.
240 */
241void efi_sync_low_kernel_mappings(void)
242{
67a9108e
MF
243 unsigned num_entries;
244 pgd_t *pgd_k, *pgd_efi;
e0c4f675 245 p4d_t *p4d_k, *p4d_efi;
67a9108e 246 pud_t *pud_k, *pud_efi;
d2f7cbe7
BP
247
248 if (efi_enabled(EFI_OLD_MEMMAP))
249 return;
250
67a9108e
MF
251 /*
252 * We can share all PGD entries apart from the one entry that
253 * covers the EFI runtime mapping space.
254 *
255 * Make sure the EFI runtime region mappings are guaranteed to
256 * only span a single PGD entry and that the entry also maps
257 * other important kernel regions.
258 */
259 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
260 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
261 (EFI_VA_END & PGDIR_MASK));
262
263 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
264 pgd_k = pgd_offset_k(PAGE_OFFSET);
265
266 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
267 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
d2f7cbe7 268
e981316f
KS
269 /*
270 * As with PGDs, we share all P4D entries apart from the one entry
271 * that covers the EFI runtime mapping space.
272 */
273 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
274 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
275
276 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
277 pgd_k = pgd_offset_k(EFI_VA_END);
278 p4d_efi = p4d_offset(pgd_efi, 0);
279 p4d_k = p4d_offset(pgd_k, 0);
280
281 num_entries = p4d_index(EFI_VA_END);
282 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
283
67a9108e
MF
284 /*
285 * We share all the PUD entries apart from those that map the
286 * EFI regions. Copy around them.
287 */
288 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
289 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
290
e981316f
KS
291 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
292 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
e0c4f675 293 pud_efi = pud_offset(p4d_efi, 0);
e0c4f675 294 pud_k = pud_offset(p4d_k, 0);
67a9108e
MF
295
296 num_entries = pud_index(EFI_VA_END);
297 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
298
e0c4f675 299 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
e0c4f675 300 pud_k = pud_offset(p4d_k, EFI_VA_START);
67a9108e
MF
301
302 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
303 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
d2f7cbe7
BP
304}
305
f6697df3
MF
306/*
307 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
308 */
309static inline phys_addr_t
310virt_to_phys_or_null_size(void *va, unsigned long size)
311{
312 bool bad_size;
313
314 if (!va)
315 return 0;
316
317 if (virt_addr_valid(va))
318 return virt_to_phys(va);
319
320 /*
321 * A fully aligned variable on the stack is guaranteed not to
322 * cross a page bounary. Try to catch strings on the stack by
323 * checking that 'size' is a power of two.
324 */
325 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
326
327 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
328
329 return slow_virt_to_phys(va);
330}
331
332#define virt_to_phys_or_null(addr) \
333 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
334
4e78eb05 335int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
d2f7cbe7 336{
38eecccd 337 unsigned long pfn, text, pf;
4f9dbcfc 338 struct page *page;
994448f1 339 unsigned npages;
b7b898ae
BP
340 pgd_t *pgd;
341
342 if (efi_enabled(EFI_OLD_MEMMAP))
343 return 0;
344
38eecccd
TL
345 /*
346 * Since the PGD is encrypted, set the encryption mask so that when
347 * this value is loaded into cr3 the PGD will be decrypted during
348 * the pagetable walk.
349 */
350 efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd);
67a9108e 351 pgd = efi_pgd;
d2f7cbe7 352
b7b898ae
BP
353 /*
354 * It can happen that the physical address of new_memmap lands in memory
355 * which is not mapped in the EFI page table. Therefore we need to go
356 * and ident-map those pages containing the map before calling
357 * phys_efi_set_virtual_address_map().
358 */
edc3b912 359 pfn = pa_memmap >> PAGE_SHIFT;
38eecccd
TL
360 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
361 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
b7b898ae
BP
362 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
363 return 1;
364 }
365
366 efi_scratch.use_pgd = true;
367
bf29bddf
JK
368 /*
369 * Certain firmware versions are way too sentimential and still believe
370 * they are exclusive and unquestionable owners of the first physical page,
371 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
372 * (but then write-access it later during SetVirtualAddressMap()).
373 *
374 * Create a 1:1 mapping for this page, to avoid triple faults during early
375 * boot with such firmware. We are free to hand this page to the BIOS,
376 * as trim_bios_range() will reserve the first page and isolate it away
377 * from memory allocators anyway.
378 */
1379edd5
TL
379 pf = _PAGE_RW;
380 if (sev_active())
381 pf |= _PAGE_ENC;
382
383 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
bf29bddf
JK
384 pr_err("Failed to create 1:1 mapping for the first page!\n");
385 return 1;
386 }
387
4f9dbcfc
MF
388 /*
389 * When making calls to the firmware everything needs to be 1:1
390 * mapped and addressable with 32-bit pointers. Map the kernel
391 * text and allocate a new stack because we can't rely on the
392 * stack pointer being < 4GB.
393 */
12976670 394 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
994448f1 395 return 0;
4f9dbcfc
MF
396
397 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
398 if (!page)
399 panic("Unable to allocate EFI runtime stack < 4GB\n");
400
401 efi_scratch.phys_stack = virt_to_phys(page_address(page));
402 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
403
2ad510dc 404 npages = (_etext - _text) >> PAGE_SHIFT;
4f9dbcfc 405 text = __pa(_text);
edc3b912 406 pfn = text >> PAGE_SHIFT;
4f9dbcfc 407
38eecccd
TL
408 pf = _PAGE_RW | _PAGE_ENC;
409 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
4f9dbcfc 410 pr_err("Failed to map kernel text 1:1\n");
994448f1 411 return 1;
4f9dbcfc 412 }
b7b898ae
BP
413
414 return 0;
415}
416
d2f7cbe7
BP
417static void __init __map_region(efi_memory_desc_t *md, u64 va)
418{
15f003d2 419 unsigned long flags = _PAGE_RW;
edc3b912 420 unsigned long pfn;
67a9108e 421 pgd_t *pgd = efi_pgd;
d2f7cbe7
BP
422
423 if (!(md->attribute & EFI_MEMORY_WB))
edc3b912 424 flags |= _PAGE_PCD;
d2f7cbe7 425
1379edd5
TL
426 if (sev_active())
427 flags |= _PAGE_ENC;
428
edc3b912
MF
429 pfn = md->phys_addr >> PAGE_SHIFT;
430 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
d2f7cbe7
BP
431 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
432 md->phys_addr, va);
433}
434
435void __init efi_map_region(efi_memory_desc_t *md)
436{
437 unsigned long size = md->num_pages << PAGE_SHIFT;
438 u64 pa = md->phys_addr;
439
440 if (efi_enabled(EFI_OLD_MEMMAP))
441 return old_map_region(md);
442
443 /*
444 * Make sure the 1:1 mappings are present as a catch-all for b0rked
445 * firmware which doesn't update all internal pointers after switching
446 * to virtual mode and would otherwise crap on us.
447 */
448 __map_region(md, md->phys_addr);
449
4f9dbcfc
MF
450 /*
451 * Enforce the 1:1 mapping as the default virtual address when
452 * booting in EFI mixed mode, because even though we may be
453 * running a 64-bit kernel, the firmware may only be 32-bit.
454 */
455 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
456 md->virt_addr = md->phys_addr;
457 return;
458 }
459
d2f7cbe7
BP
460 efi_va -= size;
461
462 /* Is PA 2M-aligned? */
463 if (!(pa & (PMD_SIZE - 1))) {
464 efi_va &= PMD_MASK;
465 } else {
466 u64 pa_offset = pa & (PMD_SIZE - 1);
467 u64 prev_va = efi_va;
468
469 /* get us the same offset within this 2M page */
470 efi_va = (efi_va & PMD_MASK) + pa_offset;
471
472 if (efi_va > prev_va)
473 efi_va -= PMD_SIZE;
474 }
475
476 if (efi_va < EFI_VA_END) {
477 pr_warn(FW_WARN "VA address range overflow!\n");
478 return;
479 }
480
481 /* Do the VA map */
482 __map_region(md, efi_va);
483 md->virt_addr = efi_va;
484}
485
3b266496
DY
486/*
487 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
488 * md->virt_addr is the original virtual address which had been mapped in kexec
489 * 1st kernel.
490 */
491void __init efi_map_region_fixed(efi_memory_desc_t *md)
492{
0513fe1d 493 __map_region(md, md->phys_addr);
3b266496
DY
494 __map_region(md, md->virt_addr);
495}
496
e1ad783b 497void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
3e8fa263 498 u32 type, u64 attribute)
e1ad783b
KP
499{
500 unsigned long last_map_pfn;
501
502 if (type == EFI_MEMORY_MAPPED_IO)
503 return ioremap(phys_addr, size);
504
505 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
506 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
507 unsigned long top = last_map_pfn << PAGE_SHIFT;
3e8fa263 508 efi_ioremap(top, size - (top - phys_addr), type, attribute);
e1ad783b
KP
509 }
510
3e8fa263
MF
511 if (!(attribute & EFI_MEMORY_WB))
512 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
513
e1ad783b
KP
514 return (void __iomem *)__va(phys_addr);
515}
1fec0533
DY
516
517void __init parse_efi_setup(u64 phys_addr, u32 data_len)
518{
519 efi_setup = phys_addr + sizeof(struct setup_data);
1fec0533 520}
c55d016f 521
18141e89 522static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
c55d016f 523{
6d0cc887
SP
524 unsigned long pfn;
525 pgd_t *pgd = efi_pgd;
18141e89
SP
526 int err1, err2;
527
528 /* Update the 1:1 mapping */
529 pfn = md->phys_addr >> PAGE_SHIFT;
530 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
531 if (err1) {
532 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
533 md->phys_addr, md->virt_addr);
534 }
535
536 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
537 if (err2) {
538 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
539 md->phys_addr, md->virt_addr);
540 }
541
542 return err1 || err2;
543}
544
545static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
546{
547 unsigned long pf = 0;
548
549 if (md->attribute & EFI_MEMORY_XP)
550 pf |= _PAGE_NX;
551
552 if (!(md->attribute & EFI_MEMORY_RO))
553 pf |= _PAGE_RW;
554
1379edd5
TL
555 if (sev_active())
556 pf |= _PAGE_ENC;
557
18141e89
SP
558 return efi_update_mappings(md, pf);
559}
560
561void __init efi_runtime_update_mappings(void)
562{
6d0cc887 563 efi_memory_desc_t *md;
6d0cc887
SP
564
565 if (efi_enabled(EFI_OLD_MEMMAP)) {
566 if (__supported_pte_mask & _PAGE_NX)
567 runtime_code_page_mkexec();
568 return;
569 }
570
18141e89
SP
571 /*
572 * Use the EFI Memory Attribute Table for mapping permissions if it
573 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
574 */
575 if (efi_enabled(EFI_MEM_ATTR)) {
576 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
577 return;
578 }
579
580 /*
581 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
582 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
583 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
584 * published by the firmware. Even if we find a buggy implementation of
585 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
586 * EFI_PROPERTIES_TABLE, because of the same reason.
587 */
588
6d0cc887 589 if (!efi_enabled(EFI_NX_PE_DATA))
c55d016f
BP
590 return;
591
78ce248f 592 for_each_efi_memory_desc(md) {
6d0cc887 593 unsigned long pf = 0;
6d0cc887
SP
594
595 if (!(md->attribute & EFI_MEMORY_RUNTIME))
596 continue;
597
598 if (!(md->attribute & EFI_MEMORY_WB))
599 pf |= _PAGE_PCD;
600
601 if ((md->attribute & EFI_MEMORY_XP) ||
602 (md->type == EFI_RUNTIME_SERVICES_DATA))
603 pf |= _PAGE_NX;
604
605 if (!(md->attribute & EFI_MEMORY_RO) &&
606 (md->type != EFI_RUNTIME_SERVICES_CODE))
607 pf |= _PAGE_RW;
608
1379edd5
TL
609 if (sev_active())
610 pf |= _PAGE_ENC;
611
18141e89 612 efi_update_mappings(md, pf);
6d0cc887 613 }
c55d016f 614}
11cc8512
BP
615
616void __init efi_dump_pagetable(void)
617{
618#ifdef CONFIG_EFI_PGT_DUMP
ac81d3de
SP
619 if (efi_enabled(EFI_OLD_MEMMAP))
620 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
621 else
622 ptdump_walk_pgd_level(NULL, efi_pgd);
11cc8512
BP
623#endif
624}
994448f1 625
4f9dbcfc
MF
626#ifdef CONFIG_EFI_MIXED
627extern efi_status_t efi64_thunk(u32, ...);
628
629#define runtime_service32(func) \
630({ \
631 u32 table = (u32)(unsigned long)efi.systab; \
632 u32 *rt, *___f; \
633 \
634 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
635 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
636 *___f; \
637})
638
639/*
640 * Switch to the EFI page tables early so that we can access the 1:1
641 * runtime services mappings which are not mapped in any other page
642 * tables. This function must be called before runtime_service32().
643 *
644 * Also, disable interrupts because the IDT points to 64-bit handlers,
645 * which aren't going to function correctly when we switch to 32-bit.
646 */
647#define efi_thunk(f, ...) \
648({ \
649 efi_status_t __s; \
21f86625
AT
650 unsigned long __flags; \
651 u32 __func; \
4f9dbcfc 652 \
21f86625
AT
653 local_irq_save(__flags); \
654 arch_efi_call_virt_setup(); \
4f9dbcfc 655 \
21f86625
AT
656 __func = runtime_service32(f); \
657 __s = efi64_thunk(__func, __VA_ARGS__); \
4f9dbcfc 658 \
21f86625
AT
659 arch_efi_call_virt_teardown(); \
660 local_irq_restore(__flags); \
4f9dbcfc
MF
661 \
662 __s; \
663})
664
665efi_status_t efi_thunk_set_virtual_address_map(
666 void *phys_set_virtual_address_map,
667 unsigned long memory_map_size,
668 unsigned long descriptor_size,
669 u32 descriptor_version,
670 efi_memory_desc_t *virtual_map)
671{
672 efi_status_t status;
673 unsigned long flags;
674 u32 func;
675
676 efi_sync_low_kernel_mappings();
677 local_irq_save(flags);
678
6c690ee1 679 efi_scratch.prev_cr3 = __read_cr3();
4f9dbcfc
MF
680 write_cr3((unsigned long)efi_scratch.efi_pgt);
681 __flush_tlb_all();
682
683 func = (u32)(unsigned long)phys_set_virtual_address_map;
684 status = efi64_thunk(func, memory_map_size, descriptor_size,
685 descriptor_version, virtual_map);
686
687 write_cr3(efi_scratch.prev_cr3);
688 __flush_tlb_all();
689 local_irq_restore(flags);
690
691 return status;
692}
693
694static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
695{
696 efi_status_t status;
697 u32 phys_tm, phys_tc;
698
699 spin_lock(&rtc_lock);
700
f6697df3
MF
701 phys_tm = virt_to_phys_or_null(tm);
702 phys_tc = virt_to_phys_or_null(tc);
4f9dbcfc
MF
703
704 status = efi_thunk(get_time, phys_tm, phys_tc);
705
706 spin_unlock(&rtc_lock);
707
708 return status;
709}
710
711static efi_status_t efi_thunk_set_time(efi_time_t *tm)
712{
713 efi_status_t status;
714 u32 phys_tm;
715
716 spin_lock(&rtc_lock);
717
f6697df3 718 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
719
720 status = efi_thunk(set_time, phys_tm);
721
722 spin_unlock(&rtc_lock);
723
724 return status;
725}
726
727static efi_status_t
728efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
729 efi_time_t *tm)
730{
731 efi_status_t status;
732 u32 phys_enabled, phys_pending, phys_tm;
733
734 spin_lock(&rtc_lock);
735
f6697df3
MF
736 phys_enabled = virt_to_phys_or_null(enabled);
737 phys_pending = virt_to_phys_or_null(pending);
738 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
739
740 status = efi_thunk(get_wakeup_time, phys_enabled,
741 phys_pending, phys_tm);
742
743 spin_unlock(&rtc_lock);
744
745 return status;
746}
747
748static efi_status_t
749efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
750{
751 efi_status_t status;
752 u32 phys_tm;
753
754 spin_lock(&rtc_lock);
755
f6697df3 756 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
757
758 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
759
760 spin_unlock(&rtc_lock);
761
762 return status;
763}
764
f6697df3
MF
765static unsigned long efi_name_size(efi_char16_t *name)
766{
767 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
768}
4f9dbcfc
MF
769
770static efi_status_t
771efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
772 u32 *attr, unsigned long *data_size, void *data)
773{
774 efi_status_t status;
775 u32 phys_name, phys_vendor, phys_attr;
776 u32 phys_data_size, phys_data;
777
f6697df3
MF
778 phys_data_size = virt_to_phys_or_null(data_size);
779 phys_vendor = virt_to_phys_or_null(vendor);
780 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
781 phys_attr = virt_to_phys_or_null(attr);
782 phys_data = virt_to_phys_or_null_size(data, *data_size);
4f9dbcfc
MF
783
784 status = efi_thunk(get_variable, phys_name, phys_vendor,
785 phys_attr, phys_data_size, phys_data);
786
787 return status;
788}
789
790static efi_status_t
791efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
792 u32 attr, unsigned long data_size, void *data)
793{
794 u32 phys_name, phys_vendor, phys_data;
795 efi_status_t status;
796
f6697df3
MF
797 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
798 phys_vendor = virt_to_phys_or_null(vendor);
799 phys_data = virt_to_phys_or_null_size(data, data_size);
4f9dbcfc
MF
800
801 /* If data_size is > sizeof(u32) we've got problems */
802 status = efi_thunk(set_variable, phys_name, phys_vendor,
803 attr, data_size, phys_data);
804
805 return status;
806}
807
808static efi_status_t
809efi_thunk_get_next_variable(unsigned long *name_size,
810 efi_char16_t *name,
811 efi_guid_t *vendor)
812{
813 efi_status_t status;
814 u32 phys_name_size, phys_name, phys_vendor;
815
f6697df3
MF
816 phys_name_size = virt_to_phys_or_null(name_size);
817 phys_vendor = virt_to_phys_or_null(vendor);
818 phys_name = virt_to_phys_or_null_size(name, *name_size);
4f9dbcfc
MF
819
820 status = efi_thunk(get_next_variable, phys_name_size,
821 phys_name, phys_vendor);
822
823 return status;
824}
825
826static efi_status_t
827efi_thunk_get_next_high_mono_count(u32 *count)
828{
829 efi_status_t status;
830 u32 phys_count;
831
f6697df3 832 phys_count = virt_to_phys_or_null(count);
4f9dbcfc
MF
833 status = efi_thunk(get_next_high_mono_count, phys_count);
834
835 return status;
836}
837
838static void
839efi_thunk_reset_system(int reset_type, efi_status_t status,
840 unsigned long data_size, efi_char16_t *data)
841{
842 u32 phys_data;
843
f6697df3 844 phys_data = virt_to_phys_or_null_size(data, data_size);
4f9dbcfc
MF
845
846 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
847}
848
849static efi_status_t
850efi_thunk_update_capsule(efi_capsule_header_t **capsules,
851 unsigned long count, unsigned long sg_list)
852{
853 /*
854 * To properly support this function we would need to repackage
855 * 'capsules' because the firmware doesn't understand 64-bit
856 * pointers.
857 */
858 return EFI_UNSUPPORTED;
859}
860
861static efi_status_t
862efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
863 u64 *remaining_space,
864 u64 *max_variable_size)
865{
866 efi_status_t status;
867 u32 phys_storage, phys_remaining, phys_max;
868
869 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
870 return EFI_UNSUPPORTED;
871
f6697df3
MF
872 phys_storage = virt_to_phys_or_null(storage_space);
873 phys_remaining = virt_to_phys_or_null(remaining_space);
874 phys_max = virt_to_phys_or_null(max_variable_size);
4f9dbcfc 875
9a11040f 876 status = efi_thunk(query_variable_info, attr, phys_storage,
4f9dbcfc
MF
877 phys_remaining, phys_max);
878
879 return status;
880}
881
882static efi_status_t
883efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
884 unsigned long count, u64 *max_size,
885 int *reset_type)
886{
887 /*
888 * To properly support this function we would need to repackage
889 * 'capsules' because the firmware doesn't understand 64-bit
890 * pointers.
891 */
892 return EFI_UNSUPPORTED;
893}
894
895void efi_thunk_runtime_setup(void)
896{
897 efi.get_time = efi_thunk_get_time;
898 efi.set_time = efi_thunk_set_time;
899 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
900 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
901 efi.get_variable = efi_thunk_get_variable;
902 efi.get_next_variable = efi_thunk_get_next_variable;
903 efi.set_variable = efi_thunk_set_variable;
904 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
905 efi.reset_system = efi_thunk_reset_system;
906 efi.query_variable_info = efi_thunk_query_variable_info;
907 efi.update_capsule = efi_thunk_update_capsule;
908 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
909}
910#endif /* CONFIG_EFI_MIXED */