blob: dc1dd6d09dec402bd4148844ca9020b65ea8bcf2 [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
4 *
5 * Copyright (C) 1999 VA Linux Systems
6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7 * Copyright (C) 1999-2002 Hewlett-Packard Co.
8 * David Mosberger-Tang <davidm@hpl.hp.com>
9 * Stephane Eranian <eranian@hpl.hp.com>
10 * Copyright (C) 2005-2008 Intel Co.
11 * Fenghua Yu <fenghua.yu@intel.com>
12 * Bibo Mao <bibo.mao@intel.com>
13 * Chandramouli Narayanan <mouli@linux.intel.com>
14 * Huang Ying <ying.huang@intel.com>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010015 * Copyright (C) 2013 SuSE Labs
16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
Huang, Ying5b836832008-01-30 13:31:19 +010017 *
18 * Copied from efi_32.c to eliminate the duplicated code between EFI
19 * 32/64 support code. --ying 2007-10-26
20 *
21 * All EFI Runtime Services are not implemented yet as EFI only
22 * supports physical mode addressing on SoftSDV. This is to be fixed
23 * in a future version. --drummond 1999-07-20
24 *
25 * Implemented EFI runtime services and virtual mode calls. --davidm
26 *
27 * Goutham Rao: <goutham.rao@intel.com>
28 * Skip non-WB memory and ignore empty memory ranges.
29 */
30
Olof Johanssone3cb3f52012-02-12 13:24:26 -080031#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
Huang, Ying5b836832008-01-30 13:31:19 +010033#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/efi.h>
Josh Triplett2223af32012-09-28 17:57:05 -070036#include <linux/efi-bgrt.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040037#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010038#include <linux/bootmem.h>
David Howells0d01ff22013-04-11 23:51:01 +010039#include <linux/slab.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070040#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010041#include <linux/spinlock.h>
42#include <linux/uaccess.h>
43#include <linux/time.h>
44#include <linux/io.h>
45#include <linux/reboot.h>
46#include <linux/bcd.h>
47
48#include <asm/setup.h>
49#include <asm/efi.h>
50#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010051#include <asm/cacheflush.h>
52#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080053#include <asm/x86_init.h>
Prarit Bhargava3195ef52013-02-14 12:02:54 -050054#include <asm/rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010055
Borislav Petkovf4fccac2013-10-31 17:24:59 +010056#define EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +010057
Matthew Garrettf8b84042013-06-01 16:06:20 -040058#define EFI_MIN_RESERVE 5120
59
60#define EFI_DUMMY_GUID \
61 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
62
63static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
Matthew Garrett31ff2f22013-04-15 13:09:47 -070064
Huang, Ying5b836832008-01-30 13:31:19 +010065struct efi_memory_map memmap;
66
Harvey Harrisonecaea422008-02-13 13:26:13 -080067static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010068static efi_system_table_t efi_systab __initdata;
69
Leif Lindholm272686b2013-09-05 11:34:54 +010070static __initdata efi_config_table_type_t arch_tables[] = {
71#ifdef CONFIG_X86_UV
72 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
73#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010074 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010075};
76
Dave Young1fec0532013-12-20 18:02:19 +080077u64 efi_setup; /* efi setup_data physical address */
Dave Young926172d2013-12-20 18:02:18 +080078
Matt Fleming058e7b52013-02-22 15:03:47 +000079static bool __initdata disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010080static int __init setup_noefi(char *arg)
81{
Matt Flemingfb834c72013-02-20 20:36:12 +000082 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010083 return 0;
84}
85early_param("noefi", setup_noefi);
86
Paul Jackson200001e2008-06-25 05:44:46 -070087int add_efi_memmap;
88EXPORT_SYMBOL(add_efi_memmap);
89
90static int __init setup_add_efi_memmap(char *arg)
91{
92 add_efi_memmap = 1;
93 return 0;
94}
95early_param("add_efi_memmap", setup_add_efi_memmap);
96
Richard Weinberger8c58bf32013-04-17 01:00:53 +020097static bool efi_no_storage_paranoia;
98
99static int __init setup_storage_paranoia(char *arg)
100{
101 efi_no_storage_paranoia = true;
102 return 0;
103}
104early_param("efi_no_storage_paranoia", setup_storage_paranoia);
105
Huang, Ying5b836832008-01-30 13:31:19 +0100106static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
107{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100108 unsigned long flags;
109 efi_status_t status;
110
111 spin_lock_irqsave(&rtc_lock, flags);
112 status = efi_call_virt2(get_time, tm, tc);
113 spin_unlock_irqrestore(&rtc_lock, flags);
114 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100115}
116
117static efi_status_t virt_efi_set_time(efi_time_t *tm)
118{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100119 unsigned long flags;
120 efi_status_t status;
121
122 spin_lock_irqsave(&rtc_lock, flags);
123 status = efi_call_virt1(set_time, tm);
124 spin_unlock_irqrestore(&rtc_lock, flags);
125 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100126}
127
128static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
129 efi_bool_t *pending,
130 efi_time_t *tm)
131{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100132 unsigned long flags;
133 efi_status_t status;
134
135 spin_lock_irqsave(&rtc_lock, flags);
136 status = efi_call_virt3(get_wakeup_time,
137 enabled, pending, tm);
138 spin_unlock_irqrestore(&rtc_lock, flags);
139 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100140}
141
142static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
143{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100144 unsigned long flags;
145 efi_status_t status;
146
147 spin_lock_irqsave(&rtc_lock, flags);
148 status = efi_call_virt2(set_wakeup_time,
149 enabled, 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_get_variable(efi_char16_t *name,
155 efi_guid_t *vendor,
156 u32 *attr,
157 unsigned long *data_size,
158 void *data)
159{
160 return efi_call_virt5(get_variable,
161 name, vendor, attr,
162 data_size, data);
163}
164
165static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
166 efi_char16_t *name,
167 efi_guid_t *vendor)
168{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400169 return efi_call_virt3(get_next_variable,
170 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100171}
172
173static efi_status_t virt_efi_set_variable(efi_char16_t *name,
174 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400175 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100176 unsigned long data_size,
177 void *data)
178{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400179 return efi_call_virt5(set_variable,
180 name, vendor, attr,
181 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100182}
183
Matthew Garrett3b370232011-06-06 15:36:25 -0400184static efi_status_t virt_efi_query_variable_info(u32 attr,
185 u64 *storage_space,
186 u64 *remaining_space,
187 u64 *max_variable_size)
188{
189 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
190 return EFI_UNSUPPORTED;
191
192 return efi_call_virt4(query_variable_info, attr, storage_space,
193 remaining_space, max_variable_size);
194}
195
Huang, Ying5b836832008-01-30 13:31:19 +0100196static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
197{
198 return efi_call_virt1(get_next_high_mono_count, count);
199}
200
201static void virt_efi_reset_system(int reset_type,
202 efi_status_t status,
203 unsigned long data_size,
204 efi_char16_t *data)
205{
206 efi_call_virt4(reset_system, reset_type, status,
207 data_size, data);
208}
209
Matthew Garrett3b370232011-06-06 15:36:25 -0400210static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
211 unsigned long count,
212 unsigned long sg_list)
213{
214 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
215 return EFI_UNSUPPORTED;
216
217 return efi_call_virt3(update_capsule, capsules, count, sg_list);
218}
219
220static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
221 unsigned long count,
222 u64 *max_size,
223 int *reset_type)
224{
225 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
226 return EFI_UNSUPPORTED;
227
228 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
229 reset_type);
230}
231
Huang, Ying5b836832008-01-30 13:31:19 +0100232static efi_status_t __init phys_efi_set_virtual_address_map(
233 unsigned long memory_map_size,
234 unsigned long descriptor_size,
235 u32 descriptor_version,
236 efi_memory_desc_t *virtual_map)
237{
238 efi_status_t status;
239
240 efi_call_phys_prelog();
241 status = efi_call_phys4(efi_phys.set_virtual_address_map,
242 memory_map_size, descriptor_size,
243 descriptor_version, virtual_map);
244 efi_call_phys_epilog();
245 return status;
246}
247
Linus Torvalds11520e52012-12-15 15:15:24 -0800248static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
249 efi_time_cap_t *tc)
250{
251 unsigned long flags;
252 efi_status_t status;
253
254 spin_lock_irqsave(&rtc_lock, flags);
255 efi_call_phys_prelog();
256 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
257 virt_to_phys(tc));
258 efi_call_phys_epilog();
259 spin_unlock_irqrestore(&rtc_lock, flags);
260 return status;
261}
262
David Vrabel35651842013-05-13 18:56:06 +0100263int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100264{
David Vrabel35651842013-05-13 18:56:06 +0100265 unsigned long nowtime = now->tv_sec;
Huang, Ying5b836832008-01-30 13:31:19 +0100266 efi_status_t status;
267 efi_time_t eft;
268 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500269 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100270
271 status = efi.get_time(&eft, &cap);
272 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800273 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100274 return -1;
275 }
276
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500277 rtc_time_to_tm(nowtime, &tm);
278 if (!rtc_valid_tm(&tm)) {
279 eft.year = tm.tm_year + 1900;
280 eft.month = tm.tm_mon + 1;
281 eft.day = tm.tm_mday;
282 eft.minute = tm.tm_min;
283 eft.second = tm.tm_sec;
284 eft.nanosecond = 0;
285 } else {
286 printk(KERN_ERR
287 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
288 __FUNCTION__, nowtime);
289 return -1;
290 }
Huang, Ying5b836832008-01-30 13:31:19 +0100291
292 status = efi.set_time(&eft);
293 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800294 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100295 return -1;
296 }
297 return 0;
298}
299
David Vrabel35651842013-05-13 18:56:06 +0100300void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100301{
302 efi_status_t status;
303 efi_time_t eft;
304 efi_time_cap_t cap;
305
306 status = efi.get_time(&eft, &cap);
307 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800308 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100309
David Vrabel35651842013-05-13 18:56:06 +0100310 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
311 eft.minute, eft.second);
312 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100313}
314
Paul Jackson69c91892008-05-14 08:15:58 -0700315/*
316 * Tell the kernel about the EFI memory map. This might include
317 * more than the max 128 entries that can fit in the e820 legacy
318 * (zeropage) memory map.
319 */
320
Paul Jackson200001e2008-06-25 05:44:46 -0700321static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700322{
323 void *p;
324
325 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
326 efi_memory_desc_t *md = p;
327 unsigned long long start = md->phys_addr;
328 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
329 int e820_type;
330
Cliff Wickmane2a71472009-06-16 16:43:40 -0500331 switch (md->type) {
332 case EFI_LOADER_CODE:
333 case EFI_LOADER_DATA:
334 case EFI_BOOT_SERVICES_CODE:
335 case EFI_BOOT_SERVICES_DATA:
336 case EFI_CONVENTIONAL_MEMORY:
337 if (md->attribute & EFI_MEMORY_WB)
338 e820_type = E820_RAM;
339 else
340 e820_type = E820_RESERVED;
341 break;
342 case EFI_ACPI_RECLAIM_MEMORY:
343 e820_type = E820_ACPI;
344 break;
345 case EFI_ACPI_MEMORY_NVS:
346 e820_type = E820_NVS;
347 break;
348 case EFI_UNUSABLE_MEMORY:
349 e820_type = E820_UNUSABLE;
350 break;
351 default:
352 /*
353 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
354 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
355 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
356 */
Paul Jackson69c91892008-05-14 08:15:58 -0700357 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500358 break;
359 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700360 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700361 }
362 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
363}
364
Olof Johansson1adbfa32012-02-12 13:24:29 -0800365int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800366{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100367 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800368 unsigned long pmap;
369
Paul Jackson05486fa2008-06-22 07:22:02 -0700370#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800371 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100372 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800373 pr_err("Memory map is above 4GB, disabling EFI.\n");
374 return -EINVAL;
375 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100376 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700377#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100378 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800379#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100380 memmap.phys_map = (void *)pmap;
381 memmap.nr_map = e->efi_memmap_size /
382 e->efi_memdesc_size;
383 memmap.desc_size = e->efi_memdesc_size;
384 memmap.desc_version = e->efi_memdesc_version;
385
Tejun Heo24aa0782011-07-12 11:16:06 +0200386 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800387
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100388 efi.memmap = &memmap;
389
Olof Johansson1adbfa32012-02-12 13:24:29 -0800390 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800391}
392
Huang, Ying5b836832008-01-30 13:31:19 +0100393static void __init print_efi_memmap(void)
394{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100395#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100396 efi_memory_desc_t *md;
397 void *p;
398 int i;
399
400 for (p = memmap.map, i = 0;
401 p < memmap.map_end;
402 p += memmap.desc_size, i++) {
403 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800404 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100405 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
406 i, md->type, md->attribute, md->phys_addr,
407 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
408 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
409 }
Huang, Ying5b836832008-01-30 13:31:19 +0100410#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100411}
Huang, Ying5b836832008-01-30 13:31:19 +0100412
Matthew Garrett916f6762011-05-25 09:53:13 -0400413void __init efi_reserve_boot_services(void)
414{
415 void *p;
416
417 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
418 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200419 u64 start = md->phys_addr;
420 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400421
422 if (md->type != EFI_BOOT_SERVICES_CODE &&
423 md->type != EFI_BOOT_SERVICES_DATA)
424 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200425 /* Only reserve where possible:
426 * - Not within any already allocated areas
427 * - Not over any memory area (really needed, if above?)
428 * - Not within any part of the kernel
429 * - Not the bios reserved area
430 */
Dave Younga7f84f02013-12-20 18:02:15 +0800431 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800432 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200433 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200434 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200435 /* Could not reserve, skip it */
436 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800437 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200438 "[0x%010llx-0x%010llx]\n",
439 start, start+size-1);
440 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200441 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400442 }
443}
444
Olof Johansson5189c2a2012-10-24 10:00:44 -0700445void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700446{
Matt Fleming3e909592014-01-15 13:21:22 +0000447 clear_bit(EFI_MEMMAP, &efi.flags);
Josh Triplett78510792012-09-28 17:55:44 -0700448 if (memmap.map) {
449 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
450 memmap.map = NULL;
451 }
452}
453
454void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400455{
456 void *p;
457
Olof Johansson5189c2a2012-10-24 10:00:44 -0700458 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700459 return;
460
Matthew Garrett916f6762011-05-25 09:53:13 -0400461 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
462 efi_memory_desc_t *md = p;
463 unsigned long long start = md->phys_addr;
464 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
465
466 if (md->type != EFI_BOOT_SERVICES_CODE &&
467 md->type != EFI_BOOT_SERVICES_DATA)
468 continue;
469
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200470 /* Could not reserve boot area */
471 if (!size)
472 continue;
473
Matthew Garrett916f6762011-05-25 09:53:13 -0400474 free_bootmem_late(start, size);
475 }
Josh Triplett78510792012-09-28 17:55:44 -0700476
477 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400478}
479
Olof Johansson140bf272012-02-12 13:24:28 -0800480static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100481{
Matt Fleming83e68182012-11-14 09:42:35 +0000482 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800483 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800484 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800485 u64 tmp = 0;
486
Dave Young1fec0532013-12-20 18:02:19 +0800487 if (efi_setup) {
488 data = early_memremap(efi_setup, sizeof(*data));
489 if (!data)
490 return -ENOMEM;
491 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800492 systab64 = early_ioremap((unsigned long)phys,
493 sizeof(*systab64));
494 if (systab64 == NULL) {
495 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800496 if (data)
497 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800498 return -ENOMEM;
499 }
500
501 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800502 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
503 systab64->fw_vendor;
504 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800505 efi_systab.fw_revision = systab64->fw_revision;
506 efi_systab.con_in_handle = systab64->con_in_handle;
507 tmp |= systab64->con_in_handle;
508 efi_systab.con_in = systab64->con_in;
509 tmp |= systab64->con_in;
510 efi_systab.con_out_handle = systab64->con_out_handle;
511 tmp |= systab64->con_out_handle;
512 efi_systab.con_out = systab64->con_out;
513 tmp |= systab64->con_out;
514 efi_systab.stderr_handle = systab64->stderr_handle;
515 tmp |= systab64->stderr_handle;
516 efi_systab.stderr = systab64->stderr;
517 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800518 efi_systab.runtime = data ?
519 (void *)(unsigned long)data->runtime :
520 (void *)(unsigned long)systab64->runtime;
521 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800522 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
523 tmp |= systab64->boottime;
524 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800525 efi_systab.tables = data ? (unsigned long)data->tables :
526 systab64->tables;
527 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800528
529 early_iounmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800530 if (data)
531 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800532#ifdef CONFIG_X86_32
533 if (tmp >> 32) {
534 pr_err("EFI data located above 4GB, disabling EFI.\n");
535 return -EINVAL;
536 }
537#endif
538 } else {
539 efi_system_table_32_t *systab32;
540
541 systab32 = early_ioremap((unsigned long)phys,
542 sizeof(*systab32));
543 if (systab32 == NULL) {
544 pr_err("Couldn't map the system table!\n");
545 return -ENOMEM;
546 }
547
548 efi_systab.hdr = systab32->hdr;
549 efi_systab.fw_vendor = systab32->fw_vendor;
550 efi_systab.fw_revision = systab32->fw_revision;
551 efi_systab.con_in_handle = systab32->con_in_handle;
552 efi_systab.con_in = systab32->con_in;
553 efi_systab.con_out_handle = systab32->con_out_handle;
554 efi_systab.con_out = systab32->con_out;
555 efi_systab.stderr_handle = systab32->stderr_handle;
556 efi_systab.stderr = systab32->stderr;
557 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
558 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
559 efi_systab.nr_tables = systab32->nr_tables;
560 efi_systab.tables = systab32->tables;
561
562 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800563 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800564
Huang, Ying5b836832008-01-30 13:31:19 +0100565 efi.systab = &efi_systab;
566
567 /*
568 * Verify the EFI Table
569 */
Olof Johansson140bf272012-02-12 13:24:28 -0800570 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800571 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800572 return -EINVAL;
573 }
Huang, Ying5b836832008-01-30 13:31:19 +0100574 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800575 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100576 "%d.%02d, expected 1.00 or greater!\n",
577 efi.systab->hdr.revision >> 16,
578 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800579
Matt Fleming0f8093a2014-01-15 13:36:33 +0000580 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
581
Olof Johansson140bf272012-02-12 13:24:28 -0800582 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800583}
Huang, Ying5b836832008-01-30 13:31:19 +0100584
Olof Johansson140bf272012-02-12 13:24:28 -0800585static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800586{
587 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100588
589 /*
590 * Check out the runtime services table. We need to map
591 * the runtime services table so that we can grab the physical
592 * address of several of the EFI runtime functions, needed to
593 * set the firmware into virtual mode.
594 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100595 runtime = early_ioremap((unsigned long)efi.systab->runtime,
596 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800597 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800598 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800599 return -ENOMEM;
600 }
601 /*
602 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800603 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800604 * is invoked.
605 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800606 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800607 efi_phys.set_virtual_address_map =
608 (efi_set_virtual_address_map_t *)
609 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800610 /*
611 * Make efi_get_time can be called before entering
612 * virtual mode.
613 */
614 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100615 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800616
Matt Fleming0f8093a2014-01-15 13:36:33 +0000617 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
618
Olof Johansson140bf272012-02-12 13:24:28 -0800619 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800620}
Huang, Ying5b836832008-01-30 13:31:19 +0100621
Olof Johansson140bf272012-02-12 13:24:28 -0800622static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800623{
Huang, Ying5b836832008-01-30 13:31:19 +0100624 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100625 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
626 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800627 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800628 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800629 return -ENOMEM;
630 }
Huang, Ying5b836832008-01-30 13:31:19 +0100631 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500632
Paul Jackson200001e2008-06-25 05:44:46 -0700633 if (add_efi_memmap)
634 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800635
Matt Fleming0f8093a2014-01-15 13:36:33 +0000636 set_bit(EFI_MEMMAP, &efi.flags);
637
Olof Johansson140bf272012-02-12 13:24:28 -0800638 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800639}
640
Dave Young1fec0532013-12-20 18:02:19 +0800641/*
642 * A number of config table entries get remapped to virtual addresses
643 * after entering EFI virtual mode. However, the kexec kernel requires
644 * their physical addresses therefore we pass them via setup_data and
645 * correct those entries to their respective physical addresses here.
646 *
647 * Currently only handles smbios which is necessary for some firmware
648 * implementation.
649 */
650static int __init efi_reuse_config(u64 tables, int nr_tables)
651{
652 int i, sz, ret = 0;
653 void *p, *tablep;
654 struct efi_setup_data *data;
655
656 if (!efi_setup)
657 return 0;
658
659 if (!efi_enabled(EFI_64BIT))
660 return 0;
661
662 data = early_memremap(efi_setup, sizeof(*data));
663 if (!data) {
664 ret = -ENOMEM;
665 goto out;
666 }
667
668 if (!data->smbios)
669 goto out_memremap;
670
671 sz = sizeof(efi_config_table_64_t);
672
673 p = tablep = early_memremap(tables, nr_tables * sz);
674 if (!p) {
675 pr_err("Could not map Configuration table!\n");
676 ret = -ENOMEM;
677 goto out_memremap;
678 }
679
680 for (i = 0; i < efi.systab->nr_tables; i++) {
681 efi_guid_t guid;
682
683 guid = ((efi_config_table_64_t *)p)->guid;
684
685 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
686 ((efi_config_table_64_t *)p)->table = data->smbios;
687 p += sz;
688 }
689 early_iounmap(tablep, nr_tables * sz);
690
691out_memremap:
692 early_iounmap(data, sizeof(*data));
693out:
694 return ret;
695}
696
Olof Johansson83e7ee62012-02-12 13:24:25 -0800697void __init efi_init(void)
698{
699 efi_char16_t *c16;
700 char vendor[100] = "unknown";
701 int i = 0;
702 void *tmp;
703
704#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800705 if (boot_params.efi_info.efi_systab_hi ||
706 boot_params.efi_info.efi_memmap_hi) {
707 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800708 return;
709 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800710 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
711#else
712 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800713 (boot_params.efi_info.efi_systab |
714 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800715#endif
716
Matt Fleming83e68182012-11-14 09:42:35 +0000717 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800718 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000719
Matt Fleming3e909592014-01-15 13:21:22 +0000720 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800721
Dave Younga0998eb2013-12-20 18:02:17 +0800722 efi.config_table = (unsigned long)efi.systab->tables;
723 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
724 efi.runtime = (unsigned long)efi.systab->runtime;
725
Olof Johansson83e7ee62012-02-12 13:24:25 -0800726 /*
727 * Show what we know for posterity
728 */
729 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
730 if (c16) {
731 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
732 vendor[i] = *c16++;
733 vendor[i] = '\0';
734 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800735 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800736 early_iounmap(tmp, 2);
737
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800738 pr_info("EFI v%u.%.02u by %s\n",
739 efi.systab->hdr.revision >> 16,
740 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800741
Dave Young1fec0532013-12-20 18:02:19 +0800742 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
743 return;
744
Leif Lindholm272686b2013-09-05 11:34:54 +0100745 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800746 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000747
Olof Johansson1adbfa32012-02-12 13:24:29 -0800748 /*
749 * Note: We currently don't support runtime services on an EFI
750 * that doesn't match the kernel 32/64-bit mode.
751 */
752
Olof Johansson5189c2a2012-10-24 10:00:44 -0700753 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800754 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000755 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000756 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000757 return;
Olof Johansson140bf272012-02-12 13:24:28 -0800758 }
Matt Fleming83e68182012-11-14 09:42:35 +0000759 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800760 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000761
Matt Fleming3e909592014-01-15 13:21:22 +0000762 set_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming83e68182012-11-14 09:42:35 +0000763
Huang, Ying5b836832008-01-30 13:31:19 +0100764 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100765}
766
Josh Triplett2223af32012-09-28 17:57:05 -0700767void __init efi_late_init(void)
768{
769 efi_bgrt_init();
770}
771
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400772void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
773{
774 u64 addr, npages;
775
776 addr = md->virt_addr;
777 npages = md->num_pages;
778
779 memrange_efi_to_native(&addr, &npages);
780
781 if (executable)
782 set_memory_x(addr, npages);
783 else
784 set_memory_nx(addr, npages);
785}
786
Borislav Petkovc55d0162014-02-14 08:24:24 +0100787void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100788{
789 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100790 void *p;
791
Huang, Yinga2172e22008-01-30 13:33:55 +0100792 /* Make EFI runtime service code area executable */
793 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
794 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100795
796 if (md->type != EFI_RUNTIME_SERVICES_CODE)
797 continue;
798
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400799 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100800 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100801}
Huang, Yinga2172e22008-01-30 13:33:55 +0100802
Matt Fleming3e8fa262012-10-19 13:25:46 +0100803void efi_memory_uc(u64 addr, unsigned long size)
804{
805 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
806 u64 npages;
807
808 npages = round_up(size, page_shift) / page_shift;
809 memrange_efi_to_native(&addr, &npages);
810 set_memory_uc(addr, npages);
811}
812
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100813void __init old_map_region(efi_memory_desc_t *md)
814{
815 u64 start_pfn, end_pfn, end;
816 unsigned long size;
817 void *va;
818
819 start_pfn = PFN_DOWN(md->phys_addr);
820 size = md->num_pages << PAGE_SHIFT;
821 end = md->phys_addr + size;
822 end_pfn = PFN_UP(end);
823
824 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
825 va = __va(md->phys_addr);
826
827 if (!(md->attribute & EFI_MEMORY_WB))
828 efi_memory_uc((u64)(unsigned long)va, size);
829 } else
830 va = efi_ioremap(md->phys_addr, size,
831 md->type, md->attribute);
832
833 md->virt_addr = (u64) (unsigned long) va;
834 if (!va)
835 pr_err("ioremap of 0x%llX failed!\n",
836 (unsigned long long)md->phys_addr);
837}
838
Dave Young481f75c2013-12-20 18:02:16 +0800839/* Merge contiguous regions of the same type and attribute */
840static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100841{
Dave Young481f75c2013-12-20 18:02:16 +0800842 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400843 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100844
Matthew Garrett202f9d02011-05-05 15:19:44 -0400845 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
846 u64 prev_size;
847 md = p;
848
849 if (!prev_md) {
850 prev_md = md;
851 continue;
852 }
853
854 if (prev_md->type != md->type ||
855 prev_md->attribute != md->attribute) {
856 prev_md = md;
857 continue;
858 }
859
860 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
861
862 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
863 prev_md->num_pages += md->num_pages;
864 md->type = EFI_RESERVED_TYPE;
865 md->attribute = 0;
866 continue;
867 }
868 prev_md = md;
869 }
Dave Young481f75c2013-12-20 18:02:16 +0800870}
871
872static void __init get_systab_virt_addr(efi_memory_desc_t *md)
873{
874 unsigned long size;
875 u64 end, systab;
876
877 size = md->num_pages << EFI_PAGE_SHIFT;
878 end = md->phys_addr + size;
879 systab = (u64)(unsigned long)efi_phys.systab;
880 if (md->phys_addr <= systab && systab < end) {
881 systab += md->virt_addr - md->phys_addr;
882 efi.systab = (efi_system_table_t *)(unsigned long)systab;
883 }
884}
885
Dave Young926172d2013-12-20 18:02:18 +0800886static int __init save_runtime_map(void)
887{
888 efi_memory_desc_t *md;
889 void *tmp, *p, *q = NULL;
890 int count = 0;
891
892 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
893 md = p;
894
895 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
896 (md->type == EFI_BOOT_SERVICES_CODE) ||
897 (md->type == EFI_BOOT_SERVICES_DATA))
898 continue;
899 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
900 if (!tmp)
901 goto out;
902 q = tmp;
903
904 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
905 count++;
906 }
907
Matt Fleming518548a2013-12-21 16:09:46 +0000908 efi_runtime_map_setup(q, count, memmap.desc_size);
Dave Young926172d2013-12-20 18:02:18 +0800909
910 return 0;
911out:
912 kfree(q);
913 return -ENOMEM;
914}
915
Dave Young481f75c2013-12-20 18:02:16 +0800916/*
Dave Young1fec0532013-12-20 18:02:19 +0800917 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
918 * addr which was used in first kernel of a kexec boot.
919 */
920static void __init efi_map_regions_fixed(void)
921{
922 void *p;
923 efi_memory_desc_t *md;
924
925 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
926 md = p;
927 efi_map_region_fixed(md); /* FIXME: add error handling */
928 get_systab_virt_addr(md);
929 }
930
931}
932
933/*
Dave Young481f75c2013-12-20 18:02:16 +0800934 * Map efi memory ranges for runtime serivce and update new_memmap with virtual
935 * addresses.
936 */
937static void * __init efi_map_regions(int *count)
938{
939 efi_memory_desc_t *md;
940 void *p, *tmp, *new_memmap = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400941
Huang, Ying5b836832008-01-30 13:31:19 +0100942 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
943 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700944 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
945#ifdef CONFIG_X86_64
946 if (md->type != EFI_BOOT_SERVICES_CODE &&
947 md->type != EFI_BOOT_SERVICES_DATA)
948#endif
949 continue;
950 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100951
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100952 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800953 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100954
Dave Young481f75c2013-12-20 18:02:16 +0800955 tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size,
956 GFP_KERNEL);
957 if (!tmp)
Dave Young926172d2013-12-20 18:02:18 +0800958 goto out;
Dave Young481f75c2013-12-20 18:02:16 +0800959 new_memmap = tmp;
960 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400961 memmap.desc_size);
Dave Young481f75c2013-12-20 18:02:16 +0800962 (*count)++;
963 }
964
965 return new_memmap;
Dave Young926172d2013-12-20 18:02:18 +0800966out:
Dave Young481f75c2013-12-20 18:02:16 +0800967 kfree(new_memmap);
968 return NULL;
969}
970
971/*
972 * This function will switch the EFI runtime services to virtual mode.
973 * Essentially, we look through the EFI memmap and map every region that
974 * has the runtime attribute bit set in its memory descriptor into the
975 * ->trampoline_pgd page table using a top-down VA allocation scheme.
976 *
977 * The old method which used to update that memory descriptor with the
978 * virtual address obtained from ioremap() is still supported when the
979 * kernel is booted with efi=old_map on its command line. Same old
980 * method enabled the runtime services to be called without having to
981 * thunk back into physical mode for every invocation.
982 *
983 * The new method does a pagetable switch in a preemption-safe manner
984 * so that we're in a different address space when calling a runtime
985 * function. For function arguments passing we do copy the PGDs of the
986 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +0800987 *
988 * Specially for kexec boot, efi runtime maps in previous kernel should
989 * be passed in via setup_data. In that case runtime ranges will be mapped
990 * to the same virtual addresses as the first kernel.
Dave Young481f75c2013-12-20 18:02:16 +0800991 */
992void __init efi_enter_virtual_mode(void)
993{
994 efi_status_t status;
995 void *new_memmap = NULL;
Dave Young926172d2013-12-20 18:02:18 +0800996 int err, count = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800997
998 efi.systab = NULL;
999
1000 /*
1001 * We don't do virtual mode, since we don't do runtime services, on
1002 * non-native EFI
1003 */
1004 if (!efi_is_native()) {
1005 efi_unmap_memmap();
1006 return;
1007 }
1008
Dave Young1fec0532013-12-20 18:02:19 +08001009 if (efi_setup) {
1010 efi_map_regions_fixed();
1011 } else {
1012 efi_merge_regions();
1013 new_memmap = efi_map_regions(&count);
1014 if (!new_memmap) {
1015 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1016 return;
1017 }
Huang, Ying5b836832008-01-30 13:31:19 +01001018 }
1019
Dave Young926172d2013-12-20 18:02:18 +08001020 err = save_runtime_map();
1021 if (err)
1022 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1023
Huang, Ying5b836832008-01-30 13:31:19 +01001024 BUG_ON(!efi.systab);
1025
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001026 efi_setup_page_tables();
1027 efi_sync_low_kernel_mappings();
1028
Dave Young1fec0532013-12-20 18:02:19 +08001029 if (!efi_setup) {
1030 status = phys_efi_set_virtual_address_map(
1031 memmap.desc_size * count,
1032 memmap.desc_size,
1033 memmap.desc_version,
1034 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +01001035
Dave Young1fec0532013-12-20 18:02:19 +08001036 if (status != EFI_SUCCESS) {
1037 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1038 status);
1039 panic("EFI call to SetVirtualAddressMap() failed!");
1040 }
Huang, Ying5b836832008-01-30 13:31:19 +01001041 }
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 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001049 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +01001050 efi.get_time = virt_efi_get_time;
1051 efi.set_time = virt_efi_set_time;
1052 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1053 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1054 efi.get_variable = virt_efi_get_variable;
1055 efi.get_next_variable = virt_efi_get_next_variable;
1056 efi.set_variable = virt_efi_set_variable;
1057 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1058 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001059 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -04001060 efi.query_variable_info = virt_efi_query_variable_info;
1061 efi.update_capsule = virt_efi_update_capsule;
1062 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001063
Borislav Petkovc55d0162014-02-14 08:24:24 +01001064 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001065
Matthew Garrett7cb00b72011-05-05 15:19:45 -04001066 kfree(new_memmap);
Matthew Garrettf8b84042013-06-01 16:06:20 -04001067
1068 /* clean DUMMY object */
1069 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1070 EFI_VARIABLE_NON_VOLATILE |
1071 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1072 EFI_VARIABLE_RUNTIME_ACCESS,
1073 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001074}
1075
1076/*
1077 * Convenience functions to obtain memory types and attributes
1078 */
1079u32 efi_mem_type(unsigned long phys_addr)
1080{
1081 efi_memory_desc_t *md;
1082 void *p;
1083
Matt Fleming83e68182012-11-14 09:42:35 +00001084 if (!efi_enabled(EFI_MEMMAP))
1085 return 0;
1086
Huang, Ying5b836832008-01-30 13:31:19 +01001087 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1088 md = p;
1089 if ((md->phys_addr <= phys_addr) &&
1090 (phys_addr < (md->phys_addr +
1091 (md->num_pages << EFI_PAGE_SHIFT))))
1092 return md->type;
1093 }
1094 return 0;
1095}
1096
1097u64 efi_mem_attributes(unsigned long phys_addr)
1098{
1099 efi_memory_desc_t *md;
1100 void *p;
1101
1102 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1103 md = p;
1104 if ((md->phys_addr <= phys_addr) &&
1105 (phys_addr < (md->phys_addr +
1106 (md->num_pages << EFI_PAGE_SHIFT))))
1107 return md->attribute;
1108 }
1109 return 0;
1110}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001111
1112/*
Madper Xie5db80c62013-11-10 22:15:11 +08001113 * Some firmware implementations refuse to boot if there's insufficient space
1114 * in the variable store. Ensure that we never use more than a safe limit.
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001115 *
1116 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1117 * store.
1118 */
1119efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1120{
1121 efi_status_t status;
1122 u64 storage_size, remaining_size, max_size;
1123
Matthew Garrettf8b84042013-06-01 16:06:20 -04001124 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1125 return 0;
1126
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001127 status = efi.query_variable_info(attributes, &storage_size,
1128 &remaining_size, &max_size);
1129 if (status != EFI_SUCCESS)
1130 return status;
1131
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001132 /*
Madper Xie5db80c62013-11-10 22:15:11 +08001133 * We account for that by refusing the write if permitting it would
1134 * reduce the available space to under 5KB. This figure was provided by
1135 * Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001136 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001137 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1138 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001139
Matthew Garrettf8b84042013-06-01 16:06:20 -04001140 /*
1141 * Triggering garbage collection may require that the firmware
1142 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1143 * that by attempting to use more space than is available.
1144 */
1145 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001146 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1147
1148 if (!dummy)
1149 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001150
Matthew Garrettf8b84042013-06-01 16:06:20 -04001151 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1152 EFI_VARIABLE_NON_VOLATILE |
1153 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1154 EFI_VARIABLE_RUNTIME_ACCESS,
1155 dummy_size, dummy);
1156
1157 if (status == EFI_SUCCESS) {
1158 /*
1159 * This should have failed, so if it didn't make sure
1160 * that we delete it...
1161 */
1162 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 0, dummy);
1167 }
1168
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001169 kfree(dummy);
1170
Matthew Garrettf8b84042013-06-01 16:06:20 -04001171 /*
1172 * The runtime code may now have triggered a garbage collection
1173 * run, so check the variable info again
1174 */
1175 status = efi.query_variable_info(attributes, &storage_size,
1176 &remaining_size, &max_size);
1177
1178 if (status != EFI_SUCCESS)
1179 return status;
1180
1181 /*
1182 * There still isn't enough room, so return an error
1183 */
1184 if (remaining_size - size < EFI_MIN_RESERVE)
1185 return EFI_OUT_OF_RESOURCES;
1186 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001187
1188 return EFI_SUCCESS;
1189}
Sergey Vlasov36680112013-04-16 18:31:09 +04001190EXPORT_SYMBOL_GPL(efi_query_variable_store);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001191
1192static int __init parse_efi_cmdline(char *str)
1193{
1194 if (*str == '=')
1195 str++;
1196
1197 if (!strncmp(str, "old_map", 7))
Matt Fleming0f8093a2014-01-15 13:36:33 +00001198 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001199
1200 return 0;
1201}
1202early_param("efi", parse_efi_cmdline);