1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21965aae3SH. Peter Anvin #ifndef _ASM_X86_EFI_H 31965aae3SH. Peter Anvin #define _ASM_X86_EFI_H 4bb898558SAl Viro 5df6b35f4SIngo Molnar #include <asm/fpu/api.h> 6744937b0SIngo Molnar #include <asm/pgtable.h> 79788375dSMark Rutland #include <asm/processor-flags.h> 8c9f2a9a6SMatt Fleming #include <asm/tlb.h> 9dd84441aSDavid Woodhouse #include <asm/nospec-branch.h> 107e904a91SSai Praneeth #include <asm/mmu_context.h> 11*14b864f4SArvind Sankar #include <linux/build_bug.h> 12744937b0SIngo Molnar 13d2f7cbe7SBorislav Petkov /* 14d2f7cbe7SBorislav Petkov * We map the EFI regions needed for runtime services non-contiguously, 15d2f7cbe7SBorislav Petkov * with preserved alignment on virtual addresses starting from -4G down 16d2f7cbe7SBorislav Petkov * for a total max space of 64G. This way, we provide for stable runtime 17d2f7cbe7SBorislav Petkov * services addresses across kernels so that a kexec'd kernel can still 18d2f7cbe7SBorislav Petkov * use them. 19d2f7cbe7SBorislav Petkov * 20d2f7cbe7SBorislav Petkov * This is the main reason why we're doing stable VA mappings for RT 21d2f7cbe7SBorislav Petkov * services. 22d2f7cbe7SBorislav Petkov * 23a97673a1SIngo Molnar * This flag is used in conjunction with a chicken bit called 24d2f7cbe7SBorislav Petkov * "efi=old_map" which can be used as a fallback to the old runtime 25d2f7cbe7SBorislav Petkov * services mapping method in case there's some b0rkage with a 26d2f7cbe7SBorislav Petkov * particular EFI implementation (haha, it is hard to hold up the 27d2f7cbe7SBorislav Petkov * sarcasm here...). 28d2f7cbe7SBorislav Petkov */ 29d2f7cbe7SBorislav Petkov #define EFI_OLD_MEMMAP EFI_ARCH_1 30d2f7cbe7SBorislav Petkov 31b8ff87a6SMatt Fleming #define EFI32_LOADER_SIGNATURE "EL32" 32b8ff87a6SMatt Fleming #define EFI64_LOADER_SIGNATURE "EL64" 33b8ff87a6SMatt Fleming 3448fcb2d0SArd Biesheuvel #define MAX_CMDLINE_ADDRESS UINT_MAX 3548fcb2d0SArd Biesheuvel 369788375dSMark Rutland #define ARCH_EFI_IRQ_FLAGS_MASK X86_EFLAGS_IF 37bb898558SAl Viro 38*14b864f4SArvind Sankar /* 39*14b864f4SArvind Sankar * The EFI services are called through variadic functions in many cases. These 40*14b864f4SArvind Sankar * functions are implemented in assembler and support only a fixed number of 41*14b864f4SArvind Sankar * arguments. The macros below allows us to check at build time that we don't 42*14b864f4SArvind Sankar * try to call them with too many arguments. 43*14b864f4SArvind Sankar * 44*14b864f4SArvind Sankar * __efi_nargs() will return the number of arguments if it is 7 or less, and 45*14b864f4SArvind Sankar * cause a BUILD_BUG otherwise. The limitations of the C preprocessor make it 46*14b864f4SArvind Sankar * impossible to calculate the exact number of arguments beyond some 47*14b864f4SArvind Sankar * pre-defined limit. The maximum number of arguments currently supported by 48*14b864f4SArvind Sankar * any of the thunks is 7, so this is good enough for now and can be extended 49*14b864f4SArvind Sankar * in the obvious way if we ever need more. 50*14b864f4SArvind Sankar */ 51*14b864f4SArvind Sankar 52*14b864f4SArvind Sankar #define __efi_nargs(...) __efi_nargs_(__VA_ARGS__) 53*14b864f4SArvind Sankar #define __efi_nargs_(...) __efi_nargs__(0, ##__VA_ARGS__, \ 54*14b864f4SArvind Sankar __efi_arg_sentinel(7), __efi_arg_sentinel(6), \ 55*14b864f4SArvind Sankar __efi_arg_sentinel(5), __efi_arg_sentinel(4), \ 56*14b864f4SArvind Sankar __efi_arg_sentinel(3), __efi_arg_sentinel(2), \ 57*14b864f4SArvind Sankar __efi_arg_sentinel(1), __efi_arg_sentinel(0)) 58*14b864f4SArvind Sankar #define __efi_nargs__(_0, _1, _2, _3, _4, _5, _6, _7, n, ...) \ 59*14b864f4SArvind Sankar __take_second_arg(n, \ 60*14b864f4SArvind Sankar ({ BUILD_BUG_ON_MSG(1, "__efi_nargs limit exceeded"); 8; })) 61*14b864f4SArvind Sankar #define __efi_arg_sentinel(n) , n 62*14b864f4SArvind Sankar 63*14b864f4SArvind Sankar /* 64*14b864f4SArvind Sankar * __efi_nargs_check(f, n, ...) will cause a BUILD_BUG if the ellipsis 65*14b864f4SArvind Sankar * represents more than n arguments. 66*14b864f4SArvind Sankar */ 67*14b864f4SArvind Sankar 68*14b864f4SArvind Sankar #define __efi_nargs_check(f, n, ...) \ 69*14b864f4SArvind Sankar __efi_nargs_check_(f, __efi_nargs(__VA_ARGS__), n) 70*14b864f4SArvind Sankar #define __efi_nargs_check_(f, p, n) __efi_nargs_check__(f, p, n) 71*14b864f4SArvind Sankar #define __efi_nargs_check__(f, p, n) ({ \ 72*14b864f4SArvind Sankar BUILD_BUG_ON_MSG( \ 73*14b864f4SArvind Sankar (p) > (n), \ 74*14b864f4SArvind Sankar #f " called with too many arguments (" #p ">" #n ")"); \ 75*14b864f4SArvind Sankar }) 76*14b864f4SArvind Sankar 779788375dSMark Rutland #ifdef CONFIG_X86_32 78dd84441aSDavid Woodhouse #define arch_efi_call_virt_setup() \ 79dd84441aSDavid Woodhouse ({ \ 80dd84441aSDavid Woodhouse kernel_fpu_begin(); \ 81dd84441aSDavid Woodhouse firmware_restrict_branch_speculation_start(); \ 82dd84441aSDavid Woodhouse }) 83dd84441aSDavid Woodhouse 84dd84441aSDavid Woodhouse #define arch_efi_call_virt_teardown() \ 85dd84441aSDavid Woodhouse ({ \ 86dd84441aSDavid Woodhouse firmware_restrict_branch_speculation_end(); \ 87dd84441aSDavid Woodhouse kernel_fpu_end(); \ 88dd84441aSDavid Woodhouse }) 89dd84441aSDavid Woodhouse 90bc25f9dbSMark Rutland 9189ed4865SArd Biesheuvel #define arch_efi_call_virt(p, f, args...) p->f(args) 92982e239cSRicardo Neri 933e8fa263SMatt Fleming #define efi_ioremap(addr, size, type, attr) ioremap_cache(addr, size) 94e1ad783bSKeith Packard 95bb898558SAl Viro #else /* !CONFIG_X86_32 */ 96bb898558SAl Viro 9762fa6e69SMatt Fleming #define EFI_LOADER_SIGNATURE "EL64" 98bb898558SAl Viro 99*14b864f4SArvind Sankar extern asmlinkage u64 __efi_call(void *fp, ...); 100*14b864f4SArvind Sankar 101*14b864f4SArvind Sankar #define efi_call(...) ({ \ 102*14b864f4SArvind Sankar __efi_nargs_check(efi_call, 7, __VA_ARGS__); \ 103*14b864f4SArvind Sankar __efi_call(__VA_ARGS__); \ 104*14b864f4SArvind Sankar }) 105bb898558SAl Viro 106c9f2a9a6SMatt Fleming /* 10703781e40SSai Praneeth * struct efi_scratch - Scratch space used while switching to/from efi_mm 10803781e40SSai Praneeth * @phys_stack: stack used during EFI Mixed Mode 10903781e40SSai Praneeth * @prev_mm: store/restore stolen mm_struct while switching to/from efi_mm 110c9f2a9a6SMatt Fleming */ 111c9f2a9a6SMatt Fleming struct efi_scratch { 112c9f2a9a6SMatt Fleming u64 phys_stack; 11303781e40SSai Praneeth struct mm_struct *prev_mm; 114c9f2a9a6SMatt Fleming } __packed; 115c9f2a9a6SMatt Fleming 116bc25f9dbSMark Rutland #define arch_efi_call_virt_setup() \ 117d2f7cbe7SBorislav Petkov ({ \ 118d2f7cbe7SBorislav Petkov efi_sync_low_kernel_mappings(); \ 11912209993SSebastian Andrzej Siewior kernel_fpu_begin(); \ 120dd84441aSDavid Woodhouse firmware_restrict_branch_speculation_start(); \ 121c9f2a9a6SMatt Fleming \ 12203781e40SSai Praneeth if (!efi_enabled(EFI_OLD_MEMMAP)) \ 12303781e40SSai Praneeth efi_switch_mm(&efi_mm); \ 124bc25f9dbSMark Rutland }) 125bc25f9dbSMark Rutland 12680e75596SAlex Thorlton #define arch_efi_call_virt(p, f, args...) \ 12780e75596SAlex Thorlton efi_call((void *)p->f, args) \ 128bc25f9dbSMark Rutland 129bc25f9dbSMark Rutland #define arch_efi_call_virt_teardown() \ 130bc25f9dbSMark Rutland ({ \ 13103781e40SSai Praneeth if (!efi_enabled(EFI_OLD_MEMMAP)) \ 13203781e40SSai Praneeth efi_switch_mm(efi_scratch.prev_mm); \ 133c9f2a9a6SMatt Fleming \ 134dd84441aSDavid Woodhouse firmware_restrict_branch_speculation_end(); \ 13512209993SSebastian Andrzej Siewior kernel_fpu_end(); \ 136d2f7cbe7SBorislav Petkov }) 137d2f7cbe7SBorislav Petkov 1384e78eb05SMathias Krause extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size, 1393e8fa263SMatt Fleming u32 type, u64 attribute); 140e1ad783bSKeith Packard 141a523841eSAndrey Ryabinin #ifdef CONFIG_KASAN 142769a8089SAndrey Ryabinin /* 143769a8089SAndrey Ryabinin * CONFIG_KASAN may redefine memset to __memset. __memset function is present 144769a8089SAndrey Ryabinin * only in kernel binary. Since the EFI stub linked into a separate binary it 145769a8089SAndrey Ryabinin * doesn't have __memset(). So we should use standard memset from 146769a8089SAndrey Ryabinin * arch/x86/boot/compressed/string.c. The same applies to memcpy and memmove. 147769a8089SAndrey Ryabinin */ 148769a8089SAndrey Ryabinin #undef memcpy 149769a8089SAndrey Ryabinin #undef memset 150769a8089SAndrey Ryabinin #undef memmove 151a523841eSAndrey Ryabinin #endif 152769a8089SAndrey Ryabinin 153bb898558SAl Viro #endif /* CONFIG_X86_32 */ 154bb898558SAl Viro 155d2f7cbe7SBorislav Petkov extern struct efi_scratch efi_scratch; 1564e78eb05SMathias Krause extern void __init efi_set_executable(efi_memory_desc_t *md, bool executable); 1574e78eb05SMathias Krause extern int __init efi_memblock_x86_reserve_range(void); 1580bbea1ceSTaku Izumi extern void __init efi_print_memmap(void); 1594e78eb05SMathias Krause extern void __init efi_memory_uc(u64 addr, unsigned long size); 160d2f7cbe7SBorislav Petkov extern void __init efi_map_region(efi_memory_desc_t *md); 1613b266496SDave Young extern void __init efi_map_region_fixed(efi_memory_desc_t *md); 162d2f7cbe7SBorislav Petkov extern void efi_sync_low_kernel_mappings(void); 16367a9108eSMatt Fleming extern int __init efi_alloc_page_tables(void); 1644e78eb05SMathias Krause extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages); 165d2f7cbe7SBorislav Petkov extern void __init old_map_region(efi_memory_desc_t *md); 166c55d016fSBorislav Petkov extern void __init runtime_code_page_mkexec(void); 1676d0cc887SSai Praneeth extern void __init efi_runtime_update_mappings(void); 16811cc8512SBorislav Petkov extern void __init efi_dump_pagetable(void); 169a5d90c92SBorislav Petkov extern void __init efi_apply_memmap_quirks(void); 170eeb9db09SSaurabh Tangri extern int __init efi_reuse_config(u64 tables, int nr_tables); 171eeb9db09SSaurabh Tangri extern void efi_delete_dummy_variable(void); 17203781e40SSai Praneeth extern void efi_switch_mm(struct mm_struct *mm); 1733425d934SSai Praneeth extern void efi_recover_from_page_fault(unsigned long phys_addr); 17447c33a09SSai Praneeth Prakhya extern void efi_free_boot_services(void); 175bb898558SAl Viro 1761fec0533SDave Young struct efi_setup_data { 1771fec0533SDave Young u64 fw_vendor; 1781fec0533SDave Young u64 runtime; 1791fec0533SDave Young u64 tables; 1801fec0533SDave Young u64 smbios; 1811fec0533SDave Young u64 reserved[8]; 1821fec0533SDave Young }; 1831fec0533SDave Young 1841fec0533SDave Young extern u64 efi_setup; 1851fec0533SDave Young 1866b59e366SSatoru Takeuchi #ifdef CONFIG_EFI 187*14b864f4SArvind Sankar extern efi_status_t __efi64_thunk(u32, ...); 188*14b864f4SArvind Sankar 189*14b864f4SArvind Sankar #define efi64_thunk(...) ({ \ 190*14b864f4SArvind Sankar __efi_nargs_check(efi64_thunk, 6, __VA_ARGS__); \ 191*14b864f4SArvind Sankar __efi64_thunk(__VA_ARGS__); \ 192*14b864f4SArvind Sankar }) 1936b59e366SSatoru Takeuchi 194a8147dbaSArd Biesheuvel static inline bool efi_is_mixed(void) 1956b59e366SSatoru Takeuchi { 196a8147dbaSArd Biesheuvel if (!IS_ENABLED(CONFIG_EFI_MIXED)) 197a8147dbaSArd Biesheuvel return false; 198a8147dbaSArd Biesheuvel return IS_ENABLED(CONFIG_X86_64) && !efi_enabled(EFI_64BIT); 1996b59e366SSatoru Takeuchi } 2006b59e366SSatoru Takeuchi 2017d453eeeSMatt Fleming static inline bool efi_runtime_supported(void) 2027d453eeeSMatt Fleming { 2036cfcd6f0SArd Biesheuvel if (IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT)) 2047d453eeeSMatt Fleming return true; 2057d453eeeSMatt Fleming 2066cfcd6f0SArd Biesheuvel if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_enabled(EFI_OLD_MEMMAP)) 2077d453eeeSMatt Fleming return true; 2087d453eeeSMatt Fleming 2097d453eeeSMatt Fleming return false; 2107d453eeeSMatt Fleming } 2117d453eeeSMatt Fleming 2125c12af0cSDave Young extern void parse_efi_setup(u64 phys_addr, u32 data_len); 2134f9dbcfcSMatt Fleming 21421289ec0SArd Biesheuvel extern void efifb_setup_from_dmi(struct screen_info *si, const char *opt); 21521289ec0SArd Biesheuvel 2164f9dbcfcSMatt Fleming extern void efi_thunk_runtime_setup(void); 21769829470SArd Biesheuvel efi_status_t efi_set_virtual_address_map(unsigned long memory_map_size, 21869829470SArd Biesheuvel unsigned long descriptor_size, 21969829470SArd Biesheuvel u32 descriptor_version, 22069829470SArd Biesheuvel efi_memory_desc_t *virtual_map); 221243b6754SArd Biesheuvel 222243b6754SArd Biesheuvel /* arch specific definitions used by the stub code */ 223243b6754SArd Biesheuvel 224c3710de5SArd Biesheuvel __pure bool efi_is_64bit(void); 22527571616SLukas Wunner 226f958efe9SArd Biesheuvel static inline bool efi_is_native(void) 227f958efe9SArd Biesheuvel { 228f958efe9SArd Biesheuvel if (!IS_ENABLED(CONFIG_X86_64)) 229f958efe9SArd Biesheuvel return true; 230c3710de5SArd Biesheuvel if (!IS_ENABLED(CONFIG_EFI_MIXED)) 231c3710de5SArd Biesheuvel return true; 232f958efe9SArd Biesheuvel return efi_is_64bit(); 233f958efe9SArd Biesheuvel } 234f958efe9SArd Biesheuvel 235f958efe9SArd Biesheuvel #define efi_mixed_mode_cast(attr) \ 236f958efe9SArd Biesheuvel __builtin_choose_expr( \ 237f958efe9SArd Biesheuvel __builtin_types_compatible_p(u32, __typeof__(attr)), \ 238f958efe9SArd Biesheuvel (unsigned long)(attr), (attr)) 239f958efe9SArd Biesheuvel 24099ea8b1dSArd Biesheuvel #define efi_table_attr(inst, attr) \ 24199ea8b1dSArd Biesheuvel (efi_is_native() \ 24299ea8b1dSArd Biesheuvel ? inst->attr \ 24399ea8b1dSArd Biesheuvel : (__typeof__(inst->attr)) \ 24499ea8b1dSArd Biesheuvel efi_mixed_mode_cast(inst->mixed_mode.attr)) 2453552fdf2SLukas Wunner 24647c0fd39SArd Biesheuvel #define efi_call_proto(inst, func, ...) \ 247afc4cc71SArd Biesheuvel (efi_is_native() \ 24847c0fd39SArd Biesheuvel ? inst->func(inst, ##__VA_ARGS__) \ 24947c0fd39SArd Biesheuvel : efi64_thunk(inst->mixed_mode.func, inst, ##__VA_ARGS__)) 2503552fdf2SLukas Wunner 251966291f6SArd Biesheuvel #define efi_bs_call(func, ...) \ 252afc4cc71SArd Biesheuvel (efi_is_native() \ 253966291f6SArd Biesheuvel ? efi_system_table()->boottime->func(__VA_ARGS__) \ 25499ea8b1dSArd Biesheuvel : efi64_thunk(efi_table_attr(efi_system_table(), \ 255966291f6SArd Biesheuvel boottime)->mixed_mode.func, __VA_ARGS__)) 256243b6754SArd Biesheuvel 257966291f6SArd Biesheuvel #define efi_rt_call(func, ...) \ 258afc4cc71SArd Biesheuvel (efi_is_native() \ 259966291f6SArd Biesheuvel ? efi_system_table()->runtime->func(__VA_ARGS__) \ 26099ea8b1dSArd Biesheuvel : efi64_thunk(efi_table_attr(efi_system_table(), \ 261966291f6SArd Biesheuvel runtime)->mixed_mode.func, __VA_ARGS__)) 262a2cd2f3fSDavid Howells 26344be28e9SMatt Fleming extern bool efi_reboot_required(void); 264e55f31a5SArd Biesheuvel extern bool efi_is_table_address(unsigned long phys_addr); 26544be28e9SMatt Fleming 2666950e31bSDan Williams extern void efi_find_mirror(void); 2676950e31bSDan Williams extern void efi_reserve_boot_services(void); 2686b59e366SSatoru Takeuchi #else 2695c12af0cSDave Young static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {} 27044be28e9SMatt Fleming static inline bool efi_reboot_required(void) 27144be28e9SMatt Fleming { 27244be28e9SMatt Fleming return false; 27344be28e9SMatt Fleming } 274e55f31a5SArd Biesheuvel static inline bool efi_is_table_address(unsigned long phys_addr) 275e55f31a5SArd Biesheuvel { 276e55f31a5SArd Biesheuvel return false; 277e55f31a5SArd Biesheuvel } 2786950e31bSDan Williams static inline void efi_find_mirror(void) 2796950e31bSDan Williams { 2806950e31bSDan Williams } 2816950e31bSDan Williams static inline void efi_reserve_boot_services(void) 2826950e31bSDan Williams { 2836950e31bSDan Williams } 284bb898558SAl Viro #endif /* CONFIG_EFI */ 285bb898558SAl Viro 286199c8471SDan Williams #ifdef CONFIG_EFI_FAKE_MEMMAP 287199c8471SDan Williams extern void __init efi_fake_memmap_early(void); 288199c8471SDan Williams #else 289199c8471SDan Williams static inline void efi_fake_memmap_early(void) 290199c8471SDan Williams { 291199c8471SDan Williams } 292199c8471SDan Williams #endif 293199c8471SDan Williams 2941965aae3SH. Peter Anvin #endif /* _ASM_X86_EFI_H */ 295