Skip to contentSkip to conversation

arm64: Dom0 ITS emulation

PATCH Superseded Andre Przywara 6 participants 119 messages Mar 16, 2017 – Apr 4, 2017 Page 2 of 2

v2 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#84 lore raw

PATCH v2 13/27 ARM: vGICv3: Handle disabled LPIs

In reply to Andre Przywara

If a guest disables an LPI, we do not forward this to the associated host LPI to avoid queueing commands to the host ITS command queue. So it may happen that an LPI fires nevertheless on the host. In this case we can bail out early, but have to save the pending state on the virtual side.

---
 xen/arch/arm/gic-v3-lpi.c  |  8 ++++++++
 xen/arch/arm/vgic-v3.c     | 12 ++++++++++++
 xen/include/asm-arm/vgic.h |  6 ++++++
 3 files changed, 26 insertions(+)
xen/arch/arm/gic-v3-lpi.c +8 -0
index 0579976..994698e 100644
@@ -142,6 +142,14 @@ void do_LPI(unsigned int lpi)
 
     put_domain(d);
 
+    /*
+     * We keep all host LPIs enabled, so check if it's disabled on the guest
+     * side and just record this LPI in the virtual pending table in this case.
+     * The guest picks it up once it gets enabled again.
+     */
+    if ( !vgic_can_inject_lpi(vcpu, hlpi.virt_lpi) )
+        return;
+
     vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
 }
 
xen/arch/arm/vgic-v3.c +12 -0
index c6db2d7..de625bf 100644
@@ -498,6 +498,18 @@ bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi)
     return d->arch.vgic.proptable[vlpi - LPI_OFFSET] & LPI_PROP_ENABLED;
 }
 
+bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi)
+{
+    if ( vlpi >= vcpu->domain->arch.vgic.nr_lpis )
+        return false;
+
+    if ( vgic_lpi_is_enabled(vcpu->domain, vlpi) )
+        return true;
+
+    set_bit(vlpi - LPI_OFFSET, vcpu->arch.vgic.pendtable);
+    return false;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
xen/include/asm-arm/vgic.h +6 -0
index f8bccfa..302702e 100644
@@ -323,6 +323,7 @@ int vgic_v3_init(struct domain *d, int *mmio_count);
 #ifdef CONFIG_HAS_GICV3
 extern int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi);
 extern bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi);
+extern bool vgic_can_inject_lpi(struct vcpu *v, uint32_t vlpi);
 #else
 static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
 {
@@ -333,6 +334,11 @@ static inline bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi)
 {
     return false;
 }
+
+static inline bool vgic_can_inject_lpi(struct vcpu *v, uint32_t vlpi)
+{
+    return false;
+}
 #endif
 
 extern int domain_vgic_register(struct domain *d, int *mmio_count);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#85 lore raw

Re: [PATCH v2 13/27] ARM: vGICv3: Handle disabled LPIs

Hi Andre,

Quoted 7 lines · Andre Przywara

If a guest disables an LPI, we do not forward this to the associated host LPI to avoid queueing commands to the host ITS command queue. So it may happen that an LPI fires nevertheless on the host. In this case we can bail out early, but have to save the pending state on the virtual side.

Please see alpine.DEB.2.10.1701051422020.2866@sstabellini-ThinkPad-X260

Quoted 32 lines
---
 xen/arch/arm/gic-v3-lpi.c  |  8 ++++++++
 xen/arch/arm/vgic-v3.c     | 12 ++++++++++++
 xen/include/asm-arm/vgic.h |  6 ++++++
 3 files changed, 26 insertions(+)
xen/arch/arm/gic-v3-lpi.c +8 -0
index 0579976..994698e 100644
@@ -142,6 +142,14 @@ void do_LPI(unsigned int lpi)
 
     put_domain(d);
 
+    /*
+     * We keep all host LPIs enabled, so check if it's disabled on the guest
+     * side and just record this LPI in the virtual pending table in this case.
+     * The guest picks it up once it gets enabled again.
+     */
+    if ( !vgic_can_inject_lpi(vcpu, hlpi.virt_lpi) )
+        return;
+
     vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
 }
 
xen/arch/arm/vgic-v3.c +0 -0
index c6db2d7..de625bf 100644
@@ -498,6 +498,18 @@ bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi)
     return d->arch.vgic.proptable[vlpi - LPI_OFFSET] & LPI_PROP_ENABLED;
 }

xen/arch/arm/vgic-v3.c bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi) · line 509

Quoted 10 lines
xen/arch/arm/vgic-v3.c +9 -0
 
+bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi)
+{
+    if ( vlpi >= vcpu->domain->arch.vgic.nr_lpis )
+        return false;
+
+    if ( vgic_lpi_is_enabled(vcpu->domain, vlpi) )
+        return true;
+
+    set_bit(vlpi - LPI_OFFSET, vcpu->arch.vgic.pendtable);

New line here.

Quoted 19 lines
xen/arch/arm/vgic-v3.c +3 -0
+    return false;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
xen/include/asm-arm/vgic.h +1 -0
index f8bccfa..302702e 100644
@@ -323,6 +323,7 @@ int vgic_v3_init(struct domain *d, int *mmio_count);
 #ifdef CONFIG_HAS_GICV3
 extern int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi);
 extern bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi);
+extern bool vgic_can_inject_lpi(struct vcpu *v, uint32_t vlpi);
 #else
 static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
 {

xen/include/asm-arm/vgic.h static inline bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi) · line 341

Quoted 8 lines
xen/include/asm-arm/vgic.h +5 -0
@@ -333,6 +334,11 @@ static inline bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi)
 {
     return false;
 }
+
+static inline bool vgic_can_inject_lpi(struct vcpu *v, uint32_t vlpi)
+{
+    return false;
+}

Why do you need this dummy helper?

xen/include/asm-arm/vgic.h static inline bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi) · line 344

Quoted 4 lines
xen/include/asm-arm/vgic.h +0 -0
 #endif
 
 extern int domain_vgic_register(struct domain *d, int *mmio_count);

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#86 lore raw

PATCH v2 14/27 ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-v3-its.c        | 487 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 507 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-v3-its.c
xen/arch/arm/Makefile +1 -0
index 02a8737..e7ce2c83 100644
@@ -47,6 +47,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_HAS_GICV3) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-v3-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-v3-its.c new file +487 -0

487 lines not on this page · raw message

xen/arch/arm/vgic-v3.c +0 -9
index de625bf..d1382be 100644
@@ -159,15 +159,6 @@ static void vgic_store_irouter(struct domain *d, struct vgic_irq_rank *rank,
     rank->vcpu[offset] = new_vcpu->vcpu_id;
 }
 
-static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
-{
-    /*
-     * 64 bits registers can be accessible using 32-bit and 64-bit unless
-     * stated otherwise (See 8.1.3 ARM IHI 0069A).
-     */
-    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
-}
-
 static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
                                          uint32_t gicr_reg,
                                          register_t *r)
xen/include/asm-arm/gic_v3_defs.h +19 -0
index 878bae2..1e88d6b 100644
@@ -153,6 +153,16 @@
 #define LPI_PROP_RES1                (1 << 1)
 #define LPI_PROP_ENABLED             (1 << 0)
 
+/*
+ * PIDR2: Only bits[7:4] are not implementation defined. We are
+ * emulating a GICv3 ([7:4] = 0x3).
+ *
+ * We don't emulate a specific registers scheme so implement the others
+ * bits as RES0 as recommended by the spec (see 8.1.13 in ARM IHI 0069A).
+ */
+#define GICV3_GICD_PIDR2  0x30
+#define GICV3_GICR_PIDR2  GICV3_GICD_PIDR2
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
@@ -196,6 +206,15 @@ struct rdist_region {
     bool single_rdist;
 };
 
+/*
+ * 64 bits registers can be accessible using 32-bit and 64-bit unless
+ * stated otherwise (See 8.1.3 ARM IHI 0069A).
+ */
+static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
+{
+    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
+}
+
 #endif /* __ASM_ARM_GIC_V3_DEFS_H__ */
 
 /*
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#87 lore raw

Re: [PATCH v2 14/27] ARM: vGICv3: introduce basic ITS emulation bits

Hi Andre,

Quoted 180 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-v3-its.c        | 487 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 507 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-v3-its.c
xen/arch/arm/Makefile +1 -0
index 02a8737..e7ce2c83 100644
@@ -47,6 +47,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_HAS_GICV3) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-v3-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-v3-its.c new file +148 -0
new file mode 100644
index 0000000..5337638
@@ -0,0 +1,487 @@
+/*
+ * xen/arch/arm/vgic-v3-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016,2017 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;
+    uint16_t *coll_table;
+    int max_collections;
+    uint64_t *dev_table;
+    int max_devices;
+    bool enabled;
+};
+
+/*
+ * An Interrupt Translation Table Entry: this is indexed by a
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{
+    uint32_t vlpi;
+    uint16_t collection;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
+                                   int word, int shift, int size)
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;
+
+    spin_lock(&its->vcmd_lock);
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+	    break;
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",
+                   its_cmd_get_command(cmdptr));
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/*
+ * The physical address is encoded slightly differently depending on
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):

xen/arch/arm/vgic-v3-its.c line 158

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c new file +10 -0
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+	break;
+    case VREG32(GITS_IIDR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);

Please don't hard-code Devbits (bits 17-13) value to 16 here. For dom0, at least match to Kconfig option, or use ITS hardware reported value. On Qualcomm server chips, GITS_TYPER.Devbit is set to 32bits.

Signature · 9 lines
Shanker Donthineni
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#88 lore raw

Re: [PATCH v2 14/27] ARM: vGICv3: introduce basic ITS emulation bits

Quoted 188 lines · Shanker Donthineni

Hi Andre,

Quoted 184 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-v3-its.c        | 487 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 507 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-v3-its.c
xen/arch/arm/Makefile +1 -0
index 02a8737..e7ce2c83 100644
@@ -47,6 +47,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_HAS_GICV3) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-v3-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-v3-its.c new file +152 -0
new file mode 100644
index 0000000..5337638
@@ -0,0 +1,487 @@
+/*
+ * xen/arch/arm/vgic-v3-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016,2017 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;
+    uint16_t *coll_table;
+    int max_collections;
+    uint64_t *dev_table;
+    int max_devices;
+    bool enabled;
+};
+
+/*
+ * An Interrupt Translation Table Entry: this is indexed by a
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{
+    uint32_t vlpi;
+    uint16_t collection;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
+                                   int word, int shift, int size)
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;
+
+    spin_lock(&its->vcmd_lock);
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+         break;
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",
+                   its_cmd_get_command(cmdptr));
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/*
+ * The physical address is encoded slightly differently depending on
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+     break;
+    case VREG32(GITS_IIDR):
Quoted 8 lines

xen/arch/arm/vgic-v3-its.c line 158

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c new file +6 -0
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);

Please don't hard-code Devbits (bits 17-13) value to 16 here. For dom0, at least match to Kconfig option, or use ITS hardware reported value. On Qualcomm server chips, GITS_TYPER.Devbit is set to 32bits.

On Cavium also, the number Devbits is more than 17. IRC it is 22

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#89 lore raw

Re: [PATCH v2 14/27] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara

Hi Andre,

Quoted 4 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

All the comments Stefano and I made a year ago are still valid. Please have a look the answer on <[email protected]>.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#90 lore raw

PATCH v2 15/27 ARM: vITS: introduce translation table walks

In reply to Andre Przywara

The ITS stores the target (v)CPU and the (virtual) LPI number in tables. Introduce functions to walk those tables and translate an device ID - event ID pair into a pair of virtual LPI and vCPU.
Since the final interrupt translation tables can be smaller than a page, we map them on demand (which is cheap on arm64). Also we take care of the locking on the way, since we can't easily protect those ITTs from being altered by the guest.

To allow compiling without warnings, we declare two functions as non-static for the moment.

---
 xen/arch/arm/vgic-v3-its.c | 135 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 135 insertions(+)
xen/arch/arm/vgic-v3-its.c +135 -0
index 5337638..267a573 100644
@@ -62,6 +62,141 @@ struct vits_itte
     uint16_t collection;
 };
 
+#define UNMAPPED_COLLECTION      ((uint16_t)~0)
+
+/* Must be called with the ITS lock held. */
+static struct vcpu *get_vcpu_from_collection(struct virt_its *its, int collid)
+{
+    uint16_t vcpu_id;
+
+    if ( collid >= its->max_collections )
+        return NULL;
+
+    vcpu_id = its->coll_table[collid];
+    if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >= its->d->max_vcpus )
+        return NULL;
+
+    return its->d->vcpu[vcpu_id];
+}
+
+#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8))
+#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1))
+#define DEV_TABLE_ENTRY(addr, bits)                     \
+        (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0)))
+
+static paddr_t get_itte_address(struct virt_its *its,
+                                uint32_t devid, uint32_t evid)
+{
+    paddr_t addr;
+
+    if ( devid >= its->max_devices )
+        return ~0;
+
+    if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) )
+        return ~0;
+
+    addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]);
+
+    return addr + evid * sizeof(struct vits_itte);
+}
+
+/*
+ * Looks up a given deviceID/eventID pair on an ITS and returns a pointer to
+ * the corresponding ITTE. This maps the respective guest page into Xen.
+ * Once finished with handling the ITTE, call put_devid_evid() to unmap
+ * the page again.
+ * Must be called with the ITS lock held.
+ */
+static struct vits_itte *get_devid_evid(struct virt_its *its,
+                                        uint32_t devid, uint32_t evid)
+{
+    paddr_t addr = get_itte_address(its, devid, evid);
+
+    if ( addr == ~0 )
+        return NULL;
+
+    return map_guest_pages(its->d, addr, 1);
+}
+
+/* Must be called with the ITS lock held. */
+static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
+{
+    unmap_guest_pages(itte, 1);
+}
+
+/*
+ * Queries the collection and device tables to get the vCPU and virtual
+ * LPI number for a given guest event. This takes care of mapping the
+ * respective tables and validating the values, since we can't efficiently
+ * protect the ITTs with their less-than-page-size granularity.
+ * Takes and drops the its_lock.
+ */
+bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+               struct vcpu **vcpu, uint32_t *vlpi)
+{
+    struct vits_itte *itte;
+    int collid;
+    uint32_t _vlpi;
+    struct vcpu *_vcpu;
+
+    spin_lock(&its->its_lock);
+    itte = get_devid_evid(its, devid, evid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return false;
+    }
+    collid = itte->collection;
+    _vlpi = itte->vlpi;
+    put_devid_evid(its, itte);
+
+    _vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !_vcpu )
+        return false;
+
+    if ( collid >= its->max_collections )
+        return false;
+
+    *vcpu = _vcpu;
+    *vlpi = _vlpi;
+
+    return true;
+}
+
+#define SKIP_LPI_UPDATE 1
+bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+                uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)
+{
+    struct vits_itte *itte;
+
+    if ( collid >= its->max_collections )
+        return false;
+
+    /* TODO: validate vlpi */
+
+    spin_lock(&its->its_lock);
+    itte = get_devid_evid(its, devid, evid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return false;
+    }
+
+    itte->collection = collid;
+    if ( vlpi != SKIP_LPI_UPDATE )
+        itte->vlpi = vlpi;
+
+    if ( vcpu )
+        *vcpu = get_vcpu_from_collection(its, collid);
+
+    put_devid_evid(its, itte);
+    spin_unlock(&its->its_lock);
+
+    return true;
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#91 lore raw

Re: [PATCH v2 15/27] ARM: vITS: introduce translation table walks

Hi Andre,

Quoted 21 lines · Andre Przywara

The ITS stores the target (v)CPU and the (virtual) LPI number in tables. Introduce functions to walk those tables and translate an device ID - event ID pair into a pair of virtual LPI and vCPU.
Since the final interrupt translation tables can be smaller than a page, we map them on demand (which is cheap on arm64). Also we take care of the locking on the way, since we can't easily protect those ITTs from being altered by the guest.

To allow compiling without warnings, we declare two functions as non-static for the moment.

---
 xen/arch/arm/vgic-v3-its.c | 135 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 135 insertions(+)
xen/arch/arm/vgic-v3-its.c +0 -0
index 5337638..267a573 100644

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 67

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +3 -0
@@ -62,6 +62,141 @@ struct vits_itte
     uint16_t collection;
 };
 
