blob: a399c1ebf6f0e6d974da0b773248b9c0cfe811a9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_EFI_H
3#define _ASM_X86_EFI_H
Huang, Ying5b836832008-01-30 13:31:19 +01004
Ingo Molnardf6b35f2015-04-24 02:46:00 +02005#include <asm/fpu/api.h>
Ingo Molnar744937b2015-03-03 07:48:50 +01006#include <asm/pgtable.h>
Mark Rutland97883752016-04-25 21:07:11 +01007#include <asm/processor-flags.h>
Matt Flemingc9f2a9a2015-11-27 21:09:33 +00008#include <asm/tlb.h>
David Woodhousedd844412018-02-19 10:50:54 +00009#include <asm/nospec-branch.h>
Ingo Molnar744937b2015-03-03 07:48:50 +010010
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010011/*
12 * We map the EFI regions needed for runtime services non-contiguously,
13 * with preserved alignment on virtual addresses starting from -4G down
14 * for a total max space of 64G. This way, we provide for stable runtime
15 * services addresses across kernels so that a kexec'd kernel can still
16 * use them.
17 *
18 * This is the main reason why we're doing stable VA mappings for RT
19 * services.
20 *
21 * This flag is used in conjuction with a chicken bit called
22 * "efi=old_map" which can be used as a fallback to the old runtime
23 * services mapping method in case there's some b0rkage with a
24 * particular EFI implementation (haha, it is hard to hold up the
25 * sarcasm here...).
26 */
27#define EFI_OLD_MEMMAP EFI_ARCH_1
28
Matt Flemingb8ff87a2014-01-10 15:54:31 +000029#define EFI32_LOADER_SIGNATURE "EL32"
30#define EFI64_LOADER_SIGNATURE "EL64"
31
Ard Biesheuvel48fcb2d2016-01-11 11:47:49 +010032#define MAX_CMDLINE_ADDRESS UINT_MAX
33
Mark Rutland97883752016-04-25 21:07:11 +010034#define ARCH_EFI_IRQ_FLAGS_MASK X86_EFLAGS_IF
Huang, Yinge4297952008-01-30 13:31:19 +010035
Mark Rutland97883752016-04-25 21:07:11 +010036#ifdef CONFIG_X86_32
Matt Flemingf7d7d012011-11-15 12:56:14 +000037
Joe Perches0825f492017-07-12 14:37:31 -070038extern asmlinkage unsigned long efi_call_phys(void *, ...);
Huang, Yinge4297952008-01-30 13:31:19 +010039
David Woodhousedd844412018-02-19 10:50:54 +000040#define arch_efi_call_virt_setup() \
41({ \
42 kernel_fpu_begin(); \
43 firmware_restrict_branch_speculation_start(); \
44})
45
46#define arch_efi_call_virt_teardown() \
47({ \
48 firmware_restrict_branch_speculation_end(); \
49 kernel_fpu_end(); \
50})
51
Mark Rutlandbc25f9d2016-04-25 21:07:06 +010052
Huang, Yinge4297952008-01-30 13:31:19 +010053/*
54 * Wrap all the virtual calls in a way that forces the parameters on the stack.
55 */
Alex Thorlton80e75592016-06-25 08:20:27 +010056#define arch_efi_call_virt(p, f, args...) \
Ricardo Nerib738c6e2014-03-27 15:10:43 -070057({ \
Alex Thorlton80e75592016-06-25 08:20:27 +010058 ((efi_##f##_t __attribute__((regparm(0)))*) p->f)(args); \
Ricardo Nerib738c6e2014-03-27 15:10:43 -070059})
Ricardo Neri982e2392014-03-27 15:10:41 -070060
Matt Fleming3e8fa262012-10-19 13:25:46 +010061#define efi_ioremap(addr, size, type, attr) ioremap_cache(addr, size)
Keith Packarde1ad7832011-12-11 16:12:42 -080062
Huang, Ying5b836832008-01-30 13:31:19 +010063#else /* !CONFIG_X86_32 */
64
Matt Fleming62fa6e62014-03-27 15:10:39 -070065#define EFI_LOADER_SIGNATURE "EL64"
Huang, Ying5b836832008-01-30 13:31:19 +010066
Joe Perches0825f492017-07-12 14:37:31 -070067extern asmlinkage u64 efi_call(void *fp, ...);
Huang, Ying5b836832008-01-30 13:31:19 +010068
Matt Fleming62fa6e62014-03-27 15:10:39 -070069#define efi_call_phys(f, args...) efi_call((f), args)
70
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000071/*
72 * Scratch space used for switching the pagetable in the EFI stub
73 */
74struct efi_scratch {
75 u64 r15;
76 u64 prev_cr3;
77 pgd_t *efi_pgt;
78 bool use_pgd;
79 u64 phys_stack;
80} __packed;
81
Mark Rutlandbc25f9d2016-04-25 21:07:06 +010082#define arch_efi_call_virt_setup() \
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010083({ \
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010084 efi_sync_low_kernel_mappings(); \
85 preempt_disable(); \
Ricardo Neride057642014-03-27 15:10:42 -070086 __kernel_fpu_begin(); \
David Woodhousedd844412018-02-19 10:50:54 +000087 firmware_restrict_branch_speculation_start(); \
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000088 \
89 if (efi_scratch.use_pgd) { \
Andy Lutomirski6c690ee2017-06-12 10:26:14 -070090 efi_scratch.prev_cr3 = __read_cr3(); \
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000091 write_cr3((unsigned long)efi_scratch.efi_pgt); \
92 __flush_tlb_all(); \
93 } \
Mark Rutlandbc25f9d2016-04-25 21:07:06 +010094})
95
Alex Thorlton80e75592016-06-25 08:20:27 +010096#define arch_efi_call_virt(p, f, args...) \
97 efi_call((void *)p->f, args) \
Mark Rutlandbc25f9d2016-04-25 21:07:06 +010098
99#define arch_efi_call_virt_teardown() \
100({ \
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000101 if (efi_scratch.use_pgd) { \
102 write_cr3(efi_scratch.prev_cr3); \
103 __flush_tlb_all(); \
104 } \
105 \
David Woodhousedd844412018-02-19 10:50:54 +0000106 firmware_restrict_branch_speculation_end(); \
Ricardo Neride057642014-03-27 15:10:42 -0700107 __kernel_fpu_end(); \
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100108 preempt_enable(); \
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100109})
110
Mathias Krause4e78eb052014-09-07 19:42:17 +0200111extern void __iomem *__init efi_ioremap(unsigned long addr, unsigned long size,
112 u32 type, u64 attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800113
Andrey Ryabinina5238412015-10-01 15:36:48 -0700114#ifdef CONFIG_KASAN
Andrey Ryabinin769a8082015-09-22 14:59:17 -0700115/*
116 * CONFIG_KASAN may redefine memset to __memset. __memset function is present
117 * only in kernel binary. Since the EFI stub linked into a separate binary it
118 * doesn't have __memset(). So we should use standard memset from
119 * arch/x86/boot/compressed/string.c. The same applies to memcpy and memmove.
120 */
121#undef memcpy
122#undef memset
123#undef memmove
Andrey Ryabinina5238412015-10-01 15:36:48 -0700124#endif
Andrey Ryabinin769a8082015-09-22 14:59:17 -0700125
Huang, Ying5b836832008-01-30 13:31:19 +0100126#endif /* CONFIG_X86_32 */
127
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100128extern struct efi_scratch efi_scratch;
Mathias Krause4e78eb052014-09-07 19:42:17 +0200129extern void __init efi_set_executable(efi_memory_desc_t *md, bool executable);
130extern int __init efi_memblock_x86_reserve_range(void);
Ingo Molnar744937b2015-03-03 07:48:50 +0100131extern pgd_t * __init efi_call_phys_prolog(void);
132extern void __init efi_call_phys_epilog(pgd_t *save_pgd);
Taku Izumi0bbea1c2015-09-30 19:20:00 +0900133extern void __init efi_print_memmap(void);
Mathias Krause4e78eb052014-09-07 19:42:17 +0200134extern void __init efi_memory_uc(u64 addr, unsigned long size);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100135extern void __init efi_map_region(efi_memory_desc_t *md);
Dave Young3b266492013-12-20 18:02:14 +0800136extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100137extern void efi_sync_low_kernel_mappings(void);
Matt Fleming67a91082015-11-27 21:09:34 +0000138extern int __init efi_alloc_page_tables(void);
Mathias Krause4e78eb052014-09-07 19:42:17 +0200139extern int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100140extern void __init old_map_region(efi_memory_desc_t *md);
Borislav Petkovc55d0162014-02-14 08:24:24 +0100141extern void __init runtime_code_page_mkexec(void);
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000142extern void __init efi_runtime_update_mappings(void);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100143extern void __init efi_dump_pagetable(void);
Borislav Petkova5d90c92014-03-04 17:02:17 +0100144extern void __init efi_apply_memmap_quirks(void);
Saurabh Tangrieeb9db02014-06-02 05:18:35 -0700145extern int __init efi_reuse_config(u64 tables, int nr_tables);
146extern void efi_delete_dummy_variable(void);
Huang, Ying5b836832008-01-30 13:31:19 +0100147
Dave Young1fec0532013-12-20 18:02:19 +0800148struct efi_setup_data {
149 u64 fw_vendor;
150 u64 runtime;
151 u64 tables;
152 u64 smbios;
153 u64 reserved[8];
154};
155
156extern u64 efi_setup;
Dave Young1fec0532013-12-20 18:02:19 +0800157
Satoru Takeuchi6b59e362013-02-14 09:07:35 +0900158#ifdef CONFIG_EFI
159
160static inline bool efi_is_native(void)
161{
162 return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
163}
164
Matt Fleming7d453ee2014-01-10 18:52:06 +0000165static inline bool efi_runtime_supported(void)
166{
167 if (efi_is_native())
168 return true;
169
170 if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_enabled(EFI_OLD_MEMMAP))
171 return true;
172
173 return false;
174}
175
Matt Fleming72548e82013-10-04 09:36:56 +0100176extern struct console early_efi_console;
Dave Young5c12af02014-01-03 11:56:49 +0800177extern void parse_efi_setup(u64 phys_addr, u32 data_len);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000178
Ard Biesheuvel21289ec2016-04-25 21:06:50 +0100179extern void efifb_setup_from_dmi(struct screen_info *si, const char *opt);
180
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000181#ifdef CONFIG_EFI_MIXED
182extern void efi_thunk_runtime_setup(void);
183extern efi_status_t efi_thunk_set_virtual_address_map(
184 void *phys_set_virtual_address_map,
185 unsigned long memory_map_size,
186 unsigned long descriptor_size,
187 u32 descriptor_version,
188 efi_memory_desc_t *virtual_map);
189#else
190static inline void efi_thunk_runtime_setup(void) {}
191static inline efi_status_t efi_thunk_set_virtual_address_map(
192 void *phys_set_virtual_address_map,
193 unsigned long memory_map_size,
194 unsigned long descriptor_size,
195 u32 descriptor_version,
196 efi_memory_desc_t *virtual_map)
197{
198 return EFI_SUCCESS;
199}
200#endif /* CONFIG_EFI_MIXED */
Ard Biesheuvelf23cf8b2014-07-02 14:54:40 +0200201
Ard Biesheuvel243b6752014-11-05 17:00:56 +0100202
203/* arch specific definitions used by the stub code */
204
205struct efi_config {
206 u64 image_handle;
207 u64 table;
David Howellsa2cd2f32017-02-06 11:22:40 +0000208 u64 runtime_services;
Lukas Wunner0a637ee2016-08-22 12:01:21 +0200209 u64 boot_services;
Ard Biesheuvel243b6752014-11-05 17:00:56 +0100210 u64 text_output;
211 efi_status_t (*call)(unsigned long, ...);
212 bool is64;
213} __packed;
214
215__pure const struct efi_config *__efi_early(void);
216
Lukas Wunner27571612016-09-06 08:05:32 +0200217static inline bool efi_is_64bit(void)
218{
219 if (!IS_ENABLED(CONFIG_X86_64))
220 return false;
221
222 if (!IS_ENABLED(CONFIG_EFI_MIXED))
223 return true;
224
225 return __efi_early()->is64;
226}
227
Lukas Wunner3552fdf2016-11-12 21:32:35 +0000228#define efi_table_attr(table, attr, instance) \
229 (efi_is_64bit() ? \
230 ((table##_64_t *)(unsigned long)instance)->attr : \
231 ((table##_32_t *)(unsigned long)instance)->attr)
232
233#define efi_call_proto(protocol, f, instance, ...) \
234 __efi_early()->call(efi_table_attr(protocol, f, instance), \
235 instance, ##__VA_ARGS__)
236
Ard Biesheuvel243b6752014-11-05 17:00:56 +0100237#define efi_call_early(f, ...) \
Lukas Wunner3552fdf2016-11-12 21:32:35 +0000238 __efi_early()->call(efi_table_attr(efi_boot_services, f, \
239 __efi_early()->boot_services), __VA_ARGS__)
Ard Biesheuvel243b6752014-11-05 17:00:56 +0100240
Ard Biesheuvel2c23b732016-04-25 21:06:48 +0100241#define __efi_call_early(f, ...) \
242 __efi_early()->call((unsigned long)f, __VA_ARGS__);
243
David Howellsa2cd2f32017-02-06 11:22:40 +0000244#define efi_call_runtime(f, ...) \
245 __efi_early()->call(efi_table_attr(efi_runtime_services, f, \
246 __efi_early()->runtime_services), __VA_ARGS__)
247
Matt Fleming44be28e2014-06-13 12:39:55 +0100248extern bool efi_reboot_required(void);
249
Satoru Takeuchi6b59e362013-02-14 09:07:35 +0900250#else
Dave Young5c12af02014-01-03 11:56:49 +0800251static inline void parse_efi_setup(u64 phys_addr, u32 data_len) {}
Matt Fleming44be28e2014-06-13 12:39:55 +0100252static inline bool efi_reboot_required(void)
253{
254 return false;
255}
Russ Anderson7f594232008-10-03 11:59:15 -0500256#endif /* CONFIG_EFI */
257
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700258#endif /* _ASM_X86_EFI_H */