]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/arm64/kernel/efi.c
arm64: dmi: Add SMBIOS/DMI support
[mirror_ubuntu-bionic-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
668ebd10 14#include <linux/dmi.h>
f84d0275
MS
15#include <linux/efi.h>
16#include <linux/export.h>
17#include <linux/memblock.h>
18#include <linux/bootmem.h>
19#include <linux/of.h>
20#include <linux/of_fdt.h>
21#include <linux/sched.h>
22#include <linux/slab.h>
23
24#include <asm/cacheflush.h>
25#include <asm/efi.h>
26#include <asm/tlbflush.h>
27#include <asm/mmu_context.h>
28
29struct efi_memory_map memmap;
30
31static efi_runtime_services_t *runtime;
32
33static u64 efi_system_table;
34
35static int uefi_debug __initdata;
36static int __init uefi_debug_setup(char *str)
37{
38 uefi_debug = 1;
39
40 return 0;
41}
42early_param("uefi_debug", uefi_debug_setup);
43
44static int __init is_normal_ram(efi_memory_desc_t *md)
45{
46 if (md->attribute & EFI_MEMORY_WB)
47 return 1;
48 return 0;
49}
50
51static void __init efi_setup_idmap(void)
52{
53 struct memblock_region *r;
54 efi_memory_desc_t *md;
55 u64 paddr, npages, size;
56
57 for_each_memblock(memory, r)
58 create_id_mapping(r->base, r->size, 0);
59
60 /* map runtime io spaces */
61 for_each_efi_memory_desc(&memmap, md) {
62 if (!(md->attribute & EFI_MEMORY_RUNTIME) || is_normal_ram(md))
63 continue;
64 paddr = md->phys_addr;
65 npages = md->num_pages;
66 memrange_efi_to_native(&paddr, &npages);
67 size = npages << PAGE_SHIFT;
68 create_id_mapping(paddr, size, 1);
69 }
70}
71
72static int __init uefi_init(void)
73{
74 efi_char16_t *c16;
75 char vendor[100] = "unknown";
76 int i, retval;
77
78 efi.systab = early_memremap(efi_system_table,
79 sizeof(efi_system_table_t));
80 if (efi.systab == NULL) {
81 pr_warn("Unable to map EFI system table.\n");
82 return -ENOMEM;
83 }
84
85 set_bit(EFI_BOOT, &efi.flags);
86 set_bit(EFI_64BIT, &efi.flags);
87
88 /*
89 * Verify the EFI Table
90 */
91 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
92 pr_err("System table signature incorrect\n");
93 return -EINVAL;
94 }
95 if ((efi.systab->hdr.revision >> 16) < 2)
96 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
97 efi.systab->hdr.revision >> 16,
98 efi.systab->hdr.revision & 0xffff);
99
100 /* Show what we know for posterity */
101 c16 = early_memremap(efi.systab->fw_vendor,
102 sizeof(vendor));
103 if (c16) {
104 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
105 vendor[i] = c16[i];
106 vendor[i] = '\0';
107 }
108
109 pr_info("EFI v%u.%.02u by %s\n",
110 efi.systab->hdr.revision >> 16,
111 efi.systab->hdr.revision & 0xffff, vendor);
112
113 retval = efi_config_init(NULL);
114 if (retval == 0)
115 set_bit(EFI_CONFIG_TABLES, &efi.flags);
116
117 early_memunmap(c16, sizeof(vendor));
118 early_memunmap(efi.systab, sizeof(efi_system_table_t));
119
120 return retval;
121}
122
123static __initdata char memory_type_name[][32] = {
124 {"Reserved"},
125 {"Loader Code"},
126 {"Loader Data"},
127 {"Boot Code"},
128 {"Boot Data"},
129 {"Runtime Code"},
130 {"Runtime Data"},
131 {"Conventional Memory"},
132 {"Unusable Memory"},
133 {"ACPI Reclaim Memory"},
134 {"ACPI Memory NVS"},
135 {"Memory Mapped I/O"},
136 {"MMIO Port Space"},
137 {"PAL Code"},
138};
139
140/*
141 * Return true for RAM regions we want to permanently reserve.
142 */
143static __init int is_reserve_region(efi_memory_desc_t *md)
144{
145 if (!is_normal_ram(md))
146 return 0;
147
148 if (md->attribute & EFI_MEMORY_RUNTIME)
149 return 1;
150
151 if (md->type == EFI_ACPI_RECLAIM_MEMORY ||
152 md->type == EFI_RESERVED_TYPE)
153 return 1;
154
155 return 0;
156}
157
158static __init void reserve_regions(void)
159{
160 efi_memory_desc_t *md;
161 u64 paddr, npages, size;
162
163 if (uefi_debug)
164 pr_info("Processing EFI memory map:\n");
165
166 for_each_efi_memory_desc(&memmap, md) {
167 paddr = md->phys_addr;
168 npages = md->num_pages;
169
170 if (uefi_debug)
171 pr_info(" 0x%012llx-0x%012llx [%s]",
172 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
173 memory_type_name[md->type]);
174
175 memrange_efi_to_native(&paddr, &npages);
176 size = npages << PAGE_SHIFT;
177
178 if (is_normal_ram(md))
179 early_init_dt_add_memory_arch(paddr, size);
180
181 if (is_reserve_region(md) ||
182 md->type == EFI_BOOT_SERVICES_CODE ||
183 md->type == EFI_BOOT_SERVICES_DATA) {
184 memblock_reserve(paddr, size);
185 if (uefi_debug)
186 pr_cont("*");
187 }
188
189 if (uefi_debug)
190 pr_cont("\n");
191 }
86c8b27a
LL
192
193 set_bit(EFI_MEMMAP, &efi.flags);
f84d0275
MS
194}
195
196
197static u64 __init free_one_region(u64 start, u64 end)
198{
199 u64 size = end - start;
200
201 if (uefi_debug)
202 pr_info(" EFI freeing: 0x%012llx-0x%012llx\n", start, end - 1);
203
204 free_bootmem_late(start, size);
205 return size;
206}
207
208static u64 __init free_region(u64 start, u64 end)
209{
210 u64 map_start, map_end, total = 0;
211
212 if (end <= start)
213 return total;
214
215 map_start = (u64)memmap.phys_map;
216 map_end = PAGE_ALIGN(map_start + (memmap.map_end - memmap.map));
217 map_start &= PAGE_MASK;
218
219 if (start < map_end && end > map_start) {
220 /* region overlaps UEFI memmap */
221 if (start < map_start)
222 total += free_one_region(start, map_start);
223
224 if (map_end < end)
225 total += free_one_region(map_end, end);
226 } else
227 total += free_one_region(start, end);
228
229 return total;
230}
231
232static void __init free_boot_services(void)
233{
234 u64 total_freed = 0;
235 u64 keep_end, free_start, free_end;
236 efi_memory_desc_t *md;
237
238 /*
239 * If kernel uses larger pages than UEFI, we have to be careful
240 * not to inadvertantly free memory we want to keep if there is
241 * overlap at the kernel page size alignment. We do not want to
242 * free is_reserve_region() memory nor the UEFI memmap itself.
243 *
244 * The memory map is sorted, so we keep track of the end of
245 * any previous region we want to keep, remember any region
246 * we want to free and defer freeing it until we encounter
247 * the next region we want to keep. This way, before freeing
248 * it, we can clip it as needed to avoid freeing memory we
249 * want to keep for UEFI.
250 */
251
252 keep_end = 0;
253 free_start = 0;
254
255 for_each_efi_memory_desc(&memmap, md) {
256 u64 paddr, npages, size;
257
258 if (is_reserve_region(md)) {
259 /*
260 * We don't want to free any memory from this region.
261 */
262 if (free_start) {
263 /* adjust free_end then free region */
264 if (free_end > md->phys_addr)
265 free_end -= PAGE_SIZE;
266 total_freed += free_region(free_start, free_end);
267 free_start = 0;
268 }
269 keep_end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
270 continue;
271 }
272
273 if (md->type != EFI_BOOT_SERVICES_CODE &&
274 md->type != EFI_BOOT_SERVICES_DATA) {
275 /* no need to free this region */
276 continue;
277 }
278
279 /*
280 * We want to free memory from this region.
281 */
282 paddr = md->phys_addr;
283 npages = md->num_pages;
284 memrange_efi_to_native(&paddr, &npages);
285 size = npages << PAGE_SHIFT;
286
287 if (free_start) {
288 if (paddr <= free_end)
289 free_end = paddr + size;
290 else {
291 total_freed += free_region(free_start, free_end);
292 free_start = paddr;
293 free_end = paddr + size;
294 }
295 } else {
296 free_start = paddr;
297 free_end = paddr + size;
298 }
299 if (free_start < keep_end) {
300 free_start += PAGE_SIZE;
301 if (free_start >= free_end)
302 free_start = 0;
303 }
304 }
305 if (free_start)
306 total_freed += free_region(free_start, free_end);
307
308 if (total_freed)
309 pr_info("Freed 0x%llx bytes of EFI boot services memory",
310 total_freed);
311}
312
313void __init efi_init(void)
314{
315 struct efi_fdt_params params;
316
317 /* Grab UEFI information placed in FDT by stub */
318 if (!efi_get_fdt_params(&params, uefi_debug))
319 return;
320
321 efi_system_table = params.system_table;
322
323 memblock_reserve(params.mmap & PAGE_MASK,
324 PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
325 memmap.phys_map = (void *)params.mmap;
326 memmap.map = early_memremap(params.mmap, params.mmap_size);
327 memmap.map_end = memmap.map + params.mmap_size;
328 memmap.desc_size = params.desc_size;
329 memmap.desc_version = params.desc_ver;
330
331 if (uefi_init() < 0)
332 return;
333
334 reserve_regions();
335}
336
337void __init efi_idmap_init(void)
338{
74bcc249
LL
339 if (!efi_enabled(EFI_BOOT))
340 return;
341
f84d0275
MS
342 /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
343 efi_setup_idmap();
344}
345
346static int __init remap_region(efi_memory_desc_t *md, void **new)
347{
348 u64 paddr, vaddr, npages, size;
349
350 paddr = md->phys_addr;
351 npages = md->num_pages;
352 memrange_efi_to_native(&paddr, &npages);
353 size = npages << PAGE_SHIFT;
354
355 if (is_normal_ram(md))
356 vaddr = (__force u64)ioremap_cache(paddr, size);
357 else
358 vaddr = (__force u64)ioremap(paddr, size);
359
360 if (!vaddr) {
361 pr_err("Unable to remap 0x%llx pages @ %p\n",
362 npages, (void *)paddr);
363 return 0;
364 }
365
366 /* adjust for any rounding when EFI and system pagesize differs */
367 md->virt_addr = vaddr + (md->phys_addr - paddr);
368
369 if (uefi_debug)
370 pr_info(" EFI remap 0x%012llx => %p\n",
371 md->phys_addr, (void *)md->virt_addr);
372
373 memcpy(*new, md, memmap.desc_size);
374 *new += memmap.desc_size;
375
376 return 1;
377}
378
379/*
380 * Switch UEFI from an identity map to a kernel virtual map
381 */
382static int __init arm64_enter_virtual_mode(void)
383{
384 efi_memory_desc_t *md;
385 phys_addr_t virtmap_phys;
386 void *virtmap, *virt_md;
387 efi_status_t status;
388 u64 mapsize;
389 int count = 0;
390 unsigned long flags;
391
392 if (!efi_enabled(EFI_BOOT)) {
393 pr_info("EFI services will not be available.\n");
394 return -1;
395 }
396
397 pr_info("Remapping and enabling EFI services.\n");
398
399 /* replace early memmap mapping with permanent mapping */
400 mapsize = memmap.map_end - memmap.map;
401 early_memunmap(memmap.map, mapsize);
402 memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
403 mapsize);
404 memmap.map_end = memmap.map + mapsize;
405
406 efi.memmap = &memmap;
407
408 /* Map the runtime regions */
409 virtmap = kmalloc(mapsize, GFP_KERNEL);
410 if (!virtmap) {
411 pr_err("Failed to allocate EFI virtual memmap\n");
412 return -1;
413 }
414 virtmap_phys = virt_to_phys(virtmap);
415 virt_md = virtmap;
416
417 for_each_efi_memory_desc(&memmap, md) {
418 if (!(md->attribute & EFI_MEMORY_RUNTIME))
419 continue;
99a5603e
AB
420 if (!remap_region(md, &virt_md))
421 goto err_unmap;
422 ++count;
f84d0275
MS
423 }
424
425 efi.systab = (__force void *)efi_lookup_mapped_addr(efi_system_table);
99a5603e
AB
426 if (!efi.systab) {
427 /*
428 * If we have no virtual mapping for the System Table at this
429 * point, the memory map doesn't cover the physical offset where
430 * it resides. This means the System Table will be inaccessible
431 * to Runtime Services themselves once the virtual mapping is
432 * installed.
433 */
434 pr_err("Failed to remap EFI System Table -- buggy firmware?\n");
435 goto err_unmap;
436 }
437 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
f84d0275 438
668ebd10
YL
439 /*
440 * DMI depends on EFI on arm64, and dmi_scan_machine() needs to be
441 * called early because dmi_id_init(), which is an arch_initcall itself,
442 * depends on dmi_scan_machine() having been called already.
443 */
444 dmi_scan_machine();
445
f84d0275
MS
446 local_irq_save(flags);
447 cpu_switch_mm(idmap_pg_dir, &init_mm);
448
449 /* Call SetVirtualAddressMap with the physical address of the map */
450 runtime = efi.systab->runtime;
451 efi.set_virtual_address_map = runtime->set_virtual_address_map;
452
453 status = efi.set_virtual_address_map(count * memmap.desc_size,
454 memmap.desc_size,
455 memmap.desc_version,
456 (efi_memory_desc_t *)virtmap_phys);
457 cpu_set_reserved_ttbr0();
458 flush_tlb_all();
459 local_irq_restore(flags);
460
461 kfree(virtmap);
462
463 free_boot_services();
464
465 if (status != EFI_SUCCESS) {
466 pr_err("Failed to set EFI virtual address map! [%lx]\n",
467 status);
468 return -1;
469 }
470
471 /* Set up runtime services function pointers */
472 runtime = efi.systab->runtime;
e15dd494 473 efi_native_runtime_setup();
f84d0275
MS
474 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
475
6a7519e8
SP
476 efi.runtime_version = efi.systab->hdr.revision;
477
f84d0275 478 return 0;
99a5603e
AB
479
480err_unmap:
481 /* unmap all mappings that succeeded: there are 'count' of those */
482 for (virt_md = virtmap; count--; virt_md += memmap.desc_size) {
483 md = virt_md;
484 iounmap((__force void __iomem *)md->virt_addr);
485 }
486 kfree(virtmap);
487 return -1;
f84d0275
MS
488}
489early_initcall(arm64_enter_virtual_mode);