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 uefi_debug __initdata
;
55 static int __init
uefi_debug_setup(char *str
)
61 early_param("uefi_debug", uefi_debug_setup
);
63 static int __init
is_normal_ram(efi_memory_desc_t
*md
)
65 if (md
->attribute
& EFI_MEMORY_WB
)
71 * Translate a EFI virtual address into a physical address: this is necessary,
72 * as some data members of the EFI system table are virtually remapped after
73 * SetVirtualAddressMap() has been called.
75 static phys_addr_t
efi_to_phys(unsigned long addr
)
77 efi_memory_desc_t
*md
;
79 for_each_efi_memory_desc(&memmap
, md
) {
80 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
82 if (md
->virt_addr
== 0)
83 /* no virtual mapping has been installed by the stub */
85 if (md
->virt_addr
<= addr
&&
86 (addr
- md
->virt_addr
) < (md
->num_pages
<< EFI_PAGE_SHIFT
))
87 return md
->phys_addr
+ addr
- md
->virt_addr
;
92 static int __init
uefi_init(void)
97 char vendor
[100] = "unknown";
100 efi
.systab
= early_memremap(efi_system_table
,
101 sizeof(efi_system_table_t
));
102 if (efi
.systab
== NULL
) {
103 pr_warn("Unable to map EFI system table.\n");
107 set_bit(EFI_BOOT
, &efi
.flags
);
108 set_bit(EFI_64BIT
, &efi
.flags
);
111 * Verify the EFI Table
113 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
114 pr_err("System table signature incorrect\n");
118 if ((efi
.systab
->hdr
.revision
>> 16) < 2)
119 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
120 efi
.systab
->hdr
.revision
>> 16,
121 efi
.systab
->hdr
.revision
& 0xffff);
123 /* Show what we know for posterity */
124 c16
= early_memremap(efi_to_phys(efi
.systab
->fw_vendor
),
127 for (i
= 0; i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
130 early_memunmap(c16
, sizeof(vendor
));
133 pr_info("EFI v%u.%.02u by %s\n",
134 efi
.systab
->hdr
.revision
>> 16,
135 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
137 table_size
= sizeof(efi_config_table_64_t
) * efi
.systab
->nr_tables
;
138 config_tables
= early_memremap(efi_to_phys(efi
.systab
->tables
),
141 retval
= efi_config_parse_tables(config_tables
, efi
.systab
->nr_tables
,
142 sizeof(efi_config_table_64_t
), NULL
);
144 early_memunmap(config_tables
, table_size
);
146 early_memunmap(efi
.systab
, sizeof(efi_system_table_t
));
151 * Return true for RAM regions we want to permanently reserve.
153 static __init
int is_reserve_region(efi_memory_desc_t
*md
)
156 case EFI_LOADER_CODE
:
157 case EFI_LOADER_DATA
:
158 case EFI_BOOT_SERVICES_CODE
:
159 case EFI_BOOT_SERVICES_DATA
:
160 case EFI_CONVENTIONAL_MEMORY
:
165 return is_normal_ram(md
);
168 static __init
void reserve_regions(void)
170 efi_memory_desc_t
*md
;
171 u64 paddr
, npages
, size
;
174 pr_info("Processing EFI memory map:\n");
176 for_each_efi_memory_desc(&memmap
, md
) {
177 paddr
= md
->phys_addr
;
178 npages
= md
->num_pages
;
183 pr_info(" 0x%012llx-0x%012llx %s",
184 paddr
, paddr
+ (npages
<< EFI_PAGE_SHIFT
) - 1,
185 efi_md_typeattr_format(buf
, sizeof(buf
), md
));
188 memrange_efi_to_native(&paddr
, &npages
);
189 size
= npages
<< PAGE_SHIFT
;
191 if (is_normal_ram(md
))
192 early_init_dt_add_memory_arch(paddr
, size
);
194 if (is_reserve_region(md
)) {
195 memblock_reserve(paddr
, size
);
204 set_bit(EFI_MEMMAP
, &efi
.flags
);
207 void __init
efi_init(void)
209 struct efi_fdt_params params
;
211 /* Grab UEFI information placed in FDT by stub */
212 if (!efi_get_fdt_params(¶ms
, uefi_debug
))
215 efi_system_table
= params
.system_table
;
217 memblock_reserve(params
.mmap
& PAGE_MASK
,
218 PAGE_ALIGN(params
.mmap_size
+ (params
.mmap
& ~PAGE_MASK
)));
219 memmap
.phys_map
= (void *)params
.mmap
;
220 memmap
.map
= early_memremap(params
.mmap
, params
.mmap_size
);
221 memmap
.map_end
= memmap
.map
+ params
.mmap_size
;
222 memmap
.desc_size
= params
.desc_size
;
223 memmap
.desc_version
= params
.desc_ver
;
229 early_memunmap(memmap
.map
, params
.mmap_size
);
232 static bool __init
efi_virtmap_init(void)
234 efi_memory_desc_t
*md
;
236 for_each_efi_memory_desc(&memmap
, md
) {
237 u64 paddr
, npages
, size
;
240 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
242 if (md
->virt_addr
== 0)
245 paddr
= md
->phys_addr
;
246 npages
= md
->num_pages
;
247 memrange_efi_to_native(&paddr
, &npages
);
248 size
= npages
<< PAGE_SHIFT
;
250 pr_info(" EFI remap 0x%016llx => %p\n",
251 md
->phys_addr
, (void *)md
->virt_addr
);
254 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
255 * executable, everything else can be mapped with the XN bits
258 if (!is_normal_ram(md
))
259 prot
= __pgprot(PROT_DEVICE_nGnRE
);
260 else if (md
->type
== EFI_RUNTIME_SERVICES_CODE
)
261 prot
= PAGE_KERNEL_EXEC
;
265 create_pgd_mapping(&efi_mm
, paddr
, md
->virt_addr
, size
, prot
);
271 * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
272 * non-early mapping of the UEFI system table and virtual mappings for all
273 * EFI_MEMORY_RUNTIME regions.
275 static int __init
arm64_enable_runtime_services(void)
279 if (!efi_enabled(EFI_BOOT
)) {
280 pr_info("EFI services will not be available.\n");
284 if (efi_runtime_disabled()) {
285 pr_info("EFI runtime services will be disabled.\n");
289 pr_info("Remapping and enabling EFI services.\n");
291 mapsize
= memmap
.map_end
- memmap
.map
;
292 memmap
.map
= (__force
void *)ioremap_cache((phys_addr_t
)memmap
.phys_map
,
295 pr_err("Failed to remap EFI memory map\n");
298 memmap
.map_end
= memmap
.map
+ mapsize
;
299 efi
.memmap
= &memmap
;
301 efi
.systab
= (__force
void *)ioremap_cache(efi_system_table
,
302 sizeof(efi_system_table_t
));
304 pr_err("Failed to remap EFI System Table\n");
307 set_bit(EFI_SYSTEM_TABLES
, &efi
.flags
);
309 if (!efi_virtmap_init()) {
310 pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
314 /* Set up runtime services function pointers */
315 efi_native_runtime_setup();
316 set_bit(EFI_RUNTIME_SERVICES
, &efi
.flags
);
318 efi
.runtime_version
= efi
.systab
->hdr
.revision
;
322 early_initcall(arm64_enable_runtime_services
);
324 static int __init
arm64_dmi_init(void)
327 * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
328 * be called early because dmi_id_init(), which is an arch_initcall
329 * itself, depends on dmi_scan_machine() having been called already.
333 dmi_set_dump_stack_arch_desc();
336 core_initcall(arm64_dmi_init
);
338 static void efi_set_pgd(struct mm_struct
*mm
)
340 cpu_switch_mm(mm
->pgd
, mm
);
342 if (icache_is_aivivt())
343 __flush_icache_all();
346 void efi_virtmap_load(void)
349 efi_set_pgd(&efi_mm
);
352 void efi_virtmap_unload(void)
354 efi_set_pgd(current
->active_mm
);