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 efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
60 return efi_call_virt2(get_time, tm, tc);
63 static efi_status_t virt_efi_set_time(efi_time_t *tm)
65 return efi_call_virt1(set_time, tm);
68 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
72 return efi_call_virt3(get_wakeup_time,
73 enabled, pending, tm);
76 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
78 return efi_call_virt2(set_wakeup_time,
82 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
85 unsigned long *data_size,
88 return efi_call_virt5(get_variable,
93 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
97 return efi_call_virt3(get_next_variable,
98 name_size, name, vendor);
101 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
104 unsigned long data_size,
107 return efi_call_virt5(set_variable,
112 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
114 return efi_call_virt1(get_next_high_mono_count, count);
117 static void virt_efi_reset_system(int reset_type,
119 unsigned long data_size,
122 efi_call_virt4(reset_system, reset_type, status,
126 static efi_status_t virt_efi_set_virtual_address_map(
127 unsigned long memory_map_size,
128 unsigned long descriptor_size,
129 u32 descriptor_version,
130 efi_memory_desc_t *virtual_map)
132 return efi_call_virt4(set_virtual_address_map,
133 memory_map_size, descriptor_size,
134 descriptor_version, virtual_map);
137 static efi_status_t __init phys_efi_set_virtual_address_map(
138 unsigned long memory_map_size,
139 unsigned long descriptor_size,
140 u32 descriptor_version,
141 efi_memory_desc_t *virtual_map)
145 efi_call_phys_prelog();
146 status = efi_call_phys4(efi_phys.set_virtual_address_map,
147 memory_map_size, descriptor_size,
148 descriptor_version, virtual_map);
149 efi_call_phys_epilog();
153 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
158 efi_call_phys_prelog();
159 status = efi_call_phys2(efi_phys.get_time, tm, tc);
160 efi_call_phys_epilog();
164 int efi_set_rtc_mmss(unsigned long nowtime)
166 int real_seconds, real_minutes;
171 status = efi.get_time(&eft, &cap);
172 if (status != EFI_SUCCESS) {
173 printk(KERN_ERR "Oops: efitime: can't read time!\n");
177 real_seconds = nowtime % 60;
178 real_minutes = nowtime / 60;
179 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
182 eft.minute = real_minutes;
183 eft.second = real_seconds;
185 status = efi.set_time(&eft);
186 if (status != EFI_SUCCESS) {
187 printk(KERN_ERR "Oops: efitime: can't write time!\n");
193 unsigned long efi_get_time(void)
199 status = efi.get_time(&eft, &cap);
200 if (status != EFI_SUCCESS)
201 printk(KERN_ERR "Oops: efitime: can't read time!\n");
203 return mktime(eft.year, eft.month, eft.day, eft.hour,
204 eft.minute, eft.second);
208 static void __init print_efi_memmap(void)
210 efi_memory_desc_t *md;
214 for (p = memmap.map, i = 0;
216 p += memmap.desc_size, i++) {
218 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
219 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
220 i, md->type, md->attribute, md->phys_addr,
221 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
222 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
225 #endif /* EFI_DEBUG */
227 void __init efi_init(void)
229 efi_config_table_t *config_tables;
230 efi_runtime_services_t *runtime;
232 char vendor[100] = "unknown";
237 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
238 memmap.phys_map = (void *)boot_params.efi_info.efi_memmap;
240 efi_phys.systab = (efi_system_table_t *)
241 (boot_params.efi_info.efi_systab |
242 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
243 memmap.phys_map = (void *)
244 (boot_params.efi_info.efi_memmap |
245 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
247 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
248 boot_params.efi_info.efi_memdesc_size;
249 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
250 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
252 efi.systab = efi_early_ioremap((unsigned long)efi_phys.systab,
253 sizeof(efi_system_table_t));
254 if (efi.systab == NULL)
255 printk(KERN_ERR "Couldn't map the EFI system table!\n");
256 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
257 efi_early_iounmap(efi.systab, sizeof(efi_system_table_t));
258 efi.systab = &efi_systab;
261 * Verify the EFI Table
263 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
264 printk(KERN_ERR "EFI system table signature incorrect!\n");
265 if ((efi.systab->hdr.revision >> 16) == 0)
266 printk(KERN_ERR "Warning: EFI system table version "
267 "%d.%02d, expected 1.00 or greater!\n",
268 efi.systab->hdr.revision >> 16,
269 efi.systab->hdr.revision & 0xffff);
272 * Show what we know for posterity
274 c16 = tmp = efi_early_ioremap(efi.systab->fw_vendor, 2);
276 for (i = 0; i < sizeof(vendor) && *c16; ++i)
280 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
281 efi_early_iounmap(tmp, 2);
283 printk(KERN_INFO "EFI v%u.%.02u by %s \n",
284 efi.systab->hdr.revision >> 16,
285 efi.systab->hdr.revision & 0xffff, vendor);
288 * Let's see what config tables the firmware passed to us.
290 config_tables = efi_early_ioremap(
292 efi.systab->nr_tables * sizeof(efi_config_table_t));
293 if (config_tables == NULL)
294 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
297 for (i = 0; i < efi.systab->nr_tables; i++) {
298 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
299 efi.mps = config_tables[i].table;
300 printk(" MPS=0x%lx ", config_tables[i].table);
301 } else if (!efi_guidcmp(config_tables[i].guid,
302 ACPI_20_TABLE_GUID)) {
303 efi.acpi20 = config_tables[i].table;
304 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
305 } else if (!efi_guidcmp(config_tables[i].guid,
307 efi.acpi = config_tables[i].table;
308 printk(" ACPI=0x%lx ", config_tables[i].table);
309 } else if (!efi_guidcmp(config_tables[i].guid,
310 SMBIOS_TABLE_GUID)) {
311 efi.smbios = config_tables[i].table;
312 printk(" SMBIOS=0x%lx ", config_tables[i].table);
313 } else if (!efi_guidcmp(config_tables[i].guid,
315 efi.hcdp = config_tables[i].table;
316 printk(" HCDP=0x%lx ", config_tables[i].table);
317 } else if (!efi_guidcmp(config_tables[i].guid,
318 UGA_IO_PROTOCOL_GUID)) {
319 efi.uga = config_tables[i].table;
320 printk(" UGA=0x%lx ", config_tables[i].table);
324 efi_early_iounmap(config_tables,
325 efi.systab->nr_tables * sizeof(efi_config_table_t));
328 * Check out the runtime services table. We need to map
329 * the runtime services table so that we can grab the physical
330 * address of several of the EFI runtime functions, needed to
331 * set the firmware into virtual mode.
333 runtime = efi_early_ioremap((unsigned long)efi.systab->runtime,
334 sizeof(efi_runtime_services_t));
335 if (runtime != NULL) {
337 * We will only need *early* access to the following
338 * two EFI runtime services before set_virtual_address_map
341 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
342 efi_phys.set_virtual_address_map =
343 (efi_set_virtual_address_map_t *)
344 runtime->set_virtual_address_map;
346 * Make efi_get_time can be called before entering
349 efi.get_time = phys_efi_get_time;
351 printk(KERN_ERR "Could not map the EFI runtime service "
353 efi_early_iounmap(runtime, sizeof(efi_runtime_services_t));
355 /* Map the EFI memory map */
356 memmap.map = efi_early_ioremap((unsigned long)memmap.phys_map,
357 memmap.nr_map * memmap.desc_size);
358 if (memmap.map == NULL)
359 printk(KERN_ERR "Could not map the EFI memory map!\n");
360 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
361 if (memmap.desc_size != sizeof(efi_memory_desc_t))
362 printk(KERN_WARNING "Kernel-defined memdesc"
363 "doesn't match the one from EFI!\n");
366 /* Setup for EFI runtime service */
367 reboot_type = BOOT_EFI;
376 * This function will switch the EFI runtime services to virtual mode.
377 * Essentially, look through the EFI memmap and map every region that
378 * has the runtime attribute bit set in its memory descriptor and update
379 * that memory descriptor with the virtual address obtained from ioremap().
380 * This enables the runtime services to be called without having to
381 * thunk back into physical mode for every invocation.
383 void __init efi_enter_virtual_mode(void)
385 efi_memory_desc_t *md;
391 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
393 if (!(md->attribute & EFI_MEMORY_RUNTIME))
395 if ((md->attribute & EFI_MEMORY_WB) &&
396 (((md->phys_addr + (md->num_pages<<EFI_PAGE_SHIFT)) >>
397 PAGE_SHIFT) < end_pfn_map))
398 md->virt_addr = (unsigned long)__va(md->phys_addr);
400 md->virt_addr = (unsigned long)
401 efi_ioremap(md->phys_addr,
402 md->num_pages << EFI_PAGE_SHIFT);
404 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
405 (unsigned long long)md->phys_addr);
406 end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
407 if ((md->phys_addr <= (unsigned long)efi_phys.systab) &&
408 ((unsigned long)efi_phys.systab < end))
409 efi.systab = (efi_system_table_t *)(unsigned long)
410 (md->virt_addr - md->phys_addr +
411 (unsigned long)efi_phys.systab);
416 status = phys_efi_set_virtual_address_map(
417 memmap.desc_size * memmap.nr_map,
422 if (status != EFI_SUCCESS) {
423 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
424 "(status=%lx)!\n", status);
425 panic("EFI call to SetVirtualAddressMap() failed!");
429 * Now that EFI is in virtual mode, update the function
430 * pointers in the runtime service table to the new virtual addresses.
432 * Call EFI services through wrapper functions.
434 efi.get_time = virt_efi_get_time;
435 efi.set_time = virt_efi_set_time;
436 efi.get_wakeup_time = virt_efi_get_wakeup_time;
437 efi.set_wakeup_time = virt_efi_set_wakeup_time;
438 efi.get_variable = virt_efi_get_variable;
439 efi.get_next_variable = virt_efi_get_next_variable;
440 efi.set_variable = virt_efi_set_variable;
441 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
442 efi.reset_system = virt_efi_reset_system;
443 efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
445 runtime_code_page_mkexec();
450 * Convenience functions to obtain memory types and attributes
452 u32 efi_mem_type(unsigned long phys_addr)
454 efi_memory_desc_t *md;
457 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
459 if ((md->phys_addr <= phys_addr) &&
460 (phys_addr < (md->phys_addr +
461 (md->num_pages << EFI_PAGE_SHIFT))))
467 u64 efi_mem_attributes(unsigned long phys_addr)
469 efi_memory_desc_t *md;
472 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
474 if ((md->phys_addr <= phys_addr) &&
475 (phys_addr < (md->phys_addr +
476 (md->num_pages << EFI_PAGE_SHIFT))))
477 return md->attribute;