From: Bibo Mao <maobibo@loongson.cn>
To: Song Gao <gaosong@loongson.cn>, chenhuacai@kernel.org
Cc: kvm@vger.kernel.org, loongarch@lists.linux.dev,
kernel@xen0n.name, linux-kernel@vger.kernel.org,
lixianglai@loongson.cn
Subject: Re: [PATCH v3 2/4] LongArch: KVM: Add DINTC device support
Date: Mon, 8 Dec 2025 11:54:40 +0800 [thread overview]
Message-ID: <81aaba55-daf2-e455-fd0f-a0b62cc38015@loongson.cn> (raw)
In-Reply-To: <20251206064658.714100-3-gaosong@loongson.cn>
On 2025/12/6 下午2:46, Song Gao wrote:
> Add device model for AVEC interrupt controller, implement basic
> create/destroy/set_attr interfaces, and register device model to kvm
> device table.
>
> Signed-off-by: Song Gao <gaosong@loongson.cn>
> ---
> arch/loongarch/include/asm/kvm_dintc.h | 21 +++++
> arch/loongarch/include/asm/kvm_host.h | 3 +
> arch/loongarch/include/uapi/asm/kvm.h | 4 +
> arch/loongarch/kvm/Makefile | 1 +
> arch/loongarch/kvm/intc/dintc.c | 110 +++++++++++++++++++++++++
> arch/loongarch/kvm/main.c | 5 ++
> include/uapi/linux/kvm.h | 2 +
> 7 files changed, 146 insertions(+)
> create mode 100644 arch/loongarch/include/asm/kvm_dintc.h
> create mode 100644 arch/loongarch/kvm/intc/dintc.c
>
> diff --git a/arch/loongarch/include/asm/kvm_dintc.h b/arch/loongarch/include/asm/kvm_dintc.h
> new file mode 100644
> index 000000000000..d980d39c0344
> --- /dev/null
> +++ b/arch/loongarch/include/asm/kvm_dintc.h
> @@ -0,0 +1,21 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Copyright (C) 2025 Loongson Technology Corporation Limited
> + */
> +
> +#ifndef __ASM_KVM_DINTC_H
> +#define __ASM_KVM_DINTC_H
> +
> +
> +struct loongarch_dintc {
> + struct kvm *kvm;
> + uint64_t msg_addr_base;
> + uint64_t msg_addr_size;
> +};
> +
> +struct dintc_state {
> + atomic64_t vector_map[4];
> +};
> +
> +int kvm_loongarch_register_dintc_device(void);
> +#endif
> diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/include/asm/kvm_host.h
> index e4fe5b8e8149..26a90f4146ac 100644
> --- a/arch/loongarch/include/asm/kvm_host.h
> +++ b/arch/loongarch/include/asm/kvm_host.h
> @@ -22,6 +22,7 @@
> #include <asm/kvm_ipi.h>
> #include <asm/kvm_eiointc.h>
> #include <asm/kvm_pch_pic.h>
> +#include <asm/kvm_dintc.h>
> #include <asm/loongarch.h>
>
> #define __KVM_HAVE_ARCH_INTC_INITIALIZED
> @@ -134,6 +135,7 @@ struct kvm_arch {
> struct loongarch_ipi *ipi;
> struct loongarch_eiointc *eiointc;
> struct loongarch_pch_pic *pch_pic;
> + struct loongarch_dintc *dintc;
> };
>
> #define CSR_MAX_NUMS 0x800
> @@ -244,6 +246,7 @@ struct kvm_vcpu_arch {
> struct kvm_mp_state mp_state;
> /* ipi state */
> struct ipi_state ipi_state;
> + struct dintc_state dintc_state;
> /* cpucfg */
> u32 cpucfg[KVM_MAX_CPUCFG_REGS];
>
> diff --git a/arch/loongarch/include/uapi/asm/kvm.h b/arch/loongarch/include/uapi/asm/kvm.h
> index de6c3f18e40a..07da84f7002c 100644
> --- a/arch/loongarch/include/uapi/asm/kvm.h
> +++ b/arch/loongarch/include/uapi/asm/kvm.h
> @@ -154,4 +154,8 @@ struct kvm_iocsr_entry {
> #define KVM_DEV_LOONGARCH_PCH_PIC_GRP_CTRL 0x40000006
> #define KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT 0
>
> +#define KVM_DEV_LOONGARCH_DINTC_CTRL 0x40000007
> +#define KVM_DEV_LOONGARCH_DINTC_MSG_ADDR_BASE 0x0
> +#define KVM_DEV_LOONGARCH_DINTC_MSG_ADDR_SIZE 0x1
> +
> #endif /* __UAPI_ASM_LOONGARCH_KVM_H */
> diff --git a/arch/loongarch/kvm/Makefile b/arch/loongarch/kvm/Makefile
> index cb41d9265662..fe984bf1cbdb 100644
> --- a/arch/loongarch/kvm/Makefile
> +++ b/arch/loongarch/kvm/Makefile
> @@ -19,6 +19,7 @@ kvm-y += vm.o
> kvm-y += intc/ipi.o
> kvm-y += intc/eiointc.o
> kvm-y += intc/pch_pic.o
> +kvm-y += intc/dintc.o
> kvm-y += irqfd.o
>
> CFLAGS_exit.o += $(call cc-disable-warning, override-init)
> diff --git a/arch/loongarch/kvm/intc/dintc.c b/arch/loongarch/kvm/intc/dintc.c
> new file mode 100644
> index 000000000000..cd6cc9392adc
> --- /dev/null
> +++ b/arch/loongarch/kvm/intc/dintc.c
> @@ -0,0 +1,110 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (C) 2025 Loongson Technology Corporation Limited
> + */
> +
> +#include <linux/kvm_host.h>
> +#include <asm/kvm_dintc.h>
> +#include <asm/kvm_vcpu.h>
> +
> +static int kvm_dintc_ctrl_access(struct kvm_device *dev,
> + struct kvm_device_attr *attr,
> + bool is_write)
> +{
> + int addr = attr->attr;
> + void __user *data;
> + struct loongarch_dintc *s = dev->kvm->arch.dintc;
> + u64 tmp;
> +
> + data = (void __user *)attr->addr;
> + switch (addr) {
> + case KVM_DEV_LOONGARCH_DINTC_MSG_ADDR_BASE:
> + if (is_write) {
> + if (copy_from_user(&tmp, data, sizeof(s->msg_addr_base)))
> + return -EFAULT;
> + if (s->msg_addr_base) {
> + /* Duplicate setting are not allowed. */
> + return -EFAULT;
> + }
> + if (tmp > (1UL << AVEC_CPU_SHIFT))
The address check should be (tmp & (BIT(AVEC_CPU_SHIFT) - 1) == 0 ?
otherwise look good to me.
Reviewed-by: Bibo Mao <maobibo@loongson.cn>
> + s->msg_addr_base = tmp;
> + else
> + return -EFAULT;
> + }
> + break;
> + case KVM_DEV_LOONGARCH_DINTC_MSG_ADDR_SIZE:
> + if (is_write) {
> + if (copy_from_user(&tmp, data, sizeof(s->msg_addr_size)))
> + return -EFAULT;
> + if (s->msg_addr_size) {
> + /*Duplicate setting are not allowed. */
> + return -EFAULT;
> + }
> + s->msg_addr_size = tmp;
> + }
> + break;
> + default:
> + kvm_err("%s: unknown dintc register, addr = %d\n", __func__, addr);
> + return -ENXIO;
> + }
> +
> + return 0;
> +}
> +
> +static int kvm_dintc_set_attr(struct kvm_device *dev,
> + struct kvm_device_attr *attr)
> +{
> + switch (attr->group) {
> + case KVM_DEV_LOONGARCH_DINTC_CTRL:
> + return kvm_dintc_ctrl_access(dev, attr, true);
> + default:
> + kvm_err("%s: unknown group (%d)\n", __func__, attr->group);
> + return -EINVAL;
> + }
> +}
> +
> +static int kvm_dintc_create(struct kvm_device *dev, u32 type)
> +{
> + struct kvm *kvm;
> + struct loongarch_dintc *s;
> +
> + if (!dev) {
> + kvm_err("%s: kvm_device ptr is invalid!\n", __func__);
> + return -EINVAL;
> + }
> +
> + kvm = dev->kvm;
> + if (kvm->arch.dintc) {
> + kvm_err("%s: LoongArch DINTC has already been created!\n", __func__);
> + return -EINVAL;
> + }
> +
> + s = kzalloc(sizeof(struct loongarch_dintc), GFP_KERNEL);
> + if (!s)
> + return -ENOMEM;
> +
> + s->kvm = kvm;
> + kvm->arch.dintc = s;
> + return 0;
> +}
> +
> +static void kvm_dintc_destroy(struct kvm_device *dev)
> +{
> +
> + if (!dev || !dev->kvm || !dev->kvm->arch.dintc)
> + return;
> +
> + kfree(dev->kvm->arch.dintc);
> +}
> +
> +static struct kvm_device_ops kvm_dintc_dev_ops = {
> + .name = "kvm-loongarch-dintc",
> + .create = kvm_dintc_create,
> + .destroy = kvm_dintc_destroy,
> + .set_attr = kvm_dintc_set_attr,
> +};
> +
> +int kvm_loongarch_register_dintc_device(void)
> +{
> + return kvm_register_device_ops(&kvm_dintc_dev_ops, KVM_DEV_TYPE_LOONGARCH_DINTC);
> +}
> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c
> index 80ea63d465b8..d18d9f4d485c 100644
> --- a/arch/loongarch/kvm/main.c
> +++ b/arch/loongarch/kvm/main.c
> @@ -408,6 +408,11 @@ static int kvm_loongarch_env_init(void)
>
> /* Register LoongArch PCH-PIC interrupt controller interface. */
> ret = kvm_loongarch_register_pch_pic_device();
> + if (ret)
> + return ret;
> +
> + /* Register LoongArch DINTC interrupt contrroller interface */
> + ret = kvm_loongarch_register_dintc_device();
>
> return ret;
> }
> diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
> index dddb781b0507..ebf86a65eef1 100644
> --- a/include/uapi/linux/kvm.h
> +++ b/include/uapi/linux/kvm.h
> @@ -1209,6 +1209,8 @@ enum kvm_device_type {
> #define KVM_DEV_TYPE_LOONGARCH_EIOINTC KVM_DEV_TYPE_LOONGARCH_EIOINTC
> KVM_DEV_TYPE_LOONGARCH_PCHPIC,
> #define KVM_DEV_TYPE_LOONGARCH_PCHPIC KVM_DEV_TYPE_LOONGARCH_PCHPIC
> + KVM_DEV_TYPE_LOONGARCH_DINTC,
> +#define KVM_DEV_TYPE_LOONGARCH_DINTC KVM_DEV_TYPE_LOONGARCH_DINTC
>
> KVM_DEV_TYPE_MAX,
>
>
next prev parent reply other threads:[~2025-12-08 3:57 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-06 6:46 [PATCH v3 0/4] LongArch: KVM: Add AVEC support irqchip in kernel Song Gao
2025-12-06 6:46 ` [PATCH v3 1/4] LongArch: KVM: Add some maccros for AVEC Song Gao
2025-12-08 3:45 ` Bibo Mao
2025-12-06 6:46 ` [PATCH v3 2/4] LongArch: KVM: Add DINTC device support Song Gao
2025-12-08 3:54 ` Bibo Mao [this message]
2025-12-06 6:46 ` [PATCH v3 3/4] LongArch: KVM: Add irqfd set dintc msi Song Gao
2025-12-08 3:58 ` Bibo Mao
2025-12-06 6:46 ` [PATCH v3 4/4] LongArch: KVM: Add dintc inject msi to the dest vcpu Song Gao
2025-12-08 4:04 ` Bibo Mao
2025-12-13 12:39 ` [PATCH v3 0/4] LongArch: KVM: Add AVEC support irqchip in kernel Huacai Chen
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=81aaba55-daf2-e455-fd0f-a0b62cc38015@loongson.cn \
--to=maobibo@loongson.cn \
--cc=chenhuacai@kernel.org \
--cc=gaosong@loongson.cn \
--cc=kernel@xen0n.name \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lixianglai@loongson.cn \
--cc=loongarch@lists.linux.dev \
/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®