[PATCH 2/3] KVM: Add gsi_msg_pending_bitmap for MSI-X

Sheng Yang
2009-02-11T08:08:55Z

We have to handle more than one interrupt with one handler for MSI-X. So we
need a bitmap to track the triggered interrupts.

Signed-off-by: Sheng Yang <sheng@linux.intel.com>
---
include/linux/kvm_host.h | 5 +-
virt/kvm/kvm_main.c | 103 ++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 103 insertions(+), 5 deletions(-)

diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index a7d6123..c081867 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -144,6 +144,8 @@ struct kvm {
#ifdef CONFIG_HAVE_KVM_IRQCHIP
struct list_head irq_routing; /* of kvm_kernel_irq_routing_entry */
struct hlist_head mask_notifier_list;
+#define KVM_MAX_IRQ_ROUTES 1024
+ DECLARE_BITMAP(irq_routes_pending_bitmap, KVM_MAX_IRQ_ROUTES);
#endif

#ifdef KVM_ARCH_WANT_MMU_NOTIFIER
@@ -335,6 +337,7 @@ struct kvm_assigned_dev_kernel {
#define KVM_ASSIGNED_DEV_GUEST_MSI (1 << 1)
#define KVM_ASSIGNED_DEV_HOST_INTX (1 << 8)
#define KVM_ASSIGNED_DEV_HOST_MSI (1 << 9)
+#define KVM_ASSIGNED_DEV_MSIX ((1 << 2) | (1 << 10))
unsigned long irq_requested_type;
int irq_source_id;
int flags;
@@ -502,8 +505,6 @@ static inline int mmu_notifier_retry(struct kvm_vcpu *vcpu, unsigned long mmu_se

#ifdef CONFIG_HAVE_KVM_IRQCHIP

-#define KVM_MAX_IRQ_ROUTES 1024
-
int kvm_setup_default_irq_routing(struct kvm *kvm);
int kvm_set_irq_routing(struct kvm *kvm,
const struct kvm_irq_routing_entry *entries,
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index ea96690..961603f 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -95,25 +95,113 @@ static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *h
return NULL;
}

+static int find_host_irq_from_gsi(struct kvm_assigned_dev_kernel *assigned_dev,
+ u32 gsi)
+{
+ int i, entry, irq;
+ struct msix_entry *host_msix_entries, *guest_msix_entries;
+
+ host_msix_entries = assigned_dev->host_msix_entries;
+ guest_msix_entries = assigned_dev->guest_msix_entries;
+
+ entry = -1;
+ irq = 0;
+ for (i = 0; i < assigned_dev->entries_nr; i++)
+ if (gsi == (guest_msix_entries + i)->vector) {
+ entry = (guest_msix_entries + i)->entry;
+ break;
+ }
+ if (entry < 0) {
+ printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
+ return 0;
+ }
+ for (i = 0; i < assigned_dev->entries_nr; i++)
+ if (entry == (host_msix_entries + i)->entry) {
+ irq = (host_msix_entries + i)->vector;
+ break;
+ }
+ if (irq == 0) {
+ printk(KERN_WARNING "Fail to find correlated MSI-X irq!\n");
+ return 0;
+ }
+
+ return irq;
+}
+
+static u32 find_gsi_from_host_irq(struct kvm_assigned_dev_kernel *assigned_dev,
+ int irq)
+{
+ int i, entry;
+ u32 gsi;
+ struct msix_entry *host_msix_entries, *guest_msix_entries;
+
+ host_msix_entries = assigned_dev->host_msix_entries;
+ guest_msix_entries = assigned_dev->guest_msix_entries;
+
+ entry = -1;
+ gsi = 0;
+ for (i = 0; i < assigned_dev->entries_nr; i++)
+ if (irq == (host_msix_entries + i)->vector) {
+ entry = (host_msix_entries + i)->entry;
+ break;
+ }
+ if (entry < 0) {
+ printk(KERN_WARNING "Fail to find correlated MSI-X entry!\n");
+ return 0;
+ }
+ for (i = 0; i < assigned_dev->entries_nr; i++)
+ if (entry == (guest_msix_entries + i)->entry) {
+ gsi = (guest_msix_entries + i)->vector;
+ break;
+ }
+ if (gsi == 0) {
+ printk(KERN_WARNING "Fail to find correlated MSI-X gsi!\n");
+ return 0;
+ }
+
+ return gsi;
+}
+
static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
{
struct kvm_assigned_dev_kernel *assigned_dev;
+ struct kvm *kvm;
+ u32 gsi;
+ int irq;

assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
interrupt_work);
+ kvm = assigned_dev->kvm;

/* This is taken to safely inject irq inside the guest. When
* the interrupt injection (or the ioapic code) uses a
* finer-grained lock, update this
*/
- mutex_lock(&assigned_dev->kvm->lock);
- kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
- assigned_dev->guest_irq, 1);
+ mutex_lock(&kvm->lock);
+handle_irq:
+ if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_MSIX) {
+ gsi = find_first_bit(kvm->irq_routes_pending_bitmap,
+ KVM_MAX_IRQ_ROUTES);
+ BUG_ON(gsi >= KVM_MAX_IRQ_ROUTES);
+ clear_bit(gsi, kvm->irq_routes_pending_bitmap);
+ } else
+ gsi = assigned_dev->guest_irq;
+
+ kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id, gsi, 1);

if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_MSI) {
enable_irq(assigned_dev->host_irq);
assigned_dev->host_irq_disabled = false;
+ } else if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_MSIX) {
+ irq = find_host_irq_from_gsi(assigned_dev, gsi);
+ enable_irq(irq);
+ assigned_dev->host_irq_disabled = false;
+ gsi = find_first_bit(kvm->irq_routes_pending_bitmap,
+ KVM_MAX_IRQ_ROUTES);
+ if (gsi < KVM_MAX_IRQ_ROUTES)
+ goto handle_irq;
}
+
mutex_unlock(&assigned_dev->kvm->lock);
}

@@ -121,6 +209,15 @@ static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
{
struct kvm_assigned_dev_kernel *assigned_dev =
(struct kvm_assigned_dev_kernel *) dev_id;
+ struct kvm *kvm = assigned_dev->kvm;
+
+ if (assigned_dev->irq_requested_type == KVM_ASSIGNED_DEV_MSIX) {
+ u32 gsi;
+ gsi = find_gsi_from_host_irq(assigned_dev, irq);
+ if (gsi == 0)
+ return IRQ_HANDLED;
+ set_bit(gsi, kvm->irq_routes_pending_bitmap);
+ }

schedule_work(&assigned_dev->interrupt_work);

--
1.5.4.5