+#define UNMAPPED_COLLECTION      ((uint16_t)~0)
+
+/* Must be called with the ITS lock held. */

This is a call for an ASSERT in the function.

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 68

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +1 -0
+static struct vcpu *get_vcpu_from_collection(struct virt_its *its, int collid)

s/int/unsigned int/

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+{
+    uint16_t vcpu_id;
+
+    if ( collid >= its->max_collections )
+        return NULL;
+
+    vcpu_id = its->coll_table[collid];

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 85

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >= its->d->max_vcpus )
+        return NULL;
+
+    return its->d->vcpu[vcpu_id];
+}
+
+#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8))
+#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1))
+#define DEV_TABLE_ENTRY(addr, bits)                     \
+        (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0)))

The layout of dev_table[...] really needs to be explained. It took me quite a while to understand how it works. For instance why you skip the first 8 bits for the address...

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 89

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+
+static paddr_t get_itte_address(struct virt_its *its,
+                                uint32_t devid, uint32_t evid)
+{

I was expected to see the support of two-level page table for the vITS. Any plan for that?

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 93

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+    paddr_t addr;
+
+    if ( devid >= its->max_devices )
+        return ~0;

Please don't hardcode invalid address and use INVALID_PADDR.

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 96

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+
+    if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) )
+        return ~0;

Ditto.

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 98

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+
+    addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]);

You read dev_table[...] multiple time. What prevents someone to modify dev_table after you did someone sanity check?

It would be safer to do a read_atomic(..) at the beginning and use a temporary variable.

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 108

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+
+    return addr + evid * sizeof(struct vits_itte);
+}
+
+/*
+ * Looks up a given deviceID/eventID pair on an ITS and returns a pointer to
+ * the corresponding ITTE. This maps the respective guest page into Xen.
+ * Once finished with handling the ITTE, call put_devid_evid() to unmap
+ * the page again.
+ * Must be called with the ITS lock held.

This is a call for an ASSERT in the code.

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 111

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+ */
+static struct vits_itte *get_devid_evid(struct virt_its *its,
+                                        uint32_t devid, uint32_t evid)

The naming of the function is confusing. It doesn't look up a device ID/event ID but an IIT. So I would rename it to find_itte.

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 115

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+{
+    paddr_t addr = get_itte_address(its, devid, evid);
+
+    if ( addr == ~0 )

Please use INVALID_PADDR.

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+        return NULL;
+
+    return map_guest_pages(its->d, addr, 1);
+}
+
+/* Must be called with the ITS lock held. */
+static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 132

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+{
+    unmap_guest_pages(itte, 1);
+}
+
+/*
+ * Queries the collection and device tables to get the vCPU and virtual
+ * LPI number for a given guest event. This takes care of mapping the
+ * respective tables and validating the values, since we can't efficiently
+ * protect the ITTs with their less-than-page-size granularity.
+ * Takes and drops the its_lock.

I am not sure to understand the usefulness of "takes and drops the its_lock".

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 142

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+ */
+bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+               struct vcpu **vcpu, uint32_t *vlpi)
+{
+    struct vits_itte *itte;
+    int collid;
+    uint32_t _vlpi;
+    struct vcpu *_vcpu;
+
+    spin_lock(&its->its_lock);

Do we expect multiple vCPU calling read_itte at the same time? This needs to be clarify in the comments because this current function is not scalable.

Quoted 8 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+    itte = get_devid_evid(its, devid, evid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return false;
+    }
+    collid = itte->collection;
+    _vlpi = itte->vlpi;

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 160

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    put_devid_evid(its, itte);
+
+    _vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !_vcpu )
+        return false;
+
+    if ( collid >= its->max_collections )
+        return false;

No need for this check. It is already done in get_vcpu_from_collection.

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+
+    *vcpu = _vcpu;
+    *vlpi = _vlpi;
+
+    return true;
+}
+

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 177

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+#define SKIP_LPI_UPDATE 1
+bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+                uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)
+{
+    struct vits_itte *itte;
+
+    if ( collid >= its->max_collections )
+        return false;
+
+    /* TODO: validate vlpi */

This TODO needs to be fixed as soon as possible.

Quoted 16 lines
xen/arch/arm/vgic-v3-its.c +15 -0
+
+    spin_lock(&its->its_lock);
+    itte = get_devid_evid(its, devid, evid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return false;
+    }
+
+    itte->collection = collid;
+    if ( vlpi != SKIP_LPI_UPDATE )
+        itte->vlpi = vlpi;
+
+    if ( vcpu )
+        *vcpu = get_vcpu_from_collection(its, collid);

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 202

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+
+    put_devid_evid(its, itte);
+    spin_unlock(&its->its_lock);
+
+    return true;
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#92 lore raw

Re: [PATCH v2 15/27] ARM: vITS: introduce translation table walks

Hi,

Quoted 34 lines · Julien Grall

Hi Andre,

Quoted 22 lines · Andre Przywara

The ITS stores the target (v)CPU and the (virtual) LPI number in tables. Introduce functions to walk those tables and translate an device ID - event ID pair into a pair of virtual LPI and vCPU.
Since the final interrupt translation tables can be smaller than a page, we map them on demand (which is cheap on arm64). Also we take care of the locking on the way, since we can't easily protect those ITTs from being altered by the guest.

To allow compiling without warnings, we declare two functions as non-static for the moment.

---
 xen/arch/arm/vgic-v3-its.c | 135
+++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 135 insertions(+)
xen/arch/arm/vgic-v3-its.c +0 -0
index 5337638..267a573 100644

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 67

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +3 -0
@@ -62,6 +62,141 @@ struct vits_itte
     uint16_t collection;
 };
 
+#define UNMAPPED_COLLECTION      ((uint16_t)~0)
+
+/* Must be called with the ITS lock held. */

This is a call for an ASSERT in the function.

Quoted 4 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 69

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +1 -0
+static struct vcpu *get_vcpu_from_collection(struct virt_its *its,
 int collid)

s/int/unsigned int/

Fixed in v4.

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/vgic-v3-its.c +13 -0
+{
+    uint16_t vcpu_id;
+
+    if ( collid >= its->max_collections )
+        return NULL;
+
+    vcpu_id = its->coll_table[collid];
+    if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >=
 its->d->max_vcpus )
+        return NULL;
+
+    return its->d->vcpu[vcpu_id];
+}
+
Quoted 8 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 85

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8))
+#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1))
+#define DEV_TABLE_ENTRY(addr, bits)                     \
+        (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0)))

The layout of dev_table[...] really needs to be explained. It took me quite a while to understand how it works. For instance why you skip the first 8 bits for the address...

Fixed in v3.

Quoted 13 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 89

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+
+static paddr_t get_itte_address(struct virt_its *its,
+                                uint32_t devid, uint32_t evid)
+{

I was expected to see the support of two-level page table for the vITS. Any plan for that?

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 93

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+    paddr_t addr;
+
+    if ( devid >= its->max_devices )
+        return ~0;
Quoted 7 lines

Please don't hardcode invalid address and use INVALID_PADDR.

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 96

Quoted 3 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+
+    if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) )
+        return ~0;

Ditto.

Fixed in v3.

Quoted 8 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 100

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+
+    addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]);

You read dev_table[...] multiple time. What prevents someone to modify dev_table after you did someone sanity check?

It would be safer to do a read_atomic(..) at the beginning and use a temporary variable.

Fixed in v4.

Quoted 15 lines
Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+
+    return addr + evid * sizeof(struct vits_itte);
+}
+
+/*
+ * Looks up a given deviceID/eventID pair on an ITS and returns a
Quoted 5 lines

pointer to
+ * the corresponding ITTE. This maps the respective guest page into Xen.
+ * Once finished with handling the ITTE, call put_devid_evid() to unmap
+ * the page again.
+ * Must be called with the ITS lock held.

This is a call for an ASSERT in the code.

Quoted 6 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 111

Quoted 3 lines

+ */

xen/arch/arm/vgic-v3-its.c +2 -0
+static struct vits_itte *get_devid_evid(struct virt_its *its,
+                                        uint32_t devid, uint32_t evid)

The naming of the function is confusing. It doesn't look up a device ID/event ID but an IIT. So I would rename it to find_itte.

Fixed in v3.

Quoted 7 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 115

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+{
+    paddr_t addr = get_itte_address(its, devid, evid);
+
+    if ( addr == ~0 )

Please use INVALID_PADDR.

Fixed in v3.

Quoted 13 lines
Quoted 13 lines
xen/arch/arm/vgic-v3-its.c +13 -0
+        return NULL;
+
+    return map_guest_pages(its->d, addr, 1);
+}
+
+/* Must be called with the ITS lock held. */
+static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
+{
+    unmap_guest_pages(itte, 1);
+}
+
+/*
+ * Queries the collection and device tables to get the vCPU and virtual
Quoted 8 lines
Quoted 5 lines

+ * LPI number for a given guest event. This takes care of mapping the
+ * respective tables and validating the values, since we can't efficiently
+ * protect the ITTs with their less-than-page-size granularity.
+ * Takes and drops the its_lock.

I am not sure to understand the usefulness of "takes and drops the its_lock".

It tells people that they should not hold the its_lock when calling this function, as this might deadlock. Also it means that this function takes care about locking itself.
Improved the wording in v4.

Quoted 6 lines
Quoted 6 lines

+ */

