All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
From: Tianrui Zhao <zhaotianrui@loongson.cn>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Huacai Chen <chenhuacai@kernel.org>,
	WANG Xuerui <kernel@xen0n.name>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	loongarch@lists.linux.dev, 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, Xi Ruoyao <xry111@xry111.site>,
	zhaotianrui@loongson.cn
Subject: [PATCH v12 12/31] LoongArch: KVM: Implement vcpu interrupt operations
Date: Tue, 30 May 2023 09:52:04 +0800	[thread overview]
Message-ID: <20230530015223.147755-13-zhaotianrui@loongson.cn> (raw)
In-Reply-To: <20230530015223.147755-1-zhaotianrui@loongson.cn>

Implement vcpu interrupt operations such as vcpu set irq and
vcpu clear irq, using set_gcsr_estat to set irq which is
parsed by the irq bitmap.

Signed-off-by: Tianrui Zhao <zhaotianrui@loongson.cn>
---
 arch/loongarch/kvm/interrupt.c | 127 +++++++++++++++++++++++++++++++++
 arch/loongarch/kvm/vcpu.c      |  45 ++++++++++++
 2 files changed, 172 insertions(+)
 create mode 100644 arch/loongarch/kvm/interrupt.c

diff --git a/arch/loongarch/kvm/interrupt.c b/arch/loongarch/kvm/interrupt.c
new file mode 100644
index 000000000000..243bb19b387e
--- /dev/null
+++ b/arch/loongarch/kvm/interrupt.c
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
+ */
+
+#include <linux/errno.h>
+#include <linux/err.h>
+#include <asm/kvm_vcpu.h>
+#include <asm/kvm_csr.h>
+
+static unsigned int int_to_coreint[EXCCODE_INT_NUM] = {
+	[INT_TI]	= CPU_TIMER,
+	[INT_IPI]	= CPU_IPI,
+	[INT_SWI0]	= CPU_SIP0,
+	[INT_SWI1]	= CPU_SIP1,
+	[INT_HWI0]	= CPU_IP0,
+	[INT_HWI1]	= CPU_IP1,
+	[INT_HWI2]	= CPU_IP2,
+	[INT_HWI3]	= CPU_IP3,
+	[INT_HWI4]	= CPU_IP4,
+	[INT_HWI5]	= CPU_IP5,
+	[INT_HWI6]	= CPU_IP6,
+	[INT_HWI7]	= CPU_IP7,
+};
+
+static int _kvm_irq_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
+{
+	unsigned int irq = 0;
+
+	clear_bit(priority, &vcpu->arch.irq_pending);
+	if (priority < EXCCODE_INT_NUM)
+		irq = int_to_coreint[priority];
+
+	switch (priority) {
+	case INT_TI:
+	case INT_IPI:
+	case INT_SWI0:
+	case INT_SWI1:
+		set_gcsr_estat(irq);
+		break;
+
+	case INT_HWI0:
+	case INT_HWI1:
+	case INT_HWI2:
+	case INT_HWI3:
+	case INT_HWI4:
+	case INT_HWI5:
+	case INT_HWI6:
+	case INT_HWI7:
+		set_csr_gintc(irq);
+		break;
+
+	default:
+		break;
+	}
+
+	return 1;
+}
+
+static int _kvm_irq_clear(struct kvm_vcpu *vcpu, unsigned int priority)
+{
+	unsigned int irq = 0;
+
+	clear_bit(priority, &vcpu->arch.irq_clear);
+	if (priority < EXCCODE_INT_NUM)
+		irq = int_to_coreint[priority];
+
+	switch (priority) {
+	case INT_TI:
+	case INT_IPI:
+	case INT_SWI0:
+	case INT_SWI1:
+		clear_gcsr_estat(irq);
+		break;
+
+	case INT_HWI0:
+	case INT_HWI1:
+	case INT_HWI2:
+	case INT_HWI3:
+	case INT_HWI4:
+	case INT_HWI5:
+	case INT_HWI6:
+	case INT_HWI7:
+		clear_csr_gintc(irq);
+		break;
+
+	default:
+		break;
+	}
+
+	return 1;
+}
+
+void _kvm_deliver_intr(struct kvm_vcpu *vcpu)
+{
+	unsigned long *pending = &vcpu->arch.irq_pending;
+	unsigned long *pending_clr = &vcpu->arch.irq_clear;
+	unsigned int priority;
+
+	if (!(*pending) && !(*pending_clr))
+		return;
+
+	if (*pending_clr) {
+		priority = __ffs(*pending_clr);
+		while (priority <= INT_IPI) {
+			_kvm_irq_clear(vcpu, priority);
+			priority = find_next_bit(pending_clr,
+					BITS_PER_BYTE * sizeof(*pending_clr),
+					priority + 1);
+		}
+	}
+
+	if (*pending) {
+		priority = __ffs(*pending);
+		while (priority <= INT_IPI) {
+			_kvm_irq_deliver(vcpu, priority);
+			priority = find_next_bit(pending,
+					BITS_PER_BYTE * sizeof(*pending),
+					priority + 1);
+		}
+	}
+}
+
+int _kvm_pending_timer(struct kvm_vcpu *vcpu)
+{
+	return test_bit(INT_TI, &vcpu->arch.irq_pending);
+}
diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
index 60213f7f7bac..f661743dbe66 100644
--- a/arch/loongarch/kvm/vcpu.c
+++ b/arch/loongarch/kvm/vcpu.c
@@ -304,6 +304,51 @@ void kvm_lose_fpu(struct kvm_vcpu *vcpu)
 	preempt_enable();
 }
 
