Skip to content

Commit 86edf6b

Browse files
shamiali2008oupton
authored andcommitted
smccc/kvm_guest: Enable errata based on implementation CPUs
Retrieve any migration target implementation CPUs using the hypercall and enable associated errata. Reviewed-by: Cornelia Huck <cohuck@redhat.com> Reviewed-by: Sebastian Ott <sebott@redhat.com> Signed-off-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Link: https://lore.kernel.org/r/20250221140229.12588-6-shameerali.kolothum.thodi@huawei.com Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent c8c2647 commit 86edf6b

File tree

5 files changed

+114
-5
lines changed

5 files changed

+114
-5
lines changed

arch/arm64/include/asm/cputype.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ static inline bool midr_is_cpu_model_range(u32 midr, u32 model, u32 rv_min,
276276
return _model == model && rv >= rv_min && rv <= rv_max;
277277
}
278278

279+
struct target_impl_cpu {
280+
u64 midr;
281+
u64 revidr;
282+
u64 aidr;
283+
};
284+
285+
bool cpu_errata_set_target_impl(u64 num, void *impl_cpus);
279286
bool is_midr_in_range_list(struct midr_range const *ranges);
280287

281288
static inline u64 __attribute_const__ read_cpuid_mpidr(void)

arch/arm64/include/asm/hypervisor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
void kvm_init_hyp_services(void);
88
bool kvm_arm_hyp_service_available(u32 func_id);
9+
void kvm_arm_target_impl_cpu_init(void);
910

1011
#ifdef CONFIG_ARM_PKVM_GUEST
1112
void pkvm_init_hyp_services(void);

arch/arm64/kernel/cpu_errata.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,34 @@
1414
#include <asm/kvm_asm.h>
1515
#include <asm/smp_plat.h>
1616

17+
static u64 target_impl_cpu_num;
18+
static struct target_impl_cpu *target_impl_cpus;
19+
20+
bool cpu_errata_set_target_impl(u64 num, void *impl_cpus)
21+
{
22+
if (target_impl_cpu_num || !num || !impl_cpus)
23+
return false;
24+
25+
target_impl_cpu_num = num;
26+
target_impl_cpus = impl_cpus;
27+
return true;
28+
}
29+
1730
static inline bool is_midr_in_range(struct midr_range const *range)
1831
{
19-
return midr_is_cpu_model_range(read_cpuid_id(), range->model,
20-
range->rv_min, range->rv_max);
32+
int i;
33+
34+
if (!target_impl_cpu_num)
35+
return midr_is_cpu_model_range(read_cpuid_id(), range->model,
36+
range->rv_min, range->rv_max);
37+
38+
for (i = 0; i < target_impl_cpu_num; i++) {
39+
if (midr_is_cpu_model_range(target_impl_cpus[i].midr,
40+
range->model,
41+
range->rv_min, range->rv_max))
42+
return true;
43+
}
44+
return false;
2145
}
2246