xen/arch/arm/vgic-v3-its.c +5 -0
+bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+               struct vcpu **vcpu, uint32_t *vlpi)
+{
+    struct vits_itte *itte;
+    int collid;
Quoted 8 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 142

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+    uint32_t _vlpi;
+    struct vcpu *_vcpu;
+
+    spin_lock(&its->its_lock);

Do we expect multiple vCPU calling read_itte at the same time? This needs to be clarify in the comments because this current function is not scalable.

We need this lock here because this protects our data structures. A guest locks accesses to the ITS command queue anyway (otherwrite multiple VCPUs would race for CWRITER and CREADR), so I don't see a real problem. And this lock is pre ITS, not per guest.

Quoted 12 lines
Quoted 12 lines
xen/arch/arm/vgic-v3-its.c +12 -0
+    itte = get_devid_evid(its, devid, evid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return false;
+    }
+    collid = itte->collection;
+    _vlpi = itte->vlpi;
+    put_devid_evid(its, itte);
+
+    _vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
Quoted 8 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 160

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+
+    if ( !_vcpu )
+        return false;
+
+    if ( collid >= its->max_collections )
+        return false;

No need for this check. It is already done in get_vcpu_from_collection.

Good point, dropped in v4.

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +11 -0
+
+    *vcpu = _vcpu;
+    *vlpi = _vlpi;
+
+    return true;
+}
+
+#define SKIP_LPI_UPDATE 1
+bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+                uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)
+{
Quoted 8 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 177

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+    struct vits_itte *itte;
+
+    if ( collid >= its->max_collections )
+        return false;
+
+    /* TODO: validate vlpi */

This TODO needs to be fixed as soon as possible.

Done in v3.

Cheers,
Andre.

Quoted 20 lines
Quoted 20 lines
xen/arch/arm/vgic-v3-its.c +20 -0
+
+    spin_lock(&its->its_lock);
+    itte = get_devid_evid(its, devid, evid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return false;
+    }
+
+    itte->collection = collid;
+    if ( vlpi != SKIP_LPI_UPDATE )
+        itte->vlpi = vlpi;
+
+    if ( vcpu )
+        *vcpu = get_vcpu_from_collection(its, collid);
+
+    put_devid_evid(its, itte);
+    spin_unlock(&its->its_lock);
+
+    return true;
Quoted 9 lines

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 202

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/

Cheers,

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#93 lore raw

Re: [PATCH v2 15/27] ARM: vITS: introduce translation table walks

Hi Andre,

Quoted 17 lines · Andre Przywara
Quoted 15 lines · Julien Grall

xen/arch/arm/vgic-v3-its.c struct vits_itte · line 142

Quoted 10 lines · Andre Przywara

+ */

xen/arch/arm/vgic-v3-its.c +9 -0
+bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+               struct vcpu **vcpu, uint32_t *vlpi)
+{
+    struct vits_itte *itte;
+    int collid;
+    uint32_t _vlpi;
+    struct vcpu *_vcpu;
+
+    spin_lock(&its->its_lock);

Do we expect multiple vCPU calling read_itte at the same time? This needs to be clarify in the comments because this current function is not scalable.

Quoted 4 lines

We need this lock here because this protects our data structures. A guest locks accesses to the ITS command queue anyway (otherwrite multiple VCPUs would race for CWRITER and CREADR), so I don't see a real problem. And this lock is pre ITS, not per guest.

Fair point, please ignore my request then.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#94 lore raw

PATCH v2 16/27 ARM: vITS: handle CLEAR command

In reply to Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

---
 xen/arch/arm/vgic-v3-its.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
xen/arch/arm/vgic-v3-its.c +33 -2
index 267a573..e808f43 100644
@@ -131,8 +131,8 @@ static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
  * protect the ITTs with their less-than-page-size granularity.
  * Takes and drops the its_lock.
  */
-bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
-               struct vcpu **vcpu, uint32_t *vlpi)
+static bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+                      struct vcpu **vcpu, uint32_t *vlpi)
 {
     struct vits_itte *itte;
     int collid;
@@ -216,6 +216,34 @@ static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
 #define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
 #define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
 
+static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct pending_irq *pirq;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    if ( !read_itte(its, devid, eventid, &vcpu, &vlpi) )
+        return -1;
+
+    /* Remove a pending, but not yet injected guest IRQ. */
+    pirq = lpi_to_pending(vcpu, vlpi, false);
+    if ( pirq )
+    {
+        clear_bit(GIC_IRQ_GUEST_QUEUED, &pirq->status);
+        gic_remove_from_queues(vcpu, vlpi);
+
+        /* Mark this pending IRQ struct as availabe again. */
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;
+    }
+
+    clear_bit(vlpi - LPI_OFFSET, vcpu->arch.vgic.pendtable);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -236,6 +264,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#95 lore raw

Re: [PATCH v2 16/27] ARM: vITS: handle CLEAR command

Hi Andre,

Quoted 14 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

---
 xen/arch/arm/vgic-v3-its.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
xen/arch/arm/vgic-v3-its.c +0 -0
index 267a573..e808f43 100644

xen/arch/arm/vgic-v3-its.c static void put_devid_evid(struct virt_its *its, struct vits_itte *itte) · line 134

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +1 -2
@@ -131,8 +131,8 @@ static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
  * protect the ITTs with their less-than-page-size granularity.
  * Takes and drops the its_lock.
  */
-bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
-               struct vcpu **vcpu, uint32_t *vlpi)
+static bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,

