Skip to contentSkip to conversation

arm64: Dom0 ITS emulation

PATCH In review Andre Przywara 7 participants 106 messages Jan 30, 2017 – Mar 23, 2017 Page 2 of 2

lore
Share & tools
lore
#89 lore raw

Re: [PATCH 17/28] ARM: vITS: handle CLEAR command

In reply to Andre Przywara
Quoted 5 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.

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

Quoted 67 lines
---
 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 982c51d..48eb924 100644
@@ -129,8 +129,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;
@@ -214,6 +214,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,
@@ -234,6 +262,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 · 1 line
2.9.0

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

#90 lore raw

PATCH 18/28 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 48eb924..9307235 100644
@@ -242,6 +242,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,
@@ -265,6 +285,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
#91 lore raw

PATCH 19/28 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 9307235..e6523a3 100644
@@ -262,6 +262,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,
@@ -288,6 +315,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
#92 lore raw

PATCH 20/28 ARM: vITS: handle MAPD command

In reply to Andre Przywara

The MAPD command maps a device by associating a memory region for storing ITTEs with a certain device ID.
We just store the given guest physical address in the device table. We don't map the device tables permanently, as their alignment requirement is only 256 Bytes, thus making mapping of several tables complicated. We map the device tables on demand when we need them later.

---
 xen/arch/arm/vgic-v3-its.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
xen/arch/arm/vgic-v3-its.c +24 -0
index e6523a3..5be40d8 100644
@@ -289,6 +289,27 @@ 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);
+
+    if ( !its->dev_table )
+        return -1;
+
+    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,
@@ -318,6 +339,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
#93 lore raw

Re: [PATCH 20/28] ARM: vITS: handle MAPD command

Quoted 17 lines · Andre Przywara

The MAPD command maps a device by associating a memory region for storing ITTEs with a certain device ID.
We just store the given guest physical address in the device table. We don't map the device tables permanently, as their alignment requirement is only 256 Bytes, thus making mapping of several tables complicated. We map the device tables on demand when we need them later.

---
 xen/arch/arm/vgic-v3-its.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
xen/arch/arm/vgic-v3-its.c +0 -0
index e6523a3..5be40d8 100644

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

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +6 -0
@@ -289,6 +289,27 @@ 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);

please see alpine.DEB.2.10.1611081649530.3491@sstabellini-ThinkPad-X260

Quoted 30 lines
xen/arch/arm/vgic-v3-its.c +17 -0
+    if ( !its->dev_table )
+        return -1;
+
+    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,
@@ -318,6 +339,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 · 1 line
2.9.0

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

#94 lore raw

PATCH 21/28 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        | 58 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3-lpi.c        | 18 +++++++++++++
 xen/arch/arm/vgic-v3-its.c       | 27 +++++++++++++++++--
 xen/include/asm-arm/gic_v3_its.h |  5 ++++
 4 files changed, 106 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +58 -0
index 2a7093f..1268d64 100644
@@ -632,6 +632,64 @@ int gicv3_its_unmap_device(struct domain *d, int guest_devid)
     return -ENOENT;
 }
 
