blob: 3781dd39e8bd55a03b8acd779113f40c151442b4 [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
David Vrabel35651842013-05-13 18:56:06 +0100249int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100250{
David Vrabel35651842013-05-13 18:56:06 +0100251 unsigned long nowtime = now->tv_sec;
Joe Perches9b7d2042014-01-03 16:08:48 -0800252 efi_status_t status;
253 efi_time_t eft;
254 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500255 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100256
257 status = efi.get_time(&eft, &cap);
258 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800259 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100260 return -1;
261 }
262
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500263 rtc_time_to_tm(nowtime, &tm);
264 if (!rtc_valid_tm(&tm)) {
265 eft.year = tm.tm_year + 1900;
266 eft.month = tm.tm_mon + 1;
267 eft.day = tm.tm_mday;
268 eft.minute = tm.tm_min;
269 eft.second = tm.tm_sec;
270 eft.nanosecond = 0;
271 } else {
Joe Perches9b7d2042014-01-03 16:08:48 -0800272 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
273 __func__, nowtime);
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500274 return -1;
275 }
Huang, Ying5b836832008-01-30 13:31:19 +0100276
277 status = efi.set_time(&eft);
278 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800279 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100280 return -1;
281 }
282 return 0;
283}
284
David Vrabel35651842013-05-13 18:56:06 +0100285void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100286{
287 efi_status_t status;
288 efi_time_t eft;
289 efi_time_cap_t cap;
290
291 status = efi.get_time(&eft, &cap);
292 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800293 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100294
David Vrabel35651842013-05-13 18:56:06 +0100295 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
296 eft.minute, eft.second);
297 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100298}
299
Paul Jackson69c91892008-05-14 08:15:58 -0700300/*
301 * Tell the kernel about the EFI memory map. This might include
302 * more than the max 128 entries that can fit in the e820 legacy
303 * (zeropage) memory map.
304 */
305
Paul Jackson200001e2008-06-25 05:44:46 -0700306static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700307{
308 void *p;
309
310 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
311 efi_memory_desc_t *md = p;
312 unsigned long long start = md->phys_addr;
313 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
314 int e820_type;
315
Cliff Wickmane2a71472009-06-16 16:43:40 -0500316 switch (md->type) {
317 case EFI_LOADER_CODE:
318 case EFI_LOADER_DATA:
319 case EFI_BOOT_SERVICES_CODE:
320 case EFI_BOOT_SERVICES_DATA:
321 case EFI_CONVENTIONAL_MEMORY:
322 if (md->attribute & EFI_MEMORY_WB)
323 e820_type = E820_RAM;
324 else
325 e820_type = E820_RESERVED;
326 break;
327 case EFI_ACPI_RECLAIM_MEMORY:
328 e820_type = E820_ACPI;
329 break;
330 case EFI_ACPI_MEMORY_NVS:
331 e820_type = E820_NVS;
332 break;
333 case EFI_UNUSABLE_MEMORY:
334 e820_type = E820_UNUSABLE;
335 break;
336 default:
337 /*
338 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
339 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
340 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
341 */
Paul Jackson69c91892008-05-14 08:15:58 -0700342 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500343 break;
344 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700345 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700346 }
347 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
348}
349
Olof Johansson1adbfa32012-02-12 13:24:29 -0800350int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800351{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100352 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800353 unsigned long pmap;
354
Paul Jackson05486fa2008-06-22 07:22:02 -0700355#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800356 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100357 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800358 pr_err("Memory map is above 4GB, disabling EFI.\n");
359 return -EINVAL;
360 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100361 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700362#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100363 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800364#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100365 memmap.phys_map = (void *)pmap;
366 memmap.nr_map = e->efi_memmap_size /
367 e->efi_memdesc_size;
368 memmap.desc_size = e->efi_memdesc_size;
369 memmap.desc_version = e->efi_memdesc_version;
370
Tejun Heo24aa0782011-07-12 11:16:06 +0200371 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800372
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100373 efi.memmap = &memmap;
374
Olof Johansson1adbfa32012-02-12 13:24:29 -0800375 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800376}
377
Huang, Ying5b836832008-01-30 13:31:19 +0100378static void __init print_efi_memmap(void)
379{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100380#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100381 efi_memory_desc_t *md;
382 void *p;
383 int i;
384
385 for (p = memmap.map, i = 0;
386 p < memmap.map_end;
387 p += memmap.desc_size, i++) {
388 md = p;
Joe Perches9b7d2042014-01-03 16:08:48 -0800389 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100390 i, md->type, md->attribute, md->phys_addr,
391 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
392 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
393 }
Huang, Ying5b836832008-01-30 13:31:19 +0100394#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100395}
Huang, Ying5b836832008-01-30 13:31:19 +0100396
Matthew Garrett916f6762011-05-25 09:53:13 -0400397void __init efi_reserve_boot_services(void)
398{
399 void *p;
400
401 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
402 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200403 u64 start = md->phys_addr;
404 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400405
406 if (md->type != EFI_BOOT_SERVICES_CODE &&
407 md->type != EFI_BOOT_SERVICES_DATA)
408 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200409 /* Only reserve where possible:
410 * - Not within any already allocated areas
411 * - Not over any memory area (really needed, if above?)
412 * - Not within any part of the kernel
413 * - Not the bios reserved area
414 */
Dave Younga7f84f02013-12-20 18:02:15 +0800415 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800416 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200417 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200418 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200419 /* Could not reserve, skip it */
420 md->num_pages = 0;
Joe Perches9b7d2042014-01-03 16:08:48 -0800421 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
422 start, start+size-1);
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200423 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200424 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400425 }
426}
427
Olof Johansson5189c2a2012-10-24 10:00:44 -0700428void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700429{
Matt Fleming3e909592014-01-15 13:21:22 +0000430 clear_bit(EFI_MEMMAP, &efi.flags);
Josh Triplett78510792012-09-28 17:55:44 -0700431 if (memmap.map) {
432 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
433 memmap.map = NULL;
434 }
435}
436
437void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400438{
439 void *p;
440
441 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
442 efi_memory_desc_t *md = p;
443 unsigned long long start = md->phys_addr;
444 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
445
446 if (md->type != EFI_BOOT_SERVICES_CODE &&
447 md->type != EFI_BOOT_SERVICES_DATA)
448 continue;
449
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200450 /* Could not reserve boot area */
451 if (!size)
452 continue;
453
Matthew Garrett916f6762011-05-25 09:53:13 -0400454 free_bootmem_late(start, size);
455 }
Josh Triplett78510792012-09-28 17:55:44 -0700456
457 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400458}
459
Olof Johansson140bf272012-02-12 13:24:28 -0800460static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100461{
Matt Fleming83e68182012-11-14 09:42:35 +0000462 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800463 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800464 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800465 u64 tmp = 0;
466
Dave Young1fec0532013-12-20 18:02:19 +0800467 if (efi_setup) {
468 data = early_memremap(efi_setup, sizeof(*data));
469 if (!data)
470 return -ENOMEM;
471 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800472 systab64 = early_ioremap((unsigned long)phys,
473 sizeof(*systab64));
474 if (systab64 == NULL) {
475 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800476 if (data)
477 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800478 return -ENOMEM;
479 }
480
481 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800482 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
483 systab64->fw_vendor;
484 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800485 efi_systab.fw_revision = systab64->fw_revision;
486 efi_systab.con_in_handle = systab64->con_in_handle;
487 tmp |= systab64->con_in_handle;
488 efi_systab.con_in = systab64->con_in;
489 tmp |= systab64->con_in;
490 efi_systab.con_out_handle = systab64->con_out_handle;
491 tmp |= systab64->con_out_handle;
492 efi_systab.con_out = systab64->con_out;
493 tmp |= systab64->con_out;
494 efi_systab.stderr_handle = systab64->stderr_handle;
495 tmp |= systab64->stderr_handle;
496 efi_systab.stderr = systab64->stderr;
497 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800498 efi_systab.runtime = data ?
499 (void *)(unsigned long)data->runtime :
500 (void *)(unsigned long)systab64->runtime;
501 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800502 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
503 tmp |= systab64->boottime;
504 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800505 efi_systab.tables = data ? (unsigned long)data->tables :
506 systab64->tables;
507 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800508
509 early_iounmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800510 if (data)
511 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800512#ifdef CONFIG_X86_32
513 if (tmp >> 32) {
514 pr_err("EFI data located above 4GB, disabling EFI.\n");
515 return -EINVAL;
516 }
517#endif
518 } else {
519 efi_system_table_32_t *systab32;
520
521 systab32 = early_ioremap((unsigned long)phys,
522 sizeof(*systab32));
523 if (systab32 == NULL) {
524 pr_err("Couldn't map the system table!\n");
525 return -ENOMEM;
526 }
527
528 efi_systab.hdr = systab32->hdr;
529 efi_systab.fw_vendor = systab32->fw_vendor;
530 efi_systab.fw_revision = systab32->fw_revision;
531 efi_systab.con_in_handle = systab32->con_in_handle;
532 efi_systab.con_in = systab32->con_in;
533 efi_systab.con_out_handle = systab32->con_out_handle;
534 efi_systab.con_out = systab32->con_out;
535 efi_systab.stderr_handle = systab32->stderr_handle;
536 efi_systab.stderr = systab32->stderr;
537 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
538 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
539 efi_systab.nr_tables = systab32->nr_tables;
540 efi_systab.tables = systab32->tables;
541
542 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800543 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800544
Huang, Ying5b836832008-01-30 13:31:19 +0100545 efi.systab = &efi_systab;
546
547 /*
548 * Verify the EFI Table
549 */
Olof Johansson140bf272012-02-12 13:24:28 -0800550 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800551 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800552 return -EINVAL;
553 }
Huang, Ying5b836832008-01-30 13:31:19 +0100554 if ((efi.systab->hdr.revision >> 16) == 0)
Joe Perches9b7d2042014-01-03 16:08:48 -0800555 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100556 efi.systab->hdr.revision >> 16,
557 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800558
Matt Fleming0f8093a2014-01-15 13:36:33 +0000559 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
560
Olof Johansson140bf272012-02-12 13:24:28 -0800561 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800562}
Huang, Ying5b836832008-01-30 13:31:19 +0100563
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000564static int __init efi_runtime_init32(void)
565{
566 efi_runtime_services_32_t *runtime;
567
568 runtime = early_ioremap((unsigned long)efi.systab->runtime,
569 sizeof(efi_runtime_services_32_t));
570 if (!runtime) {
571 pr_err("Could not map the runtime service table!\n");
572 return -ENOMEM;
573 }
574
575 /*
576 * We will only need *early* access to the following two
577 * EFI runtime services before set_virtual_address_map
578 * is invoked.
579 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000580 efi_phys.set_virtual_address_map =
581 (efi_set_virtual_address_map_t *)
582 (unsigned long)runtime->set_virtual_address_map;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000583 early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
584
585 return 0;
586}
587
588static int __init efi_runtime_init64(void)
589{
590 efi_runtime_services_64_t *runtime;
591
592 runtime = early_ioremap((unsigned long)efi.systab->runtime,
593 sizeof(efi_runtime_services_64_t));
594 if (!runtime) {
595 pr_err("Could not map the runtime service table!\n");
596 return -ENOMEM;
597 }
598
599 /*
600 * We will only need *early* access to the following two
601 * EFI runtime services before set_virtual_address_map
602 * is invoked.
603 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000604 efi_phys.set_virtual_address_map =
605 (efi_set_virtual_address_map_t *)
606 (unsigned long)runtime->set_virtual_address_map;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000607 early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
608
609 return 0;
610}
611
Olof Johansson140bf272012-02-12 13:24:28 -0800612static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800613{
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000614 int rv;
Huang, Ying5b836832008-01-30 13:31:19 +0100615
616 /*
617 * Check out the runtime services table. We need to map
618 * the runtime services table so that we can grab the physical
619 * address of several of the EFI runtime functions, needed to
620 * set the firmware into virtual mode.
621 */
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000622 if (efi_enabled(EFI_64BIT))
623 rv = efi_runtime_init64();
624 else
625 rv = efi_runtime_init32();
626
627 if (rv)
628 return rv;
Olof Johansson140bf272012-02-12 13:24:28 -0800629
Matt Fleming0f8093a2014-01-15 13:36:33 +0000630 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
631
Olof Johansson140bf272012-02-12 13:24:28 -0800632 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800633}
Huang, Ying5b836832008-01-30 13:31:19 +0100634
Olof Johansson140bf272012-02-12 13:24:28 -0800635static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800636{
Huang, Ying5b836832008-01-30 13:31:19 +0100637 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100638 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
639 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800640 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800641 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800642 return -ENOMEM;
643 }
Huang, Ying5b836832008-01-30 13:31:19 +0100644 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500645
Paul Jackson200001e2008-06-25 05:44:46 -0700646 if (add_efi_memmap)
647 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800648
Matt Fleming0f8093a2014-01-15 13:36:33 +0000649 set_bit(EFI_MEMMAP, &efi.flags);
650
Olof Johansson140bf272012-02-12 13:24:28 -0800651 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800652}
653
Dave Young1fec0532013-12-20 18:02:19 +0800654/*
655 * A number of config table entries get remapped to virtual addresses
656 * after entering EFI virtual mode. However, the kexec kernel requires
657 * their physical addresses therefore we pass them via setup_data and
658 * correct those entries to their respective physical addresses here.
659 *
660 * Currently only handles smbios which is necessary for some firmware
661 * implementation.
662 */
663static int __init efi_reuse_config(u64 tables, int nr_tables)
664{
665 int i, sz, ret = 0;
666 void *p, *tablep;
667 struct efi_setup_data *data;
668
669 if (!efi_setup)
670 return 0;
671
672 if (!efi_enabled(EFI_64BIT))
673 return 0;
674
675 data = early_memremap(efi_setup, sizeof(*data));
676 if (!data) {
677 ret = -ENOMEM;
678 goto out;
679 }
680
681 if (!data->smbios)
682 goto out_memremap;
683
684 sz = sizeof(efi_config_table_64_t);
685
686 p = tablep = early_memremap(tables, nr_tables * sz);
687 if (!p) {
688 pr_err("Could not map Configuration table!\n");
689 ret = -ENOMEM;
690 goto out_memremap;
691 }
692
693 for (i = 0; i < efi.systab->nr_tables; i++) {
694 efi_guid_t guid;
695
696 guid = ((efi_config_table_64_t *)p)->guid;
697
698 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
699 ((efi_config_table_64_t *)p)->table = data->smbios;
700 p += sz;
701 }
702 early_iounmap(tablep, nr_tables * sz);
703
704out_memremap:
705 early_iounmap(data, sizeof(*data));
706out:
707 return ret;
708}
709
Olof Johansson83e7ee62012-02-12 13:24:25 -0800710void __init efi_init(void)
711{
712 efi_char16_t *c16;
713 char vendor[100] = "unknown";
714 int i = 0;
715 void *tmp;
716
717#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800718 if (boot_params.efi_info.efi_systab_hi ||
719 boot_params.efi_info.efi_memmap_hi) {
720 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800721 return;
722 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800723 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
724#else
725 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800726 (boot_params.efi_info.efi_systab |
727 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800728#endif
729
Matt Fleming83e68182012-11-14 09:42:35 +0000730 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800731 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000732
Matt Fleming3e909592014-01-15 13:21:22 +0000733 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800734
Dave Younga0998eb2013-12-20 18:02:17 +0800735 efi.config_table = (unsigned long)efi.systab->tables;
736 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
737 efi.runtime = (unsigned long)efi.systab->runtime;
738
Olof Johansson83e7ee62012-02-12 13:24:25 -0800739 /*
740 * Show what we know for posterity
741 */
742 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
743 if (c16) {
744 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
745 vendor[i] = *c16++;
746 vendor[i] = '\0';
747 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800748 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800749 early_iounmap(tmp, 2);
750
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800751 pr_info("EFI v%u.%.02u by %s\n",
752 efi.systab->hdr.revision >> 16,
753 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800754
Dave Young1fec0532013-12-20 18:02:19 +0800755 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
756 return;
757
Leif Lindholm272686b2013-09-05 11:34:54 +0100758 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800759 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000760
Olof Johansson1adbfa32012-02-12 13:24:29 -0800761 /*
762 * Note: We currently don't support runtime services on an EFI
763 * that doesn't match the kernel 32/64-bit mode.
764 */
765
Matt Fleming7d453ee2014-01-10 18:52:06 +0000766 if (!efi_runtime_supported())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800767 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000768 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000769 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000770 return;
Olof Johansson140bf272012-02-12 13:24:28 -0800771 }
Matt Fleming83e68182012-11-14 09:42:35 +0000772 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800773 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000774
Matt Fleming3e909592014-01-15 13:21:22 +0000775 set_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming83e68182012-11-14 09:42:35 +0000776
Huang, Ying5b836832008-01-30 13:31:19 +0100777 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100778}
779
Josh Triplett2223af32012-09-28 17:57:05 -0700780void __init efi_late_init(void)
781{
782 efi_bgrt_init();
783}
784
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400785void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
786{
787 u64 addr, npages;
788
789 addr = md->virt_addr;
790 npages = md->num_pages;
791
792 memrange_efi_to_native(&addr, &npages);
793
794 if (executable)
795 set_memory_x(addr, npages);
796 else
797 set_memory_nx(addr, npages);
798}
799
Borislav Petkovc55d0162014-02-14 08:24:24 +0100800void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100801{
802 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100803 void *p;
804
Huang, Yinga2172e22008-01-30 13:33:55 +0100805 /* Make EFI runtime service code area executable */
806 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
807 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100808
809 if (md->type != EFI_RUNTIME_SERVICES_CODE)
810 continue;
811
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400812 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100813 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100814}
Huang, Yinga2172e22008-01-30 13:33:55 +0100815
Matt Fleming3e8fa262012-10-19 13:25:46 +0100816void efi_memory_uc(u64 addr, unsigned long size)
817{
818 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
819 u64 npages;
820
821 npages = round_up(size, page_shift) / page_shift;
822 memrange_efi_to_native(&addr, &npages);
823 set_memory_uc(addr, npages);
824}
825
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100826void __init old_map_region(efi_memory_desc_t *md)
827{
828 u64 start_pfn, end_pfn, end;
829 unsigned long size;
830 void *va;
831
832 start_pfn = PFN_DOWN(md->phys_addr);
833 size = md->num_pages << PAGE_SHIFT;
834 end = md->phys_addr + size;
835 end_pfn = PFN_UP(end);
836
837 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
838 va = __va(md->phys_addr);
839
840 if (!(md->attribute & EFI_MEMORY_WB))
841 efi_memory_uc((u64)(unsigned long)va, size);
842 } else
843 va = efi_ioremap(md->phys_addr, size,
844 md->type, md->attribute);
845
846 md->virt_addr = (u64) (unsigned long) va;
847 if (!va)
848 pr_err("ioremap of 0x%llX failed!\n",
849 (unsigned long long)md->phys_addr);
850}
851
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000852static void native_runtime_setup(void)
853{
854 efi.get_time = virt_efi_get_time;
855 efi.set_time = virt_efi_set_time;
856 efi.get_wakeup_time = virt_efi_get_wakeup_time;
857 efi.set_wakeup_time = virt_efi_set_wakeup_time;
858 efi.get_variable = virt_efi_get_variable;
859 efi.get_next_variable = virt_efi_get_next_variable;
860 efi.set_variable = virt_efi_set_variable;
861 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
862 efi.reset_system = virt_efi_reset_system;
863 efi.query_variable_info = virt_efi_query_variable_info;
864 efi.update_capsule = virt_efi_update_capsule;
865 efi.query_capsule_caps = virt_efi_query_capsule_caps;
866}
867
Dave Young481f75c2013-12-20 18:02:16 +0800868/* Merge contiguous regions of the same type and attribute */
869static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100870{
Dave Young481f75c2013-12-20 18:02:16 +0800871 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400872 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100873
Matthew Garrett202f9d02011-05-05 15:19:44 -0400874 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
875 u64 prev_size;
876 md = p;
877
878 if (!prev_md) {
879 prev_md = md;
880 continue;
881 }
882
883 if (prev_md->type != md->type ||
884 prev_md->attribute != md->attribute) {
885 prev_md = md;
886 continue;
887 }
888
889 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
890
891 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
892 prev_md->num_pages += md->num_pages;
893 md->type = EFI_RESERVED_TYPE;
894 md->attribute = 0;
895 continue;
896 }
897 prev_md = md;
898 }
Dave Young481f75c2013-12-20 18:02:16 +0800899}
900
901static void __init get_systab_virt_addr(efi_memory_desc_t *md)
902{
903 unsigned long size;
904 u64 end, systab;
905
906 size = md->num_pages << EFI_PAGE_SHIFT;
907 end = md->phys_addr + size;
908 systab = (u64)(unsigned long)efi_phys.systab;
909 if (md->phys_addr <= systab && systab < end) {
910 systab += md->virt_addr - md->phys_addr;
911 efi.systab = (efi_system_table_t *)(unsigned long)systab;
912 }
913}
914
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100915static void __init save_runtime_map(void)
Dave Young926172d2013-12-20 18:02:18 +0800916{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100917#ifdef CONFIG_KEXEC
Dave Young926172d2013-12-20 18:02:18 +0800918 efi_memory_desc_t *md;
919 void *tmp, *p, *q = NULL;
920 int count = 0;
921
922 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
923 md = p;
924
925 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
926 (md->type == EFI_BOOT_SERVICES_CODE) ||
927 (md->type == EFI_BOOT_SERVICES_DATA))
928 continue;
929 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
930 if (!tmp)
931 goto out;
932 q = tmp;
933
934 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
935 count++;
936 }
937
Matt Fleming518548a2013-12-21 16:09:46 +0000938 efi_runtime_map_setup(q, count, memmap.desc_size);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100939 return;
Dave Young926172d2013-12-20 18:02:18 +0800940
Dave Young926172d2013-12-20 18:02:18 +0800941out:
942 kfree(q);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100943 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
944#endif
Dave Young1fec0532013-12-20 18:02:19 +0800945}
946
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100947static void *realloc_pages(void *old_memmap, int old_shift)
Dave Young481f75c2013-12-20 18:02:16 +0800948{
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100949 void *ret;
950
951 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
952 if (!ret)
953 goto out;
954
955 /*
956 * A first-time allocation doesn't have anything to copy.
957 */
958 if (!old_memmap)
959 return ret;
960
961 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
962
963out:
964 free_pages((unsigned long)old_memmap, old_shift);
965 return ret;
966}
967
968/*
969 * Map the efi memory ranges of the runtime services and update new_mmap with
970 * virtual addresses.
971 */
972static void * __init efi_map_regions(int *count, int *pg_shift)
973{
974 void *p, *new_memmap = NULL;
975 unsigned long left = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800976 efi_memory_desc_t *md;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400977
Huang, Ying5b836832008-01-30 13:31:19 +0100978 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
979 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700980 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
981#ifdef CONFIG_X86_64
982 if (md->type != EFI_BOOT_SERVICES_CODE &&
983 md->type != EFI_BOOT_SERVICES_DATA)
984#endif
985 continue;
986 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100987
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100988 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800989 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100990
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100991 if (left < memmap.desc_size) {
992 new_memmap = realloc_pages(new_memmap, *pg_shift);
993 if (!new_memmap)
994 return NULL;
995
996 left += PAGE_SIZE << *pg_shift;
997 (*pg_shift)++;
998 }
999
Dave Young481f75c2013-12-20 18:02:16 +08001000 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001001 memmap.desc_size);
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001002
1003 left -= memmap.desc_size;
Dave Young481f75c2013-12-20 18:02:16 +08001004 (*count)++;
1005 }
1006
1007 return new_memmap;
Dave Young481f75c2013-12-20 18:02:16 +08001008}
1009
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001010static void __init kexec_enter_virtual_mode(void)
1011{
1012#ifdef CONFIG_KEXEC
1013 efi_memory_desc_t *md;
1014 void *p;
1015
1016 efi.systab = NULL;
1017
1018 /*
1019 * We don't do virtual mode, since we don't do runtime services, on
1020 * non-native EFI
1021 */
1022 if (!efi_is_native()) {
1023 efi_unmap_memmap();
1024 return;
1025 }
1026
1027 /*
1028 * Map efi regions which were passed via setup_data. The virt_addr is a
1029 * fixed addr which was used in first kernel of a kexec boot.
1030 */
1031 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1032 md = p;
1033 efi_map_region_fixed(md); /* FIXME: add error handling */
1034 get_systab_virt_addr(md);
1035 }
1036
1037 save_runtime_map();
1038
1039 BUG_ON(!efi.systab);
1040
1041 efi_sync_low_kernel_mappings();
1042
1043 /*
1044 * Now that EFI is in virtual mode, update the function
1045 * pointers in the runtime service table to the new virtual addresses.
1046 *
1047 * Call EFI services through wrapper functions.
1048 */
1049 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming994448f2014-03-05 18:15:37 +00001050
1051 native_runtime_setup();
1052
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001053 efi.set_virtual_address_map = NULL;
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001054
1055 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1056 runtime_code_page_mkexec();
1057
1058 /* clean DUMMY object */
1059 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1060 EFI_VARIABLE_NON_VOLATILE |
1061 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1062 EFI_VARIABLE_RUNTIME_ACCESS,
1063 0, NULL);
1064#endif
1065}
1066
Dave Young481f75c2013-12-20 18:02:16 +08001067/*
1068 * This function will switch the EFI runtime services to virtual mode.
1069 * Essentially, we look through the EFI memmap and map every region that
1070 * has the runtime attribute bit set in its memory descriptor into the
1071 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1072 *
1073 * The old method which used to update that memory descriptor with the
1074 * virtual address obtained from ioremap() is still supported when the
1075 * kernel is booted with efi=old_map on its command line. Same old
1076 * method enabled the runtime services to be called without having to
1077 * thunk back into physical mode for every invocation.
1078 *
1079 * The new method does a pagetable switch in a preemption-safe manner
1080 * so that we're in a different address space when calling a runtime
1081 * function. For function arguments passing we do copy the PGDs of the
1082 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +08001083 *
1084 * Specially for kexec boot, efi runtime maps in previous kernel should
1085 * be passed in via setup_data. In that case runtime ranges will be mapped
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001086 * to the same virtual addresses as the first kernel, see
1087 * kexec_enter_virtual_mode().
Dave Young481f75c2013-12-20 18:02:16 +08001088 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001089static void __init __efi_enter_virtual_mode(void)
Dave Young481f75c2013-12-20 18:02:16 +08001090{
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001091 int count = 0, pg_shift = 0;
Dave Young481f75c2013-12-20 18:02:16 +08001092 void *new_memmap = NULL;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001093 efi_status_t status;
Dave Young481f75c2013-12-20 18:02:16 +08001094
1095 efi.systab = NULL;
1096
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001097 efi_merge_regions();
1098 new_memmap = efi_map_regions(&count, &pg_shift);
1099 if (!new_memmap) {
1100 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1101 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001102 }
Dave Young926172d2013-12-20 18:02:18 +08001103
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001104 save_runtime_map();
1105
Huang, Ying5b836832008-01-30 13:31:19 +01001106 BUG_ON(!efi.systab);
1107
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001108 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1109 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001110
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001111 efi_sync_low_kernel_mappings();
Borislav Petkov11cc8512014-01-18 12:48:15 +01001112 efi_dump_pagetable();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001113
Matt Fleming994448f2014-03-05 18:15:37 +00001114 if (efi_is_native()) {
1115 status = phys_efi_set_virtual_address_map(
1116 memmap.desc_size * count,
1117 memmap.desc_size,
1118 memmap.desc_version,
1119 (efi_memory_desc_t *)__pa(new_memmap));
1120 } else {
1121 status = efi_thunk_set_virtual_address_map(
1122 efi_phys.set_virtual_address_map,
1123 memmap.desc_size * count,
1124 memmap.desc_size,
1125 memmap.desc_version,
1126 (efi_memory_desc_t *)__pa(new_memmap));
1127 }
Huang, Ying5b836832008-01-30 13:31:19 +01001128
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001129 if (status != EFI_SUCCESS) {
1130 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1131 status);
1132 panic("EFI call to SetVirtualAddressMap() failed!");
Huang, Ying5b836832008-01-30 13:31:19 +01001133 }
1134
1135 /*
1136 * Now that EFI is in virtual mode, update the function
1137 * pointers in the runtime service table to the new virtual addresses.
1138 *
1139 * Call EFI services through wrapper functions.
1140 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001141 efi.runtime_version = efi_systab.hdr.revision;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +00001142
1143 if (efi_is_native())
1144 native_runtime_setup();
1145 else
1146 efi_thunk_runtime_setup();
1147
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001148 efi.set_virtual_address_map = NULL;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001149
Borislav Petkovc55d0162014-02-14 08:24:24 +01001150 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001151
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001152 /*
1153 * We mapped the descriptor array into the EFI pagetable above but we're
1154 * not unmapping it here. Here's why:
1155 *
1156 * We're copying select PGDs from the kernel page table to the EFI page
1157 * table and when we do so and make changes to those PGDs like unmapping
1158 * stuff from them, those changes appear in the kernel page table and we
1159 * go boom.
1160 *
1161 * From setup_real_mode():
1162 *
1163 * ...
1164 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1165 *
1166 * In this particular case, our allocation is in PGD 0 of the EFI page
1167 * table but we've copied that PGD from PGD[272] of the EFI page table:
1168 *
1169 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1170 *
1171 * where the direct memory mapping in kernel space is.
1172 *
1173 * new_memmap's VA comes from that direct mapping and thus clearing it,
1174 * it would get cleared in the kernel page table too.
1175 *
1176 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1177 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001178 free_pages((unsigned long)new_memmap, pg_shift);
Matthew Garrettf8b84042013-06-01 16:06:20 -04001179
1180 /* clean DUMMY object */
1181 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1182 EFI_VARIABLE_NON_VOLATILE |
1183 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1184 EFI_VARIABLE_RUNTIME_ACCESS,
1185 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001186}
1187
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001188void __init efi_enter_virtual_mode(void)
1189{
1190 if (efi_setup)
1191 kexec_enter_virtual_mode();
1192 else
1193 __efi_enter_virtual_mode();
1194}
1195
Huang, Ying5b836832008-01-30 13:31:19 +01001196/*
1197 * Convenience functions to obtain memory types and attributes
1198 */
1199u32 efi_mem_type(unsigned long phys_addr)
1200{
1201 efi_memory_desc_t *md;
1202 void *p;
1203
Matt Fleming83e68182012-11-14 09:42:35 +00001204 if (!efi_enabled(EFI_MEMMAP))
1205 return 0;
1206
Huang, Ying5b836832008-01-30 13:31:19 +01001207 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1208 md = p;
1209 if ((md->phys_addr <= phys_addr) &&
1210 (phys_addr < (md->phys_addr +
1211 (md->num_pages << EFI_PAGE_SHIFT))))
1212 return md->type;
1213 }
1214 return 0;
1215}
1216
1217u64 efi_mem_attributes(unsigned long phys_addr)
1218{
1219 efi_memory_desc_t *md;
1220 void *p;
1221
1222 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1223 md = p;
1224 if ((md->phys_addr <= phys_addr) &&
1225 (phys_addr < (md->phys_addr +
1226 (md->num_pages << EFI_PAGE_SHIFT))))
1227 return md->attribute;
1228 }
1229 return 0;
1230}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001231
1232/*
Madper Xie5db80c62013-11-10 22:15:11 +08001233 * Some firmware implementations refuse to boot if there's insufficient space
1234 * in the variable store. Ensure that we never use more than a safe limit.
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001235 *
1236 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1237 * store.
1238 */
1239efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1240{
1241 efi_status_t status;
1242 u64 storage_size, remaining_size, max_size;
1243
Matthew Garrettf8b84042013-06-01 16:06:20 -04001244 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1245 return 0;
1246
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001247 status = efi.query_variable_info(attributes, &storage_size,
1248 &remaining_size, &max_size);
1249 if (status != EFI_SUCCESS)
1250 return status;
1251
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001252 /*
Madper Xie5db80c62013-11-10 22:15:11 +08001253 * We account for that by refusing the write if permitting it would
1254 * reduce the available space to under 5KB. This figure was provided by
1255 * Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001256 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001257 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1258 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001259
Matthew Garrettf8b84042013-06-01 16:06:20 -04001260 /*
1261 * Triggering garbage collection may require that the firmware
1262 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1263 * that by attempting to use more space than is available.
1264 */
1265 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001266 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1267
1268 if (!dummy)
1269 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001270
Matthew Garrettf8b84042013-06-01 16:06:20 -04001271 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1272 EFI_VARIABLE_NON_VOLATILE |
1273 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1274 EFI_VARIABLE_RUNTIME_ACCESS,
1275 dummy_size, dummy);
1276
1277 if (status == EFI_SUCCESS) {
1278 /*
1279 * This should have failed, so if it didn't make sure
1280 * that we delete it...
1281 */
1282 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1283 EFI_VARIABLE_NON_VOLATILE |
1284 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1285 EFI_VARIABLE_RUNTIME_ACCESS,
1286 0, dummy);
1287 }
1288
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001289 kfree(dummy);
1290
Matthew Garrettf8b84042013-06-01 16:06:20 -04001291 /*
1292 * The runtime code may now have triggered a garbage collection
1293 * run, so check the variable info again
1294 */
1295 status = efi.query_variable_info(attributes, &storage_size,
1296 &remaining_size, &max_size);
1297
1298 if (status != EFI_SUCCESS)
1299 return status;
1300
1301 /*
1302 * There still isn't enough room, so return an error
1303 */
1304 if (remaining_size - size < EFI_MIN_RESERVE)
1305 return EFI_OUT_OF_RESOURCES;
1306 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001307
1308 return EFI_SUCCESS;
1309}
Sergey Vlasov36680112013-04-16 18:31:09 +04001310EXPORT_SYMBOL_GPL(efi_query_variable_store);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001311
1312static int __init parse_efi_cmdline(char *str)
1313{
1314 if (*str == '=')
1315 str++;
1316
1317 if (!strncmp(str, "old_map", 7))
Matt Fleming0f8093a2014-01-15 13:36:33 +00001318 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001319
1320 return 0;
1321}
1322early_param("efi", parse_efi_cmdline);
Borislav Petkova5d90c92014-03-04 17:02:17 +01001323
1324void __init efi_apply_memmap_quirks(void)
1325{
1326 /*
1327 * Once setup is done earlier, unmap the EFI memory map on mismatched
1328 * firmware/kernel architectures since there is no support for runtime
1329 * services.
1330 */
Matt Fleming994448f2014-03-05 18:15:37 +00001331 if (!efi_runtime_supported()) {
Borislav Petkova5d90c92014-03-04 17:02:17 +01001332 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1333 efi_unmap_memmap();
1334 }
1335
1336 /*
1337 * UV doesn't support the new EFI pagetable mapping yet.
1338 */
1339 if (is_uv_system())
Matt Fleming4fd69332014-03-05 17:22:57 +00001340 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkova5d90c92014-03-04 17:02:17 +01001341}