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_get_next_high_mono_count(u32 *count)
136 return efi_call_virt1(get_next_high_mono_count, count);
139 static void virt_efi_reset_system(int reset_type,
141 unsigned long data_size,
144 efi_call_virt4(reset_system, reset_type, status,
148 static efi_status_t virt_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)
154 return efi_call_virt4(set_virtual_address_map,
155 memory_map_size, descriptor_size,
156 descriptor_version, virtual_map);
159 static efi_status_t __init phys_efi_set_virtual_address_map(
160 unsigned long memory_map_size,
161 unsigned long descriptor_size,
162 u32 descriptor_version,
163 efi_memory_desc_t *virtual_map)
167 efi_call_phys_prelog();
168 status = efi_call_phys4(efi_phys.set_virtual_address_map,
169 memory_map_size, descriptor_size,
170 descriptor_version, virtual_map);
171 efi_call_phys_epilog();
175 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
180 efi_call_phys_prelog();
181 status = efi_call_phys2(efi_phys.get_time, tm, tc);
182 efi_call_phys_epilog();
186 int efi_set_rtc_mmss(unsigned long nowtime)
188 int real_seconds, real_minutes;
193 status = efi.get_time(&eft, &cap);
194 if (status != EFI_SUCCESS) {
195 printk(KERN_ERR "Oops: efitime: can't read time!\n");
199 real_seconds = nowtime % 60;
200 real_minutes = nowtime / 60;
201 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
204 eft.minute = real_minutes;
205 eft.second = real_seconds;
207 status = efi.set_time(&eft);
208 if (status != EFI_SUCCESS) {
209 printk(KERN_ERR "Oops: efitime: can't write time!\n");
215 unsigned long efi_get_time(void)
221 status = efi.get_time(&eft, &cap);
222 if (status != EFI_SUCCESS)
223 printk(KERN_ERR "Oops: efitime: can't read time!\n");
225 return mktime(eft.year, eft.month, eft.day, eft.hour,
226 eft.minute, eft.second);
230 * Tell the kernel about the EFI memory map. This might include
231 * more than the max 128 entries that can fit in the e820 legacy
232 * (zeropage) memory map.
235 static void __init do_add_efi_memmap(void)
239 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
240 efi_memory_desc_t *md = p;
241 unsigned long long start = md->phys_addr;
242 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
246 case EFI_LOADER_CODE:
247 case EFI_LOADER_DATA:
248 case EFI_BOOT_SERVICES_CODE:
249 case EFI_BOOT_SERVICES_DATA:
250 case EFI_CONVENTIONAL_MEMORY:
251 if (md->attribute & EFI_MEMORY_WB)
252 e820_type = E820_RAM;
254 e820_type = E820_RESERVED;
256 case EFI_ACPI_RECLAIM_MEMORY:
257 e820_type = E820_ACPI;
259 case EFI_ACPI_MEMORY_NVS:
260 e820_type = E820_NVS;
262 case EFI_UNUSABLE_MEMORY:
263 e820_type = E820_UNUSABLE;
267 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
268 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
269 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
271 e820_type = E820_RESERVED;
274 e820_add_region(start, size, e820_type);
276 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
279 void __init efi_memblock_x86_reserve_range(void)
284 pmap = boot_params.efi_info.efi_memmap;
286 pmap = (boot_params.efi_info.efi_memmap |
287 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
289 memmap.phys_map = (void *)pmap;
290 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
291 boot_params.efi_info.efi_memdesc_size;
292 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
293 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
294 memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
299 static void __init print_efi_memmap(void)
301 efi_memory_desc_t *md;
305 for (p = memmap.map, i = 0;
307 p += memmap.desc_size, i++) {
309 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
310 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
311 i, md->type, md->attribute, md->phys_addr,
312 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
313 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
316 #endif /* EFI_DEBUG */
318 void __init efi_init(void)
320 efi_config_table_t *config_tables;
321 efi_runtime_services_t *runtime;
323 char vendor[100] = "unknown";
328 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
330 efi_phys.systab = (efi_system_table_t *)
331 (boot_params.efi_info.efi_systab |
332 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
335 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
336 sizeof(efi_system_table_t));
337 if (efi.systab == NULL)
338 printk(KERN_ERR "Couldn't map the EFI system table!\n");
339 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
340 early_iounmap(efi.systab, sizeof(efi_system_table_t));
341 efi.systab = &efi_systab;
344 * Verify the EFI Table
346 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
347 printk(KERN_ERR "EFI system table signature incorrect!\n");
348 if ((efi.systab->hdr.revision >> 16) == 0)
349 printk(KERN_ERR "Warning: EFI system table version "
350 "%d.%02d, expected 1.00 or greater!\n",
351 efi.systab->hdr.revision >> 16,
352 efi.systab->hdr.revision & 0xffff);
355 * Show what we know for posterity
357 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
359 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
363 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
364 early_iounmap(tmp, 2);
366 printk(KERN_INFO "EFI v%u.%.02u by %s\n",
367 efi.systab->hdr.revision >> 16,
368 efi.systab->hdr.revision & 0xffff, vendor);
371 * Let's see what config tables the firmware passed to us.
373 config_tables = early_ioremap(
375 efi.systab->nr_tables * sizeof(efi_config_table_t));
376 if (config_tables == NULL)
377 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
380 for (i = 0; i < efi.systab->nr_tables; i++) {
381 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
382 efi.mps = config_tables[i].table;
383 printk(" MPS=0x%lx ", config_tables[i].table);
384 } else if (!efi_guidcmp(config_tables[i].guid,
385 ACPI_20_TABLE_GUID)) {
386 efi.acpi20 = config_tables[i].table;
387 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
388 } else if (!efi_guidcmp(config_tables[i].guid,
390 efi.acpi = config_tables[i].table;
391 printk(" ACPI=0x%lx ", config_tables[i].table);
392 } else if (!efi_guidcmp(config_tables[i].guid,
393 SMBIOS_TABLE_GUID)) {
394 efi.smbios = config_tables[i].table;
395 printk(" SMBIOS=0x%lx ", config_tables[i].table);
397 } else if (!efi_guidcmp(config_tables[i].guid,
398 UV_SYSTEM_TABLE_GUID)) {
399 efi.uv_systab = config_tables[i].table;
400 printk(" UVsystab=0x%lx ", config_tables[i].table);
402 } else if (!efi_guidcmp(config_tables[i].guid,
404 efi.hcdp = config_tables[i].table;
405 printk(" HCDP=0x%lx ", config_tables[i].table);
406 } else if (!efi_guidcmp(config_tables[i].guid,
407 UGA_IO_PROTOCOL_GUID)) {
408 efi.uga = config_tables[i].table;
409 printk(" UGA=0x%lx ", config_tables[i].table);
413 early_iounmap(config_tables,
414 efi.systab->nr_tables * sizeof(efi_config_table_t));
417 * Check out the runtime services table. We need to map
418 * the runtime services table so that we can grab the physical
419 * address of several of the EFI runtime functions, needed to
420 * set the firmware into virtual mode.
422 runtime = early_ioremap((unsigned long)efi.systab->runtime,
423 sizeof(efi_runtime_services_t));
424 if (runtime != NULL) {
426 * We will only need *early* access to the following
427 * two EFI runtime services before set_virtual_address_map
430 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
431 efi_phys.set_virtual_address_map =
432 (efi_set_virtual_address_map_t *)
433 runtime->set_virtual_address_map;
435 * Make efi_get_time can be called before entering
438 efi.get_time = phys_efi_get_time;
440 printk(KERN_ERR "Could not map the EFI runtime service "
442 early_iounmap(runtime, sizeof(efi_runtime_services_t));
444 /* Map the EFI memory map */
445 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
446 memmap.nr_map * memmap.desc_size);
447 if (memmap.map == NULL)
448 printk(KERN_ERR "Could not map the EFI memory map!\n");
449 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
451 if (memmap.desc_size != sizeof(efi_memory_desc_t))
453 "Kernel-defined memdesc doesn't match the one from EFI!\n");
459 x86_platform.get_wallclock = efi_get_time;
460 x86_platform.set_wallclock = efi_set_rtc_mmss;
463 /* Setup for EFI runtime service */
464 reboot_type = BOOT_EFI;
471 static void __init runtime_code_page_mkexec(void)
473 efi_memory_desc_t *md;
477 /* Make EFI runtime service code area executable */
478 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
481 if (md->type != EFI_RUNTIME_SERVICES_CODE)
484 addr = md->virt_addr;
485 npages = md->num_pages;
486 memrange_efi_to_native(&addr, &npages);
487 set_memory_x(addr, npages);
492 * This function will switch the EFI runtime services to virtual mode.
493 * Essentially, look through the EFI memmap and map every region that
494 * has the runtime attribute bit set in its memory descriptor and update
495 * that memory descriptor with the virtual address obtained from ioremap().
496 * This enables the runtime services to be called without having to
497 * thunk back into physical mode for every invocation.
499 void __init efi_enter_virtual_mode(void)
501 efi_memory_desc_t *md;
504 u64 end, systab, addr, npages, end_pfn;
508 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
510 if (!(md->attribute & EFI_MEMORY_RUNTIME))
513 size = md->num_pages << EFI_PAGE_SHIFT;
514 end = md->phys_addr + size;
516 end_pfn = PFN_UP(end);
517 if (end_pfn <= max_low_pfn_mapped
518 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
519 && end_pfn <= max_pfn_mapped))
520 va = __va(md->phys_addr);
522 va = efi_ioremap(md->phys_addr, size, md->type);
524 md->virt_addr = (u64) (unsigned long) va;
527 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
528 (unsigned long long)md->phys_addr);
532 if (!(md->attribute & EFI_MEMORY_WB)) {
533 addr = md->virt_addr;
534 npages = md->num_pages;
535 memrange_efi_to_native(&addr, &npages);
536 set_memory_uc(addr, npages);
539 systab = (u64) (unsigned long) efi_phys.systab;
540 if (md->phys_addr <= systab && systab < end) {
541 systab += md->virt_addr - md->phys_addr;
542 efi.systab = (efi_system_table_t *) (unsigned long) systab;
548 status = phys_efi_set_virtual_address_map(
549 memmap.desc_size * memmap.nr_map,
554 if (status != EFI_SUCCESS) {
555 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
556 "(status=%lx)!\n", status);
557 panic("EFI call to SetVirtualAddressMap() failed!");
561 * Now that EFI is in virtual mode, update the function
562 * pointers in the runtime service table to the new virtual addresses.
564 * Call EFI services through wrapper functions.
566 efi.get_time = virt_efi_get_time;
567 efi.set_time = virt_efi_set_time;
568 efi.get_wakeup_time = virt_efi_get_wakeup_time;
569 efi.set_wakeup_time = virt_efi_set_wakeup_time;
570 efi.get_variable = virt_efi_get_variable;
571 efi.get_next_variable = virt_efi_get_next_variable;
572 efi.set_variable = virt_efi_set_variable;
573 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
574 efi.reset_system = virt_efi_reset_system;
575 efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
576 if (__supported_pte_mask & _PAGE_NX)
577 runtime_code_page_mkexec();
578 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
583 * Convenience functions to obtain memory types and attributes
585 u32 efi_mem_type(unsigned long phys_addr)
587 efi_memory_desc_t *md;
590 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
592 if ((md->phys_addr <= phys_addr) &&
593 (phys_addr < (md->phys_addr +
594 (md->num_pages << EFI_PAGE_SHIFT))))
600 u64 efi_mem_attributes(unsigned long phys_addr)
602 efi_memory_desc_t *md;
605 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
607 if ((md->phys_addr <= phys_addr) &&
608 (phys_addr < (md->phys_addr +
609 (md->num_pages << EFI_PAGE_SHIFT))))
610 return md->attribute;