+/*
+ * 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,
+                                uint32_t devid, uint32_t eventid)
+{
+    struct rb_node *node;
+    struct its_devices *dev;
+    uint32_t host_lpi = 0;
+
+    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);
+        if ( dev->guest_devid == devid )
+        {
+            if ( eventid >= dev->eventids )
+                goto out;
+
+            host_lpi = dev->host_lpis[eventid / 32] + (eventid % 32);
+            if ( !is_lpi(host_lpi) )
+                host_lpi = 0;
+            goto out;
+        }
+
+        if ( devid < dev->guest_devid )
+            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, uint32_t devid, uint32_t eventid,
+                             struct vcpu *v, uint32_t virt_lpi)
+{
+    uint32_t host_lpi = translate_event(d, 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 ade8b69..494ae22 100644
@@ -135,6 +135,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, 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;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg;
xen/arch/arm/vgic-v3-its.c +25 -2
index 5be40d8..3e74f46 100644
@@ -164,8 +164,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;
 
@@ -310,6 +310,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, 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,
@@ -342,6 +361,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 +5 -0
index 0e6b06a..160ece3 100644
@@ -159,6 +159,11 @@ 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,
+                             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, int vcpu_id,
+                                uint32_t virt_lpi);
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
Signature · 7 lines
2.9.0


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

PATCH 22/28 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        | 14 ++++++++++++++
 xen/arch/arm/gic-v3-lpi.c        | 13 +++++++++++++
 xen/arch/arm/vgic-v3-its.c       | 23 +++++++++++++++++++++++
 xen/include/asm-arm/gic_v3_its.h |  4 ++++
 4 files changed, 54 insertions(+)
xen/arch/arm/gic-v3-its.c +14 -0
index 1268d64..d37b7b6 100644
@@ -690,6 +690,20 @@ int gicv3_assign_guest_event(struct domain *d, uint32_t devid, uint32_t eventid,
     return 0;
 }
 
+/* Changes the target VCPU for a given host LPI assigned to a domain. */
+int gicv3_lpi_change_vcpu(struct domain *d,
+                          uint32_t devid, uint32_t eventid, int vcpu_id)
+{
+    uint32_t host_lpi = translate_event(d, 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 494ae22..66d6eff 100644
@@ -153,6 +153,19 @@ int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id, int vcpu_id,
     return 0;
 }
 
+int gicv3_lpi_update_host_vcpuid(uint32_t host_lpi, 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;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg;
xen/arch/arm/vgic-v3-its.c +23 -0
index 3e74f46..fb9caf0 100644
@@ -329,6 +329,23 @@ 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, 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,
@@ -365,6 +382,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 160ece3..bac0fe5 100644
@@ -162,8 +162,12 @@ int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 int gicv3_assign_guest_event(struct domain *d,
                              uint32_t devid, uint32_t eventid,
                              struct vcpu *v, uint32_t virt_lpi);
+int gicv3_lpi_change_vcpu(struct domain *d,
+                          uint32_t devid, uint32_t eventid, int vcpu_id);
 int gicv3_lpi_update_host_entry(uint32_t host_lpi, int domain_id, int vcpu_id,
                                 uint32_t virt_lpi);
+int gicv3_lpi_update_host_vcpuid(uint32_t host_lpi, int vcpu_id);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
Signature · 7 lines
2.9.0


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

PATCH 23/28 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 fb9caf0..8747890 100644
@@ -346,6 +346,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, 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,
@@ -369,6 +399,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
#97 lore raw

PATCH 24/28 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 8747890..82f7bcc 100644
@@ -262,6 +262,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);
@@ -405,6 +459,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
#98 lore raw

PATCH 25/28 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 82f7bcc..75d1e12 100644
@@ -316,6 +316,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);
@@ -462,6 +496,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
#99 lore raw

PATCH 26/28 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       | 28 ++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           | 12 ++++++++++++
 xen/include/asm-arm/domain.h     |  1 +
 xen/include/asm-arm/gic_v3_its.h | 10 ++++++++++
 4 files changed, 51 insertions(+)
xen/arch/arm/vgic-v3-its.c +28 -0
index 75d1e12..6404ebd 100644
@@ -891,6 +891,34 @@ 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;
+    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 +10 -0
index bac0fe5..e75249b 100644
@@ -146,6 +146,12 @@ uint64_t gicv3_get_redist_address(int cpu, bool use_pta);
 /* Map a collection for this host CPU to each host ITS. */
 int gicv3_its_setup_collection(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).
  * "bits" specifies how many events (interrupts) this device will need.
  * Setting "valid" to false deallocates the device.
