11965aae3SH. Peter Anvin #ifndef _ASM_X86_EFI_H 21965aae3SH. Peter Anvin #define _ASM_X86_EFI_H 3bb898558SAl Viro 4df6b35f4SIngo Molnar #include <asm/fpu/api.h> 5744937b0SIngo Molnar #include <asm/pgtable.h> 6c9f2a9a6SMatt Fleming #include <asm/tlb.h> 7744937b0SIngo Molnar 8d2f7cbe7SBorislav Petkov /* 9d2f7cbe7SBorislav Petkov * We map the EFI regions needed for runtime services non-contiguously, 10d2f7cbe7SBorislav Petkov * with preserved alignment on virtual addresses starting from -4G down 11d2f7cbe7SBorislav Petkov * for a total max space of 64G. This way, we provide for stable runtime 12d2f7cbe7SBorislav Petkov * services addresses across kernels so that a kexec'd kernel can still 13d2f7cbe7SBorislav Petkov * use them. 14d2f7cbe7SBorislav Petkov * 15d2f7cbe7SBorislav Petkov * This is the main reason why we're doing stable VA mappings for RT 16d2f7cbe7SBorislav Petkov * services. 17d2f7cbe7SBorislav Petkov * 18d2f7cbe7SBorislav Petkov * This flag is used in conjuction with a chicken bit called 19d2f7cbe7SBorislav Petkov * "efi=old_map" which can be used as a fallback to the old runtime 20d2f7cbe7SBorislav Petkov * services mapping method in case there's some b0rkage with a 21d2f7cbe7SBorislav Petkov * particular EFI implementation (haha, it is hard to hold up the 22d2f7cbe7SBorislav Petkov * sarcasm here...). 23d2f7cbe7SBorislav Petkov */ 24d2f7cbe7SBorislav Petkov #define EFI_OLD_MEMMAP EFI_ARCH_1 25d2f7cbe7SBorislav Petkov 26b8ff87a6SMatt Fleming #define EFI32_LOADER_SIGNATURE "EL32" 27b8ff87a6SMatt Fleming #define EFI64_LOADER_SIGNATURE "EL64" 28b8ff87a6SMatt Fleming 29bb898558SAl Viro #ifdef CONFIG_X86_32 30bb898558SAl Viro 31f7d7d01bSMatt Fleming 32bb898558SAl Viro extern unsigned long asmlinkage efi_call_phys(void *, ...); 33bb898558SAl Viro 34bb898558SAl Viro /* 35bb898558SAl Viro * Wrap all the virtual calls in a way that forces the parameters on the stack. 36bb898558SAl Viro */ 37bb898558SAl Viro 38982e239cSRicardo Neri /* Use this macro if your virtual returns a non-void value */ 39bb898558SAl Viro #define efi_call_virt(f, args...) \ 40b738c6eaSRicardo Neri ({ \ 41b738c6eaSRicardo Neri efi_status_t __s; \ 42b738c6eaSRicardo Neri kernel_fpu_begin(); \ 43b738c6eaSRicardo Neri __s = ((efi_##f##_t __attribute__((regparm(0)))*) \ 44b738c6eaSRicardo Neri efi.systab->runtime->f)(args); \ 45b738c6eaSRicardo Neri kernel_fpu_end(); \ 46b738c6eaSRicardo Neri __s; \ 47b738c6eaSRicardo Neri }) 48bb898558SAl Viro 49982e239cSRicardo Neri /* Use this macro if your virtual call does not return any value */ 50b738c6eaSRicardo Neri #define __efi_call_virt(f, args...) \ 51b738c6eaSRicardo Neri ({ \ 52b738c6eaSRicardo Neri kernel_fpu_begin(); \ 53b738c6eaSRicardo Neri ((efi_##f##_t __attribute__((regparm(0)))*) \ 54b738c6eaSRicardo Neri efi.systab->runtime->f)(args); \ 55b738c6eaSRicardo Neri kernel_fpu_end(); \ 56b738c6eaSRicardo Neri }) 57982e239cSRicardo Neri 583e8fa263SMatt Fleming #define efi_ioremap(addr, size, type, attr) ioremap_cache(addr, size) 59e1ad783bSKeith Packard 60bb898558SAl Viro #else /* !CONFIG_X86_32 */ 61bb898558SAl Viro 6262fa6e69SMatt Fleming #define EFI_LOADER_SIGNATURE "EL64" 63bb898558SAl Viro 6462fa6e69SMatt Fleming extern u64 asmlinkage efi_call(void *fp, ...); 65bb898558SAl Viro 6662fa6e69SMatt Fleming #define efi_call_phys(f, args...) efi_call((f), args) 6762fa6e69SMatt Fleming 68c9f2a9a6SMatt Fleming /* 69c9f2a9a6SMatt Fleming * Scratch space used for switching the pagetable in the EFI stub 70c9f2a9a6SMatt Fleming */ 71c9f2a9a6SMatt Fleming struct efi_scratch { 72c9f2a9a6SMatt Fleming u64 r15; 73c9f2a9a6SMatt Fleming u64 prev_cr3; 74c9f2a9a6SMatt Fleming pgd_t *efi_pgt; 75c9f2a9a6SMatt Fleming bool use_pgd; 76c9f2a9a6SMatt Fleming u64 phys_stack; 77c9f2a9a6SMatt Fleming } __packed; 78c9f2a9a6SMatt Fleming 7962fa6e69SMatt Fleming #define efi_call_virt(f, ...) \ 80d2f7cbe7SBorislav Petkov ({ \ 81d2f7cbe7SBorislav Petkov efi_status_t __s; \ 82d2f7cbe7SBorislav Petkov \ 83d2f7cbe7SBorislav Petkov efi_sync_low_kernel_mappings(); \ 84d2f7cbe7SBorislav Petkov preempt_disable(); \ 85de05764eSRicardo Neri __kernel_fpu_begin(); \ 86c9f2a9a6SMatt Fleming \ 87c9f2a9a6SMatt Fleming if (efi_scratch.use_pgd) { \ 88c9f2a9a6SMatt Fleming efi_scratch.prev_cr3 = read_cr3(); \ 89c9f2a9a6SMatt Fleming write_cr3((unsigned long)efi_scratch.efi_pgt); \ 90c9f2a9a6SMatt Fleming __flush_tlb_all(); \ 91c9f2a9a6SMatt Fleming } \ 92c9f2a9a6SMatt Fleming \ 9362fa6e69SMatt Fleming __s = efi_call((void *)efi.systab->runtime->f, __VA_ARGS__); \ 94c9f2a9a6SMatt Fleming \ 95c9f2a9a6SMatt Fleming if (efi_scratch.use_pgd) { \ 96c9f2a9a6SMatt Fleming write_cr3(efi_scratch.prev_cr3); \ 97c9f2a9a6SMatt Fleming __flush_tlb_all(); \ 98c9f2a9a6SMatt Fleming } \ 99c9f2a9a6SMatt Fleming \ 100de05764eSRicardo Neri __kernel_fpu_end(); \ 101d2f7cbe7SBorislav Petkov preempt_enable(); \ 102d2f7cbe7SBorislav Petkov __s; \ 103d2f7cbe7SBorislav Petkov }) 104d2f7cbe7SBorislav Petkov 105982e239cSRicardo Neri /* 106982e239cSRicardo Neri * All X86_64 virt calls return non-void values. Thus, use non-void call for 107982e239cSRicardo Neri * virt calls that would be void on X86_32. 108982e239cSRicardo Neri */ 109982e239cSRicardo Neri #define __efi_call_virt(f, args...) efi_call_virt(f, args) 110982e239cSRicardo Neri 1114e78eb05SMathias Krause extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size, 1123e8fa263SMatt Fleming u32 type, u64 attribute); 113e1ad783bSKeith Packard 114a523841eSAndrey Ryabinin #ifdef CONFIG_KASAN 115769a8089SAndrey Ryabinin /* 116769a8089SAndrey Ryabinin * CONFIG_KASAN may redefine memset to __memset. __memset function is present 117769a8089SAndrey Ryabinin * only in kernel binary. Since the EFI stub linked into a separate binary it 118769a8089SAndrey Ryabinin * doesn't have __memset(). So we should use standard memset from 119769a8089SAndrey Ryabinin * arch/x86/boot/compressed/string.c. The same applies to memcpy and memmove. 120769a8089SAndrey Ryabinin */ 121769a8089SAndrey Ryabinin #undef memcpy 122769a8089SAndrey Ryabinin #undef memset 123769a8089SAndrey Ryabinin #undef memmove 124a523841eSAndrey Ryabinin #endif 125769a8089SAndrey Ryabinin 126bb898558SAl Viro #endif /* CONFIG_X86_32 */ 127bb898558SAl Viro 128d2f7cbe7SBorislav Petkov extern struct efi_scratch efi_scratch; 1294e78eb05SMathias Krause extern void __init efi_set_executable(efi_memory_desc_t *md, bool executable); 1304e78eb05SMathias Krause extern int __init efi_memblock_x86_reserve_range(void); 131744937b0SIngo Molnar extern pgd_t * __init efi_call_phys_prolog(void); 132744937b0SIngo Molnar extern void __init efi_call_phys_epilog(pgd_t *save_pgd); 1330bbea1ceSTaku Izumi extern void __init efi_print_memmap(void); 1344e78eb05SMathias Krause extern void __init efi_unmap_memmap(void); 1354e78eb05SMathias Krause extern void __init efi_memory_uc(u64 addr, unsigned long size); 136d2f7cbe7SBorislav Petkov extern void __init efi_map_region(efi_memory_desc_t *md); 1373b266496SDave Young extern void __init efi_map_region_fixed(efi_memory_desc_t *md); 138d2f7cbe7SBorislav Petkov extern void efi_sync_low_kernel_mappings(void); 139*67a9108eSMatt Fleming extern int __init efi_alloc_page_tables(void); 1404e78eb05SMathias Krause extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages); 1414e78eb05SMathias Krause extern void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages); 142d2f7cbe7SBorislav Petkov extern void __init old_map_region(efi_memory_desc_t *md); 143c55d016fSBorislav Petkov extern void __init runtime_code_page_mkexec(void); 144c55d016fSBorislav Petkov extern void __init efi_runtime_mkexec(void); 14511cc8512SBorislav Petkov extern void __init efi_dump_pagetable(void); 146a5d90c92SBorislav Petkov extern void __init efi_apply_memmap_quirks(void); 147eeb9db09SSaurabh Tangri extern int __init efi_reuse_config(u64 tables, int nr_tables); 148eeb9db09SSaurabh Tangri extern void efi_delete_dummy_variable(void); 149bb898558SAl Viro 1501fec0533SDave Young struct efi_setup_data { 1511fec0533SDave Young u64 fw_vendor; 1521fec0533SDave Young u64 runtime; 1531fec0533SDave Young u64 tables; 1541fec0533SDave Young u64 smbios; 1551fec0533SDave Young u64 reserved[8]; 1561fec0533SDave Young }; 1571fec0533SDave Young 1581fec0533SDave Young extern u64 efi_setup; 1591fec0533SDave Young 1606b59e366SSatoru Takeuchi #ifdef CONFIG_EFI 1616b59e366SSatoru Takeuchi 1626b59e366SSatoru Takeuchi static inline bool efi_is_native(void) 1636b59e366SSatoru Takeuchi { 1646b59e366SSatoru Takeuchi return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT); 1656b59e366SSatoru Takeuchi } 1666b59e366SSatoru Takeuchi 1677d453eeeSMatt Fleming static inline bool efi_runtime_supported(void) 1687d453eeeSMatt Fleming { 1697d453eeeSMatt Fleming if (efi_is_native()) 1707d453eeeSMatt Fleming return true; 1717d453eeeSMatt Fleming 1727d453eeeSMatt Fleming if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_enabled(EFI_OLD_MEMMAP)) 1737d453eeeSMatt Fleming return true; 1747d453eeeSMatt Fleming 1757d453eeeSMatt Fleming return false; 1767d453eeeSMatt Fleming } 1777d453eeeSMatt Fleming 17872548e83SMatt Fleming extern struct console early_efi_console; 1795c12af0cSDave Young extern void parse_efi_setup(u64 phys_addr, u32 data_len); 1804f9dbcfcSMatt Fleming 1814f9dbcfcSMatt Fleming #ifdef CONFIG_EFI_MIXED 1824f9dbcfcSMatt Fleming extern void efi_thunk_runtime_setup(void); 1834f9dbcfcSMatt Fleming extern efi_status_t efi_thunk_set_virtual_address_map( 1844f9dbcfcSMatt Fleming void *phys_set_virtual_address_map, 1854f9dbcfcSMatt Fleming unsigned long memory_map_size, 1864f9dbcfcSMatt Fleming unsigned long descriptor_size, 1874f9dbcfcSMatt Fleming u32 descriptor_version, 1884f9dbcfcSMatt Fleming efi_memory_desc_t *virtual_map); 1894f9dbcfcSMatt Fleming #else 1904f9dbcfcSMatt Fleming static inline void efi_thunk_runtime_setup(void) {} 1914f9dbcfcSMatt Fleming static inline efi_status_t efi_thunk_set_virtual_address_map( 1924f9dbcfcSMatt Fleming void *phys_set_virtual_address_map, 1934f9dbcfcSMatt Fleming unsigned long memory_map_size, 1944f9dbcfcSMatt Fleming unsigned long descriptor_size, 1954f9dbcfcSMatt Fleming u32 descriptor_version, 1964f9dbcfcSMatt Fleming efi_memory_desc_t *virtual_map) 1974f9dbcfcSMatt Fleming { 1984f9dbcfcSMatt Fleming return EFI_SUCCESS; 1994f9dbcfcSMatt Fleming } 2004f9dbcfcSMatt Fleming #endif /* CONFIG_EFI_MIXED */ 201f23cf8bdSArd Biesheuvel 202243b6754SArd Biesheuvel 203243b6754SArd Biesheuvel /* arch specific definitions used by the stub code */ 204243b6754SArd Biesheuvel 205243b6754SArd Biesheuvel struct efi_config { 206243b6754SArd Biesheuvel u64 image_handle; 207243b6754SArd Biesheuvel u64 table; 208243b6754SArd Biesheuvel u64 allocate_pool; 209243b6754SArd Biesheuvel u64 allocate_pages; 210243b6754SArd Biesheuvel u64 get_memory_map; 211243b6754SArd Biesheuvel u64 free_pool; 212243b6754SArd Biesheuvel u64 free_pages; 213243b6754SArd Biesheuvel u64 locate_handle; 214243b6754SArd Biesheuvel u64 handle_protocol; 215243b6754SArd Biesheuvel u64 exit_boot_services; 216243b6754SArd Biesheuvel u64 text_output; 217243b6754SArd Biesheuvel efi_status_t (*call)(unsigned long, ...); 218243b6754SArd Biesheuvel bool is64; 219243b6754SArd Biesheuvel } __packed; 220243b6754SArd Biesheuvel 221243b6754SArd Biesheuvel __pure const struct efi_config *__efi_early(void); 222243b6754SArd Biesheuvel 223243b6754SArd Biesheuvel #define efi_call_early(f, ...) \ 224243b6754SArd Biesheuvel __efi_early()->call(__efi_early()->f, __VA_ARGS__); 225243b6754SArd Biesheuvel 22644be28e9SMatt Fleming extern bool efi_reboot_required(void); 22744be28e9SMatt Fleming 2286b59e366SSatoru Takeuchi #else 2295c12af0cSDave Young static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {} 23044be28e9SMatt Fleming static inline bool efi_reboot_required(void) 23144be28e9SMatt Fleming { 23244be28e9SMatt Fleming return false; 23344be28e9SMatt Fleming } 234bb898558SAl Viro #endif /* CONFIG_EFI */ 235bb898558SAl Viro 2361965aae3SH. Peter Anvin #endif /* _ASM_X86_EFI_H */ 237