blob: ed675061dda0b978a26d062f5fcaca44415fc9c9 [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 Abbott2077be62017-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>
Suzuki K Poulose77c97b42017-01-09 17:28:31 +000033#include <asm/traps.h>
Marc Zyngierd88701b2015-01-29 11:24:05 +000034#include <asm/virt.h>
Marc Zyngier359b7062015-03-27 13:09:23 +000035
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010036unsigned long elf_hwcap __read_mostly;
37EXPORT_SYMBOL_GPL(elf_hwcap);
38
39#ifdef CONFIG_COMPAT
40#define COMPAT_ELF_HWCAP_DEFAULT \
41 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
42 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
43 COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
44 COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
45 COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
46 COMPAT_HWCAP_LPAE)
47unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
48unsigned int compat_elf_hwcap2 __read_mostly;
49#endif
50
51DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010052EXPORT_SYMBOL(cpu_hwcaps);
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +010053
Catalin Marinasefd9e032016-09-05 18:25:48 +010054DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
55EXPORT_SYMBOL(cpu_hwcap_keys);
56
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000057#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010058 { \
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000059 .sign = SIGNED, \
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000060 .visible = VISIBLE, \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010061 .strict = STRICT, \
62 .type = TYPE, \
63 .shift = SHIFT, \
64 .width = WIDTH, \
65 .safe_val = SAFE_VAL, \
66 }
67
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +000068/* Define a feature with unsigned values */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000069#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
70 __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K. Poulose4f0a6062015-11-18 17:08:57 +000071
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +000072/* Define a feature with a signed value */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000073#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
74 __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
Suzuki K Poulose0710cfd2016-01-26 10:58:14 +000075
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010076#define ARM64_FTR_END \
77 { \
78 .width = 0, \
79 }
80
James Morse70544192016-02-05 14:58:50 +000081/* meta feature for alternatives */
82static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +010083cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
84
James Morse70544192016-02-05 14:58:50 +000085
Suzuki K Poulose4aa8a472017-01-09 17:28:32 +000086/*
87 * NOTE: Any changes to the visibility of features should be kept in
88 * sync with the documentation of the CPU feature register ABI.
89 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +010090static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +000091 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
92 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
93 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
94 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
95 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
96 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +010097 ARM64_FTR_END,
98};
99
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100100static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000101 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64PFR0_GIC_SHIFT, 4, 0),
102 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
103 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100104 /* Linux doesn't care about the EL3 */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000105 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64PFR0_EL3_SHIFT, 4, 0),
106 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL2_SHIFT, 4, 0),
107 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
108 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100109 ARM64_FTR_END,
110};
111
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100112static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000113 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
114 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
115 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
116 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100117 /* Linux shouldn't care about secure memory */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000118 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
119 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
120 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100121 /*
122 * Differing PARange is fine as long as all peripherals and memory are mapped
123 * within the minimum PARange of all CPUs
124 */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000125 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100126 ARM64_FTR_END,
127};
128
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100129static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000130 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
131 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
132 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
133 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
134 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
135 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100136 ARM64_FTR_END,
137};
138
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100139static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000140 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
141 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
142 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
143 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
144 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
James Morse406e3082016-02-05 14:58:47 +0000145 ARM64_FTR_END,
146};
147
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100148static const struct arm64_ftr_bits ftr_ctr[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000149 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RAO */
150 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_SAFE, 24, 4, 0), /* CWG */
151 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0), /* ERG */
152 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 1), /* DminLine */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100153 /*
154 * Linux can handle differing I-cache policies. Userspace JITs will
Suzuki K Pouloseee7bc632016-09-09 14:07:08 +0100155 * make use of *minLine.
156 * If we have differing I-cache policies, report it as the weakest - AIVIVT.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100157 */
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000158 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_AIVIVT), /* L1Ip */
159 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* IminLine */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100160 ARM64_FTR_END,
161};
162
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100163struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
164 .name = "SYS_CTR_EL0",
165 .ftr_bits = ftr_ctr
166};
167
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100168static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000169 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 28, 4, 0xf), /* InnerShr */
170 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 24, 4, 0), /* FCSE */
171 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0), /* AuxReg */
172 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 16, 4, 0), /* TCM */
173 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 12, 4, 0), /* ShareLvl */
174 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 8, 4, 0xf), /* OuterShr */
175 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 4, 4, 0), /* PMSA */
176 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 4, 0), /* VMSA */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100177 ARM64_FTR_END,
178};
179
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100180static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000181 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 36, 28, 0),
182 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_PMSVER_SHIFT, 4, 0),
183 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
184 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
185 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
Will Deaconb20d1ba2016-07-25 16:17:52 +0100186 /*
187 * We can instantiate multiple PMU instances with different levels
188 * of support.
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000189 */
190 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
191 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
192 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100193 ARM64_FTR_END,
194};
195
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100196static const struct arm64_ftr_bits ftr_mvfr2[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000197 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 4, 4, 0), /* FPMisc */
198 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 4, 0), /* SIMDMisc */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100199 ARM64_FTR_END,
200};
201
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100202static const struct arm64_ftr_bits ftr_dczid[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000203 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 4, 1, 1), /* DZP */
204 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), /* BS */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100205 ARM64_FTR_END,
206};
207
208
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100209static const struct arm64_ftr_bits ftr_id_isar5[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000210 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_ISAR5_RDM_SHIFT, 4, 0),
211 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_ISAR5_CRC32_SHIFT, 4, 0),
212 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA2_SHIFT, 4, 0),
213 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_ISAR5_SHA1_SHIFT, 4, 0),
214 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_ISAR5_AES_SHIFT, 4, 0),
215 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_ISAR5_SEVL_SHIFT, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100216 ARM64_FTR_END,
217};
218
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100219static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000220 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 4, 4, 0), /* ac2 */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100221 ARM64_FTR_END,
222};
223
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100224static const struct arm64_ftr_bits ftr_id_pfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000225 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 12, 4, 0), /* State3 */
226 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 8, 4, 0), /* State2 */
227 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 4, 4, 0), /* State1 */
228 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 4, 0), /* State0 */
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100229 ARM64_FTR_END,
230};
231
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100232static const struct arm64_ftr_bits ftr_id_dfr0[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000233 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
234 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf), /* PerfMon */
235 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
236 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
237 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
238 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
239 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
240 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000241 ARM64_FTR_END,
242};
243
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100244/*
245 * Common ftr bits for a 32bit register with all hidden, strict
246 * attributes, with 4bit feature fields and a default safe value of
247 * 0. Covers the following 32bit registers:
248 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
249 */
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100250static const struct arm64_ftr_bits ftr_generic_32bits[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000251 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
252 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
253 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
254 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
255 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
256 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
257 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
258 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100259 ARM64_FTR_END,
260};
261
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000262/* Table for a single 32bit feature value */
263static const struct arm64_ftr_bits ftr_single32[] = {
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000264 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100265 ARM64_FTR_END,
266};
267
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000268static const struct arm64_ftr_bits ftr_raz[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100269 ARM64_FTR_END,
270};
271
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100272#define ARM64_FTR_REG(id, table) { \
273 .sys_id = id, \
274 .reg = &(struct arm64_ftr_reg){ \
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100275 .name = #id, \
276 .ftr_bits = &((table)[0]), \
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100277 }}
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100278
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100279static const struct __ftr_reg_entry {
280 u32 sys_id;
281 struct arm64_ftr_reg *reg;
282} arm64_ftr_regs[] = {
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100283
284 /* Op1 = 0, CRn = 0, CRm = 1 */
285 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
286 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
Suzuki K Poulosee5343502016-01-26 10:58:13 +0000287 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100288 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
289 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
290 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
291 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
292
293 /* Op1 = 0, CRn = 0, CRm = 2 */
294 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
295 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
296 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
297 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
298 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
299 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
300 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
301
302 /* Op1 = 0, CRn = 0, CRm = 3 */
303 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
304 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
305 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
306
307 /* Op1 = 0, CRn = 0, CRm = 4 */
308 ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000309 ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_raz),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100310
311 /* Op1 = 0, CRn = 0, CRm = 5 */
312 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000313 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100314
315 /* Op1 = 0, CRn = 0, CRm = 6 */
316 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000317 ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_raz),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100318
319 /* Op1 = 0, CRn = 0, CRm = 7 */
320 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
321 ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
James Morse406e3082016-02-05 14:58:47 +0000322 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100323
324 /* Op1 = 3, CRn = 0, CRm = 0 */
Ard Biesheuvel675b0562016-08-31 11:31:10 +0100325 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100326 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
327
328 /* Op1 = 3, CRn = 14, CRm = 0 */
Suzuki K Pouloseeab43e82017-01-09 17:28:26 +0000329 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100330};
331
332static int search_cmp_ftr_reg(const void *id, const void *regp)
333{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100334 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100335}
336
337/*
338 * get_arm64_ftr_reg - Lookup a feature register entry using its
339 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
340 * ascending order of sys_id , we use binary search to find a matching
341 * entry.
342 *
343 * returns - Upon success, matching ftr_reg entry for id.
344 * - NULL on failure. It is upto the caller to decide
345 * the impact of a failure.
346 */
347static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
348{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100349 const struct __ftr_reg_entry *ret;
350
351 ret = bsearch((const void *)(unsigned long)sys_id,
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100352 arm64_ftr_regs,
353 ARRAY_SIZE(arm64_ftr_regs),
354 sizeof(arm64_ftr_regs[0]),
355 search_cmp_ftr_reg);
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100356 if (ret)
357 return ret->reg;
358 return NULL;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100359}
360
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100361static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
362 s64 ftr_val)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100363{
364 u64 mask = arm64_ftr_mask(ftrp);
365
366 reg &= ~mask;
367 reg |= (ftr_val << ftrp->shift) & mask;
368 return reg;
369}
370
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100371static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
372 s64 cur)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100373{
374 s64 ret = 0;
375
376 switch (ftrp->type) {
377 case FTR_EXACT:
378 ret = ftrp->safe_val;
379 break;
380 case FTR_LOWER_SAFE:
381 ret = new < cur ? new : cur;
382 break;
383 case FTR_HIGHER_SAFE:
384 ret = new > cur ? new : cur;
385 break;
386 default:
387 BUG();
388 }
389
390 return ret;
391}
392
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100393static void __init sort_ftr_regs(void)
394{
Ard Biesheuvel6f2b7ee2016-08-31 11:31:09 +0100395 int i;
396
397 /* Check that the array is sorted so that we can do the binary search */
398 for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
399 BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100400}
401
402/*
403 * Initialise the CPU feature register from Boot CPU values.
404 * Also initiliases the strict_mask for the register.
Mark Rutlandb389d792017-01-09 17:28:24 +0000405 * Any bits that are not covered by an arm64_ftr_bits entry are considered
406 * RES0 for the system-wide value, and must strictly match.
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100407 */
408static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
409{
410 u64 val = 0;
411 u64 strict_mask = ~0x0ULL;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000412 u64 user_mask = 0;
Mark Rutlandb389d792017-01-09 17:28:24 +0000413 u64 valid_mask = 0;
414
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100415 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100416 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
417
418 BUG_ON(!reg);
419
420 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
Mark Rutlandb389d792017-01-09 17:28:24 +0000421 u64 ftr_mask = arm64_ftr_mask(ftrp);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100422 s64 ftr_new = arm64_ftr_value(ftrp, new);
423
424 val = arm64_ftr_set_value(ftrp, val, ftr_new);
Mark Rutlandb389d792017-01-09 17:28:24 +0000425
426 valid_mask |= ftr_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100427 if (!ftrp->strict)
Mark Rutlandb389d792017-01-09 17:28:24 +0000428 strict_mask &= ~ftr_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000429 if (ftrp->visible)
430 user_mask |= ftr_mask;
431 else
432 reg->user_val = arm64_ftr_set_value(ftrp,
433 reg->user_val,
434 ftrp->safe_val);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100435 }
Mark Rutlandb389d792017-01-09 17:28:24 +0000436
437 val &= valid_mask;
438
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100439 reg->sys_val = val;
440 reg->strict_mask = strict_mask;
Suzuki K Poulosefe4fbdb2017-01-09 17:28:30 +0000441 reg->user_mask = user_mask;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100442}
443
444void __init init_cpu_features(struct cpuinfo_arm64 *info)
445{
446 /* Before we start using the tables, make sure it is sorted */
447 sort_ftr_regs();
448
449 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
450 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
451 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
452 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
453 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
454 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
455 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
456 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
457 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000458 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100459 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
460 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100461
462 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
463 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
464 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
465 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
466 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
467 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
468 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
469 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
470 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
471 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
472 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
473 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
474 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
475 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
476 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
477 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
478 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
479 }
480
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100481}
482
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100483static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100484{
Ard Biesheuvel5e49d732016-08-31 11:31:08 +0100485 const struct arm64_ftr_bits *ftrp;
Suzuki K. Poulose3c739b52015-10-19 14:24:45 +0100486
487 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
488 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
489 s64 ftr_new = arm64_ftr_value(ftrp, new);
490
491 if (ftr_cur == ftr_new)
492 continue;
493 /* Find a safe value */
494 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
495 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
496 }
497
498}
499
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100500static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100501{
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100502 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
503
504 BUG_ON(!regp);
505 update_cpu_ftr_reg(regp, val);
506 if ((boot & regp->strict_mask) == (val & regp->strict_mask))
507 return 0;
508 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
509 regp->name, boot, cpu, val);
510 return 1;
511}
512
513/*
514 * Update system wide CPU feature registers with the values from a
515 * non-boot CPU. Also performs SANITY checks to make sure that there
516 * aren't any insane variations from that of the boot CPU.
517 */
518void update_cpu_features(int cpu,
519 struct cpuinfo_arm64 *info,
520 struct cpuinfo_arm64 *boot)
521{
522 int taint = 0;
523
524 /*
525 * The kernel can handle differing I-cache policies, but otherwise
526 * caches should look identical. Userspace JITs will make use of
527 * *minLine.
528 */
529 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
530 info->reg_ctr, boot->reg_ctr);
531
532 /*
533 * Userspace may perform DC ZVA instructions. Mismatched block sizes
534 * could result in too much or too little memory being zeroed if a
535 * process is preempted and migrated between CPUs.
536 */
537 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
538 info->reg_dczid, boot->reg_dczid);
539
540 /* If different, timekeeping will be broken (especially with KVM) */
541 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
542 info->reg_cntfrq, boot->reg_cntfrq);
543
544 /*
545 * The kernel uses self-hosted debug features and expects CPUs to
546 * support identical debug features. We presently need CTX_CMPs, WRPs,
547 * and BRPs to be identical.
548 * ID_AA64DFR1 is currently RES0.
549 */
550 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
551 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
552 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
553 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
554 /*
555 * Even in big.LITTLE, processors should be identical instruction-set
556 * wise.
557 */
558 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
559 info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
560 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
561 info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
562
563 /*
564 * Differing PARange support is fine as long as all peripherals and
565 * memory are mapped within the minimum PARange of all CPUs.
566 * Linux should not care about secure memory.
567 */
568 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
569 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
570 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
571 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
James Morse406e3082016-02-05 14:58:47 +0000572 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
573 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100574
575 /*
576 * EL3 is not our concern.
577 * ID_AA64PFR1 is currently RES0.
578 */
579 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
580 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
581 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
582 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
583
584 /*
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100585 * If we have AArch32, we care about 32-bit features for compat.
586 * If the system doesn't support AArch32, don't update them.
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100587 */
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100588 if (id_aa64pfr0_32bit_el0(read_system_reg(SYS_ID_AA64PFR0_EL1)) &&
589 id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
590
591 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100592 info->reg_id_dfr0, boot->reg_id_dfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100593 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100594 info->reg_id_isar0, boot->reg_id_isar0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100595 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100596 info->reg_id_isar1, boot->reg_id_isar1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100597 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100598 info->reg_id_isar2, boot->reg_id_isar2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100599 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100600 info->reg_id_isar3, boot->reg_id_isar3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100601 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100602 info->reg_id_isar4, boot->reg_id_isar4);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100603 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100604 info->reg_id_isar5, boot->reg_id_isar5);
605
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100606 /*
607 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
608 * ACTLR formats could differ across CPUs and therefore would have to
609 * be trapped for virtualization anyway.
610 */
611 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100612 info->reg_id_mmfr0, boot->reg_id_mmfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100613 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100614 info->reg_id_mmfr1, boot->reg_id_mmfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100615 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100616 info->reg_id_mmfr2, boot->reg_id_mmfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100617 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100618 info->reg_id_mmfr3, boot->reg_id_mmfr3);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100619 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100620 info->reg_id_pfr0, boot->reg_id_pfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100621 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100622 info->reg_id_pfr1, boot->reg_id_pfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100623 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100624 info->reg_mvfr0, boot->reg_mvfr0);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100625 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100626 info->reg_mvfr1, boot->reg_mvfr1);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100627 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100628 info->reg_mvfr2, boot->reg_mvfr2);
Suzuki K Poulosea6dc3cd2016-04-18 10:28:35 +0100629 }
Suzuki K. Poulose3086d392015-10-19 14:24:46 +0100630
631 /*
632 * Mismatched CPU features are a recipe for disaster. Don't even
633 * pretend to support them.
634 */
635 WARN_TAINT_ONCE(taint, TAINT_CPU_OUT_OF_SPEC,
636 "Unsupported CPU feature variation.\n");
Suzuki K. Poulosecdcf8172015-10-19 14:24:42 +0100637}
638
Suzuki K. Pouloseb3f15372015-10-19 14:24:47 +0100639u64 read_system_reg(u32 id)
640{
641 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
642
643 /* We shouldn't get a request for an unsupported register */
644 BUG_ON(!regp);
645 return regp->sys_val;
646}
Marc Zyngier359b7062015-03-27 13:09:23 +0000647
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100648/*
649 * __raw_read_system_reg() - Used by a STARTING cpu before cpuinfo is populated.
650 * Read the system register on the current CPU
651 */
652static u64 __raw_read_system_reg(u32 sys_id)
653{
654 switch (sys_id) {
655 case SYS_ID_PFR0_EL1: return read_cpuid(ID_PFR0_EL1);
656 case SYS_ID_PFR1_EL1: return read_cpuid(ID_PFR1_EL1);
657 case SYS_ID_DFR0_EL1: return read_cpuid(ID_DFR0_EL1);
658 case SYS_ID_MMFR0_EL1: return read_cpuid(ID_MMFR0_EL1);
659 case SYS_ID_MMFR1_EL1: return read_cpuid(ID_MMFR1_EL1);
660 case SYS_ID_MMFR2_EL1: return read_cpuid(ID_MMFR2_EL1);
661 case SYS_ID_MMFR3_EL1: return read_cpuid(ID_MMFR3_EL1);
662 case SYS_ID_ISAR0_EL1: return read_cpuid(ID_ISAR0_EL1);
663 case SYS_ID_ISAR1_EL1: return read_cpuid(ID_ISAR1_EL1);
664 case SYS_ID_ISAR2_EL1: return read_cpuid(ID_ISAR2_EL1);
665 case SYS_ID_ISAR3_EL1: return read_cpuid(ID_ISAR3_EL1);
666 case SYS_ID_ISAR4_EL1: return read_cpuid(ID_ISAR4_EL1);
667 case SYS_ID_ISAR5_EL1: return read_cpuid(ID_ISAR4_EL1);
668 case SYS_MVFR0_EL1: return read_cpuid(MVFR0_EL1);
669 case SYS_MVFR1_EL1: return read_cpuid(MVFR1_EL1);
670 case SYS_MVFR2_EL1: return read_cpuid(MVFR2_EL1);
671
672 case SYS_ID_AA64PFR0_EL1: return read_cpuid(ID_AA64PFR0_EL1);
673 case SYS_ID_AA64PFR1_EL1: return read_cpuid(ID_AA64PFR0_EL1);
674 case SYS_ID_AA64DFR0_EL1: return read_cpuid(ID_AA64DFR0_EL1);
675 case SYS_ID_AA64DFR1_EL1: return read_cpuid(ID_AA64DFR0_EL1);
676 case SYS_ID_AA64MMFR0_EL1: return read_cpuid(ID_AA64MMFR0_EL1);
677 case SYS_ID_AA64MMFR1_EL1: return read_cpuid(ID_AA64MMFR1_EL1);
678 case SYS_ID_AA64MMFR2_EL1: return read_cpuid(ID_AA64MMFR2_EL1);
679 case SYS_ID_AA64ISAR0_EL1: return read_cpuid(ID_AA64ISAR0_EL1);
680 case SYS_ID_AA64ISAR1_EL1: return read_cpuid(ID_AA64ISAR1_EL1);
681
682 case SYS_CNTFRQ_EL0: return read_cpuid(CNTFRQ_EL0);
683 case SYS_CTR_EL0: return read_cpuid(CTR_EL0);
684 case SYS_DCZID_EL0: return read_cpuid(DCZID_EL0);
685 default:
686 BUG();
687 return 0;
688 }
689}
690
Marc Zyngier963fcd42015-09-30 11:50:04 +0100691#include <linux/irqchip/arm-gic-v3.h>
692
Marc Zyngier94a9e042015-06-12 12:06:36 +0100693static bool
James Morse18ffa042015-07-21 13:23:29 +0100694feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
695{
Suzuki K Poulose28c5dcb2016-01-26 10:58:16 +0000696 int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
James Morse18ffa042015-07-21 13:23:29 +0100697
698 return val >= entry->min_field_value;
699}
700
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100701static bool
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100702has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100703{
704 u64 val;
Marc Zyngier94a9e042015-06-12 12:06:36 +0100705
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100706 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
707 if (scope == SCOPE_SYSTEM)
708 val = read_system_reg(entry->sys_reg);
709 else
710 val = __raw_read_system_reg(entry->sys_reg);
711
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100712 return feature_matches(val, entry);
713}
James Morse338d4f42015-07-22 19:05:54 +0100714
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100715static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
Marc Zyngier963fcd42015-09-30 11:50:04 +0100716{
717 bool has_sre;
718
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100719 if (!has_cpuid_feature(entry, scope))
Marc Zyngier963fcd42015-09-30 11:50:04 +0100720 return false;
721
722 has_sre = gic_enable_sre();
723 if (!has_sre)
724 pr_warn_once("%s present but disabled by higher exception level\n",
725 entry->desc);
726
727 return has_sre;
728}
729
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100730static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
Will Deacond5370f72016-02-02 12:46:24 +0000731{
732 u32 midr = read_cpuid_id();
Will Deacond5370f72016-02-02 12:46:24 +0000733
734 /* Cavium ThunderX pass 1.x and 2.x */
Robert Richterfa5ce3d2017-01-13 14:12:09 +0100735 return MIDR_IS_CPU_MODEL_RANGE(midr, MIDR_THUNDERX,
736 MIDR_CPU_VAR_REV(0, 0),
737 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
Will Deacond5370f72016-02-02 12:46:24 +0000738}
739
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100740static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
Marc Zyngierd88701b2015-01-29 11:24:05 +0000741{
742 return is_kernel_in_hyp_mode();
743}
744
Marc Zyngierd1745912016-06-30 18:40:42 +0100745static bool hyp_offset_low(const struct arm64_cpu_capabilities *entry,
746 int __unused)
747{
Laura Abbott2077be62017-01-10 13:35:49 -0800748 phys_addr_t idmap_addr = __pa_symbol(__hyp_idmap_text_start);
Marc Zyngierd1745912016-06-30 18:40:42 +0100749
750 /*
751 * Activate the lower HYP offset only if:
752 * - the idmap doesn't clash with it,
753 * - the kernel is not running at EL2.
754 */
755 return idmap_addr > GENMASK(VA_BITS - 2, 0) && !is_kernel_in_hyp_mode();
756}
757
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000758static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
759{
760 u64 pfr0 = read_system_reg(SYS_ID_AA64PFR0_EL1);
761
762 return cpuid_feature_extract_signed_field(pfr0,
763 ID_AA64PFR0_FP_SHIFT) < 0;
764}
765
Marc Zyngier359b7062015-03-27 13:09:23 +0000766static const struct arm64_cpu_capabilities arm64_features[] = {
Marc Zyngier94a9e042015-06-12 12:06:36 +0100767 {
768 .desc = "GIC system register CPU interface",
769 .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100770 .def_scope = SCOPE_SYSTEM,
Marc Zyngier963fcd42015-09-30 11:50:04 +0100771 .matches = has_useable_gicv3_cpuif,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100772 .sys_reg = SYS_ID_AA64PFR0_EL1,
773 .field_pos = ID_AA64PFR0_GIC_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000774 .sign = FTR_UNSIGNED,
James Morse18ffa042015-07-21 13:23:29 +0100775 .min_field_value = 1,
Marc Zyngier94a9e042015-06-12 12:06:36 +0100776 },
James Morse338d4f42015-07-22 19:05:54 +0100777#ifdef CONFIG_ARM64_PAN
778 {
779 .desc = "Privileged Access Never",
780 .capability = ARM64_HAS_PAN,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100781 .def_scope = SCOPE_SYSTEM,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100782 .matches = has_cpuid_feature,
783 .sys_reg = SYS_ID_AA64MMFR1_EL1,
784 .field_pos = ID_AA64MMFR1_PAN_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000785 .sign = FTR_UNSIGNED,
James Morse338d4f42015-07-22 19:05:54 +0100786 .min_field_value = 1,
787 .enable = cpu_enable_pan,
788 },
789#endif /* CONFIG_ARM64_PAN */
Will Deacon2e94da12015-07-27 16:23:58 +0100790#if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
791 {
792 .desc = "LSE atomic instructions",
793 .capability = ARM64_HAS_LSE_ATOMICS,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100794 .def_scope = SCOPE_SYSTEM,
Suzuki K. Pouloseda8d02d2015-10-19 14:24:51 +0100795 .matches = has_cpuid_feature,
796 .sys_reg = SYS_ID_AA64ISAR0_EL1,
797 .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000798 .sign = FTR_UNSIGNED,
Will Deacon2e94da12015-07-27 16:23:58 +0100799 .min_field_value = 2,
800 },
801#endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
Marc Zyngierd88701b2015-01-29 11:24:05 +0000802 {
Will Deacond5370f72016-02-02 12:46:24 +0000803 .desc = "Software prefetching using PRFM",
804 .capability = ARM64_HAS_NO_HW_PREFETCH,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100805 .def_scope = SCOPE_SYSTEM,
Will Deacond5370f72016-02-02 12:46:24 +0000806 .matches = has_no_hw_prefetch,
807 },
James Morse57f49592016-02-05 14:58:48 +0000808#ifdef CONFIG_ARM64_UAO
809 {
810 .desc = "User Access Override",
811 .capability = ARM64_HAS_UAO,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100812 .def_scope = SCOPE_SYSTEM,
James Morse57f49592016-02-05 14:58:48 +0000813 .matches = has_cpuid_feature,
814 .sys_reg = SYS_ID_AA64MMFR2_EL1,
815 .field_pos = ID_AA64MMFR2_UAO_SHIFT,
816 .min_field_value = 1,
James Morsec8b06e32017-01-09 18:14:02 +0000817 /*
818 * We rely on stop_machine() calling uao_thread_switch() to set
819 * UAO immediately after patching.
820 */
James Morse57f49592016-02-05 14:58:48 +0000821 },
822#endif /* CONFIG_ARM64_UAO */
James Morse70544192016-02-05 14:58:50 +0000823#ifdef CONFIG_ARM64_PAN
824 {
825 .capability = ARM64_ALT_PAN_NOT_UAO,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100826 .def_scope = SCOPE_SYSTEM,
James Morse70544192016-02-05 14:58:50 +0000827 .matches = cpufeature_pan_not_uao,
828 },
829#endif /* CONFIG_ARM64_PAN */
Linus Torvalds588ab3f2016-03-17 20:03:47 -0700830 {
Marc Zyngierd88701b2015-01-29 11:24:05 +0000831 .desc = "Virtualization Host Extensions",
832 .capability = ARM64_HAS_VIRT_HOST_EXTN,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100833 .def_scope = SCOPE_SYSTEM,
Marc Zyngierd88701b2015-01-29 11:24:05 +0000834 .matches = runs_at_el2,
835 },
Suzuki K Poulose042446a2016-04-18 10:28:36 +0100836 {
837 .desc = "32-bit EL0 Support",
838 .capability = ARM64_HAS_32BIT_EL0,
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100839 .def_scope = SCOPE_SYSTEM,
Suzuki K Poulose042446a2016-04-18 10:28:36 +0100840 .matches = has_cpuid_feature,
841 .sys_reg = SYS_ID_AA64PFR0_EL1,
842 .sign = FTR_UNSIGNED,
843 .field_pos = ID_AA64PFR0_EL0_SHIFT,
844 .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
845 },
Marc Zyngierd1745912016-06-30 18:40:42 +0100846 {
847 .desc = "Reduced HYP mapping offset",
848 .capability = ARM64_HYP_OFFSET_LOW,
849 .def_scope = SCOPE_SYSTEM,
850 .matches = hyp_offset_low,
851 },
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000852 {
853 /* FP/SIMD is not implemented */
854 .capability = ARM64_HAS_NO_FPSIMD,
855 .def_scope = SCOPE_SYSTEM,
856 .min_field_value = 0,
857 .matches = has_no_fpsimd,
858 },
Marc Zyngier359b7062015-03-27 13:09:23 +0000859 {},
860};
861
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000862#define HWCAP_CAP(reg, field, s, min_value, type, cap) \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100863 { \
864 .desc = #cap, \
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100865 .def_scope = SCOPE_SYSTEM, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100866 .matches = has_cpuid_feature, \
867 .sys_reg = reg, \
868 .field_pos = field, \
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000869 .sign = s, \
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100870 .min_field_value = min_value, \
871 .hwcap_type = type, \
872 .hwcap = cap, \
873 }
874
Suzuki K Poulosef3efb672016-04-18 10:28:32 +0100875static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000876 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_PMULL),
877 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_AES),
878 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA1),
879 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA2),
880 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_CRC32),
881 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ATOMICS),
Suzuki K Poulosef92f5ce02017-01-12 16:37:28 +0000882 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RDM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDRDM),
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000883 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 +0000884 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 +0000885 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 +0000886 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 +0100887 {},
888};
889
890static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100891#ifdef CONFIG_COMPAT
Suzuki K Pouloseff96f7b2016-01-26 10:58:15 +0000892 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
893 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
894 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
895 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
896 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 +0100897#endif
898 {},
899};
900
Suzuki K Poulosef3efb672016-04-18 10:28:32 +0100901static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100902{
903 switch (cap->hwcap_type) {
904 case CAP_HWCAP:
905 elf_hwcap |= cap->hwcap;
906 break;
907#ifdef CONFIG_COMPAT
908 case CAP_COMPAT_HWCAP:
909 compat_elf_hwcap |= (u32)cap->hwcap;
910 break;
911 case CAP_COMPAT_HWCAP2:
912 compat_elf_hwcap2 |= (u32)cap->hwcap;
913 break;
914#endif
915 default:
916 WARN_ON(1);
917 break;
918 }
919}
920
921/* Check if we have a particular HWCAP enabled */
Suzuki K Poulosef3efb672016-04-18 10:28:32 +0100922static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100923{
924 bool rc;
925
926 switch (cap->hwcap_type) {
927 case CAP_HWCAP:
928 rc = (elf_hwcap & cap->hwcap) != 0;
929 break;
930#ifdef CONFIG_COMPAT
931 case CAP_COMPAT_HWCAP:
932 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
933 break;
934 case CAP_COMPAT_HWCAP2:
935 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
936 break;
937#endif
938 default:
939 WARN_ON(1);
940 rc = false;
941 }
942
943 return rc;
944}
945
Suzuki K Poulose75283502016-04-18 10:28:33 +0100946static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100947{
Suzuki K Poulose77c97b42017-01-09 17:28:31 +0000948 /* We support emulation of accesses to CPU ID feature registers */
949 elf_hwcap |= HWCAP_CPUID;
Suzuki K Poulose75283502016-04-18 10:28:33 +0100950 for (; hwcaps->matches; hwcaps++)
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100951 if (hwcaps->matches(hwcaps, hwcaps->def_scope))
Suzuki K Poulose75283502016-04-18 10:28:33 +0100952 cap_set_elf_hwcap(hwcaps);
Suzuki K. Poulose37b01d532015-10-19 14:24:52 +0100953}
954
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +0100955void update_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
Marc Zyngier359b7062015-03-27 13:09:23 +0000956 const char *info)
957{
Suzuki K Poulose75283502016-04-18 10:28:33 +0100958 for (; caps->matches; caps++) {
Suzuki K Poulose92406f02016-04-22 12:25:31 +0100959 if (!caps->matches(caps, caps->def_scope))
Marc Zyngier359b7062015-03-27 13:09:23 +0000960 continue;
961
Suzuki K Poulose75283502016-04-18 10:28:33 +0100962 if (!cpus_have_cap(caps->capability) && caps->desc)
963 pr_info("%s %s\n", info, caps->desc);
964 cpus_set_cap(caps->capability);
Marc Zyngier359b7062015-03-27 13:09:23 +0000965 }
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +0100966}
James Morse1c076302015-07-21 13:23:28 +0100967
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +0100968/*
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +0100969 * Run through the enabled capabilities and enable() it on all active
970 * CPUs
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +0100971 */
Andre Przywara8e231852016-06-28 18:07:30 +0100972void __init enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps)
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +0100973{
Suzuki K Poulose75283502016-04-18 10:28:33 +0100974 for (; caps->matches; caps++)
975 if (caps->enable && cpus_have_cap(caps->capability))
James Morse2a6dcb22016-10-18 11:27:46 +0100976 /*
977 * Use stop_machine() as it schedules the work allowing
978 * us to modify PSTATE, instead of on_each_cpu() which
979 * uses an IPI, giving us a PSTATE that disappears when
980 * we return.
981 */
982 stop_machine(caps->enable, NULL, cpu_online_mask);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +0100983}
984
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +0100985/*
986 * Flag to indicate if we have computed the system wide
987 * capabilities based on the boot time active CPUs. This
988 * will be used to determine if a new booting CPU should
989 * go through the verification process to make sure that it
990 * supports the system capabilities, without using a hotplug
991 * notifier.
992 */
993static bool sys_caps_initialised;
994
995static inline void set_sys_caps_initialised(void)
996{
997 sys_caps_initialised = true;
998}
999
1000/*
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001001 * Check for CPU features that are used in early boot
1002 * based on the Boot CPU value.
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001003 */
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001004static void check_early_cpu_features(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00001005{
Suzuki K Pouloseac1ad202016-04-13 14:41:33 +01001006 verify_cpu_run_el();
Suzuki K Poulose13f417f2016-02-23 10:31:45 +00001007 verify_cpu_asid_bits();
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001008}
1009
Suzuki K Poulose75283502016-04-18 10:28:33 +01001010static void
1011verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
1012{
1013
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001014 for (; caps->matches; caps++)
1015 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01001016 pr_crit("CPU%d: missing HWCAP: %s\n",
1017 smp_processor_id(), caps->desc);
1018 cpu_die_early();
1019 }
Suzuki K Poulose75283502016-04-18 10:28:33 +01001020}
1021
1022static void
1023verify_local_cpu_features(const struct arm64_cpu_capabilities *caps)
1024{
1025 for (; caps->matches; caps++) {
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001026 if (!cpus_have_cap(caps->capability))
Suzuki K Poulose75283502016-04-18 10:28:33 +01001027 continue;
1028 /*
1029 * If the new CPU misses an advertised feature, we cannot proceed
1030 * further, park the cpu.
1031 */
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001032 if (!caps->matches(caps, SCOPE_LOCAL_CPU)) {
Suzuki K Poulose75283502016-04-18 10:28:33 +01001033 pr_crit("CPU%d: missing feature: %s\n",
1034 smp_processor_id(), caps->desc);
1035 cpu_die_early();
1036 }
1037 if (caps->enable)
1038 caps->enable(NULL);
1039 }
1040}
1041
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001042/*
1043 * Run through the enabled system capabilities and enable() it on this CPU.
1044 * The capabilities were decided based on the available CPUs at the boot time.
1045 * Any new CPU should match the system wide status of the capability. If the
1046 * new CPU doesn't have a capability which the system now has enabled, we
1047 * cannot do anything to fix it up and could cause unexpected failures. So
1048 * we park the CPU.
1049 */
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001050static void verify_local_cpu_capabilities(void)
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001051{
Suzuki K Poulose89ba2642016-09-09 14:07:09 +01001052 verify_local_cpu_errata_workarounds();
Suzuki K Poulose75283502016-04-18 10:28:33 +01001053 verify_local_cpu_features(arm64_features);
1054 verify_local_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001055 if (system_supports_32bit_el0())
1056 verify_local_elf_hwcaps(compat_elf_hwcaps);
Marc Zyngier359b7062015-03-27 13:09:23 +00001057}
1058
Suzuki K Poulosec47a1902016-09-09 14:07:10 +01001059void check_local_cpu_capabilities(void)
1060{
1061 /*
1062 * All secondary CPUs should conform to the early CPU features
1063 * in use by the kernel based on boot CPU.
1064 */
1065 check_early_cpu_features();
1066
1067 /*
1068 * If we haven't finalised the system capabilities, this CPU gets
1069 * a chance to update the errata work arounds.
1070 * Otherwise, this CPU should verify that it has all the system
1071 * advertised capabilities.
1072 */
1073 if (!sys_caps_initialised)
1074 update_cpu_errata_workarounds();
1075 else
1076 verify_local_cpu_capabilities();
1077}
1078
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001079static void __init setup_feature_capabilities(void)
Marc Zyngier359b7062015-03-27 13:09:23 +00001080{
Suzuki K. Poulosece8b6022015-10-19 14:24:49 +01001081 update_cpu_capabilities(arm64_features, "detected feature:");
1082 enable_cpu_capabilities(arm64_features);
Marc Zyngier359b7062015-03-27 13:09:23 +00001083}
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001084
Marc Zyngiere3661b12016-04-22 12:25:32 +01001085/*
1086 * Check if the current CPU has a given feature capability.
1087 * Should be called from non-preemptible context.
1088 */
1089bool this_cpu_has_cap(unsigned int cap)
1090{
1091 const struct arm64_cpu_capabilities *caps;
1092
1093 if (WARN_ON(preemptible()))
1094 return false;
1095
1096 for (caps = arm64_features; caps->desc; caps++)
1097 if (caps->capability == cap && caps->matches)
1098 return caps->matches(caps, SCOPE_LOCAL_CPU);
1099
1100 return false;
1101}
1102
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001103void __init setup_cpu_features(void)
1104{
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001105 u32 cwg;
1106 int cls;
1107
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001108 /* Set the CPU feature capabilies */
1109 setup_feature_capabilities();
Andre Przywara8e231852016-06-28 18:07:30 +01001110 enable_errata_workarounds();
Suzuki K Poulose75283502016-04-18 10:28:33 +01001111 setup_elf_hwcaps(arm64_elf_hwcaps);
Suzuki K Poulose643d7032016-04-18 10:28:37 +01001112
1113 if (system_supports_32bit_el0())
1114 setup_elf_hwcaps(compat_elf_hwcaps);
Suzuki K. Poulosedbb4e152015-10-19 14:24:50 +01001115
1116 /* Advertise that we have computed the system capabilities */
1117 set_sys_caps_initialised();
1118
Suzuki K. Poulose9cdf8ec2015-10-19 14:24:41 +01001119 /*
1120 * Check for sane CTR_EL0.CWG value.
1121 */
1122 cwg = cache_type_cwg();
1123 cls = cache_line_size();
1124 if (!cwg)
1125 pr_warn("No Cache Writeback Granule information, assuming cache line size %d\n",
1126 cls);
1127 if (L1_CACHE_BYTES < cls)
1128 pr_warn("L1_CACHE_BYTES smaller than the Cache Writeback Granule (%d < %d)\n",
1129 L1_CACHE_BYTES, cls);
Marc Zyngier359b7062015-03-27 13:09:23 +00001130}
James Morse70544192016-02-05 14:58:50 +00001131
1132static bool __maybe_unused
Suzuki K Poulose92406f02016-04-22 12:25:31 +01001133cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
James Morse70544192016-02-05 14:58:50 +00001134{
Suzuki K Poulosea4023f682016-11-08 13:56:20 +00001135 return (cpus_have_const_cap(ARM64_HAS_PAN) && !cpus_have_const_cap(ARM64_HAS_UAO));
James Morse70544192016-02-05 14:58:50 +00001136}
Suzuki K Poulose77c97b42017-01-09 17:28:31 +00001137
1138/*
1139 * We emulate only the following system register space.
1140 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 4 - 7]
1141 * See Table C5-6 System instruction encodings for System register accesses,
1142 * ARMv8 ARM(ARM DDI 0487A.f) for more details.
1143 */
1144static inline bool __attribute_const__ is_emulated(u32 id)
1145{
1146 return (sys_reg_Op0(id) == 0x3 &&
1147 sys_reg_CRn(id) == 0x0 &&
1148 sys_reg_Op1(id) == 0x0 &&
1149 (sys_reg_CRm(id) == 0 ||
1150 ((sys_reg_CRm(id) >= 4) && (sys_reg_CRm(id) <= 7))));
1151}
1152
1153/*
1154 * With CRm == 0, reg should be one of :
1155 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
1156 */
1157static inline int emulate_id_reg(u32 id, u64 *valp)
1158{
1159 switch (id) {
1160 case SYS_MIDR_EL1:
1161 *valp = read_cpuid_id();
1162 break;
1163 case SYS_MPIDR_EL1:
1164 *valp = SYS_MPIDR_SAFE_VAL;
1165 break;
1166 case SYS_REVIDR_EL1:
1167 /* IMPLEMENTATION DEFINED values are emulated with 0 */
1168 *valp = 0;
1169 break;
1170 default:
1171 return -EINVAL;
1172 }
1173
1174 return 0;
1175}
1176
1177static int emulate_sys_reg(u32 id, u64 *valp)
1178{
1179 struct arm64_ftr_reg *regp;
1180
1181 if (!is_emulated(id))
1182 return -EINVAL;
1183
1184 if (sys_reg_CRm(id) == 0)
1185 return emulate_id_reg(id, valp);
1186
1187 regp = get_arm64_ftr_reg(id);
1188 if (regp)
1189 *valp = arm64_ftr_reg_user_value(regp);
1190 else
1191 /*
1192 * The untracked registers are either IMPLEMENTATION DEFINED
1193 * (e.g, ID_AFR0_EL1) or reserved RAZ.
1194 */
1195 *valp = 0;
1196 return 0;
1197}
1198
1199static int emulate_mrs(struct pt_regs *regs, u32 insn)
1200{
1201 int rc;
1202 u32 sys_reg, dst;
1203 u64 val;
1204
1205 /*
1206 * sys_reg values are defined as used in mrs/msr instruction.
1207 * shift the imm value to get the encoding.
1208 */
1209 sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
1210 rc = emulate_sys_reg(sys_reg, &val);
1211 if (!rc) {
1212 dst = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
1213 regs->user_regs.regs[dst] = val;
1214 regs->pc += 4;
1215 }
1216
1217 return rc;
1218}
1219
1220static struct undef_hook mrs_hook = {
1221 .instr_mask = 0xfff00000,
1222 .instr_val = 0xd5300000,
1223 .pstate_mask = COMPAT_PSR_MODE_MASK,
1224 .pstate_val = PSR_MODE_EL0t,
1225 .fn = emulate_mrs,
1226};
1227
1228static int __init enable_mrs_emulation(void)
1229{
1230 register_undef_hook(&mrs_hook);
1231 return 0;
1232}
1233
1234late_initcall(enable_mrs_emulation);