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