1 // SPDX-License-Identifier: GPL-2.0-only
3 * Contains CPU feature definitions
5 * Copyright (C) 2015 ARM Ltd.
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.
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.
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.
29 * Some implementation details worth remembering:
31 * - Mismatched features are *always* sanitised to a "safe" value, which
32 * usually indicates that the feature is not supported.
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.
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.
43 * - A "feature" is typically a 4-bit register field. A "capability" is the
44 * high-level description derived from the sanitised field value.
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).
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
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
63 #define pr_fmt(fmt) "CPU features: " fmt
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/types.h>
72 #include <linux/cpu.h>
74 #include <asm/cpufeature.h>
75 #include <asm/cpu_ops.h>
76 #include <asm/fpsimd.h>
77 #include <asm/mmu_context.h>
78 #include <asm/processor.h>
79 #include <asm/sysreg.h>
80 #include <asm/traps.h>
83 /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
84 static unsigned long elf_hwcap __read_mostly;
87 #define COMPAT_ELF_HWCAP_DEFAULT \
88 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
89 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
90 COMPAT_HWCAP_TLS|COMPAT_HWCAP_IDIV|\
92 unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
93 unsigned int compat_elf_hwcap2 __read_mostly;
96 DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
97 EXPORT_SYMBOL(cpu_hwcaps);
98 static struct arm64_cpu_capabilities const __ro_after_init *cpu_hwcaps_ptrs[ARM64_NCAPS];
100 /* Need also bit for ARM64_CB_PATCH */
101 DECLARE_BITMAP(boot_capabilities, ARM64_NPATCHABLE);
103 bool arm64_use_ng_mappings = false;
104 EXPORT_SYMBOL(arm64_use_ng_mappings);
107 * Flag to indicate if we have computed the system wide
108 * capabilities based on the boot time active CPUs. This
109 * will be used to determine if a new booting CPU should
110 * go through the verification process to make sure that it
111 * supports the system capabilities, without using a hotplug
112 * notifier. This is also used to decide if we could use
113 * the fast path for checking constant CPU caps.
115 DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
116 EXPORT_SYMBOL(arm64_const_caps_ready);
117 static inline void finalize_system_capabilities(void)
119 static_branch_enable(&arm64_const_caps_ready);
122 static int dump_cpu_hwcaps(struct notifier_block *self, unsigned long v, void *p)
124 /* file-wide pr_fmt adds "CPU features: " prefix */
125 pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
129 static struct notifier_block cpu_hwcaps_notifier = {
130 .notifier_call = dump_cpu_hwcaps
133 static int __init register_cpu_hwcaps_dumper(void)
135 atomic_notifier_chain_register(&panic_notifier_list,
136 &cpu_hwcaps_notifier);
139 __initcall(register_cpu_hwcaps_dumper);
141 DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
142 EXPORT_SYMBOL(cpu_hwcap_keys);
144 #define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
147 .visible = VISIBLE, \
152 .safe_val = SAFE_VAL, \
155 /* Define a feature with unsigned values */
156 #define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
157 __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
159 /* Define a feature with a signed value */
160 #define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
161 __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
163 #define ARM64_FTR_END \
168 /* meta feature for alternatives */
169 static bool __maybe_unused
170 cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
172 static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
174 static bool __system_matches_cap(unsigned int n);
177 * NOTE: Any changes to the visibility of features should be kept in
178 * sync with the documentation of the CPU feature register ABI.
180 static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
181 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RNDR_SHIFT, 4, 0),
182 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TS_SHIFT, 4, 0),
183 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_FHM_SHIFT, 4, 0),
184 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
185 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
186 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
187 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
188 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
189 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
190 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
191 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
192 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
193 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
197 static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
198 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_I8MM_SHIFT, 4, 0),
199 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DGH_SHIFT, 4, 0),
200 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_BF16_SHIFT, 4, 0),
201 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SPECRES_SHIFT, 4, 0),
202 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SB_SHIFT, 4, 0),
203 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FRINTTS_SHIFT, 4, 0),
204 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
205 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPI_SHIFT, 4, 0),
206 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
207 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPA_SHIFT, 4, 0),
208 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_LRCPC_SHIFT, 4, 0),
209 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FCMA_SHIFT, 4, 0),
210 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_JSCVT_SHIFT, 4, 0),
211 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
212 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_API_SHIFT, 4, 0),
213 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
214 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_APA_SHIFT, 4, 0),
215 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DPB_SHIFT, 4, 0),
219 static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
220 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
221 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
222 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_DIT_SHIFT, 4, 0),
223 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_AMU_SHIFT, 4, 0),
224 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
225 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SVE_SHIFT, 4, 0),
226 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_RAS_SHIFT, 4, 0),
227 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_GIC_SHIFT, 4, 0),
228 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
229 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
230 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
231 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
232 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
233 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
237 static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
238 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_SSBS_SHIFT, 4, ID_AA64PFR1_SSBS_PSTATE_NI),
242 static const struct arm64_ftr_bits ftr_id_aa64zfr0[] = {
243 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
244 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_F64MM_SHIFT, 4, 0),
245 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
246 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_F32MM_SHIFT, 4, 0),
247 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
248 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_I8MM_SHIFT, 4, 0),
249 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
250 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SM4_SHIFT, 4, 0),
251 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
252 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SHA3_SHIFT, 4, 0),
253 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
254 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_BF16_SHIFT, 4, 0),
255 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
256 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_BITPERM_SHIFT, 4, 0),
257 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
258 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_AES_SHIFT, 4, 0),
259 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
260 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SVEVER_SHIFT, 4, 0),
264 static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
266 * We already refuse to boot CPUs that don't support our configured
267 * page size, so we can only detect mismatches for a page size other
268 * than the one we're currently using. Unfortunately, SoCs like this
269 * exist in the wild so, even though we don't like it, we'll have to go
270 * along with it and treat them as non-strict.
272 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
273 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
274 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
276 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
277 /* Linux shouldn't care about secure memory */
278 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
279 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
280 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
282 * Differing PARange is fine as long as all peripherals and memory are mapped
283 * within the minimum PARange of all CPUs
285 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
289 static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
290 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
291 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
292 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
293 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
294 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
295 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
299 static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
300 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_E0PD_SHIFT, 4, 0),
301 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_FWB_SHIFT, 4, 0),
302 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_AT_SHIFT, 4, 0),
303 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
304 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
305 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
306 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
307 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
311 static const struct arm64_ftr_bits ftr_ctr[] = {
312 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
313 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DIC_SHIFT, 1, 1),
314 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IDC_SHIFT, 1, 1),
315 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_CWG_SHIFT, 4, 0),
316 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_ERG_SHIFT, 4, 0),
317 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DMINLINE_SHIFT, 4, 1),
319 * Linux can handle differing I-cache policies. Userspace JITs will
320 * make use of *minLine.
321 * If we have differing I-cache policies, report it as the weakest - VIPT.
323 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_VIPT), /* L1Ip */
324 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IMINLINE_SHIFT, 4, 0),
328 struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
329 .name = "SYS_CTR_EL0",
333 static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
334 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0xf), /* InnerShr */
335 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0), /* FCSE */
336 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
337 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0), /* TCM */
338 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* ShareLvl */
339 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0xf), /* OuterShr */
340 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* PMSA */
341 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* VMSA */
345 static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
346 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 36, 28, 0),
347 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_PMSVER_SHIFT, 4, 0),
348 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
349 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
350 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
352 * We can instantiate multiple PMU instances with different levels
355 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
356 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
357 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
361 static const struct arm64_ftr_bits ftr_mvfr2[] = {
362 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* FPMisc */
363 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* SIMDMisc */
367 static const struct arm64_ftr_bits ftr_dczid[] = {
368 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
369 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
374 static const struct arm64_ftr_bits ftr_id_isar5[] = {
375 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_RDM_SHIFT, 4, 0),
376 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_CRC32_SHIFT, 4, 0),
377 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA2_SHIFT, 4, 0),
378 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA1_SHIFT, 4, 0),
379 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_AES_SHIFT, 4, 0),
380 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SEVL_SHIFT, 4, 0),
384 static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
385 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* ac2 */
389 static const struct arm64_ftr_bits ftr_id_isar4[] = {
390 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SWP_FRAC_SHIFT, 4, 0),
391 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_PSR_M_SHIFT, 4, 0),
392 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SYNCH_PRIM_FRAC_SHIFT, 4, 0),
393 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_BARRIER_SHIFT, 4, 0),
394 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SMC_SHIFT, 4, 0),
395 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WRITEBACK_SHIFT, 4, 0),
396 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WITHSHIFTS_SHIFT, 4, 0),
397 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_UNPRIV_SHIFT, 4, 0),
401 static const struct arm64_ftr_bits ftr_id_isar6[] = {
402 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_I8MM_SHIFT, 4, 0),
403 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_BF16_SHIFT, 4, 0),
404 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SPECRES_SHIFT, 4, 0),
405 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SB_SHIFT, 4, 0),
406 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_FHM_SHIFT, 4, 0),
407 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_DP_SHIFT, 4, 0),
408 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_JSCVT_SHIFT, 4, 0),
412 static const struct arm64_ftr_bits ftr_id_pfr0[] = {
413 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* State3 */
414 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0), /* State2 */
415 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* State1 */
416 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* State0 */
420 static const struct arm64_ftr_bits ftr_id_pfr1[] = {
421 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GIC_SHIFT, 4, 0),
422 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRT_FRAC_SHIFT, 4, 0),
423 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SEC_FRAC_SHIFT, 4, 0),
424 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GENTIMER_SHIFT, 4, 0),
425 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRTUALIZATION_SHIFT, 4, 0),
426 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_MPROGMOD_SHIFT, 4, 0),
427 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SECURITY_SHIFT, 4, 0),
428 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_PROGMOD_SHIFT, 4, 0),
432 static const struct arm64_ftr_bits ftr_id_dfr0[] = {
433 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
434 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
435 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
436 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
437 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
438 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
439 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
440 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
444 static const struct arm64_ftr_bits ftr_zcr[] = {
445 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
446 ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_SIZE, 0), /* LEN */
451 * Common ftr bits for a 32bit register with all hidden, strict
452 * attributes, with 4bit feature fields and a default safe value of
453 * 0. Covers the following 32bit registers:
454 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
456 static const struct arm64_ftr_bits ftr_generic_32bits[] = {
457 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
458 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
459 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
460 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
461 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
462 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
463 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
464 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
468 /* Table for a single 32bit feature value */
469 static const struct arm64_ftr_bits ftr_single32[] = {
470 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
474 static const struct arm64_ftr_bits ftr_raz[] = {
478 #define ARM64_FTR_REG(id, table) { \
480 .reg = &(struct arm64_ftr_reg){ \
482 .ftr_bits = &((table)[0]), \
485 static const struct __ftr_reg_entry {
487 struct arm64_ftr_reg *reg;
488 } arm64_ftr_regs[] = {
490 /* Op1 = 0, CRn = 0, CRm = 1 */
491 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
492 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_id_pfr1),
493 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
494 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
495 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
496 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
497 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
499 /* Op1 = 0, CRn = 0, CRm = 2 */
500 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
501 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
502 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
503 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
504 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_id_isar4),
505 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
506 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
507 ARM64_FTR_REG(SYS_ID_ISAR6_EL1, ftr_id_isar6),
509 /* Op1 = 0, CRn = 0, CRm = 3 */
510 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
511 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
512 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
514 /* Op1 = 0, CRn = 0, CRm = 4 */
515 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
516 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1),
517 ARM64_FTR_REG(SYS_ID_AA64ZFR0_EL1, ftr_id_aa64zfr0),
519 /* Op1 = 0, CRn = 0, CRm = 5 */
520 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
521 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
523 /* Op1 = 0, CRn = 0, CRm = 6 */
524 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
525 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1),
527 /* Op1 = 0, CRn = 0, CRm = 7 */
528 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
529 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
530 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
532 /* Op1 = 0, CRn = 1, CRm = 2 */
533 ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
535 /* Op1 = 3, CRn = 0, CRm = 0 */
536 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
537 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
539 /* Op1 = 3, CRn = 14, CRm = 0 */
540 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
543 static int search_cmp_ftr_reg(const void *id, const void *regp)
545 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
549 * get_arm64_ftr_reg - Lookup a feature register entry using its
550 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
551 * ascending order of sys_id , we use binary search to find a matching
554 * returns - Upon success, matching ftr_reg entry for id.
555 * - NULL on failure. It is upto the caller to decide
556 * the impact of a failure.
558 static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
560 const struct __ftr_reg_entry *ret;
562 ret = bsearch((const void *)(unsigned long)sys_id,
564 ARRAY_SIZE(arm64_ftr_regs),
565 sizeof(arm64_ftr_regs[0]),
572 static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
575 u64 mask = arm64_ftr_mask(ftrp);
578 reg |= (ftr_val << ftrp->shift) & mask;
582 static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
587 switch (ftrp->type) {
589 ret = ftrp->safe_val;
592 ret = new < cur ? new : cur;
594 case FTR_HIGHER_OR_ZERO_SAFE:
598 case FTR_HIGHER_SAFE:
599 ret = new > cur ? new : cur;
608 static void __init sort_ftr_regs(void)
612 /* Check that the array is sorted so that we can do the binary search */
613 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
614 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
618 * Initialise the CPU feature register from Boot CPU values.
619 * Also initiliases the strict_mask for the register.
620 * Any bits that are not covered by an arm64_ftr_bits entry are considered
621 * RES0 for the system-wide value, and must strictly match.
623 static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
626 u64 strict_mask = ~0x0ULL;
630 const struct arm64_ftr_bits *ftrp;
631 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
635 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
636 u64 ftr_mask = arm64_ftr_mask(ftrp);
637 s64 ftr_new = arm64_ftr_value(ftrp, new);
639 val = arm64_ftr_set_value(ftrp, val, ftr_new);
641 valid_mask |= ftr_mask;
643 strict_mask &= ~ftr_mask;
645 user_mask |= ftr_mask;
647 reg->user_val = arm64_ftr_set_value(ftrp,
655 reg->strict_mask = strict_mask;
656 reg->user_mask = user_mask;
659 extern const struct arm64_cpu_capabilities arm64_errata[];
660 static const struct arm64_cpu_capabilities arm64_features[];
663 init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
665 for (; caps->matches; caps++) {
666 if (WARN(caps->capability >= ARM64_NCAPS,
667 "Invalid capability %d\n", caps->capability))
669 if (WARN(cpu_hwcaps_ptrs[caps->capability],
670 "Duplicate entry for capability %d\n",
673 cpu_hwcaps_ptrs[caps->capability] = caps;
677 static void __init init_cpu_hwcaps_indirect_list(void)
679 init_cpu_hwcaps_indirect_list_from_array(arm64_features);
680 init_cpu_hwcaps_indirect_list_from_array(arm64_errata);
683 static void __init setup_boot_cpu_capabilities(void);
685 void __init init_cpu_features(struct cpuinfo_arm64 *info)
687 /* Before we start using the tables, make sure it is sorted */
690 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
691 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
692 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
693 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
694 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
695 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
696 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
697 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
698 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
699 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
700 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
701 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
702 init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
704 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
705 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
706 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
707 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
708 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
709 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
710 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
711 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
712 init_cpu_ftr_reg(SYS_ID_ISAR6_EL1, info->reg_id_isar6);
713 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
714 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
715 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
716 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
717 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
718 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
719 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
720 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
721 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
724 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
725 init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
730 * Initialize the indirect array of CPU hwcaps capabilities pointers
731 * before we handle the boot CPU below.
733 init_cpu_hwcaps_indirect_list();
736 * Detect and enable early CPU capabilities based on the boot CPU,
737 * after we have initialised the CPU feature infrastructure.
739 setup_boot_cpu_capabilities();
742 static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
744 const struct arm64_ftr_bits *ftrp;
746 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
747 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
748 s64 ftr_new = arm64_ftr_value(ftrp, new);
750 if (ftr_cur == ftr_new)
752 /* Find a safe value */
753 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
754 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
759 static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
761 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
764 update_cpu_ftr_reg(regp, val);
765 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
767 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
768 regp->name, boot, cpu, val);
772 static void relax_cpu_ftr_reg(u32 sys_id, int field)
774 const struct arm64_ftr_bits *ftrp;
775 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
780 for (ftrp = regp->ftr_bits; ftrp->width; ftrp++) {
781 if (ftrp->shift == field) {
782 regp->strict_mask &= ~arm64_ftr_mask(ftrp);
788 WARN_ON(!ftrp->width);
791 static int update_32bit_cpu_features(int cpu, struct cpuinfo_arm64 *info,
792 struct cpuinfo_arm64 *boot)
795 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
798 * If we don't have AArch32 at all then skip the checks entirely
799 * as the register values may be UNKNOWN and we're not going to be
800 * using them for anything.
802 if (!id_aa64pfr0_32bit_el0(pfr0))
806 * If we don't have AArch32 at EL1, then relax the strictness of
807 * EL1-dependent register fields to avoid spurious sanity check fails.
809 if (!id_aa64pfr0_32bit_el1(pfr0)) {
810 relax_cpu_ftr_reg(SYS_ID_ISAR4_EL1, ID_ISAR4_SMC_SHIFT);
811 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRT_FRAC_SHIFT);
812 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SEC_FRAC_SHIFT);
813 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRTUALIZATION_SHIFT);
814 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SECURITY_SHIFT);
815 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_PROGMOD_SHIFT);
818 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
819 info->reg_id_dfr0, boot->reg_id_dfr0);
820 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
821 info->reg_id_isar0, boot->reg_id_isar0);
822 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
823 info->reg_id_isar1, boot->reg_id_isar1);
824 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
825 info->reg_id_isar2, boot->reg_id_isar2);
826 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
827 info->reg_id_isar3, boot->reg_id_isar3);
828 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
829 info->reg_id_isar4, boot->reg_id_isar4);
830 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
831 info->reg_id_isar5, boot->reg_id_isar5);
832 taint |= check_update_ftr_reg(SYS_ID_ISAR6_EL1, cpu,
833 info->reg_id_isar6, boot->reg_id_isar6);
836 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
837 * ACTLR formats could differ across CPUs and therefore would have to
838 * be trapped for virtualization anyway.
840 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
841 info->reg_id_mmfr0, boot->reg_id_mmfr0);
842 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
843 info->reg_id_mmfr1, boot->reg_id_mmfr1);
844 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
845 info->reg_id_mmfr2, boot->reg_id_mmfr2);
846 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
847 info->reg_id_mmfr3, boot->reg_id_mmfr3);
848 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
849 info->reg_id_pfr0, boot->reg_id_pfr0);
850 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
851 info->reg_id_pfr1, boot->reg_id_pfr1);
852 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
853 info->reg_mvfr0, boot->reg_mvfr0);
854 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
855 info->reg_mvfr1, boot->reg_mvfr1);
856 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
857 info->reg_mvfr2, boot->reg_mvfr2);
863 * Update system wide CPU feature registers with the values from a
864 * non-boot CPU. Also performs SANITY checks to make sure that there
865 * aren't any insane variations from that of the boot CPU.
867 void update_cpu_features(int cpu,
868 struct cpuinfo_arm64 *info,
869 struct cpuinfo_arm64 *boot)
874 * The kernel can handle differing I-cache policies, but otherwise
875 * caches should look identical. Userspace JITs will make use of
878 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
879 info->reg_ctr, boot->reg_ctr);
882 * Userspace may perform DC ZVA instructions. Mismatched block sizes
883 * could result in too much or too little memory being zeroed if a
884 * process is preempted and migrated between CPUs.
886 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
887 info->reg_dczid, boot->reg_dczid);
889 /* If different, timekeeping will be broken (especially with KVM) */
890 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
891 info->reg_cntfrq, boot->reg_cntfrq);
894 * The kernel uses self-hosted debug features and expects CPUs to
895 * support identical debug features. We presently need CTX_CMPs, WRPs,
896 * and BRPs to be identical.
897 * ID_AA64DFR1 is currently RES0.
899 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
900 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
901 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
902 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
904 * Even in big.LITTLE, processors should be identical instruction-set
907 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
908 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
909 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
910 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
913 * Differing PARange support is fine as long as all peripherals and
914 * memory are mapped within the minimum PARange of all CPUs.
915 * Linux should not care about secure memory.
917 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
918 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
919 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
920 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
921 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
922 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
924 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
925 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
926 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
927 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
929 taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
930 info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
932 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
933 taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
934 info->reg_zcr, boot->reg_zcr);
936 /* Probe vector lengths, unless we already gave up on SVE */
937 if (id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
938 !system_capabilities_finalized())
943 * This relies on a sanitised view of the AArch64 ID registers
944 * (e.g. SYS_ID_AA64PFR0_EL1), so we call it last.
946 taint |= update_32bit_cpu_features(cpu, info, boot);
949 * Mismatched CPU features are a recipe for disaster. Don't even
950 * pretend to support them.
953 pr_warn_once("Unsupported CPU feature variation detected.\n");
954 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
958 u64 read_sanitised_ftr_reg(u32 id)
960 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
962 /* We shouldn't get a request for an unsupported register */
964 return regp->sys_val;
967 #define read_sysreg_case(r) \
968 case r: return read_sysreg_s(r)
971 * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
972 * Read the system register on the current CPU
974 static u64 __read_sysreg_by_encoding(u32 sys_id)
977 read_sysreg_case(SYS_ID_PFR0_EL1);
978 read_sysreg_case(SYS_ID_PFR1_EL1);
979 read_sysreg_case(SYS_ID_DFR0_EL1);
980 read_sysreg_case(SYS_ID_MMFR0_EL1);
981 read_sysreg_case(SYS_ID_MMFR1_EL1);
982 read_sysreg_case(SYS_ID_MMFR2_EL1);
983 read_sysreg_case(SYS_ID_MMFR3_EL1);
984 read_sysreg_case(SYS_ID_ISAR0_EL1);
985 read_sysreg_case(SYS_ID_ISAR1_EL1);
986 read_sysreg_case(SYS_ID_ISAR2_EL1);
987 read_sysreg_case(SYS_ID_ISAR3_EL1);
988 read_sysreg_case(SYS_ID_ISAR4_EL1);
989 read_sysreg_case(SYS_ID_ISAR5_EL1);
990 read_sysreg_case(SYS_ID_ISAR6_EL1);
991 read_sysreg_case(SYS_MVFR0_EL1);
992 read_sysreg_case(SYS_MVFR1_EL1);
993 read_sysreg_case(SYS_MVFR2_EL1);
995 read_sysreg_case(SYS_ID_AA64PFR0_EL1);
996 read_sysreg_case(SYS_ID_AA64PFR1_EL1);
997 read_sysreg_case(SYS_ID_AA64ZFR0_EL1);
998 read_sysreg_case(SYS_ID_AA64DFR0_EL1);
999 read_sysreg_case(SYS_ID_AA64DFR1_EL1);
1000 read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
1001 read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
1002 read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
1003 read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
1004 read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
1006 read_sysreg_case(SYS_CNTFRQ_EL0);
1007 read_sysreg_case(SYS_CTR_EL0);
1008 read_sysreg_case(SYS_DCZID_EL0);
1016 #include <linux/irqchip/arm-gic-v3.h>
1019 feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
1021 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
1023 return val >= entry->min_field_value;
1027 has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
1031 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
1032 if (scope == SCOPE_SYSTEM)
1033 val = read_sanitised_ftr_reg(entry->sys_reg);
1035 val = __read_sysreg_by_encoding(entry->sys_reg);
1037 return feature_matches(val, entry);
1040 static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
1044 if (!has_cpuid_feature(entry, scope))
1047 has_sre = gic_enable_sre();
1049 pr_warn_once("%s present but disabled by higher exception level\n",
1055 static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
1057 u32 midr = read_cpuid_id();
1059 /* Cavium ThunderX pass 1.x and 2.x */
1060 return midr_is_cpu_model_range(midr, MIDR_THUNDERX,
1061 MIDR_CPU_VAR_REV(0, 0),
1062 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
1065 static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
1067 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
1069 return cpuid_feature_extract_signed_field(pfr0,
1070 ID_AA64PFR0_FP_SHIFT) < 0;
1073 static bool has_cache_idc(const struct arm64_cpu_capabilities *entry,
1078 if (scope == SCOPE_SYSTEM)
1079 ctr = arm64_ftr_reg_ctrel0.sys_val;
1081 ctr = read_cpuid_effective_cachetype();
1083 return ctr & BIT(CTR_IDC_SHIFT);
1086 static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused)
1089 * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively
1090 * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses
1091 * to the CTR_EL0 on this CPU and emulate it with the real/safe
1094 if (!(read_cpuid_cachetype() & BIT(CTR_IDC_SHIFT)))
1095 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
1098 static bool has_cache_dic(const struct arm64_cpu_capabilities *entry,
1103 if (scope == SCOPE_SYSTEM)
1104 ctr = arm64_ftr_reg_ctrel0.sys_val;
1106 ctr = read_cpuid_cachetype();
1108 return ctr & BIT(CTR_DIC_SHIFT);
1111 static bool __maybe_unused
1112 has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
1115 * Kdump isn't guaranteed to power-off all secondary CPUs, CNP
1116 * may share TLB entries with a CPU stuck in the crashed
1119 if (is_kdump_kernel())
1122 return has_cpuid_feature(entry, scope);
1126 * This check is triggered during the early boot before the cpufeature
1127 * is initialised. Checking the status on the local CPU allows the boot
1128 * CPU to detect the need for non-global mappings and thus avoiding a
1129 * pagetable re-write after all the CPUs are booted. This check will be
1130 * anyway run on individual CPUs, allowing us to get the consistent
1131 * state once the SMP CPUs are up and thus make the switch to non-global
1132 * mappings if required.
1134 bool kaslr_requires_kpti(void)
1136 if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
1140 * E0PD does a similar job to KPTI so can be used instead
1143 if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
1144 u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
1145 if (cpuid_feature_extract_unsigned_field(mmfr2,
1146 ID_AA64MMFR2_E0PD_SHIFT))
1151 * Systems affected by Cavium erratum 24756 are incompatible
1154 if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
1155 extern const struct midr_range cavium_erratum_27456_cpus[];
1157 if (is_midr_in_range_list(read_cpuid_id(),
1158 cavium_erratum_27456_cpus))
1162 return kaslr_offset() > 0;
1165 static bool __meltdown_safe = true;
1166 static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
1168 static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
1171 /* List of CPUs that are not vulnerable and don't need KPTI */
1172 static const struct midr_range kpti_safe_list[] = {
1173 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
1174 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
1175 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
1176 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
1177 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
1178 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
1179 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
1180 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
1181 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
1182 MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
1183 MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL),
1186 char const *str = "kpti command line option";
1189 meltdown_safe = is_midr_in_range_list(read_cpuid_id(), kpti_safe_list);
1191 /* Defer to CPU feature registers */
1192 if (has_cpuid_feature(entry, scope))
1193 meltdown_safe = true;
1196 __meltdown_safe = false;
1199 * For reasons that aren't entirely clear, enabling KPTI on Cavium
1200 * ThunderX leads to apparent I-cache corruption of kernel text, which
1201 * ends as well as you might imagine. Don't even try.
1203 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
1204 str = "ARM64_WORKAROUND_CAVIUM_27456";
1208 /* Useful for KASLR robustness */
1209 if (kaslr_requires_kpti()) {
1210 if (!__kpti_forced) {
1216 if (cpu_mitigations_off() && !__kpti_forced) {
1217 str = "mitigations=off";
1221 if (!IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) {
1222 pr_info_once("kernel page table isolation disabled by kernel configuration\n");
1227 if (__kpti_forced) {
1228 pr_info_once("kernel page table isolation forced %s by %s\n",
1229 __kpti_forced > 0 ? "ON" : "OFF", str);
1230 return __kpti_forced > 0;
1233 return !meltdown_safe;
1236 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
1238 kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
1240 typedef void (kpti_remap_fn)(int, int, phys_addr_t);
1241 extern kpti_remap_fn idmap_kpti_install_ng_mappings;
1242 kpti_remap_fn *remap_fn;
1244 int cpu = smp_processor_id();
1247 * We don't need to rewrite the page-tables if either we've done
1248 * it already or we have KASLR enabled and therefore have not
1249 * created any global mappings at all.
1251 if (arm64_use_ng_mappings)
1254 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
1256 cpu_install_idmap();
1257 remap_fn(cpu, num_online_cpus(), __pa_symbol(swapper_pg_dir));
1258 cpu_uninstall_idmap();
1261 arm64_use_ng_mappings = true;
1267 kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
1270 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1272 static int __init parse_kpti(char *str)
1275 int ret = strtobool(str, &enabled);
1280 __kpti_forced = enabled ? 1 : -1;
1283 early_param("kpti", parse_kpti);
1285 #ifdef CONFIG_ARM64_HW_AFDBM
1286 static inline void __cpu_enable_hw_dbm(void)
1288 u64 tcr = read_sysreg(tcr_el1) | TCR_HD;
1290 write_sysreg(tcr, tcr_el1);
1294 static bool cpu_has_broken_dbm(void)
1296 /* List of CPUs which have broken DBM support. */
1297 static const struct midr_range cpus[] = {
1298 #ifdef CONFIG_ARM64_ERRATUM_1024718
1299 MIDR_RANGE(MIDR_CORTEX_A55, 0, 0, 1, 0), // A55 r0p0 -r1p0
1304 return is_midr_in_range_list(read_cpuid_id(), cpus);
1307 static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap)
1309 return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) &&
1310 !cpu_has_broken_dbm();
1313 static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap)
1315 if (cpu_can_use_dbm(cap))
1316 __cpu_enable_hw_dbm();
1319 static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap,
1322 static bool detected = false;
1324 * DBM is a non-conflicting feature. i.e, the kernel can safely
1325 * run a mix of CPUs with and without the feature. So, we
1326 * unconditionally enable the capability to allow any late CPU
1327 * to use the feature. We only enable the control bits on the
1328 * CPU, if it actually supports.
1330 * We have to make sure we print the "feature" detection only
1331 * when at least one CPU actually uses it. So check if this CPU
1332 * can actually use it and print the message exactly once.
1334 * This is safe as all CPUs (including secondary CPUs - due to the
1335 * LOCAL_CPU scope - and the hotplugged CPUs - via verification)
1336 * goes through the "matches" check exactly once. Also if a CPU
1337 * matches the criteria, it is guaranteed that the CPU will turn
1338 * the DBM on, as the capability is unconditionally enabled.
1340 if (!detected && cpu_can_use_dbm(cap)) {
1342 pr_info("detected: Hardware dirty bit management\n");
1350 #ifdef CONFIG_ARM64_AMU_EXTN
1353 * The "amu_cpus" cpumask only signals that the CPU implementation for the
1354 * flagged CPUs supports the Activity Monitors Unit (AMU) but does not provide
1355 * information regarding all the events that it supports. When a CPU bit is
1356 * set in the cpumask, the user of this feature can only rely on the presence
1357 * of the 4 fixed counters for that CPU. But this does not guarantee that the
1358 * counters are enabled or access to these counters is enabled by code
1359 * executed at higher exception levels (firmware).
1361 static struct cpumask amu_cpus __read_mostly;
1363 bool cpu_has_amu_feat(int cpu)
1365 return cpumask_test_cpu(cpu, &amu_cpus);
1368 /* Initialize the use of AMU counters for frequency invariance */
1369 extern void init_cpu_freq_invariance_counters(void);
1371 static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap)
1373 if (has_cpuid_feature(cap, SCOPE_LOCAL_CPU)) {
1374 pr_info("detected CPU%d: Activity Monitors Unit (AMU)\n",
1375 smp_processor_id());
1376 cpumask_set_cpu(smp_processor_id(), &amu_cpus);
1377 init_cpu_freq_invariance_counters();
1381 static bool has_amu(const struct arm64_cpu_capabilities *cap,
1385 * The AMU extension is a non-conflicting feature: the kernel can
1386 * safely run a mix of CPUs with and without support for the
1387 * activity monitors extension. Therefore, unconditionally enable
1388 * the capability to allow any late CPU to use the feature.
1390 * With this feature unconditionally enabled, the cpu_enable
1391 * function will be called for all CPUs that match the criteria,
1392 * including secondary and hotplugged, marking this feature as
1393 * present on that respective CPU. The enable function will also
1394 * print a detection message.
1401 #ifdef CONFIG_ARM64_VHE
1402 static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
1404 return is_kernel_in_hyp_mode();
1407 static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
1410 * Copy register values that aren't redirected by hardware.
1412 * Before code patching, we only set tpidr_el1, all CPUs need to copy
1413 * this value to tpidr_el2 before we patch the code. Once we've done
1414 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
1417 if (!alternative_is_applied(ARM64_HAS_VIRT_HOST_EXTN))
1418 write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
1422 static void cpu_has_fwb(const struct arm64_cpu_capabilities *__unused)
1424 u64 val = read_sysreg_s(SYS_CLIDR_EL1);
1426 /* Check that CLIDR_EL1.LOU{U,IS} are both 0 */
1427 WARN_ON(val & (7 << 27 | 7 << 21));
1430 #ifdef CONFIG_ARM64_SSBD
1431 static int ssbs_emulation_handler(struct pt_regs *regs, u32 instr)
1433 if (user_mode(regs))
1436 if (instr & BIT(PSTATE_Imm_shift))
1437 regs->pstate |= PSR_SSBS_BIT;
1439 regs->pstate &= ~PSR_SSBS_BIT;
1441 arm64_skip_faulting_instruction(regs, 4);
1445 static struct undef_hook ssbs_emulation_hook = {
1446 .instr_mask = ~(1U << PSTATE_Imm_shift),
1447 .instr_val = 0xd500401f | PSTATE_SSBS,
1448 .fn = ssbs_emulation_handler,
1451 static void cpu_enable_ssbs(const struct arm64_cpu_capabilities *__unused)
1453 static bool undef_hook_registered = false;
1454 static DEFINE_RAW_SPINLOCK(hook_lock);
1456 raw_spin_lock(&hook_lock);
1457 if (!undef_hook_registered) {
1458 register_undef_hook(&ssbs_emulation_hook);
1459 undef_hook_registered = true;
1461 raw_spin_unlock(&hook_lock);
1463 if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) {
1464 sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
1465 arm64_set_ssbd_mitigation(false);
1467 arm64_set_ssbd_mitigation(true);
1470 #endif /* CONFIG_ARM64_SSBD */
1472 #ifdef CONFIG_ARM64_PAN
1473 static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
1476 * We modify PSTATE. This won't work from irq context as the PSTATE
1477 * is discarded once we return from the exception.
1479 WARN_ON_ONCE(in_interrupt());
1481 sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
1482 asm(SET_PSTATE_PAN(1));
1484 #endif /* CONFIG_ARM64_PAN */
1486 #ifdef CONFIG_ARM64_RAS_EXTN
1487 static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
1489 /* Firmware may have left a deferred SError in this register. */
1490 write_sysreg_s(0, SYS_DISR_EL1);
1492 #endif /* CONFIG_ARM64_RAS_EXTN */
1494 #ifdef CONFIG_ARM64_PTR_AUTH
1495 static bool has_address_auth(const struct arm64_cpu_capabilities *entry,
1498 return __system_matches_cap(ARM64_HAS_ADDRESS_AUTH_ARCH) ||
1499 __system_matches_cap(ARM64_HAS_ADDRESS_AUTH_IMP_DEF);
1502 static bool has_generic_auth(const struct arm64_cpu_capabilities *entry,
1505 return __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH) ||
1506 __system_matches_cap(ARM64_HAS_GENERIC_AUTH_IMP_DEF);
1508 #endif /* CONFIG_ARM64_PTR_AUTH */
1510 #ifdef CONFIG_ARM64_E0PD
1511 static void cpu_enable_e0pd(struct arm64_cpu_capabilities const *cap)
1513 if (this_cpu_has_cap(ARM64_HAS_E0PD))
1514 sysreg_clear_set(tcr_el1, 0, TCR_E0PD1);
1516 #endif /* CONFIG_ARM64_E0PD */
1518 #ifdef CONFIG_ARM64_PSEUDO_NMI
1519 static bool enable_pseudo_nmi;
1521 static int __init early_enable_pseudo_nmi(char *p)
1523 return strtobool(p, &enable_pseudo_nmi);
1525 early_param("irqchip.gicv3_pseudo_nmi", early_enable_pseudo_nmi);
1527 static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry,
1530 return enable_pseudo_nmi && has_useable_gicv3_cpuif(entry, scope);
1534 /* Internal helper functions to match cpu capability type */
1536 cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap)
1538 return !!(cap->type & ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU);
1542 cpucap_late_cpu_permitted(const struct arm64_cpu_capabilities *cap)
1544 return !!(cap->type & ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU);
1548 cpucap_panic_on_conflict(const struct arm64_cpu_capabilities *cap)
1550 return !!(cap->type & ARM64_CPUCAP_PANIC_ON_CONFLICT);
1553 static const struct arm64_cpu_capabilities arm64_features[] = {
1555 .desc = "GIC system register CPU interface",
1556 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
1557 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
1558 .matches = has_useable_gicv3_cpuif,
1559 .sys_reg = SYS_ID_AA64PFR0_EL1,
1560 .field_pos = ID_AA64PFR0_GIC_SHIFT,
1561 .sign = FTR_UNSIGNED,
1562 .min_field_value = 1,
1564 #ifdef CONFIG_ARM64_PAN
1566 .desc = "Privileged Access Never",
1567 .capability = ARM64_HAS_PAN,
1568 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1569 .matches = has_cpuid_feature,
1570 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1571 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
1572 .sign = FTR_UNSIGNED,
1573 .min_field_value = 1,
1574 .cpu_enable = cpu_enable_pan,
1576 #endif /* CONFIG_ARM64_PAN */
1577 #ifdef CONFIG_ARM64_LSE_ATOMICS
1579 .desc = "LSE atomic instructions",
1580 .capability = ARM64_HAS_LSE_ATOMICS,
1581 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1582 .matches = has_cpuid_feature,
1583 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1584 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
1585 .sign = FTR_UNSIGNED,
1586 .min_field_value = 2,
1588 #endif /* CONFIG_ARM64_LSE_ATOMICS */
1590 .desc = "Software prefetching using PRFM",
1591 .capability = ARM64_HAS_NO_HW_PREFETCH,
1592 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1593 .matches = has_no_hw_prefetch,
1595 #ifdef CONFIG_ARM64_UAO
1597 .desc = "User Access Override",
1598 .capability = ARM64_HAS_UAO,
1599 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1600 .matches = has_cpuid_feature,
1601 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1602 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
1603 .min_field_value = 1,
1605 * We rely on stop_machine() calling uao_thread_switch() to set
1606 * UAO immediately after patching.
1609 #endif /* CONFIG_ARM64_UAO */
1610 #ifdef CONFIG_ARM64_PAN
1612 .capability = ARM64_ALT_PAN_NOT_UAO,
1613 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1614 .matches = cpufeature_pan_not_uao,
1616 #endif /* CONFIG_ARM64_PAN */
1617 #ifdef CONFIG_ARM64_VHE
1619 .desc = "Virtualization Host Extensions",
1620 .capability = ARM64_HAS_VIRT_HOST_EXTN,
1621 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
1622 .matches = runs_at_el2,
1623 .cpu_enable = cpu_copy_el2regs,
1625 #endif /* CONFIG_ARM64_VHE */
1627 .desc = "32-bit EL0 Support",
1628 .capability = ARM64_HAS_32BIT_EL0,
1629 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1630 .matches = has_cpuid_feature,
1631 .sys_reg = SYS_ID_AA64PFR0_EL1,
1632 .sign = FTR_UNSIGNED,
1633 .field_pos = ID_AA64PFR0_EL0_SHIFT,
1634 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
1638 .desc = "32-bit EL1 Support",
1639 .capability = ARM64_HAS_32BIT_EL1,
1640 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1641 .matches = has_cpuid_feature,
1642 .sys_reg = SYS_ID_AA64PFR0_EL1,
1643 .sign = FTR_UNSIGNED,
1644 .field_pos = ID_AA64PFR0_EL1_SHIFT,
1645 .min_field_value = ID_AA64PFR0_EL1_32BIT_64BIT,
1649 .desc = "Kernel page table isolation (KPTI)",
1650 .capability = ARM64_UNMAP_KERNEL_AT_EL0,
1651 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
1653 * The ID feature fields below are used to indicate that
1654 * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for
1657 .sys_reg = SYS_ID_AA64PFR0_EL1,
1658 .field_pos = ID_AA64PFR0_CSV3_SHIFT,
1659 .min_field_value = 1,
1660 .matches = unmap_kernel_at_el0,
1661 .cpu_enable = kpti_install_ng_mappings,
1664 /* FP/SIMD is not implemented */
1665 .capability = ARM64_HAS_NO_FPSIMD,
1666 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
1667 .min_field_value = 0,
1668 .matches = has_no_fpsimd,
1670 #ifdef CONFIG_ARM64_PMEM
1672 .desc = "Data cache clean to Point of Persistence",
1673 .capability = ARM64_HAS_DCPOP,
1674 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1675 .matches = has_cpuid_feature,
1676 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1677 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1678 .min_field_value = 1,
1681 .desc = "Data cache clean to Point of Deep Persistence",
1682 .capability = ARM64_HAS_DCPODP,
1683 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1684 .matches = has_cpuid_feature,
1685 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1686 .sign = FTR_UNSIGNED,
1687 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1688 .min_field_value = 2,
1691 #ifdef CONFIG_ARM64_SVE
1693 .desc = "Scalable Vector Extension",
1694 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1695 .capability = ARM64_SVE,
1696 .sys_reg = SYS_ID_AA64PFR0_EL1,
1697 .sign = FTR_UNSIGNED,
1698 .field_pos = ID_AA64PFR0_SVE_SHIFT,
1699 .min_field_value = ID_AA64PFR0_SVE,
1700 .matches = has_cpuid_feature,
1701 .cpu_enable = sve_kernel_enable,
1703 #endif /* CONFIG_ARM64_SVE */
1704 #ifdef CONFIG_ARM64_RAS_EXTN
1706 .desc = "RAS Extension Support",
1707 .capability = ARM64_HAS_RAS_EXTN,
1708 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1709 .matches = has_cpuid_feature,
1710 .sys_reg = SYS_ID_AA64PFR0_EL1,
1711 .sign = FTR_UNSIGNED,
1712 .field_pos = ID_AA64PFR0_RAS_SHIFT,
1713 .min_field_value = ID_AA64PFR0_RAS_V1,
1714 .cpu_enable = cpu_clear_disr,
1716 #endif /* CONFIG_ARM64_RAS_EXTN */
1717 #ifdef CONFIG_ARM64_AMU_EXTN
1720 * The feature is enabled by default if CONFIG_ARM64_AMU_EXTN=y.
1721 * Therefore, don't provide .desc as we don't want the detection
1722 * message to be shown until at least one CPU is detected to
1723 * support the feature.
1725 .capability = ARM64_HAS_AMU_EXTN,
1726 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1728 .sys_reg = SYS_ID_AA64PFR0_EL1,
1729 .sign = FTR_UNSIGNED,
1730 .field_pos = ID_AA64PFR0_AMU_SHIFT,
1731 .min_field_value = ID_AA64PFR0_AMU,
1732 .cpu_enable = cpu_amu_enable,
1734 #endif /* CONFIG_ARM64_AMU_EXTN */
1736 .desc = "Data cache clean to the PoU not required for I/D coherence",
1737 .capability = ARM64_HAS_CACHE_IDC,
1738 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1739 .matches = has_cache_idc,
1740 .cpu_enable = cpu_emulate_effective_ctr,
1743 .desc = "Instruction cache invalidation not required for I/D coherence",
1744 .capability = ARM64_HAS_CACHE_DIC,
1745 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1746 .matches = has_cache_dic,
1749 .desc = "Stage-2 Force Write-Back",
1750 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1751 .capability = ARM64_HAS_STAGE2_FWB,
1752 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1753 .sign = FTR_UNSIGNED,
1754 .field_pos = ID_AA64MMFR2_FWB_SHIFT,
1755 .min_field_value = 1,
1756 .matches = has_cpuid_feature,
1757 .cpu_enable = cpu_has_fwb,
1759 #ifdef CONFIG_ARM64_HW_AFDBM
1762 * Since we turn this on always, we don't want the user to
1763 * think that the feature is available when it may not be.
1764 * So hide the description.
1766 * .desc = "Hardware pagetable Dirty Bit Management",
1769 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1770 .capability = ARM64_HW_DBM,
1771 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1772 .sign = FTR_UNSIGNED,
1773 .field_pos = ID_AA64MMFR1_HADBS_SHIFT,
1774 .min_field_value = 2,
1775 .matches = has_hw_dbm,
1776 .cpu_enable = cpu_enable_hw_dbm,
1780 .desc = "CRC32 instructions",
1781 .capability = ARM64_HAS_CRC32,
1782 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1783 .matches = has_cpuid_feature,
1784 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1785 .field_pos = ID_AA64ISAR0_CRC32_SHIFT,
1786 .min_field_value = 1,
1788 #ifdef CONFIG_ARM64_SSBD
1790 .desc = "Speculative Store Bypassing Safe (SSBS)",
1791 .capability = ARM64_SSBS,
1792 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1793 .matches = has_cpuid_feature,
1794 .sys_reg = SYS_ID_AA64PFR1_EL1,
1795 .field_pos = ID_AA64PFR1_SSBS_SHIFT,
1796 .sign = FTR_UNSIGNED,
1797 .min_field_value = ID_AA64PFR1_SSBS_PSTATE_ONLY,
1798 .cpu_enable = cpu_enable_ssbs,
1801 #ifdef CONFIG_ARM64_CNP
1803 .desc = "Common not Private translations",
1804 .capability = ARM64_HAS_CNP,
1805 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1806 .matches = has_useable_cnp,
1807 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1808 .sign = FTR_UNSIGNED,
1809 .field_pos = ID_AA64MMFR2_CNP_SHIFT,
1810 .min_field_value = 1,
1811 .cpu_enable = cpu_enable_cnp,
1815 .desc = "Speculation barrier (SB)",
1816 .capability = ARM64_HAS_SB,
1817 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1818 .matches = has_cpuid_feature,
1819 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1820 .field_pos = ID_AA64ISAR1_SB_SHIFT,
1821 .sign = FTR_UNSIGNED,
1822 .min_field_value = 1,
1824 #ifdef CONFIG_ARM64_PTR_AUTH
1826 .desc = "Address authentication (architected algorithm)",
1827 .capability = ARM64_HAS_ADDRESS_AUTH_ARCH,
1828 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
1829 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1830 .sign = FTR_UNSIGNED,
1831 .field_pos = ID_AA64ISAR1_APA_SHIFT,
1832 .min_field_value = ID_AA64ISAR1_APA_ARCHITECTED,
1833 .matches = has_cpuid_feature,
1836 .desc = "Address authentication (IMP DEF algorithm)",
1837 .capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF,
1838 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
1839 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1840 .sign = FTR_UNSIGNED,
1841 .field_pos = ID_AA64ISAR1_API_SHIFT,
1842 .min_field_value = ID_AA64ISAR1_API_IMP_DEF,
1843 .matches = has_cpuid_feature,
1846 .capability = ARM64_HAS_ADDRESS_AUTH,
1847 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
1848 .matches = has_address_auth,
1851 .desc = "Generic authentication (architected algorithm)",
1852 .capability = ARM64_HAS_GENERIC_AUTH_ARCH,
1853 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1854 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1855 .sign = FTR_UNSIGNED,
1856 .field_pos = ID_AA64ISAR1_GPA_SHIFT,
1857 .min_field_value = ID_AA64ISAR1_GPA_ARCHITECTED,
1858 .matches = has_cpuid_feature,
1861 .desc = "Generic authentication (IMP DEF algorithm)",
1862 .capability = ARM64_HAS_GENERIC_AUTH_IMP_DEF,
1863 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1864 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1865 .sign = FTR_UNSIGNED,
1866 .field_pos = ID_AA64ISAR1_GPI_SHIFT,
1867 .min_field_value = ID_AA64ISAR1_GPI_IMP_DEF,
1868 .matches = has_cpuid_feature,
1871 .capability = ARM64_HAS_GENERIC_AUTH,
1872 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1873 .matches = has_generic_auth,
1875 #endif /* CONFIG_ARM64_PTR_AUTH */
1876 #ifdef CONFIG_ARM64_PSEUDO_NMI
1879 * Depends on having GICv3
1881 .desc = "IRQ priority masking",
1882 .capability = ARM64_HAS_IRQ_PRIO_MASKING,
1883 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
1884 .matches = can_use_gic_priorities,
1885 .sys_reg = SYS_ID_AA64PFR0_EL1,
1886 .field_pos = ID_AA64PFR0_GIC_SHIFT,
1887 .sign = FTR_UNSIGNED,
1888 .min_field_value = 1,
1891 #ifdef CONFIG_ARM64_E0PD
1894 .capability = ARM64_HAS_E0PD,
1895 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1896 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1897 .sign = FTR_UNSIGNED,
1898 .field_pos = ID_AA64MMFR2_E0PD_SHIFT,
1899 .matches = has_cpuid_feature,
1900 .min_field_value = 1,
1901 .cpu_enable = cpu_enable_e0pd,
1904 #ifdef CONFIG_ARCH_RANDOM
1906 .desc = "Random Number Generator",
1907 .capability = ARM64_HAS_RNG,
1908 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1909 .matches = has_cpuid_feature,
1910 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1911 .field_pos = ID_AA64ISAR0_RNDR_SHIFT,
1912 .sign = FTR_UNSIGNED,
1913 .min_field_value = 1,
1919 #define HWCAP_CPUID_MATCH(reg, field, s, min_value) \
1920 .matches = has_cpuid_feature, \
1922 .field_pos = field, \
1924 .min_field_value = min_value,
1926 #define __HWCAP_CAP(name, cap_type, cap) \
1928 .type = ARM64_CPUCAP_SYSTEM_FEATURE, \
1929 .hwcap_type = cap_type, \
1932 #define HWCAP_CAP(reg, field, s, min_value, cap_type, cap) \
1934 __HWCAP_CAP(#cap, cap_type, cap) \
1935 HWCAP_CPUID_MATCH(reg, field, s, min_value) \
1938 #define HWCAP_MULTI_CAP(list, cap_type, cap) \
1940 __HWCAP_CAP(#cap, cap_type, cap) \
1941 .matches = cpucap_multi_entry_cap_matches, \
1942 .match_list = list, \
1945 #define HWCAP_CAP_MATCH(match, cap_type, cap) \
1947 __HWCAP_CAP(#cap, cap_type, cap) \
1951 #ifdef CONFIG_ARM64_PTR_AUTH
1952 static const struct arm64_cpu_capabilities ptr_auth_hwcap_addr_matches[] = {
1954 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_APA_SHIFT,
1955 FTR_UNSIGNED, ID_AA64ISAR1_APA_ARCHITECTED)
1958 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_API_SHIFT,
1959 FTR_UNSIGNED, ID_AA64ISAR1_API_IMP_DEF)
1964 static const struct arm64_cpu_capabilities ptr_auth_hwcap_gen_matches[] = {
1966 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_GPA_SHIFT,
1967 FTR_UNSIGNED, ID_AA64ISAR1_GPA_ARCHITECTED)
1970 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_GPI_SHIFT,
1971 FTR_UNSIGNED, ID_AA64ISAR1_GPI_IMP_DEF)
1977 static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
1978 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_PMULL),
1979 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_AES),
1980 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA1),
1981 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA2),
1982 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_SHA512),
1983 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_CRC32),
1984 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ATOMICS),
1985 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RDM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDRDM),
1986 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA3),
1987 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM3),
1988 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM4),
1989 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_DP_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDDP),
1990 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_FHM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM),
1991 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FLAGM),
1992 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_FLAGM2),
1993 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RNDR_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RNG),
1994 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_FP),
1995 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FPHP),
1996 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_ASIMD),
1997 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDHP),
1998 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_DIT_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DIT),
1999 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DPB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DCPOP),
2000 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DPB_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_DCPODP),
2001 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_JSCVT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_JSCVT),
2002 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FCMA_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FCMA),
2003 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_LRCPC),
2004 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ILRCPC),
2005 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FRINTTS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FRINT),
2006 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_SB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SB),
2007 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_BF16_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_BF16),
2008 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DGH_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DGH),
2009 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_I8MM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_I8MM),
2010 HWCAP_CAP(SYS_ID_AA64MMFR2_EL1, ID_AA64MMFR2_AT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_USCAT),
2011 #ifdef CONFIG_ARM64_SVE
2012 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_SVE_SHIFT, FTR_UNSIGNED, ID_AA64PFR0_SVE, CAP_HWCAP, KERNEL_HWCAP_SVE),
2013 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SVEVER_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SVEVER_SVE2, CAP_HWCAP, KERNEL_HWCAP_SVE2),
2014 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_AES_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_AES, CAP_HWCAP, KERNEL_HWCAP_SVEAES),
2015 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_AES_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_AES_PMULL, CAP_HWCAP, KERNEL_HWCAP_SVEPMULL),
2016 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_BITPERM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_BITPERM, CAP_HWCAP, KERNEL_HWCAP_SVEBITPERM),
2017 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_BF16_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_BF16, CAP_HWCAP, KERNEL_HWCAP_SVEBF16),
2018 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SHA3_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SHA3, CAP_HWCAP, KERNEL_HWCAP_SVESHA3),
2019 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SM4_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SM4, CAP_HWCAP, KERNEL_HWCAP_SVESM4),
2020 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_I8MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_I8MM, CAP_HWCAP, KERNEL_HWCAP_SVEI8MM),
2021 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_F32MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_F32MM, CAP_HWCAP, KERNEL_HWCAP_SVEF32MM),
2022 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_F64MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_F64MM, CAP_HWCAP, KERNEL_HWCAP_SVEF64MM),
2024 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_SSBS_SHIFT, FTR_UNSIGNED, ID_AA64PFR1_SSBS_PSTATE_INSNS, CAP_HWCAP, KERNEL_HWCAP_SSBS),
2025 #ifdef CONFIG_ARM64_PTR_AUTH
2026 HWCAP_MULTI_CAP(ptr_auth_hwcap_addr_matches, CAP_HWCAP, KERNEL_HWCAP_PACA),
2027 HWCAP_MULTI_CAP(ptr_auth_hwcap_gen_matches, CAP_HWCAP, KERNEL_HWCAP_PACG),
2032 #ifdef CONFIG_COMPAT
2033 static bool compat_has_neon(const struct arm64_cpu_capabilities *cap, int scope)
2036 * Check that all of MVFR1_EL1.{SIMDSP, SIMDInt, SIMDLS} are available,
2037 * in line with that of arm32 as in vfp_init(). We make sure that the
2038 * check is future proof, by making sure value is non-zero.
2042 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
2043 if (scope == SCOPE_SYSTEM)
2044 mvfr1 = read_sanitised_ftr_reg(SYS_MVFR1_EL1);
2046 mvfr1 = read_sysreg_s(SYS_MVFR1_EL1);
2048 return cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDSP_SHIFT) &&
2049 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDINT_SHIFT) &&
2050 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDLS_SHIFT);
2054 static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
2055 #ifdef CONFIG_COMPAT
2056 HWCAP_CAP_MATCH(compat_has_neon, CAP_COMPAT_HWCAP, COMPAT_HWCAP_NEON),
2057 HWCAP_CAP(SYS_MVFR1_EL1, MVFR1_SIMDFMAC_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv4),
2058 /* Arm v8 mandates MVFR0.FPDP == {0, 2}. So, piggy back on this for the presence of VFP support */
2059 HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_FPDP_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFP),
2060 HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_FPDP_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv3),
2061 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
2062 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
2063 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
2064 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
2065 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32),
2070 static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
2072 switch (cap->hwcap_type) {
2074 cpu_set_feature(cap->hwcap);
2076 #ifdef CONFIG_COMPAT
2077 case CAP_COMPAT_HWCAP:
2078 compat_elf_hwcap |= (u32)cap->hwcap;
2080 case CAP_COMPAT_HWCAP2:
2081 compat_elf_hwcap2 |= (u32)cap->hwcap;
2090 /* Check if we have a particular HWCAP enabled */
2091 static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
2095 switch (cap->hwcap_type) {
2097 rc = cpu_have_feature(cap->hwcap);
2099 #ifdef CONFIG_COMPAT
2100 case CAP_COMPAT_HWCAP:
2101 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
2103 case CAP_COMPAT_HWCAP2:
2104 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
2115 static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
2117 /* We support emulation of accesses to CPU ID feature registers */
2118 cpu_set_named_feature(CPUID);
2119 for (; hwcaps->matches; hwcaps++)
2120 if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
2121 cap_set_elf_hwcap(hwcaps);
2124 static void update_cpu_capabilities(u16 scope_mask)
2127 const struct arm64_cpu_capabilities *caps;
2129 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
2130 for (i = 0; i < ARM64_NCAPS; i++) {
2131 caps = cpu_hwcaps_ptrs[i];
2132 if (!caps || !(caps->type & scope_mask) ||
2133 cpus_have_cap(caps->capability) ||
2134 !caps->matches(caps, cpucap_default_scope(caps)))
2138 pr_info("detected: %s\n", caps->desc);
2139 cpus_set_cap(caps->capability);
2141 if ((scope_mask & SCOPE_BOOT_CPU) && (caps->type & SCOPE_BOOT_CPU))
2142 set_bit(caps->capability, boot_capabilities);
2147 * Enable all the available capabilities on this CPU. The capabilities
2148 * with BOOT_CPU scope are handled separately and hence skipped here.
2150 static int cpu_enable_non_boot_scope_capabilities(void *__unused)
2153 u16 non_boot_scope = SCOPE_ALL & ~SCOPE_BOOT_CPU;
2155 for_each_available_cap(i) {
2156 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[i];
2161 if (!(cap->type & non_boot_scope))
2164 if (cap->cpu_enable)
2165 cap->cpu_enable(cap);
2171 * Run through the enabled capabilities and enable() it on all active
2174 static void __init enable_cpu_capabilities(u16 scope_mask)
2177 const struct arm64_cpu_capabilities *caps;
2180 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
2181 boot_scope = !!(scope_mask & SCOPE_BOOT_CPU);
2183 for (i = 0; i < ARM64_NCAPS; i++) {
2186 caps = cpu_hwcaps_ptrs[i];
2187 if (!caps || !(caps->type & scope_mask))
2189 num = caps->capability;
2190 if (!cpus_have_cap(num))
2193 /* Ensure cpus_have_const_cap(num) works */
2194 static_branch_enable(&cpu_hwcap_keys[num]);
2196 if (boot_scope && caps->cpu_enable)
2198 * Capabilities with SCOPE_BOOT_CPU scope are finalised
2199 * before any secondary CPU boots. Thus, each secondary
2200 * will enable the capability as appropriate via
2201 * check_local_cpu_capabilities(). The only exception is
2202 * the boot CPU, for which the capability must be
2203 * enabled here. This approach avoids costly
2204 * stop_machine() calls for this case.
2206 caps->cpu_enable(caps);
2210 * For all non-boot scope capabilities, use stop_machine()
2211 * as it schedules the work allowing us to modify PSTATE,
2212 * instead of on_each_cpu() which uses an IPI, giving us a
2213 * PSTATE that disappears when we return.
2216 stop_machine(cpu_enable_non_boot_scope_capabilities,
2217 NULL, cpu_online_mask);
2221 * Run through the list of capabilities to check for conflicts.
2222 * If the system has already detected a capability, take necessary
2223 * action on this CPU.
2225 static void verify_local_cpu_caps(u16 scope_mask)
2228 bool cpu_has_cap, system_has_cap;
2229 const struct arm64_cpu_capabilities *caps;
2231 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
2233 for (i = 0; i < ARM64_NCAPS; i++) {
2234 caps = cpu_hwcaps_ptrs[i];
2235 if (!caps || !(caps->type & scope_mask))
2238 cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU);
2239 system_has_cap = cpus_have_cap(caps->capability);
2241 if (system_has_cap) {
2243 * Check if the new CPU misses an advertised feature,
2244 * which is not safe to miss.
2246 if (!cpu_has_cap && !cpucap_late_cpu_optional(caps))
2249 * We have to issue cpu_enable() irrespective of
2250 * whether the CPU has it or not, as it is enabeld
2251 * system wide. It is upto the call back to take
2252 * appropriate action on this CPU.
2254 if (caps->cpu_enable)
2255 caps->cpu_enable(caps);
2258 * Check if the CPU has this capability if it isn't
2259 * safe to have when the system doesn't.
2261 if (cpu_has_cap && !cpucap_late_cpu_permitted(caps))
2266 if (i < ARM64_NCAPS) {
2267 pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n",
2268 smp_processor_id(), caps->capability,
2269 caps->desc, system_has_cap, cpu_has_cap);
2271 if (cpucap_panic_on_conflict(caps))
2279 * Check for CPU features that are used in early boot
2280 * based on the Boot CPU value.
2282 static void check_early_cpu_features(void)
2284 verify_cpu_asid_bits();
2286 verify_local_cpu_caps(SCOPE_BOOT_CPU);
2290 verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
2293 for (; caps->matches; caps++)
2294 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
2295 pr_crit("CPU%d: missing HWCAP: %s\n",
2296 smp_processor_id(), caps->desc);
2301 static void verify_sve_features(void)
2303 u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
2304 u64 zcr = read_zcr_features();
2306 unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
2307 unsigned int len = zcr & ZCR_ELx_LEN_MASK;
2309 if (len < safe_len || sve_verify_vq_map()) {
2310 pr_crit("CPU%d: SVE: vector length support mismatch\n",
2311 smp_processor_id());
2315 /* Add checks on other ZCR bits here if necessary */
2318 static void verify_hyp_capabilities(void)
2320 u64 safe_mmfr1, mmfr0, mmfr1;
2321 int parange, ipa_max;
2322 unsigned int safe_vmid_bits, vmid_bits;
2324 if (!IS_ENABLED(CONFIG_KVM) || !IS_ENABLED(CONFIG_KVM_ARM_HOST))
2327 safe_mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
2328 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
2329 mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
2331 /* Verify VMID bits */
2332 safe_vmid_bits = get_vmid_bits(safe_mmfr1);
2333 vmid_bits = get_vmid_bits(mmfr1);
2334 if (vmid_bits < safe_vmid_bits) {
2335 pr_crit("CPU%d: VMID width mismatch\n", smp_processor_id());
2339 /* Verify IPA range */
2340 parange = mmfr0 & 0x7;
2341 ipa_max = id_aa64mmfr0_parange_to_phys_shift(parange);
2342 if (ipa_max < get_kvm_ipa_limit()) {
2343 pr_crit("CPU%d: IPA range mismatch\n", smp_processor_id());
2349 * Run through the enabled system capabilities and enable() it on this CPU.
2350 * The capabilities were decided based on the available CPUs at the boot time.
2351 * Any new CPU should match the system wide status of the capability. If the
2352 * new CPU doesn't have a capability which the system now has enabled, we
2353 * cannot do anything to fix it up and could cause unexpected failures. So
2356 static void verify_local_cpu_capabilities(void)
2359 * The capabilities with SCOPE_BOOT_CPU are checked from
2360 * check_early_cpu_features(), as they need to be verified
2361 * on all secondary CPUs.
2363 verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU);
2365 verify_local_elf_hwcaps(arm64_elf_hwcaps);
2367 if (system_supports_32bit_el0())
2368 verify_local_elf_hwcaps(compat_elf_hwcaps);
2370 if (system_supports_sve())
2371 verify_sve_features();
2373 if (is_hyp_mode_available())
2374 verify_hyp_capabilities();
2377 void check_local_cpu_capabilities(void)
2380 * All secondary CPUs should conform to the early CPU features
2381 * in use by the kernel based on boot CPU.
2383 check_early_cpu_features();
2386 * If we haven't finalised the system capabilities, this CPU gets
2387 * a chance to update the errata work arounds and local features.
2388 * Otherwise, this CPU should verify that it has all the system
2389 * advertised capabilities.
2391 if (!system_capabilities_finalized())
2392 update_cpu_capabilities(SCOPE_LOCAL_CPU);
2394 verify_local_cpu_capabilities();
2397 static void __init setup_boot_cpu_capabilities(void)
2399 /* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */
2400 update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU);
2401 /* Enable the SCOPE_BOOT_CPU capabilities alone right away */
2402 enable_cpu_capabilities(SCOPE_BOOT_CPU);
2405 bool this_cpu_has_cap(unsigned int n)
2407 if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
2408 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
2411 return cap->matches(cap, SCOPE_LOCAL_CPU);
2418 * This helper function is used in a narrow window when,
2419 * - The system wide safe registers are set with all the SMP CPUs and,
2420 * - The SYSTEM_FEATURE cpu_hwcaps may not have been set.
2421 * In all other cases cpus_have_{const_}cap() should be used.
2423 static bool __system_matches_cap(unsigned int n)
2425 if (n < ARM64_NCAPS) {
2426 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
2429 return cap->matches(cap, SCOPE_SYSTEM);
2434 void cpu_set_feature(unsigned int num)
2436 WARN_ON(num >= MAX_CPU_FEATURES);
2437 elf_hwcap |= BIT(num);
2439 EXPORT_SYMBOL_GPL(cpu_set_feature);
2441 bool cpu_have_feature(unsigned int num)
2443 WARN_ON(num >= MAX_CPU_FEATURES);
2444 return elf_hwcap & BIT(num);
2446 EXPORT_SYMBOL_GPL(cpu_have_feature);
2448 unsigned long cpu_get_elf_hwcap(void)
2451 * We currently only populate the first 32 bits of AT_HWCAP. Please
2452 * note that for userspace compatibility we guarantee that bits 62
2453 * and 63 will always be returned as 0.
2455 return lower_32_bits(elf_hwcap);
2458 unsigned long cpu_get_elf_hwcap2(void)
2460 return upper_32_bits(elf_hwcap);
2463 static void __init setup_system_capabilities(void)
2466 * We have finalised the system-wide safe feature
2467 * registers, finalise the capabilities that depend
2468 * on it. Also enable all the available capabilities,
2469 * that are not enabled already.
2471 update_cpu_capabilities(SCOPE_SYSTEM);
2472 enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
2475 void __init setup_cpu_features(void)
2479 setup_system_capabilities();
2480 setup_elf_hwcaps(arm64_elf_hwcaps);
2482 if (system_supports_32bit_el0())
2483 setup_elf_hwcaps(compat_elf_hwcaps);
2485 if (system_uses_ttbr0_pan())
2486 pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
2489 minsigstksz_setup();
2491 /* Advertise that we have computed the system capabilities */
2492 finalize_system_capabilities();
2495 * Check for sane CTR_EL0.CWG value.
2497 cwg = cache_type_cwg();
2499 pr_warn("No Cache Writeback Granule information, assuming %d\n",
2503 static bool __maybe_unused
2504 cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
2506 return (__system_matches_cap(ARM64_HAS_PAN) && !__system_matches_cap(ARM64_HAS_UAO));
2509 static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap)
2511 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
2515 * We emulate only the following system register space.
2516 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 4 - 7]
2517 * See Table C5-6 System instruction encodings for System register accesses,
2518 * ARMv8 ARM(ARM DDI 0487A.f) for more details.
2520 static inline bool __attribute_const__ is_emulated(u32 id)
2522 return (sys_reg_Op0(id) == 0x3 &&
2523 sys_reg_CRn(id) == 0x0 &&
2524 sys_reg_Op1(id) == 0x0 &&
2525 (sys_reg_CRm(id) == 0 ||
2526 ((sys_reg_CRm(id) >= 4) && (sys_reg_CRm(id) <= 7))));
2530 * With CRm == 0, reg should be one of :
2531 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
2533 static inline int emulate_id_reg(u32 id, u64 *valp)
2537 *valp = read_cpuid_id();
2540 *valp = SYS_MPIDR_SAFE_VAL;
2542 case SYS_REVIDR_EL1:
2543 /* IMPLEMENTATION DEFINED values are emulated with 0 */
2553 static int emulate_sys_reg(u32 id, u64 *valp)
2555 struct arm64_ftr_reg *regp;
2557 if (!is_emulated(id))
2560 if (sys_reg_CRm(id) == 0)
2561 return emulate_id_reg(id, valp);
2563 regp = get_arm64_ftr_reg(id);
2565 *valp = arm64_ftr_reg_user_value(regp);
2568 * The untracked registers are either IMPLEMENTATION DEFINED
2569 * (e.g, ID_AFR0_EL1) or reserved RAZ.
2575 int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt)
2580 rc = emulate_sys_reg(sys_reg, &val);
2582 pt_regs_write_reg(regs, rt, val);
2583 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
2588 static int emulate_mrs(struct pt_regs *regs, u32 insn)
2593 * sys_reg values are defined as used in mrs/msr instruction.
2594 * shift the imm value to get the encoding.
2596 sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
2597 rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
2598 return do_emulate_mrs(regs, sys_reg, rt);
2601 static struct undef_hook mrs_hook = {
2602 .instr_mask = 0xfff00000,
2603 .instr_val = 0xd5300000,
2604 .pstate_mask = PSR_AA32_MODE_MASK,
2605 .pstate_val = PSR_MODE_EL0t,
2609 static int __init enable_mrs_emulation(void)
2611 register_undef_hook(&mrs_hook);
2615 core_initcall(enable_mrs_emulation);
2617 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr,
2620 if (__meltdown_safe)
2621 return sprintf(buf, "Not affected\n");
2623 if (arm64_kernel_unmapped_at_el0())
2624 return sprintf(buf, "Mitigation: PTI\n");
2626 return sprintf(buf, "Vulnerable\n");