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/efi.h>
15 #include <linux/io.h>
16 #include <linux/memblock.h>
17 #include <linux/mm_types.h>
18 #include <linux/preempt.h>
19 #include <linux/rbtree.h>
20 #include <linux/rwsem.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 
25 #include <asm/cacheflush.h>
26 #include <asm/efi.h>
27 #include <asm/mmu.h>
28 #include <asm/pgalloc.h>
29 #include <asm/pgtable.h>
30 
31 extern u64 efi_system_table;
32 
33 static struct mm_struct efi_mm = {
34 	.mm_rb			= RB_ROOT,
35 	.mm_users		= ATOMIC_INIT(2),
36 	.mm_count		= ATOMIC_INIT(1),
37 	.mmap_sem		= __RWSEM_INITIALIZER(efi_mm.mmap_sem),
38 	.page_table_lock	= __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
39 	.mmlist			= LIST_HEAD_INIT(efi_mm.mmlist),
40 };
41 
42 static bool __init efi_virtmap_init(void)
43 {
44 	efi_memory_desc_t *md;
45 	bool systab_found;
46 
47 	efi_mm.pgd = pgd_alloc(&efi_mm);
48 	init_new_context(NULL, &efi_mm);
49 
50 	systab_found = false;
51 	for_each_efi_memory_desc(md) {
52 		phys_addr_t phys = md->phys_addr;
53 		int ret;
54 
55 		if (!(md->attribute & EFI_MEMORY_RUNTIME))
56 			continue;
57 		if (md->virt_addr == 0)
58 			return false;
59 
60 		ret = efi_create_mapping(&efi_mm, md);
61 		if  (!ret) {
62 			pr_info("  EFI remap %pa => %p\n",
63 				&phys, (void *)(unsigned long)md->virt_addr);
64 		} else {
65 			pr_warn("  EFI remap %pa: failed to create mapping (%d)\n",
66 				&phys, ret);
67 			return false;
68 		}
69 		/*
70 		 * If this entry covers the address of the UEFI system table,
71 		 * calculate and record its virtual address.
72 		 */
73 		if (efi_system_table >= phys &&
74 		    efi_system_table < phys + (md->num_pages * EFI_PAGE_SIZE)) {
75 			efi.systab = (void *)(unsigned long)(efi_system_table -
76 							     phys + md->virt_addr);
77 			systab_found = true;
78 		}
79 	}
80 	if (!systab_found) {
81 		pr_err("No virtual mapping found for the UEFI System Table\n");
82 		return false;
83 	}
84 
85 	if (efi_memattr_apply_permissions(&efi_mm, efi_set_mapping_permissions))
86 		return false;
87 
88 	return true;
89 }
90 
91 /*
92  * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
93  * non-early mapping of the UEFI system table and virtual mappings for all
94  * EFI_MEMORY_RUNTIME regions.
95  */
96 static int __init arm_enable_runtime_services(void)
97 {
98 	u64 mapsize;
99 
100 	if (!efi_enabled(EFI_BOOT)) {
101 		pr_info("EFI services will not be available.\n");
102 		return 0;
103 	}
104 
105 	if (efi_runtime_disabled()) {
106 		pr_info("EFI runtime services will be disabled.\n");
107 		return 0;
108 	}
109 
110 	if (efi_enabled(EFI_RUNTIME_SERVICES)) {
111 		pr_info("EFI runtime services access via paravirt.\n");
112 		return 0;
113 	}
114 
115 	pr_info("Remapping and enabling EFI services.\n");
116 
117 	mapsize = efi.memmap.map_end - efi.memmap.map;
118 
119 	efi.memmap.map = memremap(efi.memmap.phys_map, mapsize, MEMREMAP_WB);
120 	if (!efi.memmap.map) {
121 		pr_err("Failed to remap EFI memory map\n");
122 		return -ENOMEM;
123 	}
124 	efi.memmap.map_end = efi.memmap.map + mapsize;
125 
126 	if (!efi_virtmap_init()) {
127 		pr_err("UEFI virtual mapping missing or invalid -- runtime services will not be available\n");
128 		return -ENOMEM;
129 	}
130 
131 	/* Set up runtime services function pointers */
132 	efi_native_runtime_setup();
133 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
134 
135 	return 0;
136 }
137 early_initcall(arm_enable_runtime_services);
138 
139 void efi_virtmap_load(void)
140 {
141 	preempt_disable();
142 	efi_set_pgd(&efi_mm);
143 }
144 
145 void efi_virtmap_unload(void)
146 {
147 	efi_set_pgd(current->active_mm);
148 	preempt_enable();
149 }
150