All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tianrui Zhao <zhaotianrui@loongson.cn>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Huacai Chen <chenhuacai@kernel.org>,
	WANG Xuerui <kernel@xen0n.name>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	loongarch@lists.linux.dev, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org, Jens Axboe <axboe@kernel.dk>,
	Mark Brown <broonie@kernel.org>,
	Alex Deucher <alexander.deucher@amd.com>,
	Oliver Upton <oliver.upton@linux.dev>,
	maobibo@loongson.cn
Subject: [PATCH v2 18/29] LoongArch: KVM: Implement vcpu timer operations
Date: Mon, 20 Feb 2023 14:57:24 +0800	[thread overview]
Message-ID: <20230220065735.1282809-19-zhaotianrui@loongson.cn> (raw)
In-Reply-To: <20230220065735.1282809-1-zhaotianrui@loongson.cn>

Implement loongarch vcpu timer operations such as init kvm timer,
require kvm timer, save kvm timer and restore kvm timer. When
vcpu exit, we use kvm soft timer to emulate hardware timer. If
timeout happens, the vcpu timer interrupt will be set and it is
going to be handled at vcpu next entrance.

Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
---
 arch/loongarch/kvm/timer.c | 266 +++++++++++++++++++++++++++++++++++++
 1 file changed, 266 insertions(+)
 create mode 100644 arch/loongarch/kvm/timer.c

