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>
43 #include <asm/nospec-branch.h>
46 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
47 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
49 static u64 efi_va = EFI_VA_START;
52 * Scratch space used for switching the pagetable in the EFI stub
62 static void __init early_code_mapping_set_exec(int executable)
64 efi_memory_desc_t *md;
67 if (!(__supported_pte_mask & _PAGE_NX))
70 /* Make EFI service code area executable */
71 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
73 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
74 md->type == EFI_BOOT_SERVICES_CODE)
75 efi_set_executable(md, executable);
79 pgd_t * __init efi_call_phys_prolog(void)
81 unsigned long vaddress;
87 if (!efi_enabled(EFI_OLD_MEMMAP))
90 early_code_mapping_set_exec(1);
92 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
93 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
95 for (pgd = 0; pgd < n_pgds; pgd++) {
96 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
97 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
98 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
105 void __init efi_call_phys_epilog(pgd_t *save_pgd)
108 * After the lock is released, the original page table is restored.
116 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
118 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++)
119 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
124 early_code_mapping_set_exec(0);
128 * Add low kernel mappings for passing arguments to EFI functions.
130 void efi_sync_low_kernel_mappings(void)
133 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
135 if (efi_enabled(EFI_OLD_MEMMAP))
138 num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET);
140 memcpy(pgd + pgd_index(PAGE_OFFSET),
141 init_mm.pgd + pgd_index(PAGE_OFFSET),
142 sizeof(pgd_t) * num_pgds);
145 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
152 if (efi_enabled(EFI_OLD_MEMMAP))
155 efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
156 pgd = __va(efi_scratch.efi_pgt);
159 * It can happen that the physical address of new_memmap lands in memory
160 * which is not mapped in the EFI page table. Therefore we need to go
161 * and ident-map those pages containing the map before calling
162 * phys_efi_set_virtual_address_map().
164 if (kernel_map_pages_in_pgd(pgd, pa_memmap, pa_memmap, num_pages, _PAGE_NX)) {
165 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
169 efi_scratch.use_pgd = true;
172 * When making calls to the firmware everything needs to be 1:1
173 * mapped and addressable with 32-bit pointers. Map the kernel
174 * text and allocate a new stack because we can't rely on the
175 * stack pointer being < 4GB.
177 if (!IS_ENABLED(CONFIG_EFI_MIXED))
180 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
182 panic("Unable to allocate EFI runtime stack < 4GB\n");
184 efi_scratch.phys_stack = virt_to_phys(page_address(page));
185 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
187 npages = (_end - _text) >> PAGE_SHIFT;
190 if (kernel_map_pages_in_pgd(pgd, text >> PAGE_SHIFT, text, npages, 0)) {
191 pr_err("Failed to map kernel text 1:1\n");
198 void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
200 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
202 kernel_unmap_pages_in_pgd(pgd, pa_memmap, num_pages);
205 static void __init __map_region(efi_memory_desc_t *md, u64 va)
207 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
208 unsigned long pf = 0;
210 if (!(md->attribute & EFI_MEMORY_WB))
213 if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
214 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
218 void __init efi_map_region(efi_memory_desc_t *md)
220 unsigned long size = md->num_pages << PAGE_SHIFT;
221 u64 pa = md->phys_addr;
223 if (efi_enabled(EFI_OLD_MEMMAP))
224 return old_map_region(md);
227 * Make sure the 1:1 mappings are present as a catch-all for b0rked
228 * firmware which doesn't update all internal pointers after switching
229 * to virtual mode and would otherwise crap on us.
231 __map_region(md, md->phys_addr);
234 * Enforce the 1:1 mapping as the default virtual address when
235 * booting in EFI mixed mode, because even though we may be
236 * running a 64-bit kernel, the firmware may only be 32-bit.
238 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
239 md->virt_addr = md->phys_addr;
245 /* Is PA 2M-aligned? */
246 if (!(pa & (PMD_SIZE - 1))) {
249 u64 pa_offset = pa & (PMD_SIZE - 1);
250 u64 prev_va = efi_va;
252 /* get us the same offset within this 2M page */
253 efi_va = (efi_va & PMD_MASK) + pa_offset;
255 if (efi_va > prev_va)
259 if (efi_va < EFI_VA_END) {
260 pr_warn(FW_WARN "VA address range overflow!\n");
265 __map_region(md, efi_va);
266 md->virt_addr = efi_va;
270 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
271 * md->virt_addr is the original virtual address which had been mapped in kexec
274 void __init efi_map_region_fixed(efi_memory_desc_t *md)
276 __map_region(md, md->virt_addr);
279 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
280 u32 type, u64 attribute)
282 unsigned long last_map_pfn;
284 if (type == EFI_MEMORY_MAPPED_IO)
285 return ioremap(phys_addr, size);
287 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
288 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
289 unsigned long top = last_map_pfn << PAGE_SHIFT;
290 efi_ioremap(top, size - (top - phys_addr), type, attribute);
293 if (!(attribute & EFI_MEMORY_WB))
294 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
296 return (void __iomem *)__va(phys_addr);
299 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
301 efi_setup = phys_addr + sizeof(struct setup_data);
304 void __init efi_runtime_mkexec(void)
306 if (!efi_enabled(EFI_OLD_MEMMAP))
309 if (__supported_pte_mask & _PAGE_NX)
310 runtime_code_page_mkexec();
313 void __init efi_dump_pagetable(void)
315 #ifdef CONFIG_EFI_PGT_DUMP
316 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
318 ptdump_walk_pgd_level(NULL, pgd);
322 #ifdef CONFIG_EFI_MIXED
323 extern efi_status_t efi64_thunk(u32, ...);
325 #define runtime_service32(func) \
327 u32 table = (u32)(unsigned long)efi.systab; \
330 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
331 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
336 * Switch to the EFI page tables early so that we can access the 1:1
337 * runtime services mappings which are not mapped in any other page
338 * tables. This function must be called before runtime_service32().
340 * Also, disable interrupts because the IDT points to 64-bit handlers,
341 * which aren't going to function correctly when we switch to 32-bit.
343 #define efi_thunk(f, ...) \
346 unsigned long flags; \
349 efi_sync_low_kernel_mappings(); \
350 local_irq_save(flags); \
351 firmware_restrict_branch_speculation_start(); \
353 efi_scratch.prev_cr3 = read_cr3(); \
354 write_cr3((unsigned long)efi_scratch.efi_pgt); \
357 func = runtime_service32(f); \
358 __s = efi64_thunk(func, __VA_ARGS__); \
360 write_cr3(efi_scratch.prev_cr3); \
362 firmware_restrict_branch_speculation_end(); \
363 local_irq_restore(flags); \
368 efi_status_t efi_thunk_set_virtual_address_map(
369 void *phys_set_virtual_address_map,
370 unsigned long memory_map_size,
371 unsigned long descriptor_size,
372 u32 descriptor_version,
373 efi_memory_desc_t *virtual_map)
379 efi_sync_low_kernel_mappings();
380 local_irq_save(flags);
382 efi_scratch.prev_cr3 = read_cr3();
383 write_cr3((unsigned long)efi_scratch.efi_pgt);
386 func = (u32)(unsigned long)phys_set_virtual_address_map;
387 status = efi64_thunk(func, memory_map_size, descriptor_size,
388 descriptor_version, virtual_map);
390 write_cr3(efi_scratch.prev_cr3);
392 local_irq_restore(flags);
397 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
400 u32 phys_tm, phys_tc;
402 spin_lock(&rtc_lock);
404 phys_tm = virt_to_phys(tm);
405 phys_tc = virt_to_phys(tc);
407 status = efi_thunk(get_time, phys_tm, phys_tc);
409 spin_unlock(&rtc_lock);
414 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
419 spin_lock(&rtc_lock);
421 phys_tm = virt_to_phys(tm);
423 status = efi_thunk(set_time, phys_tm);
425 spin_unlock(&rtc_lock);
431 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
435 u32 phys_enabled, phys_pending, phys_tm;
437 spin_lock(&rtc_lock);
439 phys_enabled = virt_to_phys(enabled);
440 phys_pending = virt_to_phys(pending);
441 phys_tm = virt_to_phys(tm);
443 status = efi_thunk(get_wakeup_time, phys_enabled,
444 phys_pending, phys_tm);
446 spin_unlock(&rtc_lock);
452 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
457 spin_lock(&rtc_lock);
459 phys_tm = virt_to_phys(tm);
461 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
463 spin_unlock(&rtc_lock);
470 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
471 u32 *attr, unsigned long *data_size, void *data)
474 u32 phys_name, phys_vendor, phys_attr;
475 u32 phys_data_size, phys_data;
477 phys_data_size = virt_to_phys(data_size);
478 phys_vendor = virt_to_phys(vendor);
479 phys_name = virt_to_phys(name);
480 phys_attr = virt_to_phys(attr);
481 phys_data = virt_to_phys(data);
483 status = efi_thunk(get_variable, phys_name, phys_vendor,
484 phys_attr, phys_data_size, phys_data);
490 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
491 u32 attr, unsigned long data_size, void *data)
493 u32 phys_name, phys_vendor, phys_data;
496 phys_name = virt_to_phys(name);
497 phys_vendor = virt_to_phys(vendor);
498 phys_data = virt_to_phys(data);
500 /* If data_size is > sizeof(u32) we've got problems */
501 status = efi_thunk(set_variable, phys_name, phys_vendor,
502 attr, data_size, phys_data);
508 efi_thunk_get_next_variable(unsigned long *name_size,
513 u32 phys_name_size, phys_name, phys_vendor;
515 phys_name_size = virt_to_phys(name_size);
516 phys_vendor = virt_to_phys(vendor);
517 phys_name = virt_to_phys(name);
519 status = efi_thunk(get_next_variable, phys_name_size,
520 phys_name, phys_vendor);
526 efi_thunk_get_next_high_mono_count(u32 *count)
531 phys_count = virt_to_phys(count);
532 status = efi_thunk(get_next_high_mono_count, phys_count);
538 efi_thunk_reset_system(int reset_type, efi_status_t status,
539 unsigned long data_size, efi_char16_t *data)
543 phys_data = virt_to_phys(data);
545 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
549 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
550 unsigned long count, unsigned long sg_list)
553 * To properly support this function we would need to repackage
554 * 'capsules' because the firmware doesn't understand 64-bit
557 return EFI_UNSUPPORTED;
561 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
562 u64 *remaining_space,
563 u64 *max_variable_size)
566 u32 phys_storage, phys_remaining, phys_max;
568 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
569 return EFI_UNSUPPORTED;
571 phys_storage = virt_to_phys(storage_space);
572 phys_remaining = virt_to_phys(remaining_space);
573 phys_max = virt_to_phys(max_variable_size);
575 status = efi_thunk(query_variable_info, attr, phys_storage,
576 phys_remaining, phys_max);
582 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
583 unsigned long count, u64 *max_size,
587 * To properly support this function we would need to repackage
588 * 'capsules' because the firmware doesn't understand 64-bit
591 return EFI_UNSUPPORTED;
594 void efi_thunk_runtime_setup(void)
596 efi.get_time = efi_thunk_get_time;
597 efi.set_time = efi_thunk_set_time;
598 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
599 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
600 efi.get_variable = efi_thunk_get_variable;
601 efi.get_next_variable = efi_thunk_get_next_variable;
602 efi.set_variable = efi_thunk_set_variable;
603 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
604 efi.reset_system = efi_thunk_reset_system;
605 efi.query_variable_info = efi_thunk_query_variable_info;
606 efi.update_capsule = efi_thunk_update_capsule;
607 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
609 #endif /* CONFIG_EFI_MIXED */