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 __init
phys_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
)
156 efi_call_phys_prelog();
157 status
= efi_call_phys4(efi_phys
.set_virtual_address_map
,
158 memory_map_size
, descriptor_size
,
159 descriptor_version
, virtual_map
);
160 efi_call_phys_epilog();
164 static efi_status_t __init
phys_efi_get_time(efi_time_t
*tm
,
169 efi_call_phys_prelog();
170 status
= efi_call_phys2(efi_phys
.get_time
, tm
, tc
);
171 efi_call_phys_epilog();
175 int efi_set_rtc_mmss(unsigned long nowtime
)
177 int real_seconds
, real_minutes
;
182 status
= efi
.get_time(&eft
, &cap
);
183 if (status
!= EFI_SUCCESS
) {
184 printk(KERN_ERR
"Oops: efitime: can't read time!\n");
188 real_seconds
= nowtime
% 60;
189 real_minutes
= nowtime
/ 60;
190 if (((abs(real_minutes
- eft
.minute
) + 15)/30) & 1)
193 eft
.minute
= real_minutes
;
194 eft
.second
= real_seconds
;
196 status
= efi
.set_time(&eft
);
197 if (status
!= EFI_SUCCESS
) {
198 printk(KERN_ERR
"Oops: efitime: can't write time!\n");
204 unsigned long efi_get_time(void)
210 status
= efi
.get_time(&eft
, &cap
);
211 if (status
!= EFI_SUCCESS
)
212 printk(KERN_ERR
"Oops: efitime: can't read time!\n");
214 return mktime(eft
.year
, eft
.month
, eft
.day
, eft
.hour
,
215 eft
.minute
, eft
.second
);
219 * Tell the kernel about the EFI memory map. This might include
220 * more than the max 128 entries that can fit in the e820 legacy
221 * (zeropage) memory map.
224 static void __init
do_add_efi_memmap(void)
228 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
229 efi_memory_desc_t
*md
= p
;
230 unsigned long long start
= md
->phys_addr
;
231 unsigned long long size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
235 case EFI_LOADER_CODE
:
236 case EFI_LOADER_DATA
:
237 case EFI_BOOT_SERVICES_CODE
:
238 case EFI_BOOT_SERVICES_DATA
:
239 case EFI_CONVENTIONAL_MEMORY
:
240 if (md
->attribute
& EFI_MEMORY_WB
)
241 e820_type
= E820_RAM
;
243 e820_type
= E820_RESERVED
;
245 case EFI_ACPI_RECLAIM_MEMORY
:
246 e820_type
= E820_ACPI
;
248 case EFI_ACPI_MEMORY_NVS
:
249 e820_type
= E820_NVS
;
251 case EFI_UNUSABLE_MEMORY
:
252 e820_type
= E820_UNUSABLE
;
256 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
257 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
258 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
260 e820_type
= E820_RESERVED
;
263 e820_add_region(start
, size
, e820_type
);
265 sanitize_e820_map(e820
.map
, ARRAY_SIZE(e820
.map
), &e820
.nr_map
);
268 void __init
efi_memblock_x86_reserve_range(void)
273 pmap
= boot_params
.efi_info
.efi_memmap
;
275 pmap
= (boot_params
.efi_info
.efi_memmap
|
276 ((__u64
)boot_params
.efi_info
.efi_memmap_hi
<<32));
278 memmap
.phys_map
= (void *)pmap
;
279 memmap
.nr_map
= boot_params
.efi_info
.efi_memmap_size
/
280 boot_params
.efi_info
.efi_memdesc_size
;
281 memmap
.desc_version
= boot_params
.efi_info
.efi_memdesc_version
;
282 memmap
.desc_size
= boot_params
.efi_info
.efi_memdesc_size
;
283 memblock_x86_reserve_range(pmap
, pmap
+ memmap
.nr_map
* memmap
.desc_size
,
288 static void __init
print_efi_memmap(void)
290 efi_memory_desc_t
*md
;
294 for (p
= memmap
.map
, i
= 0;
296 p
+= memmap
.desc_size
, i
++) {
298 printk(KERN_INFO PFX
"mem%02u: type=%u, attr=0x%llx, "
299 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
300 i
, md
->type
, md
->attribute
, md
->phys_addr
,
301 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
302 (md
->num_pages
>> (20 - EFI_PAGE_SHIFT
)));
305 #endif /* EFI_DEBUG */
307 void __init
efi_reserve_boot_services(void)
311 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
312 efi_memory_desc_t
*md
= p
;
313 u64 start
= md
->phys_addr
;
314 u64 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
316 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
317 md
->type
!= EFI_BOOT_SERVICES_DATA
)
319 /* Only reserve where possible:
320 * - Not within any already allocated areas
321 * - Not over any memory area (really needed, if above?)
322 * - Not within any part of the kernel
323 * - Not the bios reserved area
325 if ((start
+size
>= virt_to_phys(_text
)
326 && start
<= virt_to_phys(_end
)) ||
327 !e820_all_mapped(start
, start
+size
, E820_RAM
) ||
328 memblock_x86_check_reserved_size(&start
, &size
,
329 1<<EFI_PAGE_SHIFT
)) {
330 /* Could not reserve, skip it */
332 memblock_dbg(PFX
"Could not reserve boot range "
333 "[0x%010llx-0x%010llx]\n",
334 start
, start
+size
-1);
336 memblock_x86_reserve_range(start
, start
+size
,
341 static void __init
efi_free_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 /* Could not reserve boot area */
358 free_bootmem_late(start
, size
);
362 void __init
efi_init(void)
364 efi_config_table_t
*config_tables
;
365 efi_runtime_services_t
*runtime
;
367 char vendor
[100] = "unknown";
372 efi_phys
.systab
= (efi_system_table_t
*)boot_params
.efi_info
.efi_systab
;
374 efi_phys
.systab
= (efi_system_table_t
*)
375 (boot_params
.efi_info
.efi_systab
|
376 ((__u64
)boot_params
.efi_info
.efi_systab_hi
<<32));
379 efi
.systab
= early_ioremap((unsigned long)efi_phys
.systab
,
380 sizeof(efi_system_table_t
));
381 if (efi
.systab
== NULL
)
382 printk(KERN_ERR
"Couldn't map the EFI system table!\n");
383 memcpy(&efi_systab
, efi
.systab
, sizeof(efi_system_table_t
));
384 early_iounmap(efi
.systab
, sizeof(efi_system_table_t
));
385 efi
.systab
= &efi_systab
;
388 * Verify the EFI Table
390 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
391 printk(KERN_ERR
"EFI system table signature incorrect!\n");
392 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
393 printk(KERN_ERR
"Warning: EFI system table version "
394 "%d.%02d, expected 1.00 or greater!\n",
395 efi
.systab
->hdr
.revision
>> 16,
396 efi
.systab
->hdr
.revision
& 0xffff);
399 * Show what we know for posterity
401 c16
= tmp
= early_ioremap(efi
.systab
->fw_vendor
, 2);
403 for (i
= 0; i
< sizeof(vendor
) - 1 && *c16
; ++i
)
407 printk(KERN_ERR PFX
"Could not map the firmware vendor!\n");
408 early_iounmap(tmp
, 2);
410 printk(KERN_INFO
"EFI v%u.%.02u by %s\n",
411 efi
.systab
->hdr
.revision
>> 16,
412 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
415 * Let's see what config tables the firmware passed to us.
417 config_tables
= early_ioremap(
419 efi
.systab
->nr_tables
* sizeof(efi_config_table_t
));
420 if (config_tables
== NULL
)
421 printk(KERN_ERR
"Could not map EFI Configuration Table!\n");
424 for (i
= 0; i
< efi
.systab
->nr_tables
; i
++) {
425 if (!efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
)) {
426 efi
.mps
= config_tables
[i
].table
;
427 printk(" MPS=0x%lx ", config_tables
[i
].table
);
428 } else if (!efi_guidcmp(config_tables
[i
].guid
,
429 ACPI_20_TABLE_GUID
)) {
430 efi
.acpi20
= config_tables
[i
].table
;
431 printk(" ACPI 2.0=0x%lx ", config_tables
[i
].table
);
432 } else if (!efi_guidcmp(config_tables
[i
].guid
,
434 efi
.acpi
= config_tables
[i
].table
;
435 printk(" ACPI=0x%lx ", config_tables
[i
].table
);
436 } else if (!efi_guidcmp(config_tables
[i
].guid
,
437 SMBIOS_TABLE_GUID
)) {
438 efi
.smbios
= config_tables
[i
].table
;
439 printk(" SMBIOS=0x%lx ", config_tables
[i
].table
);
441 } else if (!efi_guidcmp(config_tables
[i
].guid
,
442 UV_SYSTEM_TABLE_GUID
)) {
443 efi
.uv_systab
= config_tables
[i
].table
;
444 printk(" UVsystab=0x%lx ", config_tables
[i
].table
);
446 } else if (!efi_guidcmp(config_tables
[i
].guid
,
448 efi
.hcdp
= config_tables
[i
].table
;
449 printk(" HCDP=0x%lx ", config_tables
[i
].table
);
450 } else if (!efi_guidcmp(config_tables
[i
].guid
,
451 UGA_IO_PROTOCOL_GUID
)) {
452 efi
.uga
= config_tables
[i
].table
;
453 printk(" UGA=0x%lx ", config_tables
[i
].table
);
457 early_iounmap(config_tables
,
458 efi
.systab
->nr_tables
* sizeof(efi_config_table_t
));
461 * Check out the runtime services table. We need to map
462 * the runtime services table so that we can grab the physical
463 * address of several of the EFI runtime functions, needed to
464 * set the firmware into virtual mode.
466 runtime
= early_ioremap((unsigned long)efi
.systab
->runtime
,
467 sizeof(efi_runtime_services_t
));
468 if (runtime
!= NULL
) {
470 * We will only need *early* access to the following
471 * two EFI runtime services before set_virtual_address_map
474 efi_phys
.get_time
= (efi_get_time_t
*)runtime
->get_time
;
475 efi_phys
.set_virtual_address_map
=
476 (efi_set_virtual_address_map_t
*)
477 runtime
->set_virtual_address_map
;
479 * Make efi_get_time can be called before entering
482 efi
.get_time
= phys_efi_get_time
;
484 printk(KERN_ERR
"Could not map the EFI runtime service "
486 early_iounmap(runtime
, sizeof(efi_runtime_services_t
));
488 /* Map the EFI memory map */
489 memmap
.map
= early_ioremap((unsigned long)memmap
.phys_map
,
490 memmap
.nr_map
* memmap
.desc_size
);
491 if (memmap
.map
== NULL
)
492 printk(KERN_ERR
"Could not map the EFI memory map!\n");
493 memmap
.map_end
= memmap
.map
+ (memmap
.nr_map
* memmap
.desc_size
);
495 if (memmap
.desc_size
!= sizeof(efi_memory_desc_t
))
497 "Kernel-defined memdesc doesn't match the one from EFI!\n");
503 x86_platform
.get_wallclock
= efi_get_time
;
504 x86_platform
.set_wallclock
= efi_set_rtc_mmss
;
512 void __init
efi_set_executable(efi_memory_desc_t
*md
, bool executable
)
516 addr
= md
->virt_addr
;
517 npages
= md
->num_pages
;
519 memrange_efi_to_native(&addr
, &npages
);
522 set_memory_x(addr
, npages
);
524 set_memory_nx(addr
, npages
);
527 static void __init
runtime_code_page_mkexec(void)
529 efi_memory_desc_t
*md
;
532 /* Make EFI runtime service code area executable */
533 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
536 if (md
->type
!= EFI_RUNTIME_SERVICES_CODE
)
539 efi_set_executable(md
, true);
544 * This function will switch the EFI runtime services to virtual mode.
545 * Essentially, look through the EFI memmap and map every region that
546 * has the runtime attribute bit set in its memory descriptor and update
547 * that memory descriptor with the virtual address obtained from ioremap().
548 * This enables the runtime services to be called without having to
549 * thunk back into physical mode for every invocation.
551 void __init
efi_enter_virtual_mode(void)
553 efi_memory_desc_t
*md
, *prev_md
= NULL
;
556 u64 end
, systab
, addr
, npages
, end_pfn
;
557 void *p
, *va
, *new_memmap
= NULL
;
562 /* Merge contiguous regions of the same type and attribute */
563 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
572 if (prev_md
->type
!= md
->type
||
573 prev_md
->attribute
!= md
->attribute
) {
578 prev_size
= prev_md
->num_pages
<< EFI_PAGE_SHIFT
;
580 if (md
->phys_addr
== (prev_md
->phys_addr
+ prev_size
)) {
581 prev_md
->num_pages
+= md
->num_pages
;
582 md
->type
= EFI_RESERVED_TYPE
;
589 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
591 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
) &&
592 md
->type
!= EFI_BOOT_SERVICES_CODE
&&
593 md
->type
!= EFI_BOOT_SERVICES_DATA
)
596 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
597 end
= md
->phys_addr
+ size
;
599 end_pfn
= PFN_UP(end
);
600 if (end_pfn
<= max_low_pfn_mapped
601 || (end_pfn
> (1UL << (32 - PAGE_SHIFT
))
602 && end_pfn
<= max_pfn_mapped
))
603 va
= __va(md
->phys_addr
);
605 va
= efi_ioremap(md
->phys_addr
, size
, md
->type
);
607 md
->virt_addr
= (u64
) (unsigned long) va
;
610 printk(KERN_ERR PFX
"ioremap of 0x%llX failed!\n",
611 (unsigned long long)md
->phys_addr
);
615 if (!(md
->attribute
& EFI_MEMORY_WB
)) {
616 addr
= md
->virt_addr
;
617 npages
= md
->num_pages
;
618 memrange_efi_to_native(&addr
, &npages
);
619 set_memory_uc(addr
, npages
);
622 systab
= (u64
) (unsigned long) efi_phys
.systab
;
623 if (md
->phys_addr
<= systab
&& systab
< end
) {
624 systab
+= md
->virt_addr
- md
->phys_addr
;
625 efi
.systab
= (efi_system_table_t
*) (unsigned long) systab
;
627 new_memmap
= krealloc(new_memmap
,
628 (count
+ 1) * memmap
.desc_size
,
630 memcpy(new_memmap
+ (count
* memmap
.desc_size
), md
,
637 status
= phys_efi_set_virtual_address_map(
638 memmap
.desc_size
* count
,
641 (efi_memory_desc_t
*)__pa(new_memmap
));
643 if (status
!= EFI_SUCCESS
) {
644 printk(KERN_ALERT
"Unable to switch EFI into virtual mode "
645 "(status=%lx)!\n", status
);
646 panic("EFI call to SetVirtualAddressMap() failed!");
650 * Thankfully, it does seem that no runtime services other than
651 * SetVirtualAddressMap() will touch boot services code, so we can
652 * get rid of it all at this point
654 efi_free_boot_services();
657 * Now that EFI is in virtual mode, update the function
658 * pointers in the runtime service table to the new virtual addresses.
660 * Call EFI services through wrapper functions.
662 efi
.get_time
= virt_efi_get_time
;
663 efi
.set_time
= virt_efi_set_time
;
664 efi
.get_wakeup_time
= virt_efi_get_wakeup_time
;
665 efi
.set_wakeup_time
= virt_efi_set_wakeup_time
;
666 efi
.get_variable
= virt_efi_get_variable
;
667 efi
.get_next_variable
= virt_efi_get_next_variable
;
668 efi
.set_variable
= virt_efi_set_variable
;
669 efi
.get_next_high_mono_count
= virt_efi_get_next_high_mono_count
;
670 efi
.reset_system
= virt_efi_reset_system
;
671 efi
.set_virtual_address_map
= NULL
;
672 if (__supported_pte_mask
& _PAGE_NX
)
673 runtime_code_page_mkexec();
674 early_iounmap(memmap
.map
, memmap
.nr_map
* memmap
.desc_size
);
680 * Convenience functions to obtain memory types and attributes
682 u32
efi_mem_type(unsigned long phys_addr
)
684 efi_memory_desc_t
*md
;
687 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
689 if ((md
->phys_addr
<= phys_addr
) &&
690 (phys_addr
< (md
->phys_addr
+
691 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
697 u64
efi_mem_attributes(unsigned long phys_addr
)
699 efi_memory_desc_t
*md
;
702 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
704 if ((md
->phys_addr
<= phys_addr
) &&
705 (phys_addr
< (md
->phys_addr
+
706 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
707 return md
->attribute
;