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/spinlock.h>
34 #include <linux/uaccess.h>
35 #include <linux/time.h>
37 #include <linux/reboot.h>
38 #include <linux/bcd.h>
40 #include <asm/setup.h>
48 EXPORT_SYMBOL(efi_enabled);
53 struct efi_memory_map memmap;
55 struct efi efi_phys __initdata;
56 static efi_system_table_t efi_systab __initdata;
58 static int __init setup_noefi(char *arg)
63 early_param("noefi", setup_noefi);
65 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
67 return efi_call_virt2(get_time, tm, tc);
70 static efi_status_t virt_efi_set_time(efi_time_t *tm)
72 return efi_call_virt1(set_time, tm);
75 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
79 return efi_call_virt3(get_wakeup_time,
80 enabled, pending, tm);
83 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
85 return efi_call_virt2(set_wakeup_time,
89 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
92 unsigned long *data_size,
95 return efi_call_virt5(get_variable,
100 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
104 return efi_call_virt3(get_next_variable,
105 name_size, name, vendor);
108 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
111 unsigned long data_size,
114 return efi_call_virt5(set_variable,
119 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
121 return efi_call_virt1(get_next_high_mono_count, count);
124 static void virt_efi_reset_system(int reset_type,
126 unsigned long data_size,
129 efi_call_virt4(reset_system, reset_type, status,
133 static efi_status_t virt_efi_set_virtual_address_map(
134 unsigned long memory_map_size,
135 unsigned long descriptor_size,
136 u32 descriptor_version,
137 efi_memory_desc_t *virtual_map)
139 return efi_call_virt4(set_virtual_address_map,
140 memory_map_size, descriptor_size,
141 descriptor_version, virtual_map);
144 static efi_status_t __init phys_efi_set_virtual_address_map(
145 unsigned long memory_map_size,
146 unsigned long descriptor_size,
147 u32 descriptor_version,
148 efi_memory_desc_t *virtual_map)
152 efi_call_phys_prelog();
153 status = efi_call_phys4(efi_phys.set_virtual_address_map,
154 memory_map_size, descriptor_size,
155 descriptor_version, virtual_map);
156 efi_call_phys_epilog();
160 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
165 efi_call_phys_prelog();
166 status = efi_call_phys2(efi_phys.get_time, tm, tc);
167 efi_call_phys_epilog();
171 int efi_set_rtc_mmss(unsigned long nowtime)
173 int real_seconds, real_minutes;
178 status = efi.get_time(&eft, &cap);
179 if (status != EFI_SUCCESS) {
180 printk(KERN_ERR "Oops: efitime: can't read time!\n");
184 real_seconds = nowtime % 60;
185 real_minutes = nowtime / 60;
186 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
189 eft.minute = real_minutes;
190 eft.second = real_seconds;
192 status = efi.set_time(&eft);
193 if (status != EFI_SUCCESS) {
194 printk(KERN_ERR "Oops: efitime: can't write time!\n");
200 unsigned long efi_get_time(void)
206 status = efi.get_time(&eft, &cap);
207 if (status != EFI_SUCCESS)
208 printk(KERN_ERR "Oops: efitime: can't read time!\n");
210 return mktime(eft.year, eft.month, eft.day, eft.hour,
211 eft.minute, eft.second);
215 static void __init print_efi_memmap(void)
217 efi_memory_desc_t *md;
221 for (p = memmap.map, i = 0;
223 p += memmap.desc_size, i++) {
225 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
226 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
227 i, md->type, md->attribute, md->phys_addr,
228 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
229 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
232 #endif /* EFI_DEBUG */
234 void __init efi_init(void)
236 efi_config_table_t *config_tables;
237 efi_runtime_services_t *runtime;
239 char vendor[100] = "unknown";
244 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
245 memmap.phys_map = (void *)boot_params.efi_info.efi_memmap;
247 efi_phys.systab = (efi_system_table_t *)
248 (boot_params.efi_info.efi_systab |
249 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
250 memmap.phys_map = (void *)
251 (boot_params.efi_info.efi_memmap |
252 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
254 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
255 boot_params.efi_info.efi_memdesc_size;
256 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
257 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
259 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
260 sizeof(efi_system_table_t));
261 if (efi.systab == NULL)
262 printk(KERN_ERR "Couldn't map the EFI system table!\n");
263 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
264 early_iounmap(efi.systab, sizeof(efi_system_table_t));
265 efi.systab = &efi_systab;
268 * Verify the EFI Table
270 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
271 printk(KERN_ERR "EFI system table signature incorrect!\n");
272 if ((efi.systab->hdr.revision >> 16) == 0)
273 printk(KERN_ERR "Warning: EFI system table version "
274 "%d.%02d, expected 1.00 or greater!\n",
275 efi.systab->hdr.revision >> 16,
276 efi.systab->hdr.revision & 0xffff);
279 * Show what we know for posterity
281 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
283 for (i = 0; i < sizeof(vendor) && *c16; ++i)
287 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
288 early_iounmap(tmp, 2);
290 printk(KERN_INFO "EFI v%u.%.02u by %s \n",
291 efi.systab->hdr.revision >> 16,
292 efi.systab->hdr.revision & 0xffff, vendor);
295 * Let's see what config tables the firmware passed to us.
297 config_tables = early_ioremap(
299 efi.systab->nr_tables * sizeof(efi_config_table_t));
300 if (config_tables == NULL)
301 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
304 for (i = 0; i < efi.systab->nr_tables; i++) {
305 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
306 efi.mps = config_tables[i].table;
307 printk(" MPS=0x%lx ", config_tables[i].table);
308 } else if (!efi_guidcmp(config_tables[i].guid,
309 ACPI_20_TABLE_GUID)) {
310 efi.acpi20 = config_tables[i].table;
311 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
312 } else if (!efi_guidcmp(config_tables[i].guid,
314 efi.acpi = config_tables[i].table;
315 printk(" ACPI=0x%lx ", config_tables[i].table);
316 } else if (!efi_guidcmp(config_tables[i].guid,
317 SMBIOS_TABLE_GUID)) {
318 efi.smbios = config_tables[i].table;
319 printk(" SMBIOS=0x%lx ", config_tables[i].table);
320 } else if (!efi_guidcmp(config_tables[i].guid,
322 efi.hcdp = config_tables[i].table;
323 printk(" HCDP=0x%lx ", config_tables[i].table);
324 } else if (!efi_guidcmp(config_tables[i].guid,
325 UGA_IO_PROTOCOL_GUID)) {
326 efi.uga = config_tables[i].table;
327 printk(" UGA=0x%lx ", config_tables[i].table);
331 early_iounmap(config_tables,
332 efi.systab->nr_tables * sizeof(efi_config_table_t));
335 * Check out the runtime services table. We need to map
336 * the runtime services table so that we can grab the physical
337 * address of several of the EFI runtime functions, needed to
338 * set the firmware into virtual mode.
340 runtime = early_ioremap((unsigned long)efi.systab->runtime,
341 sizeof(efi_runtime_services_t));
342 if (runtime != NULL) {
344 * We will only need *early* access to the following
345 * two EFI runtime services before set_virtual_address_map
348 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
349 efi_phys.set_virtual_address_map =
350 (efi_set_virtual_address_map_t *)
351 runtime->set_virtual_address_map;
353 * Make efi_get_time can be called before entering
356 efi.get_time = phys_efi_get_time;
358 printk(KERN_ERR "Could not map the EFI runtime service "
360 early_iounmap(runtime, sizeof(efi_runtime_services_t));
362 /* Map the EFI memory map */
363 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
364 memmap.nr_map * memmap.desc_size);
365 if (memmap.map == NULL)
366 printk(KERN_ERR "Could not map the EFI memory map!\n");
367 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
368 if (memmap.desc_size != sizeof(efi_memory_desc_t))
369 printk(KERN_WARNING "Kernel-defined memdesc"
370 "doesn't match the one from EFI!\n");
373 /* Setup for EFI runtime service */
374 reboot_type = BOOT_EFI;
383 * This function will switch the EFI runtime services to virtual mode.
384 * Essentially, look through the EFI memmap and map every region that
385 * has the runtime attribute bit set in its memory descriptor and update
386 * that memory descriptor with the virtual address obtained from ioremap().
387 * This enables the runtime services to be called without having to
388 * thunk back into physical mode for every invocation.
390 void __init efi_enter_virtual_mode(void)
392 efi_memory_desc_t *md;
398 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
400 if (!(md->attribute & EFI_MEMORY_RUNTIME))
402 if ((md->attribute & EFI_MEMORY_WB) &&
403 (((md->phys_addr + (md->num_pages<<EFI_PAGE_SHIFT)) >>
404 PAGE_SHIFT) < end_pfn_map))
405 md->virt_addr = (unsigned long)__va(md->phys_addr);
407 md->virt_addr = (unsigned long)
408 efi_ioremap(md->phys_addr,
409 md->num_pages << EFI_PAGE_SHIFT);
411 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
412 (unsigned long long)md->phys_addr);
413 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
414 if ((md->phys_addr <= (unsigned long)efi_phys.systab) &&
415 ((unsigned long)efi_phys.systab < end))
416 efi.systab = (efi_system_table_t *)(unsigned long)
417 (md->virt_addr - md->phys_addr +
418 (unsigned long)efi_phys.systab);
423 status = phys_efi_set_virtual_address_map(
424 memmap.desc_size * memmap.nr_map,
429 if (status != EFI_SUCCESS) {
430 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
431 "(status=%lx)!\n", status);
432 panic("EFI call to SetVirtualAddressMap() failed!");
436 * Now that EFI is in virtual mode, update the function
437 * pointers in the runtime service table to the new virtual addresses.
439 * Call EFI services through wrapper functions.
441 efi.get_time = virt_efi_get_time;
442 efi.set_time = virt_efi_set_time;
443 efi.get_wakeup_time = virt_efi_get_wakeup_time;
444 efi.set_wakeup_time = virt_efi_set_wakeup_time;
445 efi.get_variable = virt_efi_get_variable;
446 efi.get_next_variable = virt_efi_get_next_variable;
447 efi.set_variable = virt_efi_set_variable;
448 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
449 efi.reset_system = virt_efi_reset_system;
450 efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
452 runtime_code_page_mkexec();
457 * Convenience functions to obtain memory types and attributes
459 u32 efi_mem_type(unsigned long phys_addr)
461 efi_memory_desc_t *md;
464 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
466 if ((md->phys_addr <= phys_addr) &&
467 (phys_addr < (md->phys_addr +
468 (md->num_pages << EFI_PAGE_SHIFT))))
474 u64 efi_mem_attributes(unsigned long phys_addr)
476 efi_memory_desc_t *md;
479 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
481 if ((md->phys_addr <= phys_addr) &&
482 (phys_addr < (md->phys_addr +
483 (md->num_pages << EFI_PAGE_SHIFT))))
484 return md->attribute;