blob: a84dddb54c14943a9cfcfbcde6ec7562f2f640b6 [file] [log] [blame]
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +01001/*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 2.4
5 *
6 * Copyright (C) 2013 - 2015 Linaro Ltd.
7 *
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.
11 *
12 */
13
14#include <linux/efi.h>
15#include <linux/init.h>
16#include <linux/memblock.h>
17#include <linux/mm_types.h>
18#include <linux/of.h>
19#include <linux/of_fdt.h>
20
21#include <asm/efi.h>
22
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010023u64 efi_system_table;
24
25static int __init is_normal_ram(efi_memory_desc_t *md)
26{
27 if (md->attribute & EFI_MEMORY_WB)
28 return 1;
29 return 0;
30}
31
32/*
33 * Translate a EFI virtual address into a physical address: this is necessary,
34 * as some data members of the EFI system table are virtually remapped after
35 * SetVirtualAddressMap() has been called.
36 */
37static phys_addr_t efi_to_phys(unsigned long addr)
38{
39 efi_memory_desc_t *md;
40
Matt Fleming884f4f62016-04-25 21:06:39 +010041 for_each_efi_memory_desc(md) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010042 if (!(md->attribute & EFI_MEMORY_RUNTIME))
43 continue;
44 if (md->virt_addr == 0)
45 /* no virtual mapping has been installed by the stub */
46 break;
47 if (md->virt_addr <= addr &&
48 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
49 return md->phys_addr + addr - md->virt_addr;
50 }
51 return addr;
52}
53
54static int __init uefi_init(void)
55{
56 efi_char16_t *c16;
57 void *config_tables;
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010058 size_t table_size;
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010059 char vendor[100] = "unknown";
60 int i, retval;
61
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +000062 efi.systab = early_memremap_ro(efi_system_table,
63 sizeof(efi_system_table_t));
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010064 if (efi.systab == NULL) {
65 pr_warn("Unable to map EFI system table.\n");
66 return -ENOMEM;
67 }
68
69 set_bit(EFI_BOOT, &efi.flags);
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010070 if (IS_ENABLED(CONFIG_64BIT))
71 set_bit(EFI_64BIT, &efi.flags);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010072
73 /*
74 * Verify the EFI Table
75 */
76 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
77 pr_err("System table signature incorrect\n");
78 retval = -EINVAL;
79 goto out;
80 }
81 if ((efi.systab->hdr.revision >> 16) < 2)
82 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
83 efi.systab->hdr.revision >> 16,
84 efi.systab->hdr.revision & 0xffff);
85
Ard Biesheuvel14c43be2016-04-25 21:06:34 +010086 efi.runtime_version = efi.systab->hdr.revision;
87
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010088 /* Show what we know for posterity */
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +000089 c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
90 sizeof(vendor) * sizeof(efi_char16_t));
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010091 if (c16) {
92 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
93 vendor[i] = c16[i];
94 vendor[i] = '\0';
95 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
96 }
97
98 pr_info("EFI v%u.%.02u by %s\n",
99 efi.systab->hdr.revision >> 16,
100 efi.systab->hdr.revision & 0xffff, vendor);
101
102 table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +0000103 config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
104 table_size);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100105 if (config_tables == NULL) {
106 pr_warn("Unable to map EFI config table array.\n");
107 retval = -ENOMEM;
108 goto out;
109 }
110 retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
Ard Biesheuvelf7d92482015-11-30 13:28:19 +0100111 sizeof(efi_config_table_t), NULL);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100112
113 early_memunmap(config_tables, table_size);
114out:
115 early_memunmap(efi.systab, sizeof(efi_system_table_t));
116 return retval;
117}
118
119/*
120 * Return true for RAM regions we want to permanently reserve.
121 */
122static __init int is_reserve_region(efi_memory_desc_t *md)
123{
124 switch (md->type) {
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:
131 return 0;
132 default:
133 break;
134 }
135 return is_normal_ram(md);
136}
137
138static __init void reserve_regions(void)
139{
140 efi_memory_desc_t *md;
141 u64 paddr, npages, size;
142
143 if (efi_enabled(EFI_DBG))
144 pr_info("Processing EFI memory map:\n");
145
Matt Fleming884f4f62016-04-25 21:06:39 +0100146 for_each_efi_memory_desc(md) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100147 paddr = md->phys_addr;
148 npages = md->num_pages;
149
150 if (efi_enabled(EFI_DBG)) {
151 char buf[64];
152
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));
156 }
157
158 memrange_efi_to_native(&paddr, &npages);
159 size = npages << PAGE_SHIFT;
160
161 if (is_normal_ram(md))
162 early_init_dt_add_memory_arch(paddr, size);
163
164 if (is_reserve_region(md)) {
165 memblock_mark_nomap(paddr, size);
166 if (efi_enabled(EFI_DBG))
167 pr_cont("*");
168 }
169
170 if (efi_enabled(EFI_DBG))
171 pr_cont("\n");
172 }
173
174 set_bit(EFI_MEMMAP, &efi.flags);
175}
176
177void __init efi_init(void)
178{
179 struct efi_fdt_params params;
180
181 /* Grab UEFI information placed in FDT by stub */
182 if (!efi_get_fdt_params(&params))
183 return;
184
185 efi_system_table = params.system_table;
186
Matt Fleming884f4f62016-04-25 21:06:39 +0100187 efi.memmap.phys_map = params.mmap;
188 efi.memmap.map = early_memremap_ro(params.mmap, params.mmap_size);
189 if (efi.memmap.map == NULL) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100190 /*
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.
194 */
195 panic("Unable to map EFI memory map.\n");
196 }
Matt Fleming884f4f62016-04-25 21:06:39 +0100197 efi.memmap.map_end = efi.memmap.map + params.mmap_size;
198 efi.memmap.desc_size = params.desc_size;
199 efi.memmap.desc_version = params.desc_ver;
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100200
Ard Biesheuvel0d054ad2016-04-25 21:06:40 +0100201 WARN(efi.memmap.desc_version != 1,
202 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
203 efi.memmap.desc_version);
204
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100205 if (uefi_init() < 0)
206 return;
207
208 reserve_regions();
Matt Fleming884f4f62016-04-25 21:06:39 +0100209 early_memunmap(efi.memmap.map, params.mmap_size);
Ard Biesheuvel7cc8cbc2016-03-30 09:46:23 +0200210
211 if (IS_ENABLED(CONFIG_ARM)) {
212 /*
213 * ARM currently does not allow ioremap_cache() to be called on
214 * memory regions that are covered by struct page. So remove the
215 * UEFI memory map from the linear mapping.
216 */
217 memblock_mark_nomap(params.mmap & PAGE_MASK,
218 PAGE_ALIGN(params.mmap_size +
219 (params.mmap & ~PAGE_MASK)));
220 } else {
221 memblock_reserve(params.mmap & PAGE_MASK,
222 PAGE_ALIGN(params.mmap_size +
223 (params.mmap & ~PAGE_MASK)));
224 }
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100225}