Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / arch / x86 / platform / efi / efi.c
blob72d88992f38454b56744026b10dbc8362cba9d1b
1 /*
2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
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>
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
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
23 * Implemented EFI runtime services and virtual mode calls. --davidm
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/efi.h>
34 #include <linux/export.h>
35 #include <linux/bootmem.h>
36 #include <linux/memblock.h>
37 #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>
44 #include <asm/setup.h>
45 #include <asm/efi.h>
46 #include <asm/time.h>
47 #include <asm/cacheflush.h>
48 #include <asm/tlbflush.h>
49 #include <asm/x86_init.h>
51 #define EFI_DEBUG 1
53 int efi_enabled;
54 EXPORT_SYMBOL(efi_enabled);
56 struct 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,
67 EXPORT_SYMBOL(efi);
69 struct efi_memory_map memmap;
71 bool efi_64bit;
73 static struct efi efi_phys __initdata;
74 static efi_system_table_t efi_systab __initdata;
76 static inline bool efi_is_native(void)
78 return IS_ENABLED(CONFIG_X86_64) == efi_64bit;
81 static int __init setup_noefi(char *arg)
83 efi_enabled = 0;
84 return 0;
86 early_param("noefi", setup_noefi);
88 int add_efi_memmap;
89 EXPORT_SYMBOL(add_efi_memmap);
91 static int __init setup_add_efi_memmap(char *arg)
93 add_efi_memmap = 1;
94 return 0;
96 early_param("add_efi_memmap", setup_add_efi_memmap);
99 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
101 unsigned long flags;
102 efi_status_t status;
104 spin_lock_irqsave(&rtc_lock, flags);
105 status = efi_call_virt2(get_time, tm, tc);
106 spin_unlock_irqrestore(&rtc_lock, flags);
107 return status;
110 static efi_status_t virt_efi_set_time(efi_time_t *tm)
112 unsigned long flags;
113 efi_status_t status;
115 spin_lock_irqsave(&rtc_lock, flags);
116 status = efi_call_virt1(set_time, tm);
117 spin_unlock_irqrestore(&rtc_lock, flags);
118 return status;
121 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
122 efi_bool_t *pending,
123 efi_time_t *tm)
125 unsigned long flags;
126 efi_status_t status;
128 spin_lock_irqsave(&rtc_lock, flags);
129 status = efi_call_virt3(get_wakeup_time,
130 enabled, pending, tm);
131 spin_unlock_irqrestore(&rtc_lock, flags);
132 return status;
135 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
137 unsigned long flags;
138 efi_status_t status;
140 spin_lock_irqsave(&rtc_lock, flags);
141 status = efi_call_virt2(set_wakeup_time,
142 enabled, tm);
143 spin_unlock_irqrestore(&rtc_lock, flags);
144 return status;
147 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
148 efi_guid_t *vendor,
149 u32 *attr,
150 unsigned long *data_size,
151 void *data)
153 return efi_call_virt5(get_variable,
154 name, vendor, attr,
155 data_size, data);
158 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
159 efi_char16_t *name,
160 efi_guid_t *vendor)
162 return efi_call_virt3(get_next_variable,
163 name_size, name, vendor);
166 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
167 efi_guid_t *vendor,
168 u32 attr,
169 unsigned long data_size,
170 void *data)
172 return efi_call_virt5(set_variable,
173 name, vendor, attr,
174 data_size, data);
177 static efi_status_t virt_efi_query_variable_info(u32 attr,
178 u64 *storage_space,
179 u64 *remaining_space,
180 u64 *max_variable_size)
182 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
183 return EFI_UNSUPPORTED;
185 return efi_call_virt4(query_variable_info, attr, storage_space,
186 remaining_space, max_variable_size);
189 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
191 return efi_call_virt1(get_next_high_mono_count, count);
194 static void virt_efi_reset_system(int reset_type,
195 efi_status_t status,
196 unsigned long data_size,
197 efi_char16_t *data)
199 efi_call_virt4(reset_system, reset_type, status,
200 data_size, data);
203 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
204 unsigned long count,
205 unsigned long sg_list)
207 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
208 return EFI_UNSUPPORTED;
210 return efi_call_virt3(update_capsule, capsules, count, sg_list);
213 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
214 unsigned long count,
215 u64 *max_size,
216 int *reset_type)
218 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
219 return EFI_UNSUPPORTED;
221 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
222 reset_type);
225 static efi_status_t __init phys_efi_set_virtual_address_map(
226 unsigned long memory_map_size,
227 unsigned long descriptor_size,
228 u32 descriptor_version,
229 efi_memory_desc_t *virtual_map)
231 efi_status_t status;
233 efi_call_phys_prelog();
234 status = efi_call_phys4(efi_phys.set_virtual_address_map,
235 memory_map_size, descriptor_size,
236 descriptor_version, virtual_map);
237 efi_call_phys_epilog();
238 return status;
241 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
242 efi_time_cap_t *tc)
244 unsigned long flags;
245 efi_status_t status;
247 spin_lock_irqsave(&rtc_lock, flags);
248 efi_call_phys_prelog();
249 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
250 virt_to_phys(tc));
251 efi_call_phys_epilog();
252 spin_unlock_irqrestore(&rtc_lock, flags);
253 return status;
256 int efi_set_rtc_mmss(unsigned long nowtime)
258 int real_seconds, real_minutes;
259 efi_status_t status;
260 efi_time_t eft;
261 efi_time_cap_t cap;
263 status = efi.get_time(&eft, &cap);
264 if (status != EFI_SUCCESS) {
265 pr_err("Oops: efitime: can't read time!\n");
266 return -1;
269 real_seconds = nowtime % 60;
270 real_minutes = nowtime / 60;
271 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
272 real_minutes += 30;
273 real_minutes %= 60;
274 eft.minute = real_minutes;
275 eft.second = real_seconds;
277 status = efi.set_time(&eft);
278 if (status != EFI_SUCCESS) {
279 pr_err("Oops: efitime: can't write time!\n");
280 return -1;
282 return 0;
285 unsigned long efi_get_time(void)
287 efi_status_t status;
288 efi_time_t eft;
289 efi_time_cap_t cap;
291 status = efi.get_time(&eft, &cap);
292 if (status != EFI_SUCCESS)
293 pr_err("Oops: efitime: can't read time!\n");
295 return mktime(eft.year, eft.month, eft.day, eft.hour,
296 eft.minute, eft.second);
300 * Tell the kernel about the EFI memory map. This might include
301 * more than the max 128 entries that can fit in the e820 legacy
302 * (zeropage) memory map.
305 static void __init do_add_efi_memmap(void)
307 void *p;
309 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
310 efi_memory_desc_t *md = p;
311 unsigned long long start = md->phys_addr;
312 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
313 int e820_type;
315 switch (md->type) {
316 case EFI_LOADER_CODE:
317 case EFI_LOADER_DATA:
318 case EFI_BOOT_SERVICES_CODE:
319 case EFI_BOOT_SERVICES_DATA:
320 case EFI_CONVENTIONAL_MEMORY:
321 if (md->attribute & EFI_MEMORY_WB)
322 e820_type = E820_RAM;
323 else
324 e820_type = E820_RESERVED;
325 break;
326 case EFI_ACPI_RECLAIM_MEMORY:
327 e820_type = E820_ACPI;
328 break;
329 case EFI_ACPI_MEMORY_NVS:
330 e820_type = E820_NVS;
331 break;
332 case EFI_UNUSABLE_MEMORY:
333 e820_type = E820_UNUSABLE;
334 break;
335 default:
337 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
338 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
339 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
341 e820_type = E820_RESERVED;
342 break;
344 e820_add_region(start, size, e820_type);
346 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
349 int __init efi_memblock_x86_reserve_range(void)
351 unsigned long pmap;
353 #ifdef CONFIG_X86_32
354 /* Can't handle data above 4GB at this time */
355 if (boot_params.efi_info.efi_memmap_hi) {
356 pr_err("Memory map is above 4GB, disabling EFI.\n");
357 return -EINVAL;
359 pmap = boot_params.efi_info.efi_memmap;
360 #else
361 pmap = (boot_params.efi_info.efi_memmap |
362 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
363 #endif
364 memmap.phys_map = (void *)pmap;
365 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
366 boot_params.efi_info.efi_memdesc_size;
367 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
368 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
369 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
371 return 0;
374 #if EFI_DEBUG
375 static void __init print_efi_memmap(void)
377 efi_memory_desc_t *md;
378 void *p;
379 int i;
381 for (p = memmap.map, i = 0;
382 p < memmap.map_end;
383 p += memmap.desc_size, i++) {
384 md = p;
385 pr_info("mem%02u: type=%u, attr=0x%llx, "
386 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
387 i, md->type, md->attribute, md->phys_addr,
388 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
389 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
392 #endif /* EFI_DEBUG */
394 void __init efi_reserve_boot_services(void)
396 void *p;
398 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
399 efi_memory_desc_t *md = p;
400 u64 start = md->phys_addr;
401 u64 size = md->num_pages << EFI_PAGE_SHIFT;
403 if (md->type != EFI_BOOT_SERVICES_CODE &&
404 md->type != EFI_BOOT_SERVICES_DATA)
405 continue;
406 /* Only reserve where possible:
407 * - Not within any already allocated areas
408 * - Not over any memory area (really needed, if above?)
409 * - Not within any part of the kernel
410 * - Not the bios reserved area
412 if ((start+size >= virt_to_phys(_text)
413 && start <= virt_to_phys(_end)) ||
414 !e820_all_mapped(start, start+size, E820_RAM) ||
415 memblock_is_region_reserved(start, size)) {
416 /* Could not reserve, skip it */
417 md->num_pages = 0;
418 memblock_dbg("Could not reserve boot range "
419 "[0x%010llx-0x%010llx]\n",
420 start, start+size-1);
421 } else
422 memblock_reserve(start, size);
426 void __init efi_unmap_memmap(void)
428 if (memmap.map) {
429 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
430 memmap.map = NULL;
434 void __init efi_free_boot_services(void)
436 void *p;
438 if (!efi_is_native())
439 return;
441 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
442 efi_memory_desc_t *md = p;
443 unsigned long long start = md->phys_addr;
444 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
446 if (md->type != EFI_BOOT_SERVICES_CODE &&
447 md->type != EFI_BOOT_SERVICES_DATA)
448 continue;
450 /* Could not reserve boot area */
451 if (!size)
452 continue;
454 free_bootmem_late(start, size);
457 efi_unmap_memmap();
460 static int __init efi_systab_init(void *phys)
462 if (efi_64bit) {
463 efi_system_table_64_t *systab64;
464 u64 tmp = 0;
466 systab64 = early_ioremap((unsigned long)phys,
467 sizeof(*systab64));
468 if (systab64 == NULL) {
469 pr_err("Couldn't map the system table!\n");
470 return -ENOMEM;
473 efi_systab.hdr = systab64->hdr;
474 efi_systab.fw_vendor = systab64->fw_vendor;
475 tmp |= systab64->fw_vendor;
476 efi_systab.fw_revision = systab64->fw_revision;
477 efi_systab.con_in_handle = systab64->con_in_handle;
478 tmp |= systab64->con_in_handle;
479 efi_systab.con_in = systab64->con_in;
480 tmp |= systab64->con_in;
481 efi_systab.con_out_handle = systab64->con_out_handle;
482 tmp |= systab64->con_out_handle;
483 efi_systab.con_out = systab64->con_out;
484 tmp |= systab64->con_out;
485 efi_systab.stderr_handle = systab64->stderr_handle;
486 tmp |= systab64->stderr_handle;
487 efi_systab.stderr = systab64->stderr;
488 tmp |= systab64->stderr;
489 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
490 tmp |= systab64->runtime;
491 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
492 tmp |= systab64->boottime;
493 efi_systab.nr_tables = systab64->nr_tables;
494 efi_systab.tables = systab64->tables;
495 tmp |= systab64->tables;
497 early_iounmap(systab64, sizeof(*systab64));
498 #ifdef CONFIG_X86_32
499 if (tmp >> 32) {
500 pr_err("EFI data located above 4GB, disabling EFI.\n");
501 return -EINVAL;
503 #endif
504 } else {
505 efi_system_table_32_t *systab32;
507 systab32 = early_ioremap((unsigned long)phys,
508 sizeof(*systab32));
509 if (systab32 == NULL) {
510 pr_err("Couldn't map the system table!\n");
511 return -ENOMEM;
514 efi_systab.hdr = systab32->hdr;
515 efi_systab.fw_vendor = systab32->fw_vendor;
516 efi_systab.fw_revision = systab32->fw_revision;
517 efi_systab.con_in_handle = systab32->con_in_handle;
518 efi_systab.con_in = systab32->con_in;
519 efi_systab.con_out_handle = systab32->con_out_handle;
520 efi_systab.con_out = systab32->con_out;
521 efi_systab.stderr_handle = systab32->stderr_handle;
522 efi_systab.stderr = systab32->stderr;
523 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
524 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
525 efi_systab.nr_tables = systab32->nr_tables;
526 efi_systab.tables = systab32->tables;
528 early_iounmap(systab32, sizeof(*systab32));
531 efi.systab = &efi_systab;
534 * Verify the EFI Table
536 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
537 pr_err("System table signature incorrect!\n");
538 return -EINVAL;
540 if ((efi.systab->hdr.revision >> 16) == 0)
541 pr_err("Warning: System table version "
542 "%d.%02d, expected 1.00 or greater!\n",
543 efi.systab->hdr.revision >> 16,
544 efi.systab->hdr.revision & 0xffff);
546 return 0;
549 static int __init efi_config_init(u64 tables, int nr_tables)
551 void *config_tables, *tablep;
552 int i, sz;
554 if (efi_64bit)
555 sz = sizeof(efi_config_table_64_t);
556 else
557 sz = sizeof(efi_config_table_32_t);
560 * Let's see what config tables the firmware passed to us.
562 config_tables = early_ioremap(tables, nr_tables * sz);
563 if (config_tables == NULL) {
564 pr_err("Could not map Configuration table!\n");
565 return -ENOMEM;
568 tablep = config_tables;
569 pr_info("");
570 for (i = 0; i < efi.systab->nr_tables; i++) {
571 efi_guid_t guid;
572 unsigned long table;
574 if (efi_64bit) {
575 u64 table64;
576 guid = ((efi_config_table_64_t *)tablep)->guid;
577 table64 = ((efi_config_table_64_t *)tablep)->table;
578 table = table64;
579 #ifdef CONFIG_X86_32
580 if (table64 >> 32) {
581 pr_cont("\n");
582 pr_err("Table located above 4GB, disabling EFI.\n");
583 early_iounmap(config_tables,
584 efi.systab->nr_tables * sz);
585 return -EINVAL;
587 #endif
588 } else {
589 guid = ((efi_config_table_32_t *)tablep)->guid;
590 table = ((efi_config_table_32_t *)tablep)->table;
592 if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
593 efi.mps = table;
594 pr_cont(" MPS=0x%lx ", table);
595 } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
596 efi.acpi20 = table;
597 pr_cont(" ACPI 2.0=0x%lx ", table);
598 } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
599 efi.acpi = table;
600 pr_cont(" ACPI=0x%lx ", table);
601 } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
602 efi.smbios = table;
603 pr_cont(" SMBIOS=0x%lx ", table);
604 #ifdef CONFIG_X86_UV
605 } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
606 efi.uv_systab = table;
607 pr_cont(" UVsystab=0x%lx ", table);
608 #endif
609 } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
610 efi.hcdp = table;
611 pr_cont(" HCDP=0x%lx ", table);
612 } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
613 efi.uga = table;
614 pr_cont(" UGA=0x%lx ", table);
616 tablep += sz;
618 pr_cont("\n");
619 early_iounmap(config_tables, efi.systab->nr_tables * sz);
620 return 0;
623 static int __init efi_runtime_init(void)
625 efi_runtime_services_t *runtime;
628 * Check out the runtime services table. We need to map
629 * the runtime services table so that we can grab the physical
630 * address of several of the EFI runtime functions, needed to
631 * set the firmware into virtual mode.
633 runtime = early_ioremap((unsigned long)efi.systab->runtime,
634 sizeof(efi_runtime_services_t));
635 if (!runtime) {
636 pr_err("Could not map the runtime service table!\n");
637 return -ENOMEM;
640 * We will only need *early* access to the following
641 * two EFI runtime services before set_virtual_address_map
642 * is invoked.
644 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
645 efi_phys.set_virtual_address_map =
646 (efi_set_virtual_address_map_t *)
647 runtime->set_virtual_address_map;
649 * Make efi_get_time can be called before entering
650 * virtual mode.
652 efi.get_time = phys_efi_get_time;
653 early_iounmap(runtime, sizeof(efi_runtime_services_t));
655 return 0;
658 static int __init efi_memmap_init(void)
660 /* Map the EFI memory map */
661 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
662 memmap.nr_map * memmap.desc_size);
663 if (memmap.map == NULL) {
664 pr_err("Could not map the memory map!\n");
665 return -ENOMEM;
667 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
669 if (add_efi_memmap)
670 do_add_efi_memmap();
672 return 0;
675 void __init efi_init(void)
677 efi_char16_t *c16;
678 char vendor[100] = "unknown";
679 int i = 0;
680 void *tmp;
682 #ifdef CONFIG_X86_32
683 if (boot_params.efi_info.efi_systab_hi ||
684 boot_params.efi_info.efi_memmap_hi) {
685 pr_info("Table located above 4GB, disabling EFI.\n");
686 efi_enabled = 0;
687 return;
689 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
690 #else
691 efi_phys.systab = (efi_system_table_t *)
692 (boot_params.efi_info.efi_systab |
693 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
694 #endif
696 if (efi_systab_init(efi_phys.systab)) {
697 efi_enabled = 0;
698 return;
702 * Show what we know for posterity
704 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
705 if (c16) {
706 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
707 vendor[i] = *c16++;
708 vendor[i] = '\0';
709 } else
710 pr_err("Could not map the firmware vendor!\n");
711 early_iounmap(tmp, 2);
713 pr_info("EFI v%u.%.02u by %s\n",
714 efi.systab->hdr.revision >> 16,
715 efi.systab->hdr.revision & 0xffff, vendor);
717 if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) {
718 efi_enabled = 0;
719 return;
723 * Note: We currently don't support runtime services on an EFI
724 * that doesn't match the kernel 32/64-bit mode.
727 if (!efi_is_native())
728 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
729 else if (efi_runtime_init()) {
730 efi_enabled = 0;
731 return;
734 if (efi_memmap_init()) {
735 efi_enabled = 0;
736 return;
738 #ifdef CONFIG_X86_32
739 if (efi_is_native()) {
740 x86_platform.get_wallclock = efi_get_time;
741 x86_platform.set_wallclock = efi_set_rtc_mmss;
743 #endif
745 #if EFI_DEBUG
746 print_efi_memmap();
747 #endif
750 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
752 u64 addr, npages;
754 addr = md->virt_addr;
755 npages = md->num_pages;
757 memrange_efi_to_native(&addr, &npages);
759 if (executable)
760 set_memory_x(addr, npages);
761 else
762 set_memory_nx(addr, npages);
765 static void __init runtime_code_page_mkexec(void)
767 efi_memory_desc_t *md;
768 void *p;
770 /* Make EFI runtime service code area executable */
771 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
772 md = p;
774 if (md->type != EFI_RUNTIME_SERVICES_CODE)
775 continue;
777 efi_set_executable(md, true);
782 * This function will switch the EFI runtime services to virtual mode.
783 * Essentially, look through the EFI memmap and map every region that
784 * has the runtime attribute bit set in its memory descriptor and update
785 * that memory descriptor with the virtual address obtained from ioremap().
786 * This enables the runtime services to be called without having to
787 * thunk back into physical mode for every invocation.
789 void __init efi_enter_virtual_mode(void)
791 efi_memory_desc_t *md, *prev_md = NULL;
792 efi_status_t status;
793 unsigned long size;
794 u64 end, systab, addr, npages, end_pfn;
795 void *p, *va, *new_memmap = NULL;
796 int count = 0;
798 efi.systab = NULL;
801 * We don't do virtual mode, since we don't do runtime services, on
802 * non-native EFI
805 if (!efi_is_native()) {
806 efi_unmap_memmap();
807 return;
810 /* Merge contiguous regions of the same type and attribute */
811 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
812 u64 prev_size;
813 md = p;
815 if (!prev_md) {
816 prev_md = md;
817 continue;
820 if (prev_md->type != md->type ||
821 prev_md->attribute != md->attribute) {
822 prev_md = md;
823 continue;
826 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
828 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
829 prev_md->num_pages += md->num_pages;
830 md->type = EFI_RESERVED_TYPE;
831 md->attribute = 0;
832 continue;
834 prev_md = md;
837 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
838 md = p;
839 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
840 md->type != EFI_BOOT_SERVICES_CODE &&
841 md->type != EFI_BOOT_SERVICES_DATA)
842 continue;
844 size = md->num_pages << EFI_PAGE_SHIFT;
845 end = md->phys_addr + size;
847 end_pfn = PFN_UP(end);
848 if (end_pfn <= max_low_pfn_mapped
849 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
850 && end_pfn <= max_pfn_mapped))
851 va = __va(md->phys_addr);
852 else
853 va = efi_ioremap(md->phys_addr, size, md->type);
855 md->virt_addr = (u64) (unsigned long) va;
857 if (!va) {
858 pr_err("ioremap of 0x%llX failed!\n",
859 (unsigned long long)md->phys_addr);
860 continue;
863 if (!(md->attribute & EFI_MEMORY_WB)) {
864 addr = md->virt_addr;
865 npages = md->num_pages;
866 memrange_efi_to_native(&addr, &npages);
867 set_memory_uc(addr, npages);
870 systab = (u64) (unsigned long) efi_phys.systab;
871 if (md->phys_addr <= systab && systab < end) {
872 systab += md->virt_addr - md->phys_addr;
873 efi.systab = (efi_system_table_t *) (unsigned long) systab;
875 new_memmap = krealloc(new_memmap,
876 (count + 1) * memmap.desc_size,
877 GFP_KERNEL);
878 memcpy(new_memmap + (count * memmap.desc_size), md,
879 memmap.desc_size);
880 count++;
883 BUG_ON(!efi.systab);
885 status = phys_efi_set_virtual_address_map(
886 memmap.desc_size * count,
887 memmap.desc_size,
888 memmap.desc_version,
889 (efi_memory_desc_t *)__pa(new_memmap));
891 if (status != EFI_SUCCESS) {
892 pr_alert("Unable to switch EFI into virtual mode "
893 "(status=%lx)!\n", status);
894 panic("EFI call to SetVirtualAddressMap() failed!");
898 * Now that EFI is in virtual mode, update the function
899 * pointers in the runtime service table to the new virtual addresses.
901 * Call EFI services through wrapper functions.
903 efi.runtime_version = efi_systab.fw_revision;
904 efi.get_time = virt_efi_get_time;
905 efi.set_time = virt_efi_set_time;
906 efi.get_wakeup_time = virt_efi_get_wakeup_time;
907 efi.set_wakeup_time = virt_efi_set_wakeup_time;
908 efi.get_variable = virt_efi_get_variable;
909 efi.get_next_variable = virt_efi_get_next_variable;
910 efi.set_variable = virt_efi_set_variable;
911 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
912 efi.reset_system = virt_efi_reset_system;
913 efi.set_virtual_address_map = NULL;
914 efi.query_variable_info = virt_efi_query_variable_info;
915 efi.update_capsule = virt_efi_update_capsule;
916 efi.query_capsule_caps = virt_efi_query_capsule_caps;
917 if (__supported_pte_mask & _PAGE_NX)
918 runtime_code_page_mkexec();
920 kfree(new_memmap);
924 * Convenience functions to obtain memory types and attributes
926 u32 efi_mem_type(unsigned long phys_addr)
928 efi_memory_desc_t *md;
929 void *p;
931 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
932 md = p;
933 if ((md->phys_addr <= phys_addr) &&
934 (phys_addr < (md->phys_addr +
935 (md->num_pages << EFI_PAGE_SHIFT))))
936 return md->type;
938 return 0;
941 u64 efi_mem_attributes(unsigned long phys_addr)
943 efi_memory_desc_t *md;
944 void *p;
946 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
947 md = p;
948 if ((md->phys_addr <= phys_addr) &&
949 (phys_addr < (md->phys_addr +
950 (md->num_pages << EFI_PAGE_SHIFT))))
951 return md->attribute;
953 return 0;