blob: e2cd38ffc4064e694cf8231805ce24c1067f3265 [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>
15 *
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
18 *
19 * All EFI Runtime Services are not implemented yet as EFI only
20 * supports physical mode addressing on SoftSDV. This is to be fixed
21 * in a future version. --drummond 1999-07-20
22 *
23 * Implemented EFI runtime services and virtual mode calls. --davidm
24 *
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
27 */
28
Olof Johanssone3cb3f52012-02-12 13:24:26 -080029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Huang, Ying5b836832008-01-30 13:31:19 +010031#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/efi.h>
Josh Triplett2223af32012-09-28 17:57:05 -070034#include <linux/efi-bgrt.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040035#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010036#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070037#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010038#include <linux/spinlock.h>
39#include <linux/uaccess.h>
40#include <linux/time.h>
41#include <linux/io.h>
42#include <linux/reboot.h>
43#include <linux/bcd.h>
44
45#include <asm/setup.h>
46#include <asm/efi.h>
47#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010048#include <asm/cacheflush.h>
49#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080050#include <asm/x86_init.h>
Huang, Ying5b836832008-01-30 13:31:19 +010051
52#define EFI_DEBUG 1
Huang, Ying5b836832008-01-30 13:31:19 +010053
Jan Beulichd80603c2011-07-05 12:22:18 +010054struct efi __read_mostly efi = {
55 .mps = EFI_INVALID_TABLE_ADDR,
56 .acpi = EFI_INVALID_TABLE_ADDR,
57 .acpi20 = EFI_INVALID_TABLE_ADDR,
58 .smbios = EFI_INVALID_TABLE_ADDR,
59 .sal_systab = EFI_INVALID_TABLE_ADDR,
60 .boot_info = EFI_INVALID_TABLE_ADDR,
61 .hcdp = EFI_INVALID_TABLE_ADDR,
62 .uga = EFI_INVALID_TABLE_ADDR,
63 .uv_systab = EFI_INVALID_TABLE_ADDR,
64};
Huang, Ying5b836832008-01-30 13:31:19 +010065EXPORT_SYMBOL(efi);
66
67struct efi_memory_map memmap;
68
Harvey Harrisonecaea422008-02-13 13:26:13 -080069static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010070static efi_system_table_t efi_systab __initdata;
71
Olof Johansson5189c2a2012-10-24 10:00:44 -070072static inline bool efi_is_native(void)
73{
Matt Fleming83e68182012-11-14 09:42:35 +000074 return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
Olof Johansson5189c2a2012-10-24 10:00:44 -070075}
76
Matt Fleming83e68182012-11-14 09:42:35 +000077unsigned long x86_efi_facility;
78
79/*
80 * Returns 1 if 'facility' is enabled, 0 otherwise.
81 */
82int efi_enabled(int facility)
83{
84 return test_bit(facility, &x86_efi_facility) != 0;
85}
86EXPORT_SYMBOL(efi_enabled);
87
Matt Flemingfb834c72013-02-20 20:36:12 +000088static bool disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010089static int __init setup_noefi(char *arg)
90{
Matt Flemingfb834c72013-02-20 20:36:12 +000091 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010092 return 0;
93}
94early_param("noefi", setup_noefi);
95
Paul Jackson200001e2008-06-25 05:44:46 -070096int add_efi_memmap;
97EXPORT_SYMBOL(add_efi_memmap);
98
99static int __init setup_add_efi_memmap(char *arg)
100{
101 add_efi_memmap = 1;
102 return 0;
103}
104early_param("add_efi_memmap", setup_add_efi_memmap);
105
106
Huang, Ying5b836832008-01-30 13:31:19 +0100107static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
108{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100109 unsigned long flags;
110 efi_status_t status;
111
112 spin_lock_irqsave(&rtc_lock, flags);
113 status = efi_call_virt2(get_time, tm, tc);
114 spin_unlock_irqrestore(&rtc_lock, flags);
115 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100116}
117
118static efi_status_t virt_efi_set_time(efi_time_t *tm)
119{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100120 unsigned long flags;
121 efi_status_t status;
122
123 spin_lock_irqsave(&rtc_lock, flags);
124 status = efi_call_virt1(set_time, tm);
125 spin_unlock_irqrestore(&rtc_lock, flags);
126 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100127}
128
129static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
130 efi_bool_t *pending,
131 efi_time_t *tm)
132{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100133 unsigned long flags;
134 efi_status_t status;
135
136 spin_lock_irqsave(&rtc_lock, flags);
137 status = efi_call_virt3(get_wakeup_time,
138 enabled, pending, tm);
139 spin_unlock_irqrestore(&rtc_lock, flags);
140 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100141}
142
143static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
144{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100145 unsigned long flags;
146 efi_status_t status;
147
148 spin_lock_irqsave(&rtc_lock, flags);
149 status = efi_call_virt2(set_wakeup_time,
150 enabled, tm);
151 spin_unlock_irqrestore(&rtc_lock, flags);
152 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100153}
154
155static efi_status_t virt_efi_get_variable(efi_char16_t *name,
156 efi_guid_t *vendor,
157 u32 *attr,
158 unsigned long *data_size,
159 void *data)
160{
161 return efi_call_virt5(get_variable,
162 name, vendor, attr,
163 data_size, data);
164}
165
166static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
167 efi_char16_t *name,
168 efi_guid_t *vendor)
169{
170 return efi_call_virt3(get_next_variable,
171 name_size, name, vendor);
172}
173
174static efi_status_t virt_efi_set_variable(efi_char16_t *name,
175 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400176 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100177 unsigned long data_size,
178 void *data)
179{
180 return efi_call_virt5(set_variable,
181 name, vendor, attr,
182 data_size, data);
183}
184
Matthew Garrett3b370232011-06-06 15:36:25 -0400185static efi_status_t virt_efi_query_variable_info(u32 attr,
186 u64 *storage_space,
187 u64 *remaining_space,
188 u64 *max_variable_size)
189{
190 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
191 return EFI_UNSUPPORTED;
192
193 return efi_call_virt4(query_variable_info, attr, storage_space,
194 remaining_space, max_variable_size);
195}
196
Huang, Ying5b836832008-01-30 13:31:19 +0100197static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
198{
199 return efi_call_virt1(get_next_high_mono_count, count);
200}
201
202static void virt_efi_reset_system(int reset_type,
203 efi_status_t status,
204 unsigned long data_size,
205 efi_char16_t *data)
206{
207 efi_call_virt4(reset_system, reset_type, status,
208 data_size, data);
209}
210
Matthew Garrett3b370232011-06-06 15:36:25 -0400211static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
212 unsigned long count,
213 unsigned long sg_list)
214{
215 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
216 return EFI_UNSUPPORTED;
217
218 return efi_call_virt3(update_capsule, capsules, count, sg_list);
219}
220
221static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
222 unsigned long count,
223 u64 *max_size,
224 int *reset_type)
225{
226 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
227 return EFI_UNSUPPORTED;
228
229 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
230 reset_type);
231}
232
Huang, Ying5b836832008-01-30 13:31:19 +0100233static efi_status_t __init phys_efi_set_virtual_address_map(
234 unsigned long memory_map_size,
235 unsigned long descriptor_size,
236 u32 descriptor_version,
237 efi_memory_desc_t *virtual_map)
238{
239 efi_status_t status;
240
241 efi_call_phys_prelog();
242 status = efi_call_phys4(efi_phys.set_virtual_address_map,
243 memory_map_size, descriptor_size,
244 descriptor_version, virtual_map);
245 efi_call_phys_epilog();
246 return status;
247}
248
Linus Torvalds11520e52012-12-15 15:15:24 -0800249static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
250 efi_time_cap_t *tc)
251{
252 unsigned long flags;
253 efi_status_t status;
254
255 spin_lock_irqsave(&rtc_lock, flags);
256 efi_call_phys_prelog();
257 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
258 virt_to_phys(tc));
259 efi_call_phys_epilog();
260 spin_unlock_irqrestore(&rtc_lock, flags);
261 return status;
262}
263
264int efi_set_rtc_mmss(unsigned long nowtime)
Huang, Ying5b836832008-01-30 13:31:19 +0100265{
266 int real_seconds, real_minutes;
267 efi_status_t status;
268 efi_time_t eft;
269 efi_time_cap_t cap;
270
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
277 real_seconds = nowtime % 60;
278 real_minutes = nowtime / 60;
279 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
280 real_minutes += 30;
281 real_minutes %= 60;
282 eft.minute = real_minutes;
283 eft.second = real_seconds;
284
285 status = efi.set_time(&eft);
286 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800287 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100288 return -1;
289 }
290 return 0;
291}
292
Linus Torvalds11520e52012-12-15 15:15:24 -0800293unsigned long efi_get_time(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100294{
295 efi_status_t status;
296 efi_time_t eft;
297 efi_time_cap_t cap;
298
299 status = efi.get_time(&eft, &cap);
300 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800301 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100302
303 return mktime(eft.year, eft.month, eft.day, eft.hour,
304 eft.minute, eft.second);
305}
306
Paul Jackson69c91892008-05-14 08:15:58 -0700307/*
308 * Tell the kernel about the EFI memory map. This might include
309 * more than the max 128 entries that can fit in the e820 legacy
310 * (zeropage) memory map.
311 */
312
Paul Jackson200001e2008-06-25 05:44:46 -0700313static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700314{
315 void *p;
316
317 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
318 efi_memory_desc_t *md = p;
319 unsigned long long start = md->phys_addr;
320 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
321 int e820_type;
322
Cliff Wickmane2a71472009-06-16 16:43:40 -0500323 switch (md->type) {
324 case EFI_LOADER_CODE:
325 case EFI_LOADER_DATA:
326 case EFI_BOOT_SERVICES_CODE:
327 case EFI_BOOT_SERVICES_DATA:
328 case EFI_CONVENTIONAL_MEMORY:
329 if (md->attribute & EFI_MEMORY_WB)
330 e820_type = E820_RAM;
331 else
332 e820_type = E820_RESERVED;
333 break;
334 case EFI_ACPI_RECLAIM_MEMORY:
335 e820_type = E820_ACPI;
336 break;
337 case EFI_ACPI_MEMORY_NVS:
338 e820_type = E820_NVS;
339 break;
340 case EFI_UNUSABLE_MEMORY:
341 e820_type = E820_UNUSABLE;
342 break;
343 default:
344 /*
345 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
346 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
347 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
348 */
Paul Jackson69c91892008-05-14 08:15:58 -0700349 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500350 break;
351 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700352 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700353 }
354 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
355}
356
Olof Johansson1adbfa32012-02-12 13:24:29 -0800357int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800358{
359 unsigned long pmap;
360
Paul Jackson05486fa2008-06-22 07:22:02 -0700361#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800362 /* Can't handle data above 4GB at this time */
363 if (boot_params.efi_info.efi_memmap_hi) {
364 pr_err("Memory map is above 4GB, disabling EFI.\n");
365 return -EINVAL;
366 }
Huang, Yingecacf092008-06-02 14:26:21 +0800367 pmap = boot_params.efi_info.efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700368#else
369 pmap = (boot_params.efi_info.efi_memmap |
370 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
Huang, Yingecacf092008-06-02 14:26:21 +0800371#endif
372 memmap.phys_map = (void *)pmap;
373 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
374 boot_params.efi_info.efi_memdesc_size;
375 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
376 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
Tejun Heo24aa0782011-07-12 11:16:06 +0200377 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800378
379 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800380}
381
Huang, Ying5b836832008-01-30 13:31:19 +0100382#if EFI_DEBUG
383static void __init print_efi_memmap(void)
384{
385 efi_memory_desc_t *md;
386 void *p;
387 int i;
388
389 for (p = memmap.map, i = 0;
390 p < memmap.map_end;
391 p += memmap.desc_size, i++) {
392 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800393 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100394 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
395 i, md->type, md->attribute, md->phys_addr,
396 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
397 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
398 }
399}
400#endif /* EFI_DEBUG */
401
Matthew Garrett916f6762011-05-25 09:53:13 -0400402void __init efi_reserve_boot_services(void)
403{
404 void *p;
405
406 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
407 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200408 u64 start = md->phys_addr;
409 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400410
411 if (md->type != EFI_BOOT_SERVICES_CODE &&
412 md->type != EFI_BOOT_SERVICES_DATA)
413 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200414 /* Only reserve where possible:
415 * - Not within any already allocated areas
416 * - Not over any memory area (really needed, if above?)
417 * - Not within any part of the kernel
418 * - Not the bios reserved area
419 */
420 if ((start+size >= virt_to_phys(_text)
421 && start <= virt_to_phys(_end)) ||
422 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200423 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200424 /* Could not reserve, skip it */
425 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800426 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200427 "[0x%010llx-0x%010llx]\n",
428 start, start+size-1);
429 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200430 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400431 }
432}
433
Olof Johansson5189c2a2012-10-24 10:00:44 -0700434void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700435{
Matt Fleming83e68182012-11-14 09:42:35 +0000436 clear_bit(EFI_MEMMAP, &x86_efi_facility);
Josh Triplett78510792012-09-28 17:55:44 -0700437 if (memmap.map) {
438 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
439 memmap.map = NULL;
440 }
441}
442
443void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400444{
445 void *p;
446
Olof Johansson5189c2a2012-10-24 10:00:44 -0700447 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700448 return;
449
Matthew Garrett916f6762011-05-25 09:53:13 -0400450 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
451 efi_memory_desc_t *md = p;
452 unsigned long long start = md->phys_addr;
453 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
454
455 if (md->type != EFI_BOOT_SERVICES_CODE &&
456 md->type != EFI_BOOT_SERVICES_DATA)
457 continue;
458
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200459 /* Could not reserve boot area */
460 if (!size)
461 continue;
462
Matthew Garrett916f6762011-05-25 09:53:13 -0400463 free_bootmem_late(start, size);
464 }
Josh Triplett78510792012-09-28 17:55:44 -0700465
466 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400467}
468
Olof Johansson140bf272012-02-12 13:24:28 -0800469static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100470{
Matt Fleming83e68182012-11-14 09:42:35 +0000471 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800472 efi_system_table_64_t *systab64;
473 u64 tmp = 0;
474
475 systab64 = early_ioremap((unsigned long)phys,
476 sizeof(*systab64));
477 if (systab64 == NULL) {
478 pr_err("Couldn't map the system table!\n");
479 return -ENOMEM;
480 }
481
482 efi_systab.hdr = systab64->hdr;
483 efi_systab.fw_vendor = systab64->fw_vendor;
484 tmp |= systab64->fw_vendor;
485 efi_systab.fw_revision = systab64->fw_revision;
486 efi_systab.con_in_handle = systab64->con_in_handle;
487 tmp |= systab64->con_in_handle;
488 efi_systab.con_in = systab64->con_in;
489 tmp |= systab64->con_in;
490 efi_systab.con_out_handle = systab64->con_out_handle;
491 tmp |= systab64->con_out_handle;
492 efi_systab.con_out = systab64->con_out;
493 tmp |= systab64->con_out;
494 efi_systab.stderr_handle = systab64->stderr_handle;
495 tmp |= systab64->stderr_handle;
496 efi_systab.stderr = systab64->stderr;
497 tmp |= systab64->stderr;
498 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
499 tmp |= systab64->runtime;
500 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
501 tmp |= systab64->boottime;
502 efi_systab.nr_tables = systab64->nr_tables;
503 efi_systab.tables = systab64->tables;
504 tmp |= systab64->tables;
505
506 early_iounmap(systab64, sizeof(*systab64));
507#ifdef CONFIG_X86_32
508 if (tmp >> 32) {
509 pr_err("EFI data located above 4GB, disabling EFI.\n");
510 return -EINVAL;
511 }
512#endif
513 } else {
514 efi_system_table_32_t *systab32;
515
516 systab32 = early_ioremap((unsigned long)phys,
517 sizeof(*systab32));
518 if (systab32 == NULL) {
519 pr_err("Couldn't map the system table!\n");
520 return -ENOMEM;
521 }
522
523 efi_systab.hdr = systab32->hdr;
524 efi_systab.fw_vendor = systab32->fw_vendor;
525 efi_systab.fw_revision = systab32->fw_revision;
526 efi_systab.con_in_handle = systab32->con_in_handle;
527 efi_systab.con_in = systab32->con_in;
528 efi_systab.con_out_handle = systab32->con_out_handle;
529 efi_systab.con_out = systab32->con_out;
530 efi_systab.stderr_handle = systab32->stderr_handle;
531 efi_systab.stderr = systab32->stderr;
532 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
533 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
534 efi_systab.nr_tables = systab32->nr_tables;
535 efi_systab.tables = systab32->tables;
536
537 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800538 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800539
Huang, Ying5b836832008-01-30 13:31:19 +0100540 efi.systab = &efi_systab;
541
542 /*
543 * Verify the EFI Table
544 */
Olof Johansson140bf272012-02-12 13:24:28 -0800545 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800546 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800547 return -EINVAL;
548 }
Huang, Ying5b836832008-01-30 13:31:19 +0100549 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800550 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100551 "%d.%02d, expected 1.00 or greater!\n",
552 efi.systab->hdr.revision >> 16,
553 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800554
555 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800556}
Huang, Ying5b836832008-01-30 13:31:19 +0100557
Olof Johansson140bf272012-02-12 13:24:28 -0800558static int __init efi_config_init(u64 tables, int nr_tables)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800559{
Olof Johansson1adbfa32012-02-12 13:24:29 -0800560 void *config_tables, *tablep;
561 int i, sz;
562
Matt Fleming83e68182012-11-14 09:42:35 +0000563 if (efi_enabled(EFI_64BIT))
Olof Johansson1adbfa32012-02-12 13:24:29 -0800564 sz = sizeof(efi_config_table_64_t);
565 else
566 sz = sizeof(efi_config_table_32_t);
Huang, Ying5b836832008-01-30 13:31:19 +0100567
568 /*
569 * Let's see what config tables the firmware passed to us.
570 */
Olof Johansson1adbfa32012-02-12 13:24:29 -0800571 config_tables = early_ioremap(tables, nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800572 if (config_tables == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800573 pr_err("Could not map Configuration table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800574 return -ENOMEM;
575 }
Huang, Ying5b836832008-01-30 13:31:19 +0100576
Olof Johansson1adbfa32012-02-12 13:24:29 -0800577 tablep = config_tables;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800578 pr_info("");
Huang, Ying5b836832008-01-30 13:31:19 +0100579 for (i = 0; i < efi.systab->nr_tables; i++) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800580 efi_guid_t guid;
581 unsigned long table;
Olof Johanssona6a46f42012-02-12 13:24:27 -0800582
Matt Fleming83e68182012-11-14 09:42:35 +0000583 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800584 u64 table64;
585 guid = ((efi_config_table_64_t *)tablep)->guid;
586 table64 = ((efi_config_table_64_t *)tablep)->table;
587 table = table64;
588#ifdef CONFIG_X86_32
589 if (table64 >> 32) {
590 pr_cont("\n");
591 pr_err("Table located above 4GB, disabling EFI.\n");
592 early_iounmap(config_tables,
593 efi.systab->nr_tables * sz);
594 return -EINVAL;
595 }
596#endif
597 } else {
598 guid = ((efi_config_table_32_t *)tablep)->guid;
599 table = ((efi_config_table_32_t *)tablep)->table;
600 }
Olof Johanssona6a46f42012-02-12 13:24:27 -0800601 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
602 efi.mps = table;
603 pr_cont(" MPS=0x%lx ", table);
604 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
605 efi.acpi20 = table;
606 pr_cont(" ACPI 2.0=0x%lx ", table);
607 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
608 efi.acpi = table;
609 pr_cont(" ACPI=0x%lx ", table);
610 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
611 efi.smbios = table;
612 pr_cont(" SMBIOS=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100613#ifdef CONFIG_X86_UV
Olof Johanssona6a46f42012-02-12 13:24:27 -0800614 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
615 efi.uv_systab = table;
616 pr_cont(" UVsystab=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100617#endif
Olof Johanssona6a46f42012-02-12 13:24:27 -0800618 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
619 efi.hcdp = table;
620 pr_cont(" HCDP=0x%lx ", table);
621 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
622 efi.uga = table;
623 pr_cont(" UGA=0x%lx ", table);
Huang, Ying5b836832008-01-30 13:31:19 +0100624 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800625 tablep += sz;
Huang, Ying5b836832008-01-30 13:31:19 +0100626 }
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800627 pr_cont("\n");
Olof Johanssona6a46f42012-02-12 13:24:27 -0800628 early_iounmap(config_tables, efi.systab->nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800629 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800630}
631
Olof Johansson140bf272012-02-12 13:24:28 -0800632static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800633{
634 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100635
636 /*
637 * Check out the runtime services table. We need to map
638 * the runtime services table so that we can grab the physical
639 * address of several of the EFI runtime functions, needed to
640 * set the firmware into virtual mode.
641 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100642 runtime = early_ioremap((unsigned long)efi.systab->runtime,
643 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800644 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800645 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800646 return -ENOMEM;
647 }
648 /*
649 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800650 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800651 * is invoked.
652 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800653 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800654 efi_phys.set_virtual_address_map =
655 (efi_set_virtual_address_map_t *)
656 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800657 /*
658 * Make efi_get_time can be called before entering
659 * virtual mode.
660 */
661 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100662 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800663
664 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800665}
Huang, Ying5b836832008-01-30 13:31:19 +0100666
Olof Johansson140bf272012-02-12 13:24:28 -0800667static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800668{
Huang, Ying5b836832008-01-30 13:31:19 +0100669 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100670 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
671 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800672 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800673 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800674 return -ENOMEM;
675 }
Huang, Ying5b836832008-01-30 13:31:19 +0100676 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500677
Paul Jackson200001e2008-06-25 05:44:46 -0700678 if (add_efi_memmap)
679 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800680
681 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800682}
683
684void __init efi_init(void)
685{
686 efi_char16_t *c16;
687 char vendor[100] = "unknown";
688 int i = 0;
689 void *tmp;
690
691#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800692 if (boot_params.efi_info.efi_systab_hi ||
693 boot_params.efi_info.efi_memmap_hi) {
694 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800695 return;
696 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800697 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
698#else
699 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800700 (boot_params.efi_info.efi_systab |
701 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800702#endif
703
Matt Fleming83e68182012-11-14 09:42:35 +0000704 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800705 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000706
707 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800708
709 /*
710 * Show what we know for posterity
711 */
712 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
713 if (c16) {
714 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
715 vendor[i] = *c16++;
716 vendor[i] = '\0';
717 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800718 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800719 early_iounmap(tmp, 2);
720
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800721 pr_info("EFI v%u.%.02u by %s\n",
722 efi.systab->hdr.revision >> 16,
723 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800724
Matt Fleming83e68182012-11-14 09:42:35 +0000725 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800726 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000727
728 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800729
Olof Johansson1adbfa32012-02-12 13:24:29 -0800730 /*
731 * Note: We currently don't support runtime services on an EFI
732 * that doesn't match the kernel 32/64-bit mode.
733 */
734
Olof Johansson5189c2a2012-10-24 10:00:44 -0700735 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800736 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000737 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000738 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000739 return;
740 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
Olof Johansson140bf272012-02-12 13:24:28 -0800741 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800742
Matt Fleming83e68182012-11-14 09:42:35 +0000743 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800744 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000745
746 set_bit(EFI_MEMMAP, &x86_efi_facility);
747
Linus Torvalds11520e52012-12-15 15:15:24 -0800748#ifdef CONFIG_X86_32
Olof Johansson5189c2a2012-10-24 10:00:44 -0700749 if (efi_is_native()) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800750 x86_platform.get_wallclock = efi_get_time;
751 x86_platform.set_wallclock = efi_set_rtc_mmss;
752 }
Linus Torvalds11520e52012-12-15 15:15:24 -0800753#endif
Feng Tang7bd867d2009-09-10 10:48:56 +0800754
Huang, Ying5b836832008-01-30 13:31:19 +0100755#if EFI_DEBUG
756 print_efi_memmap();
757#endif
758}
759
Josh Triplett2223af32012-09-28 17:57:05 -0700760void __init efi_late_init(void)
761{
762 efi_bgrt_init();
763}
764
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400765void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
766{
767 u64 addr, npages;
768
769 addr = md->virt_addr;
770 npages = md->num_pages;
771
772 memrange_efi_to_native(&addr, &npages);
773
774 if (executable)
775 set_memory_x(addr, npages);
776 else
777 set_memory_nx(addr, npages);
778}
779
Huang, Yinga2172e22008-01-30 13:33:55 +0100780static void __init runtime_code_page_mkexec(void)
781{
782 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100783 void *p;
784
Huang, Yinga2172e22008-01-30 13:33:55 +0100785 /* Make EFI runtime service code area executable */
786 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
787 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100788
789 if (md->type != EFI_RUNTIME_SERVICES_CODE)
790 continue;
791
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400792 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100793 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100794}
Huang, Yinga2172e22008-01-30 13:33:55 +0100795
Huang, Ying5b836832008-01-30 13:31:19 +0100796/*
Josh Triplett7bc90e02012-09-28 17:56:08 -0700797 * We can't ioremap data in EFI boot services RAM, because we've already mapped
798 * it as RAM. So, look it up in the existing EFI memory map instead. Only
799 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
800 */
801void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
802{
803 void *p;
804 if (WARN_ON(!memmap.map))
805 return NULL;
806 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
807 efi_memory_desc_t *md = p;
808 u64 size = md->num_pages << EFI_PAGE_SHIFT;
809 u64 end = md->phys_addr + size;
810 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
811 md->type != EFI_BOOT_SERVICES_CODE &&
812 md->type != EFI_BOOT_SERVICES_DATA)
813 continue;
814 if (!md->virt_addr)
815 continue;
816 if (phys_addr >= md->phys_addr && phys_addr < end) {
817 phys_addr += md->virt_addr - md->phys_addr;
818 return (__force void __iomem *)(unsigned long)phys_addr;
819 }
820 }
821 return NULL;
822}
823
Matt Fleming3e8fa262012-10-19 13:25:46 +0100824void efi_memory_uc(u64 addr, unsigned long size)
825{
826 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
827 u64 npages;
828
829 npages = round_up(size, page_shift) / page_shift;
830 memrange_efi_to_native(&addr, &npages);
831 set_memory_uc(addr, npages);
832}
833
Josh Triplett7bc90e02012-09-28 17:56:08 -0700834/*
Huang, Ying5b836832008-01-30 13:31:19 +0100835 * This function will switch the EFI runtime services to virtual mode.
836 * Essentially, look through the EFI memmap and map every region that
837 * has the runtime attribute bit set in its memory descriptor and update
838 * that memory descriptor with the virtual address obtained from ioremap().
839 * This enables the runtime services to be called without having to
840 * thunk back into physical mode for every invocation.
841 */
842void __init efi_enter_virtual_mode(void)
843{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400844 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100845 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100846 unsigned long size;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100847 u64 end, systab, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400848 void *p, *va, *new_memmap = NULL;
849 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100850
851 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400852
Olof Johansson1adbfa32012-02-12 13:24:29 -0800853 /*
854 * We don't do virtual mode, since we don't do runtime services, on
855 * non-native EFI
856 */
857
Olof Johansson5189c2a2012-10-24 10:00:44 -0700858 if (!efi_is_native()) {
Josh Triplett78510792012-09-28 17:55:44 -0700859 efi_unmap_memmap();
860 return;
861 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800862
Matthew Garrett202f9d02011-05-05 15:19:44 -0400863 /* Merge contiguous regions of the same type and attribute */
864 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
865 u64 prev_size;
866 md = p;
867
868 if (!prev_md) {
869 prev_md = md;
870 continue;
871 }
872
873 if (prev_md->type != md->type ||
874 prev_md->attribute != md->attribute) {
875 prev_md = md;
876 continue;
877 }
878
879 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
880
881 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
882 prev_md->num_pages += md->num_pages;
883 md->type = EFI_RESERVED_TYPE;
884 md->attribute = 0;
885 continue;
886 }
887 prev_md = md;
888 }
889
Huang, Ying5b836832008-01-30 13:31:19 +0100890 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
891 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -0400892 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
893 md->type != EFI_BOOT_SERVICES_CODE &&
894 md->type != EFI_BOOT_SERVICES_DATA)
Huang, Ying5b836832008-01-30 13:31:19 +0100895 continue;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100896
897 size = md->num_pages << EFI_PAGE_SHIFT;
898 end = md->phys_addr + size;
899
Huang Yingdd39ecf2009-03-04 10:58:33 +0800900 end_pfn = PFN_UP(end);
901 if (end_pfn <= max_low_pfn_mapped
902 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
Matt Fleming3e8fa262012-10-19 13:25:46 +0100903 && end_pfn <= max_pfn_mapped)) {
Huang, Ying1c083eb2008-02-04 16:48:06 +0100904 va = __va(md->phys_addr);
Matt Fleming3e8fa262012-10-19 13:25:46 +0100905
906 if (!(md->attribute & EFI_MEMORY_WB))
907 efi_memory_uc((u64)(unsigned long)va, size);
908 } else
909 va = efi_ioremap(md->phys_addr, size,
910 md->type, md->attribute);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100911
Huang, Ying1c083eb2008-02-04 16:48:06 +0100912 md->virt_addr = (u64) (unsigned long) va;
913
914 if (!va) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800915 pr_err("ioremap of 0x%llX failed!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100916 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100917 continue;
918 }
919
920 systab = (u64) (unsigned long) efi_phys.systab;
921 if (md->phys_addr <= systab && systab < end) {
922 systab += md->virt_addr - md->phys_addr;
923 efi.systab = (efi_system_table_t *) (unsigned long) systab;
924 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400925 new_memmap = krealloc(new_memmap,
926 (count + 1) * memmap.desc_size,
927 GFP_KERNEL);
928 memcpy(new_memmap + (count * memmap.desc_size), md,
929 memmap.desc_size);
930 count++;
Huang, Ying5b836832008-01-30 13:31:19 +0100931 }
932
933 BUG_ON(!efi.systab);
934
935 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400936 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +0100937 memmap.desc_size,
938 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400939 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +0100940
941 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800942 pr_alert("Unable to switch EFI into virtual mode "
943 "(status=%lx)!\n", status);
Huang, Ying5b836832008-01-30 13:31:19 +0100944 panic("EFI call to SetVirtualAddressMap() failed!");
945 }
946
947 /*
948 * Now that EFI is in virtual mode, update the function
949 * pointers in the runtime service table to the new virtual addresses.
950 *
951 * Call EFI services through wrapper functions.
952 */
Matt Fleming712ba9e2013-01-25 10:07:25 +0000953 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +0100954 efi.get_time = virt_efi_get_time;
955 efi.set_time = virt_efi_set_time;
956 efi.get_wakeup_time = virt_efi_get_wakeup_time;
957 efi.set_wakeup_time = virt_efi_set_wakeup_time;
958 efi.get_variable = virt_efi_get_variable;
959 efi.get_next_variable = virt_efi_get_next_variable;
960 efi.set_variable = virt_efi_set_variable;
961 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
962 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400963 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -0400964 efi.query_variable_info = virt_efi_query_variable_info;
965 efi.update_capsule = virt_efi_update_capsule;
966 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Huang, Ying4de0d4a2008-02-13 17:22:41 +0800967 if (__supported_pte_mask & _PAGE_NX)
968 runtime_code_page_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -0800969
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400970 kfree(new_memmap);
Huang, Ying5b836832008-01-30 13:31:19 +0100971}
972
973/*
974 * Convenience functions to obtain memory types and attributes
975 */
976u32 efi_mem_type(unsigned long phys_addr)
977{
978 efi_memory_desc_t *md;
979 void *p;
980
Matt Fleming83e68182012-11-14 09:42:35 +0000981 if (!efi_enabled(EFI_MEMMAP))
982 return 0;
983
Huang, Ying5b836832008-01-30 13:31:19 +0100984 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
985 md = p;
986 if ((md->phys_addr <= phys_addr) &&
987 (phys_addr < (md->phys_addr +
988 (md->num_pages << EFI_PAGE_SHIFT))))
989 return md->type;
990 }
991 return 0;
992}
993
994u64 efi_mem_attributes(unsigned long phys_addr)
995{
996 efi_memory_desc_t *md;
997 void *p;
998
999 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1000 md = p;
1001 if ((md->phys_addr <= phys_addr) &&
1002 (phys_addr < (md->phys_addr +
1003 (md->num_pages << EFI_PAGE_SHIFT))))
1004 return md->attribute;
1005 }
1006 return 0;
1007}