* [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line
@ 2026-12-02 14:11 Steffen Eiden
2026-12-02 14:11 ` [PATCH 01/12] arm64: Prepare for sharing with s390 Steffen Eiden
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
Add SMCCC/PSCI support, the arm64 architected timers, WFx* handling and
the userspace IRQ line for KVM/arm64 on s390. The arm64 SMCCC, timer and
vCPU power management code is shared with s390 and wired into the s390
KVM module.
This is the third series on the way to arm64 guests on s390. The first
series laid the KVM foundation (basic module, vCPU run, simple page
faults) and is included in v7.4. The second series built the system
register layer [1]. This series adds the guest CPU management layer on
top. Power management with PSCI, full SMCCC handling, the architected
timers and interrupt injection from userspace.
This series depends on the second series [1], on the vCPU kick
enabling series [2], and on the vgic stubs patch [3]
PATCH 04 and 05 can be squashed into series 2 [1] and will be when I
send v5. I just did not want to send a respin after a few days w/o much
comments.
Changes in detail:
arm64:
Prepare headers and code for sharing with s390: guard arm64 headers
that reside in include/ such that s390 can consume them, and move
a few things/ The moves add a bit of churn in the diff but reduce the
number of shared sections required for the timer from seven to two.
Share the SMCCC related files (hypercalls, psci, trng, pvtime) as a
whole.Share the timer functionality that is architecture agnostic. As
timers work differently on s390 only a subset can be reused. Share the
functions for managing SMP guests.
s390:
Implement the mp_state plumbing for arm64 guests backed by the shared
power-off/suspend infrastructure and the MPIDR data used for SMP guest
management.
Use the shared arm64 code for handling SMCCCs and implement the final
host-side plumbing, removing the primitive SYSTEM_OFF only
implementation.
Add support for the arm64 architected timers (virtual and physical) of
an arm64 guest. The SAE block holds the timer control registers, and
ISTATUS in CTL is set when a timer condition is met. The shared arm64
arch_timer.c is built with s390-specific backends: CTL and CVAL are
accessed through vcpu_{read,write}_sys_reg(), and the guest counter is
obtained with PTFF QAGPT from the guest physical time offset (GPTO) in
the SAE block.
Trap WFx* and handle them in kvm_handle_wfx(). A vCPU halted in WFI or
WFIT is woken by a background hrtimer armed for the earliest of the
enabled guest timer expiries and the WFIT deadline; on expiry the
hrtimer raises KVM_REQ_UNBLOCK and wakes the vCPU.
There is no in-kernel irqchip (yet!), so timer output level changes
cannot be injected. After each guest exit handled in the kernel,
including a timer interception (SAE_ICPTR_TIMER), ISTATUS in CTL is
compared with the level last seen by userspace and the vCPU exits with
KVM_EXIT_INTR on a change. On vCPU reset both timers are disabled and
unmasked.
Implement the IRQ line for the !irqchip_in_kernel case, i.e. interrupt
injection from userspace.
A branch with this series at kernel.org is available [4].
Steffen
[1] https://lore.kernel.org/all/20261127-sysreg-v4-0-a6318d18aab5@linux.ibm.com
[2] https://lore.kernel.org/all/20261125-vcpu_kick-v1-0-a203b01ab957@linux.ibm.com
[3] https://lore.kernel.org/all/20261125-kvm_vgic_stubs-v1-1-c53ca2c5959a@linux.ibm.com
[4] https://git.kernel.org/pub/scm/linux/kernel/git/kvms390/linux.git/log/?h=sae
---
Andreas Grapentin (1):
KVM: s390: arm64: Add IRQ line
Gautam Gala (2):
KVM: s390: arm64: Provide timer infrastructure
KVM: s390: arm64: Trap WFx
Steffen Eiden (9):
arm64: Prepare for sharing with s390
arm64: Share timer and smccc functionality with s390
KVM: arm64: Share timer and smccc code with s390
KVM: s390: arm64: Use register shadow helper
KVM: s390: arm64: Shadow the guest timer CVAL registers
KVM: s390: arm64: Use mpstate for guests
KVM: s390: arm64: Enable full smccc support
KVM: s390: arm64: Report fwregs to userspace
KVM: s390: arm64: Integrate guest timer
arch/arm64/include/asm/kvm_emulate.h | 4 +-
arch/arm64/include/asm/kvm_host.h | 40 +++---
arch/arm64/include/asm/kvm_mmu.h | 2 +
arch/arm64/include/asm/pvclock-abi.h | 1 +
arch/arm64/kvm/arch_timer.c | 95 +++++++-------
arch/arm64/kvm/arm.c | 25 +++-
arch/arm64/kvm/hypercalls.c | 4 +
arch/arm64/kvm/psci.c | 3 +
arch/arm64/kvm/pvtime.c | 2 +
arch/arm64/kvm/trng.c | 2 +
arch/s390/include/arm64/kvm_emulate.h | 6 +
arch/s390/include/asm/kvm_host_arm64.h | 45 ++++++-
arch/s390/kvm/Kconfig | 1 +
arch/s390/kvm/arm64/Makefile | 7 +-
arch/s390/kvm/arm64/Makefile.gen | 7 +-
arch/s390/kvm/arm64/arch_timer.c | 226 ++++++++++++++++++++++++++++++++
arch/s390/kvm/arm64/arm.c | 227 ++++++++++++++++++++++++++++++---
arch/s390/kvm/arm64/guest.c | 14 ++
arch/s390/kvm/arm64/handle_exit.c | 105 ++++++++++-----
arch/s390/kvm/arm64/hypercalls.c | 32 +++++
arch/s390/kvm/arm64/psci.c | 20 +++
arch/s390/kvm/arm64/pvtime.c | 15 +++
arch/s390/kvm/arm64/reset.c | 14 +-
arch/s390/kvm/arm64/sys_regs.c | 8 +-
arch/s390/kvm/arm64/trace.h | 45 +++++++
arch/s390/kvm/arm64/trng.c | 12 ++
arch/s390/tools/Makefile.arm64h | 2 +
include/kvm/arm_hypercalls.h | 3 +
include/kvm/arm_psci.h | 3 +
29 files changed, 836 insertions(+), 134 deletions(-)
base-commit: 9437ee8b6cf607e883b36e44bd755968daaebd48
change-id: 20261127-smccc-c8cb09f78ebc
prerequisite-message-id: 20261105-insn-v2-1-ff27987aa212@linux.ibm.com
prerequisite-patch-id: ed28ef94c9056cb81bb738f1b54745d012cb613f
prerequisite-change-id: 20261106-sysreg-d6adf0cd34d4:v4
prerequisite-patch-id: ed28ef94c9056cb81bb738f1b54745d012cb613f
prerequisite-patch-id: 4f6b2386967522be268f77ca6eb9487a3503dc96
prerequisite-patch-id: 539f1138a427fddb952a5f7a0691e8acd974d0fb
prerequisite-patch-id: 3e9b8384f07ff18911f48eabe625a1c0507a5e96
prerequisite-patch-id: fc58c00a1006ecffd045728020ef01308022ac03
prerequisite-patch-id: 2c22bad7059a38c4ea01ca774d0c0b49cea27fba
prerequisite-patch-id: c809f818609fed0a0f8ccb1721b96f5329505543
prerequisite-patch-id: b35581eba719557148ea84232698457adc8c8cc4
prerequisite-patch-id: 0b8792bac82c24bfac0b5912d2eec5da32413747
prerequisite-patch-id: 58fb40b78dd793ca3b24cadadad8868bc54aa20e
prerequisite-patch-id: d986b0811f1feac0678eb135ce5c44140748431b
prerequisite-patch-id: 0e3d84b8259a175fceb67b01bc37c1830805636b
prerequisite-patch-id: 53a6741f3c6ec60c798995ccbf4d2b293619ec34
prerequisite-patch-id: b71ccf22e4cb8fccf4ca9eea558af14163b256db
prerequisite-patch-id: bba91e0c077b1f308e2ec914a54399f775d9090d
prerequisite-patch-id: 9be57897346558cd0bea61d946d85d3ba9a17f2e
prerequisite-patch-id: f4ae3a54ce24b78e335eb1f019b971419d82720a
prerequisite-patch-id: cc8dfe5b2b8a3f28b3c29b1a10827d99f987694d
prerequisite-patch-id: f27d96dacb5c7dadc01f1cc72158b8b3f64d7053
prerequisite-patch-id: 1c69a0380267279633c8ba33bf1d30c13877e033
prerequisite-patch-id: 20ac948d6ec4bfd4b9c3d053bcb0fcb3fe067486
prerequisite-patch-id: db6b83091a6107dbfd9c4311af14fadf1534375f
prerequisite-patch-id: af30d541a7cabea0981dfe79276d1e9991a8c91a
prerequisite-patch-id: 5eead648d962cbdc9f45ce40538f174faedd5775
prerequisite-patch-id: 97589af6c7ac9efe15c371883511fc05d4f1c870
prerequisite-patch-id: f661ef5923e8026fab81d73e596006ac30d5cbac
prerequisite-message-id: 20261125-kvm_vgic_stubs-v1-1-c53ca2c5959a@linux.ibm.com
prerequisite-patch-id: bd1a0c1fa7ff80d9edd5fff5258d8af1de06d7d7
prerequisite-message-id: 20261125-vcpu_kick-v1-0-a203b01ab957@linux.ibm.com
prerequisite-patch-id: 90cb98820a1c6fec2085ca6830827f90c6b659ab
prerequisite-patch-id: 36feb5c5ca141ca0a94f60e8cc27e3969f3ebefd
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 01/12] arm64: Prepare for sharing with s390
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 03/12] KVM: arm64: Share timer and smccc code " Steffen Eiden
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
Add s390 guards for arm headers which reside in include/kvm. This allows
s390 to use headers placed in its arch include/arm64/ .
Move vcpu_can_run to a more convenient location such that it can be
shared without adding another shared section. Move functions in
arch_timer.c to more convenient locations such that the required
functionality can be shared with just two shared regions instead of
seven.
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/arm64/include/asm/kvm_host.h | 4 +-
arch/arm64/kvm/arch_timer.c | 87 ++++++++++++++++++++-------------------
arch/arm64/kvm/arm.c | 10 ++---
include/kvm/arm_hypercalls.h | 3 ++
include/kvm/arm_psci.h | 3 ++
5 files changed, 57 insertions(+), 50 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 3242bdf1acf8..5072de2ac8ce 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -396,6 +396,8 @@ static inline u16 kvm_mpidr_index(struct kvm_mpidr_data *data, u64 mpidr)
return index;
}
+struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
+
struct kvm_sysreg_masks;
enum fgt_group_id {
@@ -1444,8 +1446,6 @@ static inline bool kvm_arm_is_pvtime_enabled(struct kvm_vcpu_arch *vcpu_arch)
return (vcpu_arch->steal.base != INVALID_GPA);
}
-struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
-
DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
/*
diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c
index b2c5ba499aa5..2de8ce48a7a0 100644
--- a/arch/arm64/kvm/arch_timer.c
+++ b/arch/arm64/kvm/arch_timer.c
@@ -50,6 +50,7 @@ static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu,
struct arch_timer_context *timer,
enum kvm_arch_timer_regs treg);
static bool kvm_arch_timer_get_input_level(int vintid);
+static inline bool userspace_irqchip(struct kvm *kvm);
static unsigned long kvm_arch_timer_get_irq_flags(void)
{
@@ -67,14 +68,6 @@ static const struct irq_ops arch_timer_irq_ops_vgic_v5 = {
.set_direct_injection = vgic_v5_set_ppi_dvi,
};
-static int nr_timers(struct kvm_vcpu *vcpu)
-{
- if (!vcpu_has_nv(vcpu))
- return NR_KVM_EL0_TIMERS;
-
- return NR_KVM_TIMERS;
-}
-
u32 timer_get_ctl(struct arch_timer_context *ctxt)
{
struct kvm_vcpu *vcpu = timer_context_to_vcpu(ctxt);
@@ -191,22 +184,6 @@ void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
trace_kvm_get_timer_map(vcpu->vcpu_id, map);
}
-static inline bool userspace_irqchip(struct kvm *kvm)
-{
- return unlikely(!irqchip_in_kernel(kvm));
-}
-
-static void soft_timer_start(struct hrtimer *hrt, u64 ns)
-{
- hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
- HRTIMER_MODE_ABS_HARD);
-}
-
-static void soft_timer_cancel(struct hrtimer *hrt)
-{
- hrtimer_cancel(hrt);
-}
-
static irqreturn_t kvm_arch_timer_handler(int irq, void *dev_id)
{
struct kvm_vcpu *vcpu = *(struct kvm_vcpu **)dev_id;
@@ -257,11 +234,6 @@ static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx,
return 0;
}
-static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
-{
- return kvm_counter_compute_delta(timer_ctx, timer_get_cval(timer_ctx));
-}
-
static bool kvm_timer_enabled(struct arch_timer_context *timer_ctx)
{
WARN_ON(timer_ctx && timer_ctx->loaded);
@@ -276,6 +248,35 @@ static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu)
vcpu_get_flag(vcpu, IN_WFIT));
}
+static int nr_timers(struct kvm_vcpu *vcpu)
+{
+ if (!vcpu_has_nv(vcpu))
+ return NR_KVM_EL0_TIMERS;
+
+ return NR_KVM_TIMERS;
+}
+
+static inline bool userspace_irqchip(struct kvm *kvm)
+{
+ return unlikely(!irqchip_in_kernel(kvm));
+}
+
+static void soft_timer_start(struct hrtimer *hrt, u64 ns)
+{
+ hrtimer_start(hrt, ktime_add_ns(ktime_get(), ns),
+ HRTIMER_MODE_ABS_HARD);
+}
+
+static void soft_timer_cancel(struct hrtimer *hrt)
+{
+ hrtimer_cancel(hrt);
+}
+
+static u64 kvm_timer_compute_delta(struct arch_timer_context *timer_ctx)
+{
+ return kvm_counter_compute_delta(timer_ctx, timer_get_cval(timer_ctx));
+}
+
static u64 wfit_delay_ns(struct kvm_vcpu *vcpu)
{
u64 val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
@@ -436,6 +437,20 @@ bool kvm_timer_update_run(struct kvm_vcpu *vcpu)
return !!mask;
}
+static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
+{
+ struct arch_timer_cpu *timer = vcpu_timer(vcpu);
+
+ soft_timer_cancel(&timer->bg_timer);
+}
+
+void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
+{
+ struct arch_timer_cpu *timer = vcpu_timer(vcpu);
+
+ soft_timer_cancel(&timer->bg_timer);
+}
+
static void kvm_timer_update_status(struct arch_timer_context *ctx, bool level)
{
/*
@@ -612,13 +627,6 @@ static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
}
-static void kvm_timer_unblocking(struct kvm_vcpu *vcpu)
-{
- struct arch_timer_cpu *timer = vcpu_timer(vcpu);
-
- soft_timer_cancel(&timer->bg_timer);
-}
-
static void timer_restore_state(struct arch_timer_context *ctx)
{
struct arch_timer_cpu *timer = vcpu_timer(timer_context_to_vcpu(ctx));
@@ -1520,13 +1528,6 @@ int __init kvm_timer_hyp_init(bool has_gic)
return err;
}
-void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
-{
- struct arch_timer_cpu *timer = vcpu_timer(vcpu);
-
- soft_timer_cancel(&timer->bg_timer);
-}
-
static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
{
u32 ppis = 0;
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 4d44b638b7d3..0d4d4849564c 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -901,6 +901,11 @@ static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)
return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_SUSPENDED;
}
+static bool vcpu_can_run(struct kvm_vcpu *vcpu)
+{
+ return !kvm_arm_vcpu_stopped(vcpu) && !atomic_read(&vcpu->arch.pause);
+}
+
/* Only the guest's CPU_ON may start a vCPU EL2 holds powered off. */
bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu)
{
@@ -953,11 +958,6 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
return ret;
}
-static bool vcpu_can_run(struct kvm_vcpu *vcpu)
-{
- return !kvm_arm_vcpu_stopped(vcpu) && !atomic_read(&vcpu->arch.pause);
-}
-
/**
* kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled
* @v: The VCPU pointer
diff --git a/include/kvm/arm_hypercalls.h b/include/kvm/arm_hypercalls.h
index 2df152207ccd..5ce4c2d43f9a 100644
--- a/include/kvm/arm_hypercalls.h
+++ b/include/kvm/arm_hypercalls.h
@@ -4,7 +4,10 @@
#ifndef __KVM_ARM_HYPERCALLS_H
#define __KVM_ARM_HYPERCALLS_H
+/* s390 provides its own header view */
+#ifndef __s390__
#include <asm/kvm_emulate.h>
+#endif // !__s390__
int kvm_smccc_call_handler(struct kvm_vcpu *vcpu);
diff --git a/include/kvm/arm_psci.h b/include/kvm/arm_psci.h
index 06c20612e9e7..f358ae19267b 100644
--- a/include/kvm/arm_psci.h
+++ b/include/kvm/arm_psci.h
@@ -10,7 +10,10 @@
#include <linux/kvm_host.h>
#include <uapi/linux/psci.h>
+/* s390 provides its own header view */
+#ifndef __s390__
#include <asm/kvm_emulate.h>
+#endif // !__s390__
#define KVM_ARM_PSCI_0_1 PSCI_VERSION(0, 1)
#define KVM_ARM_PSCI_0_2 PSCI_VERSION(0, 2)
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 03/12] KVM: arm64: Share timer and smccc code with s390
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
2026-12-02 14:11 ` [PATCH 01/12] arm64: Prepare for sharing with s390 Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 05/12] KVM: s390: arm64: Shadow the guest timer CVAL registers Steffen Eiden
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
Share the whole smccc related files. s390 will use the whole unmodified
infrastructure. Share useful timer related functions. As timers work
differntly on s390 only a subset can be reused. Share functions for
managing SMP guests.
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/arm64/kvm/arch_timer.c | 8 ++++++++
arch/arm64/kvm/arm.c | 15 +++++++++++++++
arch/arm64/kvm/hypercalls.c | 4 ++++
arch/arm64/kvm/psci.c | 3 +++
arch/arm64/kvm/pvtime.c | 2 ++
arch/arm64/kvm/trng.c | 2 ++
6 files changed, 34 insertions(+)
diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c
index 2de8ce48a7a0..626cfa71133a 100644
--- a/arch/arm64/kvm/arch_timer.c
+++ b/arch/arm64/kvm/arch_timer.c
@@ -248,6 +248,8 @@ static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu)
vcpu_get_flag(vcpu, IN_WFIT));
}
+#if ARM64_S390_COMMON == 1
+
static int nr_timers(struct kvm_vcpu *vcpu)
{
if (!vcpu_has_nv(vcpu))
@@ -314,6 +316,8 @@ static u64 kvm_timer_earliest_exp(struct kvm_vcpu *vcpu)
return min_delta;
}
+#endif /* ARM64_S390_COMMON == 1 */
+
static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
{
struct arch_timer_cpu *timer;
@@ -406,6 +410,8 @@ static bool kvm_timer_pending(struct arch_timer_context *timer_ctx)
return cval <= now;
}
+#if ARM64_S390_COMMON == 1
+
int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
{
return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0;
@@ -451,6 +457,8 @@ void kvm_timer_vcpu_terminate(struct kvm_vcpu *vcpu)
soft_timer_cancel(&timer->bg_timer);
}
+#endif /* ARM64_S390_COMMON == 1 */
+
static void kvm_timer_update_status(struct arch_timer_context *ctx, bool level)
{
/*
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 0d4d4849564c..4b52dfdcec48 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -318,6 +318,7 @@ void kvm_arch_create_vm_debugfs(struct kvm *kvm)
kvm_s2_ptdump_create_debugfs(kvm);
}
+#if ARM64_S390_COMMON == 1
static void kvm_destroy_mpidr_data(struct kvm *kvm)
{
struct kvm_mpidr_data *data;
@@ -334,6 +335,7 @@ static void kvm_destroy_mpidr_data(struct kvm *kvm)
mutex_unlock(&kvm->arch.config_lock);
}
+#endif /* ARM64_S390_COMMON == 1 */
/**
* kvm_arch_destroy_vm - destroy the VM data structure
@@ -865,6 +867,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
vcpu->cpu = -1;
}
+#if ARM64_S390_COMMON == 1
static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu, u32 mp_state)
{
WRITE_ONCE(vcpu->arch.mp_state.mp_state, mp_state);
@@ -912,6 +915,8 @@ bool kvm_pkvm_vcpu_is_powered_off(struct kvm_vcpu *vcpu)
return READ_ONCE(vcpu->arch.mp_state.mp_state) == KVM_MP_STATE_UNINITIALIZED;
}
+#endif /* ARM64_S390_COMMON == 1 */
+
int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
struct kvm_mp_state *mp_state)
{
@@ -981,6 +986,7 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
return vcpu_mode_priv(vcpu);
}
+#if ARM64_S390_COMMON == 1
#ifdef CONFIG_GUEST_PERF_EVENTS
unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
{
@@ -1040,6 +1046,8 @@ static void kvm_init_mpidr_data(struct kvm *kvm)
mutex_unlock(&kvm->arch.config_lock);
}
+#endif /* ARM64_S390_COMMON == 1 */
+
/*
* Handle both the initialisation that is being done when the vcpu is
* run for the first time, as well as the updates that must be
@@ -1164,6 +1172,7 @@ void kvm_arm_resume_guest(struct kvm *kvm)
kvm_resume_vcpu(vcpu);
}
+#if ARM64_S390_COMMON == 1
static void kvm_vcpu_sleep(struct kvm_vcpu *vcpu)
{
struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
@@ -1249,6 +1258,7 @@ static int kvm_vcpu_suspend(struct kvm_vcpu *vcpu)
*/
return 1;
}
+#endif /* ARM64_S390_COMMON == 1 */
/**
* check_vcpu_requests - check and handle pending vCPU requests
@@ -1607,6 +1617,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
return ret;
}
+#if ARM64_S390_COMMON == 1
static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
{
int bit_index;
@@ -1641,6 +1652,8 @@ static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level)
return 0;
}
+#endif /* ARM64_S390_COMMON == 1 */
+
int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
bool line_status)
{
@@ -3157,6 +3170,7 @@ static int __init init_hyp_mode(void)
return err;
}
+#if ARM64_S390_COMMON == 1
struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
{
struct kvm_vcpu *vcpu = NULL;
@@ -3187,6 +3201,7 @@ struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr)
}
return NULL;
}
+#endif /* ARM64_S390_COMMON == 1 */
bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
{
diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c
index c00b0044ef11..1314e2e11ac1 100644
--- a/arch/arm64/kvm/hypercalls.c
+++ b/arch/arm64/kvm/hypercalls.c
@@ -67,6 +67,8 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val)
val[3] = lower_32_bits(cycles);
}
+#if ARM64_S390_COMMON == 1
+
static bool kvm_smccc_default_allowed(u32 func_id)
{
switch (func_id) {
@@ -691,3 +693,5 @@ int kvm_vm_smccc_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
return -ENXIO;
}
}
+
+#endif /* ARM64_S390_COMMON == 1 */
diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c
index b01f8d44033f..582dbf4d6c00 100644
--- a/arch/arm64/kvm/psci.c
+++ b/arch/arm64/kvm/psci.c
@@ -16,6 +16,7 @@
#include <kvm/arm_psci.h>
#include <kvm/arm_hypercalls.h>
+#if ARM64_S390_COMMON == 1
/*
* This is an implementation of the Power State Coordination Interface
* as described in ARM document number ARM DEN 0022A.
@@ -484,3 +485,5 @@ int kvm_psci_call(struct kvm_vcpu *vcpu)
return 1;
}
}
+
+#endif /* ARM64_S390_COMMON == 1 */
diff --git a/arch/arm64/kvm/pvtime.c b/arch/arm64/kvm/pvtime.c
index 053fe831fa54..84e9b1937725 100644
--- a/arch/arm64/kvm/pvtime.c
+++ b/arch/arm64/kvm/pvtime.c
@@ -10,6 +10,7 @@
#include <kvm/arm_hypercalls.h>
+#if ARM64_S390_COMMON == 1
void kvm_update_stolen_time(struct kvm_vcpu *vcpu)
{
struct kvm *kvm = vcpu->kvm;
@@ -131,3 +132,4 @@ int kvm_arm_pvtime_has_attr(struct kvm_vcpu *vcpu,
}
return -ENXIO;
}
+#endif /* ARM64_S390_COMMON == 1 */
diff --git a/arch/arm64/kvm/trng.c b/arch/arm64/kvm/trng.c
index 99bdd7103c9c..fbec1329d953 100644
--- a/arch/arm64/kvm/trng.c
+++ b/arch/arm64/kvm/trng.c
@@ -8,6 +8,7 @@
#include <kvm/arm_hypercalls.h>
+#if ARM64_S390_COMMON == 1
#define ARM_SMCCC_TRNG_VERSION_1_0 0x10000UL
/* Those values are deliberately separate from the generic SMCCC definitions. */
@@ -83,3 +84,4 @@ int kvm_trng_call(struct kvm_vcpu *vcpu)
smccc_set_retval(vcpu, val, 0, 0, 0);
return 1;
}
+#endif /* ARM64_S390_COMMON == 1 */
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 05/12] KVM: s390: arm64: Shadow the guest timer CVAL registers
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
2026-12-02 14:11 ` [PATCH 01/12] arm64: Prepare for sharing with s390 Steffen Eiden
2026-12-02 14:11 ` [PATCH 03/12] KVM: arm64: Share timer and smccc code " Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 06/12] KVM: s390: arm64: Use mpstate for guests Steffen Eiden
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
The timer registers {CNTP,CNTV}_CVAL_EL0 so far live solely in the save
area and are only accessible through EASR/SASR, which is valid only from
the vCPU thread while it owns the guest state. The accessors assert that
a loaded vCPU is the currently running one, and an EASR read issued from
any other CPU returns that CPU's registers instead of the guest's.
The timer emulation added later in this series reads CVAL from a hard
hrtimer callback that can fire on any CPU, concurrently with the vCPU
being loaded or put elsewhere. Give both registers plain memory shadows
in struct kvm_cpu_context so that guest timer state is readable from any
context.
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/s390/include/asm/kvm_host_arm64.h | 8 ++++++--
arch/s390/kvm/arm64/arm.c | 4 ++++
arch/s390/kvm/arm64/sys_regs.c | 8 ++++----
3 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/arch/s390/include/asm/kvm_host_arm64.h b/arch/s390/include/asm/kvm_host_arm64.h
index a76e1bfc64bb..3d9284e1c511 100644
--- a/arch/s390/include/asm/kvm_host_arm64.h
+++ b/arch/s390/include/asm/kvm_host_arm64.h
@@ -68,6 +68,10 @@ struct kvm_cpu_context {
/* shadowed sysregs to comply with arm64 infrastructure */
u64 elr_el1;
u64 spsr_el1;
+
+ /* shadowed timer sysregs */
+ u64 cntp_cval_el0;
+ u64 cntv_cval_el0;
};
struct kvm_vcpu_arch {
@@ -329,6 +333,8 @@ enum vcpu_sysreg {
MPIDR_EL1,
ELR_EL1,
SPSR_EL1,
+ CNTP_CVAL_EL0,
+ CNTV_CVAL_EL0,
),
/* EL 0,1 register from save area in order of appearance */
@@ -337,8 +343,6 @@ enum vcpu_sysreg {
AFSR0_EL1,
AFSR1_EL1,
CNTFRQ_EL0,
- CNTP_CVAL_EL0,
- CNTV_CVAL_EL0,
DISR_EL1,
MIDR_EL1,
OSLSR_EL1,
diff --git a/arch/s390/kvm/arm64/arm.c b/arch/s390/kvm/arm64/arm.c
index 2ca2d7e18032..61f0078e82d1 100644
--- a/arch/s390/kvm/arm64/arm.c
+++ b/arch/s390/kvm/arm64/arm.c
@@ -454,12 +454,16 @@ static void kvm_arm_restore_shadowed_regs(struct kvm_vcpu *vcpu)
vcpu_write_host_sys_reg(vcpu, vcpu->arch.mpidr, SYS_VMPIDR_EL2);
_vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.elr_el1, SYS_ELR_EL1);
_vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.spsr_el1, SYS_SPSR_EL1);
+ _vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.cntp_cval_el0, SYS_CNTP_CVAL_EL0);
+ _vcpu_write_sys_reg(vcpu, vcpu->arch.ctxt.cntv_cval_el0, SYS_CNTV_CVAL_EL0);
}
static void kvm_arm_save_shadowed_regs(struct kvm_vcpu *vcpu)
{
vcpu->arch.ctxt.elr_el1 = _vcpu_read_sys_reg(vcpu, SYS_ELR_EL1);
vcpu->arch.ctxt.spsr_el1 = _vcpu_read_sys_reg(vcpu, SYS_SPSR_EL1);
+ vcpu->arch.ctxt.cntp_cval_el0 = _vcpu_read_sys_reg(vcpu, SYS_CNTP_CVAL_EL0);
+ vcpu->arch.ctxt.cntv_cval_el0 = _vcpu_read_sys_reg(vcpu, SYS_CNTV_CVAL_EL0);
}
/** kvm_arch_vcpu_ioctl_run() - run arm64 vCPU
diff --git a/arch/s390/kvm/arm64/sys_regs.c b/arch/s390/kvm/arm64/sys_regs.c
index 50979e29fec4..11a60a74f90f 100644
--- a/arch/s390/kvm/arm64/sys_regs.c
+++ b/arch/s390/kvm/arm64/sys_regs.c
@@ -77,6 +77,8 @@ static __always_inline u64 read_special_sr(const struct kvm_vcpu *vcpu,
case CNTV_CTL_EL0: return vcpu->arch.sae_block.cntv_ctl;
case CONTEXTIDR_EL1: return vcpu->arch.sae_block.contextidr_el1;
case SP_EL1: return vcpu->arch.sae_block.sp_el1;
+ case CNTP_CVAL_EL0: return vcpu->arch.ctxt.cntp_cval_el0;
+ case CNTV_CVAL_EL0: return vcpu->arch.ctxt.cntv_cval_el0;
default:
WARN(true, "%s wants to read non-special register %x", __func__, reg);
return SR_INVALID;
@@ -91,8 +93,6 @@ static __always_inline u64 read_sr_from_vcpu(const struct kvm_vcpu *vcpu,
case AFSR0_EL1: return _vcpu_read_sys_reg(vcpu, SYS_AFSR0_EL1);
case AFSR1_EL1: return _vcpu_read_sys_reg(vcpu, SYS_AFSR1_EL1);
case CNTFRQ_EL0: return _vcpu_read_sys_reg(vcpu, SYS_CNTFRQ_EL0);
- case CNTP_CVAL_EL0: return _vcpu_read_sys_reg(vcpu, SYS_CNTP_CVAL_EL0);
- case CNTV_CVAL_EL0: return _vcpu_read_sys_reg(vcpu, SYS_CNTV_CVAL_EL0);
case DISR_EL1: return _vcpu_read_sys_reg(vcpu, SYS_DISR_EL1);
case MIDR_EL1: return _vcpu_read_sys_reg(vcpu, SYS_MIDR_EL1);
case OSLSR_EL1: return _vcpu_read_sys_reg(vcpu, SYS_OSLSR_EL1);
@@ -160,6 +160,8 @@ static __always_inline void write_special_sr(struct kvm_vcpu *vcpu, u64 val,
case SP_EL1: vcpu->arch.sae_block.sp_el1 = val; break;
case ELR_EL1: vcpu->arch.ctxt.elr_el1 = val; break;
case SPSR_EL1: vcpu->arch.ctxt.spsr_el1 = val; break;
+ case CNTP_CVAL_EL0: vcpu->arch.ctxt.cntp_cval_el0 = val; break;
+ case CNTV_CVAL_EL0: vcpu->arch.ctxt.cntv_cval_el0 = val; break;
default:
WARN(true, "%s wants to write non-special register %x", __func__, reg);
}
@@ -173,8 +175,6 @@ static __always_inline void write_sr_to_vcpu(struct kvm_vcpu *vcpu, u64 val,
case AFSR0_EL1: _vcpu_write_sys_reg(vcpu, val, SYS_AFSR0_EL1); break;
case AFSR1_EL1: _vcpu_write_sys_reg(vcpu, val, SYS_AFSR1_EL1); break;
case CNTFRQ_EL0: _vcpu_write_sys_reg(vcpu, val, SYS_CNTFRQ_EL0); break;
- case CNTP_CVAL_EL0: _vcpu_write_sys_reg(vcpu, val, SYS_CNTP_CVAL_EL0); break;
- case CNTV_CVAL_EL0: _vcpu_write_sys_reg(vcpu, val, SYS_CNTV_CVAL_EL0); break;
case DISR_EL1: _vcpu_write_sys_reg(vcpu, val, SYS_DISR_EL1); break;
case MIDR_EL1: _vcpu_write_sys_reg(vcpu, val, SYS_MIDR_EL1); break;
case PAR_EL1: _vcpu_write_sys_reg(vcpu, val, SYS_PAR_EL1); break;
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 06/12] KVM: s390: arm64: Use mpstate for guests
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
` (2 preceding siblings ...)
2026-12-02 14:11 ` [PATCH 05/12] KVM: s390: arm64: Shadow the guest timer CVAL registers Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 07/12] KVM: s390: arm64: Enable full smccc support Steffen Eiden
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
Implement the necessary plumbing to manage the mp state of an arm64
guest.
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/s390/include/asm/kvm_host_arm64.h | 3 ++-
arch/s390/kvm/arm64/arm.c | 37 ++++++++++++++++++++++++++++++++--
2 files changed, 37 insertions(+), 3 deletions(-)
diff --git a/arch/s390/include/asm/kvm_host_arm64.h b/arch/s390/include/asm/kvm_host_arm64.h
index 3d9284e1c511..cdaf8c3a0d47 100644
--- a/arch/s390/include/asm/kvm_host_arm64.h
+++ b/arch/s390/include/asm/kvm_host_arm64.h
@@ -37,7 +37,8 @@ bool cpus_have_final_cap(unsigned int num);
#define KVM_S390_ARM64_IMPL_FEATURES ( \
BIT(KVM_ARM_VCPU_PTRAUTH_ADDRESS) | \
BIT(KVM_ARM_VCPU_PTRAUTH_GENERIC) | \
- BIT(KVM_ARM_VCPU_SVE) \
+ BIT(KVM_ARM_VCPU_SVE) | \
+ BIT(KVM_ARM_VCPU_POWER_OFF) \
)
#define KVM_HALT_POLL_NS_DEFAULT 50000
diff --git a/arch/s390/kvm/arm64/arm.c b/arch/s390/kvm/arm64/arm.c
index 61f0078e82d1..483f29665524 100644
--- a/arch/s390/kvm/arm64/arm.c
+++ b/arch/s390/kvm/arm64/arm.c
@@ -52,6 +52,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_IOEVENTFD:
case KVM_CAP_PRE_FAULT_MEMORY:
case KVM_CAP_ONE_REG:
+ case KVM_CAP_MP_STATE:
ret = 1;
break;
case KVM_CAP_ARM_SVE:
@@ -175,6 +176,7 @@ long kvm_arch_dev_ioctl(struct file *filp,
void kvm_arch_destroy_vm(struct kvm *kvm)
{
+ kvm_destroy_mpidr_data(kvm);
kvm_destroy_vcpus(kvm);
debug_unregister(kvm->arch.dbf);
kvm->arch.gmap = gmap_put(kvm->arch.gmap);
@@ -212,6 +214,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
sae_block->save_area = virt_to_phys(save_area);
sae_block->gpto = vcpu->kvm->arch.epd;
+ kvm_destroy_mpidr_data(vcpu->kvm);
VM_EVENT(vcpu->kvm, 3,
"create cpu %d at 0x%p, sae block at 0x%p, satellite at 0x%p",
vcpu->vcpu_id, vcpu, &vcpu->arch.sae_block,
@@ -267,7 +270,25 @@ int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
struct kvm_mp_state *mp_state)
{
- return -EINVAL;
+ int ret = 0;
+
+ scoped_guard(spinlock, &vcpu->arch.mp_state_lock) {
+ switch (mp_state->mp_state) {
+ case KVM_MP_STATE_RUNNABLE:
+ WRITE_ONCE(vcpu->arch.mp_state, *mp_state);
+ break;
+ case KVM_MP_STATE_STOPPED:
+ __kvm_arm_vcpu_power_off(vcpu, KVM_MP_STATE_STOPPED);
+ break;
+ case KVM_MP_STATE_SUSPENDED:
+ kvm_arm_vcpu_suspend(vcpu);
+ break;
+ default:
+ ret = -EINVAL;
+ }
+ }
+
+ return ret;
}
int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
@@ -309,6 +330,8 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)
if (likely(READ_ONCE(vcpu->pid)))
return 0;
+ kvm_init_mpidr_data(kvm);
+
ret = kvm_vcpu_finalize_sys_regs(vcpu);
if (ret)
return ret;
@@ -403,16 +426,26 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
{
struct kvm_sae_save_area *save_area = &vcpu->arch.save_area;
struct kvm_sae_block *sae_block = &vcpu->arch.sae_block;
+ bool power_off = false;
int ret;
sae_block->save_area = virt_to_phys(save_area);
+ /* Limit KVM_ARM_VCPU_POWER_OFF's scope to a single KVM_ARM_VCPU_INIT call */
+ if (init->features[0] & BIT(KVM_ARM_VCPU_POWER_OFF)) {
+ init->features[0] &= ~BIT(KVM_ARM_VCPU_POWER_OFF);
+ power_off = true;
+ }
+
ret = kvm_vcpu_set_target(vcpu, init);
if (ret)
return ret;
scoped_guard(spinlock, &vcpu->arch.mp_state_lock) {
- WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
+ if (power_off)
+ __kvm_arm_vcpu_power_off(vcpu, KVM_MP_STATE_STOPPED);
+ else
+ WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_RUNNABLE);
}
return 0;
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 07/12] KVM: s390: arm64: Enable full smccc support
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
` (3 preceding siblings ...)
2026-12-02 14:11 ` [PATCH 06/12] KVM: s390: arm64: Use mpstate for guests Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 09/12] KVM: s390: arm64: Provide timer infrastructure Steffen Eiden
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
Use the shared arm64 code for handling smccc's.
Implement the final plumbing for the host to process these.
Remove the primitive SYSTEM_OFF implementation.
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/s390/include/asm/kvm_host_arm64.h | 33 +++++++++++++++-
arch/s390/kvm/Kconfig | 1 +
arch/s390/kvm/arm64/Makefile | 6 ++-
arch/s390/kvm/arm64/Makefile.gen | 6 ++-
arch/s390/kvm/arm64/arm.c | 72 +++++++++++++++++++++++++++++++++-
arch/s390/kvm/arm64/handle_exit.c | 49 ++++++++---------------
arch/s390/kvm/arm64/hypercalls.c | 32 +++++++++++++++
arch/s390/kvm/arm64/psci.c | 20 ++++++++++
arch/s390/kvm/arm64/pvtime.c | 15 +++++++
arch/s390/kvm/arm64/reset.c | 4 ++
arch/s390/kvm/arm64/trace.h | 20 ++++++++++
arch/s390/kvm/arm64/trng.c | 12 ++++++
arch/s390/tools/Makefile.arm64h | 2 +
13 files changed, 235 insertions(+), 37 deletions(-)
diff --git a/arch/s390/include/asm/kvm_host_arm64.h b/arch/s390/include/asm/kvm_host_arm64.h
index cdaf8c3a0d47..9bde7c56f08a 100644
--- a/arch/s390/include/asm/kvm_host_arm64.h
+++ b/arch/s390/include/asm/kvm_host_arm64.h
@@ -10,10 +10,13 @@
#define vcpu_gp_regs(v) ((v)->arch.sae_block.gpr)
enum vcpu_sysreg;
+struct kvm_arch;
+static inline bool __vcpu_has_feature(const struct kvm_arch *ka, int feature);
#include <arm64/cputype.h>
#include <arm64/kvm_host.h>
#include <arm64/ptrace.h>
+#include <arm64/kvm_hyp.h>
#include <asm/sae.h>
@@ -30,15 +33,19 @@ enum {
bool cpus_have_final_cap(unsigned int num);
+#include <kvm/arm_arch_timer.h>
+
#define KVM_HAVE_MMU_RWLOCK
-#define KVM_MAX_VCPUS 1
+/* GICv2 maximum TODO change with in-kernel gic support*/
+#define KVM_MAX_VCPUS 8
#define __KVM_HAVE_VCPU_RUN_PID_CHANGE
#define KVM_S390_ARM64_IMPL_FEATURES ( \
BIT(KVM_ARM_VCPU_PTRAUTH_ADDRESS) | \
BIT(KVM_ARM_VCPU_PTRAUTH_GENERIC) | \
BIT(KVM_ARM_VCPU_SVE) | \
- BIT(KVM_ARM_VCPU_POWER_OFF) \
+ BIT(KVM_ARM_VCPU_POWER_OFF) | \
+ BIT(KVM_ARM_VCPU_PSCI_0_2) \
)
#define KVM_HALT_POLL_NS_DEFAULT 50000
@@ -122,6 +129,12 @@ struct kvm_vcpu_arch {
struct kvm_s390_mmu_cache *mc;
void *debugfs_state_data;
+
+ /* Guest PV state */
+ struct {
+ u64 last_steal;
+ gpa_t base;
+ } steal;
};
struct kvm_vcpu_stat {
@@ -167,6 +180,12 @@ struct kvm_arch {
/* Per-VM ID register storage */
struct kvm_vm_id_regs id_regs;
+ /* Hypercall features firmware registers' descriptor */
+ struct kvm_smccc_features smccc_feat;
+ struct maple_tree smccc_filter;
+ /* Mandated version of PSCI */
+ u32 psci_version;
+
struct kvm_mpidr_data *mpidr_data;
};
@@ -398,6 +417,16 @@ static __always_inline u64 *___ctxt_sys_reg(struct kvm_cpu_context *ctxt,
void vcpu_write_host_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg);
u64 vcpu_read_host_sys_reg(const struct kvm_vcpu *vcpu, int reg);
+static inline void kvm_arm_pvtime_vcpu_init(struct kvm_vcpu_arch *vcpu_arch)
+{
+ vcpu_arch->steal.base = INVALID_GPA;
+}
+
+static inline bool kvm_arm_is_pvtime_enabled(struct kvm_vcpu_arch *vcpu_arch)
+{
+ return (vcpu_arch->steal.base != INVALID_GPA);
+}
+
#define kvm_debug_handle_oslar(_v, _val) /* debug not implemented yet*/
#define kvm_vm_has_ran_once(kvm) \
diff --git a/arch/s390/kvm/Kconfig b/arch/s390/kvm/Kconfig
index e7b525d04c6d..6d0852069f79 100644
--- a/arch/s390/kvm/Kconfig
+++ b/arch/s390/kvm/Kconfig
@@ -29,6 +29,7 @@ config KVM
select VIRT_XFER_TO_GUEST_WORK
select KVM_MMU_LOCKLESS_AGING
select KVM_GENERIC_PRE_FAULT_MEMORY
+ select SCHED_INFO
help
Enable support for hosting virtualized, hardware-accelerated
virtual machines via two modules: kvm and kvm_arm64.
diff --git a/arch/s390/kvm/arm64/Makefile b/arch/s390/kvm/arm64/Makefile
index b0f2da6ac482..dc3d991fb4e2 100644
--- a/arch/s390/kvm/arm64/Makefile
+++ b/arch/s390/kvm/arm64/Makefile
@@ -15,13 +15,17 @@ kvm-arm64-local := \
feature.o \
guest.o \
handle_exit.o \
+ hypercalls.o \
inject_fault.o \
mmio.o \
mmu.o \
+ psci.o \
+ pvtime.o \
qaaf.o \
reset.o \
spectre.o \
- sys_regs.o
+ sys_regs.o \
+ trng.o
kvm-arm64-obj := $(kvm-arm64-local)
kvm-arm64-obj += $(patsubst %.o,%-arm64.o,$(kvm-y))
diff --git a/arch/s390/kvm/arm64/Makefile.gen b/arch/s390/kvm/arm64/Makefile.gen
index 6c62ac412d57..6faf2e66ac8d 100644
--- a/arch/s390/kvm/arm64/Makefile.gen
+++ b/arch/s390/kvm/arm64/Makefile.gen
@@ -7,8 +7,12 @@
ARM64_CFILES := \
arm.c \
guest.c \
+ hypercalls.c \
mmio.c \
- sys_regs.c
+ psci.c \
+ pvtime.c \
+ sys_regs.c \
+ trng.c
quiet_cmd_extract_inc = GEN $@
cmd_extract_inc = mkdir -p $(dir $@); \
diff --git a/arch/s390/kvm/arm64/arm.c b/arch/s390/kvm/arm64/arm.c
index 483f29665524..7212ea1fc141 100644
--- a/arch/s390/kvm/arm64/arm.c
+++ b/arch/s390/kvm/arm64/arm.c
@@ -17,6 +17,8 @@
#include <arm64/sysreg.h>
#include <arm64/sys_regs.h>
+#include <kvm/arm_hypercalls.h>
+
#include <gmap.h>
#include <kvm_mmu.h>
@@ -44,7 +46,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_NR_VCPUS:
case KVM_CAP_MAX_VCPUS:
case KVM_CAP_MAX_VCPU_ID:
- ret = KVM_MAX_VCPUS;
+ ret = min(kvm_sae_max_vcpus(), KVM_MAX_VCPUS);
break;
case KVM_CAP_ARM_VM_IPA_SIZE:
ret = get_kvm_ipa_limit();
@@ -53,6 +55,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_PRE_FAULT_MEMORY:
case KVM_CAP_ONE_REG:
case KVM_CAP_MP_STATE:
+ case KVM_CAP_ARM_PSCI:
+ case KVM_CAP_ARM_PSCI_0_2:
ret = 1;
break;
case KVM_CAP_ARM_SVE:
@@ -62,6 +66,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_ARM_PTRAUTH_GENERIC:
ret = kvm_has_full_ptr_auth();
break;
+ case KVM_CAP_STEAL_TIME:
+ ret = kvm_arm_pvtime_supported(kvm);
+ break;
default:
ret = 0;
}
@@ -154,6 +161,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
kvm->arch.epd = ptff_qagto(0);
+ kvm_arm_init_hypercalls(kvm);
+
VM_EVENT(kvm, 3, "vm created with type %lu", type);
return 0;
@@ -180,6 +189,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
kvm_destroy_vcpus(kvm);
debug_unregister(kvm->arch.dbf);
kvm->arch.gmap = gmap_put(kvm->arch.gmap);
+ kvm_arm_teardown_hypercalls(kvm);
}
u32 get_kvm_ipa_limit(void)
@@ -189,6 +199,9 @@ u32 get_kvm_ipa_limit(void)
int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
{
+ if (id >= kvm->max_vcpus)
+ return -EINVAL;
+
return 0;
}
@@ -215,6 +228,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
sae_block->gpto = vcpu->kvm->arch.epd;
kvm_destroy_mpidr_data(vcpu->kvm);
+ kvm_arm_pvtime_vcpu_init(&vcpu->arch);
+
VM_EVENT(vcpu->kvm, 3,
"create cpu %d at 0x%p, sae block at 0x%p, satellite at 0x%p",
vcpu->vcpu_id, vcpu, &vcpu->arch.sae_block,
@@ -250,6 +265,9 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
vcpu->arch.sae_block.gpto = vcpu->kvm->arch.epd;
vcpu_set_pauth_traps(vcpu);
+
+ if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
+ kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
}
void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
@@ -362,6 +380,8 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
if (kvm_request_pending(vcpu)) {
if (kvm_test_request(KVM_REQ_VM_DEAD, vcpu))
return -EIO;
+ if (kvm_check_request(KVM_REQ_SLEEP, vcpu))
+ kvm_vcpu_sleep(vcpu);
if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu))
kvm_reset_vcpu(vcpu);
/*
@@ -372,6 +392,12 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
asce_flush_tlb(vcpu->kvm->arch.gmap->asce);
+
+ if (kvm_check_request(KVM_REQ_RECORD_STEAL, vcpu))
+ kvm_update_stolen_time(vcpu);
+
+ if (kvm_check_request(KVM_REQ_SUSPEND, vcpu))
+ return kvm_vcpu_suspend(vcpu);
}
return 1;
@@ -601,6 +627,9 @@ static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu,
int ret;
switch (attr->group) {
+ case KVM_ARM_VCPU_PVTIME_CTRL:
+ ret = kvm_arm_pvtime_set_attr(vcpu, attr);
+ break;
default:
ret = -ENXIO;
break;
@@ -615,6 +644,9 @@ static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu,
int ret;
switch (attr->group) {
+ case KVM_ARM_VCPU_PVTIME_CTRL:
+ ret = kvm_arm_pvtime_get_attr(vcpu, attr);
+ break;
default:
ret = -ENXIO;
break;
@@ -629,6 +661,9 @@ static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu,
int ret;
switch (attr->group) {
+ case KVM_ARM_VCPU_PVTIME_CTRL:
+ ret = kvm_arm_pvtime_has_attr(vcpu, attr);
+ break;
default:
ret = -ENXIO;
break;
@@ -785,9 +820,31 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
return dat_test_age_gfn(kvm->arch.gmap->asce, range->start, range->end);
}
+static int kvm_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
+{
+ switch (attr->group) {
+ case KVM_ARM_VM_SMCCC_CTRL:
+ return kvm_vm_smccc_has_attr(kvm, attr);
+ default:
+ return -ENXIO;
+ }
+}
+
+static int kvm_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
+{
+ switch (attr->group) {
+ case KVM_ARM_VM_SMCCC_CTRL:
+ return kvm_vm_smccc_set_attr(kvm, attr);
+ default:
+ return -ENXIO;
+ }
+}
+
int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
{
void __user *argp = (void __user *)arg;
+ struct kvm *kvm = filp->private_data;
+ struct kvm_device_attr attr;
switch (ioctl) {
case KVM_ARM_PREFERRED_TARGET: {
@@ -799,6 +856,19 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
return -EFAULT;
return 0;
+ case KVM_HAS_DEVICE_ATTR: {
+ if (copy_from_user(&attr, argp, sizeof(attr)))
+ return -EFAULT;
+
+ return kvm_vm_has_attr(kvm, &attr);
+ }
+ case KVM_SET_DEVICE_ATTR: {
+ if (copy_from_user(&attr, argp, sizeof(attr)))
+ return -EFAULT;
+
+ return kvm_vm_set_attr(kvm, &attr);
+ }
+
}
default:
diff --git a/arch/s390/kvm/arm64/handle_exit.c b/arch/s390/kvm/arm64/handle_exit.c
index 1653163ed6d6..a48c039fbf3f 100644
--- a/arch/s390/kvm/arm64/handle_exit.c
+++ b/arch/s390/kvm/arm64/handle_exit.c
@@ -4,46 +4,18 @@
#include <arm64/esr.h>
#include <arm64/kvm_emulate.h>
+#include <kvm/arm_hypercalls.h>
#include "handle_exit.h"
typedef int (*exit_handle_fn)(struct kvm_vcpu *);
-#define PSCI_0_2_FN_SYSTEM_OFF 0x84000008
-#define PSCI_RET_NOT_SUPPORTED -1
-#define PSCI_RET_INTERNAL_FAILURE -6
-/*
- * Temporary smc/hvc handler. Non-compliant implementation (features missing).
- * Implements only system off so that test programs are able to end their execution.
- * Multi-vCPU power state management and KVM_REQ_SLEEP handling are deferred
- * to future SMP series.
- */
-static int kvm_smccc_call_handler(struct kvm_vcpu *vcpu)
-{
- u32 func_id = vcpu_get_reg(vcpu, 0);
- u64 val = PSCI_RET_NOT_SUPPORTED;
- int ret = 1;
-
- if (func_id == PSCI_0_2_FN_SYSTEM_OFF) {
- scoped_guard(spinlock, &vcpu->arch.mp_state_lock) {
- WRITE_ONCE(vcpu->arch.mp_state.mp_state, KVM_MP_STATE_STOPPED);
- }
- memset(&vcpu->run->system_event, 0,
- sizeof(vcpu->run->system_event));
- vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SHUTDOWN;
- vcpu->run->system_event.ndata = 1;
- vcpu->run->system_event.data[0] = 0;
- vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
- val = PSCI_RET_INTERNAL_FAILURE;
- ret = 0;
- }
- vcpu_set_reg(vcpu, 0, val);
-
- return ret;
-}
+#include "trace.h"
static int handle_hvc(struct kvm_vcpu *vcpu)
{
+ trace_kvm_hvc_arm64(*vcpu_pc(vcpu), vcpu_get_reg(vcpu, 0),
+ kvm_vcpu_hvc_get_imm(vcpu));
vcpu->stat.hvc_exit_stat++;
return kvm_smccc_call_handler(vcpu);
}
@@ -59,8 +31,21 @@ static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
return 1;
}
+static int handle_smc(struct kvm_vcpu *vcpu)
+{
+ kvm_incr_pc(vcpu);
+
+ if (kvm_vcpu_hvc_get_imm(vcpu)) {
+ vcpu_set_reg(vcpu, 0, ~0UL);
+ return 1;
+ }
+
+ return kvm_smccc_call_handler(vcpu);
+}
+
static exit_handle_fn arm_exit_handlers[] = {
[0 ... ESR_ELx_EC_MAX] = kvm_handle_unknown_ec,
+ [ESR_ELx_EC_SMC64] = handle_smc,
[ESR_ELx_EC_HVC64] = handle_hvc,
[ESR_ELx_EC_SYS64] = kvm_handle_sys_reg,
[ESR_ELx_EC_IABT_LOW] = kvm_handle_guest_abort,
diff --git a/arch/s390/kvm/arm64/hypercalls.c b/arch/s390/kvm/arm64/hypercalls.c
new file mode 100644
index 000000000000..b1ad8ad5e00b
--- /dev/null
+++ b/arch/s390/kvm/arm64/hypercalls.c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/arm-smccc.h>
+#include <linux/kvm_host.h>
+
+#include <arm64/kvm_emulate.h>
+#include <arm64/sysreg.h>
+#include <arm64/spectre.h>
+
+#include <kvm/arm_hypercalls.h>
+#include <kvm/arm_psci.h>
+
+#include "trace.h"
+
+/* arm on s390 KVM does not support/need PTP */
+static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) { }
+
+#define KVM_S390_ARM_SMCCC_VENDOR_HYP_FEATURES \
+ GENMASK(KVM_REG_ARM_VENDOR_HYP_BIT_FUNC_FEAT, 0)
+
+#define KVM_ARM_SMCCC_STD_FEATURES \
+ GENMASK(KVM_REG_ARM_STD_BMAP_BIT_COUNT - 1, 0)
+#define KVM_ARM_SMCCC_STD_HYP_FEATURES \
+ GENMASK(KVM_REG_ARM_STD_HYP_BMAP_BIT_COUNT - 1, 0)
+#define KVM_ARM_SMCCC_VENDOR_HYP_FEATURES \
+ KVM_S390_ARM_SMCCC_VENDOR_HYP_FEATURES
+#define KVM_ARM_SMCCC_VENDOR_HYP_FEATURES_2 \
+ GENMASK(KVM_REG_ARM_VENDOR_HYP_BMAP_2_BIT_COUNT - 1, 0)
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/hypercalls.inc"
+#undef __INCL_GEN_ARM_FILE
diff --git a/arch/s390/kvm/arm64/psci.c b/arch/s390/kvm/arm64/psci.c
new file mode 100644
index 000000000000..fae0677cc927
--- /dev/null
+++ b/arch/s390/kvm/arm64/psci.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/arm-smccc.h>
+#include <linux/preempt.h>
+#include <linux/kvm_host.h>
+#include <linux/uaccess.h>
+#include <linux/wait.h>
+
+#include <arm64/kvm_emulate.h>
+#include <arm64/cputype.h>
+#include <arm64/sysreg.h>
+
+#include <kvm/arm_psci.h>
+#include <kvm/arm_hypercalls.h>
+
+#include "trace.h"
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/psci.inc"
+#undef __INCL_GEN_ARM_FILE
diff --git a/arch/s390/kvm/arm64/pvtime.c b/arch/s390/kvm/arm64/pvtime.c
new file mode 100644
index 000000000000..2b9656ea4e35
--- /dev/null
+++ b/arch/s390/kvm/arm64/pvtime.c
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/arm-smccc.h>
+#include <linux/kvm_host.h>
+#include <linux/sched/stat.h>
+
+#include <arm64/pvclock-abi.h>
+#include <arm64/kvm_emulate.h>
+#include <arm64/kvm_mmu.h>
+
+#include <kvm/arm_hypercalls.h>
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/pvtime.inc"
+#undef __INCL_GEN_ARM_FILE
diff --git a/arch/s390/kvm/arm64/reset.c b/arch/s390/kvm/arm64/reset.c
index 2ae363f6821a..163b78f9e21c 100644
--- a/arch/s390/kvm/arm64/reset.c
+++ b/arch/s390/kvm/arm64/reset.c
@@ -110,6 +110,10 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
vcpu_reset_icc(vcpu);
if (reset_state.reset) {
+ /* The guest found the shoot yourself in the foot button */
+ if (reset_state.be)
+ kvm_vm_dead(vcpu->kvm);
+
*vcpu_pc(vcpu) = reset_state.pc;
vcpu_clear_flag(vcpu, PENDING_EXCEPTION);
vcpu_clear_flag(vcpu, EXCEPT_MASK);
diff --git a/arch/s390/kvm/arm64/trace.h b/arch/s390/kvm/arm64/trace.h
index 2f0ac8aa7a06..694752842cc4 100644
--- a/arch/s390/kvm/arm64/trace.h
+++ b/arch/s390/kvm/arm64/trace.h
@@ -140,6 +140,26 @@ TRACE_EVENT(kvm_sys_access,
str_write_read(__entry->is_write))
);
+TRACE_EVENT(kvm_hvc_arm64,
+ TP_PROTO(unsigned long vcpu_pc, unsigned long r0, unsigned long imm),
+ TP_ARGS(vcpu_pc, r0, imm),
+
+ TP_STRUCT__entry(
+ __field(unsigned long, vcpu_pc)
+ __field(unsigned long, r0)
+ __field(unsigned long, imm)
+ ),
+
+ TP_fast_assign(
+ __entry->vcpu_pc = vcpu_pc;
+ __entry->r0 = r0;
+ __entry->imm = imm;
+ ),
+
+ TP_printk("HVC at 0x%016lx (r0: 0x%016lx, imm: 0x%lx)",
+ __entry->vcpu_pc, __entry->r0, __entry->imm)
+);
+
#endif /* KVM_ARM64_TRACE_KVM_H */
#undef TRACE_INCLUDE_PATH
diff --git a/arch/s390/kvm/arm64/trng.c b/arch/s390/kvm/arm64/trng.c
new file mode 100644
index 000000000000..ee75e7351a97
--- /dev/null
+++ b/arch/s390/kvm/arm64/trng.c
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/arm-smccc.h>
+#include <linux/kvm_host.h>
+
+#include <arm64/kvm_emulate.h>
+
+#include <kvm/arm_hypercalls.h>
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/trng.inc"
+#undef __INCL_GEN_ARM_FILE
diff --git a/arch/s390/tools/Makefile.arm64h b/arch/s390/tools/Makefile.arm64h
index 9761b0bfcfbd..b7b751f71b23 100644
--- a/arch/s390/tools/Makefile.arm64h
+++ b/arch/s390/tools/Makefile.arm64h
@@ -23,7 +23,9 @@ ARM64_SHARED_HEADERS := \
kvm_arm.h \
kvm_emulate-gen.h \
kvm_host.h \
+ kvm_mmu.h \
ptrace-gen.h \
+ pvclock-abi.h \
sysreg-gen.h \
spectre.h \
kvm:sys_regs-gen.h \
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 09/12] KVM: s390: arm64: Provide timer infrastructure
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
` (4 preceding siblings ...)
2026-12-02 14:11 ` [PATCH 07/12] KVM: s390: arm64: Enable full smccc support Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 10/12] KVM: s390: arm64: Integrate guest timer Steffen Eiden
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
From: Gautam Gala <ggala@linux.ibm.com>
Add support for the arm64 architected timers (virtual and physical) of
an arm64 guest. The SAE block holds the timer control registers, and
ISTATUS in CTL is set when a timer condition is met.
Build the shared arm64 arch_timer.c with s390-specific backends: CTL and
CVAL are accessed through vcpu_{read,write}_sys_reg(), and the guest
counter is obtained with PTFF QAGPT from the guest physical time offset
(GPTO) in the SAE block.
Co-developed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Gautam Gala <ggala@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/s390/include/arm64/kvm_emulate.h | 5 +
arch/s390/include/asm/kvm_host_arm64.h | 2 +
arch/s390/kvm/arm64/Makefile | 1 +
arch/s390/kvm/arm64/Makefile.gen | 1 +
arch/s390/kvm/arm64/arch_timer.c | 224 +++++++++++++++++++++++++++++++++
arch/s390/kvm/arm64/arm.c | 5 -
6 files changed, 233 insertions(+), 5 deletions(-)
diff --git a/arch/s390/include/arm64/kvm_emulate.h b/arch/s390/include/arm64/kvm_emulate.h
index dbaef40a2ac7..bb2ddfe22b95 100644
--- a/arch/s390/include/arm64/kvm_emulate.h
+++ b/arch/s390/include/arm64/kvm_emulate.h
@@ -127,6 +127,11 @@ static inline void kvm_reset_fpsimd(struct kvm_vcpu *vcpu)
void vcpu_reset_hcrx(struct kvm_vcpu *vcpu);
+static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
+{
+ return false;
+}
+
#include <arm64/kvm_emulate-gen.h>
#endif /* __S390_ARM64_KVM_EMULATE_H__ */
diff --git a/arch/s390/include/asm/kvm_host_arm64.h b/arch/s390/include/asm/kvm_host_arm64.h
index 9bde7c56f08a..8367d34990a9 100644
--- a/arch/s390/include/asm/kvm_host_arm64.h
+++ b/arch/s390/include/asm/kvm_host_arm64.h
@@ -135,6 +135,8 @@ struct kvm_vcpu_arch {
u64 last_steal;
gpa_t base;
} steal;
+
+ struct arch_timer_cpu timer_cpu;
};
struct kvm_vcpu_stat {
diff --git a/arch/s390/kvm/arm64/Makefile b/arch/s390/kvm/arm64/Makefile
index dc3d991fb4e2..e22037a7c498 100644
--- a/arch/s390/kvm/arm64/Makefile
+++ b/arch/s390/kvm/arm64/Makefile
@@ -11,6 +11,7 @@ ccflags-y += -I $(src) -I $(objtree)/$(obj) -I$(srctree)/arch/$(SRCARCH)/kvm/gma
kvm-arm64-local := \
arm.o \
+ arch_timer.o \
exception.o \
feature.o \
guest.o \
diff --git a/arch/s390/kvm/arm64/Makefile.gen b/arch/s390/kvm/arm64/Makefile.gen
index 6faf2e66ac8d..5175b4b5c688 100644
--- a/arch/s390/kvm/arm64/Makefile.gen
+++ b/arch/s390/kvm/arm64/Makefile.gen
@@ -5,6 +5,7 @@
# List of ARM64 C files to extract
ARM64_CFILES := \
+ arch_timer.c \
arm.c \
guest.c \
hypercalls.c \
diff --git a/arch/s390/kvm/arm64/arch_timer.c b/arch/s390/kvm/arm64/arch_timer.c
new file mode 100644
index 000000000000..f5b486afc2ad
--- /dev/null
+++ b/arch/s390/kvm/arm64/arch_timer.c
@@ -0,0 +1,224 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/hrtimer.h>
+#include <linux/kvm_host.h>
+#include <linux/units.h>
+
+#include <clocksource/arm_arch_timer.h>
+#include <arm64/kvm_emulate.h>
+#include <arm64/kvm_nested.h>
+
+#include <kvm/arm_arch_timer.h>
+
+#include <asm/timex.h>
+
+#include "qaaf.h"
+
+static bool kvm_timer_enabled(struct arch_timer_context *timer_ctx);
+static bool kvm_timer_pending(struct arch_timer_context *timer_ctx);
+static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx, u64 val);
+static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu);
+
+#define __INCL_GEN_ARM_FILE
+#include "generated/arch_timer.inc"
+#undef __INCL_GEN_ARM_FILE
+
+/*
+ * This code assumes CNTRFRQ_EL0 == 1GHz and will break if not the case.
+ * A check during vcpu_create triggering a WARN_ONCE is done.
+ */
+
+/* Clamp the delta: >2^63 ns overflows ktime_t and livelocks the bg hrtimer */
+#define COUNTER_DELTA_MAX (1ULL << 42)
+
+u32 timer_get_ctl(struct arch_timer_context *ctxt)
+{
+ struct kvm_vcpu *vcpu = timer_context_to_vcpu(ctxt);
+
+ switch (arch_timer_ctx_index(ctxt)) {
+ case TIMER_VTIMER:
+ return vcpu_read_sys_reg(vcpu, CNTV_CTL_EL0);
+ case TIMER_PTIMER:
+ return vcpu_read_sys_reg(vcpu, CNTP_CTL_EL0);
+ default:
+ WARN_ON(1);
+ return 0;
+ }
+}
+
+static void timer_set_ctl(struct arch_timer_context *ctxt, u32 ctl)
+{
+ struct kvm_vcpu *vcpu = timer_context_to_vcpu(ctxt);
+
+ switch (arch_timer_ctx_index(ctxt)) {
+ case TIMER_VTIMER:
+ vcpu_write_sys_reg(vcpu, ctl, CNTV_CTL_EL0);
+ break;
+ case TIMER_PTIMER:
+ vcpu_write_sys_reg(vcpu, ctl, CNTP_CTL_EL0);
+ break;
+ default:
+ WARN_ON(1);
+ }
+}
+
+u64 timer_get_cval(struct arch_timer_context *ctxt)
+{
+ struct kvm_vcpu *vcpu = timer_context_to_vcpu(ctxt);
+
+ switch (arch_timer_ctx_index(ctxt)) {
+ case TIMER_VTIMER:
+ return vcpu_read_sys_reg(vcpu, CNTV_CVAL_EL0);
+ case TIMER_PTIMER:
+ return vcpu_read_sys_reg(vcpu, CNTP_CVAL_EL0);
+ default:
+ WARN_ON(1);
+ return 0;
+ }
+}
+
+static bool vcpu_has_wfit_active(struct kvm_vcpu *vcpu)
+{
+ return vcpu_get_flag(vcpu, IN_WFIT);
+}
+
+static u64 kvm_counter_compute_delta(struct arch_timer_context *timer_ctx, u64 val)
+{
+ struct kvm_vcpu *vcpu = timer_context_to_vcpu(timer_ctx);
+ u64 now = ptff_qagpt(vcpu->arch.sae_block.gpto);
+
+ /*
+ * Bound the delta to a sane value. KVM/arm64 gets an equivalent bound
+ * from cyclecounter_cyc2ns() scaled math.
+ */
+ if (now < val)
+ return min(val - now, COUNTER_DELTA_MAX);
+
+ return 0;
+}
+
+static bool kvm_timer_enabled(struct arch_timer_context *timer_ctx)
+{
+ return timer_ctx &&
+ ((timer_get_ctl(timer_ctx) &
+ (ARCH_TIMER_CTRL_IT_MASK | ARCH_TIMER_CTRL_ENABLE)) ==
+ ARCH_TIMER_CTRL_ENABLE);
+}
+
+static bool kvm_timer_pending(struct arch_timer_context *timer_ctx)
+{
+ if (!kvm_timer_enabled(timer_ctx))
+ return false;
+
+ if (timer_get_ctl(timer_ctx) & ARCH_TIMER_CTRL_IT_STAT)
+ return true;
+
+ return !kvm_counter_compute_delta(timer_ctx, timer_get_cval(timer_ctx));
+}
+
+static enum hrtimer_restart kvm_bg_timer_expire(struct hrtimer *hrt)
+{
+ struct arch_timer_cpu *timer;
+ struct kvm_vcpu *vcpu;
+ u64 ns;
+
+ timer = container_of(hrt, struct arch_timer_cpu, bg_timer);
+ vcpu = container_of(timer, struct kvm_vcpu, arch.timer_cpu);
+
+ /*
+ * Check that the timer has really expired from the guest's
+ * PoV (NTP on the host may have forced it to expire
+ * early). If we should have slept longer, restart it.
+ */
+ ns = kvm_timer_earliest_exp(vcpu);
+ if (unlikely(ns)) {
+ hrtimer_forward_now(hrt, ns_to_ktime(ns));
+ return HRTIMER_RESTART;
+ }
+
+ kvm_vcpu_wake_up(vcpu);
+
+ return HRTIMER_NORESTART;
+}
+
+/*
+ * Schedule the background timer before calling kvm_vcpu_halt, so that this
+ * thread is removed from its waitqueue and made runnable when there's a timer
+ * interrupt to handle or when a WFxT times out.
+ */
+static void kvm_timer_blocking(struct kvm_vcpu *vcpu)
+{
+ struct arch_timer_cpu *timer = vcpu_timer(vcpu);
+
+ /*
+ * If no timers are capable of raising interrupts (disabled or
+ * masked), then there's no more work for us to do.
+ */
+ if (!kvm_timer_enabled(vcpu_vtimer(vcpu)) &&
+ !kvm_timer_enabled(vcpu_ptimer(vcpu)) &&
+ !vcpu_has_wfit_active(vcpu))
+ return;
+
+ /*
+ * At least one guest timer will expire or a WFxT will time out.
+ * Schedule a background timer.
+ * Set the earliest expiration time among the guest timers.
+ */
+ soft_timer_start(&timer->bg_timer, kvm_timer_earliest_exp(vcpu));
+}
+
+void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
+{
+ struct arch_timer_context *ctxt;
+
+ kvm_timer_unblocking(vcpu);
+
+ for (int timerid = 0; timerid < nr_timers(vcpu); timerid++) {
+ ctxt = vcpu_get_timer(vcpu, timerid);
+ ctxt->loaded = true;
+ }
+}
+
+void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
+{
+ struct arch_timer_context *ctxt;
+
+ for (int timerid = 0; timerid < nr_timers(vcpu); timerid++) {
+ ctxt = vcpu_get_timer(vcpu, timerid);
+ ctxt->loaded = false;
+ }
+
+ if (kvm_vcpu_is_blocking(vcpu))
+ kvm_timer_blocking(vcpu);
+}
+
+static void timer_context_init(struct kvm_vcpu *vcpu, int timerid)
+{
+ struct arch_timer_context *ctxt = vcpu_get_timer(vcpu, timerid);
+
+ ctxt->timer_id = timerid;
+}
+
+void kvm_timer_vcpu_init(struct kvm_vcpu *vcpu)
+{
+ u64 cntfrq_el0 = kvm_qaaf_read_ftr_reg(SYS_CNTFRQ_EL0);
+ struct arch_timer_cpu *timer = vcpu_timer(vcpu);
+
+ cntfrq_el0 = kvm_qaaf_read_ftr_reg(SYS_CNTFRQ_EL0);
+ if (cntfrq_el0 != HZ_PER_GHZ)
+ pr_warn_once(
+ "CNTFRQ_EL0 is %llu Hz. KVM expects 1GHz. Wakeups may arrive early or late.",
+ cntfrq_el0);
+
+ for (int timerid = 0; timerid < nr_timers(vcpu); timerid++)
+ timer_context_init(vcpu, timerid);
+
+ hrtimer_setup(&timer->bg_timer, kvm_bg_timer_expire, CLOCK_MONOTONIC,
+ HRTIMER_MODE_ABS_HARD);
+}
+
+void kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
+{
+ timer_set_ctl(vcpu_vtimer(vcpu), 0);
+ timer_set_ctl(vcpu_ptimer(vcpu), 0);
+}
diff --git a/arch/s390/kvm/arm64/arm.c b/arch/s390/kvm/arm64/arm.c
index 7212ea1fc141..cbc760043a45 100644
--- a/arch/s390/kvm/arm64/arm.c
+++ b/arch/s390/kvm/arm64/arm.c
@@ -894,11 +894,6 @@ int kvm_set_routing_entry(struct kvm *kvm,
return -EINVAL;
}
-int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
-{
- return 0;
-}
-
long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
unsigned long arg)
{
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 10/12] KVM: s390: arm64: Integrate guest timer
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
` (5 preceding siblings ...)
2026-12-02 14:11 ` [PATCH 09/12] KVM: s390: arm64: Provide timer infrastructure Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 11/12] KVM: s390: arm64: Trap WFx Steffen Eiden
2026-12-02 14:11 ` [PATCH 12/12] KVM: s390: arm64: Add IRQ line Steffen Eiden
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
Hook the timer into the vCPU lifecycle and, on vCPU reset, disable and
unmask both timers.
Co-developed-by: Gautam Gala <ggala@linux.ibm.com>
Signed-off-by: Gautam Gala <ggala@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/s390/kvm/arm64/arch_timer.c | 2 ++
arch/s390/kvm/arm64/arm.c | 35 +++++++++++++++++++++++++++++++++--
arch/s390/kvm/arm64/handle_exit.c | 2 ++
arch/s390/kvm/arm64/reset.c | 1 +
4 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/arch/s390/kvm/arm64/arch_timer.c b/arch/s390/kvm/arm64/arch_timer.c
index f5b486afc2ad..f30e6bab3f0c 100644
--- a/arch/s390/kvm/arm64/arch_timer.c
+++ b/arch/s390/kvm/arm64/arch_timer.c
@@ -173,6 +173,8 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)
kvm_timer_unblocking(vcpu);
+ vcpu->arch.sae_block.gpto = vcpu->kvm->arch.epd;
+
for (int timerid = 0; timerid < nr_timers(vcpu); timerid++) {
ctxt = vcpu_get_timer(vcpu, timerid);
ctxt->loaded = true;
diff --git a/arch/s390/kvm/arm64/arm.c b/arch/s390/kvm/arm64/arm.c
index cbc760043a45..7964848f37dd 100644
--- a/arch/s390/kvm/arm64/arm.c
+++ b/arch/s390/kvm/arm64/arm.c
@@ -27,6 +27,8 @@
#include "handle_exit.h"
#include "qaaf.h"
+#include <kvm/arm_arch_timer.h>
+
#define CREATE_TRACE_POINTS
#include "trace.h"
@@ -215,6 +217,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
/* Force users to call KVM_ARM_VCPU_INIT */
vcpu_clear_flag(vcpu, VCPU_INITIALIZED);
+ kvm_timer_vcpu_init(vcpu);
+
vcpu->arch.mc = kvm_s390_new_mmu_cache();
if (!vcpu->arch.mc)
return -ENOMEM;
@@ -244,6 +248,7 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
{
kfree(vcpu->arch.ccsidr);
+ kvm_timer_vcpu_terminate(vcpu);
kvm_s390_free_mmu_cache(vcpu->arch.mc);
VCPU_EVENT(vcpu, 3, "%s", "free cpu");
@@ -263,6 +268,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
lasrm(&vcpu->arch.save_area);
vcpu->arch.sae_block.gpto = vcpu->kvm->arch.epd;
+ kvm_timer_vcpu_load(vcpu);
vcpu_set_pauth_traps(vcpu);
@@ -272,6 +278,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
{
+ kvm_timer_vcpu_put(vcpu);
+
stiasrm(&vcpu->arch.save_area);
vcpu->cpu = -1;
@@ -525,6 +533,26 @@ static void kvm_arm_save_shadowed_regs(struct kvm_vcpu *vcpu)
vcpu->arch.ctxt.cntv_cval_el0 = _vcpu_read_sys_reg(vcpu, SYS_CNTV_CVAL_EL0);
}
+/*
+ * kvm_vcpu_exit_request - returns true if the VCPU should *not* enter the guest
+ * @vcpu: The VCPU pointer
+ * @ret: Exit reason to be propagated to userspace
+ *
+ */
+static bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu, int *ret)
+{
+ if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
+ if (unlikely(kvm_timer_update_run(vcpu))) {
+ *ret = -EINTR;
+ vcpu->run->exit_reason = KVM_EXIT_INTR;
+ return true;
+ }
+ }
+
+ return kvm_request_pending(vcpu) ||
+ xfer_to_guest_mode_work_pending();
+}
+
/** kvm_arch_vcpu_ioctl_run() - run arm64 vCPU
*
* Execute arm64 guest instructions using SAE.
@@ -584,8 +612,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
*/
smp_store_mb(vcpu->mode, IN_GUEST_MODE);
- if (ret <= 0 || kvm_request_pending(vcpu) ||
- xfer_to_guest_mode_work_pending()) {
+ if (ret <= 0 || kvm_vcpu_exit_request(vcpu, &ret)) {
vcpu->mode = OUTSIDE_GUEST_MODE;
local_irq_enable();
continue;
@@ -608,6 +635,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
ret = handle_exit(vcpu);
}
+ /* Tell userspace about in-kernel device output levels */
+ if (!irqchip_in_kernel(vcpu->kvm))
+ kvm_timer_update_run(vcpu);
+
kvm_sigset_deactivate(vcpu);
out:
if (unlikely(vcpu_get_flag(vcpu, PENDING_EXCEPTION) ||
diff --git a/arch/s390/kvm/arm64/handle_exit.c b/arch/s390/kvm/arm64/handle_exit.c
index a48c039fbf3f..551cf6fc3afe 100644
--- a/arch/s390/kvm/arm64/handle_exit.c
+++ b/arch/s390/kvm/arm64/handle_exit.c
@@ -97,6 +97,8 @@ int handle_exit(struct kvm_vcpu *vcpu)
case SAE_ICPTR_SYNCHRONOUS_EXCEPTION:
ret = handle_trap_exceptions(vcpu);
break;
+ case SAE_ICPTR_TIMER:
+ break;
default:
vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
ret = 0;
diff --git a/arch/s390/kvm/arm64/reset.c b/arch/s390/kvm/arm64/reset.c
index 163b78f9e21c..391f6e5e49b7 100644
--- a/arch/s390/kvm/arm64/reset.c
+++ b/arch/s390/kvm/arm64/reset.c
@@ -101,6 +101,7 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
kvm_reset_vcpu_core(vcpu);
kvm_reset_sys_regs(vcpu);
+ kvm_timer_vcpu_reset(vcpu);
/* Reset special registers */
vcpu_reset_hcr(vcpu);
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 11/12] KVM: s390: arm64: Trap WFx
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
` (6 preceding siblings ...)
2026-12-02 14:11 ` [PATCH 10/12] KVM: s390: arm64: Integrate guest timer Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
2026-12-02 14:11 ` [PATCH 12/12] KVM: s390: arm64: Add IRQ line Steffen Eiden
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
From: Gautam Gala <ggala@linux.ibm.com>
Enable the TWI and TWE trap bits in HCR_EL2 and handle the resulting
WFx exceptions in kvm_handle_wfx().
WFI halts the vCPU until it becomes runnable again, i.e. until userspace
asserts an IRQ/FIQ line (KVM_IRQ_LINE) or a guest timer output level
changes. WFE is treated as a directed yield to another vCPU of the same
VM. WFxT carries a deadline: compare it against the guest counter
obtained with PTFF QAGPT from the guest physical time offset (GPTO) in
the SAE block, and complete the instruction immediately if it has
already passed. Otherwise, halt as for WFI with IN_WFIT set so the timer
code takes the deadline into account. WFxT with an invalid RN is
executed as plain WFI/WFE.
Co-developed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Gautam Gala <ggala@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/s390/kvm/arm64/handle_exit.c | 54 +++++++++++++++++++++++++++++++++++++++
arch/s390/kvm/arm64/reset.c | 3 ++-
2 files changed, 56 insertions(+), 1 deletion(-)
diff --git a/arch/s390/kvm/arm64/handle_exit.c b/arch/s390/kvm/arm64/handle_exit.c
index 551cf6fc3afe..06f73be83f46 100644
--- a/arch/s390/kvm/arm64/handle_exit.c
+++ b/arch/s390/kvm/arm64/handle_exit.c
@@ -6,6 +6,8 @@
#include <arm64/kvm_emulate.h>
#include <kvm/arm_hypercalls.h>
+#include <asm/timex.h>
+
#include "handle_exit.h"
typedef int (*exit_handle_fn)(struct kvm_vcpu *);
@@ -43,8 +45,60 @@ static int handle_smc(struct kvm_vcpu *vcpu)
return kvm_smccc_call_handler(vcpu);
}
+/**
+ * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event
+ * instruction executed by a guest
+ *
+ * @vcpu: the vcpu pointer
+ *
+ * WFI: Virtualize the instruction by halting the vCPU until it becomes
+ * runnable, i.e. until userspace asserts an IRQ/FIQ line (KVM_IRQ_LINE) or
+ * a guest timer output level changes.
+ * WFE: Handled as a directed yield to another vCPU of the same VM.
+ * WFxT: WFI/WFE with a deadline; if the deadline has already passed, the
+ * instruction completes immediately.
+ */
+static int kvm_handle_wfx(struct kvm_vcpu *vcpu)
+{
+ u64 esr = kvm_vcpu_get_esr(vcpu);
+ bool is_wfe = !!(esr & ESR_ELx_WFx_ISS_WFE);
+
+ if (is_wfe)
+ vcpu->stat.wfe_exit_stat++;
+ else
+ vcpu->stat.wfi_exit_stat++;
+
+ if (esr & ESR_ELx_WFx_ISS_WFxT) {
+ if (esr & ESR_ELx_WFx_ISS_RV) {
+ u64 val, now;
+
+ now = ptff_qagpt(vcpu->arch.sae_block.gpto);
+ val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
+ if (now >= val)
+ goto out;
+ } else {
+ /* Treat WFxT as WFx if RN is invalid */
+ esr &= ~ESR_ELx_WFx_ISS_WFxT;
+ }
+ }
+
+ if (esr & ESR_ELx_WFx_ISS_WFE) {
+ kvm_vcpu_on_spin(vcpu, vcpu_mode_priv(vcpu));
+ } else {
+ if (esr & ESR_ELx_WFx_ISS_WFxT)
+ vcpu_set_flag(vcpu, IN_WFIT);
+
+ kvm_vcpu_wfi(vcpu);
+ }
+out:
+ kvm_incr_pc(vcpu);
+
+ return 1;
+}
+
static exit_handle_fn arm_exit_handlers[] = {
[0 ... ESR_ELx_EC_MAX] = kvm_handle_unknown_ec,
+ [ESR_ELx_EC_WFx] = kvm_handle_wfx,
[ESR_ELx_EC_SMC64] = handle_smc,
[ESR_ELx_EC_HVC64] = handle_hvc,
[ESR_ELx_EC_SYS64] = kvm_handle_sys_reg,
diff --git a/arch/s390/kvm/arm64/reset.c b/arch/s390/kvm/arm64/reset.c
index 391f6e5e49b7..feec5009d2cb 100644
--- a/arch/s390/kvm/arm64/reset.c
+++ b/arch/s390/kvm/arm64/reset.c
@@ -30,7 +30,8 @@ static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
/* traps */
vcpu->arch.hcr_el2 |= HCR_EL2_TSC | HCR_EL2_TID1 | HCR_EL2_TID2 |
HCR_EL2_TID3 | HCR_EL2_TID4 | HCR_EL2_TID5 |
- HCR_EL2_TIDCP | HCR_EL2_TLOR;
+ HCR_EL2_TIDCP | HCR_EL2_TLOR |
+ HCR_EL2_TWI | HCR_EL2_TWE;
}
void vcpu_reset_hcrx(struct kvm_vcpu *vcpu)
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 12/12] KVM: s390: arm64: Add IRQ line
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
` (7 preceding siblings ...)
2026-12-02 14:11 ` [PATCH 11/12] KVM: s390: arm64: Trap WFx Steffen Eiden
@ 2026-12-02 14:11 ` Steffen Eiden
8 siblings, 0 replies; 10+ messages in thread
From: Steffen Eiden @ 2026-12-02 14:11 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Andreas Grapentin, Christian Borntraeger,
Janosch Frank, Claudio Imbrenda, David Hildenbrand,
Heiko Carstens, Vasily Gorbik, Alexander Gordeev, Sven Schnelle,
Friedrich Welter, Gautam Gala, Hariharan Mari, Hendrik Brueckner,
Nico Boehr, Ulrich Weigand, Arnd Bergmann, Paolo Bonzini
Cc: linux-arm-kernel, kvmarm, linux-kernel, kvm, linux-s390,
Steffen Eiden
From: Andreas Grapentin <gra@linux.ibm.com>
As all relevant helper and CPU management functions are in place and
behave correctly it is time to add support for non-kernel IRQ handling.
Implement the IRQ line for the !irqchip_in_kernel case.
With that arm on s390 is able to run full Linux arm64 guests using the
user irq chip.
Co-developed-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Andreas Grapentin <gra@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
---
arch/s390/include/arm64/kvm_emulate.h | 1 +
arch/s390/include/asm/kvm_host_arm64.h | 1 +
arch/s390/kvm/arm64/arm.c | 49 +++++++++++++++++++++++++++++++---
arch/s390/kvm/arm64/reset.c | 6 +++--
arch/s390/kvm/arm64/trace.h | 25 +++++++++++++++++
5 files changed, 77 insertions(+), 5 deletions(-)
diff --git a/arch/s390/include/arm64/kvm_emulate.h b/arch/s390/include/arm64/kvm_emulate.h
index bb2ddfe22b95..4b6aae2d6e67 100644
--- a/arch/s390/include/arm64/kvm_emulate.h
+++ b/arch/s390/include/arm64/kvm_emulate.h
@@ -125,6 +125,7 @@ static inline void kvm_reset_fpsimd(struct kvm_vcpu *vcpu)
vcpu->arch.sae_block.fpcr = 0;
}
+void vcpu_reset_hcr(struct kvm_vcpu *vcpu);
void vcpu_reset_hcrx(struct kvm_vcpu *vcpu);
static inline bool is_hyp_ctxt(const struct kvm_vcpu *vcpu)
diff --git a/arch/s390/include/asm/kvm_host_arm64.h b/arch/s390/include/asm/kvm_host_arm64.h
index 8367d34990a9..08dd08e58f33 100644
--- a/arch/s390/include/asm/kvm_host_arm64.h
+++ b/arch/s390/include/asm/kvm_host_arm64.h
@@ -39,6 +39,7 @@ bool cpus_have_final_cap(unsigned int num);
/* GICv2 maximum TODO change with in-kernel gic support*/
#define KVM_MAX_VCPUS 8
#define __KVM_HAVE_VCPU_RUN_PID_CHANGE
+#define __KVM_HAVE_IRQ_LINE
#define KVM_S390_ARM64_IMPL_FEATURES ( \
BIT(KVM_ARM_VCPU_PTRAUTH_ADDRESS) | \
diff --git a/arch/s390/kvm/arm64/arm.c b/arch/s390/kvm/arm64/arm.c
index 7964848f37dd..0a736ff85a77 100644
--- a/arch/s390/kvm/arm64/arm.c
+++ b/arch/s390/kvm/arm64/arm.c
@@ -59,6 +59,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
case KVM_CAP_MP_STATE:
case KVM_CAP_ARM_PSCI:
case KVM_CAP_ARM_PSCI_0_2:
+ case KVM_CAP_ARM_USER_IRQ:
ret = 1;
break;
case KVM_CAP_ARM_SVE:
@@ -319,7 +320,12 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
{
- return 0;
+ int irq_lines = vcpu->arch.hcr_el2 & (HCR_EL2_VI | HCR_EL2_VF | HCR_EL2_VSE);
+
+ irq_lines |= (!irqchip_in_kernel(vcpu->kvm) &&
+ kvm_timer_should_notify_user(vcpu));
+
+ return irq_lines && vcpu_can_run(vcpu);
}
static unsigned long system_supported_vcpu_features(struct kvm_vcpu *vcpu)
@@ -475,6 +481,13 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
if (ret)
return ret;
+ /*
+ * Only KVM_ARM_VCPU_INIT ioctl rebuilds hcr_el2. The PSCI CPU_ON
+ * reset must preserve it so that the interrupt lines asserted via
+ * KVM_IRQ_LINE (VI/VF/VSE) survive.
+ */
+ vcpu_reset_hcr(vcpu);
+
scoped_guard(spinlock, &vcpu->arch.mp_state_lock) {
if (power_off)
__kvm_arm_vcpu_power_off(vcpu, KVM_MP_STATE_STOPPED);
@@ -488,8 +501,38 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
bool line_status)
{
- /* stub for now*/
- return -EINVAL;
+ u32 irq = irq_level->irq;
+ unsigned int irq_type, vcpu_id, irq_num;
+ struct kvm_vcpu *vcpu = NULL;
+ bool level = irq_level->level;
+
+ irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK;
+ vcpu_id = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK;
+ vcpu_id += ((irq >> KVM_ARM_IRQ_VCPU2_SHIFT) & KVM_ARM_IRQ_VCPU2_MASK) * (KVM_ARM_IRQ_VCPU_MASK + 1);
+ irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK;
+
+ trace_kvm_irq_line(irq_type, vcpu_id, irq_num, irq_level->level);
+
+ switch (irq_type) {
+ case KVM_ARM_IRQ_TYPE_CPU:
+ if (irqchip_in_kernel(kvm))
+ return -ENXIO;
+
+ vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
+ if (!vcpu)
+ return -EINVAL;
+
+ if (irq_num > KVM_ARM_IRQ_CPU_FIQ)
+ return -EINVAL;
+
+ return vcpu_interrupt_line(vcpu, irq_num, level);
+ /* no in kernel gic */
+ case KVM_ARM_IRQ_TYPE_PPI:
+ case KVM_ARM_IRQ_TYPE_SPI:
+ return -ENXIO;
+ default:
+ return -EINVAL;
+ }
}
static noinstr void arm_vcpu_enter_exit(phys_addr_t sae_block)
diff --git a/arch/s390/kvm/arm64/reset.c b/arch/s390/kvm/arm64/reset.c
index feec5009d2cb..891fe4b4fc16 100644
--- a/arch/s390/kvm/arm64/reset.c
+++ b/arch/s390/kvm/arm64/reset.c
@@ -23,7 +23,7 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu)
return true;
}
-static inline void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
+void vcpu_reset_hcr(struct kvm_vcpu *vcpu)
{
vcpu->arch.hcr_el2 = HCR_EL2_E2H | HCR_EL2_RW | HCR_EL2_AMO |
HCR_EL2_IMO | HCR_EL2_FMO | HCR_EL2_PTW;
@@ -105,12 +105,14 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu)
kvm_timer_vcpu_reset(vcpu);
/* Reset special registers */
- vcpu_reset_hcr(vcpu);
vcpu_reset_hcrx(vcpu);
vcpu_reset_cptr(vcpu);
vcpu_reset_cnthctl(vcpu);
vcpu_reset_icc(vcpu);
+ /* Discard a pending SError */
+ vcpu->arch.hcr_el2 &= ~HCR_EL2_VSE;
+
if (reset_state.reset) {
/* The guest found the shoot yourself in the foot button */
if (reset_state.be)
diff --git a/arch/s390/kvm/arm64/trace.h b/arch/s390/kvm/arm64/trace.h
index 694752842cc4..ab69e66aaa7b 100644
--- a/arch/s390/kvm/arm64/trace.h
+++ b/arch/s390/kvm/arm64/trace.h
@@ -43,6 +43,31 @@ TRACE_EVENT(kvm_exit,
__entry->vcpu_pc)
);
+TRACE_EVENT(kvm_irq_line,
+ TP_PROTO(unsigned int type, int vcpu_idx, int irq_num, int level),
+ TP_ARGS(type, vcpu_idx, irq_num, level),
+
+ TP_STRUCT__entry(
+ __field( unsigned int, type )
+ __field( int, vcpu_idx )
+ __field( int, irq_num )
+ __field( int, level )
+ ),
+
+ TP_fast_assign(
+ __entry->type = type;
+ __entry->vcpu_idx = vcpu_idx;
+ __entry->irq_num = irq_num;
+ __entry->level = level;
+ ),
+
+ TP_printk("Inject %s interrupt (%d), vcpu->idx: %d, num: %d, level: %d",
+ (__entry->type == KVM_ARM_IRQ_TYPE_CPU) ? "CPU" :
+ (__entry->type == KVM_ARM_IRQ_TYPE_PPI) ? "VGIC PPI" :
+ (__entry->type == KVM_ARM_IRQ_TYPE_SPI) ? "VGIC SPI" : "UNKNOWN",
+ __entry->type, __entry->vcpu_idx, __entry->irq_num, __entry->level)
+);
+
TRACE_EVENT(kvm_mmio_nisv,
TP_PROTO(unsigned long vcpu_pc, unsigned long esr,
unsigned long far, unsigned long ipa),
--
2.56.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-02 14:13 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 14:11 [PATCH 00/12] KVM: s390: arm64: SMCCC, timers and IRQ line Steffen Eiden
2026-12-02 14:11 ` [PATCH 01/12] arm64: Prepare for sharing with s390 Steffen Eiden
2026-12-02 14:11 ` [PATCH 03/12] KVM: arm64: Share timer and smccc code " Steffen Eiden
2026-12-02 14:11 ` [PATCH 05/12] KVM: s390: arm64: Shadow the guest timer CVAL registers Steffen Eiden
2026-12-02 14:11 ` [PATCH 06/12] KVM: s390: arm64: Use mpstate for guests Steffen Eiden
2026-12-02 14:11 ` [PATCH 07/12] KVM: s390: arm64: Enable full smccc support Steffen Eiden
2026-12-02 14:11 ` [PATCH 09/12] KVM: s390: arm64: Provide timer infrastructure Steffen Eiden
2026-12-02 14:11 ` [PATCH 10/12] KVM: s390: arm64: Integrate guest timer Steffen Eiden
2026-12-02 14:11 ` [PATCH 11/12] KVM: s390: arm64: Trap WFx Steffen Eiden
2026-12-02 14:11 ` [PATCH 12/12] KVM: s390: arm64: Add IRQ line Steffen Eiden
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox