xref: /openbmc/linux/arch/x86/include/asm/efi.h (revision 199c8471761273b7e287914cee968ddf21dfbfe0)
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 
54bb898558SAl Viro /*
55bb898558SAl Viro  * Wrap all the virtual calls in a way that forces the parameters on the stack.
56bb898558SAl Viro  */
5780e75596SAlex Thorlton #define arch_efi_call_virt(p, f, args...)				\
58b738c6eaSRicardo Neri ({									\
5980e75596SAlex Thorlton 	((efi_##f##_t __attribute__((regparm(0)))*) p->f)(args);	\
60b738c6eaSRicardo Neri })
61982e239cSRicardo Neri 
623e8fa263SMatt Fleming #define efi_ioremap(addr, size, type, attr)	ioremap_cache(addr, size)
63e1ad783bSKeith Packard 
64bb898558SAl Viro #else /* !CONFIG_X86_32 */
65bb898558SAl Viro 
6662fa6e69SMatt Fleming #define EFI_LOADER_SIGNATURE	"EL64"
67bb898558SAl Viro 
680825f49fSJoe Perches extern asmlinkage u64 efi_call(void *fp, ...);
69bb898558SAl Viro 
7062fa6e69SMatt Fleming #define efi_call_phys(f, args...)		efi_call((f), args)
7162fa6e69SMatt Fleming 
72c9f2a9a6SMatt Fleming /*
7303781e40SSai Praneeth  * struct efi_scratch - Scratch space used while switching to/from efi_mm
7403781e40SSai Praneeth  * @phys_stack: stack used during EFI Mixed Mode
7503781e40SSai Praneeth  * @prev_mm:    store/restore stolen mm_struct while switching to/from efi_mm
76c9f2a9a6SMatt Fleming  */
77c9f2a9a6SMatt Fleming struct efi_scratch {
78c9f2a9a6SMatt Fleming 	u64			phys_stack;
7903781e40SSai Praneeth 	struct mm_struct	*prev_mm;
80c9f2a9a6SMatt Fleming } __packed;
81c9f2a9a6SMatt Fleming 
82bc25f9dbSMark Rutland #define arch_efi_call_virt_setup()					\
83d2f7cbe7SBorislav Petkov ({									\
84d2f7cbe7SBorislav Petkov 	efi_sync_low_kernel_mappings();					\
8512209993SSebastian Andrzej Siewior 	kernel_fpu_begin();						\
86dd84441aSDavid Woodhouse 	firmware_restrict_branch_speculation_start();			\
87c9f2a9a6SMatt Fleming 									\
8803781e40SSai Praneeth 	if (!efi_enabled(EFI_OLD_MEMMAP))				\
8903781e40SSai Praneeth 		efi_switch_mm(&efi_mm);					\
90bc25f9dbSMark Rutland })
91bc25f9dbSMark Rutland 
9280e75596SAlex Thorlton #define arch_efi_call_virt(p, f, args...)				\
9380e75596SAlex Thorlton 	efi_call((void *)p->f, args)					\
94bc25f9dbSMark Rutland 
95bc25f9dbSMark Rutland #define arch_efi_call_virt_teardown()					\
96bc25f9dbSMark Rutland ({									\
9703781e40SSai Praneeth 	if (!efi_enabled(EFI_OLD_MEMMAP))				\
9803781e40SSai Praneeth 		efi_switch_mm(efi_scratch.prev_mm);			\
99c9f2a9a6SMatt Fleming 									\
100dd84441aSDavid Woodhouse 	firmware_restrict_branch_speculation_end();			\
10112209993SSebastian Andrzej Siewior 	kernel_fpu_end();						\
102d2f7cbe7SBorislav Petkov })
103d2f7cbe7SBorislav Petkov 
1044e78eb05SMathias Krause extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size,
1053e8fa263SMatt Fleming 					u32 type, u64 attribute);
106e1ad783bSKeith Packard 
107a523841eSAndrey Ryabinin #ifdef CONFIG_KASAN
108769a8089SAndrey Ryabinin /*
109769a8089SAndrey Ryabinin  * CONFIG_KASAN may redefine memset to __memset.  __memset function is present
110769a8089SAndrey Ryabinin  * only in kernel binary.  Since the EFI stub linked into a separate binary it
111769a8089SAndrey Ryabinin  * doesn't have __memset().  So we should use standard memset from
112769a8089SAndrey Ryabinin  * arch/x86/boot/compressed/string.c.  The same applies to memcpy and memmove.
113769a8089SAndrey Ryabinin  */
114769a8089SAndrey Ryabinin #undef memcpy
115769a8089SAndrey Ryabinin #undef memset
116769a8089SAndrey Ryabinin #undef memmove
117a523841eSAndrey Ryabinin #endif
118769a8089SAndrey Ryabinin 
119bb898558SAl Viro #endif /* CONFIG_X86_32 */
120bb898558SAl Viro 
121d2f7cbe7SBorislav Petkov extern struct efi_scratch efi_scratch;
1224e78eb05SMathias Krause extern void __init efi_set_executable(efi_memory_desc_t *md, bool executable);
1234e78eb05SMathias Krause extern int __init efi_memblock_x86_reserve_range(void);
124744937b0SIngo Molnar extern pgd_t * __init efi_call_phys_prolog(void);
125744937b0SIngo Molnar extern void __init efi_call_phys_epilog(pgd_t *save_pgd);
1260bbea1ceSTaku Izumi extern void __init efi_print_memmap(void);
1274e78eb05SMathias Krause extern void __init efi_memory_uc(u64 addr, unsigned long size);
128d2f7cbe7SBorislav Petkov extern void __init efi_map_region(efi_memory_desc_t *md);
1293b266496SDave Young extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
130d2f7cbe7SBorislav Petkov extern void efi_sync_low_kernel_mappings(void);
13167a9108eSMatt Fleming extern int __init efi_alloc_page_tables(void);
1324e78eb05SMathias Krause extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
133d2f7cbe7SBorislav Petkov extern void __init old_map_region(efi_memory_desc_t *md);
134c55d016fSBorislav Petkov extern void __init runtime_code_page_mkexec(void);
1356d0cc887SSai Praneeth extern void __init efi_runtime_update_mappings(void);
13611cc8512SBorislav Petkov extern void __init efi_dump_pagetable(void);
137a5d90c92SBorislav Petkov extern void __init efi_apply_memmap_quirks(void);
138eeb9db09SSaurabh Tangri extern int __init efi_reuse_config(u64 tables, int nr_tables);
139eeb9db09SSaurabh Tangri extern void efi_delete_dummy_variable(void);
14003781e40SSai Praneeth extern void efi_switch_mm(struct mm_struct *mm);
1413425d934SSai Praneeth extern void efi_recover_from_page_fault(unsigned long phys_addr);
14247c33a09SSai Praneeth Prakhya extern void efi_free_boot_services(void);
143bb898558SAl Viro 
1441fec0533SDave Young struct efi_setup_data {
1451fec0533SDave Young 	u64 fw_vendor;
1461fec0533SDave Young 	u64 runtime;
1471fec0533SDave Young 	u64 tables;
1481fec0533SDave Young 	u64 smbios;
1491fec0533SDave Young 	u64 reserved[8];
1501fec0533SDave Young };
1511fec0533SDave Young 
1521fec0533SDave Young extern u64 efi_setup;
1531fec0533SDave Young 
1546b59e366SSatoru Takeuchi #ifdef CONFIG_EFI
1556b59e366SSatoru Takeuchi 
1566b59e366SSatoru Takeuchi static inline bool efi_is_native(void)
1576b59e366SSatoru Takeuchi {
1586b59e366SSatoru Takeuchi 	return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
1596b59e366SSatoru Takeuchi }
1606b59e366SSatoru Takeuchi 
1617d453eeeSMatt Fleming static inline bool efi_runtime_supported(void)
1627d453eeeSMatt Fleming {
1637d453eeeSMatt Fleming 	if (efi_is_native())
1647d453eeeSMatt Fleming 		return true;
1657d453eeeSMatt Fleming 
1667d453eeeSMatt Fleming 	if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_enabled(EFI_OLD_MEMMAP))
1677d453eeeSMatt Fleming 		return true;
1687d453eeeSMatt Fleming 
1697d453eeeSMatt Fleming 	return false;
1707d453eeeSMatt Fleming }
1717d453eeeSMatt Fleming 
1725c12af0cSDave Young extern void parse_efi_setup(u64 phys_addr, u32 data_len);
1734f9dbcfcSMatt Fleming 
17421289ec0SArd Biesheuvel extern void efifb_setup_from_dmi(struct screen_info *si, const char *opt);
17521289ec0SArd Biesheuvel 
1764f9dbcfcSMatt Fleming #ifdef CONFIG_EFI_MIXED
1774f9dbcfcSMatt Fleming extern void efi_thunk_runtime_setup(void);
1784f9dbcfcSMatt Fleming extern 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 #else
1854f9dbcfcSMatt Fleming static inline void efi_thunk_runtime_setup(void) {}
1864f9dbcfcSMatt Fleming static inline efi_status_t efi_thunk_set_virtual_address_map(
1874f9dbcfcSMatt Fleming 	void *phys_set_virtual_address_map,
1884f9dbcfcSMatt Fleming 	unsigned long memory_map_size,
1894f9dbcfcSMatt Fleming 	unsigned long descriptor_size,
1904f9dbcfcSMatt Fleming 	u32 descriptor_version,
1914f9dbcfcSMatt Fleming 	efi_memory_desc_t *virtual_map)
1924f9dbcfcSMatt Fleming {
1934f9dbcfcSMatt Fleming 	return EFI_SUCCESS;
1944f9dbcfcSMatt Fleming }
1954f9dbcfcSMatt Fleming #endif /* CONFIG_EFI_MIXED */
196f23cf8bdSArd Biesheuvel 
197243b6754SArd Biesheuvel 
198243b6754SArd Biesheuvel /* arch specific definitions used by the stub code */
199243b6754SArd Biesheuvel 
200243b6754SArd Biesheuvel struct efi_config {
201243b6754SArd Biesheuvel 	u64 image_handle;
202243b6754SArd Biesheuvel 	u64 table;
203a2cd2f3fSDavid Howells 	u64 runtime_services;
2040a637ee6SLukas Wunner 	u64 boot_services;
205243b6754SArd Biesheuvel 	u64 text_output;
206243b6754SArd Biesheuvel 	efi_status_t (*call)(unsigned long, ...);
207243b6754SArd Biesheuvel 	bool is64;
208243b6754SArd Biesheuvel } __packed;
209243b6754SArd Biesheuvel 
210243b6754SArd Biesheuvel __pure const struct efi_config *__efi_early(void);
211243b6754SArd Biesheuvel 
21227571616SLukas Wunner static inline bool efi_is_64bit(void)
21327571616SLukas Wunner {
21427571616SLukas Wunner 	if (!IS_ENABLED(CONFIG_X86_64))
21527571616SLukas Wunner 		return false;
21627571616SLukas Wunner 
21727571616SLukas Wunner 	if (!IS_ENABLED(CONFIG_EFI_MIXED))
21827571616SLukas Wunner 		return true;
21927571616SLukas Wunner 
22027571616SLukas Wunner 	return __efi_early()->is64;
22127571616SLukas Wunner }
22227571616SLukas Wunner 
2233552fdf2SLukas Wunner #define efi_table_attr(table, attr, instance)				\
2243552fdf2SLukas Wunner 	(efi_is_64bit() ?						\
2253552fdf2SLukas Wunner 		((table##_64_t *)(unsigned long)instance)->attr :	\
2263552fdf2SLukas Wunner 		((table##_32_t *)(unsigned long)instance)->attr)
2273552fdf2SLukas Wunner 
2283552fdf2SLukas Wunner #define efi_call_proto(protocol, f, instance, ...)			\
2293552fdf2SLukas Wunner 	__efi_early()->call(efi_table_attr(protocol, f, instance),	\
2303552fdf2SLukas Wunner 		instance, ##__VA_ARGS__)
2313552fdf2SLukas Wunner 
232243b6754SArd Biesheuvel #define efi_call_early(f, ...)						\
2333552fdf2SLukas Wunner 	__efi_early()->call(efi_table_attr(efi_boot_services, f,	\
2343552fdf2SLukas Wunner 		__efi_early()->boot_services), __VA_ARGS__)
235243b6754SArd Biesheuvel 
2362c23b73cSArd Biesheuvel #define __efi_call_early(f, ...)					\
2372c23b73cSArd Biesheuvel 	__efi_early()->call((unsigned long)f, __VA_ARGS__);
2382c23b73cSArd Biesheuvel 
239a2cd2f3fSDavid Howells #define efi_call_runtime(f, ...)					\
240a2cd2f3fSDavid Howells 	__efi_early()->call(efi_table_attr(efi_runtime_services, f,	\
241a2cd2f3fSDavid Howells 		__efi_early()->runtime_services), __VA_ARGS__)
242a2cd2f3fSDavid Howells 
24344be28e9SMatt Fleming extern bool efi_reboot_required(void);
244e55f31a5SArd Biesheuvel extern bool efi_is_table_address(unsigned long phys_addr);
24544be28e9SMatt Fleming 
2466950e31bSDan Williams extern void efi_find_mirror(void);
2476950e31bSDan Williams extern void efi_reserve_boot_services(void);
2486b59e366SSatoru Takeuchi #else
2495c12af0cSDave Young static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {}
25044be28e9SMatt Fleming static inline bool efi_reboot_required(void)
25144be28e9SMatt Fleming {
25244be28e9SMatt Fleming 	return false;
25344be28e9SMatt Fleming }
254e55f31a5SArd Biesheuvel static inline  bool efi_is_table_address(unsigned long phys_addr)
255e55f31a5SArd Biesheuvel {
256e55f31a5SArd Biesheuvel 	return false;
257e55f31a5SArd Biesheuvel }
2586950e31bSDan Williams static inline void efi_find_mirror(void)
2596950e31bSDan Williams {
2606950e31bSDan Williams }
2616950e31bSDan Williams static inline void efi_reserve_boot_services(void)
2626950e31bSDan Williams {
2636950e31bSDan Williams }
264bb898558SAl Viro #endif /* CONFIG_EFI */
265bb898558SAl Viro 
266*199c8471SDan Williams #ifdef CONFIG_EFI_FAKE_MEMMAP
267*199c8471SDan Williams extern void __init efi_fake_memmap_early(void);
268*199c8471SDan Williams #else
269*199c8471SDan Williams static inline void efi_fake_memmap_early(void)
270*199c8471SDan Williams {
271*199c8471SDan Williams }
272*199c8471SDan Williams #endif
273*199c8471SDan Williams 
2741965aae3SH. Peter Anvin #endif /* _ASM_X86_EFI_H */
275