diff --git a/arch/loongarch/kvm/timer.c b/arch/loongarch/kvm/timer.c
new file mode 100644
index 000000000..2c7677248
--- /dev/null
+++ b/arch/loongarch/kvm/timer.c
@@ -0,0 +1,266 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
+ */
+
+#include <linux/kvm_host.h>
+#include <asm/kvm_csr.h>
+#include <asm/kvm_vcpu.h>
+
+/* low level hrtimer wake routine */
+enum hrtimer_restart kvm_swtimer_wakeup(struct hrtimer *timer)
+{
+	struct kvm_vcpu *vcpu;
+
+	vcpu = container_of(timer, struct kvm_vcpu, arch.swtimer);
+	_kvm_queue_irq(vcpu, LARCH_INT_TIMER);
+	rcuwait_wake_up(&vcpu->wait);
+	return kvm_count_timeout(vcpu);
+}
+
+/*
+ * ktime_to_tick() - Scale ktime_t to a 64-bit stable timer.
+ *
+ * Caches the dynamic nanosecond bias in vcpu->arch.timer_dyn_bias.
+ */
+static unsigned long ktime_to_tick(struct kvm_vcpu *vcpu, ktime_t now)
+{
+	s64 now_ns, periods;
+	unsigned long delta;
+
+	now_ns = ktime_to_ns(now);
+	delta = now_ns + vcpu->arch.timer_dyn_bias;
+
+	if (delta >= vcpu->arch.timer_period) {
+		/* If delta is out of safe range the bias needs adjusting */
+		periods = div64_s64(now_ns, vcpu->arch.timer_period);
+		vcpu->arch.timer_dyn_bias = -periods * vcpu->arch.timer_period;
+		/* Recalculate delta with new bias */
+		delta = now_ns + vcpu->arch.timer_dyn_bias;
+	}
+
+	/*
+	 * We've ensured that:
+	 *   delta < timer_period
+	 */
+	return div_u64(delta * vcpu->arch.timer_mhz, MNSEC_PER_SEC);
+}
+
+/**
+ * kvm_resume_hrtimer() - Resume hrtimer, updating expiry.
+ * @vcpu:	Virtual CPU.
+ * @now:	ktime at point of resume.
+ * @val:	stable timer at point of resume.
+ *
+ * Resumes the timer and updates the timer expiry based on @now and @count.
+ */
+static void kvm_resume_hrtimer(struct kvm_vcpu *vcpu, ktime_t now,
+				unsigned long val)
+{
+	unsigned long delta;
+	ktime_t expire;
+
+	/* Stable timer decreased to zero or
+	 * initialize to zero, set 4 second timer
+	 */
+	delta = div_u64(val * MNSEC_PER_SEC, vcpu->arch.timer_mhz);
+	expire = ktime_add_ns(now, delta);
+
+	/* Update hrtimer to use new timeout */
+	hrtimer_cancel(&vcpu->arch.swtimer);
+	hrtimer_start(&vcpu->arch.swtimer, expire, HRTIMER_MODE_ABS_PINNED);
+}
+
+/**
+ * kvm_init_timer() - Initialise stable timer.
+ * @vcpu:	Virtual CPU.
+ * @timer_hz:	Frequency of timer.
+ *
+ * Initialise the timer to the specified frequency, zero it, and set it going if
+ * it's enabled.
+ */
+void kvm_init_timer(struct kvm_vcpu *vcpu, unsigned long timer_hz)
+{
+	ktime_t now;
+	unsigned long ticks;
+	struct loongarch_csrs *csr = vcpu->arch.csr;
+
+	ticks = (unsigned long)MNSEC_PER_SEC * CSR_TCFG_VAL;
+	vcpu->arch.timer_mhz = timer_hz >> 20;
+	vcpu->arch.timer_period = div_u64(ticks, vcpu->arch.timer_mhz);
+	vcpu->arch.timer_dyn_bias = 0;
+
+	/* Starting at 0 */
+	ticks = 0;
+	now = ktime_get();
+	vcpu->arch.timer_bias = ticks - ktime_to_tick(vcpu, now);
+	vcpu->arch.timer_bias &= CSR_TCFG_VAL;
+	kvm_write_sw_gcsr(csr, LOONGARCH_CSR_TVAL, ticks);
+}
+
+/**
+ * kvm_count_timeout() - Push timer forward on timeout.
+ * @vcpu:	Virtual CPU.
+ *
+ * Handle an hrtimer event by push the hrtimer forward a period.
+ *
+ * Returns:	The hrtimer_restart value to return to the hrtimer subsystem.
+ */
+enum hrtimer_restart kvm_count_timeout(struct kvm_vcpu *vcpu)
+{
+	unsigned long cfg;
+
+	/* Add the Count period to the current expiry time */
+	cfg = kvm_read_sw_gcsr(vcpu->arch.csr, LOONGARCH_CSR_TCFG);
+	if (cfg & CSR_TCFG_PERIOD) {
+		hrtimer_add_expires_ns(&vcpu->arch.swtimer, cfg & CSR_TCFG_VAL);
+		return HRTIMER_RESTART;
+	} else
+		return HRTIMER_NORESTART;
+}
+
+/*
+ * kvm_restore_timer() - Restore timer state.
+ * @vcpu:       Virtual CPU.
+ *
+ * Restore soft timer state from saved context.
+ */
+void kvm_restore_timer(struct kvm_vcpu *vcpu)
+{
+	struct loongarch_csrs *csr = vcpu->arch.csr;
+	ktime_t saved_ktime, now;
+	unsigned long val, new, delta;
+	int expired = 0;
+	unsigned long cfg;
+
+	/*
+	 * Set guest stable timer cfg csr
+	 */
+	cfg = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_TCFG);
+	kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_ESTAT);
+	if (!(cfg & CSR_TCFG_EN)) {
+		kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_TCFG);
+		kvm_restore_hw_gcsr(csr, LOONGARCH_CSR_TVAL);
+		return;
+	}
+
+	now = ktime_get();
+	saved_ktime = vcpu->arch.stable_ktime_saved;
+	val = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_TVAL);
+
+	/*hrtimer not expire */
+	delta = ktime_to_tick(vcpu, ktime_sub(now, saved_ktime));
+	if (delta >= val) {
+		expired = 1;
+		if (cfg & CSR_TCFG_PERIOD)
+			new = (delta - val) % (cfg & CSR_TCFG_VAL);
+		else
+			new = 1;
+	} else
+		new = val - delta;
+
+	new &= CSR_TCFG_VAL;
+	write_gcsr_timercfg(cfg);
+	write_gcsr_timertick(new);
+	if (expired)
+		_kvm_queue_irq(vcpu, LARCH_INT_TIMER);
+}
+
+/*
+ * kvm_acquire_timer() - Switch to hard timer state.
+ * @vcpu:       Virtual CPU.
+ *
+ * Restore hard timer state on top of existing soft timer state if possible.
+ *
+ * Since hard timer won't remain active over preemption, preemption should be
+ * disabled by the caller.
+ */
+void kvm_acquire_timer(struct kvm_vcpu *vcpu)
+{
+	unsigned long flags, guestcfg;
+
+	guestcfg = read_csr_gcfg();
+	if (!(guestcfg & CSR_GCFG_TIT))
+		return;
+
+	/* enable guest access to hard timer */
+	write_csr_gcfg(guestcfg & ~CSR_GCFG_TIT);
+
+	/*
+	 * Freeze the soft-timer and sync the guest stable timer with it. We do
+	 * this with interrupts disabled to avoid latency.
+	 */
+	local_irq_save(flags);
+	hrtimer_cancel(&vcpu->arch.swtimer);
+	local_irq_restore(flags);
+}
+
+
+/*
+ * _kvm_save_timer() - Switch to software emulation of guest timer.
+ * @vcpu:       Virtual CPU.
+ *
+ * Save guest timer state and switch to software emulation of guest
+ * timer. The hard timer must already be in use, so preemption should be
+ * disabled.
+ */
+static ktime_t _kvm_save_timer(struct kvm_vcpu *vcpu, unsigned long *val)
+{
+	unsigned long end_time;
+	ktime_t before_time;
+
+	before_time = ktime_get();
+
+	/*
+	 * Record a final stable timer which we will transfer to the soft-timer.
+	 */
+	end_time = read_gcsr_timertick();
+	*val = end_time;
+
+	kvm_resume_hrtimer(vcpu, before_time, end_time);
+	return before_time;
+}
+
+/*
+ * kvm_save_timer() - Save guest timer state.
+ * @vcpu:       Virtual CPU.
+ *
+ * Save guest timer state and switch to soft guest timer if hard timer was in
+ * use.
+ */
+void kvm_save_timer(struct kvm_vcpu *vcpu)
+{
+	struct loongarch_csrs *csr = vcpu->arch.csr;
+	unsigned long guestcfg, val;
+	ktime_t save_ktime;
+
+	preempt_disable();
+	guestcfg = read_csr_gcfg();
+	if (!(guestcfg & CSR_GCFG_TIT)) {
+		/* disable guest use of hard timer */
+		write_csr_gcfg(guestcfg | CSR_GCFG_TIT);
+
+		/* save hard timer state */
+		kvm_save_hw_gcsr(csr, LOONGARCH_CSR_TCFG);
+		if (kvm_read_sw_gcsr(csr, LOONGARCH_CSR_TCFG) & CSR_TCFG_EN) {
+			save_ktime = _kvm_save_timer(vcpu, &val);
+			kvm_write_sw_gcsr(csr, LOONGARCH_CSR_TVAL, val);
+			vcpu->arch.stable_ktime_saved = save_ktime;
+			if (val == CSR_TCFG_VAL)
+				_kvm_queue_irq(vcpu, LARCH_INT_TIMER);
+		} else {
+			kvm_save_hw_gcsr(csr, LOONGARCH_CSR_TVAL);
+		}
+	}
+
+	/* save timer-related state to VCPU context */
+	kvm_save_hw_gcsr(csr, LOONGARCH_CSR_ESTAT);
+	preempt_enable();
+}
+
+void kvm_reset_timer(struct kvm_vcpu *vcpu)
+{
+	write_gcsr_timercfg(0);
+	kvm_write_sw_gcsr(vcpu->arch.csr, LOONGARCH_CSR_TCFG, 0);
+	hrtimer_cancel(&vcpu->arch.swtimer);
+}
-- 
2.31.1


  parent reply	other threads:[~2023-02-20  6:57 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-20  6:57 [PATCH v2 00/29] Add KVM LoongArch support Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 01/29] LoongArch: KVM: Add kvm related header files Tianrui Zhao
