]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/arm64/kernel/efi.c
arm64/efi: move SetVirtualAddressMap() to UEFI stub
[karo-tx-linux.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 int uefi_debug __initdata;
42 static int __init uefi_debug_setup(char *str)
43 {
44         uefi_debug = 1;
45
46         return 0;
47 }
48 early_param("uefi_debug", uefi_debug_setup);
49
50 static int __init is_normal_ram(efi_memory_desc_t *md)
51 {
52         if (md->attribute & EFI_MEMORY_WB)
53                 return 1;
54         return 0;
55 }
56
57 static void __init efi_setup_idmap(void)
58 {
59         struct memblock_region *r;
60         efi_memory_desc_t *md;
61         u64 paddr, npages, size;
62
63         for_each_memblock(memory, r)
64                 create_id_mapping(r->base, r->size, 0);
65
66         /* map runtime io spaces */
67         for_each_efi_memory_desc(&memmap, md) {
68                 if (!(md->attribute & EFI_MEMORY_RUNTIME) || is_normal_ram(md))
69                         continue;
70                 paddr = md->phys_addr;
71                 npages = md->num_pages;
72                 memrange_efi_to_native(&paddr, &npages);
73                 size = npages << PAGE_SHIFT;
74                 create_id_mapping(paddr, size, 1);
75         }
76 }
77
78 /*
79  * Translate a EFI virtual address into a physical address: this is necessary,
80  * as some data members of the EFI system table are virtually remapped after
81  * SetVirtualAddressMap() has been called.
82  */
83 static phys_addr_t efi_to_phys(unsigned long addr)
84 {
85         efi_memory_desc_t *md;
86
87         for_each_efi_memory_desc(&memmap, md) {
88                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
89                         continue;
90                 if (md->virt_addr == 0)
91                         /* no virtual mapping has been installed by the stub */
92                         break;
93                 if (md->virt_addr <= addr &&
94                     (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
95                         return md->phys_addr + addr - md->virt_addr;
96         }
97         return addr;
98 }
99
100 static int __init uefi_init(void)
101 {
102         efi_char16_t *c16;
103         void *config_tables;
104         u64 table_size;
105         char vendor[100] = "unknown";
106         int i, retval;
107
108         efi.systab = early_memremap(efi_system_table,
109                                     sizeof(efi_system_table_t));
110         if (efi.systab == NULL) {
111                 pr_warn("Unable to map EFI system table.\n");
112                 return -ENOMEM;
113         }
114
115         set_bit(EFI_BOOT, &efi.flags);
116         set_bit(EFI_64BIT, &efi.flags);
117
118         /*
119          * Verify the EFI Table
120          */
121         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
122                 pr_err("System table signature incorrect\n");
123                 retval = -EINVAL;
124                 goto out;
125         }
126         if ((efi.systab->hdr.revision >> 16) < 2)
127                 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
128                         efi.systab->hdr.revision >> 16,
129                         efi.systab->hdr.revision & 0xffff);
130
131         /* Show what we know for posterity */
132         c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
133                              sizeof(vendor));
134         if (c16) {
135                 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
136                         vendor[i] = c16[i];
137                 vendor[i] = '\0';
138                 early_memunmap(c16, sizeof(vendor));
139         }
140
141         pr_info("EFI v%u.%.02u by %s\n",
142                 efi.systab->hdr.revision >> 16,
143                 efi.systab->hdr.revision & 0xffff, vendor);
144
145         table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
146         config_tables = early_memremap(efi_to_phys(efi.systab->tables),
147                                        table_size);
148
149         retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
150                                          sizeof(efi_config_table_64_t), NULL);
151
152         early_memunmap(config_tables, table_size);
153 out:
154         early_memunmap(efi.systab,  sizeof(efi_system_table_t));
155         return retval;
156 }
157
158 /*
159  * Return true for RAM regions we want to permanently reserve.
160  */
161 static __init int is_reserve_region(efi_memory_desc_t *md)
162 {
163         switch (md->type) {
164         case EFI_LOADER_CODE:
165         case EFI_LOADER_DATA:
166         case EFI_BOOT_SERVICES_CODE:
167         case EFI_BOOT_SERVICES_DATA:
168         case EFI_CONVENTIONAL_MEMORY:
169                 return 0;
170         default:
171                 break;
172         }
173         return is_normal_ram(md);
174 }
175
176 static __init void reserve_regions(void)
177 {
178         efi_memory_desc_t *md;
179         u64 paddr, npages, size;
180
181         if (uefi_debug)
182                 pr_info("Processing EFI memory map:\n");
183
184         for_each_efi_memory_desc(&memmap, md) {
185                 paddr = md->phys_addr;
186                 npages = md->num_pages;
187
188                 if (uefi_debug) {
189                         char buf[64];
190
191                         pr_info("  0x%012llx-0x%012llx %s",
192                                 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
193                                 efi_md_typeattr_format(buf, sizeof(buf), md));
194                 }
195
196                 memrange_efi_to_native(&paddr, &npages);
197                 size = npages << PAGE_SHIFT;
198
199                 if (is_normal_ram(md))
200                         early_init_dt_add_memory_arch(paddr, size);
201
202                 if (is_reserve_region(md) ||
203                     md->type == EFI_BOOT_SERVICES_CODE ||
204                     md->type == EFI_BOOT_SERVICES_DATA) {
205                         memblock_reserve(paddr, size);
206                         if (uefi_debug)
207                                 pr_cont("*");
208                 }
209
210                 if (uefi_debug)
211                         pr_cont("\n");
212         }
213
214         set_bit(EFI_MEMMAP, &efi.flags);
215 }
216
217
218 static u64 __init free_one_region(u64 start, u64 end)
219 {
220         u64 size = end - start;
221
222         if (uefi_debug)
223                 pr_info("  EFI freeing: 0x%012llx-0x%012llx\n", start, end - 1);
224
225         free_bootmem_late(start, size);
226         return size;
227 }
228
229 static u64 __init free_region(u64 start, u64 end)
230 {
231         u64 map_start, map_end, total = 0;
232
233         if (end <= start)
234                 return total;
235
236         map_start = (u64)memmap.phys_map;
237         map_end = PAGE_ALIGN(map_start + (memmap.map_end - memmap.map));
238         map_start &= PAGE_MASK;
239
240         if (start < map_end && end > map_start) {
241                 /* region overlaps UEFI memmap */
242                 if (start < map_start)
243                         total += free_one_region(start, map_start);
244
245                 if (map_end < end)
246                         total += free_one_region(map_end, end);
247         } else
248                 total += free_one_region(start, end);
249
250         return total;
251 }
252
253 static void __init free_boot_services(void)
254 {
255         u64 total_freed = 0;
256         u64 keep_end, free_start, free_end;
257         efi_memory_desc_t *md;
258
259         /*
260          * If kernel uses larger pages than UEFI, we have to be careful
261          * not to inadvertantly free memory we want to keep if there is
262          * overlap at the kernel page size alignment. We do not want to
263          * free is_reserve_region() memory nor the UEFI memmap itself.
264          *
265          * The memory map is sorted, so we keep track of the end of
266          * any previous region we want to keep, remember any region
267          * we want to free and defer freeing it until we encounter
268          * the next region we want to keep. This way, before freeing
269          * it, we can clip it as needed to avoid freeing memory we
270          * want to keep for UEFI.
271          */
272
273         keep_end = 0;
274         free_start = 0;
275
276         for_each_efi_memory_desc(&memmap, md) {
277                 u64 paddr, npages, size;
278
279                 if (is_reserve_region(md)) {
280                         /*
281                          * We don't want to free any memory from this region.
282                          */
283                         if (free_start) {
284                                 /* adjust free_end then free region */
285                                 if (free_end > md->phys_addr)
286                                         free_end -= PAGE_SIZE;
287                                 total_freed += free_region(free_start, free_end);
288                                 free_start = 0;
289                         }
290                         keep_end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
291                         continue;
292                 }
293
294                 if (md->type != EFI_BOOT_SERVICES_CODE &&
295                     md->type != EFI_BOOT_SERVICES_DATA) {
296                         /* no need to free this region */
297                         continue;
298                 }
299
300                 /*
301                  * We want to free memory from this region.
302                  */
303                 paddr = md->phys_addr;
304                 npages = md->num_pages;
305                 memrange_efi_to_native(&paddr, &npages);
306                 size = npages << PAGE_SHIFT;
307
308                 if (free_start) {
309                         if (paddr <= free_end)
310                                 free_end = paddr + size;
311                         else {
312                                 total_freed += free_region(free_start, free_end);
313                                 free_start = paddr;
314                                 free_end = paddr + size;
315                         }
316                 } else {
317                         free_start = paddr;
318                         free_end = paddr + size;
319                 }
320                 if (free_start < keep_end) {
321                         free_start += PAGE_SIZE;
322                         if (free_start >= free_end)
323                                 free_start = 0;
324                 }
325         }
326         if (free_start)
327                 total_freed += free_region(free_start, free_end);
328
329         if (total_freed)
330                 pr_info("Freed 0x%llx bytes of EFI boot services memory",
331                         total_freed);
332 }
333
334 void __init efi_init(void)
335 {
336         struct efi_fdt_params params;
337
338         /* Grab UEFI information placed in FDT by stub */
339         if (!efi_get_fdt_params(&params, uefi_debug))
340                 return;
341
342         efi_system_table = params.system_table;
343
344         memblock_reserve(params.mmap & PAGE_MASK,
345                          PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
346         memmap.phys_map = (void *)params.mmap;
347         memmap.map = early_memremap(params.mmap, params.mmap_size);
348         memmap.map_end = memmap.map + params.mmap_size;
349         memmap.desc_size = params.desc_size;
350         memmap.desc_version = params.desc_ver;
351
352         if (uefi_init() < 0)
353                 return;
354
355         reserve_regions();
356 }
357
358 void __init efi_idmap_init(void)
359 {
360         if (!efi_enabled(EFI_BOOT))
361                 return;
362
363         /* boot time idmap_pg_dir is incomplete, so fill in missing parts */
364         efi_setup_idmap();
365         early_memunmap(memmap.map, memmap.map_end - memmap.map);
366 }
367
368 /*
369  * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
370  * non-early mapping of the UEFI system table and virtual mappings for all
371  * EFI_MEMORY_RUNTIME regions.
372  */
373 static int __init arm64_enable_runtime_services(void)
374 {
375         u64 mapsize;
376
377         if (!efi_enabled(EFI_BOOT)) {
378                 pr_info("EFI services will not be available.\n");
379                 return -1;
380         }
381
382         mapsize = memmap.map_end - memmap.map;
383
384         if (efi_runtime_disabled()) {
385                 pr_info("EFI runtime services will be disabled.\n");
386                 return -1;
387         }
388
389         pr_info("Remapping and enabling EFI services.\n");
390         /* replace early memmap mapping with permanent mapping */
391         memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
392                                                    mapsize);
393         memmap.map_end = memmap.map + mapsize;
394
395         efi.memmap = &memmap;
396
397         efi.systab = (__force void *)ioremap_cache(efi_system_table,
398                                                    sizeof(efi_system_table_t));
399         if (!efi.systab) {
400                 pr_err("Failed to remap EFI System Table\n");
401                 return -1;
402         }
403         set_bit(EFI_SYSTEM_TABLES, &efi.flags);
404
405         free_boot_services();
406
407         if (!efi_enabled(EFI_VIRTMAP)) {
408                 pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
409                 return -1;
410         }
411
412         /* Set up runtime services function pointers */
413         efi_native_runtime_setup();
414         set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
415
416         efi.runtime_version = efi.systab->hdr.revision;
417
418         return 0;
419 }
420 early_initcall(arm64_enable_runtime_services);
421
422 static int __init arm64_dmi_init(void)
423 {
424         /*
425          * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
426          * be called early because dmi_id_init(), which is an arch_initcall
427          * itself, depends on dmi_scan_machine() having been called already.
428          */
429         dmi_scan_machine();
430         if (dmi_available)
431                 dmi_set_dump_stack_arch_desc();
432         return 0;
433 }
434 core_initcall(arm64_dmi_init);
435
436 static pgd_t efi_pgd[PTRS_PER_PGD] __page_aligned_bss;
437
438 static struct mm_struct efi_mm = {
439         .mm_rb                  = RB_ROOT,
440         .pgd                    = efi_pgd,
441         .mm_users               = ATOMIC_INIT(2),
442         .mm_count               = ATOMIC_INIT(1),
443         .mmap_sem               = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
444         .page_table_lock        = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
445         .mmlist                 = LIST_HEAD_INIT(efi_mm.mmlist),
446         INIT_MM_CONTEXT(efi_mm)
447 };
448
449 static void efi_set_pgd(struct mm_struct *mm)
450 {
451         cpu_switch_mm(mm->pgd, mm);
452         flush_tlb_all();
453         if (icache_is_aivivt())
454                 __flush_icache_all();
455 }
456
457 void efi_virtmap_load(void)
458 {
459         preempt_disable();
460         efi_set_pgd(&efi_mm);
461 }
462
463 void efi_virtmap_unload(void)
464 {
465         efi_set_pgd(current->active_mm);
466         preempt_enable();
467 }
468
469 void __init efi_virtmap_init(void)
470 {
471         efi_memory_desc_t *md;
472
473         if (!efi_enabled(EFI_BOOT))
474                 return;
475
476         for_each_efi_memory_desc(&memmap, md) {
477                 u64 paddr, npages, size;
478                 pgprot_t prot;
479
480                 if (!(md->attribute & EFI_MEMORY_RUNTIME))
481                         continue;
482                 if (WARN(md->virt_addr == 0,
483                          "UEFI virtual mapping incomplete or missing -- no entry found for 0x%llx\n",
484                          md->phys_addr))
485                         return;
486
487                 paddr = md->phys_addr;
488                 npages = md->num_pages;
489                 memrange_efi_to_native(&paddr, &npages);
490                 size = npages << PAGE_SHIFT;
491
492                 pr_info("  EFI remap 0x%016llx => %p\n",
493                         md->phys_addr, (void *)md->virt_addr);
494
495                 /*
496                  * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
497                  * executable, everything else can be mapped with the XN bits
498                  * set.
499                  */
500                 if (!is_normal_ram(md))
501                         prot = __pgprot(PROT_DEVICE_nGnRE);
502                 else if (md->type == EFI_RUNTIME_SERVICES_CODE)
503                         prot = PAGE_KERNEL_EXEC;
504                 else
505                         prot = PAGE_KERNEL;
506
507                 create_pgd_mapping(&efi_mm, paddr, md->virt_addr, size, prot);
508         }
509         set_bit(EFI_VIRTMAP, &efi.flags);
510 }