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