1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Contains CPU feature definitions 4 * 5 * Copyright (C) 2015 ARM Ltd. 6 * 7 * A note for the weary kernel hacker: the code here is confusing and hard to 8 * follow! That's partly because it's solving a nasty problem, but also because 9 * there's a little bit of over-abstraction that tends to obscure what's going 10 * on behind a maze of helper functions and macros. 11 * 12 * The basic problem is that hardware folks have started gluing together CPUs 13 * with distinct architectural features; in some cases even creating SoCs where 14 * user-visible instructions are available only on a subset of the available 15 * cores. We try to address this by snapshotting the feature registers of the 16 * boot CPU and comparing these with the feature registers of each secondary 17 * CPU when bringing them up. If there is a mismatch, then we update the 18 * snapshot state to indicate the lowest-common denominator of the feature, 19 * known as the "safe" value. This snapshot state can be queried to view the 20 * "sanitised" value of a feature register. 21 * 22 * The sanitised register values are used to decide which capabilities we 23 * have in the system. These may be in the form of traditional "hwcaps" 24 * advertised to userspace or internal "cpucaps" which are used to configure 25 * things like alternative patching and static keys. While a feature mismatch 26 * may result in a TAINT_CPU_OUT_OF_SPEC kernel taint, a capability mismatch 27 * may prevent a CPU from being onlined at all. 28 * 29 * Some implementation details worth remembering: 30 * 31 * - Mismatched features are *always* sanitised to a "safe" value, which 32 * usually indicates that the feature is not supported. 33 * 34 * - A mismatched feature marked with FTR_STRICT will cause a "SANITY CHECK" 35 * warning when onlining an offending CPU and the kernel will be tainted 36 * with TAINT_CPU_OUT_OF_SPEC. 37 * 38 * - Features marked as FTR_VISIBLE have their sanitised value visible to 39 * userspace. FTR_VISIBLE features in registers that are only visible 40 * to EL0 by trapping *must* have a corresponding HWCAP so that late 41 * onlining of CPUs cannot lead to features disappearing at runtime. 42 * 43 * - A "feature" is typically a 4-bit register field. A "capability" is the 44 * high-level description derived from the sanitised field value. 45 * 46 * - Read the Arm ARM (DDI 0487F.a) section D13.1.3 ("Principles of the ID 47 * scheme for fields in ID registers") to understand when feature fields 48 * may be signed or unsigned (FTR_SIGNED and FTR_UNSIGNED accordingly). 49 * 50 * - KVM exposes its own view of the feature registers to guest operating 51 * systems regardless of FTR_VISIBLE. This is typically driven from the 52 * sanitised register values to allow virtual CPUs to be migrated between 53 * arbitrary physical CPUs, but some features not present on the host are 54 * also advertised and emulated. Look at sys_reg_descs[] for the gory 55 * details. 56 * 57 * - If the arm64_ftr_bits[] for a register has a missing field, then this 58 * field is treated as STRICT RES0, including for read_sanitised_ftr_reg(). 59 * This is stronger than FTR_HIDDEN and can be used to hide features from 60 * KVM guests. 61 */ 62 63 #define pr_fmt(fmt) "CPU features: " fmt 64 65 #include <linux/bsearch.h> 66 #include <linux/cpumask.h> 67 #include <linux/crash_dump.h> 68 #include <linux/sort.h> 69 #include <linux/stop_machine.h> 70 #include <linux/sysfs.h> 71 #include <linux/types.h> 72 #include <linux/minmax.h> 73 #include <linux/mm.h> 74 #include <linux/cpu.h> 75 #include <linux/kasan.h> 76 #include <linux/percpu.h> 77 78 #include <asm/cpu.h> 79 #include <asm/cpufeature.h> 80 #include <asm/cpu_ops.h> 81 #include <asm/fpsimd.h> 82 #include <asm/hwcap.h> 83 #include <asm/insn.h> 84 #include <asm/kvm_host.h> 85 #include <asm/mmu_context.h> 86 #include <asm/mte.h> 87 #include <asm/processor.h> 88 #include <asm/smp.h> 89 #include <asm/sysreg.h> 90 #include <asm/traps.h> 91 #include <asm/vectors.h> 92 #include <asm/virt.h> 93 94 /* Kernel representation of AT_HWCAP and AT_HWCAP2 */ 95 static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly; 96 97 #ifdef CONFIG_COMPAT 98 #define COMPAT_ELF_HWCAP_DEFAULT \ 99 (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\ 100 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\ 101 COMPAT_HWCAP_TLS|COMPAT_HWCAP_IDIV|\ 102 COMPAT_HWCAP_LPAE) 103 unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT; 104 unsigned int compat_elf_hwcap2 __read_mostly; 105 #endif 106 107 DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS); 108 EXPORT_SYMBOL(cpu_hwcaps); 109 static struct arm64_cpu_capabilities const __ro_after_init *cpu_hwcaps_ptrs[ARM64_NCAPS]; 110 111 DECLARE_BITMAP(boot_capabilities, ARM64_NCAPS); 112 113 bool arm64_use_ng_mappings = false; 114 EXPORT_SYMBOL(arm64_use_ng_mappings); 115 116 DEFINE_PER_CPU_READ_MOSTLY(const char *, this_cpu_vector) = vectors; 117 118 /* 119 * Permit PER_LINUX32 and execve() of 32-bit binaries even if not all CPUs 120 * support it? 121 */ 122 static bool __read_mostly allow_mismatched_32bit_el0; 123 124 /* 125 * Static branch enabled only if allow_mismatched_32bit_el0 is set and we have 126 * seen at least one CPU capable of 32-bit EL0. 127 */ 128 DEFINE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0); 129 130 /* 131 * Mask of CPUs supporting 32-bit EL0. 132 * Only valid if arm64_mismatched_32bit_el0 is enabled. 133 */ 134 static cpumask_var_t cpu_32bit_el0_mask __cpumask_var_read_mostly; 135 136 void dump_cpu_features(void) 137 { 138 /* file-wide pr_fmt adds "CPU features: " prefix */ 139 pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps); 140 } 141 142 #define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \ 143 { \ 144 .sign = SIGNED, \ 145 .visible = VISIBLE, \ 146 .strict = STRICT, \ 147 .type = TYPE, \ 148 .shift = SHIFT, \ 149 .width = WIDTH, \ 150 .safe_val = SAFE_VAL, \ 151 } 152 153 /* Define a feature with unsigned values */ 154 #define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \ 155 __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) 156 157 /* Define a feature with a signed value */ 158 #define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \ 159 __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) 160 161 #define ARM64_FTR_END \ 162 { \ 163 .width = 0, \ 164 } 165 166 static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap); 167 168 static bool __system_matches_cap(unsigned int n); 169 170 /* 171 * NOTE: Any changes to the visibility of features should be kept in 172 * sync with the documentation of the CPU feature register ABI. 173 */ 174 static const struct arm64_ftr_bits ftr_id_aa64isar0[] = { 175 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_RNDR_SHIFT, 4, 0), 176 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_TLB_SHIFT, 4, 0), 177 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_TS_SHIFT, 4, 0), 178 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_FHM_SHIFT, 4, 0), 179 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_DP_SHIFT, 4, 0), 180 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SM4_SHIFT, 4, 0), 181 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SM3_SHIFT, 4, 0), 182 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA3_SHIFT, 4, 0), 183 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_RDM_SHIFT, 4, 0), 184 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_ATOMIC_SHIFT, 4, 0), 185 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_CRC32_SHIFT, 4, 0), 186 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA2_SHIFT, 4, 0), 187 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA1_SHIFT, 4, 0), 188 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_AES_SHIFT, 4, 0), 189 ARM64_FTR_END, 190 }; 191 192 static const struct arm64_ftr_bits ftr_id_aa64isar1[] = { 193 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_I8MM_SHIFT, 4, 0), 194 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_DGH_SHIFT, 4, 0), 195 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_BF16_SHIFT, 4, 0), 196 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_SPECRES_SHIFT, 4, 0), 197 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_SB_SHIFT, 4, 0), 198 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_FRINTTS_SHIFT, 4, 0), 199 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH), 200 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_GPI_SHIFT, 4, 0), 201 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH), 202 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_GPA_SHIFT, 4, 0), 203 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_LRCPC_SHIFT, 4, 0), 204 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_FCMA_SHIFT, 4, 0), 205 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_JSCVT_SHIFT, 4, 0), 206 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH), 207 FTR_STRICT, FTR_EXACT, ID_AA64ISAR1_EL1_API_SHIFT, 4, 0), 208 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH), 209 FTR_STRICT, FTR_EXACT, ID_AA64ISAR1_EL1_APA_SHIFT, 4, 0), 210 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_DPB_SHIFT, 4, 0), 211 ARM64_FTR_END, 212 }; 213 214 static const struct arm64_ftr_bits ftr_id_aa64isar2[] = { 215 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_CSSC_SHIFT, 4, 0), 216 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_RPRFM_SHIFT, 4, 0), 217 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_AA64ISAR2_EL1_BC_SHIFT, 4, 0), 218 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH), 219 FTR_STRICT, FTR_EXACT, ID_AA64ISAR2_EL1_APA3_SHIFT, 4, 0), 220 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH), 221 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_GPA3_SHIFT, 4, 0), 222 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_RPRES_SHIFT, 4, 0), 223 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_WFxT_SHIFT, 4, 0), 224 ARM64_FTR_END, 225 }; 226 227 static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = { 228 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV3_SHIFT, 4, 0), 229 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV2_SHIFT, 4, 0), 230 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_DIT_SHIFT, 4, 0), 231 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AMU_SHIFT, 4, 0), 232 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_MPAM_SHIFT, 4, 0), 233 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SEL2_SHIFT, 4, 0), 234 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 235 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SVE_SHIFT, 4, 0), 236 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_RAS_SHIFT, 4, 0), 237 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_GIC_SHIFT, 4, 0), 238 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AdvSIMD_SHIFT, 4, ID_AA64PFR0_EL1_AdvSIMD_NI), 239 S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_FP_SHIFT, 4, ID_AA64PFR0_EL1_FP_NI), 240 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL3_SHIFT, 4, 0), 241 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL2_SHIFT, 4, 0), 242 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL1_SHIFT, 4, ID_AA64PFR0_EL1_ELx_64BIT_ONLY), 243 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL0_SHIFT, 4, ID_AA64PFR0_EL1_ELx_64BIT_ONLY), 244 ARM64_FTR_END, 245 }; 246 247 static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = { 248 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 249 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_SME_SHIFT, 4, 0), 250 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_MPAM_frac_SHIFT, 4, 0), 251 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_RAS_frac_SHIFT, 4, 0), 252 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_MTE), 253 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_MTE_SHIFT, 4, ID_AA64PFR1_EL1_MTE_NI), 254 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_SSBS_SHIFT, 4, ID_AA64PFR1_EL1_SSBS_NI), 255 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_BTI), 256 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_BT_SHIFT, 4, 0), 257 ARM64_FTR_END, 258 }; 259 260 static const struct arm64_ftr_bits ftr_id_aa64zfr0[] = { 261 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 262 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_F64MM_SHIFT, 4, 0), 263 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 264 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_F32MM_SHIFT, 4, 0), 265 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 266 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_I8MM_SHIFT, 4, 0), 267 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 268 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SM4_SHIFT, 4, 0), 269 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 270 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SHA3_SHIFT, 4, 0), 271 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 272 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_BF16_SHIFT, 4, 0), 273 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 274 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_BitPerm_SHIFT, 4, 0), 275 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 276 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_AES_SHIFT, 4, 0), 277 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE), 278 FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SVEver_SHIFT, 4, 0), 279 ARM64_FTR_END, 280 }; 281 282 static const struct arm64_ftr_bits ftr_id_aa64smfr0[] = { 283 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 284 FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_FA64_SHIFT, 1, 0), 285 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 286 FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_I16I64_SHIFT, 4, 0), 287 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 288 FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F64F64_SHIFT, 1, 0), 289 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 290 FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_I8I32_SHIFT, 4, 0), 291 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 292 FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F16F32_SHIFT, 1, 0), 293 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 294 FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_B16F32_SHIFT, 1, 0), 295 ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME), 296 FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F32F32_SHIFT, 1, 0), 297 ARM64_FTR_END, 298 }; 299 300 static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = { 301 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_ECV_SHIFT, 4, 0), 302 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_FGT_SHIFT, 4, 0), 303 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_EXS_SHIFT, 4, 0), 304 /* 305 * Page size not being supported at Stage-2 is not fatal. You 306 * just give up KVM if PAGE_SIZE isn't supported there. Go fix 307 * your favourite nesting hypervisor. 308 * 309 * There is a small corner case where the hypervisor explicitly 310 * advertises a given granule size at Stage-2 (value 2) on some 311 * vCPUs, and uses the fallback to Stage-1 (value 0) for other 312 * vCPUs. Although this is not forbidden by the architecture, it 313 * indicates that the hypervisor is being silly (or buggy). 314 * 315 * We make no effort to cope with this and pretend that if these 316 * fields are inconsistent across vCPUs, then it isn't worth 317 * trying to bring KVM up. 318 */ 319 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN4_2_SHIFT, 4, 1), 320 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN64_2_SHIFT, 4, 1), 321 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN16_2_SHIFT, 4, 1), 322 /* 323 * We already refuse to boot CPUs that don't support our configured 324 * page size, so we can only detect mismatches for a page size other 325 * than the one we're currently using. Unfortunately, SoCs like this 326 * exist in the wild so, even though we don't like it, we'll have to go 327 * along with it and treat them as non-strict. 328 */ 329 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN4_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN4_NI), 330 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN64_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN64_NI), 331 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN16_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN16_NI), 332 333 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_BIGENDEL0_SHIFT, 4, 0), 334 /* Linux shouldn't care about secure memory */ 335 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_SNSMEM_SHIFT, 4, 0), 336 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_BIGEND_SHIFT, 4, 0), 337 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_ASIDBITS_SHIFT, 4, 0), 338 /* 339 * Differing PARange is fine as long as all peripherals and memory are mapped 340 * within the minimum PARange of all CPUs 341 */ 342 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_PARANGE_SHIFT, 4, 0), 343 ARM64_FTR_END, 344 }; 345 346 static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = { 347 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_TIDCP1_SHIFT, 4, 0), 348 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_AFP_SHIFT, 4, 0), 349 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_ETS_SHIFT, 4, 0), 350 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_TWED_SHIFT, 4, 0), 351 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_XNX_SHIFT, 4, 0), 352 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_AA64MMFR1_EL1_SpecSEI_SHIFT, 4, 0), 353 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_PAN_SHIFT, 4, 0), 354 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_LO_SHIFT, 4, 0), 355 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_HPDS_SHIFT, 4, 0), 356 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_VH_SHIFT, 4, 0), 357 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_VMIDBits_SHIFT, 4, 0), 358 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_HAFDBS_SHIFT, 4, 0), 359 ARM64_FTR_END, 360 }; 361 362 static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = { 363 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_E0PD_SHIFT, 4, 0), 364 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_EVT_SHIFT, 4, 0), 365 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_BBM_SHIFT, 4, 0), 366 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_TTL_SHIFT, 4, 0), 367 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_FWB_SHIFT, 4, 0), 368 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_IDS_SHIFT, 4, 0), 369 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_AT_SHIFT, 4, 0), 370 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_ST_SHIFT, 4, 0), 371 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_NV_SHIFT, 4, 0), 372 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_CCIDX_SHIFT, 4, 0), 373 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_VARange_SHIFT, 4, 0), 374 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_IESB_SHIFT, 4, 0), 375 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_LSM_SHIFT, 4, 0), 376 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_UAO_SHIFT, 4, 0), 377 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_CnP_SHIFT, 4, 0), 378 ARM64_FTR_END, 379 }; 380 381 static const struct arm64_ftr_bits ftr_ctr[] = { 382 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */ 383 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_DIC_SHIFT, 1, 1), 384 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_IDC_SHIFT, 1, 1), 385 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_EL0_CWG_SHIFT, 4, 0), 386 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_EL0_ERG_SHIFT, 4, 0), 387 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_DminLine_SHIFT, 4, 1), 388 /* 389 * Linux can handle differing I-cache policies. Userspace JITs will 390 * make use of *minLine. 391 * If we have differing I-cache policies, report it as the weakest - VIPT. 392 */ 393 ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, CTR_EL0_L1Ip_SHIFT, 2, CTR_EL0_L1Ip_VIPT), /* L1Ip */ 394 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_IminLine_SHIFT, 4, 0), 395 ARM64_FTR_END, 396 }; 397 398 static struct arm64_ftr_override __ro_after_init no_override = { }; 399 400 struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = { 401 .name = "SYS_CTR_EL0", 402 .ftr_bits = ftr_ctr, 403 .override = &no_override, 404 }; 405 406 static const struct arm64_ftr_bits ftr_id_mmfr0[] = { 407 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_InnerShr_SHIFT, 4, 0xf), 408 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_FCSE_SHIFT, 4, 0), 409 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_AuxReg_SHIFT, 4, 0), 410 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_TCM_SHIFT, 4, 0), 411 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_ShareLvl_SHIFT, 4, 0), 412 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_OuterShr_SHIFT, 4, 0xf), 413 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_PMSA_SHIFT, 4, 0), 414 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_EL1_VMSA_SHIFT, 4, 0), 415 ARM64_FTR_END, 416 }; 417 418 static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = { 419 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_DoubleLock_SHIFT, 4, 0), 420 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_PMSVer_SHIFT, 4, 0), 421 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_CTX_CMPs_SHIFT, 4, 0), 422 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_WRPs_SHIFT, 4, 0), 423 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_BRPs_SHIFT, 4, 0), 424 /* 425 * We can instantiate multiple PMU instances with different levels 426 * of support. 427 */ 428 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_EL1_PMUVer_SHIFT, 4, 0), 429 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_EL1_DebugVer_SHIFT, 4, 0x6), 430 ARM64_FTR_END, 431 }; 432 433 static const struct arm64_ftr_bits ftr_mvfr0[] = { 434 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPRound_SHIFT, 4, 0), 435 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPShVec_SHIFT, 4, 0), 436 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPSqrt_SHIFT, 4, 0), 437 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPDivide_SHIFT, 4, 0), 438 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPTrap_SHIFT, 4, 0), 439 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPDP_SHIFT, 4, 0), 440 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_FPSP_SHIFT, 4, 0), 441 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_EL1_SIMDReg_SHIFT, 4, 0), 442 ARM64_FTR_END, 443 }; 444 445 static const struct arm64_ftr_bits ftr_mvfr1[] = { 446 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDFMAC_SHIFT, 4, 0), 447 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_FPHP_SHIFT, 4, 0), 448 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDHP_SHIFT, 4, 0), 449 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDSP_SHIFT, 4, 0), 450 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDInt_SHIFT, 4, 0), 451 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_SIMDLS_SHIFT, 4, 0), 452 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_FPDNaN_SHIFT, 4, 0), 453 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_EL1_FPFtZ_SHIFT, 4, 0), 454 ARM64_FTR_END, 455 }; 456 457 static const struct arm64_ftr_bits ftr_mvfr2[] = { 458 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR2_EL1_FPMisc_SHIFT, 4, 0), 459 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR2_EL1_SIMDMisc_SHIFT, 4, 0), 460 ARM64_FTR_END, 461 }; 462 463 static const struct arm64_ftr_bits ftr_dczid[] = { 464 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, DCZID_EL0_DZP_SHIFT, 1, 1), 465 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, DCZID_EL0_BS_SHIFT, 4, 0), 466 ARM64_FTR_END, 467 }; 468 469 static const struct arm64_ftr_bits ftr_gmid[] = { 470 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, GMID_EL1_BS_SHIFT, 4, 0), 471 ARM64_FTR_END, 472 }; 473 474 static const struct arm64_ftr_bits ftr_id_isar0[] = { 475 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Divide_SHIFT, 4, 0), 476 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Debug_SHIFT, 4, 0), 477 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Coproc_SHIFT, 4, 0), 478 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_CmpBranch_SHIFT, 4, 0), 479 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_BitField_SHIFT, 4, 0), 480 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_BitCount_SHIFT, 4, 0), 481 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_EL1_Swap_SHIFT, 4, 0), 482 ARM64_FTR_END, 483 }; 484 485 static const struct arm64_ftr_bits ftr_id_isar5[] = { 486 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_RDM_SHIFT, 4, 0), 487 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_CRC32_SHIFT, 4, 0), 488 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_SHA2_SHIFT, 4, 0), 489 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_SHA1_SHIFT, 4, 0), 490 ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_AES_SHIFT, 4, 0), 491 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_EL1_SEVL_SHIFT, 4, 0), 492 ARM64_FTR_END, 493 }; 494 495 static const struct arm64_ftr_bits ftr_id_mmfr4[] = { 496 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_EVT_SHIFT, 4, 0), 497 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_CCIDX_SHIFT, 4, 0), 498 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_LSM_SHIFT, 4, 0), 499 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_HPDS_SHIFT, 4, 0), 500 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_CnP_SHIFT, 4, 0), 501 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_XNX_SHIFT, 4, 0), 502 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EL1_AC2_SHIFT, 4, 0), 503 504 /* 505 * SpecSEI = 1 indicates that the PE might generate an SError on an 506 * external abort on speculative read. It is safe to assume that an 507 * SError might be generated than it will not be. Hence it has been 508 * classified as FTR_HIGHER_SAFE. 509 */ 510 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_MMFR4_EL1_SpecSEI_SHIFT, 4, 0), 511 ARM64_FTR_END, 512 }; 513 514 static const struct arm64_ftr_bits ftr_id_isar4[] = { 515 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_SWP_frac_SHIFT, 4, 0), 516 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_PSR_M_SHIFT, 4, 0), 517 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_SynchPrim_frac_SHIFT, 4, 0), 518 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_Barrier_SHIFT, 4, 0), 519 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_SMC_SHIFT, 4, 0), 520 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_Writeback_SHIFT, 4, 0), 521 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_WithShifts_SHIFT, 4, 0), 522 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_EL1_Unpriv_SHIFT, 4, 0), 523 ARM64_FTR_END, 524 }; 525 526 static const struct arm64_ftr_bits ftr_id_mmfr5[] = { 527 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR5_EL1_ETS_SHIFT, 4, 0), 528 ARM64_FTR_END, 529 }; 530 531 static const struct arm64_ftr_bits ftr_id_isar6[] = { 532 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_I8MM_SHIFT, 4, 0), 533 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_BF16_SHIFT, 4, 0), 534 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_SPECRES_SHIFT, 4, 0), 535 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_SB_SHIFT, 4, 0), 536 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_FHM_SHIFT, 4, 0), 537 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_DP_SHIFT, 4, 0), 538 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_EL1_JSCVT_SHIFT, 4, 0), 539 ARM64_FTR_END, 540 }; 541 542 static const struct arm64_ftr_bits ftr_id_pfr0[] = { 543 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_DIT_SHIFT, 4, 0), 544 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_CSV2_SHIFT, 4, 0), 545 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State3_SHIFT, 4, 0), 546 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State2_SHIFT, 4, 0), 547 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State1_SHIFT, 4, 0), 548 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_EL1_State0_SHIFT, 4, 0), 549 ARM64_FTR_END, 550 }; 551 552 static const struct arm64_ftr_bits ftr_id_pfr1[] = { 553 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_GIC_SHIFT, 4, 0), 554 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Virt_frac_SHIFT, 4, 0), 555 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Sec_frac_SHIFT, 4, 0), 556 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_GenTimer_SHIFT, 4, 0), 557 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Virtualization_SHIFT, 4, 0), 558 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_MProgMod_SHIFT, 4, 0), 559 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_Security_SHIFT, 4, 0), 560 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_EL1_ProgMod_SHIFT, 4, 0), 561 ARM64_FTR_END, 562 }; 563 564 static const struct arm64_ftr_bits ftr_id_pfr2[] = { 565 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR2_EL1_SSBS_SHIFT, 4, 0), 566 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR2_EL1_CSV3_SHIFT, 4, 0), 567 ARM64_FTR_END, 568 }; 569 570 static const struct arm64_ftr_bits ftr_id_dfr0[] = { 571 /* [31:28] TraceFilt */ 572 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_DFR0_EL1_PerfMon_SHIFT, 4, 0), 573 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_MProfDbg_SHIFT, 4, 0), 574 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_MMapTrc_SHIFT, 4, 0), 575 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_CopTrc_SHIFT, 4, 0), 576 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_MMapDbg_SHIFT, 4, 0), 577 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_CopSDbg_SHIFT, 4, 0), 578 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_EL1_CopDbg_SHIFT, 4, 0), 579 ARM64_FTR_END, 580 }; 581 582 static const struct arm64_ftr_bits ftr_id_dfr1[] = { 583 S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR1_EL1_MTPMU_SHIFT, 4, 0), 584 ARM64_FTR_END, 585 }; 586 587 static const struct arm64_ftr_bits ftr_zcr[] = { 588 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 589 ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_WIDTH, 0), /* LEN */ 590 ARM64_FTR_END, 591 }; 592 593 static const struct arm64_ftr_bits ftr_smcr[] = { 594 ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 595 SMCR_ELx_LEN_SHIFT, SMCR_ELx_LEN_WIDTH, 0), /* LEN */ 596 ARM64_FTR_END, 597 }; 598 599 /* 600 * Common ftr bits for a 32bit register with all hidden, strict 601 * attributes, with 4bit feature fields and a default safe value of 602 * 0. Covers the following 32bit registers: 603 * id_isar[1-3], id_mmfr[1-3] 604 */ 605 static const struct arm64_ftr_bits ftr_generic_32bits[] = { 606 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0), 607 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0), 608 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0), 609 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0), 610 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0), 611 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0), 612 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0), 613 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0), 614 ARM64_FTR_END, 615 }; 616 617 /* Table for a single 32bit feature value */ 618 static const struct arm64_ftr_bits ftr_single32[] = { 619 ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0), 620 ARM64_FTR_END, 621 }; 622 623 static const struct arm64_ftr_bits ftr_raz[] = { 624 ARM64_FTR_END, 625 }; 626 627 #define __ARM64_FTR_REG_OVERRIDE(id_str, id, table, ovr) { \ 628 .sys_id = id, \ 629 .reg = &(struct arm64_ftr_reg){ \ 630 .name = id_str, \ 631 .override = (ovr), \ 632 .ftr_bits = &((table)[0]), \ 633 }} 634 635 #define ARM64_FTR_REG_OVERRIDE(id, table, ovr) \ 636 __ARM64_FTR_REG_OVERRIDE(#id, id, table, ovr) 637 638 #define ARM64_FTR_REG(id, table) \ 639 __ARM64_FTR_REG_OVERRIDE(#id, id, table, &no_override) 640 641 struct arm64_ftr_override __ro_after_init id_aa64mmfr1_override; 642 struct arm64_ftr_override __ro_after_init id_aa64pfr0_override; 643 struct arm64_ftr_override __ro_after_init id_aa64pfr1_override; 644 struct arm64_ftr_override __ro_after_init id_aa64zfr0_override; 645 struct arm64_ftr_override __ro_after_init id_aa64smfr0_override; 646 struct arm64_ftr_override __ro_after_init id_aa64isar1_override; 647 struct arm64_ftr_override __ro_after_init id_aa64isar2_override; 648 649 static const struct __ftr_reg_entry { 650 u32 sys_id; 651 struct arm64_ftr_reg *reg; 652 } arm64_ftr_regs[] = { 653 654 /* Op1 = 0, CRn = 0, CRm = 1 */ 655 ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0), 656 ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_id_pfr1), 657 ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0), 658 ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0), 659 ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits), 660 ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits), 661 ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits), 662 663 /* Op1 = 0, CRn = 0, CRm = 2 */ 664 ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_id_isar0), 665 ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits), 666 ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits), 667 ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits), 668 ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_id_isar4), 669 ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5), 670 ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4), 671 ARM64_FTR_REG(SYS_ID_ISAR6_EL1, ftr_id_isar6), 672 673 /* Op1 = 0, CRn = 0, CRm = 3 */ 674 ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_mvfr0), 675 ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_mvfr1), 676 ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2), 677 ARM64_FTR_REG(SYS_ID_PFR2_EL1, ftr_id_pfr2), 678 ARM64_FTR_REG(SYS_ID_DFR1_EL1, ftr_id_dfr1), 679 ARM64_FTR_REG(SYS_ID_MMFR5_EL1, ftr_id_mmfr5), 680 681 /* Op1 = 0, CRn = 0, CRm = 4 */ 682 ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0, 683 &id_aa64pfr0_override), 684 ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1, 685 &id_aa64pfr1_override), 686 ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ZFR0_EL1, ftr_id_aa64zfr0, 687 &id_aa64zfr0_override), 688 ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64SMFR0_EL1, ftr_id_aa64smfr0, 689 &id_aa64smfr0_override), 690 691 /* Op1 = 0, CRn = 0, CRm = 5 */ 692 ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0), 693 ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz), 694 695 /* Op1 = 0, CRn = 0, CRm = 6 */ 696 ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0), 697 ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1, 698 &id_aa64isar1_override), 699 ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ISAR2_EL1, ftr_id_aa64isar2, 700 &id_aa64isar2_override), 701 702 /* Op1 = 0, CRn = 0, CRm = 7 */ 703 ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0), 704 ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1, 705 &id_aa64mmfr1_override), 706 ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2), 707 708 /* Op1 = 0, CRn = 1, CRm = 2 */ 709 ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr), 710 ARM64_FTR_REG(SYS_SMCR_EL1, ftr_smcr), 711 712 /* Op1 = 1, CRn = 0, CRm = 0 */ 713 ARM64_FTR_REG(SYS_GMID_EL1, ftr_gmid), 714 715 /* Op1 = 3, CRn = 0, CRm = 0 */ 716 { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 }, 717 ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid), 718 719 /* Op1 = 3, CRn = 14, CRm = 0 */ 720 ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32), 721 }; 722 723 static int search_cmp_ftr_reg(const void *id, const void *regp) 724 { 725 return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id; 726 } 727 728 /* 729 * get_arm64_ftr_reg_nowarn - Looks up a feature register entry using 730 * its sys_reg() encoding. With the array arm64_ftr_regs sorted in the 731 * ascending order of sys_id, we use binary search to find a matching 732 * entry. 733 * 734 * returns - Upon success, matching ftr_reg entry for id. 735 * - NULL on failure. It is upto the caller to decide 736 * the impact of a failure. 737 */ 738 static struct arm64_ftr_reg *get_arm64_ftr_reg_nowarn(u32 sys_id) 739 { 740 const struct __ftr_reg_entry *ret; 741 742 ret = bsearch((const void *)(unsigned long)sys_id, 743 arm64_ftr_regs, 744 ARRAY_SIZE(arm64_ftr_regs), 745 sizeof(arm64_ftr_regs[0]), 746 search_cmp_ftr_reg); 747 if (ret) 748 return ret->reg; 749 return NULL; 750 } 751 752 /* 753 * get_arm64_ftr_reg - Looks up a feature register entry using 754 * its sys_reg() encoding. This calls get_arm64_ftr_reg_nowarn(). 755 * 756 * returns - Upon success, matching ftr_reg entry for id. 757 * - NULL on failure but with an WARN_ON(). 758 */ 759 struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id) 760 { 761 struct arm64_ftr_reg *reg; 762 763 reg = get_arm64_ftr_reg_nowarn(sys_id); 764 765 /* 766 * Requesting a non-existent register search is an error. Warn 767 * and let the caller handle it. 768 */ 769 WARN_ON(!reg); 770 return reg; 771 } 772 773 static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg, 774 s64 ftr_val) 775 { 776 u64 mask = arm64_ftr_mask(ftrp); 777 778 reg &= ~mask; 779 reg |= (ftr_val << ftrp->shift) & mask; 780 return reg; 781 } 782 783 static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, 784 s64 cur) 785 { 786 s64 ret = 0; 787 788 switch (ftrp->type) { 789 case FTR_EXACT: 790 ret = ftrp->safe_val; 791 break; 792 case FTR_LOWER_SAFE: 793 ret = min(new, cur); 794 break; 795 case FTR_HIGHER_OR_ZERO_SAFE: 796 if (!cur || !new) 797 break; 798 fallthrough; 799 case FTR_HIGHER_SAFE: 800 ret = max(new, cur); 801 break; 802 default: 803 BUG(); 804 } 805 806 return ret; 807 } 808 809 static void __init sort_ftr_regs(void) 810 { 811 unsigned int i; 812 813 for (i = 0; i < ARRAY_SIZE(arm64_ftr_regs); i++) { 814 const struct arm64_ftr_reg *ftr_reg = arm64_ftr_regs[i].reg; 815 const struct arm64_ftr_bits *ftr_bits = ftr_reg->ftr_bits; 816 unsigned int j = 0; 817 818 /* 819 * Features here must be sorted in descending order with respect 820 * to their shift values and should not overlap with each other. 821 */ 822 for (; ftr_bits->width != 0; ftr_bits++, j++) { 823 unsigned int width = ftr_reg->ftr_bits[j].width; 824 unsigned int shift = ftr_reg->ftr_bits[j].shift; 825 unsigned int prev_shift; 826 827 WARN((shift + width) > 64, 828 "%s has invalid feature at shift %d\n", 829 ftr_reg->name, shift); 830 831 /* 832 * Skip the first feature. There is nothing to 833 * compare against for now. 834 */ 835 if (j == 0) 836 continue; 837 838 prev_shift = ftr_reg->ftr_bits[j - 1].shift; 839 WARN((shift + width) > prev_shift, 840 "%s has feature overlap at shift %d\n", 841 ftr_reg->name, shift); 842 } 843 844 /* 845 * Skip the first register. There is nothing to 846 * compare against for now. 847 */ 848 if (i == 0) 849 continue; 850 /* 851 * Registers here must be sorted in ascending order with respect 852 * to sys_id for subsequent binary search in get_arm64_ftr_reg() 853 * to work correctly. 854 */ 855 BUG_ON(arm64_ftr_regs[i].sys_id <= arm64_ftr_regs[i - 1].sys_id); 856 } 857 } 858 859 /* 860 * Initialise the CPU feature register from Boot CPU values. 861 * Also initiliases the strict_mask for the register. 862 * Any bits that are not covered by an arm64_ftr_bits entry are considered 863 * RES0 for the system-wide value, and must strictly match. 864 */ 865 static void init_cpu_ftr_reg(u32 sys_reg, u64 new) 866 { 867 u64 val = 0; 868 u64 strict_mask = ~0x0ULL; 869 u64 user_mask = 0; 870 u64 valid_mask = 0; 871 872 const struct arm64_ftr_bits *ftrp; 873 struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg); 874 875 if (!reg) 876 return; 877 878 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) { 879 u64 ftr_mask = arm64_ftr_mask(ftrp); 880 s64 ftr_new = arm64_ftr_value(ftrp, new); 881 s64 ftr_ovr = arm64_ftr_value(ftrp, reg->override->val); 882 883 if ((ftr_mask & reg->override->mask) == ftr_mask) { 884 s64 tmp = arm64_ftr_safe_value(ftrp, ftr_ovr, ftr_new); 885 char *str = NULL; 886 887 if (ftr_ovr != tmp) { 888 /* Unsafe, remove the override */ 889 reg->override->mask &= ~ftr_mask; 890 reg->override->val &= ~ftr_mask; 891 tmp = ftr_ovr; 892 str = "ignoring override"; 893 } else if (ftr_new != tmp) { 894 /* Override was valid */ 895 ftr_new = tmp; 896 str = "forced"; 897 } else if (ftr_ovr == tmp) { 898 /* Override was the safe value */ 899 str = "already set"; 900 } 901 902 if (str) 903 pr_warn("%s[%d:%d]: %s to %llx\n", 904 reg->name, 905 ftrp->shift + ftrp->width - 1, 906 ftrp->shift, str, tmp); 907 } else if ((ftr_mask & reg->override->val) == ftr_mask) { 908 reg->override->val &= ~ftr_mask; 909 pr_warn("%s[%d:%d]: impossible override, ignored\n", 910 reg->name, 911 ftrp->shift + ftrp->width - 1, 912 ftrp->shift); 913 } 914 915 val = arm64_ftr_set_value(ftrp, val, ftr_new); 916 917 valid_mask |= ftr_mask; 918 if (!ftrp->strict) 919 strict_mask &= ~ftr_mask; 920 if (ftrp->visible) 921 user_mask |= ftr_mask; 922 else 923 reg->user_val = arm64_ftr_set_value(ftrp, 924 reg->user_val, 925 ftrp->safe_val); 926 } 927 928 val &= valid_mask; 929 930 reg->sys_val = val; 931 reg->strict_mask = strict_mask; 932 reg->user_mask = user_mask; 933 } 934 935 extern const struct arm64_cpu_capabilities arm64_errata[]; 936 static const struct arm64_cpu_capabilities arm64_features[]; 937 938 static void __init 939 init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps) 940 { 941 for (; caps->matches; caps++) { 942 if (WARN(caps->capability >= ARM64_NCAPS, 943 "Invalid capability %d\n", caps->capability)) 944 continue; 945 if (WARN(cpu_hwcaps_ptrs[caps->capability], 946 "Duplicate entry for capability %d\n", 947 caps->capability)) 948 continue; 949 cpu_hwcaps_ptrs[caps->capability] = caps; 950 } 951 } 952 953 static void __init init_cpu_hwcaps_indirect_list(void) 954 { 955 init_cpu_hwcaps_indirect_list_from_array(arm64_features); 956 init_cpu_hwcaps_indirect_list_from_array(arm64_errata); 957 } 958 959 static void __init setup_boot_cpu_capabilities(void); 960 961 static void init_32bit_cpu_features(struct cpuinfo_32bit *info) 962 { 963 init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0); 964 init_cpu_ftr_reg(SYS_ID_DFR1_EL1, info->reg_id_dfr1); 965 init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0); 966 init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1); 967 init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2); 968 init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3); 969 init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4); 970 init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5); 971 init_cpu_ftr_reg(SYS_ID_ISAR6_EL1, info->reg_id_isar6); 972 init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0); 973 init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1); 974 init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2); 975 init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3); 976 init_cpu_ftr_reg(SYS_ID_MMFR4_EL1, info->reg_id_mmfr4); 977 init_cpu_ftr_reg(SYS_ID_MMFR5_EL1, info->reg_id_mmfr5); 978 init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0); 979 init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1); 980 init_cpu_ftr_reg(SYS_ID_PFR2_EL1, info->reg_id_pfr2); 981 init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0); 982 init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1); 983 init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2); 984 } 985 986 void __init init_cpu_features(struct cpuinfo_arm64 *info) 987 { 988 /* Before we start using the tables, make sure it is sorted */ 989 sort_ftr_regs(); 990 991 init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr); 992 init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid); 993 init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq); 994 init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0); 995 init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1); 996 init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0); 997 init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1); 998 init_cpu_ftr_reg(SYS_ID_AA64ISAR2_EL1, info->reg_id_aa64isar2); 999 init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0); 1000 init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1); 1001 init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2); 1002 init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0); 1003 init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1); 1004 init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0); 1005 init_cpu_ftr_reg(SYS_ID_AA64SMFR0_EL1, info->reg_id_aa64smfr0); 1006 1007 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) 1008 init_32bit_cpu_features(&info->aarch32); 1009 1010 if (IS_ENABLED(CONFIG_ARM64_SVE) && 1011 id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1))) { 1012 info->reg_zcr = read_zcr_features(); 1013 init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr); 1014 vec_init_vq_map(ARM64_VEC_SVE); 1015 } 1016 1017 if (IS_ENABLED(CONFIG_ARM64_SME) && 1018 id_aa64pfr1_sme(read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1))) { 1019 info->reg_smcr = read_smcr_features(); 1020 /* 1021 * We mask out SMPS since even if the hardware 1022 * supports priorities the kernel does not at present 1023 * and we block access to them. 1024 */ 1025 info->reg_smidr = read_cpuid(SMIDR_EL1) & ~SMIDR_EL1_SMPS; 1026 init_cpu_ftr_reg(SYS_SMCR_EL1, info->reg_smcr); 1027 vec_init_vq_map(ARM64_VEC_SME); 1028 } 1029 1030 if (id_aa64pfr1_mte(info->reg_id_aa64pfr1)) 1031 init_cpu_ftr_reg(SYS_GMID_EL1, info->reg_gmid); 1032 1033 /* 1034 * Initialize the indirect array of CPU hwcaps capabilities pointers 1035 * before we handle the boot CPU below. 1036 */ 1037 init_cpu_hwcaps_indirect_list(); 1038 1039 /* 1040 * Detect and enable early CPU capabilities based on the boot CPU, 1041 * after we have initialised the CPU feature infrastructure. 1042 */ 1043 setup_boot_cpu_capabilities(); 1044 } 1045 1046 static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new) 1047 { 1048 const struct arm64_ftr_bits *ftrp; 1049 1050 for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) { 1051 s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val); 1052 s64 ftr_new = arm64_ftr_value(ftrp, new); 1053 1054 if (ftr_cur == ftr_new) 1055 continue; 1056 /* Find a safe value */ 1057 ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur); 1058 reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new); 1059 } 1060 1061 } 1062 1063 static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot) 1064 { 1065 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id); 1066 1067 if (!regp) 1068 return 0; 1069 1070 update_cpu_ftr_reg(regp, val); 1071 if ((boot & regp->strict_mask) == (val & regp->strict_mask)) 1072 return 0; 1073 pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n", 1074 regp->name, boot, cpu, val); 1075 return 1; 1076 } 1077 1078 static void relax_cpu_ftr_reg(u32 sys_id, int field) 1079 { 1080 const struct arm64_ftr_bits *ftrp; 1081 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id); 1082 1083 if (!regp) 1084 return; 1085 1086 for (ftrp = regp->ftr_bits; ftrp->width; ftrp++) { 1087 if (ftrp->shift == field) { 1088 regp->strict_mask &= ~arm64_ftr_mask(ftrp); 1089 break; 1090 } 1091 } 1092 1093 /* Bogus field? */ 1094 WARN_ON(!ftrp->width); 1095 } 1096 1097 static void lazy_init_32bit_cpu_features(struct cpuinfo_arm64 *info, 1098 struct cpuinfo_arm64 *boot) 1099 { 1100 static bool boot_cpu_32bit_regs_overridden = false; 1101 1102 if (!allow_mismatched_32bit_el0 || boot_cpu_32bit_regs_overridden) 1103 return; 1104 1105 if (id_aa64pfr0_32bit_el0(boot->reg_id_aa64pfr0)) 1106 return; 1107 1108 boot->aarch32 = info->aarch32; 1109 init_32bit_cpu_features(&boot->aarch32); 1110 boot_cpu_32bit_regs_overridden = true; 1111 } 1112 1113 static int update_32bit_cpu_features(int cpu, struct cpuinfo_32bit *info, 1114 struct cpuinfo_32bit *boot) 1115 { 1116 int taint = 0; 1117 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 1118 1119 /* 1120 * If we don't have AArch32 at EL1, then relax the strictness of 1121 * EL1-dependent register fields to avoid spurious sanity check fails. 1122 */ 1123 if (!id_aa64pfr0_32bit_el1(pfr0)) { 1124 relax_cpu_ftr_reg(SYS_ID_ISAR4_EL1, ID_ISAR4_EL1_SMC_SHIFT); 1125 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Virt_frac_SHIFT); 1126 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Sec_frac_SHIFT); 1127 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Virtualization_SHIFT); 1128 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_Security_SHIFT); 1129 relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_EL1_ProgMod_SHIFT); 1130 } 1131 1132 taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu, 1133 info->reg_id_dfr0, boot->reg_id_dfr0); 1134 taint |= check_update_ftr_reg(SYS_ID_DFR1_EL1, cpu, 1135 info->reg_id_dfr1, boot->reg_id_dfr1); 1136 taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu, 1137 info->reg_id_isar0, boot->reg_id_isar0); 1138 taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu, 1139 info->reg_id_isar1, boot->reg_id_isar1); 1140 taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu, 1141 info->reg_id_isar2, boot->reg_id_isar2); 1142 taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu, 1143 info->reg_id_isar3, boot->reg_id_isar3); 1144 taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu, 1145 info->reg_id_isar4, boot->reg_id_isar4); 1146 taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu, 1147 info->reg_id_isar5, boot->reg_id_isar5); 1148 taint |= check_update_ftr_reg(SYS_ID_ISAR6_EL1, cpu, 1149 info->reg_id_isar6, boot->reg_id_isar6); 1150 1151 /* 1152 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and 1153 * ACTLR formats could differ across CPUs and therefore would have to 1154 * be trapped for virtualization anyway. 1155 */ 1156 taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu, 1157 info->reg_id_mmfr0, boot->reg_id_mmfr0); 1158 taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu, 1159 info->reg_id_mmfr1, boot->reg_id_mmfr1); 1160 taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu, 1161 info->reg_id_mmfr2, boot->reg_id_mmfr2); 1162 taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu, 1163 info->reg_id_mmfr3, boot->reg_id_mmfr3); 1164 taint |= check_update_ftr_reg(SYS_ID_MMFR4_EL1, cpu, 1165 info->reg_id_mmfr4, boot->reg_id_mmfr4); 1166 taint |= check_update_ftr_reg(SYS_ID_MMFR5_EL1, cpu, 1167 info->reg_id_mmfr5, boot->reg_id_mmfr5); 1168 taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu, 1169 info->reg_id_pfr0, boot->reg_id_pfr0); 1170 taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu, 1171 info->reg_id_pfr1, boot->reg_id_pfr1); 1172 taint |= check_update_ftr_reg(SYS_ID_PFR2_EL1, cpu, 1173 info->reg_id_pfr2, boot->reg_id_pfr2); 1174 taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu, 1175 info->reg_mvfr0, boot->reg_mvfr0); 1176 taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu, 1177 info->reg_mvfr1, boot->reg_mvfr1); 1178 taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu, 1179 info->reg_mvfr2, boot->reg_mvfr2); 1180 1181 return taint; 1182 } 1183 1184 /* 1185 * Update system wide CPU feature registers with the values from a 1186 * non-boot CPU. Also performs SANITY checks to make sure that there 1187 * aren't any insane variations from that of the boot CPU. 1188 */ 1189 void update_cpu_features(int cpu, 1190 struct cpuinfo_arm64 *info, 1191 struct cpuinfo_arm64 *boot) 1192 { 1193 int taint = 0; 1194 1195 /* 1196 * The kernel can handle differing I-cache policies, but otherwise 1197 * caches should look identical. Userspace JITs will make use of 1198 * *minLine. 1199 */ 1200 taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu, 1201 info->reg_ctr, boot->reg_ctr); 1202 1203 /* 1204 * Userspace may perform DC ZVA instructions. Mismatched block sizes 1205 * could result in too much or too little memory being zeroed if a 1206 * process is preempted and migrated between CPUs. 1207 */ 1208 taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu, 1209 info->reg_dczid, boot->reg_dczid); 1210 1211 /* If different, timekeeping will be broken (especially with KVM) */ 1212 taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu, 1213 info->reg_cntfrq, boot->reg_cntfrq); 1214 1215 /* 1216 * The kernel uses self-hosted debug features and expects CPUs to 1217 * support identical debug features. We presently need CTX_CMPs, WRPs, 1218 * and BRPs to be identical. 1219 * ID_AA64DFR1 is currently RES0. 1220 */ 1221 taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu, 1222 info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0); 1223 taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu, 1224 info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1); 1225 /* 1226 * Even in big.LITTLE, processors should be identical instruction-set 1227 * wise. 1228 */ 1229 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu, 1230 info->reg_id_aa64isar0, boot->reg_id_aa64isar0); 1231 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu, 1232 info->reg_id_aa64isar1, boot->reg_id_aa64isar1); 1233 taint |= check_update_ftr_reg(SYS_ID_AA64ISAR2_EL1, cpu, 1234 info->reg_id_aa64isar2, boot->reg_id_aa64isar2); 1235 1236 /* 1237 * Differing PARange support is fine as long as all peripherals and 1238 * memory are mapped within the minimum PARange of all CPUs. 1239 * Linux should not care about secure memory. 1240 */ 1241 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu, 1242 info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0); 1243 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu, 1244 info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1); 1245 taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu, 1246 info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2); 1247 1248 taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu, 1249 info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0); 1250 taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu, 1251 info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1); 1252 1253 taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu, 1254 info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0); 1255 1256 taint |= check_update_ftr_reg(SYS_ID_AA64SMFR0_EL1, cpu, 1257 info->reg_id_aa64smfr0, boot->reg_id_aa64smfr0); 1258 1259 if (IS_ENABLED(CONFIG_ARM64_SVE) && 1260 id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1))) { 1261 info->reg_zcr = read_zcr_features(); 1262 taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu, 1263 info->reg_zcr, boot->reg_zcr); 1264 1265 /* Probe vector lengths */ 1266 if (!system_capabilities_finalized()) 1267 vec_update_vq_map(ARM64_VEC_SVE); 1268 } 1269 1270 if (IS_ENABLED(CONFIG_ARM64_SME) && 1271 id_aa64pfr1_sme(read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1))) { 1272 info->reg_smcr = read_smcr_features(); 1273 /* 1274 * We mask out SMPS since even if the hardware 1275 * supports priorities the kernel does not at present 1276 * and we block access to them. 1277 */ 1278 info->reg_smidr = read_cpuid(SMIDR_EL1) & ~SMIDR_EL1_SMPS; 1279 taint |= check_update_ftr_reg(SYS_SMCR_EL1, cpu, 1280 info->reg_smcr, boot->reg_smcr); 1281 1282 /* Probe vector lengths */ 1283 if (!system_capabilities_finalized()) 1284 vec_update_vq_map(ARM64_VEC_SME); 1285 } 1286 1287 /* 1288 * The kernel uses the LDGM/STGM instructions and the number of tags 1289 * they read/write depends on the GMID_EL1.BS field. Check that the 1290 * value is the same on all CPUs. 1291 */ 1292 if (IS_ENABLED(CONFIG_ARM64_MTE) && 1293 id_aa64pfr1_mte(info->reg_id_aa64pfr1)) { 1294 taint |= check_update_ftr_reg(SYS_GMID_EL1, cpu, 1295 info->reg_gmid, boot->reg_gmid); 1296 } 1297 1298 /* 1299 * If we don't have AArch32 at all then skip the checks entirely 1300 * as the register values may be UNKNOWN and we're not going to be 1301 * using them for anything. 1302 * 1303 * This relies on a sanitised view of the AArch64 ID registers 1304 * (e.g. SYS_ID_AA64PFR0_EL1), so we call it last. 1305 */ 1306 if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) { 1307 lazy_init_32bit_cpu_features(info, boot); 1308 taint |= update_32bit_cpu_features(cpu, &info->aarch32, 1309 &boot->aarch32); 1310 } 1311 1312 /* 1313 * Mismatched CPU features are a recipe for disaster. Don't even 1314 * pretend to support them. 1315 */ 1316 if (taint) { 1317 pr_warn_once("Unsupported CPU feature variation detected.\n"); 1318 add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); 1319 } 1320 } 1321 1322 u64 read_sanitised_ftr_reg(u32 id) 1323 { 1324 struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id); 1325 1326 if (!regp) 1327 return 0; 1328 return regp->sys_val; 1329 } 1330 EXPORT_SYMBOL_GPL(read_sanitised_ftr_reg); 1331 1332 #define read_sysreg_case(r) \ 1333 case r: val = read_sysreg_s(r); break; 1334 1335 /* 1336 * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated. 1337 * Read the system register on the current CPU 1338 */ 1339 u64 __read_sysreg_by_encoding(u32 sys_id) 1340 { 1341 struct arm64_ftr_reg *regp; 1342 u64 val; 1343 1344 switch (sys_id) { 1345 read_sysreg_case(SYS_ID_PFR0_EL1); 1346 read_sysreg_case(SYS_ID_PFR1_EL1); 1347 read_sysreg_case(SYS_ID_PFR2_EL1); 1348 read_sysreg_case(SYS_ID_DFR0_EL1); 1349 read_sysreg_case(SYS_ID_DFR1_EL1); 1350 read_sysreg_case(SYS_ID_MMFR0_EL1); 1351 read_sysreg_case(SYS_ID_MMFR1_EL1); 1352 read_sysreg_case(SYS_ID_MMFR2_EL1); 1353 read_sysreg_case(SYS_ID_MMFR3_EL1); 1354 read_sysreg_case(SYS_ID_MMFR4_EL1); 1355 read_sysreg_case(SYS_ID_MMFR5_EL1); 1356 read_sysreg_case(SYS_ID_ISAR0_EL1); 1357 read_sysreg_case(SYS_ID_ISAR1_EL1); 1358 read_sysreg_case(SYS_ID_ISAR2_EL1); 1359 read_sysreg_case(SYS_ID_ISAR3_EL1); 1360 read_sysreg_case(SYS_ID_ISAR4_EL1); 1361 read_sysreg_case(SYS_ID_ISAR5_EL1); 1362 read_sysreg_case(SYS_ID_ISAR6_EL1); 1363 read_sysreg_case(SYS_MVFR0_EL1); 1364 read_sysreg_case(SYS_MVFR1_EL1); 1365 read_sysreg_case(SYS_MVFR2_EL1); 1366 1367 read_sysreg_case(SYS_ID_AA64PFR0_EL1); 1368 read_sysreg_case(SYS_ID_AA64PFR1_EL1); 1369 read_sysreg_case(SYS_ID_AA64ZFR0_EL1); 1370 read_sysreg_case(SYS_ID_AA64SMFR0_EL1); 1371 read_sysreg_case(SYS_ID_AA64DFR0_EL1); 1372 read_sysreg_case(SYS_ID_AA64DFR1_EL1); 1373 read_sysreg_case(SYS_ID_AA64MMFR0_EL1); 1374 read_sysreg_case(SYS_ID_AA64MMFR1_EL1); 1375 read_sysreg_case(SYS_ID_AA64MMFR2_EL1); 1376 read_sysreg_case(SYS_ID_AA64ISAR0_EL1); 1377 read_sysreg_case(SYS_ID_AA64ISAR1_EL1); 1378 read_sysreg_case(SYS_ID_AA64ISAR2_EL1); 1379 1380 read_sysreg_case(SYS_CNTFRQ_EL0); 1381 read_sysreg_case(SYS_CTR_EL0); 1382 read_sysreg_case(SYS_DCZID_EL0); 1383 1384 default: 1385 BUG(); 1386 return 0; 1387 } 1388 1389 regp = get_arm64_ftr_reg(sys_id); 1390 if (regp) { 1391 val &= ~regp->override->mask; 1392 val |= (regp->override->val & regp->override->mask); 1393 } 1394 1395 return val; 1396 } 1397 1398 #include <linux/irqchip/arm-gic-v3.h> 1399 1400 static bool 1401 has_always(const struct arm64_cpu_capabilities *entry, int scope) 1402 { 1403 return true; 1404 } 1405 1406 static bool 1407 feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry) 1408 { 1409 int val = cpuid_feature_extract_field_width(reg, entry->field_pos, 1410 entry->field_width, 1411 entry->sign); 1412 1413 return val >= entry->min_field_value; 1414 } 1415 1416 static u64 1417 read_scoped_sysreg(const struct arm64_cpu_capabilities *entry, int scope) 1418 { 1419 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible()); 1420 if (scope == SCOPE_SYSTEM) 1421 return read_sanitised_ftr_reg(entry->sys_reg); 1422 else 1423 return __read_sysreg_by_encoding(entry->sys_reg); 1424 } 1425 1426 static bool 1427 has_user_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope) 1428 { 1429 int mask; 1430 struct arm64_ftr_reg *regp; 1431 u64 val = read_scoped_sysreg(entry, scope); 1432 1433 regp = get_arm64_ftr_reg(entry->sys_reg); 1434 if (!regp) 1435 return false; 1436 1437 mask = cpuid_feature_extract_unsigned_field_width(regp->user_mask, 1438 entry->field_pos, 1439 entry->field_width); 1440 if (!mask) 1441 return false; 1442 1443 return feature_matches(val, entry); 1444 } 1445 1446 static bool 1447 has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope) 1448 { 1449 u64 val = read_scoped_sysreg(entry, scope); 1450 return feature_matches(val, entry); 1451 } 1452 1453 const struct cpumask *system_32bit_el0_cpumask(void) 1454 { 1455 if (!system_supports_32bit_el0()) 1456 return cpu_none_mask; 1457 1458 if (static_branch_unlikely(&arm64_mismatched_32bit_el0)) 1459 return cpu_32bit_el0_mask; 1460 1461 return cpu_possible_mask; 1462 } 1463 1464 static int __init parse_32bit_el0_param(char *str) 1465 { 1466 allow_mismatched_32bit_el0 = true; 1467 return 0; 1468 } 1469 early_param("allow_mismatched_32bit_el0", parse_32bit_el0_param); 1470 1471 static ssize_t aarch32_el0_show(struct device *dev, 1472 struct device_attribute *attr, char *buf) 1473 { 1474 const struct cpumask *mask = system_32bit_el0_cpumask(); 1475 1476 return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(mask)); 1477 } 1478 static const DEVICE_ATTR_RO(aarch32_el0); 1479 1480 static int __init aarch32_el0_sysfs_init(void) 1481 { 1482 if (!allow_mismatched_32bit_el0) 1483 return 0; 1484 1485 return device_create_file(cpu_subsys.dev_root, &dev_attr_aarch32_el0); 1486 } 1487 device_initcall(aarch32_el0_sysfs_init); 1488 1489 static bool has_32bit_el0(const struct arm64_cpu_capabilities *entry, int scope) 1490 { 1491 if (!has_cpuid_feature(entry, scope)) 1492 return allow_mismatched_32bit_el0; 1493 1494 if (scope == SCOPE_SYSTEM) 1495 pr_info("detected: 32-bit EL0 Support\n"); 1496 1497 return true; 1498 } 1499 1500 static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope) 1501 { 1502 bool has_sre; 1503 1504 if (!has_cpuid_feature(entry, scope)) 1505 return false; 1506 1507 has_sre = gic_enable_sre(); 1508 if (!has_sre) 1509 pr_warn_once("%s present but disabled by higher exception level\n", 1510 entry->desc); 1511 1512 return has_sre; 1513 } 1514 1515 static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused) 1516 { 1517 u32 midr = read_cpuid_id(); 1518 1519 /* Cavium ThunderX pass 1.x and 2.x */ 1520 return midr_is_cpu_model_range(midr, MIDR_THUNDERX, 1521 MIDR_CPU_VAR_REV(0, 0), 1522 MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK)); 1523 } 1524 1525 static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused) 1526 { 1527 u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 1528 1529 return cpuid_feature_extract_signed_field(pfr0, 1530 ID_AA64PFR0_EL1_FP_SHIFT) < 0; 1531 } 1532 1533 static bool has_cache_idc(const struct arm64_cpu_capabilities *entry, 1534 int scope) 1535 { 1536 u64 ctr; 1537 1538 if (scope == SCOPE_SYSTEM) 1539 ctr = arm64_ftr_reg_ctrel0.sys_val; 1540 else 1541 ctr = read_cpuid_effective_cachetype(); 1542 1543 return ctr & BIT(CTR_EL0_IDC_SHIFT); 1544 } 1545 1546 static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused) 1547 { 1548 /* 1549 * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively 1550 * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses 1551 * to the CTR_EL0 on this CPU and emulate it with the real/safe 1552 * value. 1553 */ 1554 if (!(read_cpuid_cachetype() & BIT(CTR_EL0_IDC_SHIFT))) 1555 sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0); 1556 } 1557 1558 static bool has_cache_dic(const struct arm64_cpu_capabilities *entry, 1559 int scope) 1560 { 1561 u64 ctr; 1562 1563 if (scope == SCOPE_SYSTEM) 1564 ctr = arm64_ftr_reg_ctrel0.sys_val; 1565 else 1566 ctr = read_cpuid_cachetype(); 1567 1568 return ctr & BIT(CTR_EL0_DIC_SHIFT); 1569 } 1570 1571 static bool __maybe_unused 1572 has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope) 1573 { 1574 /* 1575 * Kdump isn't guaranteed to power-off all secondary CPUs, CNP 1576 * may share TLB entries with a CPU stuck in the crashed 1577 * kernel. 1578 */ 1579 if (is_kdump_kernel()) 1580 return false; 1581 1582 if (cpus_have_const_cap(ARM64_WORKAROUND_NVIDIA_CARMEL_CNP)) 1583 return false; 1584 1585 return has_cpuid_feature(entry, scope); 1586 } 1587 1588 /* 1589 * This check is triggered during the early boot before the cpufeature 1590 * is initialised. Checking the status on the local CPU allows the boot 1591 * CPU to detect the need for non-global mappings and thus avoiding a 1592 * pagetable re-write after all the CPUs are booted. This check will be 1593 * anyway run on individual CPUs, allowing us to get the consistent 1594 * state once the SMP CPUs are up and thus make the switch to non-global 1595 * mappings if required. 1596 */ 1597 bool kaslr_requires_kpti(void) 1598 { 1599 if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE)) 1600 return false; 1601 1602 /* 1603 * E0PD does a similar job to KPTI so can be used instead 1604 * where available. 1605 */ 1606 if (IS_ENABLED(CONFIG_ARM64_E0PD)) { 1607 u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1); 1608 if (cpuid_feature_extract_unsigned_field(mmfr2, 1609 ID_AA64MMFR2_EL1_E0PD_SHIFT)) 1610 return false; 1611 } 1612 1613 /* 1614 * Systems affected by Cavium erratum 24756 are incompatible 1615 * with KPTI. 1616 */ 1617 if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) { 1618 extern const struct midr_range cavium_erratum_27456_cpus[]; 1619 1620 if (is_midr_in_range_list(read_cpuid_id(), 1621 cavium_erratum_27456_cpus)) 1622 return false; 1623 } 1624 1625 return kaslr_offset() > 0; 1626 } 1627 1628 static bool __meltdown_safe = true; 1629 static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */ 1630 1631 static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry, 1632 int scope) 1633 { 1634 /* List of CPUs that are not vulnerable and don't need KPTI */ 1635 static const struct midr_range kpti_safe_list[] = { 1636 MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2), 1637 MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN), 1638 MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53), 1639 MIDR_ALL_VERSIONS(MIDR_CORTEX_A35), 1640 MIDR_ALL_VERSIONS(MIDR_CORTEX_A53), 1641 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55), 1642 MIDR_ALL_VERSIONS(MIDR_CORTEX_A57), 1643 MIDR_ALL_VERSIONS(MIDR_CORTEX_A72), 1644 MIDR_ALL_VERSIONS(MIDR_CORTEX_A73), 1645 MIDR_ALL_VERSIONS(MIDR_HISI_TSV110), 1646 MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL), 1647 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_GOLD), 1648 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_SILVER), 1649 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER), 1650 MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER), 1651 { /* sentinel */ } 1652 }; 1653 char const *str = "kpti command line option"; 1654 bool meltdown_safe; 1655 1656 meltdown_safe = is_midr_in_range_list(read_cpuid_id(), kpti_safe_list); 1657 1658 /* Defer to CPU feature registers */ 1659 if (has_cpuid_feature(entry, scope)) 1660 meltdown_safe = true; 1661 1662 if (!meltdown_safe) 1663 __meltdown_safe = false; 1664 1665 /* 1666 * For reasons that aren't entirely clear, enabling KPTI on Cavium 1667 * ThunderX leads to apparent I-cache corruption of kernel text, which 1668 * ends as well as you might imagine. Don't even try. We cannot rely 1669 * on the cpus_have_*cap() helpers here to detect the CPU erratum 1670 * because cpucap detection order may change. However, since we know 1671 * affected CPUs are always in a homogeneous configuration, it is 1672 * safe to rely on this_cpu_has_cap() here. 1673 */ 1674 if (this_cpu_has_cap(ARM64_WORKAROUND_CAVIUM_27456)) { 1675 str = "ARM64_WORKAROUND_CAVIUM_27456"; 1676 __kpti_forced = -1; 1677 } 1678 1679 /* Useful for KASLR robustness */ 1680 if (kaslr_requires_kpti()) { 1681 if (!__kpti_forced) { 1682 str = "KASLR"; 1683 __kpti_forced = 1; 1684 } 1685 } 1686 1687 if (cpu_mitigations_off() && !__kpti_forced) { 1688 str = "mitigations=off"; 1689 __kpti_forced = -1; 1690 } 1691 1692 if (!IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) { 1693 pr_info_once("kernel page table isolation disabled by kernel configuration\n"); 1694 return false; 1695 } 1696 1697 /* Forced? */ 1698 if (__kpti_forced) { 1699 pr_info_once("kernel page table isolation forced %s by %s\n", 1700 __kpti_forced > 0 ? "ON" : "OFF", str); 1701 return __kpti_forced > 0; 1702 } 1703 1704 return !meltdown_safe; 1705 } 1706 1707 #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 1708 #define KPTI_NG_TEMP_VA (-(1UL << PMD_SHIFT)) 1709 1710 extern 1711 void create_kpti_ng_temp_pgd(pgd_t *pgdir, phys_addr_t phys, unsigned long virt, 1712 phys_addr_t size, pgprot_t prot, 1713 phys_addr_t (*pgtable_alloc)(int), int flags); 1714 1715 static phys_addr_t kpti_ng_temp_alloc; 1716 1717 static phys_addr_t kpti_ng_pgd_alloc(int shift) 1718 { 1719 kpti_ng_temp_alloc -= PAGE_SIZE; 1720 return kpti_ng_temp_alloc; 1721 } 1722 1723 static void 1724 kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused) 1725 { 1726 typedef void (kpti_remap_fn)(int, int, phys_addr_t, unsigned long); 1727 extern kpti_remap_fn idmap_kpti_install_ng_mappings; 1728 kpti_remap_fn *remap_fn; 1729 1730 int cpu = smp_processor_id(); 1731 int levels = CONFIG_PGTABLE_LEVELS; 1732 int order = order_base_2(levels); 1733 u64 kpti_ng_temp_pgd_pa = 0; 1734 pgd_t *kpti_ng_temp_pgd; 1735 u64 alloc = 0; 1736 1737 if (__this_cpu_read(this_cpu_vector) == vectors) { 1738 const char *v = arm64_get_bp_hardening_vector(EL1_VECTOR_KPTI); 1739 1740 __this_cpu_write(this_cpu_vector, v); 1741 } 1742 1743 /* 1744 * We don't need to rewrite the page-tables if either we've done 1745 * it already or we have KASLR enabled and therefore have not 1746 * created any global mappings at all. 1747 */ 1748 if (arm64_use_ng_mappings) 1749 return; 1750 1751 remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings); 1752 1753 if (!cpu) { 1754 alloc = __get_free_pages(GFP_ATOMIC | __GFP_ZERO, order); 1755 kpti_ng_temp_pgd = (pgd_t *)(alloc + (levels - 1) * PAGE_SIZE); 1756 kpti_ng_temp_alloc = kpti_ng_temp_pgd_pa = __pa(kpti_ng_temp_pgd); 1757 1758 // 1759 // Create a minimal page table hierarchy that permits us to map 1760 // the swapper page tables temporarily as we traverse them. 1761 // 1762 // The physical pages are laid out as follows: 1763 // 1764 // +--------+-/-------+-/------ +-\\--------+ 1765 // : PTE[] : | PMD[] : | PUD[] : || PGD[] : 1766 // +--------+-\-------+-\------ +-//--------+ 1767 // ^ 1768 // The first page is mapped into this hierarchy at a PMD_SHIFT 1769 // aligned virtual address, so that we can manipulate the PTE 1770 // level entries while the mapping is active. The first entry 1771 // covers the PTE[] page itself, the remaining entries are free 1772 // to be used as a ad-hoc fixmap. 1773 // 1774 create_kpti_ng_temp_pgd(kpti_ng_temp_pgd, __pa(alloc), 1775 KPTI_NG_TEMP_VA, PAGE_SIZE, PAGE_KERNEL, 1776 kpti_ng_pgd_alloc, 0); 1777 } 1778 1779 cpu_install_idmap(); 1780 remap_fn(cpu, num_online_cpus(), kpti_ng_temp_pgd_pa, KPTI_NG_TEMP_VA); 1781 cpu_uninstall_idmap(); 1782 1783 if (!cpu) { 1784 free_pages(alloc, order); 1785 arm64_use_ng_mappings = true; 1786 } 1787 } 1788 #else 1789 static void 1790 kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused) 1791 { 1792 } 1793 #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */ 1794 1795 static int __init parse_kpti(char *str) 1796 { 1797 bool enabled; 1798 int ret = strtobool(str, &enabled); 1799 1800 if (ret) 1801 return ret; 1802 1803 __kpti_forced = enabled ? 1 : -1; 1804 return 0; 1805 } 1806 early_param("kpti", parse_kpti); 1807 1808 #ifdef CONFIG_ARM64_HW_AFDBM 1809 static inline void __cpu_enable_hw_dbm(void) 1810 { 1811 u64 tcr = read_sysreg(tcr_el1) | TCR_HD; 1812 1813 write_sysreg(tcr, tcr_el1); 1814 isb(); 1815 local_flush_tlb_all(); 1816 } 1817 1818 static bool cpu_has_broken_dbm(void) 1819 { 1820 /* List of CPUs which have broken DBM support. */ 1821 static const struct midr_range cpus[] = { 1822 #ifdef CONFIG_ARM64_ERRATUM_1024718 1823 MIDR_ALL_VERSIONS(MIDR_CORTEX_A55), 1824 /* Kryo4xx Silver (rdpe => r1p0) */ 1825 MIDR_REV(MIDR_QCOM_KRYO_4XX_SILVER, 0xd, 0xe), 1826 #endif 1827 #ifdef CONFIG_ARM64_ERRATUM_2051678 1828 MIDR_REV_RANGE(MIDR_CORTEX_A510, 0, 0, 2), 1829 #endif 1830 {}, 1831 }; 1832 1833 return is_midr_in_range_list(read_cpuid_id(), cpus); 1834 } 1835 1836 static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap) 1837 { 1838 return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) && 1839 !cpu_has_broken_dbm(); 1840 } 1841 1842 static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap) 1843 { 1844 if (cpu_can_use_dbm(cap)) 1845 __cpu_enable_hw_dbm(); 1846 } 1847 1848 static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap, 1849 int __unused) 1850 { 1851 static bool detected = false; 1852 /* 1853 * DBM is a non-conflicting feature. i.e, the kernel can safely 1854 * run a mix of CPUs with and without the feature. So, we 1855 * unconditionally enable the capability to allow any late CPU 1856 * to use the feature. We only enable the control bits on the 1857 * CPU, if it actually supports. 1858 * 1859 * We have to make sure we print the "feature" detection only 1860 * when at least one CPU actually uses it. So check if this CPU 1861 * can actually use it and print the message exactly once. 1862 * 1863 * This is safe as all CPUs (including secondary CPUs - due to the 1864 * LOCAL_CPU scope - and the hotplugged CPUs - via verification) 1865 * goes through the "matches" check exactly once. Also if a CPU 1866 * matches the criteria, it is guaranteed that the CPU will turn 1867 * the DBM on, as the capability is unconditionally enabled. 1868 */ 1869 if (!detected && cpu_can_use_dbm(cap)) { 1870 detected = true; 1871 pr_info("detected: Hardware dirty bit management\n"); 1872 } 1873 1874 return true; 1875 } 1876 1877 #endif 1878 1879 #ifdef CONFIG_ARM64_AMU_EXTN 1880 1881 /* 1882 * The "amu_cpus" cpumask only signals that the CPU implementation for the 1883 * flagged CPUs supports the Activity Monitors Unit (AMU) but does not provide 1884 * information regarding all the events that it supports. When a CPU bit is 1885 * set in the cpumask, the user of this feature can only rely on the presence 1886 * of the 4 fixed counters for that CPU. But this does not guarantee that the 1887 * counters are enabled or access to these counters is enabled by code 1888 * executed at higher exception levels (firmware). 1889 */ 1890 static struct cpumask amu_cpus __read_mostly; 1891 1892 bool cpu_has_amu_feat(int cpu) 1893 { 1894 return cpumask_test_cpu(cpu, &amu_cpus); 1895 } 1896 1897 int get_cpu_with_amu_feat(void) 1898 { 1899 return cpumask_any(&amu_cpus); 1900 } 1901 1902 static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap) 1903 { 1904 if (has_cpuid_feature(cap, SCOPE_LOCAL_CPU)) { 1905 pr_info("detected CPU%d: Activity Monitors Unit (AMU)\n", 1906 smp_processor_id()); 1907 cpumask_set_cpu(smp_processor_id(), &amu_cpus); 1908 1909 /* 0 reference values signal broken/disabled counters */ 1910 if (!this_cpu_has_cap(ARM64_WORKAROUND_2457168)) 1911 update_freq_counters_refs(); 1912 } 1913 } 1914 1915 static bool has_amu(const struct arm64_cpu_capabilities *cap, 1916 int __unused) 1917 { 1918 /* 1919 * The AMU extension is a non-conflicting feature: the kernel can 1920 * safely run a mix of CPUs with and without support for the 1921 * activity monitors extension. Therefore, unconditionally enable 1922 * the capability to allow any late CPU to use the feature. 1923 * 1924 * With this feature unconditionally enabled, the cpu_enable 1925 * function will be called for all CPUs that match the criteria, 1926 * including secondary and hotplugged, marking this feature as 1927 * present on that respective CPU. The enable function will also 1928 * print a detection message. 1929 */ 1930 1931 return true; 1932 } 1933 #else 1934 int get_cpu_with_amu_feat(void) 1935 { 1936 return nr_cpu_ids; 1937 } 1938 #endif 1939 1940 static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused) 1941 { 1942 return is_kernel_in_hyp_mode(); 1943 } 1944 1945 static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused) 1946 { 1947 /* 1948 * Copy register values that aren't redirected by hardware. 1949 * 1950 * Before code patching, we only set tpidr_el1, all CPUs need to copy 1951 * this value to tpidr_el2 before we patch the code. Once we've done 1952 * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to 1953 * do anything here. 1954 */ 1955 if (!alternative_is_applied(ARM64_HAS_VIRT_HOST_EXTN)) 1956 write_sysreg(read_sysreg(tpidr_el1), tpidr_el2); 1957 } 1958 1959 #ifdef CONFIG_ARM64_PAN 1960 static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused) 1961 { 1962 /* 1963 * We modify PSTATE. This won't work from irq context as the PSTATE 1964 * is discarded once we return from the exception. 1965 */ 1966 WARN_ON_ONCE(in_interrupt()); 1967 1968 sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0); 1969 set_pstate_pan(1); 1970 } 1971 #endif /* CONFIG_ARM64_PAN */ 1972 1973 #ifdef CONFIG_ARM64_RAS_EXTN 1974 static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused) 1975 { 1976 /* Firmware may have left a deferred SError in this register. */ 1977 write_sysreg_s(0, SYS_DISR_EL1); 1978 } 1979 #endif /* CONFIG_ARM64_RAS_EXTN */ 1980 1981 #ifdef CONFIG_ARM64_PTR_AUTH 1982 static bool has_address_auth_cpucap(const struct arm64_cpu_capabilities *entry, int scope) 1983 { 1984 int boot_val, sec_val; 1985 1986 /* We don't expect to be called with SCOPE_SYSTEM */ 1987 WARN_ON(scope == SCOPE_SYSTEM); 1988 /* 1989 * The ptr-auth feature levels are not intercompatible with lower 1990 * levels. Hence we must match ptr-auth feature level of the secondary 1991 * CPUs with that of the boot CPU. The level of boot cpu is fetched 1992 * from the sanitised register whereas direct register read is done for 1993 * the secondary CPUs. 1994 * The sanitised feature state is guaranteed to match that of the 1995 * boot CPU as a mismatched secondary CPU is parked before it gets 1996 * a chance to update the state, with the capability. 1997 */ 1998 boot_val = cpuid_feature_extract_field(read_sanitised_ftr_reg(entry->sys_reg), 1999 entry->field_pos, entry->sign); 2000 if (scope & SCOPE_BOOT_CPU) 2001 return boot_val >= entry->min_field_value; 2002 /* Now check for the secondary CPUs with SCOPE_LOCAL_CPU scope */ 2003 sec_val = cpuid_feature_extract_field(__read_sysreg_by_encoding(entry->sys_reg), 2004 entry->field_pos, entry->sign); 2005 return (sec_val >= entry->min_field_value) && (sec_val == boot_val); 2006 } 2007 2008 static bool has_address_auth_metacap(const struct arm64_cpu_capabilities *entry, 2009 int scope) 2010 { 2011 bool api = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_IMP_DEF], scope); 2012 bool apa = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA5], scope); 2013 bool apa3 = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA3], scope); 2014 2015 return apa || apa3 || api; 2016 } 2017 2018 static bool has_generic_auth(const struct arm64_cpu_capabilities *entry, 2019 int __unused) 2020 { 2021 bool gpi = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_IMP_DEF); 2022 bool gpa = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH_QARMA5); 2023 bool gpa3 = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH_QARMA3); 2024 2025 return gpa || gpa3 || gpi; 2026 } 2027 #endif /* CONFIG_ARM64_PTR_AUTH */ 2028 2029 #ifdef CONFIG_ARM64_E0PD 2030 static void cpu_enable_e0pd(struct arm64_cpu_capabilities const *cap) 2031 { 2032 if (this_cpu_has_cap(ARM64_HAS_E0PD)) 2033 sysreg_clear_set(tcr_el1, 0, TCR_E0PD1); 2034 } 2035 #endif /* CONFIG_ARM64_E0PD */ 2036 2037 #ifdef CONFIG_ARM64_PSEUDO_NMI 2038 static bool enable_pseudo_nmi; 2039 2040 static int __init early_enable_pseudo_nmi(char *p) 2041 { 2042 return strtobool(p, &enable_pseudo_nmi); 2043 } 2044 early_param("irqchip.gicv3_pseudo_nmi", early_enable_pseudo_nmi); 2045 2046 static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry, 2047 int scope) 2048 { 2049 return enable_pseudo_nmi && has_useable_gicv3_cpuif(entry, scope); 2050 } 2051 #endif 2052 2053 #ifdef CONFIG_ARM64_BTI 2054 static void bti_enable(const struct arm64_cpu_capabilities *__unused) 2055 { 2056 /* 2057 * Use of X16/X17 for tail-calls and trampolines that jump to 2058 * function entry points using BR is a requirement for 2059 * marking binaries with GNU_PROPERTY_AARCH64_FEATURE_1_BTI. 2060 * So, be strict and forbid other BRs using other registers to 2061 * jump onto a PACIxSP instruction: 2062 */ 2063 sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_BT0 | SCTLR_EL1_BT1); 2064 isb(); 2065 } 2066 #endif /* CONFIG_ARM64_BTI */ 2067 2068 #ifdef CONFIG_ARM64_MTE 2069 static void cpu_enable_mte(struct arm64_cpu_capabilities const *cap) 2070 { 2071 sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_ATA | SCTLR_EL1_ATA0); 2072 2073 mte_cpu_setup(); 2074 2075 /* 2076 * Clear the tags in the zero page. This needs to be done via the 2077 * linear map which has the Tagged attribute. 2078 */ 2079 if (!test_and_set_bit(PG_mte_tagged, &ZERO_PAGE(0)->flags)) 2080 mte_clear_page_tags(lm_alias(empty_zero_page)); 2081 2082 kasan_init_hw_tags_cpu(); 2083 } 2084 #endif /* CONFIG_ARM64_MTE */ 2085 2086 static void elf_hwcap_fixup(void) 2087 { 2088 #ifdef CONFIG_ARM64_ERRATUM_1742098 2089 if (cpus_have_const_cap(ARM64_WORKAROUND_1742098)) 2090 compat_elf_hwcap2 &= ~COMPAT_HWCAP2_AES; 2091 #endif /* ARM64_ERRATUM_1742098 */ 2092 } 2093 2094 #ifdef CONFIG_KVM 2095 static bool is_kvm_protected_mode(const struct arm64_cpu_capabilities *entry, int __unused) 2096 { 2097 return kvm_get_mode() == KVM_MODE_PROTECTED; 2098 } 2099 #endif /* CONFIG_KVM */ 2100 2101 static void cpu_trap_el0_impdef(const struct arm64_cpu_capabilities *__unused) 2102 { 2103 sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_TIDCP); 2104 } 2105 2106 static void cpu_enable_dit(const struct arm64_cpu_capabilities *__unused) 2107 { 2108 set_pstate_dit(1); 2109 } 2110 2111 /* Internal helper functions to match cpu capability type */ 2112 static bool 2113 cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap) 2114 { 2115 return !!(cap->type & ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU); 2116 } 2117 2118 static bool 2119 cpucap_late_cpu_permitted(const struct arm64_cpu_capabilities *cap) 2120 { 2121 return !!(cap->type & ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU); 2122 } 2123 2124 static bool 2125 cpucap_panic_on_conflict(const struct arm64_cpu_capabilities *cap) 2126 { 2127 return !!(cap->type & ARM64_CPUCAP_PANIC_ON_CONFLICT); 2128 } 2129 2130 static const struct arm64_cpu_capabilities arm64_features[] = { 2131 { 2132 .capability = ARM64_ALWAYS_BOOT, 2133 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE, 2134 .matches = has_always, 2135 }, 2136 { 2137 .capability = ARM64_ALWAYS_SYSTEM, 2138 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2139 .matches = has_always, 2140 }, 2141 { 2142 .desc = "GIC system register CPU interface", 2143 .capability = ARM64_HAS_SYSREG_GIC_CPUIF, 2144 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, 2145 .matches = has_useable_gicv3_cpuif, 2146 .sys_reg = SYS_ID_AA64PFR0_EL1, 2147 .field_pos = ID_AA64PFR0_EL1_GIC_SHIFT, 2148 .field_width = 4, 2149 .sign = FTR_UNSIGNED, 2150 .min_field_value = 1, 2151 }, 2152 { 2153 .desc = "Enhanced Counter Virtualization", 2154 .capability = ARM64_HAS_ECV, 2155 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2156 .matches = has_cpuid_feature, 2157 .sys_reg = SYS_ID_AA64MMFR0_EL1, 2158 .field_pos = ID_AA64MMFR0_EL1_ECV_SHIFT, 2159 .field_width = 4, 2160 .sign = FTR_UNSIGNED, 2161 .min_field_value = 1, 2162 }, 2163 #ifdef CONFIG_ARM64_PAN 2164 { 2165 .desc = "Privileged Access Never", 2166 .capability = ARM64_HAS_PAN, 2167 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2168 .matches = has_cpuid_feature, 2169 .sys_reg = SYS_ID_AA64MMFR1_EL1, 2170 .field_pos = ID_AA64MMFR1_EL1_PAN_SHIFT, 2171 .field_width = 4, 2172 .sign = FTR_UNSIGNED, 2173 .min_field_value = 1, 2174 .cpu_enable = cpu_enable_pan, 2175 }, 2176 #endif /* CONFIG_ARM64_PAN */ 2177 #ifdef CONFIG_ARM64_EPAN 2178 { 2179 .desc = "Enhanced Privileged Access Never", 2180 .capability = ARM64_HAS_EPAN, 2181 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2182 .matches = has_cpuid_feature, 2183 .sys_reg = SYS_ID_AA64MMFR1_EL1, 2184 .field_pos = ID_AA64MMFR1_EL1_PAN_SHIFT, 2185 .field_width = 4, 2186 .sign = FTR_UNSIGNED, 2187 .min_field_value = 3, 2188 }, 2189 #endif /* CONFIG_ARM64_EPAN */ 2190 #ifdef CONFIG_ARM64_LSE_ATOMICS 2191 { 2192 .desc = "LSE atomic instructions", 2193 .capability = ARM64_HAS_LSE_ATOMICS, 2194 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2195 .matches = has_cpuid_feature, 2196 .sys_reg = SYS_ID_AA64ISAR0_EL1, 2197 .field_pos = ID_AA64ISAR0_EL1_ATOMIC_SHIFT, 2198 .field_width = 4, 2199 .sign = FTR_UNSIGNED, 2200 .min_field_value = 2, 2201 }, 2202 #endif /* CONFIG_ARM64_LSE_ATOMICS */ 2203 { 2204 .desc = "Software prefetching using PRFM", 2205 .capability = ARM64_HAS_NO_HW_PREFETCH, 2206 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE, 2207 .matches = has_no_hw_prefetch, 2208 }, 2209 { 2210 .desc = "Virtualization Host Extensions", 2211 .capability = ARM64_HAS_VIRT_HOST_EXTN, 2212 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, 2213 .matches = runs_at_el2, 2214 .cpu_enable = cpu_copy_el2regs, 2215 }, 2216 { 2217 .capability = ARM64_HAS_32BIT_EL0_DO_NOT_USE, 2218 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2219 .matches = has_32bit_el0, 2220 .sys_reg = SYS_ID_AA64PFR0_EL1, 2221 .sign = FTR_UNSIGNED, 2222 .field_pos = ID_AA64PFR0_EL1_EL0_SHIFT, 2223 .field_width = 4, 2224 .min_field_value = ID_AA64PFR0_EL1_ELx_32BIT_64BIT, 2225 }, 2226 #ifdef CONFIG_KVM 2227 { 2228 .desc = "32-bit EL1 Support", 2229 .capability = ARM64_HAS_32BIT_EL1, 2230 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2231 .matches = has_cpuid_feature, 2232 .sys_reg = SYS_ID_AA64PFR0_EL1, 2233 .sign = FTR_UNSIGNED, 2234 .field_pos = ID_AA64PFR0_EL1_EL1_SHIFT, 2235 .field_width = 4, 2236 .min_field_value = ID_AA64PFR0_EL1_ELx_32BIT_64BIT, 2237 }, 2238 { 2239 .desc = "Protected KVM", 2240 .capability = ARM64_KVM_PROTECTED_MODE, 2241 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2242 .matches = is_kvm_protected_mode, 2243 }, 2244 #endif 2245 { 2246 .desc = "Kernel page table isolation (KPTI)", 2247 .capability = ARM64_UNMAP_KERNEL_AT_EL0, 2248 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE, 2249 /* 2250 * The ID feature fields below are used to indicate that 2251 * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for 2252 * more details. 2253 */ 2254 .sys_reg = SYS_ID_AA64PFR0_EL1, 2255 .field_pos = ID_AA64PFR0_EL1_CSV3_SHIFT, 2256 .field_width = 4, 2257 .min_field_value = 1, 2258 .matches = unmap_kernel_at_el0, 2259 .cpu_enable = kpti_install_ng_mappings, 2260 }, 2261 { 2262 /* FP/SIMD is not implemented */ 2263 .capability = ARM64_HAS_NO_FPSIMD, 2264 .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE, 2265 .min_field_value = 0, 2266 .matches = has_no_fpsimd, 2267 }, 2268 #ifdef CONFIG_ARM64_PMEM 2269 { 2270 .desc = "Data cache clean to Point of Persistence", 2271 .capability = ARM64_HAS_DCPOP, 2272 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2273 .matches = has_cpuid_feature, 2274 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2275 .field_pos = ID_AA64ISAR1_EL1_DPB_SHIFT, 2276 .field_width = 4, 2277 .min_field_value = 1, 2278 }, 2279 { 2280 .desc = "Data cache clean to Point of Deep Persistence", 2281 .capability = ARM64_HAS_DCPODP, 2282 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2283 .matches = has_cpuid_feature, 2284 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2285 .sign = FTR_UNSIGNED, 2286 .field_pos = ID_AA64ISAR1_EL1_DPB_SHIFT, 2287 .field_width = 4, 2288 .min_field_value = 2, 2289 }, 2290 #endif 2291 #ifdef CONFIG_ARM64_SVE 2292 { 2293 .desc = "Scalable Vector Extension", 2294 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2295 .capability = ARM64_SVE, 2296 .sys_reg = SYS_ID_AA64PFR0_EL1, 2297 .sign = FTR_UNSIGNED, 2298 .field_pos = ID_AA64PFR0_EL1_SVE_SHIFT, 2299 .field_width = 4, 2300 .min_field_value = ID_AA64PFR0_EL1_SVE_IMP, 2301 .matches = has_cpuid_feature, 2302 .cpu_enable = sve_kernel_enable, 2303 }, 2304 #endif /* CONFIG_ARM64_SVE */ 2305 #ifdef CONFIG_ARM64_RAS_EXTN 2306 { 2307 .desc = "RAS Extension Support", 2308 .capability = ARM64_HAS_RAS_EXTN, 2309 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2310 .matches = has_cpuid_feature, 2311 .sys_reg = SYS_ID_AA64PFR0_EL1, 2312 .sign = FTR_UNSIGNED, 2313 .field_pos = ID_AA64PFR0_EL1_RAS_SHIFT, 2314 .field_width = 4, 2315 .min_field_value = ID_AA64PFR0_EL1_RAS_IMP, 2316 .cpu_enable = cpu_clear_disr, 2317 }, 2318 #endif /* CONFIG_ARM64_RAS_EXTN */ 2319 #ifdef CONFIG_ARM64_AMU_EXTN 2320 { 2321 /* 2322 * The feature is enabled by default if CONFIG_ARM64_AMU_EXTN=y. 2323 * Therefore, don't provide .desc as we don't want the detection 2324 * message to be shown until at least one CPU is detected to 2325 * support the feature. 2326 */ 2327 .capability = ARM64_HAS_AMU_EXTN, 2328 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE, 2329 .matches = has_amu, 2330 .sys_reg = SYS_ID_AA64PFR0_EL1, 2331 .sign = FTR_UNSIGNED, 2332 .field_pos = ID_AA64PFR0_EL1_AMU_SHIFT, 2333 .field_width = 4, 2334 .min_field_value = ID_AA64PFR0_EL1_AMU_IMP, 2335 .cpu_enable = cpu_amu_enable, 2336 }, 2337 #endif /* CONFIG_ARM64_AMU_EXTN */ 2338 { 2339 .desc = "Data cache clean to the PoU not required for I/D coherence", 2340 .capability = ARM64_HAS_CACHE_IDC, 2341 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2342 .matches = has_cache_idc, 2343 .cpu_enable = cpu_emulate_effective_ctr, 2344 }, 2345 { 2346 .desc = "Instruction cache invalidation not required for I/D coherence", 2347 .capability = ARM64_HAS_CACHE_DIC, 2348 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2349 .matches = has_cache_dic, 2350 }, 2351 { 2352 .desc = "Stage-2 Force Write-Back", 2353 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2354 .capability = ARM64_HAS_STAGE2_FWB, 2355 .sys_reg = SYS_ID_AA64MMFR2_EL1, 2356 .sign = FTR_UNSIGNED, 2357 .field_pos = ID_AA64MMFR2_EL1_FWB_SHIFT, 2358 .field_width = 4, 2359 .min_field_value = 1, 2360 .matches = has_cpuid_feature, 2361 }, 2362 { 2363 .desc = "ARMv8.4 Translation Table Level", 2364 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2365 .capability = ARM64_HAS_ARMv8_4_TTL, 2366 .sys_reg = SYS_ID_AA64MMFR2_EL1, 2367 .sign = FTR_UNSIGNED, 2368 .field_pos = ID_AA64MMFR2_EL1_TTL_SHIFT, 2369 .field_width = 4, 2370 .min_field_value = 1, 2371 .matches = has_cpuid_feature, 2372 }, 2373 { 2374 .desc = "TLB range maintenance instructions", 2375 .capability = ARM64_HAS_TLB_RANGE, 2376 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2377 .matches = has_cpuid_feature, 2378 .sys_reg = SYS_ID_AA64ISAR0_EL1, 2379 .field_pos = ID_AA64ISAR0_EL1_TLB_SHIFT, 2380 .field_width = 4, 2381 .sign = FTR_UNSIGNED, 2382 .min_field_value = ID_AA64ISAR0_EL1_TLB_RANGE, 2383 }, 2384 #ifdef CONFIG_ARM64_HW_AFDBM 2385 { 2386 /* 2387 * Since we turn this on always, we don't want the user to 2388 * think that the feature is available when it may not be. 2389 * So hide the description. 2390 * 2391 * .desc = "Hardware pagetable Dirty Bit Management", 2392 * 2393 */ 2394 .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE, 2395 .capability = ARM64_HW_DBM, 2396 .sys_reg = SYS_ID_AA64MMFR1_EL1, 2397 .sign = FTR_UNSIGNED, 2398 .field_pos = ID_AA64MMFR1_EL1_HAFDBS_SHIFT, 2399 .field_width = 4, 2400 .min_field_value = 2, 2401 .matches = has_hw_dbm, 2402 .cpu_enable = cpu_enable_hw_dbm, 2403 }, 2404 #endif 2405 { 2406 .desc = "CRC32 instructions", 2407 .capability = ARM64_HAS_CRC32, 2408 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2409 .matches = has_cpuid_feature, 2410 .sys_reg = SYS_ID_AA64ISAR0_EL1, 2411 .field_pos = ID_AA64ISAR0_EL1_CRC32_SHIFT, 2412 .field_width = 4, 2413 .min_field_value = 1, 2414 }, 2415 { 2416 .desc = "Speculative Store Bypassing Safe (SSBS)", 2417 .capability = ARM64_SSBS, 2418 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2419 .matches = has_cpuid_feature, 2420 .sys_reg = SYS_ID_AA64PFR1_EL1, 2421 .field_pos = ID_AA64PFR1_EL1_SSBS_SHIFT, 2422 .field_width = 4, 2423 .sign = FTR_UNSIGNED, 2424 .min_field_value = ID_AA64PFR1_EL1_SSBS_IMP, 2425 }, 2426 #ifdef CONFIG_ARM64_CNP 2427 { 2428 .desc = "Common not Private translations", 2429 .capability = ARM64_HAS_CNP, 2430 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2431 .matches = has_useable_cnp, 2432 .sys_reg = SYS_ID_AA64MMFR2_EL1, 2433 .sign = FTR_UNSIGNED, 2434 .field_pos = ID_AA64MMFR2_EL1_CnP_SHIFT, 2435 .field_width = 4, 2436 .min_field_value = 1, 2437 .cpu_enable = cpu_enable_cnp, 2438 }, 2439 #endif 2440 { 2441 .desc = "Speculation barrier (SB)", 2442 .capability = ARM64_HAS_SB, 2443 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2444 .matches = has_cpuid_feature, 2445 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2446 .field_pos = ID_AA64ISAR1_EL1_SB_SHIFT, 2447 .field_width = 4, 2448 .sign = FTR_UNSIGNED, 2449 .min_field_value = 1, 2450 }, 2451 #ifdef CONFIG_ARM64_PTR_AUTH 2452 { 2453 .desc = "Address authentication (architected QARMA5 algorithm)", 2454 .capability = ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA5, 2455 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE, 2456 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2457 .sign = FTR_UNSIGNED, 2458 .field_pos = ID_AA64ISAR1_EL1_APA_SHIFT, 2459 .field_width = 4, 2460 .min_field_value = ID_AA64ISAR1_EL1_APA_PAuth, 2461 .matches = has_address_auth_cpucap, 2462 }, 2463 { 2464 .desc = "Address authentication (architected QARMA3 algorithm)", 2465 .capability = ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA3, 2466 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE, 2467 .sys_reg = SYS_ID_AA64ISAR2_EL1, 2468 .sign = FTR_UNSIGNED, 2469 .field_pos = ID_AA64ISAR2_EL1_APA3_SHIFT, 2470 .field_width = 4, 2471 .min_field_value = ID_AA64ISAR2_EL1_APA3_PAuth, 2472 .matches = has_address_auth_cpucap, 2473 }, 2474 { 2475 .desc = "Address authentication (IMP DEF algorithm)", 2476 .capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF, 2477 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE, 2478 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2479 .sign = FTR_UNSIGNED, 2480 .field_pos = ID_AA64ISAR1_EL1_API_SHIFT, 2481 .field_width = 4, 2482 .min_field_value = ID_AA64ISAR1_EL1_API_PAuth, 2483 .matches = has_address_auth_cpucap, 2484 }, 2485 { 2486 .capability = ARM64_HAS_ADDRESS_AUTH, 2487 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE, 2488 .matches = has_address_auth_metacap, 2489 }, 2490 { 2491 .desc = "Generic authentication (architected QARMA5 algorithm)", 2492 .capability = ARM64_HAS_GENERIC_AUTH_ARCH_QARMA5, 2493 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2494 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2495 .sign = FTR_UNSIGNED, 2496 .field_pos = ID_AA64ISAR1_EL1_GPA_SHIFT, 2497 .field_width = 4, 2498 .min_field_value = ID_AA64ISAR1_EL1_GPA_IMP, 2499 .matches = has_cpuid_feature, 2500 }, 2501 { 2502 .desc = "Generic authentication (architected QARMA3 algorithm)", 2503 .capability = ARM64_HAS_GENERIC_AUTH_ARCH_QARMA3, 2504 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2505 .sys_reg = SYS_ID_AA64ISAR2_EL1, 2506 .sign = FTR_UNSIGNED, 2507 .field_pos = ID_AA64ISAR2_EL1_GPA3_SHIFT, 2508 .field_width = 4, 2509 .min_field_value = ID_AA64ISAR2_EL1_GPA3_IMP, 2510 .matches = has_cpuid_feature, 2511 }, 2512 { 2513 .desc = "Generic authentication (IMP DEF algorithm)", 2514 .capability = ARM64_HAS_GENERIC_AUTH_IMP_DEF, 2515 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2516 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2517 .sign = FTR_UNSIGNED, 2518 .field_pos = ID_AA64ISAR1_EL1_GPI_SHIFT, 2519 .field_width = 4, 2520 .min_field_value = ID_AA64ISAR1_EL1_GPI_IMP, 2521 .matches = has_cpuid_feature, 2522 }, 2523 { 2524 .capability = ARM64_HAS_GENERIC_AUTH, 2525 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2526 .matches = has_generic_auth, 2527 }, 2528 #endif /* CONFIG_ARM64_PTR_AUTH */ 2529 #ifdef CONFIG_ARM64_PSEUDO_NMI 2530 { 2531 /* 2532 * Depends on having GICv3 2533 */ 2534 .desc = "IRQ priority masking", 2535 .capability = ARM64_HAS_IRQ_PRIO_MASKING, 2536 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, 2537 .matches = can_use_gic_priorities, 2538 .sys_reg = SYS_ID_AA64PFR0_EL1, 2539 .field_pos = ID_AA64PFR0_EL1_GIC_SHIFT, 2540 .field_width = 4, 2541 .sign = FTR_UNSIGNED, 2542 .min_field_value = 1, 2543 }, 2544 #endif 2545 #ifdef CONFIG_ARM64_E0PD 2546 { 2547 .desc = "E0PD", 2548 .capability = ARM64_HAS_E0PD, 2549 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2550 .sys_reg = SYS_ID_AA64MMFR2_EL1, 2551 .sign = FTR_UNSIGNED, 2552 .field_width = 4, 2553 .field_pos = ID_AA64MMFR2_EL1_E0PD_SHIFT, 2554 .matches = has_cpuid_feature, 2555 .min_field_value = 1, 2556 .cpu_enable = cpu_enable_e0pd, 2557 }, 2558 #endif 2559 { 2560 .desc = "Random Number Generator", 2561 .capability = ARM64_HAS_RNG, 2562 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2563 .matches = has_cpuid_feature, 2564 .sys_reg = SYS_ID_AA64ISAR0_EL1, 2565 .field_pos = ID_AA64ISAR0_EL1_RNDR_SHIFT, 2566 .field_width = 4, 2567 .sign = FTR_UNSIGNED, 2568 .min_field_value = 1, 2569 }, 2570 #ifdef CONFIG_ARM64_BTI 2571 { 2572 .desc = "Branch Target Identification", 2573 .capability = ARM64_BTI, 2574 #ifdef CONFIG_ARM64_BTI_KERNEL 2575 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, 2576 #else 2577 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2578 #endif 2579 .matches = has_cpuid_feature, 2580 .cpu_enable = bti_enable, 2581 .sys_reg = SYS_ID_AA64PFR1_EL1, 2582 .field_pos = ID_AA64PFR1_EL1_BT_SHIFT, 2583 .field_width = 4, 2584 .min_field_value = ID_AA64PFR1_EL1_BT_IMP, 2585 .sign = FTR_UNSIGNED, 2586 }, 2587 #endif 2588 #ifdef CONFIG_ARM64_MTE 2589 { 2590 .desc = "Memory Tagging Extension", 2591 .capability = ARM64_MTE, 2592 .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE, 2593 .matches = has_cpuid_feature, 2594 .sys_reg = SYS_ID_AA64PFR1_EL1, 2595 .field_pos = ID_AA64PFR1_EL1_MTE_SHIFT, 2596 .field_width = 4, 2597 .min_field_value = ID_AA64PFR1_EL1_MTE_MTE2, 2598 .sign = FTR_UNSIGNED, 2599 .cpu_enable = cpu_enable_mte, 2600 }, 2601 { 2602 .desc = "Asymmetric MTE Tag Check Fault", 2603 .capability = ARM64_MTE_ASYMM, 2604 .type = ARM64_CPUCAP_BOOT_CPU_FEATURE, 2605 .matches = has_cpuid_feature, 2606 .sys_reg = SYS_ID_AA64PFR1_EL1, 2607 .field_pos = ID_AA64PFR1_EL1_MTE_SHIFT, 2608 .field_width = 4, 2609 .min_field_value = ID_AA64PFR1_EL1_MTE_MTE3, 2610 .sign = FTR_UNSIGNED, 2611 }, 2612 #endif /* CONFIG_ARM64_MTE */ 2613 { 2614 .desc = "RCpc load-acquire (LDAPR)", 2615 .capability = ARM64_HAS_LDAPR, 2616 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2617 .sys_reg = SYS_ID_AA64ISAR1_EL1, 2618 .sign = FTR_UNSIGNED, 2619 .field_pos = ID_AA64ISAR1_EL1_LRCPC_SHIFT, 2620 .field_width = 4, 2621 .matches = has_cpuid_feature, 2622 .min_field_value = 1, 2623 }, 2624 #ifdef CONFIG_ARM64_SME 2625 { 2626 .desc = "Scalable Matrix Extension", 2627 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2628 .capability = ARM64_SME, 2629 .sys_reg = SYS_ID_AA64PFR1_EL1, 2630 .sign = FTR_UNSIGNED, 2631 .field_pos = ID_AA64PFR1_EL1_SME_SHIFT, 2632 .field_width = 4, 2633 .min_field_value = ID_AA64PFR1_EL1_SME_IMP, 2634 .matches = has_cpuid_feature, 2635 .cpu_enable = sme_kernel_enable, 2636 }, 2637 /* FA64 should be sorted after the base SME capability */ 2638 { 2639 .desc = "FA64", 2640 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2641 .capability = ARM64_SME_FA64, 2642 .sys_reg = SYS_ID_AA64SMFR0_EL1, 2643 .sign = FTR_UNSIGNED, 2644 .field_pos = ID_AA64SMFR0_EL1_FA64_SHIFT, 2645 .field_width = 1, 2646 .min_field_value = ID_AA64SMFR0_EL1_FA64_IMP, 2647 .matches = has_cpuid_feature, 2648 .cpu_enable = fa64_kernel_enable, 2649 }, 2650 #endif /* CONFIG_ARM64_SME */ 2651 { 2652 .desc = "WFx with timeout", 2653 .capability = ARM64_HAS_WFXT, 2654 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2655 .sys_reg = SYS_ID_AA64ISAR2_EL1, 2656 .sign = FTR_UNSIGNED, 2657 .field_pos = ID_AA64ISAR2_EL1_WFxT_SHIFT, 2658 .field_width = 4, 2659 .matches = has_cpuid_feature, 2660 .min_field_value = ID_AA64ISAR2_EL1_WFxT_IMP, 2661 }, 2662 { 2663 .desc = "Trap EL0 IMPLEMENTATION DEFINED functionality", 2664 .capability = ARM64_HAS_TIDCP1, 2665 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2666 .sys_reg = SYS_ID_AA64MMFR1_EL1, 2667 .sign = FTR_UNSIGNED, 2668 .field_pos = ID_AA64MMFR1_EL1_TIDCP1_SHIFT, 2669 .field_width = 4, 2670 .min_field_value = ID_AA64MMFR1_EL1_TIDCP1_IMP, 2671 .matches = has_cpuid_feature, 2672 .cpu_enable = cpu_trap_el0_impdef, 2673 }, 2674 { 2675 .desc = "Data independent timing control (DIT)", 2676 .capability = ARM64_HAS_DIT, 2677 .type = ARM64_CPUCAP_SYSTEM_FEATURE, 2678 .sys_reg = SYS_ID_AA64PFR0_EL1, 2679 .sign = FTR_UNSIGNED, 2680 .field_pos = ID_AA64PFR0_EL1_DIT_SHIFT, 2681 .field_width = 4, 2682 .min_field_value = ID_AA64PFR0_EL1_DIT_IMP, 2683 .matches = has_cpuid_feature, 2684 .cpu_enable = cpu_enable_dit, 2685 }, 2686 {}, 2687 }; 2688 2689 #define HWCAP_CPUID_MATCH(reg, field, width, s, min_value) \ 2690 .matches = has_user_cpuid_feature, \ 2691 .sys_reg = reg, \ 2692 .field_pos = field, \ 2693 .field_width = width, \ 2694 .sign = s, \ 2695 .min_field_value = min_value, 2696 2697 #define __HWCAP_CAP(name, cap_type, cap) \ 2698 .desc = name, \ 2699 .type = ARM64_CPUCAP_SYSTEM_FEATURE, \ 2700 .hwcap_type = cap_type, \ 2701 .hwcap = cap, \ 2702 2703 #define HWCAP_CAP(reg, field, width, s, min_value, cap_type, cap) \ 2704 { \ 2705 __HWCAP_CAP(#cap, cap_type, cap) \ 2706 HWCAP_CPUID_MATCH(reg, field, width, s, min_value) \ 2707 } 2708 2709 #define HWCAP_MULTI_CAP(list, cap_type, cap) \ 2710 { \ 2711 __HWCAP_CAP(#cap, cap_type, cap) \ 2712 .matches = cpucap_multi_entry_cap_matches, \ 2713 .match_list = list, \ 2714 } 2715 2716 #define HWCAP_CAP_MATCH(match, cap_type, cap) \ 2717 { \ 2718 __HWCAP_CAP(#cap, cap_type, cap) \ 2719 .matches = match, \ 2720 } 2721 2722 #ifdef CONFIG_ARM64_PTR_AUTH 2723 static const struct arm64_cpu_capabilities ptr_auth_hwcap_addr_matches[] = { 2724 { 2725 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_APA_SHIFT, 2726 4, FTR_UNSIGNED, 2727 ID_AA64ISAR1_EL1_APA_PAuth) 2728 }, 2729 { 2730 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_APA3_SHIFT, 2731 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_APA3_PAuth) 2732 }, 2733 { 2734 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_API_SHIFT, 2735 4, FTR_UNSIGNED, ID_AA64ISAR1_EL1_API_PAuth) 2736 }, 2737 {}, 2738 }; 2739 2740 static const struct arm64_cpu_capabilities ptr_auth_hwcap_gen_matches[] = { 2741 { 2742 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_GPA_SHIFT, 2743 4, FTR_UNSIGNED, ID_AA64ISAR1_EL1_GPA_IMP) 2744 }, 2745 { 2746 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_GPA3_SHIFT, 2747 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_GPA3_IMP) 2748 }, 2749 { 2750 HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_GPI_SHIFT, 2751 4, FTR_UNSIGNED, ID_AA64ISAR1_EL1_GPI_IMP) 2752 }, 2753 {}, 2754 }; 2755 #endif 2756 2757 static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = { 2758 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_PMULL), 2759 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_AES), 2760 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA1_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA1), 2761 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA2_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA2), 2762 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA2_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_SHA512), 2763 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_CRC32_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_CRC32), 2764 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_ATOMIC_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ATOMICS), 2765 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_RDM_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDRDM), 2766 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA3_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA3), 2767 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SM3_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM3), 2768 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SM4_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM4), 2769 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_DP_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDDP), 2770 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_FHM_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM), 2771 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_TS_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FLAGM), 2772 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_TS_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_FLAGM2), 2773 HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_RNDR_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RNG), 2774 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_FP_SHIFT, 4, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_FP), 2775 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_FP_SHIFT, 4, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FPHP), 2776 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_AdvSIMD_SHIFT, 4, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_ASIMD), 2777 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_AdvSIMD_SHIFT, 4, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDHP), 2778 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_DIT_SHIFT, 4, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DIT), 2779 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_DPB_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DCPOP), 2780 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_DPB_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_DCPODP), 2781 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_JSCVT_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_JSCVT), 2782 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_FCMA_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FCMA), 2783 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_LRCPC_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_LRCPC), 2784 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_LRCPC_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ILRCPC), 2785 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_FRINTTS_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FRINT), 2786 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_SB_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SB), 2787 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_BF16), 2788 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_EBF16), 2789 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_DGH_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DGH), 2790 HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_I8MM_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_I8MM), 2791 HWCAP_CAP(SYS_ID_AA64MMFR2_EL1, ID_AA64MMFR2_EL1_AT_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_USCAT), 2792 #ifdef CONFIG_ARM64_SVE 2793 HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_SVE_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR0_EL1_SVE_IMP, CAP_HWCAP, KERNEL_HWCAP_SVE), 2794 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_SVEver_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_SVEver_SVE2p1, CAP_HWCAP, KERNEL_HWCAP_SVE2P1), 2795 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_SVEver_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_SVEver_SVE2, CAP_HWCAP, KERNEL_HWCAP_SVE2), 2796 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_AES_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEAES), 2797 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_AES_PMULL128, CAP_HWCAP, KERNEL_HWCAP_SVEPMULL), 2798 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_BitPerm_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_BitPerm_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEBITPERM), 2799 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_BF16_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEBF16), 2800 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_BF16_EBF16, CAP_HWCAP, KERNEL_HWCAP_SVE_EBF16), 2801 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_SHA3_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_SHA3_IMP, CAP_HWCAP, KERNEL_HWCAP_SVESHA3), 2802 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_SM4_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_SM4_IMP, CAP_HWCAP, KERNEL_HWCAP_SVESM4), 2803 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_I8MM_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_I8MM_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEI8MM), 2804 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_F32MM_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_F32MM_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF32MM), 2805 HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_F64MM_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_F64MM_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF64MM), 2806 #endif 2807 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_SSBS_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_SSBS_SSBS2, CAP_HWCAP, KERNEL_HWCAP_SSBS), 2808 #ifdef CONFIG_ARM64_BTI 2809 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_BT_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_BT_IMP, CAP_HWCAP, KERNEL_HWCAP_BTI), 2810 #endif 2811 #ifdef CONFIG_ARM64_PTR_AUTH 2812 HWCAP_MULTI_CAP(ptr_auth_hwcap_addr_matches, CAP_HWCAP, KERNEL_HWCAP_PACA), 2813 HWCAP_MULTI_CAP(ptr_auth_hwcap_gen_matches, CAP_HWCAP, KERNEL_HWCAP_PACG), 2814 #endif 2815 #ifdef CONFIG_ARM64_MTE 2816 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_MTE_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_MTE_MTE2, CAP_HWCAP, KERNEL_HWCAP_MTE), 2817 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_MTE_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_MTE_MTE3, CAP_HWCAP, KERNEL_HWCAP_MTE3), 2818 #endif /* CONFIG_ARM64_MTE */ 2819 HWCAP_CAP(SYS_ID_AA64MMFR0_EL1, ID_AA64MMFR0_EL1_ECV_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ECV), 2820 HWCAP_CAP(SYS_ID_AA64MMFR1_EL1, ID_AA64MMFR1_EL1_AFP_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_AFP), 2821 HWCAP_CAP(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_CSSC_SHIFT, 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_CSSC_IMP, CAP_HWCAP, KERNEL_HWCAP_CSSC), 2822 HWCAP_CAP(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_RPRFM_SHIFT, 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_RPRFM_IMP, CAP_HWCAP, KERNEL_HWCAP_RPRFM), 2823 HWCAP_CAP(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_RPRES_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RPRES), 2824 HWCAP_CAP(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_WFxT_SHIFT, 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_WFxT_IMP, CAP_HWCAP, KERNEL_HWCAP_WFXT), 2825 #ifdef CONFIG_ARM64_SME 2826 HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_SME_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_SME_IMP, CAP_HWCAP, KERNEL_HWCAP_SME), 2827 HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_FA64_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_FA64_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_FA64), 2828 HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_I16I64_SHIFT, 4, FTR_UNSIGNED, ID_AA64SMFR0_EL1_I16I64_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_I16I64), 2829 HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_F64F64_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_F64F64_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F64F64), 2830 HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_I8I32_SHIFT, 4, FTR_UNSIGNED, ID_AA64SMFR0_EL1_I8I32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_I8I32), 2831 HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_F16F32_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_F16F32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F16F32), 2832 HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_B16F32_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_B16F32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_B16F32), 2833 HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_F32F32_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_F32F32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F32F32), 2834 #endif /* CONFIG_ARM64_SME */ 2835 {}, 2836 }; 2837 2838 #ifdef CONFIG_COMPAT 2839 static bool compat_has_neon(const struct arm64_cpu_capabilities *cap, int scope) 2840 { 2841 /* 2842 * Check that all of MVFR1_EL1.{SIMDSP, SIMDInt, SIMDLS} are available, 2843 * in line with that of arm32 as in vfp_init(). We make sure that the 2844 * check is future proof, by making sure value is non-zero. 2845 */ 2846 u32 mvfr1; 2847 2848 WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible()); 2849 if (scope == SCOPE_SYSTEM) 2850 mvfr1 = read_sanitised_ftr_reg(SYS_MVFR1_EL1); 2851 else 2852 mvfr1 = read_sysreg_s(SYS_MVFR1_EL1); 2853 2854 return cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_EL1_SIMDSP_SHIFT) && 2855 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_EL1_SIMDInt_SHIFT) && 2856 cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_EL1_SIMDLS_SHIFT); 2857 } 2858 #endif 2859 2860 static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = { 2861 #ifdef CONFIG_COMPAT 2862 HWCAP_CAP_MATCH(compat_has_neon, CAP_COMPAT_HWCAP, COMPAT_HWCAP_NEON), 2863 HWCAP_CAP(SYS_MVFR1_EL1, MVFR1_EL1_SIMDFMAC_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv4), 2864 /* Arm v8 mandates MVFR0.FPDP == {0, 2}. So, piggy back on this for the presence of VFP support */ 2865 HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_EL1_FPDP_SHIFT, 4, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFP), 2866 HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_EL1_FPDP_SHIFT, 4, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv3), 2867 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_EL1_AES_SHIFT, 4, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL), 2868 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_EL1_AES_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES), 2869 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_EL1_SHA1_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1), 2870 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_EL1_SHA2_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2), 2871 HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_EL1_CRC32_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32), 2872 #endif 2873 {}, 2874 }; 2875 2876 static void cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap) 2877 { 2878 switch (cap->hwcap_type) { 2879 case CAP_HWCAP: 2880 cpu_set_feature(cap->hwcap); 2881 break; 2882 #ifdef CONFIG_COMPAT 2883 case CAP_COMPAT_HWCAP: 2884 compat_elf_hwcap |= (u32)cap->hwcap; 2885 break; 2886 case CAP_COMPAT_HWCAP2: 2887 compat_elf_hwcap2 |= (u32)cap->hwcap; 2888 break; 2889 #endif 2890 default: 2891 WARN_ON(1); 2892 break; 2893 } 2894 } 2895 2896 /* Check if we have a particular HWCAP enabled */ 2897 static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap) 2898 { 2899 bool rc; 2900 2901 switch (cap->hwcap_type) { 2902 case CAP_HWCAP: 2903 rc = cpu_have_feature(cap->hwcap); 2904 break; 2905 #ifdef CONFIG_COMPAT 2906 case CAP_COMPAT_HWCAP: 2907 rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0; 2908 break; 2909 case CAP_COMPAT_HWCAP2: 2910 rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0; 2911 break; 2912 #endif 2913 default: 2914 WARN_ON(1); 2915 rc = false; 2916 } 2917 2918 return rc; 2919 } 2920 2921 static void setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps) 2922 { 2923 /* We support emulation of accesses to CPU ID feature registers */ 2924 cpu_set_named_feature(CPUID); 2925 for (; hwcaps->matches; hwcaps++) 2926 if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps))) 2927 cap_set_elf_hwcap(hwcaps); 2928 } 2929 2930 static void update_cpu_capabilities(u16 scope_mask) 2931 { 2932 int i; 2933 const struct arm64_cpu_capabilities *caps; 2934 2935 scope_mask &= ARM64_CPUCAP_SCOPE_MASK; 2936 for (i = 0; i < ARM64_NCAPS; i++) { 2937 caps = cpu_hwcaps_ptrs[i]; 2938 if (!caps || !(caps->type & scope_mask) || 2939 cpus_have_cap(caps->capability) || 2940 !caps->matches(caps, cpucap_default_scope(caps))) 2941 continue; 2942 2943 if (caps->desc) 2944 pr_info("detected: %s\n", caps->desc); 2945 cpus_set_cap(caps->capability); 2946 2947 if ((scope_mask & SCOPE_BOOT_CPU) && (caps->type & SCOPE_BOOT_CPU)) 2948 set_bit(caps->capability, boot_capabilities); 2949 } 2950 } 2951 2952 /* 2953 * Enable all the available capabilities on this CPU. The capabilities 2954 * with BOOT_CPU scope are handled separately and hence skipped here. 2955 */ 2956 static int cpu_enable_non_boot_scope_capabilities(void *__unused) 2957 { 2958 int i; 2959 u16 non_boot_scope = SCOPE_ALL & ~SCOPE_BOOT_CPU; 2960 2961 for_each_available_cap(i) { 2962 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[i]; 2963 2964 if (WARN_ON(!cap)) 2965 continue; 2966 2967 if (!(cap->type & non_boot_scope)) 2968 continue; 2969 2970 if (cap->cpu_enable) 2971 cap->cpu_enable(cap); 2972 } 2973 return 0; 2974 } 2975 2976 /* 2977 * Run through the enabled capabilities and enable() it on all active 2978 * CPUs 2979 */ 2980 static void __init enable_cpu_capabilities(u16 scope_mask) 2981 { 2982 int i; 2983 const struct arm64_cpu_capabilities *caps; 2984 bool boot_scope; 2985 2986 scope_mask &= ARM64_CPUCAP_SCOPE_MASK; 2987 boot_scope = !!(scope_mask & SCOPE_BOOT_CPU); 2988 2989 for (i = 0; i < ARM64_NCAPS; i++) { 2990 unsigned int num; 2991 2992 caps = cpu_hwcaps_ptrs[i]; 2993 if (!caps || !(caps->type & scope_mask)) 2994 continue; 2995 num = caps->capability; 2996 if (!cpus_have_cap(num)) 2997 continue; 2998 2999 if (boot_scope && caps->cpu_enable) 3000 /* 3001 * Capabilities with SCOPE_BOOT_CPU scope are finalised 3002 * before any secondary CPU boots. Thus, each secondary 3003 * will enable the capability as appropriate via 3004 * check_local_cpu_capabilities(). The only exception is 3005 * the boot CPU, for which the capability must be 3006 * enabled here. This approach avoids costly 3007 * stop_machine() calls for this case. 3008 */ 3009 caps->cpu_enable(caps); 3010 } 3011 3012 /* 3013 * For all non-boot scope capabilities, use stop_machine() 3014 * as it schedules the work allowing us to modify PSTATE, 3015 * instead of on_each_cpu() which uses an IPI, giving us a 3016 * PSTATE that disappears when we return. 3017 */ 3018 if (!boot_scope) 3019 stop_machine(cpu_enable_non_boot_scope_capabilities, 3020 NULL, cpu_online_mask); 3021 } 3022 3023 /* 3024 * Run through the list of capabilities to check for conflicts. 3025 * If the system has already detected a capability, take necessary 3026 * action on this CPU. 3027 */ 3028 static void verify_local_cpu_caps(u16 scope_mask) 3029 { 3030 int i; 3031 bool cpu_has_cap, system_has_cap; 3032 const struct arm64_cpu_capabilities *caps; 3033 3034 scope_mask &= ARM64_CPUCAP_SCOPE_MASK; 3035 3036 for (i = 0; i < ARM64_NCAPS; i++) { 3037 caps = cpu_hwcaps_ptrs[i]; 3038 if (!caps || !(caps->type & scope_mask)) 3039 continue; 3040 3041 cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU); 3042 system_has_cap = cpus_have_cap(caps->capability); 3043 3044 if (system_has_cap) { 3045 /* 3046 * Check if the new CPU misses an advertised feature, 3047 * which is not safe to miss. 3048 */ 3049 if (!cpu_has_cap && !cpucap_late_cpu_optional(caps)) 3050 break; 3051 /* 3052 * We have to issue cpu_enable() irrespective of 3053 * whether the CPU has it or not, as it is enabeld 3054 * system wide. It is upto the call back to take 3055 * appropriate action on this CPU. 3056 */ 3057 if (caps->cpu_enable) 3058 caps->cpu_enable(caps); 3059 } else { 3060 /* 3061 * Check if the CPU has this capability if it isn't 3062 * safe to have when the system doesn't. 3063 */ 3064 if (cpu_has_cap && !cpucap_late_cpu_permitted(caps)) 3065 break; 3066 } 3067 } 3068 3069 if (i < ARM64_NCAPS) { 3070 pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n", 3071 smp_processor_id(), caps->capability, 3072 caps->desc, system_has_cap, cpu_has_cap); 3073 3074 if (cpucap_panic_on_conflict(caps)) 3075 cpu_panic_kernel(); 3076 else 3077 cpu_die_early(); 3078 } 3079 } 3080 3081 /* 3082 * Check for CPU features that are used in early boot 3083 * based on the Boot CPU value. 3084 */ 3085 static void check_early_cpu_features(void) 3086 { 3087 verify_cpu_asid_bits(); 3088 3089 verify_local_cpu_caps(SCOPE_BOOT_CPU); 3090 } 3091 3092 static void 3093 __verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps) 3094 { 3095 3096 for (; caps->matches; caps++) 3097 if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) { 3098 pr_crit("CPU%d: missing HWCAP: %s\n", 3099 smp_processor_id(), caps->desc); 3100 cpu_die_early(); 3101 } 3102 } 3103 3104 static void verify_local_elf_hwcaps(void) 3105 { 3106 __verify_local_elf_hwcaps(arm64_elf_hwcaps); 3107 3108 if (id_aa64pfr0_32bit_el0(read_cpuid(ID_AA64PFR0_EL1))) 3109 __verify_local_elf_hwcaps(compat_elf_hwcaps); 3110 } 3111 3112 static void verify_sve_features(void) 3113 { 3114 u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1); 3115 u64 zcr = read_zcr_features(); 3116 3117 unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK; 3118 unsigned int len = zcr & ZCR_ELx_LEN_MASK; 3119 3120 if (len < safe_len || vec_verify_vq_map(ARM64_VEC_SVE)) { 3121 pr_crit("CPU%d: SVE: vector length support mismatch\n", 3122 smp_processor_id()); 3123 cpu_die_early(); 3124 } 3125 3126 /* Add checks on other ZCR bits here if necessary */ 3127 } 3128 3129 static void verify_sme_features(void) 3130 { 3131 u64 safe_smcr = read_sanitised_ftr_reg(SYS_SMCR_EL1); 3132 u64 smcr = read_smcr_features(); 3133 3134 unsigned int safe_len = safe_smcr & SMCR_ELx_LEN_MASK; 3135 unsigned int len = smcr & SMCR_ELx_LEN_MASK; 3136 3137 if (len < safe_len || vec_verify_vq_map(ARM64_VEC_SME)) { 3138 pr_crit("CPU%d: SME: vector length support mismatch\n", 3139 smp_processor_id()); 3140 cpu_die_early(); 3141 } 3142 3143 /* Add checks on other SMCR bits here if necessary */ 3144 } 3145 3146 static void verify_hyp_capabilities(void) 3147 { 3148 u64 safe_mmfr1, mmfr0, mmfr1; 3149 int parange, ipa_max; 3150 unsigned int safe_vmid_bits, vmid_bits; 3151 3152 if (!IS_ENABLED(CONFIG_KVM)) 3153 return; 3154 3155 safe_mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1); 3156 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1); 3157 mmfr1 = read_cpuid(ID_AA64MMFR1_EL1); 3158 3159 /* Verify VMID bits */ 3160 safe_vmid_bits = get_vmid_bits(safe_mmfr1); 3161 vmid_bits = get_vmid_bits(mmfr1); 3162 if (vmid_bits < safe_vmid_bits) { 3163 pr_crit("CPU%d: VMID width mismatch\n", smp_processor_id()); 3164 cpu_die_early(); 3165 } 3166 3167 /* Verify IPA range */ 3168 parange = cpuid_feature_extract_unsigned_field(mmfr0, 3169 ID_AA64MMFR0_EL1_PARANGE_SHIFT); 3170 ipa_max = id_aa64mmfr0_parange_to_phys_shift(parange); 3171 if (ipa_max < get_kvm_ipa_limit()) { 3172 pr_crit("CPU%d: IPA range mismatch\n", smp_processor_id()); 3173 cpu_die_early(); 3174 } 3175 } 3176 3177 /* 3178 * Run through the enabled system capabilities and enable() it on this CPU. 3179 * The capabilities were decided based on the available CPUs at the boot time. 3180 * Any new CPU should match the system wide status of the capability. If the 3181 * new CPU doesn't have a capability which the system now has enabled, we 3182 * cannot do anything to fix it up and could cause unexpected failures. So 3183 * we park the CPU. 3184 */ 3185 static void verify_local_cpu_capabilities(void) 3186 { 3187 /* 3188 * The capabilities with SCOPE_BOOT_CPU are checked from 3189 * check_early_cpu_features(), as they need to be verified 3190 * on all secondary CPUs. 3191 */ 3192 verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU); 3193 verify_local_elf_hwcaps(); 3194 3195 if (system_supports_sve()) 3196 verify_sve_features(); 3197 3198 if (system_supports_sme()) 3199 verify_sme_features(); 3200 3201 if (is_hyp_mode_available()) 3202 verify_hyp_capabilities(); 3203 } 3204 3205 void check_local_cpu_capabilities(void) 3206 { 3207 /* 3208 * All secondary CPUs should conform to the early CPU features 3209 * in use by the kernel based on boot CPU. 3210 */ 3211 check_early_cpu_features(); 3212 3213 /* 3214 * If we haven't finalised the system capabilities, this CPU gets 3215 * a chance to update the errata work arounds and local features. 3216 * Otherwise, this CPU should verify that it has all the system 3217 * advertised capabilities. 3218 */ 3219 if (!system_capabilities_finalized()) 3220 update_cpu_capabilities(SCOPE_LOCAL_CPU); 3221 else 3222 verify_local_cpu_capabilities(); 3223 } 3224 3225 static void __init setup_boot_cpu_capabilities(void) 3226 { 3227 /* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */ 3228 update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU); 3229 /* Enable the SCOPE_BOOT_CPU capabilities alone right away */ 3230 enable_cpu_capabilities(SCOPE_BOOT_CPU); 3231 } 3232 3233 bool this_cpu_has_cap(unsigned int n) 3234 { 3235 if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) { 3236 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n]; 3237 3238 if (cap) 3239 return cap->matches(cap, SCOPE_LOCAL_CPU); 3240 } 3241 3242 return false; 3243 } 3244 EXPORT_SYMBOL_GPL(this_cpu_has_cap); 3245 3246 /* 3247 * This helper function is used in a narrow window when, 3248 * - The system wide safe registers are set with all the SMP CPUs and, 3249 * - The SYSTEM_FEATURE cpu_hwcaps may not have been set. 3250 * In all other cases cpus_have_{const_}cap() should be used. 3251 */ 3252 static bool __maybe_unused __system_matches_cap(unsigned int n) 3253 { 3254 if (n < ARM64_NCAPS) { 3255 const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n]; 3256 3257 if (cap) 3258 return cap->matches(cap, SCOPE_SYSTEM); 3259 } 3260 return false; 3261 } 3262 3263 void cpu_set_feature(unsigned int num) 3264 { 3265 set_bit(num, elf_hwcap); 3266 } 3267 3268 bool cpu_have_feature(unsigned int num) 3269 { 3270 return test_bit(num, elf_hwcap); 3271 } 3272 EXPORT_SYMBOL_GPL(cpu_have_feature); 3273 3274 unsigned long cpu_get_elf_hwcap(void) 3275 { 3276 /* 3277 * We currently only populate the first 32 bits of AT_HWCAP. Please 3278 * note that for userspace compatibility we guarantee that bits 62 3279 * and 63 will always be returned as 0. 3280 */ 3281 return elf_hwcap[0]; 3282 } 3283 3284 unsigned long cpu_get_elf_hwcap2(void) 3285 { 3286 return elf_hwcap[1]; 3287 } 3288 3289 static void __init setup_system_capabilities(void) 3290 { 3291 /* 3292 * We have finalised the system-wide safe feature 3293 * registers, finalise the capabilities that depend 3294 * on it. Also enable all the available capabilities, 3295 * that are not enabled already. 3296 */ 3297 update_cpu_capabilities(SCOPE_SYSTEM); 3298 enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU); 3299 } 3300 3301 void __init setup_cpu_features(void) 3302 { 3303 u32 cwg; 3304 3305 setup_system_capabilities(); 3306 setup_elf_hwcaps(arm64_elf_hwcaps); 3307 3308 if (system_supports_32bit_el0()) { 3309 setup_elf_hwcaps(compat_elf_hwcaps); 3310 elf_hwcap_fixup(); 3311 } 3312 3313 if (system_uses_ttbr0_pan()) 3314 pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n"); 3315 3316 sve_setup(); 3317 sme_setup(); 3318 minsigstksz_setup(); 3319 3320 /* 3321 * Check for sane CTR_EL0.CWG value. 3322 */ 3323 cwg = cache_type_cwg(); 3324 if (!cwg) 3325 pr_warn("No Cache Writeback Granule information, assuming %d\n", 3326 ARCH_DMA_MINALIGN); 3327 } 3328 3329 static int enable_mismatched_32bit_el0(unsigned int cpu) 3330 { 3331 /* 3332 * The first 32-bit-capable CPU we detected and so can no longer 3333 * be offlined by userspace. -1 indicates we haven't yet onlined 3334 * a 32-bit-capable CPU. 3335 */ 3336 static int lucky_winner = -1; 3337 3338 struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu); 3339 bool cpu_32bit = id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0); 3340 3341 if (cpu_32bit) { 3342 cpumask_set_cpu(cpu, cpu_32bit_el0_mask); 3343 static_branch_enable_cpuslocked(&arm64_mismatched_32bit_el0); 3344 } 3345 3346 if (cpumask_test_cpu(0, cpu_32bit_el0_mask) == cpu_32bit) 3347 return 0; 3348 3349 if (lucky_winner >= 0) 3350 return 0; 3351 3352 /* 3353 * We've detected a mismatch. We need to keep one of our CPUs with 3354 * 32-bit EL0 online so that is_cpu_allowed() doesn't end up rejecting 3355 * every CPU in the system for a 32-bit task. 3356 */ 3357 lucky_winner = cpu_32bit ? cpu : cpumask_any_and(cpu_32bit_el0_mask, 3358 cpu_active_mask); 3359 get_cpu_device(lucky_winner)->offline_disabled = true; 3360 setup_elf_hwcaps(compat_elf_hwcaps); 3361 elf_hwcap_fixup(); 3362 pr_info("Asymmetric 32-bit EL0 support detected on CPU %u; CPU hot-unplug disabled on CPU %u\n", 3363 cpu, lucky_winner); 3364 return 0; 3365 } 3366 3367 static int __init init_32bit_el0_mask(void) 3368 { 3369 if (!allow_mismatched_32bit_el0) 3370 return 0; 3371 3372 if (!zalloc_cpumask_var(&cpu_32bit_el0_mask, GFP_KERNEL)) 3373 return -ENOMEM; 3374 3375 return cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, 3376 "arm64/mismatched_32bit_el0:online", 3377 enable_mismatched_32bit_el0, NULL); 3378 } 3379 subsys_initcall_sync(init_32bit_el0_mask); 3380 3381 static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap) 3382 { 3383 cpu_replace_ttbr1(lm_alias(swapper_pg_dir), idmap_pg_dir); 3384 } 3385 3386 /* 3387 * We emulate only the following system register space. 3388 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 2 - 7] 3389 * See Table C5-6 System instruction encodings for System register accesses, 3390 * ARMv8 ARM(ARM DDI 0487A.f) for more details. 3391 */ 3392 static inline bool __attribute_const__ is_emulated(u32 id) 3393 { 3394 return (sys_reg_Op0(id) == 0x3 && 3395 sys_reg_CRn(id) == 0x0 && 3396 sys_reg_Op1(id) == 0x0 && 3397 (sys_reg_CRm(id) == 0 || 3398 ((sys_reg_CRm(id) >= 2) && (sys_reg_CRm(id) <= 7)))); 3399 } 3400 3401 /* 3402 * With CRm == 0, reg should be one of : 3403 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1. 3404 */ 3405 static inline int emulate_id_reg(u32 id, u64 *valp) 3406 { 3407 switch (id) { 3408 case SYS_MIDR_EL1: 3409 *valp = read_cpuid_id(); 3410 break; 3411 case SYS_MPIDR_EL1: 3412 *valp = SYS_MPIDR_SAFE_VAL; 3413 break; 3414 case SYS_REVIDR_EL1: 3415 /* IMPLEMENTATION DEFINED values are emulated with 0 */ 3416 *valp = 0; 3417 break; 3418 default: 3419 return -EINVAL; 3420 } 3421 3422 return 0; 3423 } 3424 3425 static int emulate_sys_reg(u32 id, u64 *valp) 3426 { 3427 struct arm64_ftr_reg *regp; 3428 3429 if (!is_emulated(id)) 3430 return -EINVAL; 3431 3432 if (sys_reg_CRm(id) == 0) 3433 return emulate_id_reg(id, valp); 3434 3435 regp = get_arm64_ftr_reg_nowarn(id); 3436 if (regp) 3437 *valp = arm64_ftr_reg_user_value(regp); 3438 else 3439 /* 3440 * The untracked registers are either IMPLEMENTATION DEFINED 3441 * (e.g, ID_AFR0_EL1) or reserved RAZ. 3442 */ 3443 *valp = 0; 3444 return 0; 3445 } 3446 3447 int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt) 3448 { 3449 int rc; 3450 u64 val; 3451 3452 rc = emulate_sys_reg(sys_reg, &val); 3453 if (!rc) { 3454 pt_regs_write_reg(regs, rt, val); 3455 arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE); 3456 } 3457 return rc; 3458 } 3459 3460 bool try_emulate_mrs(struct pt_regs *regs, u32 insn) 3461 { 3462 u32 sys_reg, rt; 3463 3464 if (compat_user_mode(regs) || !aarch64_insn_is_mrs(insn)) 3465 return false; 3466 3467 /* 3468 * sys_reg values are defined as used in mrs/msr instruction. 3469 * shift the imm value to get the encoding. 3470 */ 3471 sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5; 3472 rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn); 3473 return do_emulate_mrs(regs, sys_reg, rt) == 0; 3474 } 3475 3476 enum mitigation_state arm64_get_meltdown_state(void) 3477 { 3478 if (__meltdown_safe) 3479 return SPECTRE_UNAFFECTED; 3480 3481 if (arm64_kernel_unmapped_at_el0()) 3482 return SPECTRE_MITIGATED; 3483 3484 return SPECTRE_VULNERABLE; 3485 } 3486 3487 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, 3488 char *buf) 3489 { 3490 switch (arm64_get_meltdown_state()) { 3491 case SPECTRE_UNAFFECTED: 3492 return sprintf(buf, "Not affected\n"); 3493 3494 case SPECTRE_MITIGATED: 3495 return sprintf(buf, "Mitigation: PTI\n"); 3496 3497 default: 3498 return sprintf(buf, "Vulnerable\n"); 3499 } 3500 } 3501