2347
bool is_midr_in_range_list(struct midr_range const *ranges)
@@ -47,9 +71,20 @@ __is_affected_midr_range(const struct arm64_cpu_capabilities *entry,
4771
static bool __maybe_unused
4872
is_affected_midr_range(const struct arm64_cpu_capabilities *entry, int scope)
4973
{
50-
WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
51-
return __is_affected_midr_range(entry, read_cpuid_id(),
52-
read_cpuid(REVIDR_EL1));
74+
int i;
75+
76+
if (!target_impl_cpu_num) {
77+
WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
78+
return __is_affected_midr_range(entry, read_cpuid_id(),
79+
read_cpuid(REVIDR_EL1));
80+
}
81+
82+
for (i = 0; i < target_impl_cpu_num; i++) {
83+
if (__is_affected_midr_range(entry, target_impl_cpus[i].midr,
84+
target_impl_cpus[i].midr))
85+
return true;
86+
}
87+
return false;
5388
}
5489

5590
static bool __maybe_unused

arch/arm64/kernel/cpufeature.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#include <asm/kvm_host.h>
8787
#include <asm/mmu_context.h>
8888
#include <asm/mte.h>
89+
#include <asm/hypervisor.h>
8990
#include <asm/processor.h>
9091
#include <asm/smp.h>
9192
#include <asm/sysreg.h>
@@ -3680,6 +3681,7 @@ unsigned long cpu_get_elf_hwcap3(void)
36803681

36813682
static void __init setup_boot_cpu_capabilities(void)
36823683
{
3684+
kvm_arm_target_impl_cpu_init();
36833685
/*
36843686
* The boot CPU's feature register values have been recorded. Detect
36853687
* boot cpucaps and local cpucaps for the boot CPU, then enable and

drivers/firmware/smccc/kvm_guest.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
#include <linux/bitmap.h>
77
#include <linux/cache.h>
88
#include <linux/kernel.h>
9+
#include <linux/memblock.h>
910
#include <linux/string.h>
1011

12+
#include <uapi/linux/psci.h>
13+
1114
#include <asm/hypervisor.h>
1215

1316
static DECLARE_BITMAP(__kvm_arm_hyp_services, ARM_SMCCC_KVM_NUM_FUNCS) __ro_after_init = { };
@@ -51,3 +54,64 @@ bool kvm_arm_hyp_service_available(u32 func_id)
5154
return test_bit(func_id, __kvm_arm_hyp_services);
5255
}
5356
EXPORT_SYMBOL_GPL(kvm_arm_hyp_service_available);
57+
58+
void __init kvm_arm_target_impl_cpu_init(void)
59+
{
60+
int i;
61+
u32 ver;
62+
u64 max_cpus;
63+
struct arm_smccc_res res;
64+
struct target_impl_cpu *target;
65+
66+
if (!kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_DISCOVER_IMPL_VER) ||
67+
!kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_DISCOVER_IMPL_CPUS))
68+
return;
69+
70+
arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_DISCOVER_IMPL_VER_FUNC_ID,
71+
0, &res);
72+
if (res.a0 != SMCCC_RET_SUCCESS)
73+
return;
74+
75+
/* Version info is in lower 32 bits and is in SMMCCC_VERSION format */
76+
ver = lower_32_bits(res.a1);
77+
if (PSCI_VERSION_MAJOR(ver) != 1) {
78+
pr_warn("Unsupported target CPU implementation version v%d.%d\n",
79+
PSCI_VERSION_MAJOR(ver), PSCI_VERSION_MINOR(ver));
80+
return;
81+
}
82+
83+
if (!res.a2) {
84+
pr_warn("No target implementation CPUs specified\n");
85+
return;
86+
}
87+
88+
max_cpus = res.a2;
89+
target = memblock_alloc(sizeof(*target) * max_cpus, __alignof__(*target));
90+
if (!target) {
91+
pr_warn("Not enough memory for struct target_impl_cpu\n");
92+
return;
93+
}
94+
95+
for (i = 0; i < max_cpus; i++) {
96+
arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_DISCOVER_IMPL_CPUS_FUNC_ID,
97+
i, &res);
98+
if (res.a0 != SMCCC_RET_SUCCESS) {
99+
pr_warn("Discovering target implementation CPUs failed\n");
100+
goto mem_free;
101+
}
102+
target[i].midr = res.a1;
103+
target[i].revidr = res.a2;
104+
target[i].aidr = res.a3;
105+
};
106+
107+
if (!cpu_errata_set_target_impl(max_cpus, target)) {
108+
pr_warn("Failed to set target implementation CPUs\n");
109+
goto mem_free;
110+
}
111+
112+
pr_info("Number of target implementation CPUs is %lld\n", max_cpus);
113+
return;
114+
115+
mem_free:
116+
memblock_free(target, sizeof(*target) * max_cpus);
117+
}

0 commit comments

Comments
 (0)