blob: 2dc29f51e75aadbfaa29a0c70be54ce02b291ea9 [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>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040034#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010035#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070036#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010037#include <linux/spinlock.h>
38#include <linux/uaccess.h>
39#include <linux/time.h>
40#include <linux/io.h>
41#include <linux/reboot.h>
42#include <linux/bcd.h>
43
44#include <asm/setup.h>
45#include <asm/efi.h>
46#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010047#include <asm/cacheflush.h>
48#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080049#include <asm/x86_init.h>
Huang, Ying5b836832008-01-30 13:31:19 +010050
51#define EFI_DEBUG 1
Huang, Ying5b836832008-01-30 13:31:19 +010052
53int efi_enabled;
54EXPORT_SYMBOL(efi_enabled);
55
Jan Beulichd80603c2011-07-05 12:22:18 +010056struct efi __read_mostly efi = {
57 .mps = EFI_INVALID_TABLE_ADDR,
58 .acpi = EFI_INVALID_TABLE_ADDR,
59 .acpi20 = EFI_INVALID_TABLE_ADDR,
60 .smbios = EFI_INVALID_TABLE_ADDR,
61 .sal_systab = EFI_INVALID_TABLE_ADDR,
62 .boot_info = EFI_INVALID_TABLE_ADDR,
63 .hcdp = EFI_INVALID_TABLE_ADDR,
64 .uga = EFI_INVALID_TABLE_ADDR,
65 .uv_systab = EFI_INVALID_TABLE_ADDR,
66};
Huang, Ying5b836832008-01-30 13:31:19 +010067EXPORT_SYMBOL(efi);
68
69struct efi_memory_map memmap;
70
Olof Johansson1adbfa32012-02-12 13:24:29 -080071bool efi_64bit;
72static bool efi_native;
73
Harvey Harrisonecaea422008-02-13 13:26:13 -080074static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010075static efi_system_table_t efi_systab __initdata;
76
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010077static int __init setup_noefi(char *arg)
78{
79 efi_enabled = 0;
80 return 0;
81}
82early_param("noefi", setup_noefi);
83
Paul Jackson200001e2008-06-25 05:44:46 -070084int add_efi_memmap;
85EXPORT_SYMBOL(add_efi_memmap);
86
87static int __init setup_add_efi_memmap(char *arg)
88{
89 add_efi_memmap = 1;
90 return 0;
91}
92early_param("add_efi_memmap", setup_add_efi_memmap);
93
94
Huang, Ying5b836832008-01-30 13:31:19 +010095static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
96{
Jan Beulichef68c8f2011-07-19 11:53:07 +010097 unsigned long flags;
98 efi_status_t status;
99
100 spin_lock_irqsave(&rtc_lock, flags);
101 status = efi_call_virt2(get_time, tm, tc);
102 spin_unlock_irqrestore(&rtc_lock, flags);
103 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100104}
105
106static efi_status_t virt_efi_set_time(efi_time_t *tm)
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_virt1(set_time, tm);
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_get_wakeup_time(efi_bool_t *enabled,
118 efi_bool_t *pending,
119 efi_time_t *tm)
120{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100121 unsigned long flags;
122 efi_status_t status;
123
124 spin_lock_irqsave(&rtc_lock, flags);
125 status = efi_call_virt3(get_wakeup_time,
126 enabled, pending, tm);
127 spin_unlock_irqrestore(&rtc_lock, flags);
128 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100129}
130
131static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, 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_virt2(set_wakeup_time,
138 enabled, 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_get_variable(efi_char16_t *name,
144 efi_guid_t *vendor,
145 u32 *attr,
146 unsigned long *data_size,
147 void *data)
148{
149 return efi_call_virt5(get_variable,
150 name, vendor, attr,
151 data_size, data);
152}
153
154static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
155 efi_char16_t *name,
156 efi_guid_t *vendor)
157{
158 return efi_call_virt3(get_next_variable,
159 name_size, name, vendor);
160}
161
162static efi_status_t virt_efi_set_variable(efi_char16_t *name,
163 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400164 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100165 unsigned long data_size,
166 void *data)
167{
168 return efi_call_virt5(set_variable,
169 name, vendor, attr,
170 data_size, data);
171}
172
Matthew Garrett3b370232011-06-06 15:36:25 -0400173static efi_status_t virt_efi_query_variable_info(u32 attr,
174 u64 *storage_space,
175 u64 *remaining_space,
176 u64 *max_variable_size)
177{
178 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
179 return EFI_UNSUPPORTED;
180
181 return efi_call_virt4(query_variable_info, attr, storage_space,
182 remaining_space, max_variable_size);
183}
184
Huang, Ying5b836832008-01-30 13:31:19 +0100185static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
186{
187 return efi_call_virt1(get_next_high_mono_count, count);
188}
189
190static void virt_efi_reset_system(int reset_type,
191 efi_status_t status,
192 unsigned long data_size,
193 efi_char16_t *data)
194{
195 efi_call_virt4(reset_system, reset_type, status,
196 data_size, data);
197}
198
Matthew Garrett3b370232011-06-06 15:36:25 -0400199static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
200 unsigned long count,
201 unsigned long sg_list)
202{
203 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
204 return EFI_UNSUPPORTED;
205
206 return efi_call_virt3(update_capsule, capsules, count, sg_list);
207}
208
209static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
210 unsigned long count,
211 u64 *max_size,
212 int *reset_type)
213{
214 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
215 return EFI_UNSUPPORTED;
216
217 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
218 reset_type);
219}
220
Huang, Ying5b836832008-01-30 13:31:19 +0100221static efi_status_t __init phys_efi_set_virtual_address_map(
222 unsigned long memory_map_size,
223 unsigned long descriptor_size,
224 u32 descriptor_version,
225 efi_memory_desc_t *virtual_map)
226{
227 efi_status_t status;
228
229 efi_call_phys_prelog();
230 status = efi_call_phys4(efi_phys.set_virtual_address_map,
231 memory_map_size, descriptor_size,
232 descriptor_version, virtual_map);
233 efi_call_phys_epilog();
234 return status;
235}
236
Jan Beulichbacef662012-05-25 16:20:31 +0100237static int efi_set_rtc_mmss(unsigned long nowtime)
Huang, Ying5b836832008-01-30 13:31:19 +0100238{
239 int real_seconds, real_minutes;
240 efi_status_t status;
241 efi_time_t eft;
242 efi_time_cap_t cap;
243
244 status = efi.get_time(&eft, &cap);
245 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800246 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100247 return -1;
248 }
249
250 real_seconds = nowtime % 60;
251 real_minutes = nowtime / 60;
252 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
253 real_minutes += 30;
254 real_minutes %= 60;
255 eft.minute = real_minutes;
256 eft.second = real_seconds;
257
258 status = efi.set_time(&eft);
259 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800260 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100261 return -1;
262 }
263 return 0;
264}
265
Jan Beulichbacef662012-05-25 16:20:31 +0100266static unsigned long efi_get_time(void)
Huang, Ying5b836832008-01-30 13:31:19 +0100267{
268 efi_status_t status;
269 efi_time_t eft;
270 efi_time_cap_t cap;
271
272 status = efi.get_time(&eft, &cap);
273 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800274 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100275
276 return mktime(eft.year, eft.month, eft.day, eft.hour,
277 eft.minute, eft.second);
278}
279
Paul Jackson69c91892008-05-14 08:15:58 -0700280/*
281 * Tell the kernel about the EFI memory map. This might include
282 * more than the max 128 entries that can fit in the e820 legacy
283 * (zeropage) memory map.
284 */
285
Paul Jackson200001e2008-06-25 05:44:46 -0700286static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700287{
288 void *p;
289
290 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
291 efi_memory_desc_t *md = p;
292 unsigned long long start = md->phys_addr;
293 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
294 int e820_type;
295
Cliff Wickmane2a71472009-06-16 16:43:40 -0500296 switch (md->type) {
297 case EFI_LOADER_CODE:
298 case EFI_LOADER_DATA:
299 case EFI_BOOT_SERVICES_CODE:
300 case EFI_BOOT_SERVICES_DATA:
301 case EFI_CONVENTIONAL_MEMORY:
302 if (md->attribute & EFI_MEMORY_WB)
303 e820_type = E820_RAM;
304 else
305 e820_type = E820_RESERVED;
306 break;
307 case EFI_ACPI_RECLAIM_MEMORY:
308 e820_type = E820_ACPI;
309 break;
310 case EFI_ACPI_MEMORY_NVS:
311 e820_type = E820_NVS;
312 break;
313 case EFI_UNUSABLE_MEMORY:
314 e820_type = E820_UNUSABLE;
315 break;
316 default:
317 /*
318 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
319 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
320 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
321 */
Paul Jackson69c91892008-05-14 08:15:58 -0700322 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500323 break;
324 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700325 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700326 }
327 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
328}
329
Olof Johansson1adbfa32012-02-12 13:24:29 -0800330int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800331{
332 unsigned long pmap;
333
Paul Jackson05486fa2008-06-22 07:22:02 -0700334#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800335 /* Can't handle data above 4GB at this time */
336 if (boot_params.efi_info.efi_memmap_hi) {
337 pr_err("Memory map is above 4GB, disabling EFI.\n");
338 return -EINVAL;
339 }
Huang, Yingecacf092008-06-02 14:26:21 +0800340 pmap = boot_params.efi_info.efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700341#else
342 pmap = (boot_params.efi_info.efi_memmap |
343 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
Huang, Yingecacf092008-06-02 14:26:21 +0800344#endif
345 memmap.phys_map = (void *)pmap;
346 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
347 boot_params.efi_info.efi_memdesc_size;
348 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
349 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
Tejun Heo24aa0782011-07-12 11:16:06 +0200350 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800351
352 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800353}
354
Huang, Ying5b836832008-01-30 13:31:19 +0100355#if EFI_DEBUG
356static void __init print_efi_memmap(void)
357{
358 efi_memory_desc_t *md;
359 void *p;
360 int i;
361
362 for (p = memmap.map, i = 0;
363 p < memmap.map_end;
364 p += memmap.desc_size, i++) {
365 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800366 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100367 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
368 i, md->type, md->attribute, md->phys_addr,
369 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
370 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
371 }
372}
373#endif /* EFI_DEBUG */
374
Matthew Garrett916f6762011-05-25 09:53:13 -0400375void __init efi_reserve_boot_services(void)
376{
377 void *p;
378
379 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
380 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200381 u64 start = md->phys_addr;
382 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400383
384 if (md->type != EFI_BOOT_SERVICES_CODE &&
385 md->type != EFI_BOOT_SERVICES_DATA)
386 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200387 /* Only reserve where possible:
388 * - Not within any already allocated areas
389 * - Not over any memory area (really needed, if above?)
390 * - Not within any part of the kernel
391 * - Not the bios reserved area
392 */
393 if ((start+size >= virt_to_phys(_text)
394 && start <= virt_to_phys(_end)) ||
395 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200396 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200397 /* Could not reserve, skip it */
398 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800399 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200400 "[0x%010llx-0x%010llx]\n",
401 start, start+size-1);
402 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200403 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400404 }
405}
406
407static void __init efi_free_boot_services(void)
408{
409 void *p;
410
411 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
412 efi_memory_desc_t *md = p;
413 unsigned long long start = md->phys_addr;
414 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
415
416 if (md->type != EFI_BOOT_SERVICES_CODE &&
417 md->type != EFI_BOOT_SERVICES_DATA)
418 continue;
419
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200420 /* Could not reserve boot area */
421 if (!size)
422 continue;
423
Matthew Garrett916f6762011-05-25 09:53:13 -0400424 free_bootmem_late(start, size);
425 }
426}
427
Olof Johansson140bf272012-02-12 13:24:28 -0800428static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100429{
Olof Johansson1adbfa32012-02-12 13:24:29 -0800430 if (efi_64bit) {
431 efi_system_table_64_t *systab64;
432 u64 tmp = 0;
433
434 systab64 = early_ioremap((unsigned long)phys,
435 sizeof(*systab64));
436 if (systab64 == NULL) {
437 pr_err("Couldn't map the system table!\n");
438 return -ENOMEM;
439 }
440
441 efi_systab.hdr = systab64->hdr;
442 efi_systab.fw_vendor = systab64->fw_vendor;
443 tmp |= systab64->fw_vendor;
444 efi_systab.fw_revision = systab64->fw_revision;
445 efi_systab.con_in_handle = systab64->con_in_handle;
446 tmp |= systab64->con_in_handle;
447 efi_systab.con_in = systab64->con_in;
448 tmp |= systab64->con_in;
449 efi_systab.con_out_handle = systab64->con_out_handle;
450 tmp |= systab64->con_out_handle;
451 efi_systab.con_out = systab64->con_out;
452 tmp |= systab64->con_out;
453 efi_systab.stderr_handle = systab64->stderr_handle;
454 tmp |= systab64->stderr_handle;
455 efi_systab.stderr = systab64->stderr;
456 tmp |= systab64->stderr;
457 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
458 tmp |= systab64->runtime;
459 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
460 tmp |= systab64->boottime;
461 efi_systab.nr_tables = systab64->nr_tables;
462 efi_systab.tables = systab64->tables;
463 tmp |= systab64->tables;
464
465 early_iounmap(systab64, sizeof(*systab64));
466#ifdef CONFIG_X86_32
467 if (tmp >> 32) {
468 pr_err("EFI data located above 4GB, disabling EFI.\n");
469 return -EINVAL;
470 }
471#endif
472 } else {
473 efi_system_table_32_t *systab32;
474
475 systab32 = early_ioremap((unsigned long)phys,
476 sizeof(*systab32));
477 if (systab32 == NULL) {
478 pr_err("Couldn't map the system table!\n");
479 return -ENOMEM;
480 }
481
482 efi_systab.hdr = systab32->hdr;
483 efi_systab.fw_vendor = systab32->fw_vendor;
484 efi_systab.fw_revision = systab32->fw_revision;
485 efi_systab.con_in_handle = systab32->con_in_handle;
486 efi_systab.con_in = systab32->con_in;
487 efi_systab.con_out_handle = systab32->con_out_handle;
488 efi_systab.con_out = systab32->con_out;
489 efi_systab.stderr_handle = systab32->stderr_handle;
490 efi_systab.stderr = systab32->stderr;
491 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
492 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
493 efi_systab.nr_tables = systab32->nr_tables;
494 efi_systab.tables = systab32->tables;
495
496 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800497 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800498
Huang, Ying5b836832008-01-30 13:31:19 +0100499 efi.systab = &efi_systab;
500
501 /*
502 * Verify the EFI Table
503 */
Olof Johansson140bf272012-02-12 13:24:28 -0800504 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800505 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800506 return -EINVAL;
507 }
Huang, Ying5b836832008-01-30 13:31:19 +0100508 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800509 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100510 "%d.%02d, expected 1.00 or greater!\n",
511 efi.systab->hdr.revision >> 16,
512 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800513
514 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800515}
Huang, Ying5b836832008-01-30 13:31:19 +0100516
Olof Johansson140bf272012-02-12 13:24:28 -0800517static int __init efi_config_init(u64 tables, int nr_tables)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800518{
Olof Johansson1adbfa32012-02-12 13:24:29 -0800519 void *config_tables, *tablep;
520 int i, sz;
521
522 if (efi_64bit)
523 sz = sizeof(efi_config_table_64_t);
524 else
525 sz = sizeof(efi_config_table_32_t);
Huang, Ying5b836832008-01-30 13:31:19 +0100526
527 /*
528 * Let's see what config tables the firmware passed to us.
529 */
Olof Johansson1adbfa32012-02-12 13:24:29 -0800530 config_tables = early_ioremap(tables, nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800531 if (config_tables == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800532 pr_err("Could not map Configuration table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800533 return -ENOMEM;
534 }
Huang, Ying5b836832008-01-30 13:31:19 +0100535
Olof Johansson1adbfa32012-02-12 13:24:29 -0800536 tablep = config_tables;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800537 pr_info("");
Huang, Ying5b836832008-01-30 13:31:19 +0100538 for (i = 0; i < efi.systab->nr_tables; i++) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800539 efi_guid_t guid;
540 unsigned long table;
Olof Johanssona6a46f42012-02-12 13:24:27 -0800541
Olof Johansson1adbfa32012-02-12 13:24:29 -0800542 if (efi_64bit) {
543 u64 table64;
544 guid = ((efi_config_table_64_t *)tablep)->guid;
545 table64 = ((efi_config_table_64_t *)tablep)->table;
546 table = table64;
547#ifdef CONFIG_X86_32
548 if (table64 >> 32) {
549 pr_cont("\n");
550 pr_err("Table located above 4GB, disabling EFI.\n");
551 early_iounmap(config_tables,
552 efi.systab->nr_tables * sz);
553 return -EINVAL;
554 }
555#endif
556 } else {
557 guid = ((efi_config_table_32_t *)tablep)->guid;
558 table = ((efi_config_table_32_t *)tablep)->table;
559 }
Olof Johanssona6a46f42012-02-12 13:24:27 -0800560 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
561 efi.mps = table;
562 pr_cont(" MPS=0x%lx ", table);
563 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
564 efi.acpi20 = table;
565 pr_cont(" ACPI 2.0=0x%lx ", table);
566 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
567 efi.acpi = table;
568 pr_cont(" ACPI=0x%lx ", table);
569 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
570 efi.smbios = table;
571 pr_cont(" SMBIOS=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100572#ifdef CONFIG_X86_UV
Olof Johanssona6a46f42012-02-12 13:24:27 -0800573 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
574 efi.uv_systab = table;
575 pr_cont(" UVsystab=0x%lx ", table);
Nick Piggin03b48632009-01-20 04:36:04 +0100576#endif
Olof Johanssona6a46f42012-02-12 13:24:27 -0800577 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
578 efi.hcdp = table;
579 pr_cont(" HCDP=0x%lx ", table);
580 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
581 efi.uga = table;
582 pr_cont(" UGA=0x%lx ", table);
Huang, Ying5b836832008-01-30 13:31:19 +0100583 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800584 tablep += sz;
Huang, Ying5b836832008-01-30 13:31:19 +0100585 }
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800586 pr_cont("\n");
Olof Johanssona6a46f42012-02-12 13:24:27 -0800587 early_iounmap(config_tables, efi.systab->nr_tables * sz);
Olof Johansson140bf272012-02-12 13:24:28 -0800588 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800589}
590
Olof Johansson140bf272012-02-12 13:24:28 -0800591static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800592{
593 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100594
595 /*
596 * Check out the runtime services table. We need to map
597 * the runtime services table so that we can grab the physical
598 * address of several of the EFI runtime functions, needed to
599 * set the firmware into virtual mode.
600 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100601 runtime = early_ioremap((unsigned long)efi.systab->runtime,
602 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800603 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800604 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800605 return -ENOMEM;
606 }
607 /*
608 * We will only need *early* access to the following
Jan Beulichbacef662012-05-25 16:20:31 +0100609 * EFI runtime service before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800610 * is invoked.
611 */
Olof Johansson140bf272012-02-12 13:24:28 -0800612 efi_phys.set_virtual_address_map =
613 (efi_set_virtual_address_map_t *)
614 runtime->set_virtual_address_map;
Jan Beulichbacef662012-05-25 16:20:31 +0100615
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100616 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800617
618 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800619}
Huang, Ying5b836832008-01-30 13:31:19 +0100620
Olof Johansson140bf272012-02-12 13:24:28 -0800621static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800622{
Huang, Ying5b836832008-01-30 13:31:19 +0100623 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100624 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
625 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800626 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800627 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800628 return -ENOMEM;
629 }
Huang, Ying5b836832008-01-30 13:31:19 +0100630 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500631
Paul Jackson200001e2008-06-25 05:44:46 -0700632 if (add_efi_memmap)
633 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800634
635 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800636}
637
638void __init efi_init(void)
639{
640 efi_char16_t *c16;
641 char vendor[100] = "unknown";
642 int i = 0;
643 void *tmp;
644
645#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800646 if (boot_params.efi_info.efi_systab_hi ||
647 boot_params.efi_info.efi_memmap_hi) {
648 pr_info("Table located above 4GB, disabling EFI.\n");
649 efi_enabled = 0;
650 return;
651 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800652 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
Olof Johansson1adbfa32012-02-12 13:24:29 -0800653 efi_native = !efi_64bit;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800654#else
655 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800656 (boot_params.efi_info.efi_systab |
657 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
658 efi_native = efi_64bit;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800659#endif
660
Olof Johansson140bf272012-02-12 13:24:28 -0800661 if (efi_systab_init(efi_phys.systab)) {
662 efi_enabled = 0;
663 return;
664 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800665
666 /*
667 * Show what we know for posterity
668 */
669 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
670 if (c16) {
671 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
672 vendor[i] = *c16++;
673 vendor[i] = '\0';
674 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800675 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800676 early_iounmap(tmp, 2);
677
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800678 pr_info("EFI v%u.%.02u by %s\n",
679 efi.systab->hdr.revision >> 16,
680 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800681
Olof Johansson140bf272012-02-12 13:24:28 -0800682 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) {
683 efi_enabled = 0;
684 return;
685 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800686
Olof Johansson1adbfa32012-02-12 13:24:29 -0800687 /*
688 * Note: We currently don't support runtime services on an EFI
689 * that doesn't match the kernel 32/64-bit mode.
690 */
691
692 if (!efi_native)
693 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
694 else if (efi_runtime_init()) {
Olof Johansson140bf272012-02-12 13:24:28 -0800695 efi_enabled = 0;
696 return;
697 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800698
Olof Johansson140bf272012-02-12 13:24:28 -0800699 if (efi_memmap_init()) {
700 efi_enabled = 0;
701 return;
702 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800703 if (efi_native) {
704 x86_platform.get_wallclock = efi_get_time;
705 x86_platform.set_wallclock = efi_set_rtc_mmss;
706 }
Feng Tang7bd867d2009-09-10 10:48:56 +0800707
Huang, Ying5b836832008-01-30 13:31:19 +0100708#if EFI_DEBUG
709 print_efi_memmap();
710#endif
711}
712
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400713void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
714{
715 u64 addr, npages;
716
717 addr = md->virt_addr;
718 npages = md->num_pages;
719
720 memrange_efi_to_native(&addr, &npages);
721
722 if (executable)
723 set_memory_x(addr, npages);
724 else
725 set_memory_nx(addr, npages);
726}
727
Huang, Yinga2172e22008-01-30 13:33:55 +0100728static void __init runtime_code_page_mkexec(void)
729{
730 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100731 void *p;
732
Huang, Yinga2172e22008-01-30 13:33:55 +0100733 /* Make EFI runtime service code area executable */
734 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
735 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100736
737 if (md->type != EFI_RUNTIME_SERVICES_CODE)
738 continue;
739
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400740 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100741 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100742}
Huang, Yinga2172e22008-01-30 13:33:55 +0100743
Huang, Ying5b836832008-01-30 13:31:19 +0100744/*
745 * This function will switch the EFI runtime services to virtual mode.
746 * Essentially, look through the EFI memmap and map every region that
747 * has the runtime attribute bit set in its memory descriptor and update
748 * that memory descriptor with the virtual address obtained from ioremap().
749 * This enables the runtime services to be called without having to
750 * thunk back into physical mode for every invocation.
751 */
752void __init efi_enter_virtual_mode(void)
753{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400754 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100755 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100756 unsigned long size;
Huang Yingdd39ecf2009-03-04 10:58:33 +0800757 u64 end, systab, addr, npages, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400758 void *p, *va, *new_memmap = NULL;
759 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100760
761 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400762
Olof Johansson1adbfa32012-02-12 13:24:29 -0800763 /*
764 * We don't do virtual mode, since we don't do runtime services, on
765 * non-native EFI
766 */
767
768 if (!efi_native)
769 goto out;
770
Matthew Garrett202f9d02011-05-05 15:19:44 -0400771 /* Merge contiguous regions of the same type and attribute */
772 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
773 u64 prev_size;
774 md = p;
775
776 if (!prev_md) {
777 prev_md = md;
778 continue;
779 }
780
781 if (prev_md->type != md->type ||
782 prev_md->attribute != md->attribute) {
783 prev_md = md;
784 continue;
785 }
786
787 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
788
789 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
790 prev_md->num_pages += md->num_pages;
791 md->type = EFI_RESERVED_TYPE;
792 md->attribute = 0;
793 continue;
794 }
795 prev_md = md;
796 }
797
Huang, Ying5b836832008-01-30 13:31:19 +0100798 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
799 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -0400800 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
801 md->type != EFI_BOOT_SERVICES_CODE &&
802 md->type != EFI_BOOT_SERVICES_DATA)
Huang, Ying5b836832008-01-30 13:31:19 +0100803 continue;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100804
805 size = md->num_pages << EFI_PAGE_SHIFT;
806 end = md->phys_addr + size;
807
Huang Yingdd39ecf2009-03-04 10:58:33 +0800808 end_pfn = PFN_UP(end);
809 if (end_pfn <= max_low_pfn_mapped
810 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
811 && end_pfn <= max_pfn_mapped))
Huang, Ying1c083eb2008-02-04 16:48:06 +0100812 va = __va(md->phys_addr);
Huang, Ying5b836832008-01-30 13:31:19 +0100813 else
Paul Mackerras6a7bbd52009-08-03 22:38:10 +1000814 va = efi_ioremap(md->phys_addr, size, md->type);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100815
Huang, Ying1c083eb2008-02-04 16:48:06 +0100816 md->virt_addr = (u64) (unsigned long) va;
817
818 if (!va) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800819 pr_err("ioremap of 0x%llX failed!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100820 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100821 continue;
822 }
823
Huang, Ying4a3575f2008-02-25 15:18:37 +0800824 if (!(md->attribute & EFI_MEMORY_WB)) {
825 addr = md->virt_addr;
826 npages = md->num_pages;
827 memrange_efi_to_native(&addr, &npages);
828 set_memory_uc(addr, npages);
829 }
Thomas Gleixnere85f2052008-02-12 19:46:48 +0100830
Huang, Ying1c083eb2008-02-04 16:48:06 +0100831 systab = (u64) (unsigned long) efi_phys.systab;
832 if (md->phys_addr <= systab && systab < end) {
833 systab += md->virt_addr - md->phys_addr;
834 efi.systab = (efi_system_table_t *) (unsigned long) systab;
835 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400836 new_memmap = krealloc(new_memmap,
837 (count + 1) * memmap.desc_size,
838 GFP_KERNEL);
839 memcpy(new_memmap + (count * memmap.desc_size), md,
840 memmap.desc_size);
841 count++;
Huang, Ying5b836832008-01-30 13:31:19 +0100842 }
843
844 BUG_ON(!efi.systab);
845
846 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400847 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +0100848 memmap.desc_size,
849 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400850 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +0100851
852 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800853 pr_alert("Unable to switch EFI into virtual mode "
854 "(status=%lx)!\n", status);
Huang, Ying5b836832008-01-30 13:31:19 +0100855 panic("EFI call to SetVirtualAddressMap() failed!");
856 }
857
858 /*
Matthew Garrett916f6762011-05-25 09:53:13 -0400859 * Thankfully, it does seem that no runtime services other than
860 * SetVirtualAddressMap() will touch boot services code, so we can
861 * get rid of it all at this point
862 */
863 efi_free_boot_services();
864
865 /*
Huang, Ying5b836832008-01-30 13:31:19 +0100866 * Now that EFI is in virtual mode, update the function
867 * pointers in the runtime service table to the new virtual addresses.
868 *
869 * Call EFI services through wrapper functions.
870 */
871 efi.get_time = virt_efi_get_time;
872 efi.set_time = virt_efi_set_time;
873 efi.get_wakeup_time = virt_efi_get_wakeup_time;
874 efi.set_wakeup_time = virt_efi_set_wakeup_time;
875 efi.get_variable = virt_efi_get_variable;
876 efi.get_next_variable = virt_efi_get_next_variable;
877 efi.set_variable = virt_efi_set_variable;
878 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
879 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400880 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -0400881 efi.query_variable_info = virt_efi_query_variable_info;
882 efi.update_capsule = virt_efi_update_capsule;
883 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Huang, Ying4de0d4a2008-02-13 17:22:41 +0800884 if (__supported_pte_mask & _PAGE_NX)
885 runtime_code_page_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -0800886
887out:
Huang, Yinga3828062008-01-30 13:34:10 +0100888 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
889 memmap.map = NULL;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400890 kfree(new_memmap);
Huang, Ying5b836832008-01-30 13:31:19 +0100891}
892
893/*
894 * Convenience functions to obtain memory types and attributes
895 */
896u32 efi_mem_type(unsigned long phys_addr)
897{
898 efi_memory_desc_t *md;
899 void *p;
900
901 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
902 md = p;
903 if ((md->phys_addr <= phys_addr) &&
904 (phys_addr < (md->phys_addr +
905 (md->num_pages << EFI_PAGE_SHIFT))))
906 return md->type;
907 }
908 return 0;
909}
910
911u64 efi_mem_attributes(unsigned long phys_addr)
912{
913 efi_memory_desc_t *md;
914 void *p;
915
916 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
917 md = p;
918 if ((md->phys_addr <= phys_addr) &&
919 (phys_addr < (md->phys_addr +
920 (md->num_pages << EFI_PAGE_SHIFT))))
921 return md->attribute;
922 }
923 return 0;
924}