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