blob: b97acecf3fd95667e2b67bba8f88bb80428480bb [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
Matt Fleming83e68182012-11-14 09:42:35 +000071unsigned long x86_efi_facility;
72
Leif Lindholm272686b2013-09-05 11:34:54 +010073static __initdata efi_config_table_type_t arch_tables[] = {
74#ifdef CONFIG_X86_UV
75 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
76#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010077 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010078};
79
Dave Young1fec0532013-12-20 18:02:19 +080080u64 efi_setup; /* efi setup_data physical address */
Dave Young926172d2013-12-20 18:02:18 +080081
Matt Fleming83e68182012-11-14 09:42:35 +000082/*
83 * Returns 1 if 'facility' is enabled, 0 otherwise.
84 */
85int efi_enabled(int facility)
86{
87 return test_bit(facility, &x86_efi_facility) != 0;
88}
89EXPORT_SYMBOL(efi_enabled);
90
Matt Fleming058e7b52013-02-22 15:03:47 +000091static bool __initdata disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010092static int __init setup_noefi(char *arg)
93{
Matt Flemingfb834c72013-02-20 20:36:12 +000094 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010095 return 0;
96}
97early_param("noefi", setup_noefi);
98
Paul Jackson200001e2008-06-25 05:44:46 -070099int add_efi_memmap;
100EXPORT_SYMBOL(add_efi_memmap);
101
102static int __init setup_add_efi_memmap(char *arg)
103{
104 add_efi_memmap = 1;
105 return 0;
106}
107early_param("add_efi_memmap", setup_add_efi_memmap);
108
Richard Weinberger8c58bf32013-04-17 01:00:53 +0200109static bool efi_no_storage_paranoia;
110
111static int __init setup_storage_paranoia(char *arg)
112{
113 efi_no_storage_paranoia = true;
114 return 0;
115}
116early_param("efi_no_storage_paranoia", setup_storage_paranoia);
117
Huang, Ying5b836832008-01-30 13:31:19 +0100118static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
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_virt2(get_time, tm, tc);
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_set_time(efi_time_t *tm)
130{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100131 unsigned long flags;
132 efi_status_t status;
133
134 spin_lock_irqsave(&rtc_lock, flags);
135 status = efi_call_virt1(set_time, tm);
136 spin_unlock_irqrestore(&rtc_lock, flags);
137 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100138}
139
140static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
141 efi_bool_t *pending,
142 efi_time_t *tm)
143{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100144 unsigned long flags;
145 efi_status_t status;
146
147 spin_lock_irqsave(&rtc_lock, flags);
148 status = efi_call_virt3(get_wakeup_time,
149 enabled, pending, tm);
150 spin_unlock_irqrestore(&rtc_lock, flags);
151 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100152}
153
154static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
155{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100156 unsigned long flags;
157 efi_status_t status;
158
159 spin_lock_irqsave(&rtc_lock, flags);
160 status = efi_call_virt2(set_wakeup_time,
161 enabled, tm);
162 spin_unlock_irqrestore(&rtc_lock, flags);
163 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100164}
165
166static efi_status_t virt_efi_get_variable(efi_char16_t *name,
167 efi_guid_t *vendor,
168 u32 *attr,
169 unsigned long *data_size,
170 void *data)
171{
172 return efi_call_virt5(get_variable,
173 name, vendor, attr,
174 data_size, data);
175}
176
177static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
178 efi_char16_t *name,
179 efi_guid_t *vendor)
180{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400181 return efi_call_virt3(get_next_variable,
182 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100183}
184
185static efi_status_t virt_efi_set_variable(efi_char16_t *name,
186 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400187 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100188 unsigned long data_size,
189 void *data)
190{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400191 return efi_call_virt5(set_variable,
192 name, vendor, attr,
193 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100194}
195
Matthew Garrett3b370232011-06-06 15:36:25 -0400196static efi_status_t virt_efi_query_variable_info(u32 attr,
197 u64 *storage_space,
198 u64 *remaining_space,
199 u64 *max_variable_size)
200{
201 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
202 return EFI_UNSUPPORTED;
203
204 return efi_call_virt4(query_variable_info, attr, storage_space,
205 remaining_space, max_variable_size);
206}
207
Huang, Ying5b836832008-01-30 13:31:19 +0100208static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
209{
210 return efi_call_virt1(get_next_high_mono_count, count);
211}
212
213static void virt_efi_reset_system(int reset_type,
214 efi_status_t status,
215 unsigned long data_size,
216 efi_char16_t *data)
217{
218 efi_call_virt4(reset_system, reset_type, status,
219 data_size, data);
220}
221
Matthew Garrett3b370232011-06-06 15:36:25 -0400222static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
223 unsigned long count,
224 unsigned long sg_list)
225{
226 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
227 return EFI_UNSUPPORTED;
228
229 return efi_call_virt3(update_capsule, capsules, count, sg_list);
230}
231
232static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
233 unsigned long count,
234 u64 *max_size,
235 int *reset_type)
236{
237 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
238 return EFI_UNSUPPORTED;
239
240 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
241 reset_type);
242}
243
Huang, Ying5b836832008-01-30 13:31:19 +0100244static efi_status_t __init phys_efi_set_virtual_address_map(
245 unsigned long memory_map_size,
246 unsigned long descriptor_size,
247 u32 descriptor_version,
248 efi_memory_desc_t *virtual_map)
249{
250 efi_status_t status;
251
252 efi_call_phys_prelog();
253 status = efi_call_phys4(efi_phys.set_virtual_address_map,
254 memory_map_size, descriptor_size,
255 descriptor_version, virtual_map);
256 efi_call_phys_epilog();
257 return status;
258}
259
Linus Torvalds11520e52012-12-15 15:15:24 -0800260static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
261 efi_time_cap_t *tc)
262{
263 unsigned long flags;
264 efi_status_t status;
265
266 spin_lock_irqsave(&rtc_lock, flags);
267 efi_call_phys_prelog();
268 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
269 virt_to_phys(tc));
270 efi_call_phys_epilog();
271 spin_unlock_irqrestore(&rtc_lock, flags);
272 return status;
273}
274
David Vrabel35651842013-05-13 18:56:06 +0100275int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100276{
David Vrabel35651842013-05-13 18:56:06 +0100277 unsigned long nowtime = now->tv_sec;
Huang, Ying5b836832008-01-30 13:31:19 +0100278 efi_status_t status;
279 efi_time_t eft;
280 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500281 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100282
283 status = efi.get_time(&eft, &cap);
284 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800285 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100286 return -1;
287 }
288
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500289 rtc_time_to_tm(nowtime, &tm);
290 if (!rtc_valid_tm(&tm)) {
291 eft.year = tm.tm_year + 1900;
292 eft.month = tm.tm_mon + 1;
293 eft.day = tm.tm_mday;
294 eft.minute = tm.tm_min;
295 eft.second = tm.tm_sec;
296 eft.nanosecond = 0;
297 } else {
298 printk(KERN_ERR
299 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
300 __FUNCTION__, nowtime);
301 return -1;
302 }
Huang, Ying5b836832008-01-30 13:31:19 +0100303
304 status = efi.set_time(&eft);
305 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800306 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100307 return -1;
308 }
309 return 0;
310}
311
David Vrabel35651842013-05-13 18:56:06 +0100312void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100313{
314 efi_status_t status;
315 efi_time_t eft;
316 efi_time_cap_t cap;
317
318 status = efi.get_time(&eft, &cap);
319 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800320 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100321
David Vrabel35651842013-05-13 18:56:06 +0100322 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
323 eft.minute, eft.second);
324 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100325}
326
Paul Jackson69c91892008-05-14 08:15:58 -0700327/*
328 * Tell the kernel about the EFI memory map. This might include
329 * more than the max 128 entries that can fit in the e820 legacy
330 * (zeropage) memory map.
331 */
332
Paul Jackson200001e2008-06-25 05:44:46 -0700333static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700334{
335 void *p;
336
337 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
338 efi_memory_desc_t *md = p;
339 unsigned long long start = md->phys_addr;
340 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
341 int e820_type;
342
Cliff Wickmane2a71472009-06-16 16:43:40 -0500343 switch (md->type) {
344 case EFI_LOADER_CODE:
345 case EFI_LOADER_DATA:
346 case EFI_BOOT_SERVICES_CODE:
347 case EFI_BOOT_SERVICES_DATA:
348 case EFI_CONVENTIONAL_MEMORY:
349 if (md->attribute & EFI_MEMORY_WB)
350 e820_type = E820_RAM;
351 else
352 e820_type = E820_RESERVED;
353 break;
354 case EFI_ACPI_RECLAIM_MEMORY:
355 e820_type = E820_ACPI;
356 break;
357 case EFI_ACPI_MEMORY_NVS:
358 e820_type = E820_NVS;
359 break;
360 case EFI_UNUSABLE_MEMORY:
361 e820_type = E820_UNUSABLE;
362 break;
363 default:
364 /*
365 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
366 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
367 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
368 */
Paul Jackson69c91892008-05-14 08:15:58 -0700369 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500370 break;
371 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700372 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700373 }
374 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
375}
376
Olof Johansson1adbfa32012-02-12 13:24:29 -0800377int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800378{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100379 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800380 unsigned long pmap;
381
Paul Jackson05486fa2008-06-22 07:22:02 -0700382#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800383 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100384 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800385 pr_err("Memory map is above 4GB, disabling EFI.\n");
386 return -EINVAL;
387 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100388 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700389#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100390 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800391#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100392 memmap.phys_map = (void *)pmap;
393 memmap.nr_map = e->efi_memmap_size /
394 e->efi_memdesc_size;
395 memmap.desc_size = e->efi_memdesc_size;
396 memmap.desc_version = e->efi_memdesc_version;
397
Tejun Heo24aa0782011-07-12 11:16:06 +0200398 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800399
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100400 efi.memmap = &memmap;
401
Olof Johansson1adbfa32012-02-12 13:24:29 -0800402 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800403}
404
Huang, Ying5b836832008-01-30 13:31:19 +0100405static void __init print_efi_memmap(void)
406{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100407#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100408 efi_memory_desc_t *md;
409 void *p;
410 int i;
411
412 for (p = memmap.map, i = 0;
413 p < memmap.map_end;
414 p += memmap.desc_size, i++) {
415 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800416 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100417 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
418 i, md->type, md->attribute, md->phys_addr,
419 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
420 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
421 }
Huang, Ying5b836832008-01-30 13:31:19 +0100422#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100423}
Huang, Ying5b836832008-01-30 13:31:19 +0100424
Matthew Garrett916f6762011-05-25 09:53:13 -0400425void __init efi_reserve_boot_services(void)
426{
427 void *p;
428
429 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
430 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200431 u64 start = md->phys_addr;
432 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400433
434 if (md->type != EFI_BOOT_SERVICES_CODE &&
435 md->type != EFI_BOOT_SERVICES_DATA)
436 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200437 /* Only reserve where possible:
438 * - Not within any already allocated areas
439 * - Not over any memory area (really needed, if above?)
440 * - Not within any part of the kernel
441 * - Not the bios reserved area
442 */
Dave Younga7f84f02013-12-20 18:02:15 +0800443 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800444 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200445 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200446 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200447 /* Could not reserve, skip it */
448 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800449 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200450 "[0x%010llx-0x%010llx]\n",
451 start, start+size-1);
452 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200453 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400454 }
455}
456
Olof Johansson5189c2a2012-10-24 10:00:44 -0700457void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700458{
Matt Fleming83e68182012-11-14 09:42:35 +0000459 clear_bit(EFI_MEMMAP, &x86_efi_facility);
Josh Triplett78510792012-09-28 17:55:44 -0700460 if (memmap.map) {
461 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
462 memmap.map = NULL;
463 }
464}
465
466void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400467{
468 void *p;
469
Olof Johansson5189c2a2012-10-24 10:00:44 -0700470 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700471 return;
472
Matthew Garrett916f6762011-05-25 09:53:13 -0400473 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
474 efi_memory_desc_t *md = p;
475 unsigned long long start = md->phys_addr;
476 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
477
478 if (md->type != EFI_BOOT_SERVICES_CODE &&
479 md->type != EFI_BOOT_SERVICES_DATA)
480 continue;
481
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200482 /* Could not reserve boot area */
483 if (!size)
484 continue;
485
Matthew Garrett916f6762011-05-25 09:53:13 -0400486 free_bootmem_late(start, size);
487 }
Josh Triplett78510792012-09-28 17:55:44 -0700488
489 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400490}
491
Olof Johansson140bf272012-02-12 13:24:28 -0800492static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100493{
Matt Fleming83e68182012-11-14 09:42:35 +0000494 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800495 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800496 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800497 u64 tmp = 0;
498
Dave Young1fec0532013-12-20 18:02:19 +0800499 if (efi_setup) {
500 data = early_memremap(efi_setup, sizeof(*data));
501 if (!data)
502 return -ENOMEM;
503 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800504 systab64 = early_ioremap((unsigned long)phys,
505 sizeof(*systab64));
506 if (systab64 == NULL) {
507 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800508 if (data)
509 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800510 return -ENOMEM;
511 }
512
513 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800514 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
515 systab64->fw_vendor;
516 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800517 efi_systab.fw_revision = systab64->fw_revision;
518 efi_systab.con_in_handle = systab64->con_in_handle;
519 tmp |= systab64->con_in_handle;
520 efi_systab.con_in = systab64->con_in;
521 tmp |= systab64->con_in;
522 efi_systab.con_out_handle = systab64->con_out_handle;
523 tmp |= systab64->con_out_handle;
524 efi_systab.con_out = systab64->con_out;
525 tmp |= systab64->con_out;
526 efi_systab.stderr_handle = systab64->stderr_handle;
527 tmp |= systab64->stderr_handle;
528 efi_systab.stderr = systab64->stderr;
529 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800530 efi_systab.runtime = data ?
531 (void *)(unsigned long)data->runtime :
532 (void *)(unsigned long)systab64->runtime;
533 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800534 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
535 tmp |= systab64->boottime;
536 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800537 efi_systab.tables = data ? (unsigned long)data->tables :
538 systab64->tables;
539 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800540
541 early_iounmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800542 if (data)
543 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800544#ifdef CONFIG_X86_32
545 if (tmp >> 32) {
546 pr_err("EFI data located above 4GB, disabling EFI.\n");
547 return -EINVAL;
548 }
549#endif
550 } else {
551 efi_system_table_32_t *systab32;
552
553 systab32 = early_ioremap((unsigned long)phys,
554 sizeof(*systab32));
555 if (systab32 == NULL) {
556 pr_err("Couldn't map the system table!\n");
557 return -ENOMEM;
558 }
559
560 efi_systab.hdr = systab32->hdr;
561 efi_systab.fw_vendor = systab32->fw_vendor;
562 efi_systab.fw_revision = systab32->fw_revision;
563 efi_systab.con_in_handle = systab32->con_in_handle;
564 efi_systab.con_in = systab32->con_in;
565 efi_systab.con_out_handle = systab32->con_out_handle;
566 efi_systab.con_out = systab32->con_out;
567 efi_systab.stderr_handle = systab32->stderr_handle;
568 efi_systab.stderr = systab32->stderr;
569 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
570 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
571 efi_systab.nr_tables = systab32->nr_tables;
572 efi_systab.tables = systab32->tables;
573
574 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800575 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800576
Huang, Ying5b836832008-01-30 13:31:19 +0100577 efi.systab = &efi_systab;
578
579 /*
580 * Verify the EFI Table
581 */
Olof Johansson140bf272012-02-12 13:24:28 -0800582 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800583 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800584 return -EINVAL;
585 }
Huang, Ying5b836832008-01-30 13:31:19 +0100586 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800587 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100588 "%d.%02d, expected 1.00 or greater!\n",
589 efi.systab->hdr.revision >> 16,
590 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800591
592 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800593}
Huang, Ying5b836832008-01-30 13:31:19 +0100594
Olof Johansson140bf272012-02-12 13:24:28 -0800595static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800596{
597 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100598
599 /*
600 * Check out the runtime services table. We need to map
601 * the runtime services table so that we can grab the physical
602 * address of several of the EFI runtime functions, needed to
603 * set the firmware into virtual mode.
604 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100605 runtime = early_ioremap((unsigned long)efi.systab->runtime,
606 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800607 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800608 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800609 return -ENOMEM;
610 }
611 /*
612 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800613 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800614 * is invoked.
615 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800616 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800617 efi_phys.set_virtual_address_map =
618 (efi_set_virtual_address_map_t *)
619 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800620 /*
621 * Make efi_get_time can be called before entering
622 * virtual mode.
623 */
624 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100625 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800626
627 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800628}
Huang, Ying5b836832008-01-30 13:31:19 +0100629
Olof Johansson140bf272012-02-12 13:24:28 -0800630static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800631{
Huang, Ying5b836832008-01-30 13:31:19 +0100632 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100633 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
634 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800635 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800636 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800637 return -ENOMEM;
638 }
Huang, Ying5b836832008-01-30 13:31:19 +0100639 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500640
Paul Jackson200001e2008-06-25 05:44:46 -0700641 if (add_efi_memmap)
642 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800643
644 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800645}
646
Dave Young1fec0532013-12-20 18:02:19 +0800647/*
648 * A number of config table entries get remapped to virtual addresses
649 * after entering EFI virtual mode. However, the kexec kernel requires
650 * their physical addresses therefore we pass them via setup_data and
651 * correct those entries to their respective physical addresses here.
652 *
653 * Currently only handles smbios which is necessary for some firmware
654 * implementation.
655 */
656static int __init efi_reuse_config(u64 tables, int nr_tables)
657{
658 int i, sz, ret = 0;
659 void *p, *tablep;
660 struct efi_setup_data *data;
661
662 if (!efi_setup)
663 return 0;
664
665 if (!efi_enabled(EFI_64BIT))
666 return 0;
667
668 data = early_memremap(efi_setup, sizeof(*data));
669 if (!data) {
670 ret = -ENOMEM;
671 goto out;
672 }
673
674 if (!data->smbios)
675 goto out_memremap;
676
677 sz = sizeof(efi_config_table_64_t);
678
679 p = tablep = early_memremap(tables, nr_tables * sz);
680 if (!p) {
681 pr_err("Could not map Configuration table!\n");
682 ret = -ENOMEM;
683 goto out_memremap;
684 }
685
686 for (i = 0; i < efi.systab->nr_tables; i++) {
687 efi_guid_t guid;
688
689 guid = ((efi_config_table_64_t *)p)->guid;
690
691 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
692 ((efi_config_table_64_t *)p)->table = data->smbios;
693 p += sz;
694 }
695 early_iounmap(tablep, nr_tables * sz);
696
697out_memremap:
698 early_iounmap(data, sizeof(*data));
699out:
700 return ret;
701}
702
Olof Johansson83e7ee62012-02-12 13:24:25 -0800703void __init efi_init(void)
704{
705 efi_char16_t *c16;
706 char vendor[100] = "unknown";
707 int i = 0;
708 void *tmp;
709
710#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800711 if (boot_params.efi_info.efi_systab_hi ||
712 boot_params.efi_info.efi_memmap_hi) {
713 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800714 return;
715 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800716 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
717#else
718 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800719 (boot_params.efi_info.efi_systab |
720 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800721#endif
722
Matt Fleming83e68182012-11-14 09:42:35 +0000723 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800724 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000725
726 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800727
Dave Younga0998eb2013-12-20 18:02:17 +0800728 efi.config_table = (unsigned long)efi.systab->tables;
729 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
730 efi.runtime = (unsigned long)efi.systab->runtime;
731
Olof Johansson83e7ee62012-02-12 13:24:25 -0800732 /*
733 * Show what we know for posterity
734 */
735 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
736 if (c16) {
737 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
738 vendor[i] = *c16++;
739 vendor[i] = '\0';
740 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800741 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800742 early_iounmap(tmp, 2);
743
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800744 pr_info("EFI v%u.%.02u by %s\n",
745 efi.systab->hdr.revision >> 16,
746 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800747
Dave Young1fec0532013-12-20 18:02:19 +0800748 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
749 return;
750
Leif Lindholm272686b2013-09-05 11:34:54 +0100751 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800752 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000753
754 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800755
Olof Johansson1adbfa32012-02-12 13:24:29 -0800756 /*
757 * Note: We currently don't support runtime services on an EFI
758 * that doesn't match the kernel 32/64-bit mode.
759 */
760
Olof Johansson5189c2a2012-10-24 10:00:44 -0700761 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800762 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000763 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000764 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000765 return;
766 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
Olof Johansson140bf272012-02-12 13:24:28 -0800767 }
Matt Fleming83e68182012-11-14 09:42:35 +0000768 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800769 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000770
771 set_bit(EFI_MEMMAP, &x86_efi_facility);
772
Huang, Ying5b836832008-01-30 13:31:19 +0100773 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100774}
775
Josh Triplett2223af32012-09-28 17:57:05 -0700776void __init efi_late_init(void)
777{
778 efi_bgrt_init();
779}
780
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400781void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
782{
783 u64 addr, npages;
784
785 addr = md->virt_addr;
786 npages = md->num_pages;
787
788 memrange_efi_to_native(&addr, &npages);
789
790 if (executable)
791 set_memory_x(addr, npages);
792 else
793 set_memory_nx(addr, npages);
794}
795
Borislav Petkovc55d0162014-02-14 08:24:24 +0100796void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100797{
798 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100799 void *p;
800
Huang, Yinga2172e22008-01-30 13:33:55 +0100801 /* Make EFI runtime service code area executable */
802 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
803 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100804
805 if (md->type != EFI_RUNTIME_SERVICES_CODE)
806 continue;
807
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400808 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100809 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100810}
Huang, Yinga2172e22008-01-30 13:33:55 +0100811
Matt Fleming3e8fa262012-10-19 13:25:46 +0100812void efi_memory_uc(u64 addr, unsigned long size)
813{
814 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
815 u64 npages;
816
817 npages = round_up(size, page_shift) / page_shift;
818 memrange_efi_to_native(&addr, &npages);
819 set_memory_uc(addr, npages);
820}
821
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100822void __init old_map_region(efi_memory_desc_t *md)
823{
824 u64 start_pfn, end_pfn, end;
825 unsigned long size;
826 void *va;
827
828 start_pfn = PFN_DOWN(md->phys_addr);
829 size = md->num_pages << PAGE_SHIFT;
830 end = md->phys_addr + size;
831 end_pfn = PFN_UP(end);
832
833 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
834 va = __va(md->phys_addr);
835
836 if (!(md->attribute & EFI_MEMORY_WB))
837 efi_memory_uc((u64)(unsigned long)va, size);
838 } else
839 va = efi_ioremap(md->phys_addr, size,
840 md->type, md->attribute);
841
842 md->virt_addr = (u64) (unsigned long) va;
843 if (!va)
844 pr_err("ioremap of 0x%llX failed!\n",
845 (unsigned long long)md->phys_addr);
846}
847
Dave Young481f75c2013-12-20 18:02:16 +0800848/* Merge contiguous regions of the same type and attribute */
849static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100850{
Dave Young481f75c2013-12-20 18:02:16 +0800851 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400852 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100853
Matthew Garrett202f9d02011-05-05 15:19:44 -0400854 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
855 u64 prev_size;
856 md = p;
857
858 if (!prev_md) {
859 prev_md = md;
860 continue;
861 }
862
863 if (prev_md->type != md->type ||
864 prev_md->attribute != md->attribute) {
865 prev_md = md;
866 continue;
867 }
868
869 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
870
871 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
872 prev_md->num_pages += md->num_pages;
873 md->type = EFI_RESERVED_TYPE;
874 md->attribute = 0;
875 continue;
876 }
877 prev_md = md;
878 }
Dave Young481f75c2013-12-20 18:02:16 +0800879}
880
881static void __init get_systab_virt_addr(efi_memory_desc_t *md)
882{
883 unsigned long size;
884 u64 end, systab;
885
886 size = md->num_pages << EFI_PAGE_SHIFT;
887 end = md->phys_addr + size;
888 systab = (u64)(unsigned long)efi_phys.systab;
889 if (md->phys_addr <= systab && systab < end) {
890 systab += md->virt_addr - md->phys_addr;
891 efi.systab = (efi_system_table_t *)(unsigned long)systab;
892 }
893}
894
Dave Young926172d2013-12-20 18:02:18 +0800895static int __init save_runtime_map(void)
896{
897 efi_memory_desc_t *md;
898 void *tmp, *p, *q = NULL;
899 int count = 0;
900
901 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
902 md = p;
903
904 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
905 (md->type == EFI_BOOT_SERVICES_CODE) ||
906 (md->type == EFI_BOOT_SERVICES_DATA))
907 continue;
908 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
909 if (!tmp)
910 goto out;
911 q = tmp;
912
913 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
914 count++;
915 }
916
Matt Fleming518548a2013-12-21 16:09:46 +0000917 efi_runtime_map_setup(q, count, memmap.desc_size);
Dave Young926172d2013-12-20 18:02:18 +0800918
919 return 0;
920out:
921 kfree(q);
922 return -ENOMEM;
923}
924
Dave Young481f75c2013-12-20 18:02:16 +0800925/*
Dave Young1fec0532013-12-20 18:02:19 +0800926 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
927 * addr which was used in first kernel of a kexec boot.
928 */
929static void __init efi_map_regions_fixed(void)
930{
931 void *p;
932 efi_memory_desc_t *md;
933
934 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
935 md = p;
936 efi_map_region_fixed(md); /* FIXME: add error handling */
937 get_systab_virt_addr(md);
938 }
939
940}
941
942/*
Dave Young481f75c2013-12-20 18:02:16 +0800943 * Map efi memory ranges for runtime serivce and update new_memmap with virtual
944 * addresses.
945 */
946static void * __init efi_map_regions(int *count)
947{
948 efi_memory_desc_t *md;
949 void *p, *tmp, *new_memmap = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400950
Huang, Ying5b836832008-01-30 13:31:19 +0100951 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
952 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700953 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
954#ifdef CONFIG_X86_64
955 if (md->type != EFI_BOOT_SERVICES_CODE &&
956 md->type != EFI_BOOT_SERVICES_DATA)
957#endif
958 continue;
959 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100960
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100961 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800962 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100963
Dave Young481f75c2013-12-20 18:02:16 +0800964 tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size,
965 GFP_KERNEL);
966 if (!tmp)
Dave Young926172d2013-12-20 18:02:18 +0800967 goto out;
Dave Young481f75c2013-12-20 18:02:16 +0800968 new_memmap = tmp;
969 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400970 memmap.desc_size);
Dave Young481f75c2013-12-20 18:02:16 +0800971 (*count)++;
972 }
973
974 return new_memmap;
Dave Young926172d2013-12-20 18:02:18 +0800975out:
Dave Young481f75c2013-12-20 18:02:16 +0800976 kfree(new_memmap);
977 return NULL;
978}
979
980/*
981 * This function will switch the EFI runtime services to virtual mode.
982 * Essentially, we look through the EFI memmap and map every region that
983 * has the runtime attribute bit set in its memory descriptor into the
984 * ->trampoline_pgd page table using a top-down VA allocation scheme.
985 *
986 * The old method which used to update that memory descriptor with the
987 * virtual address obtained from ioremap() is still supported when the
988 * kernel is booted with efi=old_map on its command line. Same old
989 * method enabled the runtime services to be called without having to
990 * thunk back into physical mode for every invocation.
991 *
992 * The new method does a pagetable switch in a preemption-safe manner
993 * so that we're in a different address space when calling a runtime
994 * function. For function arguments passing we do copy the PGDs of the
995 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +0800996 *
997 * Specially for kexec boot, efi runtime maps in previous kernel should
998 * be passed in via setup_data. In that case runtime ranges will be mapped
999 * to the same virtual addresses as the first kernel.
Dave Young481f75c2013-12-20 18:02:16 +08001000 */
1001void __init efi_enter_virtual_mode(void)
1002{
1003 efi_status_t status;
1004 void *new_memmap = NULL;
Dave Young926172d2013-12-20 18:02:18 +08001005 int err, count = 0;
Dave Young481f75c2013-12-20 18:02:16 +08001006
1007 efi.systab = NULL;
1008
1009 /*
1010 * We don't do virtual mode, since we don't do runtime services, on
1011 * non-native EFI
1012 */
1013 if (!efi_is_native()) {
1014 efi_unmap_memmap();
1015 return;
1016 }
1017
Dave Young1fec0532013-12-20 18:02:19 +08001018 if (efi_setup) {
1019 efi_map_regions_fixed();
1020 } else {
1021 efi_merge_regions();
1022 new_memmap = efi_map_regions(&count);
1023 if (!new_memmap) {
1024 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1025 return;
1026 }
Huang, Ying5b836832008-01-30 13:31:19 +01001027 }
1028
Dave Young926172d2013-12-20 18:02:18 +08001029 err = save_runtime_map();
1030 if (err)
1031 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1032
Huang, Ying5b836832008-01-30 13:31:19 +01001033 BUG_ON(!efi.systab);
1034
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001035 efi_setup_page_tables();
1036 efi_sync_low_kernel_mappings();
1037
Dave Young1fec0532013-12-20 18:02:19 +08001038 if (!efi_setup) {
1039 status = phys_efi_set_virtual_address_map(
1040 memmap.desc_size * count,
1041 memmap.desc_size,
1042 memmap.desc_version,
1043 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +01001044
Dave Young1fec0532013-12-20 18:02:19 +08001045 if (status != EFI_SUCCESS) {
1046 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1047 status);
1048 panic("EFI call to SetVirtualAddressMap() failed!");
1049 }
Huang, Ying5b836832008-01-30 13:31:19 +01001050 }
1051
1052 /*
1053 * Now that EFI is in virtual mode, update the function
1054 * pointers in the runtime service table to the new virtual addresses.
1055 *
1056 * Call EFI services through wrapper functions.
1057 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001058 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +01001059 efi.get_time = virt_efi_get_time;
1060 efi.set_time = virt_efi_set_time;
1061 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1062 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1063 efi.get_variable = virt_efi_get_variable;
1064 efi.get_next_variable = virt_efi_get_next_variable;
1065 efi.set_variable = virt_efi_set_variable;
1066 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1067 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001068 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -04001069 efi.query_variable_info = virt_efi_query_variable_info;
1070 efi.update_capsule = virt_efi_update_capsule;
1071 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001072
Borislav Petkovc55d0162014-02-14 08:24:24 +01001073 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001074
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001075 kfree(new_memmap);
Matthew Garrettf8b84042013-06-01 16:06:20 -04001076
1077 /* clean DUMMY object */
1078 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1079 EFI_VARIABLE_NON_VOLATILE |
1080 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1081 EFI_VARIABLE_RUNTIME_ACCESS,
1082 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001083}
1084
1085/*
1086 * Convenience functions to obtain memory types and attributes
1087 */
1088u32 efi_mem_type(unsigned long phys_addr)
1089{
1090 efi_memory_desc_t *md;
1091 void *p;
1092
Matt Fleming83e68182012-11-14 09:42:35 +00001093 if (!efi_enabled(EFI_MEMMAP))
1094 return 0;
1095
Huang, Ying5b836832008-01-30 13:31:19 +01001096 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1097 md = p;
1098 if ((md->phys_addr <= phys_addr) &&
1099 (phys_addr < (md->phys_addr +
1100 (md->num_pages << EFI_PAGE_SHIFT))))
1101 return md->type;
1102 }
1103 return 0;
1104}
1105
1106u64 efi_mem_attributes(unsigned long phys_addr)
1107{
1108 efi_memory_desc_t *md;
1109 void *p;
1110
1111 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1112 md = p;
1113 if ((md->phys_addr <= phys_addr) &&
1114 (phys_addr < (md->phys_addr +
1115 (md->num_pages << EFI_PAGE_SHIFT))))
1116 return md->attribute;
1117 }
1118 return 0;
1119}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001120
1121/*
1122 * Some firmware has serious problems when using more than 50% of the EFI
1123 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1124 * we never use more than this safe limit.
1125 *
1126 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1127 * store.
1128 */
1129efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1130{
1131 efi_status_t status;
1132 u64 storage_size, remaining_size, max_size;
1133
Matthew Garrettf8b84042013-06-01 16:06:20 -04001134 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1135 return 0;
1136
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001137 status = efi.query_variable_info(attributes, &storage_size,
1138 &remaining_size, &max_size);
1139 if (status != EFI_SUCCESS)
1140 return status;
1141
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001142 /*
1143 * Some firmware implementations refuse to boot if there's insufficient
1144 * space in the variable store. We account for that by refusing the
1145 * write if permitting it would reduce the available space to under
Matthew Garrettf8b84042013-06-01 16:06:20 -04001146 * 5KB. This figure was provided by Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001147 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001148 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1149 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001150
Matthew Garrettf8b84042013-06-01 16:06:20 -04001151 /*
1152 * Triggering garbage collection may require that the firmware
1153 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1154 * that by attempting to use more space than is available.
1155 */
1156 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001157 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1158
1159 if (!dummy)
1160 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001161
Matthew Garrettf8b84042013-06-01 16:06:20 -04001162 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1163 EFI_VARIABLE_NON_VOLATILE |
1164 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1165 EFI_VARIABLE_RUNTIME_ACCESS,
1166 dummy_size, dummy);
1167
1168 if (status == EFI_SUCCESS) {
1169 /*
1170 * This should have failed, so if it didn't make sure
1171 * that we delete it...
1172 */
1173 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1174 EFI_VARIABLE_NON_VOLATILE |
1175 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1176 EFI_VARIABLE_RUNTIME_ACCESS,
1177 0, dummy);
1178 }
1179
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001180 kfree(dummy);
1181
Matthew Garrettf8b84042013-06-01 16:06:20 -04001182 /*
1183 * The runtime code may now have triggered a garbage collection
1184 * run, so check the variable info again
1185 */
1186 status = efi.query_variable_info(attributes, &storage_size,
1187 &remaining_size, &max_size);
1188
1189 if (status != EFI_SUCCESS)
1190 return status;
1191
1192 /*
1193 * There still isn't enough room, so return an error
1194 */
1195 if (remaining_size - size < EFI_MIN_RESERVE)
1196 return EFI_OUT_OF_RESOURCES;
1197 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001198
1199 return EFI_SUCCESS;
1200}
Sergey Vlasov36680112013-04-16 18:31:09 +04001201EXPORT_SYMBOL_GPL(efi_query_variable_store);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001202
1203static int __init parse_efi_cmdline(char *str)
1204{
1205 if (*str == '=')
1206 str++;
1207
1208 if (!strncmp(str, "old_map", 7))
1209 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1210
1211 return 0;
1212}
1213early_param("efi", parse_efi_cmdline);
Borislav Petkova5d90c92014-03-04 17:02:17 +01001214
1215void __init efi_apply_memmap_quirks(void)
1216{
1217 /*
1218 * Once setup is done earlier, unmap the EFI memory map on mismatched
1219 * firmware/kernel architectures since there is no support for runtime
1220 * services.
1221 */
1222 if (!efi_is_native()) {
1223 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1224 efi_unmap_memmap();
1225 }
1226
1227 /*
1228 * UV doesn't support the new EFI pagetable mapping yet.
1229 */
1230 if (is_uv_system())
1231 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility);
1232}