xref: /openbmc/linux/arch/arm64/include/asm/virt.h (revision 67559900)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 ARM Ltd.
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6 
7 #ifndef __ASM__VIRT_H
8 #define __ASM__VIRT_H
9 
10 /*
11  * The arm64 hcall implementation uses x0 to specify the hcall
12  * number. A value less than HVC_STUB_HCALL_NR indicates a special
13  * hcall, such as set vector. Any other value is handled in a
14  * hypervisor specific way.
15  *
16  * The hypercall is allowed to clobber any of the caller-saved
17  * registers (x0-x18), so it is advisable to use it through the
18  * indirection of a function call (as implemented in hyp-stub.S).
19  */
20 
21 /*
22  * HVC_SET_VECTORS - Set the value of the vbar_el2 register.
23  *
24  * @x1: Physical address of the new vector table.
25  */
26 #define HVC_SET_VECTORS 0
27 
28 /*
29  * HVC_SOFT_RESTART - CPU soft reset, used by the cpu_soft_restart routine.
30  */
31 #define HVC_SOFT_RESTART 1
32 
33 /*
34  * HVC_RESET_VECTORS - Restore the vectors to the original HYP stubs
35  */
36 #define HVC_RESET_VECTORS 2
37 
38 /*
39  * HVC_VHE_RESTART - Upgrade the CPU from EL1 to EL2, if possible
40  */
41 #define HVC_VHE_RESTART	3
42 
43 /* Max number of HYP stub hypercalls */
44 #define HVC_STUB_HCALL_NR 4
45 
46 /* Error returned when an invalid stub number is passed into x0 */
47 #define HVC_STUB_ERR	0xbadca11
48 
49 #define BOOT_CPU_MODE_EL1	(0xe11)
50 #define BOOT_CPU_MODE_EL2	(0xe12)
51 
52 #ifndef __ASSEMBLY__
53 
54 #include <asm/ptrace.h>
55 #include <asm/sections.h>
56 #include <asm/sysreg.h>
57 #include <asm/cpufeature.h>
58 
59 /*
60  * __boot_cpu_mode records what mode CPUs were booted in.
61  * A correctly-implemented bootloader must start all CPUs in the same mode:
62  * In this case, both 32bit halves of __boot_cpu_mode will contain the
63  * same value (either 0 if booted in EL1, BOOT_CPU_MODE_EL2 if booted in EL2).
64  *
65  * Should the bootloader fail to do this, the two values will be different.
66  * This allows the kernel to flag an error when the secondaries have come up.
67  */
68 extern u32 __boot_cpu_mode[2];
69 
70 #define ARM64_VECTOR_TABLE_LEN	SZ_2K
71 
72 void __hyp_set_vectors(phys_addr_t phys_vector_base);
73 void __hyp_reset_vectors(void);
74 
75 DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized);
76 
77 /* Reports the availability of HYP mode */
78 static inline bool is_hyp_mode_available(void)
79 {
80 	/*
81 	 * If KVM protected mode is initialized, all CPUs must have been booted
82 	 * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
83 	 */
84 	if (IS_ENABLED(CONFIG_KVM) &&
85 	    static_branch_likely(&kvm_protected_mode_initialized))
86 		return true;
87 
88 	return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
89 		__boot_cpu_mode[1] == BOOT_CPU_MODE_EL2);
90 }
91 
92 /* Check if the bootloader has booted CPUs in different modes */
93 static inline bool is_hyp_mode_mismatched(void)
94 {
95 	/*
96 	 * If KVM protected mode is initialized, all CPUs must have been booted
97 	 * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1.
98 	 */
99 	if (IS_ENABLED(CONFIG_KVM) &&
100 	    static_branch_likely(&kvm_protected_mode_initialized))
101 		return false;
102 
103 	return __boot_cpu_mode[0] != __boot_cpu_mode[1];
104 }
105 
106 static inline bool is_kernel_in_hyp_mode(void)
107 {
108 	return read_sysreg(CurrentEL) == CurrentEL_EL2;
109 }
110 
111 static __always_inline bool has_vhe(void)
112 {
113 	/*
114 	 * Code only run in VHE/NVHE hyp context can assume VHE is present or
115 	 * absent. Otherwise fall back to caps.
116 	 */
117 	if (is_vhe_hyp_code())
118 		return true;
119 	else if (is_nvhe_hyp_code())
120 		return false;
121 	else
122 		return cpus_have_final_cap(ARM64_HAS_VIRT_HOST_EXTN);
123 }
124 
125 static __always_inline bool is_protected_kvm_enabled(void)
126 {
127 	if (is_vhe_hyp_code())
128 		return false;
129 	else
130 		return cpus_have_final_cap(ARM64_KVM_PROTECTED_MODE);
131 }
132 
133 static inline bool is_hyp_nvhe(void)
134 {
135 	return is_hyp_mode_available() && !is_kernel_in_hyp_mode();
136 }
137 
138 #endif /* __ASSEMBLY__ */
139 
140 #endif /* ! __ASM__VIRT_H */
141