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 2948fcb2d0SArd Biesheuvel #define MAX_CMDLINE_ADDRESS UINT_MAX 3048fcb2d0SArd Biesheuvel 31bb898558SAl Viro #ifdef CONFIG_X86_32 32bb898558SAl Viro 33f7d7d01bSMatt Fleming 34bb898558SAl Viro extern unsigned long asmlinkage efi_call_phys(void *, ...); 35bb898558SAl Viro 36*bc25f9dbSMark Rutland #define arch_efi_call_virt_setup() kernel_fpu_begin() 37*bc25f9dbSMark Rutland #define arch_efi_call_virt_teardown() kernel_fpu_end() 38*bc25f9dbSMark Rutland 39bb898558SAl Viro /* 40bb898558SAl Viro * Wrap all the virtual calls in a way that forces the parameters on the stack. 41bb898558SAl Viro */ 42*bc25f9dbSMark Rutland #define arch_efi_call_virt(f, args...) \ 43b738c6eaSRicardo Neri ({ \ 44b738c6eaSRicardo Neri ((efi_##f##_t __attribute__((regparm(0)))*) \ 45b738c6eaSRicardo Neri efi.systab->runtime->f)(args); \ 46b738c6eaSRicardo Neri }) 47982e239cSRicardo Neri 483e8fa263SMatt Fleming #define efi_ioremap(addr, size, type, attr) ioremap_cache(addr, size) 49e1ad783bSKeith Packard 50bb898558SAl Viro #else /* !CONFIG_X86_32 */ 51bb898558SAl Viro 5262fa6e69SMatt Fleming #define EFI_LOADER_SIGNATURE "EL64" 53bb898558SAl Viro 5462fa6e69SMatt Fleming extern u64 asmlinkage efi_call(void *fp, ...); 55bb898558SAl Viro 5662fa6e69SMatt Fleming #define efi_call_phys(f, args...) efi_call((f), args) 5762fa6e69SMatt Fleming 58c9f2a9a6SMatt Fleming /* 59c9f2a9a6SMatt Fleming * Scratch space used for switching the pagetable in the EFI stub 60c9f2a9a6SMatt Fleming */ 61c9f2a9a6SMatt Fleming struct efi_scratch { 62c9f2a9a6SMatt Fleming u64 r15; 63c9f2a9a6SMatt Fleming u64 prev_cr3; 64c9f2a9a6SMatt Fleming pgd_t *efi_pgt; 65c9f2a9a6SMatt Fleming bool use_pgd; 66c9f2a9a6SMatt Fleming u64 phys_stack; 67c9f2a9a6SMatt Fleming } __packed; 68c9f2a9a6SMatt Fleming 69*bc25f9dbSMark Rutland #define arch_efi_call_virt_setup() \ 70d2f7cbe7SBorislav Petkov ({ \ 71d2f7cbe7SBorislav Petkov efi_sync_low_kernel_mappings(); \ 72d2f7cbe7SBorislav Petkov preempt_disable(); \ 73de05764eSRicardo Neri __kernel_fpu_begin(); \ 74c9f2a9a6SMatt Fleming \ 75c9f2a9a6SMatt Fleming if (efi_scratch.use_pgd) { \ 76c9f2a9a6SMatt Fleming efi_scratch.prev_cr3 = read_cr3(); \ 77c9f2a9a6SMatt Fleming write_cr3((unsigned long)efi_scratch.efi_pgt); \ 78c9f2a9a6SMatt Fleming __flush_tlb_all(); \ 79c9f2a9a6SMatt Fleming } \ 80*bc25f9dbSMark Rutland }) 81*bc25f9dbSMark Rutland 82*bc25f9dbSMark Rutland #define arch_efi_call_virt(f, args...) \ 83*bc25f9dbSMark Rutland efi_call((void *)efi.systab->runtime->f, args) \ 84*bc25f9dbSMark Rutland 85*bc25f9dbSMark Rutland #define arch_efi_call_virt_teardown() \ 86*bc25f9dbSMark Rutland ({ \ 87c9f2a9a6SMatt Fleming if (efi_scratch.use_pgd) { \ 88c9f2a9a6SMatt Fleming write_cr3(efi_scratch.prev_cr3); \ 89c9f2a9a6SMatt Fleming __flush_tlb_all(); \ 90c9f2a9a6SMatt Fleming } \ 91c9f2a9a6SMatt Fleming \ 92de05764eSRicardo Neri __kernel_fpu_end(); \ 93d2f7cbe7SBorislav Petkov preempt_enable(); \ 94d2f7cbe7SBorislav Petkov }) 95d2f7cbe7SBorislav Petkov 964e78eb05SMathias Krause extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size, 973e8fa263SMatt Fleming u32 type, u64 attribute); 98e1ad783bSKeith Packard 99a523841eSAndrey Ryabinin #ifdef CONFIG_KASAN 100769a8089SAndrey Ryabinin /* 101769a8089SAndrey Ryabinin * CONFIG_KASAN may redefine memset to __memset. __memset function is present 102769a8089SAndrey Ryabinin * only in kernel binary. Since the EFI stub linked into a separate binary it 103769a8089SAndrey Ryabinin * doesn't have __memset(). So we should use standard memset from 104769a8089SAndrey Ryabinin * arch/x86/boot/compressed/string.c. The same applies to memcpy and memmove. 105769a8089SAndrey Ryabinin */ 106769a8089SAndrey Ryabinin #undef memcpy 107769a8089SAndrey Ryabinin #undef memset 108769a8089SAndrey Ryabinin #undef memmove 109a523841eSAndrey Ryabinin #endif 110769a8089SAndrey Ryabinin 111bb898558SAl Viro #endif /* CONFIG_X86_32 */ 112bb898558SAl Viro 113d2f7cbe7SBorislav Petkov extern struct efi_scratch efi_scratch; 1144e78eb05SMathias Krause extern void __init efi_set_executable(efi_memory_desc_t *md, bool executable); 1154e78eb05SMathias Krause extern int __init efi_memblock_x86_reserve_range(void); 116744937b0SIngo Molnar extern pgd_t * __init efi_call_phys_prolog(void); 117744937b0SIngo Molnar extern void __init efi_call_phys_epilog(pgd_t *save_pgd); 1180bbea1ceSTaku Izumi extern void __init efi_print_memmap(void); 1194e78eb05SMathias Krause extern void __init efi_unmap_memmap(void); 1204e78eb05SMathias Krause extern void __init efi_memory_uc(u64 addr, unsigned long size); 121d2f7cbe7SBorislav Petkov extern void __init efi_map_region(efi_memory_desc_t *md); 1223b266496SDave Young extern void __init efi_map_region_fixed(efi_memory_desc_t *md); 123d2f7cbe7SBorislav Petkov extern void efi_sync_low_kernel_mappings(void); 12467a9108eSMatt Fleming extern int __init efi_alloc_page_tables(void); 1254e78eb05SMathias Krause extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages); 1264e78eb05SMathias Krause extern void __init efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages); 127d2f7cbe7SBorislav Petkov extern void __init old_map_region(efi_memory_desc_t *md); 128c55d016fSBorislav Petkov extern void __init runtime_code_page_mkexec(void); 1296d0cc887SSai Praneeth extern void __init efi_runtime_update_mappings(void); 13011cc8512SBorislav Petkov extern void __init efi_dump_pagetable(void); 131a5d90c92SBorislav Petkov extern void __init efi_apply_memmap_quirks(void); 132eeb9db09SSaurabh Tangri extern int __init efi_reuse_config(u64 tables, int nr_tables); 133eeb9db09SSaurabh Tangri extern void efi_delete_dummy_variable(void); 134bb898558SAl Viro 1351fec0533SDave Young struct efi_setup_data { 1361fec0533SDave Young u64 fw_vendor; 1371fec0533SDave Young u64 runtime; 1381fec0533SDave Young u64 tables; 1391fec0533SDave Young u64 smbios; 1401fec0533SDave Young u64 reserved[8]; 1411fec0533SDave Young }; 1421fec0533SDave Young 1431fec0533SDave Young extern u64 efi_setup; 1441fec0533SDave Young 1456b59e366SSatoru Takeuchi #ifdef CONFIG_EFI 1466b59e366SSatoru Takeuchi 1476b59e366SSatoru Takeuchi static inline bool efi_is_native(void) 1486b59e366SSatoru Takeuchi { 1496b59e366SSatoru Takeuchi return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT); 1506b59e366SSatoru Takeuchi } 1516b59e366SSatoru Takeuchi 1527d453eeeSMatt Fleming static inline bool efi_runtime_supported(void) 1537d453eeeSMatt Fleming { 1547d453eeeSMatt Fleming if (efi_is_native()) 1557d453eeeSMatt Fleming return true; 1567d453eeeSMatt Fleming 1577d453eeeSMatt Fleming if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_enabled(EFI_OLD_MEMMAP)) 1587d453eeeSMatt Fleming return true; 1597d453eeeSMatt Fleming 1607d453eeeSMatt Fleming return false; 1617d453eeeSMatt Fleming } 1627d453eeeSMatt Fleming 16372548e83SMatt Fleming extern struct console early_efi_console; 1645c12af0cSDave Young extern void parse_efi_setup(u64 phys_addr, u32 data_len); 1654f9dbcfcSMatt Fleming 16621289ec0SArd Biesheuvel extern void efifb_setup_from_dmi(struct screen_info *si, const char *opt); 16721289ec0SArd Biesheuvel 1684f9dbcfcSMatt Fleming #ifdef CONFIG_EFI_MIXED 1694f9dbcfcSMatt Fleming extern void efi_thunk_runtime_setup(void); 1704f9dbcfcSMatt Fleming extern efi_status_t efi_thunk_set_virtual_address_map( 1714f9dbcfcSMatt Fleming void *phys_set_virtual_address_map, 1724f9dbcfcSMatt Fleming unsigned long memory_map_size, 1734f9dbcfcSMatt Fleming unsigned long descriptor_size, 1744f9dbcfcSMatt Fleming u32 descriptor_version, 1754f9dbcfcSMatt Fleming efi_memory_desc_t *virtual_map); 1764f9dbcfcSMatt Fleming #else 1774f9dbcfcSMatt Fleming static inline void efi_thunk_runtime_setup(void) {} 1784f9dbcfcSMatt Fleming static inline efi_status_t efi_thunk_set_virtual_address_map( 1794f9dbcfcSMatt Fleming void *phys_set_virtual_address_map, 1804f9dbcfcSMatt Fleming unsigned long memory_map_size, 1814f9dbcfcSMatt Fleming unsigned long descriptor_size, 1824f9dbcfcSMatt Fleming u32 descriptor_version, 1834f9dbcfcSMatt Fleming efi_memory_desc_t *virtual_map) 1844f9dbcfcSMatt Fleming { 1854f9dbcfcSMatt Fleming return EFI_SUCCESS; 1864f9dbcfcSMatt Fleming } 1874f9dbcfcSMatt Fleming #endif /* CONFIG_EFI_MIXED */ 188f23cf8bdSArd Biesheuvel 189243b6754SArd Biesheuvel 190243b6754SArd Biesheuvel /* arch specific definitions used by the stub code */ 191243b6754SArd Biesheuvel 192243b6754SArd Biesheuvel struct efi_config { 193243b6754SArd Biesheuvel u64 image_handle; 194243b6754SArd Biesheuvel u64 table; 195243b6754SArd Biesheuvel u64 allocate_pool; 196243b6754SArd Biesheuvel u64 allocate_pages; 197243b6754SArd Biesheuvel u64 get_memory_map; 198243b6754SArd Biesheuvel u64 free_pool; 199243b6754SArd Biesheuvel u64 free_pages; 200243b6754SArd Biesheuvel u64 locate_handle; 201243b6754SArd Biesheuvel u64 handle_protocol; 202243b6754SArd Biesheuvel u64 exit_boot_services; 203243b6754SArd Biesheuvel u64 text_output; 204243b6754SArd Biesheuvel efi_status_t (*call)(unsigned long, ...); 205243b6754SArd Biesheuvel bool is64; 206243b6754SArd Biesheuvel } __packed; 207243b6754SArd Biesheuvel 208243b6754SArd Biesheuvel __pure const struct efi_config *__efi_early(void); 209243b6754SArd Biesheuvel 210243b6754SArd Biesheuvel #define efi_call_early(f, ...) \ 211243b6754SArd Biesheuvel __efi_early()->call(__efi_early()->f, __VA_ARGS__); 212243b6754SArd Biesheuvel 2132c23b73cSArd Biesheuvel #define __efi_call_early(f, ...) \ 2142c23b73cSArd Biesheuvel __efi_early()->call((unsigned long)f, __VA_ARGS__); 2152c23b73cSArd Biesheuvel 2162c23b73cSArd Biesheuvel #define efi_is_64bit() __efi_early()->is64 2172c23b73cSArd Biesheuvel 21844be28e9SMatt Fleming extern bool efi_reboot_required(void); 21944be28e9SMatt Fleming 2206b59e366SSatoru Takeuchi #else 2215c12af0cSDave Young static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {} 22244be28e9SMatt Fleming static inline bool efi_reboot_required(void) 22344be28e9SMatt Fleming { 22444be28e9SMatt Fleming return false; 22544be28e9SMatt Fleming } 226bb898558SAl Viro #endif /* CONFIG_EFI */ 227bb898558SAl Viro 2281965aae3SH. Peter Anvin #endif /* _ASM_X86_EFI_H */ 229