linux/arch/arm64/kernel/cpufeature.c
<<
>>
Prefs
   1/*
   2 * Contains CPU feature definitions
   3 *
   4 * Copyright (C) 2015 ARM Ltd.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU General Public License
  16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17 */
  18
  19#define pr_fmt(fmt) "CPU features: " fmt
  20
  21#include <linux/bsearch.h>
  22#include <linux/cpumask.h>
  23#include <linux/crash_dump.h>
  24#include <linux/sort.h>
  25#include <linux/stop_machine.h>
  26#include <linux/types.h>
  27#include <linux/mm.h>
  28#include <linux/cpu.h>
  29#include <asm/cpu.h>
  30#include <asm/cpufeature.h>
  31#include <asm/cpu_ops.h>
  32#include <asm/fpsimd.h>
  33#include <asm/mmu_context.h>
  34#include <asm/processor.h>
  35#include <asm/sysreg.h>
  36#include <asm/traps.h>
  37#include <asm/virt.h>
  38
  39unsigned long elf_hwcap __read_mostly;
  40EXPORT_SYMBOL_GPL(elf_hwcap);
  41
  42#ifdef CONFIG_COMPAT
  43#define COMPAT_ELF_HWCAP_DEFAULT        \
  44                                (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
  45                                 COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
  46                                 COMPAT_HWCAP_TLS|COMPAT_HWCAP_VFP|\
  47                                 COMPAT_HWCAP_VFPv3|COMPAT_HWCAP_VFPv4|\
  48                                 COMPAT_HWCAP_NEON|COMPAT_HWCAP_IDIV|\
  49                                 COMPAT_HWCAP_LPAE)
  50unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
  51unsigned int compat_elf_hwcap2 __read_mostly;
  52#endif
  53
  54DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
  55EXPORT_SYMBOL(cpu_hwcaps);
  56
  57/*
  58 * Flag to indicate if we have computed the system wide
  59 * capabilities based on the boot time active CPUs. This
  60 * will be used to determine if a new booting CPU should
  61 * go through the verification process to make sure that it
  62 * supports the system capabilities, without using a hotplug
  63 * notifier.
  64 */
  65static bool sys_caps_initialised;
  66
  67static inline void set_sys_caps_initialised(void)
  68{
  69        sys_caps_initialised = true;
  70}
  71
  72static int dump_cpu_hwcaps(struct notifier_block *self, unsigned long v, void *p)
  73{
  74        /* file-wide pr_fmt adds "CPU features: " prefix */
  75        pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
  76        return 0;
  77}
  78
  79static struct notifier_block cpu_hwcaps_notifier = {
  80        .notifier_call = dump_cpu_hwcaps
  81};
  82
  83static int __init register_cpu_hwcaps_dumper(void)
  84{
  85        atomic_notifier_chain_register(&panic_notifier_list,
  86                                       &cpu_hwcaps_notifier);
  87        return 0;
  88}
  89__initcall(register_cpu_hwcaps_dumper);
  90
  91DEFINE_STATIC_KEY_ARRAY_FALSE(cpu_hwcap_keys, ARM64_NCAPS);
  92EXPORT_SYMBOL(cpu_hwcap_keys);
  93
  94#define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
  95        {                                               \
  96                .sign = SIGNED,                         \
  97                .visible = VISIBLE,                     \
  98                .strict = STRICT,                       \
  99                .type = TYPE,                           \
 100                .shift = SHIFT,                         \
 101                .width = WIDTH,                         \
 102                .safe_val = SAFE_VAL,                   \
 103        }
 104
 105/* Define a feature with unsigned values */
 106#define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
 107        __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
 108
 109/* Define a feature with a signed value */
 110#define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
 111        __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
 112
 113#define ARM64_FTR_END                                   \
 114        {                                               \
 115                .width = 0,                             \
 116        }
 117
 118/* meta feature for alternatives */
 119static bool __maybe_unused
 120cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused);
 121
 122static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
 123
 124/*
 125 * NOTE: Any changes to the visibility of features should be kept in
 126 * sync with the documentation of the CPU feature register ABI.
 127 */
 128static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
 129        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_TS_SHIFT, 4, 0),
 130        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_FHM_SHIFT, 4, 0),
 131        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_DP_SHIFT, 4, 0),
 132        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM4_SHIFT, 4, 0),
 133        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SM3_SHIFT, 4, 0),
 134        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA3_SHIFT, 4, 0),
 135        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_RDM_SHIFT, 4, 0),
 136        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_ATOMICS_SHIFT, 4, 0),
 137        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_CRC32_SHIFT, 4, 0),
 138        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA2_SHIFT, 4, 0),
 139        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_SHA1_SHIFT, 4, 0),
 140        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_AES_SHIFT, 4, 0),
 141        ARM64_FTR_END,
 142};
 143
 144static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
 145        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_SB_SHIFT, 4, 0),
 146        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_LRCPC_SHIFT, 4, 0),
 147        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_FCMA_SHIFT, 4, 0),
 148        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_JSCVT_SHIFT, 4, 0),
 149        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_DPB_SHIFT, 4, 0),
 150        ARM64_FTR_END,
 151};
 152
 153static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
 154        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV3_SHIFT, 4, 0),
 155        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_CSV2_SHIFT, 4, 0),
 156        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_DIT_SHIFT, 4, 0),
 157        ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
 158                                   FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_SVE_SHIFT, 4, 0),
 159        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_RAS_SHIFT, 4, 0),
 160        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_GIC_SHIFT, 4, 0),
 161        S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_ASIMD_SHIFT, 4, ID_AA64PFR0_ASIMD_NI),
 162        S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_FP_SHIFT, 4, ID_AA64PFR0_FP_NI),
 163        /* Linux doesn't care about the EL3 */
 164        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL3_SHIFT, 4, 0),
 165        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL2_SHIFT, 4, 0),
 166        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SHIFT, 4, ID_AA64PFR0_EL1_64BIT_ONLY),
 167        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL0_SHIFT, 4, ID_AA64PFR0_EL0_64BIT_ONLY),
 168        ARM64_FTR_END,
 169};
 170
 171static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
 172        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_SSBS_SHIFT, 4, ID_AA64PFR1_SSBS_PSTATE_NI),
 173        ARM64_FTR_END,
 174};
 175
 176static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
 177        S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN4_SHIFT, 4, ID_AA64MMFR0_TGRAN4_NI),
 178        S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN64_SHIFT, 4, ID_AA64MMFR0_TGRAN64_NI),
 179        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_TGRAN16_SHIFT, 4, ID_AA64MMFR0_TGRAN16_NI),
 180        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL0_SHIFT, 4, 0),
 181        /* Linux shouldn't care about secure memory */
 182        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_SNSMEM_SHIFT, 4, 0),
 183        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_BIGENDEL_SHIFT, 4, 0),
 184        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_ASID_SHIFT, 4, 0),
 185        /*
 186         * Differing PARange is fine as long as all peripherals and memory are mapped
 187         * within the minimum PARange of all CPUs
 188         */
 189        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_PARANGE_SHIFT, 4, 0),
 190        ARM64_FTR_END,
 191};
 192
 193static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
 194        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_PAN_SHIFT, 4, 0),
 195        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_LOR_SHIFT, 4, 0),
 196        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HPD_SHIFT, 4, 0),
 197        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VHE_SHIFT, 4, 0),
 198        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_VMIDBITS_SHIFT, 4, 0),
 199        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_HADBS_SHIFT, 4, 0),
 200        ARM64_FTR_END,
 201};
 202
 203static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
 204        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_FWB_SHIFT, 4, 0),
 205        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_AT_SHIFT, 4, 0),
 206        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LVA_SHIFT, 4, 0),
 207        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_IESB_SHIFT, 4, 0),
 208        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_LSM_SHIFT, 4, 0),
 209        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_UAO_SHIFT, 4, 0),
 210        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_CNP_SHIFT, 4, 0),
 211        ARM64_FTR_END,
 212};
 213
 214static const struct arm64_ftr_bits ftr_ctr[] = {
 215        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
 216        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DIC_SHIFT, 1, 1),
 217        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IDC_SHIFT, 1, 1),
 218        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_SAFE, CTR_CWG_SHIFT, 4, 0),
 219        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_SAFE, CTR_ERG_SHIFT, 4, 0),
 220        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_DMINLINE_SHIFT, 4, 1),
 221        /*
 222         * Linux can handle differing I-cache policies. Userspace JITs will
 223         * make use of *minLine.
 224         * If we have differing I-cache policies, report it as the weakest - VIPT.
 225         */
 226        ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, 14, 2, ICACHE_POLICY_VIPT),       /* L1Ip */
 227        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_IMINLINE_SHIFT, 4, 0),
 228        ARM64_FTR_END,
 229};
 230
 231struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
 232        .name           = "SYS_CTR_EL0",
 233        .ftr_bits       = ftr_ctr
 234};
 235
 236static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
 237        S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0xf),   /* InnerShr */
 238        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),       /* FCSE */
 239        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, 20, 4, 0),    /* AuxReg */
 240        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),       /* TCM */
 241        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),       /* ShareLvl */
 242        S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0xf),    /* OuterShr */
 243        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),        /* PMSA */
 244        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),        /* VMSA */
 245        ARM64_FTR_END,
 246};
 247
 248static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
 249        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 36, 28, 0),
 250        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_PMSVER_SHIFT, 4, 0),
 251        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_CTX_CMPS_SHIFT, 4, 0),
 252        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_WRPS_SHIFT, 4, 0),
 253        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_BRPS_SHIFT, 4, 0),
 254        /*
 255         * We can instantiate multiple PMU instances with different levels
 256         * of support.
 257         */
 258        S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_PMUVER_SHIFT, 4, 0),
 259        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_TRACEVER_SHIFT, 4, 0),
 260        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_DEBUGVER_SHIFT, 4, 0x6),
 261        ARM64_FTR_END,
 262};
 263
 264static const struct arm64_ftr_bits ftr_mvfr2[] = {
 265        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),                /* FPMisc */
 266        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),                /* SIMDMisc */
 267        ARM64_FTR_END,
 268};
 269
 270static const struct arm64_ftr_bits ftr_dczid[] = {
 271        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 4, 1, 1),            /* DZP */
 272        ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),       /* BS */
 273        ARM64_FTR_END,
 274};
 275
 276
 277static const struct arm64_ftr_bits ftr_id_isar5[] = {
 278        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_RDM_SHIFT, 4, 0),
 279        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_CRC32_SHIFT, 4, 0),
 280        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA2_SHIFT, 4, 0),
 281        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA1_SHIFT, 4, 0),
 282        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_AES_SHIFT, 4, 0),
 283        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SEVL_SHIFT, 4, 0),
 284        ARM64_FTR_END,
 285};
 286
 287static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
 288        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),        /* ac2 */
 289        ARM64_FTR_END,
 290};
 291
 292static const struct arm64_ftr_bits ftr_id_pfr0[] = {
 293        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),               /* State3 */
 294        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),                /* State2 */
 295        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),                /* State1 */
 296        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),                /* State0 */
 297        ARM64_FTR_END,
 298};
 299
 300static const struct arm64_ftr_bits ftr_id_dfr0[] = {
 301        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
 302        S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0xf),   /* PerfMon */
 303        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
 304        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
 305        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
 306        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
 307        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
 308        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
 309        ARM64_FTR_END,
 310};
 311
 312static const struct arm64_ftr_bits ftr_zcr[] = {
 313        ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
 314                ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_SIZE, 0),        /* LEN */
 315        ARM64_FTR_END,
 316};
 317
 318/*
 319 * Common ftr bits for a 32bit register with all hidden, strict
 320 * attributes, with 4bit feature fields and a default safe value of
 321 * 0. Covers the following 32bit registers:
 322 * id_isar[0-4], id_mmfr[1-3], id_pfr1, mvfr[0-1]
 323 */
 324static const struct arm64_ftr_bits ftr_generic_32bits[] = {
 325        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
 326        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
 327        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
 328        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
 329        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
 330        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
 331        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
 332        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
 333        ARM64_FTR_END,
 334};
 335
 336/* Table for a single 32bit feature value */
 337static const struct arm64_ftr_bits ftr_single32[] = {
 338        ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
 339        ARM64_FTR_END,
 340};
 341
 342static const struct arm64_ftr_bits ftr_raz[] = {
 343        ARM64_FTR_END,
 344};
 345
 346#define ARM64_FTR_REG(id, table) {              \
 347        .sys_id = id,                           \
 348        .reg =  &(struct arm64_ftr_reg){        \
 349                .name = #id,                    \
 350                .ftr_bits = &((table)[0]),      \
 351        }}
 352
 353static const struct __ftr_reg_entry {
 354        u32                     sys_id;
 355        struct arm64_ftr_reg    *reg;
 356} arm64_ftr_regs[] = {
 357
 358        /* Op1 = 0, CRn = 0, CRm = 1 */
 359        ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
 360        ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_generic_32bits),
 361        ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
 362        ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
 363        ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
 364        ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
 365        ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
 366
 367        /* Op1 = 0, CRn = 0, CRm = 2 */
 368        ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_generic_32bits),
 369        ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
 370        ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
 371        ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
 372        ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_generic_32bits),
 373        ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
 374        ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
 375
 376        /* Op1 = 0, CRn = 0, CRm = 3 */
 377        ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_generic_32bits),
 378        ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_generic_32bits),
 379        ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
 380
 381        /* Op1 = 0, CRn = 0, CRm = 4 */
 382        ARM64_FTR_REG(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0),
 383        ARM64_FTR_REG(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1),
 384        ARM64_FTR_REG(SYS_ID_AA64ZFR0_EL1, ftr_raz),
 385
 386        /* Op1 = 0, CRn = 0, CRm = 5 */
 387        ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
 388        ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
 389
 390        /* Op1 = 0, CRn = 0, CRm = 6 */
 391        ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
 392        ARM64_FTR_REG(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1),
 393
 394        /* Op1 = 0, CRn = 0, CRm = 7 */
 395        ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
 396        ARM64_FTR_REG(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1),
 397        ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
 398
 399        /* Op1 = 0, CRn = 1, CRm = 2 */
 400        ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
 401
 402        /* Op1 = 3, CRn = 0, CRm = 0 */
 403        { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
 404        ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
 405
 406        /* Op1 = 3, CRn = 14, CRm = 0 */
 407        ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
 408};
 409
 410static int search_cmp_ftr_reg(const void *id, const void *regp)
 411{
 412        return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
 413}
 414
 415/*
 416 * get_arm64_ftr_reg - Lookup a feature register entry using its
 417 * sys_reg() encoding. With the array arm64_ftr_regs sorted in the
 418 * ascending order of sys_id , we use binary search to find a matching
 419 * entry.
 420 *
 421 * returns - Upon success,  matching ftr_reg entry for id.
 422 *         - NULL on failure. It is upto the caller to decide
 423 *           the impact of a failure.
 424 */
 425static struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
 426{
 427        const struct __ftr_reg_entry *ret;
 428
 429        ret = bsearch((const void *)(unsigned long)sys_id,
 430                        arm64_ftr_regs,
 431                        ARRAY_SIZE(arm64_ftr_regs),
 432                        sizeof(arm64_ftr_regs[0]),
 433                        search_cmp_ftr_reg);
 434        if (ret)
 435                return ret->reg;
 436        return NULL;
 437}
 438
 439static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
 440                               s64 ftr_val)
 441{
 442        u64 mask = arm64_ftr_mask(ftrp);
 443
 444        reg &= ~mask;
 445        reg |= (ftr_val << ftrp->shift) & mask;
 446        return reg;
 447}
 448
 449static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
 450                                s64 cur)
 451{
 452        s64 ret = 0;
 453
 454        switch (ftrp->type) {
 455        case FTR_EXACT:
 456                ret = ftrp->safe_val;
 457                break;
 458        case FTR_LOWER_SAFE:
 459                ret = new < cur ? new : cur;
 460                break;
 461        case FTR_HIGHER_SAFE:
 462                ret = new > cur ? new : cur;
 463                break;
 464        default:
 465                BUG();
 466        }
 467
 468        return ret;
 469}
 470
 471static void __init sort_ftr_regs(void)
 472{
 473        int i;
 474
 475        /* Check that the array is sorted so that we can do the binary search */
 476        for (i = 1; i < ARRAY_SIZE(arm64_ftr_regs); i++)
 477                BUG_ON(arm64_ftr_regs[i].sys_id < arm64_ftr_regs[i - 1].sys_id);
 478}
 479
 480/*
 481 * Initialise the CPU feature register from Boot CPU values.
 482 * Also initiliases the strict_mask for the register.
 483 * Any bits that are not covered by an arm64_ftr_bits entry are considered
 484 * RES0 for the system-wide value, and must strictly match.
 485 */
 486static void __init init_cpu_ftr_reg(u32 sys_reg, u64 new)
 487{
 488        u64 val = 0;
 489        u64 strict_mask = ~0x0ULL;
 490        u64 user_mask = 0;
 491        u64 valid_mask = 0;
 492
 493        const struct arm64_ftr_bits *ftrp;
 494        struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
 495
 496        BUG_ON(!reg);
 497
 498        for (ftrp  = reg->ftr_bits; ftrp->width; ftrp++) {
 499                u64 ftr_mask = arm64_ftr_mask(ftrp);
 500                s64 ftr_new = arm64_ftr_value(ftrp, new);
 501
 502                val = arm64_ftr_set_value(ftrp, val, ftr_new);
 503
 504                valid_mask |= ftr_mask;
 505                if (!ftrp->strict)
 506                        strict_mask &= ~ftr_mask;
 507                if (ftrp->visible)
 508                        user_mask |= ftr_mask;
 509                else
 510                        reg->user_val = arm64_ftr_set_value(ftrp,
 511                                                            reg->user_val,
 512                                                            ftrp->safe_val);
 513        }
 514
 515        val &= valid_mask;
 516
 517        reg->sys_val = val;
 518        reg->strict_mask = strict_mask;
 519        reg->user_mask = user_mask;
 520}
 521
 522extern const struct arm64_cpu_capabilities arm64_errata[];
 523static void __init setup_boot_cpu_capabilities(void);
 524
 525void __init init_cpu_features(struct cpuinfo_arm64 *info)
 526{
 527        /* Before we start using the tables, make sure it is sorted */
 528        sort_ftr_regs();
 529
 530        init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
 531        init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
 532        init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
 533        init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
 534        init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
 535        init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
 536        init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
 537        init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
 538        init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
 539        init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
 540        init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
 541        init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
 542        init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
 543
 544        if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
 545                init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
 546                init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
 547                init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
 548                init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
 549                init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
 550                init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
 551                init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
 552                init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
 553                init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
 554                init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
 555                init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
 556                init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
 557                init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
 558                init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
 559                init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
 560                init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
 561        }
 562
 563        if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
 564                init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
 565                sve_init_vq_map();
 566        }
 567
 568        /*
 569         * Detect and enable early CPU capabilities based on the boot CPU,
 570         * after we have initialised the CPU feature infrastructure.
 571         */
 572        setup_boot_cpu_capabilities();
 573}
 574
 575static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
 576{
 577        const struct arm64_ftr_bits *ftrp;
 578
 579        for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
 580                s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
 581                s64 ftr_new = arm64_ftr_value(ftrp, new);
 582
 583                if (ftr_cur == ftr_new)
 584                        continue;
 585                /* Find a safe value */
 586                ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
 587                reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
 588        }
 589
 590}
 591
 592static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
 593{
 594        struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
 595
 596        BUG_ON(!regp);
 597        update_cpu_ftr_reg(regp, val);
 598        if ((boot & regp->strict_mask) == (val & regp->strict_mask))
 599                return 0;
 600        pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
 601                        regp->name, boot, cpu, val);
 602        return 1;
 603}
 604
 605/*
 606 * Update system wide CPU feature registers with the values from a
 607 * non-boot CPU. Also performs SANITY checks to make sure that there
 608 * aren't any insane variations from that of the boot CPU.
 609 */
 610void update_cpu_features(int cpu,
 611                         struct cpuinfo_arm64 *info,
 612                         struct cpuinfo_arm64 *boot)
 613{
 614        int taint = 0;
 615
 616        /*
 617         * The kernel can handle differing I-cache policies, but otherwise
 618         * caches should look identical. Userspace JITs will make use of
 619         * *minLine.
 620         */
 621        taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
 622                                      info->reg_ctr, boot->reg_ctr);
 623
 624        /*
 625         * Userspace may perform DC ZVA instructions. Mismatched block sizes
 626         * could result in too much or too little memory being zeroed if a
 627         * process is preempted and migrated between CPUs.
 628         */
 629        taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
 630                                      info->reg_dczid, boot->reg_dczid);
 631
 632        /* If different, timekeeping will be broken (especially with KVM) */
 633        taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
 634                                      info->reg_cntfrq, boot->reg_cntfrq);
 635
 636        /*
 637         * The kernel uses self-hosted debug features and expects CPUs to
 638         * support identical debug features. We presently need CTX_CMPs, WRPs,
 639         * and BRPs to be identical.
 640         * ID_AA64DFR1 is currently RES0.
 641         */
 642        taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
 643                                      info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
 644        taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
 645                                      info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
 646        /*
 647         * Even in big.LITTLE, processors should be identical instruction-set
 648         * wise.
 649         */
 650        taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
 651                                      info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
 652        taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
 653                                      info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
 654
 655        /*
 656         * Differing PARange support is fine as long as all peripherals and
 657         * memory are mapped within the minimum PARange of all CPUs.
 658         * Linux should not care about secure memory.
 659         */
 660        taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
 661                                      info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
 662        taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
 663                                      info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
 664        taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
 665                                      info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
 666
 667        /*
 668         * EL3 is not our concern.
 669         */
 670        taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
 671                                      info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
 672        taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
 673                                      info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
 674
 675        taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
 676                                      info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
 677
 678        /*
 679         * If we have AArch32, we care about 32-bit features for compat.
 680         * If the system doesn't support AArch32, don't update them.
 681         */
 682        if (id_aa64pfr0_32bit_el0(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
 683                id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
 684
 685                taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
 686                                        info->reg_id_dfr0, boot->reg_id_dfr0);
 687                taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
 688                                        info->reg_id_isar0, boot->reg_id_isar0);
 689                taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
 690                                        info->reg_id_isar1, boot->reg_id_isar1);
 691                taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
 692                                        info->reg_id_isar2, boot->reg_id_isar2);
 693                taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
 694                                        info->reg_id_isar3, boot->reg_id_isar3);
 695                taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
 696                                        info->reg_id_isar4, boot->reg_id_isar4);
 697                taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
 698                                        info->reg_id_isar5, boot->reg_id_isar5);
 699
 700                /*
 701                 * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
 702                 * ACTLR formats could differ across CPUs and therefore would have to
 703                 * be trapped for virtualization anyway.
 704                 */
 705                taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
 706                                        info->reg_id_mmfr0, boot->reg_id_mmfr0);
 707                taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
 708                                        info->reg_id_mmfr1, boot->reg_id_mmfr1);
 709                taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
 710                                        info->reg_id_mmfr2, boot->reg_id_mmfr2);
 711                taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
 712                                        info->reg_id_mmfr3, boot->reg_id_mmfr3);
 713                taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
 714                                        info->reg_id_pfr0, boot->reg_id_pfr0);
 715                taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
 716                                        info->reg_id_pfr1, boot->reg_id_pfr1);
 717                taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
 718                                        info->reg_mvfr0, boot->reg_mvfr0);
 719                taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
 720                                        info->reg_mvfr1, boot->reg_mvfr1);
 721                taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
 722                                        info->reg_mvfr2, boot->reg_mvfr2);
 723        }
 724
 725        if (id_aa64pfr0_sve(info->reg_id_aa64pfr0)) {
 726                taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
 727                                        info->reg_zcr, boot->reg_zcr);
 728
 729                /* Probe vector lengths, unless we already gave up on SVE */
 730                if (id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1)) &&
 731                    !sys_caps_initialised)
 732                        sve_update_vq_map();
 733        }
 734
 735        /*
 736         * Mismatched CPU features are a recipe for disaster. Don't even
 737         * pretend to support them.
 738         */
 739        if (taint) {
 740                pr_warn_once("Unsupported CPU feature variation detected.\n");
 741                add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
 742        }
 743}
 744
 745u64 read_sanitised_ftr_reg(u32 id)
 746{
 747        struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
 748
 749        /* We shouldn't get a request for an unsupported register */
 750        BUG_ON(!regp);
 751        return regp->sys_val;
 752}
 753
 754#define read_sysreg_case(r)     \
 755        case r:         return read_sysreg_s(r)
 756
 757/*
 758 * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
 759 * Read the system register on the current CPU
 760 */
 761static u64 __read_sysreg_by_encoding(u32 sys_id)
 762{
 763        switch (sys_id) {
 764        read_sysreg_case(SYS_ID_PFR0_EL1);
 765        read_sysreg_case(SYS_ID_PFR1_EL1);
 766        read_sysreg_case(SYS_ID_DFR0_EL1);
 767        read_sysreg_case(SYS_ID_MMFR0_EL1);
 768        read_sysreg_case(SYS_ID_MMFR1_EL1);
 769        read_sysreg_case(SYS_ID_MMFR2_EL1);
 770        read_sysreg_case(SYS_ID_MMFR3_EL1);
 771        read_sysreg_case(SYS_ID_ISAR0_EL1);
 772        read_sysreg_case(SYS_ID_ISAR1_EL1);
 773        read_sysreg_case(SYS_ID_ISAR2_EL1);
 774        read_sysreg_case(SYS_ID_ISAR3_EL1);
 775        read_sysreg_case(SYS_ID_ISAR4_EL1);
 776        read_sysreg_case(SYS_ID_ISAR5_EL1);
 777        read_sysreg_case(SYS_MVFR0_EL1);
 778        read_sysreg_case(SYS_MVFR1_EL1);
 779        read_sysreg_case(SYS_MVFR2_EL1);
 780
 781        read_sysreg_case(SYS_ID_AA64PFR0_EL1);
 782        read_sysreg_case(SYS_ID_AA64PFR1_EL1);
 783        read_sysreg_case(SYS_ID_AA64DFR0_EL1);
 784        read_sysreg_case(SYS_ID_AA64DFR1_EL1);
 785        read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
 786        read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
 787        read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
 788        read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
 789        read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
 790
 791        read_sysreg_case(SYS_CNTFRQ_EL0);
 792        read_sysreg_case(SYS_CTR_EL0);
 793        read_sysreg_case(SYS_DCZID_EL0);
 794
 795        default:
 796                BUG();
 797                return 0;
 798        }
 799}
 800
 801#include <linux/irqchip/arm-gic-v3.h>
 802
 803static bool
 804feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
 805{
 806        int val = cpuid_feature_extract_field(reg, entry->field_pos, entry->sign);
 807
 808        return val >= entry->min_field_value;
 809}
 810
 811static bool
 812has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
 813{
 814        u64 val;
 815
 816        WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
 817        if (scope == SCOPE_SYSTEM)
 818                val = read_sanitised_ftr_reg(entry->sys_reg);
 819        else
 820                val = __read_sysreg_by_encoding(entry->sys_reg);
 821
 822        return feature_matches(val, entry);
 823}
 824
 825static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
 826{
 827        bool has_sre;
 828
 829        if (!has_cpuid_feature(entry, scope))
 830                return false;
 831
 832        has_sre = gic_enable_sre();
 833        if (!has_sre)
 834                pr_warn_once("%s present but disabled by higher exception level\n",
 835                             entry->desc);
 836
 837        return has_sre;
 838}
 839
 840static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
 841{
 842        u32 midr = read_cpuid_id();
 843
 844        /* Cavium ThunderX pass 1.x and 2.x */
 845        return MIDR_IS_CPU_MODEL_RANGE(midr, MIDR_THUNDERX,
 846                MIDR_CPU_VAR_REV(0, 0),
 847                MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
 848}
 849
 850static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
 851{
 852        u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
 853
 854        return cpuid_feature_extract_signed_field(pfr0,
 855                                        ID_AA64PFR0_FP_SHIFT) < 0;
 856}
 857
 858static bool has_cache_idc(const struct arm64_cpu_capabilities *entry,
 859                          int scope)
 860{
 861        u64 ctr;
 862
 863        if (scope == SCOPE_SYSTEM)
 864                ctr = arm64_ftr_reg_ctrel0.sys_val;
 865        else
 866                ctr = read_cpuid_effective_cachetype();
 867
 868        return ctr & BIT(CTR_IDC_SHIFT);
 869}
 870
 871static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused)
 872{
 873        /*
 874         * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively
 875         * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses
 876         * to the CTR_EL0 on this CPU and emulate it with the real/safe
 877         * value.
 878         */
 879        if (!(read_cpuid_cachetype() & BIT(CTR_IDC_SHIFT)))
 880                sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
 881}
 882
 883static bool has_cache_dic(const struct arm64_cpu_capabilities *entry,
 884                          int scope)
 885{
 886        u64 ctr;
 887
 888        if (scope == SCOPE_SYSTEM)
 889                ctr = arm64_ftr_reg_ctrel0.sys_val;
 890        else
 891                ctr = read_cpuid_cachetype();
 892
 893        return ctr & BIT(CTR_DIC_SHIFT);
 894}
 895
 896static bool __maybe_unused
 897has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
 898{
 899        /*
 900         * Kdump isn't guaranteed to power-off all secondary CPUs, CNP
 901         * may share TLB entries with a CPU stuck in the crashed
 902         * kernel.
 903         */
 904         if (is_kdump_kernel())
 905                return false;
 906
 907        return has_cpuid_feature(entry, scope);
 908}
 909
 910static bool __meltdown_safe = true;
 911static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
 912
 913static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
 914                                int scope)
 915{
 916        /* List of CPUs that are not vulnerable and don't need KPTI */
 917        static const struct midr_range kpti_safe_list[] = {
 918                MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
 919                MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
 920                MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
 921                MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
 922                MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
 923                MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
 924                MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
 925                MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
 926                MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
 927                { /* sentinel */ }
 928        };
 929        char const *str = "kpti command line option";
 930        bool meltdown_safe;
 931
 932        meltdown_safe = is_midr_in_range_list(read_cpuid_id(), kpti_safe_list);
 933
 934        /* Defer to CPU feature registers */
 935        if (has_cpuid_feature(entry, scope))
 936                meltdown_safe = true;
 937
 938        if (!meltdown_safe)
 939                __meltdown_safe = false;
 940
 941        /*
 942         * For reasons that aren't entirely clear, enabling KPTI on Cavium
 943         * ThunderX leads to apparent I-cache corruption of kernel text, which
 944         * ends as well as you might imagine. Don't even try.
 945         */
 946        if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
 947                str = "ARM64_WORKAROUND_CAVIUM_27456";
 948                __kpti_forced = -1;
 949        }
 950
 951        /* Useful for KASLR robustness */
 952        if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_offset() > 0) {
 953                if (!__kpti_forced) {
 954                        str = "KASLR";
 955                        __kpti_forced = 1;
 956                }
 957        }
 958
 959        if (cpu_mitigations_off() && !__kpti_forced) {
 960                str = "mitigations=off";
 961                __kpti_forced = -1;
 962        }
 963
 964        if (!IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) {
 965                pr_info_once("kernel page table isolation disabled by kernel configuration\n");
 966                return false;
 967        }
 968
 969        /* Forced? */
 970        if (__kpti_forced) {
 971                pr_info_once("kernel page table isolation forced %s by %s\n",
 972                             __kpti_forced > 0 ? "ON" : "OFF", str);
 973                return __kpti_forced > 0;
 974        }
 975
 976        return !meltdown_safe;
 977}
 978
 979#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
 980static void
 981kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
 982{
 983        typedef void (kpti_remap_fn)(int, int, phys_addr_t);
 984        extern kpti_remap_fn idmap_kpti_install_ng_mappings;
 985        kpti_remap_fn *remap_fn;
 986
 987        static bool kpti_applied = false;
 988        int cpu = smp_processor_id();
 989
 990        /*
 991         * We don't need to rewrite the page-tables if either we've done
 992         * it already or we have KASLR enabled and therefore have not
 993         * created any global mappings at all.
 994         */
 995        if (kpti_applied || kaslr_offset() > 0)
 996                return;
 997
 998        remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
 999
1000        cpu_install_idmap();
1001        remap_fn(cpu, num_online_cpus(), __pa_symbol(swapper_pg_dir));
1002        cpu_uninstall_idmap();
1003
1004        if (!cpu)
1005                kpti_applied = true;
1006
1007        return;
1008}
1009#else
1010static void
1011kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
1012{
1013}
1014#endif  /* CONFIG_UNMAP_KERNEL_AT_EL0 */
1015
1016static int __init parse_kpti(char *str)
1017{
1018        bool enabled;
1019        int ret = strtobool(str, &enabled);
1020
1021        if (ret)
1022                return ret;
1023
1024        __kpti_forced = enabled ? 1 : -1;
1025        return 0;
1026}
1027early_param("kpti", parse_kpti);
1028
1029#ifdef CONFIG_ARM64_HW_AFDBM
1030static inline void __cpu_enable_hw_dbm(void)
1031{
1032        u64 tcr = read_sysreg(tcr_el1) | TCR_HD;
1033
1034        write_sysreg(tcr, tcr_el1);
1035        isb();
1036}
1037
1038static bool cpu_has_broken_dbm(void)
1039{
1040        /* List of CPUs which have broken DBM support. */
1041        static const struct midr_range cpus[] = {
1042#ifdef CONFIG_ARM64_ERRATUM_1024718
1043                MIDR_RANGE(MIDR_CORTEX_A55, 0, 0, 1, 0),  // A55 r0p0 -r1p0
1044#endif
1045                {},
1046        };
1047
1048        return is_midr_in_range_list(read_cpuid_id(), cpus);
1049}
1050
1051static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap)
1052{
1053        return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) &&
1054               !cpu_has_broken_dbm();
1055}
1056
1057static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap)
1058{
1059        if (cpu_can_use_dbm(cap))
1060                __cpu_enable_hw_dbm();
1061}
1062
1063static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap,
1064                       int __unused)
1065{
1066        static bool detected = false;
1067        /*
1068         * DBM is a non-conflicting feature. i.e, the kernel can safely
1069         * run a mix of CPUs with and without the feature. So, we
1070         * unconditionally enable the capability to allow any late CPU
1071         * to use the feature. We only enable the control bits on the
1072         * CPU, if it actually supports.
1073         *
1074         * We have to make sure we print the "feature" detection only
1075         * when at least one CPU actually uses it. So check if this CPU
1076         * can actually use it and print the message exactly once.
1077         *
1078         * This is safe as all CPUs (including secondary CPUs - due to the
1079         * LOCAL_CPU scope - and the hotplugged CPUs - via verification)
1080         * goes through the "matches" check exactly once. Also if a CPU
1081         * matches the criteria, it is guaranteed that the CPU will turn
1082         * the DBM on, as the capability is unconditionally enabled.
1083         */
1084        if (!detected && cpu_can_use_dbm(cap)) {
1085                detected = true;
1086                pr_info("detected: Hardware dirty bit management\n");
1087        }
1088
1089        return true;
1090}
1091
1092#endif
1093
1094#ifdef CONFIG_ARM64_VHE
1095static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
1096{
1097        return is_kernel_in_hyp_mode();
1098}
1099
1100static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
1101{
1102        /*
1103         * Copy register values that aren't redirected by hardware.
1104         *
1105         * Before code patching, we only set tpidr_el1, all CPUs need to copy
1106         * this value to tpidr_el2 before we patch the code. Once we've done
1107         * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
1108         * do anything here.
1109         */
1110        if (!alternatives_applied)
1111                write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
1112}
1113#endif
1114
1115static void cpu_has_fwb(const struct arm64_cpu_capabilities *__unused)
1116{
1117        u64 val = read_sysreg_s(SYS_CLIDR_EL1);
1118
1119        /* Check that CLIDR_EL1.LOU{U,IS} are both 0 */
1120        WARN_ON(val & (7 << 27 | 7 << 21));
1121}
1122
1123#ifdef CONFIG_ARM64_SSBD
1124static int ssbs_emulation_handler(struct pt_regs *regs, u32 instr)
1125{
1126        if (user_mode(regs))
1127                return 1;
1128
1129        if (instr & BIT(CRm_shift))
1130                regs->pstate |= PSR_SSBS_BIT;
1131        else
1132                regs->pstate &= ~PSR_SSBS_BIT;
1133
1134        arm64_skip_faulting_instruction(regs, 4);
1135        return 0;
1136}
1137
1138static struct undef_hook ssbs_emulation_hook = {
1139        .instr_mask     = ~(1U << CRm_shift),
1140        .instr_val      = 0xd500001f | REG_PSTATE_SSBS_IMM,
1141        .fn             = ssbs_emulation_handler,
1142};
1143
1144static void cpu_enable_ssbs(const struct arm64_cpu_capabilities *__unused)
1145{
1146        static bool undef_hook_registered = false;
1147        static DEFINE_SPINLOCK(hook_lock);
1148
1149        spin_lock(&hook_lock);
1150        if (!undef_hook_registered) {
1151                register_undef_hook(&ssbs_emulation_hook);
1152                undef_hook_registered = true;
1153        }
1154        spin_unlock(&hook_lock);
1155
1156        if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE) {
1157                sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
1158                arm64_set_ssbd_mitigation(false);
1159        } else {
1160                arm64_set_ssbd_mitigation(true);
1161        }
1162}
1163#endif /* CONFIG_ARM64_SSBD */
1164
1165static const struct arm64_cpu_capabilities arm64_features[] = {
1166        {
1167                .desc = "GIC system register CPU interface",
1168                .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
1169                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1170                .matches = has_useable_gicv3_cpuif,
1171                .sys_reg = SYS_ID_AA64PFR0_EL1,
1172                .field_pos = ID_AA64PFR0_GIC_SHIFT,
1173                .sign = FTR_UNSIGNED,
1174                .min_field_value = 1,
1175        },
1176#ifdef CONFIG_ARM64_PAN
1177        {
1178                .desc = "Privileged Access Never",
1179                .capability = ARM64_HAS_PAN,
1180                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1181                .matches = has_cpuid_feature,
1182                .sys_reg = SYS_ID_AA64MMFR1_EL1,
1183                .field_pos = ID_AA64MMFR1_PAN_SHIFT,
1184                .sign = FTR_UNSIGNED,
1185                .min_field_value = 1,
1186                .cpu_enable = cpu_enable_pan,
1187        },
1188#endif /* CONFIG_ARM64_PAN */
1189#if defined(CONFIG_AS_LSE) && defined(CONFIG_ARM64_LSE_ATOMICS)
1190        {
1191                .desc = "LSE atomic instructions",
1192                .capability = ARM64_HAS_LSE_ATOMICS,
1193                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1194                .matches = has_cpuid_feature,
1195                .sys_reg = SYS_ID_AA64ISAR0_EL1,
1196                .field_pos = ID_AA64ISAR0_ATOMICS_SHIFT,
1197                .sign = FTR_UNSIGNED,
1198                .min_field_value = 2,
1199        },
1200#endif /* CONFIG_AS_LSE && CONFIG_ARM64_LSE_ATOMICS */
1201        {
1202                .desc = "Software prefetching using PRFM",
1203                .capability = ARM64_HAS_NO_HW_PREFETCH,
1204                .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1205                .matches = has_no_hw_prefetch,
1206        },
1207#ifdef CONFIG_ARM64_UAO
1208        {
1209                .desc = "User Access Override",
1210                .capability = ARM64_HAS_UAO,
1211                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1212                .matches = has_cpuid_feature,
1213                .sys_reg = SYS_ID_AA64MMFR2_EL1,
1214                .field_pos = ID_AA64MMFR2_UAO_SHIFT,
1215                .min_field_value = 1,
1216                /*
1217                 * We rely on stop_machine() calling uao_thread_switch() to set
1218                 * UAO immediately after patching.
1219                 */
1220        },
1221#endif /* CONFIG_ARM64_UAO */
1222#ifdef CONFIG_ARM64_PAN
1223        {
1224                .capability = ARM64_ALT_PAN_NOT_UAO,
1225                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1226                .matches = cpufeature_pan_not_uao,
1227        },
1228#endif /* CONFIG_ARM64_PAN */
1229#ifdef CONFIG_ARM64_VHE
1230        {
1231                .desc = "Virtualization Host Extensions",
1232                .capability = ARM64_HAS_VIRT_HOST_EXTN,
1233                .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
1234                .matches = runs_at_el2,
1235                .cpu_enable = cpu_copy_el2regs,
1236        },
1237#endif  /* CONFIG_ARM64_VHE */
1238        {
1239                .desc = "32-bit EL0 Support",
1240                .capability = ARM64_HAS_32BIT_EL0,
1241                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1242                .matches = has_cpuid_feature,
1243                .sys_reg = SYS_ID_AA64PFR0_EL1,
1244                .sign = FTR_UNSIGNED,
1245                .field_pos = ID_AA64PFR0_EL0_SHIFT,
1246                .min_field_value = ID_AA64PFR0_EL0_32BIT_64BIT,
1247        },
1248        {
1249                .desc = "Kernel page table isolation (KPTI)",
1250                .capability = ARM64_UNMAP_KERNEL_AT_EL0,
1251                .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
1252                /*
1253                 * The ID feature fields below are used to indicate that
1254                 * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for
1255                 * more details.
1256                 */
1257                .sys_reg = SYS_ID_AA64PFR0_EL1,
1258                .field_pos = ID_AA64PFR0_CSV3_SHIFT,
1259                .min_field_value = 1,
1260                .matches = unmap_kernel_at_el0,
1261                .cpu_enable = kpti_install_ng_mappings,
1262        },
1263        {
1264                /* FP/SIMD is not implemented */
1265                .capability = ARM64_HAS_NO_FPSIMD,
1266                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1267                .min_field_value = 0,
1268                .matches = has_no_fpsimd,
1269        },
1270#ifdef CONFIG_ARM64_PMEM
1271        {
1272                .desc = "Data cache clean to Point of Persistence",
1273                .capability = ARM64_HAS_DCPOP,
1274                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1275                .matches = has_cpuid_feature,
1276                .sys_reg = SYS_ID_AA64ISAR1_EL1,
1277                .field_pos = ID_AA64ISAR1_DPB_SHIFT,
1278                .min_field_value = 1,
1279        },
1280#endif
1281#ifdef CONFIG_ARM64_SVE
1282        {
1283                .desc = "Scalable Vector Extension",
1284                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1285                .capability = ARM64_SVE,
1286                .sys_reg = SYS_ID_AA64PFR0_EL1,
1287                .sign = FTR_UNSIGNED,
1288                .field_pos = ID_AA64PFR0_SVE_SHIFT,
1289                .min_field_value = ID_AA64PFR0_SVE,
1290                .matches = has_cpuid_feature,
1291                .cpu_enable = sve_kernel_enable,
1292        },
1293#endif /* CONFIG_ARM64_SVE */
1294#ifdef CONFIG_ARM64_RAS_EXTN
1295        {
1296                .desc = "RAS Extension Support",
1297                .capability = ARM64_HAS_RAS_EXTN,
1298                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1299                .matches = has_cpuid_feature,
1300                .sys_reg = SYS_ID_AA64PFR0_EL1,
1301                .sign = FTR_UNSIGNED,
1302                .field_pos = ID_AA64PFR0_RAS_SHIFT,
1303                .min_field_value = ID_AA64PFR0_RAS_V1,
1304                .cpu_enable = cpu_clear_disr,
1305        },
1306#endif /* CONFIG_ARM64_RAS_EXTN */
1307        {
1308                .desc = "Data cache clean to the PoU not required for I/D coherence",
1309                .capability = ARM64_HAS_CACHE_IDC,
1310                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1311                .matches = has_cache_idc,
1312                .cpu_enable = cpu_emulate_effective_ctr,
1313        },
1314        {
1315                .desc = "Instruction cache invalidation not required for I/D coherence",
1316                .capability = ARM64_HAS_CACHE_DIC,
1317                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1318                .matches = has_cache_dic,
1319        },
1320        {
1321                .desc = "Stage-2 Force Write-Back",
1322                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1323                .capability = ARM64_HAS_STAGE2_FWB,
1324                .sys_reg = SYS_ID_AA64MMFR2_EL1,
1325                .sign = FTR_UNSIGNED,
1326                .field_pos = ID_AA64MMFR2_FWB_SHIFT,
1327                .min_field_value = 1,
1328                .matches = has_cpuid_feature,
1329                .cpu_enable = cpu_has_fwb,
1330        },
1331#ifdef CONFIG_ARM64_HW_AFDBM
1332        {
1333                /*
1334                 * Since we turn this on always, we don't want the user to
1335                 * think that the feature is available when it may not be.
1336                 * So hide the description.
1337                 *
1338                 * .desc = "Hardware pagetable Dirty Bit Management",
1339                 *
1340                 */
1341                .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1342                .capability = ARM64_HW_DBM,
1343                .sys_reg = SYS_ID_AA64MMFR1_EL1,
1344                .sign = FTR_UNSIGNED,
1345                .field_pos = ID_AA64MMFR1_HADBS_SHIFT,
1346                .min_field_value = 2,
1347                .matches = has_hw_dbm,
1348                .cpu_enable = cpu_enable_hw_dbm,
1349        },
1350#endif
1351        {
1352                .desc = "Speculation barrier (SB)",
1353                .capability = ARM64_HAS_SB,
1354                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1355                .matches = has_cpuid_feature,
1356                .sys_reg = SYS_ID_AA64ISAR1_EL1,
1357                .field_pos = ID_AA64ISAR1_SB_SHIFT,
1358                .sign = FTR_UNSIGNED,
1359                .min_field_value = 1,
1360        },
1361#ifdef CONFIG_ARM64_CNP
1362        {
1363                .desc = "Common not Private translations",
1364                .capability = ARM64_HAS_CNP,
1365                .type = ARM64_CPUCAP_SYSTEM_FEATURE,
1366                .matches = has_useable_cnp,
1367                .sys_reg = SYS_ID_AA64MMFR2_EL1,
1368                .sign = FTR_UNSIGNED,
1369                .field_pos = ID_AA64MMFR2_CNP_SHIFT,
1370                .min_field_value = 1,
1371                .cpu_enable = cpu_enable_cnp,
1372        },
1373#endif
1374#ifdef CONFIG_ARM64_SSBD
1375        {
1376                .desc = "Speculative Store Bypassing Safe (SSBS)",
1377                .capability = ARM64_SSBS,
1378                .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
1379                .matches = has_cpuid_feature,
1380                .sys_reg = SYS_ID_AA64PFR1_EL1,
1381                .field_pos = ID_AA64PFR1_SSBS_SHIFT,
1382                .sign = FTR_UNSIGNED,
1383                .min_field_value = ID_AA64PFR1_SSBS_PSTATE_ONLY,
1384                .cpu_enable = cpu_enable_ssbs,
1385        },
1386#endif
1387        {},
1388};
1389
1390#define HWCAP_CAP(reg, field, s, min_value, cap_type, cap)      \
1391        {                                                       \
1392                .desc = #cap,                                   \
1393                .type = ARM64_CPUCAP_SYSTEM_FEATURE,            \
1394                .matches = has_cpuid_feature,                   \
1395                .sys_reg = reg,                                 \
1396                .field_pos = field,                             \
1397                .sign = s,                                      \
1398                .min_field_value = min_value,                   \
1399                .hwcap_type = cap_type,                         \
1400                .hwcap = cap,                                   \
1401        }
1402
1403static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
1404        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_PMULL),
1405        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_AES_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_AES),
1406        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA1),
1407        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA2),
1408        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA2_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_SHA512),
1409        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_CRC32),
1410        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_ATOMICS_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ATOMICS),
1411        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_RDM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDRDM),
1412        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SHA3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SHA3),
1413        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM3_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM3),
1414        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_SM4_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SM4),
1415        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_DP_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDDP),
1416        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_FHM_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_ASIMDFHM),
1417        HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_TS_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_FLAGM),
1418        HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_FP),
1419        HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_FP_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_FPHP),
1420        HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 0, CAP_HWCAP, HWCAP_ASIMD),
1421        HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_ASIMD_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_ASIMDHP),
1422        HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_DIT_SHIFT, FTR_SIGNED, 1, CAP_HWCAP, HWCAP_DIT),
1423        HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_DPB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_DCPOP),
1424        HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_JSCVT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_JSCVT),
1425        HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_FCMA_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_FCMA),
1426        HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_LRCPC),
1427        HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_LRCPC_SHIFT, FTR_UNSIGNED, 2, CAP_HWCAP, HWCAP_ILRCPC),
1428        HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_SB_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_SB),
1429        HWCAP_CAP(SYS_ID_AA64MMFR2_EL1, ID_AA64MMFR2_AT_SHIFT, FTR_UNSIGNED, 1, CAP_HWCAP, HWCAP_USCAT),
1430#ifdef CONFIG_ARM64_SVE
1431        HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_SVE_SHIFT, FTR_UNSIGNED, ID_AA64PFR0_SVE, CAP_HWCAP, HWCAP_SVE),
1432#endif
1433        HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_SSBS_SHIFT, FTR_UNSIGNED, ID_AA64PFR1_SSBS_PSTATE_INSNS, CAP_HWCAP, HWCAP_SSBS),
1434        {},
1435};
1436
1437static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
1438#ifdef CONFIG_COMPAT
1439        HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
1440        HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
1441        HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
1442        HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
1443        HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_CRC32_SHIFT, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32),
1444#endif
1445        {},
1446};
1447
1448static void __init cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
1449{
1450        switch (cap->hwcap_type) {
1451        case CAP_HWCAP:
1452                elf_hwcap |= cap->hwcap;
1453                break;
1454#ifdef CONFIG_COMPAT
1455        case CAP_COMPAT_HWCAP:
1456                compat_elf_hwcap |= (u32)cap->hwcap;
1457                break;
1458        case CAP_COMPAT_HWCAP2:
1459                compat_elf_hwcap2 |= (u32)cap->hwcap;
1460                break;
1461#endif
1462        default:
1463                WARN_ON(1);
1464                break;
1465        }
1466}
1467
1468/* Check if we have a particular HWCAP enabled */
1469static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
1470{
1471        bool rc;
1472
1473        switch (cap->hwcap_type) {
1474        case CAP_HWCAP:
1475                rc = (elf_hwcap & cap->hwcap) != 0;
1476                break;
1477#ifdef CONFIG_COMPAT
1478        case CAP_COMPAT_HWCAP:
1479                rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
1480                break;
1481        case CAP_COMPAT_HWCAP2:
1482                rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
1483                break;
1484#endif
1485        default:
1486                WARN_ON(1);
1487                rc = false;
1488        }
1489
1490        return rc;
1491}
1492
1493static void __init setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
1494{
1495        /* We support emulation of accesses to CPU ID feature registers */
1496        elf_hwcap |= HWCAP_CPUID;
1497        for (; hwcaps->matches; hwcaps++)
1498                if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
1499                        cap_set_elf_hwcap(hwcaps);
1500}
1501
1502/*
1503 * Check if the current CPU has a given feature capability.
1504 * Should be called from non-preemptible context.
1505 */
1506static bool __this_cpu_has_cap(const struct arm64_cpu_capabilities *cap_array,
1507                               unsigned int cap)
1508{
1509        const struct arm64_cpu_capabilities *caps;
1510
1511        if (WARN_ON(preemptible()))
1512                return false;
1513
1514        for (caps = cap_array; caps->matches; caps++)
1515                if (caps->capability == cap)
1516                        return caps->matches(caps, SCOPE_LOCAL_CPU);
1517
1518        return false;
1519}
1520
1521static void __update_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
1522                                      u16 scope_mask, const char *info)
1523{
1524        scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
1525        for (; caps->matches; caps++) {
1526                if (!(caps->type & scope_mask) ||
1527                    !caps->matches(caps, cpucap_default_scope(caps)))
1528                        continue;
1529
1530                if (!cpus_have_cap(caps->capability) && caps->desc)
1531                        pr_info("%s %s\n", info, caps->desc);
1532                cpus_set_cap(caps->capability);
1533        }
1534}
1535
1536static void update_cpu_capabilities(u16 scope_mask)
1537{
1538        __update_cpu_capabilities(arm64_errata, scope_mask,
1539                                  "enabling workaround for");
1540        __update_cpu_capabilities(arm64_features, scope_mask, "detected:");
1541}
1542
1543static int __enable_cpu_capability(void *arg)
1544{
1545        const struct arm64_cpu_capabilities *cap = arg;
1546
1547        cap->cpu_enable(cap);
1548        return 0;
1549}
1550
1551/*
1552 * Run through the enabled capabilities and enable() it on all active
1553 * CPUs
1554 */
1555static void __init
1556__enable_cpu_capabilities(const struct arm64_cpu_capabilities *caps,
1557                          u16 scope_mask)
1558{
1559        scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
1560        for (; caps->matches; caps++) {
1561                unsigned int num = caps->capability;
1562
1563                if (!(caps->type & scope_mask) || !cpus_have_cap(num))
1564                        continue;
1565
1566                /* Ensure cpus_have_const_cap(num) works */
1567                static_branch_enable(&cpu_hwcap_keys[num]);
1568
1569                if (caps->cpu_enable) {
1570                        /*
1571                         * Capabilities with SCOPE_BOOT_CPU scope are finalised
1572                         * before any secondary CPU boots. Thus, each secondary
1573                         * will enable the capability as appropriate via
1574                         * check_local_cpu_capabilities(). The only exception is
1575                         * the boot CPU, for which the capability must be
1576                         * enabled here. This approach avoids costly
1577                         * stop_machine() calls for this case.
1578                         *
1579                         * Otherwise, use stop_machine() as it schedules the
1580                         * work allowing us to modify PSTATE, instead of
1581                         * on_each_cpu() which uses an IPI, giving us a PSTATE
1582                         * that disappears when we return.
1583                         */
1584                        if (scope_mask & SCOPE_BOOT_CPU)
1585                                caps->cpu_enable(caps);
1586                        else
1587                                stop_machine(__enable_cpu_capability,
1588                                             (void *)caps, cpu_online_mask);
1589                }
1590        }
1591}
1592
1593static void __init enable_cpu_capabilities(u16 scope_mask)
1594{
1595        __enable_cpu_capabilities(arm64_errata, scope_mask);
1596        __enable_cpu_capabilities(arm64_features, scope_mask);
1597}
1598
1599/*
1600 * Run through the list of capabilities to check for conflicts.
1601 * If the system has already detected a capability, take necessary
1602 * action on this CPU.
1603 *
1604 * Returns "false" on conflicts.
1605 */
1606static bool
1607__verify_local_cpu_caps(const struct arm64_cpu_capabilities *caps,
1608                        u16 scope_mask)
1609{
1610        bool cpu_has_cap, system_has_cap;
1611
1612        scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
1613
1614        for (; caps->matches; caps++) {
1615                if (!(caps->type & scope_mask))
1616                        continue;
1617
1618                cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU);
1619                system_has_cap = cpus_have_cap(caps->capability);
1620
1621                if (system_has_cap) {
1622                        /*
1623                         * Check if the new CPU misses an advertised feature,
1624                         * which is not safe to miss.
1625                         */
1626                        if (!cpu_has_cap && !cpucap_late_cpu_optional(caps))
1627                                break;
1628                        /*
1629                         * We have to issue cpu_enable() irrespective of
1630                         * whether the CPU has it or not, as it is enabeld
1631                         * system wide. It is upto the call back to take
1632                         * appropriate action on this CPU.
1633                         */
1634                        if (caps->cpu_enable)
1635                                caps->cpu_enable(caps);
1636                } else {
1637                        /*
1638                         * Check if the CPU has this capability if it isn't
1639                         * safe to have when the system doesn't.
1640                         */
1641                        if (cpu_has_cap && !cpucap_late_cpu_permitted(caps))
1642                                break;
1643                }
1644        }
1645
1646        if (caps->matches) {
1647                pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n",
1648                        smp_processor_id(), caps->capability,
1649                        caps->desc, system_has_cap, cpu_has_cap);
1650                return false;
1651        }
1652
1653        return true;
1654}
1655
1656static bool verify_local_cpu_caps(u16 scope_mask)
1657{
1658        return __verify_local_cpu_caps(arm64_errata, scope_mask) &&
1659               __verify_local_cpu_caps(arm64_features, scope_mask);
1660}
1661
1662/*
1663 * Check for CPU features that are used in early boot
1664 * based on the Boot CPU value.
1665 */
1666static void check_early_cpu_features(void)
1667{
1668        verify_cpu_asid_bits();
1669        /*
1670         * Early features are used by the kernel already. If there
1671         * is a conflict, we cannot proceed further.
1672         */
1673        if (!verify_local_cpu_caps(SCOPE_BOOT_CPU))
1674                cpu_panic_kernel();
1675}
1676
1677static void
1678verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
1679{
1680
1681        for (; caps->matches; caps++)
1682                if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
1683                        pr_crit("CPU%d: missing HWCAP: %s\n",
1684                                        smp_processor_id(), caps->desc);
1685                        cpu_die_early();
1686                }
1687}
1688
1689static void verify_sve_features(void)
1690{
1691        u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
1692        u64 zcr = read_zcr_features();
1693
1694        unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
1695        unsigned int len = zcr & ZCR_ELx_LEN_MASK;
1696
1697        if (len < safe_len || sve_verify_vq_map()) {
1698                pr_crit("CPU%d: SVE: vector length support mismatch\n",
1699                        smp_processor_id());
1700                cpu_die_early();
1701        }
1702
1703        /* Add checks on other ZCR bits here if necessary */
1704}
1705
1706
1707/*
1708 * Run through the enabled system capabilities and enable() it on this CPU.
1709 * The capabilities were decided based on the available CPUs at the boot time.
1710 * Any new CPU should match the system wide status of the capability. If the
1711 * new CPU doesn't have a capability which the system now has enabled, we
1712 * cannot do anything to fix it up and could cause unexpected failures. So
1713 * we park the CPU.
1714 */
1715static void verify_local_cpu_capabilities(void)
1716{
1717        /*
1718         * The capabilities with SCOPE_BOOT_CPU are checked from
1719         * check_early_cpu_features(), as they need to be verified
1720         * on all secondary CPUs.
1721         */
1722        if (!verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU))
1723                cpu_die_early();
1724
1725        verify_local_elf_hwcaps(arm64_elf_hwcaps);
1726
1727        if (system_supports_32bit_el0())
1728                verify_local_elf_hwcaps(compat_elf_hwcaps);
1729
1730        if (system_supports_sve())
1731                verify_sve_features();
1732}
1733
1734void check_local_cpu_capabilities(void)
1735{
1736        /*
1737         * All secondary CPUs should conform to the early CPU features
1738         * in use by the kernel based on boot CPU.
1739         */
1740        check_early_cpu_features();
1741
1742        /*
1743         * If we haven't finalised the system capabilities, this CPU gets
1744         * a chance to update the errata work arounds and local features.
1745         * Otherwise, this CPU should verify that it has all the system
1746         * advertised capabilities.
1747         */
1748        if (!sys_caps_initialised)
1749                update_cpu_capabilities(SCOPE_LOCAL_CPU);
1750        else
1751                verify_local_cpu_capabilities();
1752}
1753
1754static void __init setup_boot_cpu_capabilities(void)
1755{
1756        /* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */
1757        update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU);
1758        /* Enable the SCOPE_BOOT_CPU capabilities alone right away */
1759        enable_cpu_capabilities(SCOPE_BOOT_CPU);
1760}
1761
1762DEFINE_STATIC_KEY_FALSE(arm64_const_caps_ready);
1763EXPORT_SYMBOL(arm64_const_caps_ready);
1764
1765static void __init mark_const_caps_ready(void)
1766{
1767        static_branch_enable(&arm64_const_caps_ready);
1768}
1769
1770extern const struct arm64_cpu_capabilities arm64_errata[];
1771
1772bool this_cpu_has_cap(unsigned int cap)
1773{
1774        return (__this_cpu_has_cap(arm64_features, cap) ||
1775                __this_cpu_has_cap(arm64_errata, cap));
1776}
1777
1778static void __init setup_system_capabilities(void)
1779{
1780        /*
1781         * We have finalised the system-wide safe feature
1782         * registers, finalise the capabilities that depend
1783         * on it. Also enable all the available capabilities,
1784         * that are not enabled already.
1785         */
1786        update_cpu_capabilities(SCOPE_SYSTEM);
1787        enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
1788}
1789
1790void __init setup_cpu_features(void)
1791{
1792        u32 cwg;
1793
1794        setup_system_capabilities();
1795        mark_const_caps_ready();
1796        setup_elf_hwcaps(arm64_elf_hwcaps);
1797
1798        if (system_supports_32bit_el0())
1799                setup_elf_hwcaps(compat_elf_hwcaps);
1800
1801        if (system_uses_ttbr0_pan())
1802                pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
1803
1804        sve_setup();
1805        minsigstksz_setup();
1806
1807        /* Advertise that we have computed the system capabilities */
1808        set_sys_caps_initialised();
1809
1810        /*
1811         * Check for sane CTR_EL0.CWG value.
1812         */
1813        cwg = cache_type_cwg();
1814        if (!cwg)
1815                pr_warn("No Cache Writeback Granule information, assuming %d\n",
1816                        ARCH_DMA_MINALIGN);
1817}
1818
1819static bool __maybe_unused
1820cpufeature_pan_not_uao(const struct arm64_cpu_capabilities *entry, int __unused)
1821{
1822        return (cpus_have_const_cap(ARM64_HAS_PAN) && !cpus_have_const_cap(ARM64_HAS_UAO));
1823}
1824
1825static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap)
1826{
1827        cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
1828}
1829
1830/*
1831 * We emulate only the following system register space.
1832 * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 4 - 7]
1833 * See Table C5-6 System instruction encodings for System register accesses,
1834 * ARMv8 ARM(ARM DDI 0487A.f) for more details.
1835 */
1836static inline bool __attribute_const__ is_emulated(u32 id)
1837{
1838        return (sys_reg_Op0(id) == 0x3 &&
1839                sys_reg_CRn(id) == 0x0 &&
1840                sys_reg_Op1(id) == 0x0 &&
1841                (sys_reg_CRm(id) == 0 ||
1842                 ((sys_reg_CRm(id) >= 4) && (sys_reg_CRm(id) <= 7))));
1843}
1844
1845/*
1846 * With CRm == 0, reg should be one of :
1847 * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
1848 */
1849static inline int emulate_id_reg(u32 id, u64 *valp)
1850{
1851        switch (id) {
1852        case SYS_MIDR_EL1:
1853                *valp = read_cpuid_id();
1854                break;
1855        case SYS_MPIDR_EL1:
1856                *valp = SYS_MPIDR_SAFE_VAL;
1857                break;
1858        case SYS_REVIDR_EL1:
1859                /* IMPLEMENTATION DEFINED values are emulated with 0 */
1860                *valp = 0;
1861                break;
1862        default:
1863                return -EINVAL;
1864        }
1865
1866        return 0;
1867}
1868
1869static int emulate_sys_reg(u32 id, u64 *valp)
1870{
1871        struct arm64_ftr_reg *regp;
1872
1873        if (!is_emulated(id))
1874                return -EINVAL;
1875
1876        if (sys_reg_CRm(id) == 0)
1877                return emulate_id_reg(id, valp);
1878
1879        regp = get_arm64_ftr_reg(id);
1880        if (regp)
1881                *valp = arm64_ftr_reg_user_value(regp);
1882        else
1883                /*
1884                 * The untracked registers are either IMPLEMENTATION DEFINED
1885                 * (e.g, ID_AFR0_EL1) or reserved RAZ.
1886                 */
1887                *valp = 0;
1888        return 0;
1889}
1890
1891static int emulate_mrs(struct pt_regs *regs, u32 insn)
1892{
1893        int rc;
1894        u32 sys_reg, dst;
1895        u64 val;
1896
1897        /*
1898         * sys_reg values are defined as used in mrs/msr instruction.
1899         * shift the imm value to get the encoding.
1900         */
1901        sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
1902        rc = emulate_sys_reg(sys_reg, &val);
1903        if (!rc) {
1904                dst = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
1905                pt_regs_write_reg(regs, dst, val);
1906                arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
1907        }
1908
1909        return rc;
1910}
1911
1912static struct undef_hook mrs_hook = {
1913        .instr_mask = 0xfff00000,
1914        .instr_val  = 0xd5300000,
1915        .pstate_mask = COMPAT_PSR_MODE_MASK,
1916        .pstate_val = PSR_MODE_EL0t,
1917        .fn = emulate_mrs,
1918};
1919
1920static int __init enable_mrs_emulation(void)
1921{
1922        register_undef_hook(&mrs_hook);
1923        return 0;
1924}
1925
1926core_initcall(enable_mrs_emulation);
1927
1928void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
1929{
1930        /* Firmware may have left a deferred SError in this register. */
1931        write_sysreg_s(0, SYS_DISR_EL1);
1932}
1933
1934ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr,
1935                          char *buf)
1936{
1937        if (__meltdown_safe)
1938                return sprintf(buf, "Not affected\n");
1939
1940        if (arm64_kernel_unmapped_at_el0())
1941                return sprintf(buf, "Mitigation: PTI\n");
1942
1943        return sprintf(buf, "Vulnerable\n");
1944}
1945