NIT: Please mention in the commit message why you turned those functions to static.

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +3 -0
+                      struct vcpu **vcpu, uint32_t *vlpi)
 {
     struct vits_itte *itte;
     int collid;
@@ -216,6 +216,34 @@ static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
 #define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
 #define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
 
+static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
+{

xen/arch/arm/vgic-v3-its.c static uint64_t its_cmd_mask_field(uint64_t *its_cmd, · line 230

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct pending_irq *pirq;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    if ( !read_itte(its, devid, eventid, &vcpu, &vlpi) )
+        return -1;
+
+    /* Remove a pending, but not yet injected guest IRQ. */

Copying Stefano's comment from last year:

"We need to check that the vlpi hasn't already been added to an LR register. We can do that with GIC_IRQ_GUEST_VISIBLE.

In case GIC_IRQ_GUEST_VISIBLE is set, we need to clear the lr (clear_lr). If we don't handle this case, we should at least print a warning."

Quoted 20 lines
xen/arch/arm/vgic-v3-its.c +16 -0
+    pirq = lpi_to_pending(vcpu, vlpi, false);
+    if ( pirq )
+    {
+        clear_bit(GIC_IRQ_GUEST_QUEUED, &pirq->status);
+        gic_remove_from_queues(vcpu, vlpi);
+
+        /* Mark this pending IRQ struct as availabe again. */
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;
+    }
+
+    clear_bit(vlpi - LPI_OFFSET, vcpu->arch.vgic.pendtable);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 268

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -236,6 +264,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);

Should not you check the return for its_handle_clear?

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 272

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +1 -0
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#96 lore raw

Re: [PATCH v2 16/27] ARM: vITS: handle CLEAR command

Hi,

Quoted 16 lines · Julien Grall

Hi Andre,

Quoted 13 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

---
 xen/arch/arm/vgic-v3-its.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
xen/arch/arm/vgic-v3-its.c +0 -0
index 267a573..e808f43 100644

[....]

Quoted 9 lines

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, · line 269

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -236,6 +264,9 @@ static int vgic_its_handle_cmds(struct domain *d,
 struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);

Should not you check the return for its_handle_clear?

That sounds obvious, but actually I don't know of a good way of handling this. Blame the architecture, if you like. Passing the error value up would end up in the MMIO handler, where it is not correct to return an error (since the CWRITER MMIO access itself was successful).

So I picked one of the behaviors described in 6.3.2 "Command errors", which is simply to ignore the command.
If we have a nice way of injecting an SError (do we?), I _could_ check GITS_TYPER.SEIS and then inject it. But effectively this would be untested code, since Linux does not use this feature.

So any idea here? I don't think the typical Xen answer of "Crash the guest!" is compliant with the architecture, which leaves three choices, and setting the box on fire is not one of them.

That's why I chose to ignore the return value at this point, but at least generate the error condition internally and bail out early. At least for the Tech Preview Dom0 edition of the ITS emulation. If we later gain a good method of handling (and testing!) command errors, we can easily add them.

Cheers,
Andre.

Quoted 10 lines
Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +1 -0
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore

SYNC. */

         break;

Regards,

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#97 lore raw

Re: [PATCH v2 16/27] ARM: vITS: handle CLEAR command

Quoted 32 lines · Andre Przywara

Hi,

Quoted 16 lines · Julien Grall

Hi Andre,

Quoted 13 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

---
 xen/arch/arm/vgic-v3-its.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
xen/arch/arm/vgic-v3-its.c +0 -0
index 267a573..e808f43 100644

[....]

Quoted 9 lines

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, · line 269

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -236,6 +264,9 @@ static int vgic_its_handle_cmds(struct domain *d,
 struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);

Should not you check the return for its_handle_clear?

Quoted 8 lines

That sounds obvious, but actually I don't know of a good way of handling this. Blame the architecture, if you like. Passing the error value up would end up in the MMIO handler, where it is not correct to return an error (since the CWRITER MMIO access itself was successful).

So I picked one of the behaviors described in 6.3.2 "Command errors", which is simply to ignore the command.
If we have a nice way of injecting an SError (do we?),

We do with Wei's series, which is very likely to go in before this one:

http://marc.info/?l=xen-devel&m=148940261914581

In particular, see [email protected].

Quoted 2 lines

I _could_ check GITS_TYPER.SEIS and then inject it. But effectively this would be untested code, since Linux does not use this feature.

Keep in mind that Xen supports a wide range of OSes. GITS_TYPER is emulated by Xen in the virtual ITS, right? If so, it doesn't matter the hardware value of SEIS, we can set the virtual value to 1.

Quoted 9 lines

So any idea here? I don't think the typical Xen answer of "Crash the guest!" is compliant with the architecture, which leaves three choices, and setting the box on fire is not one of them.

That's why I chose to ignore the return value at this point, but at least generate the error condition internally and bail out early. At least for the Tech Preview Dom0 edition of the ITS emulation. If we later gain a good method of handling (and testing!) command errors, we can easily add them.

At the very least, we need to print a warning (rate limited, so probably gdprintk). All errors that cannot be handled otherwise need to result in a warning.

Sending an SError would be fine, I think.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#98 lore raw

Re: [PATCH v2 16/27] ARM: vITS: handle CLEAR command

Hi,

Quoted 46 lines · Stefano Stabellini
Quoted 32 lines · Andre Przywara

Hi,

Quoted 16 lines · Julien Grall

Hi Andre,

Quoted 13 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

---
 xen/arch/arm/vgic-v3-its.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
xen/arch/arm/vgic-v3-its.c +0 -0
index 267a573..e808f43 100644

[....]

Quoted 9 lines

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, · line 269

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -236,6 +264,9 @@ static int vgic_its_handle_cmds(struct domain *d,
 struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);

Should not you check the return for its_handle_clear?

Quoted 8 lines

That sounds obvious, but actually I don't know of a good way of handling this. Blame the architecture, if you like. Passing the error value up would end up in the MMIO handler, where it is not correct to return an error (since the CWRITER MMIO access itself was successful).

So I picked one of the behaviors described in 6.3.2 "Command errors", which is simply to ignore the command.
If we have a nice way of injecting an SError (do we?),

We do with Wei's series, which is very likely to go in before this one:

http://marc.info/?l=xen-devel&m=148940261914581

Quoted 7 lines

In particular, see [email protected].

Quoted 2 lines

I _could_ check GITS_TYPER.SEIS and then inject it. But effectively this would be untested code, since Linux does not use this feature.

Keep in mind that Xen supports a wide range of OSes.

I clearly understand that and don't question it. What I wanted to point out is that using an SError to signal ITS errors is mostly uncharted territory (see the current discussion about handling SErrors in Linux[1]). So we would add code that cannot be tested. And given the current situation and the tech preview status of the ITS support I'd prefer to not go there at the moment.

I would offer to annotate the error returns with the actual ITS error codes (as in the KVM code, for instance [2]).
Then put a comment in the code explaining the missing error signalling situation, and we create a ticket to notify ourselves of fixing this in the future.
Does that make sense?

Quoted 15 lines

GITS_TYPER is
emulated by Xen in the virtual ITS, right? If so, it doesn't matter the hardware value of SEIS, we can set the virtual value to 1.

Quoted 9 lines

So any idea here? I don't think the typical Xen answer of "Crash the guest!" is compliant with the architecture, which leaves three choices, and setting the box on fire is not one of them.

That's why I chose to ignore the return value at this point, but at least generate the error condition internally and bail out early. At least for the Tech Preview Dom0 edition of the ITS emulation. If we later gain a good method of handling (and testing!) command errors, we can easily add them.

Quoted 3 lines

At the very least, we need to print a warning (rate limited, so probably gdprintk). All errors that cannot be handled otherwise need to result in a warning.

OK, I will do this.

Quoted 1 line

Sending an SError would be fine, I think.

As mentioned above, I'd refrain from it at the moment.

Cheers,
Andre.

[1]
http://lists.infradead.org/pipermail/linux-arm-kernel/2017-March/496722.html
[2]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/virt/kvm/arm/vgic/vgic-its.c#n561

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#99 lore raw

Re: [PATCH v2 16/27] ARM: vITS: handle CLEAR command

Hi Andre,

Quoted 55 lines · Andre Przywara

Hi,

Quoted 52 lines · Stefano Stabellini
Quoted 32 lines · Andre Przywara

Hi,

Quoted 16 lines · Julien Grall

Hi Andre,

Quoted 13 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

---
 xen/arch/arm/vgic-v3-its.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
xen/arch/arm/vgic-v3-its.c +0 -0
index 267a573..e808f43 100644

[....]

Quoted 9 lines

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, · line 269

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -236,6 +264,9 @@ static int vgic_its_handle_cmds(struct domain *d,
 struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);

Should not you check the return for its_handle_clear?

Quoted 8 lines

That sounds obvious, but actually I don't know of a good way of handling this. Blame the architecture, if you like. Passing the error value up would end up in the MMIO handler, where it is not correct to return an error (since the CWRITER MMIO access itself was successful).

So I picked one of the behaviors described in 6.3.2 "Command errors", which is simply to ignore the command.
If we have a nice way of injecting an SError (do we?),

We do with Wei's series, which is very likely to go in before this one:

http://marc.info/?l=xen-devel&m=148940261914581

In particular, see [email protected].

Quoted 2 lines

I _could_ check GITS_TYPER.SEIS and then inject it. But effectively this would be untested code, since Linux does not use this feature.

Quoted 6 lines
Quoted 1 line

Keep in mind that Xen supports a wide range of OSes.

I clearly understand that and don't question it. What I wanted to point out is that using an SError to signal ITS errors is mostly uncharted territory (see the current discussion about handling SErrors in Linux[1]). So we would add code that cannot be tested.

The SError is sent or not, it is not important whether the OS is able to handle it or not.

And justifying with "the code cannot be tested" is not argument as I would have expected you to hack Linux to exercise the vITS.

Quoted 6 lines

And given the
current situation and the tech preview status of the ITS support I'd prefer to not go there at the moment.

I would offer to annotate the error returns with the actual ITS error codes (as in the KVM code, for instance [2]).

I am happy with that as a first step.

Quoted 3 lines

Then put a comment in the code explaining the missing error signalling situation, and we create a ticket to notify ourselves of fixing this in the future.

I think we still need to log the error. It is useful for the user to know what's going on. You could imagine someone trying to implement an OS using Xen and KVM. He would be really grateful to know what's going on.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#100 lore raw

PATCH v2 17/27 ARM: vITS: handle INT command

In reply to Andre Przywara

The INT command sets a given LPI identified by a DeviceID/EventID pair as pending and thus triggers it to be injected.

---
 xen/arch/arm/vgic-v3-its.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
xen/arch/arm/vgic-v3-its.c +23 -0
index e808f43..23476f9 100644
@@ -244,6 +244,26 @@ static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_int(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+    uint8_t prop;
+
+    if ( !read_itte(its, devid, eventid, &vcpu, &vlpi) )
+        return -1;
+
+    prop = vcpu->domain->arch.vgic.proptable[vlpi - LPI_OFFSET];
+    if ( prop & LPI_PROP_ENABLED )
+        vgic_vcpu_inject_irq(vcpu, vlpi);
+    else
+        set_bit(vlpi - LPI_OFFSET, vcpu->arch.vgic.pendtable);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -267,6 +287,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_CLEAR:
             its_handle_clear(its, cmdptr);
             break;
+        case GITS_CMD_INT:
+            its_handle_int(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#101 lore raw

Re: [PATCH v2 17/27] ARM: vITS: handle INT command

Hi Andre,

Quoted 18 lines · Andre Przywara

The INT command sets a given LPI identified by a DeviceID/EventID pair as pending and thus triggers it to be injected.

---
 xen/arch/arm/vgic-v3-its.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
xen/arch/arm/vgic-v3-its.c +2 -0
index e808f43..23476f9 100644
@@ -244,6 +244,26 @@ static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_int(struct virt_its *its, uint64_t *cmdptr)
+{

xen/arch/arm/vgic-v3-its.c static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr) · line 258

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+    uint8_t prop;
+
+    if ( !read_itte(its, devid, eventid, &vcpu, &vlpi) )
+        return -1;
+
+    prop = vcpu->domain->arch.vgic.proptable[vlpi - LPI_OFFSET];

I looked at the code and cannot find any code which will ensure that the proptable is not NULL.

Quoted 12 lines
xen/arch/arm/vgic-v3-its.c +8 -0
+    if ( prop & LPI_PROP_ENABLED )
+        vgic_vcpu_inject_irq(vcpu, vlpi);
+    else
+        set_bit(vlpi - LPI_OFFSET, vcpu->arch.vgic.pendtable);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 291

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -267,6 +287,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_CLEAR:
             its_handle_clear(its, cmdptr);
             break;
+        case GITS_CMD_INT:
+            its_handle_int(its, cmdptr);

Should not you check the return value?

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 295

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +1 -0
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#102 lore raw

PATCH v2 18/27 ARM: vITS: handle MAPC command

In reply to Andre Przywara

The MAPC command associates a given collection ID with a given redistributor, thus mapping collections to VCPUs.
We just store the vcpu_id in the collection table for that.

---
 xen/arch/arm/vgic-v3-its.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
xen/arch/arm/vgic-v3-its.c +30 -0
index 23476f9..175a213 100644
@@ -264,6 +264,33 @@ static int its_handle_int(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    uint64_t rdbase = its_cmd_mask_field(cmdptr, 2, 16, 44);
+    int ret = -1;
+
+    if ( collid >= its->max_collections )
+        return ret;
+
+    if ( rdbase >= its->d->max_vcpus )
+        return ret;
+
+    spin_lock(&its->its_lock);
+    if ( its->coll_table )
+    {
+        if ( its_cmd_get_validbit(cmdptr) )
+            its->coll_table[collid] = rdbase;
+        else
+            its->coll_table[collid] = UNMAPPED_COLLECTION;
+
+        ret = 0;
+    }
+    spin_unlock(&its->its_lock);
+
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -290,6 +317,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INT:
             its_handle_int(its, cmdptr);
             break;
+        case GITS_CMD_MAPC:
+            its_handle_mapc(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#103 lore raw

PATCH v2 19/27 ARM: vITS: handle MAPD command

In reply to Andre Przywara

The MAPD command maps a device by associating a memory region for storing ITEs with a certain device ID.
We store the given guest physical address in the device table, and, if this command comes from Dom0, tell the host ITS driver about this new mapping, so it can issue the corresponing host MAPD command and create the required tables.
We don't map the device tables permanently, as their alignment requirement is only 256 Bytes, thus making mapping of several tables complicated. Instead we map the device tables on demand when we need them later.

---
 xen/arch/arm/vgic-v3-its.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
xen/arch/arm/vgic-v3-its.c +44 -0
index 175a213..c26d5d4 100644
@@ -38,6 +38,7 @@
 /* Data structure to describe a virtual ITS */
 struct virt_its {
     struct domain *d;
+    paddr_t doorbell_address;
     spinlock_t vcmd_lock;       /* protects the virtual command buffer */
     uint64_t cbaser;
     uint64_t *cmdbuf;
@@ -291,6 +292,46 @@ static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
     return ret;
 }
 
+static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    int size = its_cmd_get_size(cmdptr);
+    bool valid = its_cmd_get_validbit(cmdptr);
+    paddr_t itt_addr = its_cmd_mask_field(cmdptr, 2, 0, 52) & GENMASK(51, 8);
+    int ret;
+
+    if ( !its->dev_table )
+        return -1;
+
+    /*
+     * There is no easy and clean way for Xen to know the ITS device ID of a
+     * particular (PCI) device, so we have to rely on the guest telling
+     * us about it. For *now* we are just using the device ID *Dom0* uses,
+     * because the driver there has the actual knowledge.
+     * Eventually this will be replaced with a dedicated hypercall to
+     * announce pass-through of devices.
+     */
+    if ( is_hardware_domain(its->d) )
+    {
+        /* Dom0's ITSes are mapped 1:1, so both address are the same. */
+        ret = gicv3_its_map_guest_device(its->d, its->doorbell_address, devid,
+                                         its->doorbell_address, devid,
+                                         BIT(size + 1), valid);
+        if ( ret )
+            return ret;
+    }
+
+    spin_lock(&its->its_lock);
+    if ( valid )
+        its->dev_table[devid] = DEV_TABLE_ENTRY(itt_addr, size + 1);
+    else
+        its->dev_table[devid] = 0;
+
+    spin_unlock(&its->its_lock);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -320,6 +361,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
+        case GITS_CMD_MAPD:
+            its_handle_mapd(its, cmdptr);
+	    break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#104 lore raw

Re: [PATCH v2 19/27] ARM: vITS: handle MAPD command

Hi Andre,

Quoted 47 lines · Andre Przywara

The MAPD command maps a device by associating a memory region for storing ITEs with a certain device ID.
We store the given guest physical address in the device table, and, if this command comes from Dom0, tell the host ITS driver about this new mapping, so it can issue the corresponing host MAPD command and create the required tables.
We don't map the device tables permanently, as their alignment requirement is only 256 Bytes, thus making mapping of several tables complicated. Instead we map the device tables on demand when we need them later.

---
 xen/arch/arm/vgic-v3-its.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
xen/arch/arm/vgic-v3-its.c +16 -0
index 175a213..c26d5d4 100644
@@ -38,6 +38,7 @@
 /* Data structure to describe a virtual ITS */
 struct virt_its {
     struct domain *d;
+    paddr_t doorbell_address;
     spinlock_t vcmd_lock;       /* protects the virtual command buffer */
     uint64_t cbaser;
     uint64_t *cmdbuf;
@@ -291,6 +292,46 @@ static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
     return ret;
 }
 
+static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    int size = its_cmd_get_size(cmdptr);
+    bool valid = its_cmd_get_validbit(cmdptr);
+    paddr_t itt_addr = its_cmd_mask_field(cmdptr, 2, 0, 52) & GENMASK(51, 8);
+    int ret;
+
+    if ( !its->dev_table )
+        return -1;
+
+    /*
+     * There is no easy and clean way for Xen to know the ITS device ID of a
+     * particular (PCI) device, so we have to rely on the guest telling
+     * us about it. For *now* we are just using the device ID *Dom0* uses,

xen/arch/arm/vgic-v3-its.c static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr) · line 319

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+     * because the driver there has the actual knowledge.
+     * Eventually this will be replaced with a dedicated hypercall to
+     * announce pass-through of devices.
+     */
+    if ( is_hardware_domain(its->d) )
+    {
+        /* Dom0's ITSes are mapped 1:1, so both address are the same. */
+        ret = gicv3_its_map_guest_device(its->d, its->doorbell_address, devid,
+                                         its->doorbell_address, devid,
+                                         BIT(size + 1), valid);

The size should be satinized against the number of event support by the vITS.

And it should also be sanitzed in gicv3_its_map_* against the number of event supported by the pITS.

Quoted 19 lines
xen/arch/arm/vgic-v3-its.c +15 -0
+        if ( ret )
+            return ret;
+    }
+
+    spin_lock(&its->its_lock);
+    if ( valid )
+        its->dev_table[devid] = DEV_TABLE_ENTRY(itt_addr, size + 1);
+    else
+        its->dev_table[devid] = 0;
+
+    spin_unlock(&its->its_lock);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 365

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -320,6 +361,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
+        case GITS_CMD_MAPD:
+            its_handle_mapd(its, cmdptr);

Should not you check the return value?

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 369

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +1 -0
+	    break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#105 lore raw

PATCH v2 20/27 ARM: vITS: handle MAPTI command

In reply to Andre Przywara

The MAPTI commands associates a DeviceID/EventID pair with a LPI/CPU pair and actually instantiates LPI interrupts.
We connect the already allocated host LPI to this virtual LPI, so that any triggering IRQ on the host can be quickly forwarded to a guest.

---
 xen/arch/arm/gic-v3-its.c        | 63 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3-lpi.c        | 18 ++++++++++++
 xen/arch/arm/vgic-v3-its.c       | 27 +++++++++++++++--
 xen/include/asm-arm/gic_v3_its.h |  6 ++++
 4 files changed, 112 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +63 -0
index 5a2dbec..e2fcf50 100644
@@ -724,6 +724,69 @@ restart:
     spin_unlock(&d->arch.vgic.its_devices_lock);
 }
 
+/*
+ * Translates an event for a given guest device ID into the associated host
+ * LPI number. This can be used to look up the mapped guest LPI.
+ */
+static uint32_t translate_event(struct domain *d, paddr_t doorbell,
+                                uint32_t devid, uint32_t eventid)
+{
+    struct rb_node *node;
+    struct its_devices *dev;
+    uint32_t host_lpi = 0;
+    int cmp;
+
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    node = d->arch.vgic.its_devices.rb_node;
+    while (node)
+    {
+        dev = rb_entry(node, struct its_devices, rbnode);
+        cmp = compare_its_guest_devices(dev, doorbell, devid);
+
+        if ( !cmp )
+        {
+            if ( eventid >= dev->eventids )
+                goto out;
+
+            host_lpi = dev->host_lpis[eventid / LPI_BLOCK] +
+                                (eventid % LPI_BLOCK);
+            if ( !is_lpi(host_lpi) )
+                host_lpi = 0;
+            goto out;
+        }
+
+        if ( cmp > 0 )
+            node = node->rb_left;
+        else
+            node = node->rb_right;
+    }
+
+out:
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+
+    return host_lpi;
+}
+
+/*
+ * Connects the event ID for an already assigned device to the given VCPU/vLPI
+ * pair. The corresponding physical LPI is already mapped on the host side
+ * (when assigning the physical device to the guest), so we just connect the
+ * target VCPU/vLPI pair to that interrupt to inject it properly if it fires.
+ */
+int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell_address,
+                             uint32_t devid, uint32_t eventid,
+                             struct vcpu *v, uint32_t virt_lpi)
+{
+    uint32_t host_lpi = translate_event(d, doorbell_address, devid, eventid);
+
+    if ( !host_lpi )
+        return -ENOENT;
+
+    gicv3_lpi_update_host_entry(host_lpi, d->domain_id, v->vcpu_id, virt_lpi);
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
xen/arch/arm/gic-v3-lpi.c +18 -0
index 994698e..c110ec9 100644
@@ -153,6 +153,24 @@ void do_LPI(unsigned int lpi)
     vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
 }
 
+int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
+                                unsigned int vcpu_id, uint32_t virt_lpi)
+{
+    union host_lpi *hlpip, hlpi;
+
+    host_lpi -= LPI_OFFSET;
+
+    hlpip = &lpi_data.host_lpis[host_lpi / HOST_LPIS_PER_PAGE][host_lpi % HOST_LPIS_PER_PAGE];
+
+    hlpi.virt_lpi = virt_lpi;
+    hlpi.dom_id = domain_id;
+    hlpi.vcpu_id = vcpu_id;
+
+    write_u64_atomic(&hlpip->data, hlpi.data);
+
+    return 0;
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/vgic-v3-its.c +25 -2
index c26d5d4..600ff69 100644
@@ -167,8 +167,8 @@ static bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
 }
 
 #define SKIP_LPI_UPDATE 1
-bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
-                uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)
+static bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+                       uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)
 {
     struct vits_itte *itte;
 
@@ -332,6 +332,25 @@ static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    uint32_t intid = its_cmd_get_physical_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    if ( its_cmd_get_command(cmdptr) == GITS_CMD_MAPI )
+        intid = eventid;
+
+    if ( !write_itte(its, devid, eventid, collid, intid, &vcpu) )
+        return -1;
+
+    gicv3_assign_guest_event(its->d, its->doorbell_address, devid, eventid, vcpu, intid);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -364,6 +383,10 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPD:
             its_handle_mapd(its, cmdptr);
 	    break;
+        case GITS_CMD_MAPI:
+        case GITS_CMD_MAPTI:
+            its_handle_mapti(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
xen/include/asm-arm/gic_v3_its.h +6 -0
index 2387972..1ea00e9 100644
@@ -163,6 +163,12 @@ int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
                                   uint32_t host_devid, uint32_t eventid);
 int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 
+int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell,
+                             uint32_t devid, uint32_t eventid,
+                             struct vcpu *v, uint32_t virt_lpi);
+int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
+                                unsigned int vcpu_id, uint32_t virt_lpi);
+
 #else
 
 static LIST_HEAD(host_its_list);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#106 lore raw

