]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - virt/kvm/arm/vgic/vgic.c
Merge tag 'kvm-arm-for-4-7-take2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[karo-tx-linux.git] / virt / kvm / arm / vgic / vgic.c
index bce17dea46779cfd93f72c1582b20e03381bda4a..69b61abefa19a84fe4ca9f46b7d158cc6a0d4ef1 100644 (file)
@@ -257,6 +257,10 @@ static int vgic_update_irq_pending(struct kvm *kvm, int cpuid,
 
        trace_vgic_update_irq_pending(cpuid, intid, level);
 
+       ret = vgic_lazy_init(kvm);
+       if (ret)
+               return ret;
+
        vcpu = kvm_get_vcpu(kvm, cpuid);
        if (!vcpu && intid < VGIC_NR_PRIVATE_IRQS)
                return -EINVAL;
@@ -307,3 +311,309 @@ int kvm_vgic_inject_irq(struct kvm *kvm, int cpuid, unsigned int intid,
 {
        return vgic_update_irq_pending(kvm, cpuid, intid, level, false);
 }
+
+int kvm_vgic_inject_mapped_irq(struct kvm *kvm, int cpuid, unsigned int intid,
+                              bool level)
+{
+       return vgic_update_irq_pending(kvm, cpuid, intid, level, true);
+}
+
+int kvm_vgic_map_phys_irq(struct kvm_vcpu *vcpu, u32 virt_irq, u32 phys_irq)
+{
+       struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
+
+       BUG_ON(!irq);
+
+       spin_lock(&irq->irq_lock);
+
+       irq->hw = true;
+       irq->hwintid = phys_irq;
+
+       spin_unlock(&irq->irq_lock);
+
+       return 0;
+}
+
+int kvm_vgic_unmap_phys_irq(struct kvm_vcpu *vcpu, unsigned int virt_irq)
+{
+       struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
+
+       BUG_ON(!irq);
+
+       if (!vgic_initialized(vcpu->kvm))
+               return -EAGAIN;
+
+       spin_lock(&irq->irq_lock);
+
+       irq->hw = false;
+       irq->hwintid = 0;
+
+       spin_unlock(&irq->irq_lock);
+
+       return 0;
+}
+
+/**
+ * vgic_prune_ap_list - Remove non-relevant interrupts from the list
+ *
+ * @vcpu: The VCPU pointer
+ *
+ * Go over the list of "interesting" interrupts, and prune those that we
+ * won't have to consider in the near future.
+ */
+static void vgic_prune_ap_list(struct kvm_vcpu *vcpu)
+{
+       struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+       struct vgic_irq *irq, *tmp;
+
+retry:
+       spin_lock(&vgic_cpu->ap_list_lock);
+
+       list_for_each_entry_safe(irq, tmp, &vgic_cpu->ap_list_head, ap_list) {
+               struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB;
+
+               spin_lock(&irq->irq_lock);
+
+               BUG_ON(vcpu != irq->vcpu);
+
+               target_vcpu = vgic_target_oracle(irq);
+
+               if (!target_vcpu) {
+                       /*
+                        * We don't need to process this interrupt any
+                        * further, move it off the list.
+                        */
+                       list_del(&irq->ap_list);
+                       irq->vcpu = NULL;
+                       spin_unlock(&irq->irq_lock);
+                       continue;
+               }
+
+               if (target_vcpu == vcpu) {
+                       /* We're on the right CPU */
+                       spin_unlock(&irq->irq_lock);
+                       continue;
+               }
+
+               /* This interrupt looks like it has to be migrated. */
+
+               spin_unlock(&irq->irq_lock);
+               spin_unlock(&vgic_cpu->ap_list_lock);
+
+               /*
+                * Ensure locking order by always locking the smallest
+                * ID first.
+                */
+               if (vcpu->vcpu_id < target_vcpu->vcpu_id) {
+                       vcpuA = vcpu;
+                       vcpuB = target_vcpu;
+               } else {
+                       vcpuA = target_vcpu;
+                       vcpuB = vcpu;
+               }
+
+               spin_lock(&vcpuA->arch.vgic_cpu.ap_list_lock);
+               spin_lock_nested(&vcpuB->arch.vgic_cpu.ap_list_lock,
+                                SINGLE_DEPTH_NESTING);
+               spin_lock(&irq->irq_lock);
+
+               /*
+                * If the affinity has been preserved, move the
+                * interrupt around. Otherwise, it means things have
+                * changed while the interrupt was unlocked, and we
+                * need to replay this.
+                *
+                * In all cases, we cannot trust the list not to have
+                * changed, so we restart from the beginning.
+                */
+               if (target_vcpu == vgic_target_oracle(irq)) {
+                       struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
+
+                       list_del(&irq->ap_list);
+                       irq->vcpu = target_vcpu;
+                       list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
+               }
+
+               spin_unlock(&irq->irq_lock);
+               spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
+               spin_unlock(&vcpuA->arch.vgic_cpu.ap_list_lock);
+               goto retry;
+       }
+
+       spin_unlock(&vgic_cpu->ap_list_lock);
+}
+
+static inline void vgic_process_maintenance_interrupt(struct kvm_vcpu *vcpu)
+{
+       if (kvm_vgic_global_state.type == VGIC_V2)
+               vgic_v2_process_maintenance(vcpu);
+       else
+               vgic_v3_process_maintenance(vcpu);
+}
+
+static inline void vgic_fold_lr_state(struct kvm_vcpu *vcpu)
+{
+       if (kvm_vgic_global_state.type == VGIC_V2)
+               vgic_v2_fold_lr_state(vcpu);
+       else
+               vgic_v3_fold_lr_state(vcpu);
+}
+
+/* Requires the irq_lock to be held. */
+static inline void vgic_populate_lr(struct kvm_vcpu *vcpu,
+                                   struct vgic_irq *irq, int lr)
+{
+       DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&irq->irq_lock));
+
+       if (kvm_vgic_global_state.type == VGIC_V2)
+               vgic_v2_populate_lr(vcpu, irq, lr);
+       else
+               vgic_v3_populate_lr(vcpu, irq, lr);
+}
+
+static inline void vgic_clear_lr(struct kvm_vcpu *vcpu, int lr)
+{
+       if (kvm_vgic_global_state.type == VGIC_V2)
+               vgic_v2_clear_lr(vcpu, lr);
+       else
+               vgic_v3_clear_lr(vcpu, lr);
+}
+
+static inline void vgic_set_underflow(struct kvm_vcpu *vcpu)
+{
+       if (kvm_vgic_global_state.type == VGIC_V2)
+               vgic_v2_set_underflow(vcpu);
+       else
+               vgic_v3_set_underflow(vcpu);
+}
+
+/* Requires the ap_list_lock to be held. */
+static int compute_ap_list_depth(struct kvm_vcpu *vcpu)
+{
+       struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+       struct vgic_irq *irq;
+       int count = 0;
+
+       DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
+
+       list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
+               spin_lock(&irq->irq_lock);
+               /* GICv2 SGIs can count for more than one... */
+               if (vgic_irq_is_sgi(irq->intid) && irq->source)
+                       count += hweight8(irq->source);
+               else
+                       count++;
+               spin_unlock(&irq->irq_lock);
+       }
+       return count;
+}
+
+/* Requires the VCPU's ap_list_lock to be held. */
+static void vgic_flush_lr_state(struct kvm_vcpu *vcpu)
+{
+       struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+       struct vgic_irq *irq;
+       int count = 0;
+
+       DEBUG_SPINLOCK_BUG_ON(!spin_is_locked(&vgic_cpu->ap_list_lock));
+
+       if (compute_ap_list_depth(vcpu) > kvm_vgic_global_state.nr_lr) {
+               vgic_set_underflow(vcpu);
+               vgic_sort_ap_list(vcpu);
+       }
+
+       list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
+               spin_lock(&irq->irq_lock);
+
+               if (unlikely(vgic_target_oracle(irq) != vcpu))
+                       goto next;
+
+               /*
+                * If we get an SGI with multiple sources, try to get
+                * them in all at once.
+                */
+               do {
+                       vgic_populate_lr(vcpu, irq, count++);
+               } while (irq->source && count < kvm_vgic_global_state.nr_lr);
+
+next:
+               spin_unlock(&irq->irq_lock);
+
+               if (count == kvm_vgic_global_state.nr_lr)
+                       break;
+       }
+
+       vcpu->arch.vgic_cpu.used_lrs = count;
+
+       /* Nuke remaining LRs */
+       for ( ; count < kvm_vgic_global_state.nr_lr; count++)
+               vgic_clear_lr(vcpu, count);
+}
+
+/* Sync back the hardware VGIC state into our emulation after a guest's run. */
+void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu)
+{
+       vgic_process_maintenance_interrupt(vcpu);
+       vgic_fold_lr_state(vcpu);
+       vgic_prune_ap_list(vcpu);
+}
+
+/* Flush our emulation state into the GIC hardware before entering the guest. */
+void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu)
+{
+       spin_lock(&vcpu->arch.vgic_cpu.ap_list_lock);
+       vgic_flush_lr_state(vcpu);
+       spin_unlock(&vcpu->arch.vgic_cpu.ap_list_lock);
+}
+
+int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
+{
+       struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+       struct vgic_irq *irq;
+       bool pending = false;
+
+       if (!vcpu->kvm->arch.vgic.enabled)
+               return false;
+
+       spin_lock(&vgic_cpu->ap_list_lock);
+
+       list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list) {
+               spin_lock(&irq->irq_lock);
+               pending = irq->pending && irq->enabled;
+               spin_unlock(&irq->irq_lock);
+
+               if (pending)
+                       break;
+       }
+
+       spin_unlock(&vgic_cpu->ap_list_lock);
+
+       return pending;
+}
+
+void vgic_kick_vcpus(struct kvm *kvm)
+{
+       struct kvm_vcpu *vcpu;
+       int c;
+
+       /*
+        * We've injected an interrupt, time to find out who deserves
+        * a good kick...
+        */
+       kvm_for_each_vcpu(c, vcpu, kvm) {
+               if (kvm_vgic_vcpu_pending_irq(vcpu))
+                       kvm_vcpu_kick(vcpu);
+       }
+}
+
+bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int virt_irq)
+{
+       struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, virt_irq);
+       bool map_is_active;
+
+       spin_lock(&irq->irq_lock);
+       map_is_active = irq->hw && irq->active;
+       spin_unlock(&irq->irq_lock);
+
+       return map_is_active;
+}