18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_EFI_H 38c2ecf20Sopenharmony_ci#define _ASM_EFI_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <asm/boot.h> 68c2ecf20Sopenharmony_ci#include <asm/cpufeature.h> 78c2ecf20Sopenharmony_ci#include <asm/fpsimd.h> 88c2ecf20Sopenharmony_ci#include <asm/io.h> 98c2ecf20Sopenharmony_ci#include <asm/memory.h> 108c2ecf20Sopenharmony_ci#include <asm/mmu_context.h> 118c2ecf20Sopenharmony_ci#include <asm/neon.h> 128c2ecf20Sopenharmony_ci#include <asm/ptrace.h> 138c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#ifdef CONFIG_EFI 168c2ecf20Sopenharmony_ciextern void efi_init(void); 178c2ecf20Sopenharmony_ci#else 188c2ecf20Sopenharmony_ci#define efi_init() 198c2ecf20Sopenharmony_ci#endif 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ciint efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md); 228c2ecf20Sopenharmony_ciint efi_set_mapping_permissions(struct mm_struct *mm, efi_memory_desc_t *md); 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define arch_efi_call_virt_setup() \ 258c2ecf20Sopenharmony_ci({ \ 268c2ecf20Sopenharmony_ci efi_virtmap_load(); \ 278c2ecf20Sopenharmony_ci __efi_fpsimd_begin(); \ 288c2ecf20Sopenharmony_ci raw_spin_lock(&efi_rt_lock); \ 298c2ecf20Sopenharmony_ci}) 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define arch_efi_call_virt(p, f, args...) \ 328c2ecf20Sopenharmony_ci({ \ 338c2ecf20Sopenharmony_ci efi_##f##_t *__f; \ 348c2ecf20Sopenharmony_ci __f = p->f; \ 358c2ecf20Sopenharmony_ci __efi_rt_asm_wrapper(__f, #f, args); \ 368c2ecf20Sopenharmony_ci}) 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define arch_efi_call_virt_teardown() \ 398c2ecf20Sopenharmony_ci({ \ 408c2ecf20Sopenharmony_ci raw_spin_unlock(&efi_rt_lock); \ 418c2ecf20Sopenharmony_ci __efi_fpsimd_end(); \ 428c2ecf20Sopenharmony_ci efi_virtmap_unload(); \ 438c2ecf20Sopenharmony_ci}) 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ciextern raw_spinlock_t efi_rt_lock; 468c2ecf20Sopenharmony_ciefi_status_t __efi_rt_asm_wrapper(void *, const char *, ...); 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci#define ARCH_EFI_IRQ_FLAGS_MASK (PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT) 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* 518c2ecf20Sopenharmony_ci * Even when Linux uses IRQ priorities for IRQ disabling, EFI does not. 528c2ecf20Sopenharmony_ci * And EFI shouldn't really play around with priority masking as it is not aware 538c2ecf20Sopenharmony_ci * which priorities the OS has assigned to its interrupts. 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_ci#define arch_efi_save_flags(state_flags) \ 568c2ecf20Sopenharmony_ci ((void)((state_flags) = read_sysreg(daif))) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define arch_efi_restore_flags(state_flags) write_sysreg(state_flags, daif) 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* arch specific definitions used by the stub code */ 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/* 648c2ecf20Sopenharmony_ci * In some configurations (e.g. VMAP_STACK && 64K pages), stacks built into the 658c2ecf20Sopenharmony_ci * kernel need greater alignment than we require the segments to be padded to. 668c2ecf20Sopenharmony_ci */ 678c2ecf20Sopenharmony_ci#define EFI_KIMG_ALIGN \ 688c2ecf20Sopenharmony_ci (SEGMENT_ALIGN > THREAD_ALIGN ? SEGMENT_ALIGN : THREAD_ALIGN) 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci/* on arm64, the FDT may be located anywhere in system RAM */ 718c2ecf20Sopenharmony_cistatic inline unsigned long efi_get_max_fdt_addr(unsigned long image_addr) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci return ULONG_MAX; 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci/* 778c2ecf20Sopenharmony_ci * On arm64, we have to ensure that the initrd ends up in the linear region, 788c2ecf20Sopenharmony_ci * which is a 1 GB aligned region of size '1UL << (VA_BITS_MIN - 1)' that is 798c2ecf20Sopenharmony_ci * guaranteed to cover the kernel Image. 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * Since the EFI stub is part of the kernel Image, we can relax the 828c2ecf20Sopenharmony_ci * usual requirements in Documentation/arm64/booting.rst, which still 838c2ecf20Sopenharmony_ci * apply to other bootloaders, and are required for some kernel 848c2ecf20Sopenharmony_ci * configurations. 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_cistatic inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS_MIN - 1)); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci#define alloc_screen_info(x...) &screen_info 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic inline void free_screen_info(struct screen_info *si) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci#define EFI_ALLOC_ALIGN SZ_64K 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci/* 1048c2ecf20Sopenharmony_ci * On ARM systems, virtually remapped UEFI runtime services are set up in two 1058c2ecf20Sopenharmony_ci * distinct stages: 1068c2ecf20Sopenharmony_ci * - The stub retrieves the final version of the memory map from UEFI, populates 1078c2ecf20Sopenharmony_ci * the virt_addr fields and calls the SetVirtualAddressMap() [SVAM] runtime 1088c2ecf20Sopenharmony_ci * service to communicate the new mapping to the firmware (Note that the new 1098c2ecf20Sopenharmony_ci * mapping is not live at this time) 1108c2ecf20Sopenharmony_ci * - During an early initcall(), the EFI system table is permanently remapped 1118c2ecf20Sopenharmony_ci * and the virtual remapping of the UEFI Runtime Services regions is loaded 1128c2ecf20Sopenharmony_ci * into a private set of page tables. If this all succeeds, the Runtime 1138c2ecf20Sopenharmony_ci * Services are enabled and the EFI_RUNTIME_SERVICES bit set. 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic inline void efi_set_pgd(struct mm_struct *mm) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci __switch_mm(mm); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (system_uses_ttbr0_pan()) { 1218c2ecf20Sopenharmony_ci if (mm != current->active_mm) { 1228c2ecf20Sopenharmony_ci /* 1238c2ecf20Sopenharmony_ci * Update the current thread's saved ttbr0 since it is 1248c2ecf20Sopenharmony_ci * restored as part of a return from exception. Enable 1258c2ecf20Sopenharmony_ci * access to the valid TTBR0_EL1 and invoke the errata 1268c2ecf20Sopenharmony_ci * workaround directly since there is no return from 1278c2ecf20Sopenharmony_ci * exception when invoking the EFI run-time services. 1288c2ecf20Sopenharmony_ci */ 1298c2ecf20Sopenharmony_ci update_saved_ttbr0(current, mm); 1308c2ecf20Sopenharmony_ci uaccess_ttbr0_enable(); 1318c2ecf20Sopenharmony_ci post_ttbr_update_workaround(); 1328c2ecf20Sopenharmony_ci } else { 1338c2ecf20Sopenharmony_ci /* 1348c2ecf20Sopenharmony_ci * Defer the switch to the current thread's TTBR0_EL1 1358c2ecf20Sopenharmony_ci * until uaccess_enable(). Restore the current 1368c2ecf20Sopenharmony_ci * thread's saved ttbr0 corresponding to its active_mm 1378c2ecf20Sopenharmony_ci */ 1388c2ecf20Sopenharmony_ci uaccess_ttbr0_disable(); 1398c2ecf20Sopenharmony_ci update_saved_ttbr0(current, current->active_mm); 1408c2ecf20Sopenharmony_ci } 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_civoid efi_virtmap_load(void); 1458c2ecf20Sopenharmony_civoid efi_virtmap_unload(void); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci#endif /* _ASM_EFI_H */ 148