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>
15 * Copyright (C) 2013 SuSE Labs
16 * Borislav Petkov <bp@suse.de> - runtime services VA mapping
18 * Copied from efi_32.c to eliminate the duplicated code between EFI
19 * 32/64 support code. --ying 2007-10-26
21 * All EFI Runtime Services are not implemented yet as EFI only
22 * supports physical mode addressing on SoftSDV. This is to be fixed
23 * in a future version. --drummond 1999-07-20
25 * Implemented EFI runtime services and virtual mode calls. --davidm
27 * Goutham Rao: <goutham.rao@intel.com>
28 * Skip non-WB memory and ignore empty memory ranges.
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/efi.h>
36 #include <linux/efi-bgrt.h>
37 #include <linux/export.h>
38 #include <linux/bootmem.h>
39 #include <linux/slab.h>
40 #include <linux/memblock.h>
41 #include <linux/spinlock.h>
42 #include <linux/uaccess.h>
43 #include <linux/time.h>
45 #include <linux/reboot.h>
46 #include <linux/bcd.h>
48 #include <asm/setup.h>
51 #include <asm/cacheflush.h>
52 #include <asm/tlbflush.h>
53 #include <asm/x86_init.h>
55 #include <asm/uv/uv.h>
59 #define EFI_MIN_RESERVE 5120
61 #define EFI_DUMMY_GUID \
62 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
64 static efi_char16_t efi_dummy_name
[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
66 struct efi_memory_map memmap
;
68 static struct efi efi_phys __initdata
;
69 static efi_system_table_t efi_systab __initdata
;
71 unsigned long x86_efi_facility
;
73 static __initdata efi_config_table_type_t arch_tables
[] = {
75 {UV_SYSTEM_TABLE_GUID
, "UVsystab", &efi
.uv_systab
},
77 {NULL_GUID
, NULL
, NULL
},
80 u64 efi_setup
; /* efi setup_data physical address */
83 * Returns 1 if 'facility' is enabled, 0 otherwise.
85 int efi_enabled(int facility
)
87 return test_bit(facility
, &x86_efi_facility
) != 0;
89 EXPORT_SYMBOL(efi_enabled
);
91 static bool __initdata disable_runtime
= false;
92 static int __init
setup_noefi(char *arg
)
94 disable_runtime
= true;
97 early_param("noefi", setup_noefi
);
100 EXPORT_SYMBOL(add_efi_memmap
);
102 static int __init
setup_add_efi_memmap(char *arg
)
107 early_param("add_efi_memmap", setup_add_efi_memmap
);
109 static bool efi_no_storage_paranoia
;
111 static int __init
setup_storage_paranoia(char *arg
)
113 efi_no_storage_paranoia
= true;
116 early_param("efi_no_storage_paranoia", setup_storage_paranoia
);
118 static efi_status_t
virt_efi_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
123 spin_lock_irqsave(&rtc_lock
, flags
);
124 status
= efi_call_virt2(get_time
, tm
, tc
);
125 spin_unlock_irqrestore(&rtc_lock
, flags
);
129 static efi_status_t
virt_efi_set_time(efi_time_t
*tm
)
134 spin_lock_irqsave(&rtc_lock
, flags
);
135 status
= efi_call_virt1(set_time
, tm
);
136 spin_unlock_irqrestore(&rtc_lock
, flags
);
140 static efi_status_t
virt_efi_get_wakeup_time(efi_bool_t
*enabled
,
147 spin_lock_irqsave(&rtc_lock
, flags
);
148 status
= efi_call_virt3(get_wakeup_time
,
149 enabled
, pending
, tm
);
150 spin_unlock_irqrestore(&rtc_lock
, flags
);
154 static efi_status_t
virt_efi_set_wakeup_time(efi_bool_t enabled
, efi_time_t
*tm
)
159 spin_lock_irqsave(&rtc_lock
, flags
);
160 status
= efi_call_virt2(set_wakeup_time
,
162 spin_unlock_irqrestore(&rtc_lock
, flags
);
166 static efi_status_t
virt_efi_get_variable(efi_char16_t
*name
,
169 unsigned long *data_size
,
172 return efi_call_virt5(get_variable
,
177 static efi_status_t
virt_efi_get_next_variable(unsigned long *name_size
,
181 return efi_call_virt3(get_next_variable
,
182 name_size
, name
, vendor
);
185 static efi_status_t
virt_efi_set_variable(efi_char16_t
*name
,
188 unsigned long data_size
,
191 return efi_call_virt5(set_variable
,
196 static efi_status_t
virt_efi_query_variable_info(u32 attr
,
198 u64
*remaining_space
,
199 u64
*max_variable_size
)
201 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
202 return EFI_UNSUPPORTED
;
204 return efi_call_virt4(query_variable_info
, attr
, storage_space
,
205 remaining_space
, max_variable_size
);
208 static efi_status_t
virt_efi_get_next_high_mono_count(u32
*count
)
210 return efi_call_virt1(get_next_high_mono_count
, count
);
213 static void virt_efi_reset_system(int reset_type
,
215 unsigned long data_size
,
218 efi_call_virt4(reset_system
, reset_type
, status
,
222 static efi_status_t
virt_efi_update_capsule(efi_capsule_header_t
**capsules
,
224 unsigned long sg_list
)
226 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
227 return EFI_UNSUPPORTED
;
229 return efi_call_virt3(update_capsule
, capsules
, count
, sg_list
);
232 static efi_status_t
virt_efi_query_capsule_caps(efi_capsule_header_t
**capsules
,
237 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
238 return EFI_UNSUPPORTED
;
240 return efi_call_virt4(query_capsule_caps
, capsules
, count
, max_size
,
244 static efi_status_t __init
phys_efi_set_virtual_address_map(
245 unsigned long memory_map_size
,
246 unsigned long descriptor_size
,
247 u32 descriptor_version
,
248 efi_memory_desc_t
*virtual_map
)
252 efi_call_phys_prelog();
253 status
= efi_call_phys4(efi_phys
.set_virtual_address_map
,
254 memory_map_size
, descriptor_size
,
255 descriptor_version
, virtual_map
);
256 efi_call_phys_epilog();
260 static efi_status_t __init
phys_efi_get_time(efi_time_t
*tm
,
266 spin_lock_irqsave(&rtc_lock
, flags
);
267 efi_call_phys_prelog();
268 status
= efi_call_phys2(efi_phys
.get_time
, virt_to_phys(tm
),
270 efi_call_phys_epilog();
271 spin_unlock_irqrestore(&rtc_lock
, flags
);
275 int efi_set_rtc_mmss(const struct timespec
*now
)
277 unsigned long nowtime
= now
->tv_sec
;
283 status
= efi
.get_time(&eft
, &cap
);
284 if (status
!= EFI_SUCCESS
) {
285 pr_err("Oops: efitime: can't read time!\n");
289 rtc_time_to_tm(nowtime
, &tm
);
290 if (!rtc_valid_tm(&tm
)) {
291 eft
.year
= tm
.tm_year
+ 1900;
292 eft
.month
= tm
.tm_mon
+ 1;
293 eft
.day
= tm
.tm_mday
;
294 eft
.minute
= tm
.tm_min
;
295 eft
.second
= tm
.tm_sec
;
299 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
300 __FUNCTION__
, nowtime
);
304 status
= efi
.set_time(&eft
);
305 if (status
!= EFI_SUCCESS
) {
306 pr_err("Oops: efitime: can't write time!\n");
312 void efi_get_time(struct timespec
*now
)
318 status
= efi
.get_time(&eft
, &cap
);
319 if (status
!= EFI_SUCCESS
)
320 pr_err("Oops: efitime: can't read time!\n");
322 now
->tv_sec
= mktime(eft
.year
, eft
.month
, eft
.day
, eft
.hour
,
323 eft
.minute
, eft
.second
);
328 * Tell the kernel about the EFI memory map. This might include
329 * more than the max 128 entries that can fit in the e820 legacy
330 * (zeropage) memory map.
333 static void __init
do_add_efi_memmap(void)
337 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
338 efi_memory_desc_t
*md
= p
;
339 unsigned long long start
= md
->phys_addr
;
340 unsigned long long size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
344 case EFI_LOADER_CODE
:
345 case EFI_LOADER_DATA
:
346 case EFI_BOOT_SERVICES_CODE
:
347 case EFI_BOOT_SERVICES_DATA
:
348 case EFI_CONVENTIONAL_MEMORY
:
349 if (md
->attribute
& EFI_MEMORY_WB
)
350 e820_type
= E820_RAM
;
352 e820_type
= E820_RESERVED
;
354 case EFI_ACPI_RECLAIM_MEMORY
:
355 e820_type
= E820_ACPI
;
357 case EFI_ACPI_MEMORY_NVS
:
358 e820_type
= E820_NVS
;
360 case EFI_UNUSABLE_MEMORY
:
361 e820_type
= E820_UNUSABLE
;
365 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
366 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
367 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
369 e820_type
= E820_RESERVED
;
372 e820_add_region(start
, size
, e820_type
);
374 sanitize_e820_map(e820
.map
, ARRAY_SIZE(e820
.map
), &e820
.nr_map
);
377 int __init
efi_memblock_x86_reserve_range(void)
379 struct efi_info
*e
= &boot_params
.efi_info
;
383 /* Can't handle data above 4GB at this time */
384 if (e
->efi_memmap_hi
) {
385 pr_err("Memory map is above 4GB, disabling EFI.\n");
388 pmap
= e
->efi_memmap
;
390 pmap
= (e
->efi_memmap
| ((__u64
)e
->efi_memmap_hi
<< 32));
392 memmap
.phys_map
= (void *)pmap
;
393 memmap
.nr_map
= e
->efi_memmap_size
/
395 memmap
.desc_size
= e
->efi_memdesc_size
;
396 memmap
.desc_version
= e
->efi_memdesc_version
;
398 memblock_reserve(pmap
, memmap
.nr_map
* memmap
.desc_size
);
400 efi
.memmap
= &memmap
;
405 static void __init
print_efi_memmap(void)
408 efi_memory_desc_t
*md
;
412 for (p
= memmap
.map
, i
= 0;
414 p
+= memmap
.desc_size
, i
++) {
416 pr_info("mem%02u: type=%u, attr=0x%llx, "
417 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
418 i
, md
->type
, md
->attribute
, md
->phys_addr
,
419 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
420 (md
->num_pages
>> (20 - EFI_PAGE_SHIFT
)));
422 #endif /* EFI_DEBUG */
425 void __init
efi_reserve_boot_services(void)
429 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
430 efi_memory_desc_t
*md
= p
;
431 u64 start
= md
->phys_addr
;
432 u64 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
434 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
435 md
->type
!= EFI_BOOT_SERVICES_DATA
)
437 /* Only reserve where possible:
438 * - Not within any already allocated areas
439 * - Not over any memory area (really needed, if above?)
440 * - Not within any part of the kernel
441 * - Not the bios reserved area
443 if ((start
+ size
> __pa_symbol(_text
)
444 && start
<= __pa_symbol(_end
)) ||
445 !e820_all_mapped(start
, start
+size
, E820_RAM
) ||
446 memblock_is_region_reserved(start
, size
)) {
447 /* Could not reserve, skip it */
449 memblock_dbg("Could not reserve boot range "
450 "[0x%010llx-0x%010llx]\n",
451 start
, start
+size
-1);
453 memblock_reserve(start
, size
);
457 void __init
efi_unmap_memmap(void)
459 clear_bit(EFI_MEMMAP
, &x86_efi_facility
);
461 early_iounmap(memmap
.map
, memmap
.nr_map
* memmap
.desc_size
);
466 void __init
efi_free_boot_services(void)
470 if (!efi_is_native())
473 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
474 efi_memory_desc_t
*md
= p
;
475 unsigned long long start
= md
->phys_addr
;
476 unsigned long long size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
478 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
479 md
->type
!= EFI_BOOT_SERVICES_DATA
)
482 /* Could not reserve boot area */
486 free_bootmem_late(start
, size
);
492 static int __init
efi_systab_init(void *phys
)
494 if (efi_enabled(EFI_64BIT
)) {
495 efi_system_table_64_t
*systab64
;
496 struct efi_setup_data
*data
= NULL
;
500 data
= early_memremap(efi_setup
, sizeof(*data
));
504 systab64
= early_ioremap((unsigned long)phys
,
506 if (systab64
== NULL
) {
507 pr_err("Couldn't map the system table!\n");
509 early_iounmap(data
, sizeof(*data
));
513 efi_systab
.hdr
= systab64
->hdr
;
514 efi_systab
.fw_vendor
= data
? (unsigned long)data
->fw_vendor
:
516 tmp
|= data
? data
->fw_vendor
: systab64
->fw_vendor
;
517 efi_systab
.fw_revision
= systab64
->fw_revision
;
518 efi_systab
.con_in_handle
= systab64
->con_in_handle
;
519 tmp
|= systab64
->con_in_handle
;
520 efi_systab
.con_in
= systab64
->con_in
;
521 tmp
|= systab64
->con_in
;
522 efi_systab
.con_out_handle
= systab64
->con_out_handle
;
523 tmp
|= systab64
->con_out_handle
;
524 efi_systab
.con_out
= systab64
->con_out
;
525 tmp
|= systab64
->con_out
;
526 efi_systab
.stderr_handle
= systab64
->stderr_handle
;
527 tmp
|= systab64
->stderr_handle
;
528 efi_systab
.stderr
= systab64
->stderr
;
529 tmp
|= systab64
->stderr
;
530 efi_systab
.runtime
= data
?
531 (void *)(unsigned long)data
->runtime
:
532 (void *)(unsigned long)systab64
->runtime
;
533 tmp
|= data
? data
->runtime
: systab64
->runtime
;
534 efi_systab
.boottime
= (void *)(unsigned long)systab64
->boottime
;
535 tmp
|= systab64
->boottime
;
536 efi_systab
.nr_tables
= systab64
->nr_tables
;
537 efi_systab
.tables
= data
? (unsigned long)data
->tables
:
539 tmp
|= data
? data
->tables
: systab64
->tables
;
541 early_iounmap(systab64
, sizeof(*systab64
));
543 early_iounmap(data
, sizeof(*data
));
546 pr_err("EFI data located above 4GB, disabling EFI.\n");
551 efi_system_table_32_t
*systab32
;
553 systab32
= early_ioremap((unsigned long)phys
,
555 if (systab32
== NULL
) {
556 pr_err("Couldn't map the system table!\n");
560 efi_systab
.hdr
= systab32
->hdr
;
561 efi_systab
.fw_vendor
= systab32
->fw_vendor
;
562 efi_systab
.fw_revision
= systab32
->fw_revision
;
563 efi_systab
.con_in_handle
= systab32
->con_in_handle
;
564 efi_systab
.con_in
= systab32
->con_in
;
565 efi_systab
.con_out_handle
= systab32
->con_out_handle
;
566 efi_systab
.con_out
= systab32
->con_out
;
567 efi_systab
.stderr_handle
= systab32
->stderr_handle
;
568 efi_systab
.stderr
= systab32
->stderr
;
569 efi_systab
.runtime
= (void *)(unsigned long)systab32
->runtime
;
570 efi_systab
.boottime
= (void *)(unsigned long)systab32
->boottime
;
571 efi_systab
.nr_tables
= systab32
->nr_tables
;
572 efi_systab
.tables
= systab32
->tables
;
574 early_iounmap(systab32
, sizeof(*systab32
));
577 efi
.systab
= &efi_systab
;
580 * Verify the EFI Table
582 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
583 pr_err("System table signature incorrect!\n");
586 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
587 pr_err("Warning: System table version "
588 "%d.%02d, expected 1.00 or greater!\n",
589 efi
.systab
->hdr
.revision
>> 16,
590 efi
.systab
->hdr
.revision
& 0xffff);
595 static int __init
efi_runtime_init(void)
597 efi_runtime_services_t
*runtime
;
600 * Check out the runtime services table. We need to map
601 * the runtime services table so that we can grab the physical
602 * address of several of the EFI runtime functions, needed to
603 * set the firmware into virtual mode.
605 runtime
= early_ioremap((unsigned long)efi
.systab
->runtime
,
606 sizeof(efi_runtime_services_t
));
608 pr_err("Could not map the runtime service table!\n");
612 * We will only need *early* access to the following
613 * two EFI runtime services before set_virtual_address_map
616 efi_phys
.get_time
= (efi_get_time_t
*)runtime
->get_time
;
617 efi_phys
.set_virtual_address_map
=
618 (efi_set_virtual_address_map_t
*)
619 runtime
->set_virtual_address_map
;
621 * Make efi_get_time can be called before entering
624 efi
.get_time
= phys_efi_get_time
;
625 early_iounmap(runtime
, sizeof(efi_runtime_services_t
));
630 static int __init
efi_memmap_init(void)
632 /* Map the EFI memory map */
633 memmap
.map
= early_ioremap((unsigned long)memmap
.phys_map
,
634 memmap
.nr_map
* memmap
.desc_size
);
635 if (memmap
.map
== NULL
) {
636 pr_err("Could not map the memory map!\n");
639 memmap
.map_end
= memmap
.map
+ (memmap
.nr_map
* memmap
.desc_size
);
648 * A number of config table entries get remapped to virtual addresses
649 * after entering EFI virtual mode. However, the kexec kernel requires
650 * their physical addresses therefore we pass them via setup_data and
651 * correct those entries to their respective physical addresses here.
653 * Currently only handles smbios which is necessary for some firmware
656 static int __init
efi_reuse_config(u64 tables
, int nr_tables
)
660 struct efi_setup_data
*data
;
665 if (!efi_enabled(EFI_64BIT
))
668 data
= early_memremap(efi_setup
, sizeof(*data
));
677 sz
= sizeof(efi_config_table_64_t
);
679 p
= tablep
= early_memremap(tables
, nr_tables
* sz
);
681 pr_err("Could not map Configuration table!\n");
686 for (i
= 0; i
< efi
.systab
->nr_tables
; i
++) {
689 guid
= ((efi_config_table_64_t
*)p
)->guid
;
691 if (!efi_guidcmp(guid
, SMBIOS_TABLE_GUID
))
692 ((efi_config_table_64_t
*)p
)->table
= data
->smbios
;
695 early_iounmap(tablep
, nr_tables
* sz
);
698 early_iounmap(data
, sizeof(*data
));
703 void __init
efi_init(void)
706 char vendor
[100] = "unknown";
711 if (boot_params
.efi_info
.efi_systab_hi
||
712 boot_params
.efi_info
.efi_memmap_hi
) {
713 pr_info("Table located above 4GB, disabling EFI.\n");
716 efi_phys
.systab
= (efi_system_table_t
*)boot_params
.efi_info
.efi_systab
;
718 efi_phys
.systab
= (efi_system_table_t
*)
719 (boot_params
.efi_info
.efi_systab
|
720 ((__u64
)boot_params
.efi_info
.efi_systab_hi
<<32));
723 if (efi_systab_init(efi_phys
.systab
))
726 set_bit(EFI_SYSTEM_TABLES
, &x86_efi_facility
);
728 efi
.config_table
= (unsigned long)efi
.systab
->tables
;
729 efi
.fw_vendor
= (unsigned long)efi
.systab
->fw_vendor
;
730 efi
.runtime
= (unsigned long)efi
.systab
->runtime
;
733 * Show what we know for posterity
735 c16
= tmp
= early_ioremap(efi
.systab
->fw_vendor
, 2);
737 for (i
= 0; i
< sizeof(vendor
) - 1 && *c16
; ++i
)
741 pr_err("Could not map the firmware vendor!\n");
742 early_iounmap(tmp
, 2);
744 pr_info("EFI v%u.%.02u by %s\n",
745 efi
.systab
->hdr
.revision
>> 16,
746 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
748 if (efi_reuse_config(efi
.systab
->tables
, efi
.systab
->nr_tables
))
751 if (efi_config_init(arch_tables
))
754 set_bit(EFI_CONFIG_TABLES
, &x86_efi_facility
);
757 * Note: We currently don't support runtime services on an EFI
758 * that doesn't match the kernel 32/64-bit mode.
761 if (!efi_is_native())
762 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
764 if (disable_runtime
|| efi_runtime_init())
766 set_bit(EFI_RUNTIME_SERVICES
, &x86_efi_facility
);
768 if (efi_memmap_init())
771 set_bit(EFI_MEMMAP
, &x86_efi_facility
);
776 void __init
efi_late_init(void)
781 void __init
efi_set_executable(efi_memory_desc_t
*md
, bool executable
)
785 addr
= md
->virt_addr
;
786 npages
= md
->num_pages
;
788 memrange_efi_to_native(&addr
, &npages
);
791 set_memory_x(addr
, npages
);
793 set_memory_nx(addr
, npages
);
796 void __init
runtime_code_page_mkexec(void)
798 efi_memory_desc_t
*md
;
801 /* Make EFI runtime service code area executable */
802 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
805 if (md
->type
!= EFI_RUNTIME_SERVICES_CODE
)
808 efi_set_executable(md
, true);
812 void efi_memory_uc(u64 addr
, unsigned long size
)
814 unsigned long page_shift
= 1UL << EFI_PAGE_SHIFT
;
817 npages
= round_up(size
, page_shift
) / page_shift
;
818 memrange_efi_to_native(&addr
, &npages
);
819 set_memory_uc(addr
, npages
);
822 void __init
old_map_region(efi_memory_desc_t
*md
)
824 u64 start_pfn
, end_pfn
, end
;
828 start_pfn
= PFN_DOWN(md
->phys_addr
);
829 size
= md
->num_pages
<< PAGE_SHIFT
;
830 end
= md
->phys_addr
+ size
;
831 end_pfn
= PFN_UP(end
);
833 if (pfn_range_is_mapped(start_pfn
, end_pfn
)) {
834 va
= __va(md
->phys_addr
);
836 if (!(md
->attribute
& EFI_MEMORY_WB
))
837 efi_memory_uc((u64
)(unsigned long)va
, size
);
839 va
= efi_ioremap(md
->phys_addr
, size
,
840 md
->type
, md
->attribute
);
842 md
->virt_addr
= (u64
) (unsigned long) va
;
844 pr_err("ioremap of 0x%llX failed!\n",
845 (unsigned long long)md
->phys_addr
);
848 /* Merge contiguous regions of the same type and attribute */
849 static void __init
efi_merge_regions(void)
852 efi_memory_desc_t
*md
, *prev_md
= NULL
;
854 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
863 if (prev_md
->type
!= md
->type
||
864 prev_md
->attribute
!= md
->attribute
) {
869 prev_size
= prev_md
->num_pages
<< EFI_PAGE_SHIFT
;
871 if (md
->phys_addr
== (prev_md
->phys_addr
+ prev_size
)) {
872 prev_md
->num_pages
+= md
->num_pages
;
873 md
->type
= EFI_RESERVED_TYPE
;
881 static void __init
get_systab_virt_addr(efi_memory_desc_t
*md
)
886 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
887 end
= md
->phys_addr
+ size
;
888 systab
= (u64
)(unsigned long)efi_phys
.systab
;
889 if (md
->phys_addr
<= systab
&& systab
< end
) {
890 systab
+= md
->virt_addr
- md
->phys_addr
;
891 efi
.systab
= (efi_system_table_t
*)(unsigned long)systab
;
895 static int __init
save_runtime_map(void)
897 efi_memory_desc_t
*md
;
898 void *tmp
, *p
, *q
= NULL
;
901 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
904 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
) ||
905 (md
->type
== EFI_BOOT_SERVICES_CODE
) ||
906 (md
->type
== EFI_BOOT_SERVICES_DATA
))
908 tmp
= krealloc(q
, (count
+ 1) * memmap
.desc_size
, GFP_KERNEL
);
913 memcpy(q
+ count
* memmap
.desc_size
, md
, memmap
.desc_size
);
917 efi_runtime_map_setup(q
, count
, memmap
.desc_size
);
926 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
927 * addr which was used in first kernel of a kexec boot.
929 static void __init
efi_map_regions_fixed(void)
932 efi_memory_desc_t
*md
;
934 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
936 efi_map_region_fixed(md
); /* FIXME: add error handling */
937 get_systab_virt_addr(md
);
943 * Map efi memory ranges for runtime serivce and update new_memmap with virtual
946 static void * __init
efi_map_regions(int *count
)
948 efi_memory_desc_t
*md
;
949 void *p
, *tmp
, *new_memmap
= NULL
;
951 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
953 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
)) {
955 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
956 md
->type
!= EFI_BOOT_SERVICES_DATA
)
962 get_systab_virt_addr(md
);
964 tmp
= krealloc(new_memmap
, (*count
+ 1) * memmap
.desc_size
,
969 memcpy(new_memmap
+ (*count
* memmap
.desc_size
), md
,
981 * This function will switch the EFI runtime services to virtual mode.
982 * Essentially, we look through the EFI memmap and map every region that
983 * has the runtime attribute bit set in its memory descriptor into the
984 * ->trampoline_pgd page table using a top-down VA allocation scheme.
986 * The old method which used to update that memory descriptor with the
987 * virtual address obtained from ioremap() is still supported when the
988 * kernel is booted with efi=old_map on its command line. Same old
989 * method enabled the runtime services to be called without having to
990 * thunk back into physical mode for every invocation.
992 * The new method does a pagetable switch in a preemption-safe manner
993 * so that we're in a different address space when calling a runtime
994 * function. For function arguments passing we do copy the PGDs of the
995 * kernel page table into ->trampoline_pgd prior to each call.
997 * Specially for kexec boot, efi runtime maps in previous kernel should
998 * be passed in via setup_data. In that case runtime ranges will be mapped
999 * to the same virtual addresses as the first kernel.
1001 void __init
efi_enter_virtual_mode(void)
1003 efi_status_t status
;
1004 void *new_memmap
= NULL
;
1010 * We don't do virtual mode, since we don't do runtime services, on
1013 if (!efi_is_native()) {
1019 efi_map_regions_fixed();
1021 efi_merge_regions();
1022 new_memmap
= efi_map_regions(&count
);
1024 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1029 err
= save_runtime_map();
1031 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1033 BUG_ON(!efi
.systab
);
1035 efi_setup_page_tables();
1036 efi_sync_low_kernel_mappings();
1039 status
= phys_efi_set_virtual_address_map(
1040 memmap
.desc_size
* count
,
1042 memmap
.desc_version
,
1043 (efi_memory_desc_t
*)__pa(new_memmap
));
1045 if (status
!= EFI_SUCCESS
) {
1046 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1048 panic("EFI call to SetVirtualAddressMap() failed!");
1053 * Now that EFI is in virtual mode, update the function
1054 * pointers in the runtime service table to the new virtual addresses.
1056 * Call EFI services through wrapper functions.
1058 efi
.runtime_version
= efi_systab
.hdr
.revision
;
1059 efi
.get_time
= virt_efi_get_time
;
1060 efi
.set_time
= virt_efi_set_time
;
1061 efi
.get_wakeup_time
= virt_efi_get_wakeup_time
;
1062 efi
.set_wakeup_time
= virt_efi_set_wakeup_time
;
1063 efi
.get_variable
= virt_efi_get_variable
;
1064 efi
.get_next_variable
= virt_efi_get_next_variable
;
1065 efi
.set_variable
= virt_efi_set_variable
;
1066 efi
.get_next_high_mono_count
= virt_efi_get_next_high_mono_count
;
1067 efi
.reset_system
= virt_efi_reset_system
;
1068 efi
.set_virtual_address_map
= NULL
;
1069 efi
.query_variable_info
= virt_efi_query_variable_info
;
1070 efi
.update_capsule
= virt_efi_update_capsule
;
1071 efi
.query_capsule_caps
= virt_efi_query_capsule_caps
;
1073 efi_runtime_mkexec();
1077 /* clean DUMMY object */
1078 efi
.set_variable(efi_dummy_name
, &EFI_DUMMY_GUID
,
1079 EFI_VARIABLE_NON_VOLATILE
|
1080 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1081 EFI_VARIABLE_RUNTIME_ACCESS
,
1086 * Convenience functions to obtain memory types and attributes
1088 u32
efi_mem_type(unsigned long phys_addr
)
1090 efi_memory_desc_t
*md
;
1093 if (!efi_enabled(EFI_MEMMAP
))
1096 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
1098 if ((md
->phys_addr
<= phys_addr
) &&
1099 (phys_addr
< (md
->phys_addr
+
1100 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
1106 u64
efi_mem_attributes(unsigned long phys_addr
)
1108 efi_memory_desc_t
*md
;
1111 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
1113 if ((md
->phys_addr
<= phys_addr
) &&
1114 (phys_addr
< (md
->phys_addr
+
1115 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
1116 return md
->attribute
;
1122 * Some firmware has serious problems when using more than 50% of the EFI
1123 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
1124 * we never use more than this safe limit.
1126 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1129 efi_status_t
efi_query_variable_store(u32 attributes
, unsigned long size
)
1131 efi_status_t status
;
1132 u64 storage_size
, remaining_size
, max_size
;
1134 if (!(attributes
& EFI_VARIABLE_NON_VOLATILE
))
1137 status
= efi
.query_variable_info(attributes
, &storage_size
,
1138 &remaining_size
, &max_size
);
1139 if (status
!= EFI_SUCCESS
)
1143 * Some firmware implementations refuse to boot if there's insufficient
1144 * space in the variable store. We account for that by refusing the
1145 * write if permitting it would reduce the available space to under
1146 * 5KB. This figure was provided by Samsung, so should be safe.
1148 if ((remaining_size
- size
< EFI_MIN_RESERVE
) &&
1149 !efi_no_storage_paranoia
) {
1152 * Triggering garbage collection may require that the firmware
1153 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1154 * that by attempting to use more space than is available.
1156 unsigned long dummy_size
= remaining_size
+ 1024;
1157 void *dummy
= kzalloc(dummy_size
, GFP_ATOMIC
);
1160 return EFI_OUT_OF_RESOURCES
;
1162 status
= efi
.set_variable(efi_dummy_name
, &EFI_DUMMY_GUID
,
1163 EFI_VARIABLE_NON_VOLATILE
|
1164 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1165 EFI_VARIABLE_RUNTIME_ACCESS
,
1168 if (status
== EFI_SUCCESS
) {
1170 * This should have failed, so if it didn't make sure
1171 * that we delete it...
1173 efi
.set_variable(efi_dummy_name
, &EFI_DUMMY_GUID
,
1174 EFI_VARIABLE_NON_VOLATILE
|
1175 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1176 EFI_VARIABLE_RUNTIME_ACCESS
,
1183 * The runtime code may now have triggered a garbage collection
1184 * run, so check the variable info again
1186 status
= efi
.query_variable_info(attributes
, &storage_size
,
1187 &remaining_size
, &max_size
);
1189 if (status
!= EFI_SUCCESS
)
1193 * There still isn't enough room, so return an error
1195 if (remaining_size
- size
< EFI_MIN_RESERVE
)
1196 return EFI_OUT_OF_RESOURCES
;
1201 EXPORT_SYMBOL_GPL(efi_query_variable_store
);
1203 static int __init
parse_efi_cmdline(char *str
)
1208 if (!strncmp(str
, "old_map", 7))
1209 set_bit(EFI_OLD_MEMMAP
, &x86_efi_facility
);
1213 early_param("efi", parse_efi_cmdline
);
1215 void __init
efi_apply_memmap_quirks(void)
1218 * Once setup is done earlier, unmap the EFI memory map on mismatched
1219 * firmware/kernel architectures since there is no support for runtime
1222 if (!efi_is_native()) {
1223 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1228 * UV doesn't support the new EFI pagetable mapping yet.
1231 set_bit(EFI_OLD_MEMMAP
, &x86_efi_facility
);