]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/arm64/kernel/cpufeature.c
arm64: asid: Do not replace active_asids if already 0
[mirror_ubuntu-jammy-kernel.git] / arch / arm64 / kernel / cpufeature.c
CommitLineData
359b7064
MZ
1/*
2 * Contains CPU feature definitions
3 *
4 * Copyright (C) 2015 ARM Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
9cdf8ec4 19#define pr_fmt(fmt) "CPU features: " fmt
359b7064 20
3c739b57 21#include <linux/bsearch.h>
2a6dcb2b 22#include <linux/cpumask.h>
3c739b57 23#include <linux/sort.h>
2a6dcb2b 24#include <linux/stop_machine.h>
359b7064 25#include <linux/types.h>
2077be67 26#include <linux/mm.h>
359b7064
MZ
27#include <asm/cpu.h>
28#include <asm/cpufeature.h>
dbb4e152 29#include <asm/cpu_ops.h>
2e0f2478 30#include <asm/fpsimd.h>
13f417f3 31#include <asm/mmu_context.h>
338d4f49 32#include <asm/processor.h>
cdcf817b 33#include <asm/sysreg.h>
77c97b4e 34#include <asm/traps.h>
d88701be 35#include <asm/virt.h>
359b7064 36
9cdf8ec4
SP
37unsigned long elf_hwcap __read_mostly;
38EXPORT_SYMBOL_GPL(elf_hwcap);
39
40#ifdef CONFIG_COMPAT
41#define COMPAT_ELF_HWCAP_DEFAULT \
42 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
43 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
44 COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
45 COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
46 COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
47 COMPAT_HWCAP_LPAE)
48unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
49unsigned int compat_elf_hwcap2 __read_mostly;
50#endif
51
52DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
4b65a5db 53EXPORT_SYMBOL(cpu_hwcaps);
9cdf8ec4 54
8f1eec57
DM
55/*
56 * Flag to indicate if we have computed the system wide
57 * capabilities based on the boot time active CPUs. This
58 * will be used to determine if a new booting CPU should
59 * go through the verification process to make sure that it
60 * supports the system capabilities, without using a hotplug
61 * notifier.
62 */
63static bool sys_caps_initialised;
64
65static inline void set_sys_caps_initialised(void)
66{
67 sys_caps_initialised = true;
68}
69
8effeaaf
MR
70static int dump_cpu_hwcaps(struct notifier_block *self, unsigned long v, void *p)
71{
72 /* file-wide pr_fmt adds "CPU features: " prefix */
73 pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
74 return 0;
75}
76
77static struct notifier_block cpu_hwcaps_notifier = {
78 .notifier_call = dump_cpu_hwcaps
79};
80
81static int __init register_cpu_hwcaps_dumper(void)
82{
83 atomic_notifier_chain_register(&panic_notifier_list,
84 &cpu_hwcaps_notifier);
85 return 0;
86}
87__initcall(register_cpu_hwcaps_dumper);
88
efd9e03f
CM
89DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
90EXPORT_SYMBOL(cpu_hwcap_keys);
91
fe4fbdbc 92#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
3c739b57 93 { \
4f0a606b 94 .sign = SIGNED, \
fe4fbdbc 95 .visible = VISIBLE, \
3c739b57
SP
96 .strict = STRICT, \
97 .type = TYPE, \
98 .shift = SHIFT, \
99 .width = WIDTH, \
100 .safe_val = SAFE_VAL, \
101 }
102
0710cfdb 103/* Define a feature with unsigned values */
fe4fbdbc
SP
104#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
105 __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
4f0a606b 106
0710cfdb 107/* Define a feature with a signed value */
fe4fbdbc
SP
108#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
109 __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
0710cfdb 110
3c739b57
SP
111#define ARM64_FTR_END \
112 { \
113 .width = 0, \
114 }
115
70544196
JM
116/* meta feature for alternatives */
117static bool __maybe_unused
92406f0c
SP
118cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
119
70544196 120
4aa8a472
SP
121/*
122 * NOTE: Any changes to the visibility of features should be kept in
123 * sync with the documentation of the CPU feature register ABI.
124 */
5e49d73c 125static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
5bdecb79
SP
126 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
127 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
128 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
129 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
130 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
fe4fbdbc
SP
131 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
132 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
133 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
134 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
135 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
3c739b57
SP
136 ARM64_FTR_END,
137};
138
c8c3798d 139static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
5bdecb79
SP
140 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_LRCPC_SHIFT, 4, 0),
141 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FCMA_SHIFT, 4, 0),
142 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_JSCVT_SHIFT, 4, 0),
143 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DPB_SHIFT, 4, 0),
c8c3798d
SP
144 ARM64_FTR_END,
145};
146
5e49d73c 147static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
43994d82 148 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SVE_SHIFT, 4, 0),
5bdecb79 149 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_GIC_SHIFT, 4, 0),
fe4fbdbc
SP
150 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
151 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
3c739b57 152 /* Linux doesn't care about the EL3 */
5bdecb79
SP
153 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
154 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
155 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
156 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
3c739b57
SP
157 ARM64_FTR_END,
158};
159
5e49d73c 160static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
5bdecb79
SP
161 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
162 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
163 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
164 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
3c739b57 165 /* Linux shouldn't care about secure memory */
5bdecb79
SP
166 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
167 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
168 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
3c739b57
SP
169 /*
170 * Differing PARange is fine as long as all peripherals and memory are mapped
171 * within the minimum PARange of all CPUs
172 */
fe4fbdbc 173 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
3c739b57
SP
174 ARM64_FTR_END,
175};
176
5e49d73c 177static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
fe4fbdbc 178 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
5bdecb79
SP
179 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
180 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
181 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
182 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
183 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
3c739b57
SP
184 ARM64_FTR_END,
185};
186
5e49d73c 187static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
5bdecb79
SP
188 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
189 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
190 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
191 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
192 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
406e3087
JM
193 ARM64_FTR_END,
194};
195
5e49d73c 196static const struct arm64_ftr_bits ftr_ctr[] = {
fe4fbdbc
SP
197 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RAO */
198 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_SAFE, 24, 4, 0), /* CWG */
199 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0), /* ERG */
200 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 1), /* DminLine */
3c739b57
SP
201 /*
202 * Linux can handle differing I-cache policies. Userspace JITs will
ee7bc638 203 * make use of *minLine.
155433cb 204 * If we have differing I-cache policies, report it as the weakest - VIPT.
3c739b57 205 */
155433cb 206 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_VIPT), /* L1Ip */
fe4fbdbc 207 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* IminLine */
3c739b57
SP
208 ARM64_FTR_END,
209};
210
675b0563
AB
211struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
212 .name = "SYS_CTR_EL0",
213 .ftr_bits = ftr_ctr
214};
215
5e49d73c 216static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
5bdecb79
SP
217 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0xf), /* InnerShr */
218 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0), /* FCSE */
fe4fbdbc 219 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
5bdecb79
SP
220 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0), /* TCM */
221 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* ShareLvl */
222 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0xf), /* OuterShr */
223 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* PMSA */
224 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* VMSA */
3c739b57
SP
225 ARM64_FTR_END,
226};
227
5e49d73c 228static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
fe4fbdbc
SP
229 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 36, 28, 0),
230 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_PMSVER_SHIFT, 4, 0),
231 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
232 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
233 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
b20d1ba3
WD
234 /*
235 * We can instantiate multiple PMU instances with different levels
236 * of support.
fe4fbdbc
SP
237 */
238 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
239 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
240 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
3c739b57
SP
241 ARM64_FTR_END,
242};
243
5e49d73c 244static const struct arm64_ftr_bits ftr_mvfr2[] = {
5bdecb79
SP
245 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* FPMisc */
246 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* SIMDMisc */
3c739b57
SP
247 ARM64_FTR_END,
248};
249
5e49d73c 250static const struct arm64_ftr_bits ftr_dczid[] = {
fe4fbdbc
SP
251 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
252 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
3c739b57
SP
253 ARM64_FTR_END,
254};
255
256
5e49d73c 257static const struct arm64_ftr_bits ftr_id_isar5[] = {
5bdecb79
SP
258 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_RDM_SHIFT, 4, 0),
259 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_CRC32_SHIFT, 4, 0),
260 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA2_SHIFT, 4, 0),
261 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA1_SHIFT, 4, 0),
262 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_AES_SHIFT, 4, 0),
263 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SEVL_SHIFT, 4, 0),
3c739b57
SP
264 ARM64_FTR_END,
265};
266
5e49d73c 267static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
5bdecb79 268 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* ac2 */
3c739b57
SP
269 ARM64_FTR_END,
270};
271
5e49d73c 272static const struct arm64_ftr_bits ftr_id_pfr0[] = {
5bdecb79
SP
273 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* State3 */
274 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0), /* State2 */
275 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* State1 */
276 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* State0 */
3c739b57
SP
277 ARM64_FTR_END,
278};
279
5e49d73c 280static const struct arm64_ftr_bits ftr_id_dfr0[] = {
fe4fbdbc
SP
281 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
282 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
283 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
284 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
285 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
286 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
287 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
288 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
e5343503
SP
289 ARM64_FTR_END,
290};
291
2e0f2478
DM
292static const struct arm64_ftr_bits ftr_zcr[] = {
293 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
294 ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_SIZE, 0), /* LEN */
295 ARM64_FTR_END,
296};
297
3c739b57
SP
298/*
299 * Common ftr bits for a 32bit register with all hidden, strict
300 * attributes, with 4bit feature fields and a default safe value of
301 * 0. Covers the following 32bit registers:
302 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
303 */
5e49d73c 304static const struct arm64_ftr_bits ftr_generic_32bits[] = {
fe4fbdbc
SP
305 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
306 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
307 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
308 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
309 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
310 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
311 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
312 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
3c739b57
SP
313 ARM64_FTR_END,
314};
315
eab43e88
SP
316/* Table for a single 32bit feature value */
317static const struct arm64_ftr_bits ftr_single32[] = {
fe4fbdbc 318 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
3c739b57
SP
319 ARM64_FTR_END,
320};
321
eab43e88 322static const struct arm64_ftr_bits ftr_raz[] = {
3c739b57
SP
323 ARM64_FTR_END,
324};
325
6f2b7eef
AB
326#define ARM64_FTR_REG(id, table) { \
327 .sys_id = id, \
328 .reg = &(struct arm64_ftr_reg){ \
3c739b57
SP
329 .name = #id, \
330 .ftr_bits = &((table)[0]), \
6f2b7eef 331 }}
3c739b57 332
6f2b7eef
AB
333static const struct __ftr_reg_entry {
334 u32 sys_id;
335 struct arm64_ftr_reg *reg;
336} arm64_ftr_regs[] = {
3c739b57
SP
337
338 /* Op1 = 0, CRn = 0, CRm = 1 */
339 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
340 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
e5343503 341 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
3c739b57
SP
342 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
343 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
344 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
345 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
346
347 /* Op1 = 0, CRn = 0, CRm = 2 */
348 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
349 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
350 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
351 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
352 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
353 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
354 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
355
356 /* Op1 = 0, CRn = 0, CRm = 3 */
357 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
358 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
359 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
360
361 /* Op1 = 0, CRn = 0, CRm = 4 */
362 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
eab43e88 363 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_raz),
2e0f2478 364 ARM64_FTR_REG(SYS_ID_AA64ZFR0_EL1, ftr_raz),
3c739b57
SP
365
366 /* Op1 = 0, CRn = 0, CRm = 5 */
367 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
eab43e88 368 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
3c739b57
SP
369
370 /* Op1 = 0, CRn = 0, CRm = 6 */
371 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
c8c3798d 372 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1),
3c739b57
SP
373
374 /* Op1 = 0, CRn = 0, CRm = 7 */
375 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
376 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
406e3087 377 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
3c739b57 378
2e0f2478
DM
379 /* Op1 = 0, CRn = 1, CRm = 2 */
380 ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
381
3c739b57 382 /* Op1 = 3, CRn = 0, CRm = 0 */
675b0563 383 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
3c739b57
SP
384 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
385
386 /* Op1 = 3, CRn = 14, CRm = 0 */
eab43e88 387 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
3c739b57
SP
388};
389
390static int search_cmp_ftr_reg(const void *id, const void *regp)
391{
6f2b7eef 392 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
3c739b57
SP
393}
394
395/*
396 * get_arm64_ftr_reg - Lookup a feature register entry using its
397 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
398 * ascending order of sys_id , we use binary search to find a matching
399 * entry.
400 *
401 * returns - Upon success, matching ftr_reg entry for id.
402 * - NULL on failure. It is upto the caller to decide
403 * the impact of a failure.
404 */
405static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
406{
6f2b7eef
AB
407 const struct __ftr_reg_entry *ret;
408
409 ret = bsearch((const void *)(unsigned long)sys_id,
3c739b57
SP
410 arm64_ftr_regs,
411 ARRAY_SIZE(arm64_ftr_regs),
412 sizeof(arm64_ftr_regs[0]),
413 search_cmp_ftr_reg);
6f2b7eef
AB
414 if (ret)
415 return ret->reg;
416 return NULL;
3c739b57
SP
417}
418
5e49d73c
AB
419static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
420 s64 ftr_val)
3c739b57
SP
421{
422 u64 mask = arm64_ftr_mask(ftrp);
423
424 reg &= ~mask;
425 reg |= (ftr_val << ftrp->shift) & mask;
426 return reg;
427}
428
5e49d73c
AB
429static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
430 s64 cur)
3c739b57
SP
431{
432 s64 ret = 0;
433
434 switch (ftrp->type) {
435 case FTR_EXACT:
436 ret = ftrp->safe_val;
437 break;
438 case FTR_LOWER_SAFE:
439 ret = new < cur ? new : cur;
440 break;
441 case FTR_HIGHER_SAFE:
442 ret = new > cur ? new : cur;
443 break;
444 default:
445 BUG();
446 }
447
448 return ret;
449}
450
3c739b57
SP
451static void __init sort_ftr_regs(void)
452{
6f2b7eef
AB
453 int i;
454
455 /* Check that the array is sorted so that we can do the binary search */
456 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
457 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
3c739b57
SP
458}
459
460/*
461 * Initialise the CPU feature register from Boot CPU values.
462 * Also initiliases the strict_mask for the register.
b389d799
MR
463 * Any bits that are not covered by an arm64_ftr_bits entry are considered
464 * RES0 for the system-wide value, and must strictly match.
3c739b57
SP
465 */
466static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
467{
468 u64 val = 0;
469 u64 strict_mask = ~0x0ULL;
fe4fbdbc 470 u64 user_mask = 0;
b389d799
MR
471 u64 valid_mask = 0;
472
5e49d73c 473 const struct arm64_ftr_bits *ftrp;
3c739b57
SP
474 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
475
476 BUG_ON(!reg);
477
478 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
b389d799 479 u64 ftr_mask = arm64_ftr_mask(ftrp);
3c739b57
SP
480 s64 ftr_new = arm64_ftr_value(ftrp, new);
481
482 val = arm64_ftr_set_value(ftrp, val, ftr_new);
b389d799
MR
483
484 valid_mask |= ftr_mask;
3c739b57 485 if (!ftrp->strict)
b389d799 486 strict_mask &= ~ftr_mask;
fe4fbdbc
SP
487 if (ftrp->visible)
488 user_mask |= ftr_mask;
489 else
490 reg->user_val = arm64_ftr_set_value(ftrp,
491 reg->user_val,
492 ftrp->safe_val);
3c739b57 493 }
b389d799
MR
494
495 val &= valid_mask;
496
3c739b57
SP
497 reg->sys_val = val;
498 reg->strict_mask = strict_mask;
fe4fbdbc 499 reg->user_mask = user_mask;
3c739b57
SP
500}
501
502void __init init_cpu_features(struct cpuinfo_arm64 *info)
503{
504 /* Before we start using the tables, make sure it is sorted */
505 sort_ftr_regs();
506
507 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
508 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
509 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
510 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
511 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
512 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
513 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
514 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
515 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
406e3087 516 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
3c739b57
SP
517 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
518 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
2e0f2478 519 init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
a6dc3cd7
SP
520
521 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
522 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
523 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
524 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
525 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
526 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
527 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
528 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
529 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
530 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
531 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
532 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
533 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
534 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
535 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
536 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
537 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
538 }
539
2e0f2478
DM
540 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
541 init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
542 sve_init_vq_map();
543 }
3c739b57
SP
544}
545
3086d391 546static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
3c739b57 547{
5e49d73c 548 const struct arm64_ftr_bits *ftrp;
3c739b57
SP
549
550 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
551 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
552 s64 ftr_new = arm64_ftr_value(ftrp, new);
553
554 if (ftr_cur == ftr_new)
555 continue;
556 /* Find a safe value */
557 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
558 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
559 }
560
561}
562
3086d391 563static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
cdcf817b 564{
3086d391
SP
565 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
566
567 BUG_ON(!regp);
568 update_cpu_ftr_reg(regp, val);
569 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
570 return 0;
571 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
572 regp->name, boot, cpu, val);
573 return 1;
574}
575
576/*
577 * Update system wide CPU feature registers with the values from a
578 * non-boot CPU. Also performs SANITY checks to make sure that there
579 * aren't any insane variations from that of the boot CPU.
580 */
581void update_cpu_features(int cpu,
582 struct cpuinfo_arm64 *info,
583 struct cpuinfo_arm64 *boot)
584{
585 int taint = 0;
586
587 /*
588 * The kernel can handle differing I-cache policies, but otherwise
589 * caches should look identical. Userspace JITs will make use of
590 * *minLine.
591 */
592 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
593 info->reg_ctr, boot->reg_ctr);
594
595 /*
596 * Userspace may perform DC ZVA instructions. Mismatched block sizes
597 * could result in too much or too little memory being zeroed if a
598 * process is preempted and migrated between CPUs.
599 */
600 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
601 info->reg_dczid, boot->reg_dczid);
602
603 /* If different, timekeeping will be broken (especially with KVM) */
604 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
605 info->reg_cntfrq, boot->reg_cntfrq);
606
607 /*
608 * The kernel uses self-hosted debug features and expects CPUs to
609 * support identical debug features. We presently need CTX_CMPs, WRPs,
610 * and BRPs to be identical.
611 * ID_AA64DFR1 is currently RES0.
612 */
613 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
614 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
615 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
616 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
617 /*
618 * Even in big.LITTLE, processors should be identical instruction-set
619 * wise.
620 */
621 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
622 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
623 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
624 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
625
626 /*
627 * Differing PARange support is fine as long as all peripherals and
628 * memory are mapped within the minimum PARange of all CPUs.
629 * Linux should not care about secure memory.
630 */
631 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
632 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
633 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
634 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
406e3087
JM
635 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
636 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
3086d391
SP
637
638 /*
639 * EL3 is not our concern.
640 * ID_AA64PFR1 is currently RES0.
641 */
642 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
643 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
644 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
645 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
646
2e0f2478
DM
647 taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
648 info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
649
3086d391 650 /*
a6dc3cd7
SP
651 * If we have AArch32, we care about 32-bit features for compat.
652 * If the system doesn't support AArch32, don't update them.
3086d391 653 */
46823dd1 654 if (id_aa64pfr0_32bit_el0(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
a6dc3cd7
SP
655 id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
656
657 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
3086d391 658 info->reg_id_dfr0, boot->reg_id_dfr0);
a6dc3cd7 659 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
3086d391 660 info->reg_id_isar0, boot->reg_id_isar0);
a6dc3cd7 661 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
3086d391 662 info->reg_id_isar1, boot->reg_id_isar1);
a6dc3cd7 663 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
3086d391 664 info->reg_id_isar2, boot->reg_id_isar2);
a6dc3cd7 665 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
3086d391 666 info->reg_id_isar3, boot->reg_id_isar3);
a6dc3cd7 667 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
3086d391 668 info->reg_id_isar4, boot->reg_id_isar4);
a6dc3cd7 669 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
3086d391
SP
670 info->reg_id_isar5, boot->reg_id_isar5);
671
a6dc3cd7
SP
672 /*
673 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
674 * ACTLR formats could differ across CPUs and therefore would have to
675 * be trapped for virtualization anyway.
676 */
677 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
3086d391 678 info->reg_id_mmfr0, boot->reg_id_mmfr0);
a6dc3cd7 679 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
3086d391 680 info->reg_id_mmfr1, boot->reg_id_mmfr1);
a6dc3cd7 681 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
3086d391 682 info->reg_id_mmfr2, boot->reg_id_mmfr2);
a6dc3cd7 683 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
3086d391 684 info->reg_id_mmfr3, boot->reg_id_mmfr3);
a6dc3cd7 685 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
3086d391 686 info->reg_id_pfr0, boot->reg_id_pfr0);
a6dc3cd7 687 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
3086d391 688 info->reg_id_pfr1, boot->reg_id_pfr1);
a6dc3cd7 689 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
3086d391 690 info->reg_mvfr0, boot->reg_mvfr0);
a6dc3cd7 691 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
3086d391 692 info->reg_mvfr1, boot->reg_mvfr1);
a6dc3cd7 693 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
3086d391 694 info->reg_mvfr2, boot->reg_mvfr2);
a6dc3cd7 695 }
3086d391 696
2e0f2478
DM
697 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
698 taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
699 info->reg_zcr, boot->reg_zcr);
700
701 /* Probe vector lengths, unless we already gave up on SVE */
702 if (id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
703 !sys_caps_initialised)
704 sve_update_vq_map();
705 }
706
3086d391
SP
707 /*
708 * Mismatched CPU features are a recipe for disaster. Don't even
709 * pretend to support them.
710 */
8dd0ee65
WD
711 if (taint) {
712 pr_warn_once("Unsupported CPU feature variation detected.\n");
713 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
714 }
cdcf817b
SP
715}
716
46823dd1 717u64 read_sanitised_ftr_reg(u32 id)
b3f15378
SP
718{
719 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
720
721 /* We shouldn't get a request for an unsupported register */
722 BUG_ON(!regp);
723 return regp->sys_val;
724}
359b7064 725
965861d6
MR
726#define read_sysreg_case(r) \
727 case r: return read_sysreg_s(r)
728
92406f0c 729/*
46823dd1 730 * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
92406f0c
SP
731 * Read the system register on the current CPU
732 */
46823dd1 733static u64 __read_sysreg_by_encoding(u32 sys_id)
92406f0c
SP
734{
735 switch (sys_id) {
965861d6
MR
736 read_sysreg_case(SYS_ID_PFR0_EL1);
737 read_sysreg_case(SYS_ID_PFR1_EL1);
738 read_sysreg_case(SYS_ID_DFR0_EL1);
739 read_sysreg_case(SYS_ID_MMFR0_EL1);
740 read_sysreg_case(SYS_ID_MMFR1_EL1);
741 read_sysreg_case(SYS_ID_MMFR2_EL1);
742 read_sysreg_case(SYS_ID_MMFR3_EL1);
743 read_sysreg_case(SYS_ID_ISAR0_EL1);
744 read_sysreg_case(SYS_ID_ISAR1_EL1);
745 read_sysreg_case(SYS_ID_ISAR2_EL1);
746 read_sysreg_case(SYS_ID_ISAR3_EL1);
747 read_sysreg_case(SYS_ID_ISAR4_EL1);
748 read_sysreg_case(SYS_ID_ISAR5_EL1);
749 read_sysreg_case(SYS_MVFR0_EL1);
750 read_sysreg_case(SYS_MVFR1_EL1);
751 read_sysreg_case(SYS_MVFR2_EL1);
752
753 read_sysreg_case(SYS_ID_AA64PFR0_EL1);
754 read_sysreg_case(SYS_ID_AA64PFR1_EL1);
755 read_sysreg_case(SYS_ID_AA64DFR0_EL1);
756 read_sysreg_case(SYS_ID_AA64DFR1_EL1);
757 read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
758 read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
759 read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
760 read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
761 read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
762
763 read_sysreg_case(SYS_CNTFRQ_EL0);
764 read_sysreg_case(SYS_CTR_EL0);
765 read_sysreg_case(SYS_DCZID_EL0);
766
92406f0c
SP
767 default:
768 BUG();
769 return 0;
770 }
771}
772
963fcd40
MZ
773#include <linux/irqchip/arm-gic-v3.h>
774
18ffa046
JM
775static bool
776feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
777{
28c5dcb2 778 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
18ffa046
JM
779
780 return val >= entry->min_field_value;
781}
782
da8d02d1 783static bool
92406f0c 784has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
da8d02d1
SP
785{
786 u64 val;
94a9e04a 787
92406f0c
SP
788 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
789 if (scope == SCOPE_SYSTEM)
46823dd1 790 val = read_sanitised_ftr_reg(entry->sys_reg);
92406f0c 791 else
46823dd1 792 val = __read_sysreg_by_encoding(entry->sys_reg);
92406f0c 793
da8d02d1
SP
794 return feature_matches(val, entry);
795}
338d4f49 796
92406f0c 797static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
963fcd40
MZ
798{
799 bool has_sre;
800
92406f0c 801 if (!has_cpuid_feature(entry, scope))
963fcd40
MZ
802 return false;
803
804 has_sre = gic_enable_sre();
805 if (!has_sre)
806 pr_warn_once("%s present but disabled by higher exception level\n",
807 entry->desc);
808
809 return has_sre;
810}
811
92406f0c 812static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
d5370f75
WD
813{
814 u32 midr = read_cpuid_id();
d5370f75
WD
815
816 /* Cavium ThunderX pass 1.x and 2.x */
fa5ce3d1
RR
817 return MIDR_IS_CPU_MODEL_RANGE(midr, MIDR_THUNDERX,
818 MIDR_CPU_VAR_REV(0, 0),
819 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
d5370f75
WD
820}
821
92406f0c 822static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
d88701be
MZ
823{
824 return is_kernel_in_hyp_mode();
825}
826
d1745910
MZ
827static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry,
828 int __unused)
829{
2077be67 830 phys_addr_t idmap_addr = __pa_symbol(__hyp_idmap_text_start);
d1745910
MZ
831
832 /*
833 * Activate the lower HYP offset only if:
834 * - the idmap doesn't clash with it,
835 * - the kernel is not running at EL2.
836 */
837 return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode();
838}
839
82e0191a
SP
840static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
841{
46823dd1 842 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
82e0191a
SP
843
844 return cpuid_feature_extract_signed_field(pfr0,
845 ID_AA64PFR0_FP_SHIFT) < 0;
846}
847
ea1e3de8
WD
848#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
849static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
850
851static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
852 int __unused)
853{
854 /* Forced on command line? */
855 if (__kpti_forced) {
856 pr_info_once("kernel page table isolation forced %s by command line option\n",
857 __kpti_forced > 0 ? "ON" : "OFF");
858 return __kpti_forced > 0;
859 }
860
861 /* Useful for KASLR robustness */
862 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
863 return true;
864
865 return false;
866}
867
868static int __init parse_kpti(char *str)
869{
870 bool enabled;
871 int ret = strtobool(str, &enabled);
872
873 if (ret)
874 return ret;
875
876 __kpti_forced = enabled ? 1 : -1;
877 return 0;
878}
879__setup("kpti=", parse_kpti);
880#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
881
359b7064 882static const struct arm64_cpu_capabilities arm64_features[] = {
94a9e04a
MZ
883 {
884 .desc = "GIC system register CPU interface",
885 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
92406f0c 886 .def_scope = SCOPE_SYSTEM,
963fcd40 887 .matches = has_useable_gicv3_cpuif,
da8d02d1
SP
888 .sys_reg = SYS_ID_AA64PFR0_EL1,
889 .field_pos = ID_AA64PFR0_GIC_SHIFT,
ff96f7bc 890 .sign = FTR_UNSIGNED,
18ffa046 891 .min_field_value = 1,
94a9e04a 892 },
338d4f49
JM
893#ifdef CONFIG_ARM64_PAN
894 {
895 .desc = "Privileged Access Never",
896 .capability = ARM64_HAS_PAN,
92406f0c 897 .def_scope = SCOPE_SYSTEM,
da8d02d1
SP
898 .matches = has_cpuid_feature,
899 .sys_reg = SYS_ID_AA64MMFR1_EL1,
900 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
ff96f7bc 901 .sign = FTR_UNSIGNED,
338d4f49
JM
902 .min_field_value = 1,
903 .enable = cpu_enable_pan,
904 },
905#endif /* CONFIG_ARM64_PAN */
2e94da13
WD
906#if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
907 {
908 .desc = "LSE atomic instructions",
909 .capability = ARM64_HAS_LSE_ATOMICS,
92406f0c 910 .def_scope = SCOPE_SYSTEM,
da8d02d1
SP
911 .matches = has_cpuid_feature,
912 .sys_reg = SYS_ID_AA64ISAR0_EL1,
913 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
ff96f7bc 914 .sign = FTR_UNSIGNED,
2e94da13
WD
915 .min_field_value = 2,
916 },
917#endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
d5370f75
WD
918 {
919 .desc = "Software prefetching using PRFM",
920 .capability = ARM64_HAS_NO_HW_PREFETCH,
92406f0c 921 .def_scope = SCOPE_SYSTEM,
d5370f75
WD
922 .matches = has_no_hw_prefetch,
923 },
57f4959b
JM
924#ifdef CONFIG_ARM64_UAO
925 {
926 .desc = "User Access Override",
927 .capability = ARM64_HAS_UAO,
92406f0c 928 .def_scope = SCOPE_SYSTEM,
57f4959b
JM
929 .matches = has_cpuid_feature,
930 .sys_reg = SYS_ID_AA64MMFR2_EL1,
931 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
932 .min_field_value = 1,
c8b06e3f
JM
933 /*
934 * We rely on stop_machine() calling uao_thread_switch() to set
935 * UAO immediately after patching.
936 */
57f4959b
JM
937 },
938#endif /* CONFIG_ARM64_UAO */
70544196
JM
939#ifdef CONFIG_ARM64_PAN
940 {
941 .capability = ARM64_ALT_PAN_NOT_UAO,
92406f0c 942 .def_scope = SCOPE_SYSTEM,
70544196
JM
943 .matches = cpufeature_pan_not_uao,
944 },
945#endif /* CONFIG_ARM64_PAN */
d88701be
MZ
946 {
947 .desc = "Virtualization Host Extensions",
948 .capability = ARM64_HAS_VIRT_HOST_EXTN,
92406f0c 949 .def_scope = SCOPE_SYSTEM,
d88701be
MZ
950 .matches = runs_at_el2,
951 },
042446a3
SP
952 {
953 .desc = "32-bit EL0 Support",
954 .capability = ARM64_HAS_32BIT_EL0,
92406f0c 955 .def_scope = SCOPE_SYSTEM,
042446a3
SP
956 .matches = has_cpuid_feature,
957 .sys_reg = SYS_ID_AA64PFR0_EL1,
958 .sign = FTR_UNSIGNED,
959 .field_pos = ID_AA64PFR0_EL0_SHIFT,
960 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
961 },
d1745910
MZ
962 {
963 .desc = "Reduced HYP mapping offset",
964 .capability = ARM64_HYP_OFFSET_LOW,
965 .def_scope = SCOPE_SYSTEM,
966 .matches = hyp_offset_low,
967 },
ea1e3de8
WD
968#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
969 {
970 .capability = ARM64_UNMAP_KERNEL_AT_EL0,
971 .def_scope = SCOPE_SYSTEM,
972 .matches = unmap_kernel_at_el0,
973 },
974#endif
82e0191a
SP
975 {
976 /* FP/SIMD is not implemented */
977 .capability = ARM64_HAS_NO_FPSIMD,
978 .def_scope = SCOPE_SYSTEM,
979 .min_field_value = 0,
980 .matches = has_no_fpsimd,
981 },
d50e071f
RM
982#ifdef CONFIG_ARM64_PMEM
983 {
984 .desc = "Data cache clean to Point of Persistence",
985 .capability = ARM64_HAS_DCPOP,
986 .def_scope = SCOPE_SYSTEM,
987 .matches = has_cpuid_feature,
988 .sys_reg = SYS_ID_AA64ISAR1_EL1,
989 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
990 .min_field_value = 1,
991 },
992#endif
43994d82
DM
993#ifdef CONFIG_ARM64_SVE
994 {
995 .desc = "Scalable Vector Extension",
996 .capability = ARM64_SVE,
997 .def_scope = SCOPE_SYSTEM,
998 .sys_reg = SYS_ID_AA64PFR0_EL1,
999 .sign = FTR_UNSIGNED,
1000 .field_pos = ID_AA64PFR0_SVE_SHIFT,
1001 .min_field_value = ID_AA64PFR0_SVE,
1002 .matches = has_cpuid_feature,
1003 .enable = sve_kernel_enable,
1004 },
1005#endif /* CONFIG_ARM64_SVE */
359b7064
MZ
1006 {},
1007};
1008
ff96f7bc 1009#define HWCAP_CAP(reg, field, s, min_value, type, cap) \
37b01d53
SP
1010 { \
1011 .desc = #cap, \
92406f0c 1012 .def_scope = SCOPE_SYSTEM, \
37b01d53
SP
1013 .matches = has_cpuid_feature, \
1014 .sys_reg = reg, \
1015 .field_pos = field, \
ff96f7bc 1016 .sign = s, \
37b01d53
SP
1017 .min_field_value = min_value, \
1018 .hwcap_type = type, \
1019 .hwcap = cap, \
1020 }
1021
f3efb675 1022static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
ff96f7bc
SP
1023 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_PMULL),
1024 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_AES),
1025 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA1),
1026 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA2),
f5e035f8 1027 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_SHA512),
ff96f7bc
SP
1028 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_CRC32),
1029 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ATOMICS),
f92f5ce0 1030 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RDM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDRDM),
f5e035f8
SP
1031 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA3),
1032 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM3),
1033 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM4),
1034 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_DP_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDDP),
ff96f7bc 1035 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_FP),
bf500618 1036 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_FPHP),
ff96f7bc 1037 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_ASIMD),
bf500618 1038 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_ASIMDHP),
7aac405e 1039 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DPB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_DCPOP),
c8c3798d 1040 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_JSCVT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_JSCVT),
cb567e79 1041 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FCMA_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_FCMA),
c651aae5 1042 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_LRCPC),
43994d82
DM
1043#ifdef CONFIG_ARM64_SVE
1044 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_SVE_SHIFT, FTR_UNSIGNED, ID_AA64PFR0_SVE, CAP_HWCAP, HWCAP_SVE),
1045#endif
75283501
SP
1046 {},
1047};
1048
1049static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
37b01d53 1050#ifdef CONFIG_COMPAT
ff96f7bc
SP
1051 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
1052 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
1053 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
1054 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
1055 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32),
37b01d53
SP
1056#endif
1057 {},
1058};
1059
f3efb675 1060static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
37b01d53
SP
1061{
1062 switch (cap->hwcap_type) {
1063 case CAP_HWCAP:
1064 elf_hwcap |= cap->hwcap;
1065 break;
1066#ifdef CONFIG_COMPAT
1067 case CAP_COMPAT_HWCAP:
1068 compat_elf_hwcap |= (u32)cap->hwcap;
1069 break;
1070 case CAP_COMPAT_HWCAP2:
1071 compat_elf_hwcap2 |= (u32)cap->hwcap;
1072 break;
1073#endif
1074 default:
1075 WARN_ON(1);
1076 break;
1077 }
1078}
1079
1080/* Check if we have a particular HWCAP enabled */
f3efb675 1081static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
37b01d53
SP
1082{
1083 bool rc;
1084
1085 switch (cap->hwcap_type) {
1086 case CAP_HWCAP:
1087 rc = (elf_hwcap & cap->hwcap) != 0;
1088 break;
1089#ifdef CONFIG_COMPAT
1090 case CAP_COMPAT_HWCAP:
1091 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
1092 break;
1093 case CAP_COMPAT_HWCAP2:
1094 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
1095 break;
1096#endif
1097 default:
1098 WARN_ON(1);
1099 rc = false;
1100 }
1101
1102 return rc;
1103}
1104
75283501 1105static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
37b01d53 1106{
77c97b4e
SP
1107 /* We support emulation of accesses to CPU ID feature registers */
1108 elf_hwcap |= HWCAP_CPUID;
75283501 1109 for (; hwcaps->matches; hwcaps++)
92406f0c 1110 if (hwcaps->matches(hwcaps, hwcaps->def_scope))
75283501 1111 cap_set_elf_hwcap(hwcaps);
37b01d53
SP
1112}
1113
ce8b602c 1114void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
359b7064
MZ
1115 const char *info)
1116{
75283501 1117 for (; caps->matches; caps++) {
92406f0c 1118 if (!caps->matches(caps, caps->def_scope))
359b7064
MZ
1119 continue;
1120
75283501
SP
1121 if (!cpus_have_cap(caps->capability) && caps->desc)
1122 pr_info("%s %s\n", info, caps->desc);
1123 cpus_set_cap(caps->capability);
359b7064 1124 }
ce8b602c
SP
1125}
1126
1127/*
dbb4e152
SP
1128 * Run through the enabled capabilities and enable() it on all active
1129 * CPUs
ce8b602c 1130 */
8e231852 1131void __init enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps)
ce8b602c 1132{
63a1e1c9
MR
1133 for (; caps->matches; caps++) {
1134 unsigned int num = caps->capability;
1135
1136 if (!cpus_have_cap(num))
1137 continue;
1138
1139 /* Ensure cpus_have_const_cap(num) works */
1140 static_branch_enable(&cpu_hwcap_keys[num]);
1141
1142 if (caps->enable) {
2a6dcb2b
JM
1143 /*
1144 * Use stop_machine() as it schedules the work allowing
1145 * us to modify PSTATE, instead of on_each_cpu() which
1146 * uses an IPI, giving us a PSTATE that disappears when
1147 * we return.
1148 */
1149 stop_machine(caps->enable, NULL, cpu_online_mask);
63a1e1c9
MR
1150 }
1151 }
dbb4e152
SP
1152}
1153
dbb4e152 1154/*
13f417f3
SP
1155 * Check for CPU features that are used in early boot
1156 * based on the Boot CPU value.
dbb4e152 1157 */
13f417f3 1158static void check_early_cpu_features(void)
dbb4e152 1159{
ac1ad20f 1160 verify_cpu_run_el();
13f417f3 1161 verify_cpu_asid_bits();
dbb4e152 1162}
1c076303 1163
75283501
SP
1164static void
1165verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
1166{
1167
92406f0c
SP
1168 for (; caps->matches; caps++)
1169 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
75283501
SP
1170 pr_crit("CPU%d: missing HWCAP: %s\n",
1171 smp_processor_id(), caps->desc);
1172 cpu_die_early();
1173 }
75283501
SP
1174}
1175
1176static void
1177verify_local_cpu_features(const struct arm64_cpu_capabilities *caps)
1178{
1179 for (; caps->matches; caps++) {
92406f0c 1180 if (!cpus_have_cap(caps->capability))
75283501
SP
1181 continue;
1182 /*
1183 * If the new CPU misses an advertised feature, we cannot proceed
1184 * further, park the cpu.
1185 */
92406f0c 1186 if (!caps->matches(caps, SCOPE_LOCAL_CPU)) {
75283501
SP
1187 pr_crit("CPU%d: missing feature: %s\n",
1188 smp_processor_id(), caps->desc);
1189 cpu_die_early();
1190 }
1191 if (caps->enable)
1192 caps->enable(NULL);
1193 }
1194}
1195
2e0f2478
DM
1196static void verify_sve_features(void)
1197{
1198 u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
1199 u64 zcr = read_zcr_features();
1200
1201 unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
1202 unsigned int len = zcr & ZCR_ELx_LEN_MASK;
1203
1204 if (len < safe_len || sve_verify_vq_map()) {
1205 pr_crit("CPU%d: SVE: required vector length(s) missing\n",
1206 smp_processor_id());
1207 cpu_die_early();
1208 }
1209
1210 /* Add checks on other ZCR bits here if necessary */
1211}
1212
dbb4e152
SP
1213/*
1214 * Run through the enabled system capabilities and enable() it on this CPU.
1215 * The capabilities were decided based on the available CPUs at the boot time.
1216 * Any new CPU should match the system wide status of the capability. If the
1217 * new CPU doesn't have a capability which the system now has enabled, we
1218 * cannot do anything to fix it up and could cause unexpected failures. So
1219 * we park the CPU.
1220 */
c47a1900 1221static void verify_local_cpu_capabilities(void)
dbb4e152 1222{
c47a1900
SP
1223 verify_local_cpu_errata_workarounds();
1224 verify_local_cpu_features(arm64_features);
1225 verify_local_elf_hwcaps(arm64_elf_hwcaps);
2e0f2478 1226
c47a1900
SP
1227 if (system_supports_32bit_el0())
1228 verify_local_elf_hwcaps(compat_elf_hwcaps);
2e0f2478
DM
1229
1230 if (system_supports_sve())
1231 verify_sve_features();
c47a1900 1232}
dbb4e152 1233
c47a1900
SP
1234void check_local_cpu_capabilities(void)
1235{
1236 /*
1237 * All secondary CPUs should conform to the early CPU features
1238 * in use by the kernel based on boot CPU.
1239 */
13f417f3
SP
1240 check_early_cpu_features();
1241
dbb4e152 1242 /*
c47a1900
SP
1243 * If we haven't finalised the system capabilities, this CPU gets
1244 * a chance to update the errata work arounds.
1245 * Otherwise, this CPU should verify that it has all the system
1246 * advertised capabilities.
dbb4e152
SP
1247 */
1248 if (!sys_caps_initialised)
c47a1900
SP
1249 update_cpu_errata_workarounds();
1250 else
1251 verify_local_cpu_capabilities();
359b7064
MZ
1252}
1253
a7c61a34 1254static void __init setup_feature_capabilities(void)
359b7064 1255{
ce8b602c
SP
1256 update_cpu_capabilities(arm64_features, "detected feature:");
1257 enable_cpu_capabilities(arm64_features);
359b7064
MZ
1258}
1259
63a1e1c9
MR
1260DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
1261EXPORT_SYMBOL(arm64_const_caps_ready);
1262
1263static void __init mark_const_caps_ready(void)
1264{
1265 static_branch_enable(&arm64_const_caps_ready);
1266}
1267
e3661b12
MZ
1268/*
1269 * Check if the current CPU has a given feature capability.
1270 * Should be called from non-preemptible context.
1271 */
8f413758
MZ
1272static bool __this_cpu_has_cap(const struct arm64_cpu_capabilities *cap_array,
1273 unsigned int cap)
e3661b12
MZ
1274{
1275 const struct arm64_cpu_capabilities *caps;
1276
1277 if (WARN_ON(preemptible()))
1278 return false;
1279
8f413758 1280 for (caps = cap_array; caps->desc; caps++)
e3661b12
MZ
1281 if (caps->capability == cap && caps->matches)
1282 return caps->matches(caps, SCOPE_LOCAL_CPU);
1283
1284 return false;
1285}
1286
8f413758
MZ
1287extern const struct arm64_cpu_capabilities arm64_errata[];
1288
1289bool this_cpu_has_cap(unsigned int cap)
1290{
1291 return (__this_cpu_has_cap(arm64_features, cap) ||
1292 __this_cpu_has_cap(arm64_errata, cap));
1293}
1294
9cdf8ec4 1295void __init setup_cpu_features(void)
359b7064 1296{
9cdf8ec4
SP
1297 u32 cwg;
1298 int cls;
1299
dbb4e152
SP
1300 /* Set the CPU feature capabilies */
1301 setup_feature_capabilities();
8e231852 1302 enable_errata_workarounds();
63a1e1c9 1303 mark_const_caps_ready();
75283501 1304 setup_elf_hwcaps(arm64_elf_hwcaps);
643d703d
SP
1305
1306 if (system_supports_32bit_el0())
1307 setup_elf_hwcaps(compat_elf_hwcaps);
dbb4e152 1308
2e0f2478
DM
1309 sve_setup();
1310
dbb4e152
SP
1311 /* Advertise that we have computed the system capabilities */
1312 set_sys_caps_initialised();
1313
9cdf8ec4
SP
1314 /*
1315 * Check for sane CTR_EL0.CWG value.
1316 */
1317 cwg = cache_type_cwg();
1318 cls = cache_line_size();
1319 if (!cwg)
1320 pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
1321 cls);
1322 if (L1_CACHE_BYTES < cls)
1323 pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
1324 L1_CACHE_BYTES, cls);
359b7064 1325}
70544196
JM
1326
1327static bool __maybe_unused
92406f0c 1328cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
70544196 1329{
a4023f68 1330 return (cpus_have_const_cap(ARM64_HAS_PAN) && !cpus_have_const_cap(ARM64_HAS_UAO));
70544196 1331}
77c97b4e
SP
1332
1333/*
1334 * We emulate only the following system register space.
1335 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 4 - 7]
1336 * See Table C5-6 System instruction encodings for System register accesses,
1337 * ARMv8 ARM(ARM DDI 0487A.f) for more details.
1338 */
1339static inline bool __attribute_const__ is_emulated(u32 id)
1340{
1341 return (sys_reg_Op0(id) == 0x3 &&
1342 sys_reg_CRn(id) == 0x0 &&
1343 sys_reg_Op1(id) == 0x0 &&
1344 (sys_reg_CRm(id) == 0 ||
1345 ((sys_reg_CRm(id) >= 4) && (sys_reg_CRm(id) <= 7))));
1346}
1347
1348/*
1349 * With CRm == 0, reg should be one of :
1350 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
1351 */
1352static inline int emulate_id_reg(u32 id, u64 *valp)
1353{
1354 switch (id) {
1355 case SYS_MIDR_EL1:
1356 *valp = read_cpuid_id();
1357 break;
1358 case SYS_MPIDR_EL1:
1359 *valp = SYS_MPIDR_SAFE_VAL;
1360 break;
1361 case SYS_REVIDR_EL1:
1362 /* IMPLEMENTATION DEFINED values are emulated with 0 */
1363 *valp = 0;
1364 break;
1365 default:
1366 return -EINVAL;
1367 }
1368
1369 return 0;
1370}
1371
1372static int emulate_sys_reg(u32 id, u64 *valp)
1373{
1374 struct arm64_ftr_reg *regp;
1375
1376 if (!is_emulated(id))
1377 return -EINVAL;
1378
1379 if (sys_reg_CRm(id) == 0)
1380 return emulate_id_reg(id, valp);
1381
1382 regp = get_arm64_ftr_reg(id);
1383 if (regp)
1384 *valp = arm64_ftr_reg_user_value(regp);
1385 else
1386 /*
1387 * The untracked registers are either IMPLEMENTATION DEFINED
1388 * (e.g, ID_AFR0_EL1) or reserved RAZ.
1389 */
1390 *valp = 0;
1391 return 0;
1392}
1393
1394static int emulate_mrs(struct pt_regs *regs, u32 insn)
1395{
1396 int rc;
1397 u32 sys_reg, dst;
1398 u64 val;
1399
1400 /*
1401 * sys_reg values are defined as used in mrs/msr instruction.
1402 * shift the imm value to get the encoding.
1403 */
1404 sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
1405 rc = emulate_sys_reg(sys_reg, &val);
1406 if (!rc) {
1407 dst = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
521c6461 1408 pt_regs_write_reg(regs, dst, val);
6436beee 1409 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
77c97b4e
SP
1410 }
1411
1412 return rc;
1413}
1414
1415static struct undef_hook mrs_hook = {
1416 .instr_mask = 0xfff00000,
1417 .instr_val = 0xd5300000,
1418 .pstate_mask = COMPAT_PSR_MODE_MASK,
1419 .pstate_val = PSR_MODE_EL0t,
1420 .fn = emulate_mrs,
1421};
1422
1423static int __init enable_mrs_emulation(void)
1424{
1425 register_undef_hook(&mrs_hook);
1426 return 0;
1427}
1428
c0d8832e 1429core_initcall(enable_mrs_emulation);