2023-02-20 18:22   ` Paolo Bonzini
2023-02-21  2:56     ` Tianrui Zhao
2023-02-21  6:49       ` Paolo Bonzini
2023-02-20 18:54   ` WANG Xuerui
2023-02-21  4:36   ` Xi Ruoyao
2023-02-24  1:27     ` Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 02/29] LoongArch: KVM: Implement kvm module related interface Tianrui Zhao
2023-02-20 17:46   ` Paolo Bonzini
2023-02-21  3:02     ` Tianrui Zhao
2023-02-21  6:59     ` maobibo
2023-02-21  8:14       ` Paolo Bonzini
2023-02-21 10:18         ` maobibo
2023-02-21 10:37           ` WANG Xuerui
2023-02-21 11:39             ` maobibo
2023-02-21 12:38               ` WANG Xuerui
2023-02-20  6:57 ` [PATCH v2 03/29] LoongArch: KVM: Implement kvm hardware enable, disable interface Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 04/29] LoongArch: KVM: Implement VM related functions Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 05/29] LoongArch: KVM: Add vcpu related header files Tianrui Zhao
2023-02-20 18:57   ` WANG Xuerui
2023-02-27  1:39     ` Tianrui Zhao
2023-02-21  4:44   ` Xi Ruoyao
2023-02-21  6:46     ` maobibo
2023-02-21  6:48       ` Paolo Bonzini
2023-02-21  7:12       ` Xi Ruoyao
2023-02-21  7:35         ` Paolo Bonzini
2023-02-20  6:57 ` [PATCH v2 06/29] LoongArch: KVM: Implement vcpu create and destroy interface Tianrui Zhao
2023-02-20 17:53   ` Paolo Bonzini
2023-02-22  1:52     ` Tianrui Zhao
2023-02-22 12:17       ` Paolo Bonzini
2023-02-23  1:23         ` Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 07/29] LoongArch: KVM: Implement vcpu run interface Tianrui Zhao
2023-02-20 18:44   ` Paolo Bonzini
2023-02-22  2:08     ` Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 08/29] LoongArch: KVM: Implement vcpu handle exit interface Tianrui Zhao
2023-02-20 17:46   ` Paolo Bonzini
2023-02-20 18:45   ` Paolo Bonzini
2023-02-21  3:17     ` Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 09/29] LoongArch: KVM: Implement vcpu get, vcpu set registers Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 10/29] LoongArch: KVM: Implement vcpu ENABLE_CAP, CHECK_EXTENSION ioctl interface Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 11/29] LoongArch: KVM: Implement fpu related operations for vcpu Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 12/29] LoongArch: KVM: Implement vcpu interrupt operations Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 13/29] LoongArch: KVM: Implement misc vcpu related interfaces Tianrui Zhao
2023-02-20 18:50   ` Paolo Bonzini
2023-02-21  3:19     ` Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 14/29] LoongArch: KVM: Implement vcpu load and vcpu put operations Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 15/29] LoongArch: KVM: Implement vcpu status description Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 16/29] LoongArch: KVM: Implement update VM id function Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 17/29] LoongArch: KVM: Implement virtual machine tlb operations Tianrui Zhao
2023-02-20  6:57 ` Tianrui Zhao [this message]
2023-02-20  6:57 ` [PATCH v2 19/29] LoongArch: KVM: Implement kvm mmu operations Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 20/29] LoongArch: KVM: Implement handle csr excption Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 21/29] LoongArch: KVM: Implement handle iocsr exception Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 22/29] LoongArch: KVM: Implement handle idle exception Tianrui Zhao
2023-02-20 18:40   ` Paolo Bonzini
2023-02-21  9:48     ` Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 23/29] LoongArch: KVM: Implement handle gspr exception Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 24/29] LoongArch: KVM: Implement handle mmio exception Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 25/29] LoongArch: KVM: Implement handle fpu exception Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 26/29] LoongArch: KVM: Implement kvm exception vector Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 27/29] LoongArch: KVM: Implement vcpu world switch Tianrui Zhao
2023-02-21  7:45   ` Paolo Bonzini
2023-02-21 13:00     ` Tianrui Zhao
2023-02-21  8:18   ` Paolo Bonzini
2023-02-21 12:58     ` Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 28/29] LoongArch: KVM: Implement probe virtualization when loongarch cpu init Tianrui Zhao
2023-02-20  6:57 ` [PATCH v2 29/29] LoongArch: KVM: Enable kvm config and add the makefile Tianrui Zhao
2023-02-20  9:47   ` kernel test robot
2023-02-20 11:09   ` kernel test robot

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=20230220065735.1282809-19-zhaotianrui@loongson.cn \
    --to=zhaotianrui@loongson.cn \
    --cc=alexander.deucher@amd.com \
    --cc=axboe@kernel.dk \
    --cc=broonie@kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=kernel@xen0n.name \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=maobibo@loongson.cn \
    --cc=oliver.upton@linux.dev \
    --cc=pbonzini@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.