new file mode 100644
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2024 Loongson Technology Corporation Limited
+ */
+
+#ifndef LOONGARCH_EXTIOI_H
+#define LOONGARCH_EXTIOI_H
+
+#include <kvm/iodev.h>
+
+#define EXTIOI_IRQS 256
+#define EXTIOI_ROUTE_MAX_VCPUS 256
+#define EXTIOI_IRQS_U8_NUMS (EXTIOI_IRQS / 8)
+#define EXTIOI_IRQS_U16_NUMS (EXTIOI_IRQS_U8_NUMS / 2)
+#define EXTIOI_IRQS_U32_NUMS (EXTIOI_IRQS_U8_NUMS / 4)
+#define EXTIOI_IRQS_U64_NUMS (EXTIOI_IRQS_U8_NUMS / 8)
+/* map to ipnum per 32 irqs */
+#define EXTIOI_IRQS_NODETYPE_COUNT 16
+
+#define EXTIOI_BASE 0x1400
+#define EXTIOI_SIZE 0x900
+
+#define EXTIOI_VIRT_BASE (0x40000000)
+#define EXTIOI_VIRT_SIZE (0x1000)
+
+#define LS3A_IP_NUM 8
+
+struct loongarch_extioi {
+ spinlock_t lock;
+ struct kvm *kvm;
+ struct kvm_io_device device;
+ struct kvm_io_device device_extern;
+ uint32_t num_cpu;
+ uint32_t features;
+ uint32_t status;
+
+ /* hardware state */
+ union nodetype {
+ u64 reg_u64[EXTIOI_IRQS_NODETYPE_COUNT / 4];
+ u32 reg_u32[EXTIOI_IRQS_NODETYPE_COUNT / 2];
+ u16 reg_u16[EXTIOI_IRQS_NODETYPE_COUNT];
+ u8 reg_u8[EXTIOI_IRQS_NODETYPE_COUNT * 2];
+ } nodetype;
+
+ /* one bit shows the state of one irq */
+ union bounce {
+ u64 reg_u64[EXTIOI_IRQS_U64_NUMS];
+ u32 reg_u32[EXTIOI_IRQS_U32_NUMS];
+ u16 reg_u16[EXTIOI_IRQS_U16_NUMS];
+ u8 reg_u8[EXTIOI_IRQS_U8_NUMS];
+ } bounce;
+
+ union isr {
+ u64 reg_u64[EXTIOI_IRQS_U64_NUMS];
+ u32 reg_u32[EXTIOI_IRQS_U32_NUMS];
+ u16 reg_u16[EXTIOI_IRQS_U16_NUMS];
+ u8 reg_u8[EXTIOI_IRQS_U8_NUMS];
+ } isr;
+ union coreisr {
+ u64 reg_u64[EXTIOI_ROUTE_MAX_VCPUS][EXTIOI_IRQS_U64_NUMS];
+ u32 reg_u32[EXTIOI_ROUTE_MAX_VCPUS][EXTIOI_IRQS_U32_NUMS];
+ u16 reg_u16[EXTIOI_ROUTE_MAX_VCPUS][EXTIOI_IRQS_U16_NUMS];
+ u8 reg_u8[EXTIOI_ROUTE_MAX_VCPUS][EXTIOI_IRQS_U8_NUMS];
+ } coreisr;
+ union enable {
+ u64 reg_u64[EXTIOI_IRQS_U64_NUMS];
+ u32 reg_u32[EXTIOI_IRQS_U32_NUMS];
+ u16 reg_u16[EXTIOI_IRQS_U16_NUMS];
+ u8 reg_u8[EXTIOI_IRQS_U8_NUMS];
+ } enable;
+
+ /* use one byte to config ipmap for 32 irqs at once */
+ union ipmap {
+ u64 reg_u64;
+ u32 reg_u32[EXTIOI_IRQS_U32_NUMS / 4];
+ u16 reg_u16[EXTIOI_IRQS_U16_NUMS / 4];
+ u8 reg_u8[EXTIOI_IRQS_U8_NUMS / 4];
+ } ipmap;
+ /* use one byte to config coremap for one irq */
+ union coremap {
+ u64 reg_u64[EXTIOI_IRQS / 8];
+ u32 reg_u32[EXTIOI_IRQS / 4];
+ u16 reg_u16[EXTIOI_IRQS / 2];
+ u8 reg_u8[EXTIOI_IRQS];
+ } coremap;
+
+ DECLARE_BITMAP(sw_coreisr[EXTIOI_ROUTE_MAX_VCPUS][LS3A_IP_NUM], EXTIOI_IRQS);
+ uint8_t sw_coremap[EXTIOI_IRQS];
+};
+
+void extioi_set_irq(struct loongarch_extioi *s, int irq, int level);
+int kvm_loongarch_register_extioi_device(void);
+#endif /* LOONGARCH_EXTIOI_H */
@@ -20,6 +20,7 @@
#include <asm/kvm_mmu.h>
#include <asm/loongarch.h>
#include <asm/kvm_ipi.h>
+#include <asm/kvm_extioi.h>
/* Loongarch KVM register ids */
#define KVM_GET_IOC_CSR_IDX(id) ((id & KVM_CSR_IDX_MASK) >> LOONGARCH_REG_SHIFT)
@@ -114,6 +115,7 @@ struct kvm_arch {
s64 time_offset;
struct kvm_context __percpu *vmcs;
struct loongarch_ipi *ipi;
+ struct loongarch_extioi *extioi;
};
#define CSR_MAX_NUMS 0x800
@@ -19,5 +19,6 @@ kvm-y += tlb.o
kvm-y += vcpu.o
kvm-y += vm.o
kvm-y += intc/ipi.o
+kvm-y += intc/extioi.o
CFLAGS_exit.o += $(call cc-option,-Wno-override-init,)
new file mode 100644
@@ -0,0 +1,130 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024 Loongson Technology Corporation Limited
+ */
+
+#include <asm/kvm_extioi.h>
+#include <asm/kvm_vcpu.h>
+#include <linux/count_zeros.h>
+
+static int kvm_extioi_write(struct kvm_vcpu *vcpu,
+ struct kvm_io_device *dev,
+ gpa_t addr, int len, const void *val)
+{
+ return 0;
+}
+
+static int kvm_extioi_read(struct kvm_vcpu *vcpu,
+ struct kvm_io_device *dev,
+ gpa_t addr, int len, void *val)
+{
+ return 0;
+}
+
+static const struct kvm_io_device_ops kvm_extioi_ops = {
+ .read = kvm_extioi_read,
+ .write = kvm_extioi_write,
+};
+
+static int kvm_extioi_virt_read(struct kvm_vcpu *vcpu,
+ struct kvm_io_device *dev,
+ gpa_t addr, int len, void *val)
+{
+ return 0;
+}
+
+static int kvm_extioi_virt_write(struct kvm_vcpu *vcpu,
+ struct kvm_io_device *dev,
+ gpa_t addr, int len, const void *val)
+{
+ return 0;
+}
+
+static const struct kvm_io_device_ops kvm_extioi_virt_ops = {
+ .read = kvm_extioi_virt_read,
+ .write = kvm_extioi_virt_write,
+};
+
+static int kvm_extioi_get_attr(struct kvm_device *dev,
+ struct kvm_device_attr *attr)
+{
+ return 0;
+}
+
+static int kvm_extioi_set_attr(struct kvm_device *dev,
+ struct kvm_device_attr *attr)
+{
+ return 0;
+}
+
+static void kvm_extioi_destroy(struct kvm_device *dev)
+{
+ struct kvm *kvm;
+ struct loongarch_extioi *extioi;
+
+ if (!dev || !dev->kvm || !dev->kvm->arch.extioi)
+ return;
+ kvm = dev->kvm;
+ extioi = kvm->arch.extioi;
+ kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &extioi->device);
+ kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &extioi->device_extern);
+ kfree(extioi);
+}
+
+static int kvm_extioi_create(struct kvm_device *dev, u32 type)
+{
+ int ret;
+ struct loongarch_extioi *s;
+ struct kvm_io_device *device, *device1;
+ struct kvm *kvm = dev->kvm;
+
+ /* extioi has been created */
+ if (kvm->arch.extioi)
+ return -EINVAL;
+
+ s = kzalloc(sizeof(struct loongarch_extioi), GFP_KERNEL);
+ if (!s)
+ return -ENOMEM;
+ spin_lock_init(&s->lock);
+ s->kvm = kvm;
+
+ /*
+ * Initialize IOCSR device
+ */
+ device = &s->device;
+ kvm_iodevice_init(device, &kvm_extioi_ops);
+ mutex_lock(&kvm->slots_lock);
+ ret = kvm_io_bus_register_dev(kvm, KVM_IOCSR_BUS,
+ EXTIOI_BASE, EXTIOI_SIZE, device);
+ mutex_unlock(&kvm->slots_lock);
+ if (ret < 0) {
+ kfree(s);
+ return ret;
+ }
+
+ device1 = &s->device_extern;
+ kvm_iodevice_init(device1, &kvm_extioi_virt_ops);
+ ret = kvm_io_bus_register_dev(kvm, KVM_IOCSR_BUS,
+ EXTIOI_VIRT_BASE, EXTIOI_VIRT_SIZE, device1);
+ if (ret < 0) {
+ kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &s->device);
+ kfree(s);
+ return ret;
+ }
+ kvm->arch.extioi = s;
+ return 0;
+}
+
+static struct kvm_device_ops kvm_extioi_dev_ops = {
+ .name = "kvm-loongarch-extioi",
+ .create = kvm_extioi_create,
+ .destroy = kvm_extioi_destroy,
+ .set_attr = kvm_extioi_set_attr,
+ .get_attr = kvm_extioi_get_attr,
+};
+
+int kvm_loongarch_register_extioi_device(void)
+{
+ return kvm_register_device_ops(&kvm_extioi_dev_ops,
+ KVM_DEV_TYPE_LA_EXTIOI);
+}
@@ -9,6 +9,7 @@
#include <asm/cacheflush.h>
#include <asm/cpufeature.h>
#include <asm/kvm_csr.h>
+#include <asm/kvm_extioi.h>
#include "trace.h"
unsigned long vpid_mask;
@@ -370,6 +371,11 @@ static int kvm_loongarch_env_init(void)
/* Register loongarch ipi interrupt controller interface. */
ret = kvm_loongarch_register_ipi_device();
+ if (ret)
+ return ret;
+
+ /* Register loongarch extioi interrupt controller interface. */
+ ret = kvm_loongarch_register_extioi_device();
return ret;
}
@@ -1160,6 +1160,8 @@ enum kvm_device_type {
#define KVM_DEV_TYPE_RISCV_AIA KVM_DEV_TYPE_RISCV_AIA
KVM_DEV_TYPE_LA_IPI,
#define KVM_DEV_TYPE_LA_IPI KVM_DEV_TYPE_LA_IPI
+ KVM_DEV_TYPE_LA_EXTIOI,
+#define KVM_DEV_TYPE_LA_EXTIOI KVM_DEV_TYPE_LA_EXTIOI
KVM_DEV_TYPE_MAX,