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/dmi.h>
15 #include <linux/efi.h>
16 #include <linux/export.h>
17 #include <linux/memblock.h>
18 #include <linux/bootmem.h>
20 #include <linux/of_fdt.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 #include <asm/cacheflush.h>
26 #include <asm/tlbflush.h>
27 #include <asm/mmu_context.h>
29 struct efi_memory_map memmap;
31 static efi_runtime_services_t *runtime;
33 static u64 efi_system_table;
35 static int uefi_debug __initdata;
36 static int __init uefi_debug_setup(char *str)
42 early_param("uefi_debug", uefi_debug_setup);
44 static int __init is_normal_ram(efi_memory_desc_t *md)
46 if (md->attribute & EFI_MEMORY_WB)
51 static void __init efi_setup_idmap(void)
53 struct memblock_region *r;
54 efi_memory_desc_t *md;
55 u64 paddr, npages, size;
57 for_each_memblock(memory, r)
58 create_id_mapping(r->base, r->size, 0);
60 /* map runtime io spaces */
61 for_each_efi_memory_desc(&memmap, md) {
62 if (!(md->attribute & EFI_MEMORY_RUNTIME) || is_normal_ram(md))
64 paddr = md->phys_addr;
65 npages = md->num_pages;
66 memrange_efi_to_native(&paddr, &npages);
67 size = npages << PAGE_SHIFT;
68 create_id_mapping(paddr, size, 1);
72 static int __init uefi_init(void)
75 char vendor[100] = "unknown";
78 efi.systab = early_memremap(efi_system_table,
79 sizeof(efi_system_table_t));
80 if (efi.systab == NULL) {
81 pr_warn("Unable to map EFI system table.\n");
85 set_bit(EFI_BOOT, &efi.flags);
86 set_bit(EFI_64BIT, &efi.flags);
89 * Verify the EFI Table
91 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
92 pr_err("System table signature incorrect\n");
96 if ((efi.systab->hdr.revision >> 16) < 2)
97 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
98 efi.systab->hdr.revision >> 16,
99 efi.systab->hdr.revision & 0xffff);
101 /* Show what we know for posterity */
102 c16 = early_memremap(efi.systab->fw_vendor,
105 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
108 early_memunmap(c16, sizeof(vendor));
111 pr_info("EFI v%u.%.02u by %s\n",
112 efi.systab->hdr.revision >> 16,
113 efi.systab->hdr.revision & 0xffff, vendor);
115 retval = efi_config_init(NULL);
118 early_memunmap(efi.systab, sizeof(efi_system_table_t));
123 * Return true for RAM regions we want to permanently reserve.
125 static __init int is_reserve_region(efi_memory_desc_t *md)
128 case EFI_LOADER_CODE:
129 case EFI_LOADER_DATA:
130 case EFI_BOOT_SERVICES_CODE:
131 case EFI_BOOT_SERVICES_DATA:
132 case EFI_CONVENTIONAL_MEMORY:
137 return is_normal_ram(md);
140 static __init void reserve_regions(void)
142 efi_memory_desc_t *md;
143 u64 paddr, npages, size;
146 pr_info("Processing EFI memory map:\n");
148 for_each_efi_memory_desc(&memmap, md) {
149 paddr = md->phys_addr;
150 npages = md->num_pages;
155 pr_info(" 0x%012llx-0x%012llx %s",
156 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
157 efi_md_typeattr_format(buf, sizeof(buf), md));
160 memrange_efi_to_native(&paddr, &npages);
161 size = npages << PAGE_SHIFT;
163 if (is_normal_ram(md))
164 early_init_dt_add_memory_arch(paddr, size);
166 if (is_reserve_region(md) ||
167 md->type == EFI_BOOT_SERVICES_CODE ||
168 md->type == EFI_BOOT_SERVICES_DATA) {
169 memblock_reserve(paddr, size);
178 set_bit(EFI_MEMMAP, &efi.flags);
182 static u64 __init free_one_region(u64 start, u64 end)
184 u64 size = end - start;
187 pr_info(" EFI freeing: 0x%012llx-0x%012llx\n", start, end - 1);
189 free_bootmem_late(start, size);
193 static u64 __init free_region(u64 start, u64 end)
195 u64 map_start, map_end, total = 0;
200 map_start = (u64)memmap.phys_map;
201 map_end = PAGE_ALIGN(map_start + (memmap.map_end - memmap.map));
202 map_start &= PAGE_MASK;
204 if (start < map_end && end > map_start) {
205 /* region overlaps UEFI memmap */
206 if (start < map_start)
207 total += free_one_region(start, map_start);
210 total += free_one_region(map_end, end);
212 total += free_one_region(start, end);
217 static void __init free_boot_services(void)
220 u64 keep_end, free_start, free_end;
221 efi_memory_desc_t *md;
224 * If kernel uses larger pages than UEFI, we have to be careful
225 * not to inadvertantly free memory we want to keep if there is
226 * overlap at the kernel page size alignment. We do not want to
227 * free is_reserve_region() memory nor the UEFI memmap itself.
229 * The memory map is sorted, so we keep track of the end of
230 * any previous region we want to keep, remember any region
231 * we want to free and defer freeing it until we encounter
232 * the next region we want to keep. This way, before freeing
233 * it, we can clip it as needed to avoid freeing memory we
234 * want to keep for UEFI.
240 for_each_efi_memory_desc(&memmap, md) {
241 u64 paddr, npages, size;
243 if (is_reserve_region(md)) {
245 * We don't want to free any memory from this region.
248 /* adjust free_end then free region */
249 if (free_end > md->phys_addr)
250 free_end -= PAGE_SIZE;
251 total_freed += free_region(free_start, free_end);
254 keep_end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
258 if (md->type != EFI_BOOT_SERVICES_CODE &&
259 md->type != EFI_BOOT_SERVICES_DATA) {
260 /* no need to free this region */
265 * We want to free memory from this region.
267 paddr = md->phys_addr;
268 npages = md->num_pages;
269 memrange_efi_to_native(&paddr, &npages);
270 size = npages << PAGE_SHIFT;
273 if (paddr <= free_end)
274 free_end = paddr + size;
276 total_freed += free_region(free_start, free_end);
278 free_end = paddr + size;
282 free_end = paddr + size;
284 if (free_start < keep_end) {
285 free_start += PAGE_SIZE;
286 if (free_start >= free_end)
291 total_freed += free_region(free_start, free_end);
294 pr_info("Freed 0x%llx bytes of EFI boot services memory",
298 void __init efi_init(void)
300 struct efi_fdt_params params;
302 /* Grab UEFI information placed in FDT by stub */
303 if (!efi_get_fdt_params(¶ms, uefi_debug))
306 efi_system_table = params.system_table;
308 memblock_reserve(params.mmap & PAGE_MASK,
309 PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
310 memmap.phys_map = (void *)params.mmap;
311 memmap.map = early_memremap(params.mmap, params.mmap_size);
312 memmap.map_end = memmap.map + params.mmap_size;
313 memmap.desc_size = params.desc_size;
314 memmap.desc_version = params.desc_ver;
322 void __init efi_idmap_init(void)
324 if (!efi_enabled(EFI_BOOT))
327 /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
331 static int __init remap_region(efi_memory_desc_t *md, void **new)
333 u64 paddr, vaddr, npages, size;
335 paddr = md->phys_addr;
336 npages = md->num_pages;
337 memrange_efi_to_native(&paddr, &npages);
338 size = npages << PAGE_SHIFT;
340 if (is_normal_ram(md))
341 vaddr = (__force u64)ioremap_cache(paddr, size);
343 vaddr = (__force u64)ioremap(paddr, size);
346 pr_err("Unable to remap 0x%llx pages @ %p\n",
347 npages, (void *)paddr);
351 /* adjust for any rounding when EFI and system pagesize differs */
352 md->virt_addr = vaddr + (md->phys_addr - paddr);
355 pr_info(" EFI remap 0x%012llx => %p\n",
356 md->phys_addr, (void *)md->virt_addr);
358 memcpy(*new, md, memmap.desc_size);
359 *new += memmap.desc_size;
365 * Switch UEFI from an identity map to a kernel virtual map
367 static int __init arm64_enter_virtual_mode(void)
369 efi_memory_desc_t *md;
370 phys_addr_t virtmap_phys;
371 void *virtmap, *virt_md;
377 if (!efi_enabled(EFI_BOOT)) {
378 pr_info("EFI services will not be available.\n");
382 mapsize = memmap.map_end - memmap.map;
383 early_memunmap(memmap.map, mapsize);
385 if (efi_runtime_disabled()) {
386 pr_info("EFI runtime services will be disabled.\n");
390 pr_info("Remapping and enabling EFI services.\n");
391 /* replace early memmap mapping with permanent mapping */
392 memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
394 memmap.map_end = memmap.map + mapsize;
396 efi.memmap = &memmap;
398 /* Map the runtime regions */
399 virtmap = kmalloc(mapsize, GFP_KERNEL);
401 pr_err("Failed to allocate EFI virtual memmap\n");
404 virtmap_phys = virt_to_phys(virtmap);
407 for_each_efi_memory_desc(&memmap, md) {
408 if (!(md->attribute & EFI_MEMORY_RUNTIME))
410 if (!remap_region(md, &virt_md))
415 efi.systab = (__force void *)efi_lookup_mapped_addr(efi_system_table);
418 * If we have no virtual mapping for the System Table at this
419 * point, the memory map doesn't cover the physical offset where
420 * it resides. This means the System Table will be inaccessible
421 * to Runtime Services themselves once the virtual mapping is
424 pr_err("Failed to remap EFI System Table -- buggy firmware?\n");
427 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
429 local_irq_save(flags);
430 cpu_switch_mm(idmap_pg_dir, &init_mm);
432 /* Call SetVirtualAddressMap with the physical address of the map */
433 runtime = efi.systab->runtime;
434 efi.set_virtual_address_map = runtime->set_virtual_address_map;
436 status = efi.set_virtual_address_map(count * memmap.desc_size,
439 (efi_memory_desc_t *)virtmap_phys);
440 cpu_set_reserved_ttbr0();
442 local_irq_restore(flags);
446 free_boot_services();
448 if (status != EFI_SUCCESS) {
449 pr_err("Failed to set EFI virtual address map! [%lx]\n",
454 /* Set up runtime services function pointers */
455 runtime = efi.systab->runtime;
456 efi_native_runtime_setup();
457 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
459 efi.runtime_version = efi.systab->hdr.revision;
464 /* unmap all mappings that succeeded: there are 'count' of those */
465 for (virt_md = virtmap; count--; virt_md += memmap.desc_size) {
467 iounmap((__force void __iomem *)md->virt_addr);
472 early_initcall(arm64_enter_virtual_mode);
474 static int __init arm64_dmi_init(void)
477 * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
478 * be called early because dmi_id_init(), which is an arch_initcall
479 * itself, depends on dmi_scan_machine() having been called already.
483 dmi_set_dump_stack_arch_desc();
486 core_initcall(arm64_dmi_init);