Re: [PATCH v2 20/27] ARM: vITS: handle MAPTI command

Hi Andre,

Quoted 38 lines · Andre Przywara

The MAPTI commands associates a DeviceID/EventID pair with a LPI/CPU pair and actually instantiates LPI interrupts.
We connect the already allocated host LPI to this virtual LPI, so that any triggering IRQ on the host can be quickly forwarded to a guest.

---
 xen/arch/arm/gic-v3-its.c        | 63 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3-lpi.c        | 18 ++++++++++++
 xen/arch/arm/vgic-v3-its.c       | 27 +++++++++++++++--
 xen/include/asm-arm/gic_v3_its.h |  6 ++++
 4 files changed, 112 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +17 -0
index 5a2dbec..e2fcf50 100644
@@ -724,6 +724,69 @@ restart:
     spin_unlock(&d->arch.vgic.its_devices_lock);
 }
 
+/*
+ * Translates an event for a given guest device ID into the associated host
+ * LPI number. This can be used to look up the mapped guest LPI.
+ */
+static uint32_t translate_event(struct domain *d, paddr_t doorbell,
+                                uint32_t devid, uint32_t eventid)
+{
+    struct rb_node *node;
+    struct its_devices *dev;
+    uint32_t host_lpi = 0;
+    int cmp;
+
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    node = d->arch.vgic.its_devices.rb_node;
+    while (node)
+    {
+        dev = rb_entry(node, struct its_devices, rbnode);

xen/arch/arm/gic-v3-its.c restart: · line 753

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+        cmp = compare_its_guest_devices(dev, doorbell, devid);
+
+        if ( !cmp )
+        {
+            if ( eventid >= dev->eventids )
+                goto out;
+
+            host_lpi = dev->host_lpis[eventid / LPI_BLOCK] +
+                                (eventid % LPI_BLOCK);
+            if ( !is_lpi(host_lpi) )

Hmmm, I don't understand this check. host_lpi should always be an LPI. No?

Quoted 14 lines
xen/arch/arm/gic-v3-its.c +14 -0
+                host_lpi = 0;
+            goto out;
+        }
+
+        if ( cmp > 0 )
+            node = node->rb_left;
+        else
+            node = node->rb_right;
+    }
+
+out:
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+
+    return host_lpi;

xen/arch/arm/gic-v3-its.c restart: · line 777

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+}
+
+/*
+ * Connects the event ID for an already assigned device to the given VCPU/vLPI
+ * pair. The corresponding physical LPI is already mapped on the host side
+ * (when assigning the physical device to the guest), so we just connect the
+ * target VCPU/vLPI pair to that interrupt to inject it properly if it fires.
+ */
+int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell_address,
+                             uint32_t devid, uint32_t eventid,

It looks like to me that devid is the virtual deviceID. If so, please prefix with 'v', otherwise 'p'.

Quoted 20 lines
xen/arch/arm/gic-v3-its.c +12 -0
+                             struct vcpu *v, uint32_t virt_lpi)
+{
+    uint32_t host_lpi = translate_event(d, doorbell_address, devid, eventid);
+
+    if ( !host_lpi )
+        return -ENOENT;
+
+    gicv3_lpi_update_host_entry(host_lpi, d->domain_id, v->vcpu_id, virt_lpi);
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
xen/arch/arm/gic-v3-lpi.c +0 -0
index 994698e..c110ec9 100644

xen/arch/arm/gic-v3-lpi.c void do_LPI(unsigned int lpi) · line 161

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
@@ -153,6 +153,24 @@ void do_LPI(unsigned int lpi)
     vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
 }
 
+int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
+                                unsigned int vcpu_id, uint32_t virt_lpi)
+{
+    union host_lpi *hlpip, hlpi;
+
+    host_lpi -= LPI_OFFSET;

I would add an ASSERT(host_lpi > LPI_OFFSET);

Quoted 20 lines
xen/arch/arm/gic-v3-lpi.c +12 -0
+
+    hlpip = &lpi_data.host_lpis[host_lpi / HOST_LPIS_PER_PAGE][host_lpi % HOST_LPIS_PER_PAGE];
+
+    hlpi.virt_lpi = virt_lpi;
+    hlpi.dom_id = domain_id;
+    hlpi.vcpu_id = vcpu_id;
+
+    write_u64_atomic(&hlpip->data, hlpi.data);
+
+    return 0;
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/vgic-v3-its.c +0 -0
index c26d5d4..600ff69 100644

xen/arch/arm/vgic-v3-its.c static bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid, · line 171

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +2 -2
@@ -167,8 +167,8 @@ static bool read_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
 }
 
 #define SKIP_LPI_UPDATE 1
-bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
-                uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)
+static bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
+                       uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)

Please explain in the commit message why you move to static.

xen/arch/arm/vgic-v3-its.c static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr) · line 340

Quoted 13 lines
xen/arch/arm/vgic-v3-its.c +6 -0
 {
     struct vits_itte *itte;
 
@@ -332,6 +332,25 @@ static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    uint32_t intid = its_cmd_get_physical_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);

This should be unsigned.

xen/arch/arm/vgic-v3-its.c static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr) · line 349

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +9 -0
+    struct vcpu *vcpu;
+
+    if ( its_cmd_get_command(cmdptr) == GITS_CMD_MAPI )
+        intid = eventid;
+
+    if ( !write_itte(its, devid, eventid, collid, intid, &vcpu) )
+        return -1;
+
+    gicv3_assign_guest_event(its->d, its->doorbell_address, devid, eventid, vcpu, intid);

If you have a function returning an error, then you should check it and not ignoring it.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#107 lore raw

Re: [PATCH v2 20/27] ARM: vITS: handle MAPTI command

Hi,

Quoted 47 lines · Julien Grall

Hi Andre,

Quoted 44 lines · Andre Przywara

The MAPTI commands associates a DeviceID/EventID pair with a LPI/CPU pair and actually instantiates LPI interrupts.
We connect the already allocated host LPI to this virtual LPI, so that any triggering IRQ on the host can be quickly forwarded to a guest.

---
 xen/arch/arm/gic-v3-its.c        | 63
++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3-lpi.c        | 18 ++++++++++++
 xen/arch/arm/vgic-v3-its.c       | 27 +++++++++++++++--
 xen/include/asm-arm/gic_v3_its.h |  6 ++++
 4 files changed, 112 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +2 -0
index 5a2dbec..e2fcf50 100644
@@ -724,6 +724,69 @@ restart:
     spin_unlock(&d->arch.vgic.its_devices_lock);
 }
 
+/*
+ * Translates an event for a given guest device ID into the

associated host
+ * LPI number. This can be used to look up the mapped guest LPI.
+ */

xen/arch/arm/gic-v3-its.c +17 -0
+static uint32_t translate_event(struct domain *d, paddr_t doorbell,
+                                uint32_t devid, uint32_t eventid)
+{
+    struct rb_node *node;
+    struct its_devices *dev;
+    uint32_t host_lpi = 0;
+    int cmp;
+
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    node = d->arch.vgic.its_devices.rb_node;
+    while (node)
+    {
+        dev = rb_entry(node, struct its_devices, rbnode);
+        cmp = compare_its_guest_devices(dev, doorbell, devid);
+
+        if ( !cmp )
+        {
Quoted 8 lines

xen/arch/arm/gic-v3-its.c restart: · line 753

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+            if ( eventid >= dev->eventids )
+                goto out;
+
+            host_lpi = dev->host_lpis[eventid / LPI_BLOCK] +
+                                (eventid % LPI_BLOCK);
+            if ( !is_lpi(host_lpi) )

Hmmm, I don't understand this check. host_lpi should always be an LPI. No?

Looks like. Dropped in v4.

Quoted 23 lines
Quoted 23 lines
xen/arch/arm/gic-v3-its.c +18 -0
+                host_lpi = 0;
+            goto out;
+        }
+
+        if ( cmp > 0 )
+            node = node->rb_left;
+        else
+            node = node->rb_right;
+    }
+
+out:
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+
+    return host_lpi;
+}
+
+/*
+ * Connects the event ID for an already assigned device to the given

VCPU/vLPI
+ * pair. The corresponding physical LPI is already mapped on the host side
+ * (when assigning the physical device to the guest), so we just connect the

Quoted 8 lines

xen/arch/arm/gic-v3-its.c restart: · line 777

Quoted 5 lines

+ * target VCPU/vLPI pair to that interrupt to inject it properly if it fires.
+ */

xen/arch/arm/gic-v3-its.c +2 -0
+int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell_address,
+                             uint32_t devid, uint32_t eventid,

It looks like to me that devid is the virtual deviceID. If so, please prefix with 'v', otherwise 'p'.

Fixed in v4.

Quoted 26 lines
Quoted 26 lines
xen/arch/arm/gic-v3-its.c +3 -0
+                             struct vcpu *v, uint32_t virt_lpi)
+{
+    uint32_t host_lpi = translate_event(d, doorbell_address, devid,

eventid);

xen/arch/arm/gic-v3-its.c +5 -0
+
+    if ( !host_lpi )
+        return -ENOENT;
+
+    gicv3_lpi_update_host_entry(host_lpi, d->domain_id, v->vcpu_id,

virt_lpi);

xen/arch/arm/gic-v3-its.c +4 -0
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out

of it. */
void gicv3_its_dt_init(const struct dt_device_node *node) {

xen/arch/arm/gic-v3-lpi.c +0 -0
index 994698e..c110ec9 100644
@@ -153,6 +153,24 @@ void do_LPI(unsigned int lpi)
     vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
 }
 
Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c void do_LPI(unsigned int lpi) · line 161

Quoted 6 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
+int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
+                                unsigned int vcpu_id, uint32_t virt_lpi)
+{
+    union host_lpi *hlpip, hlpi;
+
+    host_lpi -= LPI_OFFSET;

I would add an ASSERT(host_lpi > LPI_OFFSET);

Fixed in v4.

Quoted 24 lines
Quoted 24 lines
xen/arch/arm/gic-v3-lpi.c +12 -0
+
+    hlpip = &lpi_data.host_lpis[host_lpi /
 HOST_LPIS_PER_PAGE][host_lpi % HOST_LPIS_PER_PAGE];
+
+    hlpi.virt_lpi = virt_lpi;
+    hlpi.dom_id = domain_id;
+    hlpi.vcpu_id = vcpu_id;
+
+    write_u64_atomic(&hlpip->data, hlpi.data);
+
+    return 0;
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/vgic-v3-its.c +0 -0
index c26d5d4..600ff69 100644
@@ -167,8 +167,8 @@ static bool read_itte(struct virt_its *its,
 uint32_t devid, uint32_t evid,
 }
 
Quoted 9 lines

xen/arch/arm/vgic-v3-its.c static bool read_itte(struct virt_its *its, · line 174

Quoted 7 lines
xen/arch/arm/vgic-v3-its.c +2 -2
 #define SKIP_LPI_UPDATE 1
-bool write_itte(struct virt_its *its, uint32_t devid, uint32_t evid,
-                uint32_t collid, uint32_t vlpi, struct vcpu **vcpu)
+static bool write_itte(struct virt_its *its, uint32_t devid, uint32_t
 evid,
+                       uint32_t collid, uint32_t vlpi, struct vcpu
 **vcpu)

Please explain in the commit message why you move to static.

Fixed in v4.

Quoted 9 lines
Quoted 9 lines
 {
     struct vits_itte *itte;
xen/arch/arm/vgic-v3-its.c +0 -0
@@ -332,6 +332,25 @@ static int its_handle_mapd(struct virt_its *its,
 uint64_t *cmdptr)
     return 0;
 }
 
Quoted 8 lines

xen/arch/arm/vgic-v3-its.c static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr) · line 340

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +6 -0
+static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    uint32_t intid = its_cmd_get_physical_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);

This should be unsigned.

Fixed in v4.

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +5 -0
+    struct vcpu *vcpu;
+
+    if ( its_cmd_get_command(cmdptr) == GITS_CMD_MAPI )
+        intid = eventid;
+
Quoted 8 lines

xen/arch/arm/vgic-v3-its.c static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+    if ( !write_itte(its, devid, eventid, collid, intid, &vcpu) )
+        return -1;
+
+    gicv3_assign_guest_event(its->d, its->doorbell_address, devid,
 eventid, vcpu, intid);

If you have a function returning an error, then you should check it and not ignoring it.

Fixed in v3.

Cheers,
Andre.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#108 lore raw

PATCH v2 21/27 ARM: vITS: handle MOVI command

In reply to Andre Przywara

The MOVI command moves the interrupt affinity from one redistributor (read: VCPU) to another.
For now migration of "live" LPIs is not yet implemented, but we store the changed affinity in the host LPI structure and in our virtual ITTE.

---
 xen/arch/arm/gic-v3-its.c        | 15 +++++++++++++++
 xen/arch/arm/gic-v3-lpi.c        | 13 +++++++++++++
 xen/arch/arm/vgic-v3-its.c       | 24 ++++++++++++++++++++++++
 xen/include/asm-arm/gic_v3_its.h |  4 ++++
 4 files changed, 56 insertions(+)