+int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
+			     struct kvm_loongarch_interrupt *irq)
+{
+	int intr = (int)irq->irq;
+	struct kvm_vcpu *dvcpu = NULL;
+
+	if (irq->cpu == -1)
+		dvcpu = vcpu;
+	else
+		dvcpu = kvm_get_vcpu(vcpu->kvm, irq->cpu);
+
+	if (intr > 0)
+		_kvm_queue_irq(dvcpu, intr);
+	else if (intr < 0)
+		_kvm_dequeue_irq(dvcpu, -intr);
+	else {
+		kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__,
+				irq->cpu, irq->irq);
+		return -EINVAL;
+	}
+
+	kvm_vcpu_kick(dvcpu);
+	return 0;
+}
+
+long kvm_arch_vcpu_async_ioctl(struct file *filp,
+			       unsigned int ioctl, unsigned long arg)
+{
+	struct kvm_vcpu *vcpu = filp->private_data;
+	void __user *argp = (void __user *)arg;
+
+	if (ioctl == KVM_INTERRUPT) {
+		struct kvm_loongarch_interrupt irq;
+
+		if (copy_from_user(&irq, argp, sizeof(irq)))
+			return -EFAULT;
+		kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__,
+			  irq.irq);
+
+		return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
+	}
+
+	return -ENOIOCTLCMD;
+}
+
 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
 {
 	return 0;
-- 
2.39.1


  parent reply	other threads:[~2023-05-30  1:53 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-30  1:51 [PATCH v12 00/31] Add KVM LoongArch support Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 01/31] LoongArch: KVM: Add kvm related header files Tianrui Zhao
2023-06-05  5:00   ` Huacai Chen
2023-06-05 10:18     ` Paolo Bonzini
2023-06-05 12:22   ` bibo, mao
2023-06-06  6:30   ` Youling Tang
2023-06-06 10:00     ` Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 02/31] LoongArch: KVM: Implement kvm module related interface Tianrui Zhao
2023-06-05 12:27   ` bibo, mao
2023-05-30  1:51 ` [PATCH v12 03/31] LoongArch: KVM: Implement kvm hardware enable, disable interface Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 04/31] LoongArch: KVM: Implement VM related functions Tianrui Zhao
2023-06-05 12:31   ` bibo, mao
2023-05-30  1:51 ` [PATCH v12 05/31] LoongArch: KVM: Add vcpu related header files Tianrui Zhao
2023-06-05 12:42   ` bibo, mao
2023-06-06  2:00     ` Tianrui Zhao
2023-05-30  1:51 ` [PATCH v12 06/31] LoongArch: KVM: Implement vcpu create and destroy interface Tianrui Zhao
2023-06-05 12:44   ` bibo, mao
2023-05-30  1:51 ` [PATCH v12 07/31] LoongArch: KVM: Implement vcpu run interface Tianrui Zhao
2023-06-05 12:53   ` bibo, mao
2023-06-06  2:02     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 08/31] LoongArch: KVM: Implement vcpu handle exit interface Tianrui Zhao
2023-06-05 13:03   ` bibo, mao
2023-06-06  2:13     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 09/31] LoongArch: KVM: Implement vcpu get, vcpu set registers Tianrui Zhao
2023-06-05 13:04   ` bibo, mao
2023-06-06  6:41   ` Youling Tang
2023-06-06  9:12     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 10/31] LoongArch: KVM: Implement vcpu ENABLE_CAP ioctl interface Tianrui Zhao
2023-06-05 13:12   ` bibo, mao
2023-06-06  3:20     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 11/31] LoongArch: KVM: Implement fpu related operations for vcpu Tianrui Zhao
2023-06-05 13:13   ` bibo, mao
2023-05-30  1:52 ` Tianrui Zhao [this message]
2023-06-05 13:13   ` [PATCH v12 12/31] LoongArch: KVM: Implement vcpu interrupt operations bibo, mao
2023-06-06  6:49   ` Youling Tang
2023-06-06  9:13     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 13/31] LoongArch: KVM: Implement misc vcpu related interfaces Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 14/31] LoongArch: KVM: Implement vcpu load and vcpu put operations Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 15/31] LoongArch: KVM: Implement vcpu status description Tianrui Zhao
2023-06-05 13:04   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 16/31] LoongArch: KVM: Implement update VM id function Tianrui Zhao
2023-06-06  1:13   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 17/31] LoongArch: KVM: Implement virtual machine tlb operations Tianrui Zhao
2023-06-06  1:20   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 18/31] LoongArch: KVM: Implement vcpu timer operations Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 19/31] LoongArch: KVM: Implement kvm mmu operations Tianrui Zhao
2023-06-06  1:28   ` bibo, mao
2023-06-06  2:51     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 20/31] LoongArch: KVM: Implement handle csr excption Tianrui Zhao
2023-06-06  1:35   ` bibo, mao
2023-06-06  2:59     ` Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 21/31] LoongArch: KVM: Implement handle iocsr exception Tianrui Zhao
2023-06-06  1:36   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 22/31] LoongArch: KVM: Implement handle idle exception Tianrui Zhao
2023-06-06  1:36   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 23/31] LoongArch: KVM: Implement handle gspr exception Tianrui Zhao
2023-06-06  1:38   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 24/31] LoongArch: KVM: Implement handle mmio exception Tianrui Zhao
2023-06-06  1:41   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 25/31] LoongArch: KVM: Implement handle fpu exception Tianrui Zhao
2023-06-06  1:42   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 26/31] LoongArch: KVM: Implement kvm exception vector Tianrui Zhao
2023-06-06  1:43   ` bibo, mao
2023-06-06  7:00   ` Youling Tang
2023-06-06  9:16     ` Tianrui Zhao
2023-06-08 11:44     ` 赵天瑞
2023-05-30  1:52 ` [PATCH v12 27/31] LoongArch: KVM: Implement vcpu world switch Tianrui Zhao
2023-06-06  2:27   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 28/31] LoongArch: KVM: Implement probe virtualization when LoongArch cpu init Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 29/31] LoongArch: KVM: Enable kvm config and add the makefile Tianrui Zhao
2023-06-06  2:37   ` bibo, mao
2023-05-30  1:52 ` [PATCH v12 30/31] LoongArch: KVM: Supplement kvm document about LoongArch-specific part Tianrui Zhao
2023-05-30  1:52 ` [PATCH v12 31/31] LoongArch: KVM: Add maintainers for LoongArch KVM Tianrui Zhao

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=20230530015223.147755-13-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 \
    --cc=xry111@xry111.site \
    /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.