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