xen/arch/arm/gic-v3-its.c +15 -0
index e2fcf50..aa9b1b2 100644
@@ -787,6 +787,21 @@ int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell_address,
     return 0;
 }
 
+/* Changes the target VCPU for a given host LPI assigned to a domain. */
+int gicv3_lpi_change_vcpu(struct domain *d, paddr_t doorbell,
+                          uint32_t devid, uint32_t eventid,
+                          unsigned int vcpu_id)
+{
+    uint32_t host_lpi = translate_event(d, doorbell, devid, eventid);
+
+    if ( !host_lpi )
+        return -ENOENT;
+
+    gicv3_lpi_update_host_vcpuid(host_lpi, vcpu_id);
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
xen/arch/arm/gic-v3-lpi.c +13 -0
index c110ec9..8c7cea1 100644
@@ -171,6 +171,19 @@ int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
     return 0;
 }
 
+int gicv3_lpi_update_host_vcpuid(uint32_t host_lpi, unsigned int vcpu_id)
+{
+    union host_lpi *hlpip;
+
+    host_lpi -= LPI_OFFSET;
+
+    hlpip = &lpi_data.host_lpis[host_lpi / HOST_LPIS_PER_PAGE][host_lpi % HOST_LPIS_PER_PAGE];
+
+    write_u16_atomic(&hlpip->vcpu_id, vcpu_id);
+
+    return 0;
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/vgic-v3-its.c +24 -0
index 600ff69..bb573de 100644
@@ -351,6 +351,24 @@ static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_movi(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    if ( !write_itte(its, devid, eventid, collid, SKIP_LPI_UPDATE, &vcpu) )
+        return -1;
+
+    /* TODO: lookup currently-in-guest virtual IRQs and migrate them */
+
+    gicv3_lpi_change_vcpu(its->d,
+                          its->doorbell_address, devid, eventid, vcpu->vcpu_id);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -387,6 +405,12 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPTI:
             its_handle_mapti(its, cmdptr);
             break;
+        case GITS_CMD_MOVALL:
+            gdprintk(XENLOG_G_INFO, "ITS: ignoring MOVALL command\n");
+            break;
+        case GITS_CMD_MOVI:
+            its_handle_movi(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
xen/include/asm-arm/gic_v3_its.h +4 -0
index 1ea00e9..25b6b3c 100644
@@ -166,8 +166,12 @@ int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell,
                              uint32_t devid, uint32_t eventid,
                              struct vcpu *v, uint32_t virt_lpi);
+int gicv3_lpi_change_vcpu(struct domain *d, paddr_t doorbell,
+                          uint32_t devid, uint32_t eventid,
+                          unsigned int vcpu_id);
 int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
                                 unsigned int vcpu_id, uint32_t virt_lpi);
+int gicv3_lpi_update_host_vcpuid(uint32_t host_lpi, unsigned int vcpu_id);
 
 #else
 
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#109 lore raw

Re: [PATCH v2 21/27] ARM: vITS: handle MOVI command

Hi Andre,

Quoted 72 lines · Andre Przywara

The MOVI command moves the interrupt affinity from one redistributor (read: VCPU) to another.
For now migration of "live" LPIs is not yet implemented, but we store the changed affinity in the host LPI structure and in our virtual ITTE.

---
 xen/arch/arm/gic-v3-its.c        | 15 +++++++++++++++
 xen/arch/arm/gic-v3-lpi.c        | 13 +++++++++++++
 xen/arch/arm/vgic-v3-its.c       | 24 ++++++++++++++++++++++++
 xen/include/asm-arm/gic_v3_its.h |  4 ++++
 4 files changed, 56 insertions(+)
xen/arch/arm/gic-v3-its.c +15 -0
index e2fcf50..aa9b1b2 100644
@@ -787,6 +787,21 @@ int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell_address,
     return 0;
 }
 
+/* Changes the target VCPU for a given host LPI assigned to a domain. */
+int gicv3_lpi_change_vcpu(struct domain *d, paddr_t doorbell,
+                          uint32_t devid, uint32_t eventid,
+                          unsigned int vcpu_id)
+{
+    uint32_t host_lpi = translate_event(d, doorbell, devid, eventid);
+
+    if ( !host_lpi )
+        return -ENOENT;
+
+    gicv3_lpi_update_host_vcpuid(host_lpi, vcpu_id);
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
xen/arch/arm/gic-v3-lpi.c +13 -0
index c110ec9..8c7cea1 100644
@@ -171,6 +171,19 @@ int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
     return 0;
 }
 
+int gicv3_lpi_update_host_vcpuid(uint32_t host_lpi, unsigned int vcpu_id)
+{
+    union host_lpi *hlpip;
+
+    host_lpi -= LPI_OFFSET;
+
+    hlpip = &lpi_data.host_lpis[host_lpi / HOST_LPIS_PER_PAGE][host_lpi % HOST_LPIS_PER_PAGE];
+
+    write_u16_atomic(&hlpip->vcpu_id, vcpu_id);
+
+    return 0;
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/vgic-v3-its.c +1 -0
index 600ff69..bb573de 100644
@@ -351,6 +351,24 @@ static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_movi(struct virt_its *its, uint64_t *cmdptr)

xen/arch/arm/vgic-v3-its.c static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr) · line 364

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    if ( !write_itte(its, devid, eventid, collid, SKIP_LPI_UPDATE, &vcpu) )
+        return -1;
+
+    /* TODO: lookup currently-in-guest virtual IRQs and migrate them */

Looking at the code, you record the vcpuID which means that the next LPI will be direct to this vCPU. However, the previous LPI may still be inflight on a different vCPU.

How the guest will behave in this case?

Quoted 11 lines
xen/arch/arm/vgic-v3-its.c +7 -0
+
+    gicv3_lpi_change_vcpu(its->d,
+                          its->doorbell_address, devid, eventid, vcpu->vcpu_id);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 409

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +2 -0
@@ -387,6 +405,12 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPTI:
             its_handle_mapti(its, cmdptr);
             break;
+        case GITS_CMD_MOVALL:
+            gdprintk(XENLOG_G_INFO, "ITS: ignoring MOVALL command\n");

Why MOVALL is not implemented?

Quoted 15 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+            break;
+        case GITS_CMD_MOVI:
+            its_handle_movi(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
xen/include/asm-arm/gic_v3_its.h +0 -0
index 1ea00e9..25b6b3c 100644
@@ -166,8 +166,12 @@ int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 int gicv3_assign_guest_event(struct domain *d, paddr_t doorbell,
                              uint32_t devid, uint32_t eventid,

xen/include/asm-arm/gic_v3_its.h int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi); · line 177

Quoted 10 lines
xen/include/asm-arm/gic_v3_its.h +4 -0
                              struct vcpu *v, uint32_t virt_lpi);
+int gicv3_lpi_change_vcpu(struct domain *d, paddr_t doorbell,
+                          uint32_t devid, uint32_t eventid,
+                          unsigned int vcpu_id);
 int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id,
                                 unsigned int vcpu_id, uint32_t virt_lpi);
+int gicv3_lpi_update_host_vcpuid(uint32_t host_lpi, unsigned int vcpu_id);
 
 #else
 

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#110 lore raw

PATCH v2 22/27 ARM: vITS: handle DISCARD command

In reply to Andre Przywara

The DISCARD command drops the connection between a DeviceID/EventID and an LPI/collection pair.
We mark the respective structure entries as not allocated and make sure that any queued IRQs are removed.

---
 xen/arch/arm/vgic-v3-its.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
