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>
34 #include <linux/ucs2_string.h>
36 #include <asm/setup.h>
39 #include <asm/pgtable.h>
40 #include <asm/tlbflush.h>
41 #include <asm/proto.h>
43 #include <asm/cacheflush.h>
44 #include <asm/fixmap.h>
45 #include <asm/realmode.h>
47 #include <asm/pgalloc.h>
50 * We allocate runtime services regions bottom-up, starting from -4G, i.e.
51 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
53 static u64 efi_va = EFI_VA_START;
55 struct efi_scratch efi_scratch;
57 static void __init early_code_mapping_set_exec(int executable)
59 efi_memory_desc_t *md;
61 if (!(__supported_pte_mask & _PAGE_NX))
64 /* Make EFI service code area executable */
65 for_each_efi_memory_desc(md) {
66 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
67 md->type == EFI_BOOT_SERVICES_CODE)
68 efi_set_executable(md, executable);
72 pgd_t * __init efi_call_phys_prolog(void)
74 unsigned long vaddress;
80 if (!efi_enabled(EFI_OLD_MEMMAP)) {
81 save_pgd = (pgd_t *)read_cr3();
82 write_cr3((unsigned long)efi_scratch.efi_pgt);
86 early_code_mapping_set_exec(1);
88 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
89 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
91 for (pgd = 0; pgd < n_pgds; pgd++) {
92 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
93 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
94 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
102 void __init efi_call_phys_epilog(pgd_t *save_pgd)
105 * After the lock is released, the original page table is restored.
110 if (!efi_enabled(EFI_OLD_MEMMAP)) {
111 write_cr3((unsigned long)save_pgd);
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);
127 static pgd_t *efi_pgd;
130 * We need our own copy of the higher levels of the page tables
131 * because we want to avoid inserting EFI region mappings (EFI_VA_END
132 * to EFI_VA_START) into the standard kernel page tables. Everything
133 * else can be shared, see efi_sync_low_kernel_mappings().
135 int __init efi_alloc_page_tables(void)
141 if (efi_enabled(EFI_OLD_MEMMAP))
144 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
145 efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
149 pgd = efi_pgd + pgd_index(EFI_VA_END);
151 pud = pud_alloc_one(NULL, 0);
153 free_page((unsigned long)efi_pgd);
157 pgd_populate(NULL, pgd, pud);
163 * Add low kernel mappings for passing arguments to EFI functions.
165 void efi_sync_low_kernel_mappings(void)
167 unsigned num_entries;
168 pgd_t *pgd_k, *pgd_efi;
169 pud_t *pud_k, *pud_efi;
171 if (efi_enabled(EFI_OLD_MEMMAP))
175 * We can share all PGD entries apart from the one entry that
176 * covers the EFI runtime mapping space.
178 * Make sure the EFI runtime region mappings are guaranteed to
179 * only span a single PGD entry and that the entry also maps
180 * other important kernel regions.
182 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
183 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
184 (EFI_VA_END & PGDIR_MASK));
186 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
187 pgd_k = pgd_offset_k(PAGE_OFFSET);
189 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
190 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
193 * We share all the PUD entries apart from those that map the
194 * EFI regions. Copy around them.
196 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
197 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
199 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
200 pud_efi = pud_offset(pgd_efi, 0);
202 pgd_k = pgd_offset_k(EFI_VA_END);
203 pud_k = pud_offset(pgd_k, 0);
205 num_entries = pud_index(EFI_VA_END);
206 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
208 pud_efi = pud_offset(pgd_efi, EFI_VA_START);
209 pud_k = pud_offset(pgd_k, EFI_VA_START);
211 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
212 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
216 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
218 static inline phys_addr_t
219 virt_to_phys_or_null_size(void *va, unsigned long size)
226 if (virt_addr_valid(va))
227 return virt_to_phys(va);
230 * A fully aligned variable on the stack is guaranteed not to
231 * cross a page bounary. Try to catch strings on the stack by
232 * checking that 'size' is a power of two.
234 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
236 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
238 return slow_virt_to_phys(va);
241 #define virt_to_phys_or_null(addr) \
242 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
244 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
246 unsigned long pfn, text;
251 if (efi_enabled(EFI_OLD_MEMMAP))
254 efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd);
258 * It can happen that the physical address of new_memmap lands in memory
259 * which is not mapped in the EFI page table. Therefore we need to go
260 * and ident-map those pages containing the map before calling
261 * phys_efi_set_virtual_address_map().
263 pfn = pa_memmap >> PAGE_SHIFT;
264 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, _PAGE_NX | _PAGE_RW)) {
265 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
269 efi_scratch.use_pgd = true;
272 * Certain firmware versions are way too sentimential and still believe
273 * they are exclusive and unquestionable owners of the first physical page,
274 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
275 * (but then write-access it later during SetVirtualAddressMap()).
277 * Create a 1:1 mapping for this page, to avoid triple faults during early
278 * boot with such firmware. We are free to hand this page to the BIOS,
279 * as trim_bios_range() will reserve the first page and isolate it away
280 * from memory allocators anyway.
282 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, _PAGE_RW)) {
283 pr_err("Failed to create 1:1 mapping for the first page!\n");
288 * When making calls to the firmware everything needs to be 1:1
289 * mapped and addressable with 32-bit pointers. Map the kernel
290 * text and allocate a new stack because we can't rely on the
291 * stack pointer being < 4GB.
293 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
296 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
298 panic("Unable to allocate EFI runtime stack < 4GB\n");
300 efi_scratch.phys_stack = virt_to_phys(page_address(page));
301 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
303 npages = (_etext - _text) >> PAGE_SHIFT;
305 pfn = text >> PAGE_SHIFT;
307 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
308 pr_err("Failed to map kernel text 1:1\n");
315 static void __init __map_region(efi_memory_desc_t *md, u64 va)
317 unsigned long flags = _PAGE_RW;
319 pgd_t *pgd = efi_pgd;
321 if (!(md->attribute & EFI_MEMORY_WB))
324 pfn = md->phys_addr >> PAGE_SHIFT;
325 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
326 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
330 void __init efi_map_region(efi_memory_desc_t *md)
332 unsigned long size = md->num_pages << PAGE_SHIFT;
333 u64 pa = md->phys_addr;
335 if (efi_enabled(EFI_OLD_MEMMAP))
336 return old_map_region(md);
339 * Make sure the 1:1 mappings are present as a catch-all for b0rked
340 * firmware which doesn't update all internal pointers after switching
341 * to virtual mode and would otherwise crap on us.
343 __map_region(md, md->phys_addr);
346 * Enforce the 1:1 mapping as the default virtual address when
347 * booting in EFI mixed mode, because even though we may be
348 * running a 64-bit kernel, the firmware may only be 32-bit.
350 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
351 md->virt_addr = md->phys_addr;
357 /* Is PA 2M-aligned? */
358 if (!(pa & (PMD_SIZE - 1))) {
361 u64 pa_offset = pa & (PMD_SIZE - 1);
362 u64 prev_va = efi_va;
364 /* get us the same offset within this 2M page */
365 efi_va = (efi_va & PMD_MASK) + pa_offset;
367 if (efi_va > prev_va)
371 if (efi_va < EFI_VA_END) {
372 pr_warn(FW_WARN "VA address range overflow!\n");
377 __map_region(md, efi_va);
378 md->virt_addr = efi_va;
382 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
383 * md->virt_addr is the original virtual address which had been mapped in kexec
386 void __init efi_map_region_fixed(efi_memory_desc_t *md)
388 __map_region(md, md->phys_addr);
389 __map_region(md, md->virt_addr);
392 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
393 u32 type, u64 attribute)
395 unsigned long last_map_pfn;
397 if (type == EFI_MEMORY_MAPPED_IO)
398 return ioremap(phys_addr, size);
400 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
401 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
402 unsigned long top = last_map_pfn << PAGE_SHIFT;
403 efi_ioremap(top, size - (top - phys_addr), type, attribute);
406 if (!(attribute & EFI_MEMORY_WB))
407 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
409 return (void __iomem *)__va(phys_addr);
412 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
414 efi_setup = phys_addr + sizeof(struct setup_data);
417 void __init efi_runtime_update_mappings(void)
420 pgd_t *pgd = efi_pgd;
421 efi_memory_desc_t *md;
423 if (efi_enabled(EFI_OLD_MEMMAP)) {
424 if (__supported_pte_mask & _PAGE_NX)
425 runtime_code_page_mkexec();
429 if (!efi_enabled(EFI_NX_PE_DATA))
432 for_each_efi_memory_desc(md) {
433 unsigned long pf = 0;
435 if (!(md->attribute & EFI_MEMORY_RUNTIME))
438 if (!(md->attribute & EFI_MEMORY_WB))
441 if ((md->attribute & EFI_MEMORY_XP) ||
442 (md->type == EFI_RUNTIME_SERVICES_DATA))
445 if (!(md->attribute & EFI_MEMORY_RO) &&
446 (md->type != EFI_RUNTIME_SERVICES_CODE))
449 /* Update the 1:1 mapping */
450 pfn = md->phys_addr >> PAGE_SHIFT;
451 if (kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf))
452 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
453 md->phys_addr, md->virt_addr);
455 if (kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf))
456 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
457 md->phys_addr, md->virt_addr);
461 void __init efi_dump_pagetable(void)
463 #ifdef CONFIG_EFI_PGT_DUMP
464 ptdump_walk_pgd_level(NULL, efi_pgd);
468 #ifdef CONFIG_EFI_MIXED
469 extern efi_status_t efi64_thunk(u32, ...);
471 #define runtime_service32(func) \
473 u32 table = (u32)(unsigned long)efi.systab; \
476 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
477 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
482 * Switch to the EFI page tables early so that we can access the 1:1
483 * runtime services mappings which are not mapped in any other page
484 * tables. This function must be called before runtime_service32().
486 * Also, disable interrupts because the IDT points to 64-bit handlers,
487 * which aren't going to function correctly when we switch to 32-bit.
489 #define efi_thunk(f, ...) \
492 unsigned long __flags; \
495 local_irq_save(__flags); \
496 arch_efi_call_virt_setup(); \
498 __func = runtime_service32(f); \
499 __s = efi64_thunk(__func, __VA_ARGS__); \
501 arch_efi_call_virt_teardown(); \
502 local_irq_restore(__flags); \
507 efi_status_t efi_thunk_set_virtual_address_map(
508 void *phys_set_virtual_address_map,
509 unsigned long memory_map_size,
510 unsigned long descriptor_size,
511 u32 descriptor_version,
512 efi_memory_desc_t *virtual_map)
518 efi_sync_low_kernel_mappings();
519 local_irq_save(flags);
521 efi_scratch.prev_cr3 = read_cr3();
522 write_cr3((unsigned long)efi_scratch.efi_pgt);
525 func = (u32)(unsigned long)phys_set_virtual_address_map;
526 status = efi64_thunk(func, memory_map_size, descriptor_size,
527 descriptor_version, virtual_map);
529 write_cr3(efi_scratch.prev_cr3);
531 local_irq_restore(flags);
536 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
539 u32 phys_tm, phys_tc;
541 spin_lock(&rtc_lock);
543 phys_tm = virt_to_phys_or_null(tm);
544 phys_tc = virt_to_phys_or_null(tc);
546 status = efi_thunk(get_time, phys_tm, phys_tc);
548 spin_unlock(&rtc_lock);
553 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
558 spin_lock(&rtc_lock);
560 phys_tm = virt_to_phys_or_null(tm);
562 status = efi_thunk(set_time, phys_tm);
564 spin_unlock(&rtc_lock);
570 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
574 u32 phys_enabled, phys_pending, phys_tm;
576 spin_lock(&rtc_lock);
578 phys_enabled = virt_to_phys_or_null(enabled);
579 phys_pending = virt_to_phys_or_null(pending);
580 phys_tm = virt_to_phys_or_null(tm);
582 status = efi_thunk(get_wakeup_time, phys_enabled,
583 phys_pending, phys_tm);
585 spin_unlock(&rtc_lock);
591 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
596 spin_lock(&rtc_lock);
598 phys_tm = virt_to_phys_or_null(tm);
600 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
602 spin_unlock(&rtc_lock);
607 static unsigned long efi_name_size(efi_char16_t *name)
609 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
613 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
614 u32 *attr, unsigned long *data_size, void *data)
617 u32 phys_name, phys_vendor, phys_attr;
618 u32 phys_data_size, phys_data;
620 phys_data_size = virt_to_phys_or_null(data_size);
621 phys_vendor = virt_to_phys_or_null(vendor);
622 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
623 phys_attr = virt_to_phys_or_null(attr);
624 phys_data = virt_to_phys_or_null_size(data, *data_size);
626 status = efi_thunk(get_variable, phys_name, phys_vendor,
627 phys_attr, phys_data_size, phys_data);
633 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
634 u32 attr, unsigned long data_size, void *data)
636 u32 phys_name, phys_vendor, phys_data;
639 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
640 phys_vendor = virt_to_phys_or_null(vendor);
641 phys_data = virt_to_phys_or_null_size(data, data_size);
643 /* If data_size is > sizeof(u32) we've got problems */
644 status = efi_thunk(set_variable, phys_name, phys_vendor,
645 attr, data_size, phys_data);
651 efi_thunk_get_next_variable(unsigned long *name_size,
656 u32 phys_name_size, phys_name, phys_vendor;
658 phys_name_size = virt_to_phys_or_null(name_size);
659 phys_vendor = virt_to_phys_or_null(vendor);
660 phys_name = virt_to_phys_or_null_size(name, *name_size);
662 status = efi_thunk(get_next_variable, phys_name_size,
663 phys_name, phys_vendor);
669 efi_thunk_get_next_high_mono_count(u32 *count)
674 phys_count = virt_to_phys_or_null(count);
675 status = efi_thunk(get_next_high_mono_count, phys_count);
681 efi_thunk_reset_system(int reset_type, efi_status_t status,
682 unsigned long data_size, efi_char16_t *data)
686 phys_data = virt_to_phys_or_null_size(data, data_size);
688 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
692 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
693 unsigned long count, unsigned long sg_list)
696 * To properly support this function we would need to repackage
697 * 'capsules' because the firmware doesn't understand 64-bit
700 return EFI_UNSUPPORTED;
704 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
705 u64 *remaining_space,
706 u64 *max_variable_size)
709 u32 phys_storage, phys_remaining, phys_max;
711 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
712 return EFI_UNSUPPORTED;
714 phys_storage = virt_to_phys_or_null(storage_space);
715 phys_remaining = virt_to_phys_or_null(remaining_space);
716 phys_max = virt_to_phys_or_null(max_variable_size);
718 status = efi_thunk(query_variable_info, attr, phys_storage,
719 phys_remaining, phys_max);
725 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
726 unsigned long count, u64 *max_size,
730 * To properly support this function we would need to repackage
731 * 'capsules' because the firmware doesn't understand 64-bit
734 return EFI_UNSUPPORTED;
737 void efi_thunk_runtime_setup(void)
739 efi.get_time = efi_thunk_get_time;
740 efi.set_time = efi_thunk_set_time;
741 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
742 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
743 efi.get_variable = efi_thunk_get_variable;
744 efi.get_next_variable = efi_thunk_get_next_variable;
745 efi.set_variable = efi_thunk_set_variable;
746 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
747 efi.reset_system = efi_thunk_reset_system;
748 efi.query_variable_info = efi_thunk_query_variable_info;
749 efi.update_capsule = efi_thunk_update_capsule;
750 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
752 #endif /* CONFIG_EFI_MIXED */