blob: 45d4f7674678499df3316a4a797d665edd3c5d15 [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);
113 status = efi_call_virt2(get_time, tm, tc);
114 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);
124 status = efi_call_virt1(set_time, tm);
125 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);
137 status = efi_call_virt3(get_wakeup_time,
138 enabled, pending, tm);
139 spin_unlock_irqrestore(&rtc_lock, flags);
140 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100141}
142
143static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
144{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100145 unsigned long flags;
146 efi_status_t status;
147
148 spin_lock_irqsave(&rtc_lock, flags);
149 status = efi_call_virt2(set_wakeup_time,
150 enabled, tm);
151 spin_unlock_irqrestore(&rtc_lock, flags);
152 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100153}
154
155static efi_status_t virt_efi_get_variable(efi_char16_t *name,
156 efi_guid_t *vendor,
157 u32 *attr,
158 unsigned long *data_size,
159 void *data)
160{
161 return efi_call_virt5(get_variable,
162 name, vendor, attr,
163 data_size, data);
164}
165
166static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
167 efi_char16_t *name,
168 efi_guid_t *vendor)
169{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400170 return efi_call_virt3(get_next_variable,
171 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100172}
173
174static efi_status_t virt_efi_set_variable(efi_char16_t *name,
175 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400176 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100177 unsigned long data_size,
178 void *data)
179{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400180 return efi_call_virt5(set_variable,
181 name, vendor, attr,
182 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100183}
184
Matthew Garrett3b370232011-06-06 15:36:25 -0400185static efi_status_t virt_efi_query_variable_info(u32 attr,
186 u64 *storage_space,
187 u64 *remaining_space,
188 u64 *max_variable_size)
189{
190 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
191 return EFI_UNSUPPORTED;
192
193 return efi_call_virt4(query_variable_info, attr, storage_space,
194 remaining_space, max_variable_size);
195}
196
Huang, Ying5b836832008-01-30 13:31:19 +0100197static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
198{
199 return efi_call_virt1(get_next_high_mono_count, count);
200}
201
202static void virt_efi_reset_system(int reset_type,
203 efi_status_t status,
204 unsigned long data_size,
205 efi_char16_t *data)
206{
207 efi_call_virt4(reset_system, reset_type, status,
208 data_size, data);
209}
210
Matthew Garrett3b370232011-06-06 15:36:25 -0400211static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
212 unsigned long count,
213 unsigned long sg_list)
214{
215 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
216 return EFI_UNSUPPORTED;
217
218 return efi_call_virt3(update_capsule, capsules, count, sg_list);
219}
220
221static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
222 unsigned long count,
223 u64 *max_size,
224 int *reset_type)
225{
226 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
227 return EFI_UNSUPPORTED;
228
229 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
230 reset_type);
231}
232
Huang, Ying5b836832008-01-30 13:31:19 +0100233static efi_status_t __init phys_efi_set_virtual_address_map(
234 unsigned long memory_map_size,
235 unsigned long descriptor_size,
236 u32 descriptor_version,
237 efi_memory_desc_t *virtual_map)
238{
239 efi_status_t status;
240
241 efi_call_phys_prelog();
242 status = efi_call_phys4(efi_phys.set_virtual_address_map,
243 memory_map_size, descriptor_size,
244 descriptor_version, virtual_map);
245 efi_call_phys_epilog();
246 return status;
247}
248
Linus Torvalds11520e52012-12-15 15:15:24 -0800249static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
250 efi_time_cap_t *tc)
251{
252 unsigned long flags;
253 efi_status_t status;
254
255 spin_lock_irqsave(&rtc_lock, flags);
256 efi_call_phys_prelog();
257 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
258 virt_to_phys(tc));
259 efi_call_phys_epilog();
260 spin_unlock_irqrestore(&rtc_lock, flags);
261 return status;
262}
263
David Vrabel35651842013-05-13 18:56:06 +0100264int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100265{
David Vrabel35651842013-05-13 18:56:06 +0100266 unsigned long nowtime = now->tv_sec;
Joe Perches9b7d2042014-01-03 16:08:48 -0800267 efi_status_t status;
268 efi_time_t eft;
269 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500270 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100271
272 status = efi.get_time(&eft, &cap);
273 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800274 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100275 return -1;
276 }
277
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500278 rtc_time_to_tm(nowtime, &tm);
279 if (!rtc_valid_tm(&tm)) {
280 eft.year = tm.tm_year + 1900;
281 eft.month = tm.tm_mon + 1;
282 eft.day = tm.tm_mday;
283 eft.minute = tm.tm_min;
284 eft.second = tm.tm_sec;
285 eft.nanosecond = 0;
286 } else {
Joe Perches9b7d2042014-01-03 16:08:48 -0800287 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
288 __func__, nowtime);
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500289 return -1;
290 }
Huang, Ying5b836832008-01-30 13:31:19 +0100291
292 status = efi.set_time(&eft);
293 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800294 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100295 return -1;
296 }
297 return 0;
298}
299
David Vrabel35651842013-05-13 18:56:06 +0100300void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100301{
302 efi_status_t status;
303 efi_time_t eft;
304 efi_time_cap_t cap;
305
306 status = efi.get_time(&eft, &cap);
307 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800308 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100309
David Vrabel35651842013-05-13 18:56:06 +0100310 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
311 eft.minute, eft.second);
312 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100313}
314
Paul Jackson69c91892008-05-14 08:15:58 -0700315/*
316 * Tell the kernel about the EFI memory map. This might include
317 * more than the max 128 entries that can fit in the e820 legacy
318 * (zeropage) memory map.
319 */
320
Paul Jackson200001e2008-06-25 05:44:46 -0700321static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700322{
323 void *p;
324
325 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
326 efi_memory_desc_t *md = p;
327 unsigned long long start = md->phys_addr;
328 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
329 int e820_type;
330
Cliff Wickmane2a71472009-06-16 16:43:40 -0500331 switch (md->type) {
332 case EFI_LOADER_CODE:
333 case EFI_LOADER_DATA:
334 case EFI_BOOT_SERVICES_CODE:
335 case EFI_BOOT_SERVICES_DATA:
336 case EFI_CONVENTIONAL_MEMORY:
337 if (md->attribute & EFI_MEMORY_WB)
338 e820_type = E820_RAM;
339 else
340 e820_type = E820_RESERVED;
341 break;
342 case EFI_ACPI_RECLAIM_MEMORY:
343 e820_type = E820_ACPI;
344 break;
345 case EFI_ACPI_MEMORY_NVS:
346 e820_type = E820_NVS;
347 break;
348 case EFI_UNUSABLE_MEMORY:
349 e820_type = E820_UNUSABLE;
350 break;
351 default:
352 /*
353 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
354 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
355 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
356 */
Paul Jackson69c91892008-05-14 08:15:58 -0700357 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500358 break;
359 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700360 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700361 }
362 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
363}
364
Olof Johansson1adbfa32012-02-12 13:24:29 -0800365int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800366{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100367 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800368 unsigned long pmap;
369
Paul Jackson05486fa2008-06-22 07:22:02 -0700370#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800371 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100372 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800373 pr_err("Memory map is above 4GB, disabling EFI.\n");
374 return -EINVAL;
375 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100376 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700377#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100378 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800379#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100380 memmap.phys_map = (void *)pmap;
381 memmap.nr_map = e->efi_memmap_size /
382 e->efi_memdesc_size;
383 memmap.desc_size = e->efi_memdesc_size;
384 memmap.desc_version = e->efi_memdesc_version;
385
Tejun Heo24aa0782011-07-12 11:16:06 +0200386 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800387
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100388 efi.memmap = &memmap;
389
Olof Johansson1adbfa32012-02-12 13:24:29 -0800390 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800391}
392
Huang, Ying5b836832008-01-30 13:31:19 +0100393static void __init print_efi_memmap(void)
394{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100395#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100396 efi_memory_desc_t *md;
397 void *p;
398 int i;
399
400 for (p = memmap.map, i = 0;
401 p < memmap.map_end;
402 p += memmap.desc_size, i++) {
403 md = p;
Joe Perches9b7d2042014-01-03 16:08:48 -0800404 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100405 i, md->type, md->attribute, md->phys_addr,
406 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
407 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
408 }
Huang, Ying5b836832008-01-30 13:31:19 +0100409#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100410}
Huang, Ying5b836832008-01-30 13:31:19 +0100411
Matthew Garrett916f6762011-05-25 09:53:13 -0400412void __init efi_reserve_boot_services(void)
413{
414 void *p;
415
416 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
417 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200418 u64 start = md->phys_addr;
419 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400420
421 if (md->type != EFI_BOOT_SERVICES_CODE &&
422 md->type != EFI_BOOT_SERVICES_DATA)
423 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200424 /* Only reserve where possible:
425 * - Not within any already allocated areas
426 * - Not over any memory area (really needed, if above?)
427 * - Not within any part of the kernel
428 * - Not the bios reserved area
429 */
Dave Younga7f84f02013-12-20 18:02:15 +0800430 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800431 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200432 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200433 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200434 /* Could not reserve, skip it */
435 md->num_pages = 0;
Joe Perches9b7d2042014-01-03 16:08:48 -0800436 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
437 start, start+size-1);
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200438 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200439 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400440 }
441}
442
Olof Johansson5189c2a2012-10-24 10:00:44 -0700443void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700444{
Matt Fleming3e909592014-01-15 13:21:22 +0000445 clear_bit(EFI_MEMMAP, &efi.flags);
Josh Triplett78510792012-09-28 17:55:44 -0700446 if (memmap.map) {
447 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
448 memmap.map = NULL;
449 }
450}
451
452void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400453{
454 void *p;
455
Olof Johansson5189c2a2012-10-24 10:00:44 -0700456 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700457 return;
458
Matthew Garrett916f6762011-05-25 09:53:13 -0400459 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
460 efi_memory_desc_t *md = p;
461 unsigned long long start = md->phys_addr;
462 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
463
464 if (md->type != EFI_BOOT_SERVICES_CODE &&
465 md->type != EFI_BOOT_SERVICES_DATA)
466 continue;
467
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200468 /* Could not reserve boot area */
469 if (!size)
470 continue;
471
Matthew Garrett916f6762011-05-25 09:53:13 -0400472 free_bootmem_late(start, size);
473 }
Josh Triplett78510792012-09-28 17:55:44 -0700474
475 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400476}
477
Olof Johansson140bf272012-02-12 13:24:28 -0800478static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100479{
Matt Fleming83e68182012-11-14 09:42:35 +0000480 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800481 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800482 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800483 u64 tmp = 0;
484
Dave Young1fec0532013-12-20 18:02:19 +0800485 if (efi_setup) {
486 data = early_memremap(efi_setup, sizeof(*data));
487 if (!data)
488 return -ENOMEM;
489 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800490 systab64 = early_ioremap((unsigned long)phys,
491 sizeof(*systab64));
492 if (systab64 == NULL) {
493 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800494 if (data)
495 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800496 return -ENOMEM;
497 }
498
499 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800500 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
501 systab64->fw_vendor;
502 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800503 efi_systab.fw_revision = systab64->fw_revision;
504 efi_systab.con_in_handle = systab64->con_in_handle;
505 tmp |= systab64->con_in_handle;
506 efi_systab.con_in = systab64->con_in;
507 tmp |= systab64->con_in;
508 efi_systab.con_out_handle = systab64->con_out_handle;
509 tmp |= systab64->con_out_handle;
510 efi_systab.con_out = systab64->con_out;
511 tmp |= systab64->con_out;
512 efi_systab.stderr_handle = systab64->stderr_handle;
513 tmp |= systab64->stderr_handle;
514 efi_systab.stderr = systab64->stderr;
515 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800516 efi_systab.runtime = data ?
517 (void *)(unsigned long)data->runtime :
518 (void *)(unsigned long)systab64->runtime;
519 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800520 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
521 tmp |= systab64->boottime;
522 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800523 efi_systab.tables = data ? (unsigned long)data->tables :
524 systab64->tables;
525 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800526
527 early_iounmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800528 if (data)
529 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800530#ifdef CONFIG_X86_32
531 if (tmp >> 32) {
532 pr_err("EFI data located above 4GB, disabling EFI.\n");
533 return -EINVAL;
534 }
535#endif
536 } else {
537 efi_system_table_32_t *systab32;
538
539 systab32 = early_ioremap((unsigned long)phys,
540 sizeof(*systab32));
541 if (systab32 == NULL) {
542 pr_err("Couldn't map the system table!\n");
543 return -ENOMEM;
544 }
545
546 efi_systab.hdr = systab32->hdr;
547 efi_systab.fw_vendor = systab32->fw_vendor;
548 efi_systab.fw_revision = systab32->fw_revision;
549 efi_systab.con_in_handle = systab32->con_in_handle;
550 efi_systab.con_in = systab32->con_in;
551 efi_systab.con_out_handle = systab32->con_out_handle;
552 efi_systab.con_out = systab32->con_out;
553 efi_systab.stderr_handle = systab32->stderr_handle;
554 efi_systab.stderr = systab32->stderr;
555 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
556 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
557 efi_systab.nr_tables = systab32->nr_tables;
558 efi_systab.tables = systab32->tables;
559
560 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800561 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800562
Huang, Ying5b836832008-01-30 13:31:19 +0100563 efi.systab = &efi_systab;
564
565 /*
566 * Verify the EFI Table
567 */
Olof Johansson140bf272012-02-12 13:24:28 -0800568 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800569 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800570 return -EINVAL;
571 }
Huang, Ying5b836832008-01-30 13:31:19 +0100572 if ((efi.systab->hdr.revision >> 16) == 0)
Joe Perches9b7d2042014-01-03 16:08:48 -0800573 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100574 efi.systab->hdr.revision >> 16,
575 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800576
Matt Fleming0f8093a2014-01-15 13:36:33 +0000577 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
578
Olof Johansson140bf272012-02-12 13:24:28 -0800579 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800580}
Huang, Ying5b836832008-01-30 13:31:19 +0100581
Olof Johansson140bf272012-02-12 13:24:28 -0800582static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800583{
584 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100585
586 /*
587 * Check out the runtime services table. We need to map
588 * the runtime services table so that we can grab the physical
589 * address of several of the EFI runtime functions, needed to
590 * set the firmware into virtual mode.
591 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100592 runtime = early_ioremap((unsigned long)efi.systab->runtime,
593 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800594 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800595 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800596 return -ENOMEM;
597 }
598 /*
599 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800600 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800601 * is invoked.
602 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800603 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800604 efi_phys.set_virtual_address_map =
605 (efi_set_virtual_address_map_t *)
606 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800607 /*
608 * Make efi_get_time can be called before entering
609 * virtual mode.
610 */
611 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100612 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800613
Matt Fleming0f8093a2014-01-15 13:36:33 +0000614 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
615
Olof Johansson140bf272012-02-12 13:24:28 -0800616 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800617}
Huang, Ying5b836832008-01-30 13:31:19 +0100618
Olof Johansson140bf272012-02-12 13:24:28 -0800619static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800620{
Huang, Ying5b836832008-01-30 13:31:19 +0100621 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100622 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
623 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800624 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800625 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800626 return -ENOMEM;
627 }
Huang, Ying5b836832008-01-30 13:31:19 +0100628 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500629
Paul Jackson200001e2008-06-25 05:44:46 -0700630 if (add_efi_memmap)
631 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800632
Matt Fleming0f8093a2014-01-15 13:36:33 +0000633 set_bit(EFI_MEMMAP, &efi.flags);
634
Olof Johansson140bf272012-02-12 13:24:28 -0800635 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800636}
637
Dave Young1fec0532013-12-20 18:02:19 +0800638/*
639 * A number of config table entries get remapped to virtual addresses
640 * after entering EFI virtual mode. However, the kexec kernel requires
641 * their physical addresses therefore we pass them via setup_data and
642 * correct those entries to their respective physical addresses here.
643 *
644 * Currently only handles smbios which is necessary for some firmware
645 * implementation.
646 */
647static int __init efi_reuse_config(u64 tables, int nr_tables)
648{
649 int i, sz, ret = 0;
650 void *p, *tablep;
651 struct efi_setup_data *data;
652
653 if (!efi_setup)
654 return 0;
655
656 if (!efi_enabled(EFI_64BIT))
657 return 0;
658
659 data = early_memremap(efi_setup, sizeof(*data));
660 if (!data) {
661 ret = -ENOMEM;
662 goto out;
663 }
664
665 if (!data->smbios)
666 goto out_memremap;
667
668 sz = sizeof(efi_config_table_64_t);
669
670 p = tablep = early_memremap(tables, nr_tables * sz);
671 if (!p) {
672 pr_err("Could not map Configuration table!\n");
673 ret = -ENOMEM;
674 goto out_memremap;
675 }
676
677 for (i = 0; i < efi.systab->nr_tables; i++) {
678 efi_guid_t guid;
679
680 guid = ((efi_config_table_64_t *)p)->guid;
681
682 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
683 ((efi_config_table_64_t *)p)->table = data->smbios;
684 p += sz;
685 }
686 early_iounmap(tablep, nr_tables * sz);
687
688out_memremap:
689 early_iounmap(data, sizeof(*data));
690out:
691 return ret;
692}
693
Olof Johansson83e7ee62012-02-12 13:24:25 -0800694void __init efi_init(void)
695{
696 efi_char16_t *c16;
697 char vendor[100] = "unknown";
698 int i = 0;
699 void *tmp;
700
701#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800702 if (boot_params.efi_info.efi_systab_hi ||
703 boot_params.efi_info.efi_memmap_hi) {
704 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800705 return;
706 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800707 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
708#else
709 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800710 (boot_params.efi_info.efi_systab |
711 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800712#endif
713
Matt Fleming83e68182012-11-14 09:42:35 +0000714 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800715 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000716
Matt Fleming3e909592014-01-15 13:21:22 +0000717 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800718
Dave Younga0998eb2013-12-20 18:02:17 +0800719 efi.config_table = (unsigned long)efi.systab->tables;
720 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
721 efi.runtime = (unsigned long)efi.systab->runtime;
722
Olof Johansson83e7ee62012-02-12 13:24:25 -0800723 /*
724 * Show what we know for posterity
725 */
726 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
727 if (c16) {
728 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
729 vendor[i] = *c16++;
730 vendor[i] = '\0';
731 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800732 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800733 early_iounmap(tmp, 2);
734
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800735 pr_info("EFI v%u.%.02u by %s\n",
736 efi.systab->hdr.revision >> 16,
737 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800738
Dave Young1fec0532013-12-20 18:02:19 +0800739 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
740 return;
741
Leif Lindholm272686b2013-09-05 11:34:54 +0100742 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800743 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000744
Olof Johansson1adbfa32012-02-12 13:24:29 -0800745 /*
746 * Note: We currently don't support runtime services on an EFI
747 * that doesn't match the kernel 32/64-bit mode.
748 */
749
Olof Johansson5189c2a2012-10-24 10:00:44 -0700750 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800751 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000752 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000753 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000754 return;
Olof Johansson140bf272012-02-12 13:24:28 -0800755 }
Matt Fleming83e68182012-11-14 09:42:35 +0000756 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800757 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000758
Matt Fleming3e909592014-01-15 13:21:22 +0000759 set_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming83e68182012-11-14 09:42:35 +0000760
Huang, Ying5b836832008-01-30 13:31:19 +0100761 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100762}
763
Josh Triplett2223af32012-09-28 17:57:05 -0700764void __init efi_late_init(void)
765{
766 efi_bgrt_init();
767}
768
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400769void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
770{
771 u64 addr, npages;
772
773 addr = md->virt_addr;
774 npages = md->num_pages;
775
776 memrange_efi_to_native(&addr, &npages);
777
778 if (executable)
779 set_memory_x(addr, npages);
780 else
781 set_memory_nx(addr, npages);
782}
783
Borislav Petkovc55d0162014-02-14 08:24:24 +0100784void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100785{
786 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100787 void *p;
788
Huang, Yinga2172e22008-01-30 13:33:55 +0100789 /* Make EFI runtime service code area executable */
790 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
791 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100792
793 if (md->type != EFI_RUNTIME_SERVICES_CODE)
794 continue;
795
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400796 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100797 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100798}
Huang, Yinga2172e22008-01-30 13:33:55 +0100799
Matt Fleming3e8fa262012-10-19 13:25:46 +0100800void efi_memory_uc(u64 addr, unsigned long size)
801{
802 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
803 u64 npages;
804
805 npages = round_up(size, page_shift) / page_shift;
806 memrange_efi_to_native(&addr, &npages);
807 set_memory_uc(addr, npages);
808}
809
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100810void __init old_map_region(efi_memory_desc_t *md)
811{
812 u64 start_pfn, end_pfn, end;
813 unsigned long size;
814 void *va;
815
816 start_pfn = PFN_DOWN(md->phys_addr);
817 size = md->num_pages << PAGE_SHIFT;
818 end = md->phys_addr + size;
819 end_pfn = PFN_UP(end);
820
821 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
822 va = __va(md->phys_addr);
823
824 if (!(md->attribute & EFI_MEMORY_WB))
825 efi_memory_uc((u64)(unsigned long)va, size);
826 } else
827 va = efi_ioremap(md->phys_addr, size,
828 md->type, md->attribute);
829
830 md->virt_addr = (u64) (unsigned long) va;
831 if (!va)
832 pr_err("ioremap of 0x%llX failed!\n",
833 (unsigned long long)md->phys_addr);
834}
835
Dave Young481f75c2013-12-20 18:02:16 +0800836/* Merge contiguous regions of the same type and attribute */
837static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100838{
Dave Young481f75c2013-12-20 18:02:16 +0800839 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400840 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100841
Matthew Garrett202f9d02011-05-05 15:19:44 -0400842 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
843 u64 prev_size;
844 md = p;
845
846 if (!prev_md) {
847 prev_md = md;
848 continue;
849 }
850
851 if (prev_md->type != md->type ||
852 prev_md->attribute != md->attribute) {
853 prev_md = md;
854 continue;
855 }
856
857 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
858
859 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
860 prev_md->num_pages += md->num_pages;
861 md->type = EFI_RESERVED_TYPE;
862 md->attribute = 0;
863 continue;
864 }
865 prev_md = md;
866 }
Dave Young481f75c2013-12-20 18:02:16 +0800867}
868
869static void __init get_systab_virt_addr(efi_memory_desc_t *md)
870{
871 unsigned long size;
872 u64 end, systab;
873
874 size = md->num_pages << EFI_PAGE_SHIFT;
875 end = md->phys_addr + size;
876 systab = (u64)(unsigned long)efi_phys.systab;
877 if (md->phys_addr <= systab && systab < end) {
878 systab += md->virt_addr - md->phys_addr;
879 efi.systab = (efi_system_table_t *)(unsigned long)systab;
880 }
881}
882
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100883static void __init save_runtime_map(void)
Dave Young926172d2013-12-20 18:02:18 +0800884{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100885#ifdef CONFIG_KEXEC
Dave Young926172d2013-12-20 18:02:18 +0800886 efi_memory_desc_t *md;
887 void *tmp, *p, *q = NULL;
888 int count = 0;
889
890 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
891 md = p;
892
893 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
894 (md->type == EFI_BOOT_SERVICES_CODE) ||
895 (md->type == EFI_BOOT_SERVICES_DATA))
896 continue;
897 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
898 if (!tmp)
899 goto out;
900 q = tmp;
901
902 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
903 count++;
904 }
905
Matt Fleming518548a2013-12-21 16:09:46 +0000906 efi_runtime_map_setup(q, count, memmap.desc_size);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100907 return;
Dave Young926172d2013-12-20 18:02:18 +0800908
Dave Young926172d2013-12-20 18:02:18 +0800909out:
910 kfree(q);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100911 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
912#endif
Dave Young1fec0532013-12-20 18:02:19 +0800913}
914
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100915static void *realloc_pages(void *old_memmap, int old_shift)
Dave Young481f75c2013-12-20 18:02:16 +0800916{
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100917 void *ret;
918
919 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
920 if (!ret)
921 goto out;
922
923 /*
924 * A first-time allocation doesn't have anything to copy.
925 */
926 if (!old_memmap)
927 return ret;
928
929 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
930
931out:
932 free_pages((unsigned long)old_memmap, old_shift);
933 return ret;
934}
935
936/*
937 * Map the efi memory ranges of the runtime services and update new_mmap with
938 * virtual addresses.
939 */
940static void * __init efi_map_regions(int *count, int *pg_shift)
941{
942 void *p, *new_memmap = NULL;
943 unsigned long left = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800944 efi_memory_desc_t *md;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400945
Huang, Ying5b836832008-01-30 13:31:19 +0100946 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
947 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700948 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
949#ifdef CONFIG_X86_64
950 if (md->type != EFI_BOOT_SERVICES_CODE &&
951 md->type != EFI_BOOT_SERVICES_DATA)
952#endif
953 continue;
954 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100955
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100956 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800957 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100958
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100959 if (left < memmap.desc_size) {
960 new_memmap = realloc_pages(new_memmap, *pg_shift);
961 if (!new_memmap)
962 return NULL;
963
964 left += PAGE_SIZE << *pg_shift;
965 (*pg_shift)++;
966 }
967
Dave Young481f75c2013-12-20 18:02:16 +0800968 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400969 memmap.desc_size);
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100970
971 left -= memmap.desc_size;
Dave Young481f75c2013-12-20 18:02:16 +0800972 (*count)++;
973 }
974
975 return new_memmap;
Dave Young481f75c2013-12-20 18:02:16 +0800976}
977
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100978static void __init kexec_enter_virtual_mode(void)
979{
980#ifdef CONFIG_KEXEC
981 efi_memory_desc_t *md;
982 void *p;
983
984 efi.systab = NULL;
985
986 /*
987 * We don't do virtual mode, since we don't do runtime services, on
988 * non-native EFI
989 */
990 if (!efi_is_native()) {
991 efi_unmap_memmap();
992 return;
993 }
994
995 /*
996 * Map efi regions which were passed via setup_data. The virt_addr is a
997 * fixed addr which was used in first kernel of a kexec boot.
998 */
999 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1000 md = p;
1001 efi_map_region_fixed(md); /* FIXME: add error handling */
1002 get_systab_virt_addr(md);
1003 }
1004
1005 save_runtime_map();
1006
1007 BUG_ON(!efi.systab);
1008
1009 efi_sync_low_kernel_mappings();
1010
1011 /*
1012 * Now that EFI is in virtual mode, update the function
1013 * pointers in the runtime service table to the new virtual addresses.
1014 *
1015 * Call EFI services through wrapper functions.
1016 */
1017 efi.runtime_version = efi_systab.hdr.revision;
1018 efi.get_time = virt_efi_get_time;
1019 efi.set_time = virt_efi_set_time;
1020 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1021 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1022 efi.get_variable = virt_efi_get_variable;
1023 efi.get_next_variable = virt_efi_get_next_variable;
1024 efi.set_variable = virt_efi_set_variable;
1025 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1026 efi.reset_system = virt_efi_reset_system;
1027 efi.set_virtual_address_map = NULL;
1028 efi.query_variable_info = virt_efi_query_variable_info;
1029 efi.update_capsule = virt_efi_update_capsule;
1030 efi.query_capsule_caps = virt_efi_query_capsule_caps;
1031
1032 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1033 runtime_code_page_mkexec();
1034
1035 /* clean DUMMY object */
1036 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1037 EFI_VARIABLE_NON_VOLATILE |
1038 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1039 EFI_VARIABLE_RUNTIME_ACCESS,
1040 0, NULL);
1041#endif
1042}
1043
Dave Young481f75c2013-12-20 18:02:16 +08001044/*
1045 * This function will switch the EFI runtime services to virtual mode.
1046 * Essentially, we look through the EFI memmap and map every region that
1047 * has the runtime attribute bit set in its memory descriptor into the
1048 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1049 *
1050 * The old method which used to update that memory descriptor with the
1051 * virtual address obtained from ioremap() is still supported when the
1052 * kernel is booted with efi=old_map on its command line. Same old
1053 * method enabled the runtime services to be called without having to
1054 * thunk back into physical mode for every invocation.
1055 *
1056 * The new method does a pagetable switch in a preemption-safe manner
1057 * so that we're in a different address space when calling a runtime
1058 * function. For function arguments passing we do copy the PGDs of the
1059 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +08001060 *
1061 * Specially for kexec boot, efi runtime maps in previous kernel should
1062 * be passed in via setup_data. In that case runtime ranges will be mapped
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001063 * to the same virtual addresses as the first kernel, see
1064 * kexec_enter_virtual_mode().
Dave Young481f75c2013-12-20 18:02:16 +08001065 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001066static void __init __efi_enter_virtual_mode(void)
Dave Young481f75c2013-12-20 18:02:16 +08001067{
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001068 int count = 0, pg_shift = 0;
Dave Young481f75c2013-12-20 18:02:16 +08001069 void *new_memmap = NULL;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001070 efi_status_t status;
Dave Young481f75c2013-12-20 18:02:16 +08001071
1072 efi.systab = NULL;
1073
1074 /*
1075 * We don't do virtual mode, since we don't do runtime services, on
1076 * non-native EFI
1077 */
1078 if (!efi_is_native()) {
1079 efi_unmap_memmap();
1080 return;
1081 }
1082
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001083 efi_merge_regions();
1084 new_memmap = efi_map_regions(&count, &pg_shift);
1085 if (!new_memmap) {
1086 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1087 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001088 }
Dave Young926172d2013-12-20 18:02:18 +08001089
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001090 save_runtime_map();
1091
Huang, Ying5b836832008-01-30 13:31:19 +01001092 BUG_ON(!efi.systab);
1093
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001094 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1095 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001096
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001097 efi_sync_low_kernel_mappings();
Borislav Petkov11cc8512014-01-18 12:48:15 +01001098 efi_dump_pagetable();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001099
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001100 status = phys_efi_set_virtual_address_map(
Dave Young1fec0532013-12-20 18:02:19 +08001101 memmap.desc_size * count,
1102 memmap.desc_size,
1103 memmap.desc_version,
1104 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +01001105
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001106 if (status != EFI_SUCCESS) {
1107 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1108 status);
1109 panic("EFI call to SetVirtualAddressMap() failed!");
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;
Huang, Ying5b836832008-01-30 13:31:19 +01001119 efi.get_time = virt_efi_get_time;
1120 efi.set_time = virt_efi_set_time;
1121 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1122 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1123 efi.get_variable = virt_efi_get_variable;
1124 efi.get_next_variable = virt_efi_get_next_variable;
1125 efi.set_variable = virt_efi_set_variable;
1126 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1127 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001128 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -04001129 efi.query_variable_info = virt_efi_query_variable_info;
1130 efi.update_capsule = virt_efi_update_capsule;
1131 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001132
Borislav Petkovc55d0162014-02-14 08:24:24 +01001133 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001134
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001135 /*
1136 * We mapped the descriptor array into the EFI pagetable above but we're
1137 * not unmapping it here. Here's why:
1138 *
1139 * We're copying select PGDs from the kernel page table to the EFI page
1140 * table and when we do so and make changes to those PGDs like unmapping
1141 * stuff from them, those changes appear in the kernel page table and we
1142 * go boom.
1143 *
1144 * From setup_real_mode():
1145 *
1146 * ...
1147 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1148 *
1149 * In this particular case, our allocation is in PGD 0 of the EFI page
1150 * table but we've copied that PGD from PGD[272] of the EFI page table:
1151 *
1152 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1153 *
1154 * where the direct memory mapping in kernel space is.
1155 *
1156 * new_memmap's VA comes from that direct mapping and thus clearing it,
1157 * it would get cleared in the kernel page table too.
1158 *
1159 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1160 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001161 free_pages((unsigned long)new_memmap, pg_shift);
Matthew Garrettf8b84042013-06-01 16:06:20 -04001162
1163 /* clean DUMMY object */
1164 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1165 EFI_VARIABLE_NON_VOLATILE |
1166 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1167 EFI_VARIABLE_RUNTIME_ACCESS,
1168 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001169}
1170
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001171void __init efi_enter_virtual_mode(void)
1172{
1173 if (efi_setup)
1174 kexec_enter_virtual_mode();
1175 else
1176 __efi_enter_virtual_mode();
1177}
1178
Huang, Ying5b836832008-01-30 13:31:19 +01001179/*
1180 * Convenience functions to obtain memory types and attributes
1181 */
1182u32 efi_mem_type(unsigned long phys_addr)
1183{
1184 efi_memory_desc_t *md;
1185 void *p;
1186
Matt Fleming83e68182012-11-14 09:42:35 +00001187 if (!efi_enabled(EFI_MEMMAP))
1188 return 0;
1189
Huang, Ying5b836832008-01-30 13:31:19 +01001190 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1191 md = p;
1192 if ((md->phys_addr <= phys_addr) &&
1193 (phys_addr < (md->phys_addr +
1194 (md->num_pages << EFI_PAGE_SHIFT))))
1195 return md->type;
1196 }
1197 return 0;
1198}
1199
1200u64 efi_mem_attributes(unsigned long phys_addr)
1201{
1202 efi_memory_desc_t *md;
1203 void *p;
1204
1205 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1206 md = p;
1207 if ((md->phys_addr <= phys_addr) &&
1208 (phys_addr < (md->phys_addr +
1209 (md->num_pages << EFI_PAGE_SHIFT))))
1210 return md->attribute;
1211 }
1212 return 0;
1213}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001214
1215/*
Madper Xie5db80c62013-11-10 22:15:11 +08001216 * Some firmware implementations refuse to boot if there's insufficient space
1217 * in the variable store. Ensure that we never use more than a safe limit.
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001218 *
1219 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1220 * store.
1221 */
1222efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1223{
1224 efi_status_t status;
1225 u64 storage_size, remaining_size, max_size;
1226
Matthew Garrettf8b84042013-06-01 16:06:20 -04001227 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1228 return 0;
1229
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001230 status = efi.query_variable_info(attributes, &storage_size,
1231 &remaining_size, &max_size);
1232 if (status != EFI_SUCCESS)
1233 return status;
1234
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001235 /*
Madper Xie5db80c62013-11-10 22:15:11 +08001236 * We account for that by refusing the write if permitting it would
1237 * reduce the available space to under 5KB. This figure was provided by
1238 * Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001239 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001240 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1241 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001242
Matthew Garrettf8b84042013-06-01 16:06:20 -04001243 /*
1244 * Triggering garbage collection may require that the firmware
1245 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1246 * that by attempting to use more space than is available.
1247 */
1248 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001249 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1250
1251 if (!dummy)
1252 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001253
Matthew Garrettf8b84042013-06-01 16:06:20 -04001254 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1255 EFI_VARIABLE_NON_VOLATILE |
1256 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1257 EFI_VARIABLE_RUNTIME_ACCESS,
1258 dummy_size, dummy);
1259
1260 if (status == EFI_SUCCESS) {
1261 /*
1262 * This should have failed, so if it didn't make sure
1263 * that we delete it...
1264 */
1265 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1266 EFI_VARIABLE_NON_VOLATILE |
1267 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1268 EFI_VARIABLE_RUNTIME_ACCESS,
1269 0, dummy);
1270 }
1271
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001272 kfree(dummy);
1273
Matthew Garrettf8b84042013-06-01 16:06:20 -04001274 /*
1275 * The runtime code may now have triggered a garbage collection
1276 * run, so check the variable info again
1277 */
1278 status = efi.query_variable_info(attributes, &storage_size,
1279 &remaining_size, &max_size);
1280
1281 if (status != EFI_SUCCESS)
1282 return status;
1283
1284 /*
1285 * There still isn't enough room, so return an error
1286 */
1287 if (remaining_size - size < EFI_MIN_RESERVE)
1288 return EFI_OUT_OF_RESOURCES;
1289 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001290
1291 return EFI_SUCCESS;
1292}
Sergey Vlasov36680112013-04-16 18:31:09 +04001293EXPORT_SYMBOL_GPL(efi_query_variable_store);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001294
1295static int __init parse_efi_cmdline(char *str)
1296{
1297 if (*str == '=')
1298 str++;
1299
1300 if (!strncmp(str, "old_map", 7))
Matt Fleming0f8093a2014-01-15 13:36:33 +00001301 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001302
1303 return 0;
1304}
1305early_param("efi", parse_efi_cmdline);
Borislav Petkova5d90c92014-03-04 17:02:17 +01001306
1307void __init efi_apply_memmap_quirks(void)
1308{
1309 /*
1310 * Once setup is done earlier, unmap the EFI memory map on mismatched
1311 * firmware/kernel architectures since there is no support for runtime
1312 * services.
1313 */
1314 if (!efi_is_native()) {
1315 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1316 efi_unmap_memmap();
1317 }
1318
1319 /*
1320 * UV doesn't support the new EFI pagetable mapping yet.
1321 */
1322 if (is_uv_system())
Matt Fleming4fd69332014-03-05 17:22:57 +00001323 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkova5d90c92014-03-04 17:02:17 +01001324}