blob: 816e940b3998e50d30a2836428797f95e79cd195 [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>
David Howells0d01ff22013-04-11 23:51:01 +010037#include <linux/slab.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070038#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010039#include <linux/spinlock.h>
40#include <linux/uaccess.h>
41#include <linux/time.h>
42#include <linux/io.h>
43#include <linux/reboot.h>
44#include <linux/bcd.h>
45
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>
Prarit Bhargava3195ef52013-02-14 12:02:54 -050052#include <asm/rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010053
54#define EFI_DEBUG 1
Huang, Ying5b836832008-01-30 13:31:19 +010055
Matthew Garrettf8b84042013-06-01 16:06:20 -040056#define EFI_MIN_RESERVE 5120
57
58#define EFI_DUMMY_GUID \
59 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
60
61static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
Matthew Garrett31ff2f22013-04-15 13:09:47 -070062
Huang, Ying5b836832008-01-30 13:31:19 +010063struct efi_memory_map memmap;
64
Harvey Harrisonecaea422008-02-13 13:26:13 -080065static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010066static efi_system_table_t efi_systab __initdata;
67
Leif Lindholmd32aff82013-09-05 11:34:54 +010068static __initdata efi_config_table_type_t arch_tables[] = {
69#ifdef CONFIG_X86_UV
70 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
71#endif
72 {NULL_GUID, NULL, 0},
73};
Matt Fleming83e68182012-11-14 09:42:35 +000074
Matt Flemingf5c16b22014-01-15 13:21:22 +000075u64 efi_setup; /* efi setup_data physical address */
Matt Fleming83e68182012-11-14 09:42:35 +000076
Matt Fleming058e7b52013-02-22 15:03:47 +000077static bool __initdata disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010078static int __init setup_noefi(char *arg)
79{
Matt Flemingfb834c72013-02-20 20:36:12 +000080 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010081 return 0;
82}
83early_param("noefi", setup_noefi);
84
Paul Jackson200001e2008-06-25 05:44:46 -070085int add_efi_memmap;
86EXPORT_SYMBOL(add_efi_memmap);
87
88static int __init setup_add_efi_memmap(char *arg)
89{
90 add_efi_memmap = 1;
91 return 0;
92}
93early_param("add_efi_memmap", setup_add_efi_memmap);
94
Richard Weinberger8c58bf32013-04-17 01:00:53 +020095static bool efi_no_storage_paranoia;
96
97static int __init setup_storage_paranoia(char *arg)
98{
99 efi_no_storage_paranoia = true;
100 return 0;
101}
102early_param("efi_no_storage_paranoia", setup_storage_paranoia);
103
Paul Jackson200001e2008-06-25 05:44:46 -0700104
Huang, Ying5b836832008-01-30 13:31:19 +0100105static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
106{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100107 unsigned long flags;
108 efi_status_t status;
109
110 spin_lock_irqsave(&rtc_lock, flags);
111 status = efi_call_virt2(get_time, tm, tc);
112 spin_unlock_irqrestore(&rtc_lock, flags);
113 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100114}
115
116static efi_status_t virt_efi_set_time(efi_time_t *tm)
117{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100118 unsigned long flags;
119 efi_status_t status;
120
121 spin_lock_irqsave(&rtc_lock, flags);
122 status = efi_call_virt1(set_time, tm);
123 spin_unlock_irqrestore(&rtc_lock, flags);
124 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100125}
126
127static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
128 efi_bool_t *pending,
129 efi_time_t *tm)
130{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100131 unsigned long flags;
132 efi_status_t status;
133
134 spin_lock_irqsave(&rtc_lock, flags);
135 status = efi_call_virt3(get_wakeup_time,
136 enabled, pending, tm);
137 spin_unlock_irqrestore(&rtc_lock, flags);
138 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100139}
140
141static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
142{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100143 unsigned long flags;
144 efi_status_t status;
145
146 spin_lock_irqsave(&rtc_lock, flags);
147 status = efi_call_virt2(set_wakeup_time,
148 enabled, tm);
149 spin_unlock_irqrestore(&rtc_lock, flags);
150 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100151}
152
153static efi_status_t virt_efi_get_variable(efi_char16_t *name,
154 efi_guid_t *vendor,
155 u32 *attr,
156 unsigned long *data_size,
157 void *data)
158{
159 return efi_call_virt5(get_variable,
160 name, vendor, attr,
161 data_size, data);
162}
163
164static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
165 efi_char16_t *name,
166 efi_guid_t *vendor)
167{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400168 return efi_call_virt3(get_next_variable,
169 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100170}
171
172static efi_status_t virt_efi_set_variable(efi_char16_t *name,
173 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400174 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100175 unsigned long data_size,
176 void *data)
177{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400178 return efi_call_virt5(set_variable,
179 name, vendor, attr,
180 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100181}
182
Matthew Garrett3b370232011-06-06 15:36:25 -0400183static efi_status_t virt_efi_query_variable_info(u32 attr,
184 u64 *storage_space,
185 u64 *remaining_space,
186 u64 *max_variable_size)
187{
188 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
189 return EFI_UNSUPPORTED;
190
191 return efi_call_virt4(query_variable_info, attr, storage_space,
192 remaining_space, max_variable_size);
193}
194
Huang, Ying5b836832008-01-30 13:31:19 +0100195static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
196{
197 return efi_call_virt1(get_next_high_mono_count, count);
198}
199
200static void virt_efi_reset_system(int reset_type,
201 efi_status_t status,
202 unsigned long data_size,
203 efi_char16_t *data)
204{
205 efi_call_virt4(reset_system, reset_type, status,
206 data_size, data);
207}
208
Matthew Garrett3b370232011-06-06 15:36:25 -0400209static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
210 unsigned long count,
211 unsigned long sg_list)
212{
213 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
214 return EFI_UNSUPPORTED;
215
216 return efi_call_virt3(update_capsule, capsules, count, sg_list);
217}
218
219static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
220 unsigned long count,
221 u64 *max_size,
222 int *reset_type)
223{
224 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
225 return EFI_UNSUPPORTED;
226
227 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
228 reset_type);
229}
230
Huang, Ying5b836832008-01-30 13:31:19 +0100231static efi_status_t __init phys_efi_set_virtual_address_map(
232 unsigned long memory_map_size,
233 unsigned long descriptor_size,
234 u32 descriptor_version,
235 efi_memory_desc_t *virtual_map)
236{
237 efi_status_t status;
238
239 efi_call_phys_prelog();
240 status = efi_call_phys4(efi_phys.set_virtual_address_map,
241 memory_map_size, descriptor_size,
242 descriptor_version, virtual_map);
243 efi_call_phys_epilog();
244 return status;
245}
246
Linus Torvalds11520e52012-12-15 15:15:24 -0800247static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
248 efi_time_cap_t *tc)
249{
250 unsigned long flags;
251 efi_status_t status;
252
253 spin_lock_irqsave(&rtc_lock, flags);
254 efi_call_phys_prelog();
255 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
256 virt_to_phys(tc));
257 efi_call_phys_epilog();
258 spin_unlock_irqrestore(&rtc_lock, flags);
259 return status;
260}
261
262int efi_set_rtc_mmss(unsigned long nowtime)
Huang, Ying5b836832008-01-30 13:31:19 +0100263{
Huang, Ying5b836832008-01-30 13:31:19 +0100264 efi_status_t status;
265 efi_time_t eft;
266 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500267 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100268
269 status = efi.get_time(&eft, &cap);
270 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800271 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100272 return -1;
273 }
274
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500275 rtc_time_to_tm(nowtime, &tm);
276 if (!rtc_valid_tm(&tm)) {
277 eft.year = tm.tm_year + 1900;
278 eft.month = tm.tm_mon + 1;
279 eft.day = tm.tm_mday;
280 eft.minute = tm.tm_min;
281 eft.second = tm.tm_sec;
282 eft.nanosecond = 0;
283 } else {
284 printk(KERN_ERR
285 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
286 __FUNCTION__, nowtime);
287 return -1;
288 }
Huang, Ying5b836832008-01-30 13:31:19 +0100289
290 status = efi.set_time(&eft);
291 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800292 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100293 return -1;
294 }
295 return 0;
296}
297
Linus Torvalds11520e52012-12-15 15:15:24 -0800298unsigned long efi_get_time(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100299{
300 efi_status_t status;
301 efi_time_t eft;
302 efi_time_cap_t cap;
303
304 status = efi.get_time(&eft, &cap);
305 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800306 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100307
308 return mktime(eft.year, eft.month, eft.day, eft.hour,
309 eft.minute, eft.second);
310}
311
Paul Jackson69c91892008-05-14 08:15:58 -0700312/*
313 * Tell the kernel about the EFI memory map. This might include
314 * more than the max 128 entries that can fit in the e820 legacy
315 * (zeropage) memory map.
316 */
317
Paul Jackson200001e2008-06-25 05:44:46 -0700318static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700319{
320 void *p;
321
322 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
323 efi_memory_desc_t *md = p;
324 unsigned long long start = md->phys_addr;
325 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
326 int e820_type;
327
Cliff Wickmane2a71472009-06-16 16:43:40 -0500328 switch (md->type) {
329 case EFI_LOADER_CODE:
330 case EFI_LOADER_DATA:
331 case EFI_BOOT_SERVICES_CODE:
332 case EFI_BOOT_SERVICES_DATA:
333 case EFI_CONVENTIONAL_MEMORY:
334 if (md->attribute & EFI_MEMORY_WB)
335 e820_type = E820_RAM;
336 else
337 e820_type = E820_RESERVED;
338 break;
339 case EFI_ACPI_RECLAIM_MEMORY:
340 e820_type = E820_ACPI;
341 break;
342 case EFI_ACPI_MEMORY_NVS:
343 e820_type = E820_NVS;
344 break;
345 case EFI_UNUSABLE_MEMORY:
346 e820_type = E820_UNUSABLE;
347 break;
348 default:
349 /*
350 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
351 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
352 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
353 */
Paul Jackson69c91892008-05-14 08:15:58 -0700354 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500355 break;
356 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700357 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700358 }
359 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
360}
361
Olof Johansson1adbfa32012-02-12 13:24:29 -0800362int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800363{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100364 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800365 unsigned long pmap;
366
Paul Jackson05486fa2008-06-22 07:22:02 -0700367#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800368 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100369 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800370 pr_err("Memory map is above 4GB, disabling EFI.\n");
371 return -EINVAL;
372 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100373 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700374#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100375 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800376#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100377 memmap.phys_map = (void *)pmap;
378 memmap.nr_map = e->efi_memmap_size /
379 e->efi_memdesc_size;
380 memmap.desc_size = e->efi_memdesc_size;
381 memmap.desc_version = e->efi_memdesc_version;
382
Tejun Heo24aa0782011-07-12 11:16:06 +0200383 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800384
Leif Lindholm44586892013-09-05 11:34:55 +0100385 efi.memmap = &memmap;
386
Olof Johansson1adbfa32012-02-12 13:24:29 -0800387 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800388}
389
Huang, Ying5b836832008-01-30 13:31:19 +0100390#if EFI_DEBUG
391static void __init print_efi_memmap(void)
392{
393 efi_memory_desc_t *md;
394 void *p;
395 int i;
396
397 for (p = memmap.map, i = 0;
398 p < memmap.map_end;
399 p += memmap.desc_size, i++) {
400 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800401 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100402 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
403 i, md->type, md->attribute, md->phys_addr,
404 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
405 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
406 }
407}
408#endif /* EFI_DEBUG */
409
Matthew Garrett916f6762011-05-25 09:53:13 -0400410void __init efi_reserve_boot_services(void)
411{
412 void *p;
413
414 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
415 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200416 u64 start = md->phys_addr;
417 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400418
419 if (md->type != EFI_BOOT_SERVICES_CODE &&
420 md->type != EFI_BOOT_SERVICES_DATA)
421 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200422 /* Only reserve where possible:
423 * - Not within any already allocated areas
424 * - Not over any memory area (really needed, if above?)
425 * - Not within any part of the kernel
426 * - Not the bios reserved area
427 */
Dave Young5a45f3d2013-12-20 18:02:15 +0800428 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800429 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200430 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200431 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200432 /* Could not reserve, skip it */
433 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800434 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200435 "[0x%010llx-0x%010llx]\n",
436 start, start+size-1);
437 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200438 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400439 }
440}
441
Olof Johansson5189c2a2012-10-24 10:00:44 -0700442void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700443{
Matt Flemingf5c16b22014-01-15 13:21:22 +0000444 clear_bit(EFI_MEMMAP, &efi.flags);
Josh Triplett78510792012-09-28 17:55:44 -0700445 if (memmap.map) {
446 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
447 memmap.map = NULL;
448 }
449}
450
451void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400452{
453 void *p;
454
Olof Johansson5189c2a2012-10-24 10:00:44 -0700455 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700456 return;
457
Matthew Garrett916f6762011-05-25 09:53:13 -0400458 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
459 efi_memory_desc_t *md = p;
460 unsigned long long start = md->phys_addr;
461 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
462
463 if (md->type != EFI_BOOT_SERVICES_CODE &&
464 md->type != EFI_BOOT_SERVICES_DATA)
465 continue;
466
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200467 /* Could not reserve boot area */
468 if (!size)
469 continue;
470
Matthew Garrett916f6762011-05-25 09:53:13 -0400471 free_bootmem_late(start, size);
472 }
Josh Triplett78510792012-09-28 17:55:44 -0700473
474 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400475}
476
Olof Johansson140bf272012-02-12 13:24:28 -0800477static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100478{
Matt Fleming83e68182012-11-14 09:42:35 +0000479 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800480 efi_system_table_64_t *systab64;
481 u64 tmp = 0;
482
483 systab64 = early_ioremap((unsigned long)phys,
484 sizeof(*systab64));
485 if (systab64 == NULL) {
486 pr_err("Couldn't map the system table!\n");
487 return -ENOMEM;
488 }
489
490 efi_systab.hdr = systab64->hdr;
491 efi_systab.fw_vendor = systab64->fw_vendor;
492 tmp |= systab64->fw_vendor;
493 efi_systab.fw_revision = systab64->fw_revision;
494 efi_systab.con_in_handle = systab64->con_in_handle;
495 tmp |= systab64->con_in_handle;
496 efi_systab.con_in = systab64->con_in;
497 tmp |= systab64->con_in;
498 efi_systab.con_out_handle = systab64->con_out_handle;
499 tmp |= systab64->con_out_handle;
500 efi_systab.con_out = systab64->con_out;
501 tmp |= systab64->con_out;
502 efi_systab.stderr_handle = systab64->stderr_handle;
503 tmp |= systab64->stderr_handle;
504 efi_systab.stderr = systab64->stderr;
505 tmp |= systab64->stderr;
506 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
507 tmp |= systab64->runtime;
508 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
509 tmp |= systab64->boottime;
510 efi_systab.nr_tables = systab64->nr_tables;
511 efi_systab.tables = systab64->tables;
512 tmp |= systab64->tables;
513
514 early_iounmap(systab64, sizeof(*systab64));
515#ifdef CONFIG_X86_32
516 if (tmp >> 32) {
517 pr_err("EFI data located above 4GB, disabling EFI.\n");
518 return -EINVAL;
519 }
520#endif
521 } else {
522 efi_system_table_32_t *systab32;
523
524 systab32 = early_ioremap((unsigned long)phys,
525 sizeof(*systab32));
526 if (systab32 == NULL) {
527 pr_err("Couldn't map the system table!\n");
528 return -ENOMEM;
529 }
530
531 efi_systab.hdr = systab32->hdr;
532 efi_systab.fw_vendor = systab32->fw_vendor;
533 efi_systab.fw_revision = systab32->fw_revision;
534 efi_systab.con_in_handle = systab32->con_in_handle;
535 efi_systab.con_in = systab32->con_in;
536 efi_systab.con_out_handle = systab32->con_out_handle;
537 efi_systab.con_out = systab32->con_out;
538 efi_systab.stderr_handle = systab32->stderr_handle;
539 efi_systab.stderr = systab32->stderr;
540 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
541 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
542 efi_systab.nr_tables = systab32->nr_tables;
543 efi_systab.tables = systab32->tables;
544
545 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800546 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800547
Huang, Ying5b836832008-01-30 13:31:19 +0100548 efi.systab = &efi_systab;
549
550 /*
551 * Verify the EFI Table
552 */
Olof Johansson140bf272012-02-12 13:24:28 -0800553 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800554 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800555 return -EINVAL;
556 }
Huang, Ying5b836832008-01-30 13:31:19 +0100557 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800558 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100559 "%d.%02d, expected 1.00 or greater!\n",
560 efi.systab->hdr.revision >> 16,
561 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800562
563 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800564}
Huang, Ying5b836832008-01-30 13:31:19 +0100565
Olof Johansson140bf272012-02-12 13:24:28 -0800566static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800567{
568 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100569
570 /*
571 * Check out the runtime services table. We need to map
572 * the runtime services table so that we can grab the physical
573 * address of several of the EFI runtime functions, needed to
574 * set the firmware into virtual mode.
575 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100576 runtime = early_ioremap((unsigned long)efi.systab->runtime,
577 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800578 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800579 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800580 return -ENOMEM;
581 }
582 /*
583 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800584 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800585 * is invoked.
586 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800587 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800588 efi_phys.set_virtual_address_map =
589 (efi_set_virtual_address_map_t *)
590 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800591 /*
592 * Make efi_get_time can be called before entering
593 * virtual mode.
594 */
595 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100596 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800597
598 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800599}
Huang, Ying5b836832008-01-30 13:31:19 +0100600
Olof Johansson140bf272012-02-12 13:24:28 -0800601static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800602{
Huang, Ying5b836832008-01-30 13:31:19 +0100603 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100604 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
605 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800606 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800607 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800608 return -ENOMEM;
609 }
Huang, Ying5b836832008-01-30 13:31:19 +0100610 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500611
Paul Jackson200001e2008-06-25 05:44:46 -0700612 if (add_efi_memmap)
613 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800614
615 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800616}
617
618void __init efi_init(void)
619{
620 efi_char16_t *c16;
621 char vendor[100] = "unknown";
622 int i = 0;
623 void *tmp;
624
625#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800626 if (boot_params.efi_info.efi_systab_hi ||
627 boot_params.efi_info.efi_memmap_hi) {
628 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800629 return;
630 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800631 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
632#else
633 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800634 (boot_params.efi_info.efi_systab |
635 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800636#endif
637
Matt Fleming83e68182012-11-14 09:42:35 +0000638 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800639 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000640
Matt Flemingf5c16b22014-01-15 13:21:22 +0000641 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800642
Matt Fleming24439fe2014-01-15 13:21:22 +0000643 efi.config_table = (unsigned long)efi.systab->tables;
644 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
645 efi.runtime = (unsigned long)efi.systab->runtime;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800646
647 /*
648 * Show what we know for posterity
649 */
650 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
651 if (c16) {
652 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
653 vendor[i] = *c16++;
654 vendor[i] = '\0';
655 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800656 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800657 early_iounmap(tmp, 2);
658
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800659 pr_info("EFI v%u.%.02u by %s\n",
660 efi.systab->hdr.revision >> 16,
661 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800662
Leif Lindholmd32aff82013-09-05 11:34:54 +0100663 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800664 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000665
Matt Flemingf5c16b22014-01-15 13:21:22 +0000666 set_bit(EFI_CONFIG_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800667
Olof Johansson1adbfa32012-02-12 13:24:29 -0800668 /*
669 * Note: We currently don't support runtime services on an EFI
670 * that doesn't match the kernel 32/64-bit mode.
671 */
672
Olof Johansson5189c2a2012-10-24 10:00:44 -0700673 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800674 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000675 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000676 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000677 return;
Matt Flemingf5c16b22014-01-15 13:21:22 +0000678 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
Olof Johansson140bf272012-02-12 13:24:28 -0800679 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800680
Matt Fleming83e68182012-11-14 09:42:35 +0000681 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800682 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000683
Matt Flemingf5c16b22014-01-15 13:21:22 +0000684 set_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming83e68182012-11-14 09:42:35 +0000685
Huang, Ying5b836832008-01-30 13:31:19 +0100686#if EFI_DEBUG
687 print_efi_memmap();
688#endif
689}
690
Josh Triplett2223af32012-09-28 17:57:05 -0700691void __init efi_late_init(void)
692{
693 efi_bgrt_init();
694}
695
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400696void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
697{
698 u64 addr, npages;
699
700 addr = md->virt_addr;
701 npages = md->num_pages;
702
703 memrange_efi_to_native(&addr, &npages);
704
705 if (executable)
706 set_memory_x(addr, npages);
707 else
708 set_memory_nx(addr, npages);
709}
710
Huang, Yinga2172e22008-01-30 13:33:55 +0100711static void __init runtime_code_page_mkexec(void)
712{
713 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100714 void *p;
715
Huang, Yinga2172e22008-01-30 13:33:55 +0100716 /* Make EFI runtime service code area executable */
717 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
718 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100719
720 if (md->type != EFI_RUNTIME_SERVICES_CODE)
721 continue;
722
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400723 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100724 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100725}
Huang, Yinga2172e22008-01-30 13:33:55 +0100726
Matt Fleming3e8fa262012-10-19 13:25:46 +0100727void efi_memory_uc(u64 addr, unsigned long size)
728{
729 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
730 u64 npages;
731
732 npages = round_up(size, page_shift) / page_shift;
733 memrange_efi_to_native(&addr, &npages);
734 set_memory_uc(addr, npages);
735}
736
Josh Triplett7bc90e02012-09-28 17:56:08 -0700737/*
Huang, Ying5b836832008-01-30 13:31:19 +0100738 * This function will switch the EFI runtime services to virtual mode.
739 * Essentially, look through the EFI memmap and map every region that
740 * has the runtime attribute bit set in its memory descriptor and update
741 * that memory descriptor with the virtual address obtained from ioremap().
742 * This enables the runtime services to be called without having to
743 * thunk back into physical mode for every invocation.
744 */
745void __init efi_enter_virtual_mode(void)
746{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400747 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100748 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100749 unsigned long size;
Jacob Shindda56e12012-11-16 19:38:48 -0800750 u64 end, systab, start_pfn, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400751 void *p, *va, *new_memmap = NULL;
752 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100753
754 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400755
Olof Johansson1adbfa32012-02-12 13:24:29 -0800756 /*
757 * We don't do virtual mode, since we don't do runtime services, on
758 * non-native EFI
759 */
760
Olof Johansson5189c2a2012-10-24 10:00:44 -0700761 if (!efi_is_native()) {
Josh Triplett78510792012-09-28 17:55:44 -0700762 efi_unmap_memmap();
763 return;
764 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800765
Matthew Garrett202f9d02011-05-05 15:19:44 -0400766 /* Merge contiguous regions of the same type and attribute */
767 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
768 u64 prev_size;
769 md = p;
770
771 if (!prev_md) {
772 prev_md = md;
773 continue;
774 }
775
776 if (prev_md->type != md->type ||
777 prev_md->attribute != md->attribute) {
778 prev_md = md;
779 continue;
780 }
781
782 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
783
784 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
785 prev_md->num_pages += md->num_pages;
786 md->type = EFI_RESERVED_TYPE;
787 md->attribute = 0;
788 continue;
789 }
790 prev_md = md;
791 }
792
Huang, Ying5b836832008-01-30 13:31:19 +0100793 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
794 md = p;
Josh Boyer81fbb94d2013-04-18 07:51:34 -0700795 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
796#ifdef CONFIG_X86_64
797 if (md->type != EFI_BOOT_SERVICES_CODE &&
798 md->type != EFI_BOOT_SERVICES_DATA)
799#endif
800 continue;
801 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100802
803 size = md->num_pages << EFI_PAGE_SHIFT;
804 end = md->phys_addr + size;
805
Jacob Shindda56e12012-11-16 19:38:48 -0800806 start_pfn = PFN_DOWN(md->phys_addr);
Huang Yingdd39ecf2009-03-04 10:58:33 +0800807 end_pfn = PFN_UP(end);
Jacob Shindda56e12012-11-16 19:38:48 -0800808 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
Huang, Ying1c083eb2008-02-04 16:48:06 +0100809 va = __va(md->phys_addr);
Matt Fleming3e8fa262012-10-19 13:25:46 +0100810
811 if (!(md->attribute & EFI_MEMORY_WB))
812 efi_memory_uc((u64)(unsigned long)va, size);
813 } else
814 va = efi_ioremap(md->phys_addr, size,
815 md->type, md->attribute);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100816
Huang, Ying1c083eb2008-02-04 16:48:06 +0100817 md->virt_addr = (u64) (unsigned long) va;
818
819 if (!va) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800820 pr_err("ioremap of 0x%llX failed!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100821 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100822 continue;
823 }
824
825 systab = (u64) (unsigned long) efi_phys.systab;
826 if (md->phys_addr <= systab && systab < end) {
827 systab += md->virt_addr - md->phys_addr;
828 efi.systab = (efi_system_table_t *) (unsigned long) systab;
829 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400830 new_memmap = krealloc(new_memmap,
831 (count + 1) * memmap.desc_size,
832 GFP_KERNEL);
833 memcpy(new_memmap + (count * memmap.desc_size), md,
834 memmap.desc_size);
835 count++;
Huang, Ying5b836832008-01-30 13:31:19 +0100836 }
837
838 BUG_ON(!efi.systab);
839
840 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400841 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +0100842 memmap.desc_size,
843 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400844 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +0100845
846 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800847 pr_alert("Unable to switch EFI into virtual mode "
848 "(status=%lx)!\n", status);
Huang, Ying5b836832008-01-30 13:31:19 +0100849 panic("EFI call to SetVirtualAddressMap() failed!");
850 }
851
852 /*
853 * Now that EFI is in virtual mode, update the function
854 * pointers in the runtime service table to the new virtual addresses.
855 *
856 * Call EFI services through wrapper functions.
857 */
Matt Fleming712ba9e2013-01-25 10:07:25 +0000858 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +0100859 efi.get_time = virt_efi_get_time;
860 efi.set_time = virt_efi_set_time;
861 efi.get_wakeup_time = virt_efi_get_wakeup_time;
862 efi.set_wakeup_time = virt_efi_set_wakeup_time;
863 efi.get_variable = virt_efi_get_variable;
864 efi.get_next_variable = virt_efi_get_next_variable;
865 efi.set_variable = virt_efi_set_variable;
866 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
867 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400868 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -0400869 efi.query_variable_info = virt_efi_query_variable_info;
870 efi.update_capsule = virt_efi_update_capsule;
871 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Huang, Ying4de0d4a2008-02-13 17:22:41 +0800872 if (__supported_pte_mask & _PAGE_NX)
873 runtime_code_page_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -0800874
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400875 kfree(new_memmap);
Matthew Garrettf8b84042013-06-01 16:06:20 -0400876
877 /* clean DUMMY object */
878 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
879 EFI_VARIABLE_NON_VOLATILE |
880 EFI_VARIABLE_BOOTSERVICE_ACCESS |
881 EFI_VARIABLE_RUNTIME_ACCESS,
882 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +0100883}
884
885/*
886 * Convenience functions to obtain memory types and attributes
887 */
888u32 efi_mem_type(unsigned long phys_addr)
889{
890 efi_memory_desc_t *md;
891 void *p;
892
Matt Fleming83e68182012-11-14 09:42:35 +0000893 if (!efi_enabled(EFI_MEMMAP))
894 return 0;
895
Huang, Ying5b836832008-01-30 13:31:19 +0100896 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
897 md = p;
898 if ((md->phys_addr <= phys_addr) &&
899 (phys_addr < (md->phys_addr +
900 (md->num_pages << EFI_PAGE_SHIFT))))
901 return md->type;
902 }
903 return 0;
904}
905
906u64 efi_mem_attributes(unsigned long phys_addr)
907{
908 efi_memory_desc_t *md;
909 void *p;
910
911 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
912 md = p;
913 if ((md->phys_addr <= phys_addr) &&
914 (phys_addr < (md->phys_addr +
915 (md->num_pages << EFI_PAGE_SHIFT))))
916 return md->attribute;
917 }
918 return 0;
919}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000920
921/*
922 * Some firmware has serious problems when using more than 50% of the EFI
923 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
924 * we never use more than this safe limit.
925 *
926 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
927 * store.
928 */
929efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
930{
931 efi_status_t status;
932 u64 storage_size, remaining_size, max_size;
933
Matthew Garrettf8b84042013-06-01 16:06:20 -0400934 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
935 return 0;
936
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000937 status = efi.query_variable_info(attributes, &storage_size,
938 &remaining_size, &max_size);
939 if (status != EFI_SUCCESS)
940 return status;
941
Matthew Garrett31ff2f22013-04-15 13:09:47 -0700942 /*
943 * Some firmware implementations refuse to boot if there's insufficient
944 * space in the variable store. We account for that by refusing the
945 * write if permitting it would reduce the available space to under
Matthew Garrettf8b84042013-06-01 16:06:20 -0400946 * 5KB. This figure was provided by Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -0700947 */
Matthew Garrettf8b84042013-06-01 16:06:20 -0400948 if ((remaining_size - size < EFI_MIN_RESERVE) &&
949 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +0200950
Matthew Garrettf8b84042013-06-01 16:06:20 -0400951 /*
952 * Triggering garbage collection may require that the firmware
953 * generate a real EFI_OUT_OF_RESOURCES error. We can force
954 * that by attempting to use more space than is available.
955 */
956 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +0100957 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
958
959 if (!dummy)
960 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +0200961
Matthew Garrettf8b84042013-06-01 16:06:20 -0400962 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
963 EFI_VARIABLE_NON_VOLATILE |
964 EFI_VARIABLE_BOOTSERVICE_ACCESS |
965 EFI_VARIABLE_RUNTIME_ACCESS,
966 dummy_size, dummy);
967
968 if (status == EFI_SUCCESS) {
969 /*
970 * This should have failed, so if it didn't make sure
971 * that we delete it...
972 */
973 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
974 EFI_VARIABLE_NON_VOLATILE |
975 EFI_VARIABLE_BOOTSERVICE_ACCESS |
976 EFI_VARIABLE_RUNTIME_ACCESS,
977 0, dummy);
978 }
979
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +0100980 kfree(dummy);
981
Matthew Garrettf8b84042013-06-01 16:06:20 -0400982 /*
983 * The runtime code may now have triggered a garbage collection
984 * run, so check the variable info again
985 */
986 status = efi.query_variable_info(attributes, &storage_size,
987 &remaining_size, &max_size);
988
989 if (status != EFI_SUCCESS)
990 return status;
991
992 /*
993 * There still isn't enough room, so return an error
994 */
995 if (remaining_size - size < EFI_MIN_RESERVE)
996 return EFI_OUT_OF_RESOURCES;
997 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000998
999 return EFI_SUCCESS;
1000}
Sergey Vlasov36680112013-04-16 18:31:09 +04001001EXPORT_SYMBOL_GPL(efi_query_variable_store);