blob: 87fc96bcc13ceb1b4d90895e35528dda85641954 [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>
Borislav Petkova5d90c92014-03-04 17:02:17 +010055#include <asm/uv/uv.h>
Huang, Ying5b836832008-01-30 13:31:19 +010056
Borislav Petkovf4fccac2013-10-31 17:24:59 +010057#define EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +010058
Matthew Garrettf8b84042013-06-01 16:06:20 -040059#define EFI_MIN_RESERVE 5120
60
61#define EFI_DUMMY_GUID \
62 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
63
64static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
Matthew Garrett31ff2f22013-04-15 13:09:47 -070065
Huang, Ying5b836832008-01-30 13:31:19 +010066struct efi_memory_map memmap;
67
Harvey Harrisonecaea422008-02-13 13:26:13 -080068static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010069static efi_system_table_t efi_systab __initdata;
70
Joe Perches9b7d2042014-01-03 16:08:48 -080071static efi_config_table_type_t arch_tables[] __initdata = {
Leif Lindholm272686b2013-09-05 11:34:54 +010072#ifdef CONFIG_X86_UV
73 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
74#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010075 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010076};
77
Dave Young1fec0532013-12-20 18:02:19 +080078u64 efi_setup; /* efi setup_data physical address */
Dave Young926172d2013-12-20 18:02:18 +080079
Joe Perches9b7d2042014-01-03 16:08:48 -080080static bool disable_runtime __initdata = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010081static int __init setup_noefi(char *arg)
82{
Matt Flemingfb834c72013-02-20 20:36:12 +000083 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010084 return 0;
85}
86early_param("noefi", setup_noefi);
87
Paul Jackson200001e2008-06-25 05:44:46 -070088int add_efi_memmap;
89EXPORT_SYMBOL(add_efi_memmap);
90
91static int __init setup_add_efi_memmap(char *arg)
92{
93 add_efi_memmap = 1;
94 return 0;
95}
96early_param("add_efi_memmap", setup_add_efi_memmap);
97
Richard Weinberger8c58bf32013-04-17 01:00:53 +020098static bool efi_no_storage_paranoia;
99
100static int __init setup_storage_paranoia(char *arg)
101{
102 efi_no_storage_paranoia = true;
103 return 0;
104}
105early_param("efi_no_storage_paranoia", setup_storage_paranoia);
106
Huang, Ying5b836832008-01-30 13:31:19 +0100107static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
108{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100109 unsigned long flags;
110 efi_status_t status;
111
112 spin_lock_irqsave(&rtc_lock, flags);
Matt Fleming62fa6e62014-03-27 15:10:39 -0700113 status = efi_call_virt(get_time, tm, tc);
Jan Beulichef68c8f2011-07-19 11:53:07 +0100114 spin_unlock_irqrestore(&rtc_lock, flags);
115 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100116}
117
118static efi_status_t virt_efi_set_time(efi_time_t *tm)
119{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100120 unsigned long flags;
121 efi_status_t status;
122
123 spin_lock_irqsave(&rtc_lock, flags);
Matt Fleming62fa6e62014-03-27 15:10:39 -0700124 status = efi_call_virt(set_time, tm);
Jan Beulichef68c8f2011-07-19 11:53:07 +0100125 spin_unlock_irqrestore(&rtc_lock, flags);
126 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100127}
128
129static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
130 efi_bool_t *pending,
131 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);
Matt Fleming62fa6e62014-03-27 15:10:39 -0700137 status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
Jan Beulichef68c8f2011-07-19 11:53:07 +0100138 spin_unlock_irqrestore(&rtc_lock, flags);
139 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100140}
141
142static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
143{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100144 unsigned long flags;
145 efi_status_t status;
146
147 spin_lock_irqsave(&rtc_lock, flags);
Matt Fleming62fa6e62014-03-27 15:10:39 -0700148 status = efi_call_virt(set_wakeup_time, enabled, tm);
Jan Beulichef68c8f2011-07-19 11:53:07 +0100149 spin_unlock_irqrestore(&rtc_lock, flags);
150 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100151}
152
153static efi_status_t virt_efi_get_variable(efi_char16_t *name,
154 efi_guid_t *vendor,
155 u32 *attr,
156 unsigned long *data_size,
157 void *data)
158{
Matt Fleming62fa6e62014-03-27 15:10:39 -0700159 return efi_call_virt(get_variable,
160 name, vendor, attr,
161 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100162}
163
164static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
165 efi_char16_t *name,
166 efi_guid_t *vendor)
167{
Matt Fleming62fa6e62014-03-27 15:10:39 -0700168 return efi_call_virt(get_next_variable,
169 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100170}
171
172static efi_status_t virt_efi_set_variable(efi_char16_t *name,
173 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400174 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100175 unsigned long data_size,
176 void *data)
177{
Matt Fleming62fa6e62014-03-27 15:10:39 -0700178 return efi_call_virt(set_variable,
179 name, vendor, attr,
180 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100181}
182
Matthew Garrett3b370232011-06-06 15:36:25 -0400183static efi_status_t virt_efi_query_variable_info(u32 attr,
184 u64 *storage_space,
185 u64 *remaining_space,
186 u64 *max_variable_size)
187{
188 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
189 return EFI_UNSUPPORTED;
190
Matt Fleming62fa6e62014-03-27 15:10:39 -0700191 return efi_call_virt(query_variable_info, attr, storage_space,
192 remaining_space, max_variable_size);
Matthew Garrett3b370232011-06-06 15:36:25 -0400193}
194
Huang, Ying5b836832008-01-30 13:31:19 +0100195static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
196{
Matt Fleming62fa6e62014-03-27 15:10:39 -0700197 return efi_call_virt(get_next_high_mono_count, count);
Huang, Ying5b836832008-01-30 13:31:19 +0100198}
199
200static void virt_efi_reset_system(int reset_type,
201 efi_status_t status,
202 unsigned long data_size,
203 efi_char16_t *data)
204{
Ricardo Neri982e2392014-03-27 15:10:41 -0700205 __efi_call_virt(reset_system, reset_type, status,
206 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100207}
208
Matthew Garrett3b370232011-06-06 15:36:25 -0400209static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
210 unsigned long count,
211 unsigned long sg_list)
212{
213 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
214 return EFI_UNSUPPORTED;
215
Matt Fleming62fa6e62014-03-27 15:10:39 -0700216 return efi_call_virt(update_capsule, capsules, count, sg_list);
Matthew Garrett3b370232011-06-06 15:36:25 -0400217}
218
219static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
220 unsigned long count,
221 u64 *max_size,
222 int *reset_type)
223{
224 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
225 return EFI_UNSUPPORTED;
226
Matt Fleming62fa6e62014-03-27 15:10:39 -0700227 return efi_call_virt(query_capsule_caps, capsules, count, max_size,
228 reset_type);
Matthew Garrett3b370232011-06-06 15:36:25 -0400229}
230
Huang, Ying5b836832008-01-30 13:31:19 +0100231static efi_status_t __init phys_efi_set_virtual_address_map(
232 unsigned long memory_map_size,
233 unsigned long descriptor_size,
234 u32 descriptor_version,
235 efi_memory_desc_t *virtual_map)
236{
237 efi_status_t status;
238
239 efi_call_phys_prelog();
Matt Fleming62fa6e62014-03-27 15:10:39 -0700240 status = efi_call_phys(efi_phys.set_virtual_address_map,
241 memory_map_size, descriptor_size,
242 descriptor_version, virtual_map);
Huang, Ying5b836832008-01-30 13:31:19 +0100243 efi_call_phys_epilog();
244 return status;
245}
246
David Vrabel35651842013-05-13 18:56:06 +0100247int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100248{
David Vrabel35651842013-05-13 18:56:06 +0100249 unsigned long nowtime = now->tv_sec;
Joe Perches9b7d2042014-01-03 16:08:48 -0800250 efi_status_t status;
251 efi_time_t eft;
252 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500253 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100254
255 status = efi.get_time(&eft, &cap);
256 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800257 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100258 return -1;
259 }
260
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500261 rtc_time_to_tm(nowtime, &tm);
262 if (!rtc_valid_tm(&tm)) {
263 eft.year = tm.tm_year + 1900;
264 eft.month = tm.tm_mon + 1;
265 eft.day = tm.tm_mday;
266 eft.minute = tm.tm_min;
267 eft.second = tm.tm_sec;
268 eft.nanosecond = 0;
269 } else {
Joe Perches9b7d2042014-01-03 16:08:48 -0800270 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
271 __func__, nowtime);
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500272 return -1;
273 }
Huang, Ying5b836832008-01-30 13:31:19 +0100274
275 status = efi.set_time(&eft);
276 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800277 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100278 return -1;
279 }
280 return 0;
281}
282
David Vrabel35651842013-05-13 18:56:06 +0100283void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100284{
285 efi_status_t status;
286 efi_time_t eft;
287 efi_time_cap_t cap;
288
289 status = efi.get_time(&eft, &cap);
290 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800291 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100292
David Vrabel35651842013-05-13 18:56:06 +0100293 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
294 eft.minute, eft.second);
295 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100296}
297
Paul Jackson69c91892008-05-14 08:15:58 -0700298/*
299 * Tell the kernel about the EFI memory map. This might include
300 * more than the max 128 entries that can fit in the e820 legacy
301 * (zeropage) memory map.
302 */
303
Paul Jackson200001e2008-06-25 05:44:46 -0700304static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700305{
306 void *p;
307
308 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
309 efi_memory_desc_t *md = p;
310 unsigned long long start = md->phys_addr;
311 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
312 int e820_type;
313
Cliff Wickmane2a71472009-06-16 16:43:40 -0500314 switch (md->type) {
315 case EFI_LOADER_CODE:
316 case EFI_LOADER_DATA:
317 case EFI_BOOT_SERVICES_CODE:
318 case EFI_BOOT_SERVICES_DATA:
319 case EFI_CONVENTIONAL_MEMORY:
320 if (md->attribute & EFI_MEMORY_WB)
321 e820_type = E820_RAM;
322 else
323 e820_type = E820_RESERVED;
324 break;
325 case EFI_ACPI_RECLAIM_MEMORY:
326 e820_type = E820_ACPI;
327 break;
328 case EFI_ACPI_MEMORY_NVS:
329 e820_type = E820_NVS;
330 break;
331 case EFI_UNUSABLE_MEMORY:
332 e820_type = E820_UNUSABLE;
333 break;
334 default:
335 /*
336 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
337 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
338 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
339 */
Paul Jackson69c91892008-05-14 08:15:58 -0700340 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500341 break;
342 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700343 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700344 }
345 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
346}
347
Olof Johansson1adbfa32012-02-12 13:24:29 -0800348int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800349{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100350 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800351 unsigned long pmap;
352
Paul Jackson05486fa2008-06-22 07:22:02 -0700353#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800354 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100355 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800356 pr_err("Memory map is above 4GB, disabling EFI.\n");
357 return -EINVAL;
358 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100359 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700360#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100361 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800362#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100363 memmap.phys_map = (void *)pmap;
364 memmap.nr_map = e->efi_memmap_size /
365 e->efi_memdesc_size;
366 memmap.desc_size = e->efi_memdesc_size;
367 memmap.desc_version = e->efi_memdesc_version;
368
Tejun Heo24aa0782011-07-12 11:16:06 +0200369 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800370
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100371 efi.memmap = &memmap;
372
Olof Johansson1adbfa32012-02-12 13:24:29 -0800373 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800374}
375
Huang, Ying5b836832008-01-30 13:31:19 +0100376static void __init print_efi_memmap(void)
377{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100378#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100379 efi_memory_desc_t *md;
380 void *p;
381 int i;
382
383 for (p = memmap.map, i = 0;
384 p < memmap.map_end;
385 p += memmap.desc_size, i++) {
386 md = p;
Joe Perches9b7d2042014-01-03 16:08:48 -0800387 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100388 i, md->type, md->attribute, md->phys_addr,
389 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
390 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
391 }
Huang, Ying5b836832008-01-30 13:31:19 +0100392#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100393}
Huang, Ying5b836832008-01-30 13:31:19 +0100394
Matthew Garrett916f6762011-05-25 09:53:13 -0400395void __init efi_reserve_boot_services(void)
396{
397 void *p;
398
399 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
400 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200401 u64 start = md->phys_addr;
402 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400403
404 if (md->type != EFI_BOOT_SERVICES_CODE &&
405 md->type != EFI_BOOT_SERVICES_DATA)
406 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200407 /* Only reserve where possible:
408 * - Not within any already allocated areas
409 * - Not over any memory area (really needed, if above?)
410 * - Not within any part of the kernel
411 * - Not the bios reserved area
412 */
Dave Younga7f84f02013-12-20 18:02:15 +0800413 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800414 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200415 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200416 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200417 /* Could not reserve, skip it */
418 md->num_pages = 0;
Joe Perches9b7d2042014-01-03 16:08:48 -0800419 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
420 start, start+size-1);
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200421 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200422 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400423 }
424}
425
Olof Johansson5189c2a2012-10-24 10:00:44 -0700426void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700427{
Matt Fleming3e909592014-01-15 13:21:22 +0000428 clear_bit(EFI_MEMMAP, &efi.flags);
Josh Triplett78510792012-09-28 17:55:44 -0700429 if (memmap.map) {
430 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
431 memmap.map = NULL;
432 }
433}
434
435void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400436{
437 void *p;
438
439 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
440 efi_memory_desc_t *md = p;
441 unsigned long long start = md->phys_addr;
442 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
443
444 if (md->type != EFI_BOOT_SERVICES_CODE &&
445 md->type != EFI_BOOT_SERVICES_DATA)
446 continue;
447
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200448 /* Could not reserve boot area */
449 if (!size)
450 continue;
451
Matthew Garrett916f6762011-05-25 09:53:13 -0400452 free_bootmem_late(start, size);
453 }
Josh Triplett78510792012-09-28 17:55:44 -0700454
455 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400456}
457
Olof Johansson140bf272012-02-12 13:24:28 -0800458static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100459{
Matt Fleming83e68182012-11-14 09:42:35 +0000460 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800461 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800462 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800463 u64 tmp = 0;
464
Dave Young1fec0532013-12-20 18:02:19 +0800465 if (efi_setup) {
466 data = early_memremap(efi_setup, sizeof(*data));
467 if (!data)
468 return -ENOMEM;
469 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800470 systab64 = early_ioremap((unsigned long)phys,
471 sizeof(*systab64));
472 if (systab64 == NULL) {
473 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800474 if (data)
475 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800476 return -ENOMEM;
477 }
478
479 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800480 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
481 systab64->fw_vendor;
482 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800483 efi_systab.fw_revision = systab64->fw_revision;
484 efi_systab.con_in_handle = systab64->con_in_handle;
485 tmp |= systab64->con_in_handle;
486 efi_systab.con_in = systab64->con_in;
487 tmp |= systab64->con_in;
488 efi_systab.con_out_handle = systab64->con_out_handle;
489 tmp |= systab64->con_out_handle;
490 efi_systab.con_out = systab64->con_out;
491 tmp |= systab64->con_out;
492 efi_systab.stderr_handle = systab64->stderr_handle;
493 tmp |= systab64->stderr_handle;
494 efi_systab.stderr = systab64->stderr;
495 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800496 efi_systab.runtime = data ?
497 (void *)(unsigned long)data->runtime :
498 (void *)(unsigned long)systab64->runtime;
499 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800500 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
501 tmp |= systab64->boottime;
502 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800503 efi_systab.tables = data ? (unsigned long)data->tables :
504 systab64->tables;
505 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800506
507 early_iounmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800508 if (data)
509 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800510#ifdef CONFIG_X86_32
511 if (tmp >> 32) {
512 pr_err("EFI data located above 4GB, disabling EFI.\n");
513 return -EINVAL;
514 }
515#endif
516 } else {
517 efi_system_table_32_t *systab32;
518
519 systab32 = early_ioremap((unsigned long)phys,
520 sizeof(*systab32));
521 if (systab32 == NULL) {
522 pr_err("Couldn't map the system table!\n");
523 return -ENOMEM;
524 }
525
526 efi_systab.hdr = systab32->hdr;
527 efi_systab.fw_vendor = systab32->fw_vendor;
528 efi_systab.fw_revision = systab32->fw_revision;
529 efi_systab.con_in_handle = systab32->con_in_handle;
530 efi_systab.con_in = systab32->con_in;
531 efi_systab.con_out_handle = systab32->con_out_handle;
532 efi_systab.con_out = systab32->con_out;
533 efi_systab.stderr_handle = systab32->stderr_handle;
534 efi_systab.stderr = systab32->stderr;
535 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
536 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
537 efi_systab.nr_tables = systab32->nr_tables;
538 efi_systab.tables = systab32->tables;
539
540 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800541 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800542
Huang, Ying5b836832008-01-30 13:31:19 +0100543 efi.systab = &efi_systab;
544
545 /*
546 * Verify the EFI Table
547 */
Olof Johansson140bf272012-02-12 13:24:28 -0800548 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800549 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800550 return -EINVAL;
551 }
Huang, Ying5b836832008-01-30 13:31:19 +0100552 if ((efi.systab->hdr.revision >> 16) == 0)
Joe Perches9b7d2042014-01-03 16:08:48 -0800553 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100554 efi.systab->hdr.revision >> 16,
555 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800556
Matt Fleming0f8093a2014-01-15 13:36:33 +0000557 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
558
Olof Johansson140bf272012-02-12 13:24:28 -0800559 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800560}
Huang, Ying5b836832008-01-30 13:31:19 +0100561
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000562static int __init efi_runtime_init32(void)
563{
564 efi_runtime_services_32_t *runtime;
565
566 runtime = early_ioremap((unsigned long)efi.systab->runtime,
567 sizeof(efi_runtime_services_32_t));
568 if (!runtime) {
569 pr_err("Could not map the runtime service table!\n");
570 return -ENOMEM;
571 }
572
573 /*
574 * We will only need *early* access to the following two
575 * EFI runtime services before set_virtual_address_map
576 * is invoked.
577 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000578 efi_phys.set_virtual_address_map =
579 (efi_set_virtual_address_map_t *)
580 (unsigned long)runtime->set_virtual_address_map;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000581 early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
582
583 return 0;
584}
585
586static int __init efi_runtime_init64(void)
587{
588 efi_runtime_services_64_t *runtime;
589
590 runtime = early_ioremap((unsigned long)efi.systab->runtime,
591 sizeof(efi_runtime_services_64_t));
592 if (!runtime) {
593 pr_err("Could not map the runtime service table!\n");
594 return -ENOMEM;
595 }
596
597 /*
598 * We will only need *early* access to the following two
599 * EFI runtime services before set_virtual_address_map
600 * is invoked.
601 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000602 efi_phys.set_virtual_address_map =
603 (efi_set_virtual_address_map_t *)
604 (unsigned long)runtime->set_virtual_address_map;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000605 early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
606
607 return 0;
608}
609
Olof Johansson140bf272012-02-12 13:24:28 -0800610static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800611{
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000612 int rv;
Huang, Ying5b836832008-01-30 13:31:19 +0100613
614 /*
615 * Check out the runtime services table. We need to map
616 * the runtime services table so that we can grab the physical
617 * address of several of the EFI runtime functions, needed to
618 * set the firmware into virtual mode.
619 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000620 if (efi_enabled(EFI_64BIT))
621 rv = efi_runtime_init64();
622 else
623 rv = efi_runtime_init32();
624
625 if (rv)
626 return rv;
Olof Johansson140bf272012-02-12 13:24:28 -0800627
Matt Fleming0f8093a2014-01-15 13:36:33 +0000628 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
629
Olof Johansson140bf272012-02-12 13:24:28 -0800630 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800631}
Huang, Ying5b836832008-01-30 13:31:19 +0100632
Olof Johansson140bf272012-02-12 13:24:28 -0800633static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800634{
Huang, Ying5b836832008-01-30 13:31:19 +0100635 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100636 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
637 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800638 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800639 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800640 return -ENOMEM;
641 }
Huang, Ying5b836832008-01-30 13:31:19 +0100642 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500643
Paul Jackson200001e2008-06-25 05:44:46 -0700644 if (add_efi_memmap)
645 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800646
Matt Fleming0f8093a2014-01-15 13:36:33 +0000647 set_bit(EFI_MEMMAP, &efi.flags);
648
Olof Johansson140bf272012-02-12 13:24:28 -0800649 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800650}
651
Dave Young1fec0532013-12-20 18:02:19 +0800652/*
653 * A number of config table entries get remapped to virtual addresses
654 * after entering EFI virtual mode. However, the kexec kernel requires
655 * their physical addresses therefore we pass them via setup_data and
656 * correct those entries to their respective physical addresses here.
657 *
658 * Currently only handles smbios which is necessary for some firmware
659 * implementation.
660 */
661static int __init efi_reuse_config(u64 tables, int nr_tables)
662{
663 int i, sz, ret = 0;
664 void *p, *tablep;
665 struct efi_setup_data *data;
666
667 if (!efi_setup)
668 return 0;
669
670 if (!efi_enabled(EFI_64BIT))
671 return 0;
672
673 data = early_memremap(efi_setup, sizeof(*data));
674 if (!data) {
675 ret = -ENOMEM;
676 goto out;
677 }
678
679 if (!data->smbios)
680 goto out_memremap;
681
682 sz = sizeof(efi_config_table_64_t);
683
684 p = tablep = early_memremap(tables, nr_tables * sz);
685 if (!p) {
686 pr_err("Could not map Configuration table!\n");
687 ret = -ENOMEM;
688 goto out_memremap;
689 }
690
691 for (i = 0; i < efi.systab->nr_tables; i++) {
692 efi_guid_t guid;
693
694 guid = ((efi_config_table_64_t *)p)->guid;
695
696 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
697 ((efi_config_table_64_t *)p)->table = data->smbios;
698 p += sz;
699 }
700 early_iounmap(tablep, nr_tables * sz);
701
702out_memremap:
703 early_iounmap(data, sizeof(*data));
704out:
705 return ret;
706}
707
Olof Johansson83e7ee62012-02-12 13:24:25 -0800708void __init efi_init(void)
709{
710 efi_char16_t *c16;
711 char vendor[100] = "unknown";
712 int i = 0;
713 void *tmp;
714
715#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800716 if (boot_params.efi_info.efi_systab_hi ||
717 boot_params.efi_info.efi_memmap_hi) {
718 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800719 return;
720 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800721 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
722#else
723 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800724 (boot_params.efi_info.efi_systab |
725 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800726#endif
727
Matt Fleming83e68182012-11-14 09:42:35 +0000728 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800729 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000730
Matt Fleming3e909592014-01-15 13:21:22 +0000731 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800732
Dave Younga0998eb2013-12-20 18:02:17 +0800733 efi.config_table = (unsigned long)efi.systab->tables;
734 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
735 efi.runtime = (unsigned long)efi.systab->runtime;
736
Olof Johansson83e7ee62012-02-12 13:24:25 -0800737 /*
738 * Show what we know for posterity
739 */
740 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
741 if (c16) {
742 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
743 vendor[i] = *c16++;
744 vendor[i] = '\0';
745 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800746 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800747 early_iounmap(tmp, 2);
748
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800749 pr_info("EFI v%u.%.02u by %s\n",
750 efi.systab->hdr.revision >> 16,
751 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800752
Dave Young1fec0532013-12-20 18:02:19 +0800753 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
754 return;
755
Leif Lindholm272686b2013-09-05 11:34:54 +0100756 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800757 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000758
Olof Johansson1adbfa32012-02-12 13:24:29 -0800759 /*
760 * Note: We currently don't support runtime services on an EFI
761 * that doesn't match the kernel 32/64-bit mode.
762 */
763
Matt Fleming7d453ee2014-01-10 18:52:06 +0000764 if (!efi_runtime_supported())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800765 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000766 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000767 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000768 return;
Olof Johansson140bf272012-02-12 13:24:28 -0800769 }
Matt Fleming83e68182012-11-14 09:42:35 +0000770 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800771 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000772
Matt Fleming3e909592014-01-15 13:21:22 +0000773 set_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming83e68182012-11-14 09:42:35 +0000774
Huang, Ying5b836832008-01-30 13:31:19 +0100775 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100776}
777
Josh Triplett2223af32012-09-28 17:57:05 -0700778void __init efi_late_init(void)
779{
780 efi_bgrt_init();
781}
782
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400783void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
784{
785 u64 addr, npages;
786
787 addr = md->virt_addr;
788 npages = md->num_pages;
789
790 memrange_efi_to_native(&addr, &npages);
791
792 if (executable)
793 set_memory_x(addr, npages);
794 else
795 set_memory_nx(addr, npages);
796}
797
Borislav Petkovc55d0162014-02-14 08:24:24 +0100798void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100799{
800 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100801 void *p;
802
Huang, Yinga2172e22008-01-30 13:33:55 +0100803 /* Make EFI runtime service code area executable */
804 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
805 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100806
807 if (md->type != EFI_RUNTIME_SERVICES_CODE)
808 continue;
809
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400810 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100811 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100812}
Huang, Yinga2172e22008-01-30 13:33:55 +0100813
Matt Fleming3e8fa262012-10-19 13:25:46 +0100814void efi_memory_uc(u64 addr, unsigned long size)
815{
816 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
817 u64 npages;
818
819 npages = round_up(size, page_shift) / page_shift;
820 memrange_efi_to_native(&addr, &npages);
821 set_memory_uc(addr, npages);
822}
823
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100824void __init old_map_region(efi_memory_desc_t *md)
825{
826 u64 start_pfn, end_pfn, end;
827 unsigned long size;
828 void *va;
829
830 start_pfn = PFN_DOWN(md->phys_addr);
831 size = md->num_pages << PAGE_SHIFT;
832 end = md->phys_addr + size;
833 end_pfn = PFN_UP(end);
834
835 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
836 va = __va(md->phys_addr);
837
838 if (!(md->attribute & EFI_MEMORY_WB))
839 efi_memory_uc((u64)(unsigned long)va, size);
840 } else
841 va = efi_ioremap(md->phys_addr, size,
842 md->type, md->attribute);
843
844 md->virt_addr = (u64) (unsigned long) va;
845 if (!va)
846 pr_err("ioremap of 0x%llX failed!\n",
847 (unsigned long long)md->phys_addr);
848}
849
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000850static void native_runtime_setup(void)
851{
852 efi.get_time = virt_efi_get_time;
853 efi.set_time = virt_efi_set_time;
854 efi.get_wakeup_time = virt_efi_get_wakeup_time;
855 efi.set_wakeup_time = virt_efi_set_wakeup_time;
856 efi.get_variable = virt_efi_get_variable;
857 efi.get_next_variable = virt_efi_get_next_variable;
858 efi.set_variable = virt_efi_set_variable;
859 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
860 efi.reset_system = virt_efi_reset_system;
861 efi.query_variable_info = virt_efi_query_variable_info;
862 efi.update_capsule = virt_efi_update_capsule;
863 efi.query_capsule_caps = virt_efi_query_capsule_caps;
864}
865
Dave Young481f75c2013-12-20 18:02:16 +0800866/* Merge contiguous regions of the same type and attribute */
867static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100868{
Dave Young481f75c2013-12-20 18:02:16 +0800869 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400870 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100871
Matthew Garrett202f9d02011-05-05 15:19:44 -0400872 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
873 u64 prev_size;
874 md = p;
875
876 if (!prev_md) {
877 prev_md = md;
878 continue;
879 }
880
881 if (prev_md->type != md->type ||
882 prev_md->attribute != md->attribute) {
883 prev_md = md;
884 continue;
885 }
886
887 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
888
889 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
890 prev_md->num_pages += md->num_pages;
891 md->type = EFI_RESERVED_TYPE;
892 md->attribute = 0;
893 continue;
894 }
895 prev_md = md;
896 }
Dave Young481f75c2013-12-20 18:02:16 +0800897}
898
899static void __init get_systab_virt_addr(efi_memory_desc_t *md)
900{
901 unsigned long size;
902 u64 end, systab;
903
904 size = md->num_pages << EFI_PAGE_SHIFT;
905 end = md->phys_addr + size;
906 systab = (u64)(unsigned long)efi_phys.systab;
907 if (md->phys_addr <= systab && systab < end) {
908 systab += md->virt_addr - md->phys_addr;
909 efi.systab = (efi_system_table_t *)(unsigned long)systab;
910 }
911}
912
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100913static void __init save_runtime_map(void)
Dave Young926172d2013-12-20 18:02:18 +0800914{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100915#ifdef CONFIG_KEXEC
Dave Young926172d2013-12-20 18:02:18 +0800916 efi_memory_desc_t *md;
917 void *tmp, *p, *q = NULL;
918 int count = 0;
919
Dave Younga3530e82014-05-30 11:20:10 +0800920 if (efi_enabled(EFI_OLD_MEMMAP))
921 return;
922
Dave Young926172d2013-12-20 18:02:18 +0800923 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
924 md = p;
925
926 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
927 (md->type == EFI_BOOT_SERVICES_CODE) ||
928 (md->type == EFI_BOOT_SERVICES_DATA))
929 continue;
930 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
931 if (!tmp)
932 goto out;
933 q = tmp;
934
935 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
936 count++;
937 }
938
Matt Fleming518548a2013-12-21 16:09:46 +0000939 efi_runtime_map_setup(q, count, memmap.desc_size);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100940 return;
Dave Young926172d2013-12-20 18:02:18 +0800941
Dave Young926172d2013-12-20 18:02:18 +0800942out:
943 kfree(q);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100944 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
945#endif
Dave Young1fec0532013-12-20 18:02:19 +0800946}
947
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100948static void *realloc_pages(void *old_memmap, int old_shift)
Dave Young481f75c2013-12-20 18:02:16 +0800949{
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100950 void *ret;
951
952 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
953 if (!ret)
954 goto out;
955
956 /*
957 * A first-time allocation doesn't have anything to copy.
958 */
959 if (!old_memmap)
960 return ret;
961
962 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
963
964out:
965 free_pages((unsigned long)old_memmap, old_shift);
966 return ret;
967}
968
969/*
970 * Map the efi memory ranges of the runtime services and update new_mmap with
971 * virtual addresses.
972 */
973static void * __init efi_map_regions(int *count, int *pg_shift)
974{
975 void *p, *new_memmap = NULL;
976 unsigned long left = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800977 efi_memory_desc_t *md;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400978
Huang, Ying5b836832008-01-30 13:31:19 +0100979 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
980 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700981 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
982#ifdef CONFIG_X86_64
983 if (md->type != EFI_BOOT_SERVICES_CODE &&
984 md->type != EFI_BOOT_SERVICES_DATA)
985#endif
986 continue;
987 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100988
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100989 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800990 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100991
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100992 if (left < memmap.desc_size) {
993 new_memmap = realloc_pages(new_memmap, *pg_shift);
994 if (!new_memmap)
995 return NULL;
996
997 left += PAGE_SIZE << *pg_shift;
998 (*pg_shift)++;
999 }
1000
Dave Young481f75c2013-12-20 18:02:16 +08001001 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001002 memmap.desc_size);
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001003
1004 left -= memmap.desc_size;
Dave Young481f75c2013-12-20 18:02:16 +08001005 (*count)++;
1006 }
1007
1008 return new_memmap;
Dave Young481f75c2013-12-20 18:02:16 +08001009}
1010
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001011static void __init kexec_enter_virtual_mode(void)
1012{
1013#ifdef CONFIG_KEXEC
1014 efi_memory_desc_t *md;
1015 void *p;
1016
1017 efi.systab = NULL;
1018
1019 /*
1020 * We don't do virtual mode, since we don't do runtime services, on
1021 * non-native EFI
1022 */
1023 if (!efi_is_native()) {
1024 efi_unmap_memmap();
1025 return;
1026 }
1027
1028 /*
1029 * Map efi regions which were passed via setup_data. The virt_addr is a
1030 * fixed addr which was used in first kernel of a kexec boot.
1031 */
1032 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1033 md = p;
1034 efi_map_region_fixed(md); /* FIXME: add error handling */
1035 get_systab_virt_addr(md);
1036 }
1037
1038 save_runtime_map();
1039
1040 BUG_ON(!efi.systab);
1041
1042 efi_sync_low_kernel_mappings();
1043
1044 /*
1045 * Now that EFI is in virtual mode, update the function
1046 * pointers in the runtime service table to the new virtual addresses.
1047 *
1048 * Call EFI services through wrapper functions.
1049 */
1050 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming994448f2014-03-05 18:15:37 +00001051
1052 native_runtime_setup();
1053
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001054 efi.set_virtual_address_map = NULL;
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001055
1056 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1057 runtime_code_page_mkexec();
1058
1059 /* clean DUMMY object */
1060 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1061 EFI_VARIABLE_NON_VOLATILE |
1062 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1063 EFI_VARIABLE_RUNTIME_ACCESS,
1064 0, NULL);
1065#endif
1066}
1067
Dave Young481f75c2013-12-20 18:02:16 +08001068/*
1069 * This function will switch the EFI runtime services to virtual mode.
1070 * Essentially, we look through the EFI memmap and map every region that
1071 * has the runtime attribute bit set in its memory descriptor into the
1072 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1073 *
1074 * The old method which used to update that memory descriptor with the
1075 * virtual address obtained from ioremap() is still supported when the
1076 * kernel is booted with efi=old_map on its command line. Same old
1077 * method enabled the runtime services to be called without having to
1078 * thunk back into physical mode for every invocation.
1079 *
1080 * The new method does a pagetable switch in a preemption-safe manner
1081 * so that we're in a different address space when calling a runtime
1082 * function. For function arguments passing we do copy the PGDs of the
1083 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +08001084 *
1085 * Specially for kexec boot, efi runtime maps in previous kernel should
1086 * be passed in via setup_data. In that case runtime ranges will be mapped
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001087 * to the same virtual addresses as the first kernel, see
1088 * kexec_enter_virtual_mode().
Dave Young481f75c2013-12-20 18:02:16 +08001089 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001090static void __init __efi_enter_virtual_mode(void)
Dave Young481f75c2013-12-20 18:02:16 +08001091{
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001092 int count = 0, pg_shift = 0;
Dave Young481f75c2013-12-20 18:02:16 +08001093 void *new_memmap = NULL;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001094 efi_status_t status;
Dave Young481f75c2013-12-20 18:02:16 +08001095
1096 efi.systab = NULL;
1097
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001098 efi_merge_regions();
1099 new_memmap = efi_map_regions(&count, &pg_shift);
1100 if (!new_memmap) {
1101 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1102 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001103 }
Dave Young926172d2013-12-20 18:02:18 +08001104
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001105 save_runtime_map();
1106
Huang, Ying5b836832008-01-30 13:31:19 +01001107 BUG_ON(!efi.systab);
1108
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001109 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1110 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001111
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001112 efi_sync_low_kernel_mappings();
Borislav Petkov11cc8512014-01-18 12:48:15 +01001113 efi_dump_pagetable();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001114
Matt Fleming994448f2014-03-05 18:15:37 +00001115 if (efi_is_native()) {
1116 status = phys_efi_set_virtual_address_map(
1117 memmap.desc_size * count,
1118 memmap.desc_size,
1119 memmap.desc_version,
1120 (efi_memory_desc_t *)__pa(new_memmap));
1121 } else {
1122 status = efi_thunk_set_virtual_address_map(
1123 efi_phys.set_virtual_address_map,
1124 memmap.desc_size * count,
1125 memmap.desc_size,
1126 memmap.desc_version,
1127 (efi_memory_desc_t *)__pa(new_memmap));
1128 }
Huang, Ying5b836832008-01-30 13:31:19 +01001129
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001130 if (status != EFI_SUCCESS) {
1131 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1132 status);
1133 panic("EFI call to SetVirtualAddressMap() failed!");
Huang, Ying5b836832008-01-30 13:31:19 +01001134 }
1135
1136 /*
1137 * Now that EFI is in virtual mode, update the function
1138 * pointers in the runtime service table to the new virtual addresses.
1139 *
1140 * Call EFI services through wrapper functions.
1141 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001142 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +00001143
1144 if (efi_is_native())
1145 native_runtime_setup();
1146 else
1147 efi_thunk_runtime_setup();
1148
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001149 efi.set_virtual_address_map = NULL;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001150
Borislav Petkovc55d0162014-02-14 08:24:24 +01001151 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001152
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001153 /*
1154 * We mapped the descriptor array into the EFI pagetable above but we're
1155 * not unmapping it here. Here's why:
1156 *
1157 * We're copying select PGDs from the kernel page table to the EFI page
1158 * table and when we do so and make changes to those PGDs like unmapping
1159 * stuff from them, those changes appear in the kernel page table and we
1160 * go boom.
1161 *
1162 * From setup_real_mode():
1163 *
1164 * ...
1165 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1166 *
1167 * In this particular case, our allocation is in PGD 0 of the EFI page
1168 * table but we've copied that PGD from PGD[272] of the EFI page table:
1169 *
1170 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1171 *
1172 * where the direct memory mapping in kernel space is.
1173 *
1174 * new_memmap's VA comes from that direct mapping and thus clearing it,
1175 * it would get cleared in the kernel page table too.
1176 *
1177 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1178 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001179 free_pages((unsigned long)new_memmap, pg_shift);
Matthew Garrettf8b84042013-06-01 16:06:20 -04001180
1181 /* clean DUMMY object */
1182 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1183 EFI_VARIABLE_NON_VOLATILE |
1184 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1185 EFI_VARIABLE_RUNTIME_ACCESS,
1186 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001187}
1188
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001189void __init efi_enter_virtual_mode(void)
1190{
1191 if (efi_setup)
1192 kexec_enter_virtual_mode();
1193 else
1194 __efi_enter_virtual_mode();
1195}
1196
Huang, Ying5b836832008-01-30 13:31:19 +01001197/*
1198 * Convenience functions to obtain memory types and attributes
1199 */
1200u32 efi_mem_type(unsigned long phys_addr)
1201{
1202 efi_memory_desc_t *md;
1203 void *p;
1204
Matt Fleming83e68182012-11-14 09:42:35 +00001205 if (!efi_enabled(EFI_MEMMAP))
1206 return 0;
1207
Huang, Ying5b836832008-01-30 13:31:19 +01001208 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1209 md = p;
1210 if ((md->phys_addr <= phys_addr) &&
1211 (phys_addr < (md->phys_addr +
1212 (md->num_pages << EFI_PAGE_SHIFT))))
1213 return md->type;
1214 }
1215 return 0;
1216}
1217
1218u64 efi_mem_attributes(unsigned long phys_addr)
1219{
1220 efi_memory_desc_t *md;
1221 void *p;
1222
1223 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1224 md = p;
1225 if ((md->phys_addr <= phys_addr) &&
1226 (phys_addr < (md->phys_addr +
1227 (md->num_pages << EFI_PAGE_SHIFT))))
1228 return md->attribute;
1229 }
1230 return 0;
1231}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001232
1233/*
Madper Xie5db80c62013-11-10 22:15:11 +08001234 * Some firmware implementations refuse to boot if there's insufficient space
1235 * in the variable store. Ensure that we never use more than a safe limit.
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001236 *
1237 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1238 * store.
1239 */
1240efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1241{
1242 efi_status_t status;
1243 u64 storage_size, remaining_size, max_size;
1244
Matthew Garrettf8b84042013-06-01 16:06:20 -04001245 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1246 return 0;
1247
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001248 status = efi.query_variable_info(attributes, &storage_size,
1249 &remaining_size, &max_size);
1250 if (status != EFI_SUCCESS)
1251 return status;
1252
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001253 /*
Madper Xie5db80c62013-11-10 22:15:11 +08001254 * We account for that by refusing the write if permitting it would
1255 * reduce the available space to under 5KB. This figure was provided by
1256 * Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001257 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001258 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1259 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001260
Matthew Garrettf8b84042013-06-01 16:06:20 -04001261 /*
1262 * Triggering garbage collection may require that the firmware
1263 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1264 * that by attempting to use more space than is available.
1265 */
1266 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001267 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1268
1269 if (!dummy)
1270 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001271
Matthew Garrettf8b84042013-06-01 16:06:20 -04001272 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1273 EFI_VARIABLE_NON_VOLATILE |
1274 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1275 EFI_VARIABLE_RUNTIME_ACCESS,
1276 dummy_size, dummy);
1277
1278 if (status == EFI_SUCCESS) {
1279 /*
1280 * This should have failed, so if it didn't make sure
1281 * that we delete it...
1282 */
1283 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1284 EFI_VARIABLE_NON_VOLATILE |
1285 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1286 EFI_VARIABLE_RUNTIME_ACCESS,
1287 0, dummy);
1288 }
1289
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001290 kfree(dummy);
1291
Matthew Garrettf8b84042013-06-01 16:06:20 -04001292 /*
1293 * The runtime code may now have triggered a garbage collection
1294 * run, so check the variable info again
1295 */
1296 status = efi.query_variable_info(attributes, &storage_size,
1297 &remaining_size, &max_size);
1298
1299 if (status != EFI_SUCCESS)
1300 return status;
1301
1302 /*
1303 * There still isn't enough room, so return an error
1304 */
1305 if (remaining_size - size < EFI_MIN_RESERVE)
1306 return EFI_OUT_OF_RESOURCES;
1307 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001308
1309 return EFI_SUCCESS;
1310}
Sergey Vlasov36680112013-04-16 18:31:09 +04001311EXPORT_SYMBOL_GPL(efi_query_variable_store);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001312
1313static int __init parse_efi_cmdline(char *str)
1314{
1315 if (*str == '=')
1316 str++;
1317
1318 if (!strncmp(str, "old_map", 7))
Matt Fleming0f8093a2014-01-15 13:36:33 +00001319 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001320
1321 return 0;
1322}
1323early_param("efi", parse_efi_cmdline);
Borislav Petkova5d90c92014-03-04 17:02:17 +01001324
1325void __init efi_apply_memmap_quirks(void)
1326{
1327 /*
1328 * Once setup is done earlier, unmap the EFI memory map on mismatched
1329 * firmware/kernel architectures since there is no support for runtime
1330 * services.
1331 */
Matt Fleming994448f2014-03-05 18:15:37 +00001332 if (!efi_runtime_supported()) {
Borislav Petkova5d90c92014-03-04 17:02:17 +01001333 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1334 efi_unmap_memmap();
1335 }
1336
1337 /*
1338 * UV doesn't support the new EFI pagetable mapping yet.
1339 */
1340 if (is_uv_system())
Matt Fleming4fd69332014-03-05 17:22:57 +00001341 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkova5d90c92014-03-04 17:02:17 +01001342}