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