]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/arm64/kernel/efi.c
efi/arm64: Clean up efi_get_fdt_params() interface
[mirror_ubuntu-artful-kernel.git] / arch / arm64 / kernel / efi.c
CommitLineData
f84d0275
MS
1/*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 2.4
5 *
6 * Copyright (C) 2013, 2014 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
f3cdfd23 14#include <linux/atomic.h>
d1ae8c00 15#include <linux/dmi.h>
f84d0275
MS
16#include <linux/efi.h>
17#include <linux/export.h>
18#include <linux/memblock.h>
f3cdfd23 19#include <linux/mm_types.h>
f84d0275
MS
20#include <linux/bootmem.h>
21#include <linux/of.h>
22#include <linux/of_fdt.h>
f3cdfd23
AB
23#include <linux/preempt.h>
24#include <linux/rbtree.h>
25#include <linux/rwsem.h>
f84d0275
MS
26#include <linux/sched.h>
27#include <linux/slab.h>
f3cdfd23 28#include <linux/spinlock.h>
f84d0275
MS
29
30#include <asm/cacheflush.h>
31#include <asm/efi.h>
32#include <asm/tlbflush.h>
33#include <asm/mmu_context.h>
f3cdfd23
AB
34#include <asm/mmu.h>
35#include <asm/pgtable.h>
f84d0275
MS
36
37struct efi_memory_map memmap;
38
f84d0275
MS
39static u64 efi_system_table;
40
60305db9
AB
41static pgd_t efi_pgd[PTRS_PER_PGD] __page_aligned_bss;
42
43static struct mm_struct efi_mm = {
44 .mm_rb = RB_ROOT,
45 .pgd = efi_pgd,
46 .mm_users = ATOMIC_INIT(2),
47 .mm_count = ATOMIC_INIT(1),
48 .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
49 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
50 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
51 INIT_MM_CONTEXT(efi_mm)
52};
53
f84d0275
MS
54static int __init is_normal_ram(efi_memory_desc_t *md)
55{
56 if (md->attribute & EFI_MEMORY_WB)
57 return 1;
58 return 0;
59}
60
f3cdfd23
AB
61/*
62 * Translate a EFI virtual address into a physical address: this is necessary,
63 * as some data members of the EFI system table are virtually remapped after
64 * SetVirtualAddressMap() has been called.
65 */
66static phys_addr_t efi_to_phys(unsigned long addr)
67{
68 efi_memory_desc_t *md;
69
70 for_each_efi_memory_desc(&memmap, md) {
71 if (!(md->attribute & EFI_MEMORY_RUNTIME))
72 continue;
73 if (md->virt_addr == 0)
74 /* no virtual mapping has been installed by the stub */
75 break;
76 if (md->virt_addr <= addr &&
77 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
78 return md->phys_addr + addr - md->virt_addr;
79 }
80 return addr;
81}
82
f84d0275
MS
83static int __init uefi_init(void)
84{
85 efi_char16_t *c16;
f3cdfd23
AB
86 void *config_tables;
87 u64 table_size;
f84d0275
MS
88 char vendor[100] = "unknown";
89 int i, retval;
90
91 efi.systab = early_memremap(efi_system_table,
92 sizeof(efi_system_table_t));
93 if (efi.systab == NULL) {
94 pr_warn("Unable to map EFI system table.\n");
95 return -ENOMEM;
96 }
97
98 set_bit(EFI_BOOT, &efi.flags);
99 set_bit(EFI_64BIT, &efi.flags);
100
101 /*
102 * Verify the EFI Table
103 */
104 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
105 pr_err("System table signature incorrect\n");
88f8abd5
DY
106 retval = -EINVAL;
107 goto out;
f84d0275
MS
108 }
109 if ((efi.systab->hdr.revision >> 16) < 2)
110 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
111 efi.systab->hdr.revision >> 16,
112 efi.systab->hdr.revision & 0xffff);
113
114 /* Show what we know for posterity */
f3cdfd23 115 c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
f91b1fea 116 sizeof(vendor) * sizeof(efi_char16_t));
f84d0275
MS
117 if (c16) {
118 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
119 vendor[i] = c16[i];
120 vendor[i] = '\0';
f91b1fea 121 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
f84d0275
MS
122 }
123
124 pr_info("EFI v%u.%.02u by %s\n",
125 efi.systab->hdr.revision >> 16,
126 efi.systab->hdr.revision & 0xffff, vendor);
127
f3cdfd23
AB
128 table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
129 config_tables = early_memremap(efi_to_phys(efi.systab->tables),
130 table_size);
131
132 retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
133 sizeof(efi_config_table_64_t), NULL);
f84d0275 134
f3cdfd23 135 early_memunmap(config_tables, table_size);
88f8abd5 136out:
f84d0275 137 early_memunmap(efi.systab, sizeof(efi_system_table_t));
f84d0275
MS
138 return retval;
139}
140
f84d0275
MS
141/*
142 * Return true for RAM regions we want to permanently reserve.
143 */
144static __init int is_reserve_region(efi_memory_desc_t *md)
145{
61139eb0
AB
146 switch (md->type) {
147 case EFI_LOADER_CODE:
148 case EFI_LOADER_DATA:
149 case EFI_BOOT_SERVICES_CODE:
150 case EFI_BOOT_SERVICES_DATA:
151 case EFI_CONVENTIONAL_MEMORY:
ad5fb870 152 case EFI_PERSISTENT_MEMORY:
f84d0275 153 return 0;
61139eb0
AB
154 default:
155 break;
156 }
157 return is_normal_ram(md);
f84d0275
MS
158}
159
160static __init void reserve_regions(void)
161{
162 efi_memory_desc_t *md;
163 u64 paddr, npages, size;
164
c9494dc8 165 if (efi_enabled(EFI_DBG))
f84d0275
MS
166 pr_info("Processing EFI memory map:\n");
167
168 for_each_efi_memory_desc(&memmap, md) {
169 paddr = md->phys_addr;
170 npages = md->num_pages;
171
c9494dc8 172 if (efi_enabled(EFI_DBG)) {
65ba758f
LE
173 char buf[64];
174
175 pr_info(" 0x%012llx-0x%012llx %s",
f84d0275 176 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
65ba758f
LE
177 efi_md_typeattr_format(buf, sizeof(buf), md));
178 }
f84d0275
MS
179
180 memrange_efi_to_native(&paddr, &npages);
181 size = npages << PAGE_SHIFT;
182
183 if (is_normal_ram(md))
184 early_init_dt_add_memory_arch(paddr, size);
185
3033b845 186 if (is_reserve_region(md)) {
f84d0275 187 memblock_reserve(paddr, size);
c9494dc8 188 if (efi_enabled(EFI_DBG))
f84d0275
MS
189 pr_cont("*");
190 }
191
c9494dc8 192 if (efi_enabled(EFI_DBG))
f84d0275
MS
193 pr_cont("\n");
194 }
86c8b27a
LL
195
196 set_bit(EFI_MEMMAP, &efi.flags);
f84d0275
MS
197}
198
f84d0275
MS
199void __init efi_init(void)
200{
201 struct efi_fdt_params params;
202
203 /* Grab UEFI information placed in FDT by stub */
7968c0e3 204 if (!efi_get_fdt_params(&params))
f84d0275
MS
205 return;
206
207 efi_system_table = params.system_table;
208
209 memblock_reserve(params.mmap & PAGE_MASK,
210 PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
211 memmap.phys_map = (void *)params.mmap;
212 memmap.map = early_memremap(params.mmap, params.mmap_size);
213 memmap.map_end = memmap.map + params.mmap_size;
214 memmap.desc_size = params.desc_size;
215 memmap.desc_version = params.desc_ver;
216
217 if (uefi_init() < 0)
218 return;
219
220 reserve_regions();
60305db9
AB
221 early_memunmap(memmap.map, params.mmap_size);
222}
223
224static bool __init efi_virtmap_init(void)
225{
226 efi_memory_desc_t *md;
227
228 for_each_efi_memory_desc(&memmap, md) {
229 u64 paddr, npages, size;
230 pgprot_t prot;
231
232 if (!(md->attribute & EFI_MEMORY_RUNTIME))
233 continue;
234 if (md->virt_addr == 0)
235 return false;
236
237 paddr = md->phys_addr;
238 npages = md->num_pages;
239 memrange_efi_to_native(&paddr, &npages);
240 size = npages << PAGE_SHIFT;
241
242 pr_info(" EFI remap 0x%016llx => %p\n",
243 md->phys_addr, (void *)md->virt_addr);
244
245 /*
246 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
247 * executable, everything else can be mapped with the XN bits
248 * set.
249 */
250 if (!is_normal_ram(md))
251 prot = __pgprot(PROT_DEVICE_nGnRE);
252 else if (md->type == EFI_RUNTIME_SERVICES_CODE)
253 prot = PAGE_KERNEL_EXEC;
254 else
255 prot = PAGE_KERNEL;
256
257 create_pgd_mapping(&efi_mm, paddr, md->virt_addr, size, prot);
258 }
259 return true;
f84d0275
MS
260}
261
f84d0275 262/*
f3cdfd23
AB
263 * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
264 * non-early mapping of the UEFI system table and virtual mappings for all
265 * EFI_MEMORY_RUNTIME regions.
f84d0275 266 */
f3cdfd23 267static int __init arm64_enable_runtime_services(void)
f84d0275 268{
f84d0275 269 u64 mapsize;
f84d0275
MS
270
271 if (!efi_enabled(EFI_BOOT)) {
272 pr_info("EFI services will not be available.\n");
273 return -1;
274 }
275
6632210f
DY
276 if (efi_runtime_disabled()) {
277 pr_info("EFI runtime services will be disabled.\n");
278 return -1;
279 }
280
281 pr_info("Remapping and enabling EFI services.\n");
7fe5d2b1
AB
282
283 mapsize = memmap.map_end - memmap.map;
f84d0275
MS
284 memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
285 mapsize);
7fe5d2b1
AB
286 if (!memmap.map) {
287 pr_err("Failed to remap EFI memory map\n");
288 return -1;
289 }
f84d0275 290 memmap.map_end = memmap.map + mapsize;
f84d0275
MS
291 efi.memmap = &memmap;
292
f3cdfd23
AB
293 efi.systab = (__force void *)ioremap_cache(efi_system_table,
294 sizeof(efi_system_table_t));
99a5603e 295 if (!efi.systab) {
f3cdfd23
AB
296 pr_err("Failed to remap EFI System Table\n");
297 return -1;
99a5603e
AB
298 }
299 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
f84d0275 300
60305db9 301 if (!efi_virtmap_init()) {
f3cdfd23 302 pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
f84d0275
MS
303 return -1;
304 }
305
306 /* Set up runtime services function pointers */
e15dd494 307 efi_native_runtime_setup();
f84d0275
MS
308 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
309
6a7519e8
SP
310 efi.runtime_version = efi.systab->hdr.revision;
311
f84d0275
MS
312 return 0;
313}
f3cdfd23 314early_initcall(arm64_enable_runtime_services);
d1ae8c00
YL
315
316static int __init arm64_dmi_init(void)
317{
318 /*
319 * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
320 * be called early because dmi_id_init(), which is an arch_initcall
321 * itself, depends on dmi_scan_machine() having been called already.
322 */
323 dmi_scan_machine();
b07bfaa3
AB
324 if (dmi_available)
325 dmi_set_dump_stack_arch_desc();
d1ae8c00
YL
326 return 0;
327}
328core_initcall(arm64_dmi_init);
f3cdfd23 329
f3cdfd23
AB
330static void efi_set_pgd(struct mm_struct *mm)
331{
130c93fd
WD
332 if (mm == &init_mm)
333 cpu_set_reserved_ttbr0();
334 else
335 cpu_switch_mm(mm->pgd, mm);
336
f3cdfd23
AB
337 flush_tlb_all();
338 if (icache_is_aivivt())
339 __flush_icache_all();
340}
341
342void efi_virtmap_load(void)
343{
344 preempt_disable();
345 efi_set_pgd(&efi_mm);
346}
347
348void efi_virtmap_unload(void)
349{
350 efi_set_pgd(current->active_mm);
351 preempt_enable();
352}
60c0d45a
AB
353
354/*
355 * UpdateCapsule() depends on the system being shutdown via
356 * ResetSystem().
357 */
358bool efi_poweroff_required(void)
359{
360 return efi_enabled(EFI_RUNTIME_SERVICES);
361}