162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_EFI_H
362306a36Sopenharmony_ci#define _ASM_EFI_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <asm/boot.h>
662306a36Sopenharmony_ci#include <asm/cpufeature.h>
762306a36Sopenharmony_ci#include <asm/fpsimd.h>
862306a36Sopenharmony_ci#include <asm/io.h>
962306a36Sopenharmony_ci#include <asm/memory.h>
1062306a36Sopenharmony_ci#include <asm/mmu_context.h>
1162306a36Sopenharmony_ci#include <asm/neon.h>
1262306a36Sopenharmony_ci#include <asm/ptrace.h>
1362306a36Sopenharmony_ci#include <asm/tlbflush.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifdef CONFIG_EFI
1662306a36Sopenharmony_ciextern void efi_init(void);
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cibool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg);
1962306a36Sopenharmony_ci#else
2062306a36Sopenharmony_ci#define efi_init()
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic inline
2362306a36Sopenharmony_cibool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	return false;
2662306a36Sopenharmony_ci}
2762306a36Sopenharmony_ci#endif
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ciint efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md);
3062306a36Sopenharmony_ciint efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md,
3162306a36Sopenharmony_ci				bool has_bti);
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#undef arch_efi_call_virt
3462306a36Sopenharmony_ci#define arch_efi_call_virt(p, f, args...)				\
3562306a36Sopenharmony_ci	__efi_rt_asm_wrapper((p)->f, #f, args)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ciextern u64 *efi_rt_stack_top;
3862306a36Sopenharmony_ciefi_status_t __efi_rt_asm_wrapper(void *, const char *, ...);
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_civoid arch_efi_call_virt_setup(void);
4162306a36Sopenharmony_civoid arch_efi_call_virt_teardown(void);
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/*
4462306a36Sopenharmony_ci * efi_rt_stack_top[-1] contains the value the stack pointer had before
4562306a36Sopenharmony_ci * switching to the EFI runtime stack.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_ci#define current_in_efi()						\
4862306a36Sopenharmony_ci	(!preemptible() && efi_rt_stack_top != NULL &&			\
4962306a36Sopenharmony_ci	 on_task_stack(current, READ_ONCE(efi_rt_stack_top[-1]), 1))
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#define ARCH_EFI_IRQ_FLAGS_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT)
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/*
5462306a36Sopenharmony_ci * Even when Linux uses IRQ priorities for IRQ disabling, EFI does not.
5562306a36Sopenharmony_ci * And EFI shouldn't really play around with priority masking as it is not aware
5662306a36Sopenharmony_ci * which priorities the OS has assigned to its interrupts.
5762306a36Sopenharmony_ci */
5862306a36Sopenharmony_ci#define arch_efi_save_flags(state_flags)		\
5962306a36Sopenharmony_ci	((void)((state_flags) = read_sysreg(daif)))
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci#define arch_efi_restore_flags(state_flags)	write_sysreg(state_flags, daif)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/* arch specific definitions used by the stub code */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/*
6762306a36Sopenharmony_ci * In some configurations (e.g. VMAP_STACK && 64K pages), stacks built into the
6862306a36Sopenharmony_ci * kernel need greater alignment than we require the segments to be padded to.
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_ci#define EFI_KIMG_ALIGN	\
7162306a36Sopenharmony_ci	(SEGMENT_ALIGN > THREAD_ALIGN ? SEGMENT_ALIGN : THREAD_ALIGN)
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci/*
7462306a36Sopenharmony_ci * On arm64, we have to ensure that the initrd ends up in the linear region,
7562306a36Sopenharmony_ci * which is a 1 GB aligned region of size '1UL << (VA_BITS_MIN - 1)' that is
7662306a36Sopenharmony_ci * guaranteed to cover the kernel Image.
7762306a36Sopenharmony_ci *
7862306a36Sopenharmony_ci * Since the EFI stub is part of the kernel Image, we can relax the
7962306a36Sopenharmony_ci * usual requirements in Documentation/arch/arm64/booting.rst, which still
8062306a36Sopenharmony_ci * apply to other bootloaders, and are required for some kernel
8162306a36Sopenharmony_ci * configurations.
8262306a36Sopenharmony_ci */
8362306a36Sopenharmony_cistatic inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS_MIN - 1));
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic inline unsigned long efi_get_kimg_min_align(void)
8962306a36Sopenharmony_ci{
9062306a36Sopenharmony_ci	extern bool efi_nokaslr;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	/*
9362306a36Sopenharmony_ci	 * Although relocatable kernels can fix up the misalignment with
9462306a36Sopenharmony_ci	 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
9562306a36Sopenharmony_ci	 * subtly out of sync with those recorded in the vmlinux when kaslr is
9662306a36Sopenharmony_ci	 * disabled but the image required relocation anyway. Therefore retain
9762306a36Sopenharmony_ci	 * 2M alignment if KASLR was explicitly disabled, even if it was not
9862306a36Sopenharmony_ci	 * going to be activated to begin with.
9962306a36Sopenharmony_ci	 */
10062306a36Sopenharmony_ci	return efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
10162306a36Sopenharmony_ci}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci#define EFI_ALLOC_ALIGN		SZ_64K
10462306a36Sopenharmony_ci#define EFI_ALLOC_LIMIT		((1UL << 48) - 1)
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciextern unsigned long primary_entry_offset(void);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/*
10962306a36Sopenharmony_ci * On ARM systems, virtually remapped UEFI runtime services are set up in two
11062306a36Sopenharmony_ci * distinct stages:
11162306a36Sopenharmony_ci * - The stub retrieves the final version of the memory map from UEFI, populates
11262306a36Sopenharmony_ci *   the virt_addr fields and calls the SetVirtualAddressMap() [SVAM] runtime
11362306a36Sopenharmony_ci *   service to communicate the new mapping to the firmware (Note that the new
11462306a36Sopenharmony_ci *   mapping is not live at this time)
11562306a36Sopenharmony_ci * - During an early initcall(), the EFI system table is permanently remapped
11662306a36Sopenharmony_ci *   and the virtual remapping of the UEFI Runtime Services regions is loaded
11762306a36Sopenharmony_ci *   into a private set of page tables. If this all succeeds, the Runtime
11862306a36Sopenharmony_ci *   Services are enabled and the EFI_RUNTIME_SERVICES bit set.
11962306a36Sopenharmony_ci */
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistatic inline void efi_set_pgd(struct mm_struct *mm)
12262306a36Sopenharmony_ci{
12362306a36Sopenharmony_ci	__switch_mm(mm);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	if (system_uses_ttbr0_pan()) {
12662306a36Sopenharmony_ci		if (mm != current->active_mm) {
12762306a36Sopenharmony_ci			/*
12862306a36Sopenharmony_ci			 * Update the current thread's saved ttbr0 since it is
12962306a36Sopenharmony_ci			 * restored as part of a return from exception. Enable
13062306a36Sopenharmony_ci			 * access to the valid TTBR0_EL1 and invoke the errata
13162306a36Sopenharmony_ci			 * workaround directly since there is no return from
13262306a36Sopenharmony_ci			 * exception when invoking the EFI run-time services.
13362306a36Sopenharmony_ci			 */
13462306a36Sopenharmony_ci			update_saved_ttbr0(current, mm);
13562306a36Sopenharmony_ci			uaccess_ttbr0_enable();
13662306a36Sopenharmony_ci			post_ttbr_update_workaround();
13762306a36Sopenharmony_ci		} else {
13862306a36Sopenharmony_ci			/*
13962306a36Sopenharmony_ci			 * Defer the switch to the current thread's TTBR0_EL1
14062306a36Sopenharmony_ci			 * until uaccess_enable(). Restore the current
14162306a36Sopenharmony_ci			 * thread's saved ttbr0 corresponding to its active_mm
14262306a36Sopenharmony_ci			 */
14362306a36Sopenharmony_ci			uaccess_ttbr0_disable();
14462306a36Sopenharmony_ci			update_saved_ttbr0(current, current->active_mm);
14562306a36Sopenharmony_ci		}
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_civoid efi_virtmap_load(void);
15062306a36Sopenharmony_civoid efi_virtmap_unload(void);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_cistatic inline void efi_capsule_flush_cache_range(void *addr, int size)
15362306a36Sopenharmony_ci{
15462306a36Sopenharmony_ci	dcache_clean_inval_poc((unsigned long)addr, (unsigned long)addr + size);
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciefi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_civoid efi_icache_sync(unsigned long start, unsigned long end);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci#endif /* _ASM_EFI_H */
162