From: Gautam Gala <ggala at 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 at linux.ibm.com>
Signed-off-by: Gautam Gala <ggala at linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden at 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