blob: 5608f95c8aa2e006d02c566f3201e352a0dd0b72 [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>
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010023#include <linux/sort.h>
James Morse2a6dcb22016-10-18 11:27:46 +010024#include <linux/stop_machine.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000025#include <linux/types.h>
Laura Abbottf8fee94e2017-01-10 13:35:49 -080026#include <linux/mm.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000027#include <asm/cpu.h>
28#include <asm/cpufeature.h>
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +010029#include <asm/cpu_ops.h>
Suzuki K Poulose13f417f2016-02-23 10:31:45 +000030#include <asm/mmu_context.h>
James Morse338d4f42015-07-22 19:05:54 +010031#include <asm/processor.h>
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +010032#include <asm/sysreg.h>
Marc Zyngierd88701b2015-01-29 11:24:05 +000033#include <asm/virt.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000034
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010035unsigned long elf_hwcap __read_mostly;
36EXPORT_SYMBOL_GPL(elf_hwcap);
37
38#ifdef CONFIG_COMPAT
39#define COMPAT_ELF_HWCAP_DEFAULT \
40 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
41 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
42 COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
43 COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
44 COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
45 COMPAT_HWCAP_LPAE)
46unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
47unsigned int compat_elf_hwcap2 __read_mostly;
48#endif
49
50DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
Catalin Marinas005bf1a2016-07-01 16:53:00 +010051EXPORT_SYMBOL(cpu_hwcaps);
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010052
Catalin Marinasefd9e032016-09-05 18:25:48 +010053DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
54EXPORT_SYMBOL(cpu_hwcap_keys);
55
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000056#define __ARM64_FTR_BITS(SIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010057 { \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000058 .sign = SIGNED, \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010059 .strict = STRICT, \
60 .type = TYPE, \
61 .shift = SHIFT, \
62 .width = WIDTH, \
63 .safe_val = SAFE_VAL, \
64 }
65
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +000066/* Define a feature with unsigned values */
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000067#define ARM64_FTR_BITS(STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000068 __ARM64_FTR_BITS(FTR_UNSIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
69
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +000070/* Define a feature with a signed value */
71#define S_ARM64_FTR_BITS(STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
72 __ARM64_FTR_BITS(FTR_SIGNED, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
73
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010074#define ARM64_FTR_END \
75 { \
76 .width = 0, \
77 }
78
James Morse70544192016-02-05 14:58:50 +000079/* meta feature for alternatives */
80static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +010081cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
82
James Morse70544192016-02-05 14:58:50 +000083
Ard Biesheuvel5e49d732016-08-31 11:31:08 +010084static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
Suzuki K Pouloseba62b302017-10-11 14:01:02 +010085 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_DP_SHIFT, 4, 0),
86 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
87 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
88 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010089 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
90 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0),
91 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
92 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
93 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
94 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
95 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
96 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
97 ARM64_FTR_END,
98};
99
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100100static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
Will Deacon73547722018-04-03 12:09:14 +0100101 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
Mark Rutland47320012018-04-12 12:11:13 +0100102 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
103 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 24, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100104 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0),
105 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_GIC_SHIFT, 4, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000106 S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
107 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 +0100108 /* Linux doesn't care about the EL3 */
109 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64PFR0_EL3_SHIFT, 4, 0),
110 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL2_SHIFT, 4, 0),
111 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
112 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
113 ARM64_FTR_END,
114};
115
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100116static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100117 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000118 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
119 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 +0100120 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
121 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
122 /* Linux shouldn't care about secure memory */
123 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
124 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
125 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
126 /*
127 * Differing PARange is fine as long as all peripherals and memory are mapped
128 * within the minimum PARange of all CPUs
129 */
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000130 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100131 ARM64_FTR_END,
132};
133
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100134static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100135 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
136 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
137 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
138 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
139 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
140 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
141 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
142 ARM64_FTR_END,
143};
144
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100145static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
Kefeng Wang7d7b4ae2016-03-25 17:30:07 +0800146 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
147 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
148 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000149 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
Kefeng Wang7d7b4ae2016-03-25 17:30:07 +0800150 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000151 ARM64_FTR_END,
152};
153
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100154static const struct arm64_ftr_bits ftr_ctr[] = {
Will Deacone364e9a2019-08-05 18:13:54 +0100155 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
156 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 30, 1, 0),
157 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 29, 1, 1), /* DIC */
158 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 1, 1), /* IDC */
Will Deacon3c5dbb92019-08-05 18:13:55 +0100159 ARM64_FTR_BITS(FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, 24, 4, 0), /* CWG */
160 ARM64_FTR_BITS(FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, 20, 4, 0), /* ERG */
Suzuki K Poulosea6830092018-07-04 23:07:45 +0100161 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, CTR_DMINLINE_SHIFT, 4, 1),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100162 /*
163 * Linux can handle differing I-cache policies. Userspace JITs will
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100164 * make use of *minLine.
165 * If we have differing I-cache policies, report it as the weakest - AIVIVT.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100166 */
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100167 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_AIVIVT), /* L1Ip */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100168 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 10, 0), /* RAZ */
Suzuki K Poulosea6830092018-07-04 23:07:45 +0100169 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, CTR_IMINLINE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100170 ARM64_FTR_END,
171};
172
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100173struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
174 .name = "SYS_CTR_EL0",
175 .ftr_bits = ftr_ctr
176};
177
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100178static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000179 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 28, 4, 0xf), /* InnerShr */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100180 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 24, 4, 0), /* FCSE */
181 ARM64_FTR_BITS(FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
182 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 4, 0), /* TCM */
183 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* ShareLvl */
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000184 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0xf), /* OuterShr */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100185 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* PMSA */
186 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* VMSA */
187 ARM64_FTR_END,
188};
189
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100190static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100191 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 32, 32, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000192 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
193 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
194 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
195 S_ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
196 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
197 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
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_mvfr2[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100202 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
203 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* FPMisc */
204 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* SIMDMisc */
205 ARM64_FTR_END,
206};
207
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100208static const struct arm64_ftr_bits ftr_dczid[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100209 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 5, 27, 0), /* RAZ */
210 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
211 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
212 ARM64_FTR_END,
213};
214
215
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100216static const struct arm64_ftr_bits ftr_id_isar5[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100217 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_RDM_SHIFT, 4, 0),
218 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 20, 4, 0), /* RAZ */
219 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_CRC32_SHIFT, 4, 0),
220 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA2_SHIFT, 4, 0),
221 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA1_SHIFT, 4, 0),
222 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_AES_SHIFT, 4, 0),
223 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, ID_ISAR5_SEVL_SHIFT, 4, 0),
224 ARM64_FTR_END,
225};
226
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100227static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100228 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 24, 0), /* RAZ */
229 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* ac2 */
230 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* RAZ */
231 ARM64_FTR_END,
232};
233
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100234static const struct arm64_ftr_bits ftr_id_pfr0[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100235 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 16, 16, 0), /* RAZ */
236 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 12, 4, 0), /* State3 */
237 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 8, 4, 0), /* State2 */
238 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 4, 4, 0), /* State1 */
239 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 4, 0), /* State0 */
240 ARM64_FTR_END,
241};
242
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100243static const struct arm64_ftr_bits ftr_id_dfr0[] = {
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000244 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +0000245 S_ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000246 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
247 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
248 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
249 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
250 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
251 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
252 ARM64_FTR_END,
253};
254
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100255/*
256 * Common ftr bits for a 32bit register with all hidden, strict
257 * attributes, with 4bit feature fields and a default safe value of
258 * 0. Covers the following 32bit registers:
259 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
260 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100261static const struct arm64_ftr_bits ftr_generic_32bits[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100262 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
263 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
264 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
265 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
266 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
267 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
268 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
269 ARM64_FTR_BITS(FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
270 ARM64_FTR_END,
271};
272
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100273static const struct arm64_ftr_bits ftr_generic[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100274 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 64, 0),
275 ARM64_FTR_END,
276};
277
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100278static const struct arm64_ftr_bits ftr_generic32[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100279 ARM64_FTR_BITS(FTR_STRICT, FTR_EXACT, 0, 32, 0),
280 ARM64_FTR_END,
281};
282
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100283static const struct arm64_ftr_bits ftr_aa64raz[] = {
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 Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100288#define ARM64_FTR_REG(id, table) { \
289 .sys_id = id, \
290 .reg = &(struct arm64_ftr_reg){ \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100291 .name = #id, \
292 .ftr_bits = &((table)[0]), \
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100293 }}
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100294
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100295static const struct __ftr_reg_entry {
296 u32 sys_id;
297 struct arm64_ftr_reg *reg;
298} arm64_ftr_regs[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100299
300 /* Op1 = 0, CRn = 0, CRm = 1 */
301 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
302 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000303 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100304 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
305 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
306 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
307 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
308
309 /* Op1 = 0, CRn = 0, CRm = 2 */
310 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
311 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
312 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
313 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
314 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
315 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
316 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
317
318 /* Op1 = 0, CRn = 0, CRm = 3 */
319 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
320 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
321 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
322
323 /* Op1 = 0, CRn = 0, CRm = 4 */
324 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
325 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_aa64raz),
326
327 /* Op1 = 0, CRn = 0, CRm = 5 */
328 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
329 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_generic),
330
331 /* Op1 = 0, CRn = 0, CRm = 6 */
332 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
333 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_aa64raz),
334
335 /* Op1 = 0, CRn = 0, CRm = 7 */
336 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
337 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
James Morse406e3082016-02-05 14:58:47 +0000338 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100339
340 /* Op1 = 3, CRn = 0, CRm = 0 */
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100341 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100342 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
343
344 /* Op1 = 3, CRn = 14, CRm = 0 */
345 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_generic32),
346};
347
348static int search_cmp_ftr_reg(const void *id, const void *regp)
349{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100350 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100351}
352
353/*
354 * get_arm64_ftr_reg - Lookup a feature register entry using its
355 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
356 * ascending order of sys_id , we use binary search to find a matching
357 * entry.
358 *
359 * returns - Upon success, matching ftr_reg entry for id.
360 * - NULL on failure. It is upto the caller to decide
361 * the impact of a failure.
362 */
363static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
364{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100365 const struct __ftr_reg_entry *ret;
366
367 ret = bsearch((const void *)(unsigned long)sys_id,
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100368 arm64_ftr_regs,
369 ARRAY_SIZE(arm64_ftr_regs),
370 sizeof(arm64_ftr_regs[0]),
371 search_cmp_ftr_reg);
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100372 if (ret)
373 return ret->reg;
374 return NULL;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100375}
376
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100377static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
378 s64 ftr_val)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100379{
380 u64 mask = arm64_ftr_mask(ftrp);
381
382 reg &= ~mask;
383 reg |= (ftr_val << ftrp->shift) & mask;
384 return reg;
385}
386
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100387static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
388 s64 cur)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100389{
390 s64 ret = 0;
391
392 switch (ftrp->type) {
393 case FTR_EXACT:
394 ret = ftrp->safe_val;
395 break;
396 case FTR_LOWER_SAFE:
397 ret = new < cur ? new : cur;
398 break;
Will Deacon3c5dbb92019-08-05 18:13:55 +0100399 case FTR_HIGHER_OR_ZERO_SAFE:
400 if (!cur || !new)
401 break;
402 /* Fallthrough */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100403 case FTR_HIGHER_SAFE:
404 ret = new > cur ? new : cur;
405 break;
406 default:
407 BUG();
408 }
409
410 return ret;
411}
412
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100413static void __init sort_ftr_regs(void)
414{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100415 int i;
416
417 /* Check that the array is sorted so that we can do the binary search */
418 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
419 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100420}
421
422/*
423 * Initialise the CPU feature register from Boot CPU values.
424 * Also initiliases the strict_mask for the register.
425 */
426static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
427{
428 u64 val = 0;
429 u64 strict_mask = ~0x0ULL;
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100430 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100431 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
432
433 BUG_ON(!reg);
434
435 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
436 s64 ftr_new = arm64_ftr_value(ftrp, new);
437
438 val = arm64_ftr_set_value(ftrp, val, ftr_new);
439 if (!ftrp->strict)
440 strict_mask &= ~arm64_ftr_mask(ftrp);
441 }
442 reg->sys_val = val;
443 reg->strict_mask = strict_mask;
444}
445
446void __init init_cpu_features(struct cpuinfo_arm64 *info)
447{
448 /* Before we start using the tables, make sure it is sorted */
449 sort_ftr_regs();
450
451 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
452 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
453 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
454 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
455 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
456 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
457 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
458 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
459 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000460 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100461 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
462 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100463
464 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
465 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
466 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
467 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
468 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
469 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
470 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
471 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
472 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
473 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
474 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
475 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
476 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
477 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
478 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
479 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
480 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
481 }
482
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100483}
484
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100485static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100486{
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100487 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100488
489 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
490 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
491 s64 ftr_new = arm64_ftr_value(ftrp, new);
492
493 if (ftr_cur == ftr_new)
494 continue;
495 /* Find a safe value */
496 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
497 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
498 }
499
500}
501
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100502static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100503{
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100504 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
505
506 BUG_ON(!regp);
507 update_cpu_ftr_reg(regp, val);
508 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
509 return 0;
510 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
511 regp->name, boot, cpu, val);
512 return 1;
513}
514
515/*
516 * Update system wide CPU feature registers with the values from a
517 * non-boot CPU. Also performs SANITY checks to make sure that there
518 * aren't any insane variations from that of the boot CPU.
519 */
520void update_cpu_features(int cpu,
521 struct cpuinfo_arm64 *info,
522 struct cpuinfo_arm64 *boot)
523{
524 int taint = 0;
525
526 /*
527 * The kernel can handle differing I-cache policies, but otherwise
528 * caches should look identical. Userspace JITs will make use of
529 * *minLine.
530 */
531 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
532 info->reg_ctr, boot->reg_ctr);
533
534 /*
535 * Userspace may perform DC ZVA instructions. Mismatched block sizes
536 * could result in too much or too little memory being zeroed if a
537 * process is preempted and migrated between CPUs.
538 */
539 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
540 info->reg_dczid, boot->reg_dczid);
541
542 /* If different, timekeeping will be broken (especially with KVM) */
543 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
544 info->reg_cntfrq, boot->reg_cntfrq);
545
546 /*
547 * The kernel uses self-hosted debug features and expects CPUs to
548 * support identical debug features. We presently need CTX_CMPs, WRPs,
549 * and BRPs to be identical.
550 * ID_AA64DFR1 is currently RES0.
551 */
552 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
553 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
554 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
555 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
556 /*
557 * Even in big.LITTLE, processors should be identical instruction-set
558 * wise.
559 */
560 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
561 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
562 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
563 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
564
565 /*
566 * Differing PARange support is fine as long as all peripherals and
567 * memory are mapped within the minimum PARange of all CPUs.
568 * Linux should not care about secure memory.
569 */
570 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
571 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
572 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
573 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000574 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
575 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100576
577 /*
578 * EL3 is not our concern.
579 * ID_AA64PFR1 is currently RES0.
580 */
581 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
582 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
583 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
584 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
585
586 /*
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100587 * If we have AArch32, we care about 32-bit features for compat.
588 * If the system doesn't support AArch32, don't update them.
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100589 */
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100590 if (id_aa64pfr0_32bit_el0(read_system_reg(SYS_ID_AA64PFR0_EL1)) &&
591 id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
592
593 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100594 info->reg_id_dfr0, boot->reg_id_dfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100595 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100596 info->reg_id_isar0, boot->reg_id_isar0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100597 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100598 info->reg_id_isar1, boot->reg_id_isar1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100599 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100600 info->reg_id_isar2, boot->reg_id_isar2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100601 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100602 info->reg_id_isar3, boot->reg_id_isar3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100603 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100604 info->reg_id_isar4, boot->reg_id_isar4);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100605 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100606 info->reg_id_isar5, boot->reg_id_isar5);
607
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100608 /*
609 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
610 * ACTLR formats could differ across CPUs and therefore would have to
611 * be trapped for virtualization anyway.
612 */
613 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100614 info->reg_id_mmfr0, boot->reg_id_mmfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100615 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100616 info->reg_id_mmfr1, boot->reg_id_mmfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100617 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100618 info->reg_id_mmfr2, boot->reg_id_mmfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100619 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100620 info->reg_id_mmfr3, boot->reg_id_mmfr3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100621 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100622 info->reg_id_pfr0, boot->reg_id_pfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100623 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100624 info->reg_id_pfr1, boot->reg_id_pfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100625 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100626 info->reg_mvfr0, boot->reg_mvfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100627 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100628 info->reg_mvfr1, boot->reg_mvfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100629 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100630 info->reg_mvfr2, boot->reg_mvfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100631 }
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100632
633 /*
634 * Mismatched CPU features are a recipe for disaster. Don't even
635 * pretend to support them.
636 */
637 WARN_TAINT_ONCE(taint, TAINT_CPU_OUT_OF_SPEC,
638 "Unsupported CPU feature variation.\n");
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100639}
640
Suzuki K. Pouloseb3f15372015-10-19 14:24:47 +0100641u64 read_system_reg(u32 id)
642{
643 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
644
645 /* We shouldn't get a request for an unsupported register */
646 BUG_ON(!regp);
647 return regp->sys_val;
648}
Marc Zyngier359b7062015-03-27 13:09:23 +0000649
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100650/*
651 * __raw_read_system_reg() - Used by a STARTING cpu before cpuinfo is populated.
652 * Read the system register on the current CPU
653 */
654static u64 __raw_read_system_reg(u32 sys_id)
655{
656 switch (sys_id) {
657 case SYS_ID_PFR0_EL1: return read_cpuid(ID_PFR0_EL1);
658 case SYS_ID_PFR1_EL1: return read_cpuid(ID_PFR1_EL1);
659 case SYS_ID_DFR0_EL1: return read_cpuid(ID_DFR0_EL1);
660 case SYS_ID_MMFR0_EL1: return read_cpuid(ID_MMFR0_EL1);
661 case SYS_ID_MMFR1_EL1: return read_cpuid(ID_MMFR1_EL1);
662 case SYS_ID_MMFR2_EL1: return read_cpuid(ID_MMFR2_EL1);
663 case SYS_ID_MMFR3_EL1: return read_cpuid(ID_MMFR3_EL1);
664 case SYS_ID_ISAR0_EL1: return read_cpuid(ID_ISAR0_EL1);
665 case SYS_ID_ISAR1_EL1: return read_cpuid(ID_ISAR1_EL1);
666 case SYS_ID_ISAR2_EL1: return read_cpuid(ID_ISAR2_EL1);
667 case SYS_ID_ISAR3_EL1: return read_cpuid(ID_ISAR3_EL1);
668 case SYS_ID_ISAR4_EL1: return read_cpuid(ID_ISAR4_EL1);
Mark Rutland7127d432017-02-02 17:32:14 +0000669 case SYS_ID_ISAR5_EL1: return read_cpuid(ID_ISAR5_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100670 case SYS_MVFR0_EL1: return read_cpuid(MVFR0_EL1);
671 case SYS_MVFR1_EL1: return read_cpuid(MVFR1_EL1);
672 case SYS_MVFR2_EL1: return read_cpuid(MVFR2_EL1);
673
674 case SYS_ID_AA64PFR0_EL1: return read_cpuid(ID_AA64PFR0_EL1);
Mark Rutland7127d432017-02-02 17:32:14 +0000675 case SYS_ID_AA64PFR1_EL1: return read_cpuid(ID_AA64PFR1_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100676 case SYS_ID_AA64DFR0_EL1: return read_cpuid(ID_AA64DFR0_EL1);
Mark Rutland7127d432017-02-02 17:32:14 +0000677 case SYS_ID_AA64DFR1_EL1: return read_cpuid(ID_AA64DFR1_EL1);
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100678 case SYS_ID_AA64MMFR0_EL1: return read_cpuid(ID_AA64MMFR0_EL1);
679 case SYS_ID_AA64MMFR1_EL1: return read_cpuid(ID_AA64MMFR1_EL1);
680 case SYS_ID_AA64MMFR2_EL1: return read_cpuid(ID_AA64MMFR2_EL1);
681 case SYS_ID_AA64ISAR0_EL1: return read_cpuid(ID_AA64ISAR0_EL1);
682 case SYS_ID_AA64ISAR1_EL1: return read_cpuid(ID_AA64ISAR1_EL1);
683
684 case SYS_CNTFRQ_EL0: return read_cpuid(CNTFRQ_EL0);
685 case SYS_CTR_EL0: return read_cpuid(CTR_EL0);
686 case SYS_DCZID_EL0: return read_cpuid(DCZID_EL0);
687 default:
688 BUG();
689 return 0;
690 }
691}
692
Marc Zyngier963fcd42015-09-30 11:50:04 +0100693#include <linux/irqchip/arm-gic-v3.h>
694
Marc Zyngier94a9e042015-06-12 12:06:36 +0100695static bool
James Morse18ffa042015-07-21 13:23:29 +0100696feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
697{
Suzuki K Poulose28c5dcb2016-01-26 10:58:16 +0000698 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
James Morse18ffa042015-07-21 13:23:29 +0100699
700 return val >= entry->min_field_value;
701}
702
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100703static bool
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100704has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100705{
706 u64 val;
Marc Zyngier94a9e042015-06-12 12:06:36 +0100707
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100708 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
709 if (scope == SCOPE_SYSTEM)
710 val = read_system_reg(entry->sys_reg);
711 else
712 val = __raw_read_system_reg(entry->sys_reg);
713
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100714 return feature_matches(val, entry);
715}
James Morse338d4f42015-07-22 19:05:54 +0100716
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100717static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
Marc Zyngier963fcd42015-09-30 11:50:04 +0100718{
719 bool has_sre;
720
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100721 if (!has_cpuid_feature(entry, scope))
Marc Zyngier963fcd42015-09-30 11:50:04 +0100722 return false;
723
724 has_sre = gic_enable_sre();
725 if (!has_sre)
726 pr_warn_once("%s present but disabled by higher exception level\n",
727 entry->desc);
728
729 return has_sre;
730}
731
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100732static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
Will Deacond5370f72016-02-02 12:46:24 +0000733{
734 u32 midr = read_cpuid_id();
735 u32 rv_min, rv_max;
736
737 /* Cavium ThunderX pass 1.x and 2.x */
738 rv_min = 0;
739 rv_max = (1 << MIDR_VARIANT_SHIFT) | MIDR_REVISION_MASK;
740
741 return MIDR_IS_CPU_MODEL_RANGE(midr, MIDR_THUNDERX, rv_min, rv_max);
742}
743
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100744static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
Marc Zyngierd88701b2015-01-29 11:24:05 +0000745{
746 return is_kernel_in_hyp_mode();
747}
748
Marc Zyngierd1745912016-06-30 18:40:42 +0100749static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry,
750 int __unused)
751{
Laura Abbottf8fee94e2017-01-10 13:35:49 -0800752 phys_addr_t idmap_addr = __pa_symbol(__hyp_idmap_text_start);
Marc Zyngierd1745912016-06-30 18:40:42 +0100753
754 /*
755 * Activate the lower HYP offset only if:
756 * - the idmap doesn't clash with it,
757 * - the kernel is not running at EL2.
758 */
759 return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode();
760}
761
Will Deaconf79ff2d2017-11-14 14:38:19 +0000762#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
763static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
764
765static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
766 int __unused)
767{
Marc Zyngierda935102018-04-03 12:09:21 +0100768 char const *str = "command line option";
Will Deacon73547722018-04-03 12:09:14 +0100769 u64 pfr0 = read_system_reg(SYS_ID_AA64PFR0_EL1);
770
Marc Zyngierda935102018-04-03 12:09:21 +0100771 /*
772 * For reasons that aren't entirely clear, enabling KPTI on Cavium
773 * ThunderX leads to apparent I-cache corruption of kernel text, which
774 * ends as well as you might imagine. Don't even try.
775 */
Suzuki K Poulosefe64d7d2016-11-08 13:56:20 +0000776 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
Marc Zyngierda935102018-04-03 12:09:21 +0100777 str = "ARM64_WORKAROUND_CAVIUM_27456";
778 __kpti_forced = -1;
779 }
780
781 /* Forced? */
Will Deaconf79ff2d2017-11-14 14:38:19 +0000782 if (__kpti_forced) {
Marc Zyngierda935102018-04-03 12:09:21 +0100783 pr_info_once("kernel page table isolation forced %s by %s\n",
784 __kpti_forced > 0 ? "ON" : "OFF", str);
Will Deaconf79ff2d2017-11-14 14:38:19 +0000785 return __kpti_forced > 0;
786 }
787
788 /* Useful for KASLR robustness */
789 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
790 return true;
791
Jayachandran C2adcb1f2018-04-03 12:09:18 +0100792 /* Don't force KPTI for CPUs that are not vulnerable */
793 switch (read_cpuid_id() & MIDR_CPU_MODEL_MASK) {
794 case MIDR_CAVIUM_THUNDERX2:
795 case MIDR_BRCM_VULCAN:
Will Deacon564907b2018-12-13 13:47:38 +0000796 case MIDR_CORTEX_A53:
797 case MIDR_CORTEX_A55:
798 case MIDR_CORTEX_A57:
799 case MIDR_CORTEX_A72:
800 case MIDR_CORTEX_A73:
Jayachandran C2adcb1f2018-04-03 12:09:18 +0100801 return false;
802 }
803
Will Deacon73547722018-04-03 12:09:14 +0100804 /* Defer to CPU feature registers */
805 return !cpuid_feature_extract_unsigned_field(pfr0,
806 ID_AA64PFR0_CSV3_SHIFT);
Will Deaconbfca1572018-04-03 12:09:09 +0100807}
808
Greg Hackmanneba1ffe2018-04-09 13:48:49 -0700809static int __nocfi kpti_install_ng_mappings(void *__unused)
Will Deacon4025fe12018-04-03 12:09:20 +0100810{
811 typedef void (kpti_remap_fn)(int, int, phys_addr_t);
812 extern kpti_remap_fn idmap_kpti_install_ng_mappings;
813 kpti_remap_fn *remap_fn;
814
815 static bool kpti_applied = false;
816 int cpu = smp_processor_id();
817
818 if (kpti_applied)
819 return 0;
820
821 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
822
823 cpu_install_idmap();
824 remap_fn(cpu, num_online_cpus(), __pa_symbol(swapper_pg_dir));
825 cpu_uninstall_idmap();
826
827 if (!cpu)
828 kpti_applied = true;
829
830 return 0;
Will Deaconf79ff2d2017-11-14 14:38:19 +0000831}
832
833static int __init parse_kpti(char *str)
834{
835 bool enabled;
836 int ret = strtobool(str, &enabled);
837
838 if (ret)
839 return ret;
840
841 __kpti_forced = enabled ? 1 : -1;
842 return 0;
843}
Will Deacon12942d52018-06-22 10:25:25 +0100844early_param("kpti", parse_kpti);
Will Deaconf79ff2d2017-11-14 14:38:19 +0000845#endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
846
James Morseeea59022018-07-20 10:56:16 +0100847static int cpu_copy_el2regs(void *__unused)
848{
849 /*
850 * Copy register values that aren't redirected by hardware.
851 *
852 * Before code patching, we only set tpidr_el1, all CPUs need to copy
853 * this value to tpidr_el2 before we patch the code. Once we've done
854 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
855 * do anything here.
856 */
857 if (!alternatives_applied)
858 write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
859
860 return 0;
861}
862
Marc Zyngier359b7062015-03-27 13:09:23 +0000863static const struct arm64_cpu_capabilities arm64_features[] = {
Marc Zyngier94a9e042015-06-12 12:06:36 +0100864 {
865 .desc = "GIC system register CPU interface",
866 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100867 .def_scope = SCOPE_SYSTEM,
Marc Zyngier963fcd42015-09-30 11:50:04 +0100868 .matches = has_useable_gicv3_cpuif,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100869 .sys_reg = SYS_ID_AA64PFR0_EL1,
870 .field_pos = ID_AA64PFR0_GIC_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000871 .sign = FTR_UNSIGNED,
James Morse18ffa042015-07-21 13:23:29 +0100872 .min_field_value = 1,
Marc Zyngier94a9e042015-06-12 12:06:36 +0100873 },
James Morse338d4f42015-07-22 19:05:54 +0100874#ifdef CONFIG_ARM64_PAN
875 {
876 .desc = "Privileged Access Never",
877 .capability = ARM64_HAS_PAN,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100878 .def_scope = SCOPE_SYSTEM,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100879 .matches = has_cpuid_feature,
880 .sys_reg = SYS_ID_AA64MMFR1_EL1,
881 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000882 .sign = FTR_UNSIGNED,
James Morse338d4f42015-07-22 19:05:54 +0100883 .min_field_value = 1,
884 .enable = cpu_enable_pan,
885 },
886#endif /* CONFIG_ARM64_PAN */
Will Deacon2e94da12015-07-27 16:23:58 +0100887#if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
888 {
889 .desc = "LSE atomic instructions",
890 .capability = ARM64_HAS_LSE_ATOMICS,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100891 .def_scope = SCOPE_SYSTEM,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100892 .matches = has_cpuid_feature,
893 .sys_reg = SYS_ID_AA64ISAR0_EL1,
894 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000895 .sign = FTR_UNSIGNED,
Will Deacon2e94da12015-07-27 16:23:58 +0100896 .min_field_value = 2,
897 },
898#endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
Marc Zyngierd88701b2015-01-29 11:24:05 +0000899 {
Will Deacond5370f72016-02-02 12:46:24 +0000900 .desc = "Software prefetching using PRFM",
901 .capability = ARM64_HAS_NO_HW_PREFETCH,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100902 .def_scope = SCOPE_SYSTEM,
Will Deacond5370f72016-02-02 12:46:24 +0000903 .matches = has_no_hw_prefetch,
904 },
James Morse57f49592016-02-05 14:58:48 +0000905#ifdef CONFIG_ARM64_UAO
906 {
907 .desc = "User Access Override",
908 .capability = ARM64_HAS_UAO,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100909 .def_scope = SCOPE_SYSTEM,
James Morse57f49592016-02-05 14:58:48 +0000910 .matches = has_cpuid_feature,
911 .sys_reg = SYS_ID_AA64MMFR2_EL1,
912 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
913 .min_field_value = 1,
914 .enable = cpu_enable_uao,
915 },
916#endif /* CONFIG_ARM64_UAO */
James Morse70544192016-02-05 14:58:50 +0000917#ifdef CONFIG_ARM64_PAN
918 {
919 .capability = ARM64_ALT_PAN_NOT_UAO,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100920 .def_scope = SCOPE_SYSTEM,
James Morse70544192016-02-05 14:58:50 +0000921 .matches = cpufeature_pan_not_uao,
922 },
923#endif /* CONFIG_ARM64_PAN */
Linus Torvalds588ab3f2016-03-17 20:03:47 -0700924 {
Marc Zyngierd88701b2015-01-29 11:24:05 +0000925 .desc = "Virtualization Host Extensions",
926 .capability = ARM64_HAS_VIRT_HOST_EXTN,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100927 .def_scope = SCOPE_SYSTEM,
Marc Zyngierd88701b2015-01-29 11:24:05 +0000928 .matches = runs_at_el2,
James Morseeea59022018-07-20 10:56:16 +0100929 .enable = cpu_copy_el2regs,
Marc Zyngierd88701b2015-01-29 11:24:05 +0000930 },
Suzuki K Poulose042446a2016-04-18 10:28:36 +0100931 {
932 .desc = "32-bit EL0 Support",
933 .capability = ARM64_HAS_32BIT_EL0,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100934 .def_scope = SCOPE_SYSTEM,
Suzuki K Poulose042446a2016-04-18 10:28:36 +0100935 .matches = has_cpuid_feature,
936 .sys_reg = SYS_ID_AA64PFR0_EL1,
937 .sign = FTR_UNSIGNED,
938 .field_pos = ID_AA64PFR0_EL0_SHIFT,
939 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
940 },
Marc Zyngierd1745912016-06-30 18:40:42 +0100941 {
942 .desc = "Reduced HYP mapping offset",
943 .capability = ARM64_HYP_OFFSET_LOW,
944 .def_scope = SCOPE_SYSTEM,
945 .matches = hyp_offset_low,
946 },
Will Deaconf79ff2d2017-11-14 14:38:19 +0000947#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
948 {
Will Deacon73547722018-04-03 12:09:14 +0100949 .desc = "Kernel page table isolation (KPTI)",
Will Deaconf79ff2d2017-11-14 14:38:19 +0000950 .capability = ARM64_UNMAP_KERNEL_AT_EL0,
951 .def_scope = SCOPE_SYSTEM,
952 .matches = unmap_kernel_at_el0,
Will Deacon4025fe12018-04-03 12:09:20 +0100953 .enable = kpti_install_ng_mappings,
Will Deaconf79ff2d2017-11-14 14:38:19 +0000954 },
955#endif
Marc Zyngier359b7062015-03-27 13:09:23 +0000956 {},
957};
958
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000959#define HWCAP_CAP(reg, field, s, min_value, type, cap) \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100960 { \
961 .desc = #cap, \
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100962 .def_scope = SCOPE_SYSTEM, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100963 .matches = has_cpuid_feature, \
964 .sys_reg = reg, \
965 .field_pos = field, \
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000966 .sign = s, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100967 .min_field_value = min_value, \
968 .hwcap_type = type, \
969 .hwcap = cap, \
970 }
971
Suzuki K Poulosef3efb672016-04-18 10:28:32 +0100972static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000973 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_PMULL),
974 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_AES),
975 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA1),
976 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 +0100977 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 +0000978 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_CRC32),
979 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 +0100980 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA3),
981 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM3),
982 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM4),
983 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 +0000984 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 +0000985 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 +0000986 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 +0000987 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 +0100988 {},
989};
990
991static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100992#ifdef CONFIG_COMPAT
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000993 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
994 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
995 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
996 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
997 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 +0100998#endif
999 {},
1000};
1001
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001002static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001003{
1004 switch (cap->hwcap_type) {
1005 case CAP_HWCAP:
1006 elf_hwcap |= cap->hwcap;
1007 break;
1008#ifdef CONFIG_COMPAT
1009 case CAP_COMPAT_HWCAP:
1010 compat_elf_hwcap |= (u32)cap->hwcap;
1011 break;
1012 case CAP_COMPAT_HWCAP2:
1013 compat_elf_hwcap2 |= (u32)cap->hwcap;
1014 break;
1015#endif
1016 default:
1017 WARN_ON(1);
1018 break;
1019 }
1020}
1021
1022/* Check if we have a particular HWCAP enabled */
Suzuki K Poulosef3efb672016-04-18 10:28:32 +01001023static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001024{
1025 bool rc;
1026
1027 switch (cap->hwcap_type) {
1028 case CAP_HWCAP:
1029 rc = (elf_hwcap & cap->hwcap) != 0;
1030 break;
1031#ifdef CONFIG_COMPAT
1032 case CAP_COMPAT_HWCAP:
1033 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
1034 break;
1035 case CAP_COMPAT_HWCAP2:
1036 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
1037 break;
1038#endif
1039 default:
1040 WARN_ON(1);
1041 rc = false;
1042 }
1043
1044 return rc;
1045}
1046
Suzuki K Poulose75283502016-04-18 10:28:33 +01001047static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001048{
Suzuki K Poulose75283502016-04-18 10:28:33 +01001049 for (; hwcaps->matches; hwcaps++)
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001050 if (hwcaps->matches(hwcaps, hwcaps->def_scope))
Suzuki K Poulose75283502016-04-18 10:28:33 +01001051 cap_set_elf_hwcap(hwcaps);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +01001052}
1053
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001054/*
1055 * Check if the current CPU has a given feature capability.
1056 * Should be called from non-preemptible context.
1057 */
1058static bool __this_cpu_has_cap(const struct arm64_cpu_capabilities *cap_array,
1059 unsigned int cap)
1060{
1061 const struct arm64_cpu_capabilities *caps;
1062
1063 if (WARN_ON(preemptible()))
1064 return false;
1065
Mark Rutland93f339e2018-04-12 12:11:07 +01001066 for (caps = cap_array; caps->matches; caps++)
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001067 if (caps->capability == cap &&
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001068 caps->matches(caps, SCOPE_LOCAL_CPU))
1069 return true;
1070 return false;
1071}
1072
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001073void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
Marc Zyngier359b7062015-03-27 13:09:23 +00001074 const char *info)
1075{
Suzuki K Poulose75283502016-04-18 10:28:33 +01001076 for (; caps->matches; caps++) {
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001077 if (!caps->matches(caps, caps->def_scope))
Marc Zyngier359b7062015-03-27 13:09:23 +00001078 continue;
1079
Suzuki K Poulose75283502016-04-18 10:28:33 +01001080 if (!cpus_have_cap(caps->capability) && caps->desc)
1081 pr_info("%s %s\n", info, caps->desc);
1082 cpus_set_cap(caps->capability);
Marc Zyngier359b7062015-03-27 13:09:23 +00001083 }
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001084}
James Morse1c076302015-07-21 13:23:28 +01001085
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001086/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001087 * Run through the enabled capabilities and enable() it on all active
1088 * CPUs
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001089 */
Andre Przywara8e231852016-06-28 18:07:30 +01001090void __init enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps)
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001091{
Mark Rutlandb1d57082017-05-16 15:18:05 +01001092 for (; caps->matches; caps++) {
1093 unsigned int num = caps->capability;
1094
1095 if (!cpus_have_cap(num))
1096 continue;
1097
1098 /* Ensure cpus_have_const_cap(num) works */
1099 static_branch_enable(&cpu_hwcap_keys[num]);
1100
1101 if (caps->enable) {
James Morse2a6dcb22016-10-18 11:27:46 +01001102 /*
1103 * Use stop_machine() as it schedules the work allowing
1104 * us to modify PSTATE, instead of on_each_cpu() which
1105 * uses an IPI, giving us a PSTATE that disappears when
1106 * we return.
1107 */
Mark Rutland92e7a832018-04-12 12:11:09 +01001108 stop_machine(caps->enable, (void *)caps, cpu_online_mask);
Mark Rutlandb1d57082017-05-16 15:18:05 +01001109 }
1110 }
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001111}
1112
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001113/*
1114 * Flag to indicate if we have computed the system wide
1115 * capabilities based on the boot time active CPUs. This
1116 * will be used to determine if a new booting CPU should
1117 * go through the verification process to make sure that it
1118 * supports the system capabilities, without using a hotplug
1119 * notifier.
1120 */
1121static bool sys_caps_initialised;
1122
1123static inline void set_sys_caps_initialised(void)
1124{
1125 sys_caps_initialised = true;
1126}
1127
1128/*
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001129 * Check for CPU features that are used in early boot
1130 * based on the Boot CPU value.
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001131 */
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001132static void check_early_cpu_features(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00001133{
Suzuki K Pouloseac1ad202016-04-13 14:41:33 +01001134 verify_cpu_run_el();
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001135 verify_cpu_asid_bits();
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001136}
1137
Suzuki K Poulose75283502016-04-18 10:28:33 +01001138static void
1139verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
1140{
1141
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001142 for (; caps->matches; caps++)
1143 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01001144 pr_crit("CPU%d: missing HWCAP: %s\n",
1145 smp_processor_id(), caps->desc);
1146 cpu_die_early();
1147 }
Suzuki K Poulose75283502016-04-18 10:28:33 +01001148}
1149
1150static void
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001151verify_local_cpu_features(const struct arm64_cpu_capabilities *caps_list)
Suzuki K Poulose75283502016-04-18 10:28:33 +01001152{
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001153 const struct arm64_cpu_capabilities *caps = caps_list;
Suzuki K Poulose75283502016-04-18 10:28:33 +01001154 for (; caps->matches; caps++) {
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001155 if (!cpus_have_cap(caps->capability))
Suzuki K Poulose75283502016-04-18 10:28:33 +01001156 continue;
1157 /*
1158 * If the new CPU misses an advertised feature, we cannot proceed
1159 * further, park the cpu.
1160 */
Suzuki K Poulose1e0946d2018-04-03 12:09:16 +01001161 if (!__this_cpu_has_cap(caps_list, caps->capability)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01001162 pr_crit("CPU%d: missing feature: %s\n",
1163 smp_processor_id(), caps->desc);
1164 cpu_die_early();
1165 }
1166 if (caps->enable)
Mark Rutland92e7a832018-04-12 12:11:09 +01001167 caps->enable((void *)caps);
Suzuki K Poulose75283502016-04-18 10:28:33 +01001168 }
1169}
1170
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001171/*
1172 * Run through the enabled system capabilities and enable() it on this CPU.
1173 * The capabilities were decided based on the available CPUs at the boot time.
1174 * Any new CPU should match the system wide status of the capability. If the
1175 * new CPU doesn't have a capability which the system now has enabled, we
1176 * cannot do anything to fix it up and could cause unexpected failures. So
1177 * we park the CPU.
1178 */
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001179static void verify_local_cpu_capabilities(void)
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001180{
Suzuki K Poulose89ba2642016-09-09 14:07:09 +01001181 verify_local_cpu_errata_workarounds();
Suzuki K Poulose75283502016-04-18 10:28:33 +01001182 verify_local_cpu_features(arm64_features);
1183 verify_local_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001184 if (system_supports_32bit_el0())
1185 verify_local_elf_hwcaps(compat_elf_hwcaps);
Marc Zyngier359b7062015-03-27 13:09:23 +00001186}
1187
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001188void check_local_cpu_capabilities(void)
1189{
1190 /*
1191 * All secondary CPUs should conform to the early CPU features
1192 * in use by the kernel based on boot CPU.
1193 */
1194 check_early_cpu_features();
1195
1196 /*
1197 * If we haven't finalised the system capabilities, this CPU gets
1198 * a chance to update the errata work arounds.
1199 * Otherwise, this CPU should verify that it has all the system
1200 * advertised capabilities.
1201 */
1202 if (!sys_caps_initialised)
1203 update_cpu_errata_workarounds();
1204 else
1205 verify_local_cpu_capabilities();
1206}
1207
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001208static void __init setup_feature_capabilities(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00001209{
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001210 update_cpu_capabilities(arm64_features, "detected feature:");
1211 enable_cpu_capabilities(arm64_features);
Marc Zyngier359b7062015-03-27 13:09:23 +00001212}
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001213
Mark Rutlandb1d57082017-05-16 15:18:05 +01001214DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
1215EXPORT_SYMBOL(arm64_const_caps_ready);
1216
1217static void __init mark_const_caps_ready(void)
1218{
1219 static_branch_enable(&arm64_const_caps_ready);
1220}
1221
Marc Zyngier1d648e42018-04-03 12:09:15 +01001222extern const struct arm64_cpu_capabilities arm64_errata[];
1223
Marc Zyngiere3661b12016-04-22 12:25:32 +01001224bool this_cpu_has_cap(unsigned int cap)
1225{
Marc Zyngier1d648e42018-04-03 12:09:15 +01001226 return (__this_cpu_has_cap(arm64_features, cap) ||
1227 __this_cpu_has_cap(arm64_errata, cap));
Marc Zyngiere3661b12016-04-22 12:25:32 +01001228}
1229
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001230void __init setup_cpu_features(void)
1231{
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001232 u32 cwg;
1233 int cls;
1234
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001235 /* Set the CPU feature capabilies */
1236 setup_feature_capabilities();
Andre Przywara8e231852016-06-28 18:07:30 +01001237 enable_errata_workarounds();
Mark Rutlandb1d57082017-05-16 15:18:05 +01001238 mark_const_caps_ready();
Suzuki K Poulose75283502016-04-18 10:28:33 +01001239 setup_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001240
1241 if (system_supports_32bit_el0())
1242 setup_elf_hwcaps(compat_elf_hwcaps);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001243
1244 /* Advertise that we have computed the system capabilities */
1245 set_sys_caps_initialised();
1246
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001247 /*
1248 * Check for sane CTR_EL0.CWG value.
1249 */
1250 cwg = cache_type_cwg();
1251 cls = cache_line_size();
1252 if (!cwg)
1253 pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
1254 cls);
1255 if (L1_CACHE_BYTES < cls)
1256 pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
1257 L1_CACHE_BYTES, cls);
Marc Zyngier359b7062015-03-27 13:09:23 +00001258}
James Morse70544192016-02-05 14:58:50 +00001259
1260static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001261cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
James Morse70544192016-02-05 14:58:50 +00001262{
Suzuki K Poulosefe64d7d2016-11-08 13:56:20 +00001263 return (cpus_have_const_cap(ARM64_HAS_PAN) && !cpus_have_const_cap(ARM64_HAS_UAO));
James Morse70544192016-02-05 14:58:50 +00001264}