blob: 88f015dc504749ff5fcf286113275bbdd6595c66 [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>
James Morse1451b7f2022-04-06 17:45:41 +010023#include <linux/percpu.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 Abbottf8fee94e2017-01-10 13:35:49 -080027#include <linux/mm.h>
James Morse1451b7f2022-04-06 17:45:41 +010028
Marc Zyngier359b7062015-03-27 13:09:23 +000029#include <asm/cpu.h>
30#include <asm/cpufeature.h>
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +010031#include <asm/cpu_ops.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>
James Morse1451b7f2022-04-06 17:45:41 +010035#include <asm/vectors.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 Marinas005bf1a2016-07-01 16:53:00 +010054EXPORT_SYMBOL(cpu_hwcaps);
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010055
James Morse1451b7f2022-04-06 17:45:41 +010056DEFINE_PER_CPU_READ_MOSTLY(const char *, this_cpu_vector) = vectors;
57
Catalin Marinasefd9e032016-09-05 18:25:48 +010058DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
59EXPORT_SYMBOL(cpu_hwcap_keys);
60
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000061#define __ARM64_FTR_BITS(SIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010062 { \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000063 .sign = SIGNED, \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010064 .strict = STRICT, \
65 .type = TYPE, \
66 .shift = SHIFT, \
67 .width = WIDTH, \
68 .safe_val = SAFE_VAL, \
69 }
70
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +000071/* Define a feature with unsigned values */
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000072#define ARM64_FTR_BITS(STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000073 __ARM64_FTR_BITS(FTR_UNSIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
74
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +000075/* Define a feature with a signed value */
76#define S_ARM64_FTR_BITS(STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
77 __ARM64_FTR_BITS(FTR_SIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
78
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010079#define ARM64_FTR_END \
80 { \
81 .width = 0, \
82 }
83
James Morse70544192016-02-05 14:58:50 +000084/* meta feature for alternatives */
85static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +010086cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
87
James Morse70544192016-02-05 14:58:50 +000088
Ard Biesheuvel5e49d732016-08-31 11:31:08 +010089static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
Suzuki K Pouloseba62b302017-10-11 14:01:02 +010090 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_DP_SHIFT, 4, 0),
91 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
92 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
93 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010094 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
95 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0),
96 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
97 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
98 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
99 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
100 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
101 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
102 ARM64_FTR_END,
103};
104
James Morse9396d5e2022-04-06 17:45:45 +0100105static const struct arm64_ftr_bits ftr_id_aa64isar2[] = {
James Morse7815cbf2022-04-06 17:45:46 +0100106 ARM64_FTR_BITS(FTR_STRICT, FTR_HIGHER_SAFE, ID_AA64ISAR2_CLEARBHB_SHIFT, 4, 0),
James Morse9396d5e2022-04-06 17:45:45 +0100107 ARM64_FTR_END,
108};
109
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100110static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
Will Deacon73547722018-04-03 12:09:14 +0100111 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
Mark Rutland47320012018-04-12 12:11:13 +0100112 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
113 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 24, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100114 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0),
115 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_GIC_SHIFT, 4, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000116 S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
117 S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100118 /* Linux doesn't care about the EL3 */
119 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64PFR0_EL3_SHIFT, 4, 0),
120 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL2_SHIFT, 4, 0),
121 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
122 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
123 ARM64_FTR_END,
124};
125
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100126static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100127 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000128 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
129 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100130 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
131 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
132 /* Linux shouldn't care about secure memory */
133 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
134 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
135 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
136 /*
137 * Differing PARange is fine as long as all peripherals and memory are mapped
138 * within the minimum PARange of all CPUs
139 */
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000140 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100141 ARM64_FTR_END,
142};
143
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100144static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100145 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
146 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
147 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
148 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
149 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
150 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
151 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
152 ARM64_FTR_END,
153};
154
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100155static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
Kefeng Wang7d7b4ae2016-03-25 17:30:07 +0800156 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
157 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
158 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000159 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
Kefeng Wang7d7b4ae2016-03-25 17:30:07 +0800160 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000161 ARM64_FTR_END,
162};
163
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100164static const struct arm64_ftr_bits ftr_ctr[] = {
Will Deacone364e9a2019-08-05 18:13:54 +0100165 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
166 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 30, 1, 0),
167 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 29, 1, 1), /* DIC */
168 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 1, 1), /* IDC */
Will Deacon3c5dbb92019-08-05 18:13:55 +0100169 ARM64_FTR_BITS(FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, 24, 4, 0), /* CWG */
170 ARM64_FTR_BITS(FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, 20, 4, 0), /* ERG */
Suzuki K Poulosea6830092018-07-04 23:07:45 +0100171 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, CTR_DMINLINE_SHIFT, 4, 1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100172 /*
173 * Linux can handle differing I-cache policies. Userspace JITs will
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100174 * make use of *minLine.
175 * If we have differing I-cache policies, report it as the weakest - AIVIVT.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100176 */
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100177 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_AIVIVT), /* L1Ip */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100178 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 10, 0), /* RAZ */
Suzuki K Poulosea6830092018-07-04 23:07:45 +0100179 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, CTR_IMINLINE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100180 ARM64_FTR_END,
181};
182
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100183struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
184 .name = "SYS_CTR_EL0",
185 .ftr_bits = ftr_ctr
186};
187
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100188static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000189 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0xf), /* InnerShr */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100190 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0), /* FCSE */
191 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
192 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 4, 0), /* TCM */
193 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* ShareLvl */
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000194 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0xf), /* OuterShr */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100195 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* PMSA */
196 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* VMSA */
197 ARM64_FTR_END,
198};
199
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100200static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100201 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000202 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
203 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
204 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
205 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
206 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
207 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
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_mvfr2[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100212 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
213 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* FPMisc */
214 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* SIMDMisc */
215 ARM64_FTR_END,
216};
217
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100218static const struct arm64_ftr_bits ftr_dczid[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100219 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 5, 27, 0), /* RAZ */
220 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
221 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
222 ARM64_FTR_END,
223};
224
225
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100226static const struct arm64_ftr_bits ftr_id_isar5[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100227 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_RDM_SHIFT, 4, 0),
228 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 20, 4, 0), /* RAZ */
229 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_CRC32_SHIFT, 4, 0),
230 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA2_SHIFT, 4, 0),
231 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA1_SHIFT, 4, 0),
232 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_AES_SHIFT, 4, 0),
233 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SEVL_SHIFT, 4, 0),
234 ARM64_FTR_END,
235};
236
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100237static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100238 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
239 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* ac2 */
240 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
241 ARM64_FTR_END,
242};
243
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100244static const struct arm64_ftr_bits ftr_id_pfr0[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100245 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 16, 0), /* RAZ */
246 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* State3 */
247 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0), /* State2 */
248 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* State1 */
249 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* State0 */
250 ARM64_FTR_END,
251};
252
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100253static const struct arm64_ftr_bits ftr_id_dfr0[] = {
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000254 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000255 S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000256 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
257 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
258 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
259 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
260 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
261 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
262 ARM64_FTR_END,
263};
264
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100265/*
266 * Common ftr bits for a 32bit register with all hidden, strict
267 * attributes, with 4bit feature fields and a default safe value of
268 * 0. Covers the following 32bit registers:
269 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
270 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100271static const struct arm64_ftr_bits ftr_generic_32bits[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100272 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
273 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
274 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
275 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
276 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
277 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
278 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
279 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
280 ARM64_FTR_END,
281};
282
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100283static const struct arm64_ftr_bits ftr_generic[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100284 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 64, 0),
285 ARM64_FTR_END,
286};
287
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100288static const struct arm64_ftr_bits ftr_generic32[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100289 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 32, 0),
290 ARM64_FTR_END,
291};
292
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100293static const struct arm64_ftr_bits ftr_aa64raz[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100294 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 64, 0),
295 ARM64_FTR_END,
296};
297
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100298#define ARM64_FTR_REG(id, table) { \
299 .sys_id = id, \
300 .reg = &(struct arm64_ftr_reg){ \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100301 .name = #id, \
302 .ftr_bits = &((table)[0]), \
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100303 }}
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100304
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100305static const struct __ftr_reg_entry {
306 u32 sys_id;
307 struct arm64_ftr_reg *reg;
308} arm64_ftr_regs[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100309
310 /* Op1 = 0, CRn = 0, CRm = 1 */
311 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
312 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000313 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100314 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
315 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
316 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
317 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
318
319 /* Op1 = 0, CRn = 0, CRm = 2 */
320 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
321 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
322 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
323 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
324 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
325 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
326 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
327
328 /* Op1 = 0, CRn = 0, CRm = 3 */
329 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
330 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
331 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
332
333 /* Op1 = 0, CRn = 0, CRm = 4 */
334 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
335 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_aa64raz),
336
337 /* Op1 = 0, CRn = 0, CRm = 5 */
338 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
339 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_generic),
340
341 /* Op1 = 0, CRn = 0, CRm = 6 */
342 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
343 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_aa64raz),
James Morse9396d5e2022-04-06 17:45:45 +0100344 ARM64_FTR_REG(SYS_ID_AA64ISAR2_EL1, ftr_id_aa64isar2),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100345
346 /* Op1 = 0, CRn = 0, CRm = 7 */
347 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
348 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
James Morse406e3082016-02-05 14:58:47 +0000349 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100350
351 /* Op1 = 3, CRn = 0, CRm = 0 */
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100352 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100353 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
354
355 /* Op1 = 3, CRn = 14, CRm = 0 */
356 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_generic32),
357};
358
359static int search_cmp_ftr_reg(const void *id, const void *regp)
360{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100361 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100362}
363
364/*
365 * get_arm64_ftr_reg - Lookup a feature register entry using its
366 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
367 * ascending order of sys_id , we use binary search to find a matching
368 * entry.
369 *
370 * returns - Upon success, matching ftr_reg entry for id.
371 * - NULL on failure. It is upto the caller to decide
372 * the impact of a failure.
373 */
374static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
375{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100376 const struct __ftr_reg_entry *ret;
377
378 ret = bsearch((const void *)(unsigned long)sys_id,
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100379 arm64_ftr_regs,
380 ARRAY_SIZE(arm64_ftr_regs),
381 sizeof(arm64_ftr_regs[0]),
382 search_cmp_ftr_reg);
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100383 if (ret)
384 return ret->reg;
385 return NULL;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100386}
387
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100388static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
389 s64 ftr_val)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100390{
391 u64 mask = arm64_ftr_mask(ftrp);
392
393 reg &= ~mask;
394 reg |= (ftr_val << ftrp->shift) & mask;
395 return reg;
396}
397
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100398static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
399 s64 cur)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100400{
401 s64 ret = 0;
402
403 switch (ftrp->type) {
404 case FTR_EXACT:
405 ret = ftrp->safe_val;
406 break;
407 case FTR_LOWER_SAFE:
408 ret = new < cur ? new : cur;
409 break;
Will Deacon3c5dbb92019-08-05 18:13:55 +0100410 case FTR_HIGHER_OR_ZERO_SAFE:
411 if (!cur || !new)
412 break;
413 /* Fallthrough */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100414 case FTR_HIGHER_SAFE:
415 ret = new > cur ? new : cur;
416 break;
417 default:
418 BUG();
419 }
420
421 return ret;
422}
423
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100424static void __init sort_ftr_regs(void)
425{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100426 int i;
427
428 /* Check that the array is sorted so that we can do the binary search */
429 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
430 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100431}
432
433/*
434 * Initialise the CPU feature register from Boot CPU values.
435 * Also initiliases the strict_mask for the register.
436 */
437static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
438{
439 u64 val = 0;
440 u64 strict_mask = ~0x0ULL;
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100441 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100442 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
443
444 BUG_ON(!reg);
445
446 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
447 s64 ftr_new = arm64_ftr_value(ftrp, new);
448
449 val = arm64_ftr_set_value(ftrp, val, ftr_new);
450 if (!ftrp->strict)
451 strict_mask &= ~arm64_ftr_mask(ftrp);
452 }
453 reg->sys_val = val;
454 reg->strict_mask = strict_mask;
455}
456
Suzuki K Poulose454f7ba2022-04-06 17:45:09 +0100457extern const struct arm64_cpu_capabilities arm64_errata[];
458static void update_cpu_errata_workarounds(void);
459
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100460void __init init_cpu_features(struct cpuinfo_arm64 *info)
461{
462 /* Before we start using the tables, make sure it is sorted */
463 sort_ftr_regs();
464
465 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
466 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
467 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
468 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
469 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
470 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
471 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
James Morse9396d5e2022-04-06 17:45:45 +0100472 init_cpu_ftr_reg(SYS_ID_AA64ISAR2_EL1, info->reg_id_aa64isar2);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100473 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
474 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000475 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100476 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
477 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100478
479 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
480 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
481 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
482 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
483 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
484 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
485 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
486 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
487 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
488 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
489 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
490 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
491 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
492 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
493 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
494 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
495 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
496 }
497
Suzuki K Pouloseb63ccc32022-04-06 17:45:08 +0100498 /*
499 * Run the errata work around checks on the boot CPU, once we have
500 * initialised the cpu feature infrastructure.
501 */
502 update_cpu_errata_workarounds();
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100503}
504
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100505static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100506{
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100507 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100508
509 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
510 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
511 s64 ftr_new = arm64_ftr_value(ftrp, new);
512
513 if (ftr_cur == ftr_new)
514 continue;
515 /* Find a safe value */
516 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
517 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
518 }
519
520}
521
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100522static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100523{
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100524 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
525
526 BUG_ON(!regp);
527 update_cpu_ftr_reg(regp, val);
528 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
529 return 0;
530 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
531 regp->name, boot, cpu, val);
532 return 1;
533}
534
535/*
536 * Update system wide CPU feature registers with the values from a
537 * non-boot CPU. Also performs SANITY checks to make sure that there
538 * aren't any insane variations from that of the boot CPU.
539 */
540void update_cpu_features(int cpu,
541 struct cpuinfo_arm64 *info,
542 struct cpuinfo_arm64 *boot)
543{
544 int taint = 0;
545
546 /*
547 * The kernel can handle differing I-cache policies, but otherwise
548 * caches should look identical. Userspace JITs will make use of
549 * *minLine.
550 */
551 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
552 info->reg_ctr, boot->reg_ctr);
553
554 /*
555 * Userspace may perform DC ZVA instructions. Mismatched block sizes
556 * could result in too much or too little memory being zeroed if a
557 * process is preempted and migrated between CPUs.
558 */
559 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
560 info->reg_dczid, boot->reg_dczid);
561
562 /* If different, timekeeping will be broken (especially with KVM) */
563 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
564 info->reg_cntfrq, boot->reg_cntfrq);
565
566 /*
567 * The kernel uses self-hosted debug features and expects CPUs to
568 * support identical debug features. We presently need CTX_CMPs, WRPs,
569 * and BRPs to be identical.
570 * ID_AA64DFR1 is currently RES0.
571 */
572 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
573 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
574 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
575 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
576 /*
577 * Even in big.LITTLE, processors should be identical instruction-set
578 * wise.
579 */
580 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
581 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
582 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
583 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
James Morse9396d5e2022-04-06 17:45:45 +0100584 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR2_EL1, cpu,
585 info->reg_id_aa64isar2, boot->reg_id_aa64isar2);
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100586
587 /*
588 * Differing PARange support is fine as long as all peripherals and
589 * memory are mapped within the minimum PARange of all CPUs.
590 * Linux should not care about secure memory.
591 */
592 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
593 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
594 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
595 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000596 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
597 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100598
599 /*
600 * EL3 is not our concern.
601 * ID_AA64PFR1 is currently RES0.
602 */
603 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
604 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
605 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
606 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
607
608 /*
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100609 * If we have AArch32, we care about 32-bit features for compat.
610 * If the system doesn't support AArch32, don't update them.
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100611 */
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100612 if (id_aa64pfr0_32bit_el0(read_system_reg(SYS_ID_AA64PFR0_EL1)) &&
613 id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
614
615 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100616 info->reg_id_dfr0, boot->reg_id_dfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100617 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100618 info->reg_id_isar0, boot->reg_id_isar0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100619 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100620 info->reg_id_isar1, boot->reg_id_isar1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100621 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100622 info->reg_id_isar2, boot->reg_id_isar2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100623 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100624 info->reg_id_isar3, boot->reg_id_isar3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100625 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100626 info->reg_id_isar4, boot->reg_id_isar4);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100627 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100628 info->reg_id_isar5, boot->reg_id_isar5);
629
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100630 /*
631 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
632 * ACTLR formats could differ across CPUs and therefore would have to
633 * be trapped for virtualization anyway.
634 */
635 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100636 info->reg_id_mmfr0, boot->reg_id_mmfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100637 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100638 info->reg_id_mmfr1, boot->reg_id_mmfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100639 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100640 info->reg_id_mmfr2, boot->reg_id_mmfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100641 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100642 info->reg_id_mmfr3, boot->reg_id_mmfr3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100643 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100644 info->reg_id_pfr0, boot->reg_id_pfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100645 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100646 info->reg_id_pfr1, boot->reg_id_pfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100647 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100648 info->reg_mvfr0, boot->reg_mvfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100649 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100650 info->reg_mvfr1, boot->reg_mvfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100651 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100652 info->reg_mvfr2, boot->reg_mvfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100653 }
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100654
655 /*
656 * Mismatched CPU features are a recipe for disaster. Don't even
657 * pretend to support them.
658 */
659 WARN_TAINT_ONCE(taint, TAINT_CPU_OUT_OF_SPEC,
660 "Unsupported CPU feature variation.\n");
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100661}
662
Suzuki K. Pouloseb3f15372015-10-19 14:24:47 +0100663u64 read_system_reg(u32 id)
664{
665 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
666
667 /* We shouldn't get a request for an unsupported register */
668 BUG_ON(!regp);
669 return regp->sys_val;
670}
Marc Zyngier359b7062015-03-27 13:09:23 +0000671
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100672/*
673 * __raw_read_system_reg() - Used by a STARTING cpu before cpuinfo is populated.
674 * Read the system register on the current CPU
675 */
676static u64 __raw_read_system_reg(u32 sys_id)
677{
678 switch (sys_id) {
679 case SYS_ID_PFR0_EL1: return read_cpuid(ID_PFR0_EL1);
680 case SYS_ID_PFR1_EL1: return read_cpuid(ID_PFR1_EL1);
681 case SYS_ID_DFR0_EL1: return read_cpuid(ID_DFR0_EL1);
682 case SYS_ID_MMFR0_EL1: return read_cpuid(ID_MMFR0_EL1);
683 case SYS_ID_MMFR1_EL1: return read_cpuid(ID_MMFR1_EL1);
684 case SYS_ID_MMFR2_EL1: return read_cpuid(ID_MMFR2_EL1);
685 case SYS_ID_MMFR3_EL1: return read_cpuid(ID_MMFR3_EL1);
686 case SYS_ID_ISAR0_EL1: return read_cpuid(ID_ISAR0_EL1);
687 case SYS_ID_ISAR1_EL1: return read_cpuid(ID_ISAR1_EL1);
688 case SYS_ID_ISAR2_EL1: return read_cpuid(ID_ISAR2_EL1);
689 case SYS_ID_ISAR3_EL1: return read_cpuid(ID_ISAR3_EL1);
690 case SYS_ID_ISAR4_EL1: return read_cpuid(ID_ISAR4_EL1);
Mark Rutland7127d432017-02-02 17:32:14 +0000691 case SYS_ID_ISAR5_EL1: return read_cpuid(ID_ISAR5_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100692 case SYS_MVFR0_EL1: return read_cpuid(MVFR0_EL1);
693 case SYS_MVFR1_EL1: return read_cpuid(MVFR1_EL1);
694 case SYS_MVFR2_EL1: return read_cpuid(MVFR2_EL1);
695
696 case SYS_ID_AA64PFR0_EL1: return read_cpuid(ID_AA64PFR0_EL1);
Mark Rutland7127d432017-02-02 17:32:14 +0000697 case SYS_ID_AA64PFR1_EL1: return read_cpuid(ID_AA64PFR1_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100698 case SYS_ID_AA64DFR0_EL1: return read_cpuid(ID_AA64DFR0_EL1);
Mark Rutland7127d432017-02-02 17:32:14 +0000699 case SYS_ID_AA64DFR1_EL1: return read_cpuid(ID_AA64DFR1_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100700 case SYS_ID_AA64MMFR0_EL1: return read_cpuid(ID_AA64MMFR0_EL1);
701 case SYS_ID_AA64MMFR1_EL1: return read_cpuid(ID_AA64MMFR1_EL1);
702 case SYS_ID_AA64MMFR2_EL1: return read_cpuid(ID_AA64MMFR2_EL1);
703 case SYS_ID_AA64ISAR0_EL1: return read_cpuid(ID_AA64ISAR0_EL1);
704 case SYS_ID_AA64ISAR1_EL1: return read_cpuid(ID_AA64ISAR1_EL1);
James Morse9396d5e2022-04-06 17:45:45 +0100705 case SYS_ID_AA64ISAR2_EL1: return read_cpuid(ID_AA64ISAR2_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100706
707 case SYS_CNTFRQ_EL0: return read_cpuid(CNTFRQ_EL0);
708 case SYS_CTR_EL0: return read_cpuid(CTR_EL0);
709 case SYS_DCZID_EL0: return read_cpuid(DCZID_EL0);
710 default:
711 BUG();
712 return 0;
713 }
714}
715
Marc Zyngier963fcd42015-09-30 11:50:04 +0100716#include <linux/irqchip/arm-gic-v3.h>
717
Marc Zyngier94a9e042015-06-12 12:06:36 +0100718static bool
James Morse18ffa042015-07-21 13:23:29 +0100719feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
720{
Suzuki K Poulose28c5dcb2016-01-26 10:58:16 +0000721 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
James Morse18ffa042015-07-21 13:23:29 +0100722
723 return val >= entry->min_field_value;
724}
725
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100726static bool
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100727has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100728{
729 u64 val;
Marc Zyngier94a9e042015-06-12 12:06:36 +0100730
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100731 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
732 if (scope == SCOPE_SYSTEM)
733 val = read_system_reg(entry->sys_reg);
734 else
735 val = __raw_read_system_reg(entry->sys_reg);
736
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100737 return feature_matches(val, entry);
738}
James Morse338d4f42015-07-22 19:05:54 +0100739
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100740static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
Marc Zyngier963fcd42015-09-30 11:50:04 +0100741{
742 bool has_sre;
743
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100744 if (!has_cpuid_feature(entry, scope))
Marc Zyngier963fcd42015-09-30 11:50:04 +0100745 return false;
746
747 has_sre = gic_enable_sre();
748 if (!has_sre)
749 pr_warn_once("%s present but disabled by higher exception level\n",
750 entry->desc);
751
752 return has_sre;
753}
754
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100755static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
Will Deacond5370f72016-02-02 12:46:24 +0000756{
757 u32 midr = read_cpuid_id();
Will Deacond5370f72016-02-02 12:46:24 +0000758
759 /* Cavium ThunderX pass 1.x and 2.x */
Robert Richter6e1ad7a2022-04-06 17:45:04 +0100760 return MIDR_IS_CPU_MODEL_RANGE(midr, MIDR_THUNDERX,
761 MIDR_CPU_VAR_REV(0, 0),
762 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
Will Deacond5370f72016-02-02 12:46:24 +0000763}
764
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100765static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
Marc Zyngierd88701b2015-01-29 11:24:05 +0000766{
767 return is_kernel_in_hyp_mode();
768}
769
Marc Zyngierd1745912016-06-30 18:40:42 +0100770static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry,
771 int __unused)
772{
Laura Abbottf8fee94e2017-01-10 13:35:49 -0800773 phys_addr_t idmap_addr = __pa_symbol(__hyp_idmap_text_start);
Marc Zyngierd1745912016-06-30 18:40:42 +0100774
775 /*
776 * Activate the lower HYP offset only if:
777 * - the idmap doesn't clash with it,
778 * - the kernel is not running at EL2.
779 */
780 return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode();
781}
782
Will Deaconf79ff2d2017-11-14 14:38:19 +0000783#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
784static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
785
786static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
787 int __unused)
788{
Suzuki K Poulosec6815502022-04-06 17:45:14 +0100789 /* List of CPUs that are not vulnerable and don't need KPTI */
790 static const struct midr_range kpti_safe_list[] = {
791 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
792 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
793 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
794 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
795 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
796 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
797 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
798 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
799 };
Marc Zyngierda935102018-04-03 12:09:21 +0100800 char const *str = "command line option";
Will Deacon73547722018-04-03 12:09:14 +0100801 u64 pfr0 = read_system_reg(SYS_ID_AA64PFR0_EL1);
802
Marc Zyngierda935102018-04-03 12:09:21 +0100803 /*
804 * For reasons that aren't entirely clear, enabling KPTI on Cavium
805 * ThunderX leads to apparent I-cache corruption of kernel text, which
806 * ends as well as you might imagine. Don't even try.
807 */
Suzuki K Poulosefe64d7d2016-11-08 13:56:20 +0000808 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
Marc Zyngierda935102018-04-03 12:09:21 +0100809 str = "ARM64_WORKAROUND_CAVIUM_27456";
810 __kpti_forced = -1;
811 }
812
813 /* Forced? */
Will Deaconf79ff2d2017-11-14 14:38:19 +0000814 if (__kpti_forced) {
Marc Zyngierda935102018-04-03 12:09:21 +0100815 pr_info_once("kernel page table isolation forced %s by %s\n",
816 __kpti_forced > 0 ? "ON" : "OFF", str);
Will Deaconf79ff2d2017-11-14 14:38:19 +0000817 return __kpti_forced > 0;
818 }
819
820 /* Useful for KASLR robustness */
821 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
822 return true;
823
Jayachandran C2adcb1f2018-04-03 12:09:18 +0100824 /* Don't force KPTI for CPUs that are not vulnerable */
Suzuki K Poulosec6815502022-04-06 17:45:14 +0100825 if (is_midr_in_range_list(read_cpuid_id(), kpti_safe_list))
Jayachandran C2adcb1f2018-04-03 12:09:18 +0100826 return false;
Jayachandran C2adcb1f2018-04-03 12:09:18 +0100827
Will Deacon73547722018-04-03 12:09:14 +0100828 /* Defer to CPU feature registers */
829 return !cpuid_feature_extract_unsigned_field(pfr0,
830 ID_AA64PFR0_CSV3_SHIFT);
Will Deaconbfca1572018-04-03 12:09:09 +0100831}
832
Greg Kroah-Hartman28d13392022-04-12 08:16:55 +0200833static void __nocfi
Dave Martin3f74ad62022-04-06 17:45:07 +0100834kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
Will Deacon4025fe12018-04-03 12:09:20 +0100835{
836 typedef void (kpti_remap_fn)(int, int, phys_addr_t);
837 extern kpti_remap_fn idmap_kpti_install_ng_mappings;
838 kpti_remap_fn *remap_fn;
839
840 static bool kpti_applied = false;
841 int cpu = smp_processor_id();
842
James Morse1451b7f2022-04-06 17:45:41 +0100843 if (__this_cpu_read(this_cpu_vector) == vectors) {
844 const char *v = arm64_get_bp_hardening_vector(EL1_VECTOR_KPTI);
845
846 __this_cpu_write(this_cpu_vector, v);
847 }
848
Will Deacon4025fe12018-04-03 12:09:20 +0100849 if (kpti_applied)
Dave Martin3f74ad62022-04-06 17:45:07 +0100850 return;
Will Deacon4025fe12018-04-03 12:09:20 +0100851
852 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
853
854 cpu_install_idmap();
855 remap_fn(cpu, num_online_cpus(), __pa_symbol(swapper_pg_dir));
856 cpu_uninstall_idmap();
857
858 if (!cpu)
859 kpti_applied = true;
860
Dave Martin3f74ad62022-04-06 17:45:07 +0100861 return;
Will Deaconf79ff2d2017-11-14 14:38:19 +0000862}
863
864static int __init parse_kpti(char *str)
865{
866 bool enabled;
867 int ret = strtobool(str, &enabled);
868
869 if (ret)
870 return ret;
871
872 __kpti_forced = enabled ? 1 : -1;
873 return 0;
874}
Will Deacon12942d52018-06-22 10:25:25 +0100875early_param("kpti", parse_kpti);
Will Deaconf79ff2d2017-11-14 14:38:19 +0000876#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
877
Dave Martin3f74ad62022-04-06 17:45:07 +0100878static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
James Morseeea59022018-07-20 10:56:16 +0100879{
880 /*
881 * Copy register values that aren't redirected by hardware.
882 *
883 * Before code patching, we only set tpidr_el1, all CPUs need to copy
884 * this value to tpidr_el2 before we patch the code. Once we've done
885 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
886 * do anything here.
887 */
888 if (!alternatives_applied)
889 write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
James Morseeea59022018-07-20 10:56:16 +0100890}
891
Marc Zyngier359b7062015-03-27 13:09:23 +0000892static const struct arm64_cpu_capabilities arm64_features[] = {
Marc Zyngier94a9e042015-06-12 12:06:36 +0100893 {
894 .desc = "GIC system register CPU interface",
895 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100896 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Marc Zyngier963fcd42015-09-30 11:50:04 +0100897 .matches = has_useable_gicv3_cpuif,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100898 .sys_reg = SYS_ID_AA64PFR0_EL1,
899 .field_pos = ID_AA64PFR0_GIC_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000900 .sign = FTR_UNSIGNED,
James Morse18ffa042015-07-21 13:23:29 +0100901 .min_field_value = 1,
Marc Zyngier94a9e042015-06-12 12:06:36 +0100902 },
James Morse338d4f42015-07-22 19:05:54 +0100903#ifdef CONFIG_ARM64_PAN
904 {
905 .desc = "Privileged Access Never",
906 .capability = ARM64_HAS_PAN,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100907 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100908 .matches = has_cpuid_feature,
909 .sys_reg = SYS_ID_AA64MMFR1_EL1,
910 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000911 .sign = FTR_UNSIGNED,
James Morse338d4f42015-07-22 19:05:54 +0100912 .min_field_value = 1,
Dave Martin3f74ad62022-04-06 17:45:07 +0100913 .cpu_enable = cpu_enable_pan,
James Morse338d4f42015-07-22 19:05:54 +0100914 },
915#endif /* CONFIG_ARM64_PAN */
Will Deacon2e94da12015-07-27 16:23:58 +0100916#if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
917 {
918 .desc = "LSE atomic instructions",
919 .capability = ARM64_HAS_LSE_ATOMICS,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100920 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100921 .matches = has_cpuid_feature,
922 .sys_reg = SYS_ID_AA64ISAR0_EL1,
923 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000924 .sign = FTR_UNSIGNED,
Will Deacon2e94da12015-07-27 16:23:58 +0100925 .min_field_value = 2,
926 },
927#endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
Marc Zyngierd88701b2015-01-29 11:24:05 +0000928 {
Will Deacond5370f72016-02-02 12:46:24 +0000929 .desc = "Software prefetching using PRFM",
930 .capability = ARM64_HAS_NO_HW_PREFETCH,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100931 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Will Deacond5370f72016-02-02 12:46:24 +0000932 .matches = has_no_hw_prefetch,
933 },
James Morse57f49592016-02-05 14:58:48 +0000934#ifdef CONFIG_ARM64_UAO
935 {
936 .desc = "User Access Override",
937 .capability = ARM64_HAS_UAO,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100938 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse57f49592016-02-05 14:58:48 +0000939 .matches = has_cpuid_feature,
940 .sys_reg = SYS_ID_AA64MMFR2_EL1,
941 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
942 .min_field_value = 1,
James Morse7f319f32022-04-06 17:45:05 +0100943 /*
944 * We rely on stop_machine() calling uao_thread_switch() to set
945 * UAO immediately after patching.
946 */
James Morse57f49592016-02-05 14:58:48 +0000947 },
948#endif /* CONFIG_ARM64_UAO */
James Morse70544192016-02-05 14:58:50 +0000949#ifdef CONFIG_ARM64_PAN
950 {
951 .capability = ARM64_ALT_PAN_NOT_UAO,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100952 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
James Morse70544192016-02-05 14:58:50 +0000953 .matches = cpufeature_pan_not_uao,
954 },
955#endif /* CONFIG_ARM64_PAN */
Linus Torvalds588ab3f2016-03-17 20:03:47 -0700956 {
Marc Zyngierd88701b2015-01-29 11:24:05 +0000957 .desc = "Virtualization Host Extensions",
958 .capability = ARM64_HAS_VIRT_HOST_EXTN,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100959 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Marc Zyngierd88701b2015-01-29 11:24:05 +0000960 .matches = runs_at_el2,
Dave Martin3f74ad62022-04-06 17:45:07 +0100961 .cpu_enable = cpu_copy_el2regs,
Marc Zyngierd88701b2015-01-29 11:24:05 +0000962 },
Suzuki K Poulose042446a2016-04-18 10:28:36 +0100963 {
964 .desc = "32-bit EL0 Support",
965 .capability = ARM64_HAS_32BIT_EL0,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100966 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Suzuki K Poulose042446a2016-04-18 10:28:36 +0100967 .matches = has_cpuid_feature,
968 .sys_reg = SYS_ID_AA64PFR0_EL1,
969 .sign = FTR_UNSIGNED,
970 .field_pos = ID_AA64PFR0_EL0_SHIFT,
971 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
972 },
Marc Zyngierd1745912016-06-30 18:40:42 +0100973 {
974 .desc = "Reduced HYP mapping offset",
975 .capability = ARM64_HYP_OFFSET_LOW,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100976 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Marc Zyngierd1745912016-06-30 18:40:42 +0100977 .matches = hyp_offset_low,
978 },
Will Deaconf79ff2d2017-11-14 14:38:19 +0000979#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
980 {
Will Deacon73547722018-04-03 12:09:14 +0100981 .desc = "Kernel page table isolation (KPTI)",
Will Deaconf79ff2d2017-11-14 14:38:19 +0000982 .capability = ARM64_UNMAP_KERNEL_AT_EL0,
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100983 .type = ARM64_CPUCAP_SYSTEM_FEATURE,
Will Deaconf79ff2d2017-11-14 14:38:19 +0000984 .matches = unmap_kernel_at_el0,
Dave Martin3f74ad62022-04-06 17:45:07 +0100985 .cpu_enable = kpti_install_ng_mappings,
Will Deaconf79ff2d2017-11-14 14:38:19 +0000986 },
987#endif
Marc Zyngier359b7062015-03-27 13:09:23 +0000988 {},
989};
990
Suzuki K Poulose3d108b72022-04-06 17:45:10 +0100991#define HWCAP_CAP(reg, field, s, min_value, cap_type, cap) \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100992 { \
993 .desc = #cap, \
Suzuki K Pouloseb49720b2022-04-06 17:45:11 +0100994 .type = ARM64_CPUCAP_SYSTEM_FEATURE, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100995 .matches = has_cpuid_feature, \
996 .sys_reg = reg, \
997 .field_pos = field, \
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000998 .sign = s, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100999 .min_field_value = min_value, \
Suzuki K Poulose3d108b72022-04-06 17:45:10 +01001000 .hwcap_type = cap_type, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001001 .hwcap = cap, \
1002 }
1003
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001004static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001005 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_PMULL),
1006 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_AES),
1007 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA1),
1008 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA2),
Suzuki K Pouloseba62b302017-10-11 14:01:02 +01001009 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 +00001010 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_CRC32),
1011 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ATOMICS),
Suzuki K Pouloseba62b302017-10-11 14:01:02 +01001012 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA3),
1013 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM3),
1014 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM4),
1015 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_DP_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDDP),
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001016 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 +00001017 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 +00001018 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 +00001019 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_ASIMDHP),
Suzuki K Poulose75283502016-04-18 10:28:33 +01001020 {},
1021};
1022
1023static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001024#ifdef CONFIG_COMPAT
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +00001025 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
1026 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
1027 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
1028 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
1029 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 +01001030#endif
1031 {},
1032};
1033
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001034static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001035{
1036 switch (cap->hwcap_type) {
1037 case CAP_HWCAP:
1038 elf_hwcap |= cap->hwcap;
1039 break;
1040#ifdef CONFIG_COMPAT
1041 case CAP_COMPAT_HWCAP:
1042 compat_elf_hwcap |= (u32)cap->hwcap;
1043 break;
1044 case CAP_COMPAT_HWCAP2:
1045 compat_elf_hwcap2 |= (u32)cap->hwcap;
1046 break;
1047#endif
1048 default:
1049 WARN_ON(1);
1050 break;
1051 }
1052}
1053
1054/* Check if we have a particular HWCAP enabled */
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001055static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001056{
1057 bool rc;
1058
1059 switch (cap->hwcap_type) {
1060 case CAP_HWCAP:
1061 rc = (elf_hwcap & cap->hwcap) != 0;
1062 break;
1063#ifdef CONFIG_COMPAT
1064 case CAP_COMPAT_HWCAP:
1065 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
1066 break;
1067 case CAP_COMPAT_HWCAP2:
1068 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
1069 break;
1070#endif
1071 default:
1072 WARN_ON(1);
1073 rc = false;
1074 }
1075
1076 return rc;
1077}
1078
Suzuki K Poulose75283502016-04-18 10:28:33 +01001079static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001080{
Suzuki K Poulose75283502016-04-18 10:28:33 +01001081 for (; hwcaps->matches; hwcaps++)
Suzuki K Poulose3d108b72022-04-06 17:45:10 +01001082 if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
Suzuki K Poulose75283502016-04-18 10:28:33 +01001083 cap_set_elf_hwcap(hwcaps);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001084}
1085
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001086/*
1087 * Check if the current CPU has a given feature capability.
1088 * Should be called from non-preemptible context.
1089 */
1090static bool __this_cpu_has_cap(const struct arm64_cpu_capabilities *cap_array,
1091 unsigned int cap)
1092{
1093 const struct arm64_cpu_capabilities *caps;
1094
1095 if (WARN_ON(preemptible()))
1096 return false;
1097
Mark Rutland93f339e2018-04-12 12:11:07 +01001098 for (caps = cap_array; caps->matches; caps++)
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001099 if (caps->capability == cap &&
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001100 caps->matches(caps, SCOPE_LOCAL_CPU))
1101 return true;
1102 return false;
1103}
1104
Suzuki K Poulose454f7ba2022-04-06 17:45:09 +01001105static void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
1106 const char *info)
Marc Zyngier359b7062015-03-27 13:09:23 +00001107{
Suzuki K Poulose75283502016-04-18 10:28:33 +01001108 for (; caps->matches; caps++) {
Suzuki K Poulose3d108b72022-04-06 17:45:10 +01001109 if (!caps->matches(caps, cpucap_default_scope(caps)))
Marc Zyngier359b7062015-03-27 13:09:23 +00001110 continue;
1111
Suzuki K Poulose75283502016-04-18 10:28:33 +01001112 if (!cpus_have_cap(caps->capability) && caps->desc)
1113 pr_info("%s %s\n", info, caps->desc);
1114 cpus_set_cap(caps->capability);
Marc Zyngier359b7062015-03-27 13:09:23 +00001115 }
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001116}
James Morse1c076302015-07-21 13:23:28 +01001117
Dave Martin3f74ad62022-04-06 17:45:07 +01001118static int __enable_cpu_capability(void *arg)
1119{
1120 const struct arm64_cpu_capabilities *cap = arg;
1121
1122 cap->cpu_enable(cap);
1123 return 0;
1124}
1125
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001126/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001127 * Run through the enabled capabilities and enable() it on all active
1128 * CPUs
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001129 */
Suzuki K Poulose454f7ba2022-04-06 17:45:09 +01001130static void __init
1131enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps)
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001132{
Mark Rutlandb1d57082017-05-16 15:18:05 +01001133 for (; caps->matches; caps++) {
1134 unsigned int num = caps->capability;
1135
1136 if (!cpus_have_cap(num))
1137 continue;
1138
1139 /* Ensure cpus_have_const_cap(num) works */
1140 static_branch_enable(&cpu_hwcap_keys[num]);
1141
Dave Martin3f74ad62022-04-06 17:45:07 +01001142 if (caps->cpu_enable) {
James Morse2a6dcb22016-10-18 11:27:46 +01001143 /*
1144 * Use stop_machine() as it schedules the work allowing
1145 * us to modify PSTATE, instead of on_each_cpu() which
1146 * uses an IPI, giving us a PSTATE that disappears when
1147 * we return.
1148 */
Dave Martin3f74ad62022-04-06 17:45:07 +01001149 stop_machine(__enable_cpu_capability, (void *)caps,
1150 cpu_online_mask);
Mark Rutlandb1d57082017-05-16 15:18:05 +01001151 }
1152 }
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001153}
1154
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001155/*
1156 * Flag to indicate if we have computed the system wide
1157 * capabilities based on the boot time active CPUs. This
1158 * will be used to determine if a new booting CPU should
1159 * go through the verification process to make sure that it
1160 * supports the system capabilities, without using a hotplug
1161 * notifier.
1162 */
1163static bool sys_caps_initialised;
1164
1165static inline void set_sys_caps_initialised(void)
1166{
1167 sys_caps_initialised = true;
1168}
1169
1170/*
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001171 * Check for CPU features that are used in early boot
1172 * based on the Boot CPU value.
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001173 */
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001174static void check_early_cpu_features(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00001175{
Suzuki K Pouloseac1ad202016-04-13 14:41:33 +01001176 verify_cpu_run_el();
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001177 verify_cpu_asid_bits();
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001178}
1179
Suzuki K Poulose75283502016-04-18 10:28:33 +01001180static void
1181verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
1182{
1183
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001184 for (; caps->matches; caps++)
1185 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01001186 pr_crit("CPU%d: missing HWCAP: %s\n",
1187 smp_processor_id(), caps->desc);
1188 cpu_die_early();
1189 }
Suzuki K Poulose75283502016-04-18 10:28:33 +01001190}
1191
1192static void
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001193verify_local_cpu_features(const struct arm64_cpu_capabilities *caps_list)
Suzuki K Poulose75283502016-04-18 10:28:33 +01001194{
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001195 const struct arm64_cpu_capabilities *caps = caps_list;
Suzuki K Poulose75283502016-04-18 10:28:33 +01001196 for (; caps->matches; caps++) {
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001197 if (!cpus_have_cap(caps->capability))
Suzuki K Poulose75283502016-04-18 10:28:33 +01001198 continue;
1199 /*
1200 * If the new CPU misses an advertised feature, we cannot proceed
1201 * further, park the cpu.
1202 */
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001203 if (!__this_cpu_has_cap(caps_list, caps->capability)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01001204 pr_crit("CPU%d: missing feature: %s\n",
1205 smp_processor_id(), caps->desc);
1206 cpu_die_early();
1207 }
Dave Martin3f74ad62022-04-06 17:45:07 +01001208 if (caps->cpu_enable)
1209 caps->cpu_enable(caps);
Suzuki K Poulose75283502016-04-18 10:28:33 +01001210 }
1211}
1212
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001213/*
Suzuki K Poulose454f7ba2022-04-06 17:45:09 +01001214 * The CPU Errata work arounds are detected and applied at boot time
1215 * and the related information is freed soon after. If the new CPU requires
1216 * an errata not detected at boot, fail this CPU.
1217 */
1218static void verify_local_cpu_errata_workarounds(void)
1219{
1220 const struct arm64_cpu_capabilities *caps = arm64_errata;
1221
1222 for (; caps->matches; caps++) {
1223 if (cpus_have_cap(caps->capability)) {
1224 if (caps->cpu_enable)
1225 caps->cpu_enable(caps);
1226 } else if (caps->matches(caps, SCOPE_LOCAL_CPU)) {
1227 pr_crit("CPU%d: Requires work around for %s, not detected"
1228 " at boot time\n",
1229 smp_processor_id(),
1230 caps->desc ? : "an erratum");
1231 cpu_die_early();
1232 }
1233 }
1234}
1235
1236static void update_cpu_errata_workarounds(void)
1237{
1238 update_cpu_capabilities(arm64_errata, "enabling workaround for");
1239}
1240
1241static void __init enable_errata_workarounds(void)
1242{
1243 enable_cpu_capabilities(arm64_errata);
1244}
1245
1246/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001247 * Run through the enabled system capabilities and enable() it on this CPU.
1248 * The capabilities were decided based on the available CPUs at the boot time.
1249 * Any new CPU should match the system wide status of the capability. If the
1250 * new CPU doesn't have a capability which the system now has enabled, we
1251 * cannot do anything to fix it up and could cause unexpected failures. So
1252 * we park the CPU.
1253 */
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001254static void verify_local_cpu_capabilities(void)
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001255{
Suzuki K Poulose89ba2642016-09-09 14:07:09 +01001256 verify_local_cpu_errata_workarounds();
Suzuki K Poulose75283502016-04-18 10:28:33 +01001257 verify_local_cpu_features(arm64_features);
1258 verify_local_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001259 if (system_supports_32bit_el0())
1260 verify_local_elf_hwcaps(compat_elf_hwcaps);
Marc Zyngier359b7062015-03-27 13:09:23 +00001261}
1262
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001263void check_local_cpu_capabilities(void)
1264{
1265 /*
1266 * All secondary CPUs should conform to the early CPU features
1267 * in use by the kernel based on boot CPU.
1268 */
1269 check_early_cpu_features();
1270
1271 /*
1272 * If we haven't finalised the system capabilities, this CPU gets
1273 * a chance to update the errata work arounds.
1274 * Otherwise, this CPU should verify that it has all the system
1275 * advertised capabilities.
1276 */
1277 if (!sys_caps_initialised)
1278 update_cpu_errata_workarounds();
1279 else
1280 verify_local_cpu_capabilities();
1281}
1282
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001283static void __init setup_feature_capabilities(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00001284{
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001285 update_cpu_capabilities(arm64_features, "detected feature:");
1286 enable_cpu_capabilities(arm64_features);
Marc Zyngier359b7062015-03-27 13:09:23 +00001287}
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001288
Mark Rutlandb1d57082017-05-16 15:18:05 +01001289DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
1290EXPORT_SYMBOL(arm64_const_caps_ready);
1291
1292static void __init mark_const_caps_ready(void)
1293{
1294 static_branch_enable(&arm64_const_caps_ready);
1295}
1296
Marc Zyngier1d648e42018-04-03 12:09:15 +01001297extern const struct arm64_cpu_capabilities arm64_errata[];
1298
Marc Zyngiere3661b12016-04-22 12:25:32 +01001299bool this_cpu_has_cap(unsigned int cap)
1300{
Marc Zyngier1d648e42018-04-03 12:09:15 +01001301 return (__this_cpu_has_cap(arm64_features, cap) ||
1302 __this_cpu_has_cap(arm64_errata, cap));
Marc Zyngiere3661b12016-04-22 12:25:32 +01001303}
1304
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001305void __init setup_cpu_features(void)
1306{
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001307 u32 cwg;
1308 int cls;
1309
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001310 /* Set the CPU feature capabilies */
1311 setup_feature_capabilities();
Andre Przywara8e231852016-06-28 18:07:30 +01001312 enable_errata_workarounds();
Mark Rutlandb1d57082017-05-16 15:18:05 +01001313 mark_const_caps_ready();
Suzuki K Poulose75283502016-04-18 10:28:33 +01001314 setup_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001315
1316 if (system_supports_32bit_el0())
1317 setup_elf_hwcaps(compat_elf_hwcaps);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001318
1319 /* Advertise that we have computed the system capabilities */
1320 set_sys_caps_initialised();
1321
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001322 /*
1323 * Check for sane CTR_EL0.CWG value.
1324 */
1325 cwg = cache_type_cwg();
1326 cls = cache_line_size();
1327 if (!cwg)
1328 pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
1329 cls);
1330 if (L1_CACHE_BYTES < cls)
1331 pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
1332 L1_CACHE_BYTES, cls);
Marc Zyngier359b7062015-03-27 13:09:23 +00001333}
James Morse70544192016-02-05 14:58:50 +00001334
1335static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001336cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
James Morse70544192016-02-05 14:58:50 +00001337{
Suzuki K Poulosefe64d7d2016-11-08 13:56:20 +00001338 return (cpus_have_const_cap(ARM64_HAS_PAN) && !cpus_have_const_cap(ARM64_HAS_UAO));
James Morse70544192016-02-05 14:58:50 +00001339}