2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 2.4
6 * Copyright (C) 2013 - 2015 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/init.h>
16 #include <linux/memblock.h>
17 #include <linux/mm_types.h>
19 #include <linux/of_fdt.h>
23 struct efi_memory_map memmap;
27 static int __init is_normal_ram(efi_memory_desc_t *md)
29 if (md->attribute & EFI_MEMORY_WB)
35 * Translate a EFI virtual address into a physical address: this is necessary,
36 * as some data members of the EFI system table are virtually remapped after
37 * SetVirtualAddressMap() has been called.
39 static phys_addr_t efi_to_phys(unsigned long addr)
41 efi_memory_desc_t *md;
43 for_each_efi_memory_desc(&memmap, md) {
44 if (!(md->attribute & EFI_MEMORY_RUNTIME))
46 if (md->virt_addr == 0)
47 /* no virtual mapping has been installed by the stub */
49 if (md->virt_addr <= addr &&
50 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
51 return md->phys_addr + addr - md->virt_addr;
56 static int __init uefi_init(void)
61 char vendor[100] = "unknown";
64 efi.systab = early_memremap(efi_system_table,
65 sizeof(efi_system_table_t));
66 if (efi.systab == NULL) {
67 pr_warn("Unable to map EFI system table.\n");
71 set_bit(EFI_BOOT, &efi.flags);
72 if (IS_ENABLED(CONFIG_64BIT))
73 set_bit(EFI_64BIT, &efi.flags);
76 * Verify the EFI Table
78 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
79 pr_err("System table signature incorrect\n");
83 if ((efi.systab->hdr.revision >> 16) < 2)
84 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
85 efi.systab->hdr.revision >> 16,
86 efi.systab->hdr.revision & 0xffff);
88 /* Show what we know for posterity */
89 c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
90 sizeof(vendor) * sizeof(efi_char16_t));
92 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
95 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
98 pr_info("EFI v%u.%.02u by %s\n",
99 efi.systab->hdr.revision >> 16,
100 efi.systab->hdr.revision & 0xffff, vendor);
102 table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
103 config_tables = early_memremap(efi_to_phys(efi.systab->tables),
105 if (config_tables == NULL) {
106 pr_warn("Unable to map EFI config table array.\n");
110 retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
111 sizeof(efi_config_table_t), NULL);
113 early_memunmap(config_tables, table_size);
115 early_memunmap(efi.systab, sizeof(efi_system_table_t));
120 * Return true for RAM regions we want to permanently reserve.
122 static __init int is_reserve_region(efi_memory_desc_t *md)
125 case EFI_LOADER_CODE:
126 case EFI_LOADER_DATA:
127 case EFI_BOOT_SERVICES_CODE:
128 case EFI_BOOT_SERVICES_DATA:
129 case EFI_CONVENTIONAL_MEMORY:
130 case EFI_PERSISTENT_MEMORY:
135 return is_normal_ram(md);
138 static __init void reserve_regions(void)
140 efi_memory_desc_t *md;
141 u64 paddr, npages, size;
143 if (efi_enabled(EFI_DBG))
144 pr_info("Processing EFI memory map:\n");
146 for_each_efi_memory_desc(&memmap, md) {
147 paddr = md->phys_addr;
148 npages = md->num_pages;
150 if (efi_enabled(EFI_DBG)) {
153 pr_info(" 0x%012llx-0x%012llx %s",
154 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
155 efi_md_typeattr_format(buf, sizeof(buf), md));
158 memrange_efi_to_native(&paddr, &npages);
159 size = npages << PAGE_SHIFT;
161 if (is_normal_ram(md))
162 early_init_dt_add_memory_arch(paddr, size);
164 if (is_reserve_region(md)) {
165 memblock_mark_nomap(paddr, size);
166 if (efi_enabled(EFI_DBG))
170 if (efi_enabled(EFI_DBG))
174 set_bit(EFI_MEMMAP, &efi.flags);
177 void __init efi_init(void)
179 struct efi_fdt_params params;
181 /* Grab UEFI information placed in FDT by stub */
182 if (!efi_get_fdt_params(¶ms))
185 efi_system_table = params.system_table;
187 memmap.phys_map = params.mmap;
188 memmap.map = early_memremap(params.mmap, params.mmap_size);
189 if (memmap.map == NULL) {
191 * If we are booting via UEFI, the UEFI memory map is the only
192 * description of memory we have, so there is little point in
193 * proceeding if we cannot access it.
195 panic("Unable to map EFI memory map.\n");
197 memmap.map_end = memmap.map + params.mmap_size;
198 memmap.desc_size = params.desc_size;
199 memmap.desc_version = params.desc_ver;
205 early_memunmap(memmap.map, params.mmap_size);
206 memblock_mark_nomap(params.mmap & PAGE_MASK,
207 PAGE_ALIGN(params.mmap_size +
208 (params.mmap & ~PAGE_MASK)));