mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steffen Eiden <seiden@linux.ibm.com>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	Joey Gouly <joey.gouly@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Zenghui Yu <yuzenghui@huawei.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Andreas Grapentin <gra@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Janosch Frank <frankja@linux.ibm.com>,
	Claudio Imbrenda <imbrenda@linux.ibm.com>,
	David Hildenbrand <david@kernel.org>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Friedrich Welter <fritz@linux.ibm.com>,
	Gautam Gala <ggala@linux.ibm.com>,
	Hariharan Mari <hari55@linux.ibm.com>,
	Hendrik Brueckner <brueckner@linux.ibm.com>,
	Nico Boehr <nrb@linux.ibm.com>,
	Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
	linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	linux-s390@vger.kernel.org, Steffen Eiden <seiden@linux.ibm.com>
Subject: [PATCH 09/12] KVM: s390: arm64: Provide timer infrastructure
Date: Wed, 02 Dec 2026 15:11:47 +0100	[thread overview]
Message-ID: <20261202-smccc-v1-9-48340e2737b6@linux.ibm.com> (raw)
In-Reply-To: <20261202-smccc-v1-0-48340e2737b6@linux.ibm.com>

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


  parent reply	other threads:[~2026-12-02 14:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 02/12] arm64: Share timer and smccc functionality " 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 04/12] KVM: s390: arm64: Use register shadow helper 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 08/12] KVM: s390: arm64: Report fwregs to userspace Steffen Eiden
2026-12-02 14:11 ` Steffen Eiden [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202-smccc-v1-9-48340e2737b6@linux.ibm.com \
    --to=seiden@linux.ibm.com \
    --cc=Ulrich.Weigand@de.ibm.com \
    --cc=agordeev@linux.ibm.com \
    --cc=arnd@arndb.de \
    --cc=borntraeger@linux.ibm.com \
    --cc=brueckner@linux.ibm.com \
    --cc=catalin.marinas@arm.com \
    --cc=david@kernel.org \
    --cc=frankja@linux.ibm.com \
    --cc=fritz@linux.ibm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=ggala@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=gra@linux.ibm.com \
    --cc=hari55@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=joey.gouly@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=nrb@linux.ibm.com \
    --cc=oupton@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=suzuki.poulose@arm.com \
    --cc=svens@linux.ibm.com \
    --cc=will@kernel.org \
    --cc=yuzenghui@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®