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