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