xen/arch/arm/vgic-v3-its.c +33 -0
index bb573de..21a663e 100644
@@ -369,6 +369,36 @@ static int its_handle_movi(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_discard(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct pending_irq *pirq;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    if ( !read_itte(its, devid, eventid, &vcpu, &vlpi) )
+        return -1;
+
+    pirq = lpi_to_pending(vcpu, vlpi, false);
+    if ( pirq )
+    {
+        clear_bit(GIC_IRQ_GUEST_QUEUED, &pirq->status);
+        gic_remove_from_queues(vcpu, vlpi);
+
+        /* Mark this pending IRQ struct as availabe again. */
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;
+    }
+
+    if ( !write_itte(its, devid, eventid, ~0, INVALID_LPI, NULL) )
+        return -1;
+
+    gicv3_assign_guest_event(its->d, its->doorbell_address, devid, eventid, NULL, 0);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -392,6 +422,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_CLEAR:
             its_handle_clear(its, cmdptr);
             break;
+        case GITS_CMD_DISCARD:
+            its_handle_discard(its, cmdptr);
+            break;
         case GITS_CMD_INT:
             its_handle_int(its, cmdptr);
             break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#111 lore raw

PATCH v2 23/27 ARM: vITS: handle INV command

In reply to Andre Przywara

The INV command instructs the ITS to update the configuration data for a given LPI by re-reading its entry from the property table. We don't need to care so much about the priority value, but enabling or disabling an LPI has some effect: We remove or push virtual LPIs to their VCPUs, also check the virtual pending bit if an LPI gets enabled.

---
 xen/arch/arm/vgic-v3-its.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
xen/arch/arm/vgic-v3-its.c +57 -0
index 21a663e..8af06ac 100644
@@ -265,6 +265,60 @@ static int its_handle_int(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+/*
+ * For a given virtual LPI read the enabled bit from the virtual property
+ * table and update the virtual IRQ's state.
+ * This takes care of removing or pushing of virtual LPIs to their VCPUs.
+ */
+static void update_lpi_enabled_status(struct virt_its* its,
+                                      struct vcpu *vcpu, uint32_t vlpi)
+{
+    struct pending_irq *pirq = lpi_to_pending(vcpu, vlpi, false);
+    uint8_t property = its->d->arch.vgic.proptable[vlpi - LPI_OFFSET];
+
+    if ( property & LPI_PROP_ENABLED )
+    {
+        if ( pirq )
+        {
+            unsigned long flags;
+
+            set_bit(GIC_IRQ_GUEST_ENABLED, &pirq->status);
+            spin_lock_irqsave(&vcpu->arch.vgic.lock, flags);
+            if ( !list_empty(&pirq->inflight) &&
+                 !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+                gic_raise_guest_irq(vcpu, vlpi, property & LPI_PROP_PRIO_MASK);
+            spin_unlock_irqrestore(&vcpu->arch.vgic.lock, flags);
+        }
+
+        /* Check whether the LPI has fired while the guest had it disabled. */
+        if ( test_and_clear_bit(vlpi - LPI_OFFSET, vcpu->arch.vgic.pendtable) )
+            vgic_vcpu_inject_irq(vcpu, vlpi);
+    }
+    else
+    {
+        if ( pirq )
+        {
+            clear_bit(GIC_IRQ_GUEST_ENABLED, &pirq->status);
+            gic_remove_from_queues(vcpu, vlpi);
+        }
+    }
+}
+
+static int its_handle_inv(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    if ( !read_itte(its, devid, eventid, &vcpu, &vlpi) )
+        return -1;
+
+    update_lpi_enabled_status(its, vcpu, vlpi);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -428,6 +482,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INT:
             its_handle_int(its, cmdptr);
             break;
+        case GITS_CMD_INV:
+            its_handle_inv(its, cmdptr);
+	    break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#112 lore raw

PATCH v2 24/27 ARM: vITS: handle INVALL command

In reply to Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). This is nasty to emulate exactly with our architecture, so we just scan the pending table and inject _every_ LPI found there that got enabled.

---
 xen/arch/arm/vgic-v3-its.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
xen/arch/arm/vgic-v3-its.c +37 -0
index 8af06ac..cc12c1c 100644
@@ -319,6 +319,40 @@ static int its_handle_inv(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+/*
+ * INVALL updates the per-LPI configuration status for every LPI mapped to
+ * a particular redistributor. Since our property table is referenced when
+ * needed, we don't need to sync anything, really. But we have to take care
+ * of LPIs getting enabled if there is an interrupt pending.
+ * To catch every LPI without iterating through the device table we just
+ * look for set bits in our virtual pending table and check the status of
+ * the enabled bit in the respective property table entry.
+ * This actually covers every (pending) LPI from every redistributor,
+ * but update_lpi_enabled_status() is a NOP for LPIs not being mapped
+ * to the redistributor/VCPU we are interested in.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+    int vlpi = 0;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    do {
+        vlpi = find_next_bit(vcpu->arch.vgic.pendtable,
+                             its->d->arch.vgic.nr_lpis, vlpi);
+        if ( vlpi >= its->d->arch.vgic.nr_lpis )
+            break;
+
+        update_lpi_enabled_status(its, vcpu, vlpi);
+    } while (1);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -485,6 +519,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INV:
             its_handle_inv(its, cmdptr);
 	    break;
+        case GITS_CMD_INVALL:
+            its_handle_invall(its, cmdptr);
+	    break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#113 lore raw

Re: [PATCH v2 24/27] ARM: vITS: handle INVALL command

Hi Andre,

Quoted 41 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). This is nasty to emulate exactly with our architecture, so we just scan the pending table and inject _every_ LPI found there that got enabled.

---
 xen/arch/arm/vgic-v3-its.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
xen/arch/arm/vgic-v3-its.c +23 -0
index 8af06ac..cc12c1c 100644
@@ -319,6 +319,40 @@ static int its_handle_inv(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+/*
+ * INVALL updates the per-LPI configuration status for every LPI mapped to
+ * a particular redistributor. Since our property table is referenced when
+ * needed, we don't need to sync anything, really. But we have to take care
+ * of LPIs getting enabled if there is an interrupt pending.
+ * To catch every LPI without iterating through the device table we just
+ * look for set bits in our virtual pending table and check the status of
+ * the enabled bit in the respective property table entry.
+ * This actually covers every (pending) LPI from every redistributor,
+ * but update_lpi_enabled_status() is a NOP for LPIs not being mapped
+ * to the redistributor/VCPU we are interested in.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+    int vlpi = 0;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    do {

xen/arch/arm/vgic-v3-its.c static int its_handle_inv(struct virt_its *its, uint64_t *cmdptr) · line 354

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+        vlpi = find_next_bit(vcpu->arch.vgic.pendtable,
+                             its->d->arch.vgic.nr_lpis, vlpi);
+        if ( vlpi >= its->d->arch.vgic.nr_lpis )
+            break;
+
+        update_lpi_enabled_status(its, vcpu, vlpi);
+    } while (1);
+
+    return 0;
+}

I still don't like the implementation of INVALL. You rely on the guest to behave well and not making sure that pendtable is full of 1 and the associate LPIs not enabled.

This could fairly be easy to achieve by sending INT command and modify the property table to have all IRQs enabled.

Quoted 6 lines
xen/arch/arm/vgic-v3-its.c +1 -0
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);

xen/arch/arm/vgic-v3-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 527

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +3 -0
@@ -485,6 +519,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INV:
             its_handle_inv(its, cmdptr);
 	    break;
+        case GITS_CMD_INVALL:
+            its_handle_invall(its, cmdptr);
+	    break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#114 lore raw

PATCH v2 25/27 ARM: vITS: create and initialize virtual ITSes for Dom0

In reply to Andre Przywara

For each hardware ITS create and initialize a virtual ITS for Dom0. We use the same memory mapped address to keep the doorbell working.

---
 xen/arch/arm/vgic-v3-its.c       | 29 +++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           | 12 ++++++++++++
 xen/include/asm-arm/domain.h     |  1 +
 xen/include/asm-arm/gic_v3_its.h | 12 ++++++++++++
 4 files changed, 54 insertions(+)
xen/arch/arm/vgic-v3-its.c +29 -0
index cc12c1c..caa7354 100644
@@ -914,6 +914,35 @@ static const struct mmio_handler_ops vgic_its_mmio_handler = {
     .write = vgic_v3_its_mmio_write,
 };
 
+int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr)
+{
+    struct virt_its *its;
+    uint64_t base_attr;
+
+    its = xzalloc(struct virt_its);
+    if ( ! its )
+        return -ENOMEM;
+
+    base_attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    base_attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    base_attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    its->cbaser  = base_attr;
+    base_attr |= 0ULL << GITS_BASER_PAGE_SIZE_SHIFT;
+    its->baser0  = GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT;
+    its->baser0 |= (7ULL << GITS_BASER_ENTRY_SIZE_SHIFT) | base_attr;
+    its->baser1  = GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT;
+    its->baser1 |= (1ULL << GITS_BASER_ENTRY_SIZE_SHIFT) | base_attr;
+    its->d = d;
+    its->doorbell_address = guest_addr + ITS_DOORBELL_OFFSET;
+    spin_lock_init(&its->vcmd_lock);
+    spin_lock_init(&its->its_lock);
+
+    register_mmio_handler(d, &vgic_its_mmio_handler, guest_addr, SZ_64K, its);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic-v3.c +12 -0
index d1382be..8faec95 100644
@@ -31,6 +31,7 @@
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
 #include <asm/vgic.h>
 #include <asm/vgic-emul.h>
 #include <asm/vreg.h>
@@ -1651,6 +1652,7 @@ static int vgic_v3_domain_init(struct domain *d)
      */
     if ( is_hardware_domain(d) )
     {
+        struct host_its *hw_its;
         unsigned int first_cpu = 0;
 
         d->arch.vgic.dbase = vgic_v3_hw.dbase;
@@ -1676,6 +1678,16 @@ static int vgic_v3_domain_init(struct domain *d)
 
             first_cpu += size / d->arch.vgic.rdist_stride;
         }
+        d->arch.vgic.nr_regions = vgic_v3_hw.nr_rdist_regions;
+
+        list_for_each_entry(hw_its, &host_its_list, entry)
+        {
+            /* Emulate the control registers frame (lower 64K). */
+            vgic_v3_its_init_virtual(d, hw_its->addr);
+
+            d->arch.vgic.has_its = true;
+        }
+
     }
     else
     {
xen/include/asm-arm/domain.h +1 -0
index 33c1851..27cc310 100644
@@ -115,6 +115,7 @@ struct arch_domain
         uint8_t *proptable;
         struct rb_root its_devices;         /* devices mapped to an ITS */
         spinlock_t its_devices_lock;        /* protects the its_devices tree */
+        bool has_its;
 #endif
     } vgic;
 
xen/include/asm-arm/gic_v3_its.h +12 -0
index 25b6b3c..55ef143 100644
@@ -148,6 +148,13 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
 int gicv3_its_setup_collection(unsigned int cpu);
 
 /*
+ * Create and register a virtual ITS at the given guest address.
+ * If a host ITS is specified, a hardware domain can reach out to that host
+ * ITS to deal with devices and LPI mappings and can enable/disable LPIs.
+ */
+int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr);
+
+/*
  * Map a device on the host by allocating an ITT on the host (ITS).
  * "nr_event" specifies how many events (interrupts) this device will need.
  * Setting "valid" to false deallocates the device.
@@ -211,6 +218,11 @@ static inline int gicv3_its_setup_collection(unsigned int cpu)
     return 0;
 }
 
+static inline int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr)
+{
+    return 0;
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#115 lore raw

Re: [PATCH v2 25/27] ARM: vITS: create and initialize virtual ITSes for Dom0

Hi Andre,

Quoted 16 lines · Andre Przywara

For each hardware ITS create and initialize a virtual ITS for Dom0. We use the same memory mapped address to keep the doorbell working.

---
 xen/arch/arm/vgic-v3-its.c       | 29 +++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           | 12 ++++++++++++
 xen/include/asm-arm/domain.h     |  1 +
 xen/include/asm-arm/gic_v3_its.h | 12 ++++++++++++
 4 files changed, 54 insertions(+)
xen/arch/arm/vgic-v3-its.c +0 -0
index cc12c1c..caa7354 100644

xen/arch/arm/vgic-v3-its.c static const struct mmio_handler_ops vgic_its_mmio_handler = { · line 917

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +1 -0
@@ -914,6 +914,35 @@ static const struct mmio_handler_ops vgic_its_mmio_handler = {
     .write = vgic_v3_its_mmio_write,
 };
 
+int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr)

*All* initialization function should have a counterpart in the same patch to free the memory.

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+{
+    struct virt_its *its;
+    uint64_t base_attr;
+

xen/arch/arm/vgic-v3-its.c static const struct mmio_handler_ops vgic_its_mmio_handler = { · line 931

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    its = xzalloc(struct virt_its);
+    if ( ! its )
+        return -ENOMEM;
+
+    base_attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    base_attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    base_attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    its->cbaser  = base_attr;
+    base_attr |= 0ULL << GITS_BASER_PAGE_SIZE_SHIFT;

Please explain the 0ULL.

xen/arch/arm/vgic-v3-its.c static const struct mmio_handler_ops vgic_its_mmio_handler = { · line 933

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+    its->baser0  = GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT;
+    its->baser0 |= (7ULL << GITS_BASER_ENTRY_SIZE_SHIFT) | base_attr;

Please explain 7ULL. I suspect you can use a sizeof of the device structure.

xen/arch/arm/vgic-v3-its.c static const struct mmio_handler_ops vgic_its_mmio_handler = { · line 935

Quoted 2 lines
xen/arch/arm/vgic-v3-its.c +2 -0
+    its->baser1  = GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT;
+    its->baser1 |= (1ULL << GITS_BASER_ENTRY_SIZE_SHIFT) | base_attr;

Please explain 1ULL. I suspect you can use a sizeof of the collection structure.

Quoted 34 lines
xen/arch/arm/vgic-v3-its.c +10 -0
+    its->d = d;
+    its->doorbell_address = guest_addr + ITS_DOORBELL_OFFSET;
+    spin_lock_init(&its->vcmd_lock);
+    spin_lock_init(&its->its_lock);
+
+    register_mmio_handler(d, &vgic_its_mmio_handler, guest_addr, SZ_64K, its);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic-v3.c +2 -0
index d1382be..8faec95 100644
@@ -31,6 +31,7 @@
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
 #include <asm/vgic.h>
 #include <asm/vgic-emul.h>
 #include <asm/vreg.h>
@@ -1651,6 +1652,7 @@ static int vgic_v3_domain_init(struct domain *d)
      */
     if ( is_hardware_domain(d) )
     {
+        struct host_its *hw_its;
         unsigned int first_cpu = 0;
 
         d->arch.vgic.dbase = vgic_v3_hw.dbase;

xen/arch/arm/vgic-v3.c static int vgic_v3_domain_init(struct domain *d) · line 1681

Quoted 4 lines
xen/arch/arm/vgic-v3.c +1 -0
@@ -1676,6 +1678,16 @@ static int vgic_v3_domain_init(struct domain *d)
 
             first_cpu += size / d->arch.vgic.rdist_stride;
         }
+        d->arch.vgic.nr_regions = vgic_v3_hw.nr_rdist_regions;

Why did you add that?

xen/arch/arm/vgic-v3.c static int vgic_v3_domain_init(struct domain *d) · line 1683

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+
+        list_for_each_entry(hw_its, &host_its_list, entry)

This could be done in vgic-v3-its.c. Also, I would prefer to see something similar to vgic_v3_setup_hw for ITS to make the code agnostic.

xen/arch/arm/vgic-v3.c static int vgic_v3_domain_init(struct domain *d) · line 1686

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+        {
+            /* Emulate the control registers frame (lower 64K). */
+            vgic_v3_its_init_virtual(d, hw_its->addr);

You likely need to pass more information to the vITS such as the number of deviceID, EventID...

Also, please check the return value.

Quoted 40 lines
xen/arch/arm/vgic-v3.c +4 -0
+
+            d->arch.vgic.has_its = true;
+        }
+
     }
     else
     {
xen/include/asm-arm/domain.h +1 -0
index 33c1851..27cc310 100644
@@ -115,6 +115,7 @@ struct arch_domain
         uint8_t *proptable;
         struct rb_root its_devices;         /* devices mapped to an ITS */
         spinlock_t its_devices_lock;        /* protects the its_devices tree */
+        bool has_its;
 #endif
     } vgic;
 
xen/include/asm-arm/gic_v3_its.h +7 -0
index 25b6b3c..55ef143 100644
@@ -148,6 +148,13 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
 int gicv3_its_setup_collection(unsigned int cpu);
 
 /*
+ * Create and register a virtual ITS at the given guest address.
+ * If a host ITS is specified, a hardware domain can reach out to that host
+ * ITS to deal with devices and LPI mappings and can enable/disable LPIs.
+ */
+int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr);
+
+/*
  * Map a device on the host by allocating an ITT on the host (ITS).
  * "nr_event" specifies how many events (interrupts) this device will need.
  * Setting "valid" to false deallocates the device.
@@ -211,6 +218,11 @@ static inline int gicv3_its_setup_collection(unsigned int cpu)
     return 0;

xen/include/asm-arm/gic_v3_its.h static inline int gicv3_its_setup_collection(unsigned int cpu) · line 228

Quoted 10 lines
xen/include/asm-arm/gic_v3_its.h +5 -0
 }
 
+static inline int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr)
+{
+    return 0;
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#116 lore raw

PATCH v2 26/27 ARM: vITS: create ITS subnodes for Dom0 DT

In reply to Andre Przywara

Dom0 expects all ITSes in the system to be propagated to be able to use MSIs.
Create Dom0 DT nodes for each hardware ITS, keeping the register frame address the same, as the doorbell address that the Dom0 drivers program into the BARs has to match the hardware.

---
 xen/arch/arm/gic-v3-its.c        | 78 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c            |  4 ++-
 xen/include/asm-arm/gic_v3_its.h | 13 +++++++
 3 files changed, 94 insertions(+), 1 deletion(-)
xen/arch/arm/gic-v3-its.c +78 -0
index aa9b1b2..b8fe5a3 100644
@@ -20,6 +20,7 @@
 
 #include <xen/lib.h>
 #include <xen/delay.h>
+#include <xen/libfdt/libfdt.h>
 #include <xen/mm.h>
 #include <xen/rbtree.h>
 #include <xen/sched.h>
@@ -802,6 +803,83 @@ int gicv3_lpi_change_vcpu(struct domain *d, paddr_t doorbell,
     return 0;
 }
 
+/*
+ * Create the respective guest DT nodes for a list of host ITSes.
+ * This copies the reg property, so the guest sees the ITS at the same address
+ * as the host.
+ * Giving NULL for the its_list will make it use the list of host ITSes.
+ */
+int gicv3_its_make_dt_nodes(struct list_head *its_list,
+                            const struct domain *d,
+                            const struct dt_device_node *gic,
+                            void *fdt)
+{
+    uint32_t len;
+    int res;
+    const void *prop = NULL;
+    const struct dt_device_node *its = NULL;
+    const struct host_its *its_data;
+
+    if ( !its_list )
+        its_list = &host_its_list;
+
+    if ( list_empty(its_list) )
+        return 0;
+
+    /* The sub-nodes require the ranges property */
+    prop = dt_get_property(gic, "ranges", &len);
+    if ( !prop )
+    {
+        printk(XENLOG_ERR "Can't find ranges property for the gic node\n");
+        return -FDT_ERR_XEN(ENOENT);
+    }
+
+    res = fdt_property(fdt, "ranges", prop, len);
+    if ( res )
+        return res;
+
+    list_for_each_entry(its_data, its_list, entry)
+    {
+        its = its_data->dt_node;
+
+        res = fdt_begin_node(fdt, its->name);
+        if ( res )
+            return res;
+
+        res = fdt_property_string(fdt, "compatible", "arm,gic-v3-its");
+        if ( res )
+            return res;
+
+        res = fdt_property(fdt, "msi-controller", NULL, 0);
+        if ( res )
+            return res;
+
+        if ( its->phandle )
+        {
+            res = fdt_property_cell(fdt, "phandle", its->phandle);
+            if ( res )
+                return res;
+        }
+
+        /* Use the same reg regions as the ITS node in host DTB. */
+        prop = dt_get_property(its, "reg", &len);
+        if ( !prop )
+        {
+            printk(XENLOG_ERR "GICv3: Can't find ITS reg property.\n");
+            res = -FDT_ERR_XEN(ENOENT);
+            return res;
+        }
+
+        res = fdt_property(fdt, "reg", prop, len);
+        if ( res )
+            return res;
+
+        fdt_end_node(fdt);
+    }
+
+    return res;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
xen/arch/arm/gic-v3.c +3 -1
index 83a839a..f9193a6 100644
@@ -1167,8 +1167,10 @@ static int gicv3_make_hwdom_dt_node(const struct domain *d,
 
     res = fdt_property(fdt, "reg", new_cells, len);
     xfree(new_cells);
+    if ( res )
+        return res;
 
-    return res;
+    return gicv3_its_make_dt_nodes(NULL, d, gic, fdt);
 }
 
 static const hw_irq_controller gicv3_host_irq_type = {
xen/include/asm-arm/gic_v3_its.h +13 -0
index 55ef143..67cd48d 100644
@@ -154,6 +154,12 @@ int gicv3_its_setup_collection(unsigned int cpu);
  */
 int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr);
 
+/* Given a list of ITSes, create the appropriate DT nodes for a domain. */
+int gicv3_its_make_dt_nodes(struct list_head *its_list,
+                            const struct domain *d,
+                            const struct dt_device_node *gic,
+                            void *fdt);
+
 /*
  * Map a device on the host by allocating an ITT on the host (ITS).
  * "nr_event" specifies how many events (interrupts) this device will need.
@@ -222,6 +228,13 @@ static inline int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr)
 {
     return 0;
 }
+static inline int gicv3_its_make_dt_nodes(struct list_head *its_list,
+                                       const struct domain *d,
+                                       const struct dt_device_node *gic,
+                                       void *fdt)
+{
+    return 0;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#117 lore raw

PATCH v2 27/27 ARM: vGIC: advertise LPI support

In reply to Andre Przywara

To let a guest know about the availability of virtual LPIs, set the respective bits in the virtual GIC registers and let a guest control the LPI enable bit.
Only report the LPI capability if the host has initialized at least one ITS.

---
 xen/arch/arm/vgic-v3.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 83 insertions(+), 5 deletions(-)
xen/arch/arm/vgic-v3.c +83 -5
index 8faec95..6d5b7f4 100644
@@ -169,8 +169,10 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* We have not implemented LPI's, read zero */
-        goto read_as_zero_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(!!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED),
+                                info);
+        return 1;
 
     case VREG32(GICR_IIDR):
         if ( dabt.size != DABT_WORD ) goto bad_width;
@@ -182,16 +184,19 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         uint64_t typer, aff;
 
         if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-        /* TBD: Update processor id in [23:8] when ITS support is added */
         aff = (MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 3) << 56 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 2) << 48 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 1) << 40 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 0) << 32);
         typer = aff;
