Commit | Line | Data |
---|---|---|
5b83683f HY |
1 | /* |
2 | * Common EFI (Extensible Firmware Interface) support functions | |
3 | * Based on Extensible Firmware Interface Specification version 1.0 | |
4 | * | |
5 | * Copyright (C) 1999 VA Linux Systems | |
6 | * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> | |
7 | * Copyright (C) 1999-2002 Hewlett-Packard Co. | |
8 | * David Mosberger-Tang <davidm@hpl.hp.com> | |
9 | * Stephane Eranian <eranian@hpl.hp.com> | |
10 | * Copyright (C) 2005-2008 Intel Co. | |
11 | * Fenghua Yu <fenghua.yu@intel.com> | |
12 | * Bibo Mao <bibo.mao@intel.com> | |
13 | * Chandramouli Narayanan <mouli@linux.intel.com> | |
14 | * Huang Ying <ying.huang@intel.com> | |
15 | * | |
16 | * Copied from efi_32.c to eliminate the duplicated code between EFI | |
17 | * 32/64 support code. --ying 2007-10-26 | |
18 | * | |
19 | * All EFI Runtime Services are not implemented yet as EFI only | |
20 | * supports physical mode addressing on SoftSDV. This is to be fixed | |
21 | * in a future version. --drummond 1999-07-20 | |
22 | * | |
23 | * Implemented EFI runtime services and virtual mode calls. --davidm | |
24 | * | |
25 | * Goutham Rao: <goutham.rao@intel.com> | |
26 | * Skip non-WB memory and ignore empty memory ranges. | |
27 | */ | |
28 | ||
e3cb3f5a OJ |
29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
30 | ||
5b83683f HY |
31 | #include <linux/kernel.h> |
32 | #include <linux/init.h> | |
33 | #include <linux/efi.h> | |
2223af38 | 34 | #include <linux/efi-bgrt.h> |
69c60c88 | 35 | #include <linux/export.h> |
5b83683f | 36 | #include <linux/bootmem.h> |
a9ce6bc1 | 37 | #include <linux/memblock.h> |
5b83683f HY |
38 | #include <linux/spinlock.h> |
39 | #include <linux/uaccess.h> | |
40 | #include <linux/time.h> | |
41 | #include <linux/io.h> | |
42 | #include <linux/reboot.h> | |
43 | #include <linux/bcd.h> | |
31ff2f20 | 44 | #include <linux/ucs2_string.h> |
5b83683f HY |
45 | |
46 | #include <asm/setup.h> | |
47 | #include <asm/efi.h> | |
48 | #include <asm/time.h> | |
a2172e25 HY |
49 | #include <asm/cacheflush.h> |
50 | #include <asm/tlbflush.h> | |
7bd867df | 51 | #include <asm/x86_init.h> |
5b83683f HY |
52 | |
53 | #define EFI_DEBUG 1 | |
5b83683f | 54 | |
31ff2f20 MG |
55 | /* |
56 | * There's some additional metadata associated with each | |
57 | * variable. Intel's reference implementation is 60 bytes - bump that | |
58 | * to account for potential alignment constraints | |
59 | */ | |
60 | #define VAR_METADATA_SIZE 64 | |
61 | ||
d80603c9 JB |
62 | struct efi __read_mostly efi = { |
63 | .mps = EFI_INVALID_TABLE_ADDR, | |
64 | .acpi = EFI_INVALID_TABLE_ADDR, | |
65 | .acpi20 = EFI_INVALID_TABLE_ADDR, | |
66 | .smbios = EFI_INVALID_TABLE_ADDR, | |
67 | .sal_systab = EFI_INVALID_TABLE_ADDR, | |
68 | .boot_info = EFI_INVALID_TABLE_ADDR, | |
69 | .hcdp = EFI_INVALID_TABLE_ADDR, | |
70 | .uga = EFI_INVALID_TABLE_ADDR, | |
71 | .uv_systab = EFI_INVALID_TABLE_ADDR, | |
72 | }; | |
5b83683f HY |
73 | EXPORT_SYMBOL(efi); |
74 | ||
75 | struct efi_memory_map memmap; | |
76 | ||
ecaea42e | 77 | static struct efi efi_phys __initdata; |
5b83683f HY |
78 | static efi_system_table_t efi_systab __initdata; |
79 | ||
cc5a080c MG |
80 | static u64 efi_var_store_size; |
81 | static u64 efi_var_remaining_size; | |
82 | static u64 efi_var_max_var_size; | |
31ff2f20 MG |
83 | static u64 boot_used_size; |
84 | static u64 boot_var_size; | |
85 | static u64 active_size; | |
cc5a080c | 86 | |
83e68189 MF |
87 | unsigned long x86_efi_facility; |
88 | ||
89 | /* | |
90 | * Returns 1 if 'facility' is enabled, 0 otherwise. | |
91 | */ | |
92 | int efi_enabled(int facility) | |
93 | { | |
94 | return test_bit(facility, &x86_efi_facility) != 0; | |
5189c2a7 | 95 | } |
83e68189 | 96 | EXPORT_SYMBOL(efi_enabled); |
5189c2a7 | 97 | |
058e7b58 | 98 | static bool __initdata disable_runtime = false; |
8b2cb7a8 HY |
99 | static int __init setup_noefi(char *arg) |
100 | { | |
fb834c7a | 101 | disable_runtime = true; |
8b2cb7a8 HY |
102 | return 0; |
103 | } | |
104 | early_param("noefi", setup_noefi); | |
105 | ||
200001eb PJ |
106 | int add_efi_memmap; |
107 | EXPORT_SYMBOL(add_efi_memmap); | |
108 | ||
109 | static int __init setup_add_efi_memmap(char *arg) | |
110 | { | |
111 | add_efi_memmap = 1; | |
112 | return 0; | |
113 | } | |
114 | early_param("add_efi_memmap", setup_add_efi_memmap); | |
115 | ||
116 | ||
5b83683f HY |
117 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
118 | { | |
ef68c8f8 JB |
119 | unsigned long flags; |
120 | efi_status_t status; | |
121 | ||
122 | spin_lock_irqsave(&rtc_lock, flags); | |
123 | status = efi_call_virt2(get_time, tm, tc); | |
124 | spin_unlock_irqrestore(&rtc_lock, flags); | |
125 | return status; | |
5b83683f HY |
126 | } |
127 | ||
128 | static efi_status_t virt_efi_set_time(efi_time_t *tm) | |
129 | { | |
ef68c8f8 JB |
130 | unsigned long flags; |
131 | efi_status_t status; | |
132 | ||
133 | spin_lock_irqsave(&rtc_lock, flags); | |
134 | status = efi_call_virt1(set_time, tm); | |
135 | spin_unlock_irqrestore(&rtc_lock, flags); | |
136 | return status; | |
5b83683f HY |
137 | } |
138 | ||
139 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, | |
140 | efi_bool_t *pending, | |
141 | efi_time_t *tm) | |
142 | { | |
ef68c8f8 JB |
143 | unsigned long flags; |
144 | efi_status_t status; | |
145 | ||
146 | spin_lock_irqsave(&rtc_lock, flags); | |
147 | status = efi_call_virt3(get_wakeup_time, | |
148 | enabled, pending, tm); | |
149 | spin_unlock_irqrestore(&rtc_lock, flags); | |
150 | return status; | |
5b83683f HY |
151 | } |
152 | ||
153 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) | |
154 | { | |
ef68c8f8 JB |
155 | unsigned long flags; |
156 | efi_status_t status; | |
157 | ||
158 | spin_lock_irqsave(&rtc_lock, flags); | |
159 | status = efi_call_virt2(set_wakeup_time, | |
160 | enabled, tm); | |
161 | spin_unlock_irqrestore(&rtc_lock, flags); | |
162 | return status; | |
5b83683f HY |
163 | } |
164 | ||
165 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, | |
166 | efi_guid_t *vendor, | |
167 | u32 *attr, | |
168 | unsigned long *data_size, | |
169 | void *data) | |
170 | { | |
171 | return efi_call_virt5(get_variable, | |
172 | name, vendor, attr, | |
173 | data_size, data); | |
174 | } | |
175 | ||
176 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, | |
177 | efi_char16_t *name, | |
178 | efi_guid_t *vendor) | |
179 | { | |
31ff2f20 MG |
180 | efi_status_t status; |
181 | static bool finished = false; | |
182 | static u64 var_size; | |
183 | ||
184 | status = efi_call_virt3(get_next_variable, | |
185 | name_size, name, vendor); | |
186 | ||
187 | if (status == EFI_NOT_FOUND) { | |
188 | finished = true; | |
189 | if (var_size < boot_used_size) { | |
190 | boot_var_size = boot_used_size - var_size; | |
191 | active_size += boot_var_size; | |
192 | } else { | |
193 | printk(KERN_WARNING FW_BUG "efi: Inconsistent initial sizes\n"); | |
194 | } | |
195 | } | |
196 | ||
197 | if (boot_used_size && !finished) { | |
198 | unsigned long size; | |
199 | u32 attr; | |
200 | efi_status_t s; | |
201 | void *tmp; | |
202 | ||
203 | s = virt_efi_get_variable(name, vendor, &attr, &size, NULL); | |
204 | ||
205 | if (s != EFI_BUFFER_TOO_SMALL || !size) | |
206 | return status; | |
207 | ||
208 | tmp = kmalloc(size, GFP_ATOMIC); | |
209 | ||
210 | if (!tmp) | |
211 | return status; | |
212 | ||
213 | s = virt_efi_get_variable(name, vendor, &attr, &size, tmp); | |
214 | ||
215 | if (s == EFI_SUCCESS && (attr & EFI_VARIABLE_NON_VOLATILE)) { | |
216 | var_size += size; | |
217 | var_size += ucs2_strsize(name, 1024); | |
218 | active_size += size; | |
219 | active_size += VAR_METADATA_SIZE; | |
220 | active_size += ucs2_strsize(name, 1024); | |
221 | } | |
222 | ||
223 | kfree(tmp); | |
224 | } | |
225 | ||
226 | return status; | |
5b83683f HY |
227 | } |
228 | ||
229 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, | |
230 | efi_guid_t *vendor, | |
f7a2d73f | 231 | u32 attr, |
5b83683f HY |
232 | unsigned long data_size, |
233 | void *data) | |
234 | { | |
31ff2f20 MG |
235 | efi_status_t status; |
236 | u32 orig_attr = 0; | |
237 | unsigned long orig_size = 0; | |
238 | ||
239 | status = virt_efi_get_variable(name, vendor, &orig_attr, &orig_size, | |
240 | NULL); | |
241 | ||
242 | if (status != EFI_BUFFER_TOO_SMALL) | |
243 | orig_size = 0; | |
244 | ||
245 | status = efi_call_virt5(set_variable, | |
246 | name, vendor, attr, | |
247 | data_size, data); | |
248 | ||
249 | if (status == EFI_SUCCESS) { | |
250 | if (orig_size) { | |
251 | active_size -= orig_size; | |
252 | active_size -= ucs2_strsize(name, 1024); | |
253 | active_size -= VAR_METADATA_SIZE; | |
254 | } | |
255 | if (data_size) { | |
256 | active_size += data_size; | |
257 | active_size += ucs2_strsize(name, 1024); | |
258 | active_size += VAR_METADATA_SIZE; | |
259 | } | |
260 | } | |
261 | ||
262 | return status; | |
5b83683f HY |
263 | } |
264 | ||
3b370237 MG |
265 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
266 | u64 *storage_space, | |
267 | u64 *remaining_space, | |
268 | u64 *max_variable_size) | |
269 | { | |
270 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
271 | return EFI_UNSUPPORTED; | |
272 | ||
273 | return efi_call_virt4(query_variable_info, attr, storage_space, | |
274 | remaining_space, max_variable_size); | |
275 | } | |
276 | ||
5b83683f HY |
277 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
278 | { | |
279 | return efi_call_virt1(get_next_high_mono_count, count); | |
280 | } | |
281 | ||
282 | static void virt_efi_reset_system(int reset_type, | |
283 | efi_status_t status, | |
284 | unsigned long data_size, | |
285 | efi_char16_t *data) | |
286 | { | |
287 | efi_call_virt4(reset_system, reset_type, status, | |
288 | data_size, data); | |
289 | } | |
290 | ||
3b370237 MG |
291 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, |
292 | unsigned long count, | |
293 | unsigned long sg_list) | |
294 | { | |
295 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
296 | return EFI_UNSUPPORTED; | |
297 | ||
298 | return efi_call_virt3(update_capsule, capsules, count, sg_list); | |
299 | } | |
300 | ||
301 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, | |
302 | unsigned long count, | |
303 | u64 *max_size, | |
304 | int *reset_type) | |
305 | { | |
306 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
307 | return EFI_UNSUPPORTED; | |
308 | ||
309 | return efi_call_virt4(query_capsule_caps, capsules, count, max_size, | |
310 | reset_type); | |
311 | } | |
312 | ||
5b83683f HY |
313 | static efi_status_t __init phys_efi_set_virtual_address_map( |
314 | unsigned long memory_map_size, | |
315 | unsigned long descriptor_size, | |
316 | u32 descriptor_version, | |
317 | efi_memory_desc_t *virtual_map) | |
318 | { | |
319 | efi_status_t status; | |
320 | ||
321 | efi_call_phys_prelog(); | |
322 | status = efi_call_phys4(efi_phys.set_virtual_address_map, | |
323 | memory_map_size, descriptor_size, | |
324 | descriptor_version, virtual_map); | |
325 | efi_call_phys_epilog(); | |
326 | return status; | |
327 | } | |
328 | ||
11520e5e LT |
329 | static efi_status_t __init phys_efi_get_time(efi_time_t *tm, |
330 | efi_time_cap_t *tc) | |
331 | { | |
332 | unsigned long flags; | |
333 | efi_status_t status; | |
334 | ||
335 | spin_lock_irqsave(&rtc_lock, flags); | |
336 | efi_call_phys_prelog(); | |
337 | status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm), | |
338 | virt_to_phys(tc)); | |
339 | efi_call_phys_epilog(); | |
340 | spin_unlock_irqrestore(&rtc_lock, flags); | |
341 | return status; | |
342 | } | |
343 | ||
344 | int efi_set_rtc_mmss(unsigned long nowtime) | |
5b83683f HY |
345 | { |
346 | int real_seconds, real_minutes; | |
347 | efi_status_t status; | |
348 | efi_time_t eft; | |
349 | efi_time_cap_t cap; | |
350 | ||
351 | status = efi.get_time(&eft, &cap); | |
352 | if (status != EFI_SUCCESS) { | |
e3cb3f5a | 353 | pr_err("Oops: efitime: can't read time!\n"); |
5b83683f HY |
354 | return -1; |
355 | } | |
356 | ||
357 | real_seconds = nowtime % 60; | |
358 | real_minutes = nowtime / 60; | |
359 | if (((abs(real_minutes - eft.minute) + 15)/30) & 1) | |
360 | real_minutes += 30; | |
361 | real_minutes %= 60; | |
362 | eft.minute = real_minutes; | |
363 | eft.second = real_seconds; | |
364 | ||
365 | status = efi.set_time(&eft); | |
366 | if (status != EFI_SUCCESS) { | |
e3cb3f5a | 367 | pr_err("Oops: efitime: can't write time!\n"); |
5b83683f HY |
368 | return -1; |
369 | } | |
370 | return 0; | |
371 | } | |
372 | ||
11520e5e | 373 | unsigned long efi_get_time(void) |
5b83683f HY |
374 | { |
375 | efi_status_t status; | |
376 | efi_time_t eft; | |
377 | efi_time_cap_t cap; | |
378 | ||
379 | status = efi.get_time(&eft, &cap); | |
380 | if (status != EFI_SUCCESS) | |
e3cb3f5a | 381 | pr_err("Oops: efitime: can't read time!\n"); |
5b83683f HY |
382 | |
383 | return mktime(eft.year, eft.month, eft.day, eft.hour, | |
384 | eft.minute, eft.second); | |
385 | } | |
386 | ||
69c91893 PJ |
387 | /* |
388 | * Tell the kernel about the EFI memory map. This might include | |
389 | * more than the max 128 entries that can fit in the e820 legacy | |
390 | * (zeropage) memory map. | |
391 | */ | |
392 | ||
200001eb | 393 | static void __init do_add_efi_memmap(void) |
69c91893 PJ |
394 | { |
395 | void *p; | |
396 | ||
397 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
398 | efi_memory_desc_t *md = p; | |
399 | unsigned long long start = md->phys_addr; | |
400 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
401 | int e820_type; | |
402 | ||
e2a71476 CW |
403 | switch (md->type) { |
404 | case EFI_LOADER_CODE: | |
405 | case EFI_LOADER_DATA: | |
406 | case EFI_BOOT_SERVICES_CODE: | |
407 | case EFI_BOOT_SERVICES_DATA: | |
408 | case EFI_CONVENTIONAL_MEMORY: | |
409 | if (md->attribute & EFI_MEMORY_WB) | |
410 | e820_type = E820_RAM; | |
411 | else | |
412 | e820_type = E820_RESERVED; | |
413 | break; | |
414 | case EFI_ACPI_RECLAIM_MEMORY: | |
415 | e820_type = E820_ACPI; | |
416 | break; | |
417 | case EFI_ACPI_MEMORY_NVS: | |
418 | e820_type = E820_NVS; | |
419 | break; | |
420 | case EFI_UNUSABLE_MEMORY: | |
421 | e820_type = E820_UNUSABLE; | |
422 | break; | |
423 | default: | |
424 | /* | |
425 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE | |
426 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO | |
427 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE | |
428 | */ | |
69c91893 | 429 | e820_type = E820_RESERVED; |
e2a71476 CW |
430 | break; |
431 | } | |
d0be6bde | 432 | e820_add_region(start, size, e820_type); |
69c91893 PJ |
433 | } |
434 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); | |
435 | } | |
436 | ||
1adbfa35 | 437 | int __init efi_memblock_x86_reserve_range(void) |
ecacf09f HY |
438 | { |
439 | unsigned long pmap; | |
440 | ||
05486fa7 | 441 | #ifdef CONFIG_X86_32 |
1adbfa35 OJ |
442 | /* Can't handle data above 4GB at this time */ |
443 | if (boot_params.efi_info.efi_memmap_hi) { | |
444 | pr_err("Memory map is above 4GB, disabling EFI.\n"); | |
445 | return -EINVAL; | |
446 | } | |
ecacf09f | 447 | pmap = boot_params.efi_info.efi_memmap; |
05486fa7 PJ |
448 | #else |
449 | pmap = (boot_params.efi_info.efi_memmap | | |
450 | ((__u64)boot_params.efi_info.efi_memmap_hi<<32)); | |
ecacf09f HY |
451 | #endif |
452 | memmap.phys_map = (void *)pmap; | |
453 | memmap.nr_map = boot_params.efi_info.efi_memmap_size / | |
454 | boot_params.efi_info.efi_memdesc_size; | |
455 | memmap.desc_version = boot_params.efi_info.efi_memdesc_version; | |
456 | memmap.desc_size = boot_params.efi_info.efi_memdesc_size; | |
24aa0788 | 457 | memblock_reserve(pmap, memmap.nr_map * memmap.desc_size); |
1adbfa35 OJ |
458 | |
459 | return 0; | |
ecacf09f HY |
460 | } |
461 | ||
5b83683f HY |
462 | #if EFI_DEBUG |
463 | static void __init print_efi_memmap(void) | |
464 | { | |
465 | efi_memory_desc_t *md; | |
466 | void *p; | |
467 | int i; | |
468 | ||
469 | for (p = memmap.map, i = 0; | |
470 | p < memmap.map_end; | |
471 | p += memmap.desc_size, i++) { | |
472 | md = p; | |
e3cb3f5a | 473 | pr_info("mem%02u: type=%u, attr=0x%llx, " |
5b83683f HY |
474 | "range=[0x%016llx-0x%016llx) (%lluMB)\n", |
475 | i, md->type, md->attribute, md->phys_addr, | |
476 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), | |
477 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); | |
478 | } | |
479 | } | |
480 | #endif /* EFI_DEBUG */ | |
481 | ||
916f676f MG |
482 | void __init efi_reserve_boot_services(void) |
483 | { | |
484 | void *p; | |
485 | ||
486 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
487 | efi_memory_desc_t *md = p; | |
7d68dc3f ML |
488 | u64 start = md->phys_addr; |
489 | u64 size = md->num_pages << EFI_PAGE_SHIFT; | |
916f676f MG |
490 | |
491 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
492 | md->type != EFI_BOOT_SERVICES_DATA) | |
493 | continue; | |
7d68dc3f ML |
494 | /* Only reserve where possible: |
495 | * - Not within any already allocated areas | |
496 | * - Not over any memory area (really needed, if above?) | |
497 | * - Not within any part of the kernel | |
498 | * - Not the bios reserved area | |
499 | */ | |
fc8d7826 AD |
500 | if ((start+size >= __pa_symbol(_text) |
501 | && start <= __pa_symbol(_end)) || | |
7d68dc3f | 502 | !e820_all_mapped(start, start+size, E820_RAM) || |
bf61549a | 503 | memblock_is_region_reserved(start, size)) { |
7d68dc3f ML |
504 | /* Could not reserve, skip it */ |
505 | md->num_pages = 0; | |
e3cb3f5a | 506 | memblock_dbg("Could not reserve boot range " |
7d68dc3f ML |
507 | "[0x%010llx-0x%010llx]\n", |
508 | start, start+size-1); | |
509 | } else | |
24aa0788 | 510 | memblock_reserve(start, size); |
916f676f MG |
511 | } |
512 | } | |
513 | ||
5189c2a7 | 514 | void __init efi_unmap_memmap(void) |
78510792 | 515 | { |
83e68189 | 516 | clear_bit(EFI_MEMMAP, &x86_efi_facility); |
78510792 JT |
517 | if (memmap.map) { |
518 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); | |
519 | memmap.map = NULL; | |
520 | } | |
521 | } | |
522 | ||
523 | void __init efi_free_boot_services(void) | |
916f676f MG |
524 | { |
525 | void *p; | |
526 | ||
5189c2a7 | 527 | if (!efi_is_native()) |
78510792 JT |
528 | return; |
529 | ||
916f676f MG |
530 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
531 | efi_memory_desc_t *md = p; | |
532 | unsigned long long start = md->phys_addr; | |
533 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
534 | ||
535 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
536 | md->type != EFI_BOOT_SERVICES_DATA) | |
537 | continue; | |
538 | ||
7d68dc3f ML |
539 | /* Could not reserve boot area */ |
540 | if (!size) | |
541 | continue; | |
542 | ||
916f676f MG |
543 | free_bootmem_late(start, size); |
544 | } | |
78510792 JT |
545 | |
546 | efi_unmap_memmap(); | |
916f676f MG |
547 | } |
548 | ||
140bf275 | 549 | static int __init efi_systab_init(void *phys) |
5b83683f | 550 | { |
83e68189 | 551 | if (efi_enabled(EFI_64BIT)) { |
1adbfa35 OJ |
552 | efi_system_table_64_t *systab64; |
553 | u64 tmp = 0; | |
554 | ||
555 | systab64 = early_ioremap((unsigned long)phys, | |
556 | sizeof(*systab64)); | |
557 | if (systab64 == NULL) { | |
558 | pr_err("Couldn't map the system table!\n"); | |
559 | return -ENOMEM; | |
560 | } | |
561 | ||
562 | efi_systab.hdr = systab64->hdr; | |
563 | efi_systab.fw_vendor = systab64->fw_vendor; | |
564 | tmp |= systab64->fw_vendor; | |
565 | efi_systab.fw_revision = systab64->fw_revision; | |
566 | efi_systab.con_in_handle = systab64->con_in_handle; | |
567 | tmp |= systab64->con_in_handle; | |
568 | efi_systab.con_in = systab64->con_in; | |
569 | tmp |= systab64->con_in; | |
570 | efi_systab.con_out_handle = systab64->con_out_handle; | |
571 | tmp |= systab64->con_out_handle; | |
572 | efi_systab.con_out = systab64->con_out; | |
573 | tmp |= systab64->con_out; | |
574 | efi_systab.stderr_handle = systab64->stderr_handle; | |
575 | tmp |= systab64->stderr_handle; | |
576 | efi_systab.stderr = systab64->stderr; | |
577 | tmp |= systab64->stderr; | |
578 | efi_systab.runtime = (void *)(unsigned long)systab64->runtime; | |
579 | tmp |= systab64->runtime; | |
580 | efi_systab.boottime = (void *)(unsigned long)systab64->boottime; | |
581 | tmp |= systab64->boottime; | |
582 | efi_systab.nr_tables = systab64->nr_tables; | |
583 | efi_systab.tables = systab64->tables; | |
584 | tmp |= systab64->tables; | |
585 | ||
586 | early_iounmap(systab64, sizeof(*systab64)); | |
587 | #ifdef CONFIG_X86_32 | |
588 | if (tmp >> 32) { | |
589 | pr_err("EFI data located above 4GB, disabling EFI.\n"); | |
590 | return -EINVAL; | |
591 | } | |
592 | #endif | |
593 | } else { | |
594 | efi_system_table_32_t *systab32; | |
595 | ||
596 | systab32 = early_ioremap((unsigned long)phys, | |
597 | sizeof(*systab32)); | |
598 | if (systab32 == NULL) { | |
599 | pr_err("Couldn't map the system table!\n"); | |
600 | return -ENOMEM; | |
601 | } | |
602 | ||
603 | efi_systab.hdr = systab32->hdr; | |
604 | efi_systab.fw_vendor = systab32->fw_vendor; | |
605 | efi_systab.fw_revision = systab32->fw_revision; | |
606 | efi_systab.con_in_handle = systab32->con_in_handle; | |
607 | efi_systab.con_in = systab32->con_in; | |
608 | efi_systab.con_out_handle = systab32->con_out_handle; | |
609 | efi_systab.con_out = systab32->con_out; | |
610 | efi_systab.stderr_handle = systab32->stderr_handle; | |
611 | efi_systab.stderr = systab32->stderr; | |
612 | efi_systab.runtime = (void *)(unsigned long)systab32->runtime; | |
613 | efi_systab.boottime = (void *)(unsigned long)systab32->boottime; | |
614 | efi_systab.nr_tables = systab32->nr_tables; | |
615 | efi_systab.tables = systab32->tables; | |
616 | ||
617 | early_iounmap(systab32, sizeof(*systab32)); | |
140bf275 | 618 | } |
1adbfa35 | 619 | |
5b83683f HY |
620 | efi.systab = &efi_systab; |
621 | ||
622 | /* | |
623 | * Verify the EFI Table | |
624 | */ | |
140bf275 | 625 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { |
e3cb3f5a | 626 | pr_err("System table signature incorrect!\n"); |
140bf275 OJ |
627 | return -EINVAL; |
628 | } | |
5b83683f | 629 | if ((efi.systab->hdr.revision >> 16) == 0) |
e3cb3f5a | 630 | pr_err("Warning: System table version " |
5b83683f HY |
631 | "%d.%02d, expected 1.00 or greater!\n", |
632 | efi.systab->hdr.revision >> 16, | |
633 | efi.systab->hdr.revision & 0xffff); | |
140bf275 OJ |
634 | |
635 | return 0; | |
83e7ee66 | 636 | } |
5b83683f | 637 | |
140bf275 | 638 | static int __init efi_config_init(u64 tables, int nr_tables) |
83e7ee66 | 639 | { |
1adbfa35 OJ |
640 | void *config_tables, *tablep; |
641 | int i, sz; | |
642 | ||
83e68189 | 643 | if (efi_enabled(EFI_64BIT)) |
1adbfa35 OJ |
644 | sz = sizeof(efi_config_table_64_t); |
645 | else | |
646 | sz = sizeof(efi_config_table_32_t); | |
5b83683f HY |
647 | |
648 | /* | |
649 | * Let's see what config tables the firmware passed to us. | |
650 | */ | |
1adbfa35 | 651 | config_tables = early_ioremap(tables, nr_tables * sz); |
140bf275 | 652 | if (config_tables == NULL) { |
e3cb3f5a | 653 | pr_err("Could not map Configuration table!\n"); |
140bf275 OJ |
654 | return -ENOMEM; |
655 | } | |
5b83683f | 656 | |
1adbfa35 | 657 | tablep = config_tables; |
e3cb3f5a | 658 | pr_info(""); |
5b83683f | 659 | for (i = 0; i < efi.systab->nr_tables; i++) { |
1adbfa35 OJ |
660 | efi_guid_t guid; |
661 | unsigned long table; | |
662 | ||
83e68189 | 663 | if (efi_enabled(EFI_64BIT)) { |
1adbfa35 OJ |
664 | u64 table64; |
665 | guid = ((efi_config_table_64_t *)tablep)->guid; | |
666 | table64 = ((efi_config_table_64_t *)tablep)->table; | |
667 | table = table64; | |
668 | #ifdef CONFIG_X86_32 | |
669 | if (table64 >> 32) { | |
670 | pr_cont("\n"); | |
671 | pr_err("Table located above 4GB, disabling EFI.\n"); | |
672 | early_iounmap(config_tables, | |
673 | efi.systab->nr_tables * sz); | |
674 | return -EINVAL; | |
675 | } | |
676 | #endif | |
677 | } else { | |
678 | guid = ((efi_config_table_32_t *)tablep)->guid; | |
679 | table = ((efi_config_table_32_t *)tablep)->table; | |
680 | } | |
a6a46f41 OJ |
681 | if (!efi_guidcmp(guid, MPS_TABLE_GUID)) { |
682 | efi.mps = table; | |
683 | pr_cont(" MPS=0x%lx ", table); | |
684 | } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) { | |
685 | efi.acpi20 = table; | |
686 | pr_cont(" ACPI 2.0=0x%lx ", table); | |
687 | } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) { | |
688 | efi.acpi = table; | |
689 | pr_cont(" ACPI=0x%lx ", table); | |
690 | } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) { | |
691 | efi.smbios = table; | |
692 | pr_cont(" SMBIOS=0x%lx ", table); | |
03b48632 | 693 | #ifdef CONFIG_X86_UV |
a6a46f41 OJ |
694 | } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) { |
695 | efi.uv_systab = table; | |
696 | pr_cont(" UVsystab=0x%lx ", table); | |
03b48632 | 697 | #endif |
a6a46f41 OJ |
698 | } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) { |
699 | efi.hcdp = table; | |
700 | pr_cont(" HCDP=0x%lx ", table); | |
701 | } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) { | |
702 | efi.uga = table; | |
703 | pr_cont(" UGA=0x%lx ", table); | |
5b83683f | 704 | } |
1adbfa35 | 705 | tablep += sz; |
5b83683f | 706 | } |
e3cb3f5a | 707 | pr_cont("\n"); |
a6a46f41 | 708 | early_iounmap(config_tables, efi.systab->nr_tables * sz); |
140bf275 | 709 | return 0; |
83e7ee66 OJ |
710 | } |
711 | ||
140bf275 | 712 | static int __init efi_runtime_init(void) |
83e7ee66 OJ |
713 | { |
714 | efi_runtime_services_t *runtime; | |
5b83683f HY |
715 | |
716 | /* | |
717 | * Check out the runtime services table. We need to map | |
718 | * the runtime services table so that we can grab the physical | |
719 | * address of several of the EFI runtime functions, needed to | |
720 | * set the firmware into virtual mode. | |
721 | */ | |
beacfaac HY |
722 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
723 | sizeof(efi_runtime_services_t)); | |
140bf275 | 724 | if (!runtime) { |
e3cb3f5a | 725 | pr_err("Could not map the runtime service table!\n"); |
140bf275 OJ |
726 | return -ENOMEM; |
727 | } | |
728 | /* | |
729 | * We will only need *early* access to the following | |
11520e5e | 730 | * two EFI runtime services before set_virtual_address_map |
140bf275 OJ |
731 | * is invoked. |
732 | */ | |
11520e5e | 733 | efi_phys.get_time = (efi_get_time_t *)runtime->get_time; |
140bf275 OJ |
734 | efi_phys.set_virtual_address_map = |
735 | (efi_set_virtual_address_map_t *) | |
736 | runtime->set_virtual_address_map; | |
11520e5e LT |
737 | /* |
738 | * Make efi_get_time can be called before entering | |
739 | * virtual mode. | |
740 | */ | |
741 | efi.get_time = phys_efi_get_time; | |
beacfaac | 742 | early_iounmap(runtime, sizeof(efi_runtime_services_t)); |
140bf275 OJ |
743 | |
744 | return 0; | |
83e7ee66 | 745 | } |
5b83683f | 746 | |
140bf275 | 747 | static int __init efi_memmap_init(void) |
83e7ee66 | 748 | { |
5b83683f | 749 | /* Map the EFI memory map */ |
beacfaac HY |
750 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
751 | memmap.nr_map * memmap.desc_size); | |
140bf275 | 752 | if (memmap.map == NULL) { |
e3cb3f5a | 753 | pr_err("Could not map the memory map!\n"); |
140bf275 OJ |
754 | return -ENOMEM; |
755 | } | |
5b83683f | 756 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); |
175e438f | 757 | |
200001eb PJ |
758 | if (add_efi_memmap) |
759 | do_add_efi_memmap(); | |
140bf275 OJ |
760 | |
761 | return 0; | |
83e7ee66 OJ |
762 | } |
763 | ||
764 | void __init efi_init(void) | |
765 | { | |
766 | efi_char16_t *c16; | |
767 | char vendor[100] = "unknown"; | |
768 | int i = 0; | |
769 | void *tmp; | |
cc5a080c MG |
770 | struct setup_data *data; |
771 | struct efi_var_bootdata *efi_var_data; | |
772 | u64 pa_data; | |
83e7ee66 OJ |
773 | |
774 | #ifdef CONFIG_X86_32 | |
1adbfa35 OJ |
775 | if (boot_params.efi_info.efi_systab_hi || |
776 | boot_params.efi_info.efi_memmap_hi) { | |
777 | pr_info("Table located above 4GB, disabling EFI.\n"); | |
1adbfa35 OJ |
778 | return; |
779 | } | |
83e7ee66 OJ |
780 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
781 | #else | |
782 | efi_phys.systab = (efi_system_table_t *) | |
1adbfa35 OJ |
783 | (boot_params.efi_info.efi_systab | |
784 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); | |
83e7ee66 OJ |
785 | #endif |
786 | ||
83e68189 | 787 | if (efi_systab_init(efi_phys.systab)) |
140bf275 | 788 | return; |
83e68189 | 789 | |
cc5a080c MG |
790 | pa_data = boot_params.hdr.setup_data; |
791 | while (pa_data) { | |
792 | data = early_ioremap(pa_data, sizeof(*efi_var_data)); | |
793 | if (data->type == SETUP_EFI_VARS) { | |
794 | efi_var_data = (struct efi_var_bootdata *)data; | |
795 | ||
796 | efi_var_store_size = efi_var_data->store_size; | |
797 | efi_var_remaining_size = efi_var_data->remaining_size; | |
798 | efi_var_max_var_size = efi_var_data->max_var_size; | |
799 | } | |
800 | pa_data = data->next; | |
801 | early_iounmap(data, sizeof(*efi_var_data)); | |
802 | } | |
803 | ||
31ff2f20 MG |
804 | boot_used_size = efi_var_store_size - efi_var_remaining_size; |
805 | ||
83e68189 | 806 | set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility); |
83e7ee66 OJ |
807 | |
808 | /* | |
809 | * Show what we know for posterity | |
810 | */ | |
811 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); | |
812 | if (c16) { | |
813 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) | |
814 | vendor[i] = *c16++; | |
815 | vendor[i] = '\0'; | |
816 | } else | |
e3cb3f5a | 817 | pr_err("Could not map the firmware vendor!\n"); |
83e7ee66 OJ |
818 | early_iounmap(tmp, 2); |
819 | ||
e3cb3f5a OJ |
820 | pr_info("EFI v%u.%.02u by %s\n", |
821 | efi.systab->hdr.revision >> 16, | |
822 | efi.systab->hdr.revision & 0xffff, vendor); | |
83e7ee66 | 823 | |
83e68189 | 824 | if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) |
140bf275 | 825 | return; |
83e68189 MF |
826 | |
827 | set_bit(EFI_CONFIG_TABLES, &x86_efi_facility); | |
83e7ee66 | 828 | |
1adbfa35 OJ |
829 | /* |
830 | * Note: We currently don't support runtime services on an EFI | |
831 | * that doesn't match the kernel 32/64-bit mode. | |
832 | */ | |
833 | ||
5189c2a7 | 834 | if (!efi_is_native()) |
1adbfa35 | 835 | pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n"); |
83e68189 | 836 | else { |
fb834c7a | 837 | if (disable_runtime || efi_runtime_init()) |
83e68189 MF |
838 | return; |
839 | set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility); | |
140bf275 | 840 | } |
83e7ee66 | 841 | |
83e68189 | 842 | if (efi_memmap_init()) |
140bf275 | 843 | return; |
83e68189 MF |
844 | |
845 | set_bit(EFI_MEMMAP, &x86_efi_facility); | |
846 | ||
11520e5e | 847 | #ifdef CONFIG_X86_32 |
5189c2a7 | 848 | if (efi_is_native()) { |
1adbfa35 OJ |
849 | x86_platform.get_wallclock = efi_get_time; |
850 | x86_platform.set_wallclock = efi_set_rtc_mmss; | |
851 | } | |
11520e5e | 852 | #endif |
7bd867df | 853 | |
5b83683f HY |
854 | #if EFI_DEBUG |
855 | print_efi_memmap(); | |
856 | #endif | |
857 | } | |
858 | ||
2223af38 JT |
859 | void __init efi_late_init(void) |
860 | { | |
861 | efi_bgrt_init(); | |
862 | } | |
863 | ||
9cd2b07c MG |
864 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
865 | { | |
866 | u64 addr, npages; | |
867 | ||
868 | addr = md->virt_addr; | |
869 | npages = md->num_pages; | |
870 | ||
871 | memrange_efi_to_native(&addr, &npages); | |
872 | ||
873 | if (executable) | |
874 | set_memory_x(addr, npages); | |
875 | else | |
876 | set_memory_nx(addr, npages); | |
877 | } | |
878 | ||
a2172e25 HY |
879 | static void __init runtime_code_page_mkexec(void) |
880 | { | |
881 | efi_memory_desc_t *md; | |
a2172e25 HY |
882 | void *p; |
883 | ||
a2172e25 HY |
884 | /* Make EFI runtime service code area executable */ |
885 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
886 | md = p; | |
1c083eb2 HY |
887 | |
888 | if (md->type != EFI_RUNTIME_SERVICES_CODE) | |
889 | continue; | |
890 | ||
9cd2b07c | 891 | efi_set_executable(md, true); |
a2172e25 | 892 | } |
a2172e25 | 893 | } |
a2172e25 | 894 | |
7bc90e01 JT |
895 | /* |
896 | * We can't ioremap data in EFI boot services RAM, because we've already mapped | |
897 | * it as RAM. So, look it up in the existing EFI memory map instead. Only | |
898 | * callable after efi_enter_virtual_mode and before efi_free_boot_services. | |
899 | */ | |
900 | void __iomem *efi_lookup_mapped_addr(u64 phys_addr) | |
901 | { | |
902 | void *p; | |
903 | if (WARN_ON(!memmap.map)) | |
904 | return NULL; | |
905 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
906 | efi_memory_desc_t *md = p; | |
907 | u64 size = md->num_pages << EFI_PAGE_SHIFT; | |
908 | u64 end = md->phys_addr + size; | |
909 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && | |
910 | md->type != EFI_BOOT_SERVICES_CODE && | |
911 | md->type != EFI_BOOT_SERVICES_DATA) | |
912 | continue; | |
913 | if (!md->virt_addr) | |
914 | continue; | |
915 | if (phys_addr >= md->phys_addr && phys_addr < end) { | |
916 | phys_addr += md->virt_addr - md->phys_addr; | |
917 | return (__force void __iomem *)(unsigned long)phys_addr; | |
918 | } | |
919 | } | |
920 | return NULL; | |
921 | } | |
922 | ||
3e8fa263 MF |
923 | void efi_memory_uc(u64 addr, unsigned long size) |
924 | { | |
925 | unsigned long page_shift = 1UL << EFI_PAGE_SHIFT; | |
926 | u64 npages; | |
927 | ||
928 | npages = round_up(size, page_shift) / page_shift; | |
929 | memrange_efi_to_native(&addr, &npages); | |
930 | set_memory_uc(addr, npages); | |
931 | } | |
932 | ||
5b83683f HY |
933 | /* |
934 | * This function will switch the EFI runtime services to virtual mode. | |
935 | * Essentially, look through the EFI memmap and map every region that | |
936 | * has the runtime attribute bit set in its memory descriptor and update | |
937 | * that memory descriptor with the virtual address obtained from ioremap(). | |
938 | * This enables the runtime services to be called without having to | |
939 | * thunk back into physical mode for every invocation. | |
940 | */ | |
941 | void __init efi_enter_virtual_mode(void) | |
942 | { | |
202f9d0a | 943 | efi_memory_desc_t *md, *prev_md = NULL; |
5b83683f | 944 | efi_status_t status; |
1c083eb2 | 945 | unsigned long size; |
dda56e13 | 946 | u64 end, systab, start_pfn, end_pfn; |
7cb00b72 MG |
947 | void *p, *va, *new_memmap = NULL; |
948 | int count = 0; | |
5b83683f HY |
949 | |
950 | efi.systab = NULL; | |
202f9d0a | 951 | |
1adbfa35 OJ |
952 | /* |
953 | * We don't do virtual mode, since we don't do runtime services, on | |
954 | * non-native EFI | |
955 | */ | |
956 | ||
5189c2a7 | 957 | if (!efi_is_native()) { |
78510792 JT |
958 | efi_unmap_memmap(); |
959 | return; | |
960 | } | |
1adbfa35 | 961 | |
202f9d0a MG |
962 | /* Merge contiguous regions of the same type and attribute */ |
963 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
964 | u64 prev_size; | |
965 | md = p; | |
966 | ||
967 | if (!prev_md) { | |
968 | prev_md = md; | |
969 | continue; | |
970 | } | |
971 | ||
972 | if (prev_md->type != md->type || | |
973 | prev_md->attribute != md->attribute) { | |
974 | prev_md = md; | |
975 | continue; | |
976 | } | |
977 | ||
978 | prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; | |
979 | ||
980 | if (md->phys_addr == (prev_md->phys_addr + prev_size)) { | |
981 | prev_md->num_pages += md->num_pages; | |
982 | md->type = EFI_RESERVED_TYPE; | |
983 | md->attribute = 0; | |
984 | continue; | |
985 | } | |
986 | prev_md = md; | |
987 | } | |
988 | ||
5b83683f HY |
989 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
990 | md = p; | |
916f676f MG |
991 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
992 | md->type != EFI_BOOT_SERVICES_CODE && | |
993 | md->type != EFI_BOOT_SERVICES_DATA) | |
5b83683f | 994 | continue; |
1c083eb2 HY |
995 | |
996 | size = md->num_pages << EFI_PAGE_SHIFT; | |
997 | end = md->phys_addr + size; | |
998 | ||
dda56e13 | 999 | start_pfn = PFN_DOWN(md->phys_addr); |
dd39ecf5 | 1000 | end_pfn = PFN_UP(end); |
dda56e13 | 1001 | if (pfn_range_is_mapped(start_pfn, end_pfn)) { |
1c083eb2 | 1002 | va = __va(md->phys_addr); |
3e8fa263 MF |
1003 | |
1004 | if (!(md->attribute & EFI_MEMORY_WB)) | |
1005 | efi_memory_uc((u64)(unsigned long)va, size); | |
1006 | } else | |
1007 | va = efi_ioremap(md->phys_addr, size, | |
1008 | md->type, md->attribute); | |
1c083eb2 | 1009 | |
1c083eb2 HY |
1010 | md->virt_addr = (u64) (unsigned long) va; |
1011 | ||
1012 | if (!va) { | |
e3cb3f5a | 1013 | pr_err("ioremap of 0x%llX failed!\n", |
5b83683f | 1014 | (unsigned long long)md->phys_addr); |
1c083eb2 HY |
1015 | continue; |
1016 | } | |
1017 | ||
1018 | systab = (u64) (unsigned long) efi_phys.systab; | |
1019 | if (md->phys_addr <= systab && systab < end) { | |
1020 | systab += md->virt_addr - md->phys_addr; | |
1021 | efi.systab = (efi_system_table_t *) (unsigned long) systab; | |
1022 | } | |
7cb00b72 MG |
1023 | new_memmap = krealloc(new_memmap, |
1024 | (count + 1) * memmap.desc_size, | |
1025 | GFP_KERNEL); | |
1026 | memcpy(new_memmap + (count * memmap.desc_size), md, | |
1027 | memmap.desc_size); | |
1028 | count++; | |
5b83683f HY |
1029 | } |
1030 | ||
1031 | BUG_ON(!efi.systab); | |
1032 | ||
1033 | status = phys_efi_set_virtual_address_map( | |
7cb00b72 | 1034 | memmap.desc_size * count, |
5b83683f HY |
1035 | memmap.desc_size, |
1036 | memmap.desc_version, | |
7cb00b72 | 1037 | (efi_memory_desc_t *)__pa(new_memmap)); |
5b83683f HY |
1038 | |
1039 | if (status != EFI_SUCCESS) { | |
e3cb3f5a OJ |
1040 | pr_alert("Unable to switch EFI into virtual mode " |
1041 | "(status=%lx)!\n", status); | |
5b83683f HY |
1042 | panic("EFI call to SetVirtualAddressMap() failed!"); |
1043 | } | |
1044 | ||
1045 | /* | |
1046 | * Now that EFI is in virtual mode, update the function | |
1047 | * pointers in the runtime service table to the new virtual addresses. | |
1048 | * | |
1049 | * Call EFI services through wrapper functions. | |
1050 | */ | |
712ba9e9 | 1051 | efi.runtime_version = efi_systab.hdr.revision; |
5b83683f HY |
1052 | efi.get_time = virt_efi_get_time; |
1053 | efi.set_time = virt_efi_set_time; | |
1054 | efi.get_wakeup_time = virt_efi_get_wakeup_time; | |
1055 | efi.set_wakeup_time = virt_efi_set_wakeup_time; | |
1056 | efi.get_variable = virt_efi_get_variable; | |
1057 | efi.get_next_variable = virt_efi_get_next_variable; | |
1058 | efi.set_variable = virt_efi_set_variable; | |
1059 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; | |
1060 | efi.reset_system = virt_efi_reset_system; | |
2b5e8ef3 | 1061 | efi.set_virtual_address_map = NULL; |
3b370237 MG |
1062 | efi.query_variable_info = virt_efi_query_variable_info; |
1063 | efi.update_capsule = virt_efi_update_capsule; | |
1064 | efi.query_capsule_caps = virt_efi_query_capsule_caps; | |
4de0d4a6 HY |
1065 | if (__supported_pte_mask & _PAGE_NX) |
1066 | runtime_code_page_mkexec(); | |
1adbfa35 | 1067 | |
7cb00b72 | 1068 | kfree(new_memmap); |
5b83683f HY |
1069 | } |
1070 | ||
1071 | /* | |
1072 | * Convenience functions to obtain memory types and attributes | |
1073 | */ | |
1074 | u32 efi_mem_type(unsigned long phys_addr) | |
1075 | { | |
1076 | efi_memory_desc_t *md; | |
1077 | void *p; | |
1078 | ||
83e68189 MF |
1079 | if (!efi_enabled(EFI_MEMMAP)) |
1080 | return 0; | |
1081 | ||
5b83683f HY |
1082 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
1083 | md = p; | |
1084 | if ((md->phys_addr <= phys_addr) && | |
1085 | (phys_addr < (md->phys_addr + | |
1086 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
1087 | return md->type; | |
1088 | } | |
1089 | return 0; | |
1090 | } | |
1091 | ||
1092 | u64 efi_mem_attributes(unsigned long phys_addr) | |
1093 | { | |
1094 | efi_memory_desc_t *md; | |
1095 | void *p; | |
1096 | ||
1097 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
1098 | md = p; | |
1099 | if ((md->phys_addr <= phys_addr) && | |
1100 | (phys_addr < (md->phys_addr + | |
1101 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
1102 | return md->attribute; | |
1103 | } | |
1104 | return 0; | |
1105 | } | |
a6e4d5a0 MF |
1106 | |
1107 | /* | |
1108 | * Some firmware has serious problems when using more than 50% of the EFI | |
1109 | * variable store, i.e. it triggers bugs that can brick machines. Ensure that | |
1110 | * we never use more than this safe limit. | |
1111 | * | |
1112 | * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable | |
1113 | * store. | |
1114 | */ | |
1115 | efi_status_t efi_query_variable_store(u32 attributes, unsigned long size) | |
1116 | { | |
1117 | efi_status_t status; | |
1118 | u64 storage_size, remaining_size, max_size; | |
1119 | ||
1120 | status = efi.query_variable_info(attributes, &storage_size, | |
1121 | &remaining_size, &max_size); | |
1122 | if (status != EFI_SUCCESS) | |
1123 | return status; | |
1124 | ||
7791c842 RW |
1125 | if (!max_size && remaining_size > size) |
1126 | printk_once(KERN_ERR FW_BUG "Broken EFI implementation" | |
1127 | " is returning MaxVariableSize=0\n"); | |
31ff2f20 MG |
1128 | /* |
1129 | * Some firmware implementations refuse to boot if there's insufficient | |
1130 | * space in the variable store. We account for that by refusing the | |
1131 | * write if permitting it would reduce the available space to under | |
1132 | * 50%. However, some firmware won't reclaim variable space until | |
1133 | * after the used (not merely the actively used) space drops below | |
1134 | * a threshold. We can approximate that case with the value calculated | |
1135 | * above. If both the firmware and our calculations indicate that the | |
1136 | * available space would drop below 50%, refuse the write. | |
1137 | */ | |
7791c842 RW |
1138 | |
1139 | if (!storage_size || size > remaining_size || | |
1140 | (max_size && size > max_size) || | |
31ff2f20 MG |
1141 | ((active_size + size + VAR_METADATA_SIZE > storage_size / 2) && |
1142 | (remaining_size - size < storage_size / 2))) | |
a6e4d5a0 MF |
1143 | return EFI_OUT_OF_RESOURCES; |
1144 | ||
1145 | return EFI_SUCCESS; | |
1146 | } | |
3668011d | 1147 | EXPORT_SYMBOL_GPL(efi_query_variable_store); |