blob: cfc588ce115563cc8f6e94487a05796939190aff [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
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/efi.h>
32#include <linux/bootmem.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070033#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010034#include <linux/spinlock.h>
35#include <linux/uaccess.h>
36#include <linux/time.h>
37#include <linux/io.h>
38#include <linux/reboot.h>
39#include <linux/bcd.h>
40
41#include <asm/setup.h>
42#include <asm/efi.h>
43#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010044#include <asm/cacheflush.h>
45#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080046#include <asm/x86_init.h>
Huang, Ying5b836832008-01-30 13:31:19 +010047
48#define EFI_DEBUG 1
49#define PFX "EFI: "
50
51int efi_enabled;
52EXPORT_SYMBOL(efi_enabled);
53
54struct efi efi;
55EXPORT_SYMBOL(efi);
56
57struct efi_memory_map memmap;
58
Harvey Harrisonecaea422008-02-13 13:26:13 -080059static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010060static efi_system_table_t efi_systab __initdata;
61
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010062static int __init setup_noefi(char *arg)
63{
64 efi_enabled = 0;
65 return 0;
66}
67early_param("noefi", setup_noefi);
68
Paul Jackson200001e2008-06-25 05:44:46 -070069int add_efi_memmap;
70EXPORT_SYMBOL(add_efi_memmap);
71
72static int __init setup_add_efi_memmap(char *arg)
73{
74 add_efi_memmap = 1;
75 return 0;
76}
77early_param("add_efi_memmap", setup_add_efi_memmap);
78
79
Huang, Ying5b836832008-01-30 13:31:19 +010080static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
81{
Jan Beulichef68c8f2011-07-19 11:53:07 +010082 unsigned long flags;
83 efi_status_t status;
84
85 spin_lock_irqsave(&rtc_lock, flags);
86 status = efi_call_virt2(get_time, tm, tc);
87 spin_unlock_irqrestore(&rtc_lock, flags);
88 return status;
Huang, Ying5b836832008-01-30 13:31:19 +010089}
90
91static efi_status_t virt_efi_set_time(efi_time_t *tm)
92{
Jan Beulichef68c8f2011-07-19 11:53:07 +010093 unsigned long flags;
94 efi_status_t status;
95
96 spin_lock_irqsave(&rtc_lock, flags);
97 status = efi_call_virt1(set_time, tm);
98 spin_unlock_irqrestore(&rtc_lock, flags);
99 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100100}
101
102static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
103 efi_bool_t *pending,
104 efi_time_t *tm)
105{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100106 unsigned long flags;
107 efi_status_t status;
108
109 spin_lock_irqsave(&rtc_lock, flags);
110 status = efi_call_virt3(get_wakeup_time,
111 enabled, pending, tm);
112 spin_unlock_irqrestore(&rtc_lock, flags);
113 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100114}
115
116static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
117{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100118 unsigned long flags;
119 efi_status_t status;
120
121 spin_lock_irqsave(&rtc_lock, flags);
122 status = efi_call_virt2(set_wakeup_time,
123 enabled, 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_variable(efi_char16_t *name,
129 efi_guid_t *vendor,
130 u32 *attr,
131 unsigned long *data_size,
132 void *data)
133{
134 return efi_call_virt5(get_variable,
135 name, vendor, attr,
136 data_size, data);
137}
138
139static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
140 efi_char16_t *name,
141 efi_guid_t *vendor)
142{
143 return efi_call_virt3(get_next_variable,
144 name_size, name, vendor);
145}
146
147static efi_status_t virt_efi_set_variable(efi_char16_t *name,
148 efi_guid_t *vendor,
149 unsigned long attr,
150 unsigned long data_size,
151 void *data)
152{
153 return efi_call_virt5(set_variable,
154 name, vendor, attr,
155 data_size, data);
156}
157
158static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
159{
160 return efi_call_virt1(get_next_high_mono_count, count);
161}
162
163static void virt_efi_reset_system(int reset_type,
164 efi_status_t status,
165 unsigned long data_size,
166 efi_char16_t *data)
167{
168 efi_call_virt4(reset_system, reset_type, status,
169 data_size, data);
170}
171
Huang, Ying5b836832008-01-30 13:31:19 +0100172static efi_status_t __init phys_efi_set_virtual_address_map(
173 unsigned long memory_map_size,
174 unsigned long descriptor_size,
175 u32 descriptor_version,
176 efi_memory_desc_t *virtual_map)
177{
178 efi_status_t status;
179
180 efi_call_phys_prelog();
181 status = efi_call_phys4(efi_phys.set_virtual_address_map,
182 memory_map_size, descriptor_size,
183 descriptor_version, virtual_map);
184 efi_call_phys_epilog();
185 return status;
186}
187
188static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
189 efi_time_cap_t *tc)
190{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100191 unsigned long flags;
Huang, Ying5b836832008-01-30 13:31:19 +0100192 efi_status_t status;
193
Jan Beulichef68c8f2011-07-19 11:53:07 +0100194 spin_lock_irqsave(&rtc_lock, flags);
Huang, Ying5b836832008-01-30 13:31:19 +0100195 efi_call_phys_prelog();
196 status = efi_call_phys2(efi_phys.get_time, tm, tc);
197 efi_call_phys_epilog();
Jan Beulichef68c8f2011-07-19 11:53:07 +0100198 spin_unlock_irqrestore(&rtc_lock, flags);
Huang, Ying5b836832008-01-30 13:31:19 +0100199 return status;
200}
201
202int efi_set_rtc_mmss(unsigned long nowtime)
203{
204 int real_seconds, real_minutes;
205 efi_status_t status;
206 efi_time_t eft;
207 efi_time_cap_t cap;
208
209 status = efi.get_time(&eft, &cap);
210 if (status != EFI_SUCCESS) {
211 printk(KERN_ERR "Oops: efitime: can't read time!\n");
212 return -1;
213 }
214
215 real_seconds = nowtime % 60;
216 real_minutes = nowtime / 60;
217 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
218 real_minutes += 30;
219 real_minutes %= 60;
220 eft.minute = real_minutes;
221 eft.second = real_seconds;
222
223 status = efi.set_time(&eft);
224 if (status != EFI_SUCCESS) {
225 printk(KERN_ERR "Oops: efitime: can't write time!\n");
226 return -1;
227 }
228 return 0;
229}
230
231unsigned long efi_get_time(void)
232{
233 efi_status_t status;
234 efi_time_t eft;
235 efi_time_cap_t cap;
236
237 status = efi.get_time(&eft, &cap);
238 if (status != EFI_SUCCESS)
239 printk(KERN_ERR "Oops: efitime: can't read time!\n");
240
241 return mktime(eft.year, eft.month, eft.day, eft.hour,
242 eft.minute, eft.second);
243}
244
Paul Jackson69c91892008-05-14 08:15:58 -0700245/*
246 * Tell the kernel about the EFI memory map. This might include
247 * more than the max 128 entries that can fit in the e820 legacy
248 * (zeropage) memory map.
249 */
250
Paul Jackson200001e2008-06-25 05:44:46 -0700251static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700252{
253 void *p;
254
255 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
256 efi_memory_desc_t *md = p;
257 unsigned long long start = md->phys_addr;
258 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
259 int e820_type;
260
Cliff Wickmane2a71472009-06-16 16:43:40 -0500261 switch (md->type) {
262 case EFI_LOADER_CODE:
263 case EFI_LOADER_DATA:
264 case EFI_BOOT_SERVICES_CODE:
265 case EFI_BOOT_SERVICES_DATA:
266 case EFI_CONVENTIONAL_MEMORY:
267 if (md->attribute & EFI_MEMORY_WB)
268 e820_type = E820_RAM;
269 else
270 e820_type = E820_RESERVED;
271 break;
272 case EFI_ACPI_RECLAIM_MEMORY:
273 e820_type = E820_ACPI;
274 break;
275 case EFI_ACPI_MEMORY_NVS:
276 e820_type = E820_NVS;
277 break;
278 case EFI_UNUSABLE_MEMORY:
279 e820_type = E820_UNUSABLE;
280 break;
281 default:
282 /*
283 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
284 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
285 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
286 */
Paul Jackson69c91892008-05-14 08:15:58 -0700287 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500288 break;
289 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700290 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700291 }
292 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
293}
294
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700295void __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800296{
297 unsigned long pmap;
298
Paul Jackson05486fa2008-06-22 07:22:02 -0700299#ifdef CONFIG_X86_32
Huang, Yingecacf092008-06-02 14:26:21 +0800300 pmap = boot_params.efi_info.efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700301#else
302 pmap = (boot_params.efi_info.efi_memmap |
303 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
Huang, Yingecacf092008-06-02 14:26:21 +0800304#endif
305 memmap.phys_map = (void *)pmap;
306 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
307 boot_params.efi_info.efi_memdesc_size;
308 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
309 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700310 memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
Huang, Yingecacf092008-06-02 14:26:21 +0800311 "EFI memmap");
312}
313
Huang, Ying5b836832008-01-30 13:31:19 +0100314#if EFI_DEBUG
315static void __init print_efi_memmap(void)
316{
317 efi_memory_desc_t *md;
318 void *p;
319 int i;
320
321 for (p = memmap.map, i = 0;
322 p < memmap.map_end;
323 p += memmap.desc_size, i++) {
324 md = p;
325 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
326 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
327 i, md->type, md->attribute, md->phys_addr,
328 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
329 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
330 }
331}
332#endif /* EFI_DEBUG */
333
Matthew Garrett916f6762011-05-25 09:53:13 -0400334void __init efi_reserve_boot_services(void)
335{
336 void *p;
337
338 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
339 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200340 u64 start = md->phys_addr;
341 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400342
343 if (md->type != EFI_BOOT_SERVICES_CODE &&
344 md->type != EFI_BOOT_SERVICES_DATA)
345 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200346 /* Only reserve where possible:
347 * - Not within any already allocated areas
348 * - Not over any memory area (really needed, if above?)
349 * - Not within any part of the kernel
350 * - Not the bios reserved area
351 */
352 if ((start+size >= virt_to_phys(_text)
353 && start <= virt_to_phys(_end)) ||
354 !e820_all_mapped(start, start+size, E820_RAM) ||
355 memblock_x86_check_reserved_size(&start, &size,
356 1<<EFI_PAGE_SHIFT)) {
357 /* Could not reserve, skip it */
358 md->num_pages = 0;
359 memblock_dbg(PFX "Could not reserve boot range "
360 "[0x%010llx-0x%010llx]\n",
361 start, start+size-1);
362 } else
363 memblock_x86_reserve_range(start, start+size,
364 "EFI Boot");
Matthew Garrett916f6762011-05-25 09:53:13 -0400365 }
366}
367
368static void __init efi_free_boot_services(void)
369{
370 void *p;
371
372 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
373 efi_memory_desc_t *md = p;
374 unsigned long long start = md->phys_addr;
375 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
376
377 if (md->type != EFI_BOOT_SERVICES_CODE &&
378 md->type != EFI_BOOT_SERVICES_DATA)
379 continue;
380
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200381 /* Could not reserve boot area */
382 if (!size)
383 continue;
384
Matthew Garrett916f6762011-05-25 09:53:13 -0400385 free_bootmem_late(start, size);
386 }
387}
388
Huang, Ying5b836832008-01-30 13:31:19 +0100389void __init efi_init(void)
390{
391 efi_config_table_t *config_tables;
392 efi_runtime_services_t *runtime;
393 efi_char16_t *c16;
394 char vendor[100] = "unknown";
395 int i = 0;
396 void *tmp;
397
Paul Jackson05486fa2008-06-22 07:22:02 -0700398#ifdef CONFIG_X86_32
Huang, Ying5b836832008-01-30 13:31:19 +0100399 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
Paul Jackson05486fa2008-06-22 07:22:02 -0700400#else
401 efi_phys.systab = (efi_system_table_t *)
402 (boot_params.efi_info.efi_systab |
403 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Huang, Ying5b836832008-01-30 13:31:19 +0100404#endif
Huang, Ying5b836832008-01-30 13:31:19 +0100405
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100406 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
407 sizeof(efi_system_table_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100408 if (efi.systab == NULL)
409 printk(KERN_ERR "Couldn't map the EFI system table!\n");
410 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100411 early_iounmap(efi.systab, sizeof(efi_system_table_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100412 efi.systab = &efi_systab;
413
414 /*
415 * Verify the EFI Table
416 */
417 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
418 printk(KERN_ERR "EFI system table signature incorrect!\n");
419 if ((efi.systab->hdr.revision >> 16) == 0)
420 printk(KERN_ERR "Warning: EFI system table version "
421 "%d.%02d, expected 1.00 or greater!\n",
422 efi.systab->hdr.revision >> 16,
423 efi.systab->hdr.revision & 0xffff);
424
425 /*
426 * Show what we know for posterity
427 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100428 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
Huang, Ying5b836832008-01-30 13:31:19 +0100429 if (c16) {
Roel Kluinfdb8a422009-08-06 15:58:13 -0700430 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
Huang, Ying5b836832008-01-30 13:31:19 +0100431 vendor[i] = *c16++;
432 vendor[i] = '\0';
433 } else
434 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100435 early_iounmap(tmp, 2);
Huang, Ying5b836832008-01-30 13:31:19 +0100436
Frans Pop3235dc32010-02-06 18:47:17 +0100437 printk(KERN_INFO "EFI v%u.%.02u by %s\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100438 efi.systab->hdr.revision >> 16,
439 efi.systab->hdr.revision & 0xffff, vendor);
440
441 /*
442 * Let's see what config tables the firmware passed to us.
443 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100444 config_tables = early_ioremap(
Huang, Ying5b836832008-01-30 13:31:19 +0100445 efi.systab->tables,
446 efi.systab->nr_tables * sizeof(efi_config_table_t));
447 if (config_tables == NULL)
448 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
449
450 printk(KERN_INFO);
451 for (i = 0; i < efi.systab->nr_tables; i++) {
452 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
453 efi.mps = config_tables[i].table;
454 printk(" MPS=0x%lx ", config_tables[i].table);
455 } else if (!efi_guidcmp(config_tables[i].guid,
456 ACPI_20_TABLE_GUID)) {
457 efi.acpi20 = config_tables[i].table;
458 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
459 } else if (!efi_guidcmp(config_tables[i].guid,
460 ACPI_TABLE_GUID)) {
461 efi.acpi = config_tables[i].table;
462 printk(" ACPI=0x%lx ", config_tables[i].table);
463 } else if (!efi_guidcmp(config_tables[i].guid,
464 SMBIOS_TABLE_GUID)) {
465 efi.smbios = config_tables[i].table;
466 printk(" SMBIOS=0x%lx ", config_tables[i].table);
Nick Piggin03b48632009-01-20 04:36:04 +0100467#ifdef CONFIG_X86_UV
Huang, Ying5b836832008-01-30 13:31:19 +0100468 } else if (!efi_guidcmp(config_tables[i].guid,
Russ Andersona50f70b2008-10-03 11:58:54 -0500469 UV_SYSTEM_TABLE_GUID)) {
470 efi.uv_systab = config_tables[i].table;
471 printk(" UVsystab=0x%lx ", config_tables[i].table);
Nick Piggin03b48632009-01-20 04:36:04 +0100472#endif
Russ Andersona50f70b2008-10-03 11:58:54 -0500473 } else if (!efi_guidcmp(config_tables[i].guid,
Huang, Ying5b836832008-01-30 13:31:19 +0100474 HCDP_TABLE_GUID)) {
475 efi.hcdp = config_tables[i].table;
476 printk(" HCDP=0x%lx ", config_tables[i].table);
477 } else if (!efi_guidcmp(config_tables[i].guid,
478 UGA_IO_PROTOCOL_GUID)) {
479 efi.uga = config_tables[i].table;
480 printk(" UGA=0x%lx ", config_tables[i].table);
481 }
482 }
483 printk("\n");
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100484 early_iounmap(config_tables,
Huang, Ying5b836832008-01-30 13:31:19 +0100485 efi.systab->nr_tables * sizeof(efi_config_table_t));
486
487 /*
488 * Check out the runtime services table. We need to map
489 * the runtime services table so that we can grab the physical
490 * address of several of the EFI runtime functions, needed to
491 * set the firmware into virtual mode.
492 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100493 runtime = early_ioremap((unsigned long)efi.systab->runtime,
494 sizeof(efi_runtime_services_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100495 if (runtime != NULL) {
496 /*
497 * We will only need *early* access to the following
498 * two EFI runtime services before set_virtual_address_map
499 * is invoked.
500 */
501 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
502 efi_phys.set_virtual_address_map =
503 (efi_set_virtual_address_map_t *)
504 runtime->set_virtual_address_map;
505 /*
506 * Make efi_get_time can be called before entering
507 * virtual mode.
508 */
509 efi.get_time = phys_efi_get_time;
510 } else
511 printk(KERN_ERR "Could not map the EFI runtime service "
512 "table!\n");
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100513 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100514
515 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100516 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
517 memmap.nr_map * memmap.desc_size);
Huang, Ying5b836832008-01-30 13:31:19 +0100518 if (memmap.map == NULL)
519 printk(KERN_ERR "Could not map the EFI memory map!\n");
520 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500521
Huang, Ying5b836832008-01-30 13:31:19 +0100522 if (memmap.desc_size != sizeof(efi_memory_desc_t))
Russ Anderson175e4382008-10-02 17:32:06 -0500523 printk(KERN_WARNING
524 "Kernel-defined memdesc doesn't match the one from EFI!\n");
525
Paul Jackson200001e2008-06-25 05:44:46 -0700526 if (add_efi_memmap)
527 do_add_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100528
Feng Tang772be892009-10-20 12:54:02 +0800529#ifdef CONFIG_X86_32
Feng Tang7bd867d2009-09-10 10:48:56 +0800530 x86_platform.get_wallclock = efi_get_time;
531 x86_platform.set_wallclock = efi_set_rtc_mmss;
Feng Tang772be892009-10-20 12:54:02 +0800532#endif
Feng Tang7bd867d2009-09-10 10:48:56 +0800533
Huang, Ying5b836832008-01-30 13:31:19 +0100534#if EFI_DEBUG
535 print_efi_memmap();
536#endif
537}
538
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400539void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
540{
541 u64 addr, npages;
542
543 addr = md->virt_addr;
544 npages = md->num_pages;
545
546 memrange_efi_to_native(&addr, &npages);
547
548 if (executable)
549 set_memory_x(addr, npages);
550 else
551 set_memory_nx(addr, npages);
552}
553
Huang, Yinga2172e22008-01-30 13:33:55 +0100554static void __init runtime_code_page_mkexec(void)
555{
556 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100557 void *p;
558
Huang, Yinga2172e22008-01-30 13:33:55 +0100559 /* Make EFI runtime service code area executable */
560 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
561 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100562
563 if (md->type != EFI_RUNTIME_SERVICES_CODE)
564 continue;
565
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400566 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100567 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100568}
Huang, Yinga2172e22008-01-30 13:33:55 +0100569
Huang, Ying5b836832008-01-30 13:31:19 +0100570/*
571 * This function will switch the EFI runtime services to virtual mode.
572 * Essentially, look through the EFI memmap and map every region that
573 * has the runtime attribute bit set in its memory descriptor and update
574 * that memory descriptor with the virtual address obtained from ioremap().
575 * This enables the runtime services to be called without having to
576 * thunk back into physical mode for every invocation.
577 */
578void __init efi_enter_virtual_mode(void)
579{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400580 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100581 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100582 unsigned long size;
Huang Yingdd39ecf2009-03-04 10:58:33 +0800583 u64 end, systab, addr, npages, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400584 void *p, *va, *new_memmap = NULL;
585 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100586
587 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400588
589 /* Merge contiguous regions of the same type and attribute */
590 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
591 u64 prev_size;
592 md = p;
593
594 if (!prev_md) {
595 prev_md = md;
596 continue;
597 }
598
599 if (prev_md->type != md->type ||
600 prev_md->attribute != md->attribute) {
601 prev_md = md;
602 continue;
603 }
604
605 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
606
607 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
608 prev_md->num_pages += md->num_pages;
609 md->type = EFI_RESERVED_TYPE;
610 md->attribute = 0;
611 continue;
612 }
613 prev_md = md;
614 }
615
Huang, Ying5b836832008-01-30 13:31:19 +0100616 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
617 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -0400618 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
619 md->type != EFI_BOOT_SERVICES_CODE &&
620 md->type != EFI_BOOT_SERVICES_DATA)
Huang, Ying5b836832008-01-30 13:31:19 +0100621 continue;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100622
623 size = md->num_pages << EFI_PAGE_SHIFT;
624 end = md->phys_addr + size;
625
Huang Yingdd39ecf2009-03-04 10:58:33 +0800626 end_pfn = PFN_UP(end);
627 if (end_pfn <= max_low_pfn_mapped
628 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
629 && end_pfn <= max_pfn_mapped))
Huang, Ying1c083eb2008-02-04 16:48:06 +0100630 va = __va(md->phys_addr);
Huang, Ying5b836832008-01-30 13:31:19 +0100631 else
Paul Mackerras6a7bbd52009-08-03 22:38:10 +1000632 va = efi_ioremap(md->phys_addr, size, md->type);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100633
Huang, Ying1c083eb2008-02-04 16:48:06 +0100634 md->virt_addr = (u64) (unsigned long) va;
635
636 if (!va) {
Huang, Ying5b836832008-01-30 13:31:19 +0100637 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
638 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100639 continue;
640 }
641
Huang, Ying4a3575f2008-02-25 15:18:37 +0800642 if (!(md->attribute & EFI_MEMORY_WB)) {
643 addr = md->virt_addr;
644 npages = md->num_pages;
645 memrange_efi_to_native(&addr, &npages);
646 set_memory_uc(addr, npages);
647 }
Thomas Gleixnere85f2052008-02-12 19:46:48 +0100648
Huang, Ying1c083eb2008-02-04 16:48:06 +0100649 systab = (u64) (unsigned long) efi_phys.systab;
650 if (md->phys_addr <= systab && systab < end) {
651 systab += md->virt_addr - md->phys_addr;
652 efi.systab = (efi_system_table_t *) (unsigned long) systab;
653 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400654 new_memmap = krealloc(new_memmap,
655 (count + 1) * memmap.desc_size,
656 GFP_KERNEL);
657 memcpy(new_memmap + (count * memmap.desc_size), md,
658 memmap.desc_size);
659 count++;
Huang, Ying5b836832008-01-30 13:31:19 +0100660 }
661
662 BUG_ON(!efi.systab);
663
664 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400665 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +0100666 memmap.desc_size,
667 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400668 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +0100669
670 if (status != EFI_SUCCESS) {
671 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
672 "(status=%lx)!\n", status);
673 panic("EFI call to SetVirtualAddressMap() failed!");
674 }
675
676 /*
Matthew Garrett916f6762011-05-25 09:53:13 -0400677 * Thankfully, it does seem that no runtime services other than
678 * SetVirtualAddressMap() will touch boot services code, so we can
679 * get rid of it all at this point
680 */
681 efi_free_boot_services();
682
683 /*
Huang, Ying5b836832008-01-30 13:31:19 +0100684 * Now that EFI is in virtual mode, update the function
685 * pointers in the runtime service table to the new virtual addresses.
686 *
687 * Call EFI services through wrapper functions.
688 */
689 efi.get_time = virt_efi_get_time;
690 efi.set_time = virt_efi_set_time;
691 efi.get_wakeup_time = virt_efi_get_wakeup_time;
692 efi.set_wakeup_time = virt_efi_set_wakeup_time;
693 efi.get_variable = virt_efi_get_variable;
694 efi.get_next_variable = virt_efi_get_next_variable;
695 efi.set_variable = virt_efi_set_variable;
696 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
697 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400698 efi.set_virtual_address_map = NULL;
Huang, Ying4de0d4a2008-02-13 17:22:41 +0800699 if (__supported_pte_mask & _PAGE_NX)
700 runtime_code_page_mkexec();
Huang, Yinga3828062008-01-30 13:34:10 +0100701 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
702 memmap.map = NULL;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400703 kfree(new_memmap);
Huang, Ying5b836832008-01-30 13:31:19 +0100704}
705
706/*
707 * Convenience functions to obtain memory types and attributes
708 */
709u32 efi_mem_type(unsigned long phys_addr)
710{
711 efi_memory_desc_t *md;
712 void *p;
713
714 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
715 md = p;
716 if ((md->phys_addr <= phys_addr) &&
717 (phys_addr < (md->phys_addr +
718 (md->num_pages << EFI_PAGE_SHIFT))))
719 return md->type;
720 }
721 return 0;
722}
723
724u64 efi_mem_attributes(unsigned long phys_addr)
725{
726 efi_memory_desc_t *md;
727 void *p;
728
729 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
730 md = p;
731 if ((md->phys_addr <= phys_addr) &&
732 (phys_addr < (md->phys_addr +
733 (md->num_pages << EFI_PAGE_SHIFT))))
734 return md->attribute;
735 }
736 return 0;
737}