Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
0004  */
0005 
0006 #ifndef __ASM_CPUFEATURE_H
0007 #define __ASM_CPUFEATURE_H
0008 
0009 #include <asm/cpucaps.h>
0010 #include <asm/cputype.h>
0011 #include <asm/hwcap.h>
0012 #include <asm/sysreg.h>
0013 
0014 #define MAX_CPU_FEATURES    128
0015 #define cpu_feature(x)      KERNEL_HWCAP_ ## x
0016 
0017 #ifndef __ASSEMBLY__
0018 
0019 #include <linux/bug.h>
0020 #include <linux/jump_label.h>
0021 #include <linux/kernel.h>
0022 
0023 /*
0024  * CPU feature register tracking
0025  *
0026  * The safe value of a CPUID feature field is dependent on the implications
0027  * of the values assigned to it by the architecture. Based on the relationship
0028  * between the values, the features are classified into 3 types - LOWER_SAFE,
0029  * HIGHER_SAFE and EXACT.
0030  *
0031  * The lowest value of all the CPUs is chosen for LOWER_SAFE and highest
0032  * for HIGHER_SAFE. It is expected that all CPUs have the same value for
0033  * a field when EXACT is specified, failing which, the safe value specified
0034  * in the table is chosen.
0035  */
0036 
0037 enum ftr_type {
0038     FTR_EXACT,          /* Use a predefined safe value */
0039     FTR_LOWER_SAFE,         /* Smaller value is safe */
0040     FTR_HIGHER_SAFE,        /* Bigger value is safe */
0041     FTR_HIGHER_OR_ZERO_SAFE,    /* Bigger value is safe, but 0 is biggest */
0042 };
0043 
0044 #define FTR_STRICT  true    /* SANITY check strict matching required */
0045 #define FTR_NONSTRICT   false   /* SANITY check ignored */
0046 
0047 #define FTR_SIGNED  true    /* Value should be treated as signed */
0048 #define FTR_UNSIGNED    false   /* Value should be treated as unsigned */
0049 
0050 #define FTR_VISIBLE true    /* Feature visible to the user space */
0051 #define FTR_HIDDEN  false   /* Feature is hidden from the user */
0052 
0053 #define FTR_VISIBLE_IF_IS_ENABLED(config)       \
0054     (IS_ENABLED(config) ? FTR_VISIBLE : FTR_HIDDEN)
0055 
0056 struct arm64_ftr_bits {
0057     bool        sign;   /* Value is signed ? */
0058     bool        visible;
0059     bool        strict; /* CPU Sanity check: strict matching required ? */
0060     enum ftr_type   type;
0061     u8      shift;
0062     u8      width;
0063     s64     safe_val; /* safe value for FTR_EXACT features */
0064 };
0065 
0066 /*
0067  * Describe the early feature override to the core override code:
0068  *
0069  * @val         Values that are to be merged into the final
0070  *          sanitised value of the register. Only the bitfields
0071  *          set to 1 in @mask are valid
0072  * @mask        Mask of the features that are overridden by @val
0073  *
0074  * A @mask field set to full-1 indicates that the corresponding field
0075  * in @val is a valid override.
0076  *
0077  * A @mask field set to full-0 with the corresponding @val field set
0078  * to full-0 denotes that this field has no override
0079  *
0080  * A @mask field set to full-0 with the corresponding @val field set
0081  * to full-1 denotes thath this field has an invalid override.
0082  */
0083 struct arm64_ftr_override {
0084     u64     val;
0085     u64     mask;
0086 };
0087 
0088 /*
0089  * @arm64_ftr_reg - Feature register
0090  * @strict_mask     Bits which should match across all CPUs for sanity.
0091  * @sys_val     Safe value across the CPUs (system view)
0092  */
0093 struct arm64_ftr_reg {
0094     const char          *name;
0095     u64             strict_mask;
0096     u64             user_mask;
0097     u64             sys_val;
0098     u64             user_val;
0099     struct arm64_ftr_override   *override;
0100     const struct arm64_ftr_bits *ftr_bits;
0101 };
0102 
0103 extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;
0104 
0105 /*
0106  * CPU capabilities:
0107  *
0108  * We use arm64_cpu_capabilities to represent system features, errata work
0109  * arounds (both used internally by kernel and tracked in cpu_hwcaps) and
0110  * ELF HWCAPs (which are exposed to user).
0111  *
0112  * To support systems with heterogeneous CPUs, we need to make sure that we
0113  * detect the capabilities correctly on the system and take appropriate
0114  * measures to ensure there are no incompatibilities.
0115  *
0116  * This comment tries to explain how we treat the capabilities.
0117  * Each capability has the following list of attributes :
0118  *
0119  * 1) Scope of Detection : The system detects a given capability by
0120  *    performing some checks at runtime. This could be, e.g, checking the
0121  *    value of a field in CPU ID feature register or checking the cpu
0122  *    model. The capability provides a call back ( @matches() ) to
0123  *    perform the check. Scope defines how the checks should be performed.
0124  *    There are three cases:
0125  *
0126  *     a) SCOPE_LOCAL_CPU: check all the CPUs and "detect" if at least one
0127  *        matches. This implies, we have to run the check on all the
0128  *        booting CPUs, until the system decides that state of the
0129  *        capability is finalised. (See section 2 below)
0130  *      Or
0131  *     b) SCOPE_SYSTEM: check all the CPUs and "detect" if all the CPUs
0132  *        matches. This implies, we run the check only once, when the
0133  *        system decides to finalise the state of the capability. If the
0134  *        capability relies on a field in one of the CPU ID feature
0135  *        registers, we use the sanitised value of the register from the
0136  *        CPU feature infrastructure to make the decision.
0137  *      Or
0138  *     c) SCOPE_BOOT_CPU: Check only on the primary boot CPU to detect the
0139  *        feature. This category is for features that are "finalised"
0140  *        (or used) by the kernel very early even before the SMP cpus
0141  *        are brought up.
0142  *
0143  *    The process of detection is usually denoted by "update" capability
0144  *    state in the code.
0145  *
0146  * 2) Finalise the state : The kernel should finalise the state of a
0147  *    capability at some point during its execution and take necessary
0148  *    actions if any. Usually, this is done, after all the boot-time
0149  *    enabled CPUs are brought up by the kernel, so that it can make
0150  *    better decision based on the available set of CPUs. However, there
0151  *    are some special cases, where the action is taken during the early
0152  *    boot by the primary boot CPU. (e.g, running the kernel at EL2 with
0153  *    Virtualisation Host Extensions). The kernel usually disallows any
0154  *    changes to the state of a capability once it finalises the capability
0155  *    and takes any action, as it may be impossible to execute the actions
0156  *    safely. A CPU brought up after a capability is "finalised" is
0157  *    referred to as "Late CPU" w.r.t the capability. e.g, all secondary
0158  *    CPUs are treated "late CPUs" for capabilities determined by the boot
0159  *    CPU.
0160  *
0161  *    At the moment there are two passes of finalising the capabilities.
0162  *      a) Boot CPU scope capabilities - Finalised by primary boot CPU via
0163  *         setup_boot_cpu_capabilities().
0164  *      b) Everything except (a) - Run via setup_system_capabilities().
0165  *
0166  * 3) Verification: When a CPU is brought online (e.g, by user or by the
0167  *    kernel), the kernel should make sure that it is safe to use the CPU,
0168  *    by verifying that the CPU is compliant with the state of the
0169  *    capabilities finalised already. This happens via :
0170  *
0171  *  secondary_start_kernel()-> check_local_cpu_capabilities()
0172  *
0173  *    As explained in (2) above, capabilities could be finalised at
0174  *    different points in the execution. Each newly booted CPU is verified
0175  *    against the capabilities that have been finalised by the time it
0176  *    boots.
0177  *
0178  *  a) SCOPE_BOOT_CPU : All CPUs are verified against the capability
0179  *  except for the primary boot CPU.
0180  *
0181  *  b) SCOPE_LOCAL_CPU, SCOPE_SYSTEM: All CPUs hotplugged on by the
0182  *  user after the kernel boot are verified against the capability.
0183  *
0184  *    If there is a conflict, the kernel takes an action, based on the
0185  *    severity (e.g, a CPU could be prevented from booting or cause a
0186  *    kernel panic). The CPU is allowed to "affect" the state of the
0187  *    capability, if it has not been finalised already. See section 5
0188  *    for more details on conflicts.
0189  *
0190  * 4) Action: As mentioned in (2), the kernel can take an action for each
0191  *    detected capability, on all CPUs on the system. Appropriate actions
0192  *    include, turning on an architectural feature, modifying the control
0193  *    registers (e.g, SCTLR, TCR etc.) or patching the kernel via
0194  *    alternatives. The kernel patching is batched and performed at later
0195  *    point. The actions are always initiated only after the capability
0196  *    is finalised. This is usally denoted by "enabling" the capability.
0197  *    The actions are initiated as follows :
0198  *  a) Action is triggered on all online CPUs, after the capability is
0199  *  finalised, invoked within the stop_machine() context from
0200  *  enable_cpu_capabilitie().
0201  *
0202  *  b) Any late CPU, brought up after (1), the action is triggered via:
0203  *
0204  *    check_local_cpu_capabilities() -> verify_local_cpu_capabilities()
0205  *
0206  * 5) Conflicts: Based on the state of the capability on a late CPU vs.
0207  *    the system state, we could have the following combinations :
0208  *
0209  *      x-----------------------------x
0210  *      | Type  | System   | Late CPU |
0211  *      |-----------------------------|
0212  *      |  a    |   y      |    n     |
0213  *      |-----------------------------|
0214  *      |  b    |   n      |    y     |
0215  *      x-----------------------------x
0216  *
0217  *     Two separate flag bits are defined to indicate whether each kind of
0218  *     conflict can be allowed:
0219  *      ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU - Case(a) is allowed
0220  *      ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU - Case(b) is allowed
0221  *
0222  *     Case (a) is not permitted for a capability that the system requires
0223  *     all CPUs to have in order for the capability to be enabled. This is
0224  *     typical for capabilities that represent enhanced functionality.
0225  *
0226  *     Case (b) is not permitted for a capability that must be enabled
0227  *     during boot if any CPU in the system requires it in order to run
0228  *     safely. This is typical for erratum work arounds that cannot be
0229  *     enabled after the corresponding capability is finalised.
0230  *
0231  *     In some non-typical cases either both (a) and (b), or neither,
0232  *     should be permitted. This can be described by including neither
0233  *     or both flags in the capability's type field.
0234  *
0235  *     In case of a conflict, the CPU is prevented from booting. If the
0236  *     ARM64_CPUCAP_PANIC_ON_CONFLICT flag is specified for the capability,
0237  *     then a kernel panic is triggered.
0238  */
0239 
0240 
0241 /*
0242  * Decide how the capability is detected.
0243  * On any local CPU vs System wide vs the primary boot CPU
0244  */
0245 #define ARM64_CPUCAP_SCOPE_LOCAL_CPU        ((u16)BIT(0))
0246 #define ARM64_CPUCAP_SCOPE_SYSTEM       ((u16)BIT(1))
0247 /*
0248  * The capabilitiy is detected on the Boot CPU and is used by kernel
0249  * during early boot. i.e, the capability should be "detected" and
0250  * "enabled" as early as possibly on all booting CPUs.
0251  */
0252 #define ARM64_CPUCAP_SCOPE_BOOT_CPU     ((u16)BIT(2))
0253 #define ARM64_CPUCAP_SCOPE_MASK         \
0254     (ARM64_CPUCAP_SCOPE_SYSTEM  |   \
0255      ARM64_CPUCAP_SCOPE_LOCAL_CPU   |   \
0256      ARM64_CPUCAP_SCOPE_BOOT_CPU)
0257 
0258 #define SCOPE_SYSTEM                ARM64_CPUCAP_SCOPE_SYSTEM
0259 #define SCOPE_LOCAL_CPU             ARM64_CPUCAP_SCOPE_LOCAL_CPU
0260 #define SCOPE_BOOT_CPU              ARM64_CPUCAP_SCOPE_BOOT_CPU
0261 #define SCOPE_ALL               ARM64_CPUCAP_SCOPE_MASK
0262 
0263 /*
0264  * Is it permitted for a late CPU to have this capability when system
0265  * hasn't already enabled it ?
0266  */
0267 #define ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU ((u16)BIT(4))
0268 /* Is it safe for a late CPU to miss this capability when system has it */
0269 #define ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU  ((u16)BIT(5))
0270 /* Panic when a conflict is detected */
0271 #define ARM64_CPUCAP_PANIC_ON_CONFLICT      ((u16)BIT(6))
0272 
0273 /*
0274  * CPU errata workarounds that need to be enabled at boot time if one or
0275  * more CPUs in the system requires it. When one of these capabilities
0276  * has been enabled, it is safe to allow any CPU to boot that doesn't
0277  * require the workaround. However, it is not safe if a "late" CPU
0278  * requires a workaround and the system hasn't enabled it already.
0279  */
0280 #define ARM64_CPUCAP_LOCAL_CPU_ERRATUM      \
0281     (ARM64_CPUCAP_SCOPE_LOCAL_CPU | ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
0282 /*
0283  * CPU feature detected at boot time based on system-wide value of a
0284  * feature. It is safe for a late CPU to have this feature even though
0285  * the system hasn't enabled it, although the feature will not be used
0286  * by Linux in this case. If the system has enabled this feature already,
0287  * then every late CPU must have it.
0288  */
0289 #define ARM64_CPUCAP_SYSTEM_FEATURE \
0290     (ARM64_CPUCAP_SCOPE_SYSTEM | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
0291 /*
0292  * CPU feature detected at boot time based on feature of one or more CPUs.
0293  * All possible conflicts for a late CPU are ignored.
0294  * NOTE: this means that a late CPU with the feature will *not* cause the
0295  * capability to be advertised by cpus_have_*cap()!
0296  */
0297 #define ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE     \
0298     (ARM64_CPUCAP_SCOPE_LOCAL_CPU       |   \
0299      ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU |   \
0300      ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
0301 
0302 /*
0303  * CPU feature detected at boot time, on one or more CPUs. A late CPU
0304  * is not allowed to have the capability when the system doesn't have it.
0305  * It is Ok for a late CPU to miss the feature.
0306  */
0307 #define ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE  \
0308     (ARM64_CPUCAP_SCOPE_LOCAL_CPU       |   \
0309      ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)
0310 
0311 /*
0312  * CPU feature used early in the boot based on the boot CPU. All secondary
0313  * CPUs must match the state of the capability as detected by the boot CPU. In
0314  * case of a conflict, a kernel panic is triggered.
0315  */
0316 #define ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE        \
0317     (ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PANIC_ON_CONFLICT)
0318 
0319 /*
0320  * CPU feature used early in the boot based on the boot CPU. It is safe for a
0321  * late CPU to have this feature even though the boot CPU hasn't enabled it,
0322  * although the feature will not be used by Linux in this case. If the boot CPU
0323  * has enabled this feature already, then every late CPU must have it.
0324  */
0325 #define ARM64_CPUCAP_BOOT_CPU_FEATURE                  \
0326     (ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)
0327 
0328 struct arm64_cpu_capabilities {
0329     const char *desc;
0330     u16 capability;
0331     u16 type;
0332     bool (*matches)(const struct arm64_cpu_capabilities *caps, int scope);
0333     /*
0334      * Take the appropriate actions to configure this capability
0335      * for this CPU. If the capability is detected by the kernel
0336      * this will be called on all the CPUs in the system,
0337      * including the hotplugged CPUs, regardless of whether the
0338      * capability is available on that specific CPU. This is
0339      * useful for some capabilities (e.g, working around CPU
0340      * errata), where all the CPUs must take some action (e.g,
0341      * changing system control/configuration). Thus, if an action
0342      * is required only if the CPU has the capability, then the
0343      * routine must check it before taking any action.
0344      */
0345     void (*cpu_enable)(const struct arm64_cpu_capabilities *cap);
0346     union {
0347         struct {    /* To be used for erratum handling only */
0348             struct midr_range midr_range;
0349             const struct arm64_midr_revidr {
0350                 u32 midr_rv;        /* revision/variant */
0351                 u32 revidr_mask;
0352             } * const fixed_revs;
0353         };
0354 
0355         const struct midr_range *midr_range_list;
0356         struct {    /* Feature register checking */
0357             u32 sys_reg;
0358             u8 field_pos;
0359             u8 field_width;
0360             u8 min_field_value;
0361             u8 hwcap_type;
0362             bool sign;
0363             unsigned long hwcap;
0364         };
0365     };
0366 
0367     /*
0368      * An optional list of "matches/cpu_enable" pair for the same
0369      * "capability" of the same "type" as described by the parent.
0370      * Only matches(), cpu_enable() and fields relevant to these
0371      * methods are significant in the list. The cpu_enable is
0372      * invoked only if the corresponding entry "matches()".
0373      * However, if a cpu_enable() method is associated
0374      * with multiple matches(), care should be taken that either
0375      * the match criteria are mutually exclusive, or that the
0376      * method is robust against being called multiple times.
0377      */
0378     const struct arm64_cpu_capabilities *match_list;
0379 };
0380 
0381 static inline int cpucap_default_scope(const struct arm64_cpu_capabilities *cap)
0382 {
0383     return cap->type & ARM64_CPUCAP_SCOPE_MASK;
0384 }
0385 
0386 /*
0387  * Generic helper for handling capabilities with multiple (match,enable) pairs
0388  * of call backs, sharing the same capability bit.
0389  * Iterate over each entry to see if at least one matches.
0390  */
0391 static inline bool
0392 cpucap_multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry,
0393                    int scope)
0394 {
0395     const struct arm64_cpu_capabilities *caps;
0396 
0397     for (caps = entry->match_list; caps->matches; caps++)
0398         if (caps->matches(caps, scope))
0399             return true;
0400 
0401     return false;
0402 }
0403 
0404 static __always_inline bool is_vhe_hyp_code(void)
0405 {
0406     /* Only defined for code run in VHE hyp context */
0407     return __is_defined(__KVM_VHE_HYPERVISOR__);
0408 }
0409 
0410 static __always_inline bool is_nvhe_hyp_code(void)
0411 {
0412     /* Only defined for code run in NVHE hyp context */
0413     return __is_defined(__KVM_NVHE_HYPERVISOR__);
0414 }
0415 
0416 static __always_inline bool is_hyp_code(void)
0417 {
0418     return is_vhe_hyp_code() || is_nvhe_hyp_code();
0419 }
0420 
0421 extern DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
0422 extern struct static_key_false cpu_hwcap_keys[ARM64_NCAPS];
0423 extern struct static_key_false arm64_const_caps_ready;
0424 
0425 /* ARM64 CAPS + alternative_cb */
0426 #define ARM64_NPATCHABLE (ARM64_NCAPS + 1)
0427 extern DECLARE_BITMAP(boot_capabilities, ARM64_NPATCHABLE);
0428 
0429 #define for_each_available_cap(cap)     \
0430     for_each_set_bit(cap, cpu_hwcaps, ARM64_NCAPS)
0431 
0432 bool this_cpu_has_cap(unsigned int cap);
0433 void cpu_set_feature(unsigned int num);
0434 bool cpu_have_feature(unsigned int num);
0435 unsigned long cpu_get_elf_hwcap(void);
0436 unsigned long cpu_get_elf_hwcap2(void);
0437 
0438 #define cpu_set_named_feature(name) cpu_set_feature(cpu_feature(name))
0439 #define cpu_have_named_feature(name) cpu_have_feature(cpu_feature(name))
0440 
0441 static __always_inline bool system_capabilities_finalized(void)
0442 {
0443     return static_branch_likely(&arm64_const_caps_ready);
0444 }
0445 
0446 /*
0447  * Test for a capability with a runtime check.
0448  *
0449  * Before the capability is detected, this returns false.
0450  */
0451 static inline bool cpus_have_cap(unsigned int num)
0452 {
0453     if (num >= ARM64_NCAPS)
0454         return false;
0455     return test_bit(num, cpu_hwcaps);
0456 }
0457 
0458 /*
0459  * Test for a capability without a runtime check.
0460  *
0461  * Before capabilities are finalized, this returns false.
0462  * After capabilities are finalized, this is patched to avoid a runtime check.
0463  *
0464  * @num must be a compile-time constant.
0465  */
0466 static __always_inline bool __cpus_have_const_cap(int num)
0467 {
0468     if (num >= ARM64_NCAPS)
0469         return false;
0470     return static_branch_unlikely(&cpu_hwcap_keys[num]);
0471 }
0472 
0473 /*
0474  * Test for a capability without a runtime check.
0475  *
0476  * Before capabilities are finalized, this will BUG().
0477  * After capabilities are finalized, this is patched to avoid a runtime check.
0478  *
0479  * @num must be a compile-time constant.
0480  */
0481 static __always_inline bool cpus_have_final_cap(int num)
0482 {
0483     if (system_capabilities_finalized())
0484         return __cpus_have_const_cap(num);
0485     else
0486         BUG();
0487 }
0488 
0489 /*
0490  * Test for a capability, possibly with a runtime check for non-hyp code.
0491  *
0492  * For hyp code, this behaves the same as cpus_have_final_cap().
0493  *
0494  * For non-hyp code:
0495  * Before capabilities are finalized, this behaves as cpus_have_cap().
0496  * After capabilities are finalized, this is patched to avoid a runtime check.
0497  *
0498  * @num must be a compile-time constant.
0499  */
0500 static __always_inline bool cpus_have_const_cap(int num)
0501 {
0502     if (is_hyp_code())
0503         return cpus_have_final_cap(num);
0504     else if (system_capabilities_finalized())
0505         return __cpus_have_const_cap(num);
0506     else
0507         return cpus_have_cap(num);
0508 }
0509 
0510 static inline void cpus_set_cap(unsigned int num)
0511 {
0512     if (num >= ARM64_NCAPS) {
0513         pr_warn("Attempt to set an illegal CPU capability (%d >= %d)\n",
0514             num, ARM64_NCAPS);
0515     } else {
0516         __set_bit(num, cpu_hwcaps);
0517     }
0518 }
0519 
0520 static inline int __attribute_const__
0521 cpuid_feature_extract_signed_field_width(u64 features, int field, int width)
0522 {
0523     return (s64)(features << (64 - width - field)) >> (64 - width);
0524 }
0525 
0526 static inline int __attribute_const__
0527 cpuid_feature_extract_signed_field(u64 features, int field)
0528 {
0529     return cpuid_feature_extract_signed_field_width(features, field, 4);
0530 }
0531 
0532 static __always_inline unsigned int __attribute_const__
0533 cpuid_feature_extract_unsigned_field_width(u64 features, int field, int width)
0534 {
0535     return (u64)(features << (64 - width - field)) >> (64 - width);
0536 }
0537 
0538 static __always_inline unsigned int __attribute_const__
0539 cpuid_feature_extract_unsigned_field(u64 features, int field)
0540 {
0541     return cpuid_feature_extract_unsigned_field_width(features, field, 4);
0542 }
0543 
0544 /*
0545  * Fields that identify the version of the Performance Monitors Extension do
0546  * not follow the standard ID scheme. See ARM DDI 0487E.a page D13-2825,
0547  * "Alternative ID scheme used for the Performance Monitors Extension version".
0548  */
0549 static inline u64 __attribute_const__
0550 cpuid_feature_cap_perfmon_field(u64 features, int field, u64 cap)
0551 {
0552     u64 val = cpuid_feature_extract_unsigned_field(features, field);
0553     u64 mask = GENMASK_ULL(field + 3, field);
0554 
0555     /* Treat IMPLEMENTATION DEFINED functionality as unimplemented */
0556     if (val == ID_AA64DFR0_PMUVER_IMP_DEF)
0557         val = 0;
0558 
0559     if (val > cap) {
0560         features &= ~mask;
0561         features |= (cap << field) & mask;
0562     }
0563 
0564     return features;
0565 }
0566 
0567 static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp)
0568 {
0569     return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);
0570 }
0571 
0572 static inline u64 arm64_ftr_reg_user_value(const struct arm64_ftr_reg *reg)
0573 {
0574     return (reg->user_val | (reg->sys_val & reg->user_mask));
0575 }
0576 
0577 static inline int __attribute_const__
0578 cpuid_feature_extract_field_width(u64 features, int field, int width, bool sign)
0579 {
0580     if (WARN_ON_ONCE(!width))
0581         width = 4;
0582     return (sign) ?
0583         cpuid_feature_extract_signed_field_width(features, field, width) :
0584         cpuid_feature_extract_unsigned_field_width(features, field, width);
0585 }
0586 
0587 static inline int __attribute_const__
0588 cpuid_feature_extract_field(u64 features, int field, bool sign)
0589 {
0590     return cpuid_feature_extract_field_width(features, field, 4, sign);
0591 }
0592 
0593 static inline s64 arm64_ftr_value(const struct arm64_ftr_bits *ftrp, u64 val)
0594 {
0595     return (s64)cpuid_feature_extract_field_width(val, ftrp->shift, ftrp->width, ftrp->sign);
0596 }
0597 
0598 static inline bool id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)
0599 {
0600     return cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_BIGENDEL_SHIFT) == 0x1 ||
0601         cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_BIGENDEL0_SHIFT) == 0x1;
0602 }
0603 
0604 static inline bool id_aa64pfr0_32bit_el1(u64 pfr0)
0605 {
0606     u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_SHIFT);
0607 
0608     return val == ID_AA64PFR0_ELx_32BIT_64BIT;
0609 }
0610 
0611 static inline bool id_aa64pfr0_32bit_el0(u64 pfr0)
0612 {
0613     u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL0_SHIFT);
0614 
0615     return val == ID_AA64PFR0_ELx_32BIT_64BIT;
0616 }
0617 
0618 static inline bool id_aa64pfr0_sve(u64 pfr0)
0619 {
0620     u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_SVE_SHIFT);
0621 
0622     return val > 0;
0623 }
0624 
0625 static inline bool id_aa64pfr1_sme(u64 pfr1)
0626 {
0627     u32 val = cpuid_feature_extract_unsigned_field(pfr1, ID_AA64PFR1_SME_SHIFT);
0628 
0629     return val > 0;
0630 }
0631 
0632 static inline bool id_aa64pfr1_mte(u64 pfr1)
0633 {
0634     u32 val = cpuid_feature_extract_unsigned_field(pfr1, ID_AA64PFR1_MTE_SHIFT);
0635 
0636     return val >= ID_AA64PFR1_MTE;
0637 }
0638 
0639 void __init setup_cpu_features(void);
0640 void check_local_cpu_capabilities(void);
0641 
0642 u64 read_sanitised_ftr_reg(u32 id);
0643 u64 __read_sysreg_by_encoding(u32 sys_id);
0644 
0645 static inline bool cpu_supports_mixed_endian_el0(void)
0646 {
0647     return id_aa64mmfr0_mixed_endian_el0(read_cpuid(ID_AA64MMFR0_EL1));
0648 }
0649 
0650 
0651 static inline bool supports_csv2p3(int scope)
0652 {
0653     u64 pfr0;
0654     u8 csv2_val;
0655 
0656     if (scope == SCOPE_LOCAL_CPU)
0657         pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1);
0658     else
0659         pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
0660 
0661     csv2_val = cpuid_feature_extract_unsigned_field(pfr0,
0662                             ID_AA64PFR0_CSV2_SHIFT);
0663     return csv2_val == 3;
0664 }
0665 
0666 static inline bool supports_clearbhb(int scope)
0667 {
0668     u64 isar2;
0669 
0670     if (scope == SCOPE_LOCAL_CPU)
0671         isar2 = read_sysreg_s(SYS_ID_AA64ISAR2_EL1);
0672     else
0673         isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);
0674 
0675     return cpuid_feature_extract_unsigned_field(isar2,
0676                             ID_AA64ISAR2_EL1_BC_SHIFT);
0677 }
0678 
0679 const struct cpumask *system_32bit_el0_cpumask(void);
0680 DECLARE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0);
0681 
0682 static inline bool system_supports_32bit_el0(void)
0683 {
0684     u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
0685 
0686     return static_branch_unlikely(&arm64_mismatched_32bit_el0) ||
0687            id_aa64pfr0_32bit_el0(pfr0);
0688 }
0689 
0690 static inline bool system_supports_4kb_granule(void)
0691 {
0692     u64 mmfr0;
0693     u32 val;
0694 
0695     mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
0696     val = cpuid_feature_extract_unsigned_field(mmfr0,
0697                         ID_AA64MMFR0_TGRAN4_SHIFT);
0698 
0699     return (val >= ID_AA64MMFR0_TGRAN4_SUPPORTED_MIN) &&
0700            (val <= ID_AA64MMFR0_TGRAN4_SUPPORTED_MAX);
0701 }
0702 
0703 static inline bool system_supports_64kb_granule(void)
0704 {
0705     u64 mmfr0;
0706     u32 val;
0707 
0708     mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
0709     val = cpuid_feature_extract_unsigned_field(mmfr0,
0710                         ID_AA64MMFR0_TGRAN64_SHIFT);
0711 
0712     return (val >= ID_AA64MMFR0_TGRAN64_SUPPORTED_MIN) &&
0713            (val <= ID_AA64MMFR0_TGRAN64_SUPPORTED_MAX);
0714 }
0715 
0716 static inline bool system_supports_16kb_granule(void)
0717 {
0718     u64 mmfr0;
0719     u32 val;
0720 
0721     mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
0722     val = cpuid_feature_extract_unsigned_field(mmfr0,
0723                         ID_AA64MMFR0_TGRAN16_SHIFT);
0724 
0725     return (val >= ID_AA64MMFR0_TGRAN16_SUPPORTED_MIN) &&
0726            (val <= ID_AA64MMFR0_TGRAN16_SUPPORTED_MAX);
0727 }
0728 
0729 static inline bool system_supports_mixed_endian_el0(void)
0730 {
0731     return id_aa64mmfr0_mixed_endian_el0(read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1));
0732 }
0733 
0734 static inline bool system_supports_mixed_endian(void)
0735 {
0736     u64 mmfr0;
0737     u32 val;
0738 
0739     mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
0740     val = cpuid_feature_extract_unsigned_field(mmfr0,
0741                         ID_AA64MMFR0_BIGENDEL_SHIFT);
0742 
0743     return val == 0x1;
0744 }
0745 
0746 static __always_inline bool system_supports_fpsimd(void)
0747 {
0748     return !cpus_have_const_cap(ARM64_HAS_NO_FPSIMD);
0749 }
0750 
0751 static inline bool system_uses_hw_pan(void)
0752 {
0753     return IS_ENABLED(CONFIG_ARM64_PAN) &&
0754         cpus_have_const_cap(ARM64_HAS_PAN);
0755 }
0756 
0757 static inline bool system_uses_ttbr0_pan(void)
0758 {
0759     return IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN) &&
0760         !system_uses_hw_pan();
0761 }
0762 
0763 static __always_inline bool system_supports_sve(void)
0764 {
0765     return IS_ENABLED(CONFIG_ARM64_SVE) &&
0766         cpus_have_const_cap(ARM64_SVE);
0767 }
0768 
0769 static __always_inline bool system_supports_sme(void)
0770 {
0771     return IS_ENABLED(CONFIG_ARM64_SME) &&
0772         cpus_have_const_cap(ARM64_SME);
0773 }
0774 
0775 static __always_inline bool system_supports_fa64(void)
0776 {
0777     return IS_ENABLED(CONFIG_ARM64_SME) &&
0778         cpus_have_const_cap(ARM64_SME_FA64);
0779 }
0780 
0781 static __always_inline bool system_supports_tpidr2(void)
0782 {
0783     return system_supports_sme();
0784 }
0785 
0786 static __always_inline bool system_supports_cnp(void)
0787 {
0788     return IS_ENABLED(CONFIG_ARM64_CNP) &&
0789         cpus_have_const_cap(ARM64_HAS_CNP);
0790 }
0791 
0792 static inline bool system_supports_address_auth(void)
0793 {
0794     return IS_ENABLED(CONFIG_ARM64_PTR_AUTH) &&
0795         cpus_have_const_cap(ARM64_HAS_ADDRESS_AUTH);
0796 }
0797 
0798 static inline bool system_supports_generic_auth(void)
0799 {
0800     return IS_ENABLED(CONFIG_ARM64_PTR_AUTH) &&
0801         cpus_have_const_cap(ARM64_HAS_GENERIC_AUTH);
0802 }
0803 
0804 static inline bool system_has_full_ptr_auth(void)
0805 {
0806     return system_supports_address_auth() && system_supports_generic_auth();
0807 }
0808 
0809 static __always_inline bool system_uses_irq_prio_masking(void)
0810 {
0811     return IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) &&
0812            cpus_have_const_cap(ARM64_HAS_IRQ_PRIO_MASKING);
0813 }
0814 
0815 static inline bool system_supports_mte(void)
0816 {
0817     return IS_ENABLED(CONFIG_ARM64_MTE) &&
0818         cpus_have_const_cap(ARM64_MTE);
0819 }
0820 
0821 static inline bool system_has_prio_mask_debugging(void)
0822 {
0823     return IS_ENABLED(CONFIG_ARM64_DEBUG_PRIORITY_MASKING) &&
0824            system_uses_irq_prio_masking();
0825 }
0826 
0827 static inline bool system_supports_bti(void)
0828 {
0829     return IS_ENABLED(CONFIG_ARM64_BTI) && cpus_have_const_cap(ARM64_BTI);
0830 }
0831 
0832 static inline bool system_supports_tlb_range(void)
0833 {
0834     return IS_ENABLED(CONFIG_ARM64_TLB_RANGE) &&
0835         cpus_have_const_cap(ARM64_HAS_TLB_RANGE);
0836 }
0837 
0838 extern int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt);
0839 
0840 static inline u32 id_aa64mmfr0_parange_to_phys_shift(int parange)
0841 {
0842     switch (parange) {
0843     case ID_AA64MMFR0_PARANGE_32: return 32;
0844     case ID_AA64MMFR0_PARANGE_36: return 36;
0845     case ID_AA64MMFR0_PARANGE_40: return 40;
0846     case ID_AA64MMFR0_PARANGE_42: return 42;
0847     case ID_AA64MMFR0_PARANGE_44: return 44;
0848     case ID_AA64MMFR0_PARANGE_48: return 48;
0849     case ID_AA64MMFR0_PARANGE_52: return 52;
0850     /*
0851      * A future PE could use a value unknown to the kernel.
0852      * However, by the "D10.1.4 Principles of the ID scheme
0853      * for fields in ID registers", ARM DDI 0487C.a, any new
0854      * value is guaranteed to be higher than what we know already.
0855      * As a safe limit, we return the limit supported by the kernel.
0856      */
0857     default: return CONFIG_ARM64_PA_BITS;
0858     }
0859 }
0860 
0861 /* Check whether hardware update of the Access flag is supported */
0862 static inline bool cpu_has_hw_af(void)
0863 {
0864     u64 mmfr1;
0865 
0866     if (!IS_ENABLED(CONFIG_ARM64_HW_AFDBM))
0867         return false;
0868 
0869     mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
0870     return cpuid_feature_extract_unsigned_field(mmfr1,
0871                         ID_AA64MMFR1_HADBS_SHIFT);
0872 }
0873 
0874 static inline bool cpu_has_pan(void)
0875 {
0876     u64 mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
0877     return cpuid_feature_extract_unsigned_field(mmfr1,
0878                             ID_AA64MMFR1_PAN_SHIFT);
0879 }
0880 
0881 #ifdef CONFIG_ARM64_AMU_EXTN
0882 /* Check whether the cpu supports the Activity Monitors Unit (AMU) */
0883 extern bool cpu_has_amu_feat(int cpu);
0884 #else
0885 static inline bool cpu_has_amu_feat(int cpu)
0886 {
0887     return false;
0888 }
0889 #endif
0890 
0891 /* Get a cpu that supports the Activity Monitors Unit (AMU) */
0892 extern int get_cpu_with_amu_feat(void);
0893 
0894 static inline unsigned int get_vmid_bits(u64 mmfr1)
0895 {
0896     int vmid_bits;
0897 
0898     vmid_bits = cpuid_feature_extract_unsigned_field(mmfr1,
0899                         ID_AA64MMFR1_VMIDBITS_SHIFT);
0900     if (vmid_bits == ID_AA64MMFR1_VMIDBITS_16)
0901         return 16;
0902 
0903     /*
0904      * Return the default here even if any reserved
0905      * value is fetched from the system register.
0906      */
0907     return 8;
0908 }
0909 
0910 extern struct arm64_ftr_override id_aa64mmfr1_override;
0911 extern struct arm64_ftr_override id_aa64pfr0_override;
0912 extern struct arm64_ftr_override id_aa64pfr1_override;
0913 extern struct arm64_ftr_override id_aa64zfr0_override;
0914 extern struct arm64_ftr_override id_aa64smfr0_override;
0915 extern struct arm64_ftr_override id_aa64isar1_override;
0916 extern struct arm64_ftr_override id_aa64isar2_override;
0917 
0918 u32 get_kvm_ipa_limit(void);
0919 void dump_cpu_features(void);
0920 
0921 #endif /* __ASSEMBLY__ */
0922 
0923 #endif