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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/efi.h>
34 #include <linux/efi-bgrt.h>
35 #include <linux/export.h>
36 #include <linux/bootmem.h>
37 #include <linux/memblock.h>
38 #include <linux/spinlock.h>
39 #include <linux/uaccess.h>
40 #include <linux/time.h>
42 #include <linux/reboot.h>
43 #include <linux/bcd.h>
45 #include <asm/setup.h>
48 #include <asm/cacheflush.h>
49 #include <asm/tlbflush.h>
50 #include <asm/x86_init.h>
54 struct efi __read_mostly efi
= {
55 .mps
= EFI_INVALID_TABLE_ADDR
,
56 .acpi
= EFI_INVALID_TABLE_ADDR
,
57 .acpi20
= EFI_INVALID_TABLE_ADDR
,
58 .smbios
= EFI_INVALID_TABLE_ADDR
,
59 .sal_systab
= EFI_INVALID_TABLE_ADDR
,
60 .boot_info
= EFI_INVALID_TABLE_ADDR
,
61 .hcdp
= EFI_INVALID_TABLE_ADDR
,
62 .uga
= EFI_INVALID_TABLE_ADDR
,
63 .uv_systab
= EFI_INVALID_TABLE_ADDR
,
67 struct efi_memory_map memmap
;
69 static struct efi efi_phys __initdata
;
70 static efi_system_table_t efi_systab __initdata
;
72 static inline bool efi_is_native(void)
74 return IS_ENABLED(CONFIG_X86_64
) == efi_enabled(EFI_64BIT
);
77 unsigned long x86_efi_facility
;
80 * Returns 1 if 'facility' is enabled, 0 otherwise.
82 int efi_enabled(int facility
)
84 return test_bit(facility
, &x86_efi_facility
) != 0;
86 EXPORT_SYMBOL(efi_enabled
);
88 static bool __initdata disable_runtime
= false;
89 static int __init
setup_noefi(char *arg
)
91 disable_runtime
= true;
94 early_param("noefi", setup_noefi
);
97 EXPORT_SYMBOL(add_efi_memmap
);
99 static int __init
setup_add_efi_memmap(char *arg
)
104 early_param("add_efi_memmap", setup_add_efi_memmap
);
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 static efi_status_t __init
phys_efi_get_time(efi_time_t
*tm
,
255 spin_lock_irqsave(&rtc_lock
, flags
);
256 efi_call_phys_prelog();
257 status
= efi_call_phys2(efi_phys
.get_time
, virt_to_phys(tm
),
259 efi_call_phys_epilog();
260 spin_unlock_irqrestore(&rtc_lock
, flags
);
264 int efi_set_rtc_mmss(unsigned long nowtime
)
266 int real_seconds
, real_minutes
;
271 status
= efi
.get_time(&eft
, &cap
);
272 if (status
!= EFI_SUCCESS
) {
273 pr_err("Oops: efitime: can't read time!\n");
277 real_seconds
= nowtime
% 60;
278 real_minutes
= nowtime
/ 60;
279 if (((abs(real_minutes
- eft
.minute
) + 15)/30) & 1)
282 eft
.minute
= real_minutes
;
283 eft
.second
= real_seconds
;
285 status
= efi
.set_time(&eft
);
286 if (status
!= EFI_SUCCESS
) {
287 pr_err("Oops: efitime: can't write time!\n");
293 unsigned long efi_get_time(void)
299 status
= efi
.get_time(&eft
, &cap
);
300 if (status
!= EFI_SUCCESS
)
301 pr_err("Oops: efitime: can't read time!\n");
303 return mktime(eft
.year
, eft
.month
, eft
.day
, eft
.hour
,
304 eft
.minute
, eft
.second
);
308 * Tell the kernel about the EFI memory map. This might include
309 * more than the max 128 entries that can fit in the e820 legacy
310 * (zeropage) memory map.
313 static void __init
do_add_efi_memmap(void)
317 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
318 efi_memory_desc_t
*md
= p
;
319 unsigned long long start
= md
->phys_addr
;
320 unsigned long long size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
324 case EFI_LOADER_CODE
:
325 case EFI_LOADER_DATA
:
326 case EFI_BOOT_SERVICES_CODE
:
327 case EFI_BOOT_SERVICES_DATA
:
328 case EFI_CONVENTIONAL_MEMORY
:
329 if (md
->attribute
& EFI_MEMORY_WB
)
330 e820_type
= E820_RAM
;
332 e820_type
= E820_RESERVED
;
334 case EFI_ACPI_RECLAIM_MEMORY
:
335 e820_type
= E820_ACPI
;
337 case EFI_ACPI_MEMORY_NVS
:
338 e820_type
= E820_NVS
;
340 case EFI_UNUSABLE_MEMORY
:
341 e820_type
= E820_UNUSABLE
;
345 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
346 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
347 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
349 e820_type
= E820_RESERVED
;
352 e820_add_region(start
, size
, e820_type
);
354 sanitize_e820_map(e820
.map
, ARRAY_SIZE(e820
.map
), &e820
.nr_map
);
357 int __init
efi_memblock_x86_reserve_range(void)
362 /* Can't handle data above 4GB at this time */
363 if (boot_params
.efi_info
.efi_memmap_hi
) {
364 pr_err("Memory map is above 4GB, disabling EFI.\n");
367 pmap
= boot_params
.efi_info
.efi_memmap
;
369 pmap
= (boot_params
.efi_info
.efi_memmap
|
370 ((__u64
)boot_params
.efi_info
.efi_memmap_hi
<<32));
372 memmap
.phys_map
= (void *)pmap
;
373 memmap
.nr_map
= boot_params
.efi_info
.efi_memmap_size
/
374 boot_params
.efi_info
.efi_memdesc_size
;
375 memmap
.desc_version
= boot_params
.efi_info
.efi_memdesc_version
;
376 memmap
.desc_size
= boot_params
.efi_info
.efi_memdesc_size
;
377 memblock_reserve(pmap
, memmap
.nr_map
* memmap
.desc_size
);
383 static void __init
print_efi_memmap(void)
385 efi_memory_desc_t
*md
;
389 for (p
= memmap
.map
, i
= 0;
391 p
+= memmap
.desc_size
, i
++) {
393 pr_info("mem%02u: type=%u, attr=0x%llx, "
394 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
395 i
, md
->type
, md
->attribute
, md
->phys_addr
,
396 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
397 (md
->num_pages
>> (20 - EFI_PAGE_SHIFT
)));
400 #endif /* EFI_DEBUG */
402 void __init
efi_reserve_boot_services(void)
406 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
407 efi_memory_desc_t
*md
= p
;
408 u64 start
= md
->phys_addr
;
409 u64 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
411 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
412 md
->type
!= EFI_BOOT_SERVICES_DATA
)
414 /* Only reserve where possible:
415 * - Not within any already allocated areas
416 * - Not over any memory area (really needed, if above?)
417 * - Not within any part of the kernel
418 * - Not the bios reserved area
420 if ((start
+size
>= virt_to_phys(_text
)
421 && start
<= virt_to_phys(_end
)) ||
422 !e820_all_mapped(start
, start
+size
, E820_RAM
) ||
423 memblock_is_region_reserved(start
, size
)) {
424 /* Could not reserve, skip it */
426 memblock_dbg("Could not reserve boot range "
427 "[0x%010llx-0x%010llx]\n",
428 start
, start
+size
-1);
430 memblock_reserve(start
, size
);
434 void __init
efi_unmap_memmap(void)
436 clear_bit(EFI_MEMMAP
, &x86_efi_facility
);
438 early_iounmap(memmap
.map
, memmap
.nr_map
* memmap
.desc_size
);
443 void __init
efi_free_boot_services(void)
447 if (!efi_is_native())
450 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
451 efi_memory_desc_t
*md
= p
;
452 unsigned long long start
= md
->phys_addr
;
453 unsigned long long size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
455 if (md
->type
!= EFI_BOOT_SERVICES_CODE
&&
456 md
->type
!= EFI_BOOT_SERVICES_DATA
)
459 /* Could not reserve boot area */
463 free_bootmem_late(start
, size
);
469 static int __init
efi_systab_init(void *phys
)
471 if (efi_enabled(EFI_64BIT
)) {
472 efi_system_table_64_t
*systab64
;
475 systab64
= early_ioremap((unsigned long)phys
,
477 if (systab64
== NULL
) {
478 pr_err("Couldn't map the system table!\n");
482 efi_systab
.hdr
= systab64
->hdr
;
483 efi_systab
.fw_vendor
= systab64
->fw_vendor
;
484 tmp
|= 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
= (void *)(unsigned long)systab64
->runtime
;
499 tmp
|= systab64
->runtime
;
500 efi_systab
.boottime
= (void *)(unsigned long)systab64
->boottime
;
501 tmp
|= systab64
->boottime
;
502 efi_systab
.nr_tables
= systab64
->nr_tables
;
503 efi_systab
.tables
= systab64
->tables
;
504 tmp
|= systab64
->tables
;
506 early_iounmap(systab64
, sizeof(*systab64
));
509 pr_err("EFI data located above 4GB, disabling EFI.\n");
514 efi_system_table_32_t
*systab32
;
516 systab32
= early_ioremap((unsigned long)phys
,
518 if (systab32
== NULL
) {
519 pr_err("Couldn't map the system table!\n");
523 efi_systab
.hdr
= systab32
->hdr
;
524 efi_systab
.fw_vendor
= systab32
->fw_vendor
;
525 efi_systab
.fw_revision
= systab32
->fw_revision
;
526 efi_systab
.con_in_handle
= systab32
->con_in_handle
;
527 efi_systab
.con_in
= systab32
->con_in
;
528 efi_systab
.con_out_handle
= systab32
->con_out_handle
;
529 efi_systab
.con_out
= systab32
->con_out
;
530 efi_systab
.stderr_handle
= systab32
->stderr_handle
;
531 efi_systab
.stderr
= systab32
->stderr
;
532 efi_systab
.runtime
= (void *)(unsigned long)systab32
->runtime
;
533 efi_systab
.boottime
= (void *)(unsigned long)systab32
->boottime
;
534 efi_systab
.nr_tables
= systab32
->nr_tables
;
535 efi_systab
.tables
= systab32
->tables
;
537 early_iounmap(systab32
, sizeof(*systab32
));
540 efi
.systab
= &efi_systab
;
543 * Verify the EFI Table
545 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
546 pr_err("System table signature incorrect!\n");
549 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
550 pr_err("Warning: System table version "
551 "%d.%02d, expected 1.00 or greater!\n",
552 efi
.systab
->hdr
.revision
>> 16,
553 efi
.systab
->hdr
.revision
& 0xffff);
558 static int __init
efi_config_init(u64 tables
, int nr_tables
)
560 void *config_tables
, *tablep
;
563 if (efi_enabled(EFI_64BIT
))
564 sz
= sizeof(efi_config_table_64_t
);
566 sz
= sizeof(efi_config_table_32_t
);
569 * Let's see what config tables the firmware passed to us.
571 config_tables
= early_ioremap(tables
, nr_tables
* sz
);
572 if (config_tables
== NULL
) {
573 pr_err("Could not map Configuration table!\n");
577 tablep
= config_tables
;
579 for (i
= 0; i
< efi
.systab
->nr_tables
; i
++) {
583 if (efi_enabled(EFI_64BIT
)) {
585 guid
= ((efi_config_table_64_t
*)tablep
)->guid
;
586 table64
= ((efi_config_table_64_t
*)tablep
)->table
;
591 pr_err("Table located above 4GB, disabling EFI.\n");
592 early_iounmap(config_tables
,
593 efi
.systab
->nr_tables
* sz
);
598 guid
= ((efi_config_table_32_t
*)tablep
)->guid
;
599 table
= ((efi_config_table_32_t
*)tablep
)->table
;
601 if (!efi_guidcmp(guid
, MPS_TABLE_GUID
)) {
603 pr_cont(" MPS=0x%lx ", table
);
604 } else if (!efi_guidcmp(guid
, ACPI_20_TABLE_GUID
)) {
606 pr_cont(" ACPI 2.0=0x%lx ", table
);
607 } else if (!efi_guidcmp(guid
, ACPI_TABLE_GUID
)) {
609 pr_cont(" ACPI=0x%lx ", table
);
610 } else if (!efi_guidcmp(guid
, SMBIOS_TABLE_GUID
)) {
612 pr_cont(" SMBIOS=0x%lx ", table
);
614 } else if (!efi_guidcmp(guid
, UV_SYSTEM_TABLE_GUID
)) {
615 efi
.uv_systab
= table
;
616 pr_cont(" UVsystab=0x%lx ", table
);
618 } else if (!efi_guidcmp(guid
, HCDP_TABLE_GUID
)) {
620 pr_cont(" HCDP=0x%lx ", table
);
621 } else if (!efi_guidcmp(guid
, UGA_IO_PROTOCOL_GUID
)) {
623 pr_cont(" UGA=0x%lx ", table
);
628 early_iounmap(config_tables
, efi
.systab
->nr_tables
* sz
);
632 static int __init
efi_runtime_init(void)
634 efi_runtime_services_t
*runtime
;
637 * Check out the runtime services table. We need to map
638 * the runtime services table so that we can grab the physical
639 * address of several of the EFI runtime functions, needed to
640 * set the firmware into virtual mode.
642 runtime
= early_ioremap((unsigned long)efi
.systab
->runtime
,
643 sizeof(efi_runtime_services_t
));
645 pr_err("Could not map the runtime service table!\n");
649 * We will only need *early* access to the following
650 * two EFI runtime services before set_virtual_address_map
653 efi_phys
.get_time
= (efi_get_time_t
*)runtime
->get_time
;
654 efi_phys
.set_virtual_address_map
=
655 (efi_set_virtual_address_map_t
*)
656 runtime
->set_virtual_address_map
;
658 * Make efi_get_time can be called before entering
661 efi
.get_time
= phys_efi_get_time
;
662 early_iounmap(runtime
, sizeof(efi_runtime_services_t
));
667 static int __init
efi_memmap_init(void)
669 /* Map the EFI memory map */
670 memmap
.map
= early_ioremap((unsigned long)memmap
.phys_map
,
671 memmap
.nr_map
* memmap
.desc_size
);
672 if (memmap
.map
== NULL
) {
673 pr_err("Could not map the memory map!\n");
676 memmap
.map_end
= memmap
.map
+ (memmap
.nr_map
* memmap
.desc_size
);
684 void __init
efi_init(void)
687 char vendor
[100] = "unknown";
692 if (boot_params
.efi_info
.efi_systab_hi
||
693 boot_params
.efi_info
.efi_memmap_hi
) {
694 pr_info("Table located above 4GB, disabling EFI.\n");
697 efi_phys
.systab
= (efi_system_table_t
*)boot_params
.efi_info
.efi_systab
;
699 efi_phys
.systab
= (efi_system_table_t
*)
700 (boot_params
.efi_info
.efi_systab
|
701 ((__u64
)boot_params
.efi_info
.efi_systab_hi
<<32));
704 if (efi_systab_init(efi_phys
.systab
))
707 set_bit(EFI_SYSTEM_TABLES
, &x86_efi_facility
);
710 * Show what we know for posterity
712 c16
= tmp
= early_ioremap(efi
.systab
->fw_vendor
, 2);
714 for (i
= 0; i
< sizeof(vendor
) - 1 && *c16
; ++i
)
718 pr_err("Could not map the firmware vendor!\n");
719 early_iounmap(tmp
, 2);
721 pr_info("EFI v%u.%.02u by %s\n",
722 efi
.systab
->hdr
.revision
>> 16,
723 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
725 if (efi_config_init(efi
.systab
->tables
, efi
.systab
->nr_tables
))
728 set_bit(EFI_CONFIG_TABLES
, &x86_efi_facility
);
731 * Note: We currently don't support runtime services on an EFI
732 * that doesn't match the kernel 32/64-bit mode.
735 if (!efi_is_native())
736 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
738 if (disable_runtime
|| efi_runtime_init())
740 set_bit(EFI_RUNTIME_SERVICES
, &x86_efi_facility
);
743 if (efi_memmap_init())
746 set_bit(EFI_MEMMAP
, &x86_efi_facility
);
749 if (efi_is_native()) {
750 x86_platform
.get_wallclock
= efi_get_time
;
751 x86_platform
.set_wallclock
= efi_set_rtc_mmss
;
760 void __init
efi_late_init(void)
765 void __init
efi_set_executable(efi_memory_desc_t
*md
, bool executable
)
769 addr
= md
->virt_addr
;
770 npages
= md
->num_pages
;
772 memrange_efi_to_native(&addr
, &npages
);
775 set_memory_x(addr
, npages
);
777 set_memory_nx(addr
, npages
);
780 static void __init
runtime_code_page_mkexec(void)
782 efi_memory_desc_t
*md
;
785 /* Make EFI runtime service code area executable */
786 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
789 if (md
->type
!= EFI_RUNTIME_SERVICES_CODE
)
792 efi_set_executable(md
, true);
797 * We can't ioremap data in EFI boot services RAM, because we've already mapped
798 * it as RAM. So, look it up in the existing EFI memory map instead. Only
799 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
801 void __iomem
*efi_lookup_mapped_addr(u64 phys_addr
)
804 if (WARN_ON(!memmap
.map
))
806 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
807 efi_memory_desc_t
*md
= p
;
808 u64 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
809 u64 end
= md
->phys_addr
+ size
;
810 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
) &&
811 md
->type
!= EFI_BOOT_SERVICES_CODE
&&
812 md
->type
!= EFI_BOOT_SERVICES_DATA
)
816 if (phys_addr
>= md
->phys_addr
&& phys_addr
< end
) {
817 phys_addr
+= md
->virt_addr
- md
->phys_addr
;
818 return (__force
void __iomem
*)(unsigned long)phys_addr
;
824 void efi_memory_uc(u64 addr
, unsigned long size
)
826 unsigned long page_shift
= 1UL << EFI_PAGE_SHIFT
;
829 npages
= round_up(size
, page_shift
) / page_shift
;
830 memrange_efi_to_native(&addr
, &npages
);
831 set_memory_uc(addr
, npages
);
835 * This function will switch the EFI runtime services to virtual mode.
836 * Essentially, look through the EFI memmap and map every region that
837 * has the runtime attribute bit set in its memory descriptor and update
838 * that memory descriptor with the virtual address obtained from ioremap().
839 * This enables the runtime services to be called without having to
840 * thunk back into physical mode for every invocation.
842 void __init
efi_enter_virtual_mode(void)
844 efi_memory_desc_t
*md
, *prev_md
= NULL
;
847 u64 end
, systab
, end_pfn
;
848 void *p
, *va
, *new_memmap
= NULL
;
854 * We don't do virtual mode, since we don't do runtime services, on
858 if (!efi_is_native()) {
863 /* Merge contiguous regions of the same type and attribute */
864 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
873 if (prev_md
->type
!= md
->type
||
874 prev_md
->attribute
!= md
->attribute
) {
879 prev_size
= prev_md
->num_pages
<< EFI_PAGE_SHIFT
;
881 if (md
->phys_addr
== (prev_md
->phys_addr
+ prev_size
)) {
882 prev_md
->num_pages
+= md
->num_pages
;
883 md
->type
= EFI_RESERVED_TYPE
;
890 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
892 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
) &&
893 md
->type
!= EFI_BOOT_SERVICES_CODE
&&
894 md
->type
!= EFI_BOOT_SERVICES_DATA
)
897 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
898 end
= md
->phys_addr
+ size
;
900 end_pfn
= PFN_UP(end
);
901 if (end_pfn
<= max_low_pfn_mapped
902 || (end_pfn
> (1UL << (32 - PAGE_SHIFT
))
903 && end_pfn
<= max_pfn_mapped
)) {
904 va
= __va(md
->phys_addr
);
906 if (!(md
->attribute
& EFI_MEMORY_WB
))
907 efi_memory_uc((u64
)(unsigned long)va
, size
);
909 va
= efi_ioremap(md
->phys_addr
, size
,
910 md
->type
, md
->attribute
);
912 md
->virt_addr
= (u64
) (unsigned long) va
;
915 pr_err("ioremap of 0x%llX failed!\n",
916 (unsigned long long)md
->phys_addr
);
920 systab
= (u64
) (unsigned long) efi_phys
.systab
;
921 if (md
->phys_addr
<= systab
&& systab
< end
) {
922 systab
+= md
->virt_addr
- md
->phys_addr
;
923 efi
.systab
= (efi_system_table_t
*) (unsigned long) systab
;
925 new_memmap
= krealloc(new_memmap
,
926 (count
+ 1) * memmap
.desc_size
,
928 memcpy(new_memmap
+ (count
* memmap
.desc_size
), md
,
935 status
= phys_efi_set_virtual_address_map(
936 memmap
.desc_size
* count
,
939 (efi_memory_desc_t
*)__pa(new_memmap
));
941 if (status
!= EFI_SUCCESS
) {
942 pr_alert("Unable to switch EFI into virtual mode "
943 "(status=%lx)!\n", status
);
944 panic("EFI call to SetVirtualAddressMap() failed!");
948 * Now that EFI is in virtual mode, update the function
949 * pointers in the runtime service table to the new virtual addresses.
951 * Call EFI services through wrapper functions.
953 efi
.runtime_version
= efi_systab
.hdr
.revision
;
954 efi
.get_time
= virt_efi_get_time
;
955 efi
.set_time
= virt_efi_set_time
;
956 efi
.get_wakeup_time
= virt_efi_get_wakeup_time
;
957 efi
.set_wakeup_time
= virt_efi_set_wakeup_time
;
958 efi
.get_variable
= virt_efi_get_variable
;
959 efi
.get_next_variable
= virt_efi_get_next_variable
;
960 efi
.set_variable
= virt_efi_set_variable
;
961 efi
.get_next_high_mono_count
= virt_efi_get_next_high_mono_count
;
962 efi
.reset_system
= virt_efi_reset_system
;
963 efi
.set_virtual_address_map
= NULL
;
964 efi
.query_variable_info
= virt_efi_query_variable_info
;
965 efi
.update_capsule
= virt_efi_update_capsule
;
966 efi
.query_capsule_caps
= virt_efi_query_capsule_caps
;
967 if (__supported_pte_mask
& _PAGE_NX
)
968 runtime_code_page_mkexec();
974 * Convenience functions to obtain memory types and attributes
976 u32
efi_mem_type(unsigned long phys_addr
)
978 efi_memory_desc_t
*md
;
981 if (!efi_enabled(EFI_MEMMAP
))
984 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
986 if ((md
->phys_addr
<= phys_addr
) &&
987 (phys_addr
< (md
->phys_addr
+
988 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
994 u64
efi_mem_attributes(unsigned long phys_addr
)
996 efi_memory_desc_t
*md
;
999 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
1001 if ((md
->phys_addr
<= phys_addr
) &&
1002 (phys_addr
< (md
->phys_addr
+
1003 (md
->num_pages
<< EFI_PAGE_SHIFT
))))
1004 return md
->attribute
;