Add dintc kvm_irqchip_in_kernel support
Function kvm_dintc_realize() is added if kvm_irqchip_in_kernel is set. It is to create and initialize DINTC device in kernel mode. and use kvm_irqchip_send_msi() to send msi to kernel. Signed-off-by: Song Gao <gaosong@loongson.cn> Reviewed-by: Bibo Mao <maobibo@loongson.cn> Message-ID: <20260813111000.446232-3-gaosong@loongson.cn>
Song Gao committed
Aug 13, 2026 at 19:09 UTC
06e75b3923a4dc265155a2859c33fc4405718771
4 files changed
+73
hw/intc/loongarch_dintc.c
+13
@@ -68,6 +68,15 @@ static void loongarch_dintc_mem_write(void *opaque, hwaddr addr,
68
}
69
irq_num = FIELD_EX64(msg_addr, MSG_ADDR, IRQ_NUM);
70
71
+ if (kvm_irqchip_in_kernel()) {
72
+ MSIMessage msg;
73
+
74
+ msg.address = msg_addr;
75
+ msg.data = val;
76
+ kvm_irqchip_send_msi(kvm_state, msg);
77
+ return;
78
+ }
79
+
80
async_run_on_cpu(cs, do_set_vcpu_dintc_irq,
81
RUN_ON_CPU_HOST_INT(irq_num));
82
qemu_set_irq(s->cpu[cpu_num].parent_irq, 1);
@@ -110,6 +119,10 @@ static void loongarch_dintc_realize(DeviceState *dev, Error **errp)
119
qdev_init_gpio_out(dev, &s->cpu[i].parent_irq, 1);
120
}
121
122
+ if (kvm_irqchip_in_kernel()) {
123
+ kvm_dintc_realize(dev, errp);
124
+ }
125
+
126
return;
127
}
128
hw/intc/loongarch_dintc_kvm.c
new
+48
@@ -0,0 +1,48 @@
1
+/* SPDX-License-Identifier: GPL-2.0-or-later */
2
+/*
3
+ * LoongArch DINTC interrupt kvm support
4
+ *
5
+ * Copyright (C) 2025 Loongson Technology Corporation Limited
6
+ */
7
+
8
+#include "qemu/osdep.h"
9
+#include "hw/intc/loongarch_dintc.h"
10
+#include "linux/kvm.h"
11
+#include "qapi/error.h"
12
+#include "system/kvm.h"
13
+
14
+void kvm_dintc_realize(DeviceState *dev, Error **errp)
15
+{
16
+ LoongArchDINTCState *lds = LOONGARCH_DINTC(dev);
17
+ int ret;
18
+
19
+ ret = kvm_create_device(kvm_state, KVM_DEV_TYPE_LOONGARCH_DMSINTC, false);
20
+ if (ret < 0) {
21
+ fprintf(stderr, "create KVM_DEV_TYPE_LOONGARCH_AVEC failed: %s\n",
22
+ strerror(-ret));
23
+ abort();
24
+ }
25
+ lds->dev_fd = ret;
26
+
27
+ /* init dintc config */
28
+ lds->msg_addr_base = VIRT_DINTC_BASE;
29
+ lds->msg_addr_size = VIRT_DINTC_SIZE;
30
+
31
+ ret = kvm_device_access(lds->dev_fd, KVM_DEV_LOONGARCH_DMSINTC_GRP_CTRL,
32
+ KVM_DEV_LOONGARCH_DMSINTC_MSG_ADDR_BASE,
33
+ &lds->msg_addr_base, true, NULL);
34
+ if (ret < 0) {
35
+ fprintf(stderr, "KVM_DEV_LOONGARCH_DINTC_MSG_ADDR_BASE failed: %s\n",
36
+ strerror(ret));
37
+ abort();
38
+ }
39
+
40
+ ret = kvm_device_access(lds->dev_fd, KVM_DEV_LOONGARCH_DMSINTC_GRP_CTRL,
41
+ KVM_DEV_LOONGARCH_DMSINTC_MSG_ADDR_SIZE,
42
+ &lds->msg_addr_size, true, NULL);
43
+ if (ret < 0) {
44
+ fprintf(stderr, "KVM_DEV_LOONGARCH_DINTC_MSG_ADDR_SIZE failed: %s\n",
45
+ strerror(ret));
46
+ abort();
47
+ }
48
+}
hw/intc/meson.build
+2
@@ -90,3 +90,5 @@ specific_ss.add(when: 'CONFIG_LOONGARCH_EXTIOI', if_true: files('loongarch_extio
90
specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_LOONGARCH_EXTIOI'],
91
if_true: files('loongarch_extioi_kvm.c'))
92
specific_ss.add(when: 'CONFIG_LOONGARCH_DINTC', if_true: files('loongarch_dintc.c'))
93
+specific_ss.add(when: ['CONFIG_KVM', 'CONFIG_LOONGARCH_DINTC'],
94
+ if_true: files('loongarch_dintc_kvm.c'))
include/hw/intc/loongarch_dintc.h
+10
@@ -9,8 +9,13 @@
9
#include "hw/core/sysbus.h"
10
#include "hw/loongarch/virt.h"
11
#include "system/memory.h"
12
+#include "hw/pci-host/ls7a.h"
13
14
#define NR_VECTORS 256
15
+#define IRQ_BIT_BASE 5
16
+#define IRQ_BIT_LEN 8
17
+#define CPU_BIT_BASE 13
18
+#define CPU_BIT_LEN 8
19
20
#define TYPE_LOONGARCH_DINTC "loongarch_dintc"
21
OBJECT_DECLARE_TYPE(LoongArchDINTCState, LoongArchDINTCClass, LOONGARCH_DINTC)
@@ -25,7 +30,10 @@ struct LoongArchDINTCState {
30
SysBusDevice parent_obj;
31
MemoryRegion dintc_mmio;
32
DINTCCore *cpu;
33
+ int dev_fd;
34
uint32_t num_cpu;
35
+ uint64_t msg_addr_base;
36
+ uint64_t msg_addr_size;
37
};
38
39
struct LoongArchDINTCClass {
@@ -34,3 +42,5 @@ struct LoongArchDINTCClass {
42
DeviceRealize parent_realize;
43
DeviceUnrealize parent_unrealize;
44
};
45
+
46
+void kvm_dintc_realize(DeviceState *dev, Error **errp);