blob: b3202a99e55971e7204abe9e3e5ff70b675374c8 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngier359b7062015-03-27 13:09:23 +00002/*
3 * Contains CPU feature definitions
4 *
5 * Copyright (C) 2015 ARM Ltd.
Will Deacona2a69962020-04-21 15:29:22 +01006 *
7 * A note for the weary kernel hacker: the code here is confusing and hard to
8 * follow! That's partly because it's solving a nasty problem, but also because
9 * there's a little bit of over-abstraction that tends to obscure what's going
10 * on behind a maze of helper functions and macros.
11 *
12 * The basic problem is that hardware folks have started gluing together CPUs
13 * with distinct architectural features; in some cases even creating SoCs where
14 * user-visible instructions are available only on a subset of the available
15 * cores. We try to address this by snapshotting the feature registers of the
16 * boot CPU and comparing these with the feature registers of each secondary
17 * CPU when bringing them up. If there is a mismatch, then we update the
18 * snapshot state to indicate the lowest-common denominator of the feature,
19 * known as the "safe" value. This snapshot state can be queried to view the
20 * "sanitised" value of a feature register.
21 *
22 * The sanitised register values are used to decide which capabilities we
23 * have in the system. These may be in the form of traditional "hwcaps"
24 * advertised to userspace or internal "cpucaps" which are used to configure
25 * things like alternative patching and static keys. While a feature mismatch
26 * may result in a TAINT_CPU_OUT_OF_SPEC kernel taint, a capability mismatch
27 * may prevent a CPU from being onlined at all.
28 *
29 * Some implementation details worth remembering:
30 *
31 * - Mismatched features are *always* sanitised to a "safe" value, which
32 * usually indicates that the feature is not supported.
33 *
34 * - A mismatched feature marked with FTR_STRICT will cause a "SANITY CHECK"
35 * warning when onlining an offending CPU and the kernel will be tainted
36 * with TAINT_CPU_OUT_OF_SPEC.
37 *
38 * - Features marked as FTR_VISIBLE have their sanitised value visible to
39 * userspace. FTR_VISIBLE features in registers that are only visible
40 * to EL0 by trapping *must* have a corresponding HWCAP so that late
41 * onlining of CPUs cannot lead to features disappearing at runtime.
42 *
43 * - A "feature" is typically a 4-bit register field. A "capability" is the
44 * high-level description derived from the sanitised field value.
45 *
46 * - Read the Arm ARM (DDI 0487F.a) section D13.1.3 ("Principles of the ID
47 * scheme for fields in ID registers") to understand when feature fields
48 * may be signed or unsigned (FTR_SIGNED and FTR_UNSIGNED accordingly).
49 *
50 * - KVM exposes its own view of the feature registers to guest operating
51 * systems regardless of FTR_VISIBLE. This is typically driven from the
52 * sanitised register values to allow virtual CPUs to be migrated between
53 * arbitrary physical CPUs, but some features not present on the host are
54 * also advertised and emulated. Look at sys_reg_descs[] for the gory
55 * details.
Will Deacon433022b2020-05-05 11:45:21 +010056 *
57 * - If the arm64_ftr_bits[] for a register has a missing field, then this
58 * field is treated as STRICT RES0, including for read_sanitised_ftr_reg().
59 * This is stronger than FTR_HIDDEN and can be used to hide features from
60 * KVM guests.
Marc Zyngier359b7062015-03-27 13:09:23 +000061 */
62
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010063#define pr_fmt(fmt) "CPU features: " fmt
Marc Zyngier359b7062015-03-27 13:09:23 +000064
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010065#include <linux/bsearch.h>
James Morse2a6dcb22016-10-18 11:27:46 +010066#include <linux/cpumask.h>
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +010067#include <linux/crash_dump.h>
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010068#include <linux/sort.h>
James Morse2a6dcb22016-10-18 11:27:46 +010069#include <linux/stop_machine.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000070#include <linux/types.h>
Laura Abbott2077be62017-01-10 13:35:49 -080071#include <linux/mm.h>
Josh Poimboeufa111b7c2019-04-12 15:39:32 -050072#include <linux/cpu.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000073#include <asm/cpu.h>
74#include <asm/cpufeature.h>
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +010075#include <asm/cpu_ops.h>
Dave Martin2e0f2472017-10-31 15:51:10 +000076#include <asm/fpsimd.h>
Suzuki K Poulose13f417f2016-02-23 10:31:45 +000077#include <asm/mmu_context.h>
James Morse338d4f42015-07-22 19:05:54 +010078#include <asm/processor.h>
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +010079#include <asm/sysreg.h>
Suzuki K Poulose77c97b42017-01-09 17:28:31 +000080#include <asm/traps.h>
Marc Zyngierd88701b2015-01-29 11:24:05 +000081#include <asm/virt.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000082
Andrew Murrayaec0bff2019-04-09 10:52:41 +010083/* Kernel representation of AT_HWCAP and AT_HWCAP2 */
84static unsigned long elf_hwcap __read_mostly;
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010085
86#ifdef CONFIG_COMPAT
87#define COMPAT_ELF_HWCAP_DEFAULT \
88 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
89 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
Suzuki K Poulose7559950a2020-01-13 23:30:20 +000090 COMPAT_HWCAP_TLS|COMPAT_HWCAP_IDIV|\
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010091 COMPAT_HWCAP_LPAE)
92unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
93unsigned int compat_elf_hwcap2 __read_mostly;
94#endif
95
96DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010097EXPORT_SYMBOL(cpu_hwcaps);
Suzuki K Poulose82a3a212018-11-30 17:18:03 +000098static struct arm64_cpu_capabilities const __ro_after_init *cpu_hwcaps_ptrs[ARM64_NCAPS];
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010099
Daniel Thompson0ceb0d52019-01-31 14:58:53 +0000100/* Need also bit for ARM64_CB_PATCH */
101DECLARE_BITMAP(boot_capabilities, ARM64_NPATCHABLE);
102
Mark Brown09e3c222019-12-09 18:12:17 +0000103bool arm64_use_ng_mappings = false;
104EXPORT_SYMBOL(arm64_use_ng_mappings);
105
Dave Martin8f1eec52017-10-31 15:51:09 +0000106/*
107 * Flag to indicate if we have computed the system wide
108 * capabilities based on the boot time active CPUs. This
109 * will be used to determine if a new booting CPU should
110 * go through the verification process to make sure that it
111 * supports the system capabilities, without using a hotplug
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +0000112 * notifier. This is also used to decide if we could use
113 * the fast path for checking constant CPU caps.
Dave Martin8f1eec52017-10-31 15:51:09 +0000114 */
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +0000115DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
116EXPORT_SYMBOL(arm64_const_caps_ready);
117static inline void finalize_system_capabilities(void)
Dave Martin8f1eec52017-10-31 15:51:09 +0000118{
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +0000119 static_branch_enable(&arm64_const_caps_ready);
Dave Martin8f1eec52017-10-31 15:51:09 +0000120}
121
Mark Rutland8effeaa2017-06-21 18:11:23 +0100122static int dump_cpu_hwcaps(struct notifier_block *self, unsigned long v, void *p)
123{
124 /* file-wide pr_fmt adds "CPU features: " prefix */
125 pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
126 return 0;
127}
128
129static struct notifier_block cpu_hwcaps_notifier = {
130 .notifier_call = dump_cpu_hwcaps
131};
132
133static int __init register_cpu_hwcaps_dumper(void)
134{
135 atomic_notifier_chain_register(&panic_notifier_list,
136 &cpu_hwcaps_notifier);
137 return 0;
138}
139__initcall(register_cpu_hwcaps_dumper);
140
Catalin Marinasefd9e032016-09-05 18:25:48 +0100141DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
142EXPORT_SYMBOL(cpu_hwcap_keys);
143
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000144#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100145 { \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +0000146 .sign = SIGNED, \
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000147 .visible = VISIBLE, \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100148 .strict = STRICT, \
149 .type = TYPE, \
150 .shift = SHIFT, \
151 .width = WIDTH, \
152 .safe_val = SAFE_VAL, \
153 }
154
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000155/* Define a feature with unsigned values */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000156#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
157 __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +0000158
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000159/* Define a feature with a signed value */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000160#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
161 __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000162
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100163#define ARM64_FTR_END \
164 { \
165 .width = 0, \
166 }
167
James Morse70544192016-02-05 14:58:50 +0000168/* meta feature for alternatives */
169static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100170cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
171
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +0100172static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
James Morse70544192016-02-05 14:58:50 +0000173
Amit Daniel Kachhap3ff047f2020-03-13 14:34:48 +0530174static bool __system_matches_cap(unsigned int n);
175
Suzuki K Poulose4aa8a472017-01-09 17:28:32 +0000176/*
177 * NOTE: Any changes to the visibility of features should be kept in
178 * sync with the documentation of the CPU feature register ABI.
179 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100180static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
Richard Henderson1a50ec02020-01-21 12:58:52 +0000181 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RNDR_SHIFT, 4, 0),
Anshuman Khandual7cd51a52020-05-19 15:10:46 +0530182 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TLB_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000183 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TS_SHIFT, 4, 0),
Dongjiu Geng3b3b6812017-12-13 18:13:56 +0800184 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_FHM_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100185 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
186 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
187 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
188 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
189 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000190 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
191 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
192 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
193 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
194 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100195 ARM64_FTR_END,
196};
197
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000198static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
Steven Priced4209d82019-12-16 11:33:37 +0000199 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_I8MM_SHIFT, 4, 0),
200 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DGH_SHIFT, 4, 0),
201 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_BF16_SHIFT, 4, 0),
202 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SPECRES_SHIFT, 4, 0),
Will Deaconbd4fb6d2018-06-14 11:21:34 +0100203 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SB_SHIFT, 4, 0),
Julien Grall7230f7e2019-10-03 12:12:08 +0100204 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FRINTTS_SHIFT, 4, 0),
Mark Rutland6984eb42018-12-07 18:39:24 +0000205 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
206 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPI_SHIFT, 4, 0),
207 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
208 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPA_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100209 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_LRCPC_SHIFT, 4, 0),
210 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FCMA_SHIFT, 4, 0),
211 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_JSCVT_SHIFT, 4, 0),
Mark Rutland6984eb42018-12-07 18:39:24 +0000212 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
213 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_API_SHIFT, 4, 0),
214 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
215 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_APA_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100216 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DPB_SHIFT, 4, 0),
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000217 ARM64_FTR_END,
218};
219
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100220static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
Will Deacon179a56f2017-11-27 18:29:30 +0000221 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
Will Deacon0f15adb2018-01-03 11:17:58 +0000222 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000223 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_DIT_SHIFT, 4, 0),
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +0000224 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_AMU_SHIFT, 4, 0),
Anshuman Khandual011e5f52020-05-19 15:10:47 +0530225 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_MPAM_SHIFT, 4, 0),
226 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SEL2_SHIFT, 4, 0),
Dave Martin3fab3992017-12-14 14:03:44 +0000227 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
228 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SVE_SHIFT, 4, 0),
Xie XiuQi64c02722018-01-15 19:38:56 +0000229 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_RAS_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100230 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_GIC_SHIFT, 4, 0),
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000231 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
232 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100233 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
Will Deacon98448cd2020-04-21 15:29:21 +0100234 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
235 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
236 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100237 ARM64_FTR_END,
238};
239
Will Deacond71be2b2018-06-15 11:37:34 +0100240static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
Anshuman Khandual14e270f2020-05-19 15:10:48 +0530241 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_MPAMFRAC_SHIFT, 4, 0),
242 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_RASFRAC_SHIFT, 4, 0),
Will Deacond71be2b2018-06-15 11:37:34 +0100243 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_SSBS_SHIFT, 4, ID_AA64PFR1_SSBS_PSTATE_NI),
244 ARM64_FTR_END,
245};
246
Dave Martin06a916f2019-04-18 18:41:38 +0100247static const struct arm64_ftr_bits ftr_id_aa64zfr0[] = {
Julien Grallec52c712019-10-14 11:21:13 +0100248 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Steven Priced4209d82019-12-16 11:33:37 +0000249 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_F64MM_SHIFT, 4, 0),
250 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
251 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_F32MM_SHIFT, 4, 0),
252 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
253 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_I8MM_SHIFT, 4, 0),
254 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Julien Grallec52c712019-10-14 11:21:13 +0100255 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SM4_SHIFT, 4, 0),
256 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
257 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SHA3_SHIFT, 4, 0),
258 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Steven Priced4209d82019-12-16 11:33:37 +0000259 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_BF16_SHIFT, 4, 0),
260 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Julien Grallec52c712019-10-14 11:21:13 +0100261 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_BITPERM_SHIFT, 4, 0),
262 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
263 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_AES_SHIFT, 4, 0),
264 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
265 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SVEVER_SHIFT, 4, 0),
Dave Martin06a916f2019-04-18 18:41:38 +0100266 ARM64_FTR_END,
267};
268
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100269static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
Will Deacon5717fe52019-08-12 16:02:25 +0100270 /*
Marc Zyngierb130a8f2020-05-28 14:12:58 +0100271 * Page size not being supported at Stage-2 is not fatal. You
272 * just give up KVM if PAGE_SIZE isn't supported there. Go fix
273 * your favourite nesting hypervisor.
274 *
275 * There is a small corner case where the hypervisor explicitly
276 * advertises a given granule size at Stage-2 (value 2) on some
277 * vCPUs, and uses the fallback to Stage-1 (value 0) for other
278 * vCPUs. Although this is not forbidden by the architecture, it
279 * indicates that the hypervisor is being silly (or buggy).
280 *
281 * We make no effort to cope with this and pretend that if these
282 * fields are inconsistent across vCPUs, then it isn't worth
283 * trying to bring KVM up.
284 */
285 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN4_2_SHIFT, 4, 1),
286 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN64_2_SHIFT, 4, 1),
287 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN16_2_SHIFT, 4, 1),
288 /*
Will Deacon5717fe52019-08-12 16:02:25 +0100289 * We already refuse to boot CPUs that don't support our configured
290 * page size, so we can only detect mismatches for a page size other
291 * than the one we're currently using. Unfortunately, SoCs like this
292 * exist in the wild so, even though we don't like it, we'll have to go
293 * along with it and treat them as non-strict.
294 */
295 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
296 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
297 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
298
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100299 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100300 /* Linux shouldn't care about secure memory */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100301 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
302 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
303 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100304 /*
305 * Differing PARange is fine as long as all peripherals and memory are mapped
306 * within the minimum PARange of all CPUs
307 */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000308 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100309 ARM64_FTR_END,
310};
311
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100312static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000313 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100314 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
315 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
316 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
317 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
318 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100319 ARM64_FTR_END,
320};
321
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100322static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
Mark Brown3e6c69a2019-12-09 18:12:14 +0000323 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_E0PD_SHIFT, 4, 0),
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100324 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_FWB_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000325 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_AT_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100326 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
Sai Prakash Ranjan9d3f8882020-04-21 15:29:15 +0100327 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100328 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
329 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
330 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000331 ARM64_FTR_END,
332};
333
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100334static const struct arm64_ftr_bits ftr_ctr[] = {
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600335 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
336 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DIC_SHIFT, 1, 1),
337 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IDC_SHIFT, 1, 1),
Will Deacon147b9632019-07-30 15:40:20 +0100338 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_CWG_SHIFT, 4, 0),
339 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_ERG_SHIFT, 4, 0),
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600340 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DMINLINE_SHIFT, 4, 1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100341 /*
342 * Linux can handle differing I-cache policies. Userspace JITs will
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100343 * make use of *minLine.
Will Deacon155433c2017-03-10 20:32:22 +0000344 * If we have differing I-cache policies, report it as the weakest - VIPT.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100345 */
Will Deacon155433c2017-03-10 20:32:22 +0000346 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_VIPT), /* L1Ip */
Suzuki K Poulose4c4a39d2018-07-04 23:07:45 +0100347 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IMINLINE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100348 ARM64_FTR_END,
349};
350
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100351struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
352 .name = "SYS_CTR_EL0",
353 .ftr_bits = ftr_ctr
354};
355
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100356static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100357 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0xf), /* InnerShr */
358 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0), /* FCSE */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000359 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100360 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0), /* TCM */
361 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* ShareLvl */
362 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0xf), /* OuterShr */
363 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* PMSA */
364 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* VMSA */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100365 ARM64_FTR_END,
366};
367
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100368static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
Anshuman Khanduale965bcb2020-05-19 15:10:40 +0530369 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 36, 4, 0),
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000370 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_PMSVER_SHIFT, 4, 0),
371 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
372 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
373 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
Will Deaconb20d1ba2016-07-25 16:17:52 +0100374 /*
375 * We can instantiate multiple PMU instances with different levels
376 * of support.
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000377 */
378 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
379 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
380 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100381 ARM64_FTR_END,
382};
383
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100384static const struct arm64_ftr_bits ftr_mvfr2[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100385 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* FPMisc */
386 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* SIMDMisc */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100387 ARM64_FTR_END,
388};
389
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100390static const struct arm64_ftr_bits ftr_dczid[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000391 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
392 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100393 ARM64_FTR_END,
394};
395
Anshuman Khandual2a5bc6c2020-05-19 15:10:38 +0530396static const struct arm64_ftr_bits ftr_id_isar0[] = {
397 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_DIVIDE_SHIFT, 4, 0),
398 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_DEBUG_SHIFT, 4, 0),
399 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_COPROC_SHIFT, 4, 0),
400 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_CMPBRANCH_SHIFT, 4, 0),
401 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_BITFIELD_SHIFT, 4, 0),
402 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_BITCOUNT_SHIFT, 4, 0),
403 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_SWAP_SHIFT, 4, 0),
404 ARM64_FTR_END,
405};
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100406
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100407static const struct arm64_ftr_bits ftr_id_isar5[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100408 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_RDM_SHIFT, 4, 0),
409 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_CRC32_SHIFT, 4, 0),
410 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA2_SHIFT, 4, 0),
411 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA1_SHIFT, 4, 0),
412 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_AES_SHIFT, 4, 0),
413 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SEVL_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100414 ARM64_FTR_END,
415};
416
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100417static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
Anshuman Khandualfcd65352020-05-19 15:10:45 +0530418 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EVT_SHIFT, 4, 0),
419 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_CCIDX_SHIFT, 4, 0),
420 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_LSM_SHIFT, 4, 0),
421 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_HPDS_SHIFT, 4, 0),
422 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_CNP_SHIFT, 4, 0),
423 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_XNX_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100424 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* ac2 */
Anshuman Khandualfcd65352020-05-19 15:10:45 +0530425 /*
426 * SpecSEI = 1 indicates that the PE might generate an SError on an
427 * external abort on speculative read. It is safe to assume that an
428 * SError might be generated than it will not be. Hence it has been
429 * classified as FTR_HIGHER_SAFE.
430 */
431 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_MMFR4_SPECSEI_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100432 ARM64_FTR_END,
433};
434
Will Deacon01133402020-04-21 15:29:16 +0100435static const struct arm64_ftr_bits ftr_id_isar4[] = {
436 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SWP_FRAC_SHIFT, 4, 0),
437 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_PSR_M_SHIFT, 4, 0),
438 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SYNCH_PRIM_FRAC_SHIFT, 4, 0),
439 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_BARRIER_SHIFT, 4, 0),
440 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SMC_SHIFT, 4, 0),
441 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WRITEBACK_SHIFT, 4, 0),
442 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WITHSHIFTS_SHIFT, 4, 0),
443 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_UNPRIV_SHIFT, 4, 0),
444 ARM64_FTR_END,
445};
446
Anshuman Khandual152accf82020-05-19 15:10:43 +0530447static const struct arm64_ftr_bits ftr_id_mmfr5[] = {
448 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR5_ETS_SHIFT, 4, 0),
449 ARM64_FTR_END,
450};
451
Anshuman Khandual8e3747b2019-12-17 20:17:32 +0530452static const struct arm64_ftr_bits ftr_id_isar6[] = {
453 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_I8MM_SHIFT, 4, 0),
454 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_BF16_SHIFT, 4, 0),
455 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SPECRES_SHIFT, 4, 0),
456 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SB_SHIFT, 4, 0),
457 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_FHM_SHIFT, 4, 0),
458 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_DP_SHIFT, 4, 0),
459 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_JSCVT_SHIFT, 4, 0),
460 ARM64_FTR_END,
461};
462
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100463static const struct arm64_ftr_bits ftr_id_pfr0[] = {
Anshuman Khandual0ae43a92020-05-19 15:10:44 +0530464 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_DIT_SHIFT, 4, 0),
465 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR0_CSV2_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100466 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* State3 */
467 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0), /* State2 */
468 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* State1 */
469 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* State0 */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100470 ARM64_FTR_END,
471};
472
Will Deacon01133402020-04-21 15:29:16 +0100473static const struct arm64_ftr_bits ftr_id_pfr1[] = {
474 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GIC_SHIFT, 4, 0),
475 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRT_FRAC_SHIFT, 4, 0),
476 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SEC_FRAC_SHIFT, 4, 0),
477 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GENTIMER_SHIFT, 4, 0),
478 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRTUALIZATION_SHIFT, 4, 0),
479 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_MPROGMOD_SHIFT, 4, 0),
480 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SECURITY_SHIFT, 4, 0),
481 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_PROGMOD_SHIFT, 4, 0),
482 ARM64_FTR_END,
483};
484
Anshuman Khandual16824082020-05-19 15:10:41 +0530485static const struct arm64_ftr_bits ftr_id_pfr2[] = {
486 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR2_SSBS_SHIFT, 4, 0),
487 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR2_CSV3_SHIFT, 4, 0),
488 ARM64_FTR_END,
489};
490
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100491static const struct arm64_ftr_bits ftr_id_dfr0[] = {
Anshuman Khandual1ed1b902020-05-19 15:10:39 +0530492 /* [31:28] TraceFilt */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000493 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
494 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
495 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
496 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
497 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
498 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
499 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000500 ARM64_FTR_END,
501};
502
Anshuman Khandualdd35ec02020-05-19 15:10:42 +0530503static const struct arm64_ftr_bits ftr_id_dfr1[] = {
504 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR1_MTPMU_SHIFT, 4, 0),
505 ARM64_FTR_END,
506};
507
Dave Martin2e0f2472017-10-31 15:51:10 +0000508static const struct arm64_ftr_bits ftr_zcr[] = {
509 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
510 ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_SIZE, 0), /* LEN */
511 ARM64_FTR_END,
512};
513
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100514/*
515 * Common ftr bits for a 32bit register with all hidden, strict
516 * attributes, with 4bit feature fields and a default safe value of
517 * 0. Covers the following 32bit registers:
Anshuman Khandual2a5bc6c2020-05-19 15:10:38 +0530518 * id_isar[1-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100519 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100520static const struct arm64_ftr_bits ftr_generic_32bits[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000521 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
522 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
523 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
524 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
525 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
526 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
527 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
528 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100529 ARM64_FTR_END,
530};
531
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000532/* Table for a single 32bit feature value */
533static const struct arm64_ftr_bits ftr_single32[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000534 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100535 ARM64_FTR_END,
536};
537
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000538static const struct arm64_ftr_bits ftr_raz[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100539 ARM64_FTR_END,
540};
541
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100542#define ARM64_FTR_REG(id, table) { \
543 .sys_id = id, \
544 .reg = &(struct arm64_ftr_reg){ \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100545 .name = #id, \
546 .ftr_bits = &((table)[0]), \
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100547 }}
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100548
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100549static const struct __ftr_reg_entry {
550 u32 sys_id;
551 struct arm64_ftr_reg *reg;
552} arm64_ftr_regs[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100553
554 /* Op1 = 0, CRn = 0, CRm = 1 */
555 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
Will Deacon01133402020-04-21 15:29:16 +0100556 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_id_pfr1),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000557 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100558 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
559 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
560 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
561 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
562
563 /* Op1 = 0, CRn = 0, CRm = 2 */
Anshuman Khandual2a5bc6c2020-05-19 15:10:38 +0530564 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_id_isar0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100565 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
566 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
567 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
Will Deacon01133402020-04-21 15:29:16 +0100568 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_id_isar4),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100569 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
570 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
Anshuman Khandual8e3747b2019-12-17 20:17:32 +0530571 ARM64_FTR_REG(SYS_ID_ISAR6_EL1, ftr_id_isar6),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100572
573 /* Op1 = 0, CRn = 0, CRm = 3 */
574 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
575 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
576 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
Anshuman Khandual16824082020-05-19 15:10:41 +0530577 ARM64_FTR_REG(SYS_ID_PFR2_EL1, ftr_id_pfr2),
Anshuman Khandualdd35ec02020-05-19 15:10:42 +0530578 ARM64_FTR_REG(SYS_ID_DFR1_EL1, ftr_id_dfr1),
Anshuman Khandual152accf82020-05-19 15:10:43 +0530579 ARM64_FTR_REG(SYS_ID_MMFR5_EL1, ftr_id_mmfr5),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100580
581 /* Op1 = 0, CRn = 0, CRm = 4 */
582 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
Will Deacond71be2b2018-06-15 11:37:34 +0100583 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1),
Dave Martin06a916f2019-04-18 18:41:38 +0100584 ARM64_FTR_REG(SYS_ID_AA64ZFR0_EL1, ftr_id_aa64zfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100585
586 /* Op1 = 0, CRn = 0, CRm = 5 */
587 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000588 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100589
590 /* Op1 = 0, CRn = 0, CRm = 6 */
591 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000592 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100593
594 /* Op1 = 0, CRn = 0, CRm = 7 */
595 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
596 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
James Morse406e3082016-02-05 14:58:47 +0000597 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100598
Dave Martin2e0f2472017-10-31 15:51:10 +0000599 /* Op1 = 0, CRn = 1, CRm = 2 */
600 ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
601
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100602 /* Op1 = 3, CRn = 0, CRm = 0 */
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100603 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100604 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
605
606 /* Op1 = 3, CRn = 14, CRm = 0 */
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000607 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100608};
609
610static int search_cmp_ftr_reg(const void *id, const void *regp)
611{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100612 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100613}
614
615/*
Anshuman Khandual3577dd32020-05-27 15:34:36 +0530616 * get_arm64_ftr_reg_nowarn - Looks up a feature register entry using
617 * its sys_reg() encoding. With the array arm64_ftr_regs sorted in the
618 * ascending order of sys_id, we use binary search to find a matching
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100619 * entry.
620 *
621 * returns - Upon success, matching ftr_reg entry for id.
622 * - NULL on failure. It is upto the caller to decide
623 * the impact of a failure.
624 */
Anshuman Khandual3577dd32020-05-27 15:34:36 +0530625static struct arm64_ftr_reg *get_arm64_ftr_reg_nowarn(u32 sys_id)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100626{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100627 const struct __ftr_reg_entry *ret;
628
629 ret = bsearch((const void *)(unsigned long)sys_id,
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100630 arm64_ftr_regs,
631 ARRAY_SIZE(arm64_ftr_regs),
632 sizeof(arm64_ftr_regs[0]),
633 search_cmp_ftr_reg);
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100634 if (ret)
635 return ret->reg;
636 return NULL;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100637}
638
Anshuman Khandual3577dd32020-05-27 15:34:36 +0530639/*
640 * get_arm64_ftr_reg - Looks up a feature register entry using
641 * its sys_reg() encoding. This calls get_arm64_ftr_reg_nowarn().
642 *
643 * returns - Upon success, matching ftr_reg entry for id.
644 * - NULL on failure but with an WARN_ON().
645 */
646static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
647{
648 struct arm64_ftr_reg *reg;
649
650 reg = get_arm64_ftr_reg_nowarn(sys_id);
651
652 /*
653 * Requesting a non-existent register search is an error. Warn
654 * and let the caller handle it.
655 */
656 WARN_ON(!reg);
657 return reg;
658}
659
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100660static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
661 s64 ftr_val)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100662{
663 u64 mask = arm64_ftr_mask(ftrp);
664
665 reg &= ~mask;
666 reg |= (ftr_val << ftrp->shift) & mask;
667 return reg;
668}
669
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100670static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
671 s64 cur)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100672{
673 s64 ret = 0;
674
675 switch (ftrp->type) {
676 case FTR_EXACT:
677 ret = ftrp->safe_val;
678 break;
679 case FTR_LOWER_SAFE:
680 ret = new < cur ? new : cur;
681 break;
Will Deacon147b9632019-07-30 15:40:20 +0100682 case FTR_HIGHER_OR_ZERO_SAFE:
683 if (!cur || !new)
684 break;
685 /* Fallthrough */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100686 case FTR_HIGHER_SAFE:
687 ret = new > cur ? new : cur;
688 break;
689 default:
690 BUG();
691 }
692
693 return ret;
694}
695
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100696static void __init sort_ftr_regs(void)
697{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100698 int i;
699
700 /* Check that the array is sorted so that we can do the binary search */
701 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
702 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100703}
704
705/*
706 * Initialise the CPU feature register from Boot CPU values.
707 * Also initiliases the strict_mask for the register.
Mark Rutlandb389d792017-01-09 17:28:24 +0000708 * Any bits that are not covered by an arm64_ftr_bits entry are considered
709 * RES0 for the system-wide value, and must strictly match.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100710 */
711static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
712{
713 u64 val = 0;
714 u64 strict_mask = ~0x0ULL;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000715 u64 user_mask = 0;
Mark Rutlandb389d792017-01-09 17:28:24 +0000716 u64 valid_mask = 0;
717
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100718 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100719 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
720
Anshuman Khandual3577dd32020-05-27 15:34:36 +0530721 if (!reg)
722 return;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100723
韩科才24b2cce2020-03-11 14:52:49 +0800724 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
Mark Rutlandb389d792017-01-09 17:28:24 +0000725 u64 ftr_mask = arm64_ftr_mask(ftrp);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100726 s64 ftr_new = arm64_ftr_value(ftrp, new);
727
728 val = arm64_ftr_set_value(ftrp, val, ftr_new);
Mark Rutlandb389d792017-01-09 17:28:24 +0000729
730 valid_mask |= ftr_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100731 if (!ftrp->strict)
Mark Rutlandb389d792017-01-09 17:28:24 +0000732 strict_mask &= ~ftr_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000733 if (ftrp->visible)
734 user_mask |= ftr_mask;
735 else
736 reg->user_val = arm64_ftr_set_value(ftrp,
737 reg->user_val,
738 ftrp->safe_val);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100739 }
Mark Rutlandb389d792017-01-09 17:28:24 +0000740
741 val &= valid_mask;
742
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100743 reg->sys_val = val;
744 reg->strict_mask = strict_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000745 reg->user_mask = user_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100746}
747
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +0100748extern const struct arm64_cpu_capabilities arm64_errata[];
Suzuki K Poulose82a3a212018-11-30 17:18:03 +0000749static const struct arm64_cpu_capabilities arm64_features[];
750
751static void __init
752init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
753{
754 for (; caps->matches; caps++) {
755 if (WARN(caps->capability >= ARM64_NCAPS,
756 "Invalid capability %d\n", caps->capability))
757 continue;
758 if (WARN(cpu_hwcaps_ptrs[caps->capability],
759 "Duplicate entry for capability %d\n",
760 caps->capability))
761 continue;
762 cpu_hwcaps_ptrs[caps->capability] = caps;
763 }
764}
765
766static void __init init_cpu_hwcaps_indirect_list(void)
767{
768 init_cpu_hwcaps_indirect_list_from_array(arm64_features);
769 init_cpu_hwcaps_indirect_list_from_array(arm64_errata);
770}
771
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100772static void __init setup_boot_cpu_capabilities(void);
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +0100773
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100774void __init init_cpu_features(struct cpuinfo_arm64 *info)
775{
776 /* Before we start using the tables, make sure it is sorted */
777 sort_ftr_regs();
778
779 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
780 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
781 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
782 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
783 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
784 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
785 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
786 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
787 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000788 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100789 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
790 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
Dave Martin2e0f2472017-10-31 15:51:10 +0000791 init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100792
793 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
794 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
Anshuman Khandualdd35ec02020-05-19 15:10:42 +0530795 init_cpu_ftr_reg(SYS_ID_DFR1_EL1, info->reg_id_dfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100796 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
797 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
798 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
799 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
800 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
801 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
Anshuman Khandual8e3747b2019-12-17 20:17:32 +0530802 init_cpu_ftr_reg(SYS_ID_ISAR6_EL1, info->reg_id_isar6);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100803 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
804 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
805 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
806 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
Anshuman Khandual858b8a82020-05-19 15:10:54 +0530807 init_cpu_ftr_reg(SYS_ID_MMFR4_EL1, info->reg_id_mmfr4);
Anshuman Khandual152accf82020-05-19 15:10:43 +0530808 init_cpu_ftr_reg(SYS_ID_MMFR5_EL1, info->reg_id_mmfr5);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100809 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
810 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
Anshuman Khandual16824082020-05-19 15:10:41 +0530811 init_cpu_ftr_reg(SYS_ID_PFR2_EL1, info->reg_id_pfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100812 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
813 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
814 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
815 }
816
Dave Martin2e0f2472017-10-31 15:51:10 +0000817 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
818 init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
819 sve_init_vq_map();
820 }
Suzuki K Poulose5e911072018-03-26 15:12:29 +0100821
822 /*
Suzuki K Poulose82a3a212018-11-30 17:18:03 +0000823 * Initialize the indirect array of CPU hwcaps capabilities pointers
824 * before we handle the boot CPU below.
825 */
826 init_cpu_hwcaps_indirect_list();
827
828 /*
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100829 * Detect and enable early CPU capabilities based on the boot CPU,
830 * after we have initialised the CPU feature infrastructure.
Suzuki K Poulose5e911072018-03-26 15:12:29 +0100831 */
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100832 setup_boot_cpu_capabilities();
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100833}
834
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100835static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100836{
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100837 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100838
839 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
840 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
841 s64 ftr_new = arm64_ftr_value(ftrp, new);
842
843 if (ftr_cur == ftr_new)
844 continue;
845 /* Find a safe value */
846 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
847 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
848 }
849
850}
851
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100852static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100853{
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100854 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
855
Anshuman Khandual3577dd32020-05-27 15:34:36 +0530856 if (!regp)
857 return 0;
858
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100859 update_cpu_ftr_reg(regp, val);
860 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
861 return 0;
862 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
863 regp->name, boot, cpu, val);
864 return 1;
865}
866
Will Deaconeab2f922020-04-21 15:29:20 +0100867static void relax_cpu_ftr_reg(u32 sys_id, int field)
868{
869 const struct arm64_ftr_bits *ftrp;
870 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
871
Anshuman Khandual3577dd32020-05-27 15:34:36 +0530872 if (!regp)
Will Deaconeab2f922020-04-21 15:29:20 +0100873 return;
874
875 for (ftrp = regp->ftr_bits; ftrp->width; ftrp++) {
876 if (ftrp->shift == field) {
877 regp->strict_mask &= ~arm64_ftr_mask(ftrp);
878 break;
879 }
880 }
881
882 /* Bogus field? */
883 WARN_ON(!ftrp->width);
884}
885
Will Deacon1efcfe72020-04-21 15:29:19 +0100886static int update_32bit_cpu_features(int cpu, struct cpuinfo_arm64 *info,
887 struct cpuinfo_arm64 *boot)
888{
889 int taint = 0;
890 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
891
892 /*
893 * If we don't have AArch32 at all then skip the checks entirely
894 * as the register values may be UNKNOWN and we're not going to be
895 * using them for anything.
896 */
897 if (!id_aa64pfr0_32bit_el0(pfr0))
898 return taint;
899
Will Deaconeab2f922020-04-21 15:29:20 +0100900 /*
901 * If we don't have AArch32 at EL1, then relax the strictness of
902 * EL1-dependent register fields to avoid spurious sanity check fails.
903 */
904 if (!id_aa64pfr0_32bit_el1(pfr0)) {
905 relax_cpu_ftr_reg(SYS_ID_ISAR4_EL1, ID_ISAR4_SMC_SHIFT);
906 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRT_FRAC_SHIFT);
907 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SEC_FRAC_SHIFT);
908 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRTUALIZATION_SHIFT);
909 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SECURITY_SHIFT);
910 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_PROGMOD_SHIFT);
911 }
912
Will Deacon1efcfe72020-04-21 15:29:19 +0100913 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
914 info->reg_id_dfr0, boot->reg_id_dfr0);
Anshuman Khandualdd35ec02020-05-19 15:10:42 +0530915 taint |= check_update_ftr_reg(SYS_ID_DFR1_EL1, cpu,
916 info->reg_id_dfr1, boot->reg_id_dfr1);
Will Deacon1efcfe72020-04-21 15:29:19 +0100917 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
918 info->reg_id_isar0, boot->reg_id_isar0);
919 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
920 info->reg_id_isar1, boot->reg_id_isar1);
921 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
922 info->reg_id_isar2, boot->reg_id_isar2);
923 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
924 info->reg_id_isar3, boot->reg_id_isar3);
925 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
926 info->reg_id_isar4, boot->reg_id_isar4);
927 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
928 info->reg_id_isar5, boot->reg_id_isar5);
929 taint |= check_update_ftr_reg(SYS_ID_ISAR6_EL1, cpu,
930 info->reg_id_isar6, boot->reg_id_isar6);
931
932 /*
933 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
934 * ACTLR formats could differ across CPUs and therefore would have to
935 * be trapped for virtualization anyway.
936 */
937 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
938 info->reg_id_mmfr0, boot->reg_id_mmfr0);
939 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
940 info->reg_id_mmfr1, boot->reg_id_mmfr1);
941 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
942 info->reg_id_mmfr2, boot->reg_id_mmfr2);
943 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
944 info->reg_id_mmfr3, boot->reg_id_mmfr3);
Anshuman Khandual858b8a82020-05-19 15:10:54 +0530945 taint |= check_update_ftr_reg(SYS_ID_MMFR4_EL1, cpu,
946 info->reg_id_mmfr4, boot->reg_id_mmfr4);
Anshuman Khandual152accf82020-05-19 15:10:43 +0530947 taint |= check_update_ftr_reg(SYS_ID_MMFR5_EL1, cpu,
948 info->reg_id_mmfr5, boot->reg_id_mmfr5);
Will Deacon1efcfe72020-04-21 15:29:19 +0100949 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
950 info->reg_id_pfr0, boot->reg_id_pfr0);
951 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
952 info->reg_id_pfr1, boot->reg_id_pfr1);
Anshuman Khandual16824082020-05-19 15:10:41 +0530953 taint |= check_update_ftr_reg(SYS_ID_PFR2_EL1, cpu,
954 info->reg_id_pfr2, boot->reg_id_pfr2);
Will Deacon1efcfe72020-04-21 15:29:19 +0100955 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
956 info->reg_mvfr0, boot->reg_mvfr0);
957 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
958 info->reg_mvfr1, boot->reg_mvfr1);
959 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
960 info->reg_mvfr2, boot->reg_mvfr2);
961
962 return taint;
963}
964
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100965/*
966 * Update system wide CPU feature registers with the values from a
967 * non-boot CPU. Also performs SANITY checks to make sure that there
968 * aren't any insane variations from that of the boot CPU.
969 */
970void update_cpu_features(int cpu,
971 struct cpuinfo_arm64 *info,
972 struct cpuinfo_arm64 *boot)
973{
974 int taint = 0;
975
976 /*
977 * The kernel can handle differing I-cache policies, but otherwise
978 * caches should look identical. Userspace JITs will make use of
979 * *minLine.
980 */
981 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
982 info->reg_ctr, boot->reg_ctr);
983
984 /*
985 * Userspace may perform DC ZVA instructions. Mismatched block sizes
986 * could result in too much or too little memory being zeroed if a
987 * process is preempted and migrated between CPUs.
988 */
989 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
990 info->reg_dczid, boot->reg_dczid);
991
992 /* If different, timekeeping will be broken (especially with KVM) */
993 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
994 info->reg_cntfrq, boot->reg_cntfrq);
995
996 /*
997 * The kernel uses self-hosted debug features and expects CPUs to
998 * support identical debug features. We presently need CTX_CMPs, WRPs,
999 * and BRPs to be identical.
1000 * ID_AA64DFR1 is currently RES0.
1001 */
1002 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
1003 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
1004 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
1005 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
1006 /*
1007 * Even in big.LITTLE, processors should be identical instruction-set
1008 * wise.
1009 */
1010 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
1011 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
1012 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
1013 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
1014
1015 /*
1016 * Differing PARange support is fine as long as all peripherals and
1017 * memory are mapped within the minimum PARange of all CPUs.
1018 * Linux should not care about secure memory.
1019 */
1020 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
1021 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
1022 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
1023 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +00001024 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
1025 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
Suzuki K. Poulose3086d392015-10-19 14:24:46 +01001026
Suzuki K. Poulose3086d392015-10-19 14:24:46 +01001027 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
1028 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
1029 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
1030 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
1031
Dave Martin2e0f2472017-10-31 15:51:10 +00001032 taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
1033 info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
1034
Dave Martin2e0f2472017-10-31 15:51:10 +00001035 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
1036 taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
1037 info->reg_zcr, boot->reg_zcr);
1038
1039 /* Probe vector lengths, unless we already gave up on SVE */
1040 if (id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +00001041 !system_capabilities_finalized())
Dave Martin2e0f2472017-10-31 15:51:10 +00001042 sve_update_vq_map();
1043 }
1044
Suzuki K. Poulose3086d392015-10-19 14:24:46 +01001045 /*
Will Deacon1efcfe72020-04-21 15:29:19 +01001046 * This relies on a sanitised view of the AArch64 ID registers
1047 * (e.g. SYS_ID_AA64PFR0_EL1), so we call it last.
1048 */
1049 taint |= update_32bit_cpu_features(cpu, info, boot);
1050
1051 /*
Suzuki K. Poulose3086d392015-10-19 14:24:46 +01001052 * Mismatched CPU features are a recipe for disaster. Don't even
1053 * pretend to support them.
1054 */
Will Deacon8dd0ee62017-06-05 11:40:23 +01001055 if (taint) {
1056 pr_warn_once("Unsupported CPU feature variation detected.\n");
1057 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1058 }
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +01001059}
1060
Dave Martin46823dd2017-03-23 15:14:39 +00001061u64 read_sanitised_ftr_reg(u32 id)
Suzuki K. Pouloseb3f15372015-10-19 14:24:47 +01001062{
1063 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
1064
Anshuman Khandual3577dd32020-05-27 15:34:36 +05301065 if (!regp)
1066 return 0;
Suzuki K. Pouloseb3f15372015-10-19 14:24:47 +01001067 return regp->sys_val;
1068}
Marc Zyngier359b7062015-03-27 13:09:23 +00001069
Mark Rutland965861d2017-02-02 17:32:15 +00001070#define read_sysreg_case(r) \
1071 case r: return read_sysreg_s(r)
1072
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001073/*
Dave Martin46823dd2017-03-23 15:14:39 +00001074 * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001075 * Read the system register on the current CPU
1076 */
Dave Martin46823dd2017-03-23 15:14:39 +00001077static u64 __read_sysreg_by_encoding(u32 sys_id)
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001078{
1079 switch (sys_id) {
Mark Rutland965861d2017-02-02 17:32:15 +00001080 read_sysreg_case(SYS_ID_PFR0_EL1);
1081 read_sysreg_case(SYS_ID_PFR1_EL1);
Anshuman Khandual16824082020-05-19 15:10:41 +05301082 read_sysreg_case(SYS_ID_PFR2_EL1);
Mark Rutland965861d2017-02-02 17:32:15 +00001083 read_sysreg_case(SYS_ID_DFR0_EL1);
Anshuman Khandualdd35ec02020-05-19 15:10:42 +05301084 read_sysreg_case(SYS_ID_DFR1_EL1);
Mark Rutland965861d2017-02-02 17:32:15 +00001085 read_sysreg_case(SYS_ID_MMFR0_EL1);
1086 read_sysreg_case(SYS_ID_MMFR1_EL1);
1087 read_sysreg_case(SYS_ID_MMFR2_EL1);
1088 read_sysreg_case(SYS_ID_MMFR3_EL1);
Anshuman Khandual858b8a82020-05-19 15:10:54 +05301089 read_sysreg_case(SYS_ID_MMFR4_EL1);
Anshuman Khandual152accf82020-05-19 15:10:43 +05301090 read_sysreg_case(SYS_ID_MMFR5_EL1);
Mark Rutland965861d2017-02-02 17:32:15 +00001091 read_sysreg_case(SYS_ID_ISAR0_EL1);
1092 read_sysreg_case(SYS_ID_ISAR1_EL1);
1093 read_sysreg_case(SYS_ID_ISAR2_EL1);
1094 read_sysreg_case(SYS_ID_ISAR3_EL1);
1095 read_sysreg_case(SYS_ID_ISAR4_EL1);
1096 read_sysreg_case(SYS_ID_ISAR5_EL1);
Anshuman Khandual8e3747b2019-12-17 20:17:32 +05301097 read_sysreg_case(SYS_ID_ISAR6_EL1);
Mark Rutland965861d2017-02-02 17:32:15 +00001098 read_sysreg_case(SYS_MVFR0_EL1);
1099 read_sysreg_case(SYS_MVFR1_EL1);
1100 read_sysreg_case(SYS_MVFR2_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001101
Mark Rutland965861d2017-02-02 17:32:15 +00001102 read_sysreg_case(SYS_ID_AA64PFR0_EL1);
1103 read_sysreg_case(SYS_ID_AA64PFR1_EL1);
Dave Martin78ed70b2019-06-03 16:35:02 +01001104 read_sysreg_case(SYS_ID_AA64ZFR0_EL1);
Mark Rutland965861d2017-02-02 17:32:15 +00001105 read_sysreg_case(SYS_ID_AA64DFR0_EL1);
1106 read_sysreg_case(SYS_ID_AA64DFR1_EL1);
1107 read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
1108 read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
1109 read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
1110 read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
1111 read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001112
Mark Rutland965861d2017-02-02 17:32:15 +00001113 read_sysreg_case(SYS_CNTFRQ_EL0);
1114 read_sysreg_case(SYS_CTR_EL0);
1115 read_sysreg_case(SYS_DCZID_EL0);
1116
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001117 default:
1118 BUG();
1119 return 0;
1120 }
1121}
1122
Marc Zyngier963fcd42015-09-30 11:50:04 +01001123#include <linux/irqchip/arm-gic-v3.h>
1124
Marc Zyngier94a9e042015-06-12 12:06:36 +01001125static bool
James Morse18ffa042015-07-21 13:23:29 +01001126feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
1127{
Suzuki K Poulose28c5dcb2016-01-26 10:58:16 +00001128 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
James Morse18ffa042015-07-21 13:23:29 +01001129
1130 return val >= entry->min_field_value;
1131}
1132
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001133static bool
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001134has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001135{
1136 u64 val;
Marc Zyngier94a9e042015-06-12 12:06:36 +01001137
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001138 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
1139 if (scope == SCOPE_SYSTEM)
Dave Martin46823dd2017-03-23 15:14:39 +00001140 val = read_sanitised_ftr_reg(entry->sys_reg);
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001141 else
Dave Martin46823dd2017-03-23 15:14:39 +00001142 val = __read_sysreg_by_encoding(entry->sys_reg);
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001143
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001144 return feature_matches(val, entry);
1145}
James Morse338d4f42015-07-22 19:05:54 +01001146
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001147static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
Marc Zyngier963fcd42015-09-30 11:50:04 +01001148{
1149 bool has_sre;
1150
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001151 if (!has_cpuid_feature(entry, scope))
Marc Zyngier963fcd42015-09-30 11:50:04 +01001152 return false;
1153
1154 has_sre = gic_enable_sre();
1155 if (!has_sre)
1156 pr_warn_once("%s present but disabled by higher exception level\n",
1157 entry->desc);
1158
1159 return has_sre;
1160}
1161
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001162static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
Will Deacond5370f72016-02-02 12:46:24 +00001163{
1164 u32 midr = read_cpuid_id();
Will Deacond5370f72016-02-02 12:46:24 +00001165
1166 /* Cavium ThunderX pass 1.x and 2.x */
Qian Caib99286b2019-08-05 23:05:03 -04001167 return midr_is_cpu_model_range(midr, MIDR_THUNDERX,
Robert Richterfa5ce3d2017-01-13 14:12:09 +01001168 MIDR_CPU_VAR_REV(0, 0),
1169 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
Will Deacond5370f72016-02-02 12:46:24 +00001170}
1171
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001172static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
1173{
Dave Martin46823dd2017-03-23 15:14:39 +00001174 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001175
1176 return cpuid_feature_extract_signed_field(pfr0,
1177 ID_AA64PFR0_FP_SHIFT) < 0;
1178}
1179
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001180static bool has_cache_idc(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001181 int scope)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001182{
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001183 u64 ctr;
1184
1185 if (scope == SCOPE_SYSTEM)
1186 ctr = arm64_ftr_reg_ctrel0.sys_val;
1187 else
Suzuki K Poulose1602df02018-10-09 14:47:06 +01001188 ctr = read_cpuid_effective_cachetype();
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001189
1190 return ctr & BIT(CTR_IDC_SHIFT);
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001191}
1192
Suzuki K Poulose1602df02018-10-09 14:47:06 +01001193static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused)
1194{
1195 /*
1196 * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively
1197 * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses
1198 * to the CTR_EL0 on this CPU and emulate it with the real/safe
1199 * value.
1200 */
1201 if (!(read_cpuid_cachetype() & BIT(CTR_IDC_SHIFT)))
1202 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
1203}
1204
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001205static bool has_cache_dic(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001206 int scope)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001207{
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001208 u64 ctr;
1209
1210 if (scope == SCOPE_SYSTEM)
1211 ctr = arm64_ftr_reg_ctrel0.sys_val;
1212 else
1213 ctr = read_cpuid_cachetype();
1214
1215 return ctr & BIT(CTR_DIC_SHIFT);
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001216}
1217
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01001218static bool __maybe_unused
1219has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
1220{
1221 /*
1222 * Kdump isn't guaranteed to power-off all secondary CPUs, CNP
1223 * may share TLB entries with a CPU stuck in the crashed
1224 * kernel.
1225 */
1226 if (is_kdump_kernel())
1227 return false;
1228
1229 return has_cpuid_feature(entry, scope);
1230}
1231
Mark Brown09e3c222019-12-09 18:12:17 +00001232/*
1233 * This check is triggered during the early boot before the cpufeature
1234 * is initialised. Checking the status on the local CPU allows the boot
1235 * CPU to detect the need for non-global mappings and thus avoiding a
1236 * pagetable re-write after all the CPUs are booted. This check will be
1237 * anyway run on individual CPUs, allowing us to get the consistent
1238 * state once the SMP CPUs are up and thus make the switch to non-global
1239 * mappings if required.
1240 */
1241bool kaslr_requires_kpti(void)
1242{
Mark Brown09e3c222019-12-09 18:12:17 +00001243 if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
1244 return false;
1245
1246 /*
1247 * E0PD does a similar job to KPTI so can be used instead
1248 * where available.
1249 */
1250 if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
Will Deacona569f5f2020-01-15 14:06:37 +00001251 u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
1252 if (cpuid_feature_extract_unsigned_field(mmfr2,
1253 ID_AA64MMFR2_E0PD_SHIFT))
Mark Brown09e3c222019-12-09 18:12:17 +00001254 return false;
1255 }
1256
1257 /*
1258 * Systems affected by Cavium erratum 24756 are incompatible
1259 * with KPTI.
1260 */
Will Deaconebac96e2020-01-15 13:59:58 +00001261 if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
Mark Brown09e3c222019-12-09 18:12:17 +00001262 extern const struct midr_range cavium_erratum_27456_cpus[];
1263
Will Deaconebac96e2020-01-15 13:59:58 +00001264 if (is_midr_in_range_list(read_cpuid_id(),
1265 cavium_erratum_27456_cpus))
1266 return false;
Mark Brown09e3c222019-12-09 18:12:17 +00001267 }
Mark Brown09e3c222019-12-09 18:12:17 +00001268
1269 return kaslr_offset() > 0;
1270}
1271
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001272static bool __meltdown_safe = true;
Will Deaconea1e3de2017-11-14 14:38:19 +00001273static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
1274
1275static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulosed3aec8a2018-03-26 15:12:40 +01001276 int scope)
Will Deaconea1e3de2017-11-14 14:38:19 +00001277{
Suzuki K Poulosebe5b2992018-03-26 15:12:45 +01001278 /* List of CPUs that are not vulnerable and don't need KPTI */
1279 static const struct midr_range kpti_safe_list[] = {
1280 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
1281 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
Florian Fainelli31d868c2020-01-06 14:54:12 -08001282 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
Will Deacon2a355ec2018-12-13 13:47:38 +00001283 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
1284 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
1285 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
1286 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
1287 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
1288 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
Hanjun Guo0ecc4712019-03-05 21:40:58 +08001289 MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
Rich Wiley918e1942019-11-05 10:45:10 -08001290 MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL),
Mark Rutland71c751f2018-04-23 11:41:33 +01001291 { /* sentinel */ }
Suzuki K Poulosebe5b2992018-03-26 15:12:45 +01001292 };
Josh Poimboeufa111b7c2019-04-12 15:39:32 -05001293 char const *str = "kpti command line option";
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001294 bool meltdown_safe;
1295
1296 meltdown_safe = is_midr_in_range_list(read_cpuid_id(), kpti_safe_list);
1297
1298 /* Defer to CPU feature registers */
1299 if (has_cpuid_feature(entry, scope))
1300 meltdown_safe = true;
1301
1302 if (!meltdown_safe)
1303 __meltdown_safe = false;
Will Deacon179a56f2017-11-27 18:29:30 +00001304
Marc Zyngier6dc52b12018-01-29 11:59:56 +00001305 /*
1306 * For reasons that aren't entirely clear, enabling KPTI on Cavium
1307 * ThunderX leads to apparent I-cache corruption of kernel text, which
1308 * ends as well as you might imagine. Don't even try.
1309 */
1310 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
1311 str = "ARM64_WORKAROUND_CAVIUM_27456";
1312 __kpti_forced = -1;
1313 }
1314
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001315 /* Useful for KASLR robustness */
Mark Brownc2d92352019-12-09 18:12:15 +00001316 if (kaslr_requires_kpti()) {
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001317 if (!__kpti_forced) {
1318 str = "KASLR";
1319 __kpti_forced = 1;
1320 }
1321 }
1322
Josh Poimboeufa111b7c2019-04-12 15:39:32 -05001323 if (cpu_mitigations_off() && !__kpti_forced) {
1324 str = "mitigations=off";
1325 __kpti_forced = -1;
1326 }
1327
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001328 if (!IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) {
1329 pr_info_once("kernel page table isolation disabled by kernel configuration\n");
1330 return false;
1331 }
1332
Marc Zyngier6dc52b12018-01-29 11:59:56 +00001333 /* Forced? */
Will Deaconea1e3de2017-11-14 14:38:19 +00001334 if (__kpti_forced) {
Marc Zyngier6dc52b12018-01-29 11:59:56 +00001335 pr_info_once("kernel page table isolation forced %s by %s\n",
1336 __kpti_forced > 0 ? "ON" : "OFF", str);
Will Deaconea1e3de2017-11-14 14:38:19 +00001337 return __kpti_forced > 0;
1338 }
1339
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001340 return !meltdown_safe;
Will Deaconea1e3de2017-11-14 14:38:19 +00001341}
1342
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001343#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
Dave Martinc0cda3b2018-03-26 15:12:28 +01001344static void
1345kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
Will Deaconf992b4d2018-02-06 22:22:50 +00001346{
1347 typedef void (kpti_remap_fn)(int, int, phys_addr_t);
1348 extern kpti_remap_fn idmap_kpti_install_ng_mappings;
1349 kpti_remap_fn *remap_fn;
1350
Will Deaconf992b4d2018-02-06 22:22:50 +00001351 int cpu = smp_processor_id();
1352
Will Deaconb89d82e2019-01-08 16:19:01 +00001353 /*
1354 * We don't need to rewrite the page-tables if either we've done
1355 * it already or we have KASLR enabled and therefore have not
1356 * created any global mappings at all.
1357 */
Mark Brown09e3c222019-12-09 18:12:17 +00001358 if (arm64_use_ng_mappings)
Dave Martinc0cda3b2018-03-26 15:12:28 +01001359 return;
Will Deaconf992b4d2018-02-06 22:22:50 +00001360
1361 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
1362
1363 cpu_install_idmap();
1364 remap_fn(cpu, num_online_cpus(), __pa_symbol(swapper_pg_dir));
1365 cpu_uninstall_idmap();
1366
1367 if (!cpu)
Mark Brown09e3c222019-12-09 18:12:17 +00001368 arm64_use_ng_mappings = true;
Will Deaconf992b4d2018-02-06 22:22:50 +00001369
Dave Martinc0cda3b2018-03-26 15:12:28 +01001370 return;
Will Deaconf992b4d2018-02-06 22:22:50 +00001371}
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001372#else
1373static void
1374kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
1375{
1376}
1377#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
Will Deaconf992b4d2018-02-06 22:22:50 +00001378
Will Deaconea1e3de2017-11-14 14:38:19 +00001379static int __init parse_kpti(char *str)
1380{
1381 bool enabled;
1382 int ret = strtobool(str, &enabled);
1383
1384 if (ret)
1385 return ret;
1386
1387 __kpti_forced = enabled ? 1 : -1;
1388 return 0;
1389}
Will Deaconb5b7dd62018-06-22 10:25:25 +01001390early_param("kpti", parse_kpti);
Will Deaconea1e3de2017-11-14 14:38:19 +00001391
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001392#ifdef CONFIG_ARM64_HW_AFDBM
1393static inline void __cpu_enable_hw_dbm(void)
1394{
1395 u64 tcr = read_sysreg(tcr_el1) | TCR_HD;
1396
1397 write_sysreg(tcr, tcr_el1);
1398 isb();
1399}
1400
Suzuki K Pouloseece13972018-03-26 15:12:49 +01001401static bool cpu_has_broken_dbm(void)
1402{
1403 /* List of CPUs which have broken DBM support. */
1404 static const struct midr_range cpus[] = {
1405#ifdef CONFIG_ARM64_ERRATUM_1024718
1406 MIDR_RANGE(MIDR_CORTEX_A55, 0, 0, 1, 0), // A55 r0p0 -r1p0
1407#endif
1408 {},
1409 };
1410
1411 return is_midr_in_range_list(read_cpuid_id(), cpus);
1412}
1413
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001414static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap)
1415{
Suzuki K Pouloseece13972018-03-26 15:12:49 +01001416 return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) &&
1417 !cpu_has_broken_dbm();
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001418}
1419
1420static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap)
1421{
1422 if (cpu_can_use_dbm(cap))
1423 __cpu_enable_hw_dbm();
1424}
1425
1426static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap,
1427 int __unused)
1428{
1429 static bool detected = false;
1430 /*
1431 * DBM is a non-conflicting feature. i.e, the kernel can safely
1432 * run a mix of CPUs with and without the feature. So, we
1433 * unconditionally enable the capability to allow any late CPU
1434 * to use the feature. We only enable the control bits on the
1435 * CPU, if it actually supports.
1436 *
1437 * We have to make sure we print the "feature" detection only
1438 * when at least one CPU actually uses it. So check if this CPU
1439 * can actually use it and print the message exactly once.
1440 *
1441 * This is safe as all CPUs (including secondary CPUs - due to the
1442 * LOCAL_CPU scope - and the hotplugged CPUs - via verification)
1443 * goes through the "matches" check exactly once. Also if a CPU
1444 * matches the criteria, it is guaranteed that the CPU will turn
1445 * the DBM on, as the capability is unconditionally enabled.
1446 */
1447 if (!detected && cpu_can_use_dbm(cap)) {
1448 detected = true;
1449 pr_info("detected: Hardware dirty bit management\n");
1450 }
1451
1452 return true;
1453}
1454
1455#endif
1456
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001457#ifdef CONFIG_ARM64_AMU_EXTN
1458
1459/*
1460 * The "amu_cpus" cpumask only signals that the CPU implementation for the
1461 * flagged CPUs supports the Activity Monitors Unit (AMU) but does not provide
1462 * information regarding all the events that it supports. When a CPU bit is
1463 * set in the cpumask, the user of this feature can only rely on the presence
1464 * of the 4 fixed counters for that CPU. But this does not guarantee that the
1465 * counters are enabled or access to these counters is enabled by code
1466 * executed at higher exception levels (firmware).
1467 */
1468static struct cpumask amu_cpus __read_mostly;
1469
1470bool cpu_has_amu_feat(int cpu)
1471{
1472 return cpumask_test_cpu(cpu, &amu_cpus);
1473}
1474
Ionela Voinescucd0ed032020-03-05 09:06:26 +00001475/* Initialize the use of AMU counters for frequency invariance */
1476extern void init_cpu_freq_invariance_counters(void);
1477
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001478static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap)
1479{
1480 if (has_cpuid_feature(cap, SCOPE_LOCAL_CPU)) {
1481 pr_info("detected CPU%d: Activity Monitors Unit (AMU)\n",
1482 smp_processor_id());
1483 cpumask_set_cpu(smp_processor_id(), &amu_cpus);
Ionela Voinescucd0ed032020-03-05 09:06:26 +00001484 init_cpu_freq_invariance_counters();
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001485 }
1486}
1487
1488static bool has_amu(const struct arm64_cpu_capabilities *cap,
1489 int __unused)
1490{
1491 /*
1492 * The AMU extension is a non-conflicting feature: the kernel can
1493 * safely run a mix of CPUs with and without support for the
1494 * activity monitors extension. Therefore, unconditionally enable
1495 * the capability to allow any late CPU to use the feature.
1496 *
1497 * With this feature unconditionally enabled, the cpu_enable
1498 * function will be called for all CPUs that match the criteria,
1499 * including secondary and hotplugged, marking this feature as
1500 * present on that respective CPU. The enable function will also
1501 * print a detection message.
1502 */
1503
1504 return true;
1505}
1506#endif
1507
Will Deacon12eb3692018-03-27 11:51:12 +01001508#ifdef CONFIG_ARM64_VHE
1509static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
1510{
1511 return is_kernel_in_hyp_mode();
1512}
1513
Dave Martinc0cda3b2018-03-26 15:12:28 +01001514static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
James Morse6d99b682018-01-08 15:38:06 +00001515{
1516 /*
1517 * Copy register values that aren't redirected by hardware.
1518 *
1519 * Before code patching, we only set tpidr_el1, all CPUs need to copy
1520 * this value to tpidr_el2 before we patch the code. Once we've done
1521 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
1522 * do anything here.
1523 */
Julien Thierrye9ab7a22019-01-31 14:58:52 +00001524 if (!alternative_is_applied(ARM64_HAS_VIRT_HOST_EXTN))
James Morse6d99b682018-01-08 15:38:06 +00001525 write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
James Morse6d99b682018-01-08 15:38:06 +00001526}
Will Deacon12eb3692018-03-27 11:51:12 +01001527#endif
James Morse6d99b682018-01-08 15:38:06 +00001528
Marc Zyngiere48d53a2018-04-06 12:27:28 +01001529static void cpu_has_fwb(const struct arm64_cpu_capabilities *__unused)
1530{
1531 u64 val = read_sysreg_s(SYS_CLIDR_EL1);
1532
1533 /* Check that CLIDR_EL1.LOU{U,IS} are both 0 */
1534 WARN_ON(val & (7 << 27 | 7 << 21));
1535}
1536
Will Deacon8f04e8e2018-08-07 13:47:06 +01001537#ifdef CONFIG_ARM64_SSBD
1538static int ssbs_emulation_handler(struct pt_regs *regs, u32 instr)
1539{
1540 if (user_mode(regs))
1541 return 1;
1542
Suzuki K Poulose74e24822018-09-16 23:17:23 +01001543 if (instr & BIT(PSTATE_Imm_shift))
Will Deacon8f04e8e2018-08-07 13:47:06 +01001544 regs->pstate |= PSR_SSBS_BIT;
1545 else
1546 regs->pstate &= ~PSR_SSBS_BIT;
1547
1548 arm64_skip_faulting_instruction(regs, 4);
1549 return 0;
1550}
1551
1552static struct undef_hook ssbs_emulation_hook = {
Suzuki K Poulose74e24822018-09-16 23:17:23 +01001553 .instr_mask = ~(1U << PSTATE_Imm_shift),
1554 .instr_val = 0xd500401f | PSTATE_SSBS,
Will Deacon8f04e8e2018-08-07 13:47:06 +01001555 .fn = ssbs_emulation_handler,
1556};
1557
1558static void cpu_enable_ssbs(const struct arm64_cpu_capabilities *__unused)
1559{
1560 static bool undef_hook_registered = false;
Julien Grall27e6e7d2019-05-30 12:30:58 +01001561 static DEFINE_RAW_SPINLOCK(hook_lock);
Will Deacon8f04e8e2018-08-07 13:47:06 +01001562
Julien Grall27e6e7d2019-05-30 12:30:58 +01001563 raw_spin_lock(&hook_lock);
Will Deacon8f04e8e2018-08-07 13:47:06 +01001564 if (!undef_hook_registered) {
1565 register_undef_hook(&ssbs_emulation_hook);
1566 undef_hook_registered = true;
1567 }
Julien Grall27e6e7d2019-05-30 12:30:58 +01001568 raw_spin_unlock(&hook_lock);
Will Deacon8f04e8e2018-08-07 13:47:06 +01001569
1570 if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) {
1571 sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
1572 arm64_set_ssbd_mitigation(false);
1573 } else {
1574 arm64_set_ssbd_mitigation(true);
1575 }
1576}
1577#endif /* CONFIG_ARM64_SSBD */
1578
Will Deaconb8925ee2018-08-07 13:53:41 +01001579#ifdef CONFIG_ARM64_PAN
1580static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
1581{
1582 /*
1583 * We modify PSTATE. This won't work from irq context as the PSTATE
1584 * is discarded once we return from the exception.
1585 */
1586 WARN_ON_ONCE(in_interrupt());
1587
1588 sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
1589 asm(SET_PSTATE_PAN(1));
1590}
1591#endif /* CONFIG_ARM64_PAN */
1592
1593#ifdef CONFIG_ARM64_RAS_EXTN
1594static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
1595{
1596 /* Firmware may have left a deferred SError in this register. */
1597 write_sysreg_s(0, SYS_DISR_EL1);
1598}
1599#endif /* CONFIG_ARM64_RAS_EXTN */
1600
Mark Rutland6984eb42018-12-07 18:39:24 +00001601#ifdef CONFIG_ARM64_PTR_AUTH
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301602static bool has_address_auth(const struct arm64_cpu_capabilities *entry,
1603 int __unused)
Mark Rutland75031972018-12-07 18:39:25 +00001604{
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301605 return __system_matches_cap(ARM64_HAS_ADDRESS_AUTH_ARCH) ||
1606 __system_matches_cap(ARM64_HAS_ADDRESS_AUTH_IMP_DEF);
1607}
1608
1609static bool has_generic_auth(const struct arm64_cpu_capabilities *entry,
1610 int __unused)
1611{
1612 return __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH) ||
1613 __system_matches_cap(ARM64_HAS_GENERIC_AUTH_IMP_DEF);
Mark Rutland75031972018-12-07 18:39:25 +00001614}
Mark Rutland6984eb42018-12-07 18:39:24 +00001615#endif /* CONFIG_ARM64_PTR_AUTH */
1616
Mark Brown3e6c69a2019-12-09 18:12:14 +00001617#ifdef CONFIG_ARM64_E0PD
1618static void cpu_enable_e0pd(struct arm64_cpu_capabilities const *cap)
1619{
1620 if (this_cpu_has_cap(ARM64_HAS_E0PD))
1621 sysreg_clear_set(tcr_el1, 0, TCR_E0PD1);
1622}
1623#endif /* CONFIG_ARM64_E0PD */
1624
Julien Thierryb90d2b22019-01-31 14:58:42 +00001625#ifdef CONFIG_ARM64_PSEUDO_NMI
Julien Thierrybc3c03c2019-01-31 14:59:03 +00001626static bool enable_pseudo_nmi;
1627
1628static int __init early_enable_pseudo_nmi(char *p)
1629{
1630 return strtobool(p, &enable_pseudo_nmi);
1631}
1632early_param("irqchip.gicv3_pseudo_nmi", early_enable_pseudo_nmi);
1633
Julien Thierryb90d2b22019-01-31 14:58:42 +00001634static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry,
1635 int scope)
1636{
Julien Thierrybc3c03c2019-01-31 14:59:03 +00001637 return enable_pseudo_nmi && has_useable_gicv3_cpuif(entry, scope);
Julien Thierryb90d2b22019-01-31 14:58:42 +00001638}
1639#endif
1640
Amit Daniel Kachhap8c176e12020-03-13 14:34:53 +05301641/* Internal helper functions to match cpu capability type */
1642static bool
1643cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap)
1644{
1645 return !!(cap->type & ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU);
1646}
1647
1648static bool
1649cpucap_late_cpu_permitted(const struct arm64_cpu_capabilities *cap)
1650{
1651 return !!(cap->type & ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU);
1652}
1653
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05301654static bool
1655cpucap_panic_on_conflict(const struct arm64_cpu_capabilities *cap)
1656{
1657 return !!(cap->type & ARM64_CPUCAP_PANIC_ON_CONFLICT);
1658}
1659
Marc Zyngier359b7062015-03-27 13:09:23 +00001660static const struct arm64_cpu_capabilities arm64_features[] = {
Marc Zyngier94a9e042015-06-12 12:06:36 +01001661 {
1662 .desc = "GIC system register CPU interface",
1663 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
Julien Thierryc9bfdf72019-01-31 14:58:41 +00001664 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
Marc Zyngier963fcd42015-09-30 11:50:04 +01001665 .matches = has_useable_gicv3_cpuif,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001666 .sys_reg = SYS_ID_AA64PFR0_EL1,
1667 .field_pos = ID_AA64PFR0_GIC_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001668 .sign = FTR_UNSIGNED,
James Morse18ffa042015-07-21 13:23:29 +01001669 .min_field_value = 1,
Marc Zyngier94a9e042015-06-12 12:06:36 +01001670 },
James Morse338d4f42015-07-22 19:05:54 +01001671#ifdef CONFIG_ARM64_PAN
1672 {
1673 .desc = "Privileged Access Never",
1674 .capability = ARM64_HAS_PAN,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001675 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001676 .matches = has_cpuid_feature,
1677 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1678 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001679 .sign = FTR_UNSIGNED,
James Morse338d4f42015-07-22 19:05:54 +01001680 .min_field_value = 1,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001681 .cpu_enable = cpu_enable_pan,
James Morse338d4f42015-07-22 19:05:54 +01001682 },
1683#endif /* CONFIG_ARM64_PAN */
Catalin Marinas395af862020-01-15 11:30:08 +00001684#ifdef CONFIG_ARM64_LSE_ATOMICS
Will Deacon2e94da12015-07-27 16:23:58 +01001685 {
1686 .desc = "LSE atomic instructions",
1687 .capability = ARM64_HAS_LSE_ATOMICS,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001688 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001689 .matches = has_cpuid_feature,
1690 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1691 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001692 .sign = FTR_UNSIGNED,
Will Deacon2e94da12015-07-27 16:23:58 +01001693 .min_field_value = 2,
1694 },
Catalin Marinas395af862020-01-15 11:30:08 +00001695#endif /* CONFIG_ARM64_LSE_ATOMICS */
Marc Zyngierd88701b2015-01-29 11:24:05 +00001696 {
Will Deacond5370f72016-02-02 12:46:24 +00001697 .desc = "Software prefetching using PRFM",
1698 .capability = ARM64_HAS_NO_HW_PREFETCH,
Suzuki K Poulose5c137712018-03-26 15:12:39 +01001699 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
Will Deacond5370f72016-02-02 12:46:24 +00001700 .matches = has_no_hw_prefetch,
1701 },
James Morse57f49592016-02-05 14:58:48 +00001702#ifdef CONFIG_ARM64_UAO
1703 {
1704 .desc = "User Access Override",
1705 .capability = ARM64_HAS_UAO,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001706 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse57f49592016-02-05 14:58:48 +00001707 .matches = has_cpuid_feature,
1708 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1709 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
1710 .min_field_value = 1,
James Morsec8b06e32017-01-09 18:14:02 +00001711 /*
1712 * We rely on stop_machine() calling uao_thread_switch() to set
1713 * UAO immediately after patching.
1714 */
James Morse57f49592016-02-05 14:58:48 +00001715 },
1716#endif /* CONFIG_ARM64_UAO */
James Morse70544192016-02-05 14:58:50 +00001717#ifdef CONFIG_ARM64_PAN
1718 {
1719 .capability = ARM64_ALT_PAN_NOT_UAO,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001720 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse70544192016-02-05 14:58:50 +00001721 .matches = cpufeature_pan_not_uao,
1722 },
1723#endif /* CONFIG_ARM64_PAN */
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001724#ifdef CONFIG_ARM64_VHE
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001725 {
Marc Zyngierd88701b2015-01-29 11:24:05 +00001726 .desc = "Virtualization Host Extensions",
1727 .capability = ARM64_HAS_VIRT_HOST_EXTN,
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001728 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
Marc Zyngierd88701b2015-01-29 11:24:05 +00001729 .matches = runs_at_el2,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001730 .cpu_enable = cpu_copy_el2regs,
Marc Zyngierd88701b2015-01-29 11:24:05 +00001731 },
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001732#endif /* CONFIG_ARM64_VHE */
Suzuki K Poulose042446a2016-04-18 10:28:36 +01001733 {
1734 .desc = "32-bit EL0 Support",
1735 .capability = ARM64_HAS_32BIT_EL0,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001736 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K Poulose042446a2016-04-18 10:28:36 +01001737 .matches = has_cpuid_feature,
1738 .sys_reg = SYS_ID_AA64PFR0_EL1,
1739 .sign = FTR_UNSIGNED,
1740 .field_pos = ID_AA64PFR0_EL0_SHIFT,
1741 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
1742 },
Will Deacon540f76d2020-04-21 15:29:17 +01001743#ifdef CONFIG_KVM
1744 {
1745 .desc = "32-bit EL1 Support",
1746 .capability = ARM64_HAS_32BIT_EL1,
1747 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1748 .matches = has_cpuid_feature,
1749 .sys_reg = SYS_ID_AA64PFR0_EL1,
1750 .sign = FTR_UNSIGNED,
1751 .field_pos = ID_AA64PFR0_EL1_SHIFT,
1752 .min_field_value = ID_AA64PFR0_EL1_32BIT_64BIT,
1753 },
1754#endif
Will Deaconea1e3de2017-11-14 14:38:19 +00001755 {
Will Deacon179a56f2017-11-27 18:29:30 +00001756 .desc = "Kernel page table isolation (KPTI)",
Will Deaconea1e3de2017-11-14 14:38:19 +00001757 .capability = ARM64_UNMAP_KERNEL_AT_EL0,
Suzuki K Poulosed3aec8a2018-03-26 15:12:40 +01001758 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
1759 /*
1760 * The ID feature fields below are used to indicate that
1761 * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for
1762 * more details.
1763 */
1764 .sys_reg = SYS_ID_AA64PFR0_EL1,
1765 .field_pos = ID_AA64PFR0_CSV3_SHIFT,
1766 .min_field_value = 1,
Will Deaconea1e3de2017-11-14 14:38:19 +00001767 .matches = unmap_kernel_at_el0,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001768 .cpu_enable = kpti_install_ng_mappings,
Will Deaconea1e3de2017-11-14 14:38:19 +00001769 },
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001770 {
1771 /* FP/SIMD is not implemented */
1772 .capability = ARM64_HAS_NO_FPSIMD,
Suzuki K Poulose449443c2020-01-13 23:30:19 +00001773 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001774 .min_field_value = 0,
1775 .matches = has_no_fpsimd,
1776 },
Robin Murphyd50e0712017-07-25 11:55:42 +01001777#ifdef CONFIG_ARM64_PMEM
1778 {
1779 .desc = "Data cache clean to Point of Persistence",
1780 .capability = ARM64_HAS_DCPOP,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001781 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Robin Murphyd50e0712017-07-25 11:55:42 +01001782 .matches = has_cpuid_feature,
1783 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1784 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1785 .min_field_value = 1,
1786 },
Andrew Murrayb9585f52019-04-09 10:52:45 +01001787 {
1788 .desc = "Data cache clean to Point of Deep Persistence",
1789 .capability = ARM64_HAS_DCPODP,
1790 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1791 .matches = has_cpuid_feature,
1792 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1793 .sign = FTR_UNSIGNED,
1794 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1795 .min_field_value = 2,
1796 },
Robin Murphyd50e0712017-07-25 11:55:42 +01001797#endif
Dave Martin43994d82017-10-31 15:51:19 +00001798#ifdef CONFIG_ARM64_SVE
1799 {
1800 .desc = "Scalable Vector Extension",
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001801 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Dave Martin43994d82017-10-31 15:51:19 +00001802 .capability = ARM64_SVE,
Dave Martin43994d82017-10-31 15:51:19 +00001803 .sys_reg = SYS_ID_AA64PFR0_EL1,
1804 .sign = FTR_UNSIGNED,
1805 .field_pos = ID_AA64PFR0_SVE_SHIFT,
1806 .min_field_value = ID_AA64PFR0_SVE,
1807 .matches = has_cpuid_feature,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001808 .cpu_enable = sve_kernel_enable,
Dave Martin43994d82017-10-31 15:51:19 +00001809 },
1810#endif /* CONFIG_ARM64_SVE */
Xie XiuQi64c02722018-01-15 19:38:56 +00001811#ifdef CONFIG_ARM64_RAS_EXTN
1812 {
1813 .desc = "RAS Extension Support",
1814 .capability = ARM64_HAS_RAS_EXTN,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001815 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Xie XiuQi64c02722018-01-15 19:38:56 +00001816 .matches = has_cpuid_feature,
1817 .sys_reg = SYS_ID_AA64PFR0_EL1,
1818 .sign = FTR_UNSIGNED,
1819 .field_pos = ID_AA64PFR0_RAS_SHIFT,
1820 .min_field_value = ID_AA64PFR0_RAS_V1,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001821 .cpu_enable = cpu_clear_disr,
Xie XiuQi64c02722018-01-15 19:38:56 +00001822 },
1823#endif /* CONFIG_ARM64_RAS_EXTN */
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001824#ifdef CONFIG_ARM64_AMU_EXTN
1825 {
1826 /*
1827 * The feature is enabled by default if CONFIG_ARM64_AMU_EXTN=y.
1828 * Therefore, don't provide .desc as we don't want the detection
1829 * message to be shown until at least one CPU is detected to
1830 * support the feature.
1831 */
1832 .capability = ARM64_HAS_AMU_EXTN,
1833 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1834 .matches = has_amu,
1835 .sys_reg = SYS_ID_AA64PFR0_EL1,
1836 .sign = FTR_UNSIGNED,
1837 .field_pos = ID_AA64PFR0_AMU_SHIFT,
1838 .min_field_value = ID_AA64PFR0_AMU,
1839 .cpu_enable = cpu_amu_enable,
1840 },
1841#endif /* CONFIG_ARM64_AMU_EXTN */
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001842 {
1843 .desc = "Data cache clean to the PoU not required for I/D coherence",
1844 .capability = ARM64_HAS_CACHE_IDC,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001845 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001846 .matches = has_cache_idc,
Suzuki K Poulose1602df02018-10-09 14:47:06 +01001847 .cpu_enable = cpu_emulate_effective_ctr,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001848 },
1849 {
1850 .desc = "Instruction cache invalidation not required for I/D coherence",
1851 .capability = ARM64_HAS_CACHE_DIC,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001852 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001853 .matches = has_cache_dic,
1854 },
Marc Zyngiere48d53a2018-04-06 12:27:28 +01001855 {
1856 .desc = "Stage-2 Force Write-Back",
1857 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1858 .capability = ARM64_HAS_STAGE2_FWB,
1859 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1860 .sign = FTR_UNSIGNED,
1861 .field_pos = ID_AA64MMFR2_FWB_SHIFT,
1862 .min_field_value = 1,
1863 .matches = has_cpuid_feature,
1864 .cpu_enable = cpu_has_fwb,
1865 },
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001866#ifdef CONFIG_ARM64_HW_AFDBM
1867 {
1868 /*
1869 * Since we turn this on always, we don't want the user to
1870 * think that the feature is available when it may not be.
1871 * So hide the description.
1872 *
1873 * .desc = "Hardware pagetable Dirty Bit Management",
1874 *
1875 */
1876 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1877 .capability = ARM64_HW_DBM,
1878 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1879 .sign = FTR_UNSIGNED,
1880 .field_pos = ID_AA64MMFR1_HADBS_SHIFT,
1881 .min_field_value = 2,
1882 .matches = has_hw_dbm,
1883 .cpu_enable = cpu_enable_hw_dbm,
1884 },
1885#endif
Ard Biesheuvel86d0dd32018-08-27 13:02:43 +02001886 {
1887 .desc = "CRC32 instructions",
1888 .capability = ARM64_HAS_CRC32,
1889 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1890 .matches = has_cpuid_feature,
1891 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1892 .field_pos = ID_AA64ISAR0_CRC32_SHIFT,
1893 .min_field_value = 1,
1894 },
Will Deacon4f9f4962018-11-21 15:07:00 +00001895#ifdef CONFIG_ARM64_SSBD
Will Deacond71be2b2018-06-15 11:37:34 +01001896 {
1897 .desc = "Speculative Store Bypassing Safe (SSBS)",
1898 .capability = ARM64_SSBS,
1899 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1900 .matches = has_cpuid_feature,
1901 .sys_reg = SYS_ID_AA64PFR1_EL1,
1902 .field_pos = ID_AA64PFR1_SSBS_SHIFT,
1903 .sign = FTR_UNSIGNED,
1904 .min_field_value = ID_AA64PFR1_SSBS_PSTATE_ONLY,
Will Deacon8f04e8e2018-08-07 13:47:06 +01001905 .cpu_enable = cpu_enable_ssbs,
Will Deacond71be2b2018-06-15 11:37:34 +01001906 },
Will Deacon8f04e8e2018-08-07 13:47:06 +01001907#endif
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01001908#ifdef CONFIG_ARM64_CNP
1909 {
1910 .desc = "Common not Private translations",
1911 .capability = ARM64_HAS_CNP,
1912 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1913 .matches = has_useable_cnp,
1914 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1915 .sign = FTR_UNSIGNED,
1916 .field_pos = ID_AA64MMFR2_CNP_SHIFT,
1917 .min_field_value = 1,
1918 .cpu_enable = cpu_enable_cnp,
1919 },
1920#endif
Will Deaconbd4fb6d2018-06-14 11:21:34 +01001921 {
1922 .desc = "Speculation barrier (SB)",
1923 .capability = ARM64_HAS_SB,
1924 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1925 .matches = has_cpuid_feature,
1926 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1927 .field_pos = ID_AA64ISAR1_SB_SHIFT,
1928 .sign = FTR_UNSIGNED,
1929 .min_field_value = 1,
1930 },
Mark Rutland6984eb42018-12-07 18:39:24 +00001931#ifdef CONFIG_ARM64_PTR_AUTH
1932 {
1933 .desc = "Address authentication (architected algorithm)",
1934 .capability = ARM64_HAS_ADDRESS_AUTH_ARCH,
Kristina Martsenko69829342020-03-13 14:34:55 +05301935 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
Mark Rutland6984eb42018-12-07 18:39:24 +00001936 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1937 .sign = FTR_UNSIGNED,
1938 .field_pos = ID_AA64ISAR1_APA_SHIFT,
1939 .min_field_value = ID_AA64ISAR1_APA_ARCHITECTED,
1940 .matches = has_cpuid_feature,
1941 },
1942 {
1943 .desc = "Address authentication (IMP DEF algorithm)",
1944 .capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF,
Kristina Martsenko69829342020-03-13 14:34:55 +05301945 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
Mark Rutland6984eb42018-12-07 18:39:24 +00001946 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1947 .sign = FTR_UNSIGNED,
1948 .field_pos = ID_AA64ISAR1_API_SHIFT,
1949 .min_field_value = ID_AA64ISAR1_API_IMP_DEF,
1950 .matches = has_cpuid_feature,
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301951 },
1952 {
1953 .capability = ARM64_HAS_ADDRESS_AUTH,
Kristina Martsenko69829342020-03-13 14:34:55 +05301954 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301955 .matches = has_address_auth,
Mark Rutland6984eb42018-12-07 18:39:24 +00001956 },
1957 {
1958 .desc = "Generic authentication (architected algorithm)",
1959 .capability = ARM64_HAS_GENERIC_AUTH_ARCH,
1960 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1961 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1962 .sign = FTR_UNSIGNED,
1963 .field_pos = ID_AA64ISAR1_GPA_SHIFT,
1964 .min_field_value = ID_AA64ISAR1_GPA_ARCHITECTED,
1965 .matches = has_cpuid_feature,
1966 },
1967 {
1968 .desc = "Generic authentication (IMP DEF algorithm)",
1969 .capability = ARM64_HAS_GENERIC_AUTH_IMP_DEF,
1970 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1971 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1972 .sign = FTR_UNSIGNED,
1973 .field_pos = ID_AA64ISAR1_GPI_SHIFT,
1974 .min_field_value = ID_AA64ISAR1_GPI_IMP_DEF,
1975 .matches = has_cpuid_feature,
1976 },
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301977 {
1978 .capability = ARM64_HAS_GENERIC_AUTH,
1979 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1980 .matches = has_generic_auth,
1981 },
Mark Rutland6984eb42018-12-07 18:39:24 +00001982#endif /* CONFIG_ARM64_PTR_AUTH */
Julien Thierryb90d2b22019-01-31 14:58:42 +00001983#ifdef CONFIG_ARM64_PSEUDO_NMI
1984 {
1985 /*
1986 * Depends on having GICv3
1987 */
1988 .desc = "IRQ priority masking",
1989 .capability = ARM64_HAS_IRQ_PRIO_MASKING,
1990 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
1991 .matches = can_use_gic_priorities,
1992 .sys_reg = SYS_ID_AA64PFR0_EL1,
1993 .field_pos = ID_AA64PFR0_GIC_SHIFT,
1994 .sign = FTR_UNSIGNED,
1995 .min_field_value = 1,
1996 },
1997#endif
Mark Brown3e6c69a2019-12-09 18:12:14 +00001998#ifdef CONFIG_ARM64_E0PD
1999 {
2000 .desc = "E0PD",
2001 .capability = ARM64_HAS_E0PD,
2002 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
2003 .sys_reg = SYS_ID_AA64MMFR2_EL1,
2004 .sign = FTR_UNSIGNED,
2005 .field_pos = ID_AA64MMFR2_E0PD_SHIFT,
2006 .matches = has_cpuid_feature,
2007 .min_field_value = 1,
2008 .cpu_enable = cpu_enable_e0pd,
2009 },
2010#endif
Richard Henderson1a50ec02020-01-21 12:58:52 +00002011#ifdef CONFIG_ARCH_RANDOM
2012 {
2013 .desc = "Random Number Generator",
2014 .capability = ARM64_HAS_RNG,
2015 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
2016 .matches = has_cpuid_feature,
2017 .sys_reg = SYS_ID_AA64ISAR0_EL1,
2018 .field_pos = ID_AA64ISAR0_RNDR_SHIFT,
2019 .sign = FTR_UNSIGNED,
2020 .min_field_value = 1,
2021 },
2022#endif
Marc Zyngier359b7062015-03-27 13:09:23 +00002023 {},
2024};
2025
Will Deacon1e013d02018-12-12 15:53:54 +00002026#define HWCAP_CPUID_MATCH(reg, field, s, min_value) \
2027 .matches = has_cpuid_feature, \
2028 .sys_reg = reg, \
2029 .field_pos = field, \
2030 .sign = s, \
2031 .min_field_value = min_value,
2032
2033#define __HWCAP_CAP(name, cap_type, cap) \
2034 .desc = name, \
2035 .type = ARM64_CPUCAP_SYSTEM_FEATURE, \
2036 .hwcap_type = cap_type, \
2037 .hwcap = cap, \
2038
2039#define HWCAP_CAP(reg, field, s, min_value, cap_type, cap) \
2040 { \
2041 __HWCAP_CAP(#cap, cap_type, cap) \
2042 HWCAP_CPUID_MATCH(reg, field, s, min_value) \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002043 }
2044
Will Deacon1e013d02018-12-12 15:53:54 +00002045#define HWCAP_MULTI_CAP(list, cap_type, cap) \
2046 { \
2047 __HWCAP_CAP(#cap, cap_type, cap) \
2048 .matches = cpucap_multi_entry_cap_matches, \
2049 .match_list = list, \
2050 }
2051
Suzuki K Poulose7559950a2020-01-13 23:30:20 +00002052#define HWCAP_CAP_MATCH(match, cap_type, cap) \
2053 { \
2054 __HWCAP_CAP(#cap, cap_type, cap) \
2055 .matches = match, \
2056 }
2057
Will Deacon1e013d02018-12-12 15:53:54 +00002058#ifdef CONFIG_ARM64_PTR_AUTH
2059static const struct arm64_cpu_capabilities ptr_auth_hwcap_addr_matches[] = {
2060 {
2061 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_APA_SHIFT,
2062 FTR_UNSIGNED, ID_AA64ISAR1_APA_ARCHITECTED)
2063 },
2064 {
2065 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_API_SHIFT,
2066 FTR_UNSIGNED, ID_AA64ISAR1_API_IMP_DEF)
2067 },
2068 {},
2069};
2070
2071static const struct arm64_cpu_capabilities ptr_auth_hwcap_gen_matches[] = {
2072 {
2073 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_GPA_SHIFT,
2074 FTR_UNSIGNED, ID_AA64ISAR1_GPA_ARCHITECTED)
2075 },
2076 {
2077 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_GPI_SHIFT,
2078 FTR_UNSIGNED, ID_AA64ISAR1_GPI_IMP_DEF)
2079 },
2080 {},
2081};
2082#endif
2083
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01002084static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
Andrew Murrayaaba0982019-04-09 10:52:40 +01002085 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_PMULL),
2086 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_AES),
2087 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA1),
2088 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA2),
2089 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_SHA512),
2090 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_CRC32),
2091 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ATOMICS),
2092 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RDM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDRDM),
2093 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA3),
2094 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM3),
2095 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM4),
2096 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_DP_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDDP),
2097 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_FHM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM),
2098 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FLAGM),
Mark Brown12019372019-06-18 19:10:54 +01002099 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_FLAGM2),
Richard Henderson1a50ec02020-01-21 12:58:52 +00002100 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RNDR_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RNG),
Andrew Murrayaaba0982019-04-09 10:52:40 +01002101 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_FP),
2102 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FPHP),
2103 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_ASIMD),
2104 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDHP),
2105 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_DIT_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DIT),
2106 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DPB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DCPOP),
Andrew Murray671db582019-04-09 10:52:43 +01002107 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DPB_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_DCPODP),
Andrew Murrayaaba0982019-04-09 10:52:40 +01002108 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_JSCVT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_JSCVT),
2109 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FCMA_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FCMA),
2110 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_LRCPC),
2111 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ILRCPC),
Mark Brownca9503f2019-06-18 19:10:55 +01002112 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FRINTTS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FRINT),
Andrew Murrayaaba0982019-04-09 10:52:40 +01002113 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_SB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SB),
Steven Priced4209d82019-12-16 11:33:37 +00002114 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_BF16_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_BF16),
2115 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DGH_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DGH),
2116 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_I8MM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_I8MM),
Andrew Murrayaaba0982019-04-09 10:52:40 +01002117 HWCAP_CAP(SYS_ID_AA64MMFR2_EL1, ID_AA64MMFR2_AT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_USCAT),
Dave Martin43994d82017-10-31 15:51:19 +00002118#ifdef CONFIG_ARM64_SVE
Andrew Murrayaaba0982019-04-09 10:52:40 +01002119 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_SVE_SHIFT, FTR_UNSIGNED, ID_AA64PFR0_SVE, CAP_HWCAP, KERNEL_HWCAP_SVE),
Dave Martin06a916f2019-04-18 18:41:38 +01002120 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SVEVER_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SVEVER_SVE2, CAP_HWCAP, KERNEL_HWCAP_SVE2),
2121 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_AES_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_AES, CAP_HWCAP, KERNEL_HWCAP_SVEAES),
2122 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_AES_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_AES_PMULL, CAP_HWCAP, KERNEL_HWCAP_SVEPMULL),
2123 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_BITPERM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_BITPERM, CAP_HWCAP, KERNEL_HWCAP_SVEBITPERM),
Steven Priced4209d82019-12-16 11:33:37 +00002124 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_BF16_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_BF16, CAP_HWCAP, KERNEL_HWCAP_SVEBF16),
Dave Martin06a916f2019-04-18 18:41:38 +01002125 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SHA3_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SHA3, CAP_HWCAP, KERNEL_HWCAP_SVESHA3),
2126 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SM4_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SM4, CAP_HWCAP, KERNEL_HWCAP_SVESM4),
Steven Priced4209d82019-12-16 11:33:37 +00002127 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_I8MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_I8MM, CAP_HWCAP, KERNEL_HWCAP_SVEI8MM),
2128 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_F32MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_F32MM, CAP_HWCAP, KERNEL_HWCAP_SVEF32MM),
2129 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_F64MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_F64MM, CAP_HWCAP, KERNEL_HWCAP_SVEF64MM),
Dave Martin43994d82017-10-31 15:51:19 +00002130#endif
Andrew Murrayaaba0982019-04-09 10:52:40 +01002131 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_SSBS_SHIFT, FTR_UNSIGNED, ID_AA64PFR1_SSBS_PSTATE_INSNS, CAP_HWCAP, KERNEL_HWCAP_SSBS),
Mark Rutland75031972018-12-07 18:39:25 +00002132#ifdef CONFIG_ARM64_PTR_AUTH
Andrew Murrayaaba0982019-04-09 10:52:40 +01002133 HWCAP_MULTI_CAP(ptr_auth_hwcap_addr_matches, CAP_HWCAP, KERNEL_HWCAP_PACA),
2134 HWCAP_MULTI_CAP(ptr_auth_hwcap_gen_matches, CAP_HWCAP, KERNEL_HWCAP_PACG),
Mark Rutland75031972018-12-07 18:39:25 +00002135#endif
Suzuki K Poulose75283502016-04-18 10:28:33 +01002136 {},
2137};
2138
Suzuki K Poulose7559950a2020-01-13 23:30:20 +00002139#ifdef CONFIG_COMPAT
2140static bool compat_has_neon(const struct arm64_cpu_capabilities *cap, int scope)
2141{
2142 /*
2143 * Check that all of MVFR1_EL1.{SIMDSP, SIMDInt, SIMDLS} are available,
2144 * in line with that of arm32 as in vfp_init(). We make sure that the
2145 * check is future proof, by making sure value is non-zero.
2146 */
2147 u32 mvfr1;
2148
2149 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
2150 if (scope == SCOPE_SYSTEM)
2151 mvfr1 = read_sanitised_ftr_reg(SYS_MVFR1_EL1);
2152 else
2153 mvfr1 = read_sysreg_s(SYS_MVFR1_EL1);
2154
2155 return cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDSP_SHIFT) &&
2156 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDINT_SHIFT) &&
2157 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDLS_SHIFT);
2158}
2159#endif
2160
Suzuki K Poulose75283502016-04-18 10:28:33 +01002161static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002162#ifdef CONFIG_COMPAT
Suzuki K Poulose7559950a2020-01-13 23:30:20 +00002163 HWCAP_CAP_MATCH(compat_has_neon, CAP_COMPAT_HWCAP, COMPAT_HWCAP_NEON),
2164 HWCAP_CAP(SYS_MVFR1_EL1, MVFR1_SIMDFMAC_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv4),
2165 /* Arm v8 mandates MVFR0.FPDP == {0, 2}. So, piggy back on this for the presence of VFP support */
2166 HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_FPDP_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFP),
2167 HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_FPDP_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv3),
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00002168 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
2169 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
2170 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
2171 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
2172 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32),
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002173#endif
2174 {},
2175};
2176
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01002177static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002178{
2179 switch (cap->hwcap_type) {
2180 case CAP_HWCAP:
Andrew Murrayaaba0982019-04-09 10:52:40 +01002181 cpu_set_feature(cap->hwcap);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002182 break;
2183#ifdef CONFIG_COMPAT
2184 case CAP_COMPAT_HWCAP:
2185 compat_elf_hwcap |= (u32)cap->hwcap;
2186 break;
2187 case CAP_COMPAT_HWCAP2:
2188 compat_elf_hwcap2 |= (u32)cap->hwcap;
2189 break;
2190#endif
2191 default:
2192 WARN_ON(1);
2193 break;
2194 }
2195}
2196
2197/* Check if we have a particular HWCAP enabled */
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01002198static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002199{
2200 bool rc;
2201
2202 switch (cap->hwcap_type) {
2203 case CAP_HWCAP:
Andrew Murrayaaba0982019-04-09 10:52:40 +01002204 rc = cpu_have_feature(cap->hwcap);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002205 break;
2206#ifdef CONFIG_COMPAT
2207 case CAP_COMPAT_HWCAP:
2208 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
2209 break;
2210 case CAP_COMPAT_HWCAP2:
2211 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
2212 break;
2213#endif
2214 default:
2215 WARN_ON(1);
2216 rc = false;
2217 }
2218
2219 return rc;
2220}
2221
Suzuki K Poulose75283502016-04-18 10:28:33 +01002222static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002223{
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002224 /* We support emulation of accesses to CPU ID feature registers */
Andrew Murrayaaba0982019-04-09 10:52:40 +01002225 cpu_set_named_feature(CPUID);
Suzuki K Poulose75283502016-04-18 10:28:33 +01002226 for (; hwcaps->matches; hwcaps++)
Suzuki K Poulose143ba052018-03-26 15:12:31 +01002227 if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
Suzuki K Poulose75283502016-04-18 10:28:33 +01002228 cap_set_elf_hwcap(hwcaps);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002229}
2230
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002231static void update_cpu_capabilities(u16 scope_mask)
Suzuki K Poulose67948af2018-01-09 16:12:18 +00002232{
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002233 int i;
Suzuki K Poulose67948af2018-01-09 16:12:18 +00002234 const struct arm64_cpu_capabilities *caps;
2235
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002236 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002237 for (i = 0; i < ARM64_NCAPS; i++) {
2238 caps = cpu_hwcaps_ptrs[i];
2239 if (!caps || !(caps->type & scope_mask) ||
2240 cpus_have_cap(caps->capability) ||
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002241 !caps->matches(caps, cpucap_default_scope(caps)))
Marc Zyngier359b7062015-03-27 13:09:23 +00002242 continue;
2243
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002244 if (caps->desc)
2245 pr_info("detected: %s\n", caps->desc);
Suzuki K Poulose75283502016-04-18 10:28:33 +01002246 cpus_set_cap(caps->capability);
Daniel Thompson0ceb0d52019-01-31 14:58:53 +00002247
2248 if ((scope_mask & SCOPE_BOOT_CPU) && (caps->type & SCOPE_BOOT_CPU))
2249 set_bit(caps->capability, boot_capabilities);
Marc Zyngier359b7062015-03-27 13:09:23 +00002250 }
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002251}
James Morse1c076302015-07-21 13:23:28 +01002252
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002253/*
2254 * Enable all the available capabilities on this CPU. The capabilities
2255 * with BOOT_CPU scope are handled separately and hence skipped here.
2256 */
2257static int cpu_enable_non_boot_scope_capabilities(void *__unused)
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002258{
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002259 int i;
2260 u16 non_boot_scope = SCOPE_ALL & ~SCOPE_BOOT_CPU;
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002261
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002262 for_each_available_cap(i) {
2263 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[i];
Dave Martinc0cda3b2018-03-26 15:12:28 +01002264
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002265 if (WARN_ON(!cap))
2266 continue;
2267
2268 if (!(cap->type & non_boot_scope))
2269 continue;
2270
2271 if (cap->cpu_enable)
2272 cap->cpu_enable(cap);
2273 }
Dave Martinc0cda3b2018-03-26 15:12:28 +01002274 return 0;
2275}
2276
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002277/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002278 * Run through the enabled capabilities and enable() it on all active
2279 * CPUs
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002280 */
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002281static void __init enable_cpu_capabilities(u16 scope_mask)
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002282{
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002283 int i;
2284 const struct arm64_cpu_capabilities *caps;
2285 bool boot_scope;
Mark Rutland63a1e1c2017-05-16 15:18:05 +01002286
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002287 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
2288 boot_scope = !!(scope_mask & SCOPE_BOOT_CPU);
2289
2290 for (i = 0; i < ARM64_NCAPS; i++) {
2291 unsigned int num;
2292
2293 caps = cpu_hwcaps_ptrs[i];
2294 if (!caps || !(caps->type & scope_mask))
2295 continue;
2296 num = caps->capability;
2297 if (!cpus_have_cap(num))
Mark Rutland63a1e1c2017-05-16 15:18:05 +01002298 continue;
2299
2300 /* Ensure cpus_have_const_cap(num) works */
2301 static_branch_enable(&cpu_hwcap_keys[num]);
2302
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002303 if (boot_scope && caps->cpu_enable)
James Morse2a6dcb22016-10-18 11:27:46 +01002304 /*
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002305 * Capabilities with SCOPE_BOOT_CPU scope are finalised
2306 * before any secondary CPU boots. Thus, each secondary
2307 * will enable the capability as appropriate via
2308 * check_local_cpu_capabilities(). The only exception is
2309 * the boot CPU, for which the capability must be
2310 * enabled here. This approach avoids costly
2311 * stop_machine() calls for this case.
James Morse2a6dcb22016-10-18 11:27:46 +01002312 */
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002313 caps->cpu_enable(caps);
Mark Rutland63a1e1c2017-05-16 15:18:05 +01002314 }
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002315
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002316 /*
2317 * For all non-boot scope capabilities, use stop_machine()
2318 * as it schedules the work allowing us to modify PSTATE,
2319 * instead of on_each_cpu() which uses an IPI, giving us a
2320 * PSTATE that disappears when we return.
2321 */
2322 if (!boot_scope)
2323 stop_machine(cpu_enable_non_boot_scope_capabilities,
2324 NULL, cpu_online_mask);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002325}
2326
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002327/*
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002328 * Run through the list of capabilities to check for conflicts.
2329 * If the system has already detected a capability, take necessary
2330 * action on this CPU.
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002331 */
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302332static void verify_local_cpu_caps(u16 scope_mask)
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002333{
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002334 int i;
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002335 bool cpu_has_cap, system_has_cap;
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002336 const struct arm64_cpu_capabilities *caps;
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002337
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002338 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
2339
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002340 for (i = 0; i < ARM64_NCAPS; i++) {
2341 caps = cpu_hwcaps_ptrs[i];
2342 if (!caps || !(caps->type & scope_mask))
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002343 continue;
2344
Suzuki K Pouloseba7d9232018-03-26 15:12:46 +01002345 cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU);
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002346 system_has_cap = cpus_have_cap(caps->capability);
2347
2348 if (system_has_cap) {
2349 /*
2350 * Check if the new CPU misses an advertised feature,
2351 * which is not safe to miss.
2352 */
2353 if (!cpu_has_cap && !cpucap_late_cpu_optional(caps))
2354 break;
2355 /*
2356 * We have to issue cpu_enable() irrespective of
2357 * whether the CPU has it or not, as it is enabeld
2358 * system wide. It is upto the call back to take
2359 * appropriate action on this CPU.
2360 */
2361 if (caps->cpu_enable)
2362 caps->cpu_enable(caps);
2363 } else {
2364 /*
2365 * Check if the CPU has this capability if it isn't
2366 * safe to have when the system doesn't.
2367 */
2368 if (cpu_has_cap && !cpucap_late_cpu_permitted(caps))
2369 break;
2370 }
2371 }
2372
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002373 if (i < ARM64_NCAPS) {
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002374 pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n",
2375 smp_processor_id(), caps->capability,
2376 caps->desc, system_has_cap, cpu_has_cap);
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002377
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302378 if (cpucap_panic_on_conflict(caps))
2379 cpu_panic_kernel();
2380 else
2381 cpu_die_early();
2382 }
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002383}
2384
2385/*
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00002386 * Check for CPU features that are used in early boot
2387 * based on the Boot CPU value.
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002388 */
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00002389static void check_early_cpu_features(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00002390{
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00002391 verify_cpu_asid_bits();
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302392
2393 verify_local_cpu_caps(SCOPE_BOOT_CPU);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002394}
2395
Suzuki K Poulose75283502016-04-18 10:28:33 +01002396static void
2397verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
2398{
2399
Suzuki K Poulose92406f02016-04-22 12:25:31 +01002400 for (; caps->matches; caps++)
2401 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01002402 pr_crit("CPU%d: missing HWCAP: %s\n",
2403 smp_processor_id(), caps->desc);
2404 cpu_die_early();
2405 }
Suzuki K Poulose75283502016-04-18 10:28:33 +01002406}
2407
Dave Martin2e0f2472017-10-31 15:51:10 +00002408static void verify_sve_features(void)
2409{
2410 u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
2411 u64 zcr = read_zcr_features();
2412
2413 unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
2414 unsigned int len = zcr & ZCR_ELx_LEN_MASK;
2415
2416 if (len < safe_len || sve_verify_vq_map()) {
Dave Martind06b76b2018-09-28 14:39:10 +01002417 pr_crit("CPU%d: SVE: vector length support mismatch\n",
Dave Martin2e0f2472017-10-31 15:51:10 +00002418 smp_processor_id());
2419 cpu_die_early();
2420 }
2421
2422 /* Add checks on other ZCR bits here if necessary */
2423}
2424
Anshuman Khandualc73433f2020-05-12 07:27:27 +05302425static void verify_hyp_capabilities(void)
2426{
2427 u64 safe_mmfr1, mmfr0, mmfr1;
2428 int parange, ipa_max;
2429 unsigned int safe_vmid_bits, vmid_bits;
2430
2431 if (!IS_ENABLED(CONFIG_KVM) || !IS_ENABLED(CONFIG_KVM_ARM_HOST))
2432 return;
2433
2434 safe_mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
2435 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
2436 mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
2437
2438 /* Verify VMID bits */
2439 safe_vmid_bits = get_vmid_bits(safe_mmfr1);
2440 vmid_bits = get_vmid_bits(mmfr1);
2441 if (vmid_bits < safe_vmid_bits) {
2442 pr_crit("CPU%d: VMID width mismatch\n", smp_processor_id());
2443 cpu_die_early();
2444 }
2445
2446 /* Verify IPA range */
Anshuman Khandualf73531f2020-05-13 14:33:34 +05302447 parange = cpuid_feature_extract_unsigned_field(mmfr0,
2448 ID_AA64MMFR0_PARANGE_SHIFT);
Anshuman Khandualc73433f2020-05-12 07:27:27 +05302449 ipa_max = id_aa64mmfr0_parange_to_phys_shift(parange);
2450 if (ipa_max < get_kvm_ipa_limit()) {
2451 pr_crit("CPU%d: IPA range mismatch\n", smp_processor_id());
2452 cpu_die_early();
2453 }
2454}
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +01002455
2456/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002457 * Run through the enabled system capabilities and enable() it on this CPU.
2458 * The capabilities were decided based on the available CPUs at the boot time.
2459 * Any new CPU should match the system wide status of the capability. If the
2460 * new CPU doesn't have a capability which the system now has enabled, we
2461 * cannot do anything to fix it up and could cause unexpected failures. So
2462 * we park the CPU.
2463 */
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002464static void verify_local_cpu_capabilities(void)
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002465{
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002466 /*
2467 * The capabilities with SCOPE_BOOT_CPU are checked from
2468 * check_early_cpu_features(), as they need to be verified
2469 * on all secondary CPUs.
2470 */
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302471 verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002472
Suzuki K Poulose75283502016-04-18 10:28:33 +01002473 verify_local_elf_hwcaps(arm64_elf_hwcaps);
Dave Martin2e0f2472017-10-31 15:51:10 +00002474
Suzuki K Poulose643d7032016-04-18 10:28:37 +01002475 if (system_supports_32bit_el0())
2476 verify_local_elf_hwcaps(compat_elf_hwcaps);
Dave Martin2e0f2472017-10-31 15:51:10 +00002477
2478 if (system_supports_sve())
2479 verify_sve_features();
Anshuman Khandualc73433f2020-05-12 07:27:27 +05302480
2481 if (is_hyp_mode_available())
2482 verify_hyp_capabilities();
Marc Zyngier359b7062015-03-27 13:09:23 +00002483}
2484
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002485void check_local_cpu_capabilities(void)
2486{
2487 /*
2488 * All secondary CPUs should conform to the early CPU features
2489 * in use by the kernel based on boot CPU.
2490 */
2491 check_early_cpu_features();
2492
2493 /*
2494 * If we haven't finalised the system capabilities, this CPU gets
Suzuki K Poulosefbd890b2018-03-26 15:12:37 +01002495 * a chance to update the errata work arounds and local features.
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002496 * Otherwise, this CPU should verify that it has all the system
2497 * advertised capabilities.
2498 */
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +00002499 if (!system_capabilities_finalized())
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002500 update_cpu_capabilities(SCOPE_LOCAL_CPU);
2501 else
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002502 verify_local_cpu_capabilities();
2503}
2504
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002505static void __init setup_boot_cpu_capabilities(void)
2506{
2507 /* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */
2508 update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU);
2509 /* Enable the SCOPE_BOOT_CPU capabilities alone right away */
2510 enable_cpu_capabilities(SCOPE_BOOT_CPU);
2511}
2512
Suzuki K Poulosef7bfc142018-11-30 17:18:04 +00002513bool this_cpu_has_cap(unsigned int n)
Marc Zyngier8f4137582017-01-30 15:39:52 +00002514{
Suzuki K Poulosef7bfc142018-11-30 17:18:04 +00002515 if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
2516 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
2517
2518 if (cap)
2519 return cap->matches(cap, SCOPE_LOCAL_CPU);
2520 }
2521
2522 return false;
Marc Zyngier8f4137582017-01-30 15:39:52 +00002523}
2524
Amit Daniel Kachhap3ff047f2020-03-13 14:34:48 +05302525/*
2526 * This helper function is used in a narrow window when,
2527 * - The system wide safe registers are set with all the SMP CPUs and,
2528 * - The SYSTEM_FEATURE cpu_hwcaps may not have been set.
2529 * In all other cases cpus_have_{const_}cap() should be used.
2530 */
2531static bool __system_matches_cap(unsigned int n)
2532{
2533 if (n < ARM64_NCAPS) {
2534 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
2535
2536 if (cap)
2537 return cap->matches(cap, SCOPE_SYSTEM);
2538 }
2539 return false;
2540}
2541
Andrew Murrayaec0bff2019-04-09 10:52:41 +01002542void cpu_set_feature(unsigned int num)
2543{
2544 WARN_ON(num >= MAX_CPU_FEATURES);
2545 elf_hwcap |= BIT(num);
2546}
2547EXPORT_SYMBOL_GPL(cpu_set_feature);
2548
2549bool cpu_have_feature(unsigned int num)
2550{
2551 WARN_ON(num >= MAX_CPU_FEATURES);
2552 return elf_hwcap & BIT(num);
2553}
2554EXPORT_SYMBOL_GPL(cpu_have_feature);
2555
2556unsigned long cpu_get_elf_hwcap(void)
2557{
2558 /*
2559 * We currently only populate the first 32 bits of AT_HWCAP. Please
2560 * note that for userspace compatibility we guarantee that bits 62
2561 * and 63 will always be returned as 0.
2562 */
2563 return lower_32_bits(elf_hwcap);
2564}
2565
2566unsigned long cpu_get_elf_hwcap2(void)
2567{
2568 return upper_32_bits(elf_hwcap);
2569}
2570
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002571static void __init setup_system_capabilities(void)
2572{
2573 /*
2574 * We have finalised the system-wide safe feature
2575 * registers, finalise the capabilities that depend
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002576 * on it. Also enable all the available capabilities,
2577 * that are not enabled already.
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002578 */
2579 update_cpu_capabilities(SCOPE_SYSTEM);
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002580 enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002581}
2582
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002583void __init setup_cpu_features(void)
2584{
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002585 u32 cwg;
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002586
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002587 setup_system_capabilities();
Suzuki K Poulose75283502016-04-18 10:28:33 +01002588 setup_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01002589
2590 if (system_supports_32bit_el0())
2591 setup_elf_hwcaps(compat_elf_hwcaps);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002592
Kees Cook2e6f5492018-02-21 10:18:21 -08002593 if (system_uses_ttbr0_pan())
2594 pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
2595
Dave Martin2e0f2472017-10-31 15:51:10 +00002596 sve_setup();
Dave Martin94b07c12018-06-01 11:10:14 +01002597 minsigstksz_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +00002598
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002599 /* Advertise that we have computed the system capabilities */
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +00002600 finalize_system_capabilities();
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002601
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002602 /*
2603 * Check for sane CTR_EL0.CWG value.
2604 */
2605 cwg = cache_type_cwg();
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002606 if (!cwg)
Catalin Marinasebc7e212018-05-11 13:33:12 +01002607 pr_warn("No Cache Writeback Granule information, assuming %d\n",
2608 ARCH_DMA_MINALIGN);
Marc Zyngier359b7062015-03-27 13:09:23 +00002609}
James Morse70544192016-02-05 14:58:50 +00002610
2611static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +01002612cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
James Morse70544192016-02-05 14:58:50 +00002613{
Amit Daniel Kachhap3ff047f2020-03-13 14:34:48 +05302614 return (__system_matches_cap(ARM64_HAS_PAN) && !__system_matches_cap(ARM64_HAS_UAO));
James Morse70544192016-02-05 14:58:50 +00002615}
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002616
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01002617static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap)
2618{
2619 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
2620}
2621
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002622/*
2623 * We emulate only the following system register space.
2624 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 4 - 7]
2625 * See Table C5-6 System instruction encodings for System register accesses,
2626 * ARMv8 ARM(ARM DDI 0487A.f) for more details.
2627 */
2628static inline bool __attribute_const__ is_emulated(u32 id)
2629{
2630 return (sys_reg_Op0(id) == 0x3 &&
2631 sys_reg_CRn(id) == 0x0 &&
2632 sys_reg_Op1(id) == 0x0 &&
2633 (sys_reg_CRm(id) == 0 ||
2634 ((sys_reg_CRm(id) >= 4) && (sys_reg_CRm(id) <= 7))));
2635}
2636
2637/*
2638 * With CRm == 0, reg should be one of :
2639 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
2640 */
2641static inline int emulate_id_reg(u32 id, u64 *valp)
2642{
2643 switch (id) {
2644 case SYS_MIDR_EL1:
2645 *valp = read_cpuid_id();
2646 break;
2647 case SYS_MPIDR_EL1:
2648 *valp = SYS_MPIDR_SAFE_VAL;
2649 break;
2650 case SYS_REVIDR_EL1:
2651 /* IMPLEMENTATION DEFINED values are emulated with 0 */
2652 *valp = 0;
2653 break;
2654 default:
2655 return -EINVAL;
2656 }
2657
2658 return 0;
2659}
2660
2661static int emulate_sys_reg(u32 id, u64 *valp)
2662{
2663 struct arm64_ftr_reg *regp;
2664
2665 if (!is_emulated(id))
2666 return -EINVAL;
2667
2668 if (sys_reg_CRm(id) == 0)
2669 return emulate_id_reg(id, valp);
2670
Anshuman Khandual3577dd32020-05-27 15:34:36 +05302671 regp = get_arm64_ftr_reg_nowarn(id);
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002672 if (regp)
2673 *valp = arm64_ftr_reg_user_value(regp);
2674 else
2675 /*
2676 * The untracked registers are either IMPLEMENTATION DEFINED
2677 * (e.g, ID_AFR0_EL1) or reserved RAZ.
2678 */
2679 *valp = 0;
2680 return 0;
2681}
2682
Anshuman Khandual520ad982018-09-20 09:36:20 +05302683int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt)
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002684{
2685 int rc;
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002686 u64 val;
2687
Anshuman Khandual520ad982018-09-20 09:36:20 +05302688 rc = emulate_sys_reg(sys_reg, &val);
2689 if (!rc) {
2690 pt_regs_write_reg(regs, rt, val);
2691 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
2692 }
2693 return rc;
2694}
2695
2696static int emulate_mrs(struct pt_regs *regs, u32 insn)
2697{
2698 u32 sys_reg, rt;
2699
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002700 /*
2701 * sys_reg values are defined as used in mrs/msr instruction.
2702 * shift the imm value to get the encoding.
2703 */
2704 sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
Anshuman Khandual520ad982018-09-20 09:36:20 +05302705 rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
2706 return do_emulate_mrs(regs, sys_reg, rt);
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002707}
2708
2709static struct undef_hook mrs_hook = {
2710 .instr_mask = 0xfff00000,
2711 .instr_val = 0xd5300000,
Mark Rutlandd64567f2018-07-05 15:16:52 +01002712 .pstate_mask = PSR_AA32_MODE_MASK,
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002713 .pstate_val = PSR_MODE_EL0t,
2714 .fn = emulate_mrs,
2715};
2716
2717static int __init enable_mrs_emulation(void)
2718{
2719 register_undef_hook(&mrs_hook);
2720 return 0;
2721}
2722
Suzuki K Poulosec0d88322017-10-06 14:16:52 +01002723core_initcall(enable_mrs_emulation);
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05002724
2725ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr,
2726 char *buf)
2727{
2728 if (__meltdown_safe)
2729 return sprintf(buf, "Not affected\n");
2730
2731 if (arm64_kernel_unmapped_at_el0())
2732 return sprintf(buf, "Mitigation: PTI\n");
2733
2734 return sprintf(buf, "Vulnerable\n");
2735}