blob: 7e0dbe2a2f2d93ceef58485f74307e222387026c [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.
Marc Zyngier359b7062015-03-27 13:09:23 +00006 */
7
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01008#define pr_fmt(fmt) "CPU features: " fmt
Marc Zyngier359b7062015-03-27 13:09:23 +00009
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010010#include <linux/bsearch.h>
James Morse2a6dcb22016-10-18 11:27:46 +010011#include <linux/cpumask.h>
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +010012#include <linux/crash_dump.h>
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010013#include <linux/sort.h>
James Morse2a6dcb22016-10-18 11:27:46 +010014#include <linux/stop_machine.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000015#include <linux/types.h>
Laura Abbott2077be62017-01-10 13:35:49 -080016#include <linux/mm.h>
Josh Poimboeufa111b7c2019-04-12 15:39:32 -050017#include <linux/cpu.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000018#include <asm/cpu.h>
19#include <asm/cpufeature.h>
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +010020#include <asm/cpu_ops.h>
Dave Martin2e0f2472017-10-31 15:51:10 +000021#include <asm/fpsimd.h>
Suzuki K Poulose13f417f2016-02-23 10:31:45 +000022#include <asm/mmu_context.h>
James Morse338d4f42015-07-22 19:05:54 +010023#include <asm/processor.h>
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +010024#include <asm/sysreg.h>
Suzuki K Poulose77c97b42017-01-09 17:28:31 +000025#include <asm/traps.h>
Marc Zyngierd88701b2015-01-29 11:24:05 +000026#include <asm/virt.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000027
Andrew Murrayaec0bff2019-04-09 10:52:41 +010028/* Kernel representation of AT_HWCAP and AT_HWCAP2 */
29static unsigned long elf_hwcap __read_mostly;
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010030
31#ifdef CONFIG_COMPAT
32#define COMPAT_ELF_HWCAP_DEFAULT \
33 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
34 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
Suzuki K Poulose7559950a2020-01-13 23:30:20 +000035 COMPAT_HWCAP_TLS|COMPAT_HWCAP_IDIV|\
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010036 COMPAT_HWCAP_LPAE)
37unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
38unsigned int compat_elf_hwcap2 __read_mostly;
39#endif
40
41DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010042EXPORT_SYMBOL(cpu_hwcaps);
Suzuki K Poulose82a3a212018-11-30 17:18:03 +000043static struct arm64_cpu_capabilities const __ro_after_init *cpu_hwcaps_ptrs[ARM64_NCAPS];
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010044
Daniel Thompson0ceb0d52019-01-31 14:58:53 +000045/* Need also bit for ARM64_CB_PATCH */
46DECLARE_BITMAP(boot_capabilities, ARM64_NPATCHABLE);
47
Mark Brown09e3c222019-12-09 18:12:17 +000048bool arm64_use_ng_mappings = false;
49EXPORT_SYMBOL(arm64_use_ng_mappings);
50
Dave Martin8f1eec52017-10-31 15:51:09 +000051/*
52 * Flag to indicate if we have computed the system wide
53 * capabilities based on the boot time active CPUs. This
54 * will be used to determine if a new booting CPU should
55 * go through the verification process to make sure that it
56 * supports the system capabilities, without using a hotplug
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +000057 * notifier. This is also used to decide if we could use
58 * the fast path for checking constant CPU caps.
Dave Martin8f1eec52017-10-31 15:51:09 +000059 */
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +000060DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
61EXPORT_SYMBOL(arm64_const_caps_ready);
62static inline void finalize_system_capabilities(void)
Dave Martin8f1eec52017-10-31 15:51:09 +000063{
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +000064 static_branch_enable(&arm64_const_caps_ready);
Dave Martin8f1eec52017-10-31 15:51:09 +000065}
66
Mark Rutland8effeaa2017-06-21 18:11:23 +010067static int dump_cpu_hwcaps(struct notifier_block *self, unsigned long v, void *p)
68{
69 /* file-wide pr_fmt adds "CPU features: " prefix */
70 pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
71 return 0;
72}
73
74static struct notifier_block cpu_hwcaps_notifier = {
75 .notifier_call = dump_cpu_hwcaps
76};
77
78static int __init register_cpu_hwcaps_dumper(void)
79{
80 atomic_notifier_chain_register(&panic_notifier_list,
81 &cpu_hwcaps_notifier);
82 return 0;
83}
84__initcall(register_cpu_hwcaps_dumper);
85
Catalin Marinasefd9e032016-09-05 18:25:48 +010086DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
87EXPORT_SYMBOL(cpu_hwcap_keys);
88
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000089#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010090 { \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000091 .sign = SIGNED, \
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000092 .visible = VISIBLE, \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010093 .strict = STRICT, \
94 .type = TYPE, \
95 .shift = SHIFT, \
96 .width = WIDTH, \
97 .safe_val = SAFE_VAL, \
98 }
99
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000100/* Define a feature with unsigned values */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000101#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
102 __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +0000103
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000104/* Define a feature with a signed value */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000105#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
106 __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000107
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100108#define ARM64_FTR_END \
109 { \
110 .width = 0, \
111 }
112
James Morse70544192016-02-05 14:58:50 +0000113/* meta feature for alternatives */
114static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100115cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
116
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +0100117static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
James Morse70544192016-02-05 14:58:50 +0000118
Amit Daniel Kachhap3ff047f2020-03-13 14:34:48 +0530119static bool __system_matches_cap(unsigned int n);
120
Suzuki K Poulose4aa8a472017-01-09 17:28:32 +0000121/*
122 * NOTE: Any changes to the visibility of features should be kept in
123 * sync with the documentation of the CPU feature register ABI.
124 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100125static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
Richard Henderson1a50ec02020-01-21 12:58:52 +0000126 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RNDR_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000127 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TS_SHIFT, 4, 0),
Dongjiu Geng3b3b6812017-12-13 18:13:56 +0800128 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_FHM_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100129 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
130 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
131 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
132 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
133 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000134 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
135 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
136 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
137 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
138 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100139 ARM64_FTR_END,
140};
141
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000142static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
Steven Priced4209d82019-12-16 11:33:37 +0000143 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_I8MM_SHIFT, 4, 0),
144 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DGH_SHIFT, 4, 0),
145 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_BF16_SHIFT, 4, 0),
146 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SPECRES_SHIFT, 4, 0),
Will Deaconbd4fb6d2018-06-14 11:21:34 +0100147 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SB_SHIFT, 4, 0),
Julien Grall7230f7e2019-10-03 12:12:08 +0100148 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FRINTTS_SHIFT, 4, 0),
Mark Rutland6984eb42018-12-07 18:39:24 +0000149 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
150 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPI_SHIFT, 4, 0),
151 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
152 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPA_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100153 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_LRCPC_SHIFT, 4, 0),
154 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FCMA_SHIFT, 4, 0),
155 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_JSCVT_SHIFT, 4, 0),
Mark Rutland6984eb42018-12-07 18:39:24 +0000156 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
157 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_API_SHIFT, 4, 0),
158 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
159 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_APA_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100160 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DPB_SHIFT, 4, 0),
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000161 ARM64_FTR_END,
162};
163
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100164static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
Will Deacon179a56f2017-11-27 18:29:30 +0000165 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
Will Deacon0f15adb2018-01-03 11:17:58 +0000166 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000167 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_DIT_SHIFT, 4, 0),
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +0000168 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_AMU_SHIFT, 4, 0),
Dave Martin3fab3992017-12-14 14:03:44 +0000169 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
170 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SVE_SHIFT, 4, 0),
Xie XiuQi64c02722018-01-15 19:38:56 +0000171 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_RAS_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100172 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_GIC_SHIFT, 4, 0),
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000173 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
174 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100175 /* Linux doesn't care about the EL3 */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100176 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
177 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
178 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
179 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100180 ARM64_FTR_END,
181};
182
Will Deacond71be2b2018-06-15 11:37:34 +0100183static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
184 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_SSBS_SHIFT, 4, ID_AA64PFR1_SSBS_PSTATE_NI),
185 ARM64_FTR_END,
186};
187
Dave Martin06a916f2019-04-18 18:41:38 +0100188static const struct arm64_ftr_bits ftr_id_aa64zfr0[] = {
Julien Grallec52c712019-10-14 11:21:13 +0100189 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Steven Priced4209d82019-12-16 11:33:37 +0000190 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_F64MM_SHIFT, 4, 0),
191 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
192 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_F32MM_SHIFT, 4, 0),
193 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
194 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_I8MM_SHIFT, 4, 0),
195 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Julien Grallec52c712019-10-14 11:21:13 +0100196 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SM4_SHIFT, 4, 0),
197 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
198 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SHA3_SHIFT, 4, 0),
199 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Steven Priced4209d82019-12-16 11:33:37 +0000200 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_BF16_SHIFT, 4, 0),
201 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
Julien Grallec52c712019-10-14 11:21:13 +0100202 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_BITPERM_SHIFT, 4, 0),
203 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
204 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_AES_SHIFT, 4, 0),
205 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
206 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_SVEVER_SHIFT, 4, 0),
Dave Martin06a916f2019-04-18 18:41:38 +0100207 ARM64_FTR_END,
208};
209
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100210static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
Will Deacon5717fe52019-08-12 16:02:25 +0100211 /*
212 * We already refuse to boot CPUs that don't support our configured
213 * page size, so we can only detect mismatches for a page size other
214 * than the one we're currently using. Unfortunately, SoCs like this
215 * exist in the wild so, even though we don't like it, we'll have to go
216 * along with it and treat them as non-strict.
217 */
218 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
219 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
220 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
221
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100222 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100223 /* Linux shouldn't care about secure memory */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100224 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
225 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
226 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100227 /*
228 * Differing PARange is fine as long as all peripherals and memory are mapped
229 * within the minimum PARange of all CPUs
230 */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000231 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100232 ARM64_FTR_END,
233};
234
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100235static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000236 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100237 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
238 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
239 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
240 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
241 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100242 ARM64_FTR_END,
243};
244
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100245static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
Mark Brown3e6c69a2019-12-09 18:12:14 +0000246 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_E0PD_SHIFT, 4, 0),
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100247 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_FWB_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000248 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_AT_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100249 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
Sai Prakash Ranjan9d3f8882020-04-21 15:29:15 +0100250 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100251 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
252 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
253 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000254 ARM64_FTR_END,
255};
256
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100257static const struct arm64_ftr_bits ftr_ctr[] = {
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600258 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
259 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DIC_SHIFT, 1, 1),
260 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IDC_SHIFT, 1, 1),
Will Deacon147b9632019-07-30 15:40:20 +0100261 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_CWG_SHIFT, 4, 0),
262 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_ERG_SHIFT, 4, 0),
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600263 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DMINLINE_SHIFT, 4, 1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100264 /*
265 * Linux can handle differing I-cache policies. Userspace JITs will
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100266 * make use of *minLine.
Will Deacon155433c2017-03-10 20:32:22 +0000267 * If we have differing I-cache policies, report it as the weakest - VIPT.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100268 */
Will Deacon155433c2017-03-10 20:32:22 +0000269 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_VIPT), /* L1Ip */
Suzuki K Poulose4c4a39d2018-07-04 23:07:45 +0100270 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IMINLINE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100271 ARM64_FTR_END,
272};
273
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100274struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
275 .name = "SYS_CTR_EL0",
276 .ftr_bits = ftr_ctr
277};
278
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100279static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100280 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0xf), /* InnerShr */
281 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0), /* FCSE */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000282 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100283 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0), /* TCM */
284 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* ShareLvl */
285 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0xf), /* OuterShr */
286 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* PMSA */
287 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* VMSA */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100288 ARM64_FTR_END,
289};
290
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100291static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000292 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 36, 28, 0),
293 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_PMSVER_SHIFT, 4, 0),
294 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
295 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
296 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
Will Deaconb20d1ba2016-07-25 16:17:52 +0100297 /*
298 * We can instantiate multiple PMU instances with different levels
299 * of support.
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000300 */
301 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
302 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
303 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100304 ARM64_FTR_END,
305};
306
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100307static const struct arm64_ftr_bits ftr_mvfr2[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100308 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* FPMisc */
309 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* SIMDMisc */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100310 ARM64_FTR_END,
311};
312
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100313static const struct arm64_ftr_bits ftr_dczid[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000314 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
315 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100316 ARM64_FTR_END,
317};
318
319
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100320static const struct arm64_ftr_bits ftr_id_isar5[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100321 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_RDM_SHIFT, 4, 0),
322 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_CRC32_SHIFT, 4, 0),
323 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA2_SHIFT, 4, 0),
324 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA1_SHIFT, 4, 0),
325 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_AES_SHIFT, 4, 0),
326 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SEVL_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100327 ARM64_FTR_END,
328};
329
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100330static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100331 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* ac2 */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100332 ARM64_FTR_END,
333};
334
Will Deacon01133402020-04-21 15:29:16 +0100335static const struct arm64_ftr_bits ftr_id_isar4[] = {
336 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SWP_FRAC_SHIFT, 4, 0),
337 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_PSR_M_SHIFT, 4, 0),
338 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SYNCH_PRIM_FRAC_SHIFT, 4, 0),
339 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_BARRIER_SHIFT, 4, 0),
340 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SMC_SHIFT, 4, 0),
341 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WRITEBACK_SHIFT, 4, 0),
342 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WITHSHIFTS_SHIFT, 4, 0),
343 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_UNPRIV_SHIFT, 4, 0),
344 ARM64_FTR_END,
345};
346
Anshuman Khandual8e3747b2019-12-17 20:17:32 +0530347static const struct arm64_ftr_bits ftr_id_isar6[] = {
348 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_I8MM_SHIFT, 4, 0),
349 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_BF16_SHIFT, 4, 0),
350 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SPECRES_SHIFT, 4, 0),
351 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SB_SHIFT, 4, 0),
352 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_FHM_SHIFT, 4, 0),
353 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_DP_SHIFT, 4, 0),
354 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_JSCVT_SHIFT, 4, 0),
355 ARM64_FTR_END,
356};
357
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100358static const struct arm64_ftr_bits ftr_id_pfr0[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100359 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* State3 */
360 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0), /* State2 */
361 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* State1 */
362 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* State0 */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100363 ARM64_FTR_END,
364};
365
Will Deacon01133402020-04-21 15:29:16 +0100366static const struct arm64_ftr_bits ftr_id_pfr1[] = {
367 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GIC_SHIFT, 4, 0),
368 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRT_FRAC_SHIFT, 4, 0),
369 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SEC_FRAC_SHIFT, 4, 0),
370 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GENTIMER_SHIFT, 4, 0),
371 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRTUALIZATION_SHIFT, 4, 0),
372 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_MPROGMOD_SHIFT, 4, 0),
373 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SECURITY_SHIFT, 4, 0),
374 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_PROGMOD_SHIFT, 4, 0),
375 ARM64_FTR_END,
376};
377
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100378static const struct arm64_ftr_bits ftr_id_dfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000379 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
380 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
381 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
382 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
383 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
384 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
385 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
386 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000387 ARM64_FTR_END,
388};
389
Dave Martin2e0f2472017-10-31 15:51:10 +0000390static const struct arm64_ftr_bits ftr_zcr[] = {
391 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
392 ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_SIZE, 0), /* LEN */
393 ARM64_FTR_END,
394};
395
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100396/*
397 * Common ftr bits for a 32bit register with all hidden, strict
398 * attributes, with 4bit feature fields and a default safe value of
399 * 0. Covers the following 32bit registers:
400 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
401 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100402static const struct arm64_ftr_bits ftr_generic_32bits[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000403 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
404 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
405 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
406 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
407 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
408 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
409 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
410 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100411 ARM64_FTR_END,
412};
413
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000414/* Table for a single 32bit feature value */
415static const struct arm64_ftr_bits ftr_single32[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000416 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100417 ARM64_FTR_END,
418};
419
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000420static const struct arm64_ftr_bits ftr_raz[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100421 ARM64_FTR_END,
422};
423
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100424#define ARM64_FTR_REG(id, table) { \
425 .sys_id = id, \
426 .reg = &(struct arm64_ftr_reg){ \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100427 .name = #id, \
428 .ftr_bits = &((table)[0]), \
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100429 }}
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100430
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100431static const struct __ftr_reg_entry {
432 u32 sys_id;
433 struct arm64_ftr_reg *reg;
434} arm64_ftr_regs[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100435
436 /* Op1 = 0, CRn = 0, CRm = 1 */
437 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
Will Deacon01133402020-04-21 15:29:16 +0100438 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_id_pfr1),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000439 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100440 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
441 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
442 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
443 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
444
445 /* Op1 = 0, CRn = 0, CRm = 2 */
446 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
447 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
448 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
449 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
Will Deacon01133402020-04-21 15:29:16 +0100450 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_id_isar4),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100451 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
452 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
Anshuman Khandual8e3747b2019-12-17 20:17:32 +0530453 ARM64_FTR_REG(SYS_ID_ISAR6_EL1, ftr_id_isar6),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100454
455 /* Op1 = 0, CRn = 0, CRm = 3 */
456 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
457 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
458 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
459
460 /* Op1 = 0, CRn = 0, CRm = 4 */
461 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
Will Deacond71be2b2018-06-15 11:37:34 +0100462 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1),
Dave Martin06a916f2019-04-18 18:41:38 +0100463 ARM64_FTR_REG(SYS_ID_AA64ZFR0_EL1, ftr_id_aa64zfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100464
465 /* Op1 = 0, CRn = 0, CRm = 5 */
466 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000467 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100468
469 /* Op1 = 0, CRn = 0, CRm = 6 */
470 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000471 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100472
473 /* Op1 = 0, CRn = 0, CRm = 7 */
474 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
475 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
James Morse406e3082016-02-05 14:58:47 +0000476 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100477
Dave Martin2e0f2472017-10-31 15:51:10 +0000478 /* Op1 = 0, CRn = 1, CRm = 2 */
479 ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
480
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100481 /* Op1 = 3, CRn = 0, CRm = 0 */
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100482 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100483 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
484
485 /* Op1 = 3, CRn = 14, CRm = 0 */
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000486 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100487};
488
489static int search_cmp_ftr_reg(const void *id, const void *regp)
490{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100491 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100492}
493
494/*
495 * get_arm64_ftr_reg - Lookup a feature register entry using its
496 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
497 * ascending order of sys_id , we use binary search to find a matching
498 * entry.
499 *
500 * returns - Upon success, matching ftr_reg entry for id.
501 * - NULL on failure. It is upto the caller to decide
502 * the impact of a failure.
503 */
504static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
505{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100506 const struct __ftr_reg_entry *ret;
507
508 ret = bsearch((const void *)(unsigned long)sys_id,
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100509 arm64_ftr_regs,
510 ARRAY_SIZE(arm64_ftr_regs),
511 sizeof(arm64_ftr_regs[0]),
512 search_cmp_ftr_reg);
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100513 if (ret)
514 return ret->reg;
515 return NULL;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100516}
517
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100518static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
519 s64 ftr_val)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100520{
521 u64 mask = arm64_ftr_mask(ftrp);
522
523 reg &= ~mask;
524 reg |= (ftr_val << ftrp->shift) & mask;
525 return reg;
526}
527
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100528static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
529 s64 cur)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100530{
531 s64 ret = 0;
532
533 switch (ftrp->type) {
534 case FTR_EXACT:
535 ret = ftrp->safe_val;
536 break;
537 case FTR_LOWER_SAFE:
538 ret = new < cur ? new : cur;
539 break;
Will Deacon147b9632019-07-30 15:40:20 +0100540 case FTR_HIGHER_OR_ZERO_SAFE:
541 if (!cur || !new)
542 break;
543 /* Fallthrough */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100544 case FTR_HIGHER_SAFE:
545 ret = new > cur ? new : cur;
546 break;
547 default:
548 BUG();
549 }
550
551 return ret;
552}
553
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100554static void __init sort_ftr_regs(void)
555{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100556 int i;
557
558 /* Check that the array is sorted so that we can do the binary search */
559 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
560 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100561}
562
563/*
564 * Initialise the CPU feature register from Boot CPU values.
565 * Also initiliases the strict_mask for the register.
Mark Rutlandb389d792017-01-09 17:28:24 +0000566 * Any bits that are not covered by an arm64_ftr_bits entry are considered
567 * RES0 for the system-wide value, and must strictly match.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100568 */
569static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
570{
571 u64 val = 0;
572 u64 strict_mask = ~0x0ULL;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000573 u64 user_mask = 0;
Mark Rutlandb389d792017-01-09 17:28:24 +0000574 u64 valid_mask = 0;
575
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100576 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100577 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
578
579 BUG_ON(!reg);
580
韩科才24b2cce2020-03-11 14:52:49 +0800581 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
Mark Rutlandb389d792017-01-09 17:28:24 +0000582 u64 ftr_mask = arm64_ftr_mask(ftrp);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100583 s64 ftr_new = arm64_ftr_value(ftrp, new);
584
585 val = arm64_ftr_set_value(ftrp, val, ftr_new);
Mark Rutlandb389d792017-01-09 17:28:24 +0000586
587 valid_mask |= ftr_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100588 if (!ftrp->strict)
Mark Rutlandb389d792017-01-09 17:28:24 +0000589 strict_mask &= ~ftr_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000590 if (ftrp->visible)
591 user_mask |= ftr_mask;
592 else
593 reg->user_val = arm64_ftr_set_value(ftrp,
594 reg->user_val,
595 ftrp->safe_val);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100596 }
Mark Rutlandb389d792017-01-09 17:28:24 +0000597
598 val &= valid_mask;
599
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100600 reg->sys_val = val;
601 reg->strict_mask = strict_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000602 reg->user_mask = user_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100603}
604
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +0100605extern const struct arm64_cpu_capabilities arm64_errata[];
Suzuki K Poulose82a3a212018-11-30 17:18:03 +0000606static const struct arm64_cpu_capabilities arm64_features[];
607
608static void __init
609init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
610{
611 for (; caps->matches; caps++) {
612 if (WARN(caps->capability >= ARM64_NCAPS,
613 "Invalid capability %d\n", caps->capability))
614 continue;
615 if (WARN(cpu_hwcaps_ptrs[caps->capability],
616 "Duplicate entry for capability %d\n",
617 caps->capability))
618 continue;
619 cpu_hwcaps_ptrs[caps->capability] = caps;
620 }
621}
622
623static void __init init_cpu_hwcaps_indirect_list(void)
624{
625 init_cpu_hwcaps_indirect_list_from_array(arm64_features);
626 init_cpu_hwcaps_indirect_list_from_array(arm64_errata);
627}
628
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100629static void __init setup_boot_cpu_capabilities(void);
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +0100630
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100631void __init init_cpu_features(struct cpuinfo_arm64 *info)
632{
633 /* Before we start using the tables, make sure it is sorted */
634 sort_ftr_regs();
635
636 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
637 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
638 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
639 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
640 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
641 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
642 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
643 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
644 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000645 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100646 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
647 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
Dave Martin2e0f2472017-10-31 15:51:10 +0000648 init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100649
650 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
651 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
652 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
653 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
654 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
655 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
656 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
657 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
Anshuman Khandual8e3747b2019-12-17 20:17:32 +0530658 init_cpu_ftr_reg(SYS_ID_ISAR6_EL1, info->reg_id_isar6);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100659 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
660 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
661 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
662 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
663 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
664 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
665 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
666 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
667 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
668 }
669
Dave Martin2e0f2472017-10-31 15:51:10 +0000670 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
671 init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
672 sve_init_vq_map();
673 }
Suzuki K Poulose5e911072018-03-26 15:12:29 +0100674
675 /*
Suzuki K Poulose82a3a212018-11-30 17:18:03 +0000676 * Initialize the indirect array of CPU hwcaps capabilities pointers
677 * before we handle the boot CPU below.
678 */
679 init_cpu_hwcaps_indirect_list();
680
681 /*
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100682 * Detect and enable early CPU capabilities based on the boot CPU,
683 * after we have initialised the CPU feature infrastructure.
Suzuki K Poulose5e911072018-03-26 15:12:29 +0100684 */
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100685 setup_boot_cpu_capabilities();
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100686}
687
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100688static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100689{
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100690 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100691
692 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
693 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
694 s64 ftr_new = arm64_ftr_value(ftrp, new);
695
696 if (ftr_cur == ftr_new)
697 continue;
698 /* Find a safe value */
699 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
700 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
701 }
702
703}
704
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100705static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100706{
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100707 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
708
709 BUG_ON(!regp);
710 update_cpu_ftr_reg(regp, val);
711 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
712 return 0;
713 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
714 regp->name, boot, cpu, val);
715 return 1;
716}
717
Will Deaconeab2f922020-04-21 15:29:20 +0100718static void relax_cpu_ftr_reg(u32 sys_id, int field)
719{
720 const struct arm64_ftr_bits *ftrp;
721 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
722
723 if (WARN_ON(!regp))
724 return;
725
726 for (ftrp = regp->ftr_bits; ftrp->width; ftrp++) {
727 if (ftrp->shift == field) {
728 regp->strict_mask &= ~arm64_ftr_mask(ftrp);
729 break;
730 }
731 }
732
733 /* Bogus field? */
734 WARN_ON(!ftrp->width);
735}
736
Will Deacon1efcfe72020-04-21 15:29:19 +0100737static int update_32bit_cpu_features(int cpu, struct cpuinfo_arm64 *info,
738 struct cpuinfo_arm64 *boot)
739{
740 int taint = 0;
741 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
742
743 /*
744 * If we don't have AArch32 at all then skip the checks entirely
745 * as the register values may be UNKNOWN and we're not going to be
746 * using them for anything.
747 */
748 if (!id_aa64pfr0_32bit_el0(pfr0))
749 return taint;
750
Will Deaconeab2f922020-04-21 15:29:20 +0100751 /*
752 * If we don't have AArch32 at EL1, then relax the strictness of
753 * EL1-dependent register fields to avoid spurious sanity check fails.
754 */
755 if (!id_aa64pfr0_32bit_el1(pfr0)) {
756 relax_cpu_ftr_reg(SYS_ID_ISAR4_EL1, ID_ISAR4_SMC_SHIFT);
757 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRT_FRAC_SHIFT);
758 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SEC_FRAC_SHIFT);
759 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRTUALIZATION_SHIFT);
760 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SECURITY_SHIFT);
761 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_PROGMOD_SHIFT);
762 }
763
Will Deacon1efcfe72020-04-21 15:29:19 +0100764 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
765 info->reg_id_dfr0, boot->reg_id_dfr0);
766 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
767 info->reg_id_isar0, boot->reg_id_isar0);
768 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
769 info->reg_id_isar1, boot->reg_id_isar1);
770 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
771 info->reg_id_isar2, boot->reg_id_isar2);
772 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
773 info->reg_id_isar3, boot->reg_id_isar3);
774 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
775 info->reg_id_isar4, boot->reg_id_isar4);
776 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
777 info->reg_id_isar5, boot->reg_id_isar5);
778 taint |= check_update_ftr_reg(SYS_ID_ISAR6_EL1, cpu,
779 info->reg_id_isar6, boot->reg_id_isar6);
780
781 /*
782 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
783 * ACTLR formats could differ across CPUs and therefore would have to
784 * be trapped for virtualization anyway.
785 */
786 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
787 info->reg_id_mmfr0, boot->reg_id_mmfr0);
788 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
789 info->reg_id_mmfr1, boot->reg_id_mmfr1);
790 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
791 info->reg_id_mmfr2, boot->reg_id_mmfr2);
792 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
793 info->reg_id_mmfr3, boot->reg_id_mmfr3);
794 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
795 info->reg_id_pfr0, boot->reg_id_pfr0);
796 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
797 info->reg_id_pfr1, boot->reg_id_pfr1);
798 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
799 info->reg_mvfr0, boot->reg_mvfr0);
800 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
801 info->reg_mvfr1, boot->reg_mvfr1);
802 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
803 info->reg_mvfr2, boot->reg_mvfr2);
804
805 return taint;
806}
807
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100808/*
809 * Update system wide CPU feature registers with the values from a
810 * non-boot CPU. Also performs SANITY checks to make sure that there
811 * aren't any insane variations from that of the boot CPU.
812 */
813void update_cpu_features(int cpu,
814 struct cpuinfo_arm64 *info,
815 struct cpuinfo_arm64 *boot)
816{
817 int taint = 0;
818
819 /*
820 * The kernel can handle differing I-cache policies, but otherwise
821 * caches should look identical. Userspace JITs will make use of
822 * *minLine.
823 */
824 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
825 info->reg_ctr, boot->reg_ctr);
826
827 /*
828 * Userspace may perform DC ZVA instructions. Mismatched block sizes
829 * could result in too much or too little memory being zeroed if a
830 * process is preempted and migrated between CPUs.
831 */
832 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
833 info->reg_dczid, boot->reg_dczid);
834
835 /* If different, timekeeping will be broken (especially with KVM) */
836 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
837 info->reg_cntfrq, boot->reg_cntfrq);
838
839 /*
840 * The kernel uses self-hosted debug features and expects CPUs to
841 * support identical debug features. We presently need CTX_CMPs, WRPs,
842 * and BRPs to be identical.
843 * ID_AA64DFR1 is currently RES0.
844 */
845 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
846 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
847 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
848 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
849 /*
850 * Even in big.LITTLE, processors should be identical instruction-set
851 * wise.
852 */
853 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
854 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
855 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
856 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
857
858 /*
859 * Differing PARange support is fine as long as all peripherals and
860 * memory are mapped within the minimum PARange of all CPUs.
861 * Linux should not care about secure memory.
862 */
863 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
864 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
865 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
866 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000867 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
868 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100869
870 /*
871 * EL3 is not our concern.
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100872 */
873 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
874 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
875 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
876 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
877
Dave Martin2e0f2472017-10-31 15:51:10 +0000878 taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
879 info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
880
Dave Martin2e0f2472017-10-31 15:51:10 +0000881 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
882 taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
883 info->reg_zcr, boot->reg_zcr);
884
885 /* Probe vector lengths, unless we already gave up on SVE */
886 if (id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +0000887 !system_capabilities_finalized())
Dave Martin2e0f2472017-10-31 15:51:10 +0000888 sve_update_vq_map();
889 }
890
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100891 /*
Will Deacon1efcfe72020-04-21 15:29:19 +0100892 * This relies on a sanitised view of the AArch64 ID registers
893 * (e.g. SYS_ID_AA64PFR0_EL1), so we call it last.
894 */
895 taint |= update_32bit_cpu_features(cpu, info, boot);
896
897 /*
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100898 * Mismatched CPU features are a recipe for disaster. Don't even
899 * pretend to support them.
900 */
Will Deacon8dd0ee62017-06-05 11:40:23 +0100901 if (taint) {
902 pr_warn_once("Unsupported CPU feature variation detected.\n");
903 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
904 }
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100905}
906
Dave Martin46823dd2017-03-23 15:14:39 +0000907u64 read_sanitised_ftr_reg(u32 id)
Suzuki K. Pouloseb3f15372015-10-19 14:24:47 +0100908{
909 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
910
911 /* We shouldn't get a request for an unsupported register */
912 BUG_ON(!regp);
913 return regp->sys_val;
914}
Marc Zyngier359b7062015-03-27 13:09:23 +0000915
Mark Rutland965861d2017-02-02 17:32:15 +0000916#define read_sysreg_case(r) \
917 case r: return read_sysreg_s(r)
918
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100919/*
Dave Martin46823dd2017-03-23 15:14:39 +0000920 * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100921 * Read the system register on the current CPU
922 */
Dave Martin46823dd2017-03-23 15:14:39 +0000923static u64 __read_sysreg_by_encoding(u32 sys_id)
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100924{
925 switch (sys_id) {
Mark Rutland965861d2017-02-02 17:32:15 +0000926 read_sysreg_case(SYS_ID_PFR0_EL1);
927 read_sysreg_case(SYS_ID_PFR1_EL1);
928 read_sysreg_case(SYS_ID_DFR0_EL1);
929 read_sysreg_case(SYS_ID_MMFR0_EL1);
930 read_sysreg_case(SYS_ID_MMFR1_EL1);
931 read_sysreg_case(SYS_ID_MMFR2_EL1);
932 read_sysreg_case(SYS_ID_MMFR3_EL1);
933 read_sysreg_case(SYS_ID_ISAR0_EL1);
934 read_sysreg_case(SYS_ID_ISAR1_EL1);
935 read_sysreg_case(SYS_ID_ISAR2_EL1);
936 read_sysreg_case(SYS_ID_ISAR3_EL1);
937 read_sysreg_case(SYS_ID_ISAR4_EL1);
938 read_sysreg_case(SYS_ID_ISAR5_EL1);
Anshuman Khandual8e3747b2019-12-17 20:17:32 +0530939 read_sysreg_case(SYS_ID_ISAR6_EL1);
Mark Rutland965861d2017-02-02 17:32:15 +0000940 read_sysreg_case(SYS_MVFR0_EL1);
941 read_sysreg_case(SYS_MVFR1_EL1);
942 read_sysreg_case(SYS_MVFR2_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100943
Mark Rutland965861d2017-02-02 17:32:15 +0000944 read_sysreg_case(SYS_ID_AA64PFR0_EL1);
945 read_sysreg_case(SYS_ID_AA64PFR1_EL1);
Dave Martin78ed70b2019-06-03 16:35:02 +0100946 read_sysreg_case(SYS_ID_AA64ZFR0_EL1);
Mark Rutland965861d2017-02-02 17:32:15 +0000947 read_sysreg_case(SYS_ID_AA64DFR0_EL1);
948 read_sysreg_case(SYS_ID_AA64DFR1_EL1);
949 read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
950 read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
951 read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
952 read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
953 read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100954
Mark Rutland965861d2017-02-02 17:32:15 +0000955 read_sysreg_case(SYS_CNTFRQ_EL0);
956 read_sysreg_case(SYS_CTR_EL0);
957 read_sysreg_case(SYS_DCZID_EL0);
958
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100959 default:
960 BUG();
961 return 0;
962 }
963}
964
Marc Zyngier963fcd42015-09-30 11:50:04 +0100965#include <linux/irqchip/arm-gic-v3.h>
966
Marc Zyngier94a9e042015-06-12 12:06:36 +0100967static bool
James Morse18ffa042015-07-21 13:23:29 +0100968feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
969{
Suzuki K Poulose28c5dcb2016-01-26 10:58:16 +0000970 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
James Morse18ffa042015-07-21 13:23:29 +0100971
972 return val >= entry->min_field_value;
973}
974
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100975static bool
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100976has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100977{
978 u64 val;
Marc Zyngier94a9e042015-06-12 12:06:36 +0100979
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100980 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
981 if (scope == SCOPE_SYSTEM)
Dave Martin46823dd2017-03-23 15:14:39 +0000982 val = read_sanitised_ftr_reg(entry->sys_reg);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100983 else
Dave Martin46823dd2017-03-23 15:14:39 +0000984 val = __read_sysreg_by_encoding(entry->sys_reg);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100985
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100986 return feature_matches(val, entry);
987}
James Morse338d4f42015-07-22 19:05:54 +0100988
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100989static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
Marc Zyngier963fcd42015-09-30 11:50:04 +0100990{
991 bool has_sre;
992
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100993 if (!has_cpuid_feature(entry, scope))
Marc Zyngier963fcd42015-09-30 11:50:04 +0100994 return false;
995
996 has_sre = gic_enable_sre();
997 if (!has_sre)
998 pr_warn_once("%s present but disabled by higher exception level\n",
999 entry->desc);
1000
1001 return has_sre;
1002}
1003
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001004static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
Will Deacond5370f72016-02-02 12:46:24 +00001005{
1006 u32 midr = read_cpuid_id();
Will Deacond5370f72016-02-02 12:46:24 +00001007
1008 /* Cavium ThunderX pass 1.x and 2.x */
Qian Caib99286b2019-08-05 23:05:03 -04001009 return midr_is_cpu_model_range(midr, MIDR_THUNDERX,
Robert Richterfa5ce3d2017-01-13 14:12:09 +01001010 MIDR_CPU_VAR_REV(0, 0),
1011 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
Will Deacond5370f72016-02-02 12:46:24 +00001012}
1013
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001014static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
1015{
Dave Martin46823dd2017-03-23 15:14:39 +00001016 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001017
1018 return cpuid_feature_extract_signed_field(pfr0,
1019 ID_AA64PFR0_FP_SHIFT) < 0;
1020}
1021
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001022static bool has_cache_idc(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001023 int scope)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001024{
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001025 u64 ctr;
1026
1027 if (scope == SCOPE_SYSTEM)
1028 ctr = arm64_ftr_reg_ctrel0.sys_val;
1029 else
Suzuki K Poulose1602df02018-10-09 14:47:06 +01001030 ctr = read_cpuid_effective_cachetype();
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001031
1032 return ctr & BIT(CTR_IDC_SHIFT);
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001033}
1034
Suzuki K Poulose1602df02018-10-09 14:47:06 +01001035static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused)
1036{
1037 /*
1038 * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively
1039 * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses
1040 * to the CTR_EL0 on this CPU and emulate it with the real/safe
1041 * value.
1042 */
1043 if (!(read_cpuid_cachetype() & BIT(CTR_IDC_SHIFT)))
1044 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
1045}
1046
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001047static bool has_cache_dic(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001048 int scope)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001049{
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +01001050 u64 ctr;
1051
1052 if (scope == SCOPE_SYSTEM)
1053 ctr = arm64_ftr_reg_ctrel0.sys_val;
1054 else
1055 ctr = read_cpuid_cachetype();
1056
1057 return ctr & BIT(CTR_DIC_SHIFT);
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001058}
1059
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01001060static bool __maybe_unused
1061has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
1062{
1063 /*
1064 * Kdump isn't guaranteed to power-off all secondary CPUs, CNP
1065 * may share TLB entries with a CPU stuck in the crashed
1066 * kernel.
1067 */
1068 if (is_kdump_kernel())
1069 return false;
1070
1071 return has_cpuid_feature(entry, scope);
1072}
1073
Mark Brown09e3c222019-12-09 18:12:17 +00001074/*
1075 * This check is triggered during the early boot before the cpufeature
1076 * is initialised. Checking the status on the local CPU allows the boot
1077 * CPU to detect the need for non-global mappings and thus avoiding a
1078 * pagetable re-write after all the CPUs are booted. This check will be
1079 * anyway run on individual CPUs, allowing us to get the consistent
1080 * state once the SMP CPUs are up and thus make the switch to non-global
1081 * mappings if required.
1082 */
1083bool kaslr_requires_kpti(void)
1084{
Mark Brown09e3c222019-12-09 18:12:17 +00001085 if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
1086 return false;
1087
1088 /*
1089 * E0PD does a similar job to KPTI so can be used instead
1090 * where available.
1091 */
1092 if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
Will Deacona569f5f2020-01-15 14:06:37 +00001093 u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
1094 if (cpuid_feature_extract_unsigned_field(mmfr2,
1095 ID_AA64MMFR2_E0PD_SHIFT))
Mark Brown09e3c222019-12-09 18:12:17 +00001096 return false;
1097 }
1098
1099 /*
1100 * Systems affected by Cavium erratum 24756 are incompatible
1101 * with KPTI.
1102 */
Will Deaconebac96e2020-01-15 13:59:58 +00001103 if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
Mark Brown09e3c222019-12-09 18:12:17 +00001104 extern const struct midr_range cavium_erratum_27456_cpus[];
1105
Will Deaconebac96e2020-01-15 13:59:58 +00001106 if (is_midr_in_range_list(read_cpuid_id(),
1107 cavium_erratum_27456_cpus))
1108 return false;
Mark Brown09e3c222019-12-09 18:12:17 +00001109 }
Mark Brown09e3c222019-12-09 18:12:17 +00001110
1111 return kaslr_offset() > 0;
1112}
1113
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001114static bool __meltdown_safe = true;
Will Deaconea1e3de2017-11-14 14:38:19 +00001115static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
1116
1117static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulosed3aec8a2018-03-26 15:12:40 +01001118 int scope)
Will Deaconea1e3de2017-11-14 14:38:19 +00001119{
Suzuki K Poulosebe5b2992018-03-26 15:12:45 +01001120 /* List of CPUs that are not vulnerable and don't need KPTI */
1121 static const struct midr_range kpti_safe_list[] = {
1122 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
1123 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
Florian Fainelli31d868c2020-01-06 14:54:12 -08001124 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
Will Deacon2a355ec2018-12-13 13:47:38 +00001125 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
1126 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
1127 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
1128 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
1129 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
1130 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
Hanjun Guo0ecc4712019-03-05 21:40:58 +08001131 MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
Rich Wiley918e1942019-11-05 10:45:10 -08001132 MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL),
Mark Rutland71c751f2018-04-23 11:41:33 +01001133 { /* sentinel */ }
Suzuki K Poulosebe5b2992018-03-26 15:12:45 +01001134 };
Josh Poimboeufa111b7c2019-04-12 15:39:32 -05001135 char const *str = "kpti command line option";
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001136 bool meltdown_safe;
1137
1138 meltdown_safe = is_midr_in_range_list(read_cpuid_id(), kpti_safe_list);
1139
1140 /* Defer to CPU feature registers */
1141 if (has_cpuid_feature(entry, scope))
1142 meltdown_safe = true;
1143
1144 if (!meltdown_safe)
1145 __meltdown_safe = false;
Will Deacon179a56f2017-11-27 18:29:30 +00001146
Marc Zyngier6dc52b12018-01-29 11:59:56 +00001147 /*
1148 * For reasons that aren't entirely clear, enabling KPTI on Cavium
1149 * ThunderX leads to apparent I-cache corruption of kernel text, which
1150 * ends as well as you might imagine. Don't even try.
1151 */
1152 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
1153 str = "ARM64_WORKAROUND_CAVIUM_27456";
1154 __kpti_forced = -1;
1155 }
1156
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001157 /* Useful for KASLR robustness */
Mark Brownc2d92352019-12-09 18:12:15 +00001158 if (kaslr_requires_kpti()) {
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001159 if (!__kpti_forced) {
1160 str = "KASLR";
1161 __kpti_forced = 1;
1162 }
1163 }
1164
Josh Poimboeufa111b7c2019-04-12 15:39:32 -05001165 if (cpu_mitigations_off() && !__kpti_forced) {
1166 str = "mitigations=off";
1167 __kpti_forced = -1;
1168 }
1169
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001170 if (!IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) {
1171 pr_info_once("kernel page table isolation disabled by kernel configuration\n");
1172 return false;
1173 }
1174
Marc Zyngier6dc52b12018-01-29 11:59:56 +00001175 /* Forced? */
Will Deaconea1e3de2017-11-14 14:38:19 +00001176 if (__kpti_forced) {
Marc Zyngier6dc52b12018-01-29 11:59:56 +00001177 pr_info_once("kernel page table isolation forced %s by %s\n",
1178 __kpti_forced > 0 ? "ON" : "OFF", str);
Will Deaconea1e3de2017-11-14 14:38:19 +00001179 return __kpti_forced > 0;
1180 }
1181
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001182 return !meltdown_safe;
Will Deaconea1e3de2017-11-14 14:38:19 +00001183}
1184
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001185#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
Dave Martinc0cda3b2018-03-26 15:12:28 +01001186static void
1187kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
Will Deaconf992b4d2018-02-06 22:22:50 +00001188{
1189 typedef void (kpti_remap_fn)(int, int, phys_addr_t);
1190 extern kpti_remap_fn idmap_kpti_install_ng_mappings;
1191 kpti_remap_fn *remap_fn;
1192
Will Deaconf992b4d2018-02-06 22:22:50 +00001193 int cpu = smp_processor_id();
1194
Will Deaconb89d82e2019-01-08 16:19:01 +00001195 /*
1196 * We don't need to rewrite the page-tables if either we've done
1197 * it already or we have KASLR enabled and therefore have not
1198 * created any global mappings at all.
1199 */
Mark Brown09e3c222019-12-09 18:12:17 +00001200 if (arm64_use_ng_mappings)
Dave Martinc0cda3b2018-03-26 15:12:28 +01001201 return;
Will Deaconf992b4d2018-02-06 22:22:50 +00001202
1203 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
1204
1205 cpu_install_idmap();
1206 remap_fn(cpu, num_online_cpus(), __pa_symbol(swapper_pg_dir));
1207 cpu_uninstall_idmap();
1208
1209 if (!cpu)
Mark Brown09e3c222019-12-09 18:12:17 +00001210 arm64_use_ng_mappings = true;
Will Deaconf992b4d2018-02-06 22:22:50 +00001211
Dave Martinc0cda3b2018-03-26 15:12:28 +01001212 return;
Will Deaconf992b4d2018-02-06 22:22:50 +00001213}
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05001214#else
1215static void
1216kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
1217{
1218}
1219#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
Will Deaconf992b4d2018-02-06 22:22:50 +00001220
Will Deaconea1e3de2017-11-14 14:38:19 +00001221static int __init parse_kpti(char *str)
1222{
1223 bool enabled;
1224 int ret = strtobool(str, &enabled);
1225
1226 if (ret)
1227 return ret;
1228
1229 __kpti_forced = enabled ? 1 : -1;
1230 return 0;
1231}
Will Deaconb5b7dd62018-06-22 10:25:25 +01001232early_param("kpti", parse_kpti);
Will Deaconea1e3de2017-11-14 14:38:19 +00001233
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001234#ifdef CONFIG_ARM64_HW_AFDBM
1235static inline void __cpu_enable_hw_dbm(void)
1236{
1237 u64 tcr = read_sysreg(tcr_el1) | TCR_HD;
1238
1239 write_sysreg(tcr, tcr_el1);
1240 isb();
1241}
1242
Suzuki K Pouloseece13972018-03-26 15:12:49 +01001243static bool cpu_has_broken_dbm(void)
1244{
1245 /* List of CPUs which have broken DBM support. */
1246 static const struct midr_range cpus[] = {
1247#ifdef CONFIG_ARM64_ERRATUM_1024718
1248 MIDR_RANGE(MIDR_CORTEX_A55, 0, 0, 1, 0), // A55 r0p0 -r1p0
1249#endif
1250 {},
1251 };
1252
1253 return is_midr_in_range_list(read_cpuid_id(), cpus);
1254}
1255
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001256static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap)
1257{
Suzuki K Pouloseece13972018-03-26 15:12:49 +01001258 return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) &&
1259 !cpu_has_broken_dbm();
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001260}
1261
1262static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap)
1263{
1264 if (cpu_can_use_dbm(cap))
1265 __cpu_enable_hw_dbm();
1266}
1267
1268static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap,
1269 int __unused)
1270{
1271 static bool detected = false;
1272 /*
1273 * DBM is a non-conflicting feature. i.e, the kernel can safely
1274 * run a mix of CPUs with and without the feature. So, we
1275 * unconditionally enable the capability to allow any late CPU
1276 * to use the feature. We only enable the control bits on the
1277 * CPU, if it actually supports.
1278 *
1279 * We have to make sure we print the "feature" detection only
1280 * when at least one CPU actually uses it. So check if this CPU
1281 * can actually use it and print the message exactly once.
1282 *
1283 * This is safe as all CPUs (including secondary CPUs - due to the
1284 * LOCAL_CPU scope - and the hotplugged CPUs - via verification)
1285 * goes through the "matches" check exactly once. Also if a CPU
1286 * matches the criteria, it is guaranteed that the CPU will turn
1287 * the DBM on, as the capability is unconditionally enabled.
1288 */
1289 if (!detected && cpu_can_use_dbm(cap)) {
1290 detected = true;
1291 pr_info("detected: Hardware dirty bit management\n");
1292 }
1293
1294 return true;
1295}
1296
1297#endif
1298
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001299#ifdef CONFIG_ARM64_AMU_EXTN
1300
1301/*
1302 * The "amu_cpus" cpumask only signals that the CPU implementation for the
1303 * flagged CPUs supports the Activity Monitors Unit (AMU) but does not provide
1304 * information regarding all the events that it supports. When a CPU bit is
1305 * set in the cpumask, the user of this feature can only rely on the presence
1306 * of the 4 fixed counters for that CPU. But this does not guarantee that the
1307 * counters are enabled or access to these counters is enabled by code
1308 * executed at higher exception levels (firmware).
1309 */
1310static struct cpumask amu_cpus __read_mostly;
1311
1312bool cpu_has_amu_feat(int cpu)
1313{
1314 return cpumask_test_cpu(cpu, &amu_cpus);
1315}
1316
Ionela Voinescucd0ed032020-03-05 09:06:26 +00001317/* Initialize the use of AMU counters for frequency invariance */
1318extern void init_cpu_freq_invariance_counters(void);
1319
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001320static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap)
1321{
1322 if (has_cpuid_feature(cap, SCOPE_LOCAL_CPU)) {
1323 pr_info("detected CPU%d: Activity Monitors Unit (AMU)\n",
1324 smp_processor_id());
1325 cpumask_set_cpu(smp_processor_id(), &amu_cpus);
Ionela Voinescucd0ed032020-03-05 09:06:26 +00001326 init_cpu_freq_invariance_counters();
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001327 }
1328}
1329
1330static bool has_amu(const struct arm64_cpu_capabilities *cap,
1331 int __unused)
1332{
1333 /*
1334 * The AMU extension is a non-conflicting feature: the kernel can
1335 * safely run a mix of CPUs with and without support for the
1336 * activity monitors extension. Therefore, unconditionally enable
1337 * the capability to allow any late CPU to use the feature.
1338 *
1339 * With this feature unconditionally enabled, the cpu_enable
1340 * function will be called for all CPUs that match the criteria,
1341 * including secondary and hotplugged, marking this feature as
1342 * present on that respective CPU. The enable function will also
1343 * print a detection message.
1344 */
1345
1346 return true;
1347}
1348#endif
1349
Will Deacon12eb3692018-03-27 11:51:12 +01001350#ifdef CONFIG_ARM64_VHE
1351static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
1352{
1353 return is_kernel_in_hyp_mode();
1354}
1355
Dave Martinc0cda3b2018-03-26 15:12:28 +01001356static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
James Morse6d99b682018-01-08 15:38:06 +00001357{
1358 /*
1359 * Copy register values that aren't redirected by hardware.
1360 *
1361 * Before code patching, we only set tpidr_el1, all CPUs need to copy
1362 * this value to tpidr_el2 before we patch the code. Once we've done
1363 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
1364 * do anything here.
1365 */
Julien Thierrye9ab7a22019-01-31 14:58:52 +00001366 if (!alternative_is_applied(ARM64_HAS_VIRT_HOST_EXTN))
James Morse6d99b682018-01-08 15:38:06 +00001367 write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
James Morse6d99b682018-01-08 15:38:06 +00001368}
Will Deacon12eb3692018-03-27 11:51:12 +01001369#endif
James Morse6d99b682018-01-08 15:38:06 +00001370
Marc Zyngiere48d53a2018-04-06 12:27:28 +01001371static void cpu_has_fwb(const struct arm64_cpu_capabilities *__unused)
1372{
1373 u64 val = read_sysreg_s(SYS_CLIDR_EL1);
1374
1375 /* Check that CLIDR_EL1.LOU{U,IS} are both 0 */
1376 WARN_ON(val & (7 << 27 | 7 << 21));
1377}
1378
Will Deacon8f04e8e2018-08-07 13:47:06 +01001379#ifdef CONFIG_ARM64_SSBD
1380static int ssbs_emulation_handler(struct pt_regs *regs, u32 instr)
1381{
1382 if (user_mode(regs))
1383 return 1;
1384
Suzuki K Poulose74e24822018-09-16 23:17:23 +01001385 if (instr & BIT(PSTATE_Imm_shift))
Will Deacon8f04e8e2018-08-07 13:47:06 +01001386 regs->pstate |= PSR_SSBS_BIT;
1387 else
1388 regs->pstate &= ~PSR_SSBS_BIT;
1389
1390 arm64_skip_faulting_instruction(regs, 4);
1391 return 0;
1392}
1393
1394static struct undef_hook ssbs_emulation_hook = {
Suzuki K Poulose74e24822018-09-16 23:17:23 +01001395 .instr_mask = ~(1U << PSTATE_Imm_shift),
1396 .instr_val = 0xd500401f | PSTATE_SSBS,
Will Deacon8f04e8e2018-08-07 13:47:06 +01001397 .fn = ssbs_emulation_handler,
1398};
1399
1400static void cpu_enable_ssbs(const struct arm64_cpu_capabilities *__unused)
1401{
1402 static bool undef_hook_registered = false;
Julien Grall27e6e7d2019-05-30 12:30:58 +01001403 static DEFINE_RAW_SPINLOCK(hook_lock);
Will Deacon8f04e8e2018-08-07 13:47:06 +01001404
Julien Grall27e6e7d2019-05-30 12:30:58 +01001405 raw_spin_lock(&hook_lock);
Will Deacon8f04e8e2018-08-07 13:47:06 +01001406 if (!undef_hook_registered) {
1407 register_undef_hook(&ssbs_emulation_hook);
1408 undef_hook_registered = true;
1409 }
Julien Grall27e6e7d2019-05-30 12:30:58 +01001410 raw_spin_unlock(&hook_lock);
Will Deacon8f04e8e2018-08-07 13:47:06 +01001411
1412 if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) {
1413 sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
1414 arm64_set_ssbd_mitigation(false);
1415 } else {
1416 arm64_set_ssbd_mitigation(true);
1417 }
1418}
1419#endif /* CONFIG_ARM64_SSBD */
1420
Will Deaconb8925ee2018-08-07 13:53:41 +01001421#ifdef CONFIG_ARM64_PAN
1422static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
1423{
1424 /*
1425 * We modify PSTATE. This won't work from irq context as the PSTATE
1426 * is discarded once we return from the exception.
1427 */
1428 WARN_ON_ONCE(in_interrupt());
1429
1430 sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
1431 asm(SET_PSTATE_PAN(1));
1432}
1433#endif /* CONFIG_ARM64_PAN */
1434
1435#ifdef CONFIG_ARM64_RAS_EXTN
1436static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
1437{
1438 /* Firmware may have left a deferred SError in this register. */
1439 write_sysreg_s(0, SYS_DISR_EL1);
1440}
1441#endif /* CONFIG_ARM64_RAS_EXTN */
1442
Mark Rutland6984eb42018-12-07 18:39:24 +00001443#ifdef CONFIG_ARM64_PTR_AUTH
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301444static bool has_address_auth(const struct arm64_cpu_capabilities *entry,
1445 int __unused)
Mark Rutland75031972018-12-07 18:39:25 +00001446{
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301447 return __system_matches_cap(ARM64_HAS_ADDRESS_AUTH_ARCH) ||
1448 __system_matches_cap(ARM64_HAS_ADDRESS_AUTH_IMP_DEF);
1449}
1450
1451static bool has_generic_auth(const struct arm64_cpu_capabilities *entry,
1452 int __unused)
1453{
1454 return __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH) ||
1455 __system_matches_cap(ARM64_HAS_GENERIC_AUTH_IMP_DEF);
Mark Rutland75031972018-12-07 18:39:25 +00001456}
Mark Rutland6984eb42018-12-07 18:39:24 +00001457#endif /* CONFIG_ARM64_PTR_AUTH */
1458
Mark Brown3e6c69a2019-12-09 18:12:14 +00001459#ifdef CONFIG_ARM64_E0PD
1460static void cpu_enable_e0pd(struct arm64_cpu_capabilities const *cap)
1461{
1462 if (this_cpu_has_cap(ARM64_HAS_E0PD))
1463 sysreg_clear_set(tcr_el1, 0, TCR_E0PD1);
1464}
1465#endif /* CONFIG_ARM64_E0PD */
1466
Julien Thierryb90d2b22019-01-31 14:58:42 +00001467#ifdef CONFIG_ARM64_PSEUDO_NMI
Julien Thierrybc3c03c2019-01-31 14:59:03 +00001468static bool enable_pseudo_nmi;
1469
1470static int __init early_enable_pseudo_nmi(char *p)
1471{
1472 return strtobool(p, &enable_pseudo_nmi);
1473}
1474early_param("irqchip.gicv3_pseudo_nmi", early_enable_pseudo_nmi);
1475
Julien Thierryb90d2b22019-01-31 14:58:42 +00001476static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry,
1477 int scope)
1478{
Julien Thierrybc3c03c2019-01-31 14:59:03 +00001479 return enable_pseudo_nmi && has_useable_gicv3_cpuif(entry, scope);
Julien Thierryb90d2b22019-01-31 14:58:42 +00001480}
1481#endif
1482
Amit Daniel Kachhap8c176e12020-03-13 14:34:53 +05301483/* Internal helper functions to match cpu capability type */
1484static bool
1485cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap)
1486{
1487 return !!(cap->type & ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU);
1488}
1489
1490static bool
1491cpucap_late_cpu_permitted(const struct arm64_cpu_capabilities *cap)
1492{
1493 return !!(cap->type & ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU);
1494}
1495
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05301496static bool
1497cpucap_panic_on_conflict(const struct arm64_cpu_capabilities *cap)
1498{
1499 return !!(cap->type & ARM64_CPUCAP_PANIC_ON_CONFLICT);
1500}
1501
Marc Zyngier359b7062015-03-27 13:09:23 +00001502static const struct arm64_cpu_capabilities arm64_features[] = {
Marc Zyngier94a9e042015-06-12 12:06:36 +01001503 {
1504 .desc = "GIC system register CPU interface",
1505 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
Julien Thierryc9bfdf72019-01-31 14:58:41 +00001506 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
Marc Zyngier963fcd42015-09-30 11:50:04 +01001507 .matches = has_useable_gicv3_cpuif,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001508 .sys_reg = SYS_ID_AA64PFR0_EL1,
1509 .field_pos = ID_AA64PFR0_GIC_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001510 .sign = FTR_UNSIGNED,
James Morse18ffa042015-07-21 13:23:29 +01001511 .min_field_value = 1,
Marc Zyngier94a9e042015-06-12 12:06:36 +01001512 },
James Morse338d4f42015-07-22 19:05:54 +01001513#ifdef CONFIG_ARM64_PAN
1514 {
1515 .desc = "Privileged Access Never",
1516 .capability = ARM64_HAS_PAN,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001517 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001518 .matches = has_cpuid_feature,
1519 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1520 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001521 .sign = FTR_UNSIGNED,
James Morse338d4f42015-07-22 19:05:54 +01001522 .min_field_value = 1,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001523 .cpu_enable = cpu_enable_pan,
James Morse338d4f42015-07-22 19:05:54 +01001524 },
1525#endif /* CONFIG_ARM64_PAN */
Catalin Marinas395af862020-01-15 11:30:08 +00001526#ifdef CONFIG_ARM64_LSE_ATOMICS
Will Deacon2e94da12015-07-27 16:23:58 +01001527 {
1528 .desc = "LSE atomic instructions",
1529 .capability = ARM64_HAS_LSE_ATOMICS,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001530 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001531 .matches = has_cpuid_feature,
1532 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1533 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001534 .sign = FTR_UNSIGNED,
Will Deacon2e94da12015-07-27 16:23:58 +01001535 .min_field_value = 2,
1536 },
Catalin Marinas395af862020-01-15 11:30:08 +00001537#endif /* CONFIG_ARM64_LSE_ATOMICS */
Marc Zyngierd88701b2015-01-29 11:24:05 +00001538 {
Will Deacond5370f72016-02-02 12:46:24 +00001539 .desc = "Software prefetching using PRFM",
1540 .capability = ARM64_HAS_NO_HW_PREFETCH,
Suzuki K Poulose5c137712018-03-26 15:12:39 +01001541 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
Will Deacond5370f72016-02-02 12:46:24 +00001542 .matches = has_no_hw_prefetch,
1543 },
James Morse57f49592016-02-05 14:58:48 +00001544#ifdef CONFIG_ARM64_UAO
1545 {
1546 .desc = "User Access Override",
1547 .capability = ARM64_HAS_UAO,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001548 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse57f49592016-02-05 14:58:48 +00001549 .matches = has_cpuid_feature,
1550 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1551 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
1552 .min_field_value = 1,
James Morsec8b06e32017-01-09 18:14:02 +00001553 /*
1554 * We rely on stop_machine() calling uao_thread_switch() to set
1555 * UAO immediately after patching.
1556 */
James Morse57f49592016-02-05 14:58:48 +00001557 },
1558#endif /* CONFIG_ARM64_UAO */
James Morse70544192016-02-05 14:58:50 +00001559#ifdef CONFIG_ARM64_PAN
1560 {
1561 .capability = ARM64_ALT_PAN_NOT_UAO,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001562 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse70544192016-02-05 14:58:50 +00001563 .matches = cpufeature_pan_not_uao,
1564 },
1565#endif /* CONFIG_ARM64_PAN */
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001566#ifdef CONFIG_ARM64_VHE
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001567 {
Marc Zyngierd88701b2015-01-29 11:24:05 +00001568 .desc = "Virtualization Host Extensions",
1569 .capability = ARM64_HAS_VIRT_HOST_EXTN,
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001570 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
Marc Zyngierd88701b2015-01-29 11:24:05 +00001571 .matches = runs_at_el2,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001572 .cpu_enable = cpu_copy_el2regs,
Marc Zyngierd88701b2015-01-29 11:24:05 +00001573 },
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001574#endif /* CONFIG_ARM64_VHE */
Suzuki K Poulose042446a2016-04-18 10:28:36 +01001575 {
1576 .desc = "32-bit EL0 Support",
1577 .capability = ARM64_HAS_32BIT_EL0,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001578 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K Poulose042446a2016-04-18 10:28:36 +01001579 .matches = has_cpuid_feature,
1580 .sys_reg = SYS_ID_AA64PFR0_EL1,
1581 .sign = FTR_UNSIGNED,
1582 .field_pos = ID_AA64PFR0_EL0_SHIFT,
1583 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
1584 },
Will Deacon540f76d2020-04-21 15:29:17 +01001585#ifdef CONFIG_KVM
1586 {
1587 .desc = "32-bit EL1 Support",
1588 .capability = ARM64_HAS_32BIT_EL1,
1589 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1590 .matches = has_cpuid_feature,
1591 .sys_reg = SYS_ID_AA64PFR0_EL1,
1592 .sign = FTR_UNSIGNED,
1593 .field_pos = ID_AA64PFR0_EL1_SHIFT,
1594 .min_field_value = ID_AA64PFR0_EL1_32BIT_64BIT,
1595 },
1596#endif
Will Deaconea1e3de2017-11-14 14:38:19 +00001597 {
Will Deacon179a56f2017-11-27 18:29:30 +00001598 .desc = "Kernel page table isolation (KPTI)",
Will Deaconea1e3de2017-11-14 14:38:19 +00001599 .capability = ARM64_UNMAP_KERNEL_AT_EL0,
Suzuki K Poulosed3aec8a2018-03-26 15:12:40 +01001600 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
1601 /*
1602 * The ID feature fields below are used to indicate that
1603 * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for
1604 * more details.
1605 */
1606 .sys_reg = SYS_ID_AA64PFR0_EL1,
1607 .field_pos = ID_AA64PFR0_CSV3_SHIFT,
1608 .min_field_value = 1,
Will Deaconea1e3de2017-11-14 14:38:19 +00001609 .matches = unmap_kernel_at_el0,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001610 .cpu_enable = kpti_install_ng_mappings,
Will Deaconea1e3de2017-11-14 14:38:19 +00001611 },
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001612 {
1613 /* FP/SIMD is not implemented */
1614 .capability = ARM64_HAS_NO_FPSIMD,
Suzuki K Poulose449443c2020-01-13 23:30:19 +00001615 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001616 .min_field_value = 0,
1617 .matches = has_no_fpsimd,
1618 },
Robin Murphyd50e0712017-07-25 11:55:42 +01001619#ifdef CONFIG_ARM64_PMEM
1620 {
1621 .desc = "Data cache clean to Point of Persistence",
1622 .capability = ARM64_HAS_DCPOP,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001623 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Robin Murphyd50e0712017-07-25 11:55:42 +01001624 .matches = has_cpuid_feature,
1625 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1626 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1627 .min_field_value = 1,
1628 },
Andrew Murrayb9585f52019-04-09 10:52:45 +01001629 {
1630 .desc = "Data cache clean to Point of Deep Persistence",
1631 .capability = ARM64_HAS_DCPODP,
1632 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1633 .matches = has_cpuid_feature,
1634 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1635 .sign = FTR_UNSIGNED,
1636 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1637 .min_field_value = 2,
1638 },
Robin Murphyd50e0712017-07-25 11:55:42 +01001639#endif
Dave Martin43994d82017-10-31 15:51:19 +00001640#ifdef CONFIG_ARM64_SVE
1641 {
1642 .desc = "Scalable Vector Extension",
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001643 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Dave Martin43994d82017-10-31 15:51:19 +00001644 .capability = ARM64_SVE,
Dave Martin43994d82017-10-31 15:51:19 +00001645 .sys_reg = SYS_ID_AA64PFR0_EL1,
1646 .sign = FTR_UNSIGNED,
1647 .field_pos = ID_AA64PFR0_SVE_SHIFT,
1648 .min_field_value = ID_AA64PFR0_SVE,
1649 .matches = has_cpuid_feature,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001650 .cpu_enable = sve_kernel_enable,
Dave Martin43994d82017-10-31 15:51:19 +00001651 },
1652#endif /* CONFIG_ARM64_SVE */
Xie XiuQi64c02722018-01-15 19:38:56 +00001653#ifdef CONFIG_ARM64_RAS_EXTN
1654 {
1655 .desc = "RAS Extension Support",
1656 .capability = ARM64_HAS_RAS_EXTN,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001657 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Xie XiuQi64c02722018-01-15 19:38:56 +00001658 .matches = has_cpuid_feature,
1659 .sys_reg = SYS_ID_AA64PFR0_EL1,
1660 .sign = FTR_UNSIGNED,
1661 .field_pos = ID_AA64PFR0_RAS_SHIFT,
1662 .min_field_value = ID_AA64PFR0_RAS_V1,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001663 .cpu_enable = cpu_clear_disr,
Xie XiuQi64c02722018-01-15 19:38:56 +00001664 },
1665#endif /* CONFIG_ARM64_RAS_EXTN */
Ionela Voinescu2c9d45b2020-03-05 09:06:21 +00001666#ifdef CONFIG_ARM64_AMU_EXTN
1667 {
1668 /*
1669 * The feature is enabled by default if CONFIG_ARM64_AMU_EXTN=y.
1670 * Therefore, don't provide .desc as we don't want the detection
1671 * message to be shown until at least one CPU is detected to
1672 * support the feature.
1673 */
1674 .capability = ARM64_HAS_AMU_EXTN,
1675 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1676 .matches = has_amu,
1677 .sys_reg = SYS_ID_AA64PFR0_EL1,
1678 .sign = FTR_UNSIGNED,
1679 .field_pos = ID_AA64PFR0_AMU_SHIFT,
1680 .min_field_value = ID_AA64PFR0_AMU,
1681 .cpu_enable = cpu_amu_enable,
1682 },
1683#endif /* CONFIG_ARM64_AMU_EXTN */
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001684 {
1685 .desc = "Data cache clean to the PoU not required for I/D coherence",
1686 .capability = ARM64_HAS_CACHE_IDC,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001687 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001688 .matches = has_cache_idc,
Suzuki K Poulose1602df02018-10-09 14:47:06 +01001689 .cpu_enable = cpu_emulate_effective_ctr,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001690 },
1691 {
1692 .desc = "Instruction cache invalidation not required for I/D coherence",
1693 .capability = ARM64_HAS_CACHE_DIC,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001694 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001695 .matches = has_cache_dic,
1696 },
Marc Zyngiere48d53a2018-04-06 12:27:28 +01001697 {
1698 .desc = "Stage-2 Force Write-Back",
1699 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1700 .capability = ARM64_HAS_STAGE2_FWB,
1701 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1702 .sign = FTR_UNSIGNED,
1703 .field_pos = ID_AA64MMFR2_FWB_SHIFT,
1704 .min_field_value = 1,
1705 .matches = has_cpuid_feature,
1706 .cpu_enable = cpu_has_fwb,
1707 },
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001708#ifdef CONFIG_ARM64_HW_AFDBM
1709 {
1710 /*
1711 * Since we turn this on always, we don't want the user to
1712 * think that the feature is available when it may not be.
1713 * So hide the description.
1714 *
1715 * .desc = "Hardware pagetable Dirty Bit Management",
1716 *
1717 */
1718 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1719 .capability = ARM64_HW_DBM,
1720 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1721 .sign = FTR_UNSIGNED,
1722 .field_pos = ID_AA64MMFR1_HADBS_SHIFT,
1723 .min_field_value = 2,
1724 .matches = has_hw_dbm,
1725 .cpu_enable = cpu_enable_hw_dbm,
1726 },
1727#endif
Ard Biesheuvel86d0dd32018-08-27 13:02:43 +02001728 {
1729 .desc = "CRC32 instructions",
1730 .capability = ARM64_HAS_CRC32,
1731 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1732 .matches = has_cpuid_feature,
1733 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1734 .field_pos = ID_AA64ISAR0_CRC32_SHIFT,
1735 .min_field_value = 1,
1736 },
Will Deacon4f9f4962018-11-21 15:07:00 +00001737#ifdef CONFIG_ARM64_SSBD
Will Deacond71be2b2018-06-15 11:37:34 +01001738 {
1739 .desc = "Speculative Store Bypassing Safe (SSBS)",
1740 .capability = ARM64_SSBS,
1741 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1742 .matches = has_cpuid_feature,
1743 .sys_reg = SYS_ID_AA64PFR1_EL1,
1744 .field_pos = ID_AA64PFR1_SSBS_SHIFT,
1745 .sign = FTR_UNSIGNED,
1746 .min_field_value = ID_AA64PFR1_SSBS_PSTATE_ONLY,
Will Deacon8f04e8e2018-08-07 13:47:06 +01001747 .cpu_enable = cpu_enable_ssbs,
Will Deacond71be2b2018-06-15 11:37:34 +01001748 },
Will Deacon8f04e8e2018-08-07 13:47:06 +01001749#endif
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01001750#ifdef CONFIG_ARM64_CNP
1751 {
1752 .desc = "Common not Private translations",
1753 .capability = ARM64_HAS_CNP,
1754 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1755 .matches = has_useable_cnp,
1756 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1757 .sign = FTR_UNSIGNED,
1758 .field_pos = ID_AA64MMFR2_CNP_SHIFT,
1759 .min_field_value = 1,
1760 .cpu_enable = cpu_enable_cnp,
1761 },
1762#endif
Will Deaconbd4fb6d2018-06-14 11:21:34 +01001763 {
1764 .desc = "Speculation barrier (SB)",
1765 .capability = ARM64_HAS_SB,
1766 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1767 .matches = has_cpuid_feature,
1768 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1769 .field_pos = ID_AA64ISAR1_SB_SHIFT,
1770 .sign = FTR_UNSIGNED,
1771 .min_field_value = 1,
1772 },
Mark Rutland6984eb42018-12-07 18:39:24 +00001773#ifdef CONFIG_ARM64_PTR_AUTH
1774 {
1775 .desc = "Address authentication (architected algorithm)",
1776 .capability = ARM64_HAS_ADDRESS_AUTH_ARCH,
Kristina Martsenko69829342020-03-13 14:34:55 +05301777 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
Mark Rutland6984eb42018-12-07 18:39:24 +00001778 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1779 .sign = FTR_UNSIGNED,
1780 .field_pos = ID_AA64ISAR1_APA_SHIFT,
1781 .min_field_value = ID_AA64ISAR1_APA_ARCHITECTED,
1782 .matches = has_cpuid_feature,
1783 },
1784 {
1785 .desc = "Address authentication (IMP DEF algorithm)",
1786 .capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF,
Kristina Martsenko69829342020-03-13 14:34:55 +05301787 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
Mark Rutland6984eb42018-12-07 18:39:24 +00001788 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1789 .sign = FTR_UNSIGNED,
1790 .field_pos = ID_AA64ISAR1_API_SHIFT,
1791 .min_field_value = ID_AA64ISAR1_API_IMP_DEF,
1792 .matches = has_cpuid_feature,
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301793 },
1794 {
1795 .capability = ARM64_HAS_ADDRESS_AUTH,
Kristina Martsenko69829342020-03-13 14:34:55 +05301796 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301797 .matches = has_address_auth,
Mark Rutland6984eb42018-12-07 18:39:24 +00001798 },
1799 {
1800 .desc = "Generic authentication (architected algorithm)",
1801 .capability = ARM64_HAS_GENERIC_AUTH_ARCH,
1802 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1803 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1804 .sign = FTR_UNSIGNED,
1805 .field_pos = ID_AA64ISAR1_GPA_SHIFT,
1806 .min_field_value = ID_AA64ISAR1_GPA_ARCHITECTED,
1807 .matches = has_cpuid_feature,
1808 },
1809 {
1810 .desc = "Generic authentication (IMP DEF algorithm)",
1811 .capability = ARM64_HAS_GENERIC_AUTH_IMP_DEF,
1812 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1813 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1814 .sign = FTR_UNSIGNED,
1815 .field_pos = ID_AA64ISAR1_GPI_SHIFT,
1816 .min_field_value = ID_AA64ISAR1_GPI_IMP_DEF,
1817 .matches = has_cpuid_feature,
1818 },
Kristina Martsenkocfef06b2020-03-13 14:34:49 +05301819 {
1820 .capability = ARM64_HAS_GENERIC_AUTH,
1821 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1822 .matches = has_generic_auth,
1823 },
Mark Rutland6984eb42018-12-07 18:39:24 +00001824#endif /* CONFIG_ARM64_PTR_AUTH */
Julien Thierryb90d2b22019-01-31 14:58:42 +00001825#ifdef CONFIG_ARM64_PSEUDO_NMI
1826 {
1827 /*
1828 * Depends on having GICv3
1829 */
1830 .desc = "IRQ priority masking",
1831 .capability = ARM64_HAS_IRQ_PRIO_MASKING,
1832 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
1833 .matches = can_use_gic_priorities,
1834 .sys_reg = SYS_ID_AA64PFR0_EL1,
1835 .field_pos = ID_AA64PFR0_GIC_SHIFT,
1836 .sign = FTR_UNSIGNED,
1837 .min_field_value = 1,
1838 },
1839#endif
Mark Brown3e6c69a2019-12-09 18:12:14 +00001840#ifdef CONFIG_ARM64_E0PD
1841 {
1842 .desc = "E0PD",
1843 .capability = ARM64_HAS_E0PD,
1844 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1845 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1846 .sign = FTR_UNSIGNED,
1847 .field_pos = ID_AA64MMFR2_E0PD_SHIFT,
1848 .matches = has_cpuid_feature,
1849 .min_field_value = 1,
1850 .cpu_enable = cpu_enable_e0pd,
1851 },
1852#endif
Richard Henderson1a50ec02020-01-21 12:58:52 +00001853#ifdef CONFIG_ARCH_RANDOM
1854 {
1855 .desc = "Random Number Generator",
1856 .capability = ARM64_HAS_RNG,
1857 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1858 .matches = has_cpuid_feature,
1859 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1860 .field_pos = ID_AA64ISAR0_RNDR_SHIFT,
1861 .sign = FTR_UNSIGNED,
1862 .min_field_value = 1,
1863 },
1864#endif
Marc Zyngier359b7062015-03-27 13:09:23 +00001865 {},
1866};
1867
Will Deacon1e013d02018-12-12 15:53:54 +00001868#define HWCAP_CPUID_MATCH(reg, field, s, min_value) \
1869 .matches = has_cpuid_feature, \
1870 .sys_reg = reg, \
1871 .field_pos = field, \
1872 .sign = s, \
1873 .min_field_value = min_value,
1874
1875#define __HWCAP_CAP(name, cap_type, cap) \
1876 .desc = name, \
1877 .type = ARM64_CPUCAP_SYSTEM_FEATURE, \
1878 .hwcap_type = cap_type, \
1879 .hwcap = cap, \
1880
1881#define HWCAP_CAP(reg, field, s, min_value, cap_type, cap) \
1882 { \
1883 __HWCAP_CAP(#cap, cap_type, cap) \
1884 HWCAP_CPUID_MATCH(reg, field, s, min_value) \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001885 }
1886
Will Deacon1e013d02018-12-12 15:53:54 +00001887#define HWCAP_MULTI_CAP(list, cap_type, cap) \
1888 { \
1889 __HWCAP_CAP(#cap, cap_type, cap) \
1890 .matches = cpucap_multi_entry_cap_matches, \
1891 .match_list = list, \
1892 }
1893
Suzuki K Poulose7559950a2020-01-13 23:30:20 +00001894#define HWCAP_CAP_MATCH(match, cap_type, cap) \
1895 { \
1896 __HWCAP_CAP(#cap, cap_type, cap) \
1897 .matches = match, \
1898 }
1899
Will Deacon1e013d02018-12-12 15:53:54 +00001900#ifdef CONFIG_ARM64_PTR_AUTH
1901static const struct arm64_cpu_capabilities ptr_auth_hwcap_addr_matches[] = {
1902 {
1903 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_APA_SHIFT,
1904 FTR_UNSIGNED, ID_AA64ISAR1_APA_ARCHITECTED)
1905 },
1906 {
1907 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_API_SHIFT,
1908 FTR_UNSIGNED, ID_AA64ISAR1_API_IMP_DEF)
1909 },
1910 {},
1911};
1912
1913static const struct arm64_cpu_capabilities ptr_auth_hwcap_gen_matches[] = {
1914 {
1915 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_GPA_SHIFT,
1916 FTR_UNSIGNED, ID_AA64ISAR1_GPA_ARCHITECTED)
1917 },
1918 {
1919 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_GPI_SHIFT,
1920 FTR_UNSIGNED, ID_AA64ISAR1_GPI_IMP_DEF)
1921 },
1922 {},
1923};
1924#endif
1925
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001926static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
Andrew Murrayaaba0982019-04-09 10:52:40 +01001927 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_PMULL),
1928 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_AES),
1929 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA1),
1930 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA2),
1931 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_SHA512),
1932 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_CRC32),
1933 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ATOMICS),
1934 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RDM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDRDM),
1935 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA3),
1936 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM3),
1937 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM4),
1938 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_DP_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDDP),
1939 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_FHM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM),
1940 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 +01001941 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 +00001942 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 +01001943 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_FP),
1944 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FPHP),
1945 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_ASIMD),
1946 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDHP),
1947 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_DIT_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DIT),
1948 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 +01001949 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 +01001950 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_JSCVT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_JSCVT),
1951 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FCMA_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FCMA),
1952 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_LRCPC),
1953 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 +01001954 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 +01001955 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 +00001956 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_BF16_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_BF16),
1957 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DGH_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DGH),
1958 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 +01001959 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 +00001960#ifdef CONFIG_ARM64_SVE
Andrew Murrayaaba0982019-04-09 10:52:40 +01001961 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 +01001962 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SVEVER_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SVEVER_SVE2, CAP_HWCAP, KERNEL_HWCAP_SVE2),
1963 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_AES_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_AES, CAP_HWCAP, KERNEL_HWCAP_SVEAES),
1964 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_AES_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_AES_PMULL, CAP_HWCAP, KERNEL_HWCAP_SVEPMULL),
1965 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 +00001966 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 +01001967 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_SHA3_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_SHA3, CAP_HWCAP, KERNEL_HWCAP_SVESHA3),
1968 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 +00001969 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_I8MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_I8MM, CAP_HWCAP, KERNEL_HWCAP_SVEI8MM),
1970 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_F32MM_SHIFT, FTR_UNSIGNED, ID_AA64ZFR0_F32MM, CAP_HWCAP, KERNEL_HWCAP_SVEF32MM),
1971 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 +00001972#endif
Andrew Murrayaaba0982019-04-09 10:52:40 +01001973 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 +00001974#ifdef CONFIG_ARM64_PTR_AUTH
Andrew Murrayaaba0982019-04-09 10:52:40 +01001975 HWCAP_MULTI_CAP(ptr_auth_hwcap_addr_matches, CAP_HWCAP, KERNEL_HWCAP_PACA),
1976 HWCAP_MULTI_CAP(ptr_auth_hwcap_gen_matches, CAP_HWCAP, KERNEL_HWCAP_PACG),
Mark Rutland75031972018-12-07 18:39:25 +00001977#endif
Suzuki K Poulose75283502016-04-18 10:28:33 +01001978 {},
1979};
1980
Suzuki K Poulose7559950a2020-01-13 23:30:20 +00001981#ifdef CONFIG_COMPAT
1982static bool compat_has_neon(const struct arm64_cpu_capabilities *cap, int scope)
1983{
1984 /*
1985 * Check that all of MVFR1_EL1.{SIMDSP, SIMDInt, SIMDLS} are available,
1986 * in line with that of arm32 as in vfp_init(). We make sure that the
1987 * check is future proof, by making sure value is non-zero.
1988 */
1989 u32 mvfr1;
1990
1991 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
1992 if (scope == SCOPE_SYSTEM)
1993 mvfr1 = read_sanitised_ftr_reg(SYS_MVFR1_EL1);
1994 else
1995 mvfr1 = read_sysreg_s(SYS_MVFR1_EL1);
1996
1997 return cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDSP_SHIFT) &&
1998 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDINT_SHIFT) &&
1999 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDLS_SHIFT);
2000}
2001#endif
2002
Suzuki K Poulose75283502016-04-18 10:28:33 +01002003static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002004#ifdef CONFIG_COMPAT
Suzuki K Poulose7559950a2020-01-13 23:30:20 +00002005 HWCAP_CAP_MATCH(compat_has_neon, CAP_COMPAT_HWCAP, COMPAT_HWCAP_NEON),
2006 HWCAP_CAP(SYS_MVFR1_EL1, MVFR1_SIMDFMAC_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv4),
2007 /* Arm v8 mandates MVFR0.FPDP == {0, 2}. So, piggy back on this for the presence of VFP support */
2008 HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_FPDP_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFP),
2009 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 +00002010 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
2011 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
2012 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
2013 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
2014 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 +01002015#endif
2016 {},
2017};
2018
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01002019static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002020{
2021 switch (cap->hwcap_type) {
2022 case CAP_HWCAP:
Andrew Murrayaaba0982019-04-09 10:52:40 +01002023 cpu_set_feature(cap->hwcap);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002024 break;
2025#ifdef CONFIG_COMPAT
2026 case CAP_COMPAT_HWCAP:
2027 compat_elf_hwcap |= (u32)cap->hwcap;
2028 break;
2029 case CAP_COMPAT_HWCAP2:
2030 compat_elf_hwcap2 |= (u32)cap->hwcap;
2031 break;
2032#endif
2033 default:
2034 WARN_ON(1);
2035 break;
2036 }
2037}
2038
2039/* Check if we have a particular HWCAP enabled */
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01002040static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002041{
2042 bool rc;
2043
2044 switch (cap->hwcap_type) {
2045 case CAP_HWCAP:
Andrew Murrayaaba0982019-04-09 10:52:40 +01002046 rc = cpu_have_feature(cap->hwcap);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002047 break;
2048#ifdef CONFIG_COMPAT
2049 case CAP_COMPAT_HWCAP:
2050 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
2051 break;
2052 case CAP_COMPAT_HWCAP2:
2053 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
2054 break;
2055#endif
2056 default:
2057 WARN_ON(1);
2058 rc = false;
2059 }
2060
2061 return rc;
2062}
2063
Suzuki K Poulose75283502016-04-18 10:28:33 +01002064static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002065{
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002066 /* We support emulation of accesses to CPU ID feature registers */
Andrew Murrayaaba0982019-04-09 10:52:40 +01002067 cpu_set_named_feature(CPUID);
Suzuki K Poulose75283502016-04-18 10:28:33 +01002068 for (; hwcaps->matches; hwcaps++)
Suzuki K Poulose143ba052018-03-26 15:12:31 +01002069 if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
Suzuki K Poulose75283502016-04-18 10:28:33 +01002070 cap_set_elf_hwcap(hwcaps);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01002071}
2072
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002073static void update_cpu_capabilities(u16 scope_mask)
Suzuki K Poulose67948af2018-01-09 16:12:18 +00002074{
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002075 int i;
Suzuki K Poulose67948af2018-01-09 16:12:18 +00002076 const struct arm64_cpu_capabilities *caps;
2077
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002078 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002079 for (i = 0; i < ARM64_NCAPS; i++) {
2080 caps = cpu_hwcaps_ptrs[i];
2081 if (!caps || !(caps->type & scope_mask) ||
2082 cpus_have_cap(caps->capability) ||
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002083 !caps->matches(caps, cpucap_default_scope(caps)))
Marc Zyngier359b7062015-03-27 13:09:23 +00002084 continue;
2085
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002086 if (caps->desc)
2087 pr_info("detected: %s\n", caps->desc);
Suzuki K Poulose75283502016-04-18 10:28:33 +01002088 cpus_set_cap(caps->capability);
Daniel Thompson0ceb0d52019-01-31 14:58:53 +00002089
2090 if ((scope_mask & SCOPE_BOOT_CPU) && (caps->type & SCOPE_BOOT_CPU))
2091 set_bit(caps->capability, boot_capabilities);
Marc Zyngier359b7062015-03-27 13:09:23 +00002092 }
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002093}
James Morse1c076302015-07-21 13:23:28 +01002094
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002095/*
2096 * Enable all the available capabilities on this CPU. The capabilities
2097 * with BOOT_CPU scope are handled separately and hence skipped here.
2098 */
2099static int cpu_enable_non_boot_scope_capabilities(void *__unused)
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002100{
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002101 int i;
2102 u16 non_boot_scope = SCOPE_ALL & ~SCOPE_BOOT_CPU;
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002103
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002104 for_each_available_cap(i) {
2105 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[i];
Dave Martinc0cda3b2018-03-26 15:12:28 +01002106
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002107 if (WARN_ON(!cap))
2108 continue;
2109
2110 if (!(cap->type & non_boot_scope))
2111 continue;
2112
2113 if (cap->cpu_enable)
2114 cap->cpu_enable(cap);
2115 }
Dave Martinc0cda3b2018-03-26 15:12:28 +01002116 return 0;
2117}
2118
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002119/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002120 * Run through the enabled capabilities and enable() it on all active
2121 * CPUs
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002122 */
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002123static void __init enable_cpu_capabilities(u16 scope_mask)
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01002124{
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002125 int i;
2126 const struct arm64_cpu_capabilities *caps;
2127 bool boot_scope;
Mark Rutland63a1e1c2017-05-16 15:18:05 +01002128
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002129 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
2130 boot_scope = !!(scope_mask & SCOPE_BOOT_CPU);
2131
2132 for (i = 0; i < ARM64_NCAPS; i++) {
2133 unsigned int num;
2134
2135 caps = cpu_hwcaps_ptrs[i];
2136 if (!caps || !(caps->type & scope_mask))
2137 continue;
2138 num = caps->capability;
2139 if (!cpus_have_cap(num))
Mark Rutland63a1e1c2017-05-16 15:18:05 +01002140 continue;
2141
2142 /* Ensure cpus_have_const_cap(num) works */
2143 static_branch_enable(&cpu_hwcap_keys[num]);
2144
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002145 if (boot_scope && caps->cpu_enable)
James Morse2a6dcb22016-10-18 11:27:46 +01002146 /*
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002147 * Capabilities with SCOPE_BOOT_CPU scope are finalised
2148 * before any secondary CPU boots. Thus, each secondary
2149 * will enable the capability as appropriate via
2150 * check_local_cpu_capabilities(). The only exception is
2151 * the boot CPU, for which the capability must be
2152 * enabled here. This approach avoids costly
2153 * stop_machine() calls for this case.
James Morse2a6dcb22016-10-18 11:27:46 +01002154 */
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002155 caps->cpu_enable(caps);
Mark Rutland63a1e1c2017-05-16 15:18:05 +01002156 }
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002157
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00002158 /*
2159 * For all non-boot scope capabilities, use stop_machine()
2160 * as it schedules the work allowing us to modify PSTATE,
2161 * instead of on_each_cpu() which uses an IPI, giving us a
2162 * PSTATE that disappears when we return.
2163 */
2164 if (!boot_scope)
2165 stop_machine(cpu_enable_non_boot_scope_capabilities,
2166 NULL, cpu_online_mask);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002167}
2168
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002169/*
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002170 * Run through the list of capabilities to check for conflicts.
2171 * If the system has already detected a capability, take necessary
2172 * action on this CPU.
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002173 */
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302174static void verify_local_cpu_caps(u16 scope_mask)
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002175{
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002176 int i;
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002177 bool cpu_has_cap, system_has_cap;
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002178 const struct arm64_cpu_capabilities *caps;
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002179
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002180 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
2181
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002182 for (i = 0; i < ARM64_NCAPS; i++) {
2183 caps = cpu_hwcaps_ptrs[i];
2184 if (!caps || !(caps->type & scope_mask))
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01002185 continue;
2186
Suzuki K Pouloseba7d9232018-03-26 15:12:46 +01002187 cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU);
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002188 system_has_cap = cpus_have_cap(caps->capability);
2189
2190 if (system_has_cap) {
2191 /*
2192 * Check if the new CPU misses an advertised feature,
2193 * which is not safe to miss.
2194 */
2195 if (!cpu_has_cap && !cpucap_late_cpu_optional(caps))
2196 break;
2197 /*
2198 * We have to issue cpu_enable() irrespective of
2199 * whether the CPU has it or not, as it is enabeld
2200 * system wide. It is upto the call back to take
2201 * appropriate action on this CPU.
2202 */
2203 if (caps->cpu_enable)
2204 caps->cpu_enable(caps);
2205 } else {
2206 /*
2207 * Check if the CPU has this capability if it isn't
2208 * safe to have when the system doesn't.
2209 */
2210 if (cpu_has_cap && !cpucap_late_cpu_permitted(caps))
2211 break;
2212 }
2213 }
2214
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00002215 if (i < ARM64_NCAPS) {
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002216 pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n",
2217 smp_processor_id(), caps->capability,
2218 caps->desc, system_has_cap, cpu_has_cap);
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002219
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302220 if (cpucap_panic_on_conflict(caps))
2221 cpu_panic_kernel();
2222 else
2223 cpu_die_early();
2224 }
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01002225}
2226
2227/*
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00002228 * Check for CPU features that are used in early boot
2229 * based on the Boot CPU value.
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002230 */
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00002231static void check_early_cpu_features(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00002232{
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00002233 verify_cpu_asid_bits();
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302234
2235 verify_local_cpu_caps(SCOPE_BOOT_CPU);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002236}
2237
Suzuki K Poulose75283502016-04-18 10:28:33 +01002238static void
2239verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
2240{
2241
Suzuki K Poulose92406f02016-04-22 12:25:31 +01002242 for (; caps->matches; caps++)
2243 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01002244 pr_crit("CPU%d: missing HWCAP: %s\n",
2245 smp_processor_id(), caps->desc);
2246 cpu_die_early();
2247 }
Suzuki K Poulose75283502016-04-18 10:28:33 +01002248}
2249
Dave Martin2e0f2472017-10-31 15:51:10 +00002250static void verify_sve_features(void)
2251{
2252 u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
2253 u64 zcr = read_zcr_features();
2254
2255 unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
2256 unsigned int len = zcr & ZCR_ELx_LEN_MASK;
2257
2258 if (len < safe_len || sve_verify_vq_map()) {
Dave Martind06b76b2018-09-28 14:39:10 +01002259 pr_crit("CPU%d: SVE: vector length support mismatch\n",
Dave Martin2e0f2472017-10-31 15:51:10 +00002260 smp_processor_id());
2261 cpu_die_early();
2262 }
2263
2264 /* Add checks on other ZCR bits here if necessary */
2265}
2266
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +01002267
2268/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002269 * Run through the enabled system capabilities and enable() it on this CPU.
2270 * The capabilities were decided based on the available CPUs at the boot time.
2271 * Any new CPU should match the system wide status of the capability. If the
2272 * new CPU doesn't have a capability which the system now has enabled, we
2273 * cannot do anything to fix it up and could cause unexpected failures. So
2274 * we park the CPU.
2275 */
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002276static void verify_local_cpu_capabilities(void)
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002277{
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002278 /*
2279 * The capabilities with SCOPE_BOOT_CPU are checked from
2280 * check_early_cpu_features(), as they need to be verified
2281 * on all secondary CPUs.
2282 */
Kristina Martsenkodeeaac52020-03-13 14:34:54 +05302283 verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002284
Suzuki K Poulose75283502016-04-18 10:28:33 +01002285 verify_local_elf_hwcaps(arm64_elf_hwcaps);
Dave Martin2e0f2472017-10-31 15:51:10 +00002286
Suzuki K Poulose643d7032016-04-18 10:28:37 +01002287 if (system_supports_32bit_el0())
2288 verify_local_elf_hwcaps(compat_elf_hwcaps);
Dave Martin2e0f2472017-10-31 15:51:10 +00002289
2290 if (system_supports_sve())
2291 verify_sve_features();
Marc Zyngier359b7062015-03-27 13:09:23 +00002292}
2293
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002294void check_local_cpu_capabilities(void)
2295{
2296 /*
2297 * All secondary CPUs should conform to the early CPU features
2298 * in use by the kernel based on boot CPU.
2299 */
2300 check_early_cpu_features();
2301
2302 /*
2303 * If we haven't finalised the system capabilities, this CPU gets
Suzuki K Poulosefbd890b2018-03-26 15:12:37 +01002304 * a chance to update the errata work arounds and local features.
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002305 * Otherwise, this CPU should verify that it has all the system
2306 * advertised capabilities.
2307 */
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +00002308 if (!system_capabilities_finalized())
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002309 update_cpu_capabilities(SCOPE_LOCAL_CPU);
2310 else
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01002311 verify_local_cpu_capabilities();
2312}
2313
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002314static void __init setup_boot_cpu_capabilities(void)
2315{
2316 /* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */
2317 update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU);
2318 /* Enable the SCOPE_BOOT_CPU capabilities alone right away */
2319 enable_cpu_capabilities(SCOPE_BOOT_CPU);
2320}
2321
Suzuki K Poulosef7bfc142018-11-30 17:18:04 +00002322bool this_cpu_has_cap(unsigned int n)
Marc Zyngier8f4137582017-01-30 15:39:52 +00002323{
Suzuki K Poulosef7bfc142018-11-30 17:18:04 +00002324 if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
2325 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
2326
2327 if (cap)
2328 return cap->matches(cap, SCOPE_LOCAL_CPU);
2329 }
2330
2331 return false;
Marc Zyngier8f4137582017-01-30 15:39:52 +00002332}
2333
Amit Daniel Kachhap3ff047f2020-03-13 14:34:48 +05302334/*
2335 * This helper function is used in a narrow window when,
2336 * - The system wide safe registers are set with all the SMP CPUs and,
2337 * - The SYSTEM_FEATURE cpu_hwcaps may not have been set.
2338 * In all other cases cpus_have_{const_}cap() should be used.
2339 */
2340static bool __system_matches_cap(unsigned int n)
2341{
2342 if (n < ARM64_NCAPS) {
2343 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
2344
2345 if (cap)
2346 return cap->matches(cap, SCOPE_SYSTEM);
2347 }
2348 return false;
2349}
2350
Andrew Murrayaec0bff2019-04-09 10:52:41 +01002351void cpu_set_feature(unsigned int num)
2352{
2353 WARN_ON(num >= MAX_CPU_FEATURES);
2354 elf_hwcap |= BIT(num);
2355}
2356EXPORT_SYMBOL_GPL(cpu_set_feature);
2357
2358bool cpu_have_feature(unsigned int num)
2359{
2360 WARN_ON(num >= MAX_CPU_FEATURES);
2361 return elf_hwcap & BIT(num);
2362}
2363EXPORT_SYMBOL_GPL(cpu_have_feature);
2364
2365unsigned long cpu_get_elf_hwcap(void)
2366{
2367 /*
2368 * We currently only populate the first 32 bits of AT_HWCAP. Please
2369 * note that for userspace compatibility we guarantee that bits 62
2370 * and 63 will always be returned as 0.
2371 */
2372 return lower_32_bits(elf_hwcap);
2373}
2374
2375unsigned long cpu_get_elf_hwcap2(void)
2376{
2377 return upper_32_bits(elf_hwcap);
2378}
2379
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002380static void __init setup_system_capabilities(void)
2381{
2382 /*
2383 * We have finalised the system-wide safe feature
2384 * registers, finalise the capabilities that depend
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002385 * on it. Also enable all the available capabilities,
2386 * that are not enabled already.
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002387 */
2388 update_cpu_capabilities(SCOPE_SYSTEM);
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01002389 enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002390}
2391
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002392void __init setup_cpu_features(void)
2393{
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002394 u32 cwg;
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002395
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01002396 setup_system_capabilities();
Suzuki K Poulose75283502016-04-18 10:28:33 +01002397 setup_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01002398
2399 if (system_supports_32bit_el0())
2400 setup_elf_hwcaps(compat_elf_hwcaps);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002401
Kees Cook2e6f5492018-02-21 10:18:21 -08002402 if (system_uses_ttbr0_pan())
2403 pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
2404
Dave Martin2e0f2472017-10-31 15:51:10 +00002405 sve_setup();
Dave Martin94b07c12018-06-01 11:10:14 +01002406 minsigstksz_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +00002407
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002408 /* Advertise that we have computed the system capabilities */
Suzuki K Pouloseb51c6ac2020-01-13 23:30:17 +00002409 finalize_system_capabilities();
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01002410
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002411 /*
2412 * Check for sane CTR_EL0.CWG value.
2413 */
2414 cwg = cache_type_cwg();
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01002415 if (!cwg)
Catalin Marinasebc7e212018-05-11 13:33:12 +01002416 pr_warn("No Cache Writeback Granule information, assuming %d\n",
2417 ARCH_DMA_MINALIGN);
Marc Zyngier359b7062015-03-27 13:09:23 +00002418}
James Morse70544192016-02-05 14:58:50 +00002419
2420static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +01002421cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
James Morse70544192016-02-05 14:58:50 +00002422{
Amit Daniel Kachhap3ff047f2020-03-13 14:34:48 +05302423 return (__system_matches_cap(ARM64_HAS_PAN) && !__system_matches_cap(ARM64_HAS_UAO));
James Morse70544192016-02-05 14:58:50 +00002424}
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002425
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01002426static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap)
2427{
2428 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
2429}
2430
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002431/*
2432 * We emulate only the following system register space.
2433 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 4 - 7]
2434 * See Table C5-6 System instruction encodings for System register accesses,
2435 * ARMv8 ARM(ARM DDI 0487A.f) for more details.
2436 */
2437static inline bool __attribute_const__ is_emulated(u32 id)
2438{
2439 return (sys_reg_Op0(id) == 0x3 &&
2440 sys_reg_CRn(id) == 0x0 &&
2441 sys_reg_Op1(id) == 0x0 &&
2442 (sys_reg_CRm(id) == 0 ||
2443 ((sys_reg_CRm(id) >= 4) && (sys_reg_CRm(id) <= 7))));
2444}
2445
2446/*
2447 * With CRm == 0, reg should be one of :
2448 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
2449 */
2450static inline int emulate_id_reg(u32 id, u64 *valp)
2451{
2452 switch (id) {
2453 case SYS_MIDR_EL1:
2454 *valp = read_cpuid_id();
2455 break;
2456 case SYS_MPIDR_EL1:
2457 *valp = SYS_MPIDR_SAFE_VAL;
2458 break;
2459 case SYS_REVIDR_EL1:
2460 /* IMPLEMENTATION DEFINED values are emulated with 0 */
2461 *valp = 0;
2462 break;
2463 default:
2464 return -EINVAL;
2465 }
2466
2467 return 0;
2468}
2469
2470static int emulate_sys_reg(u32 id, u64 *valp)
2471{
2472 struct arm64_ftr_reg *regp;
2473
2474 if (!is_emulated(id))
2475 return -EINVAL;
2476
2477 if (sys_reg_CRm(id) == 0)
2478 return emulate_id_reg(id, valp);
2479
2480 regp = get_arm64_ftr_reg(id);
2481 if (regp)
2482 *valp = arm64_ftr_reg_user_value(regp);
2483 else
2484 /*
2485 * The untracked registers are either IMPLEMENTATION DEFINED
2486 * (e.g, ID_AFR0_EL1) or reserved RAZ.
2487 */
2488 *valp = 0;
2489 return 0;
2490}
2491
Anshuman Khandual520ad982018-09-20 09:36:20 +05302492int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt)
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002493{
2494 int rc;
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002495 u64 val;
2496
Anshuman Khandual520ad982018-09-20 09:36:20 +05302497 rc = emulate_sys_reg(sys_reg, &val);
2498 if (!rc) {
2499 pt_regs_write_reg(regs, rt, val);
2500 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
2501 }
2502 return rc;
2503}
2504
2505static int emulate_mrs(struct pt_regs *regs, u32 insn)
2506{
2507 u32 sys_reg, rt;
2508
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002509 /*
2510 * sys_reg values are defined as used in mrs/msr instruction.
2511 * shift the imm value to get the encoding.
2512 */
2513 sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
Anshuman Khandual520ad982018-09-20 09:36:20 +05302514 rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
2515 return do_emulate_mrs(regs, sys_reg, rt);
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002516}
2517
2518static struct undef_hook mrs_hook = {
2519 .instr_mask = 0xfff00000,
2520 .instr_val = 0xd5300000,
Mark Rutlandd64567f2018-07-05 15:16:52 +01002521 .pstate_mask = PSR_AA32_MODE_MASK,
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002522 .pstate_val = PSR_MODE_EL0t,
2523 .fn = emulate_mrs,
2524};
2525
2526static int __init enable_mrs_emulation(void)
2527{
2528 register_undef_hook(&mrs_hook);
2529 return 0;
2530}
2531
Suzuki K Poulosec0d88322017-10-06 14:16:52 +01002532core_initcall(enable_mrs_emulation);
Jeremy Linton1b3ccf42019-04-15 16:21:22 -05002533
2534ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr,
2535 char *buf)
2536{
2537 if (__meltdown_safe)
2538 return sprintf(buf, "Not affected\n");
2539
2540 if (arm64_kernel_unmapped_at_el0())
2541 return sprintf(buf, "Mitigation: PTI\n");
2542
2543 return sprintf(buf, "Vulnerable\n");
2544}