@@ -202,6 +208,10 @@ static inline int gicv3_its_map_guest_device(struct domain *d, int host_devid,
 {
     return -ENODEV;
 }
+static inline int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr)
+{
+    return 0;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
Signature · 7 lines
2.9.0


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

PATCH 27/28 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        | 73 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c            |  4 ++-
 xen/include/asm-arm/gic_v3_its.h | 13 +++++++
 3 files changed, 89 insertions(+), 1 deletion(-)
xen/arch/arm/gic-v3-its.c +73 -0
index d37b7b6..36839c9 100644
@@ -704,6 +704,79 @@ int gicv3_lpi_change_vcpu(struct domain *d,
     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.
+ */
+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 ( 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 23cf33d..7041d48 100644
@@ -1190,8 +1190,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(&host_its_list, d, gic, fdt);
 }
 
 static const hw_irq_controller gicv3_host_irq_type = {
xen/include/asm-arm/gic_v3_its.h +13 -0
index e75249b..4f5a453 100644
@@ -152,6 +152,12 @@ int gicv3_its_setup_collection(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).
  * "bits" specifies how many events (interrupts) this device will need.
  * Setting "valid" to false deallocates the device.
@@ -212,6 +218,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
#101 lore raw

PATCH 28/28 ARM: vGIC: advertising 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
#102 lore raw
In reply to Andre Przywara

Hi Andre,

I tried your patch series on HW. Dom0 boots but no LPIs are coming to Dom0.
So I made below patch to consider segment ID in generating devid,
I see below panic from _xmalloc().

Complete log is here
http://pastebin.com/btythn2V

xen/arch/arm/physdev.c +7 -0
index 6e02de4..72ffe9f 100644
@@ -17,6 +17,7 @@
 int do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     struct physdev_manage_pci manage;
+   struct physdev_pci_device_add pci_add;
     u32 devid;
     int ret;
 
@@ -33,6 +34,19 @@ int do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
                                              cmd == PHYSDEVOP_manage_pci_add);
 
             return ret;
+       case PHYSDEVOP_pci_device_add:
+            if ( copy_from_guest(&pci_add, arg, 1) != 0 )
+                return -EFAULT;
+            devid = pci_add.seg << 16 | pci_add.bus << 8 | pci_add.devfn;
+
+            printk("In %s calling gicv3_its_map_device for S: %d B:

%d F:%d DEVID %u\n",
+ __func__, pci_add.seg,pci_add.bus, pci_add.devfn, devid);
+ /* Allocate an ITS device table with space for 32 MSIs */
+ ret = gicv3_its_map_guest_device(hardware_domain, devid, devid, 5,
+ cmd == PHYSDEVOP_pci_device_add);
+

+            return ret;
     }
[    3.280979] iommu: Adding device 0000:01:07.2 to group 26
[    3.286373] pci 0000:01:07.3: [177d:a02f] type 00 class 0x058000
[    3.292463] pci 0000:01:07.3: BAR 0: [mem
0x87e05b000000-0x87e05b7fffff 64bit] (from Enhanced Allocation,
properties 0x0)
[    3.303457] pci 0000:01:07.3: BAR 4: [mem

0x87e05bf00000-0x87e05bffffff 64bit] (from Enhanced Allocation, properties 0x0)
(XEN) In do_physdev_op calling gicv3_its_map_device for S: 0 B: 1 F:59 DEVID 315 (XEN) Hypervisor Trap. HSR=0x96000044 EC=0x25 IL=1 Syndrome=0x44 (XEN) CPU0: Unexpected Trap: Hypervisor
(XEN) ----[ Xen-4.9-unstable arm64 debug=y Not tainted ]----

(XEN) CPU:    0
(XEN) PC:     0000000000235e3c xmem_pool_alloc+0x33c/0x49c
(XEN) LR:     0000000000235c28
(XEN) SP:     0000801ffaba7c10
(XEN) CPSR:   20000349 MODE:64-bit EL2h (Hypervisor, handler)
(XEN)      X0: 000000000000001a  X1: 8022bf008022be00  X2: 0000801ffff165d0
(XEN)      X3: 000000000a0e0a0e  X4: 0000000000000001  X5: 0000000000000000
(XEN)      X6: 0000000000000004  X7: 0000000000000003  X8: 00000000fffffffb
(XEN)      X9: 000000000000000a X10: 0000801ffaba7ad8 X11: 0000000000000033
(XEN)     X12: 0000000000000003 X13: 0000000000268df8 X14: 0000000000000020
(XEN)     X15: 0000000000000000 X16: 0000000000000021 X17: 000000000000000b
(XEN)     X18: 000000000000000d X19: 0000801ffff16000 X20: 0000000000000005
(XEN)     X21: 0000000000000150 X22: 0000801ffff17868 X23: 000000000000003a
(XEN)     X24: 00000000ffffffff X25: 000000000000001f X26: 0000000000000039
(XEN)     X27: 0000000000000000 X28: 0000801fff8e9160  FP: 0000801ffaba7c10

(XEN)
(XEN) VTCR_EL2: 80053590
(XEN) VTTBR_EL2: 0001001ffabac000
(XEN)
(XEN) SCTLR_EL2: 30cd183d
(XEN) HCR_EL2: 000000008038663f
(XEN) TTBR0_EL2: 0000001fffefe000
(XEN)
(XEN) ESR_EL2: 96000044
(XEN) HPFAR_EL2: 0000008010000000
(XEN) FAR_EL2: 8022bf008022be10
(XEN)
(XEN) Xen stack trace from sp=0000801ffaba7c10:

(XEN)    0000801ffaba7c70 0000000000236394 0000000000000100 0000000000000060
(XEN)    0000000000000150 0000801ff5a62530 0000000000000000 0000801ff5a62528
(XEN)    0000801ffa32f0e0 0000000000000005 000000000000013b ffff8000ea2c6800
(XEN)    0000801ffaba7cc0 000000000024c154 0000801fff8ebec0 000000000000013b
(XEN)    0000801ff5a62000 0000801ff5a62530 0000000000000000 0000801ff5a62528
(XEN)    0000801ffa32f0e0 0000801ffaba7d70 0000801ffaba7d70 0000000000253590
(XEN)    000000000000013b 0000801ffaba7f30 ffff8000000ba1b8 ffff8000000ba1b8
(XEN)    0000000060000045 ffff800000e40000 ffff800000f50000 0000000000000000
(XEN)    ffff800000e30ec8 ffff8000ea2c6800 0000801ffaba7d30 00000000ffffffc8
(XEN)    0000000060000045 000000000026be48 0000000000000000 0000000000000001
(XEN)    000000000000003b 000000000000013b 0000801ffaba7da4 ffff8012f21bb450
(XEN)    0000801ffaba7db0 0000000000254de4 0000801ffaba7eb0 00000000002549f4
(XEN)    000000005a000ea1 000000013b010000 0000000000000000 ffff8000000ba1b8
(XEN)    0000801ffaba7e10 00000000002572a4 000000005a000ea1 0000801ffaba7eb0
(XEN)    000000005a000ea1 0000000000256550 0000801ffaba7e70 0000000000248e34
(XEN)    0000000000313c80 ffff800000d96000 ffffffffffffffff ffff800000525484
(XEN)    ffff8012f3aeb780 000000000025fb54 ffff8012f21cf098 ffff8012f21cf000
(XEN)    ffffffffffffffff ffff8000000ba1b8 0000000060000045 ffff800000e40000
(XEN)    ffff800000f50000 0000000000000000 ffff800000e30ec8 ffff8000ea2c6800
(XEN)    0000801ffaba7e90 000000000025827c 0000000000000002 0000000000258298
(XEN)    ffff8012f3aeafd0 000000000025fb58 0000000000000002 ffff800000d96000
(XEN)    0000000000000019 ffff8012f3aeb7c0 0000000000000007 0000000000000000
(XEN)    0000000000000001 ffff800000e34330 0000000080808080 ffff8012f21bb450
(XEN)    7f7f7f7f7f7f7f7f 5e646c68736d7471 7f7f7f7f7f7f7f7f 0101010101010101
(XEN)    0000000000000020 6962343620666666 6d6f726628205d74 0000000000000000
(XEN)    0000000000000021 000000000000000b 000000000000000d ffff8012f21cf098
(XEN)    ffff8012f21cf000 ffff800000d90000 0000000000000000 ffff8012f21cf098
(XEN)    ffff800000e40000 ffff800000f50000 0000000000000000 ffff800000e30ec8
(XEN)    ffff8000ea2c6800 ffff8012f3aeb780 ffff800000599718 ffffffffffffffff
(XEN)    ffff8000000ba1b8 0000000060000045 0000000060000045 0000000000000000
(XEN)    0000000000000000 0000000000000000 ffff8012f3aeb780 ffff80000010b068
(XEN)    0000000000000000 0000000000000000
(XEN) Xen call trace:
(XEN)    [<0000000000235e3c>] xmem_pool_alloc+0x33c/0x49c (PC)
(XEN)    [<0000000000235c28>] xmem_pool_alloc+0x128/0x49c (LR)
(XEN)    [<0000000000236394>] _xmalloc+0xfc/0x274
(XEN)    [<000000000024c154>] gicv3_its_map_guest_device+0xb0/0x2a0
(XEN)    [<0000000000253590>] do_physdev_op+0xc4/0x114
(XEN)    [<0000000000254de4>] traps.c#do_trap_hypercall+0x90/0x12c
(XEN)    [<00000000002572a4>] do_trap_hypervisor+0xd88/0x1c6c
(XEN)    [<000000000025fb54>] entry.o#guest_sync+0x90/0xc0

(XEN)

Note: I have added print similar to below that you see in log
(XEN) In do_physdev_op calling gicv3_its_map_device for S: 0 B: 1 F:59 DEVID 315

Quoted 184 lines · Andre Przywara

Hi,

after the two RFC versions now the first "serious" attempt for emulating an ARM GICv3 ITS interrupt controller, for Dom0 only at the moment. The ITS is an interrupt controller widget providing a sophisticated way of dealing with MSIs in a scalable manner.
For hardware which relies on the ITS to provide interrupts for its peripherals this code is needed to get a machine booted into Dom0 at all. ITS emulation for DomUs is only really useful with PCI passthrough, which is not yet available for ARM. It is expected that this feature will be co-developed with the ITS DomU code. However this code drop here considered DomU emulation already, to keep later architectural changes to a minimum.

Some generic design principles:

* The current GIC code statically allocates structures for each supported IRQ (both for the host and the guest), which due to the potentially millions of LPI interrupts is not feasible to copy for the ITS. So we refrain from introducing the ITS as a first class Xen interrupt controller, also we don't hold struct irq_desc's or struct pending_irq's for each possible LPI.
Fortunately LPIs are only interesting to guests, so we get away with storing only the virtual IRQ number and the guest VCPU for each allocated host LPI, which can be stashed into one uint64_t. This data is stored in a two-level table, which is both memory efficient and quick to access. We hook into the existing IRQ handling and VGIC code to avoid accessing the normal structures, providing alternative methods for getting the needed information (priority, is enabled?) for LPIs.
For interrupts which are queued to or are actually in a guest we allocate struct pending_irq's on demand. As it is expected that only a very small number of interrupts is ever on a VCPU at the same time, this seems like the best approach. For now allocated structs are re-used and held in a linked list. Should it emerge that traversing a linked list is a performance issue, this can be changed to use a hash table.

* On the guest side we (later will) have to deal with malicious guests trying to hog Xen with mapping requests for a lot of LPIs, for instance. As the ITS actually uses system memory for storing status information, we use this memory (which the guest has to provide) to naturally limit a guest. For those tables which are page sized (devices, collections (CPUs), LPI properties) we map those pages into Xen, so we can easily access them from the virtual GIC code.
Unfortunately the actual interrupt mapping tables are not necessarily page aligned, also can be much smaller than a page, so mapping all of them permanently is fiddly. As ITS commands in need to iterate those tables are pretty rare after all, we for now map them on demand upon emulating a virtual ITS command. This is acceptable because "mapping" them is actually very cheap on arm64. Also as we can't properly protect those areas due to their sub-page-size property, we validate the data in there before actually using it. The vITS code basically just stores the data in there which the guest has actually transferred via the virtual ITS command queue before, so there is no secret revealed nor does it create an attack vector for a malicious guest.

* An obvious approach to handling some guest ITS commands would be to propagate them to the host, for instance to map devices and LPIs and to enable or disable LPIs.
However this (later with DomU support) will create an attack vector, as a malicious guest could try to fill the host command queue with propagated commands.
So (in contrast to the first RFC post) we completely avoid this situation. For mapping devices and LPIs we rely on this being done via a hypercall prior to the actual guest run. For enabling and disabling LPIs we keep this bit on the virtual side and let LPIs always be enabled on the host side, dealing with the consequences this approach creates.

As it is expected that the ITS support will become a tech preview in the first release, there is a Kconfig option to enable it. Also it is supported on arm64 only, which will most likely not change in the future. This leads to some hideous constructs like an #ifdef'ed header file with empty function stubs, I have some hope we can still clean this up. Also some parameters are config options which can be overridden on the Xen commandline. This is to support experimentation and adaption to various platforms, ideally we find either one-size-fits-all values or find another way of getting rid of this.

Compared to the previous post (RFC-v2) this has seen a lot of reworks and cleanups in various areas.
I tried to address all of the review comments, though some are hard to follow due to rewrites. So apologies if some points have slipped through. Allocating and mapping of memory for both the physical and virtual ITS and redistributor tables has been improved, though I didn't manage to write protect the virtual tables from a guest without impacting access from Xen at the same time. I will need to take a deeper look into this, but ideally it's only a small change in get_guest_pages().

This code boots Dom0 on an ARM Fast Model with ITS support. I tried to address the issues seen by people running the previous version on real hardware, though couldn't verify this here for myself.
So any testing, bug reports (and possibly even fixes) are very welcome.

The code can also be found on the its/v1 branch here:
git://linux-arm.org/xen-ap.git
http://www.linux-arm.org/git?p=xen-ap.git;a=shortlog;h=refs/heads/its/v1

Cheers,
Andre

(Rough) changelog RFC-v2 .. v1:
- split host ITS driver into gic-v3-lpi.c and gic-v3-its.c part
- rename virtual ITS driver file to vgic-v3-its.c
- use macros and named constants for all magic numbers
- use atomic accessors for accessing the host LPI data
- remove leftovers from connecting virtual and host ITSes
- bail out if host ITS is disabled in the DT
- rework map/unmap_guest_pages():
- split off p2m part as get/put_guest_pages (to be done on allocation)
- get rid of vmap, using map_domain_page() instead
- delay allocation of virtual tables until actual LPI/ITS enablement
- properly size both virtual and physical tables upon allocation
- fix put_domain() locking issues in physdev_op and LPI handling code
- add and extend comments in various areas
- fix lotsa coding style and white space issues, including comment style
- add locking to data structures not yet covered
- fix various locking issues
- use an rbtree to deal with ITS devices (instead of a list)
- properly handle memory attributes for ITS tables
- handle cacheable/non-cacheable ITS table mappings
- sanitize guest provided ITS/LPI table attributes
- fix breakage on non-GICv2 compatible host GICv3 controllers
- add command line parameters on top of Kconfig options
- properly wait for an ITS to become quiescient before enabling it
- handle host ITS command queue errors
- actually wait for host ITS command completion (READR==WRITER)
- fix ARM32 compilation
- various patch splits and reorderings

Andre Przywara (28):
ARM: export __flush_dcache_area()
ARM: GICv3 ITS: parse and store ITS subnodes from hardware DT
ARM: GICv3: allocate LPI pending and property table
ARM: GICv3 ITS: allocate device and collection table
ARM: GICv3 ITS: map ITS command buffer
ARM: GICv3 ITS: introduce ITS command handling
ARM: GICv3 ITS: introduce device mapping
ARM: GICv3 ITS: introduce host LPI array
ARM: GICv3 ITS: map device and LPIs to the ITS on physdev_op hypercall
ARM: GICv3: introduce separate pending_irq structs for LPIs
ARM: GICv3: forward pending LPIs to guests
ARM: GICv3: enable ITS and LPIs on the host
ARM: vGICv3: handle virtual LPI pending and property tables
ARM: vGICv3: Handle disabled LPIs
ARM: vGICv3: introduce basic ITS emulation bits
ARM: vITS: introduce translation table walks
ARM: vITS: handle CLEAR command
ARM: vITS: handle INT command
ARM: vITS: handle MAPC command
ARM: vITS: handle MAPD command
ARM: vITS: handle MAPTI command
ARM: vITS: handle MOVI command
ARM: vITS: handle DISCARD command
ARM: vITS: handle INV command
ARM: vITS: handle INVALL command
ARM: vITS: create and initialize virtual ITSes for Dom0
ARM: vITS: create ITS subnodes for Dom0 DT
ARM: vGIC: advertising LPI support

 xen/arch/arm/Kconfig              |  33 ++
 xen/arch/arm/Makefile             |   3 +
 xen/arch/arm/efi/efi-boot.h       |   1 -
 xen/arch/arm/gic-v3-its.c         | 825 +++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3-lpi.c         | 414 +++++++++++++++++
 xen/arch/arm/gic-v3.c             |  98 +++-
 xen/arch/arm/gic.c                |   9 +-
 xen/arch/arm/physdev.c            |  21 +
 xen/arch/arm/vgic-v3-its.c        | 929 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            | 347 ++++++++++++--
 xen/arch/arm/vgic.c               |  68 ++-
 xen/include/asm-arm/atomic.h      |   6 +-
 xen/include/asm-arm/bitops.h      |   1 +
 xen/include/asm-arm/cache.h       |   4 +
 xen/include/asm-arm/domain.h      |  14 +-
 xen/include/asm-arm/gic.h         |   7 +
 xen/include/asm-arm/gic_v3_defs.h |  73 ++-
 xen/include/asm-arm/gic_v3_its.h  | 241 ++++++++++
 xen/include/asm-arm/irq.h         |   8 +
 xen/include/asm-arm/vgic.h        |  34 ++
 20 files changed, 3089 insertions(+), 47 deletions(-)
 create mode 100644 xen/arch/arm/gic-v3-its.c
 create mode 100644 xen/arch/arm/gic-v3-lpi.c
 create mode 100644 xen/arch/arm/vgic-v3-its.c
 create mode 100644 xen/include/asm-arm/gic_v3_its.h
Quoted 3 lines

--
2.9.0

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

#103 lore raw
Quoted 29 lines · Vijay Kilari

Hi Andre,

I tried your patch series on HW. Dom0 boots but no LPIs are coming to Dom0.
So I made below patch to consider segment ID in generating devid,
I see below panic from _xmalloc().

Complete log is here
http://pastebin.com/btythn2V

xen/arch/arm/physdev.c +5 -0
index 6e02de4..72ffe9f 100644
@@ -17,6 +17,7 @@
 int do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
     struct physdev_manage_pci manage;
+   struct physdev_pci_device_add pci_add;
     u32 devid;
     int ret;
 
@@ -33,6 +34,19 @@ int do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
                                              cmd == PHYSDEVOP_manage_pci_add);
 
             return ret;
