1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2024 Loongson Technology Corporation Limited 4 */ 5 6 #include <linux/kvm_host.h> 7 #include <trace/events/kvm.h> 8 #include <asm/kvm_pch_pic.h> 9 10 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, 11 struct kvm *kvm, int irq_source_id, int level, bool line_status) 12 { 13 /* PCH-PIC pin (0 ~ 64) <---> GSI (0 ~ 64) */ 14 pch_pic_set_irq(kvm->arch.pch_pic, e->irqchip.pin, level); 15 16 return 0; 17 } 18 19 /* 20 * kvm_set_msi: inject the MSI corresponding to the 21 * MSI routing entry 22 * 23 * This is the entry point for irqfd MSI injection 24 * and userspace MSI injection. 25 */ 26 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, 27 struct kvm *kvm, int irq_source_id, int level, bool line_status) 28 { 29 if (!level) 30 return -1; 31 32 return pch_msi_set_irq(kvm, e, level); 33 } 34 35 /* 36 * kvm_set_routing_entry: populate a kvm routing entry 37 * from a user routing entry 38 * 39 * @kvm: the VM this entry is applied to 40 * @e: kvm kernel routing entry handle 41 * @ue: user api routing entry handle 42 * return 0 on success, -EINVAL on errors. 43 */ 44 int kvm_set_routing_entry(struct kvm *kvm, 45 struct kvm_kernel_irq_routing_entry *e, 46 const struct kvm_irq_routing_entry *ue) 47 { 48 switch (ue->type) { 49 case KVM_IRQ_ROUTING_IRQCHIP: 50 e->set = kvm_set_pic_irq; 51 e->irqchip.irqchip = ue->u.irqchip.irqchip; 52 e->irqchip.pin = ue->u.irqchip.pin; 53 54 if (e->irqchip.pin >= KVM_IRQCHIP_NUM_PINS) 55 return -EINVAL; 56 57 return 0; 58 case KVM_IRQ_ROUTING_MSI: 59 e->set = kvm_set_msi; 60 e->msi.address_lo = ue->u.msi.address_lo; 61 e->msi.address_hi = ue->u.msi.address_hi; 62 e->msi.data = ue->u.msi.data; 63 return 0; 64 default: 65 return -EINVAL; 66 } 67 } 68 69 int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, 70 struct kvm *kvm, int irq_source_id, int level, bool line_status) 71 { 72 if (!level) 73 return -EWOULDBLOCK; 74 75 switch (e->type) { 76 case KVM_IRQ_ROUTING_IRQCHIP: 77 pch_pic_set_irq(kvm->arch.pch_pic, e->irqchip.pin, level); 78 return 0; 79 case KVM_IRQ_ROUTING_MSI: 80 return pch_msi_set_irq(kvm, e, level); 81 default: 82 return -EWOULDBLOCK; 83 } 84 } 85 86 bool kvm_arch_intc_initialized(struct kvm *kvm) 87 { 88 return kvm_arch_irqchip_in_kernel(kvm); 89 } 90