All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andre Przywara <andre.przywara@linaro.org>
To: Julien Grall <julien.grall@arm.com>,
	Stefano Stabellini <sstabellini@kernel.org>
Cc: xen-devel@lists.xenproject.org,
	Andre Przywara <andre.przywara@linaro.org>
Subject: [PATCH v3 13/39] ARM: new VGIC: Add IRQ sync/flush framework
Date: Wed, 21 Mar 2018 16:32:09 +0000	[thread overview]
Message-ID: <20180321163235.12529-14-andre.przywara@linaro.org> (raw)
In-Reply-To: <20180321163235.12529-1-andre.przywara@linaro.org>

Implement the framework for syncing IRQs between our emulation and the
list registers, which represent the guest's view of IRQs.
This is done in vgic_sync_from_lrs() and vgic_sync_to_lrs(), which
get called on guest entry and exit, respectively.
The code talking to the actual GICv2/v3 hardware is added in the
following patches.

This is based on Linux commit 0919e84c0fc1, written by Marc Zyngier.

Signed-off-by: Andre Przywara <andre.przywara@linaro.org>
---
Changelog v2 ... v3:
- replace "true" instead of "1" for the boolean parameter

Changelog v1 ... v2:
- make functions void
- do underflow setting directly (no v2/v3 indirection)
- fix multiple SGIs injections (as the late Linux bugfix)

 xen/arch/arm/vgic/vgic.c | 232 +++++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.h |   2 +
 2 files changed, 234 insertions(+)

diff --git a/xen/arch/arm/vgic/vgic.c b/xen/arch/arm/vgic/vgic.c
index ee0de8d2e0..52e1669888 100644
--- a/xen/arch/arm/vgic/vgic.c
+++ b/xen/arch/arm/vgic/vgic.c
@@ -409,6 +409,238 @@ void vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid,
     return;
 }
 
