1 // SPDX-License-Identifier: GPL-2.0
3 * Extensible Firmware Interface
5 * Copyright (C) 2020 Western Digital Corporation or its affiliates.
7 * Based on Extensible Firmware Interface Specification version 2.4
8 * Adapted from drivers/firmware/efi/arm-runtime.c
12 #include <linux/dmi.h>
13 #include <linux/efi.h>
15 #include <linux/memblock.h>
16 #include <linux/mm_types.h>
17 #include <linux/preempt.h>
18 #include <linux/rbtree.h>
19 #include <linux/rwsem.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/pgtable.h>
25 #include <asm/cacheflush.h>
28 #include <asm/pgalloc.h>
30 static bool __init
efi_virtmap_init(void)
32 efi_memory_desc_t
*md
;
34 efi_mm
.pgd
= pgd_alloc(&efi_mm
);
35 mm_init_cpumask(&efi_mm
);
36 init_new_context(NULL
, &efi_mm
);
38 for_each_efi_memory_desc(md
) {
39 phys_addr_t phys
= md
->phys_addr
;
42 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
44 if (md
->virt_addr
== U64_MAX
)
47 ret
= efi_create_mapping(&efi_mm
, md
);
49 pr_warn(" EFI remap %pa: failed to create mapping (%d)\n",
55 if (efi_memattr_apply_permissions(&efi_mm
, efi_set_mapping_permissions
))
62 * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
63 * non-early mapping of the UEFI system table and virtual mappings for all
64 * EFI_MEMORY_RUNTIME regions.
66 static int __init
riscv_enable_runtime_services(void)
70 if (!efi_enabled(EFI_BOOT
)) {
71 pr_info("EFI services will not be available.\n");
77 mapsize
= efi
.memmap
.desc_size
* efi
.memmap
.nr_map
;
79 if (efi_memmap_init_late(efi
.memmap
.phys_map
, mapsize
)) {
80 pr_err("Failed to remap EFI memory map\n");
84 if (efi_soft_reserve_enabled()) {
85 efi_memory_desc_t
*md
;
87 for_each_efi_memory_desc(md
) {
88 u64 md_size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
91 if (!(md
->attribute
& EFI_MEMORY_SP
))
94 res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
98 res
->start
= md
->phys_addr
;
99 res
->end
= md
->phys_addr
+ md_size
- 1;
100 res
->name
= "Soft Reserved";
101 res
->flags
= IORESOURCE_MEM
;
102 res
->desc
= IORES_DESC_SOFT_RESERVED
;
104 insert_resource(&iomem_resource
, res
);
108 if (efi_runtime_disabled()) {
109 pr_info("EFI runtime services will be disabled.\n");
113 if (efi_enabled(EFI_RUNTIME_SERVICES
)) {
114 pr_info("EFI runtime services access via paravirt.\n");
118 pr_info("Remapping and enabling EFI services.\n");
120 if (!efi_virtmap_init()) {
121 pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
125 /* Set up runtime services function pointers */
126 efi_native_runtime_setup();
127 set_bit(EFI_RUNTIME_SERVICES
, &efi
.flags
);
131 early_initcall(riscv_enable_runtime_services
);
133 static void efi_virtmap_load(void)
136 switch_mm(current
->active_mm
, &efi_mm
, NULL
);
139 static void efi_virtmap_unload(void)
141 switch_mm(&efi_mm
, current
->active_mm
, NULL
);
145 void arch_efi_call_virt_setup(void)
147 sync_kernel_mappings(efi_mm
.pgd
);
151 void arch_efi_call_virt_teardown(void)
153 efi_virtmap_unload();
156 static int __init
riscv_dmi_init(void)
159 * On riscv, DMI depends on UEFI, and dmi_setup() needs to
160 * be called early because dmi_id_init(), which is an arch_initcall
161 * itself, depends on dmi_scan_machine() having been called already.
167 core_initcall(riscv_dmi_init
);