xref: /linux/arch/arm64/kernel/cpufeature.c (revision b72b5fecc1b8a2e595bd03d7d257c88ea3f9fd45)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Contains CPU feature definitions
4  *
5  * Copyright (C) 2015 ARM Ltd.
6  *
7  * A note for the weary kernel hacker: the code here is confusing and hard to
8  * follow! That's partly because it's solving a nasty problem, but also because
9  * there's a little bit of over-abstraction that tends to obscure what's going
10  * on behind a maze of helper functions and macros.
11  *
12  * The basic problem is that hardware folks have started gluing together CPUs
13  * with distinct architectural features; in some cases even creating SoCs where
14  * user-visible instructions are available only on a subset of the available
15  * cores. We try to address this by snapshotting the feature registers of the
16  * boot CPU and comparing these with the feature registers of each secondary
17  * CPU when bringing them up. If there is a mismatch, then we update the
18  * snapshot state to indicate the lowest-common denominator of the feature,
19  * known as the "safe" value. This snapshot state can be queried to view the
20  * "sanitised" value of a feature register.
21  *
22  * The sanitised register values are used to decide which capabilities we
23  * have in the system. These may be in the form of traditional "hwcaps"
24  * advertised to userspace or internal "cpucaps" which are used to configure
25  * things like alternative patching and static keys. While a feature mismatch
26  * may result in a TAINT_CPU_OUT_OF_SPEC kernel taint, a capability mismatch
27  * may prevent a CPU from being onlined at all.
28  *
29  * Some implementation details worth remembering:
30  *
31  * - Mismatched features are *always* sanitised to a "safe" value, which
32  *   usually indicates that the feature is not supported.
33  *
34  * - A mismatched feature marked with FTR_STRICT will cause a "SANITY CHECK"
35  *   warning when onlining an offending CPU and the kernel will be tainted
36  *   with TAINT_CPU_OUT_OF_SPEC.
37  *
38  * - Features marked as FTR_VISIBLE have their sanitised value visible to
39  *   userspace. FTR_VISIBLE features in registers that are only visible
40  *   to EL0 by trapping *must* have a corresponding HWCAP so that late
41  *   onlining of CPUs cannot lead to features disappearing at runtime.
42  *
43  * - A "feature" is typically a 4-bit register field. A "capability" is the
44  *   high-level description derived from the sanitised field value.
45  *
46  * - Read the Arm ARM (DDI 0487F.a) section D13.1.3 ("Principles of the ID
47  *   scheme for fields in ID registers") to understand when feature fields
48  *   may be signed or unsigned (FTR_SIGNED and FTR_UNSIGNED accordingly).
49  *
50  * - KVM exposes its own view of the feature registers to guest operating
51  *   systems regardless of FTR_VISIBLE. This is typically driven from the
52  *   sanitised register values to allow virtual CPUs to be migrated between
53  *   arbitrary physical CPUs, but some features not present on the host are
54  *   also advertised and emulated. Look at sys_reg_descs[] for the gory
55  *   details.
56  *
57  * - If the arm64_ftr_bits[] for a register has a missing field, then this
58  *   field is treated as STRICT RES0, including for read_sanitised_ftr_reg().
59  *   This is stronger than FTR_HIDDEN and can be used to hide features from
60  *   KVM guests.
61  */
62 
63 #define pr_fmt(fmt) "CPU features: " fmt
64 
65 #include <linux/bsearch.h>
66 #include <linux/cpumask.h>
67 #include <linux/crash_dump.h>
68 #include <linux/kstrtox.h>
69 #include <linux/sort.h>
70 #include <linux/stop_machine.h>
71 #include <linux/sysfs.h>
72 #include <linux/types.h>
73 #include <linux/minmax.h>
74 #include <linux/mm.h>
75 #include <linux/cpu.h>
76 #include <linux/kasan.h>
77 #include <linux/percpu.h>
78 
79 #include <asm/cpu.h>
80 #include <asm/cpufeature.h>
81 #include <asm/cpu_ops.h>
82 #include <asm/fpsimd.h>
83 #include <asm/hwcap.h>
84 #include <asm/insn.h>
85 #include <asm/kvm_host.h>
86 #include <asm/mmu_context.h>
87 #include <asm/mte.h>
88 #include <asm/processor.h>
89 #include <asm/smp.h>
90 #include <asm/sysreg.h>
91 #include <asm/traps.h>
92 #include <asm/vectors.h>
93 #include <asm/virt.h>
94 
95 /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
96 static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly;
97 
98 #ifdef CONFIG_COMPAT
99 #define COMPAT_ELF_HWCAP_DEFAULT	\
100 				(COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
101 				 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
102 				 COMPAT_HWCAP_TLS|COMPAT_HWCAP_IDIV|\
103 				 COMPAT_HWCAP_LPAE)
104 unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
105 unsigned int compat_elf_hwcap2 __read_mostly;
106 #endif
107 
108 DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
109 EXPORT_SYMBOL(cpu_hwcaps);
110 static struct arm64_cpu_capabilities const __ro_after_init *cpu_hwcaps_ptrs[ARM64_NCAPS];
111 
112 DECLARE_BITMAP(boot_capabilities, ARM64_NCAPS);
113 
114 bool arm64_use_ng_mappings = false;
115 EXPORT_SYMBOL(arm64_use_ng_mappings);
116 
117 DEFINE_PER_CPU_READ_MOSTLY(const char *, this_cpu_vector) = vectors;
118 
119 /*
120  * Permit PER_LINUX32 and execve() of 32-bit binaries even if not all CPUs
121  * support it?
122  */
123 static bool __read_mostly allow_mismatched_32bit_el0;
124 
125 /*
126  * Static branch enabled only if allow_mismatched_32bit_el0 is set and we have
127  * seen at least one CPU capable of 32-bit EL0.
128  */
129 DEFINE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0);
130 
131 /*
132  * Mask of CPUs supporting 32-bit EL0.
133  * Only valid if arm64_mismatched_32bit_el0 is enabled.
134  */
135 static cpumask_var_t cpu_32bit_el0_mask __cpumask_var_read_mostly;
136 
137 void dump_cpu_features(void)
138 {
139 	/* file-wide pr_fmt adds "CPU features: " prefix */
140 	pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
141 }
142 
143 #define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
144 	{						\
145 		.sign = SIGNED,				\
146 		.visible = VISIBLE,			\
147 		.strict = STRICT,			\
148 		.type = TYPE,				\
149 		.shift = SHIFT,				\
150 		.width = WIDTH,				\
151 		.safe_val = SAFE_VAL,			\
152 	}
153 
154 /* Define a feature with unsigned values */
155 #define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
156 	__ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
157 
158 /* Define a feature with a signed value */
159 #define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
160 	__ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
161 
162 #define ARM64_FTR_END					\
163 	{						\
164 		.width = 0,				\
165 	}
166 
167 static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
168 
169 static bool __system_matches_cap(unsigned int n);
170 
171 /*
172  * NOTE: Any changes to the visibility of features should be kept in
173  * sync with the documentation of the CPU feature register ABI.
174  */
175 static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
176 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_RNDR_SHIFT, 4, 0),
177 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_TLB_SHIFT, 4, 0),
178 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_TS_SHIFT, 4, 0),
179 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_FHM_SHIFT, 4, 0),
180 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_DP_SHIFT, 4, 0),
181 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SM4_SHIFT, 4, 0),
182 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SM3_SHIFT, 4, 0),
183 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA3_SHIFT, 4, 0),
184 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_RDM_SHIFT, 4, 0),
185 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_ATOMIC_SHIFT, 4, 0),
186 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_CRC32_SHIFT, 4, 0),
187 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA2_SHIFT, 4, 0),
188 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA1_SHIFT, 4, 0),
189 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_AES_SHIFT, 4, 0),
190 	ARM64_FTR_END,
191 };
192 
193 static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
194 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_I8MM_SHIFT, 4, 0),
195 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_DGH_SHIFT, 4, 0),
196 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_BF16_SHIFT, 4, 0),
197 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_SPECRES_SHIFT, 4, 0),
198 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_SB_SHIFT, 4, 0),
199 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_FRINTTS_SHIFT, 4, 0),
200 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
201 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_GPI_SHIFT, 4, 0),
202 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
203 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_GPA_SHIFT, 4, 0),
204 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_LRCPC_SHIFT, 4, 0),
205 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_FCMA_SHIFT, 4, 0),
206 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_JSCVT_SHIFT, 4, 0),
207 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
208 		       FTR_STRICT, FTR_EXACT, ID_AA64ISAR1_EL1_API_SHIFT, 4, 0),
209 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
210 		       FTR_STRICT, FTR_EXACT, ID_AA64ISAR1_EL1_APA_SHIFT, 4, 0),
211 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_DPB_SHIFT, 4, 0),
212 	ARM64_FTR_END,
213 };
214 
215 static const struct arm64_ftr_bits ftr_id_aa64isar2[] = {
216 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_CSSC_SHIFT, 4, 0),
217 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_RPRFM_SHIFT, 4, 0),
218 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_AA64ISAR2_EL1_BC_SHIFT, 4, 0),
219 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
220 		       FTR_STRICT, FTR_EXACT, ID_AA64ISAR2_EL1_APA3_SHIFT, 4, 0),
221 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
222 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_GPA3_SHIFT, 4, 0),
223 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_RPRES_SHIFT, 4, 0),
224 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_WFxT_SHIFT, 4, 0),
225 	ARM64_FTR_END,
226 };
227 
228 static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
229 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV3_SHIFT, 4, 0),
230 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV2_SHIFT, 4, 0),
231 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_DIT_SHIFT, 4, 0),
232 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AMU_SHIFT, 4, 0),
233 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_MPAM_SHIFT, 4, 0),
234 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SEL2_SHIFT, 4, 0),
235 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
236 				   FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SVE_SHIFT, 4, 0),
237 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_RAS_SHIFT, 4, 0),
238 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_GIC_SHIFT, 4, 0),
239 	S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AdvSIMD_SHIFT, 4, ID_AA64PFR0_EL1_AdvSIMD_NI),
240 	S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_FP_SHIFT, 4, ID_AA64PFR0_EL1_FP_NI),
241 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL3_SHIFT, 4, 0),
242 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL2_SHIFT, 4, 0),
243 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL1_SHIFT, 4, ID_AA64PFR0_EL1_ELx_64BIT_ONLY),
244 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL0_SHIFT, 4, ID_AA64PFR0_EL1_ELx_64BIT_ONLY),
245 	ARM64_FTR_END,
246 };
247 
248 static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
249 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
250 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_SME_SHIFT, 4, 0),
251 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_MPAM_frac_SHIFT, 4, 0),
252 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_RAS_frac_SHIFT, 4, 0),
253 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_MTE),
254 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_MTE_SHIFT, 4, ID_AA64PFR1_EL1_MTE_NI),
255 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_SSBS_SHIFT, 4, ID_AA64PFR1_EL1_SSBS_NI),
256 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_BTI),
257 				    FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_BT_SHIFT, 4, 0),
258 	ARM64_FTR_END,
259 };
260 
261 static const struct arm64_ftr_bits ftr_id_aa64zfr0[] = {
262 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
263 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_F64MM_SHIFT, 4, 0),
264 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
265 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_F32MM_SHIFT, 4, 0),
266 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
267 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_I8MM_SHIFT, 4, 0),
268 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
269 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SM4_SHIFT, 4, 0),
270 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
271 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SHA3_SHIFT, 4, 0),
272 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
273 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_BF16_SHIFT, 4, 0),
274 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
275 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_BitPerm_SHIFT, 4, 0),
276 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
277 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_AES_SHIFT, 4, 0),
278 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
279 		       FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SVEver_SHIFT, 4, 0),
280 	ARM64_FTR_END,
281 };
282 
283 static const struct arm64_ftr_bits ftr_id_aa64smfr0[] = {
284 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
285 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_FA64_SHIFT, 1, 0),
286 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
287 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_SMEver_SHIFT, 4, 0),
288 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
289 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_I16I64_SHIFT, 4, 0),
290 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
291 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F64F64_SHIFT, 1, 0),
292 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
293 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_I16I32_SHIFT, 4, 0),
294 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
295 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_B16B16_SHIFT, 1, 0),
296 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
297 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F16F16_SHIFT, 1, 0),
298 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
299 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_I8I32_SHIFT, 4, 0),
300 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
301 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F16F32_SHIFT, 1, 0),
302 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
303 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_B16F32_SHIFT, 1, 0),
304 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
305 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_BI32I32_SHIFT, 1, 0),
306 	ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
307 		       FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F32F32_SHIFT, 1, 0),
308 	ARM64_FTR_END,
309 };
310 
311 static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
312 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_ECV_SHIFT, 4, 0),
313 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_FGT_SHIFT, 4, 0),
314 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_EXS_SHIFT, 4, 0),
315 	/*
316 	 * Page size not being supported at Stage-2 is not fatal. You
317 	 * just give up KVM if PAGE_SIZE isn't supported there. Go fix
318 	 * your favourite nesting hypervisor.
319 	 *
320 	 * There is a small corner case where the hypervisor explicitly
321 	 * advertises a given granule size at Stage-2 (value 2) on some
322 	 * vCPUs, and uses the fallback to Stage-1 (value 0) for other
323 	 * vCPUs. Although this is not forbidden by the architecture, it
324 	 * indicates that the hypervisor is being silly (or buggy).
325 	 *
326 	 * We make no effort to cope with this and pretend that if these
327 	 * fields are inconsistent across vCPUs, then it isn't worth
328 	 * trying to bring KVM up.
329 	 */
330 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN4_2_SHIFT, 4, 1),
331 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN64_2_SHIFT, 4, 1),
332 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN16_2_SHIFT, 4, 1),
333 	/*
334 	 * We already refuse to boot CPUs that don't support our configured
335 	 * page size, so we can only detect mismatches for a page size other
336 	 * than the one we're currently using. Unfortunately, SoCs like this
337 	 * exist in the wild so, even though we don't like it, we'll have to go
338 	 * along with it and treat them as non-strict.
339 	 */
340 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN4_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN4_NI),
341 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN64_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN64_NI),
342 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN16_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN16_NI),
343 
344 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_BIGENDEL0_SHIFT, 4, 0),
345 	/* Linux shouldn't care about secure memory */
346 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_SNSMEM_SHIFT, 4, 0),
347 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_BIGEND_SHIFT, 4, 0),
348 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_ASIDBITS_SHIFT, 4, 0),
349 	/*
350 	 * Differing PARange is fine as long as all peripherals and memory are mapped
351 	 * within the minimum PARange of all CPUs
352 	 */
353 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_PARANGE_SHIFT, 4, 0),
354 	ARM64_FTR_END,
355 };
356 
357 static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
358 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_TIDCP1_SHIFT, 4, 0),
359 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_AFP_SHIFT, 4, 0),
360 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_ETS_SHIFT, 4, 0),
361 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_TWED_SHIFT, 4, 0),
362 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_XNX_SHIFT, 4, 0),
363 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_AA64MMFR1_EL1_SpecSEI_SHIFT, 4, 0),
364 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_PAN_SHIFT, 4, 0),
365 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_LO_SHIFT, 4, 0),
366 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_HPDS_SHIFT, 4, 0),
367 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_VH_SHIFT, 4, 0),
368 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_VMIDBits_SHIFT, 4, 0),
369 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_HAFDBS_SHIFT, 4, 0),
370 	ARM64_FTR_END,
371 };
372 
373 static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
374 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_E0PD_SHIFT, 4, 0),
375 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_EVT_SHIFT, 4, 0),
376 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_BBM_SHIFT, 4, 0),
377 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_TTL_SHIFT, 4, 0),
378 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_FWB_SHIFT, 4, 0),
379 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_IDS_SHIFT, 4, 0),
380 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_AT_SHIFT, 4, 0),
381 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_ST_SHIFT, 4, 0),
382 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_NV_SHIFT, 4, 0),
383 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_CCIDX_SHIFT, 4, 0),
384 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_VARange_SHIFT, 4, 0),
385 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_IESB_SHIFT, 4, 0),
386 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_LSM_SHIFT, 4, 0),
387 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_UAO_SHIFT, 4, 0),
388 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_CnP_SHIFT, 4, 0),
389 	ARM64_FTR_END,
390 };
391 
392 static const struct arm64_ftr_bits ftr_ctr[] = {
393 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
394 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_DIC_SHIFT, 1, 1),
395 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_IDC_SHIFT, 1, 1),
396 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_EL0_CWG_SHIFT, 4, 0),
397 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_EL0_ERG_SHIFT, 4, 0),
398 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_DminLine_SHIFT, 4, 1),
399 	/*
400 	 * Linux can handle differing I-cache policies. Userspace JITs will
401 	 * make use of *minLine.
402 	 * If we have differing I-cache policies, report it as the weakest - VIPT.
403 	 */
404 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, CTR_EL0_L1Ip_SHIFT, 2, CTR_EL0_L1Ip_VIPT),	/* L1Ip */
405 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_IminLine_SHIFT, 4, 0),
406 	ARM64_FTR_END,
407 };
408 
409 static struct arm64_ftr_override __ro_after_init no_override = { };
410 
411 struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
412 	.name		= "SYS_CTR_EL0",
413 	.ftr_bits	= ftr_ctr,
414 	.override	= &no_override,
415 };
416 
417 static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
418 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_InnerShr_SHIFT, 4, 0xf),
419 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_FCSE_SHIFT, 4, 0),
420 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_AuxReg_SHIFT, 4, 0),
421 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_TCM_SHIFT, 4, 0),
422 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_ShareLvl_SHIFT, 4, 0),
423 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_OuterShr_SHIFT, 4, 0xf),
424 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_PMSA_SHIFT, 4, 0),
425 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_VMSA_SHIFT, 4, 0),
426 	ARM64_FTR_END,
427 };
428 
429 static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
430 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_DoubleLock_SHIFT, 4, 0),
431 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_PMSVer_SHIFT, 4, 0),
432 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_CTX_CMPs_SHIFT, 4, 0),
433 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_WRPs_SHIFT, 4, 0),
434 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_BRPs_SHIFT, 4, 0),
435 	/*
436 	 * We can instantiate multiple PMU instances with different levels
437 	 * of support.
438 	 */
439 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_EL1_PMUVer_SHIFT, 4, 0),
440 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_EL1_DebugVer_SHIFT, 4, 0x6),
441 	ARM64_FTR_END,
442 };
443 
444 static const struct arm64_ftr_bits ftr_mvfr0[] = {
445 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPRound_SHIFT, 4, 0),
446 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPShVec_SHIFT, 4, 0),
447 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPSqrt_SHIFT, 4, 0),
448 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPDivide_SHIFT, 4, 0),
449 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPTrap_SHIFT, 4, 0),
450 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPDP_SHIFT, 4, 0),
451 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPSP_SHIFT, 4, 0),
452 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_SIMDReg_SHIFT, 4, 0),
453 	ARM64_FTR_END,
454 };
455 
456 static const struct arm64_ftr_bits ftr_mvfr1[] = {
457 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDFMAC_SHIFT, 4, 0),
458 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_FPHP_SHIFT, 4, 0),
459 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDHP_SHIFT, 4, 0),
460 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDSP_SHIFT, 4, 0),
461 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDInt_SHIFT, 4, 0),
462 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDLS_SHIFT, 4, 0),
463 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_FPDNaN_SHIFT, 4, 0),
464 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_FPFtZ_SHIFT, 4, 0),
465 	ARM64_FTR_END,
466 };
467 
468 static const struct arm64_ftr_bits ftr_mvfr2[] = {
469 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR2_EL1_FPMisc_SHIFT, 4, 0),
470 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR2_EL1_SIMDMisc_SHIFT, 4, 0),
471 	ARM64_FTR_END,
472 };
473 
474 static const struct arm64_ftr_bits ftr_dczid[] = {
475 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, DCZID_EL0_DZP_SHIFT, 1, 1),
476 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, DCZID_EL0_BS_SHIFT, 4, 0),
477 	ARM64_FTR_END,
478 };
479 
480 static const struct arm64_ftr_bits ftr_gmid[] = {
481 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, GMID_EL1_BS_SHIFT, 4, 0),
482 	ARM64_FTR_END,
483 };
484 
485 static const struct arm64_ftr_bits ftr_id_isar0[] = {
486 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Divide_SHIFT, 4, 0),
487 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Debug_SHIFT, 4, 0),
488 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Coproc_SHIFT, 4, 0),
489 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_CmpBranch_SHIFT, 4, 0),
490 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_BitField_SHIFT, 4, 0),
491 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_BitCount_SHIFT, 4, 0),
492 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Swap_SHIFT, 4, 0),
493 	ARM64_FTR_END,
494 };
495 
496 static const struct arm64_ftr_bits ftr_id_isar5[] = {
497 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_RDM_SHIFT, 4, 0),
498 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_CRC32_SHIFT, 4, 0),
499 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_SHA2_SHIFT, 4, 0),
500 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_SHA1_SHIFT, 4, 0),
501 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_AES_SHIFT, 4, 0),
502 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_SEVL_SHIFT, 4, 0),
503 	ARM64_FTR_END,
504 };
505 
506 static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
507 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_EVT_SHIFT, 4, 0),
508 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_CCIDX_SHIFT, 4, 0),
509 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_LSM_SHIFT, 4, 0),
510 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_HPDS_SHIFT, 4, 0),
511 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_CnP_SHIFT, 4, 0),
512 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_XNX_SHIFT, 4, 0),
513 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_AC2_SHIFT, 4, 0),
514 
515 	/*
516 	 * SpecSEI = 1 indicates that the PE might generate an SError on an
517 	 * external abort on speculative read. It is safe to assume that an
518 	 * SError might be generated than it will not be. Hence it has been
519 	 * classified as FTR_HIGHER_SAFE.
520 	 */
521 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_MMFR4_EL1_SpecSEI_SHIFT, 4, 0),
522 	ARM64_FTR_END,
523 };
524 
525 static const struct arm64_ftr_bits ftr_id_isar4[] = {
526 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_SWP_frac_SHIFT, 4, 0),
527 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_PSR_M_SHIFT, 4, 0),
528 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_SynchPrim_frac_SHIFT, 4, 0),
529 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_Barrier_SHIFT, 4, 0),
530 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_SMC_SHIFT, 4, 0),
531 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_Writeback_SHIFT, 4, 0),
532 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_WithShifts_SHIFT, 4, 0),
533 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_Unpriv_SHIFT, 4, 0),
534 	ARM64_FTR_END,
535 };
536 
537 static const struct arm64_ftr_bits ftr_id_mmfr5[] = {
538 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR5_EL1_ETS_SHIFT, 4, 0),
539 	ARM64_FTR_END,
540 };
541 
542 static const struct arm64_ftr_bits ftr_id_isar6[] = {
543 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_I8MM_SHIFT, 4, 0),
544 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_BF16_SHIFT, 4, 0),
545 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_SPECRES_SHIFT, 4, 0),
546 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_SB_SHIFT, 4, 0),
547 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_FHM_SHIFT, 4, 0),
548 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_DP_SHIFT, 4, 0),
549 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_JSCVT_SHIFT, 4, 0),
550 	ARM64_FTR_END,
551 };
552 
553 static const struct arm64_ftr_bits ftr_id_pfr0[] = {
554 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_DIT_SHIFT, 4, 0),
555 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_CSV2_SHIFT, 4, 0),
556 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State3_SHIFT, 4, 0),
557 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State2_SHIFT, 4, 0),
558 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State1_SHIFT, 4, 0),
559 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State0_SHIFT, 4, 0),
560 	ARM64_FTR_END,
561 };
562 
563 static const struct arm64_ftr_bits ftr_id_pfr1[] = {
564 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_GIC_SHIFT, 4, 0),
565 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Virt_frac_SHIFT, 4, 0),
566 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Sec_frac_SHIFT, 4, 0),
567 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_GenTimer_SHIFT, 4, 0),
568 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Virtualization_SHIFT, 4, 0),
569 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_MProgMod_SHIFT, 4, 0),
570 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Security_SHIFT, 4, 0),
571 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_ProgMod_SHIFT, 4, 0),
572 	ARM64_FTR_END,
573 };
574 
575 static const struct arm64_ftr_bits ftr_id_pfr2[] = {
576 	ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR2_EL1_SSBS_SHIFT, 4, 0),
577 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR2_EL1_CSV3_SHIFT, 4, 0),
578 	ARM64_FTR_END,
579 };
580 
581 static const struct arm64_ftr_bits ftr_id_dfr0[] = {
582 	/* [31:28] TraceFilt */
583 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_DFR0_EL1_PerfMon_SHIFT, 4, 0),
584 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_MProfDbg_SHIFT, 4, 0),
585 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_MMapTrc_SHIFT, 4, 0),
586 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_CopTrc_SHIFT, 4, 0),
587 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_MMapDbg_SHIFT, 4, 0),
588 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_CopSDbg_SHIFT, 4, 0),
589 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_CopDbg_SHIFT, 4, 0),
590 	ARM64_FTR_END,
591 };
592 
593 static const struct arm64_ftr_bits ftr_id_dfr1[] = {
594 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR1_EL1_MTPMU_SHIFT, 4, 0),
595 	ARM64_FTR_END,
596 };
597 
598 static const struct arm64_ftr_bits ftr_zcr[] = {
599 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
600 		ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_WIDTH, 0),	/* LEN */
601 	ARM64_FTR_END,
602 };
603 
604 static const struct arm64_ftr_bits ftr_smcr[] = {
605 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
606 		SMCR_ELx_LEN_SHIFT, SMCR_ELx_LEN_WIDTH, 0),	/* LEN */
607 	ARM64_FTR_END,
608 };
609 
610 /*
611  * Common ftr bits for a 32bit register with all hidden, strict
612  * attributes, with 4bit feature fields and a default safe value of
613  * 0. Covers the following 32bit registers:
614  * id_isar[1-3], id_mmfr[1-3]
615  */
616 static const struct arm64_ftr_bits ftr_generic_32bits[] = {
617 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
618 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
619 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
620 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
621 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
622 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
623 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
624 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
625 	ARM64_FTR_END,
626 };
627 
628 /* Table for a single 32bit feature value */
629 static const struct arm64_ftr_bits ftr_single32[] = {
630 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
631 	ARM64_FTR_END,
632 };
633 
634 static const struct arm64_ftr_bits ftr_raz[] = {
635 	ARM64_FTR_END,
636 };
637 
638 #define __ARM64_FTR_REG_OVERRIDE(id_str, id, table, ovr) {	\
639 		.sys_id = id,					\
640 		.reg = 	&(struct arm64_ftr_reg){		\
641 			.name = id_str,				\
642 			.override = (ovr),			\
643 			.ftr_bits = &((table)[0]),		\
644 	}}
645 
646 #define ARM64_FTR_REG_OVERRIDE(id, table, ovr)	\
647 	__ARM64_FTR_REG_OVERRIDE(#id, id, table, ovr)
648 
649 #define ARM64_FTR_REG(id, table)		\
650 	__ARM64_FTR_REG_OVERRIDE(#id, id, table, &no_override)
651 
652 struct arm64_ftr_override __ro_after_init id_aa64mmfr1_override;
653 struct arm64_ftr_override __ro_after_init id_aa64pfr0_override;
654 struct arm64_ftr_override __ro_after_init id_aa64pfr1_override;
655 struct arm64_ftr_override __ro_after_init id_aa64zfr0_override;
656 struct arm64_ftr_override __ro_after_init id_aa64smfr0_override;
657 struct arm64_ftr_override __ro_after_init id_aa64isar1_override;
658 struct arm64_ftr_override __ro_after_init id_aa64isar2_override;
659 
660 static const struct __ftr_reg_entry {
661 	u32			sys_id;
662 	struct arm64_ftr_reg 	*reg;
663 } arm64_ftr_regs[] = {
664 
665 	/* Op1 = 0, CRn = 0, CRm = 1 */
666 	ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
667 	ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_id_pfr1),
668 	ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
669 	ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
670 	ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
671 	ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
672 	ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
673 
674 	/* Op1 = 0, CRn = 0, CRm = 2 */
675 	ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_id_isar0),
676 	ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
677 	ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
678 	ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
679 	ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_id_isar4),
680 	ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
681 	ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
682 	ARM64_FTR_REG(SYS_ID_ISAR6_EL1, ftr_id_isar6),
683 
684 	/* Op1 = 0, CRn = 0, CRm = 3 */
685 	ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_mvfr0),
686 	ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_mvfr1),
687 	ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
688 	ARM64_FTR_REG(SYS_ID_PFR2_EL1, ftr_id_pfr2),
689 	ARM64_FTR_REG(SYS_ID_DFR1_EL1, ftr_id_dfr1),
690 	ARM64_FTR_REG(SYS_ID_MMFR5_EL1, ftr_id_mmfr5),
691 
692 	/* Op1 = 0, CRn = 0, CRm = 4 */
693 	ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0,
694 			       &id_aa64pfr0_override),
695 	ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1,
696 			       &id_aa64pfr1_override),
697 	ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ZFR0_EL1, ftr_id_aa64zfr0,
698 			       &id_aa64zfr0_override),
699 	ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64SMFR0_EL1, ftr_id_aa64smfr0,
700 			       &id_aa64smfr0_override),
701 
702 	/* Op1 = 0, CRn = 0, CRm = 5 */
703 	ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
704 	ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
705 
706 	/* Op1 = 0, CRn = 0, CRm = 6 */
707 	ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
708 	ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1,
709 			       &id_aa64isar1_override),
710 	ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ISAR2_EL1, ftr_id_aa64isar2,
711 			       &id_aa64isar2_override),
712 
713 	/* Op1 = 0, CRn = 0, CRm = 7 */
714 	ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
715 	ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1,
716 			       &id_aa64mmfr1_override),
717 	ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
718 
719 	/* Op1 = 0, CRn = 1, CRm = 2 */
720 	ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
721 	ARM64_FTR_REG(SYS_SMCR_EL1, ftr_smcr),
722 
723 	/* Op1 = 1, CRn = 0, CRm = 0 */
724 	ARM64_FTR_REG(SYS_GMID_EL1, ftr_gmid),
725 
726 	/* Op1 = 3, CRn = 0, CRm = 0 */
727 	{ SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
728 	ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
729 
730 	/* Op1 = 3, CRn = 14, CRm = 0 */
731 	ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
732 };
733 
734 static int search_cmp_ftr_reg(const void *id, const void *regp)
735 {
736 	return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
737 }
738 
739 /*
740  * get_arm64_ftr_reg_nowarn - Looks up a feature register entry using
741  * its sys_reg() encoding. With the array arm64_ftr_regs sorted in the
742  * ascending order of sys_id, we use binary search to find a matching
743  * entry.
744  *
745  * returns - Upon success,  matching ftr_reg entry for id.
746  *         - NULL on failure. It is upto the caller to decide
747  *	     the impact of a failure.
748  */
749 static struct arm64_ftr_reg *get_arm64_ftr_reg_nowarn(u32 sys_id)
750 {
751 	const struct __ftr_reg_entry *ret;
752 
753 	ret = bsearch((const void *)(unsigned long)sys_id,
754 			arm64_ftr_regs,
755 			ARRAY_SIZE(arm64_ftr_regs),
756 			sizeof(arm64_ftr_regs[0]),
757 			search_cmp_ftr_reg);
758 	if (ret)
759 		return ret->reg;
760 	return NULL;
761 }
762 
763 /*
764  * get_arm64_ftr_reg - Looks up a feature register entry using
765  * its sys_reg() encoding. This calls get_arm64_ftr_reg_nowarn().
766  *
767  * returns - Upon success,  matching ftr_reg entry for id.
768  *         - NULL on failure but with an WARN_ON().
769  */
770 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
771 {
772 	struct arm64_ftr_reg *reg;
773 
774 	reg = get_arm64_ftr_reg_nowarn(sys_id);
775 
776 	/*
777 	 * Requesting a non-existent register search is an error. Warn
778 	 * and let the caller handle it.
779 	 */
780 	WARN_ON(!reg);
781 	return reg;
782 }
783 
784 static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
785 			       s64 ftr_val)
786 {
787 	u64 mask = arm64_ftr_mask(ftrp);
788 
789 	reg &= ~mask;
790 	reg |= (ftr_val << ftrp->shift) & mask;
791 	return reg;
792 }
793 
794 static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
795 				s64 cur)
796 {
797 	s64 ret = 0;
798 
799 	switch (ftrp->type) {
800 	case FTR_EXACT:
801 		ret = ftrp->safe_val;
802 		break;
803 	case FTR_LOWER_SAFE:
804 		ret = min(new, cur);
805 		break;
806 	case FTR_HIGHER_OR_ZERO_SAFE:
807 		if (!cur || !new)
808 			break;
809 		fallthrough;
810 	case FTR_HIGHER_SAFE:
811 		ret = max(new, cur);
812 		break;
813 	default:
814 		BUG();
815 	}
816 
817 	return ret;
818 }
819 
820 static void __init sort_ftr_regs(void)
821 {
822 	unsigned int i;
823 
824 	for (i = 0; i < ARRAY_SIZE(arm64_ftr_regs); i++) {
825 		const struct arm64_ftr_reg *ftr_reg = arm64_ftr_regs[i].reg;
826 		const struct arm64_ftr_bits *ftr_bits = ftr_reg->ftr_bits;
827 		unsigned int j = 0;
828 
829 		/*
830 		 * Features here must be sorted in descending order with respect
831 		 * to their shift values and should not overlap with each other.
832 		 */
833 		for (; ftr_bits->width != 0; ftr_bits++, j++) {
834 			unsigned int width = ftr_reg->ftr_bits[j].width;
835 			unsigned int shift = ftr_reg->ftr_bits[j].shift;
836 			unsigned int prev_shift;
837 
838 			WARN((shift  + width) > 64,
839 				"%s has invalid feature at shift %d\n",
840 				ftr_reg->name, shift);
841 
842 			/*
843 			 * Skip the first feature. There is nothing to
844 			 * compare against for now.
845 			 */
846 			if (j == 0)
847 				continue;
848 
849 			prev_shift = ftr_reg->ftr_bits[j - 1].shift;
850 			WARN((shift + width) > prev_shift,
851 				"%s has feature overlap at shift %d\n",
852 				ftr_reg->name, shift);
853 		}
854 
855 		/*
856 		 * Skip the first register. There is nothing to
857 		 * compare against for now.
858 		 */
859 		if (i == 0)
860 			continue;
861 		/*
862 		 * Registers here must be sorted in ascending order with respect
863 		 * to sys_id for subsequent binary search in get_arm64_ftr_reg()
864 		 * to work correctly.
865 		 */
866 		BUG_ON(arm64_ftr_regs[i].sys_id <= arm64_ftr_regs[i - 1].sys_id);
867 	}
868 }
869 
870 /*
871  * Initialise the CPU feature register from Boot CPU values.
872  * Also initiliases the strict_mask for the register.
873  * Any bits that are not covered by an arm64_ftr_bits entry are considered
874  * RES0 for the system-wide value, and must strictly match.
875  */
876 static void init_cpu_ftr_reg(u32 sys_reg, u64 new)
877 {
878 	u64 val = 0;
879 	u64 strict_mask = ~0x0ULL;
880 	u64 user_mask = 0;
881 	u64 valid_mask = 0;
882 
883 	const struct arm64_ftr_bits *ftrp;
884 	struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
885 
886 	if (!reg)
887 		return;
888 
889 	for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
890 		u64 ftr_mask = arm64_ftr_mask(ftrp);
891 		s64 ftr_new = arm64_ftr_value(ftrp, new);
892 		s64 ftr_ovr = arm64_ftr_value(ftrp, reg->override->val);
893 
894 		if ((ftr_mask & reg->override->mask) == ftr_mask) {
895 			s64 tmp = arm64_ftr_safe_value(ftrp, ftr_ovr, ftr_new);
896 			char *str = NULL;
897 
898 			if (ftr_ovr != tmp) {
899 				/* Unsafe, remove the override */
900 				reg->override->mask &= ~ftr_mask;
901 				reg->override->val &= ~ftr_mask;
902 				tmp = ftr_ovr;
903 				str = "ignoring override";
904 			} else if (ftr_new != tmp) {
905 				/* Override was valid */
906 				ftr_new = tmp;
907 				str = "forced";
908 			} else if (ftr_ovr == tmp) {
909 				/* Override was the safe value */
910 				str = "already set";
911 			}
912 
913 			if (str)
914 				pr_warn("%s[%d:%d]: %s to %llx\n",
915 					reg->name,
916 					ftrp->shift + ftrp->width - 1,
917 					ftrp->shift, str, tmp);
918 		} else if ((ftr_mask & reg->override->val) == ftr_mask) {
919 			reg->override->val &= ~ftr_mask;
920 			pr_warn("%s[%d:%d]: impossible override, ignored\n",
921 				reg->name,
922 				ftrp->shift + ftrp->width - 1,
923 				ftrp->shift);
924 		}
925 
926 		val = arm64_ftr_set_value(ftrp, val, ftr_new);
927 
928 		valid_mask |= ftr_mask;
929 		if (!ftrp->strict)
930 			strict_mask &= ~ftr_mask;
931 		if (ftrp->visible)
932 			user_mask |= ftr_mask;
933 		else
934 			reg->user_val = arm64_ftr_set_value(ftrp,
935 							    reg->user_val,
936 							    ftrp->safe_val);
937 	}
938 
939 	val &= valid_mask;
940 
941 	reg->sys_val = val;
942 	reg->strict_mask = strict_mask;
943 	reg->user_mask = user_mask;
944 }
945 
946 extern const struct arm64_cpu_capabilities arm64_errata[];
947 static const struct arm64_cpu_capabilities arm64_features[];
948 
949 static void __init
950 init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
951 {
952 	for (; caps->matches; caps++) {
953 		if (WARN(caps->capability >= ARM64_NCAPS,
954 			"Invalid capability %d\n", caps->capability))
955 			continue;
956 		if (WARN(cpu_hwcaps_ptrs[caps->capability],
957 			"Duplicate entry for capability %d\n",
958 			caps->capability))
959 			continue;
960 		cpu_hwcaps_ptrs[caps->capability] = caps;
961 	}
962 }
963 
964 static void __init init_cpu_hwcaps_indirect_list(void)
965 {
966 	init_cpu_hwcaps_indirect_list_from_array(arm64_features);
967 	init_cpu_hwcaps_indirect_list_from_array(arm64_errata);
968 }
969 
970 static void __init setup_boot_cpu_capabilities(void);
971 
972 static void init_32bit_cpu_features(struct cpuinfo_32bit *info)
973 {
974 	init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
975 	init_cpu_ftr_reg(SYS_ID_DFR1_EL1, info->reg_id_dfr1);
976 	init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
977 	init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
978 	init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
979 	init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
980 	init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
981 	init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
982 	init_cpu_ftr_reg(SYS_ID_ISAR6_EL1, info->reg_id_isar6);
983 	init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
984 	init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
985 	init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
986 	init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
987 	init_cpu_ftr_reg(SYS_ID_MMFR4_EL1, info->reg_id_mmfr4);
988 	init_cpu_ftr_reg(SYS_ID_MMFR5_EL1, info->reg_id_mmfr5);
989 	init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
990 	init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
991 	init_cpu_ftr_reg(SYS_ID_PFR2_EL1, info->reg_id_pfr2);
992 	init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
993 	init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
994 	init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
995 }
996 
997 void __init init_cpu_features(struct cpuinfo_arm64 *info)
998 {
999 	/* Before we start using the tables, make sure it is sorted */
1000 	sort_ftr_regs();
1001 
1002 	init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
1003 	init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
1004 	init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
1005 	init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
1006 	init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
1007 	init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
1008 	init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
1009 	init_cpu_ftr_reg(SYS_ID_AA64ISAR2_EL1, info->reg_id_aa64isar2);
1010 	init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
1011 	init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
1012 	init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
1013 	init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
1014 	init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
1015 	init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
1016 	init_cpu_ftr_reg(SYS_ID_AA64SMFR0_EL1, info->reg_id_aa64smfr0);
1017 
1018 	if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0))
1019 		init_32bit_cpu_features(&info->aarch32);
1020 
1021 	if (IS_ENABLED(CONFIG_ARM64_SVE) &&
1022 	    id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1))) {
1023 		info->reg_zcr = read_zcr_features();
1024 		init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
1025 		vec_init_vq_map(ARM64_VEC_SVE);
1026 	}
1027 
1028 	if (IS_ENABLED(CONFIG_ARM64_SME) &&
1029 	    id_aa64pfr1_sme(read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1))) {
1030 		info->reg_smcr = read_smcr_features();
1031 		/*
1032 		 * We mask out SMPS since even if the hardware
1033 		 * supports priorities the kernel does not at present
1034 		 * and we block access to them.
1035 		 */
1036 		info->reg_smidr = read_cpuid(SMIDR_EL1) & ~SMIDR_EL1_SMPS;
1037 		init_cpu_ftr_reg(SYS_SMCR_EL1, info->reg_smcr);
1038 		vec_init_vq_map(ARM64_VEC_SME);
1039 	}
1040 
1041 	if (id_aa64pfr1_mte(info->reg_id_aa64pfr1))
1042 		init_cpu_ftr_reg(SYS_GMID_EL1, info->reg_gmid);
1043 
1044 	/*
1045 	 * Initialize the indirect array of CPU hwcaps capabilities pointers
1046 	 * before we handle the boot CPU below.
1047 	 */
1048 	init_cpu_hwcaps_indirect_list();
1049 
1050 	/*
1051 	 * Detect and enable early CPU capabilities based on the boot CPU,
1052 	 * after we have initialised the CPU feature infrastructure.
1053 	 */
1054 	setup_boot_cpu_capabilities();
1055 }
1056 
1057 static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
1058 {
1059 	const struct arm64_ftr_bits *ftrp;
1060 
1061 	for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
1062 		s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
1063 		s64 ftr_new = arm64_ftr_value(ftrp, new);
1064 
1065 		if (ftr_cur == ftr_new)
1066 			continue;
1067 		/* Find a safe value */
1068 		ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
1069 		reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
1070 	}
1071 
1072 }
1073 
1074 static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
1075 {
1076 	struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
1077 
1078 	if (!regp)
1079 		return 0;
1080 
1081 	update_cpu_ftr_reg(regp, val);
1082 	if ((boot & regp->strict_mask) == (val & regp->strict_mask))
1083 		return 0;
1084 	pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
1085 			regp->name, boot, cpu, val);
1086 	return 1;
1087 }
1088 
1089 static void relax_cpu_ftr_reg(u32 sys_id, int field)
1090 {
1091 	const struct arm64_ftr_bits *ftrp;
1092 	struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
1093 
1094 	if (!regp)
1095 		return;
1096 
1097 	for (ftrp = regp->ftr_bits; ftrp->width; ftrp++) {
1098 		if (ftrp->shift == field) {
1099 			regp->strict_mask &= ~arm64_ftr_mask(ftrp);
1100 			break;
1101 		}
1102 	}
1103 
1104 	/* Bogus field? */
1105 	WARN_ON(!ftrp->width);
1106 }
1107 
1108 static void lazy_init_32bit_cpu_features(struct cpuinfo_arm64 *info,
1109 					 struct cpuinfo_arm64 *boot)
1110 {
1111 	static bool boot_cpu_32bit_regs_overridden = false;
1112 
1113 	if (!allow_mismatched_32bit_el0 || boot_cpu_32bit_regs_overridden)
1114 		return;
1115 
1116 	if (id_aa64pfr0_32bit_el0(boot->reg_id_aa64pfr0))
1117 		return;
1118 
1119 	boot->aarch32 = info->aarch32;
1120 	init_32bit_cpu_features(&boot->aarch32);
1121 	boot_cpu_32bit_regs_overridden = true;
1122 }
1123 
1124 static int update_32bit_cpu_features(int cpu, struct cpuinfo_32bit *info,
1125 				     struct cpuinfo_32bit *boot)
1126 {
1127 	int taint = 0;
1128 	u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
1129 
1130 	/*
1131 	 * If we don't have AArch32 at EL1, then relax the strictness of
1132 	 * EL1-dependent register fields to avoid spurious sanity check fails.
1133 	 */
1134 	if (!id_aa64pfr0_32bit_el1(pfr0)) {
1135 		relax_cpu_ftr_reg(SYS_ID_ISAR4_EL1, ID_ISAR4_EL1_SMC_SHIFT);
1136 		relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Virt_frac_SHIFT);
1137 		relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Sec_frac_SHIFT);
1138 		relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Virtualization_SHIFT);
1139 		relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Security_SHIFT);
1140 		relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_ProgMod_SHIFT);
1141 	}
1142 
1143 	taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
1144 				      info->reg_id_dfr0, boot->reg_id_dfr0);
1145 	taint |= check_update_ftr_reg(SYS_ID_DFR1_EL1, cpu,
1146 				      info->reg_id_dfr1, boot->reg_id_dfr1);
1147 	taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
1148 				      info->reg_id_isar0, boot->reg_id_isar0);
1149 	taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
1150 				      info->reg_id_isar1, boot->reg_id_isar1);
1151 	taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
1152 				      info->reg_id_isar2, boot->reg_id_isar2);
1153 	taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
1154 				      info->reg_id_isar3, boot->reg_id_isar3);
1155 	taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
1156 				      info->reg_id_isar4, boot->reg_id_isar4);
1157 	taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
1158 				      info->reg_id_isar5, boot->reg_id_isar5);
1159 	taint |= check_update_ftr_reg(SYS_ID_ISAR6_EL1, cpu,
1160 				      info->reg_id_isar6, boot->reg_id_isar6);
1161 
1162 	/*
1163 	 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
1164 	 * ACTLR formats could differ across CPUs and therefore would have to
1165 	 * be trapped for virtualization anyway.
1166 	 */
1167 	taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
1168 				      info->reg_id_mmfr0, boot->reg_id_mmfr0);
1169 	taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
1170 				      info->reg_id_mmfr1, boot->reg_id_mmfr1);
1171 	taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
1172 				      info->reg_id_mmfr2, boot->reg_id_mmfr2);
1173 	taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
1174 				      info->reg_id_mmfr3, boot->reg_id_mmfr3);
1175 	taint |= check_update_ftr_reg(SYS_ID_MMFR4_EL1, cpu,
1176 				      info->reg_id_mmfr4, boot->reg_id_mmfr4);
1177 	taint |= check_update_ftr_reg(SYS_ID_MMFR5_EL1, cpu,
1178 				      info->reg_id_mmfr5, boot->reg_id_mmfr5);
1179 	taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
1180 				      info->reg_id_pfr0, boot->reg_id_pfr0);
1181 	taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
1182 				      info->reg_id_pfr1, boot->reg_id_pfr1);
1183 	taint |= check_update_ftr_reg(SYS_ID_PFR2_EL1, cpu,
1184 				      info->reg_id_pfr2, boot->reg_id_pfr2);
1185 	taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
1186 				      info->reg_mvfr0, boot->reg_mvfr0);
1187 	taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
1188 				      info->reg_mvfr1, boot->reg_mvfr1);
1189 	taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
1190 				      info->reg_mvfr2, boot->reg_mvfr2);
1191 
1192 	return taint;
1193 }
1194 
1195 /*
1196  * Update system wide CPU feature registers with the values from a
1197  * non-boot CPU. Also performs SANITY checks to make sure that there
1198  * aren't any insane variations from that of the boot CPU.
1199  */
1200 void update_cpu_features(int cpu,
1201 			 struct cpuinfo_arm64 *info,
1202 			 struct cpuinfo_arm64 *boot)
1203 {
1204 	int taint = 0;
1205 
1206 	/*
1207 	 * The kernel can handle differing I-cache policies, but otherwise
1208 	 * caches should look identical. Userspace JITs will make use of
1209 	 * *minLine.
1210 	 */
1211 	taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
1212 				      info->reg_ctr, boot->reg_ctr);
1213 
1214 	/*
1215 	 * Userspace may perform DC ZVA instructions. Mismatched block sizes
1216 	 * could result in too much or too little memory being zeroed if a
1217 	 * process is preempted and migrated between CPUs.
1218 	 */
1219 	taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
1220 				      info->reg_dczid, boot->reg_dczid);
1221 
1222 	/* If different, timekeeping will be broken (especially with KVM) */
1223 	taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
1224 				      info->reg_cntfrq, boot->reg_cntfrq);
1225 
1226 	/*
1227 	 * The kernel uses self-hosted debug features and expects CPUs to
1228 	 * support identical debug features. We presently need CTX_CMPs, WRPs,
1229 	 * and BRPs to be identical.
1230 	 * ID_AA64DFR1 is currently RES0.
1231 	 */
1232 	taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
1233 				      info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
1234 	taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
1235 				      info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
1236 	/*
1237 	 * Even in big.LITTLE, processors should be identical instruction-set
1238 	 * wise.
1239 	 */
1240 	taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
1241 				      info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
1242 	taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
1243 				      info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
1244 	taint |= check_update_ftr_reg(SYS_ID_AA64ISAR2_EL1, cpu,
1245 				      info->reg_id_aa64isar2, boot->reg_id_aa64isar2);
1246 
1247 	/*
1248 	 * Differing PARange support is fine as long as all peripherals and
1249 	 * memory are mapped within the minimum PARange of all CPUs.
1250 	 * Linux should not care about secure memory.
1251 	 */
1252 	taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
1253 				      info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
1254 	taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
1255 				      info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
1256 	taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
1257 				      info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
1258 
1259 	taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
1260 				      info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
1261 	taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
1262 				      info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
1263 
1264 	taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
1265 				      info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
1266 
1267 	taint |= check_update_ftr_reg(SYS_ID_AA64SMFR0_EL1, cpu,
1268 				      info->reg_id_aa64smfr0, boot->reg_id_aa64smfr0);
1269 
1270 	if (IS_ENABLED(CONFIG_ARM64_SVE) &&
1271 	    id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1))) {
1272 		info->reg_zcr = read_zcr_features();
1273 		taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
1274 					info->reg_zcr, boot->reg_zcr);
1275 
1276 		/* Probe vector lengths */
1277 		if (!system_capabilities_finalized())
1278 			vec_update_vq_map(ARM64_VEC_SVE);
1279 	}
1280 
1281 	if (IS_ENABLED(CONFIG_ARM64_SME) &&
1282 	    id_aa64pfr1_sme(read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1))) {
1283 		info->reg_smcr = read_smcr_features();
1284 		/*
1285 		 * We mask out SMPS since even if the hardware
1286 		 * supports priorities the kernel does not at present
1287 		 * and we block access to them.
1288 		 */
1289 		info->reg_smidr = read_cpuid(SMIDR_EL1) & ~SMIDR_EL1_SMPS;
1290 		taint |= check_update_ftr_reg(SYS_SMCR_EL1, cpu,
1291 					info->reg_smcr, boot->reg_smcr);
1292 
1293 		/* Probe vector lengths */
1294 		if (!system_capabilities_finalized())
1295 			vec_update_vq_map(ARM64_VEC_SME);
1296 	}
1297 
1298 	/*
1299 	 * The kernel uses the LDGM/STGM instructions and the number of tags
1300 	 * they read/write depends on the GMID_EL1.BS field. Check that the
1301 	 * value is the same on all CPUs.
1302 	 */
1303 	if (IS_ENABLED(CONFIG_ARM64_MTE) &&
1304 	    id_aa64pfr1_mte(info->reg_id_aa64pfr1)) {
1305 		taint |= check_update_ftr_reg(SYS_GMID_EL1, cpu,
1306 					      info->reg_gmid, boot->reg_gmid);
1307 	}
1308 
1309 	/*
1310 	 * If we don't have AArch32 at all then skip the checks entirely
1311 	 * as the register values may be UNKNOWN and we're not going to be
1312 	 * using them for anything.
1313 	 *
1314 	 * This relies on a sanitised view of the AArch64 ID registers
1315 	 * (e.g. SYS_ID_AA64PFR0_EL1), so we call it last.
1316 	 */
1317 	if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
1318 		lazy_init_32bit_cpu_features(info, boot);
1319 		taint |= update_32bit_cpu_features(cpu, &info->aarch32,
1320 						   &boot->aarch32);
1321 	}
1322 
1323 	/*
1324 	 * Mismatched CPU features are a recipe for disaster. Don't even
1325 	 * pretend to support them.
1326 	 */
1327 	if (taint) {
1328 		pr_warn_once("Unsupported CPU feature variation detected.\n");
1329 		add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1330 	}
1331 }
1332 
1333 u64 read_sanitised_ftr_reg(u32 id)
1334 {
1335 	struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
1336 
1337 	if (!regp)
1338 		return 0;
1339 	return regp->sys_val;
1340 }
1341 EXPORT_SYMBOL_GPL(read_sanitised_ftr_reg);
1342 
1343 #define read_sysreg_case(r)	\
1344 	case r:		val = read_sysreg_s(r); break;
1345 
1346 /*
1347  * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
1348  * Read the system register on the current CPU
1349  */
1350 u64 __read_sysreg_by_encoding(u32 sys_id)
1351 {
1352 	struct arm64_ftr_reg *regp;
1353 	u64 val;
1354 
1355 	switch (sys_id) {
1356 	read_sysreg_case(SYS_ID_PFR0_EL1);
1357 	read_sysreg_case(SYS_ID_PFR1_EL1);
1358 	read_sysreg_case(SYS_ID_PFR2_EL1);
1359 	read_sysreg_case(SYS_ID_DFR0_EL1);
1360 	read_sysreg_case(SYS_ID_DFR1_EL1);
1361 	read_sysreg_case(SYS_ID_MMFR0_EL1);
1362 	read_sysreg_case(SYS_ID_MMFR1_EL1);
1363 	read_sysreg_case(SYS_ID_MMFR2_EL1);
1364 	read_sysreg_case(SYS_ID_MMFR3_EL1);
1365 	read_sysreg_case(SYS_ID_MMFR4_EL1);
1366 	read_sysreg_case(SYS_ID_MMFR5_EL1);
1367 	read_sysreg_case(SYS_ID_ISAR0_EL1);
1368 	read_sysreg_case(SYS_ID_ISAR1_EL1);
1369 	read_sysreg_case(SYS_ID_ISAR2_EL1);
1370 	read_sysreg_case(SYS_ID_ISAR3_EL1);
1371 	read_sysreg_case(SYS_ID_ISAR4_EL1);
1372 	read_sysreg_case(SYS_ID_ISAR5_EL1);
1373 	read_sysreg_case(SYS_ID_ISAR6_EL1);
1374 	read_sysreg_case(SYS_MVFR0_EL1);
1375 	read_sysreg_case(SYS_MVFR1_EL1);
1376 	read_sysreg_case(SYS_MVFR2_EL1);
1377 
1378 	read_sysreg_case(SYS_ID_AA64PFR0_EL1);
1379 	read_sysreg_case(SYS_ID_AA64PFR1_EL1);
1380 	read_sysreg_case(SYS_ID_AA64ZFR0_EL1);
1381 	read_sysreg_case(SYS_ID_AA64SMFR0_EL1);
1382 	read_sysreg_case(SYS_ID_AA64DFR0_EL1);
1383 	read_sysreg_case(SYS_ID_AA64DFR1_EL1);
1384 	read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
1385 	read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
1386 	read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
1387 	read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
1388 	read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
1389 	read_sysreg_case(SYS_ID_AA64ISAR2_EL1);
1390 
1391 	read_sysreg_case(SYS_CNTFRQ_EL0);
1392 	read_sysreg_case(SYS_CTR_EL0);
1393 	read_sysreg_case(SYS_DCZID_EL0);
1394 
1395 	default:
1396 		BUG();
1397 		return 0;
1398 	}
1399 
1400 	regp  = get_arm64_ftr_reg(sys_id);
1401 	if (regp) {
1402 		val &= ~regp->override->mask;
1403 		val |= (regp->override->val & regp->override->mask);
1404 	}
1405 
1406 	return val;
1407 }
1408 
1409 #include <linux/irqchip/arm-gic-v3.h>
1410 
1411 static bool
1412 has_always(const struct arm64_cpu_capabilities *entry, int scope)
1413 {
1414 	return true;
1415 }
1416 
1417 static bool
1418 feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
1419 {
1420 	int val = cpuid_feature_extract_field_width(reg, entry->field_pos,
1421 						    entry->field_width,
1422 						    entry->sign);
1423 
1424 	return val >= entry->min_field_value;
1425 }
1426 
1427 static u64
1428 read_scoped_sysreg(const struct arm64_cpu_capabilities *entry, int scope)
1429 {
1430 	WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
1431 	if (scope == SCOPE_SYSTEM)
1432 		return read_sanitised_ftr_reg(entry->sys_reg);
1433 	else
1434 		return __read_sysreg_by_encoding(entry->sys_reg);
1435 }
1436 
1437 static bool
1438 has_user_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
1439 {
1440 	int mask;
1441 	struct arm64_ftr_reg *regp;
1442 	u64 val = read_scoped_sysreg(entry, scope);
1443 
1444 	regp = get_arm64_ftr_reg(entry->sys_reg);
1445 	if (!regp)
1446 		return false;
1447 
1448 	mask = cpuid_feature_extract_unsigned_field_width(regp->user_mask,
1449 							  entry->field_pos,
1450 							  entry->field_width);
1451 	if (!mask)
1452 		return false;
1453 
1454 	return feature_matches(val, entry);
1455 }
1456 
1457 static bool
1458 has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
1459 {
1460 	u64 val = read_scoped_sysreg(entry, scope);
1461 	return feature_matches(val, entry);
1462 }
1463 
1464 const struct cpumask *system_32bit_el0_cpumask(void)
1465 {
1466 	if (!system_supports_32bit_el0())
1467 		return cpu_none_mask;
1468 
1469 	if (static_branch_unlikely(&arm64_mismatched_32bit_el0))
1470 		return cpu_32bit_el0_mask;
1471 
1472 	return cpu_possible_mask;
1473 }
1474 
1475 static int __init parse_32bit_el0_param(char *str)
1476 {
1477 	allow_mismatched_32bit_el0 = true;
1478 	return 0;
1479 }
1480 early_param("allow_mismatched_32bit_el0", parse_32bit_el0_param);
1481 
1482 static ssize_t aarch32_el0_show(struct device *dev,
1483 				struct device_attribute *attr, char *buf)
1484 {
1485 	const struct cpumask *mask = system_32bit_el0_cpumask();
1486 
1487 	return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(mask));
1488 }
1489 static const DEVICE_ATTR_RO(aarch32_el0);
1490 
1491 static int __init aarch32_el0_sysfs_init(void)
1492 {
1493 	if (!allow_mismatched_32bit_el0)
1494 		return 0;
1495 
1496 	return device_create_file(cpu_subsys.dev_root, &dev_attr_aarch32_el0);
1497 }
1498 device_initcall(aarch32_el0_sysfs_init);
1499 
1500 static bool has_32bit_el0(const struct arm64_cpu_capabilities *entry, int scope)
1501 {
1502 	if (!has_cpuid_feature(entry, scope))
1503 		return allow_mismatched_32bit_el0;
1504 
1505 	if (scope == SCOPE_SYSTEM)
1506 		pr_info("detected: 32-bit EL0 Support\n");
1507 
1508 	return true;
1509 }
1510 
1511 static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
1512 {
1513 	bool has_sre;
1514 
1515 	if (!has_cpuid_feature(entry, scope))
1516 		return false;
1517 
1518 	has_sre = gic_enable_sre();
1519 	if (!has_sre)
1520 		pr_warn_once("%s present but disabled by higher exception level\n",
1521 			     entry->desc);
1522 
1523 	return has_sre;
1524 }
1525 
1526 static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
1527 {
1528 	u32 midr = read_cpuid_id();
1529 
1530 	/* Cavium ThunderX pass 1.x and 2.x */
1531 	return midr_is_cpu_model_range(midr, MIDR_THUNDERX,
1532 		MIDR_CPU_VAR_REV(0, 0),
1533 		MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
1534 }
1535 
1536 static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
1537 {
1538 	u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
1539 
1540 	return cpuid_feature_extract_signed_field(pfr0,
1541 					ID_AA64PFR0_EL1_FP_SHIFT) < 0;
1542 }
1543 
1544 static bool has_cache_idc(const struct arm64_cpu_capabilities *entry,
1545 			  int scope)
1546 {
1547 	u64 ctr;
1548 
1549 	if (scope == SCOPE_SYSTEM)
1550 		ctr = arm64_ftr_reg_ctrel0.sys_val;
1551 	else
1552 		ctr = read_cpuid_effective_cachetype();
1553 
1554 	return ctr & BIT(CTR_EL0_IDC_SHIFT);
1555 }
1556 
1557 static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused)
1558 {
1559 	/*
1560 	 * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively
1561 	 * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses
1562 	 * to the CTR_EL0 on this CPU and emulate it with the real/safe
1563 	 * value.
1564 	 */
1565 	if (!(read_cpuid_cachetype() & BIT(CTR_EL0_IDC_SHIFT)))
1566 		sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
1567 }
1568 
1569 static bool has_cache_dic(const struct arm64_cpu_capabilities *entry,
1570 			  int scope)
1571 {
1572 	u64 ctr;
1573 
1574 	if (scope == SCOPE_SYSTEM)
1575 		ctr = arm64_ftr_reg_ctrel0.sys_val;
1576 	else
1577 		ctr = read_cpuid_cachetype();
1578 
1579 	return ctr & BIT(CTR_EL0_DIC_SHIFT);
1580 }
1581 
1582 static bool __maybe_unused
1583 has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
1584 {
1585 	/*
1586 	 * Kdump isn't guaranteed to power-off all secondary CPUs, CNP
1587 	 * may share TLB entries with a CPU stuck in the crashed
1588 	 * kernel.
1589 	 */
1590 	if (is_kdump_kernel())
1591 		return false;
1592 
1593 	if (cpus_have_const_cap(ARM64_WORKAROUND_NVIDIA_CARMEL_CNP))
1594 		return false;
1595 
1596 	return has_cpuid_feature(entry, scope);
1597 }
1598 
1599 /*
1600  * This check is triggered during the early boot before the cpufeature
1601  * is initialised. Checking the status on the local CPU allows the boot
1602  * CPU to detect the need for non-global mappings and thus avoiding a
1603  * pagetable re-write after all the CPUs are booted. This check will be
1604  * anyway run on individual CPUs, allowing us to get the consistent
1605  * state once the SMP CPUs are up and thus make the switch to non-global
1606  * mappings if required.
1607  */
1608 bool kaslr_requires_kpti(void)
1609 {
1610 	if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
1611 		return false;
1612 
1613 	/*
1614 	 * E0PD does a similar job to KPTI so can be used instead
1615 	 * where available.
1616 	 */
1617 	if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
1618 		u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
1619 		if (cpuid_feature_extract_unsigned_field(mmfr2,
1620 						ID_AA64MMFR2_EL1_E0PD_SHIFT))
1621 			return false;
1622 	}
1623 
1624 	/*
1625 	 * Systems affected by Cavium erratum 24756 are incompatible
1626 	 * with KPTI.
1627 	 */
1628 	if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
1629 		extern const struct midr_range cavium_erratum_27456_cpus[];
1630 
1631 		if (is_midr_in_range_list(read_cpuid_id(),
1632 					  cavium_erratum_27456_cpus))
1633 			return false;
1634 	}
1635 
1636 	return kaslr_offset() > 0;
1637 }
1638 
1639 static bool __meltdown_safe = true;
1640 static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
1641 
1642 static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
1643 				int scope)
1644 {
1645 	/* List of CPUs that are not vulnerable and don't need KPTI */
1646 	static const struct midr_range kpti_safe_list[] = {
1647 		MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
1648 		MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
1649 		MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
1650 		MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
1651 		MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
1652 		MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
1653 		MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
1654 		MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
1655 		MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
1656 		MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
1657 		MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL),
1658 		MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_GOLD),
1659 		MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_SILVER),
1660 		MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER),
1661 		MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER),
1662 		{ /* sentinel */ }
1663 	};
1664 	char const *str = "kpti command line option";
1665 	bool meltdown_safe;
1666 
1667 	meltdown_safe = is_midr_in_range_list(read_cpuid_id(), kpti_safe_list);
1668 
1669 	/* Defer to CPU feature registers */
1670 	if (has_cpuid_feature(entry, scope))
1671 		meltdown_safe = true;
1672 
1673 	if (!meltdown_safe)
1674 		__meltdown_safe = false;
1675 
1676 	/*
1677 	 * For reasons that aren't entirely clear, enabling KPTI on Cavium
1678 	 * ThunderX leads to apparent I-cache corruption of kernel text, which
1679 	 * ends as well as you might imagine. Don't even try. We cannot rely
1680 	 * on the cpus_have_*cap() helpers here to detect the CPU erratum
1681 	 * because cpucap detection order may change. However, since we know
1682 	 * affected CPUs are always in a homogeneous configuration, it is
1683 	 * safe to rely on this_cpu_has_cap() here.
1684 	 */
1685 	if (this_cpu_has_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
1686 		str = "ARM64_WORKAROUND_CAVIUM_27456";
1687 		__kpti_forced = -1;
1688 	}
1689 
1690 	/* Useful for KASLR robustness */
1691 	if (kaslr_requires_kpti()) {
1692 		if (!__kpti_forced) {
1693 			str = "KASLR";
1694 			__kpti_forced = 1;
1695 		}
1696 	}
1697 
1698 	if (cpu_mitigations_off() && !__kpti_forced) {
1699 		str = "mitigations=off";
1700 		__kpti_forced = -1;
1701 	}
1702 
1703 	if (!IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) {
1704 		pr_info_once("kernel page table isolation disabled by kernel configuration\n");
1705 		return false;
1706 	}
1707 
1708 	/* Forced? */
1709 	if (__kpti_forced) {
1710 		pr_info_once("kernel page table isolation forced %s by %s\n",
1711 			     __kpti_forced > 0 ? "ON" : "OFF", str);
1712 		return __kpti_forced > 0;
1713 	}
1714 
1715 	return !meltdown_safe;
1716 }
1717 
1718 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
1719 #define KPTI_NG_TEMP_VA		(-(1UL << PMD_SHIFT))
1720 
1721 extern
1722 void create_kpti_ng_temp_pgd(pgd_t *pgdir, phys_addr_t phys, unsigned long virt,
1723 			     phys_addr_t size, pgprot_t prot,
1724 			     phys_addr_t (*pgtable_alloc)(int), int flags);
1725 
1726 static phys_addr_t kpti_ng_temp_alloc;
1727 
1728 static phys_addr_t kpti_ng_pgd_alloc(int shift)
1729 {
1730 	kpti_ng_temp_alloc -= PAGE_SIZE;
1731 	return kpti_ng_temp_alloc;
1732 }
1733 
1734 static void
1735 kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
1736 {
1737 	typedef void (kpti_remap_fn)(int, int, phys_addr_t, unsigned long);
1738 	extern kpti_remap_fn idmap_kpti_install_ng_mappings;
1739 	kpti_remap_fn *remap_fn;
1740 
1741 	int cpu = smp_processor_id();
1742 	int levels = CONFIG_PGTABLE_LEVELS;
1743 	int order = order_base_2(levels);
1744 	u64 kpti_ng_temp_pgd_pa = 0;
1745 	pgd_t *kpti_ng_temp_pgd;
1746 	u64 alloc = 0;
1747 
1748 	if (__this_cpu_read(this_cpu_vector) == vectors) {
1749 		const char *v = arm64_get_bp_hardening_vector(EL1_VECTOR_KPTI);
1750 
1751 		__this_cpu_write(this_cpu_vector, v);
1752 	}
1753 
1754 	/*
1755 	 * We don't need to rewrite the page-tables if either we've done
1756 	 * it already or we have KASLR enabled and therefore have not
1757 	 * created any global mappings at all.
1758 	 */
1759 	if (arm64_use_ng_mappings)
1760 		return;
1761 
1762 	remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
1763 
1764 	if (!cpu) {
1765 		alloc = __get_free_pages(GFP_ATOMIC | __GFP_ZERO, order);
1766 		kpti_ng_temp_pgd = (pgd_t *)(alloc + (levels - 1) * PAGE_SIZE);
1767 		kpti_ng_temp_alloc = kpti_ng_temp_pgd_pa = __pa(kpti_ng_temp_pgd);
1768 
1769 		//
1770 		// Create a minimal page table hierarchy that permits us to map
1771 		// the swapper page tables temporarily as we traverse them.
1772 		//
1773 		// The physical pages are laid out as follows:
1774 		//
1775 		// +--------+-/-------+-/------ +-\\--------+
1776 		// :  PTE[] : | PMD[] : | PUD[] : || PGD[]  :
1777 		// +--------+-\-------+-\------ +-//--------+
1778 		//      ^
1779 		// The first page is mapped into this hierarchy at a PMD_SHIFT
1780 		// aligned virtual address, so that we can manipulate the PTE
1781 		// level entries while the mapping is active. The first entry
1782 		// covers the PTE[] page itself, the remaining entries are free
1783 		// to be used as a ad-hoc fixmap.
1784 		//
1785 		create_kpti_ng_temp_pgd(kpti_ng_temp_pgd, __pa(alloc),
1786 					KPTI_NG_TEMP_VA, PAGE_SIZE, PAGE_KERNEL,
1787 					kpti_ng_pgd_alloc, 0);
1788 	}
1789 
1790 	cpu_install_idmap();
1791 	remap_fn(cpu, num_online_cpus(), kpti_ng_temp_pgd_pa, KPTI_NG_TEMP_VA);
1792 	cpu_uninstall_idmap();
1793 
1794 	if (!cpu) {
1795 		free_pages(alloc, order);
1796 		arm64_use_ng_mappings = true;
1797 	}
1798 }
1799 #else
1800 static void
1801 kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
1802 {
1803 }
1804 #endif	/* CONFIG_UNMAP_KERNEL_AT_EL0 */
1805 
1806 static int __init parse_kpti(char *str)
1807 {
1808 	bool enabled;
1809 	int ret = kstrtobool(str, &enabled);
1810 
1811 	if (ret)
1812 		return ret;
1813 
1814 	__kpti_forced = enabled ? 1 : -1;
1815 	return 0;
1816 }
1817 early_param("kpti", parse_kpti);
1818 
1819 #ifdef CONFIG_ARM64_HW_AFDBM
1820 static inline void __cpu_enable_hw_dbm(void)
1821 {
1822 	u64 tcr = read_sysreg(tcr_el1) | TCR_HD;
1823 
1824 	write_sysreg(tcr, tcr_el1);
1825 	isb();
1826 	local_flush_tlb_all();
1827 }
1828 
1829 static bool cpu_has_broken_dbm(void)
1830 {
1831 	/* List of CPUs which have broken DBM support. */
1832 	static const struct midr_range cpus[] = {
1833 #ifdef CONFIG_ARM64_ERRATUM_1024718
1834 		MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
1835 		/* Kryo4xx Silver (rdpe => r1p0) */
1836 		MIDR_REV(MIDR_QCOM_KRYO_4XX_SILVER, 0xd, 0xe),
1837 #endif
1838 #ifdef CONFIG_ARM64_ERRATUM_2051678
1839 		MIDR_REV_RANGE(MIDR_CORTEX_A510, 0, 0, 2),
1840 #endif
1841 		{},
1842 	};
1843 
1844 	return is_midr_in_range_list(read_cpuid_id(), cpus);
1845 }
1846 
1847 static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap)
1848 {
1849 	return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) &&
1850 	       !cpu_has_broken_dbm();
1851 }
1852 
1853 static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap)
1854 {
1855 	if (cpu_can_use_dbm(cap))
1856 		__cpu_enable_hw_dbm();
1857 }
1858 
1859 static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap,
1860 		       int __unused)
1861 {
1862 	static bool detected = false;
1863 	/*
1864 	 * DBM is a non-conflicting feature. i.e, the kernel can safely
1865 	 * run a mix of CPUs with and without the feature. So, we
1866 	 * unconditionally enable the capability to allow any late CPU
1867 	 * to use the feature. We only enable the control bits on the
1868 	 * CPU, if it actually supports.
1869 	 *
1870 	 * We have to make sure we print the "feature" detection only
1871 	 * when at least one CPU actually uses it. So check if this CPU
1872 	 * can actually use it and print the message exactly once.
1873 	 *
1874 	 * This is safe as all CPUs (including secondary CPUs - due to the
1875 	 * LOCAL_CPU scope - and the hotplugged CPUs - via verification)
1876 	 * goes through the "matches" check exactly once. Also if a CPU
1877 	 * matches the criteria, it is guaranteed that the CPU will turn
1878 	 * the DBM on, as the capability is unconditionally enabled.
1879 	 */
1880 	if (!detected && cpu_can_use_dbm(cap)) {
1881 		detected = true;
1882 		pr_info("detected: Hardware dirty bit management\n");
1883 	}
1884 
1885 	return true;
1886 }
1887 
1888 #endif
1889 
1890 #ifdef CONFIG_ARM64_AMU_EXTN
1891 
1892 /*
1893  * The "amu_cpus" cpumask only signals that the CPU implementation for the
1894  * flagged CPUs supports the Activity Monitors Unit (AMU) but does not provide
1895  * information regarding all the events that it supports. When a CPU bit is
1896  * set in the cpumask, the user of this feature can only rely on the presence
1897  * of the 4 fixed counters for that CPU. But this does not guarantee that the
1898  * counters are enabled or access to these counters is enabled by code
1899  * executed at higher exception levels (firmware).
1900  */
1901 static struct cpumask amu_cpus __read_mostly;
1902 
1903 bool cpu_has_amu_feat(int cpu)
1904 {
1905 	return cpumask_test_cpu(cpu, &amu_cpus);
1906 }
1907 
1908 int get_cpu_with_amu_feat(void)
1909 {
1910 	return cpumask_any(&amu_cpus);
1911 }
1912 
1913 static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap)
1914 {
1915 	if (has_cpuid_feature(cap, SCOPE_LOCAL_CPU)) {
1916 		pr_info("detected CPU%d: Activity Monitors Unit (AMU)\n",
1917 			smp_processor_id());
1918 		cpumask_set_cpu(smp_processor_id(), &amu_cpus);
1919 
1920 		/* 0 reference values signal broken/disabled counters */
1921 		if (!this_cpu_has_cap(ARM64_WORKAROUND_2457168))
1922 			update_freq_counters_refs();
1923 	}
1924 }
1925 
1926 static bool has_amu(const struct arm64_cpu_capabilities *cap,
1927 		    int __unused)
1928 {
1929 	/*
1930 	 * The AMU extension is a non-conflicting feature: the kernel can
1931 	 * safely run a mix of CPUs with and without support for the
1932 	 * activity monitors extension. Therefore, unconditionally enable
1933 	 * the capability to allow any late CPU to use the feature.
1934 	 *
1935 	 * With this feature unconditionally enabled, the cpu_enable
1936 	 * function will be called for all CPUs that match the criteria,
1937 	 * including secondary and hotplugged, marking this feature as
1938 	 * present on that respective CPU. The enable function will also
1939 	 * print a detection message.
1940 	 */
1941 
1942 	return true;
1943 }
1944 #else
1945 int get_cpu_with_amu_feat(void)
1946 {
1947 	return nr_cpu_ids;
1948 }
1949 #endif
1950 
1951 static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
1952 {
1953 	return is_kernel_in_hyp_mode();
1954 }
1955 
1956 static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
1957 {
1958 	/*
1959 	 * Copy register values that aren't redirected by hardware.
1960 	 *
1961 	 * Before code patching, we only set tpidr_el1, all CPUs need to copy
1962 	 * this value to tpidr_el2 before we patch the code. Once we've done
1963 	 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
1964 	 * do anything here.
1965 	 */
1966 	if (!alternative_is_applied(ARM64_HAS_VIRT_HOST_EXTN))
1967 		write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
1968 }
1969 
1970 #ifdef CONFIG_ARM64_PAN
1971 static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
1972 {
1973 	/*
1974 	 * We modify PSTATE. This won't work from irq context as the PSTATE
1975 	 * is discarded once we return from the exception.
1976 	 */
1977 	WARN_ON_ONCE(in_interrupt());
1978 
1979 	sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
1980 	set_pstate_pan(1);
1981 }
1982 #endif /* CONFIG_ARM64_PAN */
1983 
1984 #ifdef CONFIG_ARM64_RAS_EXTN
1985 static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
1986 {
1987 	/* Firmware may have left a deferred SError in this register. */
1988 	write_sysreg_s(0, SYS_DISR_EL1);
1989 }
1990 #endif /* CONFIG_ARM64_RAS_EXTN */
1991 
1992 #ifdef CONFIG_ARM64_PTR_AUTH
1993 static bool has_address_auth_cpucap(const struct arm64_cpu_capabilities *entry, int scope)
1994 {
1995 	int boot_val, sec_val;
1996 
1997 	/* We don't expect to be called with SCOPE_SYSTEM */
1998 	WARN_ON(scope == SCOPE_SYSTEM);
1999 	/*
2000 	 * The ptr-auth feature levels are not intercompatible with lower
2001 	 * levels. Hence we must match ptr-auth feature level of the secondary
2002 	 * CPUs with that of the boot CPU. The level of boot cpu is fetched
2003 	 * from the sanitised register whereas direct register read is done for
2004 	 * the secondary CPUs.
2005 	 * The sanitised feature state is guaranteed to match that of the
2006 	 * boot CPU as a mismatched secondary CPU is parked before it gets
2007 	 * a chance to update the state, with the capability.
2008 	 */
2009 	boot_val = cpuid_feature_extract_field(read_sanitised_ftr_reg(entry->sys_reg),
2010 					       entry->field_pos, entry->sign);
2011 	if (scope & SCOPE_BOOT_CPU)
2012 		return boot_val >= entry->min_field_value;
2013 	/* Now check for the secondary CPUs with SCOPE_LOCAL_CPU scope */
2014 	sec_val = cpuid_feature_extract_field(__read_sysreg_by_encoding(entry->sys_reg),
2015 					      entry->field_pos, entry->sign);
2016 	return (sec_val >= entry->min_field_value) && (sec_val == boot_val);
2017 }
2018 
2019 static bool has_address_auth_metacap(const struct arm64_cpu_capabilities *entry,
2020 				     int scope)
2021 {
2022 	bool api = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_IMP_DEF], scope);
2023 	bool apa = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA5], scope);
2024 	bool apa3 = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA3], scope);
2025 
2026 	return apa || apa3 || api;
2027 }
2028 
2029 static bool has_generic_auth(const struct arm64_cpu_capabilities *entry,
2030 			     int __unused)
2031 {
2032 	bool gpi = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_IMP_DEF);
2033 	bool gpa = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH_QARMA5);
2034 	bool gpa3 = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH_QARMA3);
2035 
2036 	return gpa || gpa3 || gpi;
2037 }
2038 #endif /* CONFIG_ARM64_PTR_AUTH */
2039 
2040 #ifdef CONFIG_ARM64_E0PD
2041 static void cpu_enable_e0pd(struct arm64_cpu_capabilities const *cap)
2042 {
2043 	if (this_cpu_has_cap(ARM64_HAS_E0PD))
2044 		sysreg_clear_set(tcr_el1, 0, TCR_E0PD1);
2045 }
2046 #endif /* CONFIG_ARM64_E0PD */
2047 
2048 #ifdef CONFIG_ARM64_PSEUDO_NMI
2049 static bool enable_pseudo_nmi;
2050 
2051 static int __init early_enable_pseudo_nmi(char *p)
2052 {
2053 	return kstrtobool(p, &enable_pseudo_nmi);
2054 }
2055 early_param("irqchip.gicv3_pseudo_nmi", early_enable_pseudo_nmi);
2056 
2057 static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry,
2058 				   int scope)
2059 {
2060 	/*
2061 	 * ARM64_HAS_GIC_CPUIF_SYSREGS has a lower index, and is a boot CPU
2062 	 * feature, so will be detected earlier.
2063 	 */
2064 	BUILD_BUG_ON(ARM64_HAS_GIC_PRIO_MASKING <= ARM64_HAS_GIC_CPUIF_SYSREGS);
2065 	if (!cpus_have_cap(ARM64_HAS_GIC_CPUIF_SYSREGS))
2066 		return false;
2067 
2068 	return enable_pseudo_nmi;
2069 }
2070 
2071 static bool has_gic_prio_relaxed_sync(const struct arm64_cpu_capabilities *entry,
2072 				      int scope)
2073 {
2074 	/*
2075 	 * If we're not using priority masking then we won't be poking PMR_EL1,
2076 	 * and there's no need to relax synchronization of writes to it, and
2077 	 * ICC_CTLR_EL1 might not be accessible and we must avoid reads from
2078 	 * that.
2079 	 *
2080 	 * ARM64_HAS_GIC_PRIO_MASKING has a lower index, and is a boot CPU
2081 	 * feature, so will be detected earlier.
2082 	 */
2083 	BUILD_BUG_ON(ARM64_HAS_GIC_PRIO_RELAXED_SYNC <= ARM64_HAS_GIC_PRIO_MASKING);
2084 	if (!cpus_have_cap(ARM64_HAS_GIC_PRIO_MASKING))
2085 		return false;
2086 
2087 	/*
2088 	 * When Priority Mask Hint Enable (PMHE) == 0b0, PMR is not used as a
2089 	 * hint for interrupt distribution, a DSB is not necessary when
2090 	 * unmasking IRQs via PMR, and we can relax the barrier to a NOP.
2091 	 *
2092 	 * Linux itself doesn't use 1:N distribution, so has no need to
2093 	 * set PMHE. The only reason to have it set is if EL3 requires it
2094 	 * (and we can't change it).
2095 	 */
2096 	return (gic_read_ctlr() & ICC_CTLR_EL1_PMHE_MASK) == 0;
2097 }
2098 #endif
2099 
2100 #ifdef CONFIG_ARM64_BTI
2101 static void bti_enable(const struct arm64_cpu_capabilities *__unused)
2102 {
2103 	/*
2104 	 * Use of X16/X17 for tail-calls and trampolines that jump to
2105 	 * function entry points using BR is a requirement for
2106 	 * marking binaries with GNU_PROPERTY_AARCH64_FEATURE_1_BTI.
2107 	 * So, be strict and forbid other BRs using other registers to
2108 	 * jump onto a PACIxSP instruction:
2109 	 */
2110 	sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_BT0 | SCTLR_EL1_BT1);
2111 	isb();
2112 }
2113 #endif /* CONFIG_ARM64_BTI */
2114 
2115 #ifdef CONFIG_ARM64_MTE
2116 static void cpu_enable_mte(struct arm64_cpu_capabilities const *cap)
2117 {
2118 	sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_ATA | SCTLR_EL1_ATA0);
2119 
2120 	mte_cpu_setup();
2121 
2122 	/*
2123 	 * Clear the tags in the zero page. This needs to be done via the
2124 	 * linear map which has the Tagged attribute.
2125 	 */
2126 	if (try_page_mte_tagging(ZERO_PAGE(0))) {
2127 		mte_clear_page_tags(lm_alias(empty_zero_page));
2128 		set_page_mte_tagged(ZERO_PAGE(0));
2129 	}
2130 
2131 	kasan_init_hw_tags_cpu();
2132 }
2133 #endif /* CONFIG_ARM64_MTE */
2134 
2135 static void elf_hwcap_fixup(void)
2136 {
2137 #ifdef CONFIG_ARM64_ERRATUM_1742098
2138 	if (cpus_have_const_cap(ARM64_WORKAROUND_1742098))
2139 		compat_elf_hwcap2 &= ~COMPAT_HWCAP2_AES;
2140 #endif /* ARM64_ERRATUM_1742098 */
2141 }
2142 
2143 #ifdef CONFIG_KVM
2144 static bool is_kvm_protected_mode(const struct arm64_cpu_capabilities *entry, int __unused)
2145 {
2146 	return kvm_get_mode() == KVM_MODE_PROTECTED;
2147 }
2148 #endif /* CONFIG_KVM */
2149 
2150 static void cpu_trap_el0_impdef(const struct arm64_cpu_capabilities *__unused)
2151 {
2152 	sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_TIDCP);
2153 }
2154 
2155 static void cpu_enable_dit(const struct arm64_cpu_capabilities *__unused)
2156 {
2157 	set_pstate_dit(1);
2158 }
2159 
2160 /* Internal helper functions to match cpu capability type */
2161 static bool
2162 cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap)
2163 {
2164 	return !!(cap->type & ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU);
2165 }
2166 
2167 static bool
2168 cpucap_late_cpu_permitted(const struct arm64_cpu_capabilities *cap)
2169 {
2170 	return !!(cap->type & ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU);
2171 }
2172 
2173 static bool
2174 cpucap_panic_on_conflict(const struct arm64_cpu_capabilities *cap)
2175 {
2176 	return !!(cap->type & ARM64_CPUCAP_PANIC_ON_CONFLICT);
2177 }
2178 
2179 static const struct arm64_cpu_capabilities arm64_features[] = {
2180 	{
2181 		.capability = ARM64_ALWAYS_BOOT,
2182 		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
2183 		.matches = has_always,
2184 	},
2185 	{
2186 		.capability = ARM64_ALWAYS_SYSTEM,
2187 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2188 		.matches = has_always,
2189 	},
2190 	{
2191 		.desc = "GIC system register CPU interface",
2192 		.capability = ARM64_HAS_GIC_CPUIF_SYSREGS,
2193 		.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
2194 		.matches = has_useable_gicv3_cpuif,
2195 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2196 		.field_pos = ID_AA64PFR0_EL1_GIC_SHIFT,
2197 		.field_width = 4,
2198 		.sign = FTR_UNSIGNED,
2199 		.min_field_value = 1,
2200 	},
2201 	{
2202 		.desc = "Enhanced Counter Virtualization",
2203 		.capability = ARM64_HAS_ECV,
2204 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2205 		.matches = has_cpuid_feature,
2206 		.sys_reg = SYS_ID_AA64MMFR0_EL1,
2207 		.field_pos = ID_AA64MMFR0_EL1_ECV_SHIFT,
2208 		.field_width = 4,
2209 		.sign = FTR_UNSIGNED,
2210 		.min_field_value = 1,
2211 	},
2212 #ifdef CONFIG_ARM64_PAN
2213 	{
2214 		.desc = "Privileged Access Never",
2215 		.capability = ARM64_HAS_PAN,
2216 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2217 		.matches = has_cpuid_feature,
2218 		.sys_reg = SYS_ID_AA64MMFR1_EL1,
2219 		.field_pos = ID_AA64MMFR1_EL1_PAN_SHIFT,
2220 		.field_width = 4,
2221 		.sign = FTR_UNSIGNED,
2222 		.min_field_value = 1,
2223 		.cpu_enable = cpu_enable_pan,
2224 	},
2225 #endif /* CONFIG_ARM64_PAN */
2226 #ifdef CONFIG_ARM64_EPAN
2227 	{
2228 		.desc = "Enhanced Privileged Access Never",
2229 		.capability = ARM64_HAS_EPAN,
2230 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2231 		.matches = has_cpuid_feature,
2232 		.sys_reg = SYS_ID_AA64MMFR1_EL1,
2233 		.field_pos = ID_AA64MMFR1_EL1_PAN_SHIFT,
2234 		.field_width = 4,
2235 		.sign = FTR_UNSIGNED,
2236 		.min_field_value = 3,
2237 	},
2238 #endif /* CONFIG_ARM64_EPAN */
2239 #ifdef CONFIG_ARM64_LSE_ATOMICS
2240 	{
2241 		.desc = "LSE atomic instructions",
2242 		.capability = ARM64_HAS_LSE_ATOMICS,
2243 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2244 		.matches = has_cpuid_feature,
2245 		.sys_reg = SYS_ID_AA64ISAR0_EL1,
2246 		.field_pos = ID_AA64ISAR0_EL1_ATOMIC_SHIFT,
2247 		.field_width = 4,
2248 		.sign = FTR_UNSIGNED,
2249 		.min_field_value = 2,
2250 	},
2251 #endif /* CONFIG_ARM64_LSE_ATOMICS */
2252 	{
2253 		.desc = "Software prefetching using PRFM",
2254 		.capability = ARM64_HAS_NO_HW_PREFETCH,
2255 		.type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
2256 		.matches = has_no_hw_prefetch,
2257 	},
2258 	{
2259 		.desc = "Virtualization Host Extensions",
2260 		.capability = ARM64_HAS_VIRT_HOST_EXTN,
2261 		.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
2262 		.matches = runs_at_el2,
2263 		.cpu_enable = cpu_copy_el2regs,
2264 	},
2265 	{
2266 		.capability = ARM64_HAS_32BIT_EL0_DO_NOT_USE,
2267 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2268 		.matches = has_32bit_el0,
2269 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2270 		.sign = FTR_UNSIGNED,
2271 		.field_pos = ID_AA64PFR0_EL1_EL0_SHIFT,
2272 		.field_width = 4,
2273 		.min_field_value = ID_AA64PFR0_EL1_ELx_32BIT_64BIT,
2274 	},
2275 #ifdef CONFIG_KVM
2276 	{
2277 		.desc = "32-bit EL1 Support",
2278 		.capability = ARM64_HAS_32BIT_EL1,
2279 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2280 		.matches = has_cpuid_feature,
2281 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2282 		.sign = FTR_UNSIGNED,
2283 		.field_pos = ID_AA64PFR0_EL1_EL1_SHIFT,
2284 		.field_width = 4,
2285 		.min_field_value = ID_AA64PFR0_EL1_ELx_32BIT_64BIT,
2286 	},
2287 	{
2288 		.desc = "Protected KVM",
2289 		.capability = ARM64_KVM_PROTECTED_MODE,
2290 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2291 		.matches = is_kvm_protected_mode,
2292 	},
2293 #endif
2294 	{
2295 		.desc = "Kernel page table isolation (KPTI)",
2296 		.capability = ARM64_UNMAP_KERNEL_AT_EL0,
2297 		.type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
2298 		/*
2299 		 * The ID feature fields below are used to indicate that
2300 		 * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for
2301 		 * more details.
2302 		 */
2303 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2304 		.field_pos = ID_AA64PFR0_EL1_CSV3_SHIFT,
2305 		.field_width = 4,
2306 		.min_field_value = 1,
2307 		.matches = unmap_kernel_at_el0,
2308 		.cpu_enable = kpti_install_ng_mappings,
2309 	},
2310 	{
2311 		/* FP/SIMD is not implemented */
2312 		.capability = ARM64_HAS_NO_FPSIMD,
2313 		.type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
2314 		.min_field_value = 0,
2315 		.matches = has_no_fpsimd,
2316 	},
2317 #ifdef CONFIG_ARM64_PMEM
2318 	{
2319 		.desc = "Data cache clean to Point of Persistence",
2320 		.capability = ARM64_HAS_DCPOP,
2321 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2322 		.matches = has_cpuid_feature,
2323 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2324 		.field_pos = ID_AA64ISAR1_EL1_DPB_SHIFT,
2325 		.field_width = 4,
2326 		.min_field_value = 1,
2327 	},
2328 	{
2329 		.desc = "Data cache clean to Point of Deep Persistence",
2330 		.capability = ARM64_HAS_DCPODP,
2331 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2332 		.matches = has_cpuid_feature,
2333 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2334 		.sign = FTR_UNSIGNED,
2335 		.field_pos = ID_AA64ISAR1_EL1_DPB_SHIFT,
2336 		.field_width = 4,
2337 		.min_field_value = 2,
2338 	},
2339 #endif
2340 #ifdef CONFIG_ARM64_SVE
2341 	{
2342 		.desc = "Scalable Vector Extension",
2343 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2344 		.capability = ARM64_SVE,
2345 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2346 		.sign = FTR_UNSIGNED,
2347 		.field_pos = ID_AA64PFR0_EL1_SVE_SHIFT,
2348 		.field_width = 4,
2349 		.min_field_value = ID_AA64PFR0_EL1_SVE_IMP,
2350 		.matches = has_cpuid_feature,
2351 		.cpu_enable = sve_kernel_enable,
2352 	},
2353 #endif /* CONFIG_ARM64_SVE */
2354 #ifdef CONFIG_ARM64_RAS_EXTN
2355 	{
2356 		.desc = "RAS Extension Support",
2357 		.capability = ARM64_HAS_RAS_EXTN,
2358 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2359 		.matches = has_cpuid_feature,
2360 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2361 		.sign = FTR_UNSIGNED,
2362 		.field_pos = ID_AA64PFR0_EL1_RAS_SHIFT,
2363 		.field_width = 4,
2364 		.min_field_value = ID_AA64PFR0_EL1_RAS_IMP,
2365 		.cpu_enable = cpu_clear_disr,
2366 	},
2367 #endif /* CONFIG_ARM64_RAS_EXTN */
2368 #ifdef CONFIG_ARM64_AMU_EXTN
2369 	{
2370 		/*
2371 		 * The feature is enabled by default if CONFIG_ARM64_AMU_EXTN=y.
2372 		 * Therefore, don't provide .desc as we don't want the detection
2373 		 * message to be shown until at least one CPU is detected to
2374 		 * support the feature.
2375 		 */
2376 		.capability = ARM64_HAS_AMU_EXTN,
2377 		.type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
2378 		.matches = has_amu,
2379 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2380 		.sign = FTR_UNSIGNED,
2381 		.field_pos = ID_AA64PFR0_EL1_AMU_SHIFT,
2382 		.field_width = 4,
2383 		.min_field_value = ID_AA64PFR0_EL1_AMU_IMP,
2384 		.cpu_enable = cpu_amu_enable,
2385 	},
2386 #endif /* CONFIG_ARM64_AMU_EXTN */
2387 	{
2388 		.desc = "Data cache clean to the PoU not required for I/D coherence",
2389 		.capability = ARM64_HAS_CACHE_IDC,
2390 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2391 		.matches = has_cache_idc,
2392 		.cpu_enable = cpu_emulate_effective_ctr,
2393 	},
2394 	{
2395 		.desc = "Instruction cache invalidation not required for I/D coherence",
2396 		.capability = ARM64_HAS_CACHE_DIC,
2397 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2398 		.matches = has_cache_dic,
2399 	},
2400 	{
2401 		.desc = "Stage-2 Force Write-Back",
2402 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2403 		.capability = ARM64_HAS_STAGE2_FWB,
2404 		.sys_reg = SYS_ID_AA64MMFR2_EL1,
2405 		.sign = FTR_UNSIGNED,
2406 		.field_pos = ID_AA64MMFR2_EL1_FWB_SHIFT,
2407 		.field_width = 4,
2408 		.min_field_value = 1,
2409 		.matches = has_cpuid_feature,
2410 	},
2411 	{
2412 		.desc = "ARMv8.4 Translation Table Level",
2413 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2414 		.capability = ARM64_HAS_ARMv8_4_TTL,
2415 		.sys_reg = SYS_ID_AA64MMFR2_EL1,
2416 		.sign = FTR_UNSIGNED,
2417 		.field_pos = ID_AA64MMFR2_EL1_TTL_SHIFT,
2418 		.field_width = 4,
2419 		.min_field_value = 1,
2420 		.matches = has_cpuid_feature,
2421 	},
2422 	{
2423 		.desc = "TLB range maintenance instructions",
2424 		.capability = ARM64_HAS_TLB_RANGE,
2425 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2426 		.matches = has_cpuid_feature,
2427 		.sys_reg = SYS_ID_AA64ISAR0_EL1,
2428 		.field_pos = ID_AA64ISAR0_EL1_TLB_SHIFT,
2429 		.field_width = 4,
2430 		.sign = FTR_UNSIGNED,
2431 		.min_field_value = ID_AA64ISAR0_EL1_TLB_RANGE,
2432 	},
2433 #ifdef CONFIG_ARM64_HW_AFDBM
2434 	{
2435 		/*
2436 		 * Since we turn this on always, we don't want the user to
2437 		 * think that the feature is available when it may not be.
2438 		 * So hide the description.
2439 		 *
2440 		 * .desc = "Hardware pagetable Dirty Bit Management",
2441 		 *
2442 		 */
2443 		.type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
2444 		.capability = ARM64_HW_DBM,
2445 		.sys_reg = SYS_ID_AA64MMFR1_EL1,
2446 		.sign = FTR_UNSIGNED,
2447 		.field_pos = ID_AA64MMFR1_EL1_HAFDBS_SHIFT,
2448 		.field_width = 4,
2449 		.min_field_value = 2,
2450 		.matches = has_hw_dbm,
2451 		.cpu_enable = cpu_enable_hw_dbm,
2452 	},
2453 #endif
2454 	{
2455 		.desc = "CRC32 instructions",
2456 		.capability = ARM64_HAS_CRC32,
2457 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2458 		.matches = has_cpuid_feature,
2459 		.sys_reg = SYS_ID_AA64ISAR0_EL1,
2460 		.field_pos = ID_AA64ISAR0_EL1_CRC32_SHIFT,
2461 		.field_width = 4,
2462 		.min_field_value = 1,
2463 	},
2464 	{
2465 		.desc = "Speculative Store Bypassing Safe (SSBS)",
2466 		.capability = ARM64_SSBS,
2467 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2468 		.matches = has_cpuid_feature,
2469 		.sys_reg = SYS_ID_AA64PFR1_EL1,
2470 		.field_pos = ID_AA64PFR1_EL1_SSBS_SHIFT,
2471 		.field_width = 4,
2472 		.sign = FTR_UNSIGNED,
2473 		.min_field_value = ID_AA64PFR1_EL1_SSBS_IMP,
2474 	},
2475 #ifdef CONFIG_ARM64_CNP
2476 	{
2477 		.desc = "Common not Private translations",
2478 		.capability = ARM64_HAS_CNP,
2479 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2480 		.matches = has_useable_cnp,
2481 		.sys_reg = SYS_ID_AA64MMFR2_EL1,
2482 		.sign = FTR_UNSIGNED,
2483 		.field_pos = ID_AA64MMFR2_EL1_CnP_SHIFT,
2484 		.field_width = 4,
2485 		.min_field_value = 1,
2486 		.cpu_enable = cpu_enable_cnp,
2487 	},
2488 #endif
2489 	{
2490 		.desc = "Speculation barrier (SB)",
2491 		.capability = ARM64_HAS_SB,
2492 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2493 		.matches = has_cpuid_feature,
2494 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2495 		.field_pos = ID_AA64ISAR1_EL1_SB_SHIFT,
2496 		.field_width = 4,
2497 		.sign = FTR_UNSIGNED,
2498 		.min_field_value = 1,
2499 	},
2500 #ifdef CONFIG_ARM64_PTR_AUTH
2501 	{
2502 		.desc = "Address authentication (architected QARMA5 algorithm)",
2503 		.capability = ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA5,
2504 		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
2505 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2506 		.sign = FTR_UNSIGNED,
2507 		.field_pos = ID_AA64ISAR1_EL1_APA_SHIFT,
2508 		.field_width = 4,
2509 		.min_field_value = ID_AA64ISAR1_EL1_APA_PAuth,
2510 		.matches = has_address_auth_cpucap,
2511 	},
2512 	{
2513 		.desc = "Address authentication (architected QARMA3 algorithm)",
2514 		.capability = ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA3,
2515 		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
2516 		.sys_reg = SYS_ID_AA64ISAR2_EL1,
2517 		.sign = FTR_UNSIGNED,
2518 		.field_pos = ID_AA64ISAR2_EL1_APA3_SHIFT,
2519 		.field_width = 4,
2520 		.min_field_value = ID_AA64ISAR2_EL1_APA3_PAuth,
2521 		.matches = has_address_auth_cpucap,
2522 	},
2523 	{
2524 		.desc = "Address authentication (IMP DEF algorithm)",
2525 		.capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF,
2526 		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
2527 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2528 		.sign = FTR_UNSIGNED,
2529 		.field_pos = ID_AA64ISAR1_EL1_API_SHIFT,
2530 		.field_width = 4,
2531 		.min_field_value = ID_AA64ISAR1_EL1_API_PAuth,
2532 		.matches = has_address_auth_cpucap,
2533 	},
2534 	{
2535 		.capability = ARM64_HAS_ADDRESS_AUTH,
2536 		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
2537 		.matches = has_address_auth_metacap,
2538 	},
2539 	{
2540 		.desc = "Generic authentication (architected QARMA5 algorithm)",
2541 		.capability = ARM64_HAS_GENERIC_AUTH_ARCH_QARMA5,
2542 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2543 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2544 		.sign = FTR_UNSIGNED,
2545 		.field_pos = ID_AA64ISAR1_EL1_GPA_SHIFT,
2546 		.field_width = 4,
2547 		.min_field_value = ID_AA64ISAR1_EL1_GPA_IMP,
2548 		.matches = has_cpuid_feature,
2549 	},
2550 	{
2551 		.desc = "Generic authentication (architected QARMA3 algorithm)",
2552 		.capability = ARM64_HAS_GENERIC_AUTH_ARCH_QARMA3,
2553 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2554 		.sys_reg = SYS_ID_AA64ISAR2_EL1,
2555 		.sign = FTR_UNSIGNED,
2556 		.field_pos = ID_AA64ISAR2_EL1_GPA3_SHIFT,
2557 		.field_width = 4,
2558 		.min_field_value = ID_AA64ISAR2_EL1_GPA3_IMP,
2559 		.matches = has_cpuid_feature,
2560 	},
2561 	{
2562 		.desc = "Generic authentication (IMP DEF algorithm)",
2563 		.capability = ARM64_HAS_GENERIC_AUTH_IMP_DEF,
2564 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2565 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2566 		.sign = FTR_UNSIGNED,
2567 		.field_pos = ID_AA64ISAR1_EL1_GPI_SHIFT,
2568 		.field_width = 4,
2569 		.min_field_value = ID_AA64ISAR1_EL1_GPI_IMP,
2570 		.matches = has_cpuid_feature,
2571 	},
2572 	{
2573 		.capability = ARM64_HAS_GENERIC_AUTH,
2574 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2575 		.matches = has_generic_auth,
2576 	},
2577 #endif /* CONFIG_ARM64_PTR_AUTH */
2578 #ifdef CONFIG_ARM64_PSEUDO_NMI
2579 	{
2580 		/*
2581 		 * Depends on having GICv3
2582 		 */
2583 		.desc = "IRQ priority masking",
2584 		.capability = ARM64_HAS_GIC_PRIO_MASKING,
2585 		.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
2586 		.matches = can_use_gic_priorities,
2587 	},
2588 	{
2589 		/*
2590 		 * Depends on ARM64_HAS_GIC_PRIO_MASKING
2591 		 */
2592 		.capability = ARM64_HAS_GIC_PRIO_RELAXED_SYNC,
2593 		.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
2594 		.matches = has_gic_prio_relaxed_sync,
2595 	},
2596 #endif
2597 #ifdef CONFIG_ARM64_E0PD
2598 	{
2599 		.desc = "E0PD",
2600 		.capability = ARM64_HAS_E0PD,
2601 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2602 		.sys_reg = SYS_ID_AA64MMFR2_EL1,
2603 		.sign = FTR_UNSIGNED,
2604 		.field_width = 4,
2605 		.field_pos = ID_AA64MMFR2_EL1_E0PD_SHIFT,
2606 		.matches = has_cpuid_feature,
2607 		.min_field_value = 1,
2608 		.cpu_enable = cpu_enable_e0pd,
2609 	},
2610 #endif
2611 	{
2612 		.desc = "Random Number Generator",
2613 		.capability = ARM64_HAS_RNG,
2614 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2615 		.matches = has_cpuid_feature,
2616 		.sys_reg = SYS_ID_AA64ISAR0_EL1,
2617 		.field_pos = ID_AA64ISAR0_EL1_RNDR_SHIFT,
2618 		.field_width = 4,
2619 		.sign = FTR_UNSIGNED,
2620 		.min_field_value = 1,
2621 	},
2622 #ifdef CONFIG_ARM64_BTI
2623 	{
2624 		.desc = "Branch Target Identification",
2625 		.capability = ARM64_BTI,
2626 #ifdef CONFIG_ARM64_BTI_KERNEL
2627 		.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
2628 #else
2629 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2630 #endif
2631 		.matches = has_cpuid_feature,
2632 		.cpu_enable = bti_enable,
2633 		.sys_reg = SYS_ID_AA64PFR1_EL1,
2634 		.field_pos = ID_AA64PFR1_EL1_BT_SHIFT,
2635 		.field_width = 4,
2636 		.min_field_value = ID_AA64PFR1_EL1_BT_IMP,
2637 		.sign = FTR_UNSIGNED,
2638 	},
2639 #endif
2640 #ifdef CONFIG_ARM64_MTE
2641 	{
2642 		.desc = "Memory Tagging Extension",
2643 		.capability = ARM64_MTE,
2644 		.type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
2645 		.matches = has_cpuid_feature,
2646 		.sys_reg = SYS_ID_AA64PFR1_EL1,
2647 		.field_pos = ID_AA64PFR1_EL1_MTE_SHIFT,
2648 		.field_width = 4,
2649 		.min_field_value = ID_AA64PFR1_EL1_MTE_MTE2,
2650 		.sign = FTR_UNSIGNED,
2651 		.cpu_enable = cpu_enable_mte,
2652 	},
2653 	{
2654 		.desc = "Asymmetric MTE Tag Check Fault",
2655 		.capability = ARM64_MTE_ASYMM,
2656 		.type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
2657 		.matches = has_cpuid_feature,
2658 		.sys_reg = SYS_ID_AA64PFR1_EL1,
2659 		.field_pos = ID_AA64PFR1_EL1_MTE_SHIFT,
2660 		.field_width = 4,
2661 		.min_field_value = ID_AA64PFR1_EL1_MTE_MTE3,
2662 		.sign = FTR_UNSIGNED,
2663 	},
2664 #endif /* CONFIG_ARM64_MTE */
2665 	{
2666 		.desc = "RCpc load-acquire (LDAPR)",
2667 		.capability = ARM64_HAS_LDAPR,
2668 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2669 		.sys_reg = SYS_ID_AA64ISAR1_EL1,
2670 		.sign = FTR_UNSIGNED,
2671 		.field_pos = ID_AA64ISAR1_EL1_LRCPC_SHIFT,
2672 		.field_width = 4,
2673 		.matches = has_cpuid_feature,
2674 		.min_field_value = 1,
2675 	},
2676 #ifdef CONFIG_ARM64_SME
2677 	{
2678 		.desc = "Scalable Matrix Extension",
2679 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2680 		.capability = ARM64_SME,
2681 		.sys_reg = SYS_ID_AA64PFR1_EL1,
2682 		.sign = FTR_UNSIGNED,
2683 		.field_pos = ID_AA64PFR1_EL1_SME_SHIFT,
2684 		.field_width = 4,
2685 		.min_field_value = ID_AA64PFR1_EL1_SME_IMP,
2686 		.matches = has_cpuid_feature,
2687 		.cpu_enable = sme_kernel_enable,
2688 	},
2689 	/* FA64 should be sorted after the base SME capability */
2690 	{
2691 		.desc = "FA64",
2692 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2693 		.capability = ARM64_SME_FA64,
2694 		.sys_reg = SYS_ID_AA64SMFR0_EL1,
2695 		.sign = FTR_UNSIGNED,
2696 		.field_pos = ID_AA64SMFR0_EL1_FA64_SHIFT,
2697 		.field_width = 1,
2698 		.min_field_value = ID_AA64SMFR0_EL1_FA64_IMP,
2699 		.matches = has_cpuid_feature,
2700 		.cpu_enable = fa64_kernel_enable,
2701 	},
2702 	{
2703 		.desc = "SME2",
2704 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2705 		.capability = ARM64_SME2,
2706 		.sys_reg = SYS_ID_AA64PFR1_EL1,
2707 		.sign = FTR_UNSIGNED,
2708 		.field_pos = ID_AA64PFR1_EL1_SME_SHIFT,
2709 		.field_width = ID_AA64PFR1_EL1_SME_WIDTH,
2710 		.min_field_value = ID_AA64PFR1_EL1_SME_SME2,
2711 		.matches = has_cpuid_feature,
2712 		.cpu_enable = sme2_kernel_enable,
2713 	},
2714 #endif /* CONFIG_ARM64_SME */
2715 	{
2716 		.desc = "WFx with timeout",
2717 		.capability = ARM64_HAS_WFXT,
2718 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2719 		.sys_reg = SYS_ID_AA64ISAR2_EL1,
2720 		.sign = FTR_UNSIGNED,
2721 		.field_pos = ID_AA64ISAR2_EL1_WFxT_SHIFT,
2722 		.field_width = 4,
2723 		.matches = has_cpuid_feature,
2724 		.min_field_value = ID_AA64ISAR2_EL1_WFxT_IMP,
2725 	},
2726 	{
2727 		.desc = "Trap EL0 IMPLEMENTATION DEFINED functionality",
2728 		.capability = ARM64_HAS_TIDCP1,
2729 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2730 		.sys_reg = SYS_ID_AA64MMFR1_EL1,
2731 		.sign = FTR_UNSIGNED,
2732 		.field_pos = ID_AA64MMFR1_EL1_TIDCP1_SHIFT,
2733 		.field_width = 4,
2734 		.min_field_value = ID_AA64MMFR1_EL1_TIDCP1_IMP,
2735 		.matches = has_cpuid_feature,
2736 		.cpu_enable = cpu_trap_el0_impdef,
2737 	},
2738 	{
2739 		.desc = "Data independent timing control (DIT)",
2740 		.capability = ARM64_HAS_DIT,
2741 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
2742 		.sys_reg = SYS_ID_AA64PFR0_EL1,
2743 		.sign = FTR_UNSIGNED,
2744 		.field_pos = ID_AA64PFR0_EL1_DIT_SHIFT,
2745 		.field_width = 4,
2746 		.min_field_value = ID_AA64PFR0_EL1_DIT_IMP,
2747 		.matches = has_cpuid_feature,
2748 		.cpu_enable = cpu_enable_dit,
2749 	},
2750 	{},
2751 };
2752 
2753 #define HWCAP_CPUID_MATCH(reg, field, min_value)			\
2754 		.matches = has_user_cpuid_feature,					\
2755 		.sys_reg = SYS_##reg,							\
2756 		.field_pos = reg##_##field##_SHIFT,						\
2757 		.field_width = reg##_##field##_WIDTH,						\
2758 		.sign = reg##_##field##_SIGNED,							\
2759 		.min_field_value = reg##_##field##_##min_value,
2760 
2761 #define __HWCAP_CAP(name, cap_type, cap)					\
2762 		.desc = name,							\
2763 		.type = ARM64_CPUCAP_SYSTEM_FEATURE,				\
2764 		.hwcap_type = cap_type,						\
2765 		.hwcap = cap,							\
2766 
2767 #define HWCAP_CAP(reg, field, min_value, cap_type, cap)		\
2768 	{									\
2769 		__HWCAP_CAP(#cap, cap_type, cap)				\
2770 		HWCAP_CPUID_MATCH(reg, field, min_value) 		\
2771 	}
2772 
2773 #define HWCAP_MULTI_CAP(list, cap_type, cap)					\
2774 	{									\
2775 		__HWCAP_CAP(#cap, cap_type, cap)				\
2776 		.matches = cpucap_multi_entry_cap_matches,			\
2777 		.match_list = list,						\
2778 	}
2779 
2780 #define HWCAP_CAP_MATCH(match, cap_type, cap)					\
2781 	{									\
2782 		__HWCAP_CAP(#cap, cap_type, cap)				\
2783 		.matches = match,						\
2784 	}
2785 
2786 #ifdef CONFIG_ARM64_PTR_AUTH
2787 static const struct arm64_cpu_capabilities ptr_auth_hwcap_addr_matches[] = {
2788 	{
2789 		HWCAP_CPUID_MATCH(ID_AA64ISAR1_EL1, APA, PAuth)
2790 	},
2791 	{
2792 		HWCAP_CPUID_MATCH(ID_AA64ISAR2_EL1, APA3, PAuth)
2793 	},
2794 	{
2795 		HWCAP_CPUID_MATCH(ID_AA64ISAR1_EL1, API, PAuth)
2796 	},
2797 	{},
2798 };
2799 
2800 static const struct arm64_cpu_capabilities ptr_auth_hwcap_gen_matches[] = {
2801 	{
2802 		HWCAP_CPUID_MATCH(ID_AA64ISAR1_EL1, GPA, IMP)
2803 	},
2804 	{
2805 		HWCAP_CPUID_MATCH(ID_AA64ISAR2_EL1, GPA3, IMP)
2806 	},
2807 	{
2808 		HWCAP_CPUID_MATCH(ID_AA64ISAR1_EL1, GPI, IMP)
2809 	},
2810 	{},
2811 };
2812 #endif
2813 
2814 static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
2815 	HWCAP_CAP(ID_AA64ISAR0_EL1, AES, PMULL, CAP_HWCAP, KERNEL_HWCAP_PMULL),
2816 	HWCAP_CAP(ID_AA64ISAR0_EL1, AES, AES, CAP_HWCAP, KERNEL_HWCAP_AES),
2817 	HWCAP_CAP(ID_AA64ISAR0_EL1, SHA1, IMP, CAP_HWCAP, KERNEL_HWCAP_SHA1),
2818 	HWCAP_CAP(ID_AA64ISAR0_EL1, SHA2, SHA256, CAP_HWCAP, KERNEL_HWCAP_SHA2),
2819 	HWCAP_CAP(ID_AA64ISAR0_EL1, SHA2, SHA512, CAP_HWCAP, KERNEL_HWCAP_SHA512),
2820 	HWCAP_CAP(ID_AA64ISAR0_EL1, CRC32, IMP, CAP_HWCAP, KERNEL_HWCAP_CRC32),
2821 	HWCAP_CAP(ID_AA64ISAR0_EL1, ATOMIC, IMP, CAP_HWCAP, KERNEL_HWCAP_ATOMICS),
2822 	HWCAP_CAP(ID_AA64ISAR0_EL1, RDM, IMP, CAP_HWCAP, KERNEL_HWCAP_ASIMDRDM),
2823 	HWCAP_CAP(ID_AA64ISAR0_EL1, SHA3, IMP, CAP_HWCAP, KERNEL_HWCAP_SHA3),
2824 	HWCAP_CAP(ID_AA64ISAR0_EL1, SM3, IMP, CAP_HWCAP, KERNEL_HWCAP_SM3),
2825 	HWCAP_CAP(ID_AA64ISAR0_EL1, SM4, IMP, CAP_HWCAP, KERNEL_HWCAP_SM4),
2826 	HWCAP_CAP(ID_AA64ISAR0_EL1, DP, IMP, CAP_HWCAP, KERNEL_HWCAP_ASIMDDP),
2827 	HWCAP_CAP(ID_AA64ISAR0_EL1, FHM, IMP, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM),
2828 	HWCAP_CAP(ID_AA64ISAR0_EL1, TS, FLAGM, CAP_HWCAP, KERNEL_HWCAP_FLAGM),
2829 	HWCAP_CAP(ID_AA64ISAR0_EL1, TS, FLAGM2, CAP_HWCAP, KERNEL_HWCAP_FLAGM2),
2830 	HWCAP_CAP(ID_AA64ISAR0_EL1, RNDR, IMP, CAP_HWCAP, KERNEL_HWCAP_RNG),
2831 	HWCAP_CAP(ID_AA64PFR0_EL1, FP, IMP, CAP_HWCAP, KERNEL_HWCAP_FP),
2832 	HWCAP_CAP(ID_AA64PFR0_EL1, FP, FP16, CAP_HWCAP, KERNEL_HWCAP_FPHP),
2833 	HWCAP_CAP(ID_AA64PFR0_EL1, AdvSIMD, IMP, CAP_HWCAP, KERNEL_HWCAP_ASIMD),
2834 	HWCAP_CAP(ID_AA64PFR0_EL1, AdvSIMD, FP16, CAP_HWCAP, KERNEL_HWCAP_ASIMDHP),
2835 	HWCAP_CAP(ID_AA64PFR0_EL1, DIT, IMP, CAP_HWCAP, KERNEL_HWCAP_DIT),
2836 	HWCAP_CAP(ID_AA64ISAR1_EL1, DPB, IMP, CAP_HWCAP, KERNEL_HWCAP_DCPOP),
2837 	HWCAP_CAP(ID_AA64ISAR1_EL1, DPB, DPB2, CAP_HWCAP, KERNEL_HWCAP_DCPODP),
2838 	HWCAP_CAP(ID_AA64ISAR1_EL1, JSCVT, IMP, CAP_HWCAP, KERNEL_HWCAP_JSCVT),
2839 	HWCAP_CAP(ID_AA64ISAR1_EL1, FCMA, IMP, CAP_HWCAP, KERNEL_HWCAP_FCMA),
2840 	HWCAP_CAP(ID_AA64ISAR1_EL1, LRCPC, IMP, CAP_HWCAP, KERNEL_HWCAP_LRCPC),
2841 	HWCAP_CAP(ID_AA64ISAR1_EL1, LRCPC, LRCPC2, CAP_HWCAP, KERNEL_HWCAP_ILRCPC),
2842 	HWCAP_CAP(ID_AA64ISAR1_EL1, FRINTTS, IMP, CAP_HWCAP, KERNEL_HWCAP_FRINT),
2843 	HWCAP_CAP(ID_AA64ISAR1_EL1, SB, IMP, CAP_HWCAP, KERNEL_HWCAP_SB),
2844 	HWCAP_CAP(ID_AA64ISAR1_EL1, BF16, IMP, CAP_HWCAP, KERNEL_HWCAP_BF16),
2845 	HWCAP_CAP(ID_AA64ISAR1_EL1, BF16, EBF16, CAP_HWCAP, KERNEL_HWCAP_EBF16),
2846 	HWCAP_CAP(ID_AA64ISAR1_EL1, DGH, IMP, CAP_HWCAP, KERNEL_HWCAP_DGH),
2847 	HWCAP_CAP(ID_AA64ISAR1_EL1, I8MM, IMP, CAP_HWCAP, KERNEL_HWCAP_I8MM),
2848 	HWCAP_CAP(ID_AA64MMFR2_EL1, AT, IMP, CAP_HWCAP, KERNEL_HWCAP_USCAT),
2849 #ifdef CONFIG_ARM64_SVE
2850 	HWCAP_CAP(ID_AA64PFR0_EL1, SVE, IMP, CAP_HWCAP, KERNEL_HWCAP_SVE),
2851 	HWCAP_CAP(ID_AA64ZFR0_EL1, SVEver, SVE2p1, CAP_HWCAP, KERNEL_HWCAP_SVE2P1),
2852 	HWCAP_CAP(ID_AA64ZFR0_EL1, SVEver, SVE2, CAP_HWCAP, KERNEL_HWCAP_SVE2),
2853 	HWCAP_CAP(ID_AA64ZFR0_EL1, AES, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEAES),
2854 	HWCAP_CAP(ID_AA64ZFR0_EL1, AES, PMULL128, CAP_HWCAP, KERNEL_HWCAP_SVEPMULL),
2855 	HWCAP_CAP(ID_AA64ZFR0_EL1, BitPerm, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEBITPERM),
2856 	HWCAP_CAP(ID_AA64ZFR0_EL1, BF16, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEBF16),
2857 	HWCAP_CAP(ID_AA64ZFR0_EL1, BF16, EBF16, CAP_HWCAP, KERNEL_HWCAP_SVE_EBF16),
2858 	HWCAP_CAP(ID_AA64ZFR0_EL1, SHA3, IMP, CAP_HWCAP, KERNEL_HWCAP_SVESHA3),
2859 	HWCAP_CAP(ID_AA64ZFR0_EL1, SM4, IMP, CAP_HWCAP, KERNEL_HWCAP_SVESM4),
2860 	HWCAP_CAP(ID_AA64ZFR0_EL1, I8MM, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEI8MM),
2861 	HWCAP_CAP(ID_AA64ZFR0_EL1, F32MM, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF32MM),
2862 	HWCAP_CAP(ID_AA64ZFR0_EL1, F64MM, IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF64MM),
2863 #endif
2864 	HWCAP_CAP(ID_AA64PFR1_EL1, SSBS, SSBS2, CAP_HWCAP, KERNEL_HWCAP_SSBS),
2865 #ifdef CONFIG_ARM64_BTI
2866 	HWCAP_CAP(ID_AA64PFR1_EL1, BT, IMP, CAP_HWCAP, KERNEL_HWCAP_BTI),
2867 #endif
2868 #ifdef CONFIG_ARM64_PTR_AUTH
2869 	HWCAP_MULTI_CAP(ptr_auth_hwcap_addr_matches, CAP_HWCAP, KERNEL_HWCAP_PACA),
2870 	HWCAP_MULTI_CAP(ptr_auth_hwcap_gen_matches, CAP_HWCAP, KERNEL_HWCAP_PACG),
2871 #endif
2872 #ifdef CONFIG_ARM64_MTE
2873 	HWCAP_CAP(ID_AA64PFR1_EL1, MTE, MTE2, CAP_HWCAP, KERNEL_HWCAP_MTE),
2874 	HWCAP_CAP(ID_AA64PFR1_EL1, MTE, MTE3, CAP_HWCAP, KERNEL_HWCAP_MTE3),
2875 #endif /* CONFIG_ARM64_MTE */
2876 	HWCAP_CAP(ID_AA64MMFR0_EL1, ECV, IMP, CAP_HWCAP, KERNEL_HWCAP_ECV),
2877 	HWCAP_CAP(ID_AA64MMFR1_EL1, AFP, IMP, CAP_HWCAP, KERNEL_HWCAP_AFP),
2878 	HWCAP_CAP(ID_AA64ISAR2_EL1, CSSC, IMP, CAP_HWCAP, KERNEL_HWCAP_CSSC),
2879 	HWCAP_CAP(ID_AA64ISAR2_EL1, RPRFM, IMP, CAP_HWCAP, KERNEL_HWCAP_RPRFM),
2880 	HWCAP_CAP(ID_AA64ISAR2_EL1, RPRES, IMP, CAP_HWCAP, KERNEL_HWCAP_RPRES),
2881 	HWCAP_CAP(ID_AA64ISAR2_EL1, WFxT, IMP, CAP_HWCAP, KERNEL_HWCAP_WFXT),
2882 #ifdef CONFIG_ARM64_SME
2883 	HWCAP_CAP(ID_AA64PFR1_EL1, SME, IMP, CAP_HWCAP, KERNEL_HWCAP_SME),
2884 	HWCAP_CAP(ID_AA64SMFR0_EL1, FA64, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_FA64),
2885 	HWCAP_CAP(ID_AA64SMFR0_EL1, SMEver, SME2p1, CAP_HWCAP, KERNEL_HWCAP_SME2P1),
2886 	HWCAP_CAP(ID_AA64SMFR0_EL1, SMEver, SME2, CAP_HWCAP, KERNEL_HWCAP_SME2),
2887 	HWCAP_CAP(ID_AA64SMFR0_EL1, I16I64, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_I16I64),
2888 	HWCAP_CAP(ID_AA64SMFR0_EL1, F64F64, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F64F64),
2889 	HWCAP_CAP(ID_AA64SMFR0_EL1, I16I32, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_I16I32),
2890 	HWCAP_CAP(ID_AA64SMFR0_EL1, B16B16, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_B16B16),
2891 	HWCAP_CAP(ID_AA64SMFR0_EL1, F16F16, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F16F16),
2892 	HWCAP_CAP(ID_AA64SMFR0_EL1, I8I32, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_I8I32),
2893 	HWCAP_CAP(ID_AA64SMFR0_EL1, F16F32, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F16F32),
2894 	HWCAP_CAP(ID_AA64SMFR0_EL1, B16F32, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_B16F32),
2895 	HWCAP_CAP(ID_AA64SMFR0_EL1, BI32I32, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_BI32I32),
2896 	HWCAP_CAP(ID_AA64SMFR0_EL1, F32F32, IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F32F32),
2897 #endif /* CONFIG_ARM64_SME */
2898 	{},
2899 };
2900 
2901 #ifdef CONFIG_COMPAT
2902 static bool compat_has_neon(const struct arm64_cpu_capabilities *cap, int scope)
2903 {
2904 	/*
2905 	 * Check that all of MVFR1_EL1.{SIMDSP, SIMDInt, SIMDLS} are available,
2906 	 * in line with that of arm32 as in vfp_init(). We make sure that the
2907 	 * check is future proof, by making sure value is non-zero.
2908 	 */
2909 	u32 mvfr1;
2910 
2911 	WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
2912 	if (scope == SCOPE_SYSTEM)
2913 		mvfr1 = read_sanitised_ftr_reg(SYS_MVFR1_EL1);
2914 	else
2915 		mvfr1 = read_sysreg_s(SYS_MVFR1_EL1);
2916 
2917 	return cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_EL1_SIMDSP_SHIFT) &&
2918 		cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_EL1_SIMDInt_SHIFT) &&
2919 		cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_EL1_SIMDLS_SHIFT);
2920 }
2921 #endif
2922 
2923 static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
2924 #ifdef CONFIG_COMPAT
2925 	HWCAP_CAP_MATCH(compat_has_neon, CAP_COMPAT_HWCAP, COMPAT_HWCAP_NEON),
2926 	HWCAP_CAP(MVFR1_EL1, SIMDFMAC, IMP, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv4),
2927 	/* Arm v8 mandates MVFR0.FPDP == {0, 2}. So, piggy back on this for the presence of VFP support */
2928 	HWCAP_CAP(MVFR0_EL1, FPDP, VFPv3, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFP),
2929 	HWCAP_CAP(MVFR0_EL1, FPDP, VFPv3, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv3),
2930 	HWCAP_CAP(MVFR1_EL1, FPHP, FP16, CAP_COMPAT_HWCAP, COMPAT_HWCAP_FPHP),
2931 	HWCAP_CAP(MVFR1_EL1, SIMDHP, SIMDHP_FLOAT, CAP_COMPAT_HWCAP, COMPAT_HWCAP_ASIMDHP),
2932 	HWCAP_CAP(ID_ISAR5_EL1, AES, VMULL, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
2933 	HWCAP_CAP(ID_ISAR5_EL1, AES, IMP, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
2934 	HWCAP_CAP(ID_ISAR5_EL1, SHA1, IMP, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
2935 	HWCAP_CAP(ID_ISAR5_EL1, SHA2, IMP, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
2936 	HWCAP_CAP(ID_ISAR5_EL1, CRC32, IMP, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32),
2937 	HWCAP_CAP(ID_ISAR6_EL1, DP, IMP, CAP_COMPAT_HWCAP, COMPAT_HWCAP_ASIMDDP),
2938 	HWCAP_CAP(ID_ISAR6_EL1, FHM, IMP, CAP_COMPAT_HWCAP, COMPAT_HWCAP_ASIMDFHM),
2939 	HWCAP_CAP(ID_ISAR6_EL1, SB, IMP, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SB),
2940 	HWCAP_CAP(ID_ISAR6_EL1, BF16, IMP, CAP_COMPAT_HWCAP, COMPAT_HWCAP_ASIMDBF16),
2941 	HWCAP_CAP(ID_ISAR6_EL1, I8MM, IMP, CAP_COMPAT_HWCAP, COMPAT_HWCAP_I8MM),
2942 	HWCAP_CAP(ID_PFR2_EL1, SSBS, IMP, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SSBS),
2943 #endif
2944 	{},
2945 };
2946 
2947 static void cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
2948 {
2949 	switch (cap->hwcap_type) {
2950 	case CAP_HWCAP:
2951 		cpu_set_feature(cap->hwcap);
2952 		break;
2953 #ifdef CONFIG_COMPAT
2954 	case CAP_COMPAT_HWCAP:
2955 		compat_elf_hwcap |= (u32)cap->hwcap;
2956 		break;
2957 	case CAP_COMPAT_HWCAP2:
2958 		compat_elf_hwcap2 |= (u32)cap->hwcap;
2959 		break;
2960 #endif
2961 	default:
2962 		WARN_ON(1);
2963 		break;
2964 	}
2965 }
2966 
2967 /* Check if we have a particular HWCAP enabled */
2968 static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
2969 {
2970 	bool rc;
2971 
2972 	switch (cap->hwcap_type) {
2973 	case CAP_HWCAP:
2974 		rc = cpu_have_feature(cap->hwcap);
2975 		break;
2976 #ifdef CONFIG_COMPAT
2977 	case CAP_COMPAT_HWCAP:
2978 		rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
2979 		break;
2980 	case CAP_COMPAT_HWCAP2:
2981 		rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
2982 		break;
2983 #endif
2984 	default:
2985 		WARN_ON(1);
2986 		rc = false;
2987 	}
2988 
2989 	return rc;
2990 }
2991 
2992 static void setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
2993 {
2994 	/* We support emulation of accesses to CPU ID feature registers */
2995 	cpu_set_named_feature(CPUID);
2996 	for (; hwcaps->matches; hwcaps++)
2997 		if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
2998 			cap_set_elf_hwcap(hwcaps);
2999 }
3000 
3001 static void update_cpu_capabilities(u16 scope_mask)
3002 {
3003 	int i;
3004 	const struct arm64_cpu_capabilities *caps;
3005 
3006 	scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
3007 	for (i = 0; i < ARM64_NCAPS; i++) {
3008 		caps = cpu_hwcaps_ptrs[i];
3009 		if (!caps || !(caps->type & scope_mask) ||
3010 		    cpus_have_cap(caps->capability) ||
3011 		    !caps->matches(caps, cpucap_default_scope(caps)))
3012 			continue;
3013 
3014 		if (caps->desc)
3015 			pr_info("detected: %s\n", caps->desc);
3016 		cpus_set_cap(caps->capability);
3017 
3018 		if ((scope_mask & SCOPE_BOOT_CPU) && (caps->type & SCOPE_BOOT_CPU))
3019 			set_bit(caps->capability, boot_capabilities);
3020 	}
3021 }
3022 
3023 /*
3024  * Enable all the available capabilities on this CPU. The capabilities
3025  * with BOOT_CPU scope are handled separately and hence skipped here.
3026  */
3027 static int cpu_enable_non_boot_scope_capabilities(void *__unused)
3028 {
3029 	int i;
3030 	u16 non_boot_scope = SCOPE_ALL & ~SCOPE_BOOT_CPU;
3031 
3032 	for_each_available_cap(i) {
3033 		const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[i];
3034 
3035 		if (WARN_ON(!cap))
3036 			continue;
3037 
3038 		if (!(cap->type & non_boot_scope))
3039 			continue;
3040 
3041 		if (cap->cpu_enable)
3042 			cap->cpu_enable(cap);
3043 	}
3044 	return 0;
3045 }
3046 
3047 /*
3048  * Run through the enabled capabilities and enable() it on all active
3049  * CPUs
3050  */
3051 static void __init enable_cpu_capabilities(u16 scope_mask)
3052 {
3053 	int i;
3054 	const struct arm64_cpu_capabilities *caps;
3055 	bool boot_scope;
3056 
3057 	scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
3058 	boot_scope = !!(scope_mask & SCOPE_BOOT_CPU);
3059 
3060 	for (i = 0; i < ARM64_NCAPS; i++) {
3061 		unsigned int num;
3062 
3063 		caps = cpu_hwcaps_ptrs[i];
3064 		if (!caps || !(caps->type & scope_mask))
3065 			continue;
3066 		num = caps->capability;
3067 		if (!cpus_have_cap(num))
3068 			continue;
3069 
3070 		if (boot_scope && caps->cpu_enable)
3071 			/*
3072 			 * Capabilities with SCOPE_BOOT_CPU scope are finalised
3073 			 * before any secondary CPU boots. Thus, each secondary
3074 			 * will enable the capability as appropriate via
3075 			 * check_local_cpu_capabilities(). The only exception is
3076 			 * the boot CPU, for which the capability must be
3077 			 * enabled here. This approach avoids costly
3078 			 * stop_machine() calls for this case.
3079 			 */
3080 			caps->cpu_enable(caps);
3081 	}
3082 
3083 	/*
3084 	 * For all non-boot scope capabilities, use stop_machine()
3085 	 * as it schedules the work allowing us to modify PSTATE,
3086 	 * instead of on_each_cpu() which uses an IPI, giving us a
3087 	 * PSTATE that disappears when we return.
3088 	 */
3089 	if (!boot_scope)
3090 		stop_machine(cpu_enable_non_boot_scope_capabilities,
3091 			     NULL, cpu_online_mask);
3092 }
3093 
3094 /*
3095  * Run through the list of capabilities to check for conflicts.
3096  * If the system has already detected a capability, take necessary
3097  * action on this CPU.
3098  */
3099 static void verify_local_cpu_caps(u16 scope_mask)
3100 {
3101 	int i;
3102 	bool cpu_has_cap, system_has_cap;
3103 	const struct arm64_cpu_capabilities *caps;
3104 
3105 	scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
3106 
3107 	for (i = 0; i < ARM64_NCAPS; i++) {
3108 		caps = cpu_hwcaps_ptrs[i];
3109 		if (!caps || !(caps->type & scope_mask))
3110 			continue;
3111 
3112 		cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU);
3113 		system_has_cap = cpus_have_cap(caps->capability);
3114 
3115 		if (system_has_cap) {
3116 			/*
3117 			 * Check if the new CPU misses an advertised feature,
3118 			 * which is not safe to miss.
3119 			 */
3120 			if (!cpu_has_cap && !cpucap_late_cpu_optional(caps))
3121 				break;
3122 			/*
3123 			 * We have to issue cpu_enable() irrespective of
3124 			 * whether the CPU has it or not, as it is enabeld
3125 			 * system wide. It is upto the call back to take
3126 			 * appropriate action on this CPU.
3127 			 */
3128 			if (caps->cpu_enable)
3129 				caps->cpu_enable(caps);
3130 		} else {
3131 			/*
3132 			 * Check if the CPU has this capability if it isn't
3133 			 * safe to have when the system doesn't.
3134 			 */
3135 			if (cpu_has_cap && !cpucap_late_cpu_permitted(caps))
3136 				break;
3137 		}
3138 	}
3139 
3140 	if (i < ARM64_NCAPS) {
3141 		pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n",
3142 			smp_processor_id(), caps->capability,
3143 			caps->desc, system_has_cap, cpu_has_cap);
3144 
3145 		if (cpucap_panic_on_conflict(caps))
3146 			cpu_panic_kernel();
3147 		else
3148 			cpu_die_early();
3149 	}
3150 }
3151 
3152 /*
3153  * Check for CPU features that are used in early boot
3154  * based on the Boot CPU value.
3155  */
3156 static void check_early_cpu_features(void)
3157 {
3158 	verify_cpu_asid_bits();
3159 
3160 	verify_local_cpu_caps(SCOPE_BOOT_CPU);
3161 }
3162 
3163 static void
3164 __verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
3165 {
3166 
3167 	for (; caps->matches; caps++)
3168 		if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
3169 			pr_crit("CPU%d: missing HWCAP: %s\n",
3170 					smp_processor_id(), caps->desc);
3171 			cpu_die_early();
3172 		}
3173 }
3174 
3175 static void verify_local_elf_hwcaps(void)
3176 {
3177 	__verify_local_elf_hwcaps(arm64_elf_hwcaps);
3178 
3179 	if (id_aa64pfr0_32bit_el0(read_cpuid(ID_AA64PFR0_EL1)))
3180 		__verify_local_elf_hwcaps(compat_elf_hwcaps);
3181 }
3182 
3183 static void verify_sve_features(void)
3184 {
3185 	u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
3186 	u64 zcr = read_zcr_features();
3187 
3188 	unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
3189 	unsigned int len = zcr & ZCR_ELx_LEN_MASK;
3190 
3191 	if (len < safe_len || vec_verify_vq_map(ARM64_VEC_SVE)) {
3192 		pr_crit("CPU%d: SVE: vector length support mismatch\n",
3193 			smp_processor_id());
3194 		cpu_die_early();
3195 	}
3196 
3197 	/* Add checks on other ZCR bits here if necessary */
3198 }
3199 
3200 static void verify_sme_features(void)
3201 {
3202 	u64 safe_smcr = read_sanitised_ftr_reg(SYS_SMCR_EL1);
3203 	u64 smcr = read_smcr_features();
3204 
3205 	unsigned int safe_len = safe_smcr & SMCR_ELx_LEN_MASK;
3206 	unsigned int len = smcr & SMCR_ELx_LEN_MASK;
3207 
3208 	if (len < safe_len || vec_verify_vq_map(ARM64_VEC_SME)) {
3209 		pr_crit("CPU%d: SME: vector length support mismatch\n",
3210 			smp_processor_id());
3211 		cpu_die_early();
3212 	}
3213 
3214 	/* Add checks on other SMCR bits here if necessary */
3215 }
3216 
3217 static void verify_hyp_capabilities(void)
3218 {
3219 	u64 safe_mmfr1, mmfr0, mmfr1;
3220 	int parange, ipa_max;
3221 	unsigned int safe_vmid_bits, vmid_bits;
3222 
3223 	if (!IS_ENABLED(CONFIG_KVM))
3224 		return;
3225 
3226 	safe_mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
3227 	mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
3228 	mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
3229 
3230 	/* Verify VMID bits */
3231 	safe_vmid_bits = get_vmid_bits(safe_mmfr1);
3232 	vmid_bits = get_vmid_bits(mmfr1);
3233 	if (vmid_bits < safe_vmid_bits) {
3234 		pr_crit("CPU%d: VMID width mismatch\n", smp_processor_id());
3235 		cpu_die_early();
3236 	}
3237 
3238 	/* Verify IPA range */
3239 	parange = cpuid_feature_extract_unsigned_field(mmfr0,
3240 				ID_AA64MMFR0_EL1_PARANGE_SHIFT);
3241 	ipa_max = id_aa64mmfr0_parange_to_phys_shift(parange);
3242 	if (ipa_max < get_kvm_ipa_limit()) {
3243 		pr_crit("CPU%d: IPA range mismatch\n", smp_processor_id());
3244 		cpu_die_early();
3245 	}
3246 }
3247 
3248 /*
3249  * Run through the enabled system capabilities and enable() it on this CPU.
3250  * The capabilities were decided based on the available CPUs at the boot time.
3251  * Any new CPU should match the system wide status of the capability. If the
3252  * new CPU doesn't have a capability which the system now has enabled, we
3253  * cannot do anything to fix it up and could cause unexpected failures. So
3254  * we park the CPU.
3255  */
3256 static void verify_local_cpu_capabilities(void)
3257 {
3258 	/*
3259 	 * The capabilities with SCOPE_BOOT_CPU are checked from
3260 	 * check_early_cpu_features(), as they need to be verified
3261 	 * on all secondary CPUs.
3262 	 */
3263 	verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU);
3264 	verify_local_elf_hwcaps();
3265 
3266 	if (system_supports_sve())
3267 		verify_sve_features();
3268 
3269 	if (system_supports_sme())
3270 		verify_sme_features();
3271 
3272 	if (is_hyp_mode_available())
3273 		verify_hyp_capabilities();
3274 }
3275 
3276 void check_local_cpu_capabilities(void)
3277 {
3278 	/*
3279 	 * All secondary CPUs should conform to the early CPU features
3280 	 * in use by the kernel based on boot CPU.
3281 	 */
3282 	check_early_cpu_features();
3283 
3284 	/*
3285 	 * If we haven't finalised the system capabilities, this CPU gets
3286 	 * a chance to update the errata work arounds and local features.
3287 	 * Otherwise, this CPU should verify that it has all the system
3288 	 * advertised capabilities.
3289 	 */
3290 	if (!system_capabilities_finalized())
3291 		update_cpu_capabilities(SCOPE_LOCAL_CPU);
3292 	else
3293 		verify_local_cpu_capabilities();
3294 }
3295 
3296 static void __init setup_boot_cpu_capabilities(void)
3297 {
3298 	/* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */
3299 	update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU);
3300 	/* Enable the SCOPE_BOOT_CPU capabilities alone right away */
3301 	enable_cpu_capabilities(SCOPE_BOOT_CPU);
3302 }
3303 
3304 bool this_cpu_has_cap(unsigned int n)
3305 {
3306 	if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
3307 		const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
3308 
3309 		if (cap)
3310 			return cap->matches(cap, SCOPE_LOCAL_CPU);
3311 	}
3312 
3313 	return false;
3314 }
3315 EXPORT_SYMBOL_GPL(this_cpu_has_cap);
3316 
3317 /*
3318  * This helper function is used in a narrow window when,
3319  * - The system wide safe registers are set with all the SMP CPUs and,
3320  * - The SYSTEM_FEATURE cpu_hwcaps may not have been set.
3321  * In all other cases cpus_have_{const_}cap() should be used.
3322  */
3323 static bool __maybe_unused __system_matches_cap(unsigned int n)
3324 {
3325 	if (n < ARM64_NCAPS) {
3326 		const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
3327 
3328 		if (cap)
3329 			return cap->matches(cap, SCOPE_SYSTEM);
3330 	}
3331 	return false;
3332 }
3333 
3334 void cpu_set_feature(unsigned int num)
3335 {
3336 	set_bit(num, elf_hwcap);
3337 }
3338 
3339 bool cpu_have_feature(unsigned int num)
3340 {
3341 	return test_bit(num, elf_hwcap);
3342 }
3343 EXPORT_SYMBOL_GPL(cpu_have_feature);
3344 
3345 unsigned long cpu_get_elf_hwcap(void)
3346 {
3347 	/*
3348 	 * We currently only populate the first 32 bits of AT_HWCAP. Please
3349 	 * note that for userspace compatibility we guarantee that bits 62
3350 	 * and 63 will always be returned as 0.
3351 	 */
3352 	return elf_hwcap[0];
3353 }
3354 
3355 unsigned long cpu_get_elf_hwcap2(void)
3356 {
3357 	return elf_hwcap[1];
3358 }
3359 
3360 static void __init setup_system_capabilities(void)
3361 {
3362 	/*
3363 	 * We have finalised the system-wide safe feature
3364 	 * registers, finalise the capabilities that depend
3365 	 * on it. Also enable all the available capabilities,
3366 	 * that are not enabled already.
3367 	 */
3368 	update_cpu_capabilities(SCOPE_SYSTEM);
3369 	enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
3370 }
3371 
3372 void __init setup_cpu_features(void)
3373 {
3374 	u32 cwg;
3375 
3376 	setup_system_capabilities();
3377 	setup_elf_hwcaps(arm64_elf_hwcaps);
3378 
3379 	if (system_supports_32bit_el0()) {
3380 		setup_elf_hwcaps(compat_elf_hwcaps);
3381 		elf_hwcap_fixup();
3382 	}
3383 
3384 	if (system_uses_ttbr0_pan())
3385 		pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
3386 
3387 	sve_setup();
3388 	sme_setup();
3389 	minsigstksz_setup();
3390 
3391 	/*
3392 	 * Check for sane CTR_EL0.CWG value.
3393 	 */
3394 	cwg = cache_type_cwg();
3395 	if (!cwg)
3396 		pr_warn("No Cache Writeback Granule information, assuming %d\n",
3397 			ARCH_DMA_MINALIGN);
3398 }
3399 
3400 static int enable_mismatched_32bit_el0(unsigned int cpu)
3401 {
3402 	/*
3403 	 * The first 32-bit-capable CPU we detected and so can no longer
3404 	 * be offlined by userspace. -1 indicates we haven't yet onlined
3405 	 * a 32-bit-capable CPU.
3406 	 */
3407 	static int lucky_winner = -1;
3408 
3409 	struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
3410 	bool cpu_32bit = id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0);
3411 
3412 	if (cpu_32bit) {
3413 		cpumask_set_cpu(cpu, cpu_32bit_el0_mask);
3414 		static_branch_enable_cpuslocked(&arm64_mismatched_32bit_el0);
3415 	}
3416 
3417 	if (cpumask_test_cpu(0, cpu_32bit_el0_mask) == cpu_32bit)
3418 		return 0;
3419 
3420 	if (lucky_winner >= 0)
3421 		return 0;
3422 
3423 	/*
3424 	 * We've detected a mismatch. We need to keep one of our CPUs with
3425 	 * 32-bit EL0 online so that is_cpu_allowed() doesn't end up rejecting
3426 	 * every CPU in the system for a 32-bit task.
3427 	 */
3428 	lucky_winner = cpu_32bit ? cpu : cpumask_any_and(cpu_32bit_el0_mask,
3429 							 cpu_active_mask);
3430 	get_cpu_device(lucky_winner)->offline_disabled = true;
3431 	setup_elf_hwcaps(compat_elf_hwcaps);
3432 	elf_hwcap_fixup();
3433 	pr_info("Asymmetric 32-bit EL0 support detected on CPU %u; CPU hot-unplug disabled on CPU %u\n",
3434 		cpu, lucky_winner);
3435 	return 0;
3436 }
3437 
3438 static int __init init_32bit_el0_mask(void)
3439 {
3440 	if (!allow_mismatched_32bit_el0)
3441 		return 0;
3442 
3443 	if (!zalloc_cpumask_var(&cpu_32bit_el0_mask, GFP_KERNEL))
3444 		return -ENOMEM;
3445 
3446 	return cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
3447 				 "arm64/mismatched_32bit_el0:online",
3448 				 enable_mismatched_32bit_el0, NULL);
3449 }
3450 subsys_initcall_sync(init_32bit_el0_mask);
3451 
3452 static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap)
3453 {
3454 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir), idmap_pg_dir);
3455 }
3456 
3457 /*
3458  * We emulate only the following system register space.
3459  * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 2 - 7]
3460  * See Table C5-6 System instruction encodings for System register accesses,
3461  * ARMv8 ARM(ARM DDI 0487A.f) for more details.
3462  */
3463 static inline bool __attribute_const__ is_emulated(u32 id)
3464 {
3465 	return (sys_reg_Op0(id) == 0x3 &&
3466 		sys_reg_CRn(id) == 0x0 &&
3467 		sys_reg_Op1(id) == 0x0 &&
3468 		(sys_reg_CRm(id) == 0 ||
3469 		 ((sys_reg_CRm(id) >= 2) && (sys_reg_CRm(id) <= 7))));
3470 }
3471 
3472 /*
3473  * With CRm == 0, reg should be one of :
3474  * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
3475  */
3476 static inline int emulate_id_reg(u32 id, u64 *valp)
3477 {
3478 	switch (id) {
3479 	case SYS_MIDR_EL1:
3480 		*valp = read_cpuid_id();
3481 		break;
3482 	case SYS_MPIDR_EL1:
3483 		*valp = SYS_MPIDR_SAFE_VAL;
3484 		break;
3485 	case SYS_REVIDR_EL1:
3486 		/* IMPLEMENTATION DEFINED values are emulated with 0 */
3487 		*valp = 0;
3488 		break;
3489 	default:
3490 		return -EINVAL;
3491 	}
3492 
3493 	return 0;
3494 }
3495 
3496 static int emulate_sys_reg(u32 id, u64 *valp)
3497 {
3498 	struct arm64_ftr_reg *regp;
3499 
3500 	if (!is_emulated(id))
3501 		return -EINVAL;
3502 
3503 	if (sys_reg_CRm(id) == 0)
3504 		return emulate_id_reg(id, valp);
3505 
3506 	regp = get_arm64_ftr_reg_nowarn(id);
3507 	if (regp)
3508 		*valp = arm64_ftr_reg_user_value(regp);
3509 	else
3510 		/*
3511 		 * The untracked registers are either IMPLEMENTATION DEFINED
3512 		 * (e.g, ID_AFR0_EL1) or reserved RAZ.
3513 		 */
3514 		*valp = 0;
3515 	return 0;
3516 }
3517 
3518 int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt)
3519 {
3520 	int rc;
3521 	u64 val;
3522 
3523 	rc = emulate_sys_reg(sys_reg, &val);
3524 	if (!rc) {
3525 		pt_regs_write_reg(regs, rt, val);
3526 		arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
3527 	}
3528 	return rc;
3529 }
3530 
3531 bool try_emulate_mrs(struct pt_regs *regs, u32 insn)
3532 {
3533 	u32 sys_reg, rt;
3534 
3535 	if (compat_user_mode(regs) || !aarch64_insn_is_mrs(insn))
3536 		return false;
3537 
3538 	/*
3539 	 * sys_reg values are defined as used in mrs/msr instruction.
3540 	 * shift the imm value to get the encoding.
3541 	 */
3542 	sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
3543 	rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
3544 	return do_emulate_mrs(regs, sys_reg, rt) == 0;
3545 }
3546 
3547 enum mitigation_state arm64_get_meltdown_state(void)
3548 {
3549 	if (__meltdown_safe)
3550 		return SPECTRE_UNAFFECTED;
3551 
3552 	if (arm64_kernel_unmapped_at_el0())
3553 		return SPECTRE_MITIGATED;
3554 
3555 	return SPECTRE_VULNERABLE;
3556 }
3557 
3558 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr,
3559 			  char *buf)
3560 {
3561 	switch (arm64_get_meltdown_state()) {
3562 	case SPECTRE_UNAFFECTED:
3563 		return sprintf(buf, "Not affected\n");
3564 
3565 	case SPECTRE_MITIGATED:
3566 		return sprintf(buf, "Mitigation: PTI\n");
3567 
3568 	default:
3569 		return sprintf(buf, "Vulnerable\n");
3570 	}
3571 }
3572