+/**
+ * vgic_prune_ap_list() - Remove non-relevant interrupts from the ap_list
+ *
+ * @vcpu:       The VCPU of which the ap_list should be pruned.
+ *
+ * Go over the list of interrupts on a VCPU's ap_list, and prune those that
+ * we won't have to consider in the near future.
+ * This removes interrupts that have been successfully handled by the guest,
+ * or that have otherwise became obsolete (not pending anymore).
+ * Also this moves interrupts between VCPUs, if their affinity has changed.
+ */
+static void vgic_prune_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic;
+    struct vgic_irq *irq, *tmp;
+    unsigned long flags;
+
+retry:
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry_safe( irq, tmp, &vgic_cpu->ap_list_head, ap_list )
+    {
+        struct 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);
+
+            /*
+             * This vgic_put_irq call matches the
+             * vgic_get_irq_kref in vgic_queue_irq_unlock,
+             * where we added the LPI to the ap_list. As
+             * we remove the irq from the list, we drop
+             * also drop the refcount.
+             */
+            vgic_put_irq(vcpu->domain, irq);
+            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_irqrestore(&vgic_cpu->ap_list_lock, flags);
+
+        /*
+         * 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_irqsave(&vcpuA->arch.vgic.ap_list_lock, flags);
+        spin_lock(&vcpuB->arch.vgic.ap_list_lock);
+        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;
+
+            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.ap_list_lock);
+        spin_unlock_irqrestore(&vcpuA->arch.vgic.ap_list_lock, flags);
+        goto retry;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+}
+
+static void vgic_fold_lr_state(struct vcpu *vcpu)
+{
+}
+
+/* Requires the irq_lock to be held. */
+static void vgic_populate_lr(struct vcpu *vcpu,
+                             struct vgic_irq *irq, int lr)
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+}
+
+static void vgic_set_underflow(struct vcpu *vcpu)
+{
+    ASSERT(vcpu == current);
+
+    gic_hw_ops->update_hcr_status(GICH_HCR_UIE, true);
+}
+
+/* Requires the ap_list_lock to be held. */
+static int compute_ap_list_depth(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(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 vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    if ( compute_ap_list_depth(vcpu) > gic_get_nr_lrs() )
+        vgic_sort_ap_list(vcpu);
+
+    list_for_each_entry( irq, &vgic_cpu->ap_list_head, ap_list )
+    {
+        spin_lock(&irq->irq_lock);
+
+        if ( likely(vgic_target_oracle(irq) == vcpu) )
+            vgic_populate_lr(vcpu, irq, count++);
+
+        spin_unlock(&irq->irq_lock);
+
+        if ( count == gic_get_nr_lrs() )
+        {
+            if ( !list_is_last(&irq->ap_list, &vgic_cpu->ap_list_head) )
+                vgic_set_underflow(vcpu);
+            break;
+        }
+    }
+
+    vcpu->arch.vgic.used_lrs = count;
+}
+
+/**
+ * vgic_sync_from_lrs() - Update VGIC state from hardware after a guest's run.
+ * @vcpu: the VCPU for which to transfer from the LRs to the IRQ list.
+ *
+ * Sync back the hardware VGIC state after the guest has run, into our
+ * VGIC emulation structures, It reads the LRs and updates the respective
+ * struct vgic_irq, taking level/edge into account.
+ * This is the high level function which takes care of the conditions,
+ * also bails out early if there were no interrupts queued.
+ * Was: kvm_vgic_sync_hwstate()
+ */
+void vgic_sync_from_lrs(struct vcpu *vcpu)
+{
+    /* An empty ap_list_head implies used_lrs == 0 */
+    if ( list_empty(&vcpu->arch.vgic.ap_list_head) )
+        return;
+
+    vgic_fold_lr_state(vcpu);
+
+    vgic_prune_ap_list(vcpu);
+}
+
+/**
+ * vgic_sync_to_lrs() - flush emulation state into the hardware on guest entry
+ *
+ * Before we enter a guest, we have to translate the virtual GIC state of a
+ * VCPU into the GIC virtualization hardware registers, namely the LRs.
+ * This is the high level function which takes care about the conditions
+ * and the locking, also bails out early if there are no interrupts queued.
+ * Was: kvm_vgic_flush_hwstate()
+ */
+void vgic_sync_to_lrs(void)
+{
+    /*
+     * If there are no virtual interrupts active or pending for this
+     * VCPU, then there is no work to do and we can bail out without
+     * taking any lock.  There is a potential race with someone injecting
+     * interrupts to the VCPU, but it is a benign race as the VCPU will
+     * either observe the new interrupt before or after doing this check,
+     * and introducing additional synchronization mechanism doesn't change
+     * this.
+     */
+    if ( list_empty(&current->arch.vgic.ap_list_head) )
+        return;
+
+    ASSERT(!local_irq_is_enabled());
+
+    spin_lock(&current->arch.vgic.ap_list_lock);
+    vgic_flush_lr_state(current);
+    spin_unlock(&current->arch.vgic.ap_list_lock);
+}
 /*
  * Local variables:
  * mode: C
diff --git a/xen/arch/arm/vgic/vgic.h b/xen/arch/arm/vgic/vgic.h
index f9e2eeb2d6..f530cfa078 100644
--- a/xen/arch/arm/vgic/vgic.h
+++ b/xen/arch/arm/vgic/vgic.h
@@ -17,6 +17,8 @@
 #ifndef __XEN_ARM_VGIC_VGIC_H__
 #define __XEN_ARM_VGIC_VGIC_H__
 
+#define vgic_irq_is_sgi(intid) ((intid) < VGIC_NR_SGIS)
+
 static inline bool irq_is_pending(struct vgic_irq *irq)
 {
     if ( irq->config == VGIC_CONFIG_EDGE )
-- 
2.14.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-03-21 16:33 UTC|newest]

Thread overview: 122+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-21 16:31 [PATCH v3 00/39] New VGIC(-v2) implementation Andre Przywara
2018-03-21 16:31 ` [PATCH v3 01/39] xen/arm: gic: Read unconditionally the source from the LRs Andre Przywara
2018-03-21 16:31 ` [PATCH v3 02/39] ARM: GIC: add GIC_INVALID to enum gic_version Andre Przywara
2018-03-22  1:39   ` Julien Grall
2018-03-26 20:08   ` Stefano Stabellini
2018-03-21 16:31 ` [PATCH v3 03/39] ARM: GIC: Allow tweaking the active and pending state of an IRQ Andre Przywara
2018-03-22  1:51   ` Julien Grall
2018-03-22 11:11     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 04/39] ARM: GIC: Allow reading pending state of a hardware IRQ Andre Przywara
2018-03-26 20:08   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 05/39] ARM: timer: Handle level triggered IRQs correctly Andre Przywara
2018-03-22  1:58   ` Julien Grall
2018-03-26 20:28   ` Stefano Stabellini
2018-03-27 13:06     ` Andre Przywara
2018-03-27 18:17       ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 06/39] ARM: evtchn: " Andre Przywara
2018-03-22  2:08   ` Julien Grall
2018-03-26 20:08   ` Stefano Stabellini
2018-03-28  0:01   ` Stefano Stabellini
2018-03-28 15:39     ` Andre Przywara
2018-03-28 17:46       ` Stefano Stabellini
2018-03-29  0:34         ` Julien Grall
2018-03-29 13:44         ` Andre Przywara
2018-04-03 13:34     ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 07/39] ARM: vPL011: Use the VGIC's level triggered IRQs handling if available Andre Przywara
2018-03-26 20:20   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 08/39] ARM: new VGIC: Add data structure definitions Andre Przywara
2018-03-26 20:41   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 09/39] ARM: new VGIC: Add accessor to new struct vgic_irq instance Andre Przywara
2018-03-22  2:11   ` Julien Grall
2018-03-26 20:46     ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 10/39] ARM: new VGIC: Implement virtual IRQ injection Andre Przywara
2018-03-26 21:01   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 11/39] Add list_sort() routine from Linux Andre Przywara
2018-03-21 17:01   ` Jan Beulich
2018-03-22  2:14   ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 12/39] ARM: new VGIC: Add IRQ sorting Andre Przywara
2018-03-26 21:16   ` Stefano Stabellini
2018-03-21 16:32 ` Andre Przywara [this message]
2018-03-22  2:16   ` [PATCH v3 13/39] ARM: new VGIC: Add IRQ sync/flush framework Julien Grall
2018-03-26 21:30   ` Stefano Stabellini
2018-03-27 13:23     ` Andre Przywara
2018-03-27 18:55       ` Stefano Stabellini
2018-03-27 19:20         ` Stefano Stabellini
2018-03-27 22:13           ` André Przywara
2018-03-21 16:32 ` [PATCH v3 14/39] ARM: new VGIC: Add GICv2 world switch backend Andre Przywara
2018-03-22  3:48   ` Julien Grall
2018-03-22 11:04     ` Andre Przywara
2018-03-22 13:55       ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 15/39] ARM: new VGIC: Implement vgic_vcpu_pending_irq Andre Przywara
2018-03-22  3:52   ` Julien Grall
2018-03-22 11:15     ` Andre Przywara
2018-03-26 23:34       ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 16/39] ARM: new VGIC: Add MMIO handling framework Andre Przywara
2018-03-27 20:07   ` Stefano Stabellini
2018-03-28  9:28     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 17/39] ARM: new VGIC: Add GICv2 " Andre Przywara
2018-03-27 20:28   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 18/39] ARM: new VGIC: Add CTLR, TYPER and IIDR handlers Andre Przywara
2018-03-22  7:33   ` Julien Grall
2018-03-27 20:38   ` Stefano Stabellini
2018-03-28 10:36     ` Andre Przywara
2018-03-28 17:20       ` Stefano Stabellini
2018-03-28 17:22       ` [PATCH v3 26/39] ARM: new VGIC: Add SGIPENDR register handlers Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 19/39] ARM: new VGIC: Add ENABLE registers handlers Andre Przywara
2018-03-27 21:06   ` Stefano Stabellini
2018-03-28  9:09     ` Andre Przywara
2018-03-28 17:19       ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 20/39] ARM: new VGIC: Add PENDING " Andre Przywara
2018-03-27 21:14   ` Stefano Stabellini
2018-03-28 14:10     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 21/39] ARM: new VGIC: Add ACTIVE " Andre Przywara
2018-03-27 21:21   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 22/39] ARM: new VGIC: Add PRIORITY " Andre Przywara
2018-03-27 21:24   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 23/39] ARM: new VGIC: Add CONFIG " Andre Przywara
2018-03-27 21:26   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 24/39] ARM: new VGIC: Add TARGET " Andre Przywara
2018-03-27  0:24   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 25/39] ARM: new VGIC: Add SGIR register handler Andre Przywara
2018-03-22  7:54   ` Julien Grall
2018-03-27 22:23   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 26/39] ARM: new VGIC: Add SGIPENDR register handlers Andre Przywara
2018-03-27 22:27   ` Stefano Stabellini
2018-03-28 10:37     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 27/39] ARM: new VGIC: Handle hardware mapped IRQs Andre Przywara
2018-03-27 22:31   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 28/39] ARM: new VGIC: Add event channel IRQ handling Andre Przywara
2018-03-27 22:33   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 29/39] ARM: new VGIC: Handle virtual IRQ allocation/reservation Andre Przywara
2018-03-27 22:38   ` Stefano Stabellini
2018-03-28  9:17     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 30/39] ARM: new VGIC: Dump virtual IRQ info Andre Przywara
2018-03-27 22:39   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 31/39] ARM: new VGIC: Provide system register emulation stub Andre Przywara
2018-03-27 22:40   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 32/39] ARM: new VGIC: Implement arch_move_irqs() Andre Przywara
2018-03-26 23:46   ` Stefano Stabellini
2018-03-28 18:47   ` Stefano Stabellini
2018-03-29 14:57     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 33/39] ARM: new VGIC: Add preliminary stub implementation Andre Przywara
2018-03-27 22:48   ` Stefano Stabellini
2018-04-03 13:22     ` Julien Grall
2018-03-21 16:32 ` [PATCH v3 34/39] ARM: new VGIC: vgic-init: register VGIC Andre Przywara
2018-03-22  8:00   ` Julien Grall
2018-03-22 11:18     ` Andre Przywara
2018-03-27 22:50     ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 35/39] ARM: new VGIC: Add vgic_v2_enable Andre Przywara
2018-03-27 22:51   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 36/39] ARM: new VGIC: vgic-init: implement vgic_init Andre Przywara
2018-03-22  8:01   ` Julien Grall
2018-03-27 23:16   ` Stefano Stabellini
2018-03-28  0:06     ` Stefano Stabellini
2018-03-28 10:49     ` Andre Przywara
2018-03-21 16:32 ` [PATCH v3 37/39] ARM: new VGIC: vgic-init: implement map_resources Andre Przywara
2018-03-27 23:09   ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 38/39] ARM: new VGIC: Allocate two pages for struct vcpu Andre Przywara
2018-03-22  8:11   ` Julien Grall
2018-03-27 23:07     ` Stefano Stabellini
2018-03-21 16:32 ` [PATCH v3 39/39] ARM: VGIC: wire new VGIC(-v2) files into Xen build system Andre Przywara
2018-03-22  8:16   ` Julien Grall
2018-03-22 10:39     ` Andre Przywara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180321163235.12529-14-andre.przywara@linaro.org \
    --to=andre.przywara@linaro.org \
    --cc=julien.grall@arm.com \
    --cc=sstabellini@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.