blob: c82ebd8f008737948504cffb2b4d847728edac16 [file] [log] [blame]
Marc Zyngier359b7062015-03-27 13:09:23 +00001/*
2 * Contains CPU feature definitions
3 *
4 * Copyright (C) 2015 ARM Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010019#define pr_fmt(fmt) "CPU features: " fmt
Marc Zyngier359b7062015-03-27 13:09:23 +000020
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010021#include <linux/bsearch.h>
James Morse2a6dcb22016-10-18 11:27:46 +010022#include <linux/cpumask.h>
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +010023#include <linux/crash_dump.h>
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010024#include <linux/sort.h>
James Morse2a6dcb22016-10-18 11:27:46 +010025#include <linux/stop_machine.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000026#include <linux/types.h>
Laura Abbott2077be62017-01-10 13:35:49 -080027#include <linux/mm.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000028#include <asm/cpu.h>
29#include <asm/cpufeature.h>
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +010030#include <asm/cpu_ops.h>
Dave Martin2e0f2472017-10-31 15:51:10 +000031#include <asm/fpsimd.h>
Suzuki K Poulose13f417f2016-02-23 10:31:45 +000032#include <asm/mmu_context.h>
James Morse338d4f42015-07-22 19:05:54 +010033#include <asm/processor.h>
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +010034#include <asm/sysreg.h>
Suzuki K Poulose77c97b42017-01-09 17:28:31 +000035#include <asm/traps.h>
Marc Zyngierd88701b2015-01-29 11:24:05 +000036#include <asm/virt.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000037
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010038unsigned long elf_hwcap __read_mostly;
39EXPORT_SYMBOL_GPL(elf_hwcap);
40
41#ifdef CONFIG_COMPAT
42#define COMPAT_ELF_HWCAP_DEFAULT \
43 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
44 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
45 COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
46 COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
47 COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
48 COMPAT_HWCAP_LPAE)
49unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
50unsigned int compat_elf_hwcap2 __read_mostly;
51#endif
52
53DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010054EXPORT_SYMBOL(cpu_hwcaps);
Suzuki K Poulose82a3a212018-11-30 17:18:03 +000055static struct arm64_cpu_capabilities const __ro_after_init *cpu_hwcaps_ptrs[ARM64_NCAPS];
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010056
Dave Martin8f1eec52017-10-31 15:51:09 +000057/*
58 * Flag to indicate if we have computed the system wide
59 * capabilities based on the boot time active CPUs. This
60 * will be used to determine if a new booting CPU should
61 * go through the verification process to make sure that it
62 * supports the system capabilities, without using a hotplug
63 * notifier.
64 */
65static bool sys_caps_initialised;
66
67static inline void set_sys_caps_initialised(void)
68{
69 sys_caps_initialised = true;
70}
71
Mark Rutland8effeaa2017-06-21 18:11:23 +010072static int dump_cpu_hwcaps(struct notifier_block *self, unsigned long v, void *p)
73{
74 /* file-wide pr_fmt adds "CPU features: " prefix */
75 pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
76 return 0;
77}
78
79static struct notifier_block cpu_hwcaps_notifier = {
80 .notifier_call = dump_cpu_hwcaps
81};
82
83static int __init register_cpu_hwcaps_dumper(void)
84{
85 atomic_notifier_chain_register(&panic_notifier_list,
86 &cpu_hwcaps_notifier);
87 return 0;
88}
89__initcall(register_cpu_hwcaps_dumper);
90
Catalin Marinasefd9e032016-09-05 18:25:48 +010091DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
92EXPORT_SYMBOL(cpu_hwcap_keys);
93
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000094#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010095 { \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000096 .sign = SIGNED, \
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000097 .visible = VISIBLE, \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010098 .strict = STRICT, \
99 .type = TYPE, \
100 .shift = SHIFT, \
101 .width = WIDTH, \
102 .safe_val = SAFE_VAL, \
103 }
104
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000105/* Define a feature with unsigned values */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000106#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
107 __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +0000108
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000109/* Define a feature with a signed value */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000110#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
111 __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000112
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100113#define ARM64_FTR_END \
114 { \
115 .width = 0, \
116 }
117
James Morse70544192016-02-05 14:58:50 +0000118/* meta feature for alternatives */
119static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100120cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
121
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +0100122static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
James Morse70544192016-02-05 14:58:50 +0000123
Suzuki K Poulose4aa8a472017-01-09 17:28:32 +0000124/*
125 * NOTE: Any changes to the visibility of features should be kept in
126 * sync with the documentation of the CPU feature register ABI.
127 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100128static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000129 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TS_SHIFT, 4, 0),
Dongjiu Geng3b3b6812017-12-13 18:13:56 +0800130 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_FHM_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100131 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
132 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
133 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
134 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
135 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000136 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
137 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
138 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
139 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
140 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100141 ARM64_FTR_END,
142};
143
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000144static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
Will Deaconbd4fb6d2018-06-14 11:21:34 +0100145 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SB_SHIFT, 4, 0),
Mark Rutland6984eb42018-12-07 18:39:24 +0000146 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
147 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPI_SHIFT, 4, 0),
148 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
149 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_GPA_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100150 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_LRCPC_SHIFT, 4, 0),
151 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FCMA_SHIFT, 4, 0),
152 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_JSCVT_SHIFT, 4, 0),
Mark Rutland6984eb42018-12-07 18:39:24 +0000153 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
154 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_API_SHIFT, 4, 0),
155 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
156 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_APA_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100157 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DPB_SHIFT, 4, 0),
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000158 ARM64_FTR_END,
159};
160
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100161static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
Will Deacon179a56f2017-11-27 18:29:30 +0000162 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
Will Deacon0f15adb2018-01-03 11:17:58 +0000163 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000164 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_DIT_SHIFT, 4, 0),
Dave Martin3fab3992017-12-14 14:03:44 +0000165 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
166 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SVE_SHIFT, 4, 0),
Xie XiuQi64c02722018-01-15 19:38:56 +0000167 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_RAS_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100168 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_GIC_SHIFT, 4, 0),
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000169 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
170 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 +0100171 /* Linux doesn't care about the EL3 */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100172 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
173 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
174 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
175 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 +0100176 ARM64_FTR_END,
177};
178
Will Deacond71be2b2018-06-15 11:37:34 +0100179static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
180 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_SSBS_SHIFT, 4, ID_AA64PFR1_SSBS_PSTATE_NI),
181 ARM64_FTR_END,
182};
183
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100184static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100185 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
186 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
187 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
188 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100189 /* Linux shouldn't care about secure memory */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100190 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
191 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
192 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100193 /*
194 * Differing PARange is fine as long as all peripherals and memory are mapped
195 * within the minimum PARange of all CPUs
196 */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000197 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100198 ARM64_FTR_END,
199};
200
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100201static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000202 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100203 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
204 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
205 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
206 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
207 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100208 ARM64_FTR_END,
209};
210
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100211static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
Marc Zyngiere48d53a2018-04-06 12:27:28 +0100212 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_FWB_SHIFT, 4, 0),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +0000213 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_AT_SHIFT, 4, 0),
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100214 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
215 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
216 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
217 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
218 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000219 ARM64_FTR_END,
220};
221
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100222static const struct arm64_ftr_bits ftr_ctr[] = {
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600223 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
224 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DIC_SHIFT, 1, 1),
225 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IDC_SHIFT, 1, 1),
226 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_SAFE, CTR_CWG_SHIFT, 4, 0),
227 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_SAFE, CTR_ERG_SHIFT, 4, 0),
228 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DMINLINE_SHIFT, 4, 1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100229 /*
230 * Linux can handle differing I-cache policies. Userspace JITs will
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100231 * make use of *minLine.
Will Deacon155433c2017-03-10 20:32:22 +0000232 * If we have differing I-cache policies, report it as the weakest - VIPT.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100233 */
Will Deacon155433c2017-03-10 20:32:22 +0000234 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_VIPT), /* L1Ip */
Suzuki K Poulose4c4a39d2018-07-04 23:07:45 +0100235 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IMINLINE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100236 ARM64_FTR_END,
237};
238
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100239struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
240 .name = "SYS_CTR_EL0",
241 .ftr_bits = ftr_ctr
242};
243
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100244static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100245 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0xf), /* InnerShr */
246 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0), /* FCSE */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000247 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100248 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0), /* TCM */
249 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* ShareLvl */
250 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0xf), /* OuterShr */
251 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* PMSA */
252 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* VMSA */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100253 ARM64_FTR_END,
254};
255
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100256static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000257 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 36, 28, 0),
258 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_PMSVER_SHIFT, 4, 0),
259 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
260 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
261 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
Will Deaconb20d1ba2016-07-25 16:17:52 +0100262 /*
263 * We can instantiate multiple PMU instances with different levels
264 * of support.
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000265 */
266 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
267 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
268 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100269 ARM64_FTR_END,
270};
271
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100272static const struct arm64_ftr_bits ftr_mvfr2[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100273 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* FPMisc */
274 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* SIMDMisc */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100275 ARM64_FTR_END,
276};
277
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100278static const struct arm64_ftr_bits ftr_dczid[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000279 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
280 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100281 ARM64_FTR_END,
282};
283
284
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100285static const struct arm64_ftr_bits ftr_id_isar5[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100286 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_RDM_SHIFT, 4, 0),
287 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_CRC32_SHIFT, 4, 0),
288 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA2_SHIFT, 4, 0),
289 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA1_SHIFT, 4, 0),
290 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_AES_SHIFT, 4, 0),
291 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SEVL_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100292 ARM64_FTR_END,
293};
294
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100295static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100296 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* ac2 */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100297 ARM64_FTR_END,
298};
299
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100300static const struct arm64_ftr_bits ftr_id_pfr0[] = {
Suzuki K Poulose5bdecb72017-10-19 16:39:02 +0100301 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), /* State3 */
302 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0), /* State2 */
303 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), /* State1 */
304 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* State0 */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100305 ARM64_FTR_END,
306};
307
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100308static const struct arm64_ftr_bits ftr_id_dfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000309 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
310 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
311 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
312 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
313 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
314 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
315 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
316 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000317 ARM64_FTR_END,
318};
319
Dave Martin2e0f2472017-10-31 15:51:10 +0000320static const struct arm64_ftr_bits ftr_zcr[] = {
321 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
322 ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_SIZE, 0), /* LEN */
323 ARM64_FTR_END,
324};
325
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100326/*
327 * Common ftr bits for a 32bit register with all hidden, strict
328 * attributes, with 4bit feature fields and a default safe value of
329 * 0. Covers the following 32bit registers:
330 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
331 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100332static const struct arm64_ftr_bits ftr_generic_32bits[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000333 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
334 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
335 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
336 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
337 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
338 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
339 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
340 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100341 ARM64_FTR_END,
342};
343
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000344/* Table for a single 32bit feature value */
345static const struct arm64_ftr_bits ftr_single32[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000346 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100347 ARM64_FTR_END,
348};
349
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000350static const struct arm64_ftr_bits ftr_raz[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100351 ARM64_FTR_END,
352};
353
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100354#define ARM64_FTR_REG(id, table) { \
355 .sys_id = id, \
356 .reg = &(struct arm64_ftr_reg){ \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100357 .name = #id, \
358 .ftr_bits = &((table)[0]), \
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100359 }}
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100360
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100361static const struct __ftr_reg_entry {
362 u32 sys_id;
363 struct arm64_ftr_reg *reg;
364} arm64_ftr_regs[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100365
366 /* Op1 = 0, CRn = 0, CRm = 1 */
367 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
368 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000369 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100370 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
371 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
372 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
373 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
374
375 /* Op1 = 0, CRn = 0, CRm = 2 */
376 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
377 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
378 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
379 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
380 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
381 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
382 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
383
384 /* Op1 = 0, CRn = 0, CRm = 3 */
385 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
386 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
387 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
388
389 /* Op1 = 0, CRn = 0, CRm = 4 */
390 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
Will Deacond71be2b2018-06-15 11:37:34 +0100391 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1),
Dave Martin2e0f2472017-10-31 15:51:10 +0000392 ARM64_FTR_REG(SYS_ID_AA64ZFR0_EL1, ftr_raz),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100393
394 /* Op1 = 0, CRn = 0, CRm = 5 */
395 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000396 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100397
398 /* Op1 = 0, CRn = 0, CRm = 6 */
399 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
Suzuki K Poulosec8c37982017-03-14 18:13:25 +0000400 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100401
402 /* Op1 = 0, CRn = 0, CRm = 7 */
403 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
404 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
James Morse406e3082016-02-05 14:58:47 +0000405 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100406
Dave Martin2e0f2472017-10-31 15:51:10 +0000407 /* Op1 = 0, CRn = 1, CRm = 2 */
408 ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
409
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100410 /* Op1 = 3, CRn = 0, CRm = 0 */
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100411 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100412 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
413
414 /* Op1 = 3, CRn = 14, CRm = 0 */
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000415 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100416};
417
418static int search_cmp_ftr_reg(const void *id, const void *regp)
419{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100420 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100421}
422
423/*
424 * get_arm64_ftr_reg - Lookup a feature register entry using its
425 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
426 * ascending order of sys_id , we use binary search to find a matching
427 * entry.
428 *
429 * returns - Upon success, matching ftr_reg entry for id.
430 * - NULL on failure. It is upto the caller to decide
431 * the impact of a failure.
432 */
433static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
434{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100435 const struct __ftr_reg_entry *ret;
436
437 ret = bsearch((const void *)(unsigned long)sys_id,
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100438 arm64_ftr_regs,
439 ARRAY_SIZE(arm64_ftr_regs),
440 sizeof(arm64_ftr_regs[0]),
441 search_cmp_ftr_reg);
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100442 if (ret)
443 return ret->reg;
444 return NULL;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100445}
446
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100447static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
448 s64 ftr_val)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100449{
450 u64 mask = arm64_ftr_mask(ftrp);
451
452 reg &= ~mask;
453 reg |= (ftr_val << ftrp->shift) & mask;
454 return reg;
455}
456
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100457static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
458 s64 cur)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100459{
460 s64 ret = 0;
461
462 switch (ftrp->type) {
463 case FTR_EXACT:
464 ret = ftrp->safe_val;
465 break;
466 case FTR_LOWER_SAFE:
467 ret = new < cur ? new : cur;
468 break;
469 case FTR_HIGHER_SAFE:
470 ret = new > cur ? new : cur;
471 break;
472 default:
473 BUG();
474 }
475
476 return ret;
477}
478
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100479static void __init sort_ftr_regs(void)
480{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100481 int i;
482
483 /* Check that the array is sorted so that we can do the binary search */
484 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
485 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100486}
487
488/*
489 * Initialise the CPU feature register from Boot CPU values.
490 * Also initiliases the strict_mask for the register.
Mark Rutlandb389d792017-01-09 17:28:24 +0000491 * Any bits that are not covered by an arm64_ftr_bits entry are considered
492 * RES0 for the system-wide value, and must strictly match.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100493 */
494static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
495{
496 u64 val = 0;
497 u64 strict_mask = ~0x0ULL;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000498 u64 user_mask = 0;
Mark Rutlandb389d792017-01-09 17:28:24 +0000499 u64 valid_mask = 0;
500
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100501 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100502 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
503
504 BUG_ON(!reg);
505
506 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
Mark Rutlandb389d792017-01-09 17:28:24 +0000507 u64 ftr_mask = arm64_ftr_mask(ftrp);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100508 s64 ftr_new = arm64_ftr_value(ftrp, new);
509
510 val = arm64_ftr_set_value(ftrp, val, ftr_new);
Mark Rutlandb389d792017-01-09 17:28:24 +0000511
512 valid_mask |= ftr_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100513 if (!ftrp->strict)
Mark Rutlandb389d792017-01-09 17:28:24 +0000514 strict_mask &= ~ftr_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000515 if (ftrp->visible)
516 user_mask |= ftr_mask;
517 else
518 reg->user_val = arm64_ftr_set_value(ftrp,
519 reg->user_val,
520 ftrp->safe_val);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100521 }
Mark Rutlandb389d792017-01-09 17:28:24 +0000522
523 val &= valid_mask;
524
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100525 reg->sys_val = val;
526 reg->strict_mask = strict_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000527 reg->user_mask = user_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100528}
529
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +0100530extern const struct arm64_cpu_capabilities arm64_errata[];
Suzuki K Poulose82a3a212018-11-30 17:18:03 +0000531static const struct arm64_cpu_capabilities arm64_features[];
532
533static void __init
534init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
535{
536 for (; caps->matches; caps++) {
537 if (WARN(caps->capability >= ARM64_NCAPS,
538 "Invalid capability %d\n", caps->capability))
539 continue;
540 if (WARN(cpu_hwcaps_ptrs[caps->capability],
541 "Duplicate entry for capability %d\n",
542 caps->capability))
543 continue;
544 cpu_hwcaps_ptrs[caps->capability] = caps;
545 }
546}
547
548static void __init init_cpu_hwcaps_indirect_list(void)
549{
550 init_cpu_hwcaps_indirect_list_from_array(arm64_features);
551 init_cpu_hwcaps_indirect_list_from_array(arm64_errata);
552}
553
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100554static void __init setup_boot_cpu_capabilities(void);
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +0100555
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100556void __init init_cpu_features(struct cpuinfo_arm64 *info)
557{
558 /* Before we start using the tables, make sure it is sorted */
559 sort_ftr_regs();
560
561 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
562 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
563 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
564 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
565 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
566 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
567 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
568 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
569 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000570 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100571 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
572 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
Dave Martin2e0f2472017-10-31 15:51:10 +0000573 init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100574
575 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
576 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
577 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
578 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
579 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
580 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
581 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
582 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
583 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
584 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
585 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
586 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
587 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
588 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
589 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
590 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
591 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
592 }
593
Dave Martin2e0f2472017-10-31 15:51:10 +0000594 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
595 init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
596 sve_init_vq_map();
597 }
Suzuki K Poulose5e911072018-03-26 15:12:29 +0100598
599 /*
Suzuki K Poulose82a3a212018-11-30 17:18:03 +0000600 * Initialize the indirect array of CPU hwcaps capabilities pointers
601 * before we handle the boot CPU below.
602 */
603 init_cpu_hwcaps_indirect_list();
604
605 /*
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100606 * Detect and enable early CPU capabilities based on the boot CPU,
607 * after we have initialised the CPU feature infrastructure.
Suzuki K Poulose5e911072018-03-26 15:12:29 +0100608 */
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +0100609 setup_boot_cpu_capabilities();
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100610}
611
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100612static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100613{
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100614 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100615
616 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
617 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
618 s64 ftr_new = arm64_ftr_value(ftrp, new);
619
620 if (ftr_cur == ftr_new)
621 continue;
622 /* Find a safe value */
623 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
624 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
625 }
626
627}
628
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100629static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100630{
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100631 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
632
633 BUG_ON(!regp);
634 update_cpu_ftr_reg(regp, val);
635 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
636 return 0;
637 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
638 regp->name, boot, cpu, val);
639 return 1;
640}
641
642/*
643 * Update system wide CPU feature registers with the values from a
644 * non-boot CPU. Also performs SANITY checks to make sure that there
645 * aren't any insane variations from that of the boot CPU.
646 */
647void update_cpu_features(int cpu,
648 struct cpuinfo_arm64 *info,
649 struct cpuinfo_arm64 *boot)
650{
651 int taint = 0;
652
653 /*
654 * The kernel can handle differing I-cache policies, but otherwise
655 * caches should look identical. Userspace JITs will make use of
656 * *minLine.
657 */
658 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
659 info->reg_ctr, boot->reg_ctr);
660
661 /*
662 * Userspace may perform DC ZVA instructions. Mismatched block sizes
663 * could result in too much or too little memory being zeroed if a
664 * process is preempted and migrated between CPUs.
665 */
666 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
667 info->reg_dczid, boot->reg_dczid);
668
669 /* If different, timekeeping will be broken (especially with KVM) */
670 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
671 info->reg_cntfrq, boot->reg_cntfrq);
672
673 /*
674 * The kernel uses self-hosted debug features and expects CPUs to
675 * support identical debug features. We presently need CTX_CMPs, WRPs,
676 * and BRPs to be identical.
677 * ID_AA64DFR1 is currently RES0.
678 */
679 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
680 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
681 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
682 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
683 /*
684 * Even in big.LITTLE, processors should be identical instruction-set
685 * wise.
686 */
687 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
688 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
689 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
690 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
691
692 /*
693 * Differing PARange support is fine as long as all peripherals and
694 * memory are mapped within the minimum PARange of all CPUs.
695 * Linux should not care about secure memory.
696 */
697 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
698 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
699 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
700 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000701 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
702 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100703
704 /*
705 * EL3 is not our concern.
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100706 */
707 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
708 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
709 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
710 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
711
Dave Martin2e0f2472017-10-31 15:51:10 +0000712 taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
713 info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
714
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100715 /*
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100716 * If we have AArch32, we care about 32-bit features for compat.
717 * If the system doesn't support AArch32, don't update them.
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100718 */
Dave Martin46823dd2017-03-23 15:14:39 +0000719 if (id_aa64pfr0_32bit_el0(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100720 id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
721
722 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100723 info->reg_id_dfr0, boot->reg_id_dfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100724 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100725 info->reg_id_isar0, boot->reg_id_isar0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100726 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100727 info->reg_id_isar1, boot->reg_id_isar1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100728 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100729 info->reg_id_isar2, boot->reg_id_isar2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100730 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100731 info->reg_id_isar3, boot->reg_id_isar3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100732 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100733 info->reg_id_isar4, boot->reg_id_isar4);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100734 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100735 info->reg_id_isar5, boot->reg_id_isar5);
736
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100737 /*
738 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
739 * ACTLR formats could differ across CPUs and therefore would have to
740 * be trapped for virtualization anyway.
741 */
742 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100743 info->reg_id_mmfr0, boot->reg_id_mmfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100744 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100745 info->reg_id_mmfr1, boot->reg_id_mmfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100746 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100747 info->reg_id_mmfr2, boot->reg_id_mmfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100748 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100749 info->reg_id_mmfr3, boot->reg_id_mmfr3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100750 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100751 info->reg_id_pfr0, boot->reg_id_pfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100752 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100753 info->reg_id_pfr1, boot->reg_id_pfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100754 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100755 info->reg_mvfr0, boot->reg_mvfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100756 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100757 info->reg_mvfr1, boot->reg_mvfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100758 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100759 info->reg_mvfr2, boot->reg_mvfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100760 }
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100761
Dave Martin2e0f2472017-10-31 15:51:10 +0000762 if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
763 taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
764 info->reg_zcr, boot->reg_zcr);
765
766 /* Probe vector lengths, unless we already gave up on SVE */
767 if (id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
768 !sys_caps_initialised)
769 sve_update_vq_map();
770 }
771
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100772 /*
773 * Mismatched CPU features are a recipe for disaster. Don't even
774 * pretend to support them.
775 */
Will Deacon8dd0ee62017-06-05 11:40:23 +0100776 if (taint) {
777 pr_warn_once("Unsupported CPU feature variation detected.\n");
778 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
779 }
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100780}
781
Dave Martin46823dd2017-03-23 15:14:39 +0000782u64 read_sanitised_ftr_reg(u32 id)
Suzuki K. Pouloseb3f15372015-10-19 14:24:47 +0100783{
784 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
785
786 /* We shouldn't get a request for an unsupported register */
787 BUG_ON(!regp);
788 return regp->sys_val;
789}
Marc Zyngier359b7062015-03-27 13:09:23 +0000790
Mark Rutland965861d2017-02-02 17:32:15 +0000791#define read_sysreg_case(r) \
792 case r: return read_sysreg_s(r)
793
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100794/*
Dave Martin46823dd2017-03-23 15:14:39 +0000795 * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100796 * Read the system register on the current CPU
797 */
Dave Martin46823dd2017-03-23 15:14:39 +0000798static u64 __read_sysreg_by_encoding(u32 sys_id)
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100799{
800 switch (sys_id) {
Mark Rutland965861d2017-02-02 17:32:15 +0000801 read_sysreg_case(SYS_ID_PFR0_EL1);
802 read_sysreg_case(SYS_ID_PFR1_EL1);
803 read_sysreg_case(SYS_ID_DFR0_EL1);
804 read_sysreg_case(SYS_ID_MMFR0_EL1);
805 read_sysreg_case(SYS_ID_MMFR1_EL1);
806 read_sysreg_case(SYS_ID_MMFR2_EL1);
807 read_sysreg_case(SYS_ID_MMFR3_EL1);
808 read_sysreg_case(SYS_ID_ISAR0_EL1);
809 read_sysreg_case(SYS_ID_ISAR1_EL1);
810 read_sysreg_case(SYS_ID_ISAR2_EL1);
811 read_sysreg_case(SYS_ID_ISAR3_EL1);
812 read_sysreg_case(SYS_ID_ISAR4_EL1);
813 read_sysreg_case(SYS_ID_ISAR5_EL1);
814 read_sysreg_case(SYS_MVFR0_EL1);
815 read_sysreg_case(SYS_MVFR1_EL1);
816 read_sysreg_case(SYS_MVFR2_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100817
Mark Rutland965861d2017-02-02 17:32:15 +0000818 read_sysreg_case(SYS_ID_AA64PFR0_EL1);
819 read_sysreg_case(SYS_ID_AA64PFR1_EL1);
820 read_sysreg_case(SYS_ID_AA64DFR0_EL1);
821 read_sysreg_case(SYS_ID_AA64DFR1_EL1);
822 read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
823 read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
824 read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
825 read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
826 read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100827
Mark Rutland965861d2017-02-02 17:32:15 +0000828 read_sysreg_case(SYS_CNTFRQ_EL0);
829 read_sysreg_case(SYS_CTR_EL0);
830 read_sysreg_case(SYS_DCZID_EL0);
831
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100832 default:
833 BUG();
834 return 0;
835 }
836}
837
Marc Zyngier963fcd42015-09-30 11:50:04 +0100838#include <linux/irqchip/arm-gic-v3.h>
839
Marc Zyngier94a9e042015-06-12 12:06:36 +0100840static bool
James Morse18ffa042015-07-21 13:23:29 +0100841feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
842{
Suzuki K Poulose28c5dcb2016-01-26 10:58:16 +0000843 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
James Morse18ffa042015-07-21 13:23:29 +0100844
845 return val >= entry->min_field_value;
846}
847
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100848static bool
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100849has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100850{
851 u64 val;
Marc Zyngier94a9e042015-06-12 12:06:36 +0100852
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100853 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
854 if (scope == SCOPE_SYSTEM)
Dave Martin46823dd2017-03-23 15:14:39 +0000855 val = read_sanitised_ftr_reg(entry->sys_reg);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100856 else
Dave Martin46823dd2017-03-23 15:14:39 +0000857 val = __read_sysreg_by_encoding(entry->sys_reg);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100858
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100859 return feature_matches(val, entry);
860}
James Morse338d4f42015-07-22 19:05:54 +0100861
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100862static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
Marc Zyngier963fcd42015-09-30 11:50:04 +0100863{
864 bool has_sre;
865
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100866 if (!has_cpuid_feature(entry, scope))
Marc Zyngier963fcd42015-09-30 11:50:04 +0100867 return false;
868
869 has_sre = gic_enable_sre();
870 if (!has_sre)
871 pr_warn_once("%s present but disabled by higher exception level\n",
872 entry->desc);
873
874 return has_sre;
875}
876
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100877static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
Will Deacond5370f72016-02-02 12:46:24 +0000878{
879 u32 midr = read_cpuid_id();
Will Deacond5370f72016-02-02 12:46:24 +0000880
881 /* Cavium ThunderX pass 1.x and 2.x */
Robert Richterfa5ce3d2017-01-13 14:12:09 +0100882 return MIDR_IS_CPU_MODEL_RANGE(midr, MIDR_THUNDERX,
883 MIDR_CPU_VAR_REV(0, 0),
884 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
Will Deacond5370f72016-02-02 12:46:24 +0000885}
886
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000887static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
888{
Dave Martin46823dd2017-03-23 15:14:39 +0000889 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000890
891 return cpuid_feature_extract_signed_field(pfr0,
892 ID_AA64PFR0_FP_SHIFT) < 0;
893}
894
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600895static bool has_cache_idc(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +0100896 int scope)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600897{
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +0100898 u64 ctr;
899
900 if (scope == SCOPE_SYSTEM)
901 ctr = arm64_ftr_reg_ctrel0.sys_val;
902 else
Suzuki K Poulose1602df02018-10-09 14:47:06 +0100903 ctr = read_cpuid_effective_cachetype();
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +0100904
905 return ctr & BIT(CTR_IDC_SHIFT);
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600906}
907
Suzuki K Poulose1602df02018-10-09 14:47:06 +0100908static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused)
909{
910 /*
911 * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively
912 * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses
913 * to the CTR_EL0 on this CPU and emulate it with the real/safe
914 * value.
915 */
916 if (!(read_cpuid_cachetype() & BIT(CTR_IDC_SHIFT)))
917 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
918}
919
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600920static bool has_cache_dic(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +0100921 int scope)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600922{
Suzuki K Poulose8ab66cb2018-10-09 14:47:05 +0100923 u64 ctr;
924
925 if (scope == SCOPE_SYSTEM)
926 ctr = arm64_ftr_reg_ctrel0.sys_val;
927 else
928 ctr = read_cpuid_cachetype();
929
930 return ctr & BIT(CTR_DIC_SHIFT);
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600931}
932
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +0100933static bool __maybe_unused
934has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
935{
936 /*
937 * Kdump isn't guaranteed to power-off all secondary CPUs, CNP
938 * may share TLB entries with a CPU stuck in the crashed
939 * kernel.
940 */
941 if (is_kdump_kernel())
942 return false;
943
944 return has_cpuid_feature(entry, scope);
945}
946
Will Deaconea1e3de2017-11-14 14:38:19 +0000947#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
948static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
949
950static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
Suzuki K Poulosed3aec8a2018-03-26 15:12:40 +0100951 int scope)
Will Deaconea1e3de2017-11-14 14:38:19 +0000952{
Suzuki K Poulosebe5b2992018-03-26 15:12:45 +0100953 /* List of CPUs that are not vulnerable and don't need KPTI */
954 static const struct midr_range kpti_safe_list[] = {
955 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
956 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
Will Deacon2a355ec2018-12-13 13:47:38 +0000957 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
958 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
959 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
960 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
961 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
962 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
Mark Rutland71c751f2018-04-23 11:41:33 +0100963 { /* sentinel */ }
Suzuki K Poulosebe5b2992018-03-26 15:12:45 +0100964 };
Marc Zyngier6dc52b12018-01-29 11:59:56 +0000965 char const *str = "command line option";
Will Deacon179a56f2017-11-27 18:29:30 +0000966
Marc Zyngier6dc52b12018-01-29 11:59:56 +0000967 /*
968 * For reasons that aren't entirely clear, enabling KPTI on Cavium
969 * ThunderX leads to apparent I-cache corruption of kernel text, which
970 * ends as well as you might imagine. Don't even try.
971 */
972 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
973 str = "ARM64_WORKAROUND_CAVIUM_27456";
974 __kpti_forced = -1;
975 }
976
977 /* Forced? */
Will Deaconea1e3de2017-11-14 14:38:19 +0000978 if (__kpti_forced) {
Marc Zyngier6dc52b12018-01-29 11:59:56 +0000979 pr_info_once("kernel page table isolation forced %s by %s\n",
980 __kpti_forced > 0 ? "ON" : "OFF", str);
Will Deaconea1e3de2017-11-14 14:38:19 +0000981 return __kpti_forced > 0;
982 }
983
984 /* Useful for KASLR robustness */
985 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
986 return true;
987
Jayachandran C0ba2e292018-01-19 04:22:48 -0800988 /* Don't force KPTI for CPUs that are not vulnerable */
Suzuki K Poulosebe5b2992018-03-26 15:12:45 +0100989 if (is_midr_in_range_list(read_cpuid_id(), kpti_safe_list))
Jayachandran C0ba2e292018-01-19 04:22:48 -0800990 return false;
Jayachandran C0ba2e292018-01-19 04:22:48 -0800991
Will Deacon179a56f2017-11-27 18:29:30 +0000992 /* Defer to CPU feature registers */
Suzuki K Poulosed3aec8a2018-03-26 15:12:40 +0100993 return !has_cpuid_feature(entry, scope);
Will Deaconea1e3de2017-11-14 14:38:19 +0000994}
995
Dave Martinc0cda3b2018-03-26 15:12:28 +0100996static void
997kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
Will Deaconf992b4d2018-02-06 22:22:50 +0000998{
999 typedef void (kpti_remap_fn)(int, int, phys_addr_t);
1000 extern kpti_remap_fn idmap_kpti_install_ng_mappings;
1001 kpti_remap_fn *remap_fn;
1002
1003 static bool kpti_applied = false;
1004 int cpu = smp_processor_id();
1005
1006 if (kpti_applied)
Dave Martinc0cda3b2018-03-26 15:12:28 +01001007 return;
Will Deaconf992b4d2018-02-06 22:22:50 +00001008
1009 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
1010
1011 cpu_install_idmap();
1012 remap_fn(cpu, num_online_cpus(), __pa_symbol(swapper_pg_dir));
1013 cpu_uninstall_idmap();
1014
1015 if (!cpu)
1016 kpti_applied = true;
1017
Dave Martinc0cda3b2018-03-26 15:12:28 +01001018 return;
Will Deaconf992b4d2018-02-06 22:22:50 +00001019}
1020
Will Deaconea1e3de2017-11-14 14:38:19 +00001021static int __init parse_kpti(char *str)
1022{
1023 bool enabled;
1024 int ret = strtobool(str, &enabled);
1025
1026 if (ret)
1027 return ret;
1028
1029 __kpti_forced = enabled ? 1 : -1;
1030 return 0;
1031}
Will Deaconb5b7dd62018-06-22 10:25:25 +01001032early_param("kpti", parse_kpti);
Will Deaconea1e3de2017-11-14 14:38:19 +00001033#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1034
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001035#ifdef CONFIG_ARM64_HW_AFDBM
1036static inline void __cpu_enable_hw_dbm(void)
1037{
1038 u64 tcr = read_sysreg(tcr_el1) | TCR_HD;
1039
1040 write_sysreg(tcr, tcr_el1);
1041 isb();
1042}
1043
Suzuki K Pouloseece13972018-03-26 15:12:49 +01001044static bool cpu_has_broken_dbm(void)
1045{
1046 /* List of CPUs which have broken DBM support. */
1047 static const struct midr_range cpus[] = {
1048#ifdef CONFIG_ARM64_ERRATUM_1024718
1049 MIDR_RANGE(MIDR_CORTEX_A55, 0, 0, 1, 0), // A55 r0p0 -r1p0
1050#endif
1051 {},
1052 };
1053
1054 return is_midr_in_range_list(read_cpuid_id(), cpus);
1055}
1056
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001057static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap)
1058{
Suzuki K Pouloseece13972018-03-26 15:12:49 +01001059 return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) &&
1060 !cpu_has_broken_dbm();
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001061}
1062
1063static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap)
1064{
1065 if (cpu_can_use_dbm(cap))
1066 __cpu_enable_hw_dbm();
1067}
1068
1069static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap,
1070 int __unused)
1071{
1072 static bool detected = false;
1073 /*
1074 * DBM is a non-conflicting feature. i.e, the kernel can safely
1075 * run a mix of CPUs with and without the feature. So, we
1076 * unconditionally enable the capability to allow any late CPU
1077 * to use the feature. We only enable the control bits on the
1078 * CPU, if it actually supports.
1079 *
1080 * We have to make sure we print the "feature" detection only
1081 * when at least one CPU actually uses it. So check if this CPU
1082 * can actually use it and print the message exactly once.
1083 *
1084 * This is safe as all CPUs (including secondary CPUs - due to the
1085 * LOCAL_CPU scope - and the hotplugged CPUs - via verification)
1086 * goes through the "matches" check exactly once. Also if a CPU
1087 * matches the criteria, it is guaranteed that the CPU will turn
1088 * the DBM on, as the capability is unconditionally enabled.
1089 */
1090 if (!detected && cpu_can_use_dbm(cap)) {
1091 detected = true;
1092 pr_info("detected: Hardware dirty bit management\n");
1093 }
1094
1095 return true;
1096}
1097
1098#endif
1099
Will Deacon12eb3692018-03-27 11:51:12 +01001100#ifdef CONFIG_ARM64_VHE
1101static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
1102{
1103 return is_kernel_in_hyp_mode();
1104}
1105
Dave Martinc0cda3b2018-03-26 15:12:28 +01001106static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
James Morse6d99b682018-01-08 15:38:06 +00001107{
1108 /*
1109 * Copy register values that aren't redirected by hardware.
1110 *
1111 * Before code patching, we only set tpidr_el1, all CPUs need to copy
1112 * this value to tpidr_el2 before we patch the code. Once we've done
1113 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
1114 * do anything here.
1115 */
1116 if (!alternatives_applied)
1117 write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
James Morse6d99b682018-01-08 15:38:06 +00001118}
Will Deacon12eb3692018-03-27 11:51:12 +01001119#endif
James Morse6d99b682018-01-08 15:38:06 +00001120
Marc Zyngiere48d53a2018-04-06 12:27:28 +01001121static void cpu_has_fwb(const struct arm64_cpu_capabilities *__unused)
1122{
1123 u64 val = read_sysreg_s(SYS_CLIDR_EL1);
1124
1125 /* Check that CLIDR_EL1.LOU{U,IS} are both 0 */
1126 WARN_ON(val & (7 << 27 | 7 << 21));
1127}
1128
Will Deacon8f04e8e2018-08-07 13:47:06 +01001129#ifdef CONFIG_ARM64_SSBD
1130static int ssbs_emulation_handler(struct pt_regs *regs, u32 instr)
1131{
1132 if (user_mode(regs))
1133 return 1;
1134
Suzuki K Poulose74e24822018-09-16 23:17:23 +01001135 if (instr & BIT(PSTATE_Imm_shift))
Will Deacon8f04e8e2018-08-07 13:47:06 +01001136 regs->pstate |= PSR_SSBS_BIT;
1137 else
1138 regs->pstate &= ~PSR_SSBS_BIT;
1139
1140 arm64_skip_faulting_instruction(regs, 4);
1141 return 0;
1142}
1143
1144static struct undef_hook ssbs_emulation_hook = {
Suzuki K Poulose74e24822018-09-16 23:17:23 +01001145 .instr_mask = ~(1U << PSTATE_Imm_shift),
1146 .instr_val = 0xd500401f | PSTATE_SSBS,
Will Deacon8f04e8e2018-08-07 13:47:06 +01001147 .fn = ssbs_emulation_handler,
1148};
1149
1150static void cpu_enable_ssbs(const struct arm64_cpu_capabilities *__unused)
1151{
1152 static bool undef_hook_registered = false;
1153 static DEFINE_SPINLOCK(hook_lock);
1154
1155 spin_lock(&hook_lock);
1156 if (!undef_hook_registered) {
1157 register_undef_hook(&ssbs_emulation_hook);
1158 undef_hook_registered = true;
1159 }
1160 spin_unlock(&hook_lock);
1161
1162 if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) {
1163 sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
1164 arm64_set_ssbd_mitigation(false);
1165 } else {
1166 arm64_set_ssbd_mitigation(true);
1167 }
1168}
1169#endif /* CONFIG_ARM64_SSBD */
1170
Will Deaconb8925ee2018-08-07 13:53:41 +01001171#ifdef CONFIG_ARM64_PAN
1172static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
1173{
1174 /*
1175 * We modify PSTATE. This won't work from irq context as the PSTATE
1176 * is discarded once we return from the exception.
1177 */
1178 WARN_ON_ONCE(in_interrupt());
1179
1180 sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
1181 asm(SET_PSTATE_PAN(1));
1182}
1183#endif /* CONFIG_ARM64_PAN */
1184
1185#ifdef CONFIG_ARM64_RAS_EXTN
1186static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
1187{
1188 /* Firmware may have left a deferred SError in this register. */
1189 write_sysreg_s(0, SYS_DISR_EL1);
1190}
1191#endif /* CONFIG_ARM64_RAS_EXTN */
1192
Mark Rutland6984eb42018-12-07 18:39:24 +00001193#ifdef CONFIG_ARM64_PTR_AUTH
1194static bool has_address_auth(const struct arm64_cpu_capabilities *entry,
1195 int __unused)
1196{
1197 u64 isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
1198 bool api, apa;
1199
1200 apa = cpuid_feature_extract_unsigned_field(isar1,
1201 ID_AA64ISAR1_APA_SHIFT) > 0;
1202 api = cpuid_feature_extract_unsigned_field(isar1,
1203 ID_AA64ISAR1_API_SHIFT) > 0;
1204
1205 return apa || api;
1206}
1207
1208static bool has_generic_auth(const struct arm64_cpu_capabilities *entry,
1209 int __unused)
1210{
1211 u64 isar1 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR1_EL1);
1212 bool gpi, gpa;
1213
1214 gpa = cpuid_feature_extract_unsigned_field(isar1,
1215 ID_AA64ISAR1_GPA_SHIFT) > 0;
1216 gpi = cpuid_feature_extract_unsigned_field(isar1,
1217 ID_AA64ISAR1_GPI_SHIFT) > 0;
1218
1219 return gpa || gpi;
1220}
1221#endif /* CONFIG_ARM64_PTR_AUTH */
1222
Marc Zyngier359b7062015-03-27 13:09:23 +00001223static const struct arm64_cpu_capabilities arm64_features[] = {
Marc Zyngier94a9e042015-06-12 12:06:36 +01001224 {
1225 .desc = "GIC system register CPU interface",
1226 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001227 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Marc Zyngier963fcd42015-09-30 11:50:04 +01001228 .matches = has_useable_gicv3_cpuif,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001229 .sys_reg = SYS_ID_AA64PFR0_EL1,
1230 .field_pos = ID_AA64PFR0_GIC_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001231 .sign = FTR_UNSIGNED,
James Morse18ffa042015-07-21 13:23:29 +01001232 .min_field_value = 1,
Marc Zyngier94a9e042015-06-12 12:06:36 +01001233 },
James Morse338d4f42015-07-22 19:05:54 +01001234#ifdef CONFIG_ARM64_PAN
1235 {
1236 .desc = "Privileged Access Never",
1237 .capability = ARM64_HAS_PAN,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001238 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001239 .matches = has_cpuid_feature,
1240 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1241 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001242 .sign = FTR_UNSIGNED,
James Morse338d4f42015-07-22 19:05:54 +01001243 .min_field_value = 1,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001244 .cpu_enable = cpu_enable_pan,
James Morse338d4f42015-07-22 19:05:54 +01001245 },
1246#endif /* CONFIG_ARM64_PAN */
Will Deacon2e94da12015-07-27 16:23:58 +01001247#if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
1248 {
1249 .desc = "LSE atomic instructions",
1250 .capability = ARM64_HAS_LSE_ATOMICS,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001251 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +01001252 .matches = has_cpuid_feature,
1253 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1254 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001255 .sign = FTR_UNSIGNED,
Will Deacon2e94da12015-07-27 16:23:58 +01001256 .min_field_value = 2,
1257 },
1258#endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
Marc Zyngierd88701b2015-01-29 11:24:05 +00001259 {
Will Deacond5370f72016-02-02 12:46:24 +00001260 .desc = "Software prefetching using PRFM",
1261 .capability = ARM64_HAS_NO_HW_PREFETCH,
Suzuki K Poulose5c137712018-03-26 15:12:39 +01001262 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
Will Deacond5370f72016-02-02 12:46:24 +00001263 .matches = has_no_hw_prefetch,
1264 },
James Morse57f49592016-02-05 14:58:48 +00001265#ifdef CONFIG_ARM64_UAO
1266 {
1267 .desc = "User Access Override",
1268 .capability = ARM64_HAS_UAO,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001269 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse57f49592016-02-05 14:58:48 +00001270 .matches = has_cpuid_feature,
1271 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1272 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
1273 .min_field_value = 1,
James Morsec8b06e32017-01-09 18:14:02 +00001274 /*
1275 * We rely on stop_machine() calling uao_thread_switch() to set
1276 * UAO immediately after patching.
1277 */
James Morse57f49592016-02-05 14:58:48 +00001278 },
1279#endif /* CONFIG_ARM64_UAO */
James Morse70544192016-02-05 14:58:50 +00001280#ifdef CONFIG_ARM64_PAN
1281 {
1282 .capability = ARM64_ALT_PAN_NOT_UAO,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001283 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse70544192016-02-05 14:58:50 +00001284 .matches = cpufeature_pan_not_uao,
1285 },
1286#endif /* CONFIG_ARM64_PAN */
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001287#ifdef CONFIG_ARM64_VHE
Linus Torvalds588ab3f2016-03-17 20:03:47 -07001288 {
Marc Zyngierd88701b2015-01-29 11:24:05 +00001289 .desc = "Virtualization Host Extensions",
1290 .capability = ARM64_HAS_VIRT_HOST_EXTN,
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001291 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
Marc Zyngierd88701b2015-01-29 11:24:05 +00001292 .matches = runs_at_el2,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001293 .cpu_enable = cpu_copy_el2regs,
Marc Zyngierd88701b2015-01-29 11:24:05 +00001294 },
Suzuki K Poulose830dcc92018-03-26 15:12:42 +01001295#endif /* CONFIG_ARM64_VHE */
Suzuki K Poulose042446a2016-04-18 10:28:36 +01001296 {
1297 .desc = "32-bit EL0 Support",
1298 .capability = ARM64_HAS_32BIT_EL0,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001299 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K Poulose042446a2016-04-18 10:28:36 +01001300 .matches = has_cpuid_feature,
1301 .sys_reg = SYS_ID_AA64PFR0_EL1,
1302 .sign = FTR_UNSIGNED,
1303 .field_pos = ID_AA64PFR0_EL0_SHIFT,
1304 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
1305 },
Will Deaconea1e3de2017-11-14 14:38:19 +00001306#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
1307 {
Will Deacon179a56f2017-11-27 18:29:30 +00001308 .desc = "Kernel page table isolation (KPTI)",
Will Deaconea1e3de2017-11-14 14:38:19 +00001309 .capability = ARM64_UNMAP_KERNEL_AT_EL0,
Suzuki K Poulosed3aec8a2018-03-26 15:12:40 +01001310 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
1311 /*
1312 * The ID feature fields below are used to indicate that
1313 * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for
1314 * more details.
1315 */
1316 .sys_reg = SYS_ID_AA64PFR0_EL1,
1317 .field_pos = ID_AA64PFR0_CSV3_SHIFT,
1318 .min_field_value = 1,
Will Deaconea1e3de2017-11-14 14:38:19 +00001319 .matches = unmap_kernel_at_el0,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001320 .cpu_enable = kpti_install_ng_mappings,
Will Deaconea1e3de2017-11-14 14:38:19 +00001321 },
1322#endif
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001323 {
1324 /* FP/SIMD is not implemented */
1325 .capability = ARM64_HAS_NO_FPSIMD,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001326 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001327 .min_field_value = 0,
1328 .matches = has_no_fpsimd,
1329 },
Robin Murphyd50e0712017-07-25 11:55:42 +01001330#ifdef CONFIG_ARM64_PMEM
1331 {
1332 .desc = "Data cache clean to Point of Persistence",
1333 .capability = ARM64_HAS_DCPOP,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001334 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Robin Murphyd50e0712017-07-25 11:55:42 +01001335 .matches = has_cpuid_feature,
1336 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1337 .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1338 .min_field_value = 1,
1339 },
1340#endif
Dave Martin43994d82017-10-31 15:51:19 +00001341#ifdef CONFIG_ARM64_SVE
1342 {
1343 .desc = "Scalable Vector Extension",
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001344 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Dave Martin43994d82017-10-31 15:51:19 +00001345 .capability = ARM64_SVE,
Dave Martin43994d82017-10-31 15:51:19 +00001346 .sys_reg = SYS_ID_AA64PFR0_EL1,
1347 .sign = FTR_UNSIGNED,
1348 .field_pos = ID_AA64PFR0_SVE_SHIFT,
1349 .min_field_value = ID_AA64PFR0_SVE,
1350 .matches = has_cpuid_feature,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001351 .cpu_enable = sve_kernel_enable,
Dave Martin43994d82017-10-31 15:51:19 +00001352 },
1353#endif /* CONFIG_ARM64_SVE */
Xie XiuQi64c02722018-01-15 19:38:56 +00001354#ifdef CONFIG_ARM64_RAS_EXTN
1355 {
1356 .desc = "RAS Extension Support",
1357 .capability = ARM64_HAS_RAS_EXTN,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001358 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Xie XiuQi64c02722018-01-15 19:38:56 +00001359 .matches = has_cpuid_feature,
1360 .sys_reg = SYS_ID_AA64PFR0_EL1,
1361 .sign = FTR_UNSIGNED,
1362 .field_pos = ID_AA64PFR0_RAS_SHIFT,
1363 .min_field_value = ID_AA64PFR0_RAS_V1,
Dave Martinc0cda3b2018-03-26 15:12:28 +01001364 .cpu_enable = cpu_clear_disr,
Xie XiuQi64c02722018-01-15 19:38:56 +00001365 },
1366#endif /* CONFIG_ARM64_RAS_EXTN */
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001367 {
1368 .desc = "Data cache clean to the PoU not required for I/D coherence",
1369 .capability = ARM64_HAS_CACHE_IDC,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001370 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001371 .matches = has_cache_idc,
Suzuki K Poulose1602df02018-10-09 14:47:06 +01001372 .cpu_enable = cpu_emulate_effective_ctr,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001373 },
1374 {
1375 .desc = "Instruction cache invalidation not required for I/D coherence",
1376 .capability = ARM64_HAS_CACHE_DIC,
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001377 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001378 .matches = has_cache_dic,
1379 },
Marc Zyngiere48d53a2018-04-06 12:27:28 +01001380 {
1381 .desc = "Stage-2 Force Write-Back",
1382 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1383 .capability = ARM64_HAS_STAGE2_FWB,
1384 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1385 .sign = FTR_UNSIGNED,
1386 .field_pos = ID_AA64MMFR2_FWB_SHIFT,
1387 .min_field_value = 1,
1388 .matches = has_cpuid_feature,
1389 .cpu_enable = cpu_has_fwb,
1390 },
Suzuki K Poulose05abb592018-03-26 15:12:48 +01001391#ifdef CONFIG_ARM64_HW_AFDBM
1392 {
1393 /*
1394 * Since we turn this on always, we don't want the user to
1395 * think that the feature is available when it may not be.
1396 * So hide the description.
1397 *
1398 * .desc = "Hardware pagetable Dirty Bit Management",
1399 *
1400 */
1401 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1402 .capability = ARM64_HW_DBM,
1403 .sys_reg = SYS_ID_AA64MMFR1_EL1,
1404 .sign = FTR_UNSIGNED,
1405 .field_pos = ID_AA64MMFR1_HADBS_SHIFT,
1406 .min_field_value = 2,
1407 .matches = has_hw_dbm,
1408 .cpu_enable = cpu_enable_hw_dbm,
1409 },
1410#endif
Will Deacon8f04e8e2018-08-07 13:47:06 +01001411#ifdef CONFIG_ARM64_SSBD
Ard Biesheuvel86d0dd32018-08-27 13:02:43 +02001412 {
1413 .desc = "CRC32 instructions",
1414 .capability = ARM64_HAS_CRC32,
1415 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1416 .matches = has_cpuid_feature,
1417 .sys_reg = SYS_ID_AA64ISAR0_EL1,
1418 .field_pos = ID_AA64ISAR0_CRC32_SHIFT,
1419 .min_field_value = 1,
1420 },
Will Deacond71be2b2018-06-15 11:37:34 +01001421 {
1422 .desc = "Speculative Store Bypassing Safe (SSBS)",
1423 .capability = ARM64_SSBS,
1424 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1425 .matches = has_cpuid_feature,
1426 .sys_reg = SYS_ID_AA64PFR1_EL1,
1427 .field_pos = ID_AA64PFR1_SSBS_SHIFT,
1428 .sign = FTR_UNSIGNED,
1429 .min_field_value = ID_AA64PFR1_SSBS_PSTATE_ONLY,
Will Deacon8f04e8e2018-08-07 13:47:06 +01001430 .cpu_enable = cpu_enable_ssbs,
Will Deacond71be2b2018-06-15 11:37:34 +01001431 },
Will Deacon8f04e8e2018-08-07 13:47:06 +01001432#endif
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01001433#ifdef CONFIG_ARM64_CNP
1434 {
1435 .desc = "Common not Private translations",
1436 .capability = ARM64_HAS_CNP,
1437 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1438 .matches = has_useable_cnp,
1439 .sys_reg = SYS_ID_AA64MMFR2_EL1,
1440 .sign = FTR_UNSIGNED,
1441 .field_pos = ID_AA64MMFR2_CNP_SHIFT,
1442 .min_field_value = 1,
1443 .cpu_enable = cpu_enable_cnp,
1444 },
1445#endif
Will Deaconbd4fb6d2018-06-14 11:21:34 +01001446 {
1447 .desc = "Speculation barrier (SB)",
1448 .capability = ARM64_HAS_SB,
1449 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1450 .matches = has_cpuid_feature,
1451 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1452 .field_pos = ID_AA64ISAR1_SB_SHIFT,
1453 .sign = FTR_UNSIGNED,
1454 .min_field_value = 1,
1455 },
Mark Rutland6984eb42018-12-07 18:39:24 +00001456#ifdef CONFIG_ARM64_PTR_AUTH
1457 {
1458 .desc = "Address authentication (architected algorithm)",
1459 .capability = ARM64_HAS_ADDRESS_AUTH_ARCH,
1460 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1461 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1462 .sign = FTR_UNSIGNED,
1463 .field_pos = ID_AA64ISAR1_APA_SHIFT,
1464 .min_field_value = ID_AA64ISAR1_APA_ARCHITECTED,
1465 .matches = has_cpuid_feature,
1466 },
1467 {
1468 .desc = "Address authentication (IMP DEF algorithm)",
1469 .capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF,
1470 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1471 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1472 .sign = FTR_UNSIGNED,
1473 .field_pos = ID_AA64ISAR1_API_SHIFT,
1474 .min_field_value = ID_AA64ISAR1_API_IMP_DEF,
1475 .matches = has_cpuid_feature,
1476 },
1477 {
1478 .capability = ARM64_HAS_ADDRESS_AUTH,
1479 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1480 .matches = has_address_auth,
1481 },
1482 {
1483 .desc = "Generic authentication (architected algorithm)",
1484 .capability = ARM64_HAS_GENERIC_AUTH_ARCH,
1485 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1486 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1487 .sign = FTR_UNSIGNED,
1488 .field_pos = ID_AA64ISAR1_GPA_SHIFT,
1489 .min_field_value = ID_AA64ISAR1_GPA_ARCHITECTED,
1490 .matches = has_cpuid_feature,
1491 },
1492 {
1493 .desc = "Generic authentication (IMP DEF algorithm)",
1494 .capability = ARM64_HAS_GENERIC_AUTH_IMP_DEF,
1495 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1496 .sys_reg = SYS_ID_AA64ISAR1_EL1,
1497 .sign = FTR_UNSIGNED,
1498 .field_pos = ID_AA64ISAR1_GPI_SHIFT,
1499 .min_field_value = ID_AA64ISAR1_GPI_IMP_DEF,
1500 .matches = has_cpuid_feature,
1501 },
1502 {
1503 .capability = ARM64_HAS_GENERIC_AUTH,
1504 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1505 .matches = has_generic_auth,
1506 },
1507#endif /* CONFIG_ARM64_PTR_AUTH */
Marc Zyngier359b7062015-03-27 13:09:23 +00001508 {},
1509};
1510
Suzuki K Poulose143ba052018-03-26 15:12:31 +01001511#define HWCAP_CAP(reg, field, s, min_value, cap_type, cap) \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001512 { \
1513 .desc = #cap, \
Suzuki K Poulose5b4747c2018-03-26 15:12:32 +01001514 .type = ARM64_CPUCAP_SYSTEM_FEATURE, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001515 .matches = has_cpuid_feature, \
1516 .sys_reg = reg, \
1517 .field_pos = field, \
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001518 .sign = s, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001519 .min_field_value = min_value, \
Suzuki K Poulose143ba052018-03-26 15:12:31 +01001520 .hwcap_type = cap_type, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001521 .hwcap = cap, \
1522 }
1523
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001524static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001525 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_PMULL),
1526 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_AES),
1527 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA1),
1528 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA2),
Suzuki K Poulosef5e035f2017-10-11 14:01:02 +01001529 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_SHA512),
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001530 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_CRC32),
1531 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ATOMICS),
Suzuki K Poulosef92f5ce02017-01-12 16:37:28 +00001532 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RDM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDRDM),
Suzuki K Poulosef5e035f2017-10-11 14:01:02 +01001533 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA3),
1534 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM3),
1535 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM4),
1536 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_DP_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDDP),
Dongjiu Geng3b3b6812017-12-13 18:13:56 +08001537 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_FHM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDFHM),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +00001538 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_FLAGM),
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001539 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_FP),
Suzuki K Poulosebf500612016-01-26 15:52:46 +00001540 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_FPHP),
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001541 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_ASIMD),
Suzuki K Poulosebf500612016-01-26 15:52:46 +00001542 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_ASIMDHP),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +00001543 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_DIT_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_DIT),
Robin Murphy7aac4052017-07-25 11:55:40 +01001544 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DPB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_DCPOP),
Suzuki K Poulosec8c37982017-03-14 18:13:25 +00001545 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_JSCVT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_JSCVT),
Suzuki K Poulosecb567e72017-03-14 18:13:26 +00001546 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FCMA_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_FCMA),
Suzuki K Poulosec651aae2017-03-14 18:13:27 +00001547 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_LRCPC),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +00001548 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ILRCPC),
Will Deaconbd4fb6d2018-06-14 11:21:34 +01001549 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_SB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SB),
Suzuki K Poulose7206dc92018-03-12 10:04:14 +00001550 HWCAP_CAP(SYS_ID_AA64MMFR2_EL1, ID_AA64MMFR2_AT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_USCAT),
Dave Martin43994d82017-10-31 15:51:19 +00001551#ifdef CONFIG_ARM64_SVE
1552 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_SVE_SHIFT, FTR_UNSIGNED, ID_AA64PFR0_SVE, CAP_HWCAP, HWCAP_SVE),
1553#endif
Will Deacond71be2b2018-06-15 11:37:34 +01001554 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_SSBS_SHIFT, FTR_UNSIGNED, ID_AA64PFR1_SSBS_PSTATE_INSNS, CAP_HWCAP, HWCAP_SSBS),
Suzuki K Poulose75283502016-04-18 10:28:33 +01001555 {},
1556};
1557
1558static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001559#ifdef CONFIG_COMPAT
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001560 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
1561 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
1562 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
1563 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
1564 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 +01001565#endif
1566 {},
1567};
1568
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001569static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001570{
1571 switch (cap->hwcap_type) {
1572 case CAP_HWCAP:
1573 elf_hwcap |= cap->hwcap;
1574 break;
1575#ifdef CONFIG_COMPAT
1576 case CAP_COMPAT_HWCAP:
1577 compat_elf_hwcap |= (u32)cap->hwcap;
1578 break;
1579 case CAP_COMPAT_HWCAP2:
1580 compat_elf_hwcap2 |= (u32)cap->hwcap;
1581 break;
1582#endif
1583 default:
1584 WARN_ON(1);
1585 break;
1586 }
1587}
1588
1589/* Check if we have a particular HWCAP enabled */
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001590static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001591{
1592 bool rc;
1593
1594 switch (cap->hwcap_type) {
1595 case CAP_HWCAP:
1596 rc = (elf_hwcap & cap->hwcap) != 0;
1597 break;
1598#ifdef CONFIG_COMPAT
1599 case CAP_COMPAT_HWCAP:
1600 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
1601 break;
1602 case CAP_COMPAT_HWCAP2:
1603 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
1604 break;
1605#endif
1606 default:
1607 WARN_ON(1);
1608 rc = false;
1609 }
1610
1611 return rc;
1612}
1613
Suzuki K Poulose75283502016-04-18 10:28:33 +01001614static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001615{
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00001616 /* We support emulation of accesses to CPU ID feature registers */
1617 elf_hwcap |= HWCAP_CPUID;
Suzuki K Poulose75283502016-04-18 10:28:33 +01001618 for (; hwcaps->matches; hwcaps++)
Suzuki K Poulose143ba052018-03-26 15:12:31 +01001619 if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
Suzuki K Poulose75283502016-04-18 10:28:33 +01001620 cap_set_elf_hwcap(hwcaps);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001621}
1622
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001623static void update_cpu_capabilities(u16 scope_mask)
Marc Zyngier359b7062015-03-27 13:09:23 +00001624{
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001625 int i;
1626 const struct arm64_cpu_capabilities *caps;
1627
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01001628 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001629 for (i = 0; i < ARM64_NCAPS; i++) {
1630 caps = cpu_hwcaps_ptrs[i];
1631 if (!caps || !(caps->type & scope_mask) ||
1632 cpus_have_cap(caps->capability) ||
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01001633 !caps->matches(caps, cpucap_default_scope(caps)))
Marc Zyngier359b7062015-03-27 13:09:23 +00001634 continue;
1635
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001636 if (caps->desc)
1637 pr_info("detected: %s\n", caps->desc);
Suzuki K Poulose75283502016-04-18 10:28:33 +01001638 cpus_set_cap(caps->capability);
Marc Zyngier359b7062015-03-27 13:09:23 +00001639 }
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001640}
James Morse1c076302015-07-21 13:23:28 +01001641
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001642/*
1643 * Enable all the available capabilities on this CPU. The capabilities
1644 * with BOOT_CPU scope are handled separately and hence skipped here.
1645 */
1646static int cpu_enable_non_boot_scope_capabilities(void *__unused)
Dave Martinc0cda3b2018-03-26 15:12:28 +01001647{
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001648 int i;
1649 u16 non_boot_scope = SCOPE_ALL & ~SCOPE_BOOT_CPU;
Dave Martinc0cda3b2018-03-26 15:12:28 +01001650
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001651 for_each_available_cap(i) {
1652 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[i];
1653
1654 if (WARN_ON(!cap))
1655 continue;
1656
1657 if (!(cap->type & non_boot_scope))
1658 continue;
1659
1660 if (cap->cpu_enable)
1661 cap->cpu_enable(cap);
1662 }
Dave Martinc0cda3b2018-03-26 15:12:28 +01001663 return 0;
1664}
1665
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001666/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001667 * Run through the enabled capabilities and enable() it on all active
1668 * CPUs
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001669 */
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001670static void __init enable_cpu_capabilities(u16 scope_mask)
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001671{
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001672 int i;
1673 const struct arm64_cpu_capabilities *caps;
1674 bool boot_scope;
Mark Rutland63a1e1c2017-05-16 15:18:05 +01001675
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001676 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
1677 boot_scope = !!(scope_mask & SCOPE_BOOT_CPU);
1678
1679 for (i = 0; i < ARM64_NCAPS; i++) {
1680 unsigned int num;
1681
1682 caps = cpu_hwcaps_ptrs[i];
1683 if (!caps || !(caps->type & scope_mask))
1684 continue;
1685 num = caps->capability;
1686 if (!cpus_have_cap(num))
Mark Rutland63a1e1c2017-05-16 15:18:05 +01001687 continue;
1688
1689 /* Ensure cpus_have_const_cap(num) works */
1690 static_branch_enable(&cpu_hwcap_keys[num]);
1691
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001692 if (boot_scope && caps->cpu_enable)
James Morse2a6dcb22016-10-18 11:27:46 +01001693 /*
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01001694 * Capabilities with SCOPE_BOOT_CPU scope are finalised
1695 * before any secondary CPU boots. Thus, each secondary
1696 * will enable the capability as appropriate via
1697 * check_local_cpu_capabilities(). The only exception is
1698 * the boot CPU, for which the capability must be
1699 * enabled here. This approach avoids costly
1700 * stop_machine() calls for this case.
James Morse2a6dcb22016-10-18 11:27:46 +01001701 */
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001702 caps->cpu_enable(caps);
Mark Rutland63a1e1c2017-05-16 15:18:05 +01001703 }
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001704
Suzuki K Poulose0b587c842018-11-30 17:18:06 +00001705 /*
1706 * For all non-boot scope capabilities, use stop_machine()
1707 * as it schedules the work allowing us to modify PSTATE,
1708 * instead of on_each_cpu() which uses an IPI, giving us a
1709 * PSTATE that disappears when we return.
1710 */
1711 if (!boot_scope)
1712 stop_machine(cpu_enable_non_boot_scope_capabilities,
1713 NULL, cpu_online_mask);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01001714}
1715
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001716/*
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01001717 * Run through the list of capabilities to check for conflicts.
1718 * If the system has already detected a capability, take necessary
1719 * action on this CPU.
1720 *
1721 * Returns "false" on conflicts.
1722 */
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001723static bool verify_local_cpu_caps(u16 scope_mask)
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01001724{
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001725 int i;
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01001726 bool cpu_has_cap, system_has_cap;
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001727 const struct arm64_cpu_capabilities *caps;
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01001728
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01001729 scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
1730
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001731 for (i = 0; i < ARM64_NCAPS; i++) {
1732 caps = cpu_hwcaps_ptrs[i];
1733 if (!caps || !(caps->type & scope_mask))
Suzuki K Poulosecce360b2018-03-26 15:12:34 +01001734 continue;
1735
Suzuki K Pouloseba7d9232018-03-26 15:12:46 +01001736 cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU);
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01001737 system_has_cap = cpus_have_cap(caps->capability);
1738
1739 if (system_has_cap) {
1740 /*
1741 * Check if the new CPU misses an advertised feature,
1742 * which is not safe to miss.
1743 */
1744 if (!cpu_has_cap && !cpucap_late_cpu_optional(caps))
1745 break;
1746 /*
1747 * We have to issue cpu_enable() irrespective of
1748 * whether the CPU has it or not, as it is enabeld
1749 * system wide. It is upto the call back to take
1750 * appropriate action on this CPU.
1751 */
1752 if (caps->cpu_enable)
1753 caps->cpu_enable(caps);
1754 } else {
1755 /*
1756 * Check if the CPU has this capability if it isn't
1757 * safe to have when the system doesn't.
1758 */
1759 if (cpu_has_cap && !cpucap_late_cpu_permitted(caps))
1760 break;
1761 }
1762 }
1763
Suzuki K Poulose606f8e72018-11-30 17:18:05 +00001764 if (i < ARM64_NCAPS) {
Suzuki K Pouloseeaac4d82018-03-26 15:12:33 +01001765 pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n",
1766 smp_processor_id(), caps->capability,
1767 caps->desc, system_has_cap, cpu_has_cap);
1768 return false;
1769 }
1770
1771 return true;
1772}
1773
1774/*
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001775 * Check for CPU features that are used in early boot
1776 * based on the Boot CPU value.
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001777 */
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001778static void check_early_cpu_features(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00001779{
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001780 verify_cpu_asid_bits();
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01001781 /*
1782 * Early features are used by the kernel already. If there
1783 * is a conflict, we cannot proceed further.
1784 */
1785 if (!verify_local_cpu_caps(SCOPE_BOOT_CPU))
1786 cpu_panic_kernel();
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001787}
1788
Suzuki K Poulose75283502016-04-18 10:28:33 +01001789static void
1790verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
1791{
1792
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001793 for (; caps->matches; caps++)
1794 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01001795 pr_crit("CPU%d: missing HWCAP: %s\n",
1796 smp_processor_id(), caps->desc);
1797 cpu_die_early();
1798 }
Suzuki K Poulose75283502016-04-18 10:28:33 +01001799}
1800
Dave Martin2e0f2472017-10-31 15:51:10 +00001801static void verify_sve_features(void)
1802{
1803 u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
1804 u64 zcr = read_zcr_features();
1805
1806 unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
1807 unsigned int len = zcr & ZCR_ELx_LEN_MASK;
1808
1809 if (len < safe_len || sve_verify_vq_map()) {
1810 pr_crit("CPU%d: SVE: required vector length(s) missing\n",
1811 smp_processor_id());
1812 cpu_die_early();
1813 }
1814
1815 /* Add checks on other ZCR bits here if necessary */
1816}
1817
Suzuki K Poulose1e89bae2018-03-26 15:12:30 +01001818
1819/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001820 * Run through the enabled system capabilities and enable() it on this CPU.
1821 * The capabilities were decided based on the available CPUs at the boot time.
1822 * Any new CPU should match the system wide status of the capability. If the
1823 * new CPU doesn't have a capability which the system now has enabled, we
1824 * cannot do anything to fix it up and could cause unexpected failures. So
1825 * we park the CPU.
1826 */
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001827static void verify_local_cpu_capabilities(void)
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001828{
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01001829 /*
1830 * The capabilities with SCOPE_BOOT_CPU are checked from
1831 * check_early_cpu_features(), as they need to be verified
1832 * on all secondary CPUs.
1833 */
1834 if (!verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU))
Suzuki K Poulose600b9c92018-03-26 15:12:35 +01001835 cpu_die_early();
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01001836
Suzuki K Poulose75283502016-04-18 10:28:33 +01001837 verify_local_elf_hwcaps(arm64_elf_hwcaps);
Dave Martin2e0f2472017-10-31 15:51:10 +00001838
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001839 if (system_supports_32bit_el0())
1840 verify_local_elf_hwcaps(compat_elf_hwcaps);
Dave Martin2e0f2472017-10-31 15:51:10 +00001841
1842 if (system_supports_sve())
1843 verify_sve_features();
Marc Zyngier359b7062015-03-27 13:09:23 +00001844}
1845
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001846void check_local_cpu_capabilities(void)
1847{
1848 /*
1849 * All secondary CPUs should conform to the early CPU features
1850 * in use by the kernel based on boot CPU.
1851 */
1852 check_early_cpu_features();
1853
1854 /*
1855 * If we haven't finalised the system capabilities, this CPU gets
Suzuki K Poulosefbd890b2018-03-26 15:12:37 +01001856 * a chance to update the errata work arounds and local features.
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001857 * Otherwise, this CPU should verify that it has all the system
1858 * advertised capabilities.
1859 */
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01001860 if (!sys_caps_initialised)
1861 update_cpu_capabilities(SCOPE_LOCAL_CPU);
1862 else
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001863 verify_local_cpu_capabilities();
1864}
1865
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01001866static void __init setup_boot_cpu_capabilities(void)
1867{
1868 /* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */
1869 update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU);
1870 /* Enable the SCOPE_BOOT_CPU capabilities alone right away */
1871 enable_cpu_capabilities(SCOPE_BOOT_CPU);
1872}
1873
Mark Rutland63a1e1c2017-05-16 15:18:05 +01001874DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
1875EXPORT_SYMBOL(arm64_const_caps_ready);
1876
1877static void __init mark_const_caps_ready(void)
1878{
1879 static_branch_enable(&arm64_const_caps_ready);
1880}
1881
Suzuki K Poulosef7bfc142018-11-30 17:18:04 +00001882bool this_cpu_has_cap(unsigned int n)
Marc Zyngier8f4137582017-01-30 15:39:52 +00001883{
Suzuki K Poulosef7bfc142018-11-30 17:18:04 +00001884 if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
1885 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
1886
1887 if (cap)
1888 return cap->matches(cap, SCOPE_LOCAL_CPU);
1889 }
1890
1891 return false;
Marc Zyngier8f4137582017-01-30 15:39:52 +00001892}
1893
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01001894static void __init setup_system_capabilities(void)
1895{
1896 /*
1897 * We have finalised the system-wide safe feature
1898 * registers, finalise the capabilities that depend
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01001899 * on it. Also enable all the available capabilities,
1900 * that are not enabled already.
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01001901 */
1902 update_cpu_capabilities(SCOPE_SYSTEM);
Suzuki K Poulosefd9d63d2018-03-26 15:12:41 +01001903 enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01001904}
1905
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001906void __init setup_cpu_features(void)
1907{
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001908 u32 cwg;
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001909
Suzuki K Pouloseed478b32018-03-26 15:12:38 +01001910 setup_system_capabilities();
Mark Rutland63a1e1c2017-05-16 15:18:05 +01001911 mark_const_caps_ready();
Suzuki K Poulose75283502016-04-18 10:28:33 +01001912 setup_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001913
1914 if (system_supports_32bit_el0())
1915 setup_elf_hwcaps(compat_elf_hwcaps);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001916
Kees Cook2e6f5492018-02-21 10:18:21 -08001917 if (system_uses_ttbr0_pan())
1918 pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
1919
Dave Martin2e0f2472017-10-31 15:51:10 +00001920 sve_setup();
Dave Martin94b07c12018-06-01 11:10:14 +01001921 minsigstksz_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +00001922
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001923 /* Advertise that we have computed the system capabilities */
1924 set_sys_caps_initialised();
1925
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001926 /*
1927 * Check for sane CTR_EL0.CWG value.
1928 */
1929 cwg = cache_type_cwg();
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001930 if (!cwg)
Catalin Marinasebc7e212018-05-11 13:33:12 +01001931 pr_warn("No Cache Writeback Granule information, assuming %d\n",
1932 ARCH_DMA_MINALIGN);
Marc Zyngier359b7062015-03-27 13:09:23 +00001933}
James Morse70544192016-02-05 14:58:50 +00001934
1935static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001936cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
James Morse70544192016-02-05 14:58:50 +00001937{
Suzuki K Poulosea4023f682016-11-08 13:56:20 +00001938 return (cpus_have_const_cap(ARM64_HAS_PAN) && !cpus_have_const_cap(ARM64_HAS_UAO));
James Morse70544192016-02-05 14:58:50 +00001939}
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00001940
Vladimir Murzin5ffdfae2018-07-31 14:08:56 +01001941static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap)
1942{
1943 cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
1944}
1945
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00001946/*
1947 * We emulate only the following system register space.
1948 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 4 - 7]
1949 * See Table C5-6 System instruction encodings for System register accesses,
1950 * ARMv8 ARM(ARM DDI 0487A.f) for more details.
1951 */
1952static inline bool __attribute_const__ is_emulated(u32 id)
1953{
1954 return (sys_reg_Op0(id) == 0x3 &&
1955 sys_reg_CRn(id) == 0x0 &&
1956 sys_reg_Op1(id) == 0x0 &&
1957 (sys_reg_CRm(id) == 0 ||
1958 ((sys_reg_CRm(id) >= 4) && (sys_reg_CRm(id) <= 7))));
1959}
1960
1961/*
1962 * With CRm == 0, reg should be one of :
1963 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
1964 */
1965static inline int emulate_id_reg(u32 id, u64 *valp)
1966{
1967 switch (id) {
1968 case SYS_MIDR_EL1:
1969 *valp = read_cpuid_id();
1970 break;
1971 case SYS_MPIDR_EL1:
1972 *valp = SYS_MPIDR_SAFE_VAL;
1973 break;
1974 case SYS_REVIDR_EL1:
1975 /* IMPLEMENTATION DEFINED values are emulated with 0 */
1976 *valp = 0;
1977 break;
1978 default:
1979 return -EINVAL;
1980 }
1981
1982 return 0;
1983}
1984
1985static int emulate_sys_reg(u32 id, u64 *valp)
1986{
1987 struct arm64_ftr_reg *regp;
1988
1989 if (!is_emulated(id))
1990 return -EINVAL;
1991
1992 if (sys_reg_CRm(id) == 0)
1993 return emulate_id_reg(id, valp);
1994
1995 regp = get_arm64_ftr_reg(id);
1996 if (regp)
1997 *valp = arm64_ftr_reg_user_value(regp);
1998 else
1999 /*
2000 * The untracked registers are either IMPLEMENTATION DEFINED
2001 * (e.g, ID_AFR0_EL1) or reserved RAZ.
2002 */
2003 *valp = 0;
2004 return 0;
2005}
2006
Anshuman Khandual520ad982018-09-20 09:36:20 +05302007int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt)
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002008{
2009 int rc;
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002010 u64 val;
2011
Anshuman Khandual520ad982018-09-20 09:36:20 +05302012 rc = emulate_sys_reg(sys_reg, &val);
2013 if (!rc) {
2014 pt_regs_write_reg(regs, rt, val);
2015 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
2016 }
2017 return rc;
2018}
2019
2020static int emulate_mrs(struct pt_regs *regs, u32 insn)
2021{
2022 u32 sys_reg, rt;
2023
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002024 /*
2025 * sys_reg values are defined as used in mrs/msr instruction.
2026 * shift the imm value to get the encoding.
2027 */
2028 sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
Anshuman Khandual520ad982018-09-20 09:36:20 +05302029 rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
2030 return do_emulate_mrs(regs, sys_reg, rt);
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002031}
2032
2033static struct undef_hook mrs_hook = {
2034 .instr_mask = 0xfff00000,
2035 .instr_val = 0xd5300000,
Mark Rutlandd64567f2018-07-05 15:16:52 +01002036 .pstate_mask = PSR_AA32_MODE_MASK,
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00002037 .pstate_val = PSR_MODE_EL0t,
2038 .fn = emulate_mrs,
2039};
2040
2041static int __init enable_mrs_emulation(void)
2042{
2043 register_undef_hook(&mrs_hook);
2044 return 0;
2045}
2046
Suzuki K Poulosec0d88322017-10-06 14:16:52 +01002047core_initcall(enable_mrs_emulation);