+       case PHYSDEVOP_pci_device_add:
+            if ( copy_from_guest(&pci_add, arg, 1) != 0 )
+                return -EFAULT;
+            devid = pci_add.seg << 16 | pci_add.bus << 8 | pci_add.devfn;

xen/arch/arm/physdev.c int do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 50

Quoted 10 lines
xen/arch/arm/physdev.c +8 -0
+
+            printk("In %s calling gicv3_its_map_device for S: %d B:
 %d F:%d DEVID %u\n",
+                    __func__, pci_add.seg,pci_add.bus, pci_add.devfn, devid);
+            /* Allocate an ITS device table with space for 32 MSIs */
+            ret = gicv3_its_map_guest_device(hardware_domain, devid, devid, 5,
+                                       cmd == PHYSDEVOP_pci_device_add);
+
+            return ret;
     }

Hi Vijay, thanks for testing the series. Instead of implementing PHYSDEVOP_pci_device_add here, could you call gicv3_its_map_guest_device for each device statically from a Cavium specific platform file under xen/arch/arm/platforms?

Once we'll have a clearer idea about how to implement which hypercalls, we'll do this properly.

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

#104 lore raw
In reply to Vijay Kilari

Hi Vijay,

Quoted 3 lines · Vijay Kilari

I tried your patch series on HW. Dom0 boots but no LPIs are coming to Dom0.
So I made below patch to consider segment ID in generating devid,
I see below panic from _xmalloc().

I found the root cause of this bug. The size of the ITT entry is not read correctly from GITS_TYPER. Can you try the below patch?

xen/arch/arm/gic-v3-its.c +1 -1
index 36839c919d..46519648e8 100644
@@ -363,7 +363,7 @@ int gicv3_its_init(struct host_its *hw_its)
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     if ( reg & GITS_TYPER_PTA )
         hw_its->flags |= HOST_ITS_USES_PTA;
-    hw_its->itte_size = GITS_TYPER_ITT_SIZE(reg);
+    hw_its->itte_size = GITS_TYPER_ITT_SIZE(reg) + 1;
 
     for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
     {

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#105 lore raw
In reply to Andre Przywara
Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 4 lines

Compared to the previous post (RFC-v2) this has seen a lot of reworks and cleanups in various areas.
I tried to address all of the review comments, though some are hard to follow due to rewrites. So apologies if some points have slipped through.

A lot of the comments were not in the code reworked. It is not that difficult to check whether a comment still apply or not before sending a series and can save a lot of bandwidth review.

For the next version please go through all the comments from the first 2 versions and see whether they need to be addressed.

Cheers,

Signature · 6 lines
Julien Grall

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