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 #include <linux/kernel.h>
19 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/spinlock.h>
23 #include <linux/bootmem.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/efi.h>
27 #include <linux/uaccess.h>
29 #include <linux/reboot.h>
30 #include <linux/slab.h>
32 #include <asm/setup.h>
35 #include <asm/pgtable.h>
36 #include <asm/tlbflush.h>
37 #include <asm/proto.h>
39 #include <asm/cacheflush.h>
40 #include <asm/fixmap.h>
41 #include <asm/realmode.h>
45 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
46 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
48 static u64 efi_va
= EFI_VA_START
;
51 * Scratch space used for switching the pagetable in the EFI stub
61 static void __init
early_code_mapping_set_exec(int executable
)
63 efi_memory_desc_t
*md
;
66 if (!(__supported_pte_mask
& _PAGE_NX
))
69 /* Make EFI service code area executable */
70 for (p
= memmap
.map
; p
< memmap
.map_end
; p
+= memmap
.desc_size
) {
72 if (md
->type
== EFI_RUNTIME_SERVICES_CODE
||
73 md
->type
== EFI_BOOT_SERVICES_CODE
)
74 efi_set_executable(md
, executable
);
78 pgd_t
* __init
efi_call_phys_prolog(void)
80 unsigned long vaddress
;
86 if (!efi_enabled(EFI_OLD_MEMMAP
))
89 early_code_mapping_set_exec(1);
91 n_pgds
= DIV_ROUND_UP((max_pfn
<< PAGE_SHIFT
), PGDIR_SIZE
);
92 save_pgd
= kmalloc(n_pgds
* sizeof(pgd_t
), GFP_KERNEL
);
94 for (pgd
= 0; pgd
< n_pgds
; pgd
++) {
95 save_pgd
[pgd
] = *pgd_offset_k(pgd
* PGDIR_SIZE
);
96 vaddress
= (unsigned long)__va(pgd
* PGDIR_SIZE
);
97 set_pgd(pgd_offset_k(pgd
* PGDIR_SIZE
), *pgd_offset_k(vaddress
));
104 void __init
efi_call_phys_epilog(pgd_t
*save_pgd
)
107 * After the lock is released, the original page table is restored.
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);
127 * Add low kernel mappings for passing arguments to EFI functions.
129 void efi_sync_low_kernel_mappings(void)
132 pgd_t
*pgd
= (pgd_t
*)__va(real_mode_header
->trampoline_pgd
);
134 if (efi_enabled(EFI_OLD_MEMMAP
))
137 num_pgds
= pgd_index(MODULES_END
- 1) - pgd_index(PAGE_OFFSET
);
139 memcpy(pgd
+ pgd_index(PAGE_OFFSET
),
140 init_mm
.pgd
+ pgd_index(PAGE_OFFSET
),
141 sizeof(pgd_t
) * num_pgds
);
144 int __init
efi_setup_page_tables(unsigned long pa_memmap
, unsigned num_pages
)
151 if (efi_enabled(EFI_OLD_MEMMAP
))
154 efi_scratch
.efi_pgt
= (pgd_t
*)(unsigned long)real_mode_header
->trampoline_pgd
;
155 pgd
= __va(efi_scratch
.efi_pgt
);
158 * It can happen that the physical address of new_memmap lands in memory
159 * which is not mapped in the EFI page table. Therefore we need to go
160 * and ident-map those pages containing the map before calling
161 * phys_efi_set_virtual_address_map().
163 if (kernel_map_pages_in_pgd(pgd
, pa_memmap
, pa_memmap
, num_pages
, _PAGE_NX
)) {
164 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap
);
168 efi_scratch
.use_pgd
= true;
171 * When making calls to the firmware everything needs to be 1:1
172 * mapped and addressable with 32-bit pointers. Map the kernel
173 * text and allocate a new stack because we can't rely on the
174 * stack pointer being < 4GB.
176 if (!IS_ENABLED(CONFIG_EFI_MIXED
))
179 page
= alloc_page(GFP_KERNEL
|__GFP_DMA32
);
181 panic("Unable to allocate EFI runtime stack < 4GB\n");
183 efi_scratch
.phys_stack
= virt_to_phys(page_address(page
));
184 efi_scratch
.phys_stack
+= PAGE_SIZE
; /* stack grows down */
186 npages
= (_end
- _text
) >> PAGE_SHIFT
;
189 if (kernel_map_pages_in_pgd(pgd
, text
>> PAGE_SHIFT
, text
, npages
, 0)) {
190 pr_err("Failed to map kernel text 1:1\n");
197 void __init
efi_cleanup_page_tables(unsigned long pa_memmap
, unsigned num_pages
)
199 pgd_t
*pgd
= (pgd_t
*)__va(real_mode_header
->trampoline_pgd
);
201 kernel_unmap_pages_in_pgd(pgd
, pa_memmap
, num_pages
);
204 static void __init
__map_region(efi_memory_desc_t
*md
, u64 va
)
206 pgd_t
*pgd
= (pgd_t
*)__va(real_mode_header
->trampoline_pgd
);
207 unsigned long pf
= 0;
209 if (!(md
->attribute
& EFI_MEMORY_WB
))
212 if (kernel_map_pages_in_pgd(pgd
, md
->phys_addr
, va
, md
->num_pages
, pf
))
213 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
217 void __init
efi_map_region(efi_memory_desc_t
*md
)
219 unsigned long size
= md
->num_pages
<< PAGE_SHIFT
;
220 u64 pa
= md
->phys_addr
;
222 if (efi_enabled(EFI_OLD_MEMMAP
))
223 return old_map_region(md
);
226 * Make sure the 1:1 mappings are present as a catch-all for b0rked
227 * firmware which doesn't update all internal pointers after switching
228 * to virtual mode and would otherwise crap on us.
230 __map_region(md
, md
->phys_addr
);
233 * Enforce the 1:1 mapping as the default virtual address when
234 * booting in EFI mixed mode, because even though we may be
235 * running a 64-bit kernel, the firmware may only be 32-bit.
237 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED
)) {
238 md
->virt_addr
= md
->phys_addr
;
244 /* Is PA 2M-aligned? */
245 if (!(pa
& (PMD_SIZE
- 1))) {
248 u64 pa_offset
= pa
& (PMD_SIZE
- 1);
249 u64 prev_va
= efi_va
;
251 /* get us the same offset within this 2M page */
252 efi_va
= (efi_va
& PMD_MASK
) + pa_offset
;
254 if (efi_va
> prev_va
)
258 if (efi_va
< EFI_VA_END
) {
259 pr_warn(FW_WARN
"VA address range overflow!\n");
264 __map_region(md
, efi_va
);
265 md
->virt_addr
= efi_va
;
269 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
270 * md->virt_addr is the original virtual address which had been mapped in kexec
273 void __init
efi_map_region_fixed(efi_memory_desc_t
*md
)
275 __map_region(md
, md
->virt_addr
);
278 void __iomem
*__init
efi_ioremap(unsigned long phys_addr
, unsigned long size
,
279 u32 type
, u64 attribute
)
281 unsigned long last_map_pfn
;
283 if (type
== EFI_MEMORY_MAPPED_IO
)
284 return ioremap(phys_addr
, size
);
286 last_map_pfn
= init_memory_mapping(phys_addr
, phys_addr
+ size
);
287 if ((last_map_pfn
<< PAGE_SHIFT
) < phys_addr
+ size
) {
288 unsigned long top
= last_map_pfn
<< PAGE_SHIFT
;
289 efi_ioremap(top
, size
- (top
- phys_addr
), type
, attribute
);
292 if (!(attribute
& EFI_MEMORY_WB
))
293 efi_memory_uc((u64
)(unsigned long)__va(phys_addr
), size
);
295 return (void __iomem
*)__va(phys_addr
);
298 void __init
parse_efi_setup(u64 phys_addr
, u32 data_len
)
300 efi_setup
= phys_addr
+ sizeof(struct setup_data
);
303 void __init
efi_runtime_mkexec(void)
305 if (!efi_enabled(EFI_OLD_MEMMAP
))
308 if (__supported_pte_mask
& _PAGE_NX
)
309 runtime_code_page_mkexec();
312 void __init
efi_dump_pagetable(void)
314 #ifdef CONFIG_EFI_PGT_DUMP
315 pgd_t
*pgd
= (pgd_t
*)__va(real_mode_header
->trampoline_pgd
);
317 ptdump_walk_pgd_level(NULL
, pgd
);
321 #ifdef CONFIG_EFI_MIXED
322 extern efi_status_t
efi64_thunk(u32
, ...);
324 #define runtime_service32(func) \
326 u32 table = (u32)(unsigned long)efi.systab; \
329 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
330 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
335 * Switch to the EFI page tables early so that we can access the 1:1
336 * runtime services mappings which are not mapped in any other page
337 * tables. This function must be called before runtime_service32().
339 * Also, disable interrupts because the IDT points to 64-bit handlers,
340 * which aren't going to function correctly when we switch to 32-bit.
342 #define efi_thunk(f, ...) \
345 unsigned long flags; \
348 efi_sync_low_kernel_mappings(); \
349 local_irq_save(flags); \
351 efi_scratch.prev_cr3 = read_cr3(); \
352 write_cr3((unsigned long)efi_scratch.efi_pgt); \
355 func = runtime_service32(f); \
356 __s = efi64_thunk(func, __VA_ARGS__); \
358 write_cr3(efi_scratch.prev_cr3); \
360 local_irq_restore(flags); \
365 efi_status_t
efi_thunk_set_virtual_address_map(
366 void *phys_set_virtual_address_map
,
367 unsigned long memory_map_size
,
368 unsigned long descriptor_size
,
369 u32 descriptor_version
,
370 efi_memory_desc_t
*virtual_map
)
376 efi_sync_low_kernel_mappings();
377 local_irq_save(flags
);
379 efi_scratch
.prev_cr3
= read_cr3();
380 write_cr3((unsigned long)efi_scratch
.efi_pgt
);
383 func
= (u32
)(unsigned long)phys_set_virtual_address_map
;
384 status
= efi64_thunk(func
, memory_map_size
, descriptor_size
,
385 descriptor_version
, virtual_map
);
387 write_cr3(efi_scratch
.prev_cr3
);
389 local_irq_restore(flags
);
394 static efi_status_t
efi_thunk_get_time(efi_time_t
*tm
, efi_time_cap_t
*tc
)
397 u32 phys_tm
, phys_tc
;
399 spin_lock(&rtc_lock
);
401 phys_tm
= virt_to_phys(tm
);
402 phys_tc
= virt_to_phys(tc
);
404 status
= efi_thunk(get_time
, phys_tm
, phys_tc
);
406 spin_unlock(&rtc_lock
);
411 static efi_status_t
efi_thunk_set_time(efi_time_t
*tm
)
416 spin_lock(&rtc_lock
);
418 phys_tm
= virt_to_phys(tm
);
420 status
= efi_thunk(set_time
, phys_tm
);
422 spin_unlock(&rtc_lock
);
428 efi_thunk_get_wakeup_time(efi_bool_t
*enabled
, efi_bool_t
*pending
,
432 u32 phys_enabled
, phys_pending
, phys_tm
;
434 spin_lock(&rtc_lock
);
436 phys_enabled
= virt_to_phys(enabled
);
437 phys_pending
= virt_to_phys(pending
);
438 phys_tm
= virt_to_phys(tm
);
440 status
= efi_thunk(get_wakeup_time
, phys_enabled
,
441 phys_pending
, phys_tm
);
443 spin_unlock(&rtc_lock
);
449 efi_thunk_set_wakeup_time(efi_bool_t enabled
, efi_time_t
*tm
)
454 spin_lock(&rtc_lock
);
456 phys_tm
= virt_to_phys(tm
);
458 status
= efi_thunk(set_wakeup_time
, enabled
, phys_tm
);
460 spin_unlock(&rtc_lock
);
467 efi_thunk_get_variable(efi_char16_t
*name
, efi_guid_t
*vendor
,
468 u32
*attr
, unsigned long *data_size
, void *data
)
471 u32 phys_name
, phys_vendor
, phys_attr
;
472 u32 phys_data_size
, phys_data
;
474 phys_data_size
= virt_to_phys(data_size
);
475 phys_vendor
= virt_to_phys(vendor
);
476 phys_name
= virt_to_phys(name
);
477 phys_attr
= virt_to_phys(attr
);
478 phys_data
= virt_to_phys(data
);
480 status
= efi_thunk(get_variable
, phys_name
, phys_vendor
,
481 phys_attr
, phys_data_size
, phys_data
);
487 efi_thunk_set_variable(efi_char16_t
*name
, efi_guid_t
*vendor
,
488 u32 attr
, unsigned long data_size
, void *data
)
490 u32 phys_name
, phys_vendor
, phys_data
;
493 phys_name
= virt_to_phys(name
);
494 phys_vendor
= virt_to_phys(vendor
);
495 phys_data
= virt_to_phys(data
);
497 /* If data_size is > sizeof(u32) we've got problems */
498 status
= efi_thunk(set_variable
, phys_name
, phys_vendor
,
499 attr
, data_size
, phys_data
);
505 efi_thunk_get_next_variable(unsigned long *name_size
,
510 u32 phys_name_size
, phys_name
, phys_vendor
;
512 phys_name_size
= virt_to_phys(name_size
);
513 phys_vendor
= virt_to_phys(vendor
);
514 phys_name
= virt_to_phys(name
);
516 status
= efi_thunk(get_next_variable
, phys_name_size
,
517 phys_name
, phys_vendor
);
523 efi_thunk_get_next_high_mono_count(u32
*count
)
528 phys_count
= virt_to_phys(count
);
529 status
= efi_thunk(get_next_high_mono_count
, phys_count
);
535 efi_thunk_reset_system(int reset_type
, efi_status_t status
,
536 unsigned long data_size
, efi_char16_t
*data
)
540 phys_data
= virt_to_phys(data
);
542 efi_thunk(reset_system
, reset_type
, status
, data_size
, phys_data
);
546 efi_thunk_update_capsule(efi_capsule_header_t
**capsules
,
547 unsigned long count
, unsigned long sg_list
)
550 * To properly support this function we would need to repackage
551 * 'capsules' because the firmware doesn't understand 64-bit
554 return EFI_UNSUPPORTED
;
558 efi_thunk_query_variable_info(u32 attr
, u64
*storage_space
,
559 u64
*remaining_space
,
560 u64
*max_variable_size
)
563 u32 phys_storage
, phys_remaining
, phys_max
;
565 if (efi
.runtime_version
< EFI_2_00_SYSTEM_TABLE_REVISION
)
566 return EFI_UNSUPPORTED
;
568 phys_storage
= virt_to_phys(storage_space
);
569 phys_remaining
= virt_to_phys(remaining_space
);
570 phys_max
= virt_to_phys(max_variable_size
);
572 status
= efi_thunk(query_variable_info
, attr
, phys_storage
,
573 phys_remaining
, phys_max
);
579 efi_thunk_query_capsule_caps(efi_capsule_header_t
**capsules
,
580 unsigned long count
, u64
*max_size
,
584 * To properly support this function we would need to repackage
585 * 'capsules' because the firmware doesn't understand 64-bit
588 return EFI_UNSUPPORTED
;
591 void efi_thunk_runtime_setup(void)
593 efi
.get_time
= efi_thunk_get_time
;
594 efi
.set_time
= efi_thunk_set_time
;
595 efi
.get_wakeup_time
= efi_thunk_get_wakeup_time
;
596 efi
.set_wakeup_time
= efi_thunk_set_wakeup_time
;
597 efi
.get_variable
= efi_thunk_get_variable
;
598 efi
.get_next_variable
= efi_thunk_get_next_variable
;
599 efi
.set_variable
= efi_thunk_set_variable
;
600 efi
.get_next_high_mono_count
= efi_thunk_get_next_high_mono_count
;
601 efi
.reset_system
= efi_thunk_reset_system
;
602 efi
.query_variable_info
= efi_thunk_query_variable_info
;
603 efi
.update_capsule
= efi_thunk_update_capsule
;
604 efi
.query_capsule_caps
= efi_thunk_query_capsule_caps
;
606 #endif /* CONFIG_EFI_MIXED */