xref: /openbmc/linux/arch/x86/include/asm/efi.h (revision 6982947045734480b8b57521e8068073fe36bd14)
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>
11744937b0SIngo Molnar 
12d2f7cbe7SBorislav Petkov /*
13d2f7cbe7SBorislav Petkov  * We map the EFI regions needed for runtime services non-contiguously,
14d2f7cbe7SBorislav Petkov  * with preserved alignment on virtual addresses starting from -4G down
15d2f7cbe7SBorislav Petkov  * for a total max space of 64G. This way, we provide for stable runtime
16d2f7cbe7SBorislav Petkov  * services addresses across kernels so that a kexec'd kernel can still
17d2f7cbe7SBorislav Petkov  * use them.
18d2f7cbe7SBorislav Petkov  *
19d2f7cbe7SBorislav Petkov  * This is the main reason why we're doing stable VA mappings for RT
20d2f7cbe7SBorislav Petkov  * services.
21d2f7cbe7SBorislav Petkov  *
22a97673a1SIngo Molnar  * This flag is used in conjunction with a chicken bit called
23d2f7cbe7SBorislav Petkov  * "efi=old_map" which can be used as a fallback to the old runtime
24d2f7cbe7SBorislav Petkov  * services mapping method in case there's some b0rkage with a
25d2f7cbe7SBorislav Petkov  * particular EFI implementation (haha, it is hard to hold up the
26d2f7cbe7SBorislav Petkov  * sarcasm here...).
27d2f7cbe7SBorislav Petkov  */
28d2f7cbe7SBorislav Petkov #define EFI_OLD_MEMMAP		EFI_ARCH_1
29d2f7cbe7SBorislav Petkov 
30b8ff87a6SMatt Fleming #define EFI32_LOADER_SIGNATURE	"EL32"
31b8ff87a6SMatt Fleming #define EFI64_LOADER_SIGNATURE	"EL64"
32b8ff87a6SMatt Fleming 
3348fcb2d0SArd Biesheuvel #define MAX_CMDLINE_ADDRESS	UINT_MAX
3448fcb2d0SArd Biesheuvel 
359788375dSMark Rutland #define ARCH_EFI_IRQ_FLAGS_MASK	X86_EFLAGS_IF
36bb898558SAl Viro 
379788375dSMark Rutland #ifdef CONFIG_X86_32
38f7d7d01bSMatt Fleming 
390825f49fSJoe Perches extern asmlinkage unsigned long efi_call_phys(void *, ...);
40bb898558SAl Viro 
41dd84441aSDavid Woodhouse #define arch_efi_call_virt_setup()					\
42dd84441aSDavid Woodhouse ({									\
43dd84441aSDavid Woodhouse 	kernel_fpu_begin();						\
44dd84441aSDavid Woodhouse 	firmware_restrict_branch_speculation_start();			\
45dd84441aSDavid Woodhouse })
46dd84441aSDavid Woodhouse 
47dd84441aSDavid Woodhouse #define arch_efi_call_virt_teardown()					\
48dd84441aSDavid Woodhouse ({									\
49dd84441aSDavid Woodhouse 	firmware_restrict_branch_speculation_end();			\
50dd84441aSDavid Woodhouse 	kernel_fpu_end();						\
51dd84441aSDavid Woodhouse })
52dd84441aSDavid Woodhouse 
53bc25f9dbSMark Rutland 
5489ed4865SArd Biesheuvel #define arch_efi_call_virt(p, f, args...)	p->f(args)
55982e239cSRicardo Neri 
563e8fa263SMatt Fleming #define efi_ioremap(addr, size, type, attr)	ioremap_cache(addr, size)
57e1ad783bSKeith Packard 
58bb898558SAl Viro #else /* !CONFIG_X86_32 */
59bb898558SAl Viro 
6062fa6e69SMatt Fleming #define EFI_LOADER_SIGNATURE	"EL64"
61bb898558SAl Viro 
620825f49fSJoe Perches extern asmlinkage u64 efi_call(void *fp, ...);
63bb898558SAl Viro 
64c9f2a9a6SMatt Fleming /*
6503781e40SSai Praneeth  * struct efi_scratch - Scratch space used while switching to/from efi_mm
6603781e40SSai Praneeth  * @phys_stack: stack used during EFI Mixed Mode
6703781e40SSai Praneeth  * @prev_mm:    store/restore stolen mm_struct while switching to/from efi_mm
68c9f2a9a6SMatt Fleming  */
69c9f2a9a6SMatt Fleming struct efi_scratch {
70c9f2a9a6SMatt Fleming 	u64			phys_stack;
7103781e40SSai Praneeth 	struct mm_struct	*prev_mm;
72c9f2a9a6SMatt Fleming } __packed;
73c9f2a9a6SMatt Fleming 
74bc25f9dbSMark Rutland #define arch_efi_call_virt_setup()					\
75d2f7cbe7SBorislav Petkov ({									\
76d2f7cbe7SBorislav Petkov 	efi_sync_low_kernel_mappings();					\
7712209993SSebastian Andrzej Siewior 	kernel_fpu_begin();						\
78dd84441aSDavid Woodhouse 	firmware_restrict_branch_speculation_start();			\
79c9f2a9a6SMatt Fleming 									\
8003781e40SSai Praneeth 	if (!efi_enabled(EFI_OLD_MEMMAP))				\
8103781e40SSai Praneeth 		efi_switch_mm(&efi_mm);					\
82bc25f9dbSMark Rutland })
83bc25f9dbSMark Rutland 
8480e75596SAlex Thorlton #define arch_efi_call_virt(p, f, args...)				\
8580e75596SAlex Thorlton 	efi_call((void *)p->f, args)					\
86bc25f9dbSMark Rutland 
87bc25f9dbSMark Rutland #define arch_efi_call_virt_teardown()					\
88bc25f9dbSMark Rutland ({									\
8903781e40SSai Praneeth 	if (!efi_enabled(EFI_OLD_MEMMAP))				\
9003781e40SSai Praneeth 		efi_switch_mm(efi_scratch.prev_mm);			\
91c9f2a9a6SMatt Fleming 									\
92dd84441aSDavid Woodhouse 	firmware_restrict_branch_speculation_end();			\
9312209993SSebastian Andrzej Siewior 	kernel_fpu_end();						\
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);
1160bbea1ceSTaku Izumi extern void __init efi_print_memmap(void);
1174e78eb05SMathias Krause extern void __init efi_memory_uc(u64 addr, unsigned long size);
118d2f7cbe7SBorislav Petkov extern void __init efi_map_region(efi_memory_desc_t *md);
1193b266496SDave Young extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
120d2f7cbe7SBorislav Petkov extern void efi_sync_low_kernel_mappings(void);
12167a9108eSMatt Fleming extern int __init efi_alloc_page_tables(void);
1224e78eb05SMathias Krause extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
123d2f7cbe7SBorislav Petkov extern void __init old_map_region(efi_memory_desc_t *md);
124c55d016fSBorislav Petkov extern void __init runtime_code_page_mkexec(void);
1256d0cc887SSai Praneeth extern void __init efi_runtime_update_mappings(void);
12611cc8512SBorislav Petkov extern void __init efi_dump_pagetable(void);
127a5d90c92SBorislav Petkov extern void __init efi_apply_memmap_quirks(void);
128eeb9db09SSaurabh Tangri extern int __init efi_reuse_config(u64 tables, int nr_tables);
129eeb9db09SSaurabh Tangri extern void efi_delete_dummy_variable(void);
13003781e40SSai Praneeth extern void efi_switch_mm(struct mm_struct *mm);
1313425d934SSai Praneeth extern void efi_recover_from_page_fault(unsigned long phys_addr);
13247c33a09SSai Praneeth Prakhya extern void efi_free_boot_services(void);
133bb898558SAl Viro 
1341fec0533SDave Young struct efi_setup_data {
1351fec0533SDave Young 	u64 fw_vendor;
1361fec0533SDave Young 	u64 runtime;
1371fec0533SDave Young 	u64 tables;
1381fec0533SDave Young 	u64 smbios;
1391fec0533SDave Young 	u64 reserved[8];
1401fec0533SDave Young };
1411fec0533SDave Young 
1421fec0533SDave Young extern u64 efi_setup;
1431fec0533SDave Young 
1446b59e366SSatoru Takeuchi #ifdef CONFIG_EFI
145afc4cc71SArd Biesheuvel extern efi_status_t efi64_thunk(u32, ...);
1466b59e366SSatoru Takeuchi 
147a8147dbaSArd Biesheuvel static inline bool efi_is_mixed(void)
1486b59e366SSatoru Takeuchi {
149a8147dbaSArd Biesheuvel 	if (!IS_ENABLED(CONFIG_EFI_MIXED))
150a8147dbaSArd Biesheuvel 		return false;
151a8147dbaSArd Biesheuvel 	return IS_ENABLED(CONFIG_X86_64) && !efi_enabled(EFI_64BIT);
1526b59e366SSatoru Takeuchi }
1536b59e366SSatoru Takeuchi 
1547d453eeeSMatt Fleming static inline bool efi_runtime_supported(void)
1557d453eeeSMatt Fleming {
1566cfcd6f0SArd Biesheuvel 	if (IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT))
1577d453eeeSMatt Fleming 		return true;
1587d453eeeSMatt Fleming 
1596cfcd6f0SArd Biesheuvel 	if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_enabled(EFI_OLD_MEMMAP))
1607d453eeeSMatt Fleming 		return true;
1617d453eeeSMatt Fleming 
1627d453eeeSMatt Fleming 	return false;
1637d453eeeSMatt Fleming }
1647d453eeeSMatt Fleming 
1655c12af0cSDave Young extern void parse_efi_setup(u64 phys_addr, u32 data_len);
1664f9dbcfcSMatt Fleming 
16721289ec0SArd Biesheuvel extern void efifb_setup_from_dmi(struct screen_info *si, const char *opt);
16821289ec0SArd Biesheuvel 
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);
176*69829470SArd Biesheuvel efi_status_t efi_set_virtual_address_map(unsigned long memory_map_size,
177*69829470SArd Biesheuvel 					 unsigned long descriptor_size,
178*69829470SArd Biesheuvel 					 u32 descriptor_version,
179*69829470SArd Biesheuvel 					 efi_memory_desc_t *virtual_map);
180243b6754SArd Biesheuvel 
181243b6754SArd Biesheuvel /* arch specific definitions used by the stub code */
182243b6754SArd Biesheuvel 
183c3710de5SArd Biesheuvel __pure bool efi_is_64bit(void);
18427571616SLukas Wunner 
185f958efe9SArd Biesheuvel static inline bool efi_is_native(void)
186f958efe9SArd Biesheuvel {
187f958efe9SArd Biesheuvel 	if (!IS_ENABLED(CONFIG_X86_64))
188f958efe9SArd Biesheuvel 		return true;
189c3710de5SArd Biesheuvel 	if (!IS_ENABLED(CONFIG_EFI_MIXED))
190c3710de5SArd Biesheuvel 		return true;
191f958efe9SArd Biesheuvel 	return efi_is_64bit();
192f958efe9SArd Biesheuvel }
193f958efe9SArd Biesheuvel 
194f958efe9SArd Biesheuvel #define efi_mixed_mode_cast(attr)					\
195f958efe9SArd Biesheuvel 	__builtin_choose_expr(						\
196f958efe9SArd Biesheuvel 		__builtin_types_compatible_p(u32, __typeof__(attr)),	\
197f958efe9SArd Biesheuvel 			(unsigned long)(attr), (attr))
198f958efe9SArd Biesheuvel 
19999ea8b1dSArd Biesheuvel #define efi_table_attr(inst, attr)					\
20099ea8b1dSArd Biesheuvel 	(efi_is_native()						\
20199ea8b1dSArd Biesheuvel 		? inst->attr						\
20299ea8b1dSArd Biesheuvel 		: (__typeof__(inst->attr))				\
20399ea8b1dSArd Biesheuvel 			efi_mixed_mode_cast(inst->mixed_mode.attr))
2043552fdf2SLukas Wunner 
20547c0fd39SArd Biesheuvel #define efi_call_proto(inst, func, ...)					\
206afc4cc71SArd Biesheuvel 	(efi_is_native()						\
20747c0fd39SArd Biesheuvel 		? inst->func(inst, ##__VA_ARGS__)			\
20847c0fd39SArd Biesheuvel 		: efi64_thunk(inst->mixed_mode.func, inst, ##__VA_ARGS__))
2093552fdf2SLukas Wunner 
210966291f6SArd Biesheuvel #define efi_bs_call(func, ...)						\
211afc4cc71SArd Biesheuvel 	(efi_is_native()						\
212966291f6SArd Biesheuvel 		? efi_system_table()->boottime->func(__VA_ARGS__)	\
21399ea8b1dSArd Biesheuvel 		: efi64_thunk(efi_table_attr(efi_system_table(),	\
214966291f6SArd Biesheuvel 				boottime)->mixed_mode.func, __VA_ARGS__))
215243b6754SArd Biesheuvel 
216966291f6SArd Biesheuvel #define efi_rt_call(func, ...)						\
217afc4cc71SArd Biesheuvel 	(efi_is_native()						\
218966291f6SArd Biesheuvel 		? efi_system_table()->runtime->func(__VA_ARGS__)	\
21999ea8b1dSArd Biesheuvel 		: efi64_thunk(efi_table_attr(efi_system_table(),	\
220966291f6SArd Biesheuvel 				runtime)->mixed_mode.func, __VA_ARGS__))
221a2cd2f3fSDavid Howells 
22244be28e9SMatt Fleming extern bool efi_reboot_required(void);
223e55f31a5SArd Biesheuvel extern bool efi_is_table_address(unsigned long phys_addr);
22444be28e9SMatt Fleming 
2256950e31bSDan Williams extern void efi_find_mirror(void);
2266950e31bSDan Williams extern void efi_reserve_boot_services(void);
2276b59e366SSatoru Takeuchi #else
2285c12af0cSDave Young static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {}
22944be28e9SMatt Fleming static inline bool efi_reboot_required(void)
23044be28e9SMatt Fleming {
23144be28e9SMatt Fleming 	return false;
23244be28e9SMatt Fleming }
233e55f31a5SArd Biesheuvel static inline  bool efi_is_table_address(unsigned long phys_addr)
234e55f31a5SArd Biesheuvel {
235e55f31a5SArd Biesheuvel 	return false;
236e55f31a5SArd Biesheuvel }
2376950e31bSDan Williams static inline void efi_find_mirror(void)
2386950e31bSDan Williams {
2396950e31bSDan Williams }
2406950e31bSDan Williams static inline void efi_reserve_boot_services(void)
2416950e31bSDan Williams {
2426950e31bSDan Williams }
243bb898558SAl Viro #endif /* CONFIG_EFI */
244bb898558SAl Viro 
245199c8471SDan Williams #ifdef CONFIG_EFI_FAKE_MEMMAP
246199c8471SDan Williams extern void __init efi_fake_memmap_early(void);
247199c8471SDan Williams #else
248199c8471SDan Williams static inline void efi_fake_memmap_early(void)
249199c8471SDan Williams {
250199c8471SDan Williams }
251199c8471SDan Williams #endif
252199c8471SDan Williams 
2531965aae3SH. Peter Anvin #endif /* _ASM_X86_EFI_H */
254