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>
44 static pgd_t *save_pgd __initdata;
45 static unsigned long efi_flags __initdata;
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;
54 * Scratch space used for switching the pagetable in the EFI stub
64 static void __init early_code_mapping_set_exec(int executable)
66 efi_memory_desc_t *md;
69 if (!(__supported_pte_mask & _PAGE_NX))
72 /* Make EFI service code area executable */
73 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
75 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
76 md->type == EFI_BOOT_SERVICES_CODE)
77 efi_set_executable(md, executable);
81 void __init efi_call_phys_prolog(void)
83 unsigned long vaddress;
87 if (!efi_enabled(EFI_OLD_MEMMAP))
90 early_code_mapping_set_exec(1);
91 local_irq_save(efi_flags);
93 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
94 save_pgd = kmalloc(n_pgds * sizeof(pgd_t), GFP_KERNEL);
96 for (pgd = 0; pgd < n_pgds; pgd++) {
97 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
98 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
99 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
104 void __init efi_call_phys_epilog(void)
107 * After the lock is released, the original page table is restored.
110 int n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
112 if (!efi_enabled(EFI_OLD_MEMMAP))
115 for (pgd = 0; pgd < n_pgds; pgd++)
116 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), save_pgd[pgd]);
119 local_irq_restore(efi_flags);
120 early_code_mapping_set_exec(0);
124 * Add low kernel mappings for passing arguments to EFI functions.
126 void efi_sync_low_kernel_mappings(void)
129 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
131 if (efi_enabled(EFI_OLD_MEMMAP))
134 num_pgds = pgd_index(MODULES_END - 1) - pgd_index(PAGE_OFFSET);
136 memcpy(pgd + pgd_index(PAGE_OFFSET),
137 init_mm.pgd + pgd_index(PAGE_OFFSET),
138 sizeof(pgd_t) * num_pgds);
141 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
148 if (efi_enabled(EFI_OLD_MEMMAP))
151 efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
152 pgd = __va(efi_scratch.efi_pgt);
155 * It can happen that the physical address of new_memmap lands in memory
156 * which is not mapped in the EFI page table. Therefore we need to go
157 * and ident-map those pages containing the map before calling
158 * phys_efi_set_virtual_address_map().
160 if (kernel_map_pages_in_pgd(pgd, pa_memmap, pa_memmap, num_pages, _PAGE_NX)) {
161 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
165 efi_scratch.use_pgd = true;
168 * When making calls to the firmware everything needs to be 1:1
169 * mapped and addressable with 32-bit pointers. Map the kernel
170 * text and allocate a new stack because we can't rely on the
171 * stack pointer being < 4GB.
173 if (!IS_ENABLED(CONFIG_EFI_MIXED))
176 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
178 panic("Unable to allocate EFI runtime stack < 4GB\n");
180 efi_scratch.phys_stack = virt_to_phys(page_address(page));
181 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
183 npages = (_end - _text) >> PAGE_SHIFT;
186 if (kernel_map_pages_in_pgd(pgd, text >> PAGE_SHIFT, text, npages, 0)) {
187 pr_err("Failed to map kernel text 1:1\n");
194 void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
196 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
198 kernel_unmap_pages_in_pgd(pgd, pa_memmap, num_pages);
201 static void __init __map_region(efi_memory_desc_t *md, u64 va)
203 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
204 unsigned long pf = 0;
206 if (!(md->attribute & EFI_MEMORY_WB))
209 if (kernel_map_pages_in_pgd(pgd, md->phys_addr, va, md->num_pages, pf))
210 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
214 void __init efi_map_region(efi_memory_desc_t *md)
216 unsigned long size = md->num_pages << PAGE_SHIFT;
217 u64 pa = md->phys_addr;
219 if (efi_enabled(EFI_OLD_MEMMAP))
220 return old_map_region(md);
223 * Make sure the 1:1 mappings are present as a catch-all for b0rked
224 * firmware which doesn't update all internal pointers after switching
225 * to virtual mode and would otherwise crap on us.
227 __map_region(md, md->phys_addr);
230 * Enforce the 1:1 mapping as the default virtual address when
231 * booting in EFI mixed mode, because even though we may be
232 * running a 64-bit kernel, the firmware may only be 32-bit.
234 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
235 md->virt_addr = md->phys_addr;
241 /* Is PA 2M-aligned? */
242 if (!(pa & (PMD_SIZE - 1))) {
245 u64 pa_offset = pa & (PMD_SIZE - 1);
246 u64 prev_va = efi_va;
248 /* get us the same offset within this 2M page */
249 efi_va = (efi_va & PMD_MASK) + pa_offset;
251 if (efi_va > prev_va)
255 if (efi_va < EFI_VA_END) {
256 pr_warn(FW_WARN "VA address range overflow!\n");
261 __map_region(md, efi_va);
262 md->virt_addr = efi_va;
266 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
267 * md->virt_addr is the original virtual address which had been mapped in kexec
270 void __init efi_map_region_fixed(efi_memory_desc_t *md)
272 __map_region(md, md->virt_addr);
275 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
276 u32 type, u64 attribute)
278 unsigned long last_map_pfn;
280 if (type == EFI_MEMORY_MAPPED_IO)
281 return ioremap(phys_addr, size);
283 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
284 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
285 unsigned long top = last_map_pfn << PAGE_SHIFT;
286 efi_ioremap(top, size - (top - phys_addr), type, attribute);
289 if (!(attribute & EFI_MEMORY_WB))
290 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
292 return (void __iomem *)__va(phys_addr);
295 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
297 efi_setup = phys_addr + sizeof(struct setup_data);
300 void __init efi_runtime_mkexec(void)
302 if (!efi_enabled(EFI_OLD_MEMMAP))
305 if (__supported_pte_mask & _PAGE_NX)
306 runtime_code_page_mkexec();
309 void __init efi_dump_pagetable(void)
311 #ifdef CONFIG_EFI_PGT_DUMP
312 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
314 ptdump_walk_pgd_level(NULL, pgd);
318 #ifdef CONFIG_EFI_MIXED
319 extern efi_status_t efi64_thunk(u32, ...);
321 #define runtime_service32(func) \
323 u32 table = (u32)(unsigned long)efi.systab; \
326 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
327 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
332 * Switch to the EFI page tables early so that we can access the 1:1
333 * runtime services mappings which are not mapped in any other page
334 * tables. This function must be called before runtime_service32().
336 * Also, disable interrupts because the IDT points to 64-bit handlers,
337 * which aren't going to function correctly when we switch to 32-bit.
339 #define efi_thunk(f, ...) \
342 unsigned long flags; \
345 efi_sync_low_kernel_mappings(); \
346 local_irq_save(flags); \
348 efi_scratch.prev_cr3 = read_cr3(); \
349 write_cr3((unsigned long)efi_scratch.efi_pgt); \
352 func = runtime_service32(f); \
353 __s = efi64_thunk(func, __VA_ARGS__); \
355 write_cr3(efi_scratch.prev_cr3); \
357 local_irq_restore(flags); \
362 efi_status_t efi_thunk_set_virtual_address_map(
363 void *phys_set_virtual_address_map,
364 unsigned long memory_map_size,
365 unsigned long descriptor_size,
366 u32 descriptor_version,
367 efi_memory_desc_t *virtual_map)
373 efi_sync_low_kernel_mappings();
374 local_irq_save(flags);
376 efi_scratch.prev_cr3 = read_cr3();
377 write_cr3((unsigned long)efi_scratch.efi_pgt);
380 func = (u32)(unsigned long)phys_set_virtual_address_map;
381 status = efi64_thunk(func, memory_map_size, descriptor_size,
382 descriptor_version, virtual_map);
384 write_cr3(efi_scratch.prev_cr3);
386 local_irq_restore(flags);
391 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
394 u32 phys_tm, phys_tc;
396 spin_lock(&rtc_lock);
398 phys_tm = virt_to_phys(tm);
399 phys_tc = virt_to_phys(tc);
401 status = efi_thunk(get_time, phys_tm, phys_tc);
403 spin_unlock(&rtc_lock);
408 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
413 spin_lock(&rtc_lock);
415 phys_tm = virt_to_phys(tm);
417 status = efi_thunk(set_time, phys_tm);
419 spin_unlock(&rtc_lock);
425 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
429 u32 phys_enabled, phys_pending, phys_tm;
431 spin_lock(&rtc_lock);
433 phys_enabled = virt_to_phys(enabled);
434 phys_pending = virt_to_phys(pending);
435 phys_tm = virt_to_phys(tm);
437 status = efi_thunk(get_wakeup_time, phys_enabled,
438 phys_pending, phys_tm);
440 spin_unlock(&rtc_lock);
446 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
451 spin_lock(&rtc_lock);
453 phys_tm = virt_to_phys(tm);
455 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
457 spin_unlock(&rtc_lock);
464 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
465 u32 *attr, unsigned long *data_size, void *data)
468 u32 phys_name, phys_vendor, phys_attr;
469 u32 phys_data_size, phys_data;
471 phys_data_size = virt_to_phys(data_size);
472 phys_vendor = virt_to_phys(vendor);
473 phys_name = virt_to_phys(name);
474 phys_attr = virt_to_phys(attr);
475 phys_data = virt_to_phys(data);
477 status = efi_thunk(get_variable, phys_name, phys_vendor,
478 phys_attr, phys_data_size, phys_data);
484 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
485 u32 attr, unsigned long data_size, void *data)
487 u32 phys_name, phys_vendor, phys_data;
490 phys_name = virt_to_phys(name);
491 phys_vendor = virt_to_phys(vendor);
492 phys_data = virt_to_phys(data);
494 /* If data_size is > sizeof(u32) we've got problems */
495 status = efi_thunk(set_variable, phys_name, phys_vendor,
496 attr, data_size, phys_data);
502 efi_thunk_get_next_variable(unsigned long *name_size,
507 u32 phys_name_size, phys_name, phys_vendor;
509 phys_name_size = virt_to_phys(name_size);
510 phys_vendor = virt_to_phys(vendor);
511 phys_name = virt_to_phys(name);
513 status = efi_thunk(get_next_variable, phys_name_size,
514 phys_name, phys_vendor);
520 efi_thunk_get_next_high_mono_count(u32 *count)
525 phys_count = virt_to_phys(count);
526 status = efi_thunk(get_next_high_mono_count, phys_count);
532 efi_thunk_reset_system(int reset_type, efi_status_t status,
533 unsigned long data_size, efi_char16_t *data)
537 phys_data = virt_to_phys(data);
539 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
543 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
544 unsigned long count, unsigned long sg_list)
547 * To properly support this function we would need to repackage
548 * 'capsules' because the firmware doesn't understand 64-bit
551 return EFI_UNSUPPORTED;
555 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
556 u64 *remaining_space,
557 u64 *max_variable_size)
560 u32 phys_storage, phys_remaining, phys_max;
562 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
563 return EFI_UNSUPPORTED;
565 phys_storage = virt_to_phys(storage_space);
566 phys_remaining = virt_to_phys(remaining_space);
567 phys_max = virt_to_phys(max_variable_size);
569 status = efi_thunk(query_variable_info, attr, phys_storage,
570 phys_remaining, phys_max);
576 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
577 unsigned long count, u64 *max_size,
581 * To properly support this function we would need to repackage
582 * 'capsules' because the firmware doesn't understand 64-bit
585 return EFI_UNSUPPORTED;
588 void efi_thunk_runtime_setup(void)
590 efi.get_time = efi_thunk_get_time;
591 efi.set_time = efi_thunk_set_time;
592 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
593 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
594 efi.get_variable = efi_thunk_get_variable;
595 efi.get_next_variable = efi_thunk_get_next_variable;
596 efi.set_variable = efi_thunk_set_variable;
597 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
598 efi.reset_system = efi_thunk_reset_system;
599 efi.query_variable_info = efi_thunk_query_variable_info;
600 efi.update_capsule = efi_thunk_update_capsule;
601 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
603 #endif /* CONFIG_EFI_MIXED */