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