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