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/module.h>
28 #include <linux/efi.h>
29 #include <linux/uaccess.h>
31 #include <linux/reboot.h>
32 #include <linux/slab.h>
34 #include <asm/setup.h>
37 #include <asm/pgtable.h>
38 #include <asm/tlbflush.h>
39 #include <asm/proto.h>
41 #include <asm/cacheflush.h>
42 #include <asm/fixmap.h>
43 #include <asm/realmode.h>
45 #include <asm/pgalloc.h>
48 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
49 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
51 static u64 efi_va = EFI_VA_START;
53 struct efi_scratch efi_scratch;
55 static void __init early_code_mapping_set_exec(int executable)
57 efi_memory_desc_t *md;
59 if (!(__supported_pte_mask & _PAGE_NX))
62 /* Make EFI service code area executable */
63 for_each_efi_memory_desc(md) {
64 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
65 md->type == EFI_BOOT_SERVICES_CODE)
66 efi_set_executable(md, executable);
70 pgd_t * __init efi_call_phys_prolog(void)
72 unsigned long vaddress;
78 if (!efi_enabled(EFI_OLD_MEMMAP)) {
79 save_pgd = (pgd_t *)read_cr3();
80 write_cr3((unsigned long)efi_scratch.efi_pgt);
84 early_code_mapping_set_exec(1);
86 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
87 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
89 for (pgd = 0; pgd < n_pgds; pgd++) {
90 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
91 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
92 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
100 void __init efi_call_phys_epilog(pgd_t *save_pgd)
103 * After the lock is released, the original page table is restored.
108 if (!efi_enabled(EFI_OLD_MEMMAP)) {
109 write_cr3((unsigned long)save_pgd);
114 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
116 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++)
117 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
122 early_code_mapping_set_exec(0);
125 static pgd_t *efi_pgd;
128 * We need our own copy of the higher levels of the page tables
129 * because we want to avoid inserting EFI region mappings (EFI_VA_END
130 * to EFI_VA_START) into the standard kernel page tables. Everything
131 * else can be shared, see efi_sync_low_kernel_mappings().
133 int __init efi_alloc_page_tables(void)
139 if (efi_enabled(EFI_OLD_MEMMAP))
142 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
143 efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
147 pgd = efi_pgd + pgd_index(EFI_VA_END);
149 pud = pud_alloc_one(NULL, 0);
151 free_page((unsigned long)efi_pgd);
155 pgd_populate(NULL, pgd, pud);
161 * Add low kernel mappings for passing arguments to EFI functions.
163 void efi_sync_low_kernel_mappings(void)
165 unsigned num_entries;
166 pgd_t *pgd_k, *pgd_efi;
167 pud_t *pud_k, *pud_efi;
169 if (efi_enabled(EFI_OLD_MEMMAP))
173 * We can share all PGD entries apart from the one entry that
174 * covers the EFI runtime mapping space.
176 * Make sure the EFI runtime region mappings are guaranteed to
177 * only span a single PGD entry and that the entry also maps
178 * other important kernel regions.
180 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
181 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
182 (EFI_VA_END & PGDIR_MASK));
184 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
185 pgd_k = pgd_offset_k(PAGE_OFFSET);
187 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
188 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
191 * We share all the PUD entries apart from those that map the
192 * EFI regions. Copy around them.
194 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
195 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
197 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
198 pud_efi = pud_offset(pgd_efi, 0);
200 pgd_k = pgd_offset_k(EFI_VA_END);
201 pud_k = pud_offset(pgd_k, 0);
203 num_entries = pud_index(EFI_VA_END);
204 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
206 pud_efi = pud_offset(pgd_efi, EFI_VA_START);
207 pud_k = pud_offset(pgd_k, EFI_VA_START);
209 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
210 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
213 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
215 unsigned long pfn, text;
216 efi_memory_desc_t *md;
221 if (efi_enabled(EFI_OLD_MEMMAP))
224 efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd);
228 * It can happen that the physical address of new_memmap lands in memory
229 * which is not mapped in the EFI page table. Therefore we need to go
230 * and ident-map those pages containing the map before calling
231 * phys_efi_set_virtual_address_map().
233 pfn = pa_memmap >> PAGE_SHIFT;
234 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, _PAGE_NX | _PAGE_RW)) {
235 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
239 efi_scratch.use_pgd = true;
242 * When making calls to the firmware everything needs to be 1:1
243 * mapped and addressable with 32-bit pointers. Map the kernel
244 * text and allocate a new stack because we can't rely on the
245 * stack pointer being < 4GB.
247 if (!IS_ENABLED(CONFIG_EFI_MIXED))
251 * Map all of RAM so that we can access arguments in the 1:1
252 * mapping when making EFI runtime calls.
254 for_each_efi_memory_desc(md) {
255 if (md->type != EFI_CONVENTIONAL_MEMORY &&
256 md->type != EFI_LOADER_DATA &&
257 md->type != EFI_LOADER_CODE)
260 pfn = md->phys_addr >> PAGE_SHIFT;
261 npages = md->num_pages;
263 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, npages, _PAGE_RW)) {
264 pr_err("Failed to map 1:1 memory\n");
269 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
271 panic("Unable to allocate EFI runtime stack < 4GB\n");
273 efi_scratch.phys_stack = virt_to_phys(page_address(page));
274 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
276 npages = (_etext - _text) >> PAGE_SHIFT;
278 pfn = text >> PAGE_SHIFT;
280 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
281 pr_err("Failed to map kernel text 1:1\n");
288 void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
290 kernel_unmap_pages_in_pgd(efi_pgd, pa_memmap, num_pages);
293 static void __init __map_region(efi_memory_desc_t *md, u64 va)
295 unsigned long flags = _PAGE_RW;
297 pgd_t *pgd = efi_pgd;
299 if (!(md->attribute & EFI_MEMORY_WB))
302 pfn = md->phys_addr >> PAGE_SHIFT;
303 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
304 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
308 void __init efi_map_region(efi_memory_desc_t *md)
310 unsigned long size = md->num_pages << PAGE_SHIFT;
311 u64 pa = md->phys_addr;
313 if (efi_enabled(EFI_OLD_MEMMAP))
314 return old_map_region(md);
317 * Make sure the 1:1 mappings are present as a catch-all for b0rked
318 * firmware which doesn't update all internal pointers after switching
319 * to virtual mode and would otherwise crap on us.
321 __map_region(md, md->phys_addr);
324 * Enforce the 1:1 mapping as the default virtual address when
325 * booting in EFI mixed mode, because even though we may be
326 * running a 64-bit kernel, the firmware may only be 32-bit.
328 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
329 md->virt_addr = md->phys_addr;
335 /* Is PA 2M-aligned? */
336 if (!(pa & (PMD_SIZE - 1))) {
339 u64 pa_offset = pa & (PMD_SIZE - 1);
340 u64 prev_va = efi_va;
342 /* get us the same offset within this 2M page */
343 efi_va = (efi_va & PMD_MASK) + pa_offset;
345 if (efi_va > prev_va)
349 if (efi_va < EFI_VA_END) {
350 pr_warn(FW_WARN "VA address range overflow!\n");
355 __map_region(md, efi_va);
356 md->virt_addr = efi_va;
360 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
361 * md->virt_addr is the original virtual address which had been mapped in kexec
364 void __init efi_map_region_fixed(efi_memory_desc_t *md)
366 __map_region(md, md->virt_addr);
369 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
370 u32 type, u64 attribute)
372 unsigned long last_map_pfn;
374 if (type == EFI_MEMORY_MAPPED_IO)
375 return ioremap(phys_addr, size);
377 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
378 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
379 unsigned long top = last_map_pfn << PAGE_SHIFT;
380 efi_ioremap(top, size - (top - phys_addr), type, attribute);
383 if (!(attribute & EFI_MEMORY_WB))
384 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
386 return (void __iomem *)__va(phys_addr);
389 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
391 efi_setup = phys_addr + sizeof(struct setup_data);
394 void __init efi_runtime_update_mappings(void)
397 pgd_t *pgd = efi_pgd;
398 efi_memory_desc_t *md;
400 if (efi_enabled(EFI_OLD_MEMMAP)) {
401 if (__supported_pte_mask & _PAGE_NX)
402 runtime_code_page_mkexec();
406 if (!efi_enabled(EFI_NX_PE_DATA))
409 for_each_efi_memory_desc(md) {
410 unsigned long pf = 0;
412 if (!(md->attribute & EFI_MEMORY_RUNTIME))
415 if (!(md->attribute & EFI_MEMORY_WB))
418 if ((md->attribute & EFI_MEMORY_XP) ||
419 (md->type == EFI_RUNTIME_SERVICES_DATA))
422 if (!(md->attribute & EFI_MEMORY_RO) &&
423 (md->type != EFI_RUNTIME_SERVICES_CODE))
426 /* Update the 1:1 mapping */
427 pfn = md->phys_addr >> PAGE_SHIFT;
428 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf))
429 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
430 md->phys_addr, md->virt_addr);
432 if (kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf))
433 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
434 md->phys_addr, md->virt_addr);
438 void __init efi_dump_pagetable(void)
440 #ifdef CONFIG_EFI_PGT_DUMP
441 ptdump_walk_pgd_level(NULL, efi_pgd);
445 #ifdef CONFIG_EFI_MIXED
446 extern efi_status_t efi64_thunk(u32, ...);
448 #define runtime_service32(func) \
450 u32 table = (u32)(unsigned long)efi.systab; \
453 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
454 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
459 * Switch to the EFI page tables early so that we can access the 1:1
460 * runtime services mappings which are not mapped in any other page
461 * tables. This function must be called before runtime_service32().
463 * Also, disable interrupts because the IDT points to 64-bit handlers,
464 * which aren't going to function correctly when we switch to 32-bit.
466 #define efi_thunk(f, ...) \
469 unsigned long flags; \
472 efi_sync_low_kernel_mappings(); \
473 local_irq_save(flags); \
475 efi_scratch.prev_cr3 = read_cr3(); \
476 write_cr3((unsigned long)efi_scratch.efi_pgt); \
479 func = runtime_service32(f); \
480 __s = efi64_thunk(func, __VA_ARGS__); \
482 write_cr3(efi_scratch.prev_cr3); \
484 local_irq_restore(flags); \
489 efi_status_t efi_thunk_set_virtual_address_map(
490 void *phys_set_virtual_address_map,
491 unsigned long memory_map_size,
492 unsigned long descriptor_size,
493 u32 descriptor_version,
494 efi_memory_desc_t *virtual_map)
500 efi_sync_low_kernel_mappings();
501 local_irq_save(flags);
503 efi_scratch.prev_cr3 = read_cr3();
504 write_cr3((unsigned long)efi_scratch.efi_pgt);
507 func = (u32)(unsigned long)phys_set_virtual_address_map;
508 status = efi64_thunk(func, memory_map_size, descriptor_size,
509 descriptor_version, virtual_map);
511 write_cr3(efi_scratch.prev_cr3);
513 local_irq_restore(flags);
518 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
521 u32 phys_tm, phys_tc;
523 spin_lock(&rtc_lock);
525 phys_tm = virt_to_phys(tm);
526 phys_tc = virt_to_phys(tc);
528 status = efi_thunk(get_time, phys_tm, phys_tc);
530 spin_unlock(&rtc_lock);
535 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
540 spin_lock(&rtc_lock);
542 phys_tm = virt_to_phys(tm);
544 status = efi_thunk(set_time, phys_tm);
546 spin_unlock(&rtc_lock);
552 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
556 u32 phys_enabled, phys_pending, phys_tm;
558 spin_lock(&rtc_lock);
560 phys_enabled = virt_to_phys(enabled);
561 phys_pending = virt_to_phys(pending);
562 phys_tm = virt_to_phys(tm);
564 status = efi_thunk(get_wakeup_time, phys_enabled,
565 phys_pending, phys_tm);
567 spin_unlock(&rtc_lock);
573 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
578 spin_lock(&rtc_lock);
580 phys_tm = virt_to_phys(tm);
582 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
584 spin_unlock(&rtc_lock);
591 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
592 u32 *attr, unsigned long *data_size, void *data)
595 u32 phys_name, phys_vendor, phys_attr;
596 u32 phys_data_size, phys_data;
598 phys_data_size = virt_to_phys(data_size);
599 phys_vendor = virt_to_phys(vendor);
600 phys_name = virt_to_phys(name);
601 phys_attr = virt_to_phys(attr);
602 phys_data = virt_to_phys(data);
604 status = efi_thunk(get_variable, phys_name, phys_vendor,
605 phys_attr, phys_data_size, phys_data);
611 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
612 u32 attr, unsigned long data_size, void *data)
614 u32 phys_name, phys_vendor, phys_data;
617 phys_name = virt_to_phys(name);
618 phys_vendor = virt_to_phys(vendor);
619 phys_data = virt_to_phys(data);
621 /* If data_size is > sizeof(u32) we've got problems */
622 status = efi_thunk(set_variable, phys_name, phys_vendor,
623 attr, data_size, phys_data);
629 efi_thunk_get_next_variable(unsigned long *name_size,
634 u32 phys_name_size, phys_name, phys_vendor;
636 phys_name_size = virt_to_phys(name_size);
637 phys_vendor = virt_to_phys(vendor);
638 phys_name = virt_to_phys(name);
640 status = efi_thunk(get_next_variable, phys_name_size,
641 phys_name, phys_vendor);
647 efi_thunk_get_next_high_mono_count(u32 *count)
652 phys_count = virt_to_phys(count);
653 status = efi_thunk(get_next_high_mono_count, phys_count);
659 efi_thunk_reset_system(int reset_type, efi_status_t status,
660 unsigned long data_size, efi_char16_t *data)
664 phys_data = virt_to_phys(data);
666 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
670 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
671 unsigned long count, unsigned long sg_list)
674 * To properly support this function we would need to repackage
675 * 'capsules' because the firmware doesn't understand 64-bit
678 return EFI_UNSUPPORTED;
682 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
683 u64 *remaining_space,
684 u64 *max_variable_size)
687 u32 phys_storage, phys_remaining, phys_max;
689 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
690 return EFI_UNSUPPORTED;
692 phys_storage = virt_to_phys(storage_space);
693 phys_remaining = virt_to_phys(remaining_space);
694 phys_max = virt_to_phys(max_variable_size);
696 status = efi_thunk(query_variable_info, attr, phys_storage,
697 phys_remaining, phys_max);
703 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
704 unsigned long count, u64 *max_size,
708 * To properly support this function we would need to repackage
709 * 'capsules' because the firmware doesn't understand 64-bit
712 return EFI_UNSUPPORTED;
715 void efi_thunk_runtime_setup(void)
717 efi.get_time = efi_thunk_get_time;
718 efi.set_time = efi_thunk_set_time;
719 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
720 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
721 efi.get_variable = efi_thunk_get_variable;
722 efi.get_next_variable = efi_thunk_get_next_variable;
723 efi.set_variable = efi_thunk_set_variable;
724 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
725 efi.reset_system = efi_thunk_reset_system;
726 efi.query_variable_info = efi_thunk_query_variable_info;
727 efi.update_capsule = efi_thunk_update_capsule;
728 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
730 #endif /* CONFIG_EFI_MIXED */