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 static efi_config_table_type_t arch_tables
[] __initdata
= {
73 {UV_SYSTEM_TABLE_GUID
, "UVsystab", &efi
.uv_systab
},
75 {NULL_GUID
, NULL
, NULL
},
78 u64 efi_setup
; /* efi setup_data physical address */
80 static bool disable_runtime __initdata
= false;
81 static int __init
setup_noefi(char *arg
)
83 disable_runtime
= true;
86 early_param("noefi", setup_noefi
);
89 EXPORT_SYMBOL(add_efi_memmap
);
91 static int __init
setup_add_efi_memmap(char *arg
)
96 early_param("add_efi_memmap", setup_add_efi_memmap
);
98 static bool efi_no_storage_paranoia
;
100 static int __init
setup_storage_paranoia(char *arg
)
102 efi_no_storage_paranoia
= true;
105 early_param("efi_no_storage_paranoia", setup_storage_paranoia
);
107 static efi_status_t
virt_efi_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
112 spin_lock_irqsave(&rtc_lock
, flags
);
113 status
= efi_call_virt2(get_time
, tm
, tc
);
114 spin_unlock_irqrestore(&rtc_lock
, flags
);
118 static efi_status_t
virt_efi_set_time(efi_time_t
*tm
)
123 spin_lock_irqsave(&rtc_lock
, flags
);
124 status
= efi_call_virt1(set_time
, tm
);
125 spin_unlock_irqrestore(&rtc_lock
, flags
);
129 static efi_status_t
virt_efi_get_wakeup_time(efi_bool_t
*enabled
,
136 spin_lock_irqsave(&rtc_lock
, flags
);
137 status
= efi_call_virt3(get_wakeup_time
,
138 enabled
, pending
, tm
);
139 spin_unlock_irqrestore(&rtc_lock
, flags
);
143 static efi_status_t
virt_efi_set_wakeup_time(efi_bool_t enabled
, efi_time_t
*tm
)
148 spin_lock_irqsave(&rtc_lock
, flags
);
149 status
= efi_call_virt2(set_wakeup_time
,
151 spin_unlock_irqrestore(&rtc_lock
, flags
);
155 static efi_status_t
virt_efi_get_variable(efi_char16_t
*name
,
158 unsigned long *data_size
,
161 return efi_call_virt5(get_variable
,
166 static efi_status_t
virt_efi_get_next_variable(unsigned long *name_size
,
170 return efi_call_virt3(get_next_variable
,
171 name_size
, name
, vendor
);
174 static efi_status_t
virt_efi_set_variable(efi_char16_t
*name
,
177 unsigned long data_size
,
180 return efi_call_virt5(set_variable
,
185 static efi_status_t
virt_efi_query_variable_info(u32 attr
,
187 u64
*remaining_space
,
188 u64
*max_variable_size
)
190 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
191 return EFI_UNSUPPORTED
;
193 return efi_call_virt4(query_variable_info
, attr
, storage_space
,
194 remaining_space
, max_variable_size
);
197 static efi_status_t
virt_efi_get_next_high_mono_count(u32
*count
)
199 return efi_call_virt1(get_next_high_mono_count
, count
);
202 static void virt_efi_reset_system(int reset_type
,
204 unsigned long data_size
,
207 efi_call_virt4(reset_system
, reset_type
, status
,
211 static efi_status_t
virt_efi_update_capsule(efi_capsule_header_t
**capsules
,
213 unsigned long sg_list
)
215 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
216 return EFI_UNSUPPORTED
;
218 return efi_call_virt3(update_capsule
, capsules
, count
, sg_list
);
221 static efi_status_t
virt_efi_query_capsule_caps(efi_capsule_header_t
**capsules
,
226 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
227 return EFI_UNSUPPORTED
;
229 return efi_call_virt4(query_capsule_caps
, capsules
, count
, max_size
,
233 static efi_status_t __init
phys_efi_set_virtual_address_map(
234 unsigned long memory_map_size
,
235 unsigned long descriptor_size
,
236 u32 descriptor_version
,
237 efi_memory_desc_t
*virtual_map
)
241 efi_call_phys_prelog();
242 status
= efi_call_phys4(efi_phys
.set_virtual_address_map
,
243 memory_map_size
, descriptor_size
,
244 descriptor_version
, virtual_map
);
245 efi_call_phys_epilog();
249 int efi_set_rtc_mmss(const struct timespec
*now
)
251 unsigned long nowtime
= now
->tv_sec
;
257 status
= efi
.get_time(&eft
, &cap
);
258 if (status
!= EFI_SUCCESS
) {
259 pr_err("Oops: efitime: can't read time!\n");
263 rtc_time_to_tm(nowtime
, &tm
);
264 if (!rtc_valid_tm(&tm
)) {
265 eft
.year
= tm
.tm_year
+ 1900;
266 eft
.month
= tm
.tm_mon
+ 1;
267 eft
.day
= tm
.tm_mday
;
268 eft
.minute
= tm
.tm_min
;
269 eft
.second
= tm
.tm_sec
;
272 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
277 status
= efi
.set_time(&eft
);
278 if (status
!= EFI_SUCCESS
) {
279 pr_err("Oops: efitime: can't write time!\n");
285 void efi_get_time(struct timespec
*now
)
291 status
= efi
.get_time(&eft
, &cap
);
292 if (status
!= EFI_SUCCESS
)
293 pr_err("Oops: efitime: can't read time!\n");
295 now
->tv_sec
= mktime(eft
.year
, eft
.month
, eft
.day
, eft
.hour
,
296 eft
.minute
, eft
.second
);
301 * Tell the kernel about the EFI memory map. This might include
302 * more than the max 128 entries that can fit in the e820 legacy
303 * (zeropage) memory map.
306 static void __init
do_add_efi_memmap(void)
310 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
311 efi_memory_desc_t
*md
= p
;
312 unsigned long long start
= md
->phys_addr
;
313 unsigned long long size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
317 case EFI_LOADER_CODE
:
318 case EFI_LOADER_DATA
:
319 case EFI_BOOT_SERVICES_CODE
:
320 case EFI_BOOT_SERVICES_DATA
:
321 case EFI_CONVENTIONAL_MEMORY
:
322 if (md
->attribute
& EFI_MEMORY_WB
)
323 e820_type
= E820_RAM
;
325 e820_type
= E820_RESERVED
;
327 case EFI_ACPI_RECLAIM_MEMORY
:
328 e820_type
= E820_ACPI
;
330 case EFI_ACPI_MEMORY_NVS
:
331 e820_type
= E820_NVS
;
333 case EFI_UNUSABLE_MEMORY
:
334 e820_type
= E820_UNUSABLE
;
338 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
339 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
340 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
342 e820_type
= E820_RESERVED
;
345 e820_add_region(start
, size
, e820_type
);
347 sanitize_e820_map(e820
.map
, ARRAY_SIZE(e820
.map
), &e820
.nr_map
);
350 int __init
efi_memblock_x86_reserve_range(void)
352 struct efi_info
*e
= &boot_params
.efi_info
;
356 /* Can't handle data above 4GB at this time */
357 if (e
->efi_memmap_hi
) {
358 pr_err("Memory map is above 4GB, disabling EFI.\n");
361 pmap
= e
->efi_memmap
;
363 pmap
= (e
->efi_memmap
| ((__u64
)e
->efi_memmap_hi
<< 32));
365 memmap
.phys_map
= (void *)pmap
;
366 memmap
.nr_map
= e
->efi_memmap_size
/
368 memmap
.desc_size
= e
->efi_memdesc_size
;
369 memmap
.desc_version
= e
->efi_memdesc_version
;
371 memblock_reserve(pmap
, memmap
.nr_map
* memmap
.desc_size
);
373 efi
.memmap
= &memmap
;
378 static void __init
print_efi_memmap(void)
381 efi_memory_desc_t
*md
;
385 for (p
= memmap
.map
, i
= 0;
387 p
+= memmap
.desc_size
, i
++) {
389 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
390 i
, md
->type
, md
->attribute
, md
->phys_addr
,
391 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
392 (md
->num_pages
>> (20 - EFI_PAGE_SHIFT
)));
394 #endif /* EFI_DEBUG */
397 void __init
efi_reserve_boot_services(void)
401 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
402 efi_memory_desc_t
*md
= p
;
403 u64 start
= md
->phys_addr
;
404 u64 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
406 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
407 md
->type
!= EFI_BOOT_SERVICES_DATA
)
409 /* Only reserve where possible:
410 * - Not within any already allocated areas
411 * - Not over any memory area (really needed, if above?)
412 * - Not within any part of the kernel
413 * - Not the bios reserved area
415 if ((start
+ size
> __pa_symbol(_text
)
416 && start
<= __pa_symbol(_end
)) ||
417 !e820_all_mapped(start
, start
+size
, E820_RAM
) ||
418 memblock_is_region_reserved(start
, size
)) {
419 /* Could not reserve, skip it */
421 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
422 start
, start
+size
-1);
424 memblock_reserve(start
, size
);
428 void __init
efi_unmap_memmap(void)
430 clear_bit(EFI_MEMMAP
, &efi
.flags
);
432 early_iounmap(memmap
.map
, memmap
.nr_map
* memmap
.desc_size
);
437 void __init
efi_free_boot_services(void)
441 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
442 efi_memory_desc_t
*md
= p
;
443 unsigned long long start
= md
->phys_addr
;
444 unsigned long long size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
446 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
447 md
->type
!= EFI_BOOT_SERVICES_DATA
)
450 /* Could not reserve boot area */
454 free_bootmem_late(start
, size
);
460 static int __init
efi_systab_init(void *phys
)
462 if (efi_enabled(EFI_64BIT
)) {
463 efi_system_table_64_t
*systab64
;
464 struct efi_setup_data
*data
= NULL
;
468 data
= early_memremap(efi_setup
, sizeof(*data
));
472 systab64
= early_ioremap((unsigned long)phys
,
474 if (systab64
== NULL
) {
475 pr_err("Couldn't map the system table!\n");
477 early_iounmap(data
, sizeof(*data
));
481 efi_systab
.hdr
= systab64
->hdr
;
482 efi_systab
.fw_vendor
= data
? (unsigned long)data
->fw_vendor
:
484 tmp
|= data
? data
->fw_vendor
: systab64
->fw_vendor
;
485 efi_systab
.fw_revision
= systab64
->fw_revision
;
486 efi_systab
.con_in_handle
= systab64
->con_in_handle
;
487 tmp
|= systab64
->con_in_handle
;
488 efi_systab
.con_in
= systab64
->con_in
;
489 tmp
|= systab64
->con_in
;
490 efi_systab
.con_out_handle
= systab64
->con_out_handle
;
491 tmp
|= systab64
->con_out_handle
;
492 efi_systab
.con_out
= systab64
->con_out
;
493 tmp
|= systab64
->con_out
;
494 efi_systab
.stderr_handle
= systab64
->stderr_handle
;
495 tmp
|= systab64
->stderr_handle
;
496 efi_systab
.stderr
= systab64
->stderr
;
497 tmp
|= systab64
->stderr
;
498 efi_systab
.runtime
= data
?
499 (void *)(unsigned long)data
->runtime
:
500 (void *)(unsigned long)systab64
->runtime
;
501 tmp
|= data
? data
->runtime
: systab64
->runtime
;
502 efi_systab
.boottime
= (void *)(unsigned long)systab64
->boottime
;
503 tmp
|= systab64
->boottime
;
504 efi_systab
.nr_tables
= systab64
->nr_tables
;
505 efi_systab
.tables
= data
? (unsigned long)data
->tables
:
507 tmp
|= data
? data
->tables
: systab64
->tables
;
509 early_iounmap(systab64
, sizeof(*systab64
));
511 early_iounmap(data
, sizeof(*data
));
514 pr_err("EFI data located above 4GB, disabling EFI.\n");
519 efi_system_table_32_t
*systab32
;
521 systab32
= early_ioremap((unsigned long)phys
,
523 if (systab32
== NULL
) {
524 pr_err("Couldn't map the system table!\n");
528 efi_systab
.hdr
= systab32
->hdr
;
529 efi_systab
.fw_vendor
= systab32
->fw_vendor
;
530 efi_systab
.fw_revision
= systab32
->fw_revision
;
531 efi_systab
.con_in_handle
= systab32
->con_in_handle
;
532 efi_systab
.con_in
= systab32
->con_in
;
533 efi_systab
.con_out_handle
= systab32
->con_out_handle
;
534 efi_systab
.con_out
= systab32
->con_out
;
535 efi_systab
.stderr_handle
= systab32
->stderr_handle
;
536 efi_systab
.stderr
= systab32
->stderr
;
537 efi_systab
.runtime
= (void *)(unsigned long)systab32
->runtime
;
538 efi_systab
.boottime
= (void *)(unsigned long)systab32
->boottime
;
539 efi_systab
.nr_tables
= systab32
->nr_tables
;
540 efi_systab
.tables
= systab32
->tables
;
542 early_iounmap(systab32
, sizeof(*systab32
));
545 efi
.systab
= &efi_systab
;
548 * Verify the EFI Table
550 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
551 pr_err("System table signature incorrect!\n");
554 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
555 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
556 efi
.systab
->hdr
.revision
>> 16,
557 efi
.systab
->hdr
.revision
& 0xffff);
559 set_bit(EFI_SYSTEM_TABLES
, &efi
.flags
);
564 static int __init
efi_runtime_init32(void)
566 efi_runtime_services_32_t
*runtime
;
568 runtime
= early_ioremap((unsigned long)efi
.systab
->runtime
,
569 sizeof(efi_runtime_services_32_t
));
571 pr_err("Could not map the runtime service table!\n");
576 * We will only need *early* access to the following two
577 * EFI runtime services before set_virtual_address_map
580 efi_phys
.set_virtual_address_map
=
581 (efi_set_virtual_address_map_t
*)
582 (unsigned long)runtime
->set_virtual_address_map
;
583 early_iounmap(runtime
, sizeof(efi_runtime_services_32_t
));
588 static int __init
efi_runtime_init64(void)
590 efi_runtime_services_64_t
*runtime
;
592 runtime
= early_ioremap((unsigned long)efi
.systab
->runtime
,
593 sizeof(efi_runtime_services_64_t
));
595 pr_err("Could not map the runtime service table!\n");
600 * We will only need *early* access to the following two
601 * EFI runtime services before set_virtual_address_map
604 efi_phys
.set_virtual_address_map
=
605 (efi_set_virtual_address_map_t
*)
606 (unsigned long)runtime
->set_virtual_address_map
;
607 early_iounmap(runtime
, sizeof(efi_runtime_services_64_t
));
612 static int __init
efi_runtime_init(void)
617 * Check out the runtime services table. We need to map
618 * the runtime services table so that we can grab the physical
619 * address of several of the EFI runtime functions, needed to
620 * set the firmware into virtual mode.
622 if (efi_enabled(EFI_64BIT
))
623 rv
= efi_runtime_init64();
625 rv
= efi_runtime_init32();
630 set_bit(EFI_RUNTIME_SERVICES
, &efi
.flags
);
635 static int __init
efi_memmap_init(void)
637 /* Map the EFI memory map */
638 memmap
.map
= early_ioremap((unsigned long)memmap
.phys_map
,
639 memmap
.nr_map
* memmap
.desc_size
);
640 if (memmap
.map
== NULL
) {
641 pr_err("Could not map the memory map!\n");
644 memmap
.map_end
= memmap
.map
+ (memmap
.nr_map
* memmap
.desc_size
);
649 set_bit(EFI_MEMMAP
, &efi
.flags
);
655 * A number of config table entries get remapped to virtual addresses
656 * after entering EFI virtual mode. However, the kexec kernel requires
657 * their physical addresses therefore we pass them via setup_data and
658 * correct those entries to their respective physical addresses here.
660 * Currently only handles smbios which is necessary for some firmware
663 static int __init
efi_reuse_config(u64 tables
, int nr_tables
)
667 struct efi_setup_data
*data
;
672 if (!efi_enabled(EFI_64BIT
))
675 data
= early_memremap(efi_setup
, sizeof(*data
));
684 sz
= sizeof(efi_config_table_64_t
);
686 p
= tablep
= early_memremap(tables
, nr_tables
* sz
);
688 pr_err("Could not map Configuration table!\n");
693 for (i
= 0; i
< efi
.systab
->nr_tables
; i
++) {
696 guid
= ((efi_config_table_64_t
*)p
)->guid
;
698 if (!efi_guidcmp(guid
, SMBIOS_TABLE_GUID
))
699 ((efi_config_table_64_t
*)p
)->table
= data
->smbios
;
702 early_iounmap(tablep
, nr_tables
* sz
);
705 early_iounmap(data
, sizeof(*data
));
710 void __init
efi_init(void)
713 char vendor
[100] = "unknown";
718 if (boot_params
.efi_info
.efi_systab_hi
||
719 boot_params
.efi_info
.efi_memmap_hi
) {
720 pr_info("Table located above 4GB, disabling EFI.\n");
723 efi_phys
.systab
= (efi_system_table_t
*)boot_params
.efi_info
.efi_systab
;
725 efi_phys
.systab
= (efi_system_table_t
*)
726 (boot_params
.efi_info
.efi_systab
|
727 ((__u64
)boot_params
.efi_info
.efi_systab_hi
<<32));
730 if (efi_systab_init(efi_phys
.systab
))
733 set_bit(EFI_SYSTEM_TABLES
, &efi
.flags
);
735 efi
.config_table
= (unsigned long)efi
.systab
->tables
;
736 efi
.fw_vendor
= (unsigned long)efi
.systab
->fw_vendor
;
737 efi
.runtime
= (unsigned long)efi
.systab
->runtime
;
740 * Show what we know for posterity
742 c16
= tmp
= early_ioremap(efi
.systab
->fw_vendor
, 2);
744 for (i
= 0; i
< sizeof(vendor
) - 1 && *c16
; ++i
)
748 pr_err("Could not map the firmware vendor!\n");
749 early_iounmap(tmp
, 2);
751 pr_info("EFI v%u.%.02u by %s\n",
752 efi
.systab
->hdr
.revision
>> 16,
753 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
755 if (efi_reuse_config(efi
.systab
->tables
, efi
.systab
->nr_tables
))
758 if (efi_config_init(arch_tables
))
762 * Note: We currently don't support runtime services on an EFI
763 * that doesn't match the kernel 32/64-bit mode.
766 if (!efi_runtime_supported())
767 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
769 if (disable_runtime
|| efi_runtime_init())
772 if (efi_memmap_init())
775 set_bit(EFI_MEMMAP
, &efi
.flags
);
780 void __init
efi_late_init(void)
785 void __init
efi_set_executable(efi_memory_desc_t
*md
, bool executable
)
789 addr
= md
->virt_addr
;
790 npages
= md
->num_pages
;
792 memrange_efi_to_native(&addr
, &npages
);
795 set_memory_x(addr
, npages
);
797 set_memory_nx(addr
, npages
);
800 void __init
runtime_code_page_mkexec(void)
802 efi_memory_desc_t
*md
;
805 /* Make EFI runtime service code area executable */
806 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
809 if (md
->type
!= EFI_RUNTIME_SERVICES_CODE
)
812 efi_set_executable(md
, true);
816 void efi_memory_uc(u64 addr
, unsigned long size
)
818 unsigned long page_shift
= 1UL << EFI_PAGE_SHIFT
;
821 npages
= round_up(size
, page_shift
) / page_shift
;
822 memrange_efi_to_native(&addr
, &npages
);
823 set_memory_uc(addr
, npages
);
826 void __init
old_map_region(efi_memory_desc_t
*md
)
828 u64 start_pfn
, end_pfn
, end
;
832 start_pfn
= PFN_DOWN(md
->phys_addr
);
833 size
= md
->num_pages
<< PAGE_SHIFT
;
834 end
= md
->phys_addr
+ size
;
835 end_pfn
= PFN_UP(end
);
837 if (pfn_range_is_mapped(start_pfn
, end_pfn
)) {
838 va
= __va(md
->phys_addr
);
840 if (!(md
->attribute
& EFI_MEMORY_WB
))
841 efi_memory_uc((u64
)(unsigned long)va
, size
);
843 va
= efi_ioremap(md
->phys_addr
, size
,
844 md
->type
, md
->attribute
);
846 md
->virt_addr
= (u64
) (unsigned long) va
;
848 pr_err("ioremap of 0x%llX failed!\n",
849 (unsigned long long)md
->phys_addr
);
852 static void native_runtime_setup(void)
854 efi
.get_time
= virt_efi_get_time
;
855 efi
.set_time
= virt_efi_set_time
;
856 efi
.get_wakeup_time
= virt_efi_get_wakeup_time
;
857 efi
.set_wakeup_time
= virt_efi_set_wakeup_time
;
858 efi
.get_variable
= virt_efi_get_variable
;
859 efi
.get_next_variable
= virt_efi_get_next_variable
;
860 efi
.set_variable
= virt_efi_set_variable
;
861 efi
.get_next_high_mono_count
= virt_efi_get_next_high_mono_count
;
862 efi
.reset_system
= virt_efi_reset_system
;
863 efi
.query_variable_info
= virt_efi_query_variable_info
;
864 efi
.update_capsule
= virt_efi_update_capsule
;
865 efi
.query_capsule_caps
= virt_efi_query_capsule_caps
;
868 /* Merge contiguous regions of the same type and attribute */
869 static void __init
efi_merge_regions(void)
872 efi_memory_desc_t
*md
, *prev_md
= NULL
;
874 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
883 if (prev_md
->type
!= md
->type
||
884 prev_md
->attribute
!= md
->attribute
) {
889 prev_size
= prev_md
->num_pages
<< EFI_PAGE_SHIFT
;
891 if (md
->phys_addr
== (prev_md
->phys_addr
+ prev_size
)) {
892 prev_md
->num_pages
+= md
->num_pages
;
893 md
->type
= EFI_RESERVED_TYPE
;
901 static void __init
get_systab_virt_addr(efi_memory_desc_t
*md
)
906 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
907 end
= md
->phys_addr
+ size
;
908 systab
= (u64
)(unsigned long)efi_phys
.systab
;
909 if (md
->phys_addr
<= systab
&& systab
< end
) {
910 systab
+= md
->virt_addr
- md
->phys_addr
;
911 efi
.systab
= (efi_system_table_t
*)(unsigned long)systab
;
915 static void __init
save_runtime_map(void)
918 efi_memory_desc_t
*md
;
919 void *tmp
, *p
, *q
= NULL
;
922 if (efi_enabled(EFI_OLD_MEMMAP
))
925 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
928 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
) ||
929 (md
->type
== EFI_BOOT_SERVICES_CODE
) ||
930 (md
->type
== EFI_BOOT_SERVICES_DATA
))
932 tmp
= krealloc(q
, (count
+ 1) * memmap
.desc_size
, GFP_KERNEL
);
937 memcpy(q
+ count
* memmap
.desc_size
, md
, memmap
.desc_size
);
941 efi_runtime_map_setup(q
, count
, memmap
.desc_size
);
946 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
950 static void *realloc_pages(void *old_memmap
, int old_shift
)
954 ret
= (void *)__get_free_pages(GFP_KERNEL
, old_shift
+ 1);
959 * A first-time allocation doesn't have anything to copy.
964 memcpy(ret
, old_memmap
, PAGE_SIZE
<< old_shift
);
967 free_pages((unsigned long)old_memmap
, old_shift
);
972 * Map the efi memory ranges of the runtime services and update new_mmap with
975 static void * __init
efi_map_regions(int *count
, int *pg_shift
)
977 void *p
, *new_memmap
= NULL
;
978 unsigned long left
= 0;
979 efi_memory_desc_t
*md
;
981 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
983 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
)) {
985 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
986 md
->type
!= EFI_BOOT_SERVICES_DATA
)
992 get_systab_virt_addr(md
);
994 if (left
< memmap
.desc_size
) {
995 new_memmap
= realloc_pages(new_memmap
, *pg_shift
);
999 left
+= PAGE_SIZE
<< *pg_shift
;
1003 memcpy(new_memmap
+ (*count
* memmap
.desc_size
), md
,
1006 left
-= memmap
.desc_size
;
1013 static void __init
kexec_enter_virtual_mode(void)
1016 efi_memory_desc_t
*md
;
1022 * We don't do virtual mode, since we don't do runtime services, on
1025 if (!efi_is_native()) {
1031 * Map efi regions which were passed via setup_data. The virt_addr is a
1032 * fixed addr which was used in first kernel of a kexec boot.
1034 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
1036 efi_map_region_fixed(md
); /* FIXME: add error handling */
1037 get_systab_virt_addr(md
);
1042 BUG_ON(!efi
.systab
);
1044 efi_sync_low_kernel_mappings();
1047 * Now that EFI is in virtual mode, update the function
1048 * pointers in the runtime service table to the new virtual addresses.
1050 * Call EFI services through wrapper functions.
1052 efi
.runtime_version
= efi_systab
.hdr
.revision
;
1054 native_runtime_setup();
1056 efi
.set_virtual_address_map
= NULL
;
1058 if (efi_enabled(EFI_OLD_MEMMAP
) && (__supported_pte_mask
& _PAGE_NX
))
1059 runtime_code_page_mkexec();
1061 /* clean DUMMY object */
1062 efi
.set_variable(efi_dummy_name
, &EFI_DUMMY_GUID
,
1063 EFI_VARIABLE_NON_VOLATILE
|
1064 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1065 EFI_VARIABLE_RUNTIME_ACCESS
,
1071 * This function will switch the EFI runtime services to virtual mode.
1072 * Essentially, we look through the EFI memmap and map every region that
1073 * has the runtime attribute bit set in its memory descriptor into the
1074 * ->trampoline_pgd page table using a top-down VA allocation scheme.
1076 * The old method which used to update that memory descriptor with the
1077 * virtual address obtained from ioremap() is still supported when the
1078 * kernel is booted with efi=old_map on its command line. Same old
1079 * method enabled the runtime services to be called without having to
1080 * thunk back into physical mode for every invocation.
1082 * The new method does a pagetable switch in a preemption-safe manner
1083 * so that we're in a different address space when calling a runtime
1084 * function. For function arguments passing we do copy the PGDs of the
1085 * kernel page table into ->trampoline_pgd prior to each call.
1087 * Specially for kexec boot, efi runtime maps in previous kernel should
1088 * be passed in via setup_data. In that case runtime ranges will be mapped
1089 * to the same virtual addresses as the first kernel, see
1090 * kexec_enter_virtual_mode().
1092 static void __init
__efi_enter_virtual_mode(void)
1094 int count
= 0, pg_shift
= 0;
1095 void *new_memmap
= NULL
;
1096 efi_status_t status
;
1100 efi_merge_regions();
1101 new_memmap
= efi_map_regions(&count
, &pg_shift
);
1103 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1109 BUG_ON(!efi
.systab
);
1111 if (efi_setup_page_tables(__pa(new_memmap
), 1 << pg_shift
))
1114 efi_sync_low_kernel_mappings();
1115 efi_dump_pagetable();
1117 if (efi_is_native()) {
1118 status
= phys_efi_set_virtual_address_map(
1119 memmap
.desc_size
* count
,
1121 memmap
.desc_version
,
1122 (efi_memory_desc_t
*)__pa(new_memmap
));
1124 status
= efi_thunk_set_virtual_address_map(
1125 efi_phys
.set_virtual_address_map
,
1126 memmap
.desc_size
* count
,
1128 memmap
.desc_version
,
1129 (efi_memory_desc_t
*)__pa(new_memmap
));
1132 if (status
!= EFI_SUCCESS
) {
1133 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1135 panic("EFI call to SetVirtualAddressMap() failed!");
1139 * Now that EFI is in virtual mode, update the function
1140 * pointers in the runtime service table to the new virtual addresses.
1142 * Call EFI services through wrapper functions.
1144 efi
.runtime_version
= efi_systab
.hdr
.revision
;
1146 if (efi_is_native())
1147 native_runtime_setup();
1149 efi_thunk_runtime_setup();
1151 efi
.set_virtual_address_map
= NULL
;
1153 efi_runtime_mkexec();
1156 * We mapped the descriptor array into the EFI pagetable above but we're
1157 * not unmapping it here. Here's why:
1159 * We're copying select PGDs from the kernel page table to the EFI page
1160 * table and when we do so and make changes to those PGDs like unmapping
1161 * stuff from them, those changes appear in the kernel page table and we
1164 * From setup_real_mode():
1167 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1169 * In this particular case, our allocation is in PGD 0 of the EFI page
1170 * table but we've copied that PGD from PGD[272] of the EFI page table:
1172 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1174 * where the direct memory mapping in kernel space is.
1176 * new_memmap's VA comes from that direct mapping and thus clearing it,
1177 * it would get cleared in the kernel page table too.
1179 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1181 free_pages((unsigned long)new_memmap
, pg_shift
);
1183 /* clean DUMMY object */
1184 efi
.set_variable(efi_dummy_name
, &EFI_DUMMY_GUID
,
1185 EFI_VARIABLE_NON_VOLATILE
|
1186 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1187 EFI_VARIABLE_RUNTIME_ACCESS
,
1191 void __init
efi_enter_virtual_mode(void)
1194 kexec_enter_virtual_mode();
1196 __efi_enter_virtual_mode();
1200 * Convenience functions to obtain memory types and attributes
1202 u32
efi_mem_type(unsigned long phys_addr
)
1204 efi_memory_desc_t
*md
;
1207 if (!efi_enabled(EFI_MEMMAP
))
1210 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
1212 if ((md
->phys_addr
<= phys_addr
) &&
1213 (phys_addr
< (md
->phys_addr
+
1214 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
1220 u64
efi_mem_attributes(unsigned long phys_addr
)
1222 efi_memory_desc_t
*md
;
1225 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
1227 if ((md
->phys_addr
<= phys_addr
) &&
1228 (phys_addr
< (md
->phys_addr
+
1229 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
1230 return md
->attribute
;
1236 * Some firmware implementations refuse to boot if there's insufficient space
1237 * in the variable store. Ensure that we never use more than a safe limit.
1239 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1242 efi_status_t
efi_query_variable_store(u32 attributes
, unsigned long size
)
1244 efi_status_t status
;
1245 u64 storage_size
, remaining_size
, max_size
;
1247 if (!(attributes
& EFI_VARIABLE_NON_VOLATILE
))
1250 status
= efi
.query_variable_info(attributes
, &storage_size
,
1251 &remaining_size
, &max_size
);
1252 if (status
!= EFI_SUCCESS
)
1256 * We account for that by refusing the write if permitting it would
1257 * reduce the available space to under 5KB. This figure was provided by
1258 * Samsung, so should be safe.
1260 if ((remaining_size
- size
< EFI_MIN_RESERVE
) &&
1261 !efi_no_storage_paranoia
) {
1264 * Triggering garbage collection may require that the firmware
1265 * generate a real EFI_OUT_OF_RESOURCES error. We can force
1266 * that by attempting to use more space than is available.
1268 unsigned long dummy_size
= remaining_size
+ 1024;
1269 void *dummy
= kzalloc(dummy_size
, GFP_ATOMIC
);
1272 return EFI_OUT_OF_RESOURCES
;
1274 status
= efi
.set_variable(efi_dummy_name
, &EFI_DUMMY_GUID
,
1275 EFI_VARIABLE_NON_VOLATILE
|
1276 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1277 EFI_VARIABLE_RUNTIME_ACCESS
,
1280 if (status
== EFI_SUCCESS
) {
1282 * This should have failed, so if it didn't make sure
1283 * that we delete it...
1285 efi
.set_variable(efi_dummy_name
, &EFI_DUMMY_GUID
,
1286 EFI_VARIABLE_NON_VOLATILE
|
1287 EFI_VARIABLE_BOOTSERVICE_ACCESS
|
1288 EFI_VARIABLE_RUNTIME_ACCESS
,
1295 * The runtime code may now have triggered a garbage collection
1296 * run, so check the variable info again
1298 status
= efi
.query_variable_info(attributes
, &storage_size
,
1299 &remaining_size
, &max_size
);
1301 if (status
!= EFI_SUCCESS
)
1305 * There still isn't enough room, so return an error
1307 if (remaining_size
- size
< EFI_MIN_RESERVE
)
1308 return EFI_OUT_OF_RESOURCES
;
1313 EXPORT_SYMBOL_GPL(efi_query_variable_store
);
1315 static int __init
parse_efi_cmdline(char *str
)
1320 if (!strncmp(str
, "old_map", 7))
1321 set_bit(EFI_OLD_MEMMAP
, &efi
.flags
);
1325 early_param("efi", parse_efi_cmdline
);
1327 void __init
efi_apply_memmap_quirks(void)
1330 * Once setup is done earlier, unmap the EFI memory map on mismatched
1331 * firmware/kernel architectures since there is no support for runtime
1334 if (!efi_runtime_supported()) {
1335 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1340 * UV doesn't support the new EFI pagetable mapping yet.
1343 set_bit(EFI_OLD_MEMMAP
, &efi
.flags
);