blob: 6f0a46730826e106b974944858e706253203e583 [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
Joe Perches9b7d2042014-01-03 16:08:48 -080070static efi_config_table_type_t arch_tables[] __initdata = {
Leif Lindholm272686b2013-09-05 11:34:54 +010071#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
Joe Perches9b7d2042014-01-03 16:08:48 -080079static bool disable_runtime __initdata = 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;
Joe Perches9b7d2042014-01-03 16:08:48 -0800266 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 {
Joe Perches9b7d2042014-01-03 16:08:48 -0800286 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
287 __func__, nowtime);
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500288 return -1;
289 }
Huang, Ying5b836832008-01-30 13:31:19 +0100290
291 status = efi.set_time(&eft);
292 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800293 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100294 return -1;
295 }
296 return 0;
297}
298
David Vrabel35651842013-05-13 18:56:06 +0100299void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100300{
301 efi_status_t status;
302 efi_time_t eft;
303 efi_time_cap_t cap;
304
305 status = efi.get_time(&eft, &cap);
306 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800307 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100308
David Vrabel35651842013-05-13 18:56:06 +0100309 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
310 eft.minute, eft.second);
311 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100312}
313
Paul Jackson69c91892008-05-14 08:15:58 -0700314/*
315 * Tell the kernel about the EFI memory map. This might include
316 * more than the max 128 entries that can fit in the e820 legacy
317 * (zeropage) memory map.
318 */
319
Paul Jackson200001e2008-06-25 05:44:46 -0700320static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700321{
322 void *p;
323
324 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
325 efi_memory_desc_t *md = p;
326 unsigned long long start = md->phys_addr;
327 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
328 int e820_type;
329
Cliff Wickmane2a71472009-06-16 16:43:40 -0500330 switch (md->type) {
331 case EFI_LOADER_CODE:
332 case EFI_LOADER_DATA:
333 case EFI_BOOT_SERVICES_CODE:
334 case EFI_BOOT_SERVICES_DATA:
335 case EFI_CONVENTIONAL_MEMORY:
336 if (md->attribute & EFI_MEMORY_WB)
337 e820_type = E820_RAM;
338 else
339 e820_type = E820_RESERVED;
340 break;
341 case EFI_ACPI_RECLAIM_MEMORY:
342 e820_type = E820_ACPI;
343 break;
344 case EFI_ACPI_MEMORY_NVS:
345 e820_type = E820_NVS;
346 break;
347 case EFI_UNUSABLE_MEMORY:
348 e820_type = E820_UNUSABLE;
349 break;
350 default:
351 /*
352 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
353 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
354 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
355 */
Paul Jackson69c91892008-05-14 08:15:58 -0700356 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500357 break;
358 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700359 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700360 }
361 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
362}
363
Olof Johansson1adbfa32012-02-12 13:24:29 -0800364int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800365{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100366 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800367 unsigned long pmap;
368
Paul Jackson05486fa2008-06-22 07:22:02 -0700369#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800370 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100371 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800372 pr_err("Memory map is above 4GB, disabling EFI.\n");
373 return -EINVAL;
374 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100375 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700376#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100377 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800378#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100379 memmap.phys_map = (void *)pmap;
380 memmap.nr_map = e->efi_memmap_size /
381 e->efi_memdesc_size;
382 memmap.desc_size = e->efi_memdesc_size;
383 memmap.desc_version = e->efi_memdesc_version;
384
Tejun Heo24aa0782011-07-12 11:16:06 +0200385 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800386
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100387 efi.memmap = &memmap;
388
Olof Johansson1adbfa32012-02-12 13:24:29 -0800389 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800390}
391
Huang, Ying5b836832008-01-30 13:31:19 +0100392static void __init print_efi_memmap(void)
393{
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100394#ifdef EFI_DEBUG
Huang, Ying5b836832008-01-30 13:31:19 +0100395 efi_memory_desc_t *md;
396 void *p;
397 int i;
398
399 for (p = memmap.map, i = 0;
400 p < memmap.map_end;
401 p += memmap.desc_size, i++) {
402 md = p;
Joe Perches9b7d2042014-01-03 16:08:48 -0800403 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100404 i, md->type, md->attribute, md->phys_addr,
405 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
406 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
407 }
Huang, Ying5b836832008-01-30 13:31:19 +0100408#endif /* EFI_DEBUG */
Borislav Petkovf4fccac2013-10-31 17:24:59 +0100409}
Huang, Ying5b836832008-01-30 13:31:19 +0100410
Matthew Garrett916f6762011-05-25 09:53:13 -0400411void __init efi_reserve_boot_services(void)
412{
413 void *p;
414
415 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
416 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200417 u64 start = md->phys_addr;
418 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400419
420 if (md->type != EFI_BOOT_SERVICES_CODE &&
421 md->type != EFI_BOOT_SERVICES_DATA)
422 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200423 /* Only reserve where possible:
424 * - Not within any already allocated areas
425 * - Not over any memory area (really needed, if above?)
426 * - Not within any part of the kernel
427 * - Not the bios reserved area
428 */
Dave Younga7f84f02013-12-20 18:02:15 +0800429 if ((start + size > __pa_symbol(_text)
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800430 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200431 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200432 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200433 /* Could not reserve, skip it */
434 md->num_pages = 0;
Joe Perches9b7d2042014-01-03 16:08:48 -0800435 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
436 start, start+size-1);
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200437 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200438 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400439 }
440}
441
Olof Johansson5189c2a2012-10-24 10:00:44 -0700442void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700443{
Matt Fleming3e909592014-01-15 13:21:22 +0000444 clear_bit(EFI_MEMMAP, &efi.flags);
Josh Triplett78510792012-09-28 17:55:44 -0700445 if (memmap.map) {
446 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
447 memmap.map = NULL;
448 }
449}
450
451void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400452{
453 void *p;
454
Olof Johansson5189c2a2012-10-24 10:00:44 -0700455 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700456 return;
457
Matthew Garrett916f6762011-05-25 09:53:13 -0400458 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
459 efi_memory_desc_t *md = p;
460 unsigned long long start = md->phys_addr;
461 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
462
463 if (md->type != EFI_BOOT_SERVICES_CODE &&
464 md->type != EFI_BOOT_SERVICES_DATA)
465 continue;
466
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200467 /* Could not reserve boot area */
468 if (!size)
469 continue;
470
Matthew Garrett916f6762011-05-25 09:53:13 -0400471 free_bootmem_late(start, size);
472 }
Josh Triplett78510792012-09-28 17:55:44 -0700473
474 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400475}
476
Olof Johansson140bf272012-02-12 13:24:28 -0800477static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100478{
Matt Fleming83e68182012-11-14 09:42:35 +0000479 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800480 efi_system_table_64_t *systab64;
Dave Young1fec0532013-12-20 18:02:19 +0800481 struct efi_setup_data *data = NULL;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800482 u64 tmp = 0;
483
Dave Young1fec0532013-12-20 18:02:19 +0800484 if (efi_setup) {
485 data = early_memremap(efi_setup, sizeof(*data));
486 if (!data)
487 return -ENOMEM;
488 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800489 systab64 = early_ioremap((unsigned long)phys,
490 sizeof(*systab64));
491 if (systab64 == NULL) {
492 pr_err("Couldn't map the system table!\n");
Dave Young1fec0532013-12-20 18:02:19 +0800493 if (data)
494 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800495 return -ENOMEM;
496 }
497
498 efi_systab.hdr = systab64->hdr;
Dave Young1fec0532013-12-20 18:02:19 +0800499 efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor :
500 systab64->fw_vendor;
501 tmp |= data ? data->fw_vendor : systab64->fw_vendor;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800502 efi_systab.fw_revision = systab64->fw_revision;
503 efi_systab.con_in_handle = systab64->con_in_handle;
504 tmp |= systab64->con_in_handle;
505 efi_systab.con_in = systab64->con_in;
506 tmp |= systab64->con_in;
507 efi_systab.con_out_handle = systab64->con_out_handle;
508 tmp |= systab64->con_out_handle;
509 efi_systab.con_out = systab64->con_out;
510 tmp |= systab64->con_out;
511 efi_systab.stderr_handle = systab64->stderr_handle;
512 tmp |= systab64->stderr_handle;
513 efi_systab.stderr = systab64->stderr;
514 tmp |= systab64->stderr;
Dave Young1fec0532013-12-20 18:02:19 +0800515 efi_systab.runtime = data ?
516 (void *)(unsigned long)data->runtime :
517 (void *)(unsigned long)systab64->runtime;
518 tmp |= data ? data->runtime : systab64->runtime;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800519 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
520 tmp |= systab64->boottime;
521 efi_systab.nr_tables = systab64->nr_tables;
Dave Young1fec0532013-12-20 18:02:19 +0800522 efi_systab.tables = data ? (unsigned long)data->tables :
523 systab64->tables;
524 tmp |= data ? data->tables : systab64->tables;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800525
526 early_iounmap(systab64, sizeof(*systab64));
Dave Young1fec0532013-12-20 18:02:19 +0800527 if (data)
528 early_iounmap(data, sizeof(*data));
Olof Johansson1adbfa32012-02-12 13:24:29 -0800529#ifdef CONFIG_X86_32
530 if (tmp >> 32) {
531 pr_err("EFI data located above 4GB, disabling EFI.\n");
532 return -EINVAL;
533 }
534#endif
535 } else {
536 efi_system_table_32_t *systab32;
537
538 systab32 = early_ioremap((unsigned long)phys,
539 sizeof(*systab32));
540 if (systab32 == NULL) {
541 pr_err("Couldn't map the system table!\n");
542 return -ENOMEM;
543 }
544
545 efi_systab.hdr = systab32->hdr;
546 efi_systab.fw_vendor = systab32->fw_vendor;
547 efi_systab.fw_revision = systab32->fw_revision;
548 efi_systab.con_in_handle = systab32->con_in_handle;
549 efi_systab.con_in = systab32->con_in;
550 efi_systab.con_out_handle = systab32->con_out_handle;
551 efi_systab.con_out = systab32->con_out;
552 efi_systab.stderr_handle = systab32->stderr_handle;
553 efi_systab.stderr = systab32->stderr;
554 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
555 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
556 efi_systab.nr_tables = systab32->nr_tables;
557 efi_systab.tables = systab32->tables;
558
559 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800560 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800561
Huang, Ying5b836832008-01-30 13:31:19 +0100562 efi.systab = &efi_systab;
563
564 /*
565 * Verify the EFI Table
566 */
Olof Johansson140bf272012-02-12 13:24:28 -0800567 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800568 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800569 return -EINVAL;
570 }
Huang, Ying5b836832008-01-30 13:31:19 +0100571 if ((efi.systab->hdr.revision >> 16) == 0)
Joe Perches9b7d2042014-01-03 16:08:48 -0800572 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100573 efi.systab->hdr.revision >> 16,
574 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800575
Matt Fleming0f8093a2014-01-15 13:36:33 +0000576 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
577
Olof Johansson140bf272012-02-12 13:24:28 -0800578 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800579}
Huang, Ying5b836832008-01-30 13:31:19 +0100580
Olof Johansson140bf272012-02-12 13:24:28 -0800581static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800582{
583 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100584
585 /*
586 * Check out the runtime services table. We need to map
587 * the runtime services table so that we can grab the physical
588 * address of several of the EFI runtime functions, needed to
589 * set the firmware into virtual mode.
590 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100591 runtime = early_ioremap((unsigned long)efi.systab->runtime,
592 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800593 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800594 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800595 return -ENOMEM;
596 }
597 /*
598 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800599 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800600 * is invoked.
601 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800602 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800603 efi_phys.set_virtual_address_map =
604 (efi_set_virtual_address_map_t *)
605 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800606 /*
607 * Make efi_get_time can be called before entering
608 * virtual mode.
609 */
610 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100611 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800612
Matt Fleming0f8093a2014-01-15 13:36:33 +0000613 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
614
Olof Johansson140bf272012-02-12 13:24:28 -0800615 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800616}
Huang, Ying5b836832008-01-30 13:31:19 +0100617
Olof Johansson140bf272012-02-12 13:24:28 -0800618static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800619{
Huang, Ying5b836832008-01-30 13:31:19 +0100620 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100621 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
622 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800623 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800624 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800625 return -ENOMEM;
626 }
Huang, Ying5b836832008-01-30 13:31:19 +0100627 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500628
Paul Jackson200001e2008-06-25 05:44:46 -0700629 if (add_efi_memmap)
630 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800631
Matt Fleming0f8093a2014-01-15 13:36:33 +0000632 set_bit(EFI_MEMMAP, &efi.flags);
633
Olof Johansson140bf272012-02-12 13:24:28 -0800634 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800635}
636
Dave Young1fec0532013-12-20 18:02:19 +0800637/*
638 * A number of config table entries get remapped to virtual addresses
639 * after entering EFI virtual mode. However, the kexec kernel requires
640 * their physical addresses therefore we pass them via setup_data and
641 * correct those entries to their respective physical addresses here.
642 *
643 * Currently only handles smbios which is necessary for some firmware
644 * implementation.
645 */
646static int __init efi_reuse_config(u64 tables, int nr_tables)
647{
648 int i, sz, ret = 0;
649 void *p, *tablep;
650 struct efi_setup_data *data;
651
652 if (!efi_setup)
653 return 0;
654
655 if (!efi_enabled(EFI_64BIT))
656 return 0;
657
658 data = early_memremap(efi_setup, sizeof(*data));
659 if (!data) {
660 ret = -ENOMEM;
661 goto out;
662 }
663
664 if (!data->smbios)
665 goto out_memremap;
666
667 sz = sizeof(efi_config_table_64_t);
668
669 p = tablep = early_memremap(tables, nr_tables * sz);
670 if (!p) {
671 pr_err("Could not map Configuration table!\n");
672 ret = -ENOMEM;
673 goto out_memremap;
674 }
675
676 for (i = 0; i < efi.systab->nr_tables; i++) {
677 efi_guid_t guid;
678
679 guid = ((efi_config_table_64_t *)p)->guid;
680
681 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
682 ((efi_config_table_64_t *)p)->table = data->smbios;
683 p += sz;
684 }
685 early_iounmap(tablep, nr_tables * sz);
686
687out_memremap:
688 early_iounmap(data, sizeof(*data));
689out:
690 return ret;
691}
692
Olof Johansson83e7ee62012-02-12 13:24:25 -0800693void __init efi_init(void)
694{
695 efi_char16_t *c16;
696 char vendor[100] = "unknown";
697 int i = 0;
698 void *tmp;
699
700#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800701 if (boot_params.efi_info.efi_systab_hi ||
702 boot_params.efi_info.efi_memmap_hi) {
703 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800704 return;
705 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800706 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
707#else
708 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800709 (boot_params.efi_info.efi_systab |
710 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800711#endif
712
Matt Fleming83e68182012-11-14 09:42:35 +0000713 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800714 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000715
Matt Fleming3e909592014-01-15 13:21:22 +0000716 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800717
Dave Younga0998eb2013-12-20 18:02:17 +0800718 efi.config_table = (unsigned long)efi.systab->tables;
719 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
720 efi.runtime = (unsigned long)efi.systab->runtime;
721
Olof Johansson83e7ee62012-02-12 13:24:25 -0800722 /*
723 * Show what we know for posterity
724 */
725 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
726 if (c16) {
727 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
728 vendor[i] = *c16++;
729 vendor[i] = '\0';
730 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800731 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800732 early_iounmap(tmp, 2);
733
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800734 pr_info("EFI v%u.%.02u by %s\n",
735 efi.systab->hdr.revision >> 16,
736 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800737
Dave Young1fec0532013-12-20 18:02:19 +0800738 if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables))
739 return;
740
Leif Lindholm272686b2013-09-05 11:34:54 +0100741 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800742 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000743
Olof Johansson1adbfa32012-02-12 13:24:29 -0800744 /*
745 * Note: We currently don't support runtime services on an EFI
746 * that doesn't match the kernel 32/64-bit mode.
747 */
748
Olof Johansson5189c2a2012-10-24 10:00:44 -0700749 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800750 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000751 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000752 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000753 return;
Olof Johansson140bf272012-02-12 13:24:28 -0800754 }
Matt Fleming83e68182012-11-14 09:42:35 +0000755 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800756 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000757
Matt Fleming3e909592014-01-15 13:21:22 +0000758 set_bit(EFI_MEMMAP, &efi.flags);
Matt Fleming83e68182012-11-14 09:42:35 +0000759
Huang, Ying5b836832008-01-30 13:31:19 +0100760 print_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100761}
762
Josh Triplett2223af32012-09-28 17:57:05 -0700763void __init efi_late_init(void)
764{
765 efi_bgrt_init();
766}
767
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400768void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
769{
770 u64 addr, npages;
771
772 addr = md->virt_addr;
773 npages = md->num_pages;
774
775 memrange_efi_to_native(&addr, &npages);
776
777 if (executable)
778 set_memory_x(addr, npages);
779 else
780 set_memory_nx(addr, npages);
781}
782
Borislav Petkovc55d0162014-02-14 08:24:24 +0100783void __init runtime_code_page_mkexec(void)
Huang, Yinga2172e22008-01-30 13:33:55 +0100784{
785 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100786 void *p;
787
Huang, Yinga2172e22008-01-30 13:33:55 +0100788 /* Make EFI runtime service code area executable */
789 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
790 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100791
792 if (md->type != EFI_RUNTIME_SERVICES_CODE)
793 continue;
794
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400795 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100796 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100797}
Huang, Yinga2172e22008-01-30 13:33:55 +0100798
Matt Fleming3e8fa262012-10-19 13:25:46 +0100799void efi_memory_uc(u64 addr, unsigned long size)
800{
801 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
802 u64 npages;
803
804 npages = round_up(size, page_shift) / page_shift;
805 memrange_efi_to_native(&addr, &npages);
806 set_memory_uc(addr, npages);
807}
808
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100809void __init old_map_region(efi_memory_desc_t *md)
810{
811 u64 start_pfn, end_pfn, end;
812 unsigned long size;
813 void *va;
814
815 start_pfn = PFN_DOWN(md->phys_addr);
816 size = md->num_pages << PAGE_SHIFT;
817 end = md->phys_addr + size;
818 end_pfn = PFN_UP(end);
819
820 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
821 va = __va(md->phys_addr);
822
823 if (!(md->attribute & EFI_MEMORY_WB))
824 efi_memory_uc((u64)(unsigned long)va, size);
825 } else
826 va = efi_ioremap(md->phys_addr, size,
827 md->type, md->attribute);
828
829 md->virt_addr = (u64) (unsigned long) va;
830 if (!va)
831 pr_err("ioremap of 0x%llX failed!\n",
832 (unsigned long long)md->phys_addr);
833}
834
Dave Young481f75c2013-12-20 18:02:16 +0800835/* Merge contiguous regions of the same type and attribute */
836static void __init efi_merge_regions(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100837{
Dave Young481f75c2013-12-20 18:02:16 +0800838 void *p;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400839 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100840
Matthew Garrett202f9d02011-05-05 15:19:44 -0400841 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
842 u64 prev_size;
843 md = p;
844
845 if (!prev_md) {
846 prev_md = md;
847 continue;
848 }
849
850 if (prev_md->type != md->type ||
851 prev_md->attribute != md->attribute) {
852 prev_md = md;
853 continue;
854 }
855
856 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
857
858 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
859 prev_md->num_pages += md->num_pages;
860 md->type = EFI_RESERVED_TYPE;
861 md->attribute = 0;
862 continue;
863 }
864 prev_md = md;
865 }
Dave Young481f75c2013-12-20 18:02:16 +0800866}
867
868static void __init get_systab_virt_addr(efi_memory_desc_t *md)
869{
870 unsigned long size;
871 u64 end, systab;
872
873 size = md->num_pages << EFI_PAGE_SHIFT;
874 end = md->phys_addr + size;
875 systab = (u64)(unsigned long)efi_phys.systab;
876 if (md->phys_addr <= systab && systab < end) {
877 systab += md->virt_addr - md->phys_addr;
878 efi.systab = (efi_system_table_t *)(unsigned long)systab;
879 }
880}
881
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100882static void __init save_runtime_map(void)
Dave Young926172d2013-12-20 18:02:18 +0800883{
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100884#ifdef CONFIG_KEXEC
Dave Young926172d2013-12-20 18:02:18 +0800885 efi_memory_desc_t *md;
886 void *tmp, *p, *q = NULL;
887 int count = 0;
888
889 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
890 md = p;
891
892 if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
893 (md->type == EFI_BOOT_SERVICES_CODE) ||
894 (md->type == EFI_BOOT_SERVICES_DATA))
895 continue;
896 tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL);
897 if (!tmp)
898 goto out;
899 q = tmp;
900
901 memcpy(q + count * memmap.desc_size, md, memmap.desc_size);
902 count++;
903 }
904
Matt Fleming518548a2013-12-21 16:09:46 +0000905 efi_runtime_map_setup(q, count, memmap.desc_size);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100906 return;
Dave Young926172d2013-12-20 18:02:18 +0800907
Dave Young926172d2013-12-20 18:02:18 +0800908out:
909 kfree(q);
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100910 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
911#endif
Dave Young1fec0532013-12-20 18:02:19 +0800912}
913
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100914static void *realloc_pages(void *old_memmap, int old_shift)
Dave Young481f75c2013-12-20 18:02:16 +0800915{
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100916 void *ret;
917
918 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
919 if (!ret)
920 goto out;
921
922 /*
923 * A first-time allocation doesn't have anything to copy.
924 */
925 if (!old_memmap)
926 return ret;
927
928 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
929
930out:
931 free_pages((unsigned long)old_memmap, old_shift);
932 return ret;
933}
934
935/*
936 * Map the efi memory ranges of the runtime services and update new_mmap with
937 * virtual addresses.
938 */
939static void * __init efi_map_regions(int *count, int *pg_shift)
940{
941 void *p, *new_memmap = NULL;
942 unsigned long left = 0;
Dave Young481f75c2013-12-20 18:02:16 +0800943 efi_memory_desc_t *md;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400944
Huang, Ying5b836832008-01-30 13:31:19 +0100945 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
946 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700947 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
948#ifdef CONFIG_X86_64
949 if (md->type != EFI_BOOT_SERVICES_CODE &&
950 md->type != EFI_BOOT_SERVICES_DATA)
951#endif
952 continue;
953 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100954
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100955 efi_map_region(md);
Dave Young481f75c2013-12-20 18:02:16 +0800956 get_systab_virt_addr(md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100957
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100958 if (left < memmap.desc_size) {
959 new_memmap = realloc_pages(new_memmap, *pg_shift);
960 if (!new_memmap)
961 return NULL;
962
963 left += PAGE_SIZE << *pg_shift;
964 (*pg_shift)++;
965 }
966
Dave Young481f75c2013-12-20 18:02:16 +0800967 memcpy(new_memmap + (*count * memmap.desc_size), md,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400968 memmap.desc_size);
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100969
970 left -= memmap.desc_size;
Dave Young481f75c2013-12-20 18:02:16 +0800971 (*count)++;
972 }
973
974 return new_memmap;
Dave Young481f75c2013-12-20 18:02:16 +0800975}
976
Borislav Petkovfabb37c2014-01-18 12:48:18 +0100977static void __init kexec_enter_virtual_mode(void)
978{
979#ifdef CONFIG_KEXEC
980 efi_memory_desc_t *md;
981 void *p;
982
983 efi.systab = NULL;
984
985 /*
986 * We don't do virtual mode, since we don't do runtime services, on
987 * non-native EFI
988 */
989 if (!efi_is_native()) {
990 efi_unmap_memmap();
991 return;
992 }
993
994 /*
995 * Map efi regions which were passed via setup_data. The virt_addr is a
996 * fixed addr which was used in first kernel of a kexec boot.
997 */
998 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
999 md = p;
1000 efi_map_region_fixed(md); /* FIXME: add error handling */
1001 get_systab_virt_addr(md);
1002 }
1003
1004 save_runtime_map();
1005
1006 BUG_ON(!efi.systab);
1007
1008 efi_sync_low_kernel_mappings();
1009
1010 /*
1011 * Now that EFI is in virtual mode, update the function
1012 * pointers in the runtime service table to the new virtual addresses.
1013 *
1014 * Call EFI services through wrapper functions.
1015 */
1016 efi.runtime_version = efi_systab.hdr.revision;
1017 efi.get_time = virt_efi_get_time;
1018 efi.set_time = virt_efi_set_time;
1019 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1020 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1021 efi.get_variable = virt_efi_get_variable;
1022 efi.get_next_variable = virt_efi_get_next_variable;
1023 efi.set_variable = virt_efi_set_variable;
1024 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1025 efi.reset_system = virt_efi_reset_system;
1026 efi.set_virtual_address_map = NULL;
1027 efi.query_variable_info = virt_efi_query_variable_info;
1028 efi.update_capsule = virt_efi_update_capsule;
1029 efi.query_capsule_caps = virt_efi_query_capsule_caps;
1030
1031 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1032 runtime_code_page_mkexec();
1033
1034 /* clean DUMMY object */
1035 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1036 EFI_VARIABLE_NON_VOLATILE |
1037 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1038 EFI_VARIABLE_RUNTIME_ACCESS,
1039 0, NULL);
1040#endif
1041}
1042
Dave Young481f75c2013-12-20 18:02:16 +08001043/*
1044 * This function will switch the EFI runtime services to virtual mode.
1045 * Essentially, we look through the EFI memmap and map every region that
1046 * has the runtime attribute bit set in its memory descriptor into the
1047 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1048 *
1049 * The old method which used to update that memory descriptor with the
1050 * virtual address obtained from ioremap() is still supported when the
1051 * kernel is booted with efi=old_map on its command line. Same old
1052 * method enabled the runtime services to be called without having to
1053 * thunk back into physical mode for every invocation.
1054 *
1055 * The new method does a pagetable switch in a preemption-safe manner
1056 * so that we're in a different address space when calling a runtime
1057 * function. For function arguments passing we do copy the PGDs of the
1058 * kernel page table into ->trampoline_pgd prior to each call.
Dave Young1fec0532013-12-20 18:02:19 +08001059 *
1060 * Specially for kexec boot, efi runtime maps in previous kernel should
1061 * be passed in via setup_data. In that case runtime ranges will be mapped
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001062 * to the same virtual addresses as the first kernel, see
1063 * kexec_enter_virtual_mode().
Dave Young481f75c2013-12-20 18:02:16 +08001064 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001065static void __init __efi_enter_virtual_mode(void)
Dave Young481f75c2013-12-20 18:02:16 +08001066{
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001067 int count = 0, pg_shift = 0;
Dave Young481f75c2013-12-20 18:02:16 +08001068 void *new_memmap = NULL;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001069 efi_status_t status;
Dave Young481f75c2013-12-20 18:02:16 +08001070
1071 efi.systab = NULL;
1072
1073 /*
1074 * We don't do virtual mode, since we don't do runtime services, on
1075 * non-native EFI
1076 */
1077 if (!efi_is_native()) {
1078 efi_unmap_memmap();
1079 return;
1080 }
1081
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001082 efi_merge_regions();
1083 new_memmap = efi_map_regions(&count, &pg_shift);
1084 if (!new_memmap) {
1085 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1086 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001087 }
Dave Young926172d2013-12-20 18:02:18 +08001088
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001089 save_runtime_map();
1090
Huang, Ying5b836832008-01-30 13:31:19 +01001091 BUG_ON(!efi.systab);
1092
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001093 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1094 return;
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001095
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001096 efi_sync_low_kernel_mappings();
Borislav Petkov11cc8512014-01-18 12:48:15 +01001097 efi_dump_pagetable();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001098
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001099 status = phys_efi_set_virtual_address_map(
Dave Young1fec0532013-12-20 18:02:19 +08001100 memmap.desc_size * count,
1101 memmap.desc_size,
1102 memmap.desc_version,
1103 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +01001104
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001105 if (status != EFI_SUCCESS) {
1106 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1107 status);
1108 panic("EFI call to SetVirtualAddressMap() failed!");
Huang, Ying5b836832008-01-30 13:31:19 +01001109 }
1110
1111 /*
1112 * Now that EFI is in virtual mode, update the function
1113 * pointers in the runtime service table to the new virtual addresses.
1114 *
1115 * Call EFI services through wrapper functions.
1116 */
Matt Fleming712ba9e2013-01-25 10:07:25 +00001117 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +01001118 efi.get_time = virt_efi_get_time;
1119 efi.set_time = virt_efi_set_time;
1120 efi.get_wakeup_time = virt_efi_get_wakeup_time;
1121 efi.set_wakeup_time = virt_efi_set_wakeup_time;
1122 efi.get_variable = virt_efi_get_variable;
1123 efi.get_next_variable = virt_efi_get_next_variable;
1124 efi.set_variable = virt_efi_set_variable;
1125 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1126 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -04001127 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -04001128 efi.query_variable_info = virt_efi_query_variable_info;
1129 efi.update_capsule = virt_efi_update_capsule;
1130 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001131
Borislav Petkovc55d0162014-02-14 08:24:24 +01001132 efi_runtime_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -08001133
Borislav Petkovb7b898a2014-01-18 12:48:17 +01001134 /*
1135 * We mapped the descriptor array into the EFI pagetable above but we're
1136 * not unmapping it here. Here's why:
1137 *
1138 * We're copying select PGDs from the kernel page table to the EFI page
1139 * table and when we do so and make changes to those PGDs like unmapping
1140 * stuff from them, those changes appear in the kernel page table and we
1141 * go boom.
1142 *
1143 * From setup_real_mode():
1144 *
1145 * ...
1146 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1147 *
1148 * In this particular case, our allocation is in PGD 0 of the EFI page
1149 * table but we've copied that PGD from PGD[272] of the EFI page table:
1150 *
1151 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1152 *
1153 * where the direct memory mapping in kernel space is.
1154 *
1155 * new_memmap's VA comes from that direct mapping and thus clearing it,
1156 * it would get cleared in the kernel page table too.
1157 *
1158 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1159 */
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001160 free_pages((unsigned long)new_memmap, pg_shift);
Matthew Garrettf8b84042013-06-01 16:06:20 -04001161
1162 /* clean DUMMY object */
1163 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1164 EFI_VARIABLE_NON_VOLATILE |
1165 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1166 EFI_VARIABLE_RUNTIME_ACCESS,
1167 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +01001168}
1169
Borislav Petkovfabb37c2014-01-18 12:48:18 +01001170void __init efi_enter_virtual_mode(void)
1171{
1172 if (efi_setup)
1173 kexec_enter_virtual_mode();
1174 else
1175 __efi_enter_virtual_mode();
1176}
1177
Huang, Ying5b836832008-01-30 13:31:19 +01001178/*
1179 * Convenience functions to obtain memory types and attributes
1180 */
1181u32 efi_mem_type(unsigned long phys_addr)
1182{
1183 efi_memory_desc_t *md;
1184 void *p;
1185
Matt Fleming83e68182012-11-14 09:42:35 +00001186 if (!efi_enabled(EFI_MEMMAP))
1187 return 0;
1188
Huang, Ying5b836832008-01-30 13:31:19 +01001189 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1190 md = p;
1191 if ((md->phys_addr <= phys_addr) &&
1192 (phys_addr < (md->phys_addr +
1193 (md->num_pages << EFI_PAGE_SHIFT))))
1194 return md->type;
1195 }
1196 return 0;
1197}
1198
1199u64 efi_mem_attributes(unsigned long phys_addr)
1200{
1201 efi_memory_desc_t *md;
1202 void *p;
1203
1204 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1205 md = p;
1206 if ((md->phys_addr <= phys_addr) &&
1207 (phys_addr < (md->phys_addr +
1208 (md->num_pages << EFI_PAGE_SHIFT))))
1209 return md->attribute;
1210 }
1211 return 0;
1212}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001213
1214/*
Madper Xie5db80c62013-11-10 22:15:11 +08001215 * Some firmware implementations refuse to boot if there's insufficient space
1216 * in the variable store. Ensure that we never use more than a safe limit.
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001217 *
1218 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1219 * store.
1220 */
1221efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1222{
1223 efi_status_t status;
1224 u64 storage_size, remaining_size, max_size;
1225
Matthew Garrettf8b84042013-06-01 16:06:20 -04001226 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
1227 return 0;
1228
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001229 status = efi.query_variable_info(attributes, &storage_size,
1230 &remaining_size, &max_size);
1231 if (status != EFI_SUCCESS)
1232 return status;
1233
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001234 /*
Madper Xie5db80c62013-11-10 22:15:11 +08001235 * We account for that by refusing the write if permitting it would
1236 * reduce the available space to under 5KB. This figure was provided by
1237 * Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -07001238 */
Matthew Garrettf8b84042013-06-01 16:06:20 -04001239 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1240 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +02001241
Matthew Garrettf8b84042013-06-01 16:06:20 -04001242 /*
1243 * Triggering garbage collection may require that the firmware
1244 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1245 * that by attempting to use more space than is available.
1246 */
1247 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001248 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
1249
1250 if (!dummy)
1251 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +02001252
Matthew Garrettf8b84042013-06-01 16:06:20 -04001253 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1254 EFI_VARIABLE_NON_VOLATILE |
1255 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1256 EFI_VARIABLE_RUNTIME_ACCESS,
1257 dummy_size, dummy);
1258
1259 if (status == EFI_SUCCESS) {
1260 /*
1261 * This should have failed, so if it didn't make sure
1262 * that we delete it...
1263 */
1264 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1265 EFI_VARIABLE_NON_VOLATILE |
1266 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1267 EFI_VARIABLE_RUNTIME_ACCESS,
1268 0, dummy);
1269 }
1270
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +01001271 kfree(dummy);
1272
Matthew Garrettf8b84042013-06-01 16:06:20 -04001273 /*
1274 * The runtime code may now have triggered a garbage collection
1275 * run, so check the variable info again
1276 */
1277 status = efi.query_variable_info(attributes, &storage_size,
1278 &remaining_size, &max_size);
1279
1280 if (status != EFI_SUCCESS)
1281 return status;
1282
1283 /*
1284 * There still isn't enough room, so return an error
1285 */
1286 if (remaining_size - size < EFI_MIN_RESERVE)
1287 return EFI_OUT_OF_RESOURCES;
1288 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001289
1290 return EFI_SUCCESS;
1291}
Sergey Vlasov36680112013-04-16 18:31:09 +04001292EXPORT_SYMBOL_GPL(efi_query_variable_store);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001293
1294static int __init parse_efi_cmdline(char *str)
1295{
1296 if (*str == '=')
1297 str++;
1298
1299 if (!strncmp(str, "old_map", 7))
Matt Fleming0f8093a2014-01-15 13:36:33 +00001300 set_bit(EFI_OLD_MEMMAP, &efi.flags);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +01001301
1302 return 0;
1303}
1304early_param("efi", parse_efi_cmdline);