+        typer |= (v->vcpu_id & 0xffff) << 8;
 
         if ( v->arch.vgic.flags & VGIC_V3_RDIST_LAST )
             typer |= GICR_TYPER_LAST;
 
+        if ( v->domain->arch.vgic.has_its )
+            typer |= GICR_TYPER_PLPIS;
+
         *r = vgic_reg64_extract(typer, info);
 
         return 1;
@@ -502,6 +507,40 @@ bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi)
     return false;
 }
 
+static void vgic_vcpu_enable_lpis(struct vcpu *v)
+{
+    uint64_t reg = v->domain->arch.vgic.rdist_propbase;
+    unsigned int nr_lpis = BIT((reg & 0x1f) + 1) - LPI_OFFSET;
+    int nr_pages;
+
+    /* The first VCPU to enable LPIs maps the property table. */
+    if ( !v->domain->arch.vgic.proptable )
+    {
+        v->domain->arch.vgic.nr_lpis = nr_lpis;
+        nr_pages = DIV_ROUND_UP(nr_lpis, PAGE_SIZE);
+
+        get_guest_pages(v->domain, reg & GENMASK(51, 12), nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(51, 12),
+                                                         nr_pages);
+        printk("VGIC-v3: VCPU%d mapped %d pages for property table\n",
+               v->vcpu_id, nr_pages);
+    }
+    nr_pages = DIV_ROUND_UP(((nr_lpis + LPI_OFFSET) / 8), PAGE_SIZE);
+    reg = v->arch.vgic.rdist_pendbase;
+
+    get_guest_pages(v->domain, reg & GENMASK(51, 12), nr_pages);
+    v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                             reg & GENMASK(51, 12), nr_pages);
+
+    printk("VGIC-v3: VCPU%d mapped %d pages for pending table\n",
+           v->vcpu_id, nr_pages);
+
+    v->arch.vgic.flags |= VGIC_V3_LPIS_ENABLED;
+
+    printk("VGICv3: enabled %d LPIs for VCPU%d\n", nr_lpis, v->vcpu_id);
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
@@ -512,8 +551,18 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* LPI's not implemented */
-        goto write_ignore_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        if ( !v->domain->arch.vgic.has_its )
+            return 1;
+
+        /* LPIs can only be enabled once, but never disabled again. */
+        if ( !(r & GICR_CTLR_ENABLE_LPIS) ||
+             (v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED) )
+            return 1;
+
+        vgic_vcpu_enable_lpis(v);
+
+        return 1;
 
     case VREG32(GICR_IIDR):
         /* RO */
@@ -1113,6 +1162,11 @@ static int vgic_v3_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         typer = ((ncpus - 1) << GICD_TYPE_CPUS_SHIFT |
                  DIV_ROUND_UP(v->domain->arch.vgic.nr_spis, 32));
 
+        if ( v->domain->arch.vgic.has_its )
+        {
+            typer |= GICD_TYPE_LPIS;
+            irq_bits = 16;
+        }
         typer |= (irq_bits - 1) << GICD_TYPE_ID_BITS_SHIFT;
 
         *r = vgic_reg32_extract(typer, info);
@@ -1729,6 +1783,30 @@ static int vgic_v3_domain_init(struct domain *d)
 
 static void vgic_v3_domain_free(struct domain *d)
 {
+    int nr_pages;
+    struct vcpu *v;
+
+    if ( d->arch.vgic.proptable )
+    {
+        nr_pages = DIV_ROUND_UP(d->arch.vgic.nr_lpis, PAGE_SIZE);
+
+        unmap_guest_pages(d->arch.vgic.proptable, nr_pages);
+        put_guest_pages(d, d->arch.vgic.rdist_propbase & GENMASK(51, 12),
+                        nr_pages);
+        printk("VGICv3: freeing PROPBASE for Domain%d\n", d->domain_id);
+    }
+
+    nr_pages = DIV_ROUND_UP((d->arch.vgic.nr_lpis + LPI_OFFSET) / 8, PAGE_SIZE);
+    for_each_vcpu(d, v)
+    {
+        if ( !v->arch.vgic.pendtable )
+            continue;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, nr_pages);
+        put_guest_pages(d, v->arch.vgic.rdist_pendbase & GENMASK(51, 12),
+                        nr_pages);
+        printk("VGICv3: freeing PENDBASE for VCPU%d\n", v->vcpu_id);
+    }
     xfree(d->arch.vgic.rdist_regions);
 }
 
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#118 lore raw

Re: [PATCH v2 27/27] ARM: vGIC: advertise LPI support

Hi Andre,

Quoted 16 lines · Andre Przywara

To let a guest know about the availability of virtual LPIs, set the respective bits in the virtual GIC registers and let a guest control the LPI enable bit.
Only report the LPI capability if the host has initialized at least one ITS.

---
 xen/arch/arm/vgic-v3.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 83 insertions(+), 5 deletions(-)
xen/arch/arm/vgic-v3.c +0 -0
index 8faec95..6d5b7f4 100644

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info, · line 174

Quoted 8 lines
xen/arch/arm/vgic-v3.c +3 -2
@@ -169,8 +169,10 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* We have not implemented LPI's, read zero */
-        goto read_as_zero_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(!!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED),
+                                info);

I would be more readable to have:

uint32_t ctlr;

ctlr = !!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED); *r = vgic_reg32_extract(ctlr, info);

Quoted 5 lines
xen/arch/arm/vgic-v3.c +1 -0
+        return 1;
 
     case VREG32(GICR_IIDR):
         if ( dabt.size != DABT_WORD ) goto bad_width;

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info, · line 192

Quoted 10 lines
xen/arch/arm/vgic-v3.c +1 -1
@@ -182,16 +184,19 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         uint64_t typer, aff;
 
         if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-        /* TBD: Update processor id in [23:8] when ITS support is added */
         aff = (MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 3) << 56 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 2) << 48 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 1) << 40 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 0) << 32);
         typer = aff;
+        typer |= (v->vcpu_id & 0xffff) << 8;

Please explain in the commit message why this change. At first glance, this should be a separate patch.

Quoted 20 lines
xen/arch/arm/vgic-v3.c +9 -0
         if ( v->arch.vgic.flags & VGIC_V3_RDIST_LAST )
             typer |= GICR_TYPER_LAST;
 
+        if ( v->domain->arch.vgic.has_its )
+            typer |= GICR_TYPER_PLPIS;
+
         *r = vgic_reg64_extract(typer, info);
 
         return 1;
@@ -502,6 +507,40 @@ bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi)
     return false;
 }
 
+static void vgic_vcpu_enable_lpis(struct vcpu *v)
+{
+    uint64_t reg = v->domain->arch.vgic.rdist_propbase;
+    unsigned int nr_lpis = BIT((reg & 0x1f) + 1) - LPI_OFFSET;
+    int nr_pages;
+

xen/arch/arm/vgic-v3.c bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi) · line 525

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    /* The first VCPU to enable LPIs maps the property table. */
+    if ( !v->domain->arch.vgic.proptable )
+    {
+        v->domain->arch.vgic.nr_lpis = nr_lpis;
+        nr_pages = DIV_ROUND_UP(nr_lpis, PAGE_SIZE);
+
+        get_guest_pages(v->domain, reg & GENMASK(51, 12), nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(51, 12),
+                                                         nr_pages);

What is map_guest_pages fail?

xen/arch/arm/vgic-v3.c bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi) · line 527

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+        printk("VGIC-v3: VCPU%d mapped %d pages for property table\n",
+               v->vcpu_id, nr_pages);

Please don't use printk in function called by a guest, as a guest could potentially DOS xen.

I think this should be gdprintk(XENLOG_DEBUG,...)

xen/arch/arm/vgic-v3.c bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi) · line 537

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    }
+    nr_pages = DIV_ROUND_UP(((nr_lpis + LPI_OFFSET) / 8), PAGE_SIZE);
+    reg = v->arch.vgic.rdist_pendbase;
+
+    get_guest_pages(v->domain, reg & GENMASK(51, 12), nr_pages);
+    v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                             reg & GENMASK(51, 12), nr_pages);
+
+    printk("VGIC-v3: VCPU%d mapped %d pages for pending table\n",
+           v->vcpu_id, nr_pages);

Ditto.

xen/arch/arm/vgic-v3.c bool vgic_can_inject_lpi(struct vcpu *vcpu, uint32_t vlpi) · line 541

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+
+    v->arch.vgic.flags |= VGIC_V3_LPIS_ENABLED;
+
+    printk("VGICv3: enabled %d LPIs for VCPU%d\n", nr_lpis, v->vcpu_id);

Ditto.

Quoted 8 lines
xen/arch/arm/vgic-v3.c +2 -0
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
@@ -512,8 +551,18 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 560

Quoted 10 lines
xen/arch/arm/vgic-v3.c +7 -2
     case VREG32(GICR_CTLR):
-        /* LPI's not implemented */
-        goto write_ignore_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        if ( !v->domain->arch.vgic.has_its )
+            return 1;
+
+        /* LPIs can only be enabled once, but never disabled again. */
+        if ( !(r & GICR_CTLR_ENABLE_LPIS) ||
+             (v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED) )

This is fragile. A re-distributor can be updated from any vCPU. So you would end up to call vgic_vcpu_enable_lpis twice. You like want to use a lock protecting the GICR_CTLR emulation.

Quoted 9 lines
xen/arch/arm/vgic-v3.c +5 -0
+            return 1;
+
+        vgic_vcpu_enable_lpis(v);
+
+        return 1;
 
     case VREG32(GICR_IIDR):
         /* RO */

xen/arch/arm/vgic-v3.c static int vgic_v3_distr_mmio_read(struct vcpu *v, mmio_info_t *info, · line 1168

Quoted 7 lines
xen/arch/arm/vgic-v3.c +4 -0
@@ -1113,6 +1162,11 @@ static int vgic_v3_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         typer = ((ncpus - 1) << GICD_TYPE_CPUS_SHIFT |
                  DIV_ROUND_UP(v->domain->arch.vgic.nr_spis, 32));
 
+        if ( v->domain->arch.vgic.has_its )
+        {
+            typer |= GICD_TYPE_LPIS;
+            irq_bits = 16;

Why 16?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel