blob: 8f5db4a3c9d9c51f284c276d2d98bea9883d3529 [file] [log] [blame]
Mark Salterf84d0272014-04-15 21:59:30 -04001/*
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/efi.h>
15#include <linux/export.h>
16#include <linux/memblock.h>
17#include <linux/bootmem.h>
18#include <linux/of.h>
19#include <linux/of_fdt.h>
20#include <linux/sched.h>
21#include <linux/slab.h>
22
23#include <asm/cacheflush.h>
24#include <asm/efi.h>
25#include <asm/tlbflush.h>
26#include <asm/mmu_context.h>
27
28struct efi_memory_map memmap;
29
30static efi_runtime_services_t *runtime;
31
32static u64 efi_system_table;
33
34static int uefi_debug __initdata;
35static int __init uefi_debug_setup(char *str)
36{
37 uefi_debug = 1;
38
39 return 0;
40}
41early_param("uefi_debug", uefi_debug_setup);
42
43static int __init is_normal_ram(efi_memory_desc_t *md)
44{
45 if (md->attribute & EFI_MEMORY_WB)
46 return 1;
47 return 0;
48}
49
50static void __init efi_setup_idmap(void)
51{
52 struct memblock_region *r;
53 efi_memory_desc_t *md;
54 u64 paddr, npages, size;
55
56 for_each_memblock(memory, r)
57 create_id_mapping(r->base, r->size, 0);
58
59 /* map runtime io spaces */
60 for_each_efi_memory_desc(&memmap, md) {
61 if (!(md->attribute & EFI_MEMORY_RUNTIME) || is_normal_ram(md))
62 continue;
63 paddr = md->phys_addr;
64 npages = md->num_pages;
65 memrange_efi_to_native(&paddr, &npages);
66 size = npages << PAGE_SHIFT;
67 create_id_mapping(paddr, size, 1);
68 }
69}
70
71static int __init uefi_init(void)
72{
73 efi_char16_t *c16;
74 char vendor[100] = "unknown";
75 int i, retval;
76
77 efi.systab = early_memremap(efi_system_table,
78 sizeof(efi_system_table_t));
79 if (efi.systab == NULL) {
80 pr_warn("Unable to map EFI system table.\n");
81 return -ENOMEM;
82 }
83
84 set_bit(EFI_BOOT, &efi.flags);
85 set_bit(EFI_64BIT, &efi.flags);
86
87 /*
88 * Verify the EFI Table
89 */
90 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
91 pr_err("System table signature incorrect\n");
Dave Young88f8abd2014-08-14 17:15:29 +080092 retval = -EINVAL;
93 goto out;
Mark Salterf84d0272014-04-15 21:59:30 -040094 }
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';
Dave Young88f8abd2014-08-14 17:15:29 +0800107 early_memunmap(c16, sizeof(vendor));
Mark Salterf84d0272014-04-15 21:59:30 -0400108 }
109
110 pr_info("EFI v%u.%.02u by %s\n",
111 efi.systab->hdr.revision >> 16,
112 efi.systab->hdr.revision & 0xffff, vendor);
113
114 retval = efi_config_init(NULL);
115 if (retval == 0)
116 set_bit(EFI_CONFIG_TABLES, &efi.flags);
117
Dave Young88f8abd2014-08-14 17:15:29 +0800118out:
Mark Salterf84d0272014-04-15 21:59:30 -0400119 early_memunmap(efi.systab, sizeof(efi_system_table_t));
Mark Salterf84d0272014-04-15 21:59:30 -0400120 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 }
192}
193
194
195static u64 __init free_one_region(u64 start, u64 end)
196{
197 u64 size = end - start;
198
199 if (uefi_debug)
200 pr_info(" EFI freeing: 0x%012llx-0x%012llx\n", start, end - 1);
201
202 free_bootmem_late(start, size);
203 return size;
204}
205
206static u64 __init free_region(u64 start, u64 end)
207{
208 u64 map_start, map_end, total = 0;
209
210 if (end <= start)
211 return total;
212
213 map_start = (u64)memmap.phys_map;
214 map_end = PAGE_ALIGN(map_start + (memmap.map_end - memmap.map));
215 map_start &= PAGE_MASK;
216
217 if (start < map_end && end > map_start) {
218 /* region overlaps UEFI memmap */
219 if (start < map_start)
220 total += free_one_region(start, map_start);
221
222 if (map_end < end)
223 total += free_one_region(map_end, end);
224 } else
225 total += free_one_region(start, end);
226
227 return total;
228}
229
230static void __init free_boot_services(void)
231{
232 u64 total_freed = 0;
233 u64 keep_end, free_start, free_end;
234 efi_memory_desc_t *md;
235
236 /*
237 * If kernel uses larger pages than UEFI, we have to be careful
238 * not to inadvertantly free memory we want to keep if there is
239 * overlap at the kernel page size alignment. We do not want to
240 * free is_reserve_region() memory nor the UEFI memmap itself.
241 *
242 * The memory map is sorted, so we keep track of the end of
243 * any previous region we want to keep, remember any region
244 * we want to free and defer freeing it until we encounter
245 * the next region we want to keep. This way, before freeing
246 * it, we can clip it as needed to avoid freeing memory we
247 * want to keep for UEFI.
248 */
249
250 keep_end = 0;
251 free_start = 0;
252
253 for_each_efi_memory_desc(&memmap, md) {
254 u64 paddr, npages, size;
255
256 if (is_reserve_region(md)) {
257 /*
258 * We don't want to free any memory from this region.
259 */
260 if (free_start) {
261 /* adjust free_end then free region */
262 if (free_end > md->phys_addr)
263 free_end -= PAGE_SIZE;
264 total_freed += free_region(free_start, free_end);
265 free_start = 0;
266 }
267 keep_end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
268 continue;
269 }
270
271 if (md->type != EFI_BOOT_SERVICES_CODE &&
272 md->type != EFI_BOOT_SERVICES_DATA) {
273 /* no need to free this region */
274 continue;
275 }
276
277 /*
278 * We want to free memory from this region.
279 */
280 paddr = md->phys_addr;
281 npages = md->num_pages;
282 memrange_efi_to_native(&paddr, &npages);
283 size = npages << PAGE_SHIFT;
284
285 if (free_start) {
286 if (paddr <= free_end)
287 free_end = paddr + size;
288 else {
289 total_freed += free_region(free_start, free_end);
290 free_start = paddr;
291 free_end = paddr + size;
292 }
293 } else {
294 free_start = paddr;
295 free_end = paddr + size;
296 }
297 if (free_start < keep_end) {
298 free_start += PAGE_SIZE;
299 if (free_start >= free_end)
300 free_start = 0;
301 }
302 }
303 if (free_start)
304 total_freed += free_region(free_start, free_end);
305
306 if (total_freed)
307 pr_info("Freed 0x%llx bytes of EFI boot services memory",
308 total_freed);
309}
310
311void __init efi_init(void)
312{
313 struct efi_fdt_params params;
314
315 /* Grab UEFI information placed in FDT by stub */
316 if (!efi_get_fdt_params(&params, uefi_debug))
317 return;
318
319 efi_system_table = params.system_table;
320
321 memblock_reserve(params.mmap & PAGE_MASK,
322 PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
323 memmap.phys_map = (void *)params.mmap;
324 memmap.map = early_memremap(params.mmap, params.mmap_size);
325 memmap.map_end = memmap.map + params.mmap_size;
326 memmap.desc_size = params.desc_size;
327 memmap.desc_version = params.desc_ver;
328
329 if (uefi_init() < 0)
330 return;
331
332 reserve_regions();
333}
334
335void __init efi_idmap_init(void)
336{
Leif Lindholm74bcc242014-05-23 14:16:56 +0100337 if (!efi_enabled(EFI_BOOT))
338 return;
339
Mark Salterf84d0272014-04-15 21:59:30 -0400340 /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
341 efi_setup_idmap();
342}
343
344static int __init remap_region(efi_memory_desc_t *md, void **new)
345{
346 u64 paddr, vaddr, npages, size;
347
348 paddr = md->phys_addr;
349 npages = md->num_pages;
350 memrange_efi_to_native(&paddr, &npages);
351 size = npages << PAGE_SHIFT;
352
353 if (is_normal_ram(md))
354 vaddr = (__force u64)ioremap_cache(paddr, size);
355 else
356 vaddr = (__force u64)ioremap(paddr, size);
357
358 if (!vaddr) {
359 pr_err("Unable to remap 0x%llx pages @ %p\n",
360 npages, (void *)paddr);
361 return 0;
362 }
363
364 /* adjust for any rounding when EFI and system pagesize differs */
365 md->virt_addr = vaddr + (md->phys_addr - paddr);
366
367 if (uefi_debug)
368 pr_info(" EFI remap 0x%012llx => %p\n",
369 md->phys_addr, (void *)md->virt_addr);
370
371 memcpy(*new, md, memmap.desc_size);
372 *new += memmap.desc_size;
373
374 return 1;
375}
376
377/*
378 * Switch UEFI from an identity map to a kernel virtual map
379 */
380static int __init arm64_enter_virtual_mode(void)
381{
382 efi_memory_desc_t *md;
383 phys_addr_t virtmap_phys;
384 void *virtmap, *virt_md;
385 efi_status_t status;
386 u64 mapsize;
387 int count = 0;
388 unsigned long flags;
389
390 if (!efi_enabled(EFI_BOOT)) {
391 pr_info("EFI services will not be available.\n");
392 return -1;
393 }
394
Mark Salterf84d0272014-04-15 21:59:30 -0400395 mapsize = memmap.map_end - memmap.map;
396 early_memunmap(memmap.map, mapsize);
Dave Young66322102014-08-18 09:30:07 +0800397
398 if (efi_runtime_disabled()) {
399 pr_info("EFI runtime services will be disabled.\n");
400 return -1;
401 }
402
403 pr_info("Remapping and enabling EFI services.\n");
404 /* replace early memmap mapping with permanent mapping */
Mark Salterf84d0272014-04-15 21:59:30 -0400405 memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
406 mapsize);
407 memmap.map_end = memmap.map + mapsize;
408
409 efi.memmap = &memmap;
410
411 /* Map the runtime regions */
412 virtmap = kmalloc(mapsize, GFP_KERNEL);
413 if (!virtmap) {
414 pr_err("Failed to allocate EFI virtual memmap\n");
415 return -1;
416 }
417 virtmap_phys = virt_to_phys(virtmap);
418 virt_md = virtmap;
419
420 for_each_efi_memory_desc(&memmap, md) {
421 if (!(md->attribute & EFI_MEMORY_RUNTIME))
422 continue;
Ard Biesheuvel99a56032014-07-04 17:25:09 +0200423 if (!remap_region(md, &virt_md))
424 goto err_unmap;
425 ++count;
Mark Salterf84d0272014-04-15 21:59:30 -0400426 }
427
428 efi.systab = (__force void *)efi_lookup_mapped_addr(efi_system_table);
Ard Biesheuvel99a56032014-07-04 17:25:09 +0200429 if (!efi.systab) {
430 /*
431 * If we have no virtual mapping for the System Table at this
432 * point, the memory map doesn't cover the physical offset where
433 * it resides. This means the System Table will be inaccessible
434 * to Runtime Services themselves once the virtual mapping is
435 * installed.
436 */
437 pr_err("Failed to remap EFI System Table -- buggy firmware?\n");
438 goto err_unmap;
439 }
440 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Mark Salterf84d0272014-04-15 21:59:30 -0400441
442 local_irq_save(flags);
443 cpu_switch_mm(idmap_pg_dir, &init_mm);
444
445 /* Call SetVirtualAddressMap with the physical address of the map */
446 runtime = efi.systab->runtime;
447 efi.set_virtual_address_map = runtime->set_virtual_address_map;
448
449 status = efi.set_virtual_address_map(count * memmap.desc_size,
450 memmap.desc_size,
451 memmap.desc_version,
452 (efi_memory_desc_t *)virtmap_phys);
453 cpu_set_reserved_ttbr0();
454 flush_tlb_all();
455 local_irq_restore(flags);
456
457 kfree(virtmap);
458
459 free_boot_services();
460
461 if (status != EFI_SUCCESS) {
462 pr_err("Failed to set EFI virtual address map! [%lx]\n",
463 status);
464 return -1;
465 }
466
467 /* Set up runtime services function pointers */
468 runtime = efi.systab->runtime;
Ard Biesheuvele15dd492014-07-04 19:41:53 +0200469 efi_native_runtime_setup();
Mark Salterf84d0272014-04-15 21:59:30 -0400470 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
471
472 return 0;
Ard Biesheuvel99a56032014-07-04 17:25:09 +0200473
474err_unmap:
475 /* unmap all mappings that succeeded: there are 'count' of those */
476 for (virt_md = virtmap; count--; virt_md += memmap.desc_size) {
477 md = virt_md;
478 iounmap((__force void __iomem *)md->virt_addr);
479 }
480 kfree(virtmap);
481 return -1;
Mark Salterf84d0272014-04-15 21:59:30 -0400482}
483early_initcall(arm64_enter_virtual_mode);