Add irqfd choice dintc to set msi irq by the msg_addr and implement dintc set msi irq. Signed-off-by: Song Gao --- arch/loongarch/include/asm/kvm_dintc.h | 1 + arch/loongarch/kvm/intc/dintc.c | 6 ++++++ arch/loongarch/kvm/irqfd.c | 28 +++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/loongarch/include/asm/kvm_dintc.h b/arch/loongarch/include/asm/kvm_dintc.h index d980d39c0344..f87fb802a7bf 100644 --- a/arch/loongarch/include/asm/kvm_dintc.h +++ b/arch/loongarch/include/asm/kvm_dintc.h @@ -11,6 +11,7 @@ struct loongarch_dintc { struct kvm *kvm; uint64_t msg_addr_base; uint64_t msg_addr_size; + uint32_t cpu_mask; }; struct dintc_state { diff --git a/arch/loongarch/kvm/intc/dintc.c b/arch/loongarch/kvm/intc/dintc.c index d30616ded1b1..ae0d61e2b8f0 100644 --- a/arch/loongarch/kvm/intc/dintc.c +++ b/arch/loongarch/kvm/intc/dintc.c @@ -15,6 +15,7 @@ static int kvm_dintc_ctrl_access(struct kvm_device *dev, void __user *data; struct loongarch_dintc *s = dev->kvm->arch.dintc; u64 tmp; + u32 cpu_bit; data = (void __user *)attr->addr; switch (addr) { @@ -30,6 +31,11 @@ static int kvm_dintc_ctrl_access(struct kvm_device *dev, s->msg_addr_base = tmp; else return -EFAULT; + s->msg_addr_base = tmp; + cpu_bit = find_first_bit((unsigned long *)&(s->msg_addr_base), 64) + - AVEC_CPU_SHIFT; + cpu_bit = min(cpu_bit, AVEC_CPU_BIT); + s->cpu_mask = GENMASK(cpu_bit - 1, 0) & AVEC_CPU_MASK; } break; case KVM_DEV_LOONGARCH_DINTC_MSG_ADDR_SIZE: diff --git a/arch/loongarch/kvm/irqfd.c b/arch/loongarch/kvm/irqfd.c index 9a39627aecf0..fdcce472abb1 100644 --- a/arch/loongarch/kvm/irqfd.c +++ b/arch/loongarch/kvm/irqfd.c @@ -6,6 +6,7 @@ #include #include #include +#include static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, int irq_source_id, int level, bool line_status) @@ -16,6 +17,22 @@ static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, return 0; } +static int kvm_dintc_set_msi_irq(struct kvm *kvm, u32 addr, int data, int level) +{ + unsigned int virq, dest; + struct kvm_vcpu *vcpu; + + virq = (addr >> AVEC_VIRQ_SHIFT) & AVEC_VIRQ_MASK; + dest = (addr >> AVEC_CPU_SHIFT) & kvm->arch.dintc->cpu_mask; + if (dest > KVM_MAX_VCPUS) + return -EINVAL; + vcpu = kvm_get_vcpu_by_cpuid(kvm, dest); + if (!vcpu) + return -EINVAL; + return kvm_loongarch_deliver_msi_to_vcpu(kvm, vcpu, virq, level); +} + + /* * kvm_set_msi: inject the MSI corresponding to the * MSI routing entry @@ -26,10 +43,19 @@ static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, int irq_source_id, int level, bool line_status) { + u64 msg_addr; + if (!level) return -1; - pch_msi_set_irq(kvm, e->msi.data, level); + msg_addr = (((u64)e->msi.address_hi) << 32) | e->msi.address_lo; + if (cpu_has_msgint && kvm->arch.dintc && + msg_addr >= kvm->arch.dintc->msg_addr_base && + msg_addr < (kvm->arch.dintc->msg_addr_base + kvm->arch.dintc->msg_addr_size)) { + return kvm_dintc_set_msi_irq(kvm, msg_addr, e->msi.data, level); + } else if (msg_addr == 0) { + pch_msi_set_irq(kvm, e->msi.data, level); + } return 0; } -- 2.39.3