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