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 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/efi.h>
32 #include <linux/bootmem.h>
33 #include <linux/memblock.h>
34 #include <linux/spinlock.h>
35 #include <linux/uaccess.h>
36 #include <linux/time.h>
38 #include <linux/reboot.h>
39 #include <linux/bcd.h>
41 #include <asm/setup.h>
44 #include <asm/cacheflush.h>
45 #include <asm/tlbflush.h>
46 #include <asm/x86_init.h>
52 EXPORT_SYMBOL(efi_enabled);
57 struct efi_memory_map memmap;
59 static struct efi efi_phys __initdata;
60 static efi_system_table_t efi_systab __initdata;
62 static int __init setup_noefi(char *arg)
67 early_param("noefi", setup_noefi);
70 EXPORT_SYMBOL(add_efi_memmap);
72 static int __init setup_add_efi_memmap(char *arg)
77 early_param("add_efi_memmap", setup_add_efi_memmap);
80 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
82 return efi_call_virt2(get_time, tm, tc);
85 static efi_status_t virt_efi_set_time(efi_time_t *tm)
87 return efi_call_virt1(set_time, tm);
90 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
94 return efi_call_virt3(get_wakeup_time,
95 enabled, pending, tm);
98 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
100 return efi_call_virt2(set_wakeup_time,
104 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
107 unsigned long *data_size,
110 return efi_call_virt5(get_variable,
115 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
119 return efi_call_virt3(get_next_variable,
120 name_size, name, vendor);
123 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
126 unsigned long data_size,
129 return efi_call_virt5(set_variable,
134 static efi_status_t virt_efi_query_variable_info(u32 attr,
136 u64 *remaining_space,
137 u64 *max_variable_size)
139 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
140 return EFI_UNSUPPORTED;
142 return efi_call_virt4(query_variable_info, attr, storage_space,
143 remaining_space, max_variable_size);
146 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
148 return efi_call_virt1(get_next_high_mono_count, count);
151 static void virt_efi_reset_system(int reset_type,
153 unsigned long data_size,
156 efi_call_virt4(reset_system, reset_type, status,
160 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
162 unsigned long sg_list)
164 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
165 return EFI_UNSUPPORTED;
167 return efi_call_virt3(update_capsule, capsules, count, sg_list);
170 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
175 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
176 return EFI_UNSUPPORTED;
178 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
182 static efi_status_t __init phys_efi_set_virtual_address_map(
183 unsigned long memory_map_size,
184 unsigned long descriptor_size,
185 u32 descriptor_version,
186 efi_memory_desc_t *virtual_map)
190 efi_call_phys_prelog();
191 status = efi_call_phys4(efi_phys.set_virtual_address_map,
192 memory_map_size, descriptor_size,
193 descriptor_version, virtual_map);
194 efi_call_phys_epilog();
198 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
203 efi_call_phys_prelog();
204 status = efi_call_phys2(efi_phys.get_time, tm, tc);
205 efi_call_phys_epilog();
209 int efi_set_rtc_mmss(unsigned long nowtime)
211 int real_seconds, real_minutes;
216 status = efi.get_time(&eft, &cap);
217 if (status != EFI_SUCCESS) {
218 printk(KERN_ERR "Oops: efitime: can't read time!\n");
222 real_seconds = nowtime % 60;
223 real_minutes = nowtime / 60;
224 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
227 eft.minute = real_minutes;
228 eft.second = real_seconds;
230 status = efi.set_time(&eft);
231 if (status != EFI_SUCCESS) {
232 printk(KERN_ERR "Oops: efitime: can't write time!\n");
238 unsigned long efi_get_time(void)
244 status = efi.get_time(&eft, &cap);
245 if (status != EFI_SUCCESS)
246 printk(KERN_ERR "Oops: efitime: can't read time!\n");
248 return mktime(eft.year, eft.month, eft.day, eft.hour,
249 eft.minute, eft.second);
253 * Tell the kernel about the EFI memory map. This might include
254 * more than the max 128 entries that can fit in the e820 legacy
255 * (zeropage) memory map.
258 static void __init do_add_efi_memmap(void)
262 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
263 efi_memory_desc_t *md = p;
264 unsigned long long start = md->phys_addr;
265 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
269 case EFI_LOADER_CODE:
270 case EFI_LOADER_DATA:
271 case EFI_BOOT_SERVICES_CODE:
272 case EFI_BOOT_SERVICES_DATA:
273 case EFI_CONVENTIONAL_MEMORY:
274 if (md->attribute & EFI_MEMORY_WB)
275 e820_type = E820_RAM;
277 e820_type = E820_RESERVED;
279 case EFI_ACPI_RECLAIM_MEMORY:
280 e820_type = E820_ACPI;
282 case EFI_ACPI_MEMORY_NVS:
283 e820_type = E820_NVS;
285 case EFI_UNUSABLE_MEMORY:
286 e820_type = E820_UNUSABLE;
290 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
291 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
292 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
294 e820_type = E820_RESERVED;
297 e820_add_region(start, size, e820_type);
299 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
302 void __init efi_memblock_x86_reserve_range(void)
307 pmap = boot_params.efi_info.efi_memmap;
309 pmap = (boot_params.efi_info.efi_memmap |
310 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
312 memmap.phys_map = (void *)pmap;
313 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
314 boot_params.efi_info.efi_memdesc_size;
315 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
316 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
317 memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
322 static void __init print_efi_memmap(void)
324 efi_memory_desc_t *md;
328 for (p = memmap.map, i = 0;
330 p += memmap.desc_size, i++) {
332 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
333 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
334 i, md->type, md->attribute, md->phys_addr,
335 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
336 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
339 #endif /* EFI_DEBUG */
341 void __init efi_reserve_boot_services(void)
345 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
346 efi_memory_desc_t *md = p;
347 unsigned long long start = md->phys_addr;
348 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
350 if (md->type != EFI_BOOT_SERVICES_CODE &&
351 md->type != EFI_BOOT_SERVICES_DATA)
354 memblock_x86_reserve_range(start, start + size, "EFI Boot");
358 static void __init efi_free_boot_services(void)
362 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
363 efi_memory_desc_t *md = p;
364 unsigned long long start = md->phys_addr;
365 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
367 if (md->type != EFI_BOOT_SERVICES_CODE &&
368 md->type != EFI_BOOT_SERVICES_DATA)
371 free_bootmem_late(start, size);
375 void __init efi_init(void)
377 efi_config_table_t *config_tables;
378 efi_runtime_services_t *runtime;
380 char vendor[100] = "unknown";
385 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
387 efi_phys.systab = (efi_system_table_t *)
388 (boot_params.efi_info.efi_systab |
389 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
392 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
393 sizeof(efi_system_table_t));
394 if (efi.systab == NULL)
395 printk(KERN_ERR "Couldn't map the EFI system table!\n");
396 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
397 early_iounmap(efi.systab, sizeof(efi_system_table_t));
398 efi.systab = &efi_systab;
401 * Verify the EFI Table
403 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
404 printk(KERN_ERR "EFI system table signature incorrect!\n");
405 if ((efi.systab->hdr.revision >> 16) == 0)
406 printk(KERN_ERR "Warning: EFI system table version "
407 "%d.%02d, expected 1.00 or greater!\n",
408 efi.systab->hdr.revision >> 16,
409 efi.systab->hdr.revision & 0xffff);
412 * Show what we know for posterity
414 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
416 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
420 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
421 early_iounmap(tmp, 2);
423 printk(KERN_INFO "EFI v%u.%.02u by %s\n",
424 efi.systab->hdr.revision >> 16,
425 efi.systab->hdr.revision & 0xffff, vendor);
428 * Let's see what config tables the firmware passed to us.
430 config_tables = early_ioremap(
432 efi.systab->nr_tables * sizeof(efi_config_table_t));
433 if (config_tables == NULL)
434 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
437 for (i = 0; i < efi.systab->nr_tables; i++) {
438 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
439 efi.mps = config_tables[i].table;
440 printk(" MPS=0x%lx ", config_tables[i].table);
441 } else if (!efi_guidcmp(config_tables[i].guid,
442 ACPI_20_TABLE_GUID)) {
443 efi.acpi20 = config_tables[i].table;
444 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
445 } else if (!efi_guidcmp(config_tables[i].guid,
447 efi.acpi = config_tables[i].table;
448 printk(" ACPI=0x%lx ", config_tables[i].table);
449 } else if (!efi_guidcmp(config_tables[i].guid,
450 SMBIOS_TABLE_GUID)) {
451 efi.smbios = config_tables[i].table;
452 printk(" SMBIOS=0x%lx ", config_tables[i].table);
454 } else if (!efi_guidcmp(config_tables[i].guid,
455 UV_SYSTEM_TABLE_GUID)) {
456 efi.uv_systab = config_tables[i].table;
457 printk(" UVsystab=0x%lx ", config_tables[i].table);
459 } else if (!efi_guidcmp(config_tables[i].guid,
461 efi.hcdp = config_tables[i].table;
462 printk(" HCDP=0x%lx ", config_tables[i].table);
463 } else if (!efi_guidcmp(config_tables[i].guid,
464 UGA_IO_PROTOCOL_GUID)) {
465 efi.uga = config_tables[i].table;
466 printk(" UGA=0x%lx ", config_tables[i].table);
470 early_iounmap(config_tables,
471 efi.systab->nr_tables * sizeof(efi_config_table_t));
474 * Check out the runtime services table. We need to map
475 * the runtime services table so that we can grab the physical
476 * address of several of the EFI runtime functions, needed to
477 * set the firmware into virtual mode.
479 runtime = early_ioremap((unsigned long)efi.systab->runtime,
480 sizeof(efi_runtime_services_t));
481 if (runtime != NULL) {
483 * We will only need *early* access to the following
484 * two EFI runtime services before set_virtual_address_map
487 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
488 efi_phys.set_virtual_address_map =
489 (efi_set_virtual_address_map_t *)
490 runtime->set_virtual_address_map;
492 * Make efi_get_time can be called before entering
495 efi.get_time = phys_efi_get_time;
497 printk(KERN_ERR "Could not map the EFI runtime service "
499 early_iounmap(runtime, sizeof(efi_runtime_services_t));
501 /* Map the EFI memory map */
502 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
503 memmap.nr_map * memmap.desc_size);
504 if (memmap.map == NULL)
505 printk(KERN_ERR "Could not map the EFI memory map!\n");
506 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
508 if (memmap.desc_size != sizeof(efi_memory_desc_t))
510 "Kernel-defined memdesc doesn't match the one from EFI!\n");
516 x86_platform.get_wallclock = efi_get_time;
517 x86_platform.set_wallclock = efi_set_rtc_mmss;
520 /* Setup for EFI runtime service */
521 reboot_type = BOOT_EFI;
528 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
532 addr = md->virt_addr;
533 npages = md->num_pages;
535 memrange_efi_to_native(&addr, &npages);
538 set_memory_x(addr, npages);
540 set_memory_nx(addr, npages);
543 static void __init runtime_code_page_mkexec(void)
545 efi_memory_desc_t *md;
548 /* Make EFI runtime service code area executable */
549 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
552 if (md->type != EFI_RUNTIME_SERVICES_CODE)
555 efi_set_executable(md, true);
560 * This function will switch the EFI runtime services to virtual mode.
561 * Essentially, look through the EFI memmap and map every region that
562 * has the runtime attribute bit set in its memory descriptor and update
563 * that memory descriptor with the virtual address obtained from ioremap().
564 * This enables the runtime services to be called without having to
565 * thunk back into physical mode for every invocation.
567 void __init efi_enter_virtual_mode(void)
569 efi_memory_desc_t *md, *prev_md = NULL;
572 u64 end, systab, addr, npages, end_pfn;
573 void *p, *va, *new_memmap = NULL;
578 /* Merge contiguous regions of the same type and attribute */
579 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
588 if (prev_md->type != md->type ||
589 prev_md->attribute != md->attribute) {
594 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
596 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
597 prev_md->num_pages += md->num_pages;
598 md->type = EFI_RESERVED_TYPE;
605 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
607 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
608 md->type != EFI_BOOT_SERVICES_CODE &&
609 md->type != EFI_BOOT_SERVICES_DATA)
612 size = md->num_pages << EFI_PAGE_SHIFT;
613 end = md->phys_addr + size;
615 end_pfn = PFN_UP(end);
616 if (end_pfn <= max_low_pfn_mapped
617 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
618 && end_pfn <= max_pfn_mapped))
619 va = __va(md->phys_addr);
621 va = efi_ioremap(md->phys_addr, size, md->type);
623 md->virt_addr = (u64) (unsigned long) va;
626 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
627 (unsigned long long)md->phys_addr);
631 if (!(md->attribute & EFI_MEMORY_WB)) {
632 addr = md->virt_addr;
633 npages = md->num_pages;
634 memrange_efi_to_native(&addr, &npages);
635 set_memory_uc(addr, npages);
638 systab = (u64) (unsigned long) efi_phys.systab;
639 if (md->phys_addr <= systab && systab < end) {
640 systab += md->virt_addr - md->phys_addr;
641 efi.systab = (efi_system_table_t *) (unsigned long) systab;
643 new_memmap = krealloc(new_memmap,
644 (count + 1) * memmap.desc_size,
646 memcpy(new_memmap + (count * memmap.desc_size), md,
653 status = phys_efi_set_virtual_address_map(
654 memmap.desc_size * count,
657 (efi_memory_desc_t *)__pa(new_memmap));
659 if (status != EFI_SUCCESS) {
660 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
661 "(status=%lx)!\n", status);
662 panic("EFI call to SetVirtualAddressMap() failed!");
666 * Thankfully, it does seem that no runtime services other than
667 * SetVirtualAddressMap() will touch boot services code, so we can
668 * get rid of it all at this point
670 efi_free_boot_services();
673 * Now that EFI is in virtual mode, update the function
674 * pointers in the runtime service table to the new virtual addresses.
676 * Call EFI services through wrapper functions.
678 efi.get_time = virt_efi_get_time;
679 efi.set_time = virt_efi_set_time;
680 efi.get_wakeup_time = virt_efi_get_wakeup_time;
681 efi.set_wakeup_time = virt_efi_set_wakeup_time;
682 efi.get_variable = virt_efi_get_variable;
683 efi.get_next_variable = virt_efi_get_next_variable;
684 efi.set_variable = virt_efi_set_variable;
685 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
686 efi.reset_system = virt_efi_reset_system;
687 efi.set_virtual_address_map = NULL;
688 efi.query_variable_info = virt_efi_query_variable_info;
689 efi.update_capsule = virt_efi_update_capsule;
690 efi.query_capsule_caps = virt_efi_query_capsule_caps;
691 if (__supported_pte_mask & _PAGE_NX)
692 runtime_code_page_mkexec();
693 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
699 * Convenience functions to obtain memory types and attributes
701 u32 efi_mem_type(unsigned long phys_addr)
703 efi_memory_desc_t *md;
706 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
708 if ((md->phys_addr <= phys_addr) &&
709 (phys_addr < (md->phys_addr +
710 (md->num_pages << EFI_PAGE_SHIFT))))
716 u64 efi_mem_attributes(unsigned long phys_addr)
718 efi_memory_desc_t *md;
721 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
723 if ((md->phys_addr <= phys_addr) &&
724 (phys_addr < (md->phys_addr +
725 (md->num_pages << EFI_PAGE_SHIFT))))
726 return md->attribute;