blob: a4c322ca1a5d505df12c76142c91fcf15f0e3597 [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{
82 return efi_call_virt2(get_time, tm, tc);
83}
84
85static efi_status_t virt_efi_set_time(efi_time_t *tm)
86{
87 return efi_call_virt1(set_time, tm);
88}
89
90static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
91 efi_bool_t *pending,
92 efi_time_t *tm)
93{
94 return efi_call_virt3(get_wakeup_time,
95 enabled, pending, tm);
96}
97
98static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
99{
100 return efi_call_virt2(set_wakeup_time,
101 enabled, tm);
102}
103
104static efi_status_t virt_efi_get_variable(efi_char16_t *name,
105 efi_guid_t *vendor,
106 u32 *attr,
107 unsigned long *data_size,
108 void *data)
109{
110 return efi_call_virt5(get_variable,
111 name, vendor, attr,
112 data_size, data);
113}
114
115static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
116 efi_char16_t *name,
117 efi_guid_t *vendor)
118{
119 return efi_call_virt3(get_next_variable,
120 name_size, name, vendor);
121}
122
123static efi_status_t virt_efi_set_variable(efi_char16_t *name,
124 efi_guid_t *vendor,
125 unsigned long attr,
126 unsigned long data_size,
127 void *data)
128{
129 return efi_call_virt5(set_variable,
130 name, vendor, attr,
131 data_size, data);
132}
133
134static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
135{
136 return efi_call_virt1(get_next_high_mono_count, count);
137}
138
139static void virt_efi_reset_system(int reset_type,
140 efi_status_t status,
141 unsigned long data_size,
142 efi_char16_t *data)
143{
144 efi_call_virt4(reset_system, reset_type, status,
145 data_size, data);
146}
147
Huang, Ying5b836832008-01-30 13:31:19 +0100148static efi_status_t __init phys_efi_set_virtual_address_map(
149 unsigned long memory_map_size,
150 unsigned long descriptor_size,
151 u32 descriptor_version,
152 efi_memory_desc_t *virtual_map)
153{
154 efi_status_t status;
155
156 efi_call_phys_prelog();
157 status = efi_call_phys4(efi_phys.set_virtual_address_map,
158 memory_map_size, descriptor_size,
159 descriptor_version, virtual_map);
160 efi_call_phys_epilog();
161 return status;
162}
163
164static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
165 efi_time_cap_t *tc)
166{
167 efi_status_t status;
168
169 efi_call_phys_prelog();
170 status = efi_call_phys2(efi_phys.get_time, tm, tc);
171 efi_call_phys_epilog();
172 return status;
173}
174
175int efi_set_rtc_mmss(unsigned long nowtime)
176{
177 int real_seconds, real_minutes;
178 efi_status_t status;
179 efi_time_t eft;
180 efi_time_cap_t cap;
181
182 status = efi.get_time(&eft, &cap);
183 if (status != EFI_SUCCESS) {
184 printk(KERN_ERR "Oops: efitime: can't read time!\n");
185 return -1;
186 }
187
188 real_seconds = nowtime % 60;
189 real_minutes = nowtime / 60;
190 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
191 real_minutes += 30;
192 real_minutes %= 60;
193 eft.minute = real_minutes;
194 eft.second = real_seconds;
195
196 status = efi.set_time(&eft);
197 if (status != EFI_SUCCESS) {
198 printk(KERN_ERR "Oops: efitime: can't write time!\n");
199 return -1;
200 }
201 return 0;
202}
203
204unsigned long efi_get_time(void)
205{
206 efi_status_t status;
207 efi_time_t eft;
208 efi_time_cap_t cap;
209
210 status = efi.get_time(&eft, &cap);
211 if (status != EFI_SUCCESS)
212 printk(KERN_ERR "Oops: efitime: can't read time!\n");
213
214 return mktime(eft.year, eft.month, eft.day, eft.hour,
215 eft.minute, eft.second);
216}
217
Paul Jackson69c91892008-05-14 08:15:58 -0700218/*
219 * Tell the kernel about the EFI memory map. This might include
220 * more than the max 128 entries that can fit in the e820 legacy
221 * (zeropage) memory map.
222 */
223
Paul Jackson200001e2008-06-25 05:44:46 -0700224static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700225{
226 void *p;
227
228 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
229 efi_memory_desc_t *md = p;
230 unsigned long long start = md->phys_addr;
231 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
232 int e820_type;
233
Cliff Wickmane2a71472009-06-16 16:43:40 -0500234 switch (md->type) {
235 case EFI_LOADER_CODE:
236 case EFI_LOADER_DATA:
237 case EFI_BOOT_SERVICES_CODE:
238 case EFI_BOOT_SERVICES_DATA:
239 case EFI_CONVENTIONAL_MEMORY:
240 if (md->attribute & EFI_MEMORY_WB)
241 e820_type = E820_RAM;
242 else
243 e820_type = E820_RESERVED;
244 break;
245 case EFI_ACPI_RECLAIM_MEMORY:
246 e820_type = E820_ACPI;
247 break;
248 case EFI_ACPI_MEMORY_NVS:
249 e820_type = E820_NVS;
250 break;
251 case EFI_UNUSABLE_MEMORY:
252 e820_type = E820_UNUSABLE;
253 break;
254 default:
255 /*
256 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
257 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
258 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
259 */
Paul Jackson69c91892008-05-14 08:15:58 -0700260 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500261 break;
262 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700263 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700264 }
265 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
266}
267
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700268void __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800269{
270 unsigned long pmap;
271
Paul Jackson05486fa2008-06-22 07:22:02 -0700272#ifdef CONFIG_X86_32
Huang, Yingecacf092008-06-02 14:26:21 +0800273 pmap = boot_params.efi_info.efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700274#else
275 pmap = (boot_params.efi_info.efi_memmap |
276 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
Huang, Yingecacf092008-06-02 14:26:21 +0800277#endif
278 memmap.phys_map = (void *)pmap;
279 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
280 boot_params.efi_info.efi_memdesc_size;
281 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
282 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
Yinghai Lua9ce6bc2010-08-25 13:39:17 -0700283 memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
Huang, Yingecacf092008-06-02 14:26:21 +0800284 "EFI memmap");
285}
286
Huang, Ying5b836832008-01-30 13:31:19 +0100287#if EFI_DEBUG
288static void __init print_efi_memmap(void)
289{
290 efi_memory_desc_t *md;
291 void *p;
292 int i;
293
294 for (p = memmap.map, i = 0;
295 p < memmap.map_end;
296 p += memmap.desc_size, i++) {
297 md = p;
298 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
299 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
300 i, md->type, md->attribute, md->phys_addr,
301 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
302 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
303 }
304}
305#endif /* EFI_DEBUG */
306
Matthew Garrett916f6762011-05-25 09:53:13 -0400307void __init efi_reserve_boot_services(void)
308{
309 void *p;
310
311 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
312 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200313 u64 start = md->phys_addr;
314 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400315
316 if (md->type != EFI_BOOT_SERVICES_CODE &&
317 md->type != EFI_BOOT_SERVICES_DATA)
318 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200319 /* Only reserve where possible:
320 * - Not within any already allocated areas
321 * - Not over any memory area (really needed, if above?)
322 * - Not within any part of the kernel
323 * - Not the bios reserved area
324 */
325 if ((start+size >= virt_to_phys(_text)
326 && start <= virt_to_phys(_end)) ||
327 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200328 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200329 /* Could not reserve, skip it */
330 md->num_pages = 0;
331 memblock_dbg(PFX "Could not reserve boot range "
332 "[0x%010llx-0x%010llx]\n",
333 start, start+size-1);
334 } else
335 memblock_x86_reserve_range(start, start+size,
336 "EFI Boot");
Matthew Garrett916f6762011-05-25 09:53:13 -0400337 }
338}
339
340static void __init efi_free_boot_services(void)
341{
342 void *p;
343
344 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
345 efi_memory_desc_t *md = p;
346 unsigned long long start = md->phys_addr;
347 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
348
349 if (md->type != EFI_BOOT_SERVICES_CODE &&
350 md->type != EFI_BOOT_SERVICES_DATA)
351 continue;
352
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200353 /* Could not reserve boot area */
354 if (!size)
355 continue;
356
Matthew Garrett916f6762011-05-25 09:53:13 -0400357 free_bootmem_late(start, size);
358 }
359}
360
Huang, Ying5b836832008-01-30 13:31:19 +0100361void __init efi_init(void)
362{
363 efi_config_table_t *config_tables;
364 efi_runtime_services_t *runtime;
365 efi_char16_t *c16;
366 char vendor[100] = "unknown";
367 int i = 0;
368 void *tmp;
369
Paul Jackson05486fa2008-06-22 07:22:02 -0700370#ifdef CONFIG_X86_32
Huang, Ying5b836832008-01-30 13:31:19 +0100371 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
Paul Jackson05486fa2008-06-22 07:22:02 -0700372#else
373 efi_phys.systab = (efi_system_table_t *)
374 (boot_params.efi_info.efi_systab |
375 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Huang, Ying5b836832008-01-30 13:31:19 +0100376#endif
Huang, Ying5b836832008-01-30 13:31:19 +0100377
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100378 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
379 sizeof(efi_system_table_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100380 if (efi.systab == NULL)
381 printk(KERN_ERR "Couldn't map the EFI system table!\n");
382 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100383 early_iounmap(efi.systab, sizeof(efi_system_table_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100384 efi.systab = &efi_systab;
385
386 /*
387 * Verify the EFI Table
388 */
389 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
390 printk(KERN_ERR "EFI system table signature incorrect!\n");
391 if ((efi.systab->hdr.revision >> 16) == 0)
392 printk(KERN_ERR "Warning: EFI system table version "
393 "%d.%02d, expected 1.00 or greater!\n",
394 efi.systab->hdr.revision >> 16,
395 efi.systab->hdr.revision & 0xffff);
396
397 /*
398 * Show what we know for posterity
399 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100400 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
Huang, Ying5b836832008-01-30 13:31:19 +0100401 if (c16) {
Roel Kluinfdb8a422009-08-06 15:58:13 -0700402 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
Huang, Ying5b836832008-01-30 13:31:19 +0100403 vendor[i] = *c16++;
404 vendor[i] = '\0';
405 } else
406 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100407 early_iounmap(tmp, 2);
Huang, Ying5b836832008-01-30 13:31:19 +0100408
Frans Pop3235dc32010-02-06 18:47:17 +0100409 printk(KERN_INFO "EFI v%u.%.02u by %s\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100410 efi.systab->hdr.revision >> 16,
411 efi.systab->hdr.revision & 0xffff, vendor);
412
413 /*
414 * Let's see what config tables the firmware passed to us.
415 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100416 config_tables = early_ioremap(
Huang, Ying5b836832008-01-30 13:31:19 +0100417 efi.systab->tables,
418 efi.systab->nr_tables * sizeof(efi_config_table_t));
419 if (config_tables == NULL)
420 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
421
422 printk(KERN_INFO);
423 for (i = 0; i < efi.systab->nr_tables; i++) {
424 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
425 efi.mps = config_tables[i].table;
426 printk(" MPS=0x%lx ", config_tables[i].table);
427 } else if (!efi_guidcmp(config_tables[i].guid,
428 ACPI_20_TABLE_GUID)) {
429 efi.acpi20 = config_tables[i].table;
430 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
431 } else if (!efi_guidcmp(config_tables[i].guid,
432 ACPI_TABLE_GUID)) {
433 efi.acpi = config_tables[i].table;
434 printk(" ACPI=0x%lx ", config_tables[i].table);
435 } else if (!efi_guidcmp(config_tables[i].guid,
436 SMBIOS_TABLE_GUID)) {
437 efi.smbios = config_tables[i].table;
438 printk(" SMBIOS=0x%lx ", config_tables[i].table);
Nick Piggin03b48632009-01-20 04:36:04 +0100439#ifdef CONFIG_X86_UV
Huang, Ying5b836832008-01-30 13:31:19 +0100440 } else if (!efi_guidcmp(config_tables[i].guid,
Russ Andersona50f70b2008-10-03 11:58:54 -0500441 UV_SYSTEM_TABLE_GUID)) {
442 efi.uv_systab = config_tables[i].table;
443 printk(" UVsystab=0x%lx ", config_tables[i].table);
Nick Piggin03b48632009-01-20 04:36:04 +0100444#endif
Russ Andersona50f70b2008-10-03 11:58:54 -0500445 } else if (!efi_guidcmp(config_tables[i].guid,
Huang, Ying5b836832008-01-30 13:31:19 +0100446 HCDP_TABLE_GUID)) {
447 efi.hcdp = config_tables[i].table;
448 printk(" HCDP=0x%lx ", config_tables[i].table);
449 } else if (!efi_guidcmp(config_tables[i].guid,
450 UGA_IO_PROTOCOL_GUID)) {
451 efi.uga = config_tables[i].table;
452 printk(" UGA=0x%lx ", config_tables[i].table);
453 }
454 }
455 printk("\n");
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100456 early_iounmap(config_tables,
Huang, Ying5b836832008-01-30 13:31:19 +0100457 efi.systab->nr_tables * sizeof(efi_config_table_t));
458
459 /*
460 * Check out the runtime services table. We need to map
461 * the runtime services table so that we can grab the physical
462 * address of several of the EFI runtime functions, needed to
463 * set the firmware into virtual mode.
464 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100465 runtime = early_ioremap((unsigned long)efi.systab->runtime,
466 sizeof(efi_runtime_services_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100467 if (runtime != NULL) {
468 /*
469 * We will only need *early* access to the following
470 * two EFI runtime services before set_virtual_address_map
471 * is invoked.
472 */
473 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
474 efi_phys.set_virtual_address_map =
475 (efi_set_virtual_address_map_t *)
476 runtime->set_virtual_address_map;
477 /*
478 * Make efi_get_time can be called before entering
479 * virtual mode.
480 */
481 efi.get_time = phys_efi_get_time;
482 } else
483 printk(KERN_ERR "Could not map the EFI runtime service "
484 "table!\n");
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100485 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Huang, Ying5b836832008-01-30 13:31:19 +0100486
487 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100488 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
489 memmap.nr_map * memmap.desc_size);
Huang, Ying5b836832008-01-30 13:31:19 +0100490 if (memmap.map == NULL)
491 printk(KERN_ERR "Could not map the EFI memory map!\n");
492 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500493
Huang, Ying5b836832008-01-30 13:31:19 +0100494 if (memmap.desc_size != sizeof(efi_memory_desc_t))
Russ Anderson175e4382008-10-02 17:32:06 -0500495 printk(KERN_WARNING
496 "Kernel-defined memdesc doesn't match the one from EFI!\n");
497
Paul Jackson200001e2008-06-25 05:44:46 -0700498 if (add_efi_memmap)
499 do_add_efi_memmap();
Huang, Ying5b836832008-01-30 13:31:19 +0100500
Feng Tang772be892009-10-20 12:54:02 +0800501#ifdef CONFIG_X86_32
Feng Tang7bd867d2009-09-10 10:48:56 +0800502 x86_platform.get_wallclock = efi_get_time;
503 x86_platform.set_wallclock = efi_set_rtc_mmss;
Feng Tang772be892009-10-20 12:54:02 +0800504#endif
Feng Tang7bd867d2009-09-10 10:48:56 +0800505
Huang, Ying5b836832008-01-30 13:31:19 +0100506#if EFI_DEBUG
507 print_efi_memmap();
508#endif
509}
510
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400511void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
512{
513 u64 addr, npages;
514
515 addr = md->virt_addr;
516 npages = md->num_pages;
517
518 memrange_efi_to_native(&addr, &npages);
519
520 if (executable)
521 set_memory_x(addr, npages);
522 else
523 set_memory_nx(addr, npages);
524}
525
Huang, Yinga2172e22008-01-30 13:33:55 +0100526static void __init runtime_code_page_mkexec(void)
527{
528 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100529 void *p;
530
Huang, Yinga2172e22008-01-30 13:33:55 +0100531 /* Make EFI runtime service code area executable */
532 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
533 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100534
535 if (md->type != EFI_RUNTIME_SERVICES_CODE)
536 continue;
537
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400538 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100539 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100540}
Huang, Yinga2172e22008-01-30 13:33:55 +0100541
Huang, Ying5b836832008-01-30 13:31:19 +0100542/*
543 * This function will switch the EFI runtime services to virtual mode.
544 * Essentially, look through the EFI memmap and map every region that
545 * has the runtime attribute bit set in its memory descriptor and update
546 * that memory descriptor with the virtual address obtained from ioremap().
547 * This enables the runtime services to be called without having to
548 * thunk back into physical mode for every invocation.
549 */
550void __init efi_enter_virtual_mode(void)
551{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400552 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100553 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100554 unsigned long size;
Huang Yingdd39ecf2009-03-04 10:58:33 +0800555 u64 end, systab, addr, npages, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400556 void *p, *va, *new_memmap = NULL;
557 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100558
559 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400560
561 /* Merge contiguous regions of the same type and attribute */
562 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
563 u64 prev_size;
564 md = p;
565
566 if (!prev_md) {
567 prev_md = md;
568 continue;
569 }
570
571 if (prev_md->type != md->type ||
572 prev_md->attribute != md->attribute) {
573 prev_md = md;
574 continue;
575 }
576
577 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
578
579 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
580 prev_md->num_pages += md->num_pages;
581 md->type = EFI_RESERVED_TYPE;
582 md->attribute = 0;
583 continue;
584 }
585 prev_md = md;
586 }
587
Huang, Ying5b836832008-01-30 13:31:19 +0100588 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
589 md = p;
Matthew Garrett916f6762011-05-25 09:53:13 -0400590 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
591 md->type != EFI_BOOT_SERVICES_CODE &&
592 md->type != EFI_BOOT_SERVICES_DATA)
Huang, Ying5b836832008-01-30 13:31:19 +0100593 continue;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100594
595 size = md->num_pages << EFI_PAGE_SHIFT;
596 end = md->phys_addr + size;
597
Huang Yingdd39ecf2009-03-04 10:58:33 +0800598 end_pfn = PFN_UP(end);
599 if (end_pfn <= max_low_pfn_mapped
600 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
601 && end_pfn <= max_pfn_mapped))
Huang, Ying1c083eb2008-02-04 16:48:06 +0100602 va = __va(md->phys_addr);
Huang, Ying5b836832008-01-30 13:31:19 +0100603 else
Paul Mackerras6a7bbd52009-08-03 22:38:10 +1000604 va = efi_ioremap(md->phys_addr, size, md->type);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100605
Huang, Ying1c083eb2008-02-04 16:48:06 +0100606 md->virt_addr = (u64) (unsigned long) va;
607
608 if (!va) {
Huang, Ying5b836832008-01-30 13:31:19 +0100609 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
610 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100611 continue;
612 }
613
Huang, Ying4a3575f2008-02-25 15:18:37 +0800614 if (!(md->attribute & EFI_MEMORY_WB)) {
615 addr = md->virt_addr;
616 npages = md->num_pages;
617 memrange_efi_to_native(&addr, &npages);
618 set_memory_uc(addr, npages);
619 }
Thomas Gleixnere85f2052008-02-12 19:46:48 +0100620
Huang, Ying1c083eb2008-02-04 16:48:06 +0100621 systab = (u64) (unsigned long) efi_phys.systab;
622 if (md->phys_addr <= systab && systab < end) {
623 systab += md->virt_addr - md->phys_addr;
624 efi.systab = (efi_system_table_t *) (unsigned long) systab;
625 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400626 new_memmap = krealloc(new_memmap,
627 (count + 1) * memmap.desc_size,
628 GFP_KERNEL);
629 memcpy(new_memmap + (count * memmap.desc_size), md,
630 memmap.desc_size);
631 count++;
Huang, Ying5b836832008-01-30 13:31:19 +0100632 }
633
634 BUG_ON(!efi.systab);
635
636 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400637 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +0100638 memmap.desc_size,
639 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400640 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +0100641
642 if (status != EFI_SUCCESS) {
643 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
644 "(status=%lx)!\n", status);
645 panic("EFI call to SetVirtualAddressMap() failed!");
646 }
647
648 /*
Matthew Garrett916f6762011-05-25 09:53:13 -0400649 * Thankfully, it does seem that no runtime services other than
650 * SetVirtualAddressMap() will touch boot services code, so we can
651 * get rid of it all at this point
652 */
653 efi_free_boot_services();
654
655 /*
Huang, Ying5b836832008-01-30 13:31:19 +0100656 * Now that EFI is in virtual mode, update the function
657 * pointers in the runtime service table to the new virtual addresses.
658 *
659 * Call EFI services through wrapper functions.
660 */
661 efi.get_time = virt_efi_get_time;
662 efi.set_time = virt_efi_set_time;
663 efi.get_wakeup_time = virt_efi_get_wakeup_time;
664 efi.set_wakeup_time = virt_efi_set_wakeup_time;
665 efi.get_variable = virt_efi_get_variable;
666 efi.get_next_variable = virt_efi_get_next_variable;
667 efi.set_variable = virt_efi_set_variable;
668 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
669 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400670 efi.set_virtual_address_map = NULL;
Huang, Ying4de0d4a2008-02-13 17:22:41 +0800671 if (__supported_pte_mask & _PAGE_NX)
672 runtime_code_page_mkexec();
Huang, Yinga3828062008-01-30 13:34:10 +0100673 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
674 memmap.map = NULL;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400675 kfree(new_memmap);
Huang, Ying5b836832008-01-30 13:31:19 +0100676}
677
678/*
679 * Convenience functions to obtain memory types and attributes
680 */
681u32 efi_mem_type(unsigned long phys_addr)
682{
683 efi_memory_desc_t *md;
684 void *p;
685
686 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
687 md = p;
688 if ((md->phys_addr <= phys_addr) &&
689 (phys_addr < (md->phys_addr +
690 (md->num_pages << EFI_PAGE_SHIFT))))
691 return md->type;
692 }
693 return 0;
694}
695
696u64 efi_mem_attributes(unsigned long phys_addr)
697{
698 efi_memory_desc_t *md;
699 void *p;
700
701 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
702 md = p;
703 if ((md->phys_addr <= phys_addr) &&
704 (phys_addr < (md->phys_addr +
705 (md->num_pages << EFI_PAGE_SHIFT))))
706 return md->attribute;
707 }
708 return 0;
709}