|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Contains implementation-defined CPU feature definitions. |
| 4 | + */ |
| 5 | + |
| 6 | +#define pr_fmt(fmt) "CPU features: " fmt |
| 7 | + |
| 8 | +#include <asm/cpufeature.h> |
| 9 | +#include <asm/apple_cpufeature.h> |
| 10 | +#include <linux/irqflags.h> |
| 11 | +#include <linux/preempt.h> |
| 12 | +#include <linux/printk.h> |
| 13 | + |
| 14 | +#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL |
| 15 | +static bool has_apple_feature(const struct arm64_cpu_capabilities *entry, int scope) |
| 16 | +{ |
| 17 | + u64 val; |
| 18 | + WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible()); |
| 19 | + |
| 20 | + if (read_cpuid_implementor() != ARM_CPU_IMP_APPLE) |
| 21 | + return false; |
| 22 | + |
| 23 | + val = read_sysreg(aidr_el1); |
| 24 | + return cpufeature_matches(val, entry); |
| 25 | +} |
| 26 | + |
| 27 | +static bool has_apple_tso(const struct arm64_cpu_capabilities *entry, int scope) |
| 28 | +{ |
| 29 | + u64 val; |
| 30 | + |
| 31 | + if (!has_apple_feature(entry, scope)) |
| 32 | + return false; |
| 33 | + |
| 34 | + /* |
| 35 | + * KVM and old versions of the macOS hypervisor will advertise TSO in |
| 36 | + * AIDR_EL1, but then ignore writes to ACTLR_EL1. Test that the bit is |
| 37 | + * actually writable before enabling TSO. |
| 38 | + */ |
| 39 | + |
| 40 | + val = read_sysreg(actlr_el1); |
| 41 | + write_sysreg(val ^ ACTLR_APPLE_TSO, actlr_el1); |
| 42 | + if (!((val ^ read_sysreg(actlr_el1)) & ACTLR_APPLE_TSO)) { |
| 43 | + pr_info_once("CPU advertises Apple TSO but it is broken, ignoring\n"); |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + write_sysreg(val, actlr_el1); |
| 48 | + return true; |
| 49 | +} |
| 50 | + |
| 51 | +static bool has_tso_fixed(const struct arm64_cpu_capabilities *entry, int scope) |
| 52 | +{ |
| 53 | + /* List of CPUs that always use the TSO memory model */ |
| 54 | + static const struct midr_range fixed_tso_list[] = { |
| 55 | + MIDR_ALL_VERSIONS(MIDR_NVIDIA_DENVER), |
| 56 | + MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL), |
| 57 | + MIDR_ALL_VERSIONS(MIDR_FUJITSU_A64FX), |
| 58 | + { /* sentinel */ } |
| 59 | + }; |
| 60 | + |
| 61 | + return is_midr_in_range_list(read_cpuid_id(), fixed_tso_list); |
| 62 | +} |
| 63 | +#endif |
| 64 | + |
| 65 | +static bool has_apple_actlr_virt_impdef(const struct arm64_cpu_capabilities *entry, int scope) |
| 66 | +{ |
| 67 | + u64 midr = read_cpuid_id() & MIDR_CPU_MODEL_MASK; |
| 68 | + |
| 69 | + return midr >= MIDR_APPLE_M1_ICESTORM && midr <= MIDR_APPLE_M1_FIRESTORM_MAX; |
| 70 | +} |
| 71 | + |
| 72 | +static bool has_apple_actlr_virt(const struct arm64_cpu_capabilities *entry, int scope) |
| 73 | +{ |
| 74 | + u64 midr = read_cpuid_id() & MIDR_CPU_MODEL_MASK; |
| 75 | + |
| 76 | + return midr >= MIDR_APPLE_M2_BLIZZARD && midr <= MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, 0xfff); |
| 77 | +} |
| 78 | + |
| 79 | +static const struct arm64_cpu_capabilities arm64_impdef_features[] = { |
| 80 | +#ifdef CONFIG_ARM64_MEMORY_MODEL_CONTROL |
| 81 | + { |
| 82 | + .desc = "TSO memory model (Apple)", |
| 83 | + .capability = ARM64_HAS_TSO_APPLE, |
| 84 | + .type = SCOPE_LOCAL_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU, |
| 85 | + .matches = has_apple_tso, |
| 86 | + .field_pos = AIDR_APPLE_TSO_SHIFT, |
| 87 | + .field_width = 1, |
| 88 | + .sign = FTR_UNSIGNED, |
| 89 | + .min_field_value = 1, |
| 90 | + .max_field_value = 1, |
| 91 | + }, |
| 92 | + { |
| 93 | + .desc = "TSO memory model (Fixed)", |
| 94 | + .capability = ARM64_HAS_TSO_FIXED, |
| 95 | + .type = SCOPE_LOCAL_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU, |
| 96 | + .matches = has_tso_fixed, |
| 97 | + }, |
| 98 | +#endif |
| 99 | + { |
| 100 | + .desc = "ACTLR virtualization (IMPDEF, Apple)", |
| 101 | + .capability = ARM64_HAS_ACTLR_VIRT_APPLE, |
| 102 | + .type = SCOPE_LOCAL_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU, |
| 103 | + .matches = has_apple_actlr_virt_impdef, |
| 104 | + }, |
| 105 | + { |
| 106 | + .desc = "ACTLR virtualization (architectural?)", |
| 107 | + .capability = ARM64_HAS_ACTLR_VIRT, |
| 108 | + .type = SCOPE_LOCAL_CPU | ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU, |
| 109 | + .matches = has_apple_actlr_virt, |
| 110 | + }, |
| 111 | + {}, |
| 112 | +}; |
| 113 | + |
| 114 | +void __init init_cpucap_indirect_list_impdef(void) |
| 115 | +{ |
| 116 | + init_cpucap_indirect_list_from_array(arm64_impdef_features); |
| 117 | +} |
0 commit comments