Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/lrg/voltage-2.6
[sfrench/cifs-2.6.git] / virt / kvm / irq_comm.c
index aec7a0d93a3fc3ee7121a68b991da37146e5d81a..a8bd466d00ccf1af71ab09e229197c2366fcdef5 100644 (file)
@@ -22,6 +22,9 @@
 #include <linux/kvm_host.h>
 
 #include <asm/msidef.h>
+#ifdef CONFIG_IA64
+#include <asm/iosapic.h>
+#endif
 
 #include "irq.h"
 
@@ -43,78 +46,73 @@ static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
        return kvm_ioapic_set_irq(kvm->arch.vioapic, e->irqchip.pin, level);
 }
 
-void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
-                                  union kvm_ioapic_redirect_entry *entry,
-                                  unsigned long *deliver_bitmask)
+inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 {
-       struct kvm_vcpu *vcpu;
-
-       *deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
-                               entry->fields.dest_id, entry->fields.dest_mode);
-       switch (entry->fields.delivery_mode) {
-       case IOAPIC_LOWEST_PRIORITY:
-               vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm,
-                               entry->fields.vector, *deliver_bitmask);
-               *deliver_bitmask = 1 << vcpu->vcpu_id;
-               break;
-       case IOAPIC_FIXED:
-       case IOAPIC_NMI:
-               break;
-       default:
-               if (printk_ratelimit())
-                       printk(KERN_INFO "kvm: unsupported delivery mode %d\n",
-                               entry->fields.delivery_mode);
-               *deliver_bitmask = 0;
-       }
+#ifdef CONFIG_IA64
+       return irq->delivery_mode ==
+               (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
+#else
+       return irq->delivery_mode == APIC_DM_LOWEST;
+#endif
 }
 
-static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
-                      struct kvm *kvm, int level)
+int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
+               struct kvm_lapic_irq *irq)
 {
-       int vcpu_id, r = -1;
-       struct kvm_vcpu *vcpu;
-       struct kvm_ioapic *ioapic = ioapic_irqchip(kvm);
-       union kvm_ioapic_redirect_entry entry;
-       unsigned long deliver_bitmask;
+       int i, r = -1;
+       struct kvm_vcpu *vcpu, *lowest = NULL;
 
-       BUG_ON(!ioapic);
+       if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
+                       kvm_is_dm_lowest_prio(irq))
+               printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
 
-       entry.bits = 0;
-       entry.fields.dest_id = (e->msi.address_lo &
-                       MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
-       entry.fields.vector = (e->msi.data &
-                       MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
-       entry.fields.dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
-                       (unsigned long *)&e->msi.address_lo);
-       entry.fields.trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
-                       (unsigned long *)&e->msi.data);
-       entry.fields.delivery_mode = test_bit(
-                       MSI_DATA_DELIVERY_MODE_SHIFT,
-                       (unsigned long *)&e->msi.data);
-
-       /* TODO Deal with RH bit of MSI message address */
+       for (i = 0; i < KVM_MAX_VCPUS; i++) {
+               vcpu = kvm->vcpus[i];
 
-       kvm_get_intr_delivery_bitmask(ioapic, &entry, &deliver_bitmask);
+               if (!vcpu || !kvm_apic_present(vcpu))
+                       continue;
 
-       if (!deliver_bitmask) {
-               printk(KERN_WARNING "kvm: no destination for MSI delivery!");
-               return -1;
-       }
-       for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
-               if (!(deliver_bitmask & (1 << vcpu_id)))
+               if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
+                                       irq->dest_id, irq->dest_mode))
                        continue;
-               deliver_bitmask &= ~(1 << vcpu_id);
-               vcpu = ioapic->kvm->vcpus[vcpu_id];
-               if (vcpu) {
+
+               if (!kvm_is_dm_lowest_prio(irq)) {
                        if (r < 0)
                                r = 0;
-                       r += kvm_apic_set_irq(vcpu, entry.fields.vector,
-                                             entry.fields.trig_mode);
+                       r += kvm_apic_set_irq(vcpu, irq);
+               } else {
+                       if (!lowest)
+                               lowest = vcpu;
+                       else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
+                               lowest = vcpu;
                }
        }
+
+       if (lowest)
+               r = kvm_apic_set_irq(lowest, irq);
+
        return r;
 }
 
+static int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
+                      struct kvm *kvm, int level)
+{
+       struct kvm_lapic_irq irq;
+
+       irq.dest_id = (e->msi.address_lo &
+                       MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
+       irq.vector = (e->msi.data &
+                       MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
+       irq.dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
+       irq.trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
+       irq.delivery_mode = e->msi.data & 0x700;
+       irq.level = 1;
+       irq.shorthand = 0;
+
+       /* TODO Deal with RH bit of MSI message address */
+       return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
+}
+
 /* This should be called with the kvm->lock mutex held
  * Return value:
  *  < 0   Interrupt was ignored (masked or not delivered for other reasons)
@@ -273,7 +271,7 @@ static int setup_routing_entry(struct kvm_kernel_irq_routing_entry *e,
                        delta = 8;
                        break;
                case KVM_IRQCHIP_IOAPIC:
-                               e->set = kvm_set_ioapic_irq;
+                       e->set = kvm_set_ioapic_irq;
                        break;
                default:
                        goto out;