blob: e4a86a677ce163ec5f911fb14db65590d813aee1 [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
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
Olof Johanssone3cb3f52012-02-12 13:24:26 -080029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Huang, Ying5b836832008-01-30 13:31:19 +010031#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/efi.h>
Josh Triplett2223af32012-09-28 17:57:05 -070034#include <linux/efi-bgrt.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040035#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010036#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070037#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010038#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>
Matthew Garrett31ff2f22013-04-15 13:09:47 -070044#include <linux/ucs2_string.h>
Huang, Ying5b836832008-01-30 13:31:19 +010045
46#include <asm/setup.h>
47#include <asm/efi.h>
48#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010049#include <asm/cacheflush.h>
50#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080051#include <asm/x86_init.h>
Huang, Ying5b836832008-01-30 13:31:19 +010052
53#define EFI_DEBUG 1
Huang, Ying5b836832008-01-30 13:31:19 +010054
Matthew Garrett31ff2f22013-04-15 13:09:47 -070055/*
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
Jan Beulichd80603c2011-07-05 12:22:18 +010062struct 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};
Huang, Ying5b836832008-01-30 13:31:19 +010073EXPORT_SYMBOL(efi);
74
75struct efi_memory_map memmap;
76
Harvey Harrisonecaea422008-02-13 13:26:13 -080077static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010078static efi_system_table_t efi_systab __initdata;
79
Matthew Garrettcc5a0802013-04-15 13:09:46 -070080static u64 efi_var_store_size;
81static u64 efi_var_remaining_size;
82static u64 efi_var_max_var_size;
Matthew Garrett31ff2f22013-04-15 13:09:47 -070083static u64 boot_used_size;
84static u64 boot_var_size;
85static u64 active_size;
Matthew Garrettcc5a0802013-04-15 13:09:46 -070086
Matt Fleming83e68182012-11-14 09:42:35 +000087unsigned long x86_efi_facility;
88
89/*
90 * Returns 1 if 'facility' is enabled, 0 otherwise.
91 */
92int efi_enabled(int facility)
93{
94 return test_bit(facility, &x86_efi_facility) != 0;
95}
96EXPORT_SYMBOL(efi_enabled);
97
Matt Fleming058e7b52013-02-22 15:03:47 +000098static bool __initdata disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010099static int __init setup_noefi(char *arg)
100{
Matt Flemingfb834c72013-02-20 20:36:12 +0000101 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +0100102 return 0;
103}
104early_param("noefi", setup_noefi);
105
Paul Jackson200001e2008-06-25 05:44:46 -0700106int add_efi_memmap;
107EXPORT_SYMBOL(add_efi_memmap);
108
109static int __init setup_add_efi_memmap(char *arg)
110{
111 add_efi_memmap = 1;
112 return 0;
113}
114early_param("add_efi_memmap", setup_add_efi_memmap);
115
Richard Weinberger8c58bf32013-04-17 01:00:53 +0200116static bool efi_no_storage_paranoia;
117
118static int __init setup_storage_paranoia(char *arg)
119{
120 efi_no_storage_paranoia = true;
121 return 0;
122}
123early_param("efi_no_storage_paranoia", setup_storage_paranoia);
124
Paul Jackson200001e2008-06-25 05:44:46 -0700125
Huang, Ying5b836832008-01-30 13:31:19 +0100126static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
127{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100128 unsigned long flags;
129 efi_status_t status;
130
131 spin_lock_irqsave(&rtc_lock, flags);
132 status = efi_call_virt2(get_time, tm, tc);
133 spin_unlock_irqrestore(&rtc_lock, flags);
134 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100135}
136
137static efi_status_t virt_efi_set_time(efi_time_t *tm)
138{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100139 unsigned long flags;
140 efi_status_t status;
141
142 spin_lock_irqsave(&rtc_lock, flags);
143 status = efi_call_virt1(set_time, tm);
144 spin_unlock_irqrestore(&rtc_lock, flags);
145 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100146}
147
148static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
149 efi_bool_t *pending,
150 efi_time_t *tm)
151{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100152 unsigned long flags;
153 efi_status_t status;
154
155 spin_lock_irqsave(&rtc_lock, flags);
156 status = efi_call_virt3(get_wakeup_time,
157 enabled, pending, tm);
158 spin_unlock_irqrestore(&rtc_lock, flags);
159 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100160}
161
162static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
163{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100164 unsigned long flags;
165 efi_status_t status;
166
167 spin_lock_irqsave(&rtc_lock, flags);
168 status = efi_call_virt2(set_wakeup_time,
169 enabled, tm);
170 spin_unlock_irqrestore(&rtc_lock, flags);
171 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100172}
173
174static efi_status_t virt_efi_get_variable(efi_char16_t *name,
175 efi_guid_t *vendor,
176 u32 *attr,
177 unsigned long *data_size,
178 void *data)
179{
180 return efi_call_virt5(get_variable,
181 name, vendor, attr,
182 data_size, data);
183}
184
185static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
186 efi_char16_t *name,
187 efi_guid_t *vendor)
188{
Matthew Garrett31ff2f22013-04-15 13:09:47 -0700189 efi_status_t status;
190 static bool finished = false;
191 static u64 var_size;
192
193 status = efi_call_virt3(get_next_variable,
194 name_size, name, vendor);
195
196 if (status == EFI_NOT_FOUND) {
197 finished = true;
198 if (var_size < boot_used_size) {
199 boot_var_size = boot_used_size - var_size;
200 active_size += boot_var_size;
201 } else {
202 printk(KERN_WARNING FW_BUG "efi: Inconsistent initial sizes\n");
203 }
204 }
205
206 if (boot_used_size && !finished) {
207 unsigned long size;
208 u32 attr;
209 efi_status_t s;
210 void *tmp;
211
212 s = virt_efi_get_variable(name, vendor, &attr, &size, NULL);
213
214 if (s != EFI_BUFFER_TOO_SMALL || !size)
215 return status;
216
217 tmp = kmalloc(size, GFP_ATOMIC);
218
219 if (!tmp)
220 return status;
221
222 s = virt_efi_get_variable(name, vendor, &attr, &size, tmp);
223
224 if (s == EFI_SUCCESS && (attr & EFI_VARIABLE_NON_VOLATILE)) {
225 var_size += size;
226 var_size += ucs2_strsize(name, 1024);
227 active_size += size;
228 active_size += VAR_METADATA_SIZE;
229 active_size += ucs2_strsize(name, 1024);
230 }
231
232 kfree(tmp);
233 }
234
235 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100236}
237
238static efi_status_t virt_efi_set_variable(efi_char16_t *name,
239 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400240 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100241 unsigned long data_size,
242 void *data)
243{
Matthew Garrett31ff2f22013-04-15 13:09:47 -0700244 efi_status_t status;
245 u32 orig_attr = 0;
246 unsigned long orig_size = 0;
247
248 status = virt_efi_get_variable(name, vendor, &orig_attr, &orig_size,
249 NULL);
250
251 if (status != EFI_BUFFER_TOO_SMALL)
252 orig_size = 0;
253
254 status = efi_call_virt5(set_variable,
255 name, vendor, attr,
256 data_size, data);
257
258 if (status == EFI_SUCCESS) {
259 if (orig_size) {
260 active_size -= orig_size;
261 active_size -= ucs2_strsize(name, 1024);
262 active_size -= VAR_METADATA_SIZE;
263 }
264 if (data_size) {
265 active_size += data_size;
266 active_size += ucs2_strsize(name, 1024);
267 active_size += VAR_METADATA_SIZE;
268 }
269 }
270
271 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100272}
273
Matthew Garrett3b370232011-06-06 15:36:25 -0400274static efi_status_t virt_efi_query_variable_info(u32 attr,
275 u64 *storage_space,
276 u64 *remaining_space,
277 u64 *max_variable_size)
278{
279 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
280 return EFI_UNSUPPORTED;
281
282 return efi_call_virt4(query_variable_info, attr, storage_space,
283 remaining_space, max_variable_size);
284}
285
Huang, Ying5b836832008-01-30 13:31:19 +0100286static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
287{
288 return efi_call_virt1(get_next_high_mono_count, count);
289}
290
291static void virt_efi_reset_system(int reset_type,
292 efi_status_t status,
293 unsigned long data_size,
294 efi_char16_t *data)
295{
296 efi_call_virt4(reset_system, reset_type, status,
297 data_size, data);
298}
299
Matthew Garrett3b370232011-06-06 15:36:25 -0400300static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
301 unsigned long count,
302 unsigned long sg_list)
303{
304 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
305 return EFI_UNSUPPORTED;
306
307 return efi_call_virt3(update_capsule, capsules, count, sg_list);
308}
309
310static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
311 unsigned long count,
312 u64 *max_size,
313 int *reset_type)
314{
315 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
316 return EFI_UNSUPPORTED;
317
318 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
319 reset_type);
320}
321
Huang, Ying5b836832008-01-30 13:31:19 +0100322static efi_status_t __init phys_efi_set_virtual_address_map(
323 unsigned long memory_map_size,
324 unsigned long descriptor_size,
325 u32 descriptor_version,
326 efi_memory_desc_t *virtual_map)
327{
328 efi_status_t status;
329
330 efi_call_phys_prelog();
331 status = efi_call_phys4(efi_phys.set_virtual_address_map,
332 memory_map_size, descriptor_size,
333 descriptor_version, virtual_map);
334 efi_call_phys_epilog();
335 return status;
336}
337
Linus Torvalds11520e52012-12-15 15:15:24 -0800338static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
339 efi_time_cap_t *tc)
340{
341 unsigned long flags;
342 efi_status_t status;
343
344 spin_lock_irqsave(&rtc_lock, flags);
345 efi_call_phys_prelog();
346 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
347 virt_to_phys(tc));
348 efi_call_phys_epilog();
349 spin_unlock_irqrestore(&rtc_lock, flags);
350 return status;
351}
352
353int efi_set_rtc_mmss(unsigned long nowtime)
Huang, Ying5b836832008-01-30 13:31:19 +0100354{
355 int real_seconds, real_minutes;
356 efi_status_t status;
357 efi_time_t eft;
358 efi_time_cap_t cap;
359
360 status = efi.get_time(&eft, &cap);
361 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800362 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100363 return -1;
364 }
365
366 real_seconds = nowtime % 60;
367 real_minutes = nowtime / 60;
368 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
369 real_minutes += 30;
370 real_minutes %= 60;
371 eft.minute = real_minutes;
372 eft.second = real_seconds;
373
374 status = efi.set_time(&eft);
375 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800376 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100377 return -1;
378 }
379 return 0;
380}
381
Linus Torvalds11520e52012-12-15 15:15:24 -0800382unsigned long efi_get_time(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100383{
384 efi_status_t status;
385 efi_time_t eft;
386 efi_time_cap_t cap;
387
388 status = efi.get_time(&eft, &cap);
389 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800390 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100391
392 return mktime(eft.year, eft.month, eft.day, eft.hour,
393 eft.minute, eft.second);
394}
395
Paul Jackson69c91892008-05-14 08:15:58 -0700396/*
397 * Tell the kernel about the EFI memory map. This might include
398 * more than the max 128 entries that can fit in the e820 legacy
399 * (zeropage) memory map.
400 */
401
Paul Jackson200001e2008-06-25 05:44:46 -0700402static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700403{
404 void *p;
405
406 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
407 efi_memory_desc_t *md = p;
408 unsigned long long start = md->phys_addr;
409 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
410 int e820_type;
411
Cliff Wickmane2a71472009-06-16 16:43:40 -0500412 switch (md->type) {
413 case EFI_LOADER_CODE:
414 case EFI_LOADER_DATA:
415 case EFI_BOOT_SERVICES_CODE:
416 case EFI_BOOT_SERVICES_DATA:
417 case EFI_CONVENTIONAL_MEMORY:
418 if (md->attribute & EFI_MEMORY_WB)
419 e820_type = E820_RAM;
420 else
421 e820_type = E820_RESERVED;
422 break;
423 case EFI_ACPI_RECLAIM_MEMORY:
424 e820_type = E820_ACPI;
425 break;
426 case EFI_ACPI_MEMORY_NVS:
427 e820_type = E820_NVS;
428 break;
429 case EFI_UNUSABLE_MEMORY:
430 e820_type = E820_UNUSABLE;
431 break;
432 default:
433 /*
434 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
435 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
436 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
437 */
Paul Jackson69c91892008-05-14 08:15:58 -0700438 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500439 break;
440 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700441 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700442 }
443 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
444}
445
Olof Johansson1adbfa32012-02-12 13:24:29 -0800446int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800447{
448 unsigned long pmap;
449
Paul Jackson05486fa2008-06-22 07:22:02 -0700450#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800451 /* Can't handle data above 4GB at this time */
452 if (boot_params.efi_info.efi_memmap_hi) {
453 pr_err("Memory map is above 4GB, disabling EFI.\n");
454 return -EINVAL;
455 }
Huang, Yingecacf092008-06-02 14:26:21 +0800456 pmap = boot_params.efi_info.efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700457#else
458 pmap = (boot_params.efi_info.efi_memmap |
459 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
Huang, Yingecacf092008-06-02 14:26:21 +0800460#endif
461 memmap.phys_map = (void *)pmap;
462 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
463 boot_params.efi_info.efi_memdesc_size;
464 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
465 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
Tejun Heo24aa0782011-07-12 11:16:06 +0200466 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800467
468 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800469}
470
Huang, Ying5b836832008-01-30 13:31:19 +0100471#if EFI_DEBUG
472static void __init print_efi_memmap(void)
473{
474 efi_memory_desc_t *md;
475 void *p;
476 int i;
477
478 for (p = memmap.map, i = 0;
479 p < memmap.map_end;
480 p += memmap.desc_size, i++) {
481 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800482 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100483 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
484 i, md->type, md->attribute, md->phys_addr,
485 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
486 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
487 }
488}
489#endif /* EFI_DEBUG */
490
Matthew Garrett916f6762011-05-25 09:53:13 -0400491void __init efi_reserve_boot_services(void)
492{
493 void *p;
494
495 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
496 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200497 u64 start = md->phys_addr;
498 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400499
500 if (md->type != EFI_BOOT_SERVICES_CODE &&
501 md->type != EFI_BOOT_SERVICES_DATA)
502 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200503 /* Only reserve where possible:
504 * - Not within any already allocated areas
505 * - Not over any memory area (really needed, if above?)
506 * - Not within any part of the kernel
507 * - Not the bios reserved area
508 */
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800509 if ((start+size >= __pa_symbol(_text)
510 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200511 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200512 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200513 /* Could not reserve, skip it */
514 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800515 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200516 "[0x%010llx-0x%010llx]\n",
517 start, start+size-1);
518 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200519 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400520 }
521}
522
Olof Johansson5189c2a2012-10-24 10:00:44 -0700523void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700524{
Matt Fleming83e68182012-11-14 09:42:35 +0000525 clear_bit(EFI_MEMMAP, &x86_efi_facility);
Josh Triplett78510792012-09-28 17:55:44 -0700526 if (memmap.map) {
527 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
528 memmap.map = NULL;
529 }
530}
531
532void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400533{
534 void *p;
535
Olof Johansson5189c2a2012-10-24 10:00:44 -0700536 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700537 return;
538
Matthew Garrett916f6762011-05-25 09:53:13 -0400539 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
540 efi_memory_desc_t *md = p;
541 unsigned long long start = md->phys_addr;
542 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
543
544 if (md->type != EFI_BOOT_SERVICES_CODE &&
545 md->type != EFI_BOOT_SERVICES_DATA)
546 continue;
547
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200548 /* Could not reserve boot area */
549 if (!size)
550 continue;
551
Matthew Garrett916f6762011-05-25 09:53:13 -0400552 free_bootmem_late(start, size);
553 }
Josh Triplett78510792012-09-28 17:55:44 -0700554
555 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400556}
557
Olof Johansson140bf272012-02-12 13:24:28 -0800558static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100559{
Matt Fleming83e68182012-11-14 09:42:35 +0000560 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800561 efi_system_table_64_t *systab64;
562 u64 tmp = 0;
563
564 systab64 = early_ioremap((unsigned long)phys,
565 sizeof(*systab64));
566 if (systab64 == NULL) {
567 pr_err("Couldn't map the system table!\n");
568 return -ENOMEM;
569 }
570
571 efi_systab.hdr = systab64->hdr;
572 efi_systab.fw_vendor = systab64->fw_vendor;
573 tmp |= systab64->fw_vendor;
574 efi_systab.fw_revision = systab64->fw_revision;
575 efi_systab.con_in_handle = systab64->con_in_handle;
576 tmp |= systab64->con_in_handle;
577 efi_systab.con_in = systab64->con_in;
578 tmp |= systab64->con_in;
579 efi_systab.con_out_handle = systab64->con_out_handle;
580 tmp |= systab64->con_out_handle;
581 efi_systab.con_out = systab64->con_out;
582 tmp |= systab64->con_out;
583 efi_systab.stderr_handle = systab64->stderr_handle;
584 tmp |= systab64->stderr_handle;
585 efi_systab.stderr = systab64->stderr;
586 tmp |= systab64->stderr;
587 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
588 tmp |= systab64->runtime;
589 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
590 tmp |= systab64->boottime;
591 efi_systab.nr_tables = systab64->nr_tables;
592 efi_systab.tables = systab64->tables;
593 tmp |= systab64->tables;
594
595 early_iounmap(systab64, sizeof(*systab64));
596#ifdef CONFIG_X86_32
597 if (tmp >> 32) {
598 pr_err("EFI data located above 4GB, disabling EFI.\n");
599 return -EINVAL;
600 }
601#endif
602 } else {
603 efi_system_table_32_t *systab32;
604
605 systab32 = early_ioremap((unsigned long)phys,
606 sizeof(*systab32));
607 if (systab32 == NULL) {
608 pr_err("Couldn't map the system table!\n");
609 return -ENOMEM;
610 }
611
612 efi_systab.hdr = systab32->hdr;
613 efi_systab.fw_vendor = systab32->fw_vendor;
614 efi_systab.fw_revision = systab32->fw_revision;
615 efi_systab.con_in_handle = systab32->con_in_handle;
616 efi_systab.con_in = systab32->con_in;
617 efi_systab.con_out_handle = systab32->con_out_handle;
618 efi_systab.con_out = systab32->con_out;
619 efi_systab.stderr_handle = systab32->stderr_handle;
620 efi_systab.stderr = systab32->stderr;
621 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
622 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
623 efi_systab.nr_tables = systab32->nr_tables;
624 efi_systab.tables = systab32->tables;
625
626 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800627 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800628
Huang, Ying5b836832008-01-30 13:31:19 +0100629 efi.systab = &efi_systab;
630
631 /*
632 * Verify the EFI Table
633 */
Olof Johansson140bf272012-02-12 13:24:28 -0800634 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800635 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800636 return -EINVAL;
637 }
Huang, Ying5b836832008-01-30 13:31:19 +0100638 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800639 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100640 "%d.%02d, expected 1.00 or greater!\n",
641 efi.systab->hdr.revision >> 16,
642 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800643
644 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800645}
Huang, Ying5b836832008-01-30 13:31:19 +0100646
Olof Johansson140bf272012-02-12 13:24:28 -0800647static int __init efi_config_init(u64 tables, int nr_tables)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800648{
Olof Johansson1adbfa32012-02-12 13:24:29 -0800649 void *config_tables, *tablep;
650 int i, sz;
651
Matt Fleming83e68182012-11-14 09:42:35 +0000652 if (efi_enabled(EFI_64BIT))
Olof Johansson1adbfa32012-02-12 13:24:29 -0800653 sz = sizeof(efi_config_table_64_t);
654 else
655 sz = sizeof(efi_config_table_32_t);
Huang, Ying5b836832008-01-30 13:31:19 +0100656
657 /*
658 * Let's see what config tables the firmware passed to us.
659 */
Olof Johansson1adbfa32012-02-12 13:24:29 -0800660 config_tables = early_ioremap(tables, nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800661 if (config_tables == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800662 pr_err("Could not map Configuration table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800663 return -ENOMEM;
664 }
Huang, Ying5b836832008-01-30 13:31:19 +0100665
Olof Johansson1adbfa32012-02-12 13:24:29 -0800666 tablep = config_tables;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800667 pr_info("");
Huang, Ying5b836832008-01-30 13:31:19 +0100668 for (i = 0; i < efi.systab->nr_tables; i++) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800669 efi_guid_t guid;
670 unsigned long table;
Olof Johanssona6a46f42012-02-12 13:24:27 -0800671
Matt Fleming83e68182012-11-14 09:42:35 +0000672 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800673 u64 table64;
674 guid = ((efi_config_table_64_t *)tablep)->guid;
675 table64 = ((efi_config_table_64_t *)tablep)->table;
676 table = table64;
677#ifdef CONFIG_X86_32
678 if (table64 >> 32) {
679 pr_cont("\n");
680 pr_err("Table located above 4GB, disabling EFI.\n");
681 early_iounmap(config_tables,
682 efi.systab->nr_tables * sz);
683 return -EINVAL;
684 }
685#endif
686 } else {
687 guid = ((efi_config_table_32_t *)tablep)->guid;
688 table = ((efi_config_table_32_t *)tablep)->table;
689 }
Olof Johanssona6a46f42012-02-12 13:24:27 -0800690 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
691 efi.mps = table;
692 pr_cont(" MPS=0x%lx ", table);
693 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
694 efi.acpi20 = table;
695 pr_cont(" ACPI 2.0=0x%lx ", table);
696 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
697 efi.acpi = table;
698 pr_cont(" ACPI=0x%lx ", table);
699 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
700 efi.smbios = table;
701 pr_cont(" SMBIOS=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100702#ifdef CONFIG_X86_UV
Olof Johanssona6a46f42012-02-12 13:24:27 -0800703 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
704 efi.uv_systab = table;
705 pr_cont(" UVsystab=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100706#endif
Olof Johanssona6a46f42012-02-12 13:24:27 -0800707 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
708 efi.hcdp = table;
709 pr_cont(" HCDP=0x%lx ", table);
710 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
711 efi.uga = table;
712 pr_cont(" UGA=0x%lx ", table);
Huang, Ying5b836832008-01-30 13:31:19 +0100713 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800714 tablep += sz;
Huang, Ying5b836832008-01-30 13:31:19 +0100715 }
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800716 pr_cont("\n");
Olof Johanssona6a46f42012-02-12 13:24:27 -0800717 early_iounmap(config_tables, efi.systab->nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800718 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800719}
720
Olof Johansson140bf272012-02-12 13:24:28 -0800721static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800722{
723 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100724
725 /*
726 * Check out the runtime services table. We need to map
727 * the runtime services table so that we can grab the physical
728 * address of several of the EFI runtime functions, needed to
729 * set the firmware into virtual mode.
730 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100731 runtime = early_ioremap((unsigned long)efi.systab->runtime,
732 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800733 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800734 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800735 return -ENOMEM;
736 }
737 /*
738 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800739 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800740 * is invoked.
741 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800742 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800743 efi_phys.set_virtual_address_map =
744 (efi_set_virtual_address_map_t *)
745 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800746 /*
747 * Make efi_get_time can be called before entering
748 * virtual mode.
749 */
750 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100751 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800752
753 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800754}
Huang, Ying5b836832008-01-30 13:31:19 +0100755
Olof Johansson140bf272012-02-12 13:24:28 -0800756static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800757{
Huang, Ying5b836832008-01-30 13:31:19 +0100758 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100759 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
760 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800761 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800762 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800763 return -ENOMEM;
764 }
Huang, Ying5b836832008-01-30 13:31:19 +0100765 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500766
Paul Jackson200001e2008-06-25 05:44:46 -0700767 if (add_efi_memmap)
768 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800769
770 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800771}
772
773void __init efi_init(void)
774{
775 efi_char16_t *c16;
776 char vendor[100] = "unknown";
777 int i = 0;
778 void *tmp;
Matthew Garrettcc5a0802013-04-15 13:09:46 -0700779 struct setup_data *data;
780 struct efi_var_bootdata *efi_var_data;
781 u64 pa_data;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800782
783#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800784 if (boot_params.efi_info.efi_systab_hi ||
785 boot_params.efi_info.efi_memmap_hi) {
786 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800787 return;
788 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800789 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
790#else
791 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800792 (boot_params.efi_info.efi_systab |
793 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800794#endif
795
Matt Fleming83e68182012-11-14 09:42:35 +0000796 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800797 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000798
Matthew Garrettcc5a0802013-04-15 13:09:46 -0700799 pa_data = boot_params.hdr.setup_data;
800 while (pa_data) {
801 data = early_ioremap(pa_data, sizeof(*efi_var_data));
802 if (data->type == SETUP_EFI_VARS) {
803 efi_var_data = (struct efi_var_bootdata *)data;
804
805 efi_var_store_size = efi_var_data->store_size;
806 efi_var_remaining_size = efi_var_data->remaining_size;
807 efi_var_max_var_size = efi_var_data->max_var_size;
808 }
809 pa_data = data->next;
810 early_iounmap(data, sizeof(*efi_var_data));
811 }
812
Matthew Garrett31ff2f22013-04-15 13:09:47 -0700813 boot_used_size = efi_var_store_size - efi_var_remaining_size;
814
Matt Fleming83e68182012-11-14 09:42:35 +0000815 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800816
817 /*
818 * Show what we know for posterity
819 */
820 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
821 if (c16) {
822 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
823 vendor[i] = *c16++;
824 vendor[i] = '\0';
825 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800826 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800827 early_iounmap(tmp, 2);
828
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800829 pr_info("EFI v%u.%.02u by %s\n",
830 efi.systab->hdr.revision >> 16,
831 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800832
Matt Fleming83e68182012-11-14 09:42:35 +0000833 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800834 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000835
836 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800837
Olof Johansson1adbfa32012-02-12 13:24:29 -0800838 /*
839 * Note: We currently don't support runtime services on an EFI
840 * that doesn't match the kernel 32/64-bit mode.
841 */
842
Olof Johansson5189c2a2012-10-24 10:00:44 -0700843 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800844 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000845 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000846 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000847 return;
848 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
Olof Johansson140bf272012-02-12 13:24:28 -0800849 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800850
Matt Fleming83e68182012-11-14 09:42:35 +0000851 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800852 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000853
854 set_bit(EFI_MEMMAP, &x86_efi_facility);
855
Linus Torvalds11520e52012-12-15 15:15:24 -0800856#ifdef CONFIG_X86_32
Olof Johansson5189c2a2012-10-24 10:00:44 -0700857 if (efi_is_native()) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800858 x86_platform.get_wallclock = efi_get_time;
859 x86_platform.set_wallclock = efi_set_rtc_mmss;
860 }
Linus Torvalds11520e52012-12-15 15:15:24 -0800861#endif
Feng Tang7bd867d2009-09-10 10:48:56 +0800862
Huang, Ying5b836832008-01-30 13:31:19 +0100863#if EFI_DEBUG
864 print_efi_memmap();
865#endif
866}
867
Josh Triplett2223af32012-09-28 17:57:05 -0700868void __init efi_late_init(void)
869{
870 efi_bgrt_init();
871}
872
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400873void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
874{
875 u64 addr, npages;
876
877 addr = md->virt_addr;
878 npages = md->num_pages;
879
880 memrange_efi_to_native(&addr, &npages);
881
882 if (executable)
883 set_memory_x(addr, npages);
884 else
885 set_memory_nx(addr, npages);
886}
887
Huang, Yinga2172e22008-01-30 13:33:55 +0100888static void __init runtime_code_page_mkexec(void)
889{
890 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100891 void *p;
892
Huang, Yinga2172e22008-01-30 13:33:55 +0100893 /* Make EFI runtime service code area executable */
894 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
895 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100896
897 if (md->type != EFI_RUNTIME_SERVICES_CODE)
898 continue;
899
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400900 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100901 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100902}
Huang, Yinga2172e22008-01-30 13:33:55 +0100903
Huang, Ying5b836832008-01-30 13:31:19 +0100904/*
Josh Triplett7bc90e02012-09-28 17:56:08 -0700905 * We can't ioremap data in EFI boot services RAM, because we've already mapped
906 * it as RAM. So, look it up in the existing EFI memory map instead. Only
907 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
908 */
909void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
910{
911 void *p;
912 if (WARN_ON(!memmap.map))
913 return NULL;
914 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
915 efi_memory_desc_t *md = p;
916 u64 size = md->num_pages << EFI_PAGE_SHIFT;
917 u64 end = md->phys_addr + size;
918 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
919 md->type != EFI_BOOT_SERVICES_CODE &&
920 md->type != EFI_BOOT_SERVICES_DATA)
921 continue;
922 if (!md->virt_addr)
923 continue;
924 if (phys_addr >= md->phys_addr && phys_addr < end) {
925 phys_addr += md->virt_addr - md->phys_addr;
926 return (__force void __iomem *)(unsigned long)phys_addr;
927 }
928 }
929 return NULL;
930}
931
Matt Fleming3e8fa262012-10-19 13:25:46 +0100932void efi_memory_uc(u64 addr, unsigned long size)
933{
934 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
935 u64 npages;
936
937 npages = round_up(size, page_shift) / page_shift;
938 memrange_efi_to_native(&addr, &npages);
939 set_memory_uc(addr, npages);
940}
941
Josh Triplett7bc90e02012-09-28 17:56:08 -0700942/*
Huang, Ying5b836832008-01-30 13:31:19 +0100943 * This function will switch the EFI runtime services to virtual mode.
944 * Essentially, look through the EFI memmap and map every region that
945 * has the runtime attribute bit set in its memory descriptor and update
946 * that memory descriptor with the virtual address obtained from ioremap().
947 * This enables the runtime services to be called without having to
948 * thunk back into physical mode for every invocation.
949 */
950void __init efi_enter_virtual_mode(void)
951{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400952 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100953 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100954 unsigned long size;
Jacob Shindda56e12012-11-16 19:38:48 -0800955 u64 end, systab, start_pfn, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400956 void *p, *va, *new_memmap = NULL;
957 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100958
959 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400960
Olof Johansson1adbfa32012-02-12 13:24:29 -0800961 /*
962 * We don't do virtual mode, since we don't do runtime services, on
963 * non-native EFI
964 */
965
Olof Johansson5189c2a2012-10-24 10:00:44 -0700966 if (!efi_is_native()) {
Josh Triplett78510792012-09-28 17:55:44 -0700967 efi_unmap_memmap();
968 return;
969 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800970
Matthew Garrett202f9d02011-05-05 15:19:44 -0400971 /* Merge contiguous regions of the same type and attribute */
972 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
973 u64 prev_size;
974 md = p;
975
976 if (!prev_md) {
977 prev_md = md;
978 continue;
979 }
980
981 if (prev_md->type != md->type ||
982 prev_md->attribute != md->attribute) {
983 prev_md = md;
984 continue;
985 }
986
987 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
988
989 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
990 prev_md->num_pages += md->num_pages;
991 md->type = EFI_RESERVED_TYPE;
992 md->attribute = 0;
993 continue;
994 }
995 prev_md = md;
996 }
997
Huang, Ying5b836832008-01-30 13:31:19 +0100998 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
999 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -04001000 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
1001 md->type != EFI_BOOT_SERVICES_CODE &&
1002 md->type != EFI_BOOT_SERVICES_DATA)
Huang, Ying5b836832008-01-30 13:31:19 +01001003 continue;
Huang, Ying1c083eb2008-02-04 16:48:06 +01001004
1005 size = md->num_pages << EFI_PAGE_SHIFT;
1006 end = md->phys_addr + size;
1007
Jacob Shindda56e12012-11-16 19:38:48 -08001008 start_pfn = PFN_DOWN(md->phys_addr);
Huang Yingdd39ecf2009-03-04 10:58:33 +08001009 end_pfn = PFN_UP(end);
Jacob Shindda56e12012-11-16 19:38:48 -08001010 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
Huang, Ying1c083eb2008-02-04 16:48:06 +01001011 va = __va(md->phys_addr);
Matt Fleming3e8fa262012-10-19 13:25:46 +01001012
1013 if (!(md->attribute & EFI_MEMORY_WB))
1014 efi_memory_uc((u64)(unsigned long)va, size);
1015 } else
1016 va = efi_ioremap(md->phys_addr, size,
1017 md->type, md->attribute);
Huang, Ying1c083eb2008-02-04 16:48:06 +01001018
Huang, Ying1c083eb2008-02-04 16:48:06 +01001019 md->virt_addr = (u64) (unsigned long) va;
1020
1021 if (!va) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -08001022 pr_err("ioremap of 0x%llX failed!\n",
Huang, Ying5b836832008-01-30 13:31:19 +01001023 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +01001024 continue;
1025 }
1026
1027 systab = (u64) (unsigned long) efi_phys.systab;
1028 if (md->phys_addr <= systab && systab < end) {
1029 systab += md->virt_addr - md->phys_addr;
1030 efi.systab = (efi_system_table_t *) (unsigned long) systab;
1031 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001032 new_memmap = krealloc(new_memmap,
1033 (count + 1) * memmap.desc_size,
1034 GFP_KERNEL);
1035 memcpy(new_memmap + (count * memmap.desc_size), md,
1036 memmap.desc_size);
1037 count++;
Huang, Ying5b836832008-01-30 13:31:19 +01001038 }
1039
1040 BUG_ON(!efi.systab);
1041
1042 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001043 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +01001044 memmap.desc_size,
1045 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001046 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +01001047
1048 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -08001049 pr_alert("Unable to switch EFI into virtual mode "
1050 "(status=%lx)!\n", status);
Huang, Ying5b836832008-01-30 13:31:19 +01001051 panic("EFI call to SetVirtualAddressMap() failed!");
1052 }
1053
1054 /*
1055 * Now that EFI is in virtual mode, update the function
1056 * pointers in the runtime service table to the new virtual addresses.
1057 *
1058 * Call EFI services through wrapper functions.
1059 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001060 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +01001061 efi.get_time = virt_efi_get_time;
1062 efi.set_time = virt_efi_set_time;
1063 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1064 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1065 efi.get_variable = virt_efi_get_variable;
1066 efi.get_next_variable = virt_efi_get_next_variable;
1067 efi.set_variable = virt_efi_set_variable;
1068 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1069 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001070 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -04001071 efi.query_variable_info = virt_efi_query_variable_info;
1072 efi.update_capsule = virt_efi_update_capsule;
1073 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Huang, Ying4de0d4a2008-02-13 17:22:41 +08001074 if (__supported_pte_mask & _PAGE_NX)
1075 runtime_code_page_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001076
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001077 kfree(new_memmap);
Huang, Ying5b836832008-01-30 13:31:19 +01001078}
1079
1080/*
1081 * Convenience functions to obtain memory types and attributes
1082 */
1083u32 efi_mem_type(unsigned long phys_addr)
1084{
1085 efi_memory_desc_t *md;
1086 void *p;
1087
Matt Fleming83e68182012-11-14 09:42:35 +00001088 if (!efi_enabled(EFI_MEMMAP))
1089 return 0;
1090
Huang, Ying5b836832008-01-30 13:31:19 +01001091 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1092 md = p;
1093 if ((md->phys_addr <= phys_addr) &&
1094 (phys_addr < (md->phys_addr +
1095 (md->num_pages << EFI_PAGE_SHIFT))))
1096 return md->type;
1097 }
1098 return 0;
1099}
1100
1101u64 efi_mem_attributes(unsigned long phys_addr)
1102{
1103 efi_memory_desc_t *md;
1104 void *p;
1105
1106 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1107 md = p;
1108 if ((md->phys_addr <= phys_addr) &&
1109 (phys_addr < (md->phys_addr +
1110 (md->num_pages << EFI_PAGE_SHIFT))))
1111 return md->attribute;
1112 }
1113 return 0;
1114}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001115
1116/*
1117 * Some firmware has serious problems when using more than 50% of the EFI
1118 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1119 * we never use more than this safe limit.
1120 *
1121 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1122 * store.
1123 */
1124efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1125{
1126 efi_status_t status;
1127 u64 storage_size, remaining_size, max_size;
1128
1129 status = efi.query_variable_info(attributes, &storage_size,
1130 &remaining_size, &max_size);
1131 if (status != EFI_SUCCESS)
1132 return status;
1133
Richard Weinberger7791c842013-04-10 10:59:34 +02001134 if (!max_size && remaining_size > size)
1135 printk_once(KERN_ERR FW_BUG "Broken EFI implementation"
1136 " is returning MaxVariableSize=0\n");
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001137 /*
1138 * Some firmware implementations refuse to boot if there's insufficient
1139 * space in the variable store. We account for that by refusing the
1140 * write if permitting it would reduce the available space to under
1141 * 50%. However, some firmware won't reclaim variable space until
1142 * after the used (not merely the actively used) space drops below
1143 * a threshold. We can approximate that case with the value calculated
1144 * above. If both the firmware and our calculations indicate that the
1145 * available space would drop below 50%, refuse the write.
1146 */
Richard Weinberger7791c842013-04-10 10:59:34 +02001147
1148 if (!storage_size || size > remaining_size ||
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001149 (max_size && size > max_size))
1150 return EFI_OUT_OF_RESOURCES;
1151
1152 if (!efi_no_storage_paranoia &&
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001153 ((active_size + size + VAR_METADATA_SIZE > storage_size / 2) &&
1154 (remaining_size - size < storage_size / 2)))
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001155 return EFI_OUT_OF_RESOURCES;
1156
1157 return EFI_SUCCESS;
1158}
Sergey Vlasov36680112013-04-16 18:31:09 +04001159EXPORT_SYMBOL_GPL(efi_query_variable_store);