2 * Contains CPU feature definitions
4 * Copyright (C) 2015 ARM Ltd.
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.
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.
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/>.
19 #define pr_fmt(fmt) "CPU features: " fmt
21 #include <linux/bsearch.h>
22 #include <linux/sort.h>
23 #include <linux/types.h>
25 #include <asm/cpufeature.h>
26 #include <asm/processor.h>
27 #include <asm/sysreg.h>
29 unsigned long elf_hwcap __read_mostly;
30 EXPORT_SYMBOL_GPL(elf_hwcap);
33 #define COMPAT_ELF_HWCAP_DEFAULT \
34 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
35 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
36 COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
37 COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
38 COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
40 unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
41 unsigned int compat_elf_hwcap2 __read_mostly;
44 DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
46 #define ARM64_FTR_BITS(STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
52 .safe_val = SAFE_VAL, \
55 #define ARM64_FTR_END \
60 static struct arm64_ftr_bits ftr_id_aa64isar0[] = {
61 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
62 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
63 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0),
64 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
65 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
66 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
67 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
68 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
69 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
73 static struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
74 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
75 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0),
76 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_GIC_SHIFT, 4, 0),
77 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
78 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
79 /* Linux doesn't care about the EL3 */
80 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64PFR0_EL3_SHIFT, 4, 0),
81 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL2_SHIFT, 4, 0),
82 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
83 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
87 static struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
88 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
89 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
90 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
91 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
92 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
93 /* Linux shouldn't care about secure memory */
94 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
95 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
96 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
98 * Differing PARange is fine as long as all peripherals and memory are mapped
99 * within the minimum PARange of all CPUs
101 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
105 static struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
106 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
107 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
108 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
109 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
110 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
111 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
112 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
116 static struct arm64_ftr_bits ftr_ctr[] = {
117 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RAO */
118 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 3, 0),
119 ARM64_FTR_BITS(FTR_STRICT, FTR_HIGHER_SAFE, 24, 4, 0), /* CWG */
120 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0), /* ERG */
121 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 1), /* DminLine */
123 * Linux can handle differing I-cache policies. Userspace JITs will
124 * make use of *minLine
126 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, 14, 2, 0), /* L1Ip */
127 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 10, 0), /* RAZ */
128 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* IminLine */
132 static struct arm64_ftr_bits ftr_id_mmfr0[] = {
133 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0), /* InnerShr */
134 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0), /* FCSE */
135 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
136 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 4, 0), /* TCM */
137 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* ShareLvl */
138 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0), /* OuterShr */
139 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* PMSA */
140 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* VMSA */
144 static struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
145 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
146 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
147 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
148 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
149 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
150 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
151 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
155 static struct arm64_ftr_bits ftr_mvfr2[] = {
156 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
157 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* FPMisc */
158 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* SIMDMisc */
162 static struct arm64_ftr_bits ftr_dczid[] = {
163 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 5, 27, 0), /* RAZ */
164 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
165 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
170 static struct arm64_ftr_bits ftr_id_isar5[] = {
171 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_RDM_SHIFT, 4, 0),
172 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 20, 4, 0), /* RAZ */
173 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_CRC32_SHIFT, 4, 0),
174 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA2_SHIFT, 4, 0),
175 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA1_SHIFT, 4, 0),
176 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_AES_SHIFT, 4, 0),
177 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SEVL_SHIFT, 4, 0),
181 static struct arm64_ftr_bits ftr_id_mmfr4[] = {
182 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
183 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* ac2 */
184 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
188 static struct arm64_ftr_bits ftr_id_pfr0[] = {
189 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 16, 0), /* RAZ */
190 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* State3 */
191 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0), /* State2 */
192 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* State1 */
193 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* State0 */
198 * Common ftr bits for a 32bit register with all hidden, strict
199 * attributes, with 4bit feature fields and a default safe value of
200 * 0. Covers the following 32bit registers:
201 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
203 static struct arm64_ftr_bits ftr_generic_32bits[] = {
204 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
205 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
206 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
207 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
208 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
209 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
210 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
211 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
215 static struct arm64_ftr_bits ftr_generic[] = {
216 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 64, 0),
220 static struct arm64_ftr_bits ftr_generic32[] = {
221 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 32, 0),
225 static struct arm64_ftr_bits ftr_aa64raz[] = {
226 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 64, 0),
230 #define ARM64_FTR_REG(id, table) \
234 .ftr_bits = &((table)[0]), \
237 static struct arm64_ftr_reg arm64_ftr_regs[] = {
239 /* Op1 = 0, CRn = 0, CRm = 1 */
240 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
241 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
242 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_generic_32bits),
243 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
244 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
245 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
246 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
248 /* Op1 = 0, CRn = 0, CRm = 2 */
249 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
250 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
251 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
252 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
253 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
254 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
255 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
257 /* Op1 = 0, CRn = 0, CRm = 3 */
258 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
259 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
260 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
262 /* Op1 = 0, CRn = 0, CRm = 4 */
263 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
264 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_aa64raz),
266 /* Op1 = 0, CRn = 0, CRm = 5 */
267 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
268 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_generic),
270 /* Op1 = 0, CRn = 0, CRm = 6 */
271 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
272 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_aa64raz),
274 /* Op1 = 0, CRn = 0, CRm = 7 */
275 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
276 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
278 /* Op1 = 3, CRn = 0, CRm = 0 */
279 ARM64_FTR_REG(SYS_CTR_EL0, ftr_ctr),
280 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
282 /* Op1 = 3, CRn = 14, CRm = 0 */
283 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_generic32),
286 static int search_cmp_ftr_reg(const void *id, const void *regp)
288 return (int)(unsigned long)id - (int)((const struct arm64_ftr_reg *)regp)->sys_id;
292 * get_arm64_ftr_reg - Lookup a feature register entry using its
293 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
294 * ascending order of sys_id , we use binary search to find a matching
297 * returns - Upon success, matching ftr_reg entry for id.
298 * - NULL on failure. It is upto the caller to decide
299 * the impact of a failure.
301 static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
303 return bsearch((const void *)(unsigned long)sys_id,
305 ARRAY_SIZE(arm64_ftr_regs),
306 sizeof(arm64_ftr_regs[0]),
310 static u64 arm64_ftr_set_value(struct arm64_ftr_bits *ftrp, s64 reg, s64 ftr_val)
312 u64 mask = arm64_ftr_mask(ftrp);
315 reg |= (ftr_val << ftrp->shift) & mask;
319 static s64 arm64_ftr_safe_value(struct arm64_ftr_bits *ftrp, s64 new, s64 cur)
323 switch (ftrp->type) {
325 ret = ftrp->safe_val;
328 ret = new < cur ? new : cur;
330 case FTR_HIGHER_SAFE:
331 ret = new > cur ? new : cur;
340 static int __init sort_cmp_ftr_regs(const void *a, const void *b)
342 return ((const struct arm64_ftr_reg *)a)->sys_id -
343 ((const struct arm64_ftr_reg *)b)->sys_id;
346 static void __init swap_ftr_regs(void *a, void *b, int size)
348 struct arm64_ftr_reg tmp = *(struct arm64_ftr_reg *)a;
349 *(struct arm64_ftr_reg *)a = *(struct arm64_ftr_reg *)b;
350 *(struct arm64_ftr_reg *)b = tmp;
353 static void __init sort_ftr_regs(void)
355 /* Keep the array sorted so that we can do the binary search */
357 ARRAY_SIZE(arm64_ftr_regs),
358 sizeof(arm64_ftr_regs[0]),
364 * Initialise the CPU feature register from Boot CPU values.
365 * Also initiliases the strict_mask for the register.
367 static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
370 u64 strict_mask = ~0x0ULL;
371 struct arm64_ftr_bits *ftrp;
372 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
376 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
377 s64 ftr_new = arm64_ftr_value(ftrp, new);
379 val = arm64_ftr_set_value(ftrp, val, ftr_new);
381 strict_mask &= ~arm64_ftr_mask(ftrp);
384 reg->strict_mask = strict_mask;
387 void __init init_cpu_features(struct cpuinfo_arm64 *info)
389 /* Before we start using the tables, make sure it is sorted */
392 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
393 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
394 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
395 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
396 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
397 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
398 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
399 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
400 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
401 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
402 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
403 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
404 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
405 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
406 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
407 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
408 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
409 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
410 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
411 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
412 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
413 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
414 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
415 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
416 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
417 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
418 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
421 static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
423 struct arm64_ftr_bits *ftrp;
425 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
426 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
427 s64 ftr_new = arm64_ftr_value(ftrp, new);
429 if (ftr_cur == ftr_new)
431 /* Find a safe value */
432 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
433 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
438 static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
440 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
443 update_cpu_ftr_reg(regp, val);
444 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
446 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
447 regp->name, boot, cpu, val);
452 * Update system wide CPU feature registers with the values from a
453 * non-boot CPU. Also performs SANITY checks to make sure that there
454 * aren't any insane variations from that of the boot CPU.
456 void update_cpu_features(int cpu,
457 struct cpuinfo_arm64 *info,
458 struct cpuinfo_arm64 *boot)
463 * The kernel can handle differing I-cache policies, but otherwise
464 * caches should look identical. Userspace JITs will make use of
467 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
468 info->reg_ctr, boot->reg_ctr);
471 * Userspace may perform DC ZVA instructions. Mismatched block sizes
472 * could result in too much or too little memory being zeroed if a
473 * process is preempted and migrated between CPUs.
475 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
476 info->reg_dczid, boot->reg_dczid);
478 /* If different, timekeeping will be broken (especially with KVM) */
479 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
480 info->reg_cntfrq, boot->reg_cntfrq);
483 * The kernel uses self-hosted debug features and expects CPUs to
484 * support identical debug features. We presently need CTX_CMPs, WRPs,
485 * and BRPs to be identical.
486 * ID_AA64DFR1 is currently RES0.
488 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
489 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
490 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
491 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
493 * Even in big.LITTLE, processors should be identical instruction-set
496 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
497 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
498 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
499 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
502 * Differing PARange support is fine as long as all peripherals and
503 * memory are mapped within the minimum PARange of all CPUs.
504 * Linux should not care about secure memory.
506 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
507 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
508 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
509 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
512 * EL3 is not our concern.
513 * ID_AA64PFR1 is currently RES0.
515 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
516 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
517 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
518 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
521 * If we have AArch32, we care about 32-bit features for compat. These
522 * registers should be RES0 otherwise.
524 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
525 info->reg_id_dfr0, boot->reg_id_dfr0);
526 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
527 info->reg_id_isar0, boot->reg_id_isar0);
528 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
529 info->reg_id_isar1, boot->reg_id_isar1);
530 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
531 info->reg_id_isar2, boot->reg_id_isar2);
532 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
533 info->reg_id_isar3, boot->reg_id_isar3);
534 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
535 info->reg_id_isar4, boot->reg_id_isar4);
536 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
537 info->reg_id_isar5, boot->reg_id_isar5);
540 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
541 * ACTLR formats could differ across CPUs and therefore would have to
542 * be trapped for virtualization anyway.
544 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
545 info->reg_id_mmfr0, boot->reg_id_mmfr0);
546 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
547 info->reg_id_mmfr1, boot->reg_id_mmfr1);
548 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
549 info->reg_id_mmfr2, boot->reg_id_mmfr2);
550 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
551 info->reg_id_mmfr3, boot->reg_id_mmfr3);
552 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
553 info->reg_id_pfr0, boot->reg_id_pfr0);
554 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
555 info->reg_id_pfr1, boot->reg_id_pfr1);
556 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
557 info->reg_mvfr0, boot->reg_mvfr0);
558 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
559 info->reg_mvfr1, boot->reg_mvfr1);
560 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
561 info->reg_mvfr2, boot->reg_mvfr2);
564 * Mismatched CPU features are a recipe for disaster. Don't even
565 * pretend to support them.
567 WARN_TAINT_ONCE(taint, TAINT_CPU_OUT_OF_SPEC,
568 "Unsupported CPU feature variation.\n");
571 u64 read_system_reg(u32 id)
573 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
575 /* We shouldn't get a request for an unsupported register */
577 return regp->sys_val;
581 feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
583 int val = cpuid_feature_extract_field(reg, entry->field_pos);
585 return val >= entry->min_field_value;
588 #define __ID_FEAT_CHK(reg) \
589 static bool __maybe_unused \
590 has_##reg##_feature(const struct arm64_cpu_capabilities *entry) \
594 val = read_cpuid(reg##_el1); \
595 return feature_matches(val, entry); \
598 __ID_FEAT_CHK(id_aa64pfr0);
599 __ID_FEAT_CHK(id_aa64mmfr1);
600 __ID_FEAT_CHK(id_aa64isar0);
602 static const struct arm64_cpu_capabilities arm64_features[] = {
604 .desc = "GIC system register CPU interface",
605 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
606 .matches = has_id_aa64pfr0_feature,
608 .min_field_value = 1,
610 #ifdef CONFIG_ARM64_PAN
612 .desc = "Privileged Access Never",
613 .capability = ARM64_HAS_PAN,
614 .matches = has_id_aa64mmfr1_feature,
616 .min_field_value = 1,
617 .enable = cpu_enable_pan,
619 #endif /* CONFIG_ARM64_PAN */
620 #if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
622 .desc = "LSE atomic instructions",
623 .capability = ARM64_HAS_LSE_ATOMICS,
624 .matches = has_id_aa64isar0_feature,
626 .min_field_value = 2,
628 #endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
632 void check_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
637 for (i = 0; caps[i].desc; i++) {
638 if (!caps[i].matches(&caps[i]))
641 if (!cpus_have_cap(caps[i].capability))
642 pr_info("%s %s\n", info, caps[i].desc);
643 cpus_set_cap(caps[i].capability);
646 /* second pass allows enable() to consider interacting capabilities */
647 for (i = 0; caps[i].desc; i++) {
648 if (cpus_have_cap(caps[i].capability) && caps[i].enable)
653 void check_local_cpu_features(void)
655 check_cpu_capabilities(arm64_features, "detected feature:");
658 void __init setup_cpu_features(void)
666 * Check for sane CTR_EL0.CWG value.
668 cwg = cache_type_cwg();
669 cls = cache_line_size();
671 pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
673 if (L1_CACHE_BYTES < cls)
674 pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
675 L1_CACHE_BYTES, cls);
678 * ID_AA64ISAR0_EL1 contains 4-bit wide signed feature blocks.
679 * The blocks we test below represent incremental functionality
680 * for non-negative values. Negative values are reserved.
682 features = read_cpuid(ID_AA64ISAR0_EL1);
683 block = cpuid_feature_extract_field(features, 4);
688 elf_hwcap |= HWCAP_PMULL;
690 elf_hwcap |= HWCAP_AES;
696 if (cpuid_feature_extract_field(features, 8) > 0)
697 elf_hwcap |= HWCAP_SHA1;
699 if (cpuid_feature_extract_field(features, 12) > 0)
700 elf_hwcap |= HWCAP_SHA2;
702 if (cpuid_feature_extract_field(features, 16) > 0)
703 elf_hwcap |= HWCAP_CRC32;
705 block = cpuid_feature_extract_field(features, 20);
710 elf_hwcap |= HWCAP_ATOMICS;
720 * ID_ISAR5_EL1 carries similar information as above, but pertaining to
721 * the AArch32 32-bit execution state.
723 features = read_cpuid(ID_ISAR5_EL1);
724 block = cpuid_feature_extract_field(features, 4);
729 compat_elf_hwcap2 |= COMPAT_HWCAP2_PMULL;
731 compat_elf_hwcap2 |= COMPAT_HWCAP2_AES;
737 if (cpuid_feature_extract_field(features, 8) > 0)
738 compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA1;
740 if (cpuid_feature_extract_field(features, 12) > 0)
741 compat_elf_hwcap2 |= COMPAT_HWCAP2_SHA2;
743 if (cpuid_feature_extract_field(features, 16) > 0)
744 compat_elf_hwcap2 |= COMPAT_HWCAP2_CRC32;