2 * x86_64 specific EFI support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
5 * Copyright (C) 2005-2008 Intel Co.
6 * Fenghua Yu <fenghua.yu@intel.com>
7 * Bibo Mao <bibo.mao@intel.com>
8 * Chandramouli Narayanan <mouli@linux.intel.com>
9 * Huang Ying <ying.huang@intel.com>
11 * Code to convert EFI to E820 map has been implemented in elilo bootloader
12 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
13 * is setup appropriately for EFI runtime code.
18 #define pr_fmt(fmt) "efi: " fmt
20 #include <linux/kernel.h>
21 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/spinlock.h>
25 #include <linux/bootmem.h>
26 #include <linux/ioport.h>
27 #include <linux/init.h>
28 #include <linux/mc146818rtc.h>
29 #include <linux/efi.h>
30 #include <linux/uaccess.h>
32 #include <linux/reboot.h>
33 #include <linux/slab.h>
35 #include <asm/setup.h>
38 #include <asm/pgtable.h>
39 #include <asm/tlbflush.h>
40 #include <asm/proto.h>
42 #include <asm/cacheflush.h>
43 #include <asm/fixmap.h>
44 #include <asm/realmode.h>
46 #include <asm/pgalloc.h>
49 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
50 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
52 static u64 efi_va
= EFI_VA_START
;
54 struct efi_scratch efi_scratch
;
56 static void __init
early_code_mapping_set_exec(int executable
)
58 efi_memory_desc_t
*md
;
60 if (!(__supported_pte_mask
& _PAGE_NX
))
63 /* Make EFI service code area executable */
64 for_each_efi_memory_desc(md
) {
65 if (md
->type
== EFI_RUNTIME_SERVICES_CODE
||
66 md
->type
== EFI_BOOT_SERVICES_CODE
)
67 efi_set_executable(md
, executable
);
71 pgd_t
* __init
efi_call_phys_prolog(void)
73 unsigned long vaddress
;
79 if (!efi_enabled(EFI_OLD_MEMMAP
)) {
80 save_pgd
= (pgd_t
*)read_cr3();
81 write_cr3((unsigned long)efi_scratch
.efi_pgt
);
85 early_code_mapping_set_exec(1);
87 n_pgds
= DIV_ROUND_UP((max_pfn
<< PAGE_SHIFT
), PGDIR_SIZE
);
88 save_pgd
= kmalloc(n_pgds
* sizeof(pgd_t
), GFP_KERNEL
);
90 for (pgd
= 0; pgd
< n_pgds
; pgd
++) {
91 save_pgd
[pgd
] = *pgd_offset_k(pgd
* PGDIR_SIZE
);
92 vaddress
= (unsigned long)__va(pgd
* PGDIR_SIZE
);
93 set_pgd(pgd_offset_k(pgd
* PGDIR_SIZE
), *pgd_offset_k(vaddress
));
101 void __init
efi_call_phys_epilog(pgd_t
*save_pgd
)
104 * After the lock is released, the original page table is restored.
109 if (!efi_enabled(EFI_OLD_MEMMAP
)) {
110 write_cr3((unsigned long)save_pgd
);
115 nr_pgds
= DIV_ROUND_UP((max_pfn
<< PAGE_SHIFT
) , PGDIR_SIZE
);
117 for (pgd_idx
= 0; pgd_idx
< nr_pgds
; pgd_idx
++)
118 set_pgd(pgd_offset_k(pgd_idx
* PGDIR_SIZE
), save_pgd
[pgd_idx
]);
123 early_code_mapping_set_exec(0);
126 static pgd_t
*efi_pgd
;
129 * We need our own copy of the higher levels of the page tables
130 * because we want to avoid inserting EFI region mappings (EFI_VA_END
131 * to EFI_VA_START) into the standard kernel page tables. Everything
132 * else can be shared, see efi_sync_low_kernel_mappings().
134 int __init
efi_alloc_page_tables(void)
140 if (efi_enabled(EFI_OLD_MEMMAP
))
143 gfp_mask
= GFP_KERNEL
| __GFP_NOTRACK
| __GFP_ZERO
;
144 efi_pgd
= (pgd_t
*)__get_free_page(gfp_mask
);
148 pgd
= efi_pgd
+ pgd_index(EFI_VA_END
);
150 pud
= pud_alloc_one(NULL
, 0);
152 free_page((unsigned long)efi_pgd
);
156 pgd_populate(NULL
, pgd
, pud
);
162 * Add low kernel mappings for passing arguments to EFI functions.
164 void efi_sync_low_kernel_mappings(void)
166 unsigned num_entries
;
167 pgd_t
*pgd_k
, *pgd_efi
;
168 pud_t
*pud_k
, *pud_efi
;
170 if (efi_enabled(EFI_OLD_MEMMAP
))
174 * We can share all PGD entries apart from the one entry that
175 * covers the EFI runtime mapping space.
177 * Make sure the EFI runtime region mappings are guaranteed to
178 * only span a single PGD entry and that the entry also maps
179 * other important kernel regions.
181 BUILD_BUG_ON(pgd_index(EFI_VA_END
) != pgd_index(MODULES_END
));
182 BUILD_BUG_ON((EFI_VA_START
& PGDIR_MASK
) !=
183 (EFI_VA_END
& PGDIR_MASK
));
185 pgd_efi
= efi_pgd
+ pgd_index(PAGE_OFFSET
);
186 pgd_k
= pgd_offset_k(PAGE_OFFSET
);
188 num_entries
= pgd_index(EFI_VA_END
) - pgd_index(PAGE_OFFSET
);
189 memcpy(pgd_efi
, pgd_k
, sizeof(pgd_t
) * num_entries
);
192 * We share all the PUD entries apart from those that map the
193 * EFI regions. Copy around them.
195 BUILD_BUG_ON((EFI_VA_START
& ~PUD_MASK
) != 0);
196 BUILD_BUG_ON((EFI_VA_END
& ~PUD_MASK
) != 0);
198 pgd_efi
= efi_pgd
+ pgd_index(EFI_VA_END
);
199 pud_efi
= pud_offset(pgd_efi
, 0);
201 pgd_k
= pgd_offset_k(EFI_VA_END
);
202 pud_k
= pud_offset(pgd_k
, 0);
204 num_entries
= pud_index(EFI_VA_END
);
205 memcpy(pud_efi
, pud_k
, sizeof(pud_t
) * num_entries
);
207 pud_efi
= pud_offset(pgd_efi
, EFI_VA_START
);
208 pud_k
= pud_offset(pgd_k
, EFI_VA_START
);
210 num_entries
= PTRS_PER_PUD
- pud_index(EFI_VA_START
);
211 memcpy(pud_efi
, pud_k
, sizeof(pud_t
) * num_entries
);
214 int __init
efi_setup_page_tables(unsigned long pa_memmap
, unsigned num_pages
)
216 unsigned long pfn
, text
;
217 efi_memory_desc_t
*md
;
222 if (efi_enabled(EFI_OLD_MEMMAP
))
225 efi_scratch
.efi_pgt
= (pgd_t
*)__pa(efi_pgd
);
229 * It can happen that the physical address of new_memmap lands in memory
230 * which is not mapped in the EFI page table. Therefore we need to go
231 * and ident-map those pages containing the map before calling
232 * phys_efi_set_virtual_address_map().
234 pfn
= pa_memmap
>> PAGE_SHIFT
;
235 if (kernel_map_pages_in_pgd(pgd
, pfn
, pa_memmap
, num_pages
, _PAGE_NX
| _PAGE_RW
)) {
236 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap
);
240 efi_scratch
.use_pgd
= true;
243 * When making calls to the firmware everything needs to be 1:1
244 * mapped and addressable with 32-bit pointers. Map the kernel
245 * text and allocate a new stack because we can't rely on the
246 * stack pointer being < 4GB.
248 if (!IS_ENABLED(CONFIG_EFI_MIXED
))
252 * Map all of RAM so that we can access arguments in the 1:1
253 * mapping when making EFI runtime calls.
255 for_each_efi_memory_desc(md
) {
256 if (md
->type
!= EFI_CONVENTIONAL_MEMORY
&&
257 md
->type
!= EFI_LOADER_DATA
&&
258 md
->type
!= EFI_LOADER_CODE
)
261 pfn
= md
->phys_addr
>> PAGE_SHIFT
;
262 npages
= md
->num_pages
;
264 if (kernel_map_pages_in_pgd(pgd
, pfn
, md
->phys_addr
, npages
, _PAGE_RW
)) {
265 pr_err("Failed to map 1:1 memory\n");
270 page
= alloc_page(GFP_KERNEL
|__GFP_DMA32
);
272 panic("Unable to allocate EFI runtime stack < 4GB\n");
274 efi_scratch
.phys_stack
= virt_to_phys(page_address(page
));
275 efi_scratch
.phys_stack
+= PAGE_SIZE
; /* stack grows down */
277 npages
= (_etext
- _text
) >> PAGE_SHIFT
;
279 pfn
= text
>> PAGE_SHIFT
;
281 if (kernel_map_pages_in_pgd(pgd
, pfn
, text
, npages
, _PAGE_RW
)) {
282 pr_err("Failed to map kernel text 1:1\n");
289 static void __init
__map_region(efi_memory_desc_t
*md
, u64 va
)
291 unsigned long flags
= _PAGE_RW
;
293 pgd_t
*pgd
= efi_pgd
;
295 if (!(md
->attribute
& EFI_MEMORY_WB
))
298 pfn
= md
->phys_addr
>> PAGE_SHIFT
;
299 if (kernel_map_pages_in_pgd(pgd
, pfn
, va
, md
->num_pages
, flags
))
300 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
304 void __init
efi_map_region(efi_memory_desc_t
*md
)
306 unsigned long size
= md
->num_pages
<< PAGE_SHIFT
;
307 u64 pa
= md
->phys_addr
;
309 if (efi_enabled(EFI_OLD_MEMMAP
))
310 return old_map_region(md
);
313 * Make sure the 1:1 mappings are present as a catch-all for b0rked
314 * firmware which doesn't update all internal pointers after switching
315 * to virtual mode and would otherwise crap on us.
317 __map_region(md
, md
->phys_addr
);
320 * Enforce the 1:1 mapping as the default virtual address when
321 * booting in EFI mixed mode, because even though we may be
322 * running a 64-bit kernel, the firmware may only be 32-bit.
324 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED
)) {
325 md
->virt_addr
= md
->phys_addr
;
331 /* Is PA 2M-aligned? */
332 if (!(pa
& (PMD_SIZE
- 1))) {
335 u64 pa_offset
= pa
& (PMD_SIZE
- 1);
336 u64 prev_va
= efi_va
;
338 /* get us the same offset within this 2M page */
339 efi_va
= (efi_va
& PMD_MASK
) + pa_offset
;
341 if (efi_va
> prev_va
)
345 if (efi_va
< EFI_VA_END
) {
346 pr_warn(FW_WARN
"VA address range overflow!\n");
351 __map_region(md
, efi_va
);
352 md
->virt_addr
= efi_va
;
356 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
357 * md->virt_addr is the original virtual address which had been mapped in kexec
360 void __init
efi_map_region_fixed(efi_memory_desc_t
*md
)
362 __map_region(md
, md
->virt_addr
);
365 void __iomem
*__init
efi_ioremap(unsigned long phys_addr
, unsigned long size
,
366 u32 type
, u64 attribute
)
368 unsigned long last_map_pfn
;
370 if (type
== EFI_MEMORY_MAPPED_IO
)
371 return ioremap(phys_addr
, size
);
373 last_map_pfn
= init_memory_mapping(phys_addr
, phys_addr
+ size
);
374 if ((last_map_pfn
<< PAGE_SHIFT
) < phys_addr
+ size
) {
375 unsigned long top
= last_map_pfn
<< PAGE_SHIFT
;
376 efi_ioremap(top
, size
- (top
- phys_addr
), type
, attribute
);
379 if (!(attribute
& EFI_MEMORY_WB
))
380 efi_memory_uc((u64
)(unsigned long)__va(phys_addr
), size
);
382 return (void __iomem
*)__va(phys_addr
);
385 void __init
parse_efi_setup(u64 phys_addr
, u32 data_len
)
387 efi_setup
= phys_addr
+ sizeof(struct setup_data
);
390 void __init
efi_runtime_update_mappings(void)
393 pgd_t
*pgd
= efi_pgd
;
394 efi_memory_desc_t
*md
;
396 if (efi_enabled(EFI_OLD_MEMMAP
)) {
397 if (__supported_pte_mask
& _PAGE_NX
)
398 runtime_code_page_mkexec();
402 if (!efi_enabled(EFI_NX_PE_DATA
))
405 for_each_efi_memory_desc(md
) {
406 unsigned long pf
= 0;
408 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
411 if (!(md
->attribute
& EFI_MEMORY_WB
))
414 if ((md
->attribute
& EFI_MEMORY_XP
) ||
415 (md
->type
== EFI_RUNTIME_SERVICES_DATA
))
418 if (!(md
->attribute
& EFI_MEMORY_RO
) &&
419 (md
->type
!= EFI_RUNTIME_SERVICES_CODE
))
422 /* Update the 1:1 mapping */
423 pfn
= md
->phys_addr
>> PAGE_SHIFT
;
424 if (kernel_map_pages_in_pgd(pgd
, pfn
, md
->phys_addr
, md
->num_pages
, pf
))
425 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
426 md
->phys_addr
, md
->virt_addr
);
428 if (kernel_map_pages_in_pgd(pgd
, pfn
, md
->virt_addr
, md
->num_pages
, pf
))
429 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
430 md
->phys_addr
, md
->virt_addr
);
434 void __init
efi_dump_pagetable(void)
436 #ifdef CONFIG_EFI_PGT_DUMP
437 ptdump_walk_pgd_level(NULL
, efi_pgd
);
441 #ifdef CONFIG_EFI_MIXED
442 extern efi_status_t
efi64_thunk(u32
, ...);
444 #define runtime_service32(func) \
446 u32 table = (u32)(unsigned long)efi.systab; \
449 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
450 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
455 * Switch to the EFI page tables early so that we can access the 1:1
456 * runtime services mappings which are not mapped in any other page
457 * tables. This function must be called before runtime_service32().
459 * Also, disable interrupts because the IDT points to 64-bit handlers,
460 * which aren't going to function correctly when we switch to 32-bit.
462 #define efi_thunk(f, ...) \
465 unsigned long __flags; \
468 local_irq_save(__flags); \
469 arch_efi_call_virt_setup(); \
471 __func = runtime_service32(f); \
472 __s = efi64_thunk(__func, __VA_ARGS__); \
474 arch_efi_call_virt_teardown(); \
475 local_irq_restore(__flags); \
480 efi_status_t
efi_thunk_set_virtual_address_map(
481 void *phys_set_virtual_address_map
,
482 unsigned long memory_map_size
,
483 unsigned long descriptor_size
,
484 u32 descriptor_version
,
485 efi_memory_desc_t
*virtual_map
)
491 efi_sync_low_kernel_mappings();
492 local_irq_save(flags
);
494 efi_scratch
.prev_cr3
= read_cr3();
495 write_cr3((unsigned long)efi_scratch
.efi_pgt
);
498 func
= (u32
)(unsigned long)phys_set_virtual_address_map
;
499 status
= efi64_thunk(func
, memory_map_size
, descriptor_size
,
500 descriptor_version
, virtual_map
);
502 write_cr3(efi_scratch
.prev_cr3
);
504 local_irq_restore(flags
);
509 static efi_status_t
efi_thunk_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
512 u32 phys_tm
, phys_tc
;
514 spin_lock(&rtc_lock
);
516 phys_tm
= virt_to_phys(tm
);
517 phys_tc
= virt_to_phys(tc
);
519 status
= efi_thunk(get_time
, phys_tm
, phys_tc
);
521 spin_unlock(&rtc_lock
);
526 static efi_status_t
efi_thunk_set_time(efi_time_t
*tm
)
531 spin_lock(&rtc_lock
);
533 phys_tm
= virt_to_phys(tm
);
535 status
= efi_thunk(set_time
, phys_tm
);
537 spin_unlock(&rtc_lock
);
543 efi_thunk_get_wakeup_time(efi_bool_t
*enabled
, efi_bool_t
*pending
,
547 u32 phys_enabled
, phys_pending
, phys_tm
;
549 spin_lock(&rtc_lock
);
551 phys_enabled
= virt_to_phys(enabled
);
552 phys_pending
= virt_to_phys(pending
);
553 phys_tm
= virt_to_phys(tm
);
555 status
= efi_thunk(get_wakeup_time
, phys_enabled
,
556 phys_pending
, phys_tm
);
558 spin_unlock(&rtc_lock
);
564 efi_thunk_set_wakeup_time(efi_bool_t enabled
, efi_time_t
*tm
)
569 spin_lock(&rtc_lock
);
571 phys_tm
= virt_to_phys(tm
);
573 status
= efi_thunk(set_wakeup_time
, enabled
, phys_tm
);
575 spin_unlock(&rtc_lock
);
582 efi_thunk_get_variable(efi_char16_t
*name
, efi_guid_t
*vendor
,
583 u32
*attr
, unsigned long *data_size
, void *data
)
586 u32 phys_name
, phys_vendor
, phys_attr
;
587 u32 phys_data_size
, phys_data
;
589 phys_data_size
= virt_to_phys(data_size
);
590 phys_vendor
= virt_to_phys(vendor
);
591 phys_name
= virt_to_phys(name
);
592 phys_attr
= virt_to_phys(attr
);
593 phys_data
= virt_to_phys(data
);
595 status
= efi_thunk(get_variable
, phys_name
, phys_vendor
,
596 phys_attr
, phys_data_size
, phys_data
);
602 efi_thunk_set_variable(efi_char16_t
*name
, efi_guid_t
*vendor
,
603 u32 attr
, unsigned long data_size
, void *data
)
605 u32 phys_name
, phys_vendor
, phys_data
;
608 phys_name
= virt_to_phys(name
);
609 phys_vendor
= virt_to_phys(vendor
);
610 phys_data
= virt_to_phys(data
);
612 /* If data_size is > sizeof(u32) we've got problems */
613 status
= efi_thunk(set_variable
, phys_name
, phys_vendor
,
614 attr
, data_size
, phys_data
);
620 efi_thunk_get_next_variable(unsigned long *name_size
,
625 u32 phys_name_size
, phys_name
, phys_vendor
;
627 phys_name_size
= virt_to_phys(name_size
);
628 phys_vendor
= virt_to_phys(vendor
);
629 phys_name
= virt_to_phys(name
);
631 status
= efi_thunk(get_next_variable
, phys_name_size
,
632 phys_name
, phys_vendor
);
638 efi_thunk_get_next_high_mono_count(u32
*count
)
643 phys_count
= virt_to_phys(count
);
644 status
= efi_thunk(get_next_high_mono_count
, phys_count
);
650 efi_thunk_reset_system(int reset_type
, efi_status_t status
,
651 unsigned long data_size
, efi_char16_t
*data
)
655 phys_data
= virt_to_phys(data
);
657 efi_thunk(reset_system
, reset_type
, status
, data_size
, phys_data
);
661 efi_thunk_update_capsule(efi_capsule_header_t
**capsules
,
662 unsigned long count
, unsigned long sg_list
)
665 * To properly support this function we would need to repackage
666 * 'capsules' because the firmware doesn't understand 64-bit
669 return EFI_UNSUPPORTED
;
673 efi_thunk_query_variable_info(u32 attr
, u64
*storage_space
,
674 u64
*remaining_space
,
675 u64
*max_variable_size
)
678 u32 phys_storage
, phys_remaining
, phys_max
;
680 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
681 return EFI_UNSUPPORTED
;
683 phys_storage
= virt_to_phys(storage_space
);
684 phys_remaining
= virt_to_phys(remaining_space
);
685 phys_max
= virt_to_phys(max_variable_size
);
687 status
= efi_thunk(query_variable_info
, attr
, phys_storage
,
688 phys_remaining
, phys_max
);
694 efi_thunk_query_capsule_caps(efi_capsule_header_t
**capsules
,
695 unsigned long count
, u64
*max_size
,
699 * To properly support this function we would need to repackage
700 * 'capsules' because the firmware doesn't understand 64-bit
703 return EFI_UNSUPPORTED
;
706 void efi_thunk_runtime_setup(void)
708 efi
.get_time
= efi_thunk_get_time
;
709 efi
.set_time
= efi_thunk_set_time
;
710 efi
.get_wakeup_time
= efi_thunk_get_wakeup_time
;
711 efi
.set_wakeup_time
= efi_thunk_set_wakeup_time
;
712 efi
.get_variable
= efi_thunk_get_variable
;
713 efi
.get_next_variable
= efi_thunk_get_next_variable
;
714 efi
.set_variable
= efi_thunk_set_variable
;
715 efi
.get_next_high_mono_count
= efi_thunk_get_next_high_mono_count
;
716 efi
.reset_system
= efi_thunk_reset_system
;
717 efi
.query_variable_info
= efi_thunk_query_variable_info
;
718 efi
.update_capsule
= efi_thunk_update_capsule
;
719 efi
.query_capsule_caps
= efi_thunk_query_capsule_caps
;
721 #endif /* CONFIG_EFI_MIXED */