blob: 39f5b7bba695b67fc17ce099dc4dc5b8f25aa037 [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>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010015 * Copyright (C) 2013 SuSE Labs
16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
Huang, Ying5b836832008-01-30 13:31:19 +010017 *
18 * Copied from efi_32.c to eliminate the duplicated code between EFI
19 * 32/64 support code. --ying 2007-10-26
20 *
21 * All EFI Runtime Services are not implemented yet as EFI only
22 * supports physical mode addressing on SoftSDV. This is to be fixed
23 * in a future version. --drummond 1999-07-20
24 *
25 * Implemented EFI runtime services and virtual mode calls. --davidm
26 *
27 * Goutham Rao: <goutham.rao@intel.com>
28 * Skip non-WB memory and ignore empty memory ranges.
29 */
30
Olof Johanssone3cb3f52012-02-12 13:24:26 -080031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Huang, Ying5b836832008-01-30 13:31:19 +010033#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/efi.h>
Josh Triplett2223af32012-09-28 17:57:05 -070036#include <linux/efi-bgrt.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040037#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010038#include <linux/bootmem.h>
David Howells0d01ff22013-04-11 23:51:01 +010039#include <linux/slab.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070040#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010041#include <linux/spinlock.h>
42#include <linux/uaccess.h>
43#include <linux/time.h>
44#include <linux/io.h>
45#include <linux/reboot.h>
46#include <linux/bcd.h>
47
48#include <asm/setup.h>
49#include <asm/efi.h>
50#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010051#include <asm/cacheflush.h>
52#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080053#include <asm/x86_init.h>
Prarit Bhargava3195ef52013-02-14 12:02:54 -050054#include <asm/rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010055
Borislav Petkovf4fccac2013-10-31 17:24:59 +010056#define EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +010057
Matthew Garrettf8b84042013-06-01 16:06:20 -040058#define EFI_MIN_RESERVE 5120
59
60#define EFI_DUMMY_GUID \
61 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
62
63static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
Matthew Garrett31ff2f22013-04-15 13:09:47 -070064
Huang, Ying5b836832008-01-30 13:31:19 +010065struct efi_memory_map memmap;
66
Harvey Harrisonecaea422008-02-13 13:26:13 -080067static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010068static efi_system_table_t efi_systab __initdata;
69
Matt Fleming83e68182012-11-14 09:42:35 +000070unsigned long x86_efi_facility;
71
Leif Lindholm272686b2013-09-05 11:34:54 +010072static __initdata efi_config_table_type_t arch_tables[] = {
73#ifdef CONFIG_X86_UV
74 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
75#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010076 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010077};
78
Dave Young1fec0532013-12-20 18:02:19 +080079u64 efi_setup; /* efi setup_data physical address */
Dave Young926172d2013-12-20 18:02:18 +080080
Matt Fleming83e68182012-11-14 09:42:35 +000081/*
82 * Returns 1 if 'facility' is enabled, 0 otherwise.
83 */
84int efi_enabled(int facility)
85{
86 return test_bit(facility, &x86_efi_facility) != 0;
87}
88EXPORT_SYMBOL(efi_enabled);
89
Matt Fleming058e7b52013-02-22 15:03:47 +000090static bool __initdata disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010091static int __init setup_noefi(char *arg)
92{
Matt Flemingfb834c72013-02-20 20:36:12 +000093 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010094 return 0;
95}
96early_param("noefi", setup_noefi);
97
Paul Jackson200001e2008-06-25 05:44:46 -070098int add_efi_memmap;
99EXPORT_SYMBOL(add_efi_memmap);
100
101static int __init setup_add_efi_memmap(char *arg)
102{
103 add_efi_memmap = 1;
104 return 0;
105}
106early_param("add_efi_memmap", setup_add_efi_memmap);
107
Richard Weinberger8c58bf32013-04-17 01:00:53 +0200108static bool efi_no_storage_paranoia;
109
110static int __init setup_storage_paranoia(char *arg)
111{
112 efi_no_storage_paranoia = true;
113 return 0;
114}
115early_param("efi_no_storage_paranoia", setup_storage_paranoia);
116
Huang, Ying5b836832008-01-30 13:31:19 +0100117static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
118{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100119 unsigned long flags;
120 efi_status_t status;
121
122 spin_lock_irqsave(&rtc_lock, flags);
123 status = efi_call_virt2(get_time, tm, tc);
124 spin_unlock_irqrestore(&rtc_lock, flags);
125 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100126}
127
128static efi_status_t virt_efi_set_time(efi_time_t *tm)
129{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100130 unsigned long flags;
131 efi_status_t status;
132
133 spin_lock_irqsave(&rtc_lock, flags);
134 status = efi_call_virt1(set_time, tm);
135 spin_unlock_irqrestore(&rtc_lock, flags);
136 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100137}
138
139static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
140 efi_bool_t *pending,
141 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_virt3(get_wakeup_time,
148 enabled, pending, 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_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
154{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100155 unsigned long flags;
156 efi_status_t status;
157
158 spin_lock_irqsave(&rtc_lock, flags);
159 status = efi_call_virt2(set_wakeup_time,
160 enabled, tm);
161 spin_unlock_irqrestore(&rtc_lock, flags);
162 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100163}
164
165static efi_status_t virt_efi_get_variable(efi_char16_t *name,
166 efi_guid_t *vendor,
167 u32 *attr,
168 unsigned long *data_size,
169 void *data)
170{
171 return efi_call_virt5(get_variable,
172 name, vendor, attr,
173 data_size, data);
174}
175
176static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
177 efi_char16_t *name,
178 efi_guid_t *vendor)
179{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400180 return efi_call_virt3(get_next_variable,
181 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100182}
183
184static efi_status_t virt_efi_set_variable(efi_char16_t *name,
185 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400186 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100187 unsigned long data_size,
188 void *data)
189{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400190 return efi_call_virt5(set_variable,
191 name, vendor, attr,
192 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100193}
194
Matthew Garrett3b370232011-06-06 15:36:25 -0400195static efi_status_t virt_efi_query_variable_info(u32 attr,
196 u64 *storage_space,
197 u64 *remaining_space,
198 u64 *max_variable_size)
199{
200 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
201 return EFI_UNSUPPORTED;
202
203 return efi_call_virt4(query_variable_info, attr, storage_space,
204 remaining_space, max_variable_size);
205}
206
Huang, Ying5b836832008-01-30 13:31:19 +0100207static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
208{
209 return efi_call_virt1(get_next_high_mono_count, count);
210}
211
212static void virt_efi_reset_system(int reset_type,
213 efi_status_t status,
214 unsigned long data_size,
215 efi_char16_t *data)
216{
217 efi_call_virt4(reset_system, reset_type, status,
218 data_size, data);
219}
220
Matthew Garrett3b370232011-06-06 15:36:25 -0400221static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
222 unsigned long count,
223 unsigned long sg_list)
224{
225 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
226 return EFI_UNSUPPORTED;
227
228 return efi_call_virt3(update_capsule, capsules, count, sg_list);
229}
230
231static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
232 unsigned long count,
233 u64 *max_size,
234 int *reset_type)
235{
236 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
237 return EFI_UNSUPPORTED;
238
239 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
240 reset_type);
241}
242
Huang, Ying5b836832008-01-30 13:31:19 +0100243static efi_status_t __init phys_efi_set_virtual_address_map(
244 unsigned long memory_map_size,
245 unsigned long descriptor_size,
246 u32 descriptor_version,
247 efi_memory_desc_t *virtual_map)
248{
249 efi_status_t status;
250
251 efi_call_phys_prelog();
252 status = efi_call_phys4(efi_phys.set_virtual_address_map,
253 memory_map_size, descriptor_size,
254 descriptor_version, virtual_map);
255 efi_call_phys_epilog();
256 return status;
257}
258
Linus Torvalds11520e52012-12-15 15:15:24 -0800259static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
260 efi_time_cap_t *tc)
261{
262 unsigned long flags;
263 efi_status_t status;
264
265 spin_lock_irqsave(&rtc_lock, flags);
266 efi_call_phys_prelog();
267 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
268 virt_to_phys(tc));
269 efi_call_phys_epilog();
270 spin_unlock_irqrestore(&rtc_lock, flags);
271 return status;
272}
273
David Vrabel35651842013-05-13 18:56:06 +0100274int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100275{
David Vrabel35651842013-05-13 18:56:06 +0100276 unsigned long nowtime = now->tv_sec;
Huang, Ying5b836832008-01-30 13:31:19 +0100277 efi_status_t status;
278 efi_time_t eft;
279 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500280 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100281
282 status = efi.get_time(&eft, &cap);
283 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800284 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100285 return -1;
286 }
287
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500288 rtc_time_to_tm(nowtime, &tm);
289 if (!rtc_valid_tm(&tm)) {
290 eft.year = tm.tm_year + 1900;
291 eft.month = tm.tm_mon + 1;
292 eft.day = tm.tm_mday;
293 eft.minute = tm.tm_min;
294 eft.second = tm.tm_sec;
295 eft.nanosecond = 0;
296 } else {
297 printk(KERN_ERR
298 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
299 __FUNCTION__, nowtime);
300 return -1;
301 }
Huang, Ying5b836832008-01-30 13:31:19 +0100302
303 status = efi.set_time(&eft);
304 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800305 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100306 return -1;
307 }
308 return 0;
309}
310
David Vrabel35651842013-05-13 18:56:06 +0100311void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100312{
313 efi_status_t status;
314 efi_time_t eft;
315 efi_time_cap_t cap;
316
317 status = efi.get_time(&eft, &cap);
318 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800319 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100320
David Vrabel35651842013-05-13 18:56:06 +0100321 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
322 eft.minute, eft.second);
323 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100324}
325
Paul Jackson69c91892008-05-14 08:15:58 -0700326/*
327 * Tell the kernel about the EFI memory map. This might include
328 * more than the max 128 entries that can fit in the e820 legacy
329 * (zeropage) memory map.
330 */
331
Paul Jackson200001e2008-06-25 05:44:46 -0700332static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700333{
334 void *p;
335
336 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
337 efi_memory_desc_t *md = p;
338 unsigned long long start = md->phys_addr;
339 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
340 int e820_type;
341
Cliff Wickmane2a71472009-06-16 16:43:40 -0500342 switch (md->type) {
343 case EFI_LOADER_CODE:
344 case EFI_LOADER_DATA:
345 case EFI_BOOT_SERVICES_CODE:
346 case EFI_BOOT_SERVICES_DATA:
347 case EFI_CONVENTIONAL_MEMORY:
348 if (md->attribute & EFI_MEMORY_WB)
349 e820_type = E820_RAM;
350 else
351 e820_type = E820_RESERVED;
352 break;
353 case EFI_ACPI_RECLAIM_MEMORY:
354 e820_type = E820_ACPI;
355 break;
356 case EFI_ACPI_MEMORY_NVS:
357 e820_type = E820_NVS;
358 break;
359 case EFI_UNUSABLE_MEMORY:
360 e820_type = E820_UNUSABLE;
361 break;
362 default:
363 /*
364 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
365 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
366 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
367 */
Paul Jackson69c91892008-05-14 08:15:58 -0700368 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500369 break;
370 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700371 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700372 }
373 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
374}
375
Olof Johansson1adbfa32012-02-12 13:24:29 -0800376int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800377{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100378 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800379 unsigned long pmap;
380
Paul Jackson05486fa2008-06-22 07:22:02 -0700381#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800382 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100383 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800384 pr_err("Memory map is above 4GB, disabling EFI.\n");
385 return -EINVAL;
386 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100387 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700388#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100389 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800390#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100391 memmap.phys_map = (void *)pmap;
392 memmap.nr_map = e->efi_memmap_size /
393 e->efi_memdesc_size;
394 memmap.desc_size = e->efi_memdesc_size;
395 memmap.desc_version = e->efi_memdesc_version;
396
Tejun Heo24aa0782011-07-12 11:16:06 +0200397 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800398
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100399 efi.memmap = &memmap;
400
Olof Johansson1adbfa32012-02-12 13:24:29 -0800401 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800402}
403
Huang, Ying5b836832008-01-30 13:31:19 +0100404static void __init print_efi_memmap(void)
405{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100406#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100407 efi_memory_desc_t *md;
408 void *p;
409 int i;
410
411 for (p = memmap.map, i = 0;
412 p < memmap.map_end;
413 p += memmap.desc_size, i++) {
414 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800415 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100416 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
417 i, md->type, md->attribute, md->phys_addr,
418 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
419 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
420 }
Huang, Ying5b836832008-01-30 13:31:19 +0100421#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100422}
Huang, Ying5b836832008-01-30 13:31:19 +0100423
Matthew Garrett916f6762011-05-25 09:53:13 -0400424void __init efi_reserve_boot_services(void)
425{
426 void *p;
427
428 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
429 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200430 u64 start = md->phys_addr;
431 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400432
433 if (md->type != EFI_BOOT_SERVICES_CODE &&
434 md->type != EFI_BOOT_SERVICES_DATA)
435 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200436 /* Only reserve where possible:
437 * - Not within any already allocated areas
438 * - Not over any memory area (really needed, if above?)
439 * - Not within any part of the kernel
440 * - Not the bios reserved area
441 */
Dave Younga7f84f02013-12-20 18:02:15 +0800442 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800443 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200444 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200445 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200446 /* Could not reserve, skip it */
447 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800448 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200449 "[0x%010llx-0x%010llx]\n",
450 start, start+size-1);
451 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200452 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400453 }
454}
455
Olof Johansson5189c2a2012-10-24 10:00:44 -0700456void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700457{
Matt Fleming83e68182012-11-14 09:42:35 +0000458 clear_bit(EFI_MEMMAP, &x86_efi_facility);
Josh Triplett78510792012-09-28 17:55:44 -0700459 if (memmap.map) {
460 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
461 memmap.map = NULL;
462 }
463}
464
465void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400466{
467 void *p;
468
469 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
470 efi_memory_desc_t *md = p;
471 unsigned long long start = md->phys_addr;
472 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
473
474 if (md->type != EFI_BOOT_SERVICES_CODE &&
475 md->type != EFI_BOOT_SERVICES_DATA)
476 continue;
477
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200478 /* Could not reserve boot area */
479 if (!size)
480 continue;
481
Matthew Garrett916f6762011-05-25 09:53:13 -0400482 free_bootmem_late(start, size);
483 }
Josh Triplett78510792012-09-28 17:55:44 -0700484
485 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400486}
487
Olof Johansson140bf272012-02-12 13:24:28 -0800488static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100489{
Matt Fleming83e68182012-11-14 09:42:35 +0000490 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800491 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800492 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800493 u64 tmp = 0;
494
Dave Young1fec0532013-12-20 18:02:19 +0800495 if (efi_setup) {
496 data = early_memremap(efi_setup, sizeof(*data));
497 if (!data)
498 return -ENOMEM;
499 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800500 systab64 = early_ioremap((unsigned long)phys,
501 sizeof(*systab64));
502 if (systab64 == NULL) {
503 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800504 if (data)
505 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800506 return -ENOMEM;
507 }
508
509 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800510 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
511 systab64->fw_vendor;
512 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800513 efi_systab.fw_revision = systab64->fw_revision;
514 efi_systab.con_in_handle = systab64->con_in_handle;
515 tmp |= systab64->con_in_handle;
516 efi_systab.con_in = systab64->con_in;
517 tmp |= systab64->con_in;
518 efi_systab.con_out_handle = systab64->con_out_handle;
519 tmp |= systab64->con_out_handle;
520 efi_systab.con_out = systab64->con_out;
521 tmp |= systab64->con_out;
522 efi_systab.stderr_handle = systab64->stderr_handle;
523 tmp |= systab64->stderr_handle;
524 efi_systab.stderr = systab64->stderr;
525 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800526 efi_systab.runtime = data ?
527 (void *)(unsigned long)data->runtime :
528 (void *)(unsigned long)systab64->runtime;
529 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800530 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
531 tmp |= systab64->boottime;
532 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800533 efi_systab.tables = data ? (unsigned long)data->tables :
534 systab64->tables;
535 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800536
537 early_iounmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800538 if (data)
539 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800540#ifdef CONFIG_X86_32
541 if (tmp >> 32) {
542 pr_err("EFI data located above 4GB, disabling EFI.\n");
543 return -EINVAL;
544 }
545#endif
546 } else {
547 efi_system_table_32_t *systab32;
548
549 systab32 = early_ioremap((unsigned long)phys,
550 sizeof(*systab32));
551 if (systab32 == NULL) {
552 pr_err("Couldn't map the system table!\n");
553 return -ENOMEM;
554 }
555
556 efi_systab.hdr = systab32->hdr;
557 efi_systab.fw_vendor = systab32->fw_vendor;
558 efi_systab.fw_revision = systab32->fw_revision;
559 efi_systab.con_in_handle = systab32->con_in_handle;
560 efi_systab.con_in = systab32->con_in;
561 efi_systab.con_out_handle = systab32->con_out_handle;
562 efi_systab.con_out = systab32->con_out;
563 efi_systab.stderr_handle = systab32->stderr_handle;
564 efi_systab.stderr = systab32->stderr;
565 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
566 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
567 efi_systab.nr_tables = systab32->nr_tables;
568 efi_systab.tables = systab32->tables;
569
570 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800571 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800572
Huang, Ying5b836832008-01-30 13:31:19 +0100573 efi.systab = &efi_systab;
574
575 /*
576 * Verify the EFI Table
577 */
Olof Johansson140bf272012-02-12 13:24:28 -0800578 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800579 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800580 return -EINVAL;
581 }
Huang, Ying5b836832008-01-30 13:31:19 +0100582 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800583 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100584 "%d.%02d, expected 1.00 or greater!\n",
585 efi.systab->hdr.revision >> 16,
586 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800587
588 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800589}
Huang, Ying5b836832008-01-30 13:31:19 +0100590
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000591static int __init efi_runtime_init32(void)
592{
593 efi_runtime_services_32_t *runtime;
594
595 runtime = early_ioremap((unsigned long)efi.systab->runtime,
596 sizeof(efi_runtime_services_32_t));
597 if (!runtime) {
598 pr_err("Could not map the runtime service table!\n");
599 return -ENOMEM;
600 }
601
602 /*
603 * We will only need *early* access to the following two
604 * EFI runtime services before set_virtual_address_map
605 * is invoked.
606 */
607 efi_phys.get_time = (efi_get_time_t *)
608 (unsigned long)runtime->get_time;
609 efi_phys.set_virtual_address_map =
610 (efi_set_virtual_address_map_t *)
611 (unsigned long)runtime->set_virtual_address_map;
612 /*
613 * Make efi_get_time can be called before entering
614 * virtual mode.
615 */
616 efi.get_time = phys_efi_get_time;
617 early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
618
619 return 0;
620}
621
622static int __init efi_runtime_init64(void)
623{
624 efi_runtime_services_64_t *runtime;
625
626 runtime = early_ioremap((unsigned long)efi.systab->runtime,
627 sizeof(efi_runtime_services_64_t));
628 if (!runtime) {
629 pr_err("Could not map the runtime service table!\n");
630 return -ENOMEM;
631 }
632
633 /*
634 * We will only need *early* access to the following two
635 * EFI runtime services before set_virtual_address_map
636 * is invoked.
637 */
638 efi_phys.get_time = (efi_get_time_t *)
639 (unsigned long)runtime->get_time;
640 efi_phys.set_virtual_address_map =
641 (efi_set_virtual_address_map_t *)
642 (unsigned long)runtime->set_virtual_address_map;
643 /*
644 * Make efi_get_time can be called before entering
645 * virtual mode.
646 */
647 efi.get_time = phys_efi_get_time;
648 early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
649
650 return 0;
651}
652
Olof Johansson140bf272012-02-12 13:24:28 -0800653static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800654{
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000655 int rv;
Huang, Ying5b836832008-01-30 13:31:19 +0100656
657 /*
658 * Check out the runtime services table. We need to map
659 * the runtime services table so that we can grab the physical
660 * address of several of the EFI runtime functions, needed to
661 * set the firmware into virtual mode.
662 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000663 if (efi_enabled(EFI_64BIT))
664 rv = efi_runtime_init64();
665 else
666 rv = efi_runtime_init32();
667
668 if (rv)
669 return rv;
Olof Johansson140bf272012-02-12 13:24:28 -0800670
671 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800672}
Huang, Ying5b836832008-01-30 13:31:19 +0100673
Olof Johansson140bf272012-02-12 13:24:28 -0800674static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800675{
Huang, Ying5b836832008-01-30 13:31:19 +0100676 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100677 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
678 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800679 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800680 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800681 return -ENOMEM;
682 }
Huang, Ying5b836832008-01-30 13:31:19 +0100683 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500684
Paul Jackson200001e2008-06-25 05:44:46 -0700685 if (add_efi_memmap)
686 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800687
688 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800689}
690
Dave Young1fec0532013-12-20 18:02:19 +0800691/*
692 * A number of config table entries get remapped to virtual addresses
693 * after entering EFI virtual mode. However, the kexec kernel requires
694 * their physical addresses therefore we pass them via setup_data and
695 * correct those entries to their respective physical addresses here.
696 *
697 * Currently only handles smbios which is necessary for some firmware
698 * implementation.
699 */
700static int __init efi_reuse_config(u64 tables, int nr_tables)
701{
702 int i, sz, ret = 0;
703 void *p, *tablep;
704 struct efi_setup_data *data;
705
706 if (!efi_setup)
707 return 0;
708
709 if (!efi_enabled(EFI_64BIT))
710 return 0;
711
712 data = early_memremap(efi_setup, sizeof(*data));
713 if (!data) {
714 ret = -ENOMEM;
715 goto out;
716 }
717
718 if (!data->smbios)
719 goto out_memremap;
720
721 sz = sizeof(efi_config_table_64_t);
722
723 p = tablep = early_memremap(tables, nr_tables * sz);
724 if (!p) {
725 pr_err("Could not map Configuration table!\n");
726 ret = -ENOMEM;
727 goto out_memremap;
728 }
729
730 for (i = 0; i < efi.systab->nr_tables; i++) {
731 efi_guid_t guid;
732
733 guid = ((efi_config_table_64_t *)p)->guid;
734
735 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
736 ((efi_config_table_64_t *)p)->table = data->smbios;
737 p += sz;
738 }
739 early_iounmap(tablep, nr_tables * sz);
740
741out_memremap:
742 early_iounmap(data, sizeof(*data));
743out:
744 return ret;
745}
746
Olof Johansson83e7ee62012-02-12 13:24:25 -0800747void __init efi_init(void)
748{
749 efi_char16_t *c16;
750 char vendor[100] = "unknown";
751 int i = 0;
752 void *tmp;
753
754#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800755 if (boot_params.efi_info.efi_systab_hi ||
756 boot_params.efi_info.efi_memmap_hi) {
757 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800758 return;
759 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800760 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
761#else
762 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800763 (boot_params.efi_info.efi_systab |
764 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800765#endif
766
Matt Fleming83e68182012-11-14 09:42:35 +0000767 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800768 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000769
770 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800771
Dave Younga0998eb2013-12-20 18:02:17 +0800772 efi.config_table = (unsigned long)efi.systab->tables;
773 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
774 efi.runtime = (unsigned long)efi.systab->runtime;
775
Olof Johansson83e7ee62012-02-12 13:24:25 -0800776 /*
777 * Show what we know for posterity
778 */
779 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
780 if (c16) {
781 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
782 vendor[i] = *c16++;
783 vendor[i] = '\0';
784 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800785 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800786 early_iounmap(tmp, 2);
787
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800788 pr_info("EFI v%u.%.02u by %s\n",
789 efi.systab->hdr.revision >> 16,
790 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800791
Dave Young1fec0532013-12-20 18:02:19 +0800792 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
793 return;
794
Leif Lindholm272686b2013-09-05 11:34:54 +0100795 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800796 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000797
798 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800799
Olof Johansson1adbfa32012-02-12 13:24:29 -0800800 /*
801 * Note: We currently don't support runtime services on an EFI
802 * that doesn't match the kernel 32/64-bit mode.
803 */
804
Olof Johansson5189c2a2012-10-24 10:00:44 -0700805 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800806 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000807 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000808 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000809 return;
810 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
Olof Johansson140bf272012-02-12 13:24:28 -0800811 }
Matt Fleming83e68182012-11-14 09:42:35 +0000812 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800813 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000814
815 set_bit(EFI_MEMMAP, &x86_efi_facility);
816
Huang, Ying5b836832008-01-30 13:31:19 +0100817 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100818}
819
Josh Triplett2223af32012-09-28 17:57:05 -0700820void __init efi_late_init(void)
821{
822 efi_bgrt_init();
823}
824
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400825void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
826{
827 u64 addr, npages;
828
829 addr = md->virt_addr;
830 npages = md->num_pages;
831
832 memrange_efi_to_native(&addr, &npages);
833
834 if (executable)
835 set_memory_x(addr, npages);
836 else
837 set_memory_nx(addr, npages);
838}
839
Borislav Petkovc55d0162014-02-14 08:24:24 +0100840void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100841{
842 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100843 void *p;
844
Huang, Yinga2172e22008-01-30 13:33:55 +0100845 /* Make EFI runtime service code area executable */
846 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
847 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100848
849 if (md->type != EFI_RUNTIME_SERVICES_CODE)
850 continue;
851
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400852 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100853 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100854}
Huang, Yinga2172e22008-01-30 13:33:55 +0100855
Matt Fleming3e8fa262012-10-19 13:25:46 +0100856void efi_memory_uc(u64 addr, unsigned long size)
857{
858 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
859 u64 npages;
860
861 npages = round_up(size, page_shift) / page_shift;
862 memrange_efi_to_native(&addr, &npages);
863 set_memory_uc(addr, npages);
864}
865
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100866void __init old_map_region(efi_memory_desc_t *md)
867{
868 u64 start_pfn, end_pfn, end;
869 unsigned long size;
870 void *va;
871
872 start_pfn = PFN_DOWN(md->phys_addr);
873 size = md->num_pages << PAGE_SHIFT;
874 end = md->phys_addr + size;
875 end_pfn = PFN_UP(end);
876
877 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
878 va = __va(md->phys_addr);
879
880 if (!(md->attribute & EFI_MEMORY_WB))
881 efi_memory_uc((u64)(unsigned long)va, size);
882 } else
883 va = efi_ioremap(md->phys_addr, size,
884 md->type, md->attribute);
885
886 md->virt_addr = (u64) (unsigned long) va;
887 if (!va)
888 pr_err("ioremap of 0x%llX failed!\n",
889 (unsigned long long)md->phys_addr);
890}
891
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000892static void native_runtime_setup(void)
893{
894 efi.get_time = virt_efi_get_time;
895 efi.set_time = virt_efi_set_time;
896 efi.get_wakeup_time = virt_efi_get_wakeup_time;
897 efi.set_wakeup_time = virt_efi_set_wakeup_time;
898 efi.get_variable = virt_efi_get_variable;
899 efi.get_next_variable = virt_efi_get_next_variable;
900 efi.set_variable = virt_efi_set_variable;
901 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
902 efi.reset_system = virt_efi_reset_system;
903 efi.query_variable_info = virt_efi_query_variable_info;
904 efi.update_capsule = virt_efi_update_capsule;
905 efi.query_capsule_caps = virt_efi_query_capsule_caps;
906}
907
Dave Young481f75c2013-12-20 18:02:16 +0800908/* Merge contiguous regions of the same type and attribute */
909static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100910{
Dave Young481f75c2013-12-20 18:02:16 +0800911 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400912 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100913
Matthew Garrett202f9d02011-05-05 15:19:44 -0400914 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
915 u64 prev_size;
916 md = p;
917
918 if (!prev_md) {
919 prev_md = md;
920 continue;
921 }
922
923 if (prev_md->type != md->type ||
924 prev_md->attribute != md->attribute) {
925 prev_md = md;
926 continue;
927 }
928
929 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
930
931 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
932 prev_md->num_pages += md->num_pages;
933 md->type = EFI_RESERVED_TYPE;
934 md->attribute = 0;
935 continue;
936 }
937 prev_md = md;
938 }
Dave Young481f75c2013-12-20 18:02:16 +0800939}
940
941static void __init get_systab_virt_addr(efi_memory_desc_t *md)
942{
943 unsigned long size;
944 u64 end, systab;
945
946 size = md->num_pages << EFI_PAGE_SHIFT;
947 end = md->phys_addr + size;
948 systab = (u64)(unsigned long)efi_phys.systab;
949 if (md->phys_addr <= systab && systab < end) {
950 systab += md->virt_addr - md->phys_addr;
951 efi.systab = (efi_system_table_t *)(unsigned long)systab;
952 }
953}
954
Dave Young926172d2013-12-20 18:02:18 +0800955static int __init save_runtime_map(void)
956{
957 efi_memory_desc_t *md;
958 void *tmp, *p, *q = NULL;
959 int count = 0;
960
961 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
962 md = p;
963
964 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
965 (md->type == EFI_BOOT_SERVICES_CODE) ||
966 (md->type == EFI_BOOT_SERVICES_DATA))
967 continue;
968 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
969 if (!tmp)
970 goto out;
971 q = tmp;
972
973 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
974 count++;
975 }
976
Matt Fleming518548a2013-12-21 16:09:46 +0000977 efi_runtime_map_setup(q, count, memmap.desc_size);
Dave Young926172d2013-12-20 18:02:18 +0800978
979 return 0;
980out:
981 kfree(q);
982 return -ENOMEM;
983}
984
Dave Young481f75c2013-12-20 18:02:16 +0800985/*
Dave Young1fec0532013-12-20 18:02:19 +0800986 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
987 * addr which was used in first kernel of a kexec boot.
988 */
989static void __init efi_map_regions_fixed(void)
990{
991 void *p;
992 efi_memory_desc_t *md;
993
994 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
995 md = p;
996 efi_map_region_fixed(md); /* FIXME: add error handling */
997 get_systab_virt_addr(md);
998 }
999
1000}
1001
1002/*
Dave Young481f75c2013-12-20 18:02:16 +08001003 * Map efi memory ranges for runtime serivce and update new_memmap with virtual
1004 * addresses.
1005 */
1006static void * __init efi_map_regions(int *count)
1007{
1008 efi_memory_desc_t *md;
1009 void *p, *tmp, *new_memmap = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -04001010
Huang, Ying5b836832008-01-30 13:31:19 +01001011 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1012 md = p;
Josh Boyer70087012013-04-18 07:51:34 -07001013 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
1014#ifdef CONFIG_X86_64
1015 if (md->type != EFI_BOOT_SERVICES_CODE &&
1016 md->type != EFI_BOOT_SERVICES_DATA)
1017#endif
1018 continue;
1019 }
Huang, Ying1c083eb2008-02-04 16:48:06 +01001020
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001021 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +08001022 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001023
Dave Young481f75c2013-12-20 18:02:16 +08001024 tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size,
1025 GFP_KERNEL);
1026 if (!tmp)
Dave Young926172d2013-12-20 18:02:18 +08001027 goto out;
Dave Young481f75c2013-12-20 18:02:16 +08001028 new_memmap = tmp;
1029 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001030 memmap.desc_size);
Dave Young481f75c2013-12-20 18:02:16 +08001031 (*count)++;
1032 }
1033
1034 return new_memmap;
Dave Young926172d2013-12-20 18:02:18 +08001035out:
Dave Young481f75c2013-12-20 18:02:16 +08001036 kfree(new_memmap);
1037 return NULL;
1038}
1039
1040/*
1041 * This function will switch the EFI runtime services to virtual mode.
1042 * Essentially, we look through the EFI memmap and map every region that
1043 * has the runtime attribute bit set in its memory descriptor into the
1044 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1045 *
1046 * The old method which used to update that memory descriptor with the
1047 * virtual address obtained from ioremap() is still supported when the
1048 * kernel is booted with efi=old_map on its command line. Same old
1049 * method enabled the runtime services to be called without having to
1050 * thunk back into physical mode for every invocation.
1051 *
1052 * The new method does a pagetable switch in a preemption-safe manner
1053 * so that we're in a different address space when calling a runtime
1054 * function. For function arguments passing we do copy the PGDs of the
1055 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +08001056 *
1057 * Specially for kexec boot, efi runtime maps in previous kernel should
1058 * be passed in via setup_data. In that case runtime ranges will be mapped
1059 * to the same virtual addresses as the first kernel.
Dave Young481f75c2013-12-20 18:02:16 +08001060 */
1061void __init efi_enter_virtual_mode(void)
1062{
1063 efi_status_t status;
1064 void *new_memmap = NULL;
Dave Young926172d2013-12-20 18:02:18 +08001065 int err, count = 0;
Dave Young481f75c2013-12-20 18:02:16 +08001066
1067 efi.systab = NULL;
1068
Dave Young1fec0532013-12-20 18:02:19 +08001069 if (efi_setup) {
1070 efi_map_regions_fixed();
1071 } else {
1072 efi_merge_regions();
1073 new_memmap = efi_map_regions(&count);
1074 if (!new_memmap) {
1075 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1076 return;
1077 }
Huang, Ying5b836832008-01-30 13:31:19 +01001078 }
1079
Dave Young926172d2013-12-20 18:02:18 +08001080 err = save_runtime_map();
1081 if (err)
1082 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1083
Huang, Ying5b836832008-01-30 13:31:19 +01001084 BUG_ON(!efi.systab);
1085
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001086 efi_setup_page_tables();
1087 efi_sync_low_kernel_mappings();
1088
Dave Young1fec0532013-12-20 18:02:19 +08001089 if (!efi_setup) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +00001090 if (efi_is_native()) {
1091 status = phys_efi_set_virtual_address_map(
1092 memmap.desc_size * count,
1093 memmap.desc_size,
1094 memmap.desc_version,
1095 (efi_memory_desc_t *)__pa(new_memmap));
1096 } else {
1097 status = efi_thunk_set_virtual_address_map(
1098 efi_phys.set_virtual_address_map,
1099 memmap.desc_size * count,
1100 memmap.desc_size,
1101 memmap.desc_version,
1102 (efi_memory_desc_t *)__pa(new_memmap));
1103 }
Huang, Ying5b836832008-01-30 13:31:19 +01001104
Dave Young1fec0532013-12-20 18:02:19 +08001105 if (status != EFI_SUCCESS) {
1106 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1107 status);
1108 panic("EFI call to SetVirtualAddressMap() failed!");
1109 }
Huang, Ying5b836832008-01-30 13:31:19 +01001110 }
1111
1112 /*
1113 * Now that EFI is in virtual mode, update the function
1114 * pointers in the runtime service table to the new virtual addresses.
1115 *
1116 * Call EFI services through wrapper functions.
1117 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001118 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +00001119
1120 if (efi_is_native())
1121 native_runtime_setup();
1122 else
1123 efi_thunk_runtime_setup();
1124
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001125 efi.set_virtual_address_map = NULL;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001126
Borislav Petkovc55d0162014-02-14 08:24:24 +01001127 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001128
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001129 kfree(new_memmap);
Matthew Garrettf8b84042013-06-01 16:06:20 -04001130
1131 /* clean DUMMY object */
1132 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1133 EFI_VARIABLE_NON_VOLATILE |
1134 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1135 EFI_VARIABLE_RUNTIME_ACCESS,
1136 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001137}
1138
1139/*
1140 * Convenience functions to obtain memory types and attributes
1141 */
1142u32 efi_mem_type(unsigned long phys_addr)
1143{
1144 efi_memory_desc_t *md;
1145 void *p;
1146
Matt Fleming83e68182012-11-14 09:42:35 +00001147 if (!efi_enabled(EFI_MEMMAP))
1148 return 0;
1149
Huang, Ying5b836832008-01-30 13:31:19 +01001150 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1151 md = p;
1152 if ((md->phys_addr <= phys_addr) &&
1153 (phys_addr < (md->phys_addr +
1154 (md->num_pages << EFI_PAGE_SHIFT))))
1155 return md->type;
1156 }
1157 return 0;
1158}
1159
1160u64 efi_mem_attributes(unsigned long phys_addr)
1161{
1162 efi_memory_desc_t *md;
1163 void *p;
1164
1165 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1166 md = p;
1167 if ((md->phys_addr <= phys_addr) &&
1168 (phys_addr < (md->phys_addr +
1169 (md->num_pages << EFI_PAGE_SHIFT))))
1170 return md->attribute;
1171 }
1172 return 0;
1173}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001174
1175/*
1176 * Some firmware has serious problems when using more than 50% of the EFI
1177 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1178 * we never use more than this safe limit.
1179 *
1180 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1181 * store.
1182 */
1183efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1184{
1185 efi_status_t status;
1186 u64 storage_size, remaining_size, max_size;
1187
Matthew Garrettf8b84042013-06-01 16:06:20 -04001188 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1189 return 0;
1190
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001191 status = efi.query_variable_info(attributes, &storage_size,
1192 &remaining_size, &max_size);
1193 if (status != EFI_SUCCESS)
1194 return status;
1195
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001196 /*
1197 * Some firmware implementations refuse to boot if there's insufficient
1198 * space in the variable store. We account for that by refusing the
1199 * write if permitting it would reduce the available space to under
Matthew Garrettf8b84042013-06-01 16:06:20 -04001200 * 5KB. This figure was provided by Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001201 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001202 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1203 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001204
Matthew Garrettf8b84042013-06-01 16:06:20 -04001205 /*
1206 * Triggering garbage collection may require that the firmware
1207 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1208 * that by attempting to use more space than is available.
1209 */
1210 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001211 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1212
1213 if (!dummy)
1214 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001215
Matthew Garrettf8b84042013-06-01 16:06:20 -04001216 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1217 EFI_VARIABLE_NON_VOLATILE |
1218 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1219 EFI_VARIABLE_RUNTIME_ACCESS,
1220 dummy_size, dummy);
1221
1222 if (status == EFI_SUCCESS) {
1223 /*
1224 * This should have failed, so if it didn't make sure
1225 * that we delete it...
1226 */
1227 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1228 EFI_VARIABLE_NON_VOLATILE |
1229 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1230 EFI_VARIABLE_RUNTIME_ACCESS,
1231 0, dummy);
1232 }
1233
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001234 kfree(dummy);
1235
Matthew Garrettf8b84042013-06-01 16:06:20 -04001236 /*
1237 * The runtime code may now have triggered a garbage collection
1238 * run, so check the variable info again
1239 */
1240 status = efi.query_variable_info(attributes, &storage_size,
1241 &remaining_size, &max_size);
1242
1243 if (status != EFI_SUCCESS)
1244 return status;
1245
1246 /*
1247 * There still isn't enough room, so return an error
1248 */
1249 if (remaining_size - size < EFI_MIN_RESERVE)
1250 return EFI_OUT_OF_RESOURCES;
1251 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001252
1253 return EFI_SUCCESS;
1254}
Sergey Vlasov36680112013-04-16 18:31:09 +04001255EXPORT_SYMBOL_GPL(efi_query_variable_store);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001256
1257static int __init parse_efi_cmdline(char *str)
1258{
1259 if (*str == '=')
1260 str++;
1261
1262 if (!strncmp(str, "old_map", 7))
1263 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1264
1265 return 0;
1266}
1267early_param("efi", parse_efi_cmdline);