/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (C) 2014 Linaro Ltd. <[email protected]>3*/45#ifndef __ASM_CPUFEATURE_H6#define __ASM_CPUFEATURE_H78#include <asm/alternative-macros.h>9#include <asm/cpucaps.h>10#include <asm/cputype.h>11#include <asm/hwcap.h>12#include <asm/sysreg.h>1314#define MAX_CPU_FEATURES 19215#define cpu_feature(x) KERNEL_HWCAP_ ## x1617#define ARM64_SW_FEATURE_OVERRIDE_NOKASLR 018#define ARM64_SW_FEATURE_OVERRIDE_HVHE 419#define ARM64_SW_FEATURE_OVERRIDE_RODATA_OFF 82021#ifndef __ASSEMBLY__2223#include <linux/bug.h>24#include <linux/jump_label.h>25#include <linux/kernel.h>26#include <linux/cpumask.h>2728/*29* CPU feature register tracking30*31* The safe value of a CPUID feature field is dependent on the implications32* of the values assigned to it by the architecture. Based on the relationship33* between the values, the features are classified into 3 types - LOWER_SAFE,34* HIGHER_SAFE and EXACT.35*36* The lowest value of all the CPUs is chosen for LOWER_SAFE and highest37* for HIGHER_SAFE. It is expected that all CPUs have the same value for38* a field when EXACT is specified, failing which, the safe value specified39* in the table is chosen.40*/4142enum ftr_type {43FTR_EXACT, /* Use a predefined safe value */44FTR_LOWER_SAFE, /* Smaller value is safe */45FTR_HIGHER_SAFE, /* Bigger value is safe */46FTR_HIGHER_OR_ZERO_SAFE, /* Bigger value is safe, but 0 is biggest */47};4849#define FTR_STRICT true /* SANITY check strict matching required */50#define FTR_NONSTRICT false /* SANITY check ignored */5152#define FTR_SIGNED true /* Value should be treated as signed */53#define FTR_UNSIGNED false /* Value should be treated as unsigned */5455#define FTR_VISIBLE true /* Feature visible to the user space */56#define FTR_HIDDEN false /* Feature is hidden from the user */5758#define FTR_VISIBLE_IF_IS_ENABLED(config) \59(IS_ENABLED(config) ? FTR_VISIBLE : FTR_HIDDEN)6061struct arm64_ftr_bits {62bool sign; /* Value is signed ? */63bool visible;64bool strict; /* CPU Sanity check: strict matching required ? */65enum ftr_type type;66u8 shift;67u8 width;68s64 safe_val; /* safe value for FTR_EXACT features */69};7071/*72* Describe the early feature override to the core override code:73*74* @val Values that are to be merged into the final75* sanitised value of the register. Only the bitfields76* set to 1 in @mask are valid77* @mask Mask of the features that are overridden by @val78*79* A @mask field set to full-1 indicates that the corresponding field80* in @val is a valid override.81*82* A @mask field set to full-0 with the corresponding @val field set83* to full-0 denotes that this field has no override84*85* A @mask field set to full-0 with the corresponding @val field set86* to full-1 denotes that this field has an invalid override.87*/88struct arm64_ftr_override {89u64 val;90u64 mask;91};9293/*94* @arm64_ftr_reg - Feature register95* @strict_mask Bits which should match across all CPUs for sanity.96* @sys_val Safe value across the CPUs (system view)97*/98struct arm64_ftr_reg {99const char *name;100u64 strict_mask;101u64 user_mask;102u64 sys_val;103u64 user_val;104struct arm64_ftr_override *override;105const struct arm64_ftr_bits *ftr_bits;106};107108extern struct arm64_ftr_reg arm64_ftr_reg_ctrel0;109110/*111* CPU capabilities:112*113* We use arm64_cpu_capabilities to represent system features, errata work114* arounds (both used internally by kernel and tracked in system_cpucaps) and115* ELF HWCAPs (which are exposed to user).116*117* To support systems with heterogeneous CPUs, we need to make sure that we118* detect the capabilities correctly on the system and take appropriate119* measures to ensure there are no incompatibilities.120*121* This comment tries to explain how we treat the capabilities.122* Each capability has the following list of attributes :123*124* 1) Scope of Detection : The system detects a given capability by125* performing some checks at runtime. This could be, e.g, checking the126* value of a field in CPU ID feature register or checking the cpu127* model. The capability provides a call back ( @matches() ) to128* perform the check. Scope defines how the checks should be performed.129* There are three cases:130*131* a) SCOPE_LOCAL_CPU: check all the CPUs and "detect" if at least one132* matches. This implies, we have to run the check on all the133* booting CPUs, until the system decides that state of the134* capability is finalised. (See section 2 below)135* Or136* b) SCOPE_SYSTEM: check all the CPUs and "detect" if all the CPUs137* matches. This implies, we run the check only once, when the138* system decides to finalise the state of the capability. If the139* capability relies on a field in one of the CPU ID feature140* registers, we use the sanitised value of the register from the141* CPU feature infrastructure to make the decision.142* Or143* c) SCOPE_BOOT_CPU: Check only on the primary boot CPU to detect the144* feature. This category is for features that are "finalised"145* (or used) by the kernel very early even before the SMP cpus146* are brought up.147*148* The process of detection is usually denoted by "update" capability149* state in the code.150*151* 2) Finalise the state : The kernel should finalise the state of a152* capability at some point during its execution and take necessary153* actions if any. Usually, this is done, after all the boot-time154* enabled CPUs are brought up by the kernel, so that it can make155* better decision based on the available set of CPUs. However, there156* are some special cases, where the action is taken during the early157* boot by the primary boot CPU. (e.g, running the kernel at EL2 with158* Virtualisation Host Extensions). The kernel usually disallows any159* changes to the state of a capability once it finalises the capability160* and takes any action, as it may be impossible to execute the actions161* safely. A CPU brought up after a capability is "finalised" is162* referred to as "Late CPU" w.r.t the capability. e.g, all secondary163* CPUs are treated "late CPUs" for capabilities determined by the boot164* CPU.165*166* At the moment there are two passes of finalising the capabilities.167* a) Boot CPU scope capabilities - Finalised by primary boot CPU via168* setup_boot_cpu_capabilities().169* b) Everything except (a) - Run via setup_system_capabilities().170*171* 3) Verification: When a CPU is brought online (e.g, by user or by the172* kernel), the kernel should make sure that it is safe to use the CPU,173* by verifying that the CPU is compliant with the state of the174* capabilities finalised already. This happens via :175*176* secondary_start_kernel()-> check_local_cpu_capabilities()177*178* As explained in (2) above, capabilities could be finalised at179* different points in the execution. Each newly booted CPU is verified180* against the capabilities that have been finalised by the time it181* boots.182*183* a) SCOPE_BOOT_CPU : All CPUs are verified against the capability184* except for the primary boot CPU.185*186* b) SCOPE_LOCAL_CPU, SCOPE_SYSTEM: All CPUs hotplugged on by the187* user after the kernel boot are verified against the capability.188*189* If there is a conflict, the kernel takes an action, based on the190* severity (e.g, a CPU could be prevented from booting or cause a191* kernel panic). The CPU is allowed to "affect" the state of the192* capability, if it has not been finalised already. See section 5193* for more details on conflicts.194*195* 4) Action: As mentioned in (2), the kernel can take an action for each196* detected capability, on all CPUs on the system. Appropriate actions197* include, turning on an architectural feature, modifying the control198* registers (e.g, SCTLR, TCR etc.) or patching the kernel via199* alternatives. The kernel patching is batched and performed at later200* point. The actions are always initiated only after the capability201* is finalised. This is usally denoted by "enabling" the capability.202* The actions are initiated as follows :203* a) Action is triggered on all online CPUs, after the capability is204* finalised, invoked within the stop_machine() context from205* enable_cpu_capabilitie().206*207* b) Any late CPU, brought up after (1), the action is triggered via:208*209* check_local_cpu_capabilities() -> verify_local_cpu_capabilities()210*211* 5) Conflicts: Based on the state of the capability on a late CPU vs.212* the system state, we could have the following combinations :213*214* x-----------------------------x215* | Type | System | Late CPU |216* |-----------------------------|217* | a | y | n |218* |-----------------------------|219* | b | n | y |220* x-----------------------------x221*222* Two separate flag bits are defined to indicate whether each kind of223* conflict can be allowed:224* ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU - Case(a) is allowed225* ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU - Case(b) is allowed226*227* Case (a) is not permitted for a capability that the system requires228* all CPUs to have in order for the capability to be enabled. This is229* typical for capabilities that represent enhanced functionality.230*231* Case (b) is not permitted for a capability that must be enabled232* during boot if any CPU in the system requires it in order to run233* safely. This is typical for erratum work arounds that cannot be234* enabled after the corresponding capability is finalised.235*236* In some non-typical cases either both (a) and (b), or neither,237* should be permitted. This can be described by including neither238* or both flags in the capability's type field.239*240* In case of a conflict, the CPU is prevented from booting. If the241* ARM64_CPUCAP_PANIC_ON_CONFLICT flag is specified for the capability,242* then a kernel panic is triggered.243*/244245246/*247* Decide how the capability is detected.248* On any local CPU vs System wide vs the primary boot CPU249*/250#define ARM64_CPUCAP_SCOPE_LOCAL_CPU ((u16)BIT(0))251#define ARM64_CPUCAP_SCOPE_SYSTEM ((u16)BIT(1))252/*253* The capabilitiy is detected on the Boot CPU and is used by kernel254* during early boot. i.e, the capability should be "detected" and255* "enabled" as early as possibly on all booting CPUs.256*/257#define ARM64_CPUCAP_SCOPE_BOOT_CPU ((u16)BIT(2))258#define ARM64_CPUCAP_SCOPE_MASK \259(ARM64_CPUCAP_SCOPE_SYSTEM | \260ARM64_CPUCAP_SCOPE_LOCAL_CPU | \261ARM64_CPUCAP_SCOPE_BOOT_CPU)262263#define SCOPE_SYSTEM ARM64_CPUCAP_SCOPE_SYSTEM264#define SCOPE_LOCAL_CPU ARM64_CPUCAP_SCOPE_LOCAL_CPU265#define SCOPE_BOOT_CPU ARM64_CPUCAP_SCOPE_BOOT_CPU266#define SCOPE_ALL ARM64_CPUCAP_SCOPE_MASK267268/*269* Is it permitted for a late CPU to have this capability when system270* hasn't already enabled it ?271*/272#define ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU ((u16)BIT(4))273/* Is it safe for a late CPU to miss this capability when system has it */274#define ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU ((u16)BIT(5))275/* Panic when a conflict is detected */276#define ARM64_CPUCAP_PANIC_ON_CONFLICT ((u16)BIT(6))277/*278* When paired with SCOPE_LOCAL_CPU, all early CPUs must satisfy the279* condition. This is different from SCOPE_SYSTEM where the check is performed280* only once at the end of the SMP boot on the sanitised ID registers.281* SCOPE_SYSTEM is not suitable for cases where the capability depends on282* properties local to a CPU like MIDR_EL1.283*/284#define ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS ((u16)BIT(7))285286/*287* CPU errata workarounds that need to be enabled at boot time if one or288* more CPUs in the system requires it. When one of these capabilities289* has been enabled, it is safe to allow any CPU to boot that doesn't290* require the workaround. However, it is not safe if a "late" CPU291* requires a workaround and the system hasn't enabled it already.292*/293#define ARM64_CPUCAP_LOCAL_CPU_ERRATUM \294(ARM64_CPUCAP_SCOPE_LOCAL_CPU | ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)295/*296* CPU feature detected at boot time based on system-wide value of a297* feature. It is safe for a late CPU to have this feature even though298* the system hasn't enabled it, although the feature will not be used299* by Linux in this case. If the system has enabled this feature already,300* then every late CPU must have it.301*/302#define ARM64_CPUCAP_SYSTEM_FEATURE \303(ARM64_CPUCAP_SCOPE_SYSTEM | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)304/*305* CPU feature detected at boot time based on feature of one or more CPUs.306* All possible conflicts for a late CPU are ignored.307* NOTE: this means that a late CPU with the feature will *not* cause the308* capability to be advertised by cpus_have_*cap()!309*/310#define ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE \311(ARM64_CPUCAP_SCOPE_LOCAL_CPU | \312ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU | \313ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)314/*315* CPU feature detected at boot time and present on all early CPUs. Late CPUs316* are permitted to have the feature even if it hasn't been enabled, although317* the feature will not be used by Linux in this case. If all early CPUs have318* the feature, then every late CPU must have it.319*/320#define ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE \321(ARM64_CPUCAP_SCOPE_LOCAL_CPU | \322ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU | \323ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS)324325/*326* CPU feature detected at boot time, on one or more CPUs. A late CPU327* is not allowed to have the capability when the system doesn't have it.328* It is Ok for a late CPU to miss the feature.329*/330#define ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE \331(ARM64_CPUCAP_SCOPE_LOCAL_CPU | \332ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU)333334/*335* CPU feature used early in the boot based on the boot CPU. All secondary336* CPUs must match the state of the capability as detected by the boot CPU. In337* case of a conflict, a kernel panic is triggered.338*/339#define ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE \340(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PANIC_ON_CONFLICT)341342/*343* CPU feature used early in the boot based on the boot CPU. It is safe for a344* late CPU to have this feature even though the boot CPU hasn't enabled it,345* although the feature will not be used by Linux in this case. If the boot CPU346* has enabled this feature already, then every late CPU must have it.347*/348#define ARM64_CPUCAP_BOOT_CPU_FEATURE \349(ARM64_CPUCAP_SCOPE_BOOT_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU)350351struct arm64_cpu_capabilities {352const char *desc;353u16 capability;354u16 type;355bool (*matches)(const struct arm64_cpu_capabilities *caps, int scope);356/*357* Take the appropriate actions to configure this capability358* for this CPU. If the capability is detected by the kernel359* this will be called on all the CPUs in the system,360* including the hotplugged CPUs, regardless of whether the361* capability is available on that specific CPU. This is362* useful for some capabilities (e.g, working around CPU363* errata), where all the CPUs must take some action (e.g,364* changing system control/configuration). Thus, if an action365* is required only if the CPU has the capability, then the366* routine must check it before taking any action.367*/368void (*cpu_enable)(const struct arm64_cpu_capabilities *cap);369union {370struct { /* To be used for erratum handling only */371struct midr_range midr_range;372const struct arm64_midr_revidr {373u32 midr_rv; /* revision/variant */374u32 revidr_mask;375} * const fixed_revs;376};377378const struct midr_range *midr_range_list;379struct { /* Feature register checking */380u32 sys_reg;381u8 field_pos;382u8 field_width;383u8 min_field_value;384u8 max_field_value;385u8 hwcap_type;386bool sign;387unsigned long hwcap;388};389};390391/*392* An optional list of "matches/cpu_enable" pair for the same393* "capability" of the same "type" as described by the parent.394* Only matches(), cpu_enable() and fields relevant to these395* methods are significant in the list. The cpu_enable is396* invoked only if the corresponding entry "matches()".397* However, if a cpu_enable() method is associated398* with multiple matches(), care should be taken that either399* the match criteria are mutually exclusive, or that the400* method is robust against being called multiple times.401*/402const struct arm64_cpu_capabilities *match_list;403const struct cpumask *cpus;404};405406static inline int cpucap_default_scope(const struct arm64_cpu_capabilities *cap)407{408return cap->type & ARM64_CPUCAP_SCOPE_MASK;409}410411static inline bool cpucap_match_all_early_cpus(const struct arm64_cpu_capabilities *cap)412{413return cap->type & ARM64_CPUCAP_MATCH_ALL_EARLY_CPUS;414}415416/*417* Generic helper for handling capabilities with multiple (match,enable) pairs418* of call backs, sharing the same capability bit.419* Iterate over each entry to see if at least one matches.420*/421static inline bool422cpucap_multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry,423int scope)424{425const struct arm64_cpu_capabilities *caps;426427for (caps = entry->match_list; caps->matches; caps++)428if (caps->matches(caps, scope))429return true;430431return false;432}433434static __always_inline bool is_vhe_hyp_code(void)435{436/* Only defined for code run in VHE hyp context */437return __is_defined(__KVM_VHE_HYPERVISOR__);438}439440static __always_inline bool is_nvhe_hyp_code(void)441{442/* Only defined for code run in NVHE hyp context */443return __is_defined(__KVM_NVHE_HYPERVISOR__);444}445446static __always_inline bool is_hyp_code(void)447{448return is_vhe_hyp_code() || is_nvhe_hyp_code();449}450451extern DECLARE_BITMAP(system_cpucaps, ARM64_NCAPS);452453extern DECLARE_BITMAP(boot_cpucaps, ARM64_NCAPS);454455#define for_each_available_cap(cap) \456for_each_set_bit(cap, system_cpucaps, ARM64_NCAPS)457458bool this_cpu_has_cap(unsigned int cap);459void cpu_set_feature(unsigned int num);460bool cpu_have_feature(unsigned int num);461unsigned long cpu_get_elf_hwcap(void);462unsigned long cpu_get_elf_hwcap2(void);463unsigned long cpu_get_elf_hwcap3(void);464465#define cpu_set_named_feature(name) cpu_set_feature(cpu_feature(name))466#define cpu_have_named_feature(name) cpu_have_feature(cpu_feature(name))467468static __always_inline bool boot_capabilities_finalized(void)469{470return alternative_has_cap_likely(ARM64_ALWAYS_BOOT);471}472473static __always_inline bool system_capabilities_finalized(void)474{475return alternative_has_cap_likely(ARM64_ALWAYS_SYSTEM);476}477478/*479* Test for a capability with a runtime check.480*481* Before the capability is detected, this returns false.482*/483static __always_inline bool cpus_have_cap(unsigned int num)484{485if (__builtin_constant_p(num) && !cpucap_is_possible(num))486return false;487if (num >= ARM64_NCAPS)488return false;489return arch_test_bit(num, system_cpucaps);490}491492/*493* Test for a capability without a runtime check.494*495* Before boot capabilities are finalized, this will BUG().496* After boot capabilities are finalized, this is patched to avoid a runtime497* check.498*499* @num must be a compile-time constant.500*/501static __always_inline bool cpus_have_final_boot_cap(int num)502{503if (boot_capabilities_finalized())504return alternative_has_cap_unlikely(num);505else506BUG();507}508509/*510* Test for a capability without a runtime check.511*512* Before system capabilities are finalized, this will BUG().513* After system capabilities are finalized, this is patched to avoid a runtime514* check.515*516* @num must be a compile-time constant.517*/518static __always_inline bool cpus_have_final_cap(int num)519{520if (system_capabilities_finalized())521return alternative_has_cap_unlikely(num);522else523BUG();524}525526static inline int __attribute_const__527cpuid_feature_extract_signed_field_width(u64 features, int field, int width)528{529return (s64)(features << (64 - width - field)) >> (64 - width);530}531532static inline int __attribute_const__533cpuid_feature_extract_signed_field(u64 features, int field)534{535return cpuid_feature_extract_signed_field_width(features, field, 4);536}537538static __always_inline unsigned int __attribute_const__539cpuid_feature_extract_unsigned_field_width(u64 features, int field, int width)540{541return (u64)(features << (64 - width - field)) >> (64 - width);542}543544static __always_inline unsigned int __attribute_const__545cpuid_feature_extract_unsigned_field(u64 features, int field)546{547return cpuid_feature_extract_unsigned_field_width(features, field, 4);548}549550static inline u64 arm64_ftr_mask(const struct arm64_ftr_bits *ftrp)551{552return (u64)GENMASK(ftrp->shift + ftrp->width - 1, ftrp->shift);553}554555static inline u64 arm64_ftr_reg_user_value(const struct arm64_ftr_reg *reg)556{557return (reg->user_val | (reg->sys_val & reg->user_mask));558}559560static inline int __attribute_const__561cpuid_feature_extract_field_width(u64 features, int field, int width, bool sign)562{563if (WARN_ON_ONCE(!width))564width = 4;565return (sign) ?566cpuid_feature_extract_signed_field_width(features, field, width) :567cpuid_feature_extract_unsigned_field_width(features, field, width);568}569570static inline int __attribute_const__571cpuid_feature_extract_field(u64 features, int field, bool sign)572{573return cpuid_feature_extract_field_width(features, field, 4, sign);574}575576static inline s64 arm64_ftr_value(const struct arm64_ftr_bits *ftrp, u64 val)577{578return (s64)cpuid_feature_extract_field_width(val, ftrp->shift, ftrp->width, ftrp->sign);579}580581static inline bool id_aa64mmfr0_mixed_endian_el0(u64 mmfr0)582{583return cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_BIGEND_SHIFT) == 0x1 ||584cpuid_feature_extract_unsigned_field(mmfr0, ID_AA64MMFR0_EL1_BIGENDEL0_SHIFT) == 0x1;585}586587static inline bool id_aa64pfr0_32bit_el1(u64 pfr0)588{589u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_EL1_SHIFT);590591return val == ID_AA64PFR0_EL1_EL1_AARCH32;592}593594static inline bool id_aa64pfr0_32bit_el0(u64 pfr0)595{596u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_EL0_SHIFT);597598return val == ID_AA64PFR0_EL1_EL0_AARCH32;599}600601static inline bool id_aa64pfr0_sve(u64 pfr0)602{603u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_SVE_SHIFT);604605return val > 0;606}607608static inline bool id_aa64pfr1_sme(u64 pfr1)609{610u32 val = cpuid_feature_extract_unsigned_field(pfr1, ID_AA64PFR1_EL1_SME_SHIFT);611612return val > 0;613}614615static inline bool id_aa64pfr0_mpam(u64 pfr0)616{617u32 val = cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_MPAM_SHIFT);618619return val > 0;620}621622static inline bool id_aa64pfr1_mte(u64 pfr1)623{624u32 val = cpuid_feature_extract_unsigned_field(pfr1, ID_AA64PFR1_EL1_MTE_SHIFT);625626return val >= ID_AA64PFR1_EL1_MTE_MTE2;627}628629void __init setup_boot_cpu_features(void);630void __init setup_system_features(void);631void __init setup_user_features(void);632633void check_local_cpu_capabilities(void);634635u64 read_sanitised_ftr_reg(u32 id);636u64 __read_sysreg_by_encoding(u32 sys_id);637638static inline bool cpu_supports_mixed_endian_el0(void)639{640return id_aa64mmfr0_mixed_endian_el0(read_cpuid(ID_AA64MMFR0_EL1));641}642643644static inline bool supports_csv2p3(int scope)645{646u64 pfr0;647u8 csv2_val;648649if (scope == SCOPE_LOCAL_CPU)650pfr0 = read_sysreg_s(SYS_ID_AA64PFR0_EL1);651else652pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);653654csv2_val = cpuid_feature_extract_unsigned_field(pfr0,655ID_AA64PFR0_EL1_CSV2_SHIFT);656return csv2_val == 3;657}658659static inline bool supports_clearbhb(int scope)660{661u64 isar2;662663if (scope == SCOPE_LOCAL_CPU)664isar2 = read_sysreg_s(SYS_ID_AA64ISAR2_EL1);665else666isar2 = read_sanitised_ftr_reg(SYS_ID_AA64ISAR2_EL1);667668return cpuid_feature_extract_unsigned_field(isar2,669ID_AA64ISAR2_EL1_CLRBHB_SHIFT);670}671672const struct cpumask *system_32bit_el0_cpumask(void);673const struct cpumask *fallback_32bit_el0_cpumask(void);674DECLARE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0);675676static inline bool system_supports_32bit_el0(void)677{678u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);679680return static_branch_unlikely(&arm64_mismatched_32bit_el0) ||681id_aa64pfr0_32bit_el0(pfr0);682}683684static inline bool system_supports_4kb_granule(void)685{686u64 mmfr0;687u32 val;688689mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);690val = cpuid_feature_extract_unsigned_field(mmfr0,691ID_AA64MMFR0_EL1_TGRAN4_SHIFT);692693return (val >= ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MIN) &&694(val <= ID_AA64MMFR0_EL1_TGRAN4_SUPPORTED_MAX);695}696697static inline bool system_supports_64kb_granule(void)698{699u64 mmfr0;700u32 val;701702mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);703val = cpuid_feature_extract_unsigned_field(mmfr0,704ID_AA64MMFR0_EL1_TGRAN64_SHIFT);705706return (val >= ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MIN) &&707(val <= ID_AA64MMFR0_EL1_TGRAN64_SUPPORTED_MAX);708}709710static inline bool system_supports_16kb_granule(void)711{712u64 mmfr0;713u32 val;714715mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);716val = cpuid_feature_extract_unsigned_field(mmfr0,717ID_AA64MMFR0_EL1_TGRAN16_SHIFT);718719return (val >= ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MIN) &&720(val <= ID_AA64MMFR0_EL1_TGRAN16_SUPPORTED_MAX);721}722723static inline bool system_supports_mixed_endian_el0(void)724{725return id_aa64mmfr0_mixed_endian_el0(read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1));726}727728static inline bool system_supports_mixed_endian(void)729{730u64 mmfr0;731u32 val;732733mmfr0 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);734val = cpuid_feature_extract_unsigned_field(mmfr0,735ID_AA64MMFR0_EL1_BIGEND_SHIFT);736737return val == 0x1;738}739740static __always_inline bool system_supports_fpsimd(void)741{742return alternative_has_cap_likely(ARM64_HAS_FPSIMD);743}744745static inline bool system_uses_hw_pan(void)746{747return alternative_has_cap_unlikely(ARM64_HAS_PAN);748}749750static inline bool system_uses_ttbr0_pan(void)751{752return IS_ENABLED(CONFIG_ARM64_SW_TTBR0_PAN) &&753!system_uses_hw_pan();754}755756static __always_inline bool system_supports_sve(void)757{758return alternative_has_cap_unlikely(ARM64_SVE);759}760761static __always_inline bool system_supports_sme(void)762{763return alternative_has_cap_unlikely(ARM64_SME);764}765766static __always_inline bool system_supports_sme2(void)767{768return alternative_has_cap_unlikely(ARM64_SME2);769}770771static __always_inline bool system_supports_fa64(void)772{773return alternative_has_cap_unlikely(ARM64_SME_FA64);774}775776static __always_inline bool system_supports_tpidr2(void)777{778return system_supports_sme();779}780781static __always_inline bool system_supports_fpmr(void)782{783return alternative_has_cap_unlikely(ARM64_HAS_FPMR);784}785786static __always_inline bool system_supports_cnp(void)787{788return alternative_has_cap_unlikely(ARM64_HAS_CNP);789}790791static inline bool system_supports_address_auth(void)792{793return cpus_have_final_boot_cap(ARM64_HAS_ADDRESS_AUTH);794}795796static inline bool system_supports_generic_auth(void)797{798return alternative_has_cap_unlikely(ARM64_HAS_GENERIC_AUTH);799}800801static inline bool system_has_full_ptr_auth(void)802{803return system_supports_address_auth() && system_supports_generic_auth();804}805806static __always_inline bool system_uses_irq_prio_masking(void)807{808return alternative_has_cap_unlikely(ARM64_HAS_GIC_PRIO_MASKING);809}810811static inline bool system_supports_mte(void)812{813return alternative_has_cap_unlikely(ARM64_MTE);814}815816static inline bool system_has_prio_mask_debugging(void)817{818return IS_ENABLED(CONFIG_ARM64_DEBUG_PRIORITY_MASKING) &&819system_uses_irq_prio_masking();820}821822static inline bool system_supports_bti(void)823{824return cpus_have_final_cap(ARM64_BTI);825}826827static inline bool system_supports_bti_kernel(void)828{829return IS_ENABLED(CONFIG_ARM64_BTI_KERNEL) &&830cpus_have_final_boot_cap(ARM64_BTI);831}832833static inline bool system_supports_tlb_range(void)834{835return alternative_has_cap_unlikely(ARM64_HAS_TLB_RANGE);836}837838static inline bool system_supports_lpa2(void)839{840return cpus_have_final_cap(ARM64_HAS_LPA2);841}842843static inline bool system_supports_poe(void)844{845return alternative_has_cap_unlikely(ARM64_HAS_S1POE);846}847848static inline bool system_supports_gcs(void)849{850return alternative_has_cap_unlikely(ARM64_HAS_GCS);851}852853static inline bool system_supports_haft(void)854{855return cpus_have_final_cap(ARM64_HAFT);856}857858static __always_inline bool system_supports_mpam(void)859{860return alternative_has_cap_unlikely(ARM64_MPAM);861}862863static __always_inline bool system_supports_mpam_hcr(void)864{865return alternative_has_cap_unlikely(ARM64_MPAM_HCR);866}867868static inline bool system_supports_pmuv3(void)869{870return cpus_have_final_cap(ARM64_HAS_PMUV3);871}872873static inline bool system_supports_bbml2_noabort(void)874{875return alternative_has_cap_unlikely(ARM64_HAS_BBML2_NOABORT);876}877878int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt);879bool try_emulate_mrs(struct pt_regs *regs, u32 isn);880881static inline u32 id_aa64mmfr0_parange_to_phys_shift(int parange)882{883switch (parange) {884case ID_AA64MMFR0_EL1_PARANGE_32: return 32;885case ID_AA64MMFR0_EL1_PARANGE_36: return 36;886case ID_AA64MMFR0_EL1_PARANGE_40: return 40;887case ID_AA64MMFR0_EL1_PARANGE_42: return 42;888case ID_AA64MMFR0_EL1_PARANGE_44: return 44;889case ID_AA64MMFR0_EL1_PARANGE_48: return 48;890case ID_AA64MMFR0_EL1_PARANGE_52: return 52;891/*892* A future PE could use a value unknown to the kernel.893* However, by the "D10.1.4 Principles of the ID scheme894* for fields in ID registers", ARM DDI 0487C.a, any new895* value is guaranteed to be higher than what we know already.896* As a safe limit, we return the limit supported by the kernel.897*/898default: return CONFIG_ARM64_PA_BITS;899}900}901902/* Check whether hardware update of the Access flag is supported */903static inline bool cpu_has_hw_af(void)904{905u64 mmfr1;906907if (!IS_ENABLED(CONFIG_ARM64_HW_AFDBM))908return false;909910/*911* Use cached version to avoid emulated msr operation on KVM912* guests.913*/914mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);915return cpuid_feature_extract_unsigned_field(mmfr1,916ID_AA64MMFR1_EL1_HAFDBS_SHIFT);917}918919static inline bool cpu_has_pan(void)920{921u64 mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);922return cpuid_feature_extract_unsigned_field(mmfr1,923ID_AA64MMFR1_EL1_PAN_SHIFT);924}925926#ifdef CONFIG_ARM64_AMU_EXTN927/* Check whether the cpu supports the Activity Monitors Unit (AMU) */928extern bool cpu_has_amu_feat(int cpu);929#else930static inline bool cpu_has_amu_feat(int cpu)931{932return false;933}934#endif935936/* Get a cpu that supports the Activity Monitors Unit (AMU) */937extern int get_cpu_with_amu_feat(void);938939static inline unsigned int get_vmid_bits(u64 mmfr1)940{941int vmid_bits;942943vmid_bits = cpuid_feature_extract_unsigned_field(mmfr1,944ID_AA64MMFR1_EL1_VMIDBits_SHIFT);945if (vmid_bits == ID_AA64MMFR1_EL1_VMIDBits_16)946return 16;947948/*949* Return the default here even if any reserved950* value is fetched from the system register.951*/952return 8;953}954955s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new, s64 cur);956struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id);957958extern struct arm64_ftr_override id_aa64mmfr0_override;959extern struct arm64_ftr_override id_aa64mmfr1_override;960extern struct arm64_ftr_override id_aa64mmfr2_override;961extern struct arm64_ftr_override id_aa64pfr0_override;962extern struct arm64_ftr_override id_aa64pfr1_override;963extern struct arm64_ftr_override id_aa64zfr0_override;964extern struct arm64_ftr_override id_aa64smfr0_override;965extern struct arm64_ftr_override id_aa64isar1_override;966extern struct arm64_ftr_override id_aa64isar2_override;967968extern struct arm64_ftr_override arm64_sw_feature_override;969970static inline971u64 arm64_apply_feature_override(u64 val, int feat, int width,972const struct arm64_ftr_override *override)973{974u64 oval = override->val;975976/*977* When it encounters an invalid override (e.g., an override that978* cannot be honoured due to a missing CPU feature), the early idreg979* override code will set the mask to 0x0 and the value to non-zero for980* the field in question. In order to determine whether the override is981* valid or not for the field we are interested in, we first need to982* disregard bits belonging to other fields.983*/984oval &= GENMASK_ULL(feat + width - 1, feat);985986/*987* The override is valid if all value bits are accounted for in the988* mask. If so, replace the masked bits with the override value.989*/990if (oval == (oval & override->mask)) {991val &= ~override->mask;992val |= oval;993}994995/* Extract the field from the updated value */996return cpuid_feature_extract_unsigned_field(val, feat);997}998999static inline bool arm64_test_sw_feature_override(int feat)1000{1001/*1002* Software features are pseudo CPU features that have no underlying1003* CPUID system register value to apply the override to.1004*/1005return arm64_apply_feature_override(0, feat, 4,1006&arm64_sw_feature_override);1007}10081009static inline bool kaslr_disabled_cmdline(void)1010{1011return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_NOKASLR);1012}10131014u32 get_kvm_ipa_limit(void);1015void dump_cpu_features(void);10161017static inline bool cpu_has_bti(void)1018{1019if (!IS_ENABLED(CONFIG_ARM64_BTI))1020return false;10211022return arm64_apply_feature_override(read_cpuid(ID_AA64PFR1_EL1),1023ID_AA64PFR1_EL1_BT_SHIFT, 4,1024&id_aa64pfr1_override);1025}10261027static inline bool cpu_has_pac(void)1028{1029u64 isar1, isar2;10301031if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH))1032return false;10331034isar1 = read_cpuid(ID_AA64ISAR1_EL1);1035isar2 = read_cpuid(ID_AA64ISAR2_EL1);10361037if (arm64_apply_feature_override(isar1, ID_AA64ISAR1_EL1_APA_SHIFT, 4,1038&id_aa64isar1_override))1039return true;10401041if (arm64_apply_feature_override(isar1, ID_AA64ISAR1_EL1_API_SHIFT, 4,1042&id_aa64isar1_override))1043return true;10441045return arm64_apply_feature_override(isar2, ID_AA64ISAR2_EL1_APA3_SHIFT, 4,1046&id_aa64isar2_override);1047}10481049static inline bool cpu_has_lva(void)1050{1051u64 mmfr2;10521053mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);1054mmfr2 &= ~id_aa64mmfr2_override.mask;1055mmfr2 |= id_aa64mmfr2_override.val;1056return cpuid_feature_extract_unsigned_field(mmfr2,1057ID_AA64MMFR2_EL1_VARange_SHIFT);1058}10591060static inline bool cpu_has_lpa2(void)1061{1062#ifdef CONFIG_ARM64_LPA21063u64 mmfr0;1064int feat;10651066mmfr0 = read_sysreg(id_aa64mmfr0_el1);1067mmfr0 &= ~id_aa64mmfr0_override.mask;1068mmfr0 |= id_aa64mmfr0_override.val;1069feat = cpuid_feature_extract_signed_field(mmfr0,1070ID_AA64MMFR0_EL1_TGRAN_SHIFT);10711072return feat >= ID_AA64MMFR0_EL1_TGRAN_LPA2;1073#else1074return false;1075#endif1076}10771078#endif /* __ASSEMBLY__ */10791080#endif108110821083