blob: 90f6ed127096566ab06c0a9e6f25a355ccdfc048 [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 Triplett2223af382012-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
Jan Beulichd80603c2011-07-05 12:22:18 +010063struct efi __read_mostly efi = {
64 .mps = EFI_INVALID_TABLE_ADDR,
65 .acpi = EFI_INVALID_TABLE_ADDR,
66 .acpi20 = EFI_INVALID_TABLE_ADDR,
67 .smbios = EFI_INVALID_TABLE_ADDR,
68 .sal_systab = EFI_INVALID_TABLE_ADDR,
69 .boot_info = EFI_INVALID_TABLE_ADDR,
70 .hcdp = EFI_INVALID_TABLE_ADDR,
71 .uga = EFI_INVALID_TABLE_ADDR,
72 .uv_systab = EFI_INVALID_TABLE_ADDR,
73};
Huang, Ying5b836832008-01-30 13:31:19 +010074EXPORT_SYMBOL(efi);
75
76struct efi_memory_map memmap;
77
Harvey Harrisonecaea422008-02-13 13:26:13 -080078static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010079static efi_system_table_t efi_systab __initdata;
80
Matt Fleming83e68182012-11-14 09:42:35 +000081unsigned long x86_efi_facility;
82
83/*
84 * Returns 1 if 'facility' is enabled, 0 otherwise.
85 */
86int efi_enabled(int facility)
87{
88 return test_bit(facility, &x86_efi_facility) != 0;
89}
90EXPORT_SYMBOL(efi_enabled);
91
Matt Fleming058e7b52013-02-22 15:03:47 +000092static bool __initdata disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010093static int __init setup_noefi(char *arg)
94{
Matt Flemingfb834c72013-02-20 20:36:12 +000095 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010096 return 0;
97}
98early_param("noefi", setup_noefi);
99
Paul Jackson200001e2008-06-25 05:44:46 -0700100int add_efi_memmap;
101EXPORT_SYMBOL(add_efi_memmap);
102
103static int __init setup_add_efi_memmap(char *arg)
104{
105 add_efi_memmap = 1;
106 return 0;
107}
108early_param("add_efi_memmap", setup_add_efi_memmap);
109
Richard Weinberger8c58bf32013-04-17 01:00:53 +0200110static bool efi_no_storage_paranoia;
111
112static int __init setup_storage_paranoia(char *arg)
113{
114 efi_no_storage_paranoia = true;
115 return 0;
116}
117early_param("efi_no_storage_paranoia", setup_storage_paranoia);
118
Paul Jackson200001e2008-06-25 05:44:46 -0700119
Huang, Ying5b836832008-01-30 13:31:19 +0100120static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
121{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100122 unsigned long flags;
123 efi_status_t status;
124
125 spin_lock_irqsave(&rtc_lock, flags);
126 status = efi_call_virt2(get_time, tm, tc);
127 spin_unlock_irqrestore(&rtc_lock, flags);
128 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100129}
130
131static efi_status_t virt_efi_set_time(efi_time_t *tm)
132{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100133 unsigned long flags;
134 efi_status_t status;
135
136 spin_lock_irqsave(&rtc_lock, flags);
137 status = efi_call_virt1(set_time, tm);
138 spin_unlock_irqrestore(&rtc_lock, flags);
139 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100140}
141
142static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
143 efi_bool_t *pending,
144 efi_time_t *tm)
145{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100146 unsigned long flags;
147 efi_status_t status;
148
149 spin_lock_irqsave(&rtc_lock, flags);
150 status = efi_call_virt3(get_wakeup_time,
151 enabled, pending, tm);
152 spin_unlock_irqrestore(&rtc_lock, flags);
153 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100154}
155
156static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
157{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100158 unsigned long flags;
159 efi_status_t status;
160
161 spin_lock_irqsave(&rtc_lock, flags);
162 status = efi_call_virt2(set_wakeup_time,
163 enabled, tm);
164 spin_unlock_irqrestore(&rtc_lock, flags);
165 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100166}
167
168static efi_status_t virt_efi_get_variable(efi_char16_t *name,
169 efi_guid_t *vendor,
170 u32 *attr,
171 unsigned long *data_size,
172 void *data)
173{
174 return efi_call_virt5(get_variable,
175 name, vendor, attr,
176 data_size, data);
177}
178
179static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
180 efi_char16_t *name,
181 efi_guid_t *vendor)
182{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400183 return efi_call_virt3(get_next_variable,
184 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100185}
186
187static efi_status_t virt_efi_set_variable(efi_char16_t *name,
188 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400189 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100190 unsigned long data_size,
191 void *data)
192{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400193 return efi_call_virt5(set_variable,
194 name, vendor, attr,
195 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100196}
197
Matthew Garrett3b370232011-06-06 15:36:25 -0400198static efi_status_t virt_efi_query_variable_info(u32 attr,
199 u64 *storage_space,
200 u64 *remaining_space,
201 u64 *max_variable_size)
202{
203 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
204 return EFI_UNSUPPORTED;
205
206 return efi_call_virt4(query_variable_info, attr, storage_space,
207 remaining_space, max_variable_size);
208}
209
Huang, Ying5b836832008-01-30 13:31:19 +0100210static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
211{
212 return efi_call_virt1(get_next_high_mono_count, count);
213}
214
215static void virt_efi_reset_system(int reset_type,
216 efi_status_t status,
217 unsigned long data_size,
218 efi_char16_t *data)
219{
220 efi_call_virt4(reset_system, reset_type, status,
221 data_size, data);
222}
223
Matthew Garrett3b370232011-06-06 15:36:25 -0400224static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
225 unsigned long count,
226 unsigned long sg_list)
227{
228 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
229 return EFI_UNSUPPORTED;
230
231 return efi_call_virt3(update_capsule, capsules, count, sg_list);
232}
233
234static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
235 unsigned long count,
236 u64 *max_size,
237 int *reset_type)
238{
239 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
240 return EFI_UNSUPPORTED;
241
242 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
243 reset_type);
244}
245
Huang, Ying5b836832008-01-30 13:31:19 +0100246static efi_status_t __init phys_efi_set_virtual_address_map(
247 unsigned long memory_map_size,
248 unsigned long descriptor_size,
249 u32 descriptor_version,
250 efi_memory_desc_t *virtual_map)
251{
252 efi_status_t status;
253
254 efi_call_phys_prelog();
255 status = efi_call_phys4(efi_phys.set_virtual_address_map,
256 memory_map_size, descriptor_size,
257 descriptor_version, virtual_map);
258 efi_call_phys_epilog();
259 return status;
260}
261
Linus Torvalds11520e52012-12-15 15:15:24 -0800262static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
263 efi_time_cap_t *tc)
264{
265 unsigned long flags;
266 efi_status_t status;
267
268 spin_lock_irqsave(&rtc_lock, flags);
269 efi_call_phys_prelog();
270 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
271 virt_to_phys(tc));
272 efi_call_phys_epilog();
273 spin_unlock_irqrestore(&rtc_lock, flags);
274 return status;
275}
276
David Vrabel35651842013-05-13 18:56:06 +0100277int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100278{
David Vrabel35651842013-05-13 18:56:06 +0100279 unsigned long nowtime = now->tv_sec;
Huang, Ying5b836832008-01-30 13:31:19 +0100280 efi_status_t status;
281 efi_time_t eft;
282 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500283 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100284
285 status = efi.get_time(&eft, &cap);
286 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800287 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100288 return -1;
289 }
290
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500291 rtc_time_to_tm(nowtime, &tm);
292 if (!rtc_valid_tm(&tm)) {
293 eft.year = tm.tm_year + 1900;
294 eft.month = tm.tm_mon + 1;
295 eft.day = tm.tm_mday;
296 eft.minute = tm.tm_min;
297 eft.second = tm.tm_sec;
298 eft.nanosecond = 0;
299 } else {
300 printk(KERN_ERR
301 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
302 __FUNCTION__, nowtime);
303 return -1;
304 }
Huang, Ying5b836832008-01-30 13:31:19 +0100305
306 status = efi.set_time(&eft);
307 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800308 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100309 return -1;
310 }
311 return 0;
312}
313
David Vrabel35651842013-05-13 18:56:06 +0100314void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100315{
316 efi_status_t status;
317 efi_time_t eft;
318 efi_time_cap_t cap;
319
320 status = efi.get_time(&eft, &cap);
321 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800322 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100323
David Vrabel35651842013-05-13 18:56:06 +0100324 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
325 eft.minute, eft.second);
326 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100327}
328
Paul Jackson69c91892008-05-14 08:15:58 -0700329/*
330 * Tell the kernel about the EFI memory map. This might include
331 * more than the max 128 entries that can fit in the e820 legacy
332 * (zeropage) memory map.
333 */
334
Paul Jackson200001e2008-06-25 05:44:46 -0700335static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700336{
337 void *p;
338
339 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
340 efi_memory_desc_t *md = p;
341 unsigned long long start = md->phys_addr;
342 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
343 int e820_type;
344
Cliff Wickmane2a71472009-06-16 16:43:40 -0500345 switch (md->type) {
346 case EFI_LOADER_CODE:
347 case EFI_LOADER_DATA:
348 case EFI_BOOT_SERVICES_CODE:
349 case EFI_BOOT_SERVICES_DATA:
350 case EFI_CONVENTIONAL_MEMORY:
351 if (md->attribute & EFI_MEMORY_WB)
352 e820_type = E820_RAM;
353 else
354 e820_type = E820_RESERVED;
355 break;
356 case EFI_ACPI_RECLAIM_MEMORY:
357 e820_type = E820_ACPI;
358 break;
359 case EFI_ACPI_MEMORY_NVS:
360 e820_type = E820_NVS;
361 break;
362 case EFI_UNUSABLE_MEMORY:
363 e820_type = E820_UNUSABLE;
364 break;
365 default:
366 /*
367 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
368 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
369 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
370 */
Paul Jackson69c91892008-05-14 08:15:58 -0700371 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500372 break;
373 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700374 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700375 }
376 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
377}
378
Olof Johansson1adbfa32012-02-12 13:24:29 -0800379int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800380{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100381 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800382 unsigned long pmap;
383
Paul Jackson05486fa2008-06-22 07:22:02 -0700384#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800385 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100386 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800387 pr_err("Memory map is above 4GB, disabling EFI.\n");
388 return -EINVAL;
389 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100390 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700391#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100392 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800393#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100394 memmap.phys_map = (void *)pmap;
395 memmap.nr_map = e->efi_memmap_size /
396 e->efi_memdesc_size;
397 memmap.desc_size = e->efi_memdesc_size;
398 memmap.desc_version = e->efi_memdesc_version;
399
Tejun Heo24aa0782011-07-12 11:16:06 +0200400 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800401
402 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800403}
404
Huang, Ying5b836832008-01-30 13:31:19 +0100405#if EFI_DEBUG
406static void __init print_efi_memmap(void)
407{
408 efi_memory_desc_t *md;
409 void *p;
410 int i;
411
412 for (p = memmap.map, i = 0;
413 p < memmap.map_end;
414 p += memmap.desc_size, i++) {
415 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800416 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100417 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
418 i, md->type, md->attribute, md->phys_addr,
419 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
420 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
421 }
422}
423#endif /* EFI_DEBUG */
424
Matthew Garrett916f6762011-05-25 09:53:13 -0400425void __init efi_reserve_boot_services(void)
426{
427 void *p;
428
429 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
430 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200431 u64 start = md->phys_addr;
432 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400433
434 if (md->type != EFI_BOOT_SERVICES_CODE &&
435 md->type != EFI_BOOT_SERVICES_DATA)
436 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200437 /* Only reserve where possible:
438 * - Not within any already allocated areas
439 * - Not over any memory area (really needed, if above?)
440 * - Not within any part of the kernel
441 * - Not the bios reserved area
442 */
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800443 if ((start+size >= __pa_symbol(_text)
444 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200445 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200446 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200447 /* Could not reserve, skip it */
448 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800449 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200450 "[0x%010llx-0x%010llx]\n",
451 start, start+size-1);
452 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200453 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400454 }
455}
456
Olof Johansson5189c2a2012-10-24 10:00:44 -0700457void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700458{
Matt Fleming83e68182012-11-14 09:42:35 +0000459 clear_bit(EFI_MEMMAP, &x86_efi_facility);
Josh Triplett78510792012-09-28 17:55:44 -0700460 if (memmap.map) {
461 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
462 memmap.map = NULL;
463 }
464}
465
466void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400467{
468 void *p;
469
Olof Johansson5189c2a2012-10-24 10:00:44 -0700470 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700471 return;
472
Matthew Garrett916f6762011-05-25 09:53:13 -0400473 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
474 efi_memory_desc_t *md = p;
475 unsigned long long start = md->phys_addr;
476 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
477
478 if (md->type != EFI_BOOT_SERVICES_CODE &&
479 md->type != EFI_BOOT_SERVICES_DATA)
480 continue;
481
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200482 /* Could not reserve boot area */
483 if (!size)
484 continue;
485
Matthew Garrett916f6762011-05-25 09:53:13 -0400486 free_bootmem_late(start, size);
487 }
Josh Triplett78510792012-09-28 17:55:44 -0700488
489 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400490}
491
Olof Johansson140bf272012-02-12 13:24:28 -0800492static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100493{
Matt Fleming83e68182012-11-14 09:42:35 +0000494 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800495 efi_system_table_64_t *systab64;
496 u64 tmp = 0;
497
498 systab64 = early_ioremap((unsigned long)phys,
499 sizeof(*systab64));
500 if (systab64 == NULL) {
501 pr_err("Couldn't map the system table!\n");
502 return -ENOMEM;
503 }
504
505 efi_systab.hdr = systab64->hdr;
506 efi_systab.fw_vendor = systab64->fw_vendor;
507 tmp |= systab64->fw_vendor;
508 efi_systab.fw_revision = systab64->fw_revision;
509 efi_systab.con_in_handle = systab64->con_in_handle;
510 tmp |= systab64->con_in_handle;
511 efi_systab.con_in = systab64->con_in;
512 tmp |= systab64->con_in;
513 efi_systab.con_out_handle = systab64->con_out_handle;
514 tmp |= systab64->con_out_handle;
515 efi_systab.con_out = systab64->con_out;
516 tmp |= systab64->con_out;
517 efi_systab.stderr_handle = systab64->stderr_handle;
518 tmp |= systab64->stderr_handle;
519 efi_systab.stderr = systab64->stderr;
520 tmp |= systab64->stderr;
521 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
522 tmp |= systab64->runtime;
523 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
524 tmp |= systab64->boottime;
525 efi_systab.nr_tables = systab64->nr_tables;
526 efi_systab.tables = systab64->tables;
527 tmp |= systab64->tables;
528
529 early_iounmap(systab64, sizeof(*systab64));
530#ifdef CONFIG_X86_32
531 if (tmp >> 32) {
532 pr_err("EFI data located above 4GB, disabling EFI.\n");
533 return -EINVAL;
534 }
535#endif
536 } else {
537 efi_system_table_32_t *systab32;
538
539 systab32 = early_ioremap((unsigned long)phys,
540 sizeof(*systab32));
541 if (systab32 == NULL) {
542 pr_err("Couldn't map the system table!\n");
543 return -ENOMEM;
544 }
545
546 efi_systab.hdr = systab32->hdr;
547 efi_systab.fw_vendor = systab32->fw_vendor;
548 efi_systab.fw_revision = systab32->fw_revision;
549 efi_systab.con_in_handle = systab32->con_in_handle;
550 efi_systab.con_in = systab32->con_in;
551 efi_systab.con_out_handle = systab32->con_out_handle;
552 efi_systab.con_out = systab32->con_out;
553 efi_systab.stderr_handle = systab32->stderr_handle;
554 efi_systab.stderr = systab32->stderr;
555 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
556 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
557 efi_systab.nr_tables = systab32->nr_tables;
558 efi_systab.tables = systab32->tables;
559
560 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800561 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800562
Huang, Ying5b836832008-01-30 13:31:19 +0100563 efi.systab = &efi_systab;
564
565 /*
566 * Verify the EFI Table
567 */
Olof Johansson140bf272012-02-12 13:24:28 -0800568 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800569 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800570 return -EINVAL;
571 }
Huang, Ying5b836832008-01-30 13:31:19 +0100572 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800573 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100574 "%d.%02d, expected 1.00 or greater!\n",
575 efi.systab->hdr.revision >> 16,
576 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800577
578 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800579}
Huang, Ying5b836832008-01-30 13:31:19 +0100580
Olof Johansson140bf272012-02-12 13:24:28 -0800581static int __init efi_config_init(u64 tables, int nr_tables)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800582{
Olof Johansson1adbfa32012-02-12 13:24:29 -0800583 void *config_tables, *tablep;
584 int i, sz;
585
Matt Fleming83e68182012-11-14 09:42:35 +0000586 if (efi_enabled(EFI_64BIT))
Olof Johansson1adbfa32012-02-12 13:24:29 -0800587 sz = sizeof(efi_config_table_64_t);
588 else
589 sz = sizeof(efi_config_table_32_t);
Huang, Ying5b836832008-01-30 13:31:19 +0100590
591 /*
592 * Let's see what config tables the firmware passed to us.
593 */
Olof Johansson1adbfa32012-02-12 13:24:29 -0800594 config_tables = early_ioremap(tables, nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800595 if (config_tables == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800596 pr_err("Could not map Configuration table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800597 return -ENOMEM;
598 }
Huang, Ying5b836832008-01-30 13:31:19 +0100599
Olof Johansson1adbfa32012-02-12 13:24:29 -0800600 tablep = config_tables;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800601 pr_info("");
Huang, Ying5b836832008-01-30 13:31:19 +0100602 for (i = 0; i < efi.systab->nr_tables; i++) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800603 efi_guid_t guid;
604 unsigned long table;
Olof Johanssona6a46f42012-02-12 13:24:27 -0800605
Matt Fleming83e68182012-11-14 09:42:35 +0000606 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800607 u64 table64;
608 guid = ((efi_config_table_64_t *)tablep)->guid;
609 table64 = ((efi_config_table_64_t *)tablep)->table;
610 table = table64;
611#ifdef CONFIG_X86_32
612 if (table64 >> 32) {
613 pr_cont("\n");
614 pr_err("Table located above 4GB, disabling EFI.\n");
615 early_iounmap(config_tables,
616 efi.systab->nr_tables * sz);
617 return -EINVAL;
618 }
619#endif
620 } else {
621 guid = ((efi_config_table_32_t *)tablep)->guid;
622 table = ((efi_config_table_32_t *)tablep)->table;
623 }
Olof Johanssona6a46f42012-02-12 13:24:27 -0800624 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
625 efi.mps = table;
626 pr_cont(" MPS=0x%lx ", table);
627 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
628 efi.acpi20 = table;
629 pr_cont(" ACPI 2.0=0x%lx ", table);
630 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
631 efi.acpi = table;
632 pr_cont(" ACPI=0x%lx ", table);
633 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
634 efi.smbios = table;
635 pr_cont(" SMBIOS=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100636#ifdef CONFIG_X86_UV
Olof Johanssona6a46f42012-02-12 13:24:27 -0800637 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
638 efi.uv_systab = table;
639 pr_cont(" UVsystab=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100640#endif
Olof Johanssona6a46f42012-02-12 13:24:27 -0800641 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
642 efi.hcdp = table;
643 pr_cont(" HCDP=0x%lx ", table);
644 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
645 efi.uga = table;
646 pr_cont(" UGA=0x%lx ", table);
Huang, Ying5b836832008-01-30 13:31:19 +0100647 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800648 tablep += sz;
Huang, Ying5b836832008-01-30 13:31:19 +0100649 }
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800650 pr_cont("\n");
Olof Johanssona6a46f42012-02-12 13:24:27 -0800651 early_iounmap(config_tables, efi.systab->nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800652 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800653}
654
Olof Johansson140bf272012-02-12 13:24:28 -0800655static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800656{
657 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100658
659 /*
660 * Check out the runtime services table. We need to map
661 * the runtime services table so that we can grab the physical
662 * address of several of the EFI runtime functions, needed to
663 * set the firmware into virtual mode.
664 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100665 runtime = early_ioremap((unsigned long)efi.systab->runtime,
666 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800667 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800668 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800669 return -ENOMEM;
670 }
671 /*
672 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800673 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800674 * is invoked.
675 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800676 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800677 efi_phys.set_virtual_address_map =
678 (efi_set_virtual_address_map_t *)
679 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800680 /*
681 * Make efi_get_time can be called before entering
682 * virtual mode.
683 */
684 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100685 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800686
687 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800688}
Huang, Ying5b836832008-01-30 13:31:19 +0100689
Olof Johansson140bf272012-02-12 13:24:28 -0800690static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800691{
Huang, Ying5b836832008-01-30 13:31:19 +0100692 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100693 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
694 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800695 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800696 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800697 return -ENOMEM;
698 }
Huang, Ying5b836832008-01-30 13:31:19 +0100699 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500700
Paul Jackson200001e2008-06-25 05:44:46 -0700701 if (add_efi_memmap)
702 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800703
704 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800705}
706
707void __init efi_init(void)
708{
709 efi_char16_t *c16;
710 char vendor[100] = "unknown";
711 int i = 0;
712 void *tmp;
713
714#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800715 if (boot_params.efi_info.efi_systab_hi ||
716 boot_params.efi_info.efi_memmap_hi) {
717 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800718 return;
719 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800720 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
721#else
722 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800723 (boot_params.efi_info.efi_systab |
724 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800725#endif
726
Matt Fleming83e68182012-11-14 09:42:35 +0000727 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800728 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000729
730 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800731
732 /*
733 * Show what we know for posterity
734 */
735 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
736 if (c16) {
737 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
738 vendor[i] = *c16++;
739 vendor[i] = '\0';
740 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800741 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800742 early_iounmap(tmp, 2);
743
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800744 pr_info("EFI v%u.%.02u by %s\n",
745 efi.systab->hdr.revision >> 16,
746 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800747
Matt Fleming83e68182012-11-14 09:42:35 +0000748 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800749 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000750
751 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800752
Olof Johansson1adbfa32012-02-12 13:24:29 -0800753 /*
754 * Note: We currently don't support runtime services on an EFI
755 * that doesn't match the kernel 32/64-bit mode.
756 */
757
Olof Johansson5189c2a2012-10-24 10:00:44 -0700758 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800759 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000760 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000761 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000762 return;
763 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
Olof Johansson140bf272012-02-12 13:24:28 -0800764 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800765
Matt Fleming83e68182012-11-14 09:42:35 +0000766 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800767 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000768
769 set_bit(EFI_MEMMAP, &x86_efi_facility);
770
Linus Torvalds11520e52012-12-15 15:15:24 -0800771#ifdef CONFIG_X86_32
Olof Johansson5189c2a2012-10-24 10:00:44 -0700772 if (efi_is_native()) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800773 x86_platform.get_wallclock = efi_get_time;
774 x86_platform.set_wallclock = efi_set_rtc_mmss;
775 }
Linus Torvalds11520e52012-12-15 15:15:24 -0800776#endif
Feng Tang7bd867d2009-09-10 10:48:56 +0800777
Huang, Ying5b836832008-01-30 13:31:19 +0100778#if EFI_DEBUG
779 print_efi_memmap();
780#endif
781}
782
Josh Triplett2223af382012-09-28 17:57:05 -0700783void __init efi_late_init(void)
784{
785 efi_bgrt_init();
786}
787
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400788void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
789{
790 u64 addr, npages;
791
792 addr = md->virt_addr;
793 npages = md->num_pages;
794
795 memrange_efi_to_native(&addr, &npages);
796
797 if (executable)
798 set_memory_x(addr, npages);
799 else
800 set_memory_nx(addr, npages);
801}
802
Huang, Yinga2172e22008-01-30 13:33:55 +0100803static void __init runtime_code_page_mkexec(void)
804{
805 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100806 void *p;
807
Huang, Yinga2172e22008-01-30 13:33:55 +0100808 /* Make EFI runtime service code area executable */
809 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
810 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100811
812 if (md->type != EFI_RUNTIME_SERVICES_CODE)
813 continue;
814
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400815 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100816 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100817}
Huang, Yinga2172e22008-01-30 13:33:55 +0100818
Huang, Ying5b836832008-01-30 13:31:19 +0100819/*
Josh Triplett7bc90e012012-09-28 17:56:08 -0700820 * We can't ioremap data in EFI boot services RAM, because we've already mapped
821 * it as RAM. So, look it up in the existing EFI memory map instead. Only
822 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
823 */
824void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
825{
826 void *p;
827 if (WARN_ON(!memmap.map))
828 return NULL;
829 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
830 efi_memory_desc_t *md = p;
831 u64 size = md->num_pages << EFI_PAGE_SHIFT;
832 u64 end = md->phys_addr + size;
833 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
834 md->type != EFI_BOOT_SERVICES_CODE &&
835 md->type != EFI_BOOT_SERVICES_DATA)
836 continue;
837 if (!md->virt_addr)
838 continue;
839 if (phys_addr >= md->phys_addr && phys_addr < end) {
840 phys_addr += md->virt_addr - md->phys_addr;
841 return (__force void __iomem *)(unsigned long)phys_addr;
842 }
843 }
844 return NULL;
845}
846
Matt Fleming3e8fa262012-10-19 13:25:46 +0100847void efi_memory_uc(u64 addr, unsigned long size)
848{
849 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
850 u64 npages;
851
852 npages = round_up(size, page_shift) / page_shift;
853 memrange_efi_to_native(&addr, &npages);
854 set_memory_uc(addr, npages);
855}
856
Josh Triplett7bc90e012012-09-28 17:56:08 -0700857/*
Huang, Ying5b836832008-01-30 13:31:19 +0100858 * This function will switch the EFI runtime services to virtual mode.
859 * Essentially, look through the EFI memmap and map every region that
860 * has the runtime attribute bit set in its memory descriptor and update
861 * that memory descriptor with the virtual address obtained from ioremap().
862 * This enables the runtime services to be called without having to
863 * thunk back into physical mode for every invocation.
864 */
865void __init efi_enter_virtual_mode(void)
866{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400867 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100868 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100869 unsigned long size;
Jacob Shindda56e12012-11-16 19:38:48 -0800870 u64 end, systab, start_pfn, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400871 void *p, *va, *new_memmap = NULL;
872 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100873
874 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400875
Olof Johansson1adbfa32012-02-12 13:24:29 -0800876 /*
877 * We don't do virtual mode, since we don't do runtime services, on
878 * non-native EFI
879 */
880
Olof Johansson5189c2a2012-10-24 10:00:44 -0700881 if (!efi_is_native()) {
Josh Triplett78510792012-09-28 17:55:44 -0700882 efi_unmap_memmap();
883 return;
884 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800885
Matthew Garrett202f9d02011-05-05 15:19:44 -0400886 /* Merge contiguous regions of the same type and attribute */
887 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
888 u64 prev_size;
889 md = p;
890
891 if (!prev_md) {
892 prev_md = md;
893 continue;
894 }
895
896 if (prev_md->type != md->type ||
897 prev_md->attribute != md->attribute) {
898 prev_md = md;
899 continue;
900 }
901
902 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
903
904 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
905 prev_md->num_pages += md->num_pages;
906 md->type = EFI_RESERVED_TYPE;
907 md->attribute = 0;
908 continue;
909 }
910 prev_md = md;
911 }
912
Huang, Ying5b836832008-01-30 13:31:19 +0100913 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
914 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -0400915 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
916 md->type != EFI_BOOT_SERVICES_CODE &&
917 md->type != EFI_BOOT_SERVICES_DATA)
Huang, Ying5b836832008-01-30 13:31:19 +0100918 continue;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100919
920 size = md->num_pages << EFI_PAGE_SHIFT;
921 end = md->phys_addr + size;
922
Jacob Shindda56e12012-11-16 19:38:48 -0800923 start_pfn = PFN_DOWN(md->phys_addr);
Huang Yingdd39ecf2009-03-04 10:58:33 +0800924 end_pfn = PFN_UP(end);
Jacob Shindda56e12012-11-16 19:38:48 -0800925 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
Huang, Ying1c083eb2008-02-04 16:48:06 +0100926 va = __va(md->phys_addr);
Matt Fleming3e8fa262012-10-19 13:25:46 +0100927
928 if (!(md->attribute & EFI_MEMORY_WB))
929 efi_memory_uc((u64)(unsigned long)va, size);
930 } else
931 va = efi_ioremap(md->phys_addr, size,
932 md->type, md->attribute);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100933
Huang, Ying1c083eb2008-02-04 16:48:06 +0100934 md->virt_addr = (u64) (unsigned long) va;
935
936 if (!va) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800937 pr_err("ioremap of 0x%llX failed!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100938 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100939 continue;
940 }
941
942 systab = (u64) (unsigned long) efi_phys.systab;
943 if (md->phys_addr <= systab && systab < end) {
944 systab += md->virt_addr - md->phys_addr;
945 efi.systab = (efi_system_table_t *) (unsigned long) systab;
946 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400947 new_memmap = krealloc(new_memmap,
948 (count + 1) * memmap.desc_size,
949 GFP_KERNEL);
950 memcpy(new_memmap + (count * memmap.desc_size), md,
951 memmap.desc_size);
952 count++;
Huang, Ying5b836832008-01-30 13:31:19 +0100953 }
954
955 BUG_ON(!efi.systab);
956
957 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400958 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +0100959 memmap.desc_size,
960 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400961 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +0100962
963 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800964 pr_alert("Unable to switch EFI into virtual mode "
965 "(status=%lx)!\n", status);
Huang, Ying5b836832008-01-30 13:31:19 +0100966 panic("EFI call to SetVirtualAddressMap() failed!");
967 }
968
969 /*
970 * Now that EFI is in virtual mode, update the function
971 * pointers in the runtime service table to the new virtual addresses.
972 *
973 * Call EFI services through wrapper functions.
974 */
Matt Fleming712ba9e2013-01-25 10:07:25 +0000975 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +0100976 efi.get_time = virt_efi_get_time;
977 efi.set_time = virt_efi_set_time;
978 efi.get_wakeup_time = virt_efi_get_wakeup_time;
979 efi.set_wakeup_time = virt_efi_set_wakeup_time;
980 efi.get_variable = virt_efi_get_variable;
981 efi.get_next_variable = virt_efi_get_next_variable;
982 efi.set_variable = virt_efi_set_variable;
983 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
984 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400985 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -0400986 efi.query_variable_info = virt_efi_query_variable_info;
987 efi.update_capsule = virt_efi_update_capsule;
988 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Huang, Ying4de0d4a2008-02-13 17:22:41 +0800989 if (__supported_pte_mask & _PAGE_NX)
990 runtime_code_page_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -0800991
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400992 kfree(new_memmap);
Matthew Garrettf8b84042013-06-01 16:06:20 -0400993
994 /* clean DUMMY object */
995 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
996 EFI_VARIABLE_NON_VOLATILE |
997 EFI_VARIABLE_BOOTSERVICE_ACCESS |
998 EFI_VARIABLE_RUNTIME_ACCESS,
999 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001000}
1001
1002/*
1003 * Convenience functions to obtain memory types and attributes
1004 */
1005u32 efi_mem_type(unsigned long phys_addr)
1006{
1007 efi_memory_desc_t *md;
1008 void *p;
1009
Matt Fleming83e68182012-11-14 09:42:35 +00001010 if (!efi_enabled(EFI_MEMMAP))
1011 return 0;
1012
Huang, Ying5b836832008-01-30 13:31:19 +01001013 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1014 md = p;
1015 if ((md->phys_addr <= phys_addr) &&
1016 (phys_addr < (md->phys_addr +
1017 (md->num_pages << EFI_PAGE_SHIFT))))
1018 return md->type;
1019 }
1020 return 0;
1021}
1022
1023u64 efi_mem_attributes(unsigned long phys_addr)
1024{
1025 efi_memory_desc_t *md;
1026 void *p;
1027
1028 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1029 md = p;
1030 if ((md->phys_addr <= phys_addr) &&
1031 (phys_addr < (md->phys_addr +
1032 (md->num_pages << EFI_PAGE_SHIFT))))
1033 return md->attribute;
1034 }
1035 return 0;
1036}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001037
1038/*
1039 * Some firmware has serious problems when using more than 50% of the EFI
1040 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1041 * we never use more than this safe limit.
1042 *
1043 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1044 * store.
1045 */
1046efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1047{
1048 efi_status_t status;
1049 u64 storage_size, remaining_size, max_size;
1050
Matthew Garrettf8b84042013-06-01 16:06:20 -04001051 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1052 return 0;
1053
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001054 status = efi.query_variable_info(attributes, &storage_size,
1055 &remaining_size, &max_size);
1056 if (status != EFI_SUCCESS)
1057 return status;
1058
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001059 /*
1060 * Some firmware implementations refuse to boot if there's insufficient
1061 * space in the variable store. We account for that by refusing the
1062 * write if permitting it would reduce the available space to under
Matthew Garrettf8b84042013-06-01 16:06:20 -04001063 * 5KB. This figure was provided by Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001064 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001065 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1066 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001067
Matthew Garrettf8b84042013-06-01 16:06:20 -04001068 /*
1069 * Triggering garbage collection may require that the firmware
1070 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1071 * that by attempting to use more space than is available.
1072 */
1073 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001074 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1075
1076 if (!dummy)
1077 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001078
Matthew Garrettf8b84042013-06-01 16:06:20 -04001079 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1080 EFI_VARIABLE_NON_VOLATILE |
1081 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1082 EFI_VARIABLE_RUNTIME_ACCESS,
1083 dummy_size, dummy);
1084
1085 if (status == EFI_SUCCESS) {
1086 /*
1087 * This should have failed, so if it didn't make sure
1088 * that we delete it...
1089 */
1090 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1091 EFI_VARIABLE_NON_VOLATILE |
1092 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1093 EFI_VARIABLE_RUNTIME_ACCESS,
1094 0, dummy);
1095 }
1096
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001097 kfree(dummy);
1098
Matthew Garrettf8b84042013-06-01 16:06:20 -04001099 /*
1100 * The runtime code may now have triggered a garbage collection
1101 * run, so check the variable info again
1102 */
1103 status = efi.query_variable_info(attributes, &storage_size,
1104 &remaining_size, &max_size);
1105
1106 if (status != EFI_SUCCESS)
1107 return status;
1108
1109 /*
1110 * There still isn't enough room, so return an error
1111 */
1112 if (remaining_size - size < EFI_MIN_RESERVE)
1113 return EFI_OUT_OF_RESOURCES;
1114 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001115
1116 return EFI_SUCCESS;
1117}
Sergey Vlasov36680112013-04-16 18:31:09 +04001118EXPORT_SYMBOL_GPL(efi_query_variable_store);