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 int uefi_debug __initdata;
42 static int __init uefi_debug_setup(char *str)
48 early_param("uefi_debug", uefi_debug_setup);
50 static int __init is_normal_ram(efi_memory_desc_t *md)
52 if (md->attribute & EFI_MEMORY_WB)
57 static void __init efi_setup_idmap(void)
59 struct memblock_region *r;
60 efi_memory_desc_t *md;
61 u64 paddr, npages, size;
63 for_each_memblock(memory, r)
64 create_id_mapping(r->base, r->size, 0);
66 /* map runtime io spaces */
67 for_each_efi_memory_desc(&memmap, md) {
68 if (!(md->attribute & EFI_MEMORY_RUNTIME) || is_normal_ram(md))
70 paddr = md->phys_addr;
71 npages = md->num_pages;
72 memrange_efi_to_native(&paddr, &npages);
73 size = npages << PAGE_SHIFT;
74 create_id_mapping(paddr, size, 1);
79 * Translate a EFI virtual address into a physical address: this is necessary,
80 * as some data members of the EFI system table are virtually remapped after
81 * SetVirtualAddressMap() has been called.
83 static phys_addr_t efi_to_phys(unsigned long addr)
85 efi_memory_desc_t *md;
87 for_each_efi_memory_desc(&memmap, md) {
88 if (!(md->attribute & EFI_MEMORY_RUNTIME))
90 if (md->virt_addr == 0)
91 /* no virtual mapping has been installed by the stub */
93 if (md->virt_addr <= addr &&
94 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
95 return md->phys_addr + addr - md->virt_addr;
100 static int __init uefi_init(void)
105 char vendor[100] = "unknown";
108 efi.systab = early_memremap(efi_system_table,
109 sizeof(efi_system_table_t));
110 if (efi.systab == NULL) {
111 pr_warn("Unable to map EFI system table.\n");
115 set_bit(EFI_BOOT, &efi.flags);
116 set_bit(EFI_64BIT, &efi.flags);
119 * Verify the EFI Table
121 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
122 pr_err("System table signature incorrect\n");
126 if ((efi.systab->hdr.revision >> 16) < 2)
127 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
128 efi.systab->hdr.revision >> 16,
129 efi.systab->hdr.revision & 0xffff);
131 /* Show what we know for posterity */
132 c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
135 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
138 early_memunmap(c16, sizeof(vendor));
141 pr_info("EFI v%u.%.02u by %s\n",
142 efi.systab->hdr.revision >> 16,
143 efi.systab->hdr.revision & 0xffff, vendor);
145 table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
146 config_tables = early_memremap(efi_to_phys(efi.systab->tables),
149 retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
150 sizeof(efi_config_table_64_t), NULL);
152 early_memunmap(config_tables, table_size);
154 early_memunmap(efi.systab, sizeof(efi_system_table_t));
159 * Return true for RAM regions we want to permanently reserve.
161 static __init int is_reserve_region(efi_memory_desc_t *md)
164 case EFI_LOADER_CODE:
165 case EFI_LOADER_DATA:
166 case EFI_BOOT_SERVICES_CODE:
167 case EFI_BOOT_SERVICES_DATA:
168 case EFI_CONVENTIONAL_MEMORY:
173 return is_normal_ram(md);
176 static __init void reserve_regions(void)
178 efi_memory_desc_t *md;
179 u64 paddr, npages, size;
182 pr_info("Processing EFI memory map:\n");
184 for_each_efi_memory_desc(&memmap, md) {
185 paddr = md->phys_addr;
186 npages = md->num_pages;
191 pr_info(" 0x%012llx-0x%012llx %s",
192 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
193 efi_md_typeattr_format(buf, sizeof(buf), md));
196 memrange_efi_to_native(&paddr, &npages);
197 size = npages << PAGE_SHIFT;
199 if (is_normal_ram(md))
200 early_init_dt_add_memory_arch(paddr, size);
202 if (is_reserve_region(md) ||
203 md->type == EFI_BOOT_SERVICES_CODE ||
204 md->type == EFI_BOOT_SERVICES_DATA) {
205 memblock_reserve(paddr, size);
214 set_bit(EFI_MEMMAP, &efi.flags);
218 static u64 __init free_one_region(u64 start, u64 end)
220 u64 size = end - start;
223 pr_info(" EFI freeing: 0x%012llx-0x%012llx\n", start, end - 1);
225 free_bootmem_late(start, size);
229 static u64 __init free_region(u64 start, u64 end)
231 u64 map_start, map_end, total = 0;
236 map_start = (u64)memmap.phys_map;
237 map_end = PAGE_ALIGN(map_start + (memmap.map_end - memmap.map));
238 map_start &= PAGE_MASK;
240 if (start < map_end && end > map_start) {
241 /* region overlaps UEFI memmap */
242 if (start < map_start)
243 total += free_one_region(start, map_start);
246 total += free_one_region(map_end, end);
248 total += free_one_region(start, end);
253 static void __init free_boot_services(void)
256 u64 keep_end, free_start, free_end;
257 efi_memory_desc_t *md;
260 * If kernel uses larger pages than UEFI, we have to be careful
261 * not to inadvertantly free memory we want to keep if there is
262 * overlap at the kernel page size alignment. We do not want to
263 * free is_reserve_region() memory nor the UEFI memmap itself.
265 * The memory map is sorted, so we keep track of the end of
266 * any previous region we want to keep, remember any region
267 * we want to free and defer freeing it until we encounter
268 * the next region we want to keep. This way, before freeing
269 * it, we can clip it as needed to avoid freeing memory we
270 * want to keep for UEFI.
276 for_each_efi_memory_desc(&memmap, md) {
277 u64 paddr, npages, size;
279 if (is_reserve_region(md)) {
281 * We don't want to free any memory from this region.
284 /* adjust free_end then free region */
285 if (free_end > md->phys_addr)
286 free_end -= PAGE_SIZE;
287 total_freed += free_region(free_start, free_end);
290 keep_end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
294 if (md->type != EFI_BOOT_SERVICES_CODE &&
295 md->type != EFI_BOOT_SERVICES_DATA) {
296 /* no need to free this region */
301 * We want to free memory from this region.
303 paddr = md->phys_addr;
304 npages = md->num_pages;
305 memrange_efi_to_native(&paddr, &npages);
306 size = npages << PAGE_SHIFT;
309 if (paddr <= free_end)
310 free_end = paddr + size;
312 total_freed += free_region(free_start, free_end);
314 free_end = paddr + size;
318 free_end = paddr + size;
320 if (free_start < keep_end) {
321 free_start += PAGE_SIZE;
322 if (free_start >= free_end)
327 total_freed += free_region(free_start, free_end);
330 pr_info("Freed 0x%llx bytes of EFI boot services memory",
334 void __init efi_init(void)
336 struct efi_fdt_params params;
338 /* Grab UEFI information placed in FDT by stub */
339 if (!efi_get_fdt_params(¶ms, uefi_debug))
342 efi_system_table = params.system_table;
344 memblock_reserve(params.mmap & PAGE_MASK,
345 PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
346 memmap.phys_map = (void *)params.mmap;
347 memmap.map = early_memremap(params.mmap, params.mmap_size);
348 memmap.map_end = memmap.map + params.mmap_size;
349 memmap.desc_size = params.desc_size;
350 memmap.desc_version = params.desc_ver;
358 void __init efi_idmap_init(void)
360 if (!efi_enabled(EFI_BOOT))
363 /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
365 early_memunmap(memmap.map, memmap.map_end - memmap.map);
369 * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
370 * non-early mapping of the UEFI system table and virtual mappings for all
371 * EFI_MEMORY_RUNTIME regions.
373 static int __init arm64_enable_runtime_services(void)
377 if (!efi_enabled(EFI_BOOT)) {
378 pr_info("EFI services will not be available.\n");
382 mapsize = memmap.map_end - memmap.map;
384 if (efi_runtime_disabled()) {
385 pr_info("EFI runtime services will be disabled.\n");
389 pr_info("Remapping and enabling EFI services.\n");
390 /* replace early memmap mapping with permanent mapping */
391 memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
393 memmap.map_end = memmap.map + mapsize;
395 efi.memmap = &memmap;
397 efi.systab = (__force void *)ioremap_cache(efi_system_table,
398 sizeof(efi_system_table_t));
400 pr_err("Failed to remap EFI System Table\n");
403 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
405 free_boot_services();
407 if (!efi_enabled(EFI_VIRTMAP)) {
408 pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
412 /* Set up runtime services function pointers */
413 efi_native_runtime_setup();
414 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
416 efi.runtime_version = efi.systab->hdr.revision;
420 early_initcall(arm64_enable_runtime_services);
422 static int __init arm64_dmi_init(void)
425 * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
426 * be called early because dmi_id_init(), which is an arch_initcall
427 * itself, depends on dmi_scan_machine() having been called already.
431 dmi_set_dump_stack_arch_desc();
434 core_initcall(arm64_dmi_init);
436 static pgd_t efi_pgd[PTRS_PER_PGD] __page_aligned_bss;
438 static struct mm_struct efi_mm = {
441 .mm_users = ATOMIC_INIT(2),
442 .mm_count = ATOMIC_INIT(1),
443 .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
444 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
445 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
446 INIT_MM_CONTEXT(efi_mm)
449 static void efi_set_pgd(struct mm_struct *mm)
451 cpu_switch_mm(mm->pgd, mm);
453 if (icache_is_aivivt())
454 __flush_icache_all();
457 void efi_virtmap_load(void)
460 efi_set_pgd(&efi_mm);
463 void efi_virtmap_unload(void)
465 efi_set_pgd(current->active_mm);
469 void __init efi_virtmap_init(void)
471 efi_memory_desc_t *md;
473 if (!efi_enabled(EFI_BOOT))
476 for_each_efi_memory_desc(&memmap, md) {
477 u64 paddr, npages, size;
480 if (!(md->attribute & EFI_MEMORY_RUNTIME))
482 if (WARN(md->virt_addr == 0,
483 "UEFI virtual mapping incomplete or missing -- no entry found for 0x%llx\n",
487 paddr = md->phys_addr;
488 npages = md->num_pages;
489 memrange_efi_to_native(&paddr, &npages);
490 size = npages << PAGE_SHIFT;
492 pr_info(" EFI remap 0x%016llx => %p\n",
493 md->phys_addr, (void *)md->virt_addr);
496 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
497 * executable, everything else can be mapped with the XN bits
500 if (!is_normal_ram(md))
501 prot = __pgprot(PROT_DEVICE_nGnRE);
502 else if (md->type == EFI_RUNTIME_SERVICES_CODE)
503 prot = PAGE_KERNEL_EXEC;
507 create_pgd_mapping(&efi_mm, paddr, md->virt_addr, size, prot);
509 set_bit(EFI_VIRTMAP, &efi.flags);