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/efi.h>
15 #include <linux/export.h>
16 #include <linux/memblock.h>
17 #include <linux/bootmem.h>
19 #include <linux/of_fdt.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
23 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
26 #include <asm/mmu_context.h>
28 struct efi_memory_map memmap;
30 static efi_runtime_services_t *runtime;
32 static u64 efi_system_table;
34 static int uefi_debug __initdata;
35 static int __init uefi_debug_setup(char *str)
41 early_param("uefi_debug", uefi_debug_setup);
43 static int __init is_normal_ram(efi_memory_desc_t *md)
45 if (md->attribute & EFI_MEMORY_WB)
50 static void __init efi_setup_idmap(void)
52 struct memblock_region *r;
53 efi_memory_desc_t *md;
54 u64 paddr, npages, size;
56 for_each_memblock(memory, r)
57 create_id_mapping(r->base, r->size, 0);
59 /* map runtime io spaces */
60 for_each_efi_memory_desc(&memmap, md) {
61 if (!(md->attribute & EFI_MEMORY_RUNTIME) || is_normal_ram(md))
63 paddr = md->phys_addr;
64 npages = md->num_pages;
65 memrange_efi_to_native(&paddr, &npages);
66 size = npages << PAGE_SHIFT;
67 create_id_mapping(paddr, size, 1);
71 static int __init uefi_init(void)
74 char vendor[100] = "unknown";
77 efi.systab = early_memremap(efi_system_table,
78 sizeof(efi_system_table_t));
79 if (efi.systab == NULL) {
80 pr_warn("Unable to map EFI system table.\n");
84 set_bit(EFI_BOOT, &efi.flags);
85 set_bit(EFI_64BIT, &efi.flags);
88 * Verify the EFI Table
90 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
91 pr_err("System table signature incorrect\n");
95 if ((efi.systab->hdr.revision >> 16) < 2)
96 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
97 efi.systab->hdr.revision >> 16,
98 efi.systab->hdr.revision & 0xffff);
100 /* Show what we know for posterity */
101 c16 = early_memremap(efi.systab->fw_vendor,
104 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
107 early_memunmap(c16, sizeof(vendor));
110 pr_info("EFI v%u.%.02u by %s\n",
111 efi.systab->hdr.revision >> 16,
112 efi.systab->hdr.revision & 0xffff, vendor);
114 retval = efi_config_init(NULL);
116 set_bit(EFI_CONFIG_TABLES, &efi.flags);
119 early_memunmap(efi.systab, sizeof(efi_system_table_t));
123 static __initdata char memory_type_name[][32] = {
131 {"Conventional Memory"},
133 {"ACPI Reclaim Memory"},
135 {"Memory Mapped I/O"},
141 * Return true for RAM regions we want to permanently reserve.
143 static __init int is_reserve_region(efi_memory_desc_t *md)
145 if (!is_normal_ram(md))
148 if (md->attribute & EFI_MEMORY_RUNTIME)
151 if (md->type == EFI_ACPI_RECLAIM_MEMORY ||
152 md->type == EFI_RESERVED_TYPE)
158 static __init void reserve_regions(void)
160 efi_memory_desc_t *md;
161 u64 paddr, npages, size;
164 pr_info("Processing EFI memory map:\n");
166 for_each_efi_memory_desc(&memmap, md) {
167 paddr = md->phys_addr;
168 npages = md->num_pages;
171 pr_info(" 0x%012llx-0x%012llx [%s]",
172 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
173 memory_type_name[md->type]);
175 memrange_efi_to_native(&paddr, &npages);
176 size = npages << PAGE_SHIFT;
178 if (is_normal_ram(md))
179 early_init_dt_add_memory_arch(paddr, size);
181 if (is_reserve_region(md) ||
182 md->type == EFI_BOOT_SERVICES_CODE ||
183 md->type == EFI_BOOT_SERVICES_DATA) {
184 memblock_reserve(paddr, size);
195 static u64 __init free_one_region(u64 start, u64 end)
197 u64 size = end - start;
200 pr_info(" EFI freeing: 0x%012llx-0x%012llx\n", start, end - 1);
202 free_bootmem_late(start, size);
206 static u64 __init free_region(u64 start, u64 end)
208 u64 map_start, map_end, total = 0;
213 map_start = (u64)memmap.phys_map;
214 map_end = PAGE_ALIGN(map_start + (memmap.map_end - memmap.map));
215 map_start &= PAGE_MASK;
217 if (start < map_end && end > map_start) {
218 /* region overlaps UEFI memmap */
219 if (start < map_start)
220 total += free_one_region(start, map_start);
223 total += free_one_region(map_end, end);
225 total += free_one_region(start, end);
230 static void __init free_boot_services(void)
233 u64 keep_end, free_start, free_end;
234 efi_memory_desc_t *md;
237 * If kernel uses larger pages than UEFI, we have to be careful
238 * not to inadvertantly free memory we want to keep if there is
239 * overlap at the kernel page size alignment. We do not want to
240 * free is_reserve_region() memory nor the UEFI memmap itself.
242 * The memory map is sorted, so we keep track of the end of
243 * any previous region we want to keep, remember any region
244 * we want to free and defer freeing it until we encounter
245 * the next region we want to keep. This way, before freeing
246 * it, we can clip it as needed to avoid freeing memory we
247 * want to keep for UEFI.
253 for_each_efi_memory_desc(&memmap, md) {
254 u64 paddr, npages, size;
256 if (is_reserve_region(md)) {
258 * We don't want to free any memory from this region.
261 /* adjust free_end then free region */
262 if (free_end > md->phys_addr)
263 free_end -= PAGE_SIZE;
264 total_freed += free_region(free_start, free_end);
267 keep_end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
271 if (md->type != EFI_BOOT_SERVICES_CODE &&
272 md->type != EFI_BOOT_SERVICES_DATA) {
273 /* no need to free this region */
278 * We want to free memory from this region.
280 paddr = md->phys_addr;
281 npages = md->num_pages;
282 memrange_efi_to_native(&paddr, &npages);
283 size = npages << PAGE_SHIFT;
286 if (paddr <= free_end)
287 free_end = paddr + size;
289 total_freed += free_region(free_start, free_end);
291 free_end = paddr + size;
295 free_end = paddr + size;
297 if (free_start < keep_end) {
298 free_start += PAGE_SIZE;
299 if (free_start >= free_end)
304 total_freed += free_region(free_start, free_end);
307 pr_info("Freed 0x%llx bytes of EFI boot services memory",
311 void __init efi_init(void)
313 struct efi_fdt_params params;
315 /* Grab UEFI information placed in FDT by stub */
316 if (!efi_get_fdt_params(¶ms, uefi_debug))
319 efi_system_table = params.system_table;
321 memblock_reserve(params.mmap & PAGE_MASK,
322 PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
323 memmap.phys_map = (void *)params.mmap;
324 memmap.map = early_memremap(params.mmap, params.mmap_size);
325 memmap.map_end = memmap.map + params.mmap_size;
326 memmap.desc_size = params.desc_size;
327 memmap.desc_version = params.desc_ver;
335 void __init efi_idmap_init(void)
337 if (!efi_enabled(EFI_BOOT))
340 /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
344 static int __init remap_region(efi_memory_desc_t *md, void **new)
346 u64 paddr, vaddr, npages, size;
348 paddr = md->phys_addr;
349 npages = md->num_pages;
350 memrange_efi_to_native(&paddr, &npages);
351 size = npages << PAGE_SHIFT;
353 if (is_normal_ram(md))
354 vaddr = (__force u64)ioremap_cache(paddr, size);
356 vaddr = (__force u64)ioremap(paddr, size);
359 pr_err("Unable to remap 0x%llx pages @ %p\n",
360 npages, (void *)paddr);
364 /* adjust for any rounding when EFI and system pagesize differs */
365 md->virt_addr = vaddr + (md->phys_addr - paddr);
368 pr_info(" EFI remap 0x%012llx => %p\n",
369 md->phys_addr, (void *)md->virt_addr);
371 memcpy(*new, md, memmap.desc_size);
372 *new += memmap.desc_size;
378 * Switch UEFI from an identity map to a kernel virtual map
380 static int __init arm64_enter_virtual_mode(void)
382 efi_memory_desc_t *md;
383 phys_addr_t virtmap_phys;
384 void *virtmap, *virt_md;
390 if (!efi_enabled(EFI_BOOT)) {
391 pr_info("EFI services will not be available.\n");
395 mapsize = memmap.map_end - memmap.map;
396 early_memunmap(memmap.map, mapsize);
398 if (efi_runtime_disabled()) {
399 pr_info("EFI runtime services will be disabled.\n");
403 pr_info("Remapping and enabling EFI services.\n");
404 /* replace early memmap mapping with permanent mapping */
405 memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
407 memmap.map_end = memmap.map + mapsize;
409 efi.memmap = &memmap;
411 /* Map the runtime regions */
412 virtmap = kmalloc(mapsize, GFP_KERNEL);
414 pr_err("Failed to allocate EFI virtual memmap\n");
417 virtmap_phys = virt_to_phys(virtmap);
420 for_each_efi_memory_desc(&memmap, md) {
421 if (!(md->attribute & EFI_MEMORY_RUNTIME))
423 if (!remap_region(md, &virt_md))
428 efi.systab = (__force void *)efi_lookup_mapped_addr(efi_system_table);
431 * If we have no virtual mapping for the System Table at this
432 * point, the memory map doesn't cover the physical offset where
433 * it resides. This means the System Table will be inaccessible
434 * to Runtime Services themselves once the virtual mapping is
437 pr_err("Failed to remap EFI System Table -- buggy firmware?\n");
440 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
442 local_irq_save(flags);
443 cpu_switch_mm(idmap_pg_dir, &init_mm);
445 /* Call SetVirtualAddressMap with the physical address of the map */
446 runtime = efi.systab->runtime;
447 efi.set_virtual_address_map = runtime->set_virtual_address_map;
449 status = efi.set_virtual_address_map(count * memmap.desc_size,
452 (efi_memory_desc_t *)virtmap_phys);
453 cpu_set_reserved_ttbr0();
455 local_irq_restore(flags);
459 free_boot_services();
461 if (status != EFI_SUCCESS) {
462 pr_err("Failed to set EFI virtual address map! [%lx]\n",
467 /* Set up runtime services function pointers */
468 runtime = efi.systab->runtime;
469 efi_native_runtime_setup();
470 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
475 /* unmap all mappings that succeeded: there are 'count' of those */
476 for (virt_md = virtmap; count--; virt_md += memmap.desc_size) {
478 iounmap((__force void __iomem *)md->virt_addr);
483 early_initcall(arm64_enter_virtual_mode);