2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 2.4
6 * Copyright (C) 2013, 2014 Linaro Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/atomic.h>
15 #include <linux/dmi.h>
16 #include <linux/efi.h>
17 #include <linux/export.h>
18 #include <linux/memblock.h>
19 #include <linux/mm_types.h>
20 #include <linux/bootmem.h>
22 #include <linux/of_fdt.h>
23 #include <linux/preempt.h>
24 #include <linux/rbtree.h>
25 #include <linux/rwsem.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
30 #include <asm/cacheflush.h>
32 #include <asm/tlbflush.h>
33 #include <asm/mmu_context.h>
35 #include <asm/pgtable.h>
37 struct efi_memory_map memmap
;
39 static u64 efi_system_table
;
41 static pgd_t efi_pgd
[PTRS_PER_PGD
] __page_aligned_bss
;
43 static struct mm_struct efi_mm
= {
46 .mm_users
= ATOMIC_INIT(2),
47 .mm_count
= ATOMIC_INIT(1),
48 .mmap_sem
= __RWSEM_INITIALIZER(efi_mm
.mmap_sem
),
49 .page_table_lock
= __SPIN_LOCK_UNLOCKED(efi_mm
.page_table_lock
),
50 .mmlist
= LIST_HEAD_INIT(efi_mm
.mmlist
),
51 INIT_MM_CONTEXT(efi_mm
)
54 static int __init
is_normal_ram(efi_memory_desc_t
*md
)
56 if (md
->attribute
& EFI_MEMORY_WB
)
62 * Translate a EFI virtual address into a physical address: this is necessary,
63 * as some data members of the EFI system table are virtually remapped after
64 * SetVirtualAddressMap() has been called.
66 static phys_addr_t
efi_to_phys(unsigned long addr
)
68 efi_memory_desc_t
*md
;
70 for_each_efi_memory_desc(&memmap
, md
) {
71 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
73 if (md
->virt_addr
== 0)
74 /* no virtual mapping has been installed by the stub */
76 if (md
->virt_addr
<= addr
&&
77 (addr
- md
->virt_addr
) < (md
->num_pages
<< EFI_PAGE_SHIFT
))
78 return md
->phys_addr
+ addr
- md
->virt_addr
;
83 static int __init
uefi_init(void)
88 char vendor
[100] = "unknown";
91 efi
.systab
= early_memremap(efi_system_table
,
92 sizeof(efi_system_table_t
));
93 if (efi
.systab
== NULL
) {
94 pr_warn("Unable to map EFI system table.\n");
98 set_bit(EFI_BOOT
, &efi
.flags
);
99 set_bit(EFI_64BIT
, &efi
.flags
);
102 * Verify the EFI Table
104 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
105 pr_err("System table signature incorrect\n");
109 if ((efi
.systab
->hdr
.revision
>> 16) < 2)
110 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
111 efi
.systab
->hdr
.revision
>> 16,
112 efi
.systab
->hdr
.revision
& 0xffff);
114 /* Show what we know for posterity */
115 c16
= early_memremap(efi_to_phys(efi
.systab
->fw_vendor
),
116 sizeof(vendor
) * sizeof(efi_char16_t
));
118 for (i
= 0; i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
121 early_memunmap(c16
, sizeof(vendor
) * sizeof(efi_char16_t
));
124 pr_info("EFI v%u.%.02u by %s\n",
125 efi
.systab
->hdr
.revision
>> 16,
126 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
128 table_size
= sizeof(efi_config_table_64_t
) * efi
.systab
->nr_tables
;
129 config_tables
= early_memremap(efi_to_phys(efi
.systab
->tables
),
132 retval
= efi_config_parse_tables(config_tables
, efi
.systab
->nr_tables
,
133 sizeof(efi_config_table_64_t
), NULL
);
135 early_memunmap(config_tables
, table_size
);
137 early_memunmap(efi
.systab
, sizeof(efi_system_table_t
));
142 * Return true for RAM regions we want to permanently reserve.
144 static __init
int is_reserve_region(efi_memory_desc_t
*md
)
147 case EFI_LOADER_CODE
:
148 case EFI_LOADER_DATA
:
149 case EFI_BOOT_SERVICES_CODE
:
150 case EFI_BOOT_SERVICES_DATA
:
151 case EFI_CONVENTIONAL_MEMORY
:
152 case EFI_PERSISTENT_MEMORY
:
157 return is_normal_ram(md
);
160 static __init
void reserve_regions(void)
162 efi_memory_desc_t
*md
;
163 u64 paddr
, npages
, size
;
165 if (efi_enabled(EFI_DBG
))
166 pr_info("Processing EFI memory map:\n");
168 for_each_efi_memory_desc(&memmap
, md
) {
169 paddr
= md
->phys_addr
;
170 npages
= md
->num_pages
;
172 if (efi_enabled(EFI_DBG
)) {
175 pr_info(" 0x%012llx-0x%012llx %s",
176 paddr
, paddr
+ (npages
<< EFI_PAGE_SHIFT
) - 1,
177 efi_md_typeattr_format(buf
, sizeof(buf
), md
));
180 memrange_efi_to_native(&paddr
, &npages
);
181 size
= npages
<< PAGE_SHIFT
;
183 if (is_normal_ram(md
))
184 early_init_dt_add_memory_arch(paddr
, size
);
186 if (is_reserve_region(md
)) {
187 memblock_reserve(paddr
, size
);
188 if (efi_enabled(EFI_DBG
))
192 if (efi_enabled(EFI_DBG
))
196 set_bit(EFI_MEMMAP
, &efi
.flags
);
199 void __init
efi_init(void)
201 struct efi_fdt_params params
;
203 /* Grab UEFI information placed in FDT by stub */
204 if (!efi_get_fdt_params(¶ms
))
207 efi_system_table
= params
.system_table
;
209 memblock_reserve(params
.mmap
& PAGE_MASK
,
210 PAGE_ALIGN(params
.mmap_size
+ (params
.mmap
& ~PAGE_MASK
)));
211 memmap
.phys_map
= (void *)params
.mmap
;
212 memmap
.map
= early_memremap(params
.mmap
, params
.mmap_size
);
213 memmap
.map_end
= memmap
.map
+ params
.mmap_size
;
214 memmap
.desc_size
= params
.desc_size
;
215 memmap
.desc_version
= params
.desc_ver
;
221 early_memunmap(memmap
.map
, params
.mmap_size
);
224 static bool __init
efi_virtmap_init(void)
226 efi_memory_desc_t
*md
;
228 for_each_efi_memory_desc(&memmap
, md
) {
229 u64 paddr
, npages
, size
;
232 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
234 if (md
->virt_addr
== 0)
237 paddr
= md
->phys_addr
;
238 npages
= md
->num_pages
;
239 memrange_efi_to_native(&paddr
, &npages
);
240 size
= npages
<< PAGE_SHIFT
;
242 pr_info(" EFI remap 0x%016llx => %p\n",
243 md
->phys_addr
, (void *)md
->virt_addr
);
246 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
247 * executable, everything else can be mapped with the XN bits
250 if (!is_normal_ram(md
))
251 prot
= __pgprot(PROT_DEVICE_nGnRE
);
252 else if (md
->type
== EFI_RUNTIME_SERVICES_CODE
||
253 !PAGE_ALIGNED(md
->phys_addr
))
254 prot
= PAGE_KERNEL_EXEC
;
258 create_pgd_mapping(&efi_mm
, paddr
, md
->virt_addr
, size
, prot
);
264 * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
265 * non-early mapping of the UEFI system table and virtual mappings for all
266 * EFI_MEMORY_RUNTIME regions.
268 static int __init
arm64_enable_runtime_services(void)
272 if (!efi_enabled(EFI_BOOT
)) {
273 pr_info("EFI services will not be available.\n");
277 if (efi_runtime_disabled()) {
278 pr_info("EFI runtime services will be disabled.\n");
282 pr_info("Remapping and enabling EFI services.\n");
284 mapsize
= memmap
.map_end
- memmap
.map
;
285 memmap
.map
= (__force
void *)ioremap_cache((phys_addr_t
)memmap
.phys_map
,
288 pr_err("Failed to remap EFI memory map\n");
291 memmap
.map_end
= memmap
.map
+ mapsize
;
292 efi
.memmap
= &memmap
;
294 efi
.systab
= (__force
void *)ioremap_cache(efi_system_table
,
295 sizeof(efi_system_table_t
));
297 pr_err("Failed to remap EFI System Table\n");
300 set_bit(EFI_SYSTEM_TABLES
, &efi
.flags
);
302 if (!efi_virtmap_init()) {
303 pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
307 /* Set up runtime services function pointers */
308 efi_native_runtime_setup();
309 set_bit(EFI_RUNTIME_SERVICES
, &efi
.flags
);
311 efi
.runtime_version
= efi
.systab
->hdr
.revision
;
315 early_initcall(arm64_enable_runtime_services
);
317 static int __init
arm64_dmi_init(void)
320 * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
321 * be called early because dmi_id_init(), which is an arch_initcall
322 * itself, depends on dmi_scan_machine() having been called already.
326 dmi_set_dump_stack_arch_desc();
329 core_initcall(arm64_dmi_init
);
331 static void efi_set_pgd(struct mm_struct
*mm
)
334 cpu_set_reserved_ttbr0();
336 cpu_switch_mm(mm
->pgd
, mm
);
339 if (icache_is_aivivt())
340 __flush_icache_all();
343 void efi_virtmap_load(void)
346 efi_set_pgd(&efi_mm
);
349 void efi_virtmap_unload(void)
351 efi_set_pgd(current
->active_mm
);
356 * UpdateCapsule() depends on the system being shutdown via
359 bool efi_poweroff_required(void)
361 return efi_enabled(EFI_RUNTIME_SERVICES
);