Skip to contentSkip to conversation

arm64: Dom0 ITS emulation

RFC PATCH Superseded Andre Przywara 6 participants 144 messages Sep 28, 2016 – Mar 29, 2017 Page 1 of 2

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

cover arm64: Dom0 ITS emulation

Hi,

apologies for sending this series, which due to its early status is now targeting Xen 4.9, still before the 4.8 hard feature freeze, but there seems to be some interested parties and I wanted to get the discussion started on this.

This series introduces ARM GICv3 ITS emulation, for now restricted to Dom0 only. The ITS is an interrupt controller widget providing a sophisticated way to deal 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.

This implementation is totally independent from earlier submissons and tries to provide a new approach:

* 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.

* 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.

* At least for the Dom0 use case we need to pass some virtual ITS commands on to the hardware, namely device and interrupt mapping requests. As we have to deal with command emulation synchronously, this could lead to situations where the host command queue gets congested and CPUs stall on this. For now we assume that Dom0 (as being non-malicious) is not affected by this, so we allow this.
DomUs will later use PCI passthrough, so we can do the device and also IRQ mapping upon domain creation time, obviating ITS command passthrough for DomUs during their runtime at all.

This series is an early draft, with some known and many unknown issues. I made ITS support a Kconfig option, also it is only supported on arm64. This leads to some hideous constructs like an #ifdef'ed header file with empty function stubs, but I guess we can clean this up later in the upstreaming process.
Also I am not sure the host ITS and LPI initialization code is correct, especially when it comes to the subtle differences between ITS and LPIs enabling and initialization. Affinity handling is only rudimentarily implemented at this point. Also locking some of the shared data structures isn't fully implemented yet, partly because Xen's lack of mutexes, RCUs and preemption requiring some more clever solutions than my Linux experiences would easily provide offhand.

So for now I am mostly interested in feedback on the general architecture approach. Please comment on anything that looks suspicious or not sustainable.
Oh, and apologies in advance for any tabs and missing "spaces in if-statements" that slipped through my (unfortunately only manual) QA process.

For now this code happens to boot Dom0 on an ARM fast model with ITS support. I haven't had the chance to get hold of a Xen supported hardware platform with an ITS yet, so I expect some surprises when this code sees real hardware for the first time ;-)
That being said any testing and feedback is warmly welcomed!

The code can also be found on the its/rfc 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/rfc

Cheers,
Andre

Andre Przywara (24):
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 host LPI array
ARM: GICv3 ITS: introduce device mapping
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: introduce basic ITS emulation bits
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              |  11 +
 xen/arch/arm/Makefile             |   2 +
 xen/arch/arm/efi/efi-boot.h       |   1 -
 xen/arch/arm/gic-its.c            | 861 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             |  78 +++-
 xen/arch/arm/gic.c                |   9 +-
 xen/arch/arm/vgic-its.c           | 861 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            | 240 +++++++++--
 xen/arch/arm/vgic.c               |  60 ++-
 xen/include/asm-arm/cache.h       |   4 +
 xen/include/asm-arm/domain.h      |   9 +-
 xen/include/asm-arm/gic-its.h     | 246 +++++++++++
 xen/include/asm-arm/gic_v3_defs.h |  67 ++-
 xen/include/asm-arm/irq.h         |   8 +
 xen/include/asm-arm/vgic.h        |  12 +
 15 files changed, 2432 insertions(+), 37 deletions(-)
 create mode 100644 xen/arch/arm/gic-its.c
 create mode 100644 xen/arch/arm/vgic-its.c
 create mode 100644 xen/include/asm-arm/gic-its.h
Signature · 7 lines
2.9.0


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

RFC PATCH 1/24 ARM: GICv3 ITS: parse and store ITS subnodes from hardware DT

Parse the DT GIC subnodes to find every ITS MSI controller the hardware offers. Store that information in a list to both propagate all of them later to Dom0, but also to be able to iterate over all ITSes. This introduces an ITS Kconfig option.

---
 xen/arch/arm/Kconfig          |  5 ++++
 xen/arch/arm/Makefile         |  1 +
 xen/arch/arm/gic-its.c        | 67 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  6 ++++
 xen/include/asm-arm/gic-its.h | 57 ++++++++++++++++++++++++++++++++++++
 5 files changed, 136 insertions(+)
 create mode 100644 xen/arch/arm/gic-its.c
 create mode 100644 xen/include/asm-arm/gic-its.h
xen/arch/arm/Kconfig +5 -0
index 797c91f..9fe3b8e 100644
@@ -45,6 +45,11 @@ config ACPI
 config HAS_GICV3
 	bool
 
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on ARM_64
+        depends on HAS_GICV3
+
 config ALTERNATIVE
 	bool
 
xen/arch/arm/Makefile +1 -0
index 64fdf41..c2c4daa 100644
@@ -18,6 +18,7 @@ obj-$(EARLY_PRINTK) += early_printk.o
 obj-y += gic.o
 obj-y += gic-v2.o
 obj-$(CONFIG_HAS_GICV3) += gic-v3.o
+obj-$(CONFIG_HAS_ITS) += gic-its.o
 obj-y += guestcopy.o
 obj-y += hvm.o
 obj-y += io.o
xen/arch/arm/gic-its.c new file +67 -0

67 lines not on this page · raw message

xen/arch/arm/gic-v3.c +6 -0
index b8be395..238da84 100644
@@ -43,9 +43,12 @@
 #include <asm/device.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 #include <asm/cpufeature.h>
 #include <asm/acpi.h>
 
+LIST_HEAD(host_its_list);
+
 /* Global state */
 static struct {
     void __iomem *map_dbase;  /* Mapped address of distributor registers */
@@ -1229,6 +1232,9 @@ static void __init gicv3_dt_init(void)
 
     dt_device_get_address(node, 1 + gicv3.rdist_count + 2,
                           &vbase, &vsize);
+
+    /* Check for ITS child nodes and build the host ITS list accordingly. */
+    gicv3_its_dt_init(node);
 }
 
 static int gicv3_iomem_deny_access(const struct domain *d)
xen/include/asm-arm/gic-its.h new file +57 -0

57 lines not on this page · raw message

Signature · 7 lines
2.9.0


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

RFC PATCH 2/24 ARM: GICv3: allocate LPI pending and property table

In reply to Andre Przywara

The ARM GICv3 ITS provides a new kind of interrupt called LPIs. The pending bits and the configuration data (priority, enable bits) for those LPIs are stored in tables in normal memory, which software has to provide to the hardware.
Allocate the required memory, initialize it and hand it over to each ITS. We limit the number of LPIs we use with a compile time constant to avoid wasting memory.

---
 xen/arch/arm/Kconfig              |  6 ++++
 xen/arch/arm/efi/efi-boot.h       |  1 -
 xen/arch/arm/gic-its.c            | 76 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             | 27 ++++++++++++++
 xen/include/asm-arm/cache.h       |  4 +++
 xen/include/asm-arm/gic-its.h     | 22 +++++++++++-
 xen/include/asm-arm/gic_v3_defs.h | 48 ++++++++++++++++++++++++-
 7 files changed, 181 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +6 -0
index 9fe3b8e..66e2bb8 100644
@@ -50,6 +50,12 @@ config HAS_ITS
         depends on ARM_64
         depends on HAS_GICV3
 
+config HOST_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+
 config ALTERNATIVE
 	bool
 
xen/arch/arm/efi/efi-boot.h +0 -1
index 045d6ce..dc64aec 100644
@@ -10,7 +10,6 @@
 #include "efi-dom0.h"
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
-void __flush_dcache_area(const void *vaddr, unsigned long size);
 
 #define DEVICE_TREE_GUID \
 {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}
xen/arch/arm/gic-its.c +76 -0

86 lines not on this page · raw message

xen/arch/arm/gic-v3.c +27 -0
index 238da84..2534aa5 100644
@@ -546,6 +546,9 @@ static void __init gicv3_dist_init(void)
     type = readl_relaxed(GICD + GICD_TYPER);
     nr_lines = 32 * ((type & GICD_TYPE_LINES) + 1);
 
+    if ( type & GICD_TYPE_LPIS )
+        gicv3_lpi_init_host_lpis(((type >> GICD_TYPE_ID_BITS_SHIFT) & 0x1f) + 1);
+
     printk("GICv3: %d lines, (IID %8.8x).\n",
            nr_lines, readl_relaxed(GICD + GICD_IIDR));
 
@@ -615,6 +618,26 @@ static int gicv3_enable_redist(void)
 
     return 0;
 }
+static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+
+    if ( list_empty(&host_its_list) )
+        return;
+
+    /* Make sure LPIs are disabled before setting up the BASERs. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    writel_relaxed(reg & ~GICR_CTLR_ENABLE_LPIS, rdist_base + GICR_CTLR);
+
+    table_reg = gicv3_lpi_allocate_pendtable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);
+
+    table_reg = gicv3_lpi_get_proptable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);
+}
 
 static int __init gicv3_populate_rdist(void)
 {
@@ -658,6 +681,10 @@ static int __init gicv3_populate_rdist(void)
             if ( (typer >> 32) == aff )
             {
                 this_cpu(rbase) = ptr;
+
+                if ( typer & GICR_TYPER_PLPIS )
+                    gicv3_rdist_init_lpis(ptr);
+
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
                 return 0;
xen/include/asm-arm/cache.h +4 -0
index 2de6564..af96eee 100644
@@ -7,6 +7,10 @@
 #define L1_CACHE_SHIFT  (CONFIG_ARM_L1_CACHE_SHIFT)
 #define L1_CACHE_BYTES  (1 << L1_CACHE_SHIFT)
 
+#ifndef __ASSEMBLY__
+void __flush_dcache_area(const void *vaddr, unsigned long size);
+#endif
+
 #define __read_mostly __section(".data.read_mostly")
 
 #endif
xen/include/asm-arm/gic-its.h +21 -1
index 2f5c51c..48c6c78 100644
@@ -36,12 +36,32 @@ extern struct list_head host_its_list;
 /* Parse the host DT and pick up all host ITSes. */
 void gicv3_its_dt_init(const struct dt_device_node *node);
 
+/* Allocate and initialize tables for each host redistributor.
+ * Returns the respective {PROP,PEND}BASER register value.
+ */
+uint64_t gicv3_lpi_get_proptable(void);
+uint64_t gicv3_lpi_allocate_pendtable(void);
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(int nr_lpis);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
 {
 }
-
+static inline uint64_t gicv3_lpi_get_proptable(void)
+{
+    return 0;
+}
+static inline uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    return 0;
+}
+static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
xen/include/asm-arm/gic_v3_defs.h +47 -1

66 lines not on this page · raw message

Signature · 7 lines
2.9.0


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

RFC PATCH 3/24 ARM: GICv3 ITS: allocate device and collection table

In reply to Andre Przywara

Each ITS maps a pair of a DeviceID (usually the PCI b/d/f triplet) and an EventID (the MSI payload or interrupt ID) to a pair of LPI number and collection ID, which points to the target CPU.
This mapping is stored in the device and collection tables, which software has to provide for the ITS to use.
Allocate the required memory and hand it the ITS.
We limit the number of devices to cover 4 PCI busses for now.

---
 xen/arch/arm/gic-its.c        | 114 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   5 ++
 xen/include/asm-arm/gic-its.h |  49 +++++++++++++++++-
 3 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +114 -0
index b52dff3..40238a2 100644
@@ -21,6 +21,7 @@
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
@@ -38,6 +39,119 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define BASER_ATTR_MASK                                           \
+        ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
+         (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
+         (0x7UL << GITS_BASER_INNER_CACHEABILITY_SHIFT))
+#define BASER_RO_MASK   (GENMASK(52, 48) | GENMASK(58, 56))
+
+static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
+{
+    uint64_t ret;
+
+    if ( page_bits < 16)
+        return (uint64_t)addr & GENMASK(47, page_bits);
+
+    ret = addr & GENMASK(47, 16);
+    return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
+}
+
+static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
+{
+    uint64_t attr;
+    int entry_size = (regc >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f;
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */
+    for (pagesz = 0; pagesz < 3; pagesz++)
+    {
+        uint64_t reg;
+        int nr_bytes;
+
+        nr_bytes = ROUNDUP(nr_items * entry_size, BIT(pagesz * 2 + 12));
+        order = get_order_from_bytes(nr_bytes);
+
+        if ( !buffer )
+            buffer = alloc_xenheap_pages(order, 0);
+        if ( !buffer )
+            return -ENOMEM;
+
+        reg  = attr;
+        reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+        reg |= nr_bytes >> (pagesz * 2 + 12);
+        reg |= regc & BASER_RO_MASK;
+        reg |= GITS_BASER_VALID;
+        reg |= encode_phys_addr(virt_to_maddr(buffer), pagesz * 2 + 12);
+
+        writeq_relaxed(reg, basereg);
+        regc = readl_relaxed(basereg);
+
+        /* The host didn't like our attributes, just use what it returned. */
+        if ( (regc & BASER_ATTR_MASK) != attr )
+            attr = regc & BASER_ATTR_MASK;
+
+        /* If the host accepted our page size, we are done. */
+        if ( (reg & (3UL << GITS_BASER_PAGE_SIZE_SHIFT)) == pagesz )
+            return 0;
+
+        /* Check whether our buffer is aligned to the next page size already. */
+        if ( !(virt_to_maddr(buffer) & (BIT(pagesz * 2 + 12 + 2) - 1)) )
+        {
+            free_xenheap_pages(buffer, order);
+            buffer = NULL;
+        }
+    }
+
+    if ( buffer )
+        free_xenheap_pages(buffer, order);
+
+    return -EINVAL;
+}
+
+int gicv3_its_init(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    for (i = 0; i < 8; i++)
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
+        int type;
+
+        reg = readq_relaxed(basereg);
+        type = (reg >> 56) & 0x7;
+        switch ( type )
+        {
+        case GITS_BASER_TYPE_NONE:
+            continue;
+        case GITS_BASER_TYPE_DEVICE:
+            /* TODO: find some better way of limiting the number of devices */
+            gicv3_map_baser(basereg, reg, 1024);
+            break;
+        case GITS_BASER_TYPE_COLLECTION:
+            gicv3_map_baser(basereg, reg, NR_CPUS);
+            break;
+        default:
+            continue;
+        }
+    }
+
+    return 0;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
xen/arch/arm/gic-v3.c +5 -0
index 2534aa5..5cf4618 100644
@@ -29,6 +29,7 @@
 #include <xen/irq.h>
 #include <xen/iocap.h>
 #include <xen/sched.h>
+#include <xen/err.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
@@ -1548,6 +1549,7 @@ static int __init gicv3_init(void)
 {
     int res, i;
     uint32_t reg;
+    struct host_its *hw_its;
 
     if ( !cpu_has_gicv3 )
     {
@@ -1603,6 +1605,9 @@ static int __init gicv3_init(void)
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 
+    list_for_each_entry(hw_its, &host_its_list, entry)
+        gicv3_its_init(hw_its);
+
     spin_unlock(&gicv3.lock);
 
     return res;
xen/include/asm-arm/gic-its.h +48 -1

74 lines not on this page · raw message

Signature · 7 lines
2.9.0


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

RFC PATCH 5/24 ARM: GICv3 ITS: introduce ITS command handling

In reply to Andre Przywara

To be able to easily send commands to the ITS, create the respective wrapper functions, which take care of the ring buffer.
The first two commands we implement provide methods to map a collection to a redistributor (aka host core) and to flush the command queue (SYNC). Start using these commands for mapping one collection to each host CPU.

---
 xen/arch/arm/gic-its.c        | 101 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  17 +++++++
 xen/include/asm-arm/gic-its.h |  32 +++++++++++++
 3 files changed, 150 insertions(+)
xen/arch/arm/gic-its.c +101 -0
index c8a7a7e..88397bc 100644
@@ -33,6 +33,10 @@ static struct {
     int host_lpi_bits;
 } lpi_data;
 
+/* Physical redistributor address */
+static DEFINE_PER_CPU(uint64_t, rdist_addr);
+/* Redistributor ID */
+static DEFINE_PER_CPU(uint64_t, rdist_id);
 /* Pending table for each redistributor */
 static DEFINE_PER_CPU(void *, pending_table);
 
@@ -40,6 +44,86 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define ITS_COMMAND_SIZE        32
+
+static int its_send_command(struct host_its *hw_its, void *its_cmd)
+{
+    int readp, writep;
+
+    spin_lock(&hw_its->cmd_lock);
+
+    readp = readl_relaxed(hw_its->its_base + GITS_CREADR) & GENMASK(19, 5);
+    writep = readl_relaxed(hw_its->its_base + GITS_CWRITER) & GENMASK(19, 5);
+
+    if ( ((writep + ITS_COMMAND_SIZE) % PAGE_SIZE) == readp )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        return -EBUSY;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_COMMAND_SIZE);
+    __flush_dcache_area(hw_its->cmd_buf + writep, ITS_COMMAND_SIZE);
+    writep = (writep + ITS_COMMAND_SIZE) % PAGE_SIZE;
+
+    writeq_relaxed(writep & GENMASK(19, 5), hw_its->its_base + GITS_CWRITER);
+
+    spin_unlock(&hw_its->cmd_lock);
+
+    return 0;
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, int cpu, uint64_t reg)
+{
+    reg &= ~GENMASK(51, 16);
+
+    if ( hw_its->pta )
+        reg |= per_cpu(rdist_addr, cpu) & GENMASK(51, 16);
+    else
+        reg |= per_cpu(rdist_id, cpu) << 16;
+
+    return reg;
+}
+
+static int its_send_cmd_sync(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_SYNC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, 0x0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, (collection_id & GENMASK(15, 0)) | BIT(63));
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+void gicv3_its_setup_collection(int cpu)
+{
+    struct host_its *its;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        /* Only send commands to ITS that have been initialized already. */
+        if ( !its->cmd_buf )
+            continue;
+
+        its_send_cmd_mapc(its, cpu, cpu);
+        its_send_cmd_sync(its, cpu);
+    }
+}
+
 #define BASER_ATTR_MASK                                           \
         ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
          (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
@@ -147,6 +231,13 @@ int gicv3_its_init(struct host_its *hw_its)
     if ( !hw_its->its_base )
         return -ENOMEM;
 
+    /* Make sure the ITS is disabled before programming the BASE registers. */
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg & ~GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
+    reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
+    hw_its->pta = reg & GITS_TYPER_PTA;
+
     for (i = 0; i < 8; i++)
     {
         void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
@@ -174,9 +265,18 @@ int gicv3_its_init(struct host_its *hw_its)
     if ( IS_ERR(hw_its->cmd_buf) )
         return PTR_ERR(hw_its->cmd_buf);
 
+    its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
+    its_send_cmd_sync(hw_its, smp_processor_id());
+
     return 0;
 }
 
+void gicv3_set_redist_addr(paddr_t address, int redist_id)
+{
+    this_cpu(rdist_addr) = address;
+    this_cpu(rdist_id) = redist_id;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
@@ -265,6 +365,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->addr = addr;
         its_data->size = size;
         its_data->dt_node = its;
+        spin_lock_init(&its_data->cmd_lock);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/arch/arm/gic-v3.c +17 -0
index 5cf4618..b9387a3 100644
@@ -638,6 +638,8 @@ static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
     table_reg = gicv3_lpi_get_proptable();
     if ( table_reg )
         writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);
+
+    gicv3_its_setup_collection(smp_processor_id());
 }
 
 static int __init gicv3_populate_rdist(void)
@@ -684,7 +686,22 @@ static int __init gicv3_populate_rdist(void)
                 this_cpu(rbase) = ptr;
 
                 if ( typer & GICR_TYPER_PLPIS )
+                {
+                    paddr_t rdist_addr;
+
+                    rdist_addr = gicv3.rdist_regions[i].base;
+                    rdist_addr += ptr - gicv3.rdist_regions[i].map_base;
+
+                    /* The ITS refers to redistributors either by their physical
+                     * address or by their ID. Determine those two values and
+                     * let the ITS code store them in per host CPU variables to
+                     * later be able to address those redistributors.
+                     */
+                    gicv3_set_redist_addr(rdist_addr,
+                                          (typer >> 8) & GENMASK(15, 0));
+
                     gicv3_rdist_init_lpis(ptr);
+                }
 
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
xen/include/asm-arm/gic-its.h +32 -0
index b2a003f..b49d274 100644
@@ -37,6 +37,7 @@
 
 /* Register bits */
 #define GITS_CTLR_ENABLE     0x1
+#define GITS_TYPER_PTA       BIT(19)
 #define GITS_IIDR_VALUE      0x34c
 
 #define GITS_BASER_VALID                BIT(63)
@@ -59,6 +60,22 @@
                                         (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
                                         GITS_BASER_INDIRECT)
 
+/* ITS command definitions */
+#define ITS_CMD_SIZE                    32
+
+#define GITS_CMD_MOVI                   0x01
+#define GITS_CMD_INT                    0x03
+#define GITS_CMD_CLEAR                  0x04
+#define GITS_CMD_SYNC                   0x05
+#define GITS_CMD_MAPD                   0x08
+#define GITS_CMD_MAPC                   0x09
+#define GITS_CMD_MAPTI                  0x0a
+#define GITS_CMD_MAPI                   0x0b
+#define GITS_CMD_INV                    0x0c
+#define GITS_CMD_INVALL                 0x0d
+#define GITS_CMD_MOVALL                 0x0e
+#define GITS_CMD_DISCARD                0x0f
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
@@ -69,7 +86,9 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;
     void *cmd_buf;
+    bool pta;
 };
 
 extern struct list_head host_its_list;
@@ -89,6 +108,12 @@ uint64_t gicv3_lpi_allocate_pendtable(void);
 int gicv3_lpi_init_host_lpis(int nr_lpis);
 int gicv3_its_init(struct host_its *hw_its);
 
+/* Set the physical address and ID for each redistributor as read from DT. */
+void gicv3_set_redist_addr(paddr_t address, int redist_id);
+
+/* Map a collection for this host CPU to each host ITS. */
+void gicv3_its_setup_collection(int cpu);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -110,6 +135,13 @@ static inline int gicv3_its_init(struct host_its *hw_its)
 {
     return 0;
 }
+static inline void gicv3_set_redist_addr(paddr_t address, int redist_id)
+{
+}
+static inline void gicv3_its_setup_collection(int cpu)
+{
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
Signature · 7 lines
2.9.0


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

RFC PATCH 6/24 ARM: GICv3 ITS: introduce host LPI array

In reply to Andre Przywara

The number of LPIs on a host can be potentially huge (millions), although in practise will be mostly reasonable. So prematurely allocating an array of struct irq_desc's for each LPI is not an option. However Xen itself does not care about LPIs, as every LPI will be injected into a guest (Dom0 for now).
Create a dense data structure (8 Bytes) for each LPI which holds just enough information to determine the virtual IRQ number and the VCPU into which the LPI needs to be injected.
Also to not artificially limit the number of LPIs, we create a 2-level table for holding those structures.
This patch introduces functions to initialize these tables and to create, lookup and destroy entries for a given LPI.
We allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-its.c        | 154 ++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  18 +++++
 2 files changed, 172 insertions(+)
xen/arch/arm/gic-its.c +154 -0
index 88397bc..2140e4a 100644
@@ -18,18 +18,31 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/sched.h>
 #include <xen/err.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/domain.h>
 #include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
 
+/* LPIs on the host always go to a guest, so no struct irq_desc for them. */
+union host_lpi {
+    uint64_t data;
+    struct {
+        uint64_t virt_lpi:32;
+        uint64_t dom_id:16;
+        uint64_t vcpu_id:16;
+    };
+};
+
 /* Global state */
 static struct {
     uint8_t *lpi_property;
+    union host_lpi **host_lpis;
     int host_lpi_bits;
 } lpi_data;
 
@@ -43,6 +56,26 @@ static DEFINE_PER_CPU(void *, pending_table);
 #define MAX_HOST_LPI_BITS                                                \
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
+#define HOST_LPIS_PER_PAGE      (PAGE_SIZE / sizeof(union host_lpi))
+
+static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d)
+{
+    union host_lpi *hlpi;
+
+    if ( lpi < 8192 || lpi >= MAX_HOST_LPIS + 8192 )
+        return NULL;
+
+    lpi -= 8192;
+    if ( !lpi_data.host_lpis[lpi / HOST_LPIS_PER_PAGE] )
+        return NULL;
+
+    hlpi = &lpi_data.host_lpis[lpi / HOST_LPIS_PER_PAGE][lpi % HOST_LPIS_PER_PAGE];
+
+    if ( d && hlpi->dom_id != d->domain_id )
+        return NULL;
+
+    return hlpi;
+}
 
 #define ITS_COMMAND_SIZE        32
 
@@ -96,6 +129,33 @@ static int its_send_cmd_sync(struct host_its *its, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_discard(struct host_its *its,
+                                uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_DISCARD | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+static int its_send_cmd_mapti(struct host_its *its,
+                              uint32_t deviceid, uint32_t eventid,
+                              uint32_t pintid, uint16_t icid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPTI | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid | ((uint64_t)pintid << 32);
+    cmd[2] = icid;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
 {
     uint64_t cmd[4];
@@ -330,15 +390,109 @@ uint64_t gicv3_lpi_get_proptable()
     return reg;
 }
 
+/* Allocate the 2nd level array for host LPIs. This one holds pointers
+ * to the page with the actual "union host_lpi" entries. Our LPI limit
+ * avoids excessive memory usage.
+ */
 int gicv3_lpi_init_host_lpis(int lpi_bits)
 {
+    int nr_lpi_ptrs;
+
     lpi_data.host_lpi_bits = lpi_bits;
 
+    nr_lpi_ptrs = MAX_HOST_LPIS / (PAGE_SIZE / sizeof(union host_lpi));
+
+    lpi_data.host_lpis = xzalloc_array(union host_lpi *, nr_lpi_ptrs);
+    if ( !lpi_data.host_lpis )
+        return -ENOMEM;
+
     printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);
 
     return 0;
 }
 
+/* Allocates a new host LPI to be injected as "virt_lpi" into the specified
+ * VCPU. Returns the host LPI ID or a negative error value.
+ */
+int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                uint32_t devid, uint32_t eventid,
+                                struct vcpu *v, int virt_lpi)
+{
+    int chunk, i;
+    union host_lpi hlpi, *new_chunk;
+
+    /* TODO: handle some kind of preassigned LPI mapping for DomUs */
+    if ( !its )
+        return -EPERM;
+
+    /* TODO: This could be optimized by storing some "next available" hint and
+     * only iterate if this one doesn't work. But this function should be
+     * called rarely.
+     */
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        /* If we hit an unallocated chunk, we initialize it and use entry 0. */
+        if ( !lpi_data.host_lpis[chunk] )
+        {
+            new_chunk = alloc_xenheap_pages(0, 0);
+            if ( !new_chunk )
+                return -ENOMEM;
+
+            memset(new_chunk, 0, PAGE_SIZE);
+            lpi_data.host_lpis[chunk] = new_chunk;
+            i = 0;
+        }
+        else
+        {
+            /* Find an unallocted entry in this chunk. */
+            for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+                if ( !lpi_data.host_lpis[chunk][i].virt_lpi )
+                    break;
+
+            /* If this chunk is fully allocted, advance to the next one. */
+            if ( i == HOST_LPIS_PER_PAGE)
+                continue;
+        }
+
+        hlpi.virt_lpi = virt_lpi;
+        hlpi.dom_id = v->domain->domain_id;
+        hlpi.vcpu_id = v->vcpu_id;
+        lpi_data.host_lpis[chunk][i].data = hlpi.data;
+
+        if (its)
+        {
+            its_send_cmd_mapti(its, devid, eventid,
+                               chunk * HOST_LPIS_PER_PAGE + i + 8192, 0);
+            its_send_cmd_sync(its, 0);
+        }
+
+        return chunk * HOST_LPIS_PER_PAGE + i + 8192;
+    }
+
+    return -ENOSPC;
+}
+
+/* Drops the connection of the given host LPI to a virtual LPI.
+ */
+int gicv3_lpi_drop_host_lpi(struct host_its *its,
+                            uint32_t devid, uint32_t eventid, uint32_t host_lpi)
+{
+    union host_lpi *hlpip;
+
+    if ( !its )
+        return -EPERM;
+
+    hlpip = gic_find_host_lpi(host_lpi, NULL);
+    if ( !hlpip )
+        return -1;
+
+    hlpip->data = 0;
+
+    its_send_cmd_discard(its, devid, eventid);
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/include/asm-arm/gic-its.h +18 -0
index b49d274..512a388 100644
@@ -114,6 +114,12 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                uint32_t devid, uint32_t eventid,
+                                struct vcpu *v, int virt_lpi);
+int gicv3_lpi_drop_host_lpi(struct host_its *its,
+                            uint32_t devid, uint32_t eventid,
+                            uint32_t host_lpi);
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -141,6 +147,18 @@ static inline void gicv3_set_redist_addr(paddr_t address, int redist_id)
 static inline void gicv3_its_setup_collection(int cpu)
 {
 }
+static inline int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                              uint32_t devid, uint32_t eventid,
+                                              struct vcpu *v, int virt_lpi)
+{
+    return 0;
+}
+static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
+                                          uint32_t devid, uint32_t eventid,
+                                          uint32_t host_lpi)
+{
+    return 0;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
Signature · 7 lines
2.9.0


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

RFC PATCH 4/24 ARM: GICv3 ITS: map ITS command buffer

In reply to Andre Przywara

Instead of directly manipulating the tables in memory, an ITS driver sends commands via a ring buffer to the ITS h/w to create or alter the LPI mappings.
Allocate memory for that buffer and tell the ITS about it to be able to send ITS commands.

---
 xen/arch/arm/gic-its.c        | 25 +++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  1 +
 2 files changed, 26 insertions(+)
xen/arch/arm/gic-its.c +25 -0
index 40238a2..c8a7a7e 100644
@@ -18,6 +18,7 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/err.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
@@ -56,6 +57,26 @@ static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
     return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
 }
 
+static void *gicv3_map_cbaser(void __iomem *cbasereg)
+{
+    uint64_t attr, reg;
+    void *buffer;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    buffer = alloc_xenheap_pages(0, 0);
+    if ( !buffer )
+        return ERR_PTR(-ENOMEM);
+
+    /* We use exactly one 4K page, so the "Size" field is 0. */
+    reg = attr | BIT(63) | (virt_to_maddr(buffer) & GENMASK(51, 12));
+    writeq_relaxed(reg, cbasereg);
+
+    return buffer;
+}
+
 static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
 {
     uint64_t attr;
@@ -149,6 +170,10 @@ int gicv3_its_init(struct host_its *hw_its)
         }
     }
 
+    hw_its->cmd_buf = gicv3_map_cbaser(hw_its->its_base + GITS_CBASER);
+    if ( IS_ERR(hw_its->cmd_buf) )
+        return PTR_ERR(hw_its->cmd_buf);
+
     return 0;
 }
 
xen/include/asm-arm/gic-its.h +1 -0
index 589b889..b2a003f 100644
@@ -69,6 +69,7 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    void *cmd_buf;
 };
 
 extern struct list_head host_its_list;
Signature · 7 lines
2.9.0


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

RFC PATCH 7/24 ARM: GICv3 ITS: introduce device mapping

In reply to Andre Przywara

The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host.
For this we have to map each device that Dom0 may request to a host ITS device with the same identifier.
Allocate the respective memory and enter each device into a list to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-its.c        | 90 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h | 16 ++++++++
 2 files changed, 106 insertions(+)
xen/arch/arm/gic-its.c +90 -0
index 2140e4a..bf1f5b5 100644
@@ -168,6 +168,94 @@ static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             int size, uint64_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = size & GENMASK(4, 0);
+    cmd[2] = itt_addr & GENMASK(51, 8);
+    if ( valid )
+        cmd[2] |= BIT(63);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid)
+{
+    void *itt_addr = NULL;
+    struct its_devices *dev, *temp;
+    bool reuse_dev = false;
+
+    list_for_each_entry_safe(dev, temp, &hw_its->its_devices, entry)
+    {
+        if ( (dev->d->domain_id != d->domain_id) || (dev->devid != devid) )
+            continue;
+
+        its_send_cmd_mapd(hw_its, dev->devid, 0, 0, false);
+        xfree(dev->itt_addr);
+        if ( !valid )
+        {
+            xfree(dev);
+            list_del(&dev->entry);
+
+            return 0;
+        }
+
+        reuse_dev = true;
+        break;
+    }
+
+    if ( !valid )
+        return 0;
+
+    itt_addr = _xmalloc(BIT(bits) * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        return -ENOMEM;
+
+    if ( !reuse_dev )
+    {
+        dev = xmalloc(struct its_devices);
+        if ( !dev )
+            return -ENOMEM;
+
+        list_add_tail(&dev->entry, &hw_its->its_devices);
+    }
+
+    dev->itt_addr = itt_addr;
+    dev->d = d;
+    dev->devid = devid;
+
+    return its_send_cmd_mapd(hw_its, devid, bits - 1,
+                             itt_addr ? virt_to_maddr(itt_addr) : 0, true);
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+int its_remove_domain(struct domain *d)
+{
+    struct host_its *its;
+    struct its_devices *dev, *temp;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        list_for_each_entry_safe(dev, temp, &its->its_devices, entry)
+        {
+            if ( dev->d->domain_id != d->domain_id )
+                continue;
+
+            its_send_cmd_mapd(its, dev->devid, 0, 0, false);
+            xfree(dev->itt_addr);
+            xfree(dev);
+            list_del(&dev->entry);
+        }
+    }
+
+    return 0;
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 void gicv3_its_setup_collection(int cpu)
 {
@@ -297,6 +385,7 @@ int gicv3_its_init(struct host_its *hw_its)
 
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     hw_its->pta = reg & GITS_TYPER_PTA;
+    hw_its->itte_size = ((reg >> 4) & 0xf) + 1;
 
     for (i = 0; i < 8; i++)
     {
@@ -520,6 +609,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->size = size;
         its_data->dt_node = its;
         spin_lock_init(&its_data->cmd_lock);
+        INIT_LIST_HEAD(&its_data->its_devices);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/include/asm-arm/gic-its.h +16 -0
index 512a388..4e9841a 100644
@@ -79,6 +79,13 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
+struct its_devices {
+    struct list_head entry;
+    struct domain *d;
+    void *itt_addr;
+    int devid;
+};
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -88,6 +95,8 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    struct list_head its_devices;
+    int itte_size;
     bool pta;
 };
 
@@ -114,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* 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.
+ */
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid);
+
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
Signature · 7 lines
2.9.0


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

RFC PATCH 8/24 ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +3 -0
index 63c744a..ebe4035 100644
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +53 -3

102 lines not on this page · raw message

xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +10 -0
index 4e9841a..1f881c0 100644
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -0
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
Signature · 7 lines
2.9.0


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

RFC PATCH 9/24 ARM: GICv3: forward pending LPIs to guests

In reply to Andre Przywara

Upon receiving an LPI, we need to find the right VCPU and virtual IRQ number to get this IRQ injected.
Iterate our two-level LPI table to find this information quickly when the host takes an LPI. Call the existing injection function to let the GIC emulation deal with this interrupt.

---
 xen/arch/arm/gic-its.c    | 32 ++++++++++++++++++++++++++++++++
 xen/arch/arm/gic.c        |  6 ++++--
 xen/include/asm-arm/irq.h |  8 ++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)
xen/arch/arm/gic-its.c +32 -0
index bf1f5b5..b7aa918 100644
@@ -77,6 +77,38 @@ static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d)
     return hlpi;
 }
 
+/* Handle incoming LPIs, which are a bit special, because they are potentially
+ * numerous and also only get injected into guests. Treat them specially here,
+ * by just looking up their target vCPU and virtual LPI number and hand it
+ * over to the injection function.
+ */
+void do_LPI(unsigned int lpi)
+{
+    struct domain *d;
+    union host_lpi *hlpip, hlpi;
+
+    WRITE_SYSREG32(lpi, ICC_EOIR1_EL1);
+    WRITE_SYSREG32(lpi, ICC_DIR_EL1);
+
+    hlpip = gic_find_host_lpi(lpi, NULL);
+    if ( !hlpip )
+        return;
+
+    hlpi.data = hlpip->data;
+
+    if ( !hlpi.virt_lpi )
+        return;
+
+    d = get_domain_by_id(hlpi.dom_id);
+    if ( !d )
+        return;
+
+    if ( hlpi.vcpu_id >= d->max_vcpus )
+        return;
+
+    vgic_vcpu_inject_irq(d->vcpu[hlpi.vcpu_id], hlpi.virt_lpi);
+}
+
 #define ITS_COMMAND_SIZE        32
 
 static int its_send_command(struct host_its *hw_its, void *its_cmd)
xen/arch/arm/gic.c +4 -2
index ebe4035..2fad2f1 100644
@@ -697,8 +697,10 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
             local_irq_enable();
             do_IRQ(regs, irq, is_fiq);
             local_irq_disable();
-        }
-        else if (unlikely(irq < 16))
+        } else if ( irq >= 8192 )
+        {
+            do_LPI(irq);
+        } else if ( unlikely(irq < 16) )
         {
             do_sgi(regs, irq);
         }
xen/include/asm-arm/irq.h +8 -0
index 8f7a167..ee47de8 100644
@@ -34,6 +34,14 @@ struct irq_desc *__irq_to_desc(int irq);
 
 void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq);
 
+#ifdef CONFIG_HAS_ITS
+void do_LPI(unsigned int irq);
+#else
+static inline void do_LPI(unsigned int irq)
+{
+}
+#endif
+
 #define domain_pirq_to_irq(d, pirq) (pirq)
 
 bool_t is_assignable_irq(unsigned int irq);
Signature · 7 lines
2.9.0


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

RFC PATCH 10/24 ARM: GICv3: enable ITS and LPIs on the host

In reply to Andre Przywara

Now that the host part of the ITS code is in place, we can enable the ITS and also LPIs on each redistributor to get the show rolling. At this point there would be no LPIs mapped, as guests don't know about the ITS yet.

---
 xen/arch/arm/gic-its.c |  4 ++++
 xen/arch/arm/gic-v3.c  | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
xen/arch/arm/gic-its.c +4 -0
index b7aa918..6bac422 100644
@@ -449,6 +449,10 @@ int gicv3_its_init(struct host_its *hw_its)
     its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
     its_send_cmd_sync(hw_its, smp_processor_id());
 
+    /* Now enable interrupt translation on that ITS. */
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg | GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
     return 0;
 }
 
xen/arch/arm/gic-v3.c +19 -0
index b9387a3..57009c6 100644
@@ -642,6 +642,21 @@ static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
     gicv3_its_setup_collection(smp_processor_id());
 }
 
+/* Enable LPIs on this redistributor (only useful when the host has an ITS. */
+static bool gicv3_enable_lpis(void)
+{
+    uint32_t val;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_TYPER);
+    if ( !(val & GICR_TYPER_PLPIS) )
+        return false;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_CTLR);
+    writel_relaxed(val | GICR_CTLR_ENABLE_LPIS, GICD_RDIST_BASE + GICR_CTLR);
+
+    return true;
+}
+
 static int __init gicv3_populate_rdist(void)
 {
     int i;
@@ -741,6 +756,10 @@ static int gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+    /* If the host has any ITSes, enable LPIs now. */
+    if ( !list_empty(&host_its_list) )
+        gicv3_enable_lpis();
+
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
                 GIC_PRI_IPI);
Signature · 7 lines
2.9.0


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

RFC PATCH 11/24 ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +175 -14
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #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 <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);
+    }
+
+    ptr = vmap(pages, nr_pages);
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;
+
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;
+
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+	reg = v->arch.vgic.rdist_pendbase;
+	vgic_reg64_update(&reg, r, info);
+	reg = sanitize_pendbaser(reg);
+	v->arch.vgic.rdist_pendbase = reg;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);
+	v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);
+	return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +4 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +6 -1
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +9 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +3 -0
index 4e29ba6..2b216cc 100644
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*
Signature · 7 lines
2.9.0


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

RFC PATCH 12/24 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-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +378 -0

378 lines not on this page · raw message

xen/arch/arm/vgic-v3.c +0 -9
index 8fe8386..aa53a1e 100644
@@ -158,15 +158,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 da5fb77..6a91f5b 100644
@@ -147,6 +147,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)
 
@@ -190,6 +200,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
#80 lore raw

RFC PATCH 13/24 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.

In addition this patch introduces the lookup function which translates a given DeviceID/EventID pair into a pointer to our vITTE structure.

---
 xen/arch/arm/vgic-its.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
xen/arch/arm/vgic-its.c +115 -0
index 875b992..99d9e9c 100644
@@ -61,6 +61,73 @@ struct vits_itte
     uint64_t collection:16;
 };
 
+#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);
+    struct vits_itte *itte;
+
+    if (addr == ~0)
+        return NULL;
+
+    /* TODO: check locking for map_guest_pages() */
+    itte = map_guest_pages(its->d, addr & PAGE_MASK, 1);
+    if ( !itte )
+        return NULL;
+
+    return itte + (addr & ~PAGE_MASK) / sizeof(struct vits_itte);
+}
+
+/* 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);
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/
@@ -80,6 +147,51 @@ 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 vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vcpu = get_vcpu_from_collection(its, itte->collection);
+    if ( !vcpu )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vlpi = itte->vlpi;
+
+    put_devid_evid(its, itte);
+    spin_unlock(&its->its_lock);
+
+    /* 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;
+    }
+
+    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,
@@ -100,6 +212,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
#84 lore raw

RFC PATCH 14/24 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-its.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
xen/arch/arm/vgic-its.c +34 -0
index 99d9e9c..7072753 100644
@@ -192,6 +192,37 @@ 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 vits_itte *itte;
+    struct vcpu *vcpu;
+    int ret = -1;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    vcpu = its->d->vcpu[itte->collection];
+    vlpi = itte->vlpi;
+
+    ret = 0;
+
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+
+    if ( !ret)
+        vgic_vcpu_inject_irq(vcpu, vlpi);
+
+    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,
@@ -215,6 +246,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
#86 lore raw

RFC PATCH 15/24 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-its.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
xen/arch/arm/vgic-its.c +30 -0
index 7072753..caad320 100644
@@ -223,6 +223,33 @@ out_unlock:
     return ret;
 }
 
+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,
@@ -249,6 +276,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
#88 lore raw

RFC PATCH 16/24 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.

Also we propagate the MAPD request to the hardware ITS, as the device ID is only meaningful there.

---
 xen/arch/arm/vgic-its.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
xen/arch/arm/vgic-its.c +31 -0
index caad320..83d47e1 100644
@@ -250,6 +250,34 @@ 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);
+
+    /* DomUs (will later) have their ITTs allocated at domain creation time,
+     * when Dom0 configures the passthrough.
+     */
+    if ( its->hw_its )
+        return gicv3_its_map_device(its->hw_its,
+                                    its->d, devid, size + 1, valid);
+
+    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,
@@ -279,6 +307,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
#92 lore raw

RFC PATCH 18/24 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-its.c        | 16 +++++++++++++++
 xen/arch/arm/vgic-its.c       | 46 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  1 +
 3 files changed, 63 insertions(+)
xen/arch/arm/gic-its.c +16 -0
index 6bac422..d1b1cbb 100644
@@ -618,6 +618,22 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
     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 host_lpi, int new_vcpu_id)
+{
+    union host_lpi *hlpip, hlpi;
+
+    hlpip = gic_find_host_lpi(host_lpi, d);
+    if ( !hlpip )
+        return -1;
+
+    hlpi.data = hlpip->data;
+    hlpi.vcpu_id = new_vcpu_id;
+    hlpip->data = hlpi.data;
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +46 -0

58 lines not on this page · raw message

xen/include/asm-arm/gic-its.h +1 -0
index 3b2e5c0..7e1142f 100644
@@ -133,6 +133,7 @@ int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
+int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
Signature · 7 lines
2.9.0


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

RFC PATCH 19/24 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/gic-its.c        | 21 +++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 48 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  5 +++++
 3 files changed, 74 insertions(+)
xen/arch/arm/gic-its.c +21 -0
index d1b1cbb..766a7cb 100644
@@ -634,6 +634,27 @@ int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id)
     return 0;
 }
 
+/* Looks up a given host LPI assigned to that domain and returns the
+ * connected virtual LPI number. Also stores the target vcpu ID in
+ * the passed vcpu_id pointer.
+ * Returns 0 if no host LPI could be found for that domain, or the
+ * virtual LPI number (>= 8192) if the lookup succeeded.
+ */
+uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
+{
+    union host_lpi *hlpip, hlpi;
+
+    hlpip = gic_find_host_lpi(host_lpi, d);
+    if ( !hlpip )
+        return 0;
+
+    hlpi.data = hlpip->data;
+    if ( vcpu_id )
+        *vcpu_id = hlpi.vcpu_id;
+
+    return hlpi.virt_lpi;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +48 -0

60 lines not on this page · raw message

xen/include/asm-arm/gic-its.h +5 -0
index 7e1142f..3f5698d 100644
@@ -133,6 +133,11 @@ int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
+/* Given a physical LPI, looks up and returns the associated virtual LPI
+ * and the target VCPU in the given domain.
+ */
+uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi,
+                              int *vcpu_id);
 int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
Signature · 7 lines
2.9.0


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

RFC PATCH 17/24 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 allocate a new host LPI and connect that one to this virtual LPI, so that any triggering IRQ on the host can be quickly forwarded to a guest.

---
 xen/arch/arm/vgic-its.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
xen/arch/arm/vgic-its.c +53 -0
index 83d47e1..70897dd 100644
@@ -278,6 +278,55 @@ 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 vits_itte *itte;
+    uint32_t host_lpi;
+    struct vcpu *vcpu;
+    int ret = -1;
+
+    if ( its_cmd_get_command(cmdptr) == GITS_CMD_MAPI )
+        intid = eventid;
+
+    if ( collid >= its->max_collections )
+        return -1;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    if ( !vcpu )
+        goto out_unlock;
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    if ( itte->hlpi )
+        goto out_unmap;
+
+    host_lpi = gicv3_lpi_allocate_host_lpi(its->hw_its,
+                                           devid, eventid,
+                                           vcpu, intid);
+    if ( host_lpi >= 0 )
+        itte->hlpi = host_lpi;
+
+    itte->vlpi = intid;
+    itte->collection = collid;
+
+    ret = 0;
+
+out_unmap:
+    put_devid_evid(its, itte);
+
+out_unlock:
+    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,
@@ -310,6 +359,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;
Signature · 7 lines
2.9.0


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

RFC PATCH 20/24 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 propagate the enable bit to the hardware.

---
 xen/arch/arm/gic-its.c        | 35 ++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 74 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  3 ++
 3 files changed, 112 insertions(+)
xen/arch/arm/gic-its.c +35 -0
index 766a7cb..6f4329f 100644
@@ -215,6 +215,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -655,6 +668,28 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+void gicv3_lpi_set_enable(struct host_its *its,
+                          uint32_t deviceid, uint32_t eventid,
+                          uint32_t host_lpi, bool enabled)
+{
+    host_lpi -= 8192;
+
+    if ( host_lpi >= MAX_HOST_LPIS )
+        return;
+
+    if ( !its )
+        return;
+
+    if (enabled)
+        lpi_data.lpi_property[host_lpi] |= LPI_PROP_ENABLED;
+    else
+        lpi_data.lpi_property[host_lpi] &= ~LPI_PROP_ENABLED;
+
+    __flush_dcache_area(&lpi_data.lpi_property[host_lpi], 1);
+    its_send_cmd_inv(its, deviceid, eventid);
+    its_send_cmd_sync(its, 0);
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +74 -0

86 lines not on this page · raw message

xen/include/asm-arm/gic-its.h +3 -0
index 3f5698d..2cdb3e1 100644
@@ -139,6 +139,9 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi,
                               int *vcpu_id);
 int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
+void gicv3_lpi_set_enable(struct host_its *its,
+                          uint32_t deviceid, uint32_t eventid,
+                          uint32_t host_lpi, bool enabled);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
Signature · 7 lines
2.9.0


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

RFC PATCH 21/24 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). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +58 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }
+
+    /* Tell all ITSes that they should update the property table for CPU 0,
+     * which is where we map all LPIs to.
+     */
+    list_for_each_entry(its, &host_its_list, entry)
+        its_send_cmd_invall(its, 0);
+}
+
 void gicv3_lpi_set_enable(struct host_its *its,
                           uint32_t deviceid, uint32_t eventid,
                           uint32_t host_lpi, bool enabled)
xen/arch/arm/vgic-its.c +30 -0
index 74da8fc..1e429b7 100644
@@ -294,6 +294,33 @@ out_unlock:
     return ret;
 }
 
+/* INVALL updates the per-LPI configuration status for every LPI mapped to
+ * this redistributor. For the guest side we don't need to update anything,
+ * as we always refer to the actual table for the enabled bit and the
+ * priority.
+ * Enabling or disabling a virtual LPI however needs to be propagated to
+ * the respective host LPI. Instead of iterating over all mapped LPIs in our
+ * emulated GIC (which is expensive due to the required on-demand mapping),
+ * we iterate over all mapped _host_ LPIs and filter for those which are
+ * forwarded to this virtual redistributor.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !vcpu )
+        return -1;
+
+    gicv3_lpi_update_configurations(vcpu, its->d->arch.vgic.proptable);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -515,6 +542,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;
xen/include/asm-arm/gic-its.h +2 -0
index 2cdb3e1..ba6b2d5 100644
@@ -146,6 +146,8 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
 
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable);
+
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
Signature · 7 lines
2.9.0


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

RFC PATCH 22/24 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-its.c       | 22 ++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c        | 12 ++++++++++++
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 13 +++++++++++++
 4 files changed, 48 insertions(+)
xen/arch/arm/vgic-its.c +22 -0
index 1e429b7..5c605b5 100644
@@ -829,6 +829,28 @@ 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, struct host_its *hw_its,
+                             paddr_t guest_addr)
+{
+    struct virt_its *its;
+
+    its = xzalloc(struct virt_its);
+    if ( ! its )
+        return -ENOMEM;
+
+    its->d = d;
+    its->hw_its = hw_its;
+    its->baser0 = 0x7917000000000400;
+    its->baser1 = 0x3c01000000000400;
+    its->cbaser = 0x380e000000000400;
+    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 aa53a1e..d230a1f 100644
@@ -31,6 +31,7 @@
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 #include <asm/vgic.h>
 #include <asm/vgic-emul.h>
 
@@ -1572,6 +1573,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;
@@ -1597,6 +1599,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, hw_its->addr);
+
+            d->arch.vgic.has_its = true;
+        }
+
     }
     else
     {
xen/include/asm-arm/domain.h +1 -0
index 0cd3500..1c2f7c7 100644
@@ -111,6 +111,7 @@ struct arch_domain
         uint32_t rdist_stride;              /* Re-Distributor stride */
         uint64_t rdist_propbase;
         uint8_t *proptable;
+        bool has_its;
 #endif
     } vgic;
 
xen/include/asm-arm/gic-its.h +13 -0
index ba6b2d5..b58e092 100644
@@ -123,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void 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, struct host_its *hw_its,
+                             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.
@@ -204,6 +211,12 @@ static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
 {
     return false;
 }
+static inline int vgic_v3_its_init_virtual(struct domain *d,
+                                           struct host_its *hw_its,
+                                           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
#139 lore raw

RFC PATCH 23/24 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-its.c        | 68 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  4 ++-
 xen/include/asm-arm/gic-its.h | 13 +++++++++
 3 files changed, 84 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +68 -0
index 5129d6e..bb0b80a 100644
@@ -748,6 +748,74 @@ void gicv3_lpi_set_enable(struct host_its *its,
     its_send_cmd_sync(its, 0);
 }
 
+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;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/gic-v3.c +3 -1
index 57009c6..9fba3eb 100644
@@ -1176,8 +1176,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-its.h +13 -0
index b58e092..e20b5bc 100644
@@ -130,6 +130,12 @@ void gicv3_its_setup_collection(int cpu);
 int vgic_v3_its_init_virtual(struct domain *d, struct host_its *hw_its,
                              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.
@@ -217,6 +223,13 @@ static inline int vgic_v3_its_init_virtual(struct domain *d,
 {
     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
#140 lore raw

RFC PATCH 24/24 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 | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
xen/arch/arm/vgic-v3.c +23 -5
index d230a1f..61c97a2 100644
@@ -168,8 +168,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;
@@ -181,16 +183,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;
@@ -468,8 +473,16 @@ 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;
+
+        if ( r & 1 )
+            v->arch.vgic.flags |= VGIC_V3_LPIS_ENABLED;
+        else
+            v->arch.vgic.flags &= !VGIC_V3_LPIS_ENABLED;
+
+        return 1;
 
     case VREG32(GICR_IIDR):
         /* RO */
@@ -1075,6 +1088,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);
Signature · 7 lines
2.9.0


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

Re: [RFC PATCH 03/24] ARM: GICv3 ITS: allocate device and collection table

In reply to Andre Przywara

Hi Andre,

On Thunderx, MAPD commands are failing with error 0x1,
which mean DEVID out of range.

Quoted 122 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (usually the PCI b/d/f triplet) and an EventID (the MSI payload or interrupt ID) to a pair of LPI number and collection ID, which points to the target CPU.
This mapping is stored in the device and collection tables, which software has to provide for the ITS to use.
Allocate the required memory and hand it the ITS.
We limit the number of devices to cover 4 PCI busses for now.

---
 xen/arch/arm/gic-its.c        | 114 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   5 ++
 xen/include/asm-arm/gic-its.h |  49 +++++++++++++++++-
 3 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +92 -0
index b52dff3..40238a2 100644
@@ -21,6 +21,7 @@
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
@@ -38,6 +39,119 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define BASER_ATTR_MASK                                           \
+        ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
+         (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
+         (0x7UL << GITS_BASER_INNER_CACHEABILITY_SHIFT))
+#define BASER_RO_MASK   (GENMASK(52, 48) | GENMASK(58, 56))
+
+static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
+{
+    uint64_t ret;
+
+    if ( page_bits < 16)
+        return (uint64_t)addr & GENMASK(47, page_bits);
+
+    ret = addr & GENMASK(47, 16);
+    return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
+}
+
+static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
+{
+    uint64_t attr;
+    int entry_size = (regc >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f;
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */
+    for (pagesz = 0; pagesz < 3; pagesz++)
+    {
+        uint64_t reg;
+        int nr_bytes;
+
+        nr_bytes = ROUNDUP(nr_items * entry_size, BIT(pagesz * 2 + 12));
+        order = get_order_from_bytes(nr_bytes);
+
+        if ( !buffer )
+            buffer = alloc_xenheap_pages(order, 0);
+        if ( !buffer )
+            return -ENOMEM;
+
+        reg  = attr;
+        reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+        reg |= nr_bytes >> (pagesz * 2 + 12);
+        reg |= regc & BASER_RO_MASK;
+        reg |= GITS_BASER_VALID;
+        reg |= encode_phys_addr(virt_to_maddr(buffer), pagesz * 2 + 12);
+
+        writeq_relaxed(reg, basereg);
+        regc = readl_relaxed(basereg);
+
+        /* The host didn't like our attributes, just use what it returned. */
+        if ( (regc & BASER_ATTR_MASK) != attr )
+            attr = regc & BASER_ATTR_MASK;
+
+        /* If the host accepted our page size, we are done. */
+        if ( (reg & (3UL << GITS_BASER_PAGE_SIZE_SHIFT)) == pagesz )
+            return 0;
+
+        /* Check whether our buffer is aligned to the next page size already. */
+        if ( !(virt_to_maddr(buffer) & (BIT(pagesz * 2 + 12 + 2) - 1)) )
+        {
+            free_xenheap_pages(buffer, order);
+            buffer = NULL;
+        }
+    }
+
+    if ( buffer )
+        free_xenheap_pages(buffer, order);
+
+    return -EINVAL;
+}
+
+int gicv3_its_init(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    for (i = 0; i < 8; i++)
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
+        int type;

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 142

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+        reg = readq_relaxed(basereg);
+        type = (reg >> 56) & 0x7;
+        switch ( type )
+        {
+        case GITS_BASER_TYPE_NONE:
+            continue;
+        case GITS_BASER_TYPE_DEVICE:
+            /* TODO: find some better way of limiting the number of devices */
+            gicv3_map_baser(basereg, reg, 1024);

Thunderx has larger device id values.
Changing to hw supported device ids makes MAPD commands to pass and Thunderx to boot.
You can refer to Thunderx bdf numbers here
https://github.com/vijaykilari/its_v6/commit/e1a8ec82ad2bb00b299727d0847b89671e9ba66d

Quoted 131 lines
xen/arch/arm/gic-its.c +12 -0
+            break;
+        case GITS_BASER_TYPE_COLLECTION:
+            gicv3_map_baser(basereg, reg, NR_CPUS);
+            break;
+        default:
+            continue;
+        }
+    }
+
+    return 0;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
xen/arch/arm/gic-v3.c +5 -0
index 2534aa5..5cf4618 100644
@@ -29,6 +29,7 @@
 #include <xen/irq.h>
 #include <xen/iocap.h>
 #include <xen/sched.h>
+#include <xen/err.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
@@ -1548,6 +1549,7 @@ static int __init gicv3_init(void)
 {
     int res, i;
     uint32_t reg;
+    struct host_its *hw_its;
 
     if ( !cpu_has_gicv3 )
     {
@@ -1603,6 +1605,9 @@ static int __init gicv3_init(void)
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 
+    list_for_each_entry(hw_its, &host_its_list, entry)
+        gicv3_its_init(hw_its);
+
     spin_unlock(&gicv3.lock);
 
     return res;
xen/include/asm-arm/gic-its.h +48 -2
index 48c6c78..589b889 100644
@@ -18,6 +18,47 @@
 #ifndef __ASM_ARM_ITS_H__
 #define __ASM_ARM_ITS_H__
 
+#define LPI_OFFSET      8192
+
+#define GITS_CTLR       (0x000)
+#define GITS_IIDR       (0x004)
+#define GITS_TYPER      (0x008)
+#define GITS_CBASER     (0x080)
+#define GITS_CWRITER    (0x088)
+#define GITS_CREADR     (0x090)
+#define GITS_BASER0     (0x100)
+#define GITS_BASER1     (0x108)
+#define GITS_BASER2     (0x110)
+#define GITS_BASER3     (0x118)
+#define GITS_BASER4     (0x120)
+#define GITS_BASER5     (0x128)
+#define GITS_BASER6     (0x130)
+#define GITS_BASER7     (0x138)
+
+/* Register bits */
+#define GITS_CTLR_ENABLE     0x1
+#define GITS_IIDR_VALUE      0x34c
+
+#define GITS_BASER_VALID                BIT(63)
+#define GITS_BASER_INDIRECT             BIT(62)
+#define GITS_BASER_INNER_CACHEABILITY_SHIFT        59
+#define GITS_BASER_TYPE_SHIFT           56
+#define GITS_BASER_OUTER_CACHEABILITY_SHIFT        53
+#define GITS_BASER_TYPE_NONE            0UL
+#define GITS_BASER_TYPE_DEVICE          1UL
+#define GITS_BASER_TYPE_VCPU            2UL
+#define GITS_BASER_TYPE_CPU             3UL
+#define GITS_BASER_TYPE_COLLECTION      4UL
+#define GITS_BASER_TYPE_RESERVED5       5UL
+#define GITS_BASER_TYPE_RESERVED6       6UL
+#define GITS_BASER_TYPE_RESERVED7       7UL
+#define GITS_BASER_ENTRY_SIZE_SHIFT     48
+#define GITS_BASER_SHAREABILITY_SHIFT   10
+#define GITS_BASER_PAGE_SIZE_SHIFT      8
+#define GITS_BASER_RO_MASK              ((7UL << GITS_BASER_TYPE_SHIFT) | \
+                                        (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
+                                        GITS_BASER_INDIRECT)
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
@@ -27,6 +68,7 @@ struct host_its {
     const struct dt_device_node *dt_node;
     paddr_t addr;
     paddr_t size;
+    void __iomem *its_base;
 };
 
 extern struct list_head host_its_list;
@@ -42,8 +84,9 @@ void gicv3_its_dt_init(const struct dt_device_node *node);
 uint64_t gicv3_lpi_get_proptable(void);
 uint64_t gicv3_lpi_allocate_pendtable(void);
 
-/* Initialize the host structures for LPIs. */
+/* Initialize the host structures for LPIs and the host ITSes. */
 int gicv3_lpi_init_host_lpis(int nr_lpis);
+int gicv3_its_init(struct host_its *hw_its);
 
 #else
 
@@ -62,6 +105,10 @@ static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
 {
     return 0;
 }
+static inline int gicv3_its_init(struct host_its *hw_its)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
--

2.9.0

Quoted 4 lines

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

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

#73 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara
Quoted 178 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-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +146 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#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-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    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
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * 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-its.c line 156

Quoted 10 lines
xen/arch/arm/vgic-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);

Here you are limiting DevID bits to 15, which is not enough

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

#17 lore raw

Re: [RFC PATCH 03/24] ARM: GICv3 ITS: allocate device and collection table

In reply to Vijay Kilari

Hi,

Quoted 4 lines · Vijay Kilari

Hi Andre,

On Thunderx, MAPD commands are failing with error 0x1,
which mean DEVID out of range.

MAPD commands from Dom0, you mean?

And thanks for giving it a try!

Quoted 129 lines · Andre Przywara
Quoted 128 lines

Each ITS maps a pair of a DeviceID (usually the PCI b/d/f triplet) and an EventID (the MSI payload or interrupt ID) to a pair of LPI number and collection ID, which points to the target CPU.
This mapping is stored in the device and collection tables, which software has to provide for the ITS to use.
Allocate the required memory and hand it the ITS.
We limit the number of devices to cover 4 PCI busses for now.

---
 xen/arch/arm/gic-its.c        | 114 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   5 ++
 xen/include/asm-arm/gic-its.h |  49 +++++++++++++++++-
 3 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +98 -0
index b52dff3..40238a2 100644
@@ -21,6 +21,7 @@
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
@@ -38,6 +39,119 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define BASER_ATTR_MASK                                           \
+        ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
+         (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
+         (0x7UL << GITS_BASER_INNER_CACHEABILITY_SHIFT))
+#define BASER_RO_MASK   (GENMASK(52, 48) | GENMASK(58, 56))
+
+static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
+{
+    uint64_t ret;
+
+    if ( page_bits < 16)
+        return (uint64_t)addr & GENMASK(47, page_bits);
+
+    ret = addr & GENMASK(47, 16);
+    return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
+}
+
+static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
+{
+    uint64_t attr;
+    int entry_size = (regc >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f;
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */
+    for (pagesz = 0; pagesz < 3; pagesz++)
+    {
+        uint64_t reg;
+        int nr_bytes;
+
+        nr_bytes = ROUNDUP(nr_items * entry_size, BIT(pagesz * 2 + 12));
+        order = get_order_from_bytes(nr_bytes);
+
+        if ( !buffer )
+            buffer = alloc_xenheap_pages(order, 0);
+        if ( !buffer )
+            return -ENOMEM;
+
+        reg  = attr;
+        reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+        reg |= nr_bytes >> (pagesz * 2 + 12);
+        reg |= regc & BASER_RO_MASK;
+        reg |= GITS_BASER_VALID;
+        reg |= encode_phys_addr(virt_to_maddr(buffer), pagesz * 2 + 12);
+
+        writeq_relaxed(reg, basereg);
+        regc = readl_relaxed(basereg);
+
+        /* The host didn't like our attributes, just use what it returned. */
+        if ( (regc & BASER_ATTR_MASK) != attr )
+            attr = regc & BASER_ATTR_MASK;
+
+        /* If the host accepted our page size, we are done. */
+        if ( (reg & (3UL << GITS_BASER_PAGE_SIZE_SHIFT)) == pagesz )
+            return 0;
+
+        /* Check whether our buffer is aligned to the next page size already. */
+        if ( !(virt_to_maddr(buffer) & (BIT(pagesz * 2 + 12 + 2) - 1)) )
+        {
+            free_xenheap_pages(buffer, order);
+            buffer = NULL;
+        }
+    }
+
+    if ( buffer )
+        free_xenheap_pages(buffer, order);
+
+    return -EINVAL;
+}
+
+int gicv3_its_init(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    for (i = 0; i < 8; i++)
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
+        int type;
+
+        reg = readq_relaxed(basereg);
+        type = (reg >> 56) & 0x7;
+        switch ( type )
+        {
+        case GITS_BASER_TYPE_NONE:
Quoted 8 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 142

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+            continue;
+        case GITS_BASER_TYPE_DEVICE:
+            /* TODO: find some better way of limiting the number of devices */
+            gicv3_map_baser(basereg, reg, 1024);

Thunderx has larger device id values.
Changing to hw supported device ids makes MAPD commands to pass and Thunderx to boot.

Ah, thanks for the heads up.
Obviously this "1024" is a hack.
Julien wanted to use platform specific code for the Dom0 device mapping, which could take care of those cases.
I am not so happy with this, since it requires code for each and every platform. Instead I was thinking of using the PV PCI calls that Dom0 issues anyway to get an idea of how many devices we need, but this maybe too late in the boot process.
We would need to take a closer look into this.

Quoted 2 lines

You can refer to Thunderx bdf numbers here
https://github.com/vijaykilari/its_v6/commit/e1a8ec82ad2bb00b299727d0847b89671e9ba66d

Thanks for the link. To be honest, I deliberately didn't look into the previous patches to get a clean start. I guess I will take a look now.

So you need something like 200,000 devices to cover those "three and some" segments?
I guess this means no excuse anymore for postponing indirect mapping ;-)

Cheers,
Andre.

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

#74 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Vijay Kilari

Hi,

Quoted 183 lines · Vijay Kilari
Quoted 182 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-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +150 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#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-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    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
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * 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-its.c line 156

Quoted 6 lines
xen/arch/arm/vgic-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);

Here you are limiting DevID bits to 15, which is not enough

Right, I wasn't aware of those segments Cavium uses.
I will try to find out other vendor's requirements for maximum device IDs and will adjust this number.
And it seems to be one of the places where I missed to change the hacked up numbers into nice #defines ;-)

Cheers,
Andre.

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

#7 lore raw

Re: [RFC PATCH 02/24] ARM: GICv3: allocate LPI pending and property table

In reply to Andre Przywara
Quoted 79 lines · Andre Przywara

The ARM GICv3 ITS provides a new kind of interrupt called LPIs. The pending bits and the configuration data (priority, enable bits) for those LPIs are stored in tables in normal memory, which software has to provide to the hardware.
Allocate the required memory, initialize it and hand it over to each ITS. We limit the number of LPIs we use with a compile time constant to avoid wasting memory.

---
 xen/arch/arm/Kconfig              |  6 ++++
 xen/arch/arm/efi/efi-boot.h       |  1 -
 xen/arch/arm/gic-its.c            | 76 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             | 27 ++++++++++++++
 xen/include/asm-arm/cache.h       |  4 +++
 xen/include/asm-arm/gic-its.h     | 22 +++++++++++-
 xen/include/asm-arm/gic_v3_defs.h | 48 ++++++++++++++++++++++++-
 7 files changed, 181 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +6 -0
index 9fe3b8e..66e2bb8 100644
@@ -50,6 +50,12 @@ config HAS_ITS
         depends on ARM_64
         depends on HAS_GICV3
 
+config HOST_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+
 config ALTERNATIVE
        bool
 
xen/arch/arm/efi/efi-boot.h +0 -1
index 045d6ce..dc64aec 100644
@@ -10,7 +10,6 @@
 #include "efi-dom0.h"
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
-void __flush_dcache_area(const void *vaddr, unsigned long size);
 
 #define DEVICE_TREE_GUID \
 {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}
xen/arch/arm/gic-its.c +19 -0
index 0f42a77..b52dff3 100644
@@ -20,10 +20,86 @@
 #include <xen/lib.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
+#include <asm/p2m.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
 
+/* Global state */
+static struct {
+    uint8_t *lpi_property;
+    int host_lpi_bits;
+} lpi_data;
+
+/* Pending table for each redistributor */
+static DEFINE_PER_CPU(void *, pending_table);
+
+#define MAX_HOST_LPI_BITS                                                \
+        min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
+#define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
+
+uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    uint64_t reg, attr;
+    void *pendtable;
+

xen/arch/arm/gic-its.c line 55

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;
+
+    /*
+     * The pending table holds one bit per LPI, so we need three bits less
+     * than the number of LPI_BITs. But the alignment requirement from the
+     * ITS is 64K, so make order at least 16 (-12).
+     */
+    pendtable = alloc_xenheap_pages(MAX(lpi_data.host_lpi_bits - 3, 16) - 12, 0);

The pend table size allocated is differ from proptable size?

xen/arch/arm/gic-its.c line 59

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+    if ( !pendtable )
+        return 0;
+
+    memset(pendtable, 0, BIT(lpi_data.host_lpi_bits - 3));

memset size is different from allocated size? flushing zeroed pendtable?

xen/arch/arm/gic-its.c line 63

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+    this_cpu(pending_table) = pendtable;
+
+    reg  = attr | GICR_PENDBASER_PTZ;
+    reg |= virt_to_maddr(pendtable) & GENMASK(51, 16);

can use __pa instead of virt_to_maddr()
Isn't GENMASK(47, 12) here?

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+
+    return reg;
+}
+
+uint64_t gicv3_lpi_get_proptable()
+{

xen/arch/arm/gic-its.c line 79

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    uint64_t attr;
+    static uint64_t reg = 0;
+
+    /* The property table is shared across all redistributors. */
+    if ( reg )
+        return reg;
+
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;

using PENDBASER definitions for PROPBASER?

xen/arch/arm/gic-its.c line 89

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+    lpi_data.lpi_property = alloc_xenheap_pages(MAX_HOST_LPI_BITS - 12, 0);
+    if ( !lpi_data.lpi_property )
+        return 0;
+
+    memset(lpi_data.lpi_property, GIC_PRI_IRQ | LPI_PROP_RES1, MAX_HOST_LPIS);
+    __flush_dcache_area(lpi_data.lpi_property, MAX_HOST_LPIS);
+
+    reg  = attr | ((MAX_HOST_LPI_BITS - 1) << 0);
+    reg |= virt_to_maddr(lpi_data.lpi_property) & GENMASK(51, 12);

Isn't GENMASK(47, 12)?

Quoted 41 lines
xen/arch/arm/gic-its.c +13 -0
+
+    return reg;
+}
+
+int gicv3_lpi_init_host_lpis(int lpi_bits)
+{
+    lpi_data.host_lpi_bits = lpi_bits;
+
+    printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/gic-v3.c +10 -0
index 238da84..2534aa5 100644
@@ -546,6 +546,9 @@ static void __init gicv3_dist_init(void)
     type = readl_relaxed(GICD + GICD_TYPER);
     nr_lines = 32 * ((type & GICD_TYPE_LINES) + 1);
 
+    if ( type & GICD_TYPE_LPIS )
+        gicv3_lpi_init_host_lpis(((type >> GICD_TYPE_ID_BITS_SHIFT) & 0x1f) + 1);
+
     printk("GICv3: %d lines, (IID %8.8x).\n",
            nr_lines, readl_relaxed(GICD + GICD_IIDR));
 
@@ -615,6 +618,26 @@ static int gicv3_enable_redist(void)
 
     return 0;
 }
+static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+
+    if ( list_empty(&host_its_list) )
+        return;

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 637

Quoted 10 lines
xen/arch/arm/gic-v3.c +10 -0
+
+    /* Make sure LPIs are disabled before setting up the BASERs. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    writel_relaxed(reg & ~GICR_CTLR_ENABLE_LPIS, rdist_base + GICR_CTLR);
+
+    table_reg = gicv3_lpi_allocate_pendtable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);
+
+    table_reg = gicv3_lpi_get_proptable();

Here LPI property table is allocated per cpu. One property table
should be enough and can be shared by all cpus.

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 639

Quoted 2 lines
xen/arch/arm/gic-v3.c +2 -0
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);

After updating GICR_PENDBASER and GICR_PROPBASER regs
shouldn't we read back and check if sharability bits are support by HW or not like it is done in linux driver?

Quoted 144 lines
xen/arch/arm/gic-v3.c +5 -0
+}
 
 static int __init gicv3_populate_rdist(void)
 {
@@ -658,6 +681,10 @@ static int __init gicv3_populate_rdist(void)
             if ( (typer >> 32) == aff )
             {
                 this_cpu(rbase) = ptr;
+
+                if ( typer & GICR_TYPER_PLPIS )
+                    gicv3_rdist_init_lpis(ptr);
+
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
                 return 0;
xen/include/asm-arm/cache.h +4 -0
index 2de6564..af96eee 100644
@@ -7,6 +7,10 @@
 #define L1_CACHE_SHIFT  (CONFIG_ARM_L1_CACHE_SHIFT)
 #define L1_CACHE_BYTES  (1 << L1_CACHE_SHIFT)
 
+#ifndef __ASSEMBLY__
+void __flush_dcache_area(const void *vaddr, unsigned long size);
+#endif
+
 #define __read_mostly __section(".data.read_mostly")
 
 #endif
xen/include/asm-arm/gic-its.h +21 -1
index 2f5c51c..48c6c78 100644
@@ -36,12 +36,32 @@ extern struct list_head host_its_list;
 /* Parse the host DT and pick up all host ITSes. */
 void gicv3_its_dt_init(const struct dt_device_node *node);
 
+/* Allocate and initialize tables for each host redistributor.
+ * Returns the respective {PROP,PEND}BASER register value.
+ */
+uint64_t gicv3_lpi_get_proptable(void);
+uint64_t gicv3_lpi_allocate_pendtable(void);
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(int nr_lpis);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
 {
 }
-
+static inline uint64_t gicv3_lpi_get_proptable(void)
+{
+    return 0;
+}
+static inline uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    return 0;
+}
+static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
xen/include/asm-arm/gic_v3_defs.h +47 -2
index 6bd25a5..da5fb77 100644
@@ -44,7 +44,8 @@
 #define GICC_SRE_EL2_ENEL1           (1UL << 3)
 
 /* Additional bits in GICD_TYPER defined by GICv3 */
-#define GICD_TYPE_ID_BITS_SHIFT 19
+#define GICD_TYPE_ID_BITS_SHIFT      19
+#define GICD_TYPE_LPIS               (1U << 17)
 
 #define GICD_CTLR_RWP                (1UL << 31)
 #define GICD_CTLR_ARE_NS             (1U << 4)
@@ -95,12 +96,57 @@
 #define GICR_IGRPMODR0               (0x0D00)
 #define GICR_NSACR                   (0x0E00)
 
+#define GICR_CTLR_ENABLE_LPIS        (1U << 0)
 #define GICR_TYPER_PLPIS             (1U << 0)
 #define GICR_TYPER_VLPIS             (1U << 1)
 #define GICR_TYPER_LAST              (1U << 4)
 
+#define GIC_BASER_CACHE_nCnB         0ULL
+#define GIC_BASER_CACHE_SameAsInner  0ULL
+#define GIC_BASER_CACHE_nC           1ULL
+#define GIC_BASER_CACHE_RaWt         2ULL
+#define GIC_BASER_CACHE_RaWb         3ULL
+#define GIC_BASER_CACHE_WaWt         4ULL
+#define GIC_BASER_CACHE_WaWb         5ULL
+#define GIC_BASER_CACHE_RaWaWt       6ULL
+#define GIC_BASER_CACHE_RaWaWb       7ULL
+#define GIC_BASER_CACHE_MASK         7ULL
+#define GIC_BASER_NonShareable       0ULL
+#define GIC_BASER_InnerShareable     1ULL
+#define GIC_BASER_OuterShareable     2ULL
+
+#define GICR_PROPBASER_SHAREABILITY_SHIFT               10
+#define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT         7
+#define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT         56
+#define GICR_PROPBASER_SHAREABILITY_MASK                     \
+        (3UL << GICR_PROPBASER_SHAREABILITY_SHIFT)
+#define GICR_PROPBASER_INNER_CACHEABILITY_MASK               \
+        (7UL << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT)
+#define GICR_PROPBASER_OUTER_CACHEABILITY_MASK               \
+        (7UL << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT)
+#define PROPBASER_RES0_MASK                                  \
+        (GENMASK(63, 59) | GENMASK(55, 52) | GENMASK(6, 5))
+
+#define GICR_PENDBASER_SHAREABILITY_SHIFT               10
+#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT         7
+#define GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT         56
+#define GICR_PENDBASER_SHAREABILITY_MASK                     \
+       (3UL << GICR_PENDBASER_SHAREABILITY_SHIFT)
+#define GICR_PENDBASER_INNER_CACHEABILITY_MASK               \
+       (7UL << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT)
+#define GICR_PENDBASER_OUTER_CACHEABILITY_MASK               \
+        (7UL << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT)
+#define GICR_PENDBASER_PTZ                              BIT(62)
+#define PENDBASER_RES0_MASK                                  \
+        (BIT(63) | GENMASK(61, 59) | GENMASK(55, 52) |       \
+         GENMASK(15, 12) | GENMASK(6, 0))
+
 #define DEFAULT_PMR_VALUE            0xff
 
+#define LPI_PROP_DEFAULT_PRIO        0xa0
+#define LPI_PROP_RES1                (1 << 1)
+#define LPI_PROP_ENABLED             (1 << 0)
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
--

2.9.0

Quoted 4 lines

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

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

#18 lore raw

Re: [RFC PATCH 03/24] ARM: GICv3 ITS: allocate device and collection table

In reply to Andre Przywara
Quoted 7 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (usually the PCI b/d/f triplet) and an EventID (the MSI payload or interrupt ID) to a pair of LPI number and collection ID, which points to the target CPU.
This mapping is stored in the device and collection tables, which software has to provide for the ITS to use.
Allocate the required memory and hand it the ITS.
We limit the number of devices to cover 4 PCI busses for now.

Thunderx has more than 4 PCI busses

Quoted 29 lines
---
 xen/arch/arm/gic-its.c        | 114 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   5 ++
 xen/include/asm-arm/gic-its.h |  49 +++++++++++++++++-
 3 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +6 -0
index b52dff3..40238a2 100644
@@ -21,6 +21,7 @@
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
@@ -38,6 +39,119 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define BASER_ATTR_MASK                                           \
+        ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
+         (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
+         (0x7UL << GITS_BASER_INNER_CACHEABILITY_SHIFT))
+#define BASER_RO_MASK   (GENMASK(52, 48) | GENMASK(58, 56))

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 56

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
+{
+    uint64_t ret;
+
+    if ( page_bits < 16)
+        return (uint64_t)addr & GENMASK(47, page_bits);
+
+    ret = addr & GENMASK(47, 16);
+    return ret | (addr & GENMASK(51, 48)) >> (48 - 12);

why this mask and shift for?.

Quoted 18 lines
xen/arch/arm/gic-its.c +18 -0
+}
+
+static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
+{
+    uint64_t attr;
+    int entry_size = (regc >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f;
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 84

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    for (pagesz = 0; pagesz < 3; pagesz++)
+    {
+        uint64_t reg;
+        int nr_bytes;
+
+        nr_bytes = ROUNDUP(nr_items * entry_size, BIT(pagesz * 2 + 12));
+        order = get_order_from_bytes(nr_bytes);
+
+        if ( !buffer )
+            buffer = alloc_xenheap_pages(order, 0);

Don't we need to reset to zero all the pages before handing
memory to ITS hw?

Quoted 41 lines
xen/arch/arm/gic-its.c +41 -0
+        if ( !buffer )
+            return -ENOMEM;
+
+        reg  = attr;
+        reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+        reg |= nr_bytes >> (pagesz * 2 + 12);
+        reg |= regc & BASER_RO_MASK;
+        reg |= GITS_BASER_VALID;
+        reg |= encode_phys_addr(virt_to_maddr(buffer), pagesz * 2 + 12);
+
+        writeq_relaxed(reg, basereg);
+        regc = readl_relaxed(basereg);
+
+        /* The host didn't like our attributes, just use what it returned. */
+        if ( (regc & BASER_ATTR_MASK) != attr )
+            attr = regc & BASER_ATTR_MASK;
+
+        /* If the host accepted our page size, we are done. */
+        if ( (reg & (3UL << GITS_BASER_PAGE_SIZE_SHIFT)) == pagesz )
+            return 0;
+
+        /* Check whether our buffer is aligned to the next page size already. */
+        if ( !(virt_to_maddr(buffer) & (BIT(pagesz * 2 + 12 + 2) - 1)) )
+        {
+            free_xenheap_pages(buffer, order);
+            buffer = NULL;
+        }
+    }
+
+    if ( buffer )
+        free_xenheap_pages(buffer, order);
+
+    return -EINVAL;
+}
+
+int gicv3_its_init(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 135

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    for (i = 0; i < 8; i++)
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
+        int type;
+
+        reg = readq_relaxed(basereg);
+        type = (reg >> 56) & 0x7;

define a macro for these constants

Quoted 138 lines
xen/arch/arm/gic-its.c +19 -0
+        switch ( type )
+        {
+        case GITS_BASER_TYPE_NONE:
+            continue;
+        case GITS_BASER_TYPE_DEVICE:
+            /* TODO: find some better way of limiting the number of devices */
+            gicv3_map_baser(basereg, reg, 1024);
+            break;
+        case GITS_BASER_TYPE_COLLECTION:
+            gicv3_map_baser(basereg, reg, NR_CPUS);
+            break;
+        default:
+            continue;
+        }
+    }
+
+    return 0;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
xen/arch/arm/gic-v3.c +5 -0
index 2534aa5..5cf4618 100644
@@ -29,6 +29,7 @@
 #include <xen/irq.h>
 #include <xen/iocap.h>
 #include <xen/sched.h>
+#include <xen/err.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
@@ -1548,6 +1549,7 @@ static int __init gicv3_init(void)
 {
     int res, i;
     uint32_t reg;
+    struct host_its *hw_its;
 
     if ( !cpu_has_gicv3 )
     {
@@ -1603,6 +1605,9 @@ static int __init gicv3_init(void)
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 
+    list_for_each_entry(hw_its, &host_its_list, entry)
+        gicv3_its_init(hw_its);
+
     spin_unlock(&gicv3.lock);
 
     return res;
xen/include/asm-arm/gic-its.h +48 -2
index 48c6c78..589b889 100644
@@ -18,6 +18,47 @@
 #ifndef __ASM_ARM_ITS_H__
 #define __ASM_ARM_ITS_H__
 
+#define LPI_OFFSET      8192
+
+#define GITS_CTLR       (0x000)
+#define GITS_IIDR       (0x004)
+#define GITS_TYPER      (0x008)
+#define GITS_CBASER     (0x080)
+#define GITS_CWRITER    (0x088)
+#define GITS_CREADR     (0x090)
+#define GITS_BASER0     (0x100)
+#define GITS_BASER1     (0x108)
+#define GITS_BASER2     (0x110)
+#define GITS_BASER3     (0x118)
+#define GITS_BASER4     (0x120)
+#define GITS_BASER5     (0x128)
+#define GITS_BASER6     (0x130)
+#define GITS_BASER7     (0x138)
+
+/* Register bits */
+#define GITS_CTLR_ENABLE     0x1
+#define GITS_IIDR_VALUE      0x34c
+
+#define GITS_BASER_VALID                BIT(63)
+#define GITS_BASER_INDIRECT             BIT(62)
+#define GITS_BASER_INNER_CACHEABILITY_SHIFT        59
+#define GITS_BASER_TYPE_SHIFT           56
+#define GITS_BASER_OUTER_CACHEABILITY_SHIFT        53
+#define GITS_BASER_TYPE_NONE            0UL
+#define GITS_BASER_TYPE_DEVICE          1UL
+#define GITS_BASER_TYPE_VCPU            2UL
+#define GITS_BASER_TYPE_CPU             3UL
+#define GITS_BASER_TYPE_COLLECTION      4UL
+#define GITS_BASER_TYPE_RESERVED5       5UL
+#define GITS_BASER_TYPE_RESERVED6       6UL
+#define GITS_BASER_TYPE_RESERVED7       7UL
+#define GITS_BASER_ENTRY_SIZE_SHIFT     48
+#define GITS_BASER_SHAREABILITY_SHIFT   10
+#define GITS_BASER_PAGE_SIZE_SHIFT      8
+#define GITS_BASER_RO_MASK              ((7UL << GITS_BASER_TYPE_SHIFT) | \
+                                        (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
+                                        GITS_BASER_INDIRECT)
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
@@ -27,6 +68,7 @@ struct host_its {
     const struct dt_device_node *dt_node;
     paddr_t addr;
     paddr_t size;
+    void __iomem *its_base;
 };
 
 extern struct list_head host_its_list;
@@ -42,8 +84,9 @@ void gicv3_its_dt_init(const struct dt_device_node *node);
 uint64_t gicv3_lpi_get_proptable(void);
 uint64_t gicv3_lpi_allocate_pendtable(void);
 
-/* Initialize the host structures for LPIs. */
+/* Initialize the host structures for LPIs and the host ITSes. */
 int gicv3_lpi_init_host_lpis(int nr_lpis);
+int gicv3_its_init(struct host_its *hw_its);
 
 #else
 
@@ -62,6 +105,10 @@ static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
 {
     return 0;
 }
+static inline int gicv3_its_init(struct host_its *hw_its)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
--

2.9.0

Quoted 4 lines

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

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

#26 lore raw

Re: [RFC PATCH 04/24] ARM: GICv3 ITS: map ITS command buffer

In reply to Andre Przywara
Quoted 33 lines · Andre Przywara

Instead of directly manipulating the tables in memory, an ITS driver sends commands via a ring buffer to the ITS h/w to create or alter the LPI mappings.
Allocate memory for that buffer and tell the ITS about it to be able to send ITS commands.

---
 xen/arch/arm/gic-its.c        | 25 +++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  1 +
 2 files changed, 26 insertions(+)
xen/arch/arm/gic-its.c +6 -0
index 40238a2..c8a7a7e 100644
@@ -18,6 +18,7 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/err.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
@@ -56,6 +57,26 @@ static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
     return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
 }
 
+static void *gicv3_map_cbaser(void __iomem *cbasereg)
+{
+    uint64_t attr, reg;
+    void *buffer;
+

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 74

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    buffer = alloc_xenheap_pages(0, 0);
+    if ( !buffer )
+        return ERR_PTR(-ENOMEM);
+
+    /* We use exactly one 4K page, so the "Size" field is 0. */
+    reg = attr | BIT(63) | (virt_to_maddr(buffer) & GENMASK(51, 12));

Isn't GENMASK(47, 12)?. Am I referring to wrong spec?

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

#75 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara
Quoted 178 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-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +146 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#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-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    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
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * 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-its.c line 156

Quoted 10 lines
xen/arch/arm/vgic-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);

GITS_TYPER.HCC is not set. Should be max vcpus of the domain GITS_TYPER.ID_bits are also just set to 15.

Quoted 19 lines
xen/arch/arm/vgic-its.c new file +19 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

xen/arch/arm/vgic-its.c line 185

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        *r = vgic_reg64_extract(its->baser1, info);
+        break;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0, info);
+        break;
+    case VREG32(GICD_PIDR2):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GICV3_GICD_PIDR2, info);
+        break;

missing default

xen/arch/arm/vgic-its.c line 190

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+    }
+
+    return 1;
+
+bad_width:

print would be helpful

Quoted 57 lines
xen/arch/arm/vgic-its.c new file +57 -0
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+/******************************
+ * ITS registers write access *
+ ******************************/
+
+static int its_baser_table_size(uint64_t baser)
+{
+    int page_size = 0;
+
+    switch ( (baser >> 8) & 3 )
+    {
+    case 0: page_size = SZ_4K; break;
+    case 1: page_size = SZ_16K; break;
+    case 2:
+    case 3: page_size = SZ_64K; break;
+    }
+
+    return page_size * ((baser & GENMASK(7, 0)) + 1);
+}
+
+static int its_baser_nr_entries(uint64_t baser)
+{
+    int entry_size = ((baser & GENMASK(52, 48)) >> 48) + 1;
+
+    return its_baser_table_size(baser) / entry_size;
+}
+
+static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,
+                                  register_t r, void *priv)
+{
+    struct domain *d = v->domain;
+    struct virt_its *its = priv;
+    uint64_t reg;
+    uint32_t ctlr;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        ctlr = its->enabled ? GITS_CTLR_ENABLE : 0;
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+       vgic_reg32_update(&ctlr, r, info);
+       its->enabled = ctlr & GITS_CTLR_ENABLE;
+       /* TODO: trigger something ... */
+        return 1;
+    case VREG32(GITS_IIDR):
+        goto write_ignore_32;
+    case VREG32(GITS_TYPER):
+        goto write_ignore_32;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )

xen/arch/arm/vgic-its.c line 257

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+            return 1;
+
+        reg = its->cbaser;
+        vgic_reg64_update(&reg, r, info);
+        /* TODO: sanitise! */
+        its->cbaser = reg;
+
+        if ( reg & BIT(63) )
+        {
+            its->cmdbuf = map_guest_pages(d, reg & GENMASK(51, 12), 1);

Only one page of guest cmd queue is mapped. After cwriter
moving beyond 1 page,
panic is observed. Map all the guest pages

xen/arch/arm/vgic-its.c line 261

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->cmdbuf, 1);

Same here.

Quoted 178 lines
xen/arch/arm/vgic-its.c new file +117 -0
+            its->cmdbuf = NULL;
+        }
+
+       return 1;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        reg = its->cwriter;
+        vgic_reg64_update(&reg, r, info);
+        vgic_its_handle_cmds(d, its, reg);
+        return 1;
+    case VREG64(GITS_CREADR):
+        goto write_ignore_64;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->baser0;
+        vgic_reg64_update(&reg, r, info);
+
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint64_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT;
+        /* TODO: sanitise! */
+        /* TODO: locking(?) */
+
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->dev_table = map_guest_pages(d,
+                                             get_baser_phys_addr(reg),
+                                             its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = its_baser_nr_entries(reg);
+            memset(its->dev_table, 0, its->max_devices * sizeof(uint64_t));
+        }
+        else
+        {
+            unmap_guest_pages(its->dev_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = 0;
+        }
+
+        its->baser0 = reg;
+        return 1;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->baser1;
+        vgic_reg64_update(&reg, r, info);
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint16_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT;
+        /* TODO: sanitise! */
+
+        /* TODO: sort out locking */
+        /* TODO: repeated calls: free old mapping */
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->coll_table = map_guest_pages(d, get_baser_phys_addr(reg),
+                                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = its_baser_nr_entries(reg);
+            memset(its->coll_table, 0xff,
+                   its->max_collections * sizeof(uint16_t));
+        }
+        else
+        {
+            unmap_guest_pages(its->coll_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = 0;
+        }
+        its->baser1 = reg;
+        return 1;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        goto write_ignore_64;
+    default:
+        gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS register 0x%lx\n",
+                 info->gpa & 0xffff);
+        return 0;
+    }
+
+    return 1;
+
+write_ignore_64:
+    if ( ! vgic_reg64_check_access(info->dabt) ) goto bad_width;
+    return 1;
+
+write_ignore_32:
+    if ( info->dabt.size != DABT_WORD ) goto bad_width;
+    return 1;
+
+bad_width:
+    printk(XENLOG_G_ERR "%pv vGICR: bad read width %d r%d offset %#08lx\n",
+           v, info->dabt.size, info->dabt.reg, info->gpa & 0xffff);
+
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+static const struct mmio_handler_ops vgic_its_mmio_handler = {
+    .read  = vgic_v3_its_mmio_read,
+    .write = vgic_v3_its_mmio_write,
+};
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic-v3.c +0 -9
index 8fe8386..aa53a1e 100644
@@ -158,15 +158,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 -1
index da5fb77..6a91f5b 100644
@@ -147,6 +147,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)
 
@@ -190,6 +200,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__ */
 
 /*
--

2.9.0

Quoted 4 lines

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

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

#43 lore raw

Re: [RFC PATCH 07/24] ARM: GICv3 ITS: introduce device mapping

In reply to Andre Przywara
Quoted 43 lines · Andre Przywara

The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host.
For this we have to map each device that Dom0 may request to a host ITS device with the same identifier.
Allocate the respective memory and enter each device into a list to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-its.c        | 90 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h | 16 ++++++++
 2 files changed, 106 insertions(+)
xen/arch/arm/gic-its.c +22 -0
index 2140e4a..bf1f5b5 100644
@@ -168,6 +168,94 @@ static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             int size, uint64_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = size & GENMASK(4, 0);
+    cmd[2] = itt_addr & GENMASK(51, 8);
+    if ( valid )
+        cmd[2] |= BIT(63);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid)
+{
+    void *itt_addr = NULL;
+    struct its_devices *dev, *temp;
+    bool reuse_dev = false;
+

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 202

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    list_for_each_entry_safe(dev, temp, &hw_its->its_devices, entry)
+    {
+        if ( (dev->d->domain_id != d->domain_id) || (dev->devid != devid) )
+            continue;
+
+        its_send_cmd_mapd(hw_its, dev->devid, 0, 0, false);
+        xfree(dev->itt_addr);
+        if ( !valid )
+        {
+            xfree(dev);

xfree() should be done after list_del()

Quoted 21 lines
xen/arch/arm/gic-its.c +21 -0
+            list_del(&dev->entry);
+
+            return 0;
+        }
+
+        reuse_dev = true;
+        break;
+    }
+
+    if ( !valid )
+        return 0;
+
+    itt_addr = _xmalloc(BIT(bits) * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        return -ENOMEM;
+
+    if ( !reuse_dev )
+    {
+        dev = xmalloc(struct its_devices);
+        if ( !dev )
+            return -ENOMEM;

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 233

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+        list_add_tail(&dev->entry, &hw_its->its_devices);
+    }
+
+    dev->itt_addr = itt_addr;
+    dev->d = d;
+    dev->devid = devid;
+
+    return its_send_cmd_mapd(hw_its, devid, bits - 1,
+                             itt_addr ? virt_to_maddr(itt_addr) : 0, true);

check on itt_addr is redundant

Quoted 8 lines
xen/arch/arm/gic-its.c +8 -0
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+int its_remove_domain(struct domain *d)
+{
+    struct host_its *its;
+    struct its_devices *dev, *temp;
+

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 251

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        list_for_each_entry_safe(dev, temp, &its->its_devices, entry)
+        {
+            if ( dev->d->domain_id != d->domain_id )
+                continue;
+
+            its_send_cmd_mapd(its, dev->devid, 0, 0, false);
+            xfree(dev->itt_addr);
+            xfree(dev);

xfree() should be done after list_del(

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 253

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+            list_del(&dev->entry);
+        }

This code is same as above. Can be moved to a separate function?

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 388

Quoted 13 lines
xen/arch/arm/gic-its.c +6 -0
+    }
+
+    return 0;
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 void gicv3_its_setup_collection(int cpu)
 {
@@ -297,6 +385,7 @@ int gicv3_its_init(struct host_its *hw_its)
 
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     hw_its->pta = reg & GITS_TYPER_PTA;
+    hw_its->itte_size = ((reg >> 4) & 0xf) + 1;

can define a macro for these constants

Quoted 56 lines
xen/arch/arm/gic-its.c +1 -0
     for (i = 0; i < 8; i++)
     {
@@ -520,6 +609,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->size = size;
         its_data->dt_node = its;
         spin_lock_init(&its_data->cmd_lock);
+        INIT_LIST_HEAD(&its_data->its_devices);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/include/asm-arm/gic-its.h +16 -1
index 512a388..4e9841a 100644
@@ -79,6 +79,13 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
+struct its_devices {
+    struct list_head entry;
+    struct domain *d;
+    void *itt_addr;
+    int devid;
+};
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -88,6 +95,8 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    struct list_head its_devices;
+    int itte_size;
     bool pta;
 };
 
@@ -114,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* 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.
+ */
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid);
+
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
--

2.9.0

Quoted 4 lines

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

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

#47 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara
Quoted 25 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +0 -0
index 63c744a..ebe4035 100644

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 510

Quoted 5 lines
xen/arch/arm/gic.c +2 -0
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )

Can define something line is_lpi(irq) and use it everywhere

Quoted 70 lines
xen/arch/arm/gic.c +1 -0
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +19 -2
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
+    if ( virq >= 8192 )
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    /* TODO: locking! */
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

With this approach of allocating pending_irq on demand, if the
pending_lpi_list
is at n position then it iterates for long time to find pending_irq entry. This will increase LPI injection time to domain.

Why can't we use btree?

Quoted 66 lines
xen/arch/arm/vgic.c +24 -1
+
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);
+
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +0 -0
index 4e9841a..1f881c0 100644

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 142

Quoted 7 lines
xen/include/asm-arm/gic-its.h +4 -0
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;

Why lpi priority is fixed?. can't we use domain set lpi priority?

Quoted 49 lines
xen/include/asm-arm/gic-its.h +6 -0
+}
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -1
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
--

2.9.0

Quoted 4 lines

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

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

#60 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara
Quoted 144 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +83 -9
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #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 <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

I think this file is not right place to put this generic function

Quoted 13 lines
xen/arch/arm/vgic-v3.c +13 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;

xen/arch/arm/vgic-v3.c read_unknown: · line 439

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);

check return value of this function

xen/arch/arm/vgic-v3.c read_unknown: · line 451

Quoted 12 lines
xen/arch/arm/vgic-v3.c +12 -0
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);
+    }
+
+    ptr = vmap(pages, nr_pages);
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)

same here. Can be put in generic file p2m.c?

xen/arch/arm/vgic-v3.c read_unknown: · line 460

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);

can use _pa()

Quoted 27 lines
xen/arch/arm/vgic-v3.c +12 -3
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;
+

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

Quoted 10 lines
xen/arch/arm/vgic-v3.c +9 -0
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;
+
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;

should be validated against HOST_LPIS?

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

Quoted 11 lines
xen/arch/arm/vgic-v3.c +8 -2
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

check on VGIC_V3_LPIS_ENABLED is required

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

Quoted 6 lines
xen/arch/arm/vgic-v3.c +6 -0
+       reg = v->arch.vgic.rdist_pendbase;
+       vgic_reg64_update(&reg, r, info);
+       reg = sanitize_pendbaser(reg);
+       v->arch.vgic.rdist_pendbase = reg;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);

why only 16 pages are unmapped?

Quoted 92 lines
xen/arch/arm/vgic-v3.c +3 -0
+       v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);
+       return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +4 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +6 -1
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +9 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +3 -1
index 4e29ba6..2b216cc 100644
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*
--

2.9.0

Quoted 4 lines

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

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

#99 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Andre Przywara
Quoted 68 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +40 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )

xen/arch/arm/gic-its.c uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id) · line 720

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }

AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

Quoted 76 lines
xen/arch/arm/gic-its.c +8 -0
+
+    /* Tell all ITSes that they should update the property table for CPU 0,
+     * which is where we map all LPIs to.
+     */
+    list_for_each_entry(its, &host_its_list, entry)
+        its_send_cmd_invall(its, 0);
+}
+
 void gicv3_lpi_set_enable(struct host_its *its,
                           uint32_t deviceid, uint32_t eventid,
                           uint32_t host_lpi, bool enabled)
xen/arch/arm/vgic-its.c +30 -0
index 74da8fc..1e429b7 100644
@@ -294,6 +294,33 @@ out_unlock:
     return ret;
 }
 
+/* INVALL updates the per-LPI configuration status for every LPI mapped to
+ * this redistributor. For the guest side we don't need to update anything,
+ * as we always refer to the actual table for the enabled bit and the
+ * priority.
+ * Enabling or disabling a virtual LPI however needs to be propagated to
+ * the respective host LPI. Instead of iterating over all mapped LPIs in our
+ * emulated GIC (which is expensive due to the required on-demand mapping),
+ * we iterate over all mapped _host_ LPIs and filter for those which are
+ * forwarded to this virtual redistributor.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !vcpu )
+        return -1;
+
+    gicv3_lpi_update_configurations(vcpu, its->d->arch.vgic.proptable);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -515,6 +542,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;
xen/include/asm-arm/gic-its.h +2 -1
index 2cdb3e1..ba6b2d5 100644
@@ -146,6 +146,8 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
 
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable);
+
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
--

2.9.0

Quoted 4 lines

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

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

#9 lore raw

Re: [RFC PATCH 02/24] ARM: GICv3: allocate LPI pending and property table

In reply to Andre Przywara

Hi Andre,

Sorry for the late reply, I'll try to be faster for the next rounds of review. The patch looks good for a first iteration. Some comments below.

Quoted 61 lines · Andre Przywara

The ARM GICv3 ITS provides a new kind of interrupt called LPIs. The pending bits and the configuration data (priority, enable bits) for those LPIs are stored in tables in normal memory, which software has to provide to the hardware.
Allocate the required memory, initialize it and hand it over to each ITS. We limit the number of LPIs we use with a compile time constant to avoid wasting memory.

---
 xen/arch/arm/Kconfig              |  6 ++++
 xen/arch/arm/efi/efi-boot.h       |  1 -
 xen/arch/arm/gic-its.c            | 76 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             | 27 ++++++++++++++
 xen/include/asm-arm/cache.h       |  4 +++
 xen/include/asm-arm/gic-its.h     | 22 +++++++++++-
 xen/include/asm-arm/gic_v3_defs.h | 48 ++++++++++++++++++++++++-
 7 files changed, 181 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +6 -0
index 9fe3b8e..66e2bb8 100644
@@ -50,6 +50,12 @@ config HAS_ITS
         depends on ARM_64
         depends on HAS_GICV3
 
+config HOST_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+
 config ALTERNATIVE
 	bool
 
xen/arch/arm/efi/efi-boot.h +0 -1
index 045d6ce..dc64aec 100644
@@ -10,7 +10,6 @@
 #include "efi-dom0.h"
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
-void __flush_dcache_area(const void *vaddr, unsigned long size);
 
 #define DEVICE_TREE_GUID \
 {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}
xen/arch/arm/gic-its.c +1 -0
index 0f42a77..b52dff3 100644
@@ -20,10 +20,86 @@
 #include <xen/lib.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
+#include <asm/p2m.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
 

xen/arch/arm/gic-its.c line 37

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+/* Global state */
+static struct {
+    uint8_t *lpi_property;
+    int host_lpi_bits;
+} lpi_data;
+
+/* Pending table for each redistributor */
+static DEFINE_PER_CPU(void *, pending_table);
+
+#define MAX_HOST_LPI_BITS                                                \

To avoid confusion, I would call this MAX_PHYS_LPI_BITS

xen/arch/arm/gic-its.c line 39

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+        min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
+#define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)

And this MAX_PHYS_LPIS

xen/arch/arm/gic-its.c line 44

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    uint64_t reg, attr;
+    void *pendtable;

I would introduce a check to make sure that this_cpu(pending_table) == NULL.

xen/arch/arm/gic-its.c

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;
+
+    /*
+     * The pending table holds one bit per LPI, so we need three bits less
+     * than the number of LPI_BITs.

Why 3 bit less? Please add more info on how you came up with 3.

xen/arch/arm/gic-its.c line 52

Quoted 2 lines
xen/arch/arm/gic-its.c +1 -0
        But the alignment requirement from the
+     * ITS is 64K, so make order at least 16 (-12).

Does it need to be 64K aligned or does it need to be at least 64K in size? That makes a big difference. If it just needs to be 64K aligned, you can do that with xmalloc.

xen/arch/arm/gic-its.c line 55

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+     */
+    pendtable = alloc_xenheap_pages(MAX(lpi_data.host_lpi_bits - 3, 16) - 12, 0);

Shouldn't we be using MAX_HOST_LPI_BITS instead of lpi_data.host_lpi_bits to make this calculation?

xen/arch/arm/gic-its.c line 59

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+    if ( !pendtable )
+        return 0;
+
+    memset(pendtable, 0, BIT(lpi_data.host_lpi_bits - 3));

flush_dcache?

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+    this_cpu(pending_table) = pendtable;
+
+    reg  = attr | GICR_PENDBASER_PTZ;
+    reg |= virt_to_maddr(pendtable) & GENMASK(51, 16);
+
+    return reg;

xen/arch/arm/gic-its.c line 75

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+}
+
+uint64_t gicv3_lpi_get_proptable()
+{
+    uint64_t attr;
+    static uint64_t reg = 0;
+
+    /* The property table is shared across all redistributors. */
+    if ( reg )
+        return reg;

Can't you just use lpi_data.lpi_property != NULL instead of introducing a new static local variable?

xen/arch/arm/gic-its.c line 81

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;
+
+    lpi_data.lpi_property = alloc_xenheap_pages(MAX_HOST_LPI_BITS - 12, 0);

Please add a comment on how the order is calculated.

Quoted 29 lines
xen/arch/arm/gic-its.c +21 -0
+    if ( !lpi_data.lpi_property )
+        return 0;
+
+    memset(lpi_data.lpi_property, GIC_PRI_IRQ | LPI_PROP_RES1, MAX_HOST_LPIS);
+    __flush_dcache_area(lpi_data.lpi_property, MAX_HOST_LPIS);
+
+    reg  = attr | ((MAX_HOST_LPI_BITS - 1) << 0);
+    reg |= virt_to_maddr(lpi_data.lpi_property) & GENMASK(51, 12);
+
+    return reg;
+}
+
+int gicv3_lpi_init_host_lpis(int lpi_bits)
+{
+    lpi_data.host_lpi_bits = lpi_bits;
+
+    printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/gic-v3.c +0 -0
index 238da84..2534aa5 100644

xen/arch/arm/gic-v3.c static void __init gicv3_dist_init(void) · line 550

Quoted 5 lines
xen/arch/arm/gic-v3.c +2 -0
@@ -546,6 +546,9 @@ static void __init gicv3_dist_init(void)
     type = readl_relaxed(GICD + GICD_TYPER);
     nr_lines = 32 * ((type & GICD_TYPE_LINES) + 1);
 
+    if ( type & GICD_TYPE_LPIS )
+        gicv3_lpi_init_host_lpis(((type >> GICD_TYPE_ID_BITS_SHIFT) & 0x1f) + 1);

Please #define a mask instead of using 0x1f

Quoted 13 lines
xen/arch/arm/gic-v3.c +6 -0
+
     printk("GICv3: %d lines, (IID %8.8x).\n",
            nr_lines, readl_relaxed(GICD + GICD_IIDR));
 
@@ -615,6 +618,26 @@ static int gicv3_enable_redist(void)
 
     return 0;
 }
+static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 635

Quoted 10 lines
xen/arch/arm/gic-v3.c +10 -0
+    if ( list_empty(&host_its_list) )
+        return;
+
+    /* Make sure LPIs are disabled before setting up the BASERs. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    writel_relaxed(reg & ~GICR_CTLR_ENABLE_LPIS, rdist_base + GICR_CTLR);
+
+    table_reg = gicv3_lpi_allocate_pendtable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);

Maybe we want to return in case table_reg == NULL ?

Quoted 146 lines
xen/arch/arm/gic-v3.c +8 -0
+    table_reg = gicv3_lpi_get_proptable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);
+}
 
 static int __init gicv3_populate_rdist(void)
 {
@@ -658,6 +681,10 @@ static int __init gicv3_populate_rdist(void)
             if ( (typer >> 32) == aff )
             {
                 this_cpu(rbase) = ptr;
+
+                if ( typer & GICR_TYPER_PLPIS )
+                    gicv3_rdist_init_lpis(ptr);
+
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
                 return 0;
xen/include/asm-arm/cache.h +4 -0
index 2de6564..af96eee 100644
@@ -7,6 +7,10 @@
 #define L1_CACHE_SHIFT  (CONFIG_ARM_L1_CACHE_SHIFT)
 #define L1_CACHE_BYTES  (1 << L1_CACHE_SHIFT)
 
+#ifndef __ASSEMBLY__
+void __flush_dcache_area(const void *vaddr, unsigned long size);
+#endif
+
 #define __read_mostly __section(".data.read_mostly")
 
 #endif
xen/include/asm-arm/gic-its.h +21 -1
index 2f5c51c..48c6c78 100644
@@ -36,12 +36,32 @@ extern struct list_head host_its_list;
 /* Parse the host DT and pick up all host ITSes. */
 void gicv3_its_dt_init(const struct dt_device_node *node);
 
+/* Allocate and initialize tables for each host redistributor.
+ * Returns the respective {PROP,PEND}BASER register value.
+ */
+uint64_t gicv3_lpi_get_proptable(void);
+uint64_t gicv3_lpi_allocate_pendtable(void);
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(int nr_lpis);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
 {
 }
-
+static inline uint64_t gicv3_lpi_get_proptable(void)
+{
+    return 0;
+}
+static inline uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    return 0;
+}
+static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
xen/include/asm-arm/gic_v3_defs.h +47 -1
index 6bd25a5..da5fb77 100644
@@ -44,7 +44,8 @@
 #define GICC_SRE_EL2_ENEL1           (1UL << 3)
 
 /* Additional bits in GICD_TYPER defined by GICv3 */
-#define GICD_TYPE_ID_BITS_SHIFT 19
+#define GICD_TYPE_ID_BITS_SHIFT      19
+#define GICD_TYPE_LPIS               (1U << 17)
 
 #define GICD_CTLR_RWP                (1UL << 31)
 #define GICD_CTLR_ARE_NS             (1U << 4)
@@ -95,12 +96,57 @@
 #define GICR_IGRPMODR0               (0x0D00)
 #define GICR_NSACR                   (0x0E00)
 
+#define GICR_CTLR_ENABLE_LPIS        (1U << 0)
 #define GICR_TYPER_PLPIS             (1U << 0)
 #define GICR_TYPER_VLPIS             (1U << 1)
 #define GICR_TYPER_LAST              (1U << 4)
 
+#define GIC_BASER_CACHE_nCnB         0ULL
+#define GIC_BASER_CACHE_SameAsInner  0ULL
+#define GIC_BASER_CACHE_nC           1ULL
+#define GIC_BASER_CACHE_RaWt         2ULL
+#define GIC_BASER_CACHE_RaWb         3ULL
+#define GIC_BASER_CACHE_WaWt         4ULL
+#define GIC_BASER_CACHE_WaWb         5ULL
+#define GIC_BASER_CACHE_RaWaWt       6ULL
+#define GIC_BASER_CACHE_RaWaWb       7ULL
+#define GIC_BASER_CACHE_MASK         7ULL
+#define GIC_BASER_NonShareable       0ULL
+#define GIC_BASER_InnerShareable     1ULL
+#define GIC_BASER_OuterShareable     2ULL
+
+#define GICR_PROPBASER_SHAREABILITY_SHIFT               10
+#define GICR_PROPBASER_INNER_CACHEABILITY_SHIFT         7
+#define GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT         56
+#define GICR_PROPBASER_SHAREABILITY_MASK                     \
+        (3UL << GICR_PROPBASER_SHAREABILITY_SHIFT)
+#define GICR_PROPBASER_INNER_CACHEABILITY_MASK               \
+        (7UL << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT)
+#define GICR_PROPBASER_OUTER_CACHEABILITY_MASK               \
+        (7UL << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT)
+#define PROPBASER_RES0_MASK                                  \
+        (GENMASK(63, 59) | GENMASK(55, 52) | GENMASK(6, 5))
+
+#define GICR_PENDBASER_SHAREABILITY_SHIFT               10
+#define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT         7
+#define GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT         56
+#define GICR_PENDBASER_SHAREABILITY_MASK                     \
+	(3UL << GICR_PENDBASER_SHAREABILITY_SHIFT)
+#define GICR_PENDBASER_INNER_CACHEABILITY_MASK               \
+	(7UL << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT)
+#define GICR_PENDBASER_OUTER_CACHEABILITY_MASK               \
+        (7UL << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT)
+#define GICR_PENDBASER_PTZ                              BIT(62)
+#define PENDBASER_RES0_MASK                                  \
+        (BIT(63) | GENMASK(61, 59) | GENMASK(55, 52) |       \
+         GENMASK(15, 12) | GENMASK(6, 0))
+
 #define DEFAULT_PMR_VALUE            0xff
 
+#define LPI_PROP_DEFAULT_PRIO        0xa0
+#define LPI_PROP_RES1                (1 << 1)
+#define LPI_PROP_ENABLED             (1 << 0)
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
Signature · 1 line
2.9.0

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

#2 lore raw

Re: [RFC PATCH 01/24] ARM: GICv3 ITS: parse and store ITS subnodes from hardware DT

In reply to Andre Przywara
Quoted 6 lines · Andre Przywara

Parse the DT GIC subnodes to find every ITS MSI controller the hardware offers. Store that information in a list to both propagate all of them later to Dom0, but also to be able to iterate over all ITSes. This introduces an ITS Kconfig option.

Looks good to me

Quoted 203 lines
 xen/arch/arm/Kconfig          |  5 ++++
 xen/arch/arm/Makefile         |  1 +
 xen/arch/arm/gic-its.c        | 67 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  6 ++++
 xen/include/asm-arm/gic-its.h | 57 ++++++++++++++++++++++++++++++++++++
 5 files changed, 136 insertions(+)
 create mode 100644 xen/arch/arm/gic-its.c
 create mode 100644 xen/include/asm-arm/gic-its.h
xen/arch/arm/Kconfig +5 -0
index 797c91f..9fe3b8e 100644
@@ -45,6 +45,11 @@ config ACPI
 config HAS_GICV3
 	bool
 
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on ARM_64
+        depends on HAS_GICV3
+
 config ALTERNATIVE
 	bool
 
xen/arch/arm/Makefile +1 -0
index 64fdf41..c2c4daa 100644
@@ -18,6 +18,7 @@ obj-$(EARLY_PRINTK) += early_printk.o
 obj-y += gic.o
 obj-y += gic-v2.o
 obj-$(CONFIG_HAS_GICV3) += gic-v3.o
+obj-$(CONFIG_HAS_ITS) += gic-its.o
 obj-y += guestcopy.o
 obj-y += hvm.o
 obj-y += io.o
xen/arch/arm/gic-its.c new file +67 -0
new file mode 100644
index 0000000..0f42a77
@@ -0,0 +1,67 @@
+/*
+ * xen/arch/arm/gic-its.c
+ *
+ * ARM Generic Interrupt Controller ITS support
+ *
+ * Copyright (C) 2016 - 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#include <xen/config.h>
+#include <xen/lib.h>
+#include <xen/device_tree.h>
+#include <xen/libfdt/libfdt.h>
+#include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
+
+void gicv3_its_dt_init(const struct dt_device_node *node)
+{
+    const struct dt_device_node *its = NULL;
+    struct host_its *its_data;
+
+    /*
+     * Check for ITS MSI subnodes. If any, add the ITS register
+     * frames to the ITS list.
+     */
+    dt_for_each_child_node(node, its)
+    {
+        paddr_t addr, size;
+
+        if ( !dt_device_is_compatible(its, "arm,gic-v3-its") )
+            continue;
+
+        if ( dt_device_get_address(its, 0, &addr, &size) )
+            panic("GICv3: Cannot find a valid ITS frame address");
+
+        its_data = xzalloc(struct host_its);
+        if ( !its_data )
+            panic("GICv3: Cannot allocate memory for ITS frame");
+
+        its_data->addr = addr;
+        its_data->size = size;
+        its_data->dt_node = its;
+
+        printk("GICv3: Found ITS @0x%lx\n", addr);
+
+        list_add_tail(&its_data->entry, &host_its_list);
+    }
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/gic-v3.c +6 -0
index b8be395..238da84 100644
@@ -43,9 +43,12 @@
 #include <asm/device.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 #include <asm/cpufeature.h>
 #include <asm/acpi.h>
 
+LIST_HEAD(host_its_list);
+
 /* Global state */
 static struct {
     void __iomem *map_dbase;  /* Mapped address of distributor registers */
@@ -1229,6 +1232,9 @@ static void __init gicv3_dt_init(void)
 
     dt_device_get_address(node, 1 + gicv3.rdist_count + 2,
                           &vbase, &vsize);
+
+    /* Check for ITS child nodes and build the host ITS list accordingly. */
+    gicv3_its_dt_init(node);
 }
 
 static int gicv3_iomem_deny_access(const struct domain *d)
xen/include/asm-arm/gic-its.h new file +57 -0
new file mode 100644
index 0000000..2f5c51c
@@ -0,0 +1,57 @@
+/*
+ * ARM GICv3 ITS support
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#ifndef __ASM_ARM_ITS_H__
+#define __ASM_ARM_ITS_H__
+
+#ifndef __ASSEMBLY__
+#include <xen/device_tree.h>
+
+/* data structure for each hardware ITS */
+struct host_its {
+    struct list_head entry;
+    const struct dt_device_node *dt_node;
+    paddr_t addr;
+    paddr_t size;
+};
+
+extern struct list_head host_its_list;
+
+#ifdef CONFIG_HAS_ITS
+
+/* Parse the host DT and pick up all host ITSes. */
+void gicv3_its_dt_init(const struct dt_device_node *node);
+
+#else
+
+static inline void gicv3_its_dt_init(const struct dt_device_node *node)
+{
+}
+
+#endif /* CONFIG_HAS_ITS */
+
+#endif /* __ASSEMBLY__ */
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 1 line
2.9.0

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

#20 lore raw

Re: [RFC PATCH 03/24] ARM: GICv3 ITS: allocate device and collection table

In reply to Andre Przywara
Quoted 39 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (usually the PCI b/d/f triplet) and an EventID (the MSI payload or interrupt ID) to a pair of LPI number and collection ID, which points to the target CPU.
This mapping is stored in the device and collection tables, which software has to provide for the ITS to use.
Allocate the required memory and hand it the ITS.
We limit the number of devices to cover 4 PCI busses for now.

---
 xen/arch/arm/gic-its.c        | 114 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   5 ++
 xen/include/asm-arm/gic-its.h |  49 +++++++++++++++++-
 3 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +9 -0
index b52dff3..40238a2 100644
@@ -21,6 +21,7 @@
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
@@ -38,6 +39,119 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define BASER_ATTR_MASK                                           \
+        ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
+         (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
+         (0x7UL << GITS_BASER_INNER_CACHEABILITY_SHIFT))
+#define BASER_RO_MASK   (GENMASK(52, 48) | GENMASK(58, 56))
+
+static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
+{

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 59

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    uint64_t ret;
+
+    if ( page_bits < 16)
+        return (uint64_t)addr & GENMASK(47, page_bits);
+
+    ret = addr & GENMASK(47, 16);
+    return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
+}
+
+static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)

Shouldn't this be called its_map_baser?

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 62

Quoted 3 lines
xen/arch/arm/gic-its.c +3 -0
+{
+    uint64_t attr;
+    int entry_size = (regc >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f;

The spec says "This field is read-only and specifies the number of bytes per entry, minus one." Do we need to increment it by 1?

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 74

Quoted 12 lines
xen/arch/arm/gic-its.c +12 -0
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */

Is this really the best way to do it? Can't we assume ITS supports 4K, given that Xen requires 4K pages at the moment? Is it actually possible to find hardware that supports 4K but with an ITS that only support 64K or 16K pages? It seems insane to me. Otherwise can't we probe the page size somehow?

Quoted 45 lines
xen/arch/arm/gic-its.c +45 -0
+    for (pagesz = 0; pagesz < 3; pagesz++)
+    {
+        uint64_t reg;
+        int nr_bytes;
+
+        nr_bytes = ROUNDUP(nr_items * entry_size, BIT(pagesz * 2 + 12));
+        order = get_order_from_bytes(nr_bytes);
+
+        if ( !buffer )
+            buffer = alloc_xenheap_pages(order, 0);
+        if ( !buffer )
+            return -ENOMEM;
+
+        reg  = attr;
+        reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+        reg |= nr_bytes >> (pagesz * 2 + 12);
+        reg |= regc & BASER_RO_MASK;
+        reg |= GITS_BASER_VALID;
+        reg |= encode_phys_addr(virt_to_maddr(buffer), pagesz * 2 + 12);
+
+        writeq_relaxed(reg, basereg);
+        regc = readl_relaxed(basereg);
+
+        /* The host didn't like our attributes, just use what it returned. */
+        if ( (regc & BASER_ATTR_MASK) != attr )
+            attr = regc & BASER_ATTR_MASK;
+
+        /* If the host accepted our page size, we are done. */
+        if ( (reg & (3UL << GITS_BASER_PAGE_SIZE_SHIFT)) == pagesz )
+            return 0;
+
+        /* Check whether our buffer is aligned to the next page size already. */
+        if ( !(virt_to_maddr(buffer) & (BIT(pagesz * 2 + 12 + 2) - 1)) )
+        {
+            free_xenheap_pages(buffer, order);
+            buffer = NULL;
+        }
+    }
+
+    if ( buffer )
+        free_xenheap_pages(buffer, order);
+
+    return -EINVAL;
+}
+

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 129

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+int gicv3_its_init(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    for (i = 0; i < 8; i++)

Code style. Unfortunately we don't have a script to check, but please refer to CODING_STYLE. I'd prefer if every number was #define'ed, including `8' (something like GITS_BASER_MAX).

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 135

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
+        int type;
+
+        reg = readq_relaxed(basereg);
+        type = (reg >> 56) & 0x7;

Please #define 56 and 0x7

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 142

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+        switch ( type )
+        {
+        case GITS_BASER_TYPE_NONE:
+            continue;
+        case GITS_BASER_TYPE_DEVICE:
+            /* TODO: find some better way of limiting the number of devices */
+            gicv3_map_baser(basereg, reg, 1024);

An hardcoded max value might be OK, but please #define it.

Quoted 130 lines
xen/arch/arm/gic-its.c +12 -0
+            break;
+        case GITS_BASER_TYPE_COLLECTION:
+            gicv3_map_baser(basereg, reg, NR_CPUS);
+            break;
+        default:
+            continue;
+        }
+    }
+
+    return 0;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
xen/arch/arm/gic-v3.c +5 -0
index 2534aa5..5cf4618 100644
@@ -29,6 +29,7 @@
 #include <xen/irq.h>
 #include <xen/iocap.h>
 #include <xen/sched.h>
+#include <xen/err.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
@@ -1548,6 +1549,7 @@ static int __init gicv3_init(void)
 {
     int res, i;
     uint32_t reg;
+    struct host_its *hw_its;
 
     if ( !cpu_has_gicv3 )
     {
@@ -1603,6 +1605,9 @@ static int __init gicv3_init(void)
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 
+    list_for_each_entry(hw_its, &host_its_list, entry)
+        gicv3_its_init(hw_its);
+
     spin_unlock(&gicv3.lock);
 
     return res;
xen/include/asm-arm/gic-its.h +48 -1
index 48c6c78..589b889 100644
@@ -18,6 +18,47 @@
 #ifndef __ASM_ARM_ITS_H__
 #define __ASM_ARM_ITS_H__
 
+#define LPI_OFFSET      8192
+
+#define GITS_CTLR       (0x000)
+#define GITS_IIDR       (0x004)
+#define GITS_TYPER      (0x008)
+#define GITS_CBASER     (0x080)
+#define GITS_CWRITER    (0x088)
+#define GITS_CREADR     (0x090)
+#define GITS_BASER0     (0x100)
+#define GITS_BASER1     (0x108)
+#define GITS_BASER2     (0x110)
+#define GITS_BASER3     (0x118)
+#define GITS_BASER4     (0x120)
+#define GITS_BASER5     (0x128)
+#define GITS_BASER6     (0x130)
+#define GITS_BASER7     (0x138)
+
+/* Register bits */
+#define GITS_CTLR_ENABLE     0x1
+#define GITS_IIDR_VALUE      0x34c
+
+#define GITS_BASER_VALID                BIT(63)
+#define GITS_BASER_INDIRECT             BIT(62)
+#define GITS_BASER_INNER_CACHEABILITY_SHIFT        59
+#define GITS_BASER_TYPE_SHIFT           56
+#define GITS_BASER_OUTER_CACHEABILITY_SHIFT        53
+#define GITS_BASER_TYPE_NONE            0UL
+#define GITS_BASER_TYPE_DEVICE          1UL
+#define GITS_BASER_TYPE_VCPU            2UL
+#define GITS_BASER_TYPE_CPU             3UL
+#define GITS_BASER_TYPE_COLLECTION      4UL
+#define GITS_BASER_TYPE_RESERVED5       5UL
+#define GITS_BASER_TYPE_RESERVED6       6UL
+#define GITS_BASER_TYPE_RESERVED7       7UL
+#define GITS_BASER_ENTRY_SIZE_SHIFT     48
+#define GITS_BASER_SHAREABILITY_SHIFT   10
+#define GITS_BASER_PAGE_SIZE_SHIFT      8
+#define GITS_BASER_RO_MASK              ((7UL << GITS_BASER_TYPE_SHIFT) | \
+                                        (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
+                                        GITS_BASER_INDIRECT)
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
@@ -27,6 +68,7 @@ struct host_its {
     const struct dt_device_node *dt_node;
     paddr_t addr;
     paddr_t size;
+    void __iomem *its_base;
 };
 
 extern struct list_head host_its_list;
@@ -42,8 +84,9 @@ void gicv3_its_dt_init(const struct dt_device_node *node);
 uint64_t gicv3_lpi_get_proptable(void);
 uint64_t gicv3_lpi_allocate_pendtable(void);
 
-/* Initialize the host structures for LPIs. */
+/* Initialize the host structures for LPIs and the host ITSes. */
 int gicv3_lpi_init_host_lpis(int nr_lpis);
+int gicv3_its_init(struct host_its *hw_its);
 
 #else
 
@@ -62,6 +105,10 @@ static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
 {
     return 0;
 }
+static inline int gicv3_its_init(struct host_its *hw_its)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
Signature · 1 line
2.9.0

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

#27 lore raw

Re: [RFC PATCH 04/24] ARM: GICv3 ITS: map ITS command buffer

In reply to Andre Przywara
Quoted 25 lines · Andre Przywara

Instead of directly manipulating the tables in memory, an ITS driver sends commands via a ring buffer to the ITS h/w to create or alter the LPI mappings.
Allocate memory for that buffer and tell the ITS about it to be able to send ITS commands.

---
 xen/arch/arm/gic-its.c        | 25 +++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  1 +
 2 files changed, 26 insertions(+)
xen/arch/arm/gic-its.c +1 -0
index 40238a2..c8a7a7e 100644
@@ -18,6 +18,7 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/err.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 60

Quoted 4 lines
xen/arch/arm/gic-its.c +1 -0
@@ -56,6 +57,26 @@ static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
     return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
 }
 
+static void *gicv3_map_cbaser(void __iomem *cbasereg)

Shouldn't it be its_map_cbaser?

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 71

Quoted 11 lines
xen/arch/arm/gic-its.c +11 -0
+{
+    uint64_t attr, reg;
+    void *buffer;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    buffer = alloc_xenheap_pages(0, 0);
+    if ( !buffer )
+        return ERR_PTR(-ENOMEM);

We haven't use much ERR_PTR on arm so far. In this case I'd just return NULL.

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 74

Quoted 3 lines
xen/arch/arm/gic-its.c +3 -0
+
+    /* We use exactly one 4K page, so the "Size" field is 0. */
+    reg = attr | BIT(63) | (virt_to_maddr(buffer) & GENMASK(51, 12));

Shouldn't the mask be GENMASK(47, 12)? Maybe I have an old spec version.

Quoted 34 lines
xen/arch/arm/gic-its.c +9 -0
+    writeq_relaxed(reg, cbasereg);
+
+    return buffer;
+}
+
 static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
 {
     uint64_t attr;
@@ -149,6 +170,10 @@ int gicv3_its_init(struct host_its *hw_its)
         }
     }
 
+    hw_its->cmd_buf = gicv3_map_cbaser(hw_its->its_base + GITS_CBASER);
+    if ( IS_ERR(hw_its->cmd_buf) )
+        return PTR_ERR(hw_its->cmd_buf);
+
     return 0;
 }
 
xen/include/asm-arm/gic-its.h +1 -0
index 589b889..b2a003f 100644
@@ -69,6 +69,7 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    void *cmd_buf;
 };
 
 extern struct list_head host_its_list;
Signature · 1 line
2.9.0

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

#31 lore raw

Re: [RFC PATCH 05/24] ARM: GICv3 ITS: introduce ITS command handling

In reply to Andre Przywara
Quoted 29 lines · Andre Przywara

To be able to easily send commands to the ITS, create the respective wrapper functions, which take care of the ring buffer.
The first two commands we implement provide methods to map a collection to a redistributor (aka host core) and to flush the command queue (SYNC). Start using these commands for mapping one collection to each host CPU.

---
 xen/arch/arm/gic-its.c        | 101 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  17 +++++++
 xen/include/asm-arm/gic-its.h |  32 +++++++++++++
 3 files changed, 150 insertions(+)
xen/arch/arm/gic-its.c +4 -0
index c8a7a7e..88397bc 100644
@@ -33,6 +33,10 @@ static struct {
     int host_lpi_bits;
 } lpi_data;
 
+/* Physical redistributor address */
+static DEFINE_PER_CPU(uint64_t, rdist_addr);
+/* Redistributor ID */
+static DEFINE_PER_CPU(uint64_t, rdist_id);
 /* Pending table for each redistributor */
 static DEFINE_PER_CPU(void *, pending_table);
 

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 51

Quoted 8 lines
xen/arch/arm/gic-its.c +5 -0
@@ -40,6 +44,86 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define ITS_COMMAND_SIZE        32
+
+static int its_send_command(struct host_its *hw_its, void *its_cmd)
+{
+    int readp, writep;

uint64_t

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 56

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+    spin_lock(&hw_its->cmd_lock);
+
+    readp = readl_relaxed(hw_its->its_base + GITS_CREADR) & GENMASK(19, 5);
+    writep = readl_relaxed(hw_its->its_base + GITS_CWRITER) & GENMASK(19, 5);

It might be worth to

  #define ITS_CMD_RING_SIZE PAGE_SIZE

for clarity

Quoted 13 lines
xen/arch/arm/gic-its.c +13 -0
+    if ( ((writep + ITS_COMMAND_SIZE) % PAGE_SIZE) == readp )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        return -EBUSY;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_COMMAND_SIZE);
+    __flush_dcache_area(hw_its->cmd_buf + writep, ITS_COMMAND_SIZE);
+    writep = (writep + ITS_COMMAND_SIZE) % PAGE_SIZE;
+
+    writeq_relaxed(writep & GENMASK(19, 5), hw_its->its_base + GITS_CWRITER);
+
+    spin_unlock(&hw_its->cmd_lock);

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 80

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+    return 0;
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, int cpu, uint64_t reg)
+{
+    reg &= ~GENMASK(51, 16);
+
+    if ( hw_its->pta )
+        reg |= per_cpu(rdist_addr, cpu) & GENMASK(51, 16);

Again in my version of the spec is GENMASK(47, 16).

Quoted 49 lines
xen/arch/arm/gic-its.c +45 -0
+    else
+        reg |= per_cpu(rdist_id, cpu) << 16;
+    return reg;
+}
+
+static int its_send_cmd_sync(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_SYNC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, 0x0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, (collection_id & GENMASK(15, 0)) | BIT(63));
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+void gicv3_its_setup_collection(int cpu)
+{
+    struct host_its *its;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        /* Only send commands to ITS that have been initialized already. */
+        if ( !its->cmd_buf )
+            continue;
+
+        its_send_cmd_mapc(its, cpu, cpu);
+        its_send_cmd_sync(its, cpu);
+    }
+}
+
 #define BASER_ATTR_MASK                                           \
         ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
          (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 239

Quoted 9 lines
xen/arch/arm/gic-its.c +6 -0
@@ -147,6 +231,13 @@ int gicv3_its_init(struct host_its *hw_its)
     if ( !hw_its->its_base )
         return -ENOMEM;
 
+    /* Make sure the ITS is disabled before programming the BASE registers. */
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg & ~GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
+    reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
+    hw_its->pta = reg & GITS_TYPER_PTA;

To avoid problems:

  pta = !!(reg & GITS_TYPER_PTA);

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 269

Quoted 9 lines
xen/arch/arm/gic-its.c +2 -0
     for (i = 0; i < 8; i++)
     {
         void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
@@ -174,9 +265,18 @@ int gicv3_its_init(struct host_its *hw_its)
     if ( IS_ERR(hw_its->cmd_buf) )
         return PTR_ERR(hw_its->cmd_buf);
 
+    its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
+    its_send_cmd_sync(hw_its, smp_processor_id());

Why do we need these two commands in addition to the ones issued by gicv3_its_setup_collection?

Quoted 40 lines
xen/arch/arm/gic-its.c +7 -0
     return 0;
 }
 
+void gicv3_set_redist_addr(paddr_t address, int redist_id)
+{
+    this_cpu(rdist_addr) = address;
+    this_cpu(rdist_id) = redist_id;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
@@ -265,6 +365,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->addr = addr;
         its_data->size = size;
         its_data->dt_node = its;
+        spin_lock_init(&its_data->cmd_lock);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/arch/arm/gic-v3.c +5 -0
index 5cf4618..b9387a3 100644
@@ -638,6 +638,8 @@ static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
     table_reg = gicv3_lpi_get_proptable();
     if ( table_reg )
         writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);
+
+    gicv3_its_setup_collection(smp_processor_id());
 }
 
 static int __init gicv3_populate_rdist(void)
@@ -684,7 +686,22 @@ static int __init gicv3_populate_rdist(void)
                 this_cpu(rbase) = ptr;
 
                 if ( typer & GICR_TYPER_PLPIS )
+                {
+                    paddr_t rdist_addr;
+

xen/arch/arm/gic-v3.c static int __init gicv3_populate_rdist(void) · line 701

Quoted 10 lines
xen/arch/arm/gic-v3.c +10 -0
+                    rdist_addr = gicv3.rdist_regions[i].base;
+                    rdist_addr += ptr - gicv3.rdist_regions[i].map_base;
+
+                    /* The ITS refers to redistributors either by their physical
+                     * address or by their ID. Determine those two values and
+                     * let the ITS code store them in per host CPU variables to
+                     * later be able to address those redistributors.
+                     */
+                    gicv3_set_redist_addr(rdist_addr,
+                                          (typer >> 8) & GENMASK(15, 0));

Please #define the 8

Quoted 21 lines
xen/arch/arm/gic-v3.c +1 -0
                     gicv3_rdist_init_lpis(ptr);
+                }
 
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
xen/include/asm-arm/gic-its.h +1 -0
index b2a003f..b49d274 100644
@@ -37,6 +37,7 @@
 
 /* Register bits */
 #define GITS_CTLR_ENABLE     0x1
+#define GITS_TYPER_PTA       BIT(19)
 #define GITS_IIDR_VALUE      0x34c
 
 #define GITS_BASER_VALID                BIT(63)
@@ -59,6 +60,22 @@
                                         (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
                                         GITS_BASER_INDIRECT)
 

xen/include/asm-arm/gic-its.h line 72

Quoted 10 lines
xen/include/asm-arm/gic-its.h +10 -0
+/* ITS command definitions */
+#define ITS_CMD_SIZE                    32
+
+#define GITS_CMD_MOVI                   0x01
+#define GITS_CMD_INT                    0x03
+#define GITS_CMD_CLEAR                  0x04
+#define GITS_CMD_SYNC                   0x05
+#define GITS_CMD_MAPD                   0x08
+#define GITS_CMD_MAPC                   0x09
+#define GITS_CMD_MAPTI                  0x0a

In my version of the spec (PRD03-GENC-010745 24.0) 0x0a is MAPVI.

Quoted 36 lines
xen/include/asm-arm/gic-its.h +14 -0
+#define GITS_CMD_MAPI                   0x0b
+#define GITS_CMD_INV                    0x0c
+#define GITS_CMD_INVALL                 0x0d
+#define GITS_CMD_MOVALL                 0x0e
+#define GITS_CMD_DISCARD                0x0f
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
@@ -69,7 +86,9 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;
     void *cmd_buf;
+    bool pta;
 };
 
 extern struct list_head host_its_list;
@@ -89,6 +108,12 @@ uint64_t gicv3_lpi_allocate_pendtable(void);
 int gicv3_lpi_init_host_lpis(int nr_lpis);
 int gicv3_its_init(struct host_its *hw_its);
 
+/* Set the physical address and ID for each redistributor as read from DT. */
+void gicv3_set_redist_addr(paddr_t address, int redist_id);
+
+/* Map a collection for this host CPU to each host ITS. */
+void gicv3_its_setup_collection(int cpu);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -110,6 +135,13 @@ static inline int gicv3_its_init(struct host_its *hw_its)
 {
     return 0;
 }

xen/include/asm-arm/gic-its.h static inline int gicv3_its_init(struct host_its *hw_its) · line 147

Quoted 10 lines
xen/include/asm-arm/gic-its.h +7 -0
+static inline void gicv3_set_redist_addr(paddr_t address, int redist_id)
+{
+}
+static inline void gicv3_its_setup_collection(int cpu)
+{
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */

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

#32 lore raw

Re: [RFC PATCH 05/24] ARM: GICv3 ITS: introduce ITS command handling

Quoted 4 lines · Stefano Stabellini
Quoted 4 lines
xen/include/asm-arm/gic-its.h +4 -0
+/* ITS command definitions */
+#define ITS_CMD_SIZE                    32
+
+#define GITS_CMD_MOVI                   0x01
Quoted 8 lines

xen/include/asm-arm/gic-its.h line 72

Quoted 6 lines
xen/include/asm-arm/gic-its.h +6 -0
+#define GITS_CMD_INT                    0x03
+#define GITS_CMD_CLEAR                  0x04
+#define GITS_CMD_SYNC                   0x05
+#define GITS_CMD_MAPD                   0x08
+#define GITS_CMD_MAPC                   0x09
+#define GITS_CMD_MAPTI                  0x0a

In my version of the spec (PRD03-GENC-010745 24.0) 0x0a is MAPVI.

For reference, I had an older version of the spec. I found the new version, which confirms Andre's numbers.

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

#38 lore raw

Re: [RFC PATCH 06/24] ARM: GICv3 ITS: introduce host LPI array

In reply to Andre Przywara
Quoted 39 lines · Andre Przywara

The number of LPIs on a host can be potentially huge (millions), although in practise will be mostly reasonable. So prematurely allocating an array of struct irq_desc's for each LPI is not an option. However Xen itself does not care about LPIs, as every LPI will be injected into a guest (Dom0 for now).
Create a dense data structure (8 Bytes) for each LPI which holds just enough information to determine the virtual IRQ number and the VCPU into which the LPI needs to be injected.
Also to not artificially limit the number of LPIs, we create a 2-level table for holding those structures.
This patch introduces functions to initialize these tables and to create, lookup and destroy entries for a given LPI.
We allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-its.c        | 154 ++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  18 +++++
 2 files changed, 172 insertions(+)
xen/arch/arm/gic-its.c +2 -0
index 88397bc..2140e4a 100644
@@ -18,18 +18,31 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/sched.h>
 #include <xen/err.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/domain.h>
 #include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>

xen/arch/arm/gic-its.c line 40

Quoted 10 lines
xen/arch/arm/gic-its.c +9 -0
 
+/* LPIs on the host always go to a guest, so no struct irq_desc for them. */
+union host_lpi {
+    uint64_t data;
+    struct {
+        uint64_t virt_lpi:32;
+        uint64_t dom_id:16;
+        uint64_t vcpu_id:16;
+    };
+};

Why not the following?

  union host_lpi {
      uint64_t data;
      struct {
          uint32_t virt_lpi;
          uint16_t dom_id;
          uint16_t vcpu_id;
      };
  };
Quoted 8 lines
xen/arch/arm/gic-its.c +1 -0
 /* Global state */
 static struct {
     uint8_t *lpi_property;
+    union host_lpi **host_lpis;
     int host_lpi_bits;
 } lpi_data;
 

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 61

Quoted 6 lines
xen/arch/arm/gic-its.c +3 -0
@@ -43,6 +56,26 @@ static DEFINE_PER_CPU(void *, pending_table);
 #define MAX_HOST_LPI_BITS                                                \
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
+#define HOST_LPIS_PER_PAGE      (PAGE_SIZE / sizeof(union host_lpi))
+
+static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d)

I take "lpi" is the physical lpi here. Maybe we would rename it to "plpi" for clarity.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 72

Quoted 11 lines
xen/arch/arm/gic-its.c +11 -0
+{
+    union host_lpi *hlpi;
+
+    if ( lpi < 8192 || lpi >= MAX_HOST_LPIS + 8192 )
+        return NULL;
+
+    lpi -= 8192;
+    if ( !lpi_data.host_lpis[lpi / HOST_LPIS_PER_PAGE] )
+        return NULL;
+
+    hlpi = &lpi_data.host_lpis[lpi / HOST_LPIS_PER_PAGE][lpi % HOST_LPIS_PER_PAGE];

I realize I am sometimes obsessive about this, but division operations are expensive and this is on the hot path, so I would do:

#define HOST_LPIS_PER_PAGE (PAGE_SIZE >> 3)

unsigned int table = lpi / HOST_LPIS_PER_PAGE;

then use table throughout this function.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 75

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+    if ( d && hlpi->dom_id != d->domain_id )
+        return NULL;

I think this function is very useful so I would avoid making any domain checks here: one day we might want to retrieve hlpi even if hlpi->dom_id != d->domain_id. I would move the domain check outside.

Quoted 48 lines
xen/arch/arm/gic-its.c +33 -0
+    return hlpi;
+}
 
 #define ITS_COMMAND_SIZE        32
 
@@ -96,6 +129,33 @@ static int its_send_cmd_sync(struct host_its *its, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_discard(struct host_its *its,
+                                uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_DISCARD | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+static int its_send_cmd_mapti(struct host_its *its,
+                              uint32_t deviceid, uint32_t eventid,
+                              uint32_t pintid, uint16_t icid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPTI | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid | ((uint64_t)pintid << 32);
+    cmd[2] = icid;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
 {
     uint64_t cmd[4];
@@ -330,15 +390,109 @@ uint64_t gicv3_lpi_get_proptable()
     return reg;
 }
 
+/* Allocate the 2nd level array for host LPIs. This one holds pointers
+ * to the page with the actual "union host_lpi" entries. Our LPI limit
+ * avoids excessive memory usage.
+ */
 int gicv3_lpi_init_host_lpis(int lpi_bits)

xen/arch/arm/gic-its.c uint64_t gicv3_lpi_get_proptable() · line 407

Quoted 10 lines
xen/arch/arm/gic-its.c +7 -0
 {
+    int nr_lpi_ptrs;
+
     lpi_data.host_lpi_bits = lpi_bits;
 
+    nr_lpi_ptrs = MAX_HOST_LPIS / (PAGE_SIZE / sizeof(union host_lpi));
+
+    lpi_data.host_lpis = xzalloc_array(union host_lpi *, nr_lpi_ptrs);
+    if ( !lpi_data.host_lpis )
+        return -ENOMEM;

Why are we not allocating the 2nd level right away? To save memory? If so, I would like some numbers in a real use case scenario written either here on in the commit message.

Quoted 13 lines
xen/arch/arm/gic-its.c +8 -0
     printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);
 
     return 0;
 }
 
+/* Allocates a new host LPI to be injected as "virt_lpi" into the specified
+ * VCPU. Returns the host LPI ID or a negative error value.
+ */
+int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                uint32_t devid, uint32_t eventid,
+                                struct vcpu *v, int virt_lpi)
+{
+    int chunk, i;

xen/arch/arm/gic-its.c uint64_t gicv3_lpi_get_proptable() · line 431

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    union host_lpi hlpi, *new_chunk;
+
+    /* TODO: handle some kind of preassigned LPI mapping for DomUs */
+    if ( !its )
+        return -EPERM;
+
+    /* TODO: This could be optimized by storing some "next available" hint and
+     * only iterate if this one doesn't work. But this function should be
+     * called rarely.
+     */

Yes please. Even a trivial pointer to last would be far better than this. It would be nice to run some numbers and prove that in realistic scenarios finding an empty plpi doesn't take more than 5-10 ops, which should be the case unless we have to loop over and the initial chucks are still fully populated, causing Xen to scan for 512 units at a time. We defenitely want to avoid that, if not in rare worse case scenarios.

Quoted 11 lines
xen/arch/arm/gic-its.c +11 -0
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        /* If we hit an unallocated chunk, we initialize it and use entry 0. */
+        if ( !lpi_data.host_lpis[chunk] )
+        {
+            new_chunk = alloc_xenheap_pages(0, 0);
+            if ( !new_chunk )
+                return -ENOMEM;
+
+            memset(new_chunk, 0, PAGE_SIZE);
+            lpi_data.host_lpis[chunk] = new_chunk;

xen/arch/arm/gic-its.c uint64_t gicv3_lpi_get_proptable() · line 452

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+            i = 0;
+        }
+        else
+        {
+            /* Find an unallocted entry in this chunk. */
+            for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+                if ( !lpi_data.host_lpis[chunk][i].virt_lpi )
+                    break;
+
+            /* If this chunk is fully allocted, advance to the next one. */

^ allocated

xen/arch/arm/gic-its.c uint64_t gicv3_lpi_get_proptable() · line 462

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+            if ( i == HOST_LPIS_PER_PAGE)
+                continue;
+        }
+
+        hlpi.virt_lpi = virt_lpi;
+        hlpi.dom_id = v->domain->domain_id;
+        hlpi.vcpu_id = v->vcpu_id;
+        lpi_data.host_lpis[chunk][i].data = hlpi.data;
+
+        if (its)

code style

xen/arch/arm/gic-its.c uint64_t gicv3_lpi_get_proptable() · line 466

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+        {
+            its_send_cmd_mapti(its, devid, eventid,
+                               chunk * HOST_LPIS_PER_PAGE + i + 8192, 0);
+            its_send_cmd_sync(its, 0);

Why hardcode the physical cpu to 0? Should we get the pcpu the vcpu is currently running on?

Quoted 71 lines
xen/arch/arm/gic-its.c +29 -0
+        }
+
+        return chunk * HOST_LPIS_PER_PAGE + i + 8192;
+    }
+
+    return -ENOSPC;
+}
+
+/* Drops the connection of the given host LPI to a virtual LPI.
+ */
+int gicv3_lpi_drop_host_lpi(struct host_its *its,
+                            uint32_t devid, uint32_t eventid, uint32_t host_lpi)
+{
+    union host_lpi *hlpip;
+
+    if ( !its )
+        return -EPERM;
+
+    hlpip = gic_find_host_lpi(host_lpi, NULL);
+    if ( !hlpip )
+        return -1;
+
+    hlpip->data = 0;
+
+    its_send_cmd_discard(its, devid, eventid);
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/include/asm-arm/gic-its.h +18 -0
index b49d274..512a388 100644
@@ -114,6 +114,12 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                uint32_t devid, uint32_t eventid,
+                                struct vcpu *v, int virt_lpi);
+int gicv3_lpi_drop_host_lpi(struct host_its *its,
+                            uint32_t devid, uint32_t eventid,
+                            uint32_t host_lpi);
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -141,6 +147,18 @@ static inline void gicv3_set_redist_addr(paddr_t address, int redist_id)
 static inline void gicv3_its_setup_collection(int cpu)
 {
 }
+static inline int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                              uint32_t devid, uint32_t eventid,
+                                              struct vcpu *v, int virt_lpi)
+{
+    return 0;
+}
+static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
+                                          uint32_t devid, uint32_t eventid,
+                                          uint32_t host_lpi)
+{
+    return 0;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
Signature · 1 line
2.9.0

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

#45 lore raw

Re: [RFC PATCH 07/24] ARM: GICv3 ITS: introduce device mapping

In reply to Andre Przywara
Quoted 51 lines · Andre Przywara

The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host.
For this we have to map each device that Dom0 may request to a host ITS device with the same identifier.
Allocate the respective memory and enter each device into a list to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-its.c        | 90 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h | 16 ++++++++
 2 files changed, 106 insertions(+)
xen/arch/arm/gic-its.c +30 -0
index 2140e4a..bf1f5b5 100644
@@ -168,6 +168,94 @@ static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             int size, uint64_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = size & GENMASK(4, 0);
+    cmd[2] = itt_addr & GENMASK(51, 8);
+    if ( valid )
+        cmd[2] |= BIT(63);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid)
+{
+    void *itt_addr = NULL;
+    struct its_devices *dev, *temp;
+    bool reuse_dev = false;
+
+    list_for_each_entry_safe(dev, temp, &hw_its->its_devices, entry)
+    {
+        if ( (dev->d->domain_id != d->domain_id) || (dev->devid != devid) )
+            continue;
+
+        its_send_cmd_mapd(hw_its, dev->devid, 0, 0, false);
+        xfree(dev->itt_addr);
+        if ( !valid )

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 210

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+        {
+            xfree(dev);
+            list_del(&dev->entry);
+
+            return 0;
+        }
+
+        reuse_dev = true;
+        break;
+    }

I don't think we want to go through the whole list every time this function is called. Devices can be thousands. I would split it in two:
one to retrieve existing mappings and another to allocate new ones. We need to make sure we don't call the function to retrieve existing mappings often.

We can also consider using a rbtree instead of a list, or if devids are a dense numeric group, we could use an array and direct access.

Quoted 33 lines
xen/arch/arm/gic-its.c +33 -0
+    if ( !valid )
+        return 0;
+
+    itt_addr = _xmalloc(BIT(bits) * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        return -ENOMEM;
+
+    if ( !reuse_dev )
+    {
+        dev = xmalloc(struct its_devices);
+        if ( !dev )
+            return -ENOMEM;
+
+        list_add_tail(&dev->entry, &hw_its->its_devices);
+    }
+
+    dev->itt_addr = itt_addr;
+    dev->d = d;
+    dev->devid = devid;
+
+    return its_send_cmd_mapd(hw_its, devid, bits - 1,
+                             itt_addr ? virt_to_maddr(itt_addr) : 0, true);
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+int its_remove_domain(struct domain *d)
+{
+    struct host_its *its;
+    struct its_devices *dev, *temp;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        list_for_each_entry_safe(dev, temp, &its->its_devices, entry)

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 254

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+        {
+            if ( dev->d->domain_id != d->domain_id )
+                continue;
+
+            its_send_cmd_mapd(its, dev->devid, 0, 0, false);
+            xfree(dev->itt_addr);
+            xfree(dev);
+            list_del(&dev->entry);
+        }
+    }

Again scanning the full list on every domain destruction is not good. This is easy to work around, even without completely reworking the data structures, because we could add a second per-domain list to link all devices that belong to the same domain.

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 388

Quoted 11 lines
xen/arch/arm/gic-its.c +4 -0
+    return 0;
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 void gicv3_its_setup_collection(int cpu)
 {
@@ -297,6 +385,7 @@ int gicv3_its_init(struct host_its *hw_its)
 
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     hw_its->pta = reg & GITS_TYPER_PTA;
+    hw_its->itte_size = ((reg >> 4) & 0xf) + 1;

Please add some #define

Quoted 54 lines
xen/arch/arm/gic-its.c +1 -0
     for (i = 0; i < 8; i++)
     {
@@ -520,6 +609,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->size = size;
         its_data->dt_node = its;
         spin_lock_init(&its_data->cmd_lock);
+        INIT_LIST_HEAD(&its_data->its_devices);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/include/asm-arm/gic-its.h +16 -0
index 512a388..4e9841a 100644
@@ -79,6 +79,13 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
+struct its_devices {
+    struct list_head entry;
+    struct domain *d;
+    void *itt_addr;
+    int devid;
+};
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -88,6 +95,8 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    struct list_head its_devices;
+    int itte_size;
     bool pta;
 };
 
@@ -114,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* 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.
+ */
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid);
+
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
Signature · 1 line
2.9.0

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

#49 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara
Quoted 25 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +0 -0
index 63c744a..ebe4035 100644

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 511

Quoted 6 lines
xen/arch/arm/gic.c +3 -0
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )
+                p->irq = 0;

I believe that 0 is a valid irq number, we need to come up with a different invalid_irq value, and we should #define it. We could also consider checking if the irq is inflight (linked to the inflight list) instead of using irq == 0 to understand if it is reusable.

Quoted 39 lines
xen/arch/arm/gic.c +0 -0
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +3 -1
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);

xen/arch/arm/vgic.c struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq) · line 253

Quoted 9 lines
xen/arch/arm/vgic.c +2 -1
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
+    if ( virq >= 8192 )

Please introduce a convenience static inline function such as:

bool is_lpi(unsigned int irq)

Quoted 13 lines
xen/arch/arm/vgic.c +6 -0
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 467

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    /* TODO: locking! */

Yeah, this needs to be fixed in v1 :-)

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 8 lines
xen/arch/arm/vgic.c +8 -0
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

This is another one of those cases where a list is too slow for the hot path. The idea of allocating pending_irq struct on demand is good, but storing them in a linked list would kill performance. Probably the best thing we could do is an hashtable and we should preallocate the initial array of elements. I don't know what the size of the initial array should be, but we can start around 50, and change it in the future once we do tests with real workloads. Of course the other key parameter is the hash function, not sure which one is the right one, but ideally we would never have to allocate new pending_irq struct for LPIs because the preallocated set would suffice.

I could be convinced that a list is sufficient if we do some real benchmarking and it turns out that lpi_to_pending always resolve in less than ~5 steps.

Quoted 11 lines
xen/arch/arm/vgic.c +11 -0
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 497

Quoted 10 lines
xen/arch/arm/vgic.c +7 -0
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+

spurious change

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 502

Quoted 5 lines
xen/arch/arm/vgic.c +1 -0
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )

Use the new static inline

Quoted 14 lines
xen/arch/arm/vgic.c +2 -1
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 

xen/arch/arm/vgic.c void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq) · line 539

Quoted 4 lines
xen/arch/arm/vgic.c +1 -0
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);

Why this change?

Quoted 20 lines
xen/arch/arm/vgic.c +0 -0
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +0 -0
index 4e9841a..1f881c0 100644

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 143

Quoted 8 lines
xen/include/asm-arm/gic-its.h +5 -0
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}

Does it mean that we don't allow changes to LPI priorities?

Quoted 46 lines
xen/include/asm-arm/gic-its.h +4 -0
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -0
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
Signature · 1 line
2.9.0

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

#56 lore raw

Re: [RFC PATCH 09/24] ARM: GICv3: forward pending LPIs to guests

In reply to Andre Przywara
Quoted 23 lines · Andre Przywara

Upon receiving an LPI, we need to find the right VCPU and virtual IRQ number to get this IRQ injected.
Iterate our two-level LPI table to find this information quickly when the host takes an LPI. Call the existing injection function to let the GIC emulation deal with this interrupt.

---
 xen/arch/arm/gic-its.c    | 32 ++++++++++++++++++++++++++++++++
 xen/arch/arm/gic.c        |  6 ++++--
 xen/include/asm-arm/irq.h |  8 ++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)
xen/arch/arm/gic-its.c +2 -0
index bf1f5b5..b7aa918 100644
@@ -77,6 +77,38 @@ static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d)
     return hlpi;
 }
 
+/* Handle incoming LPIs, which are a bit special, because they are potentially
+ * numerous and also only get injected into guests. Treat them specially here,

xen/arch/arm/gic-its.c static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d) · line 91

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+ * by just looking up their target vCPU and virtual LPI number and hand it
+ * over to the injection function.
+ */
+void do_LPI(unsigned int lpi)
+{
+    struct domain *d;
+    union host_lpi *hlpip, hlpi;
+
+    WRITE_SYSREG32(lpi, ICC_EOIR1_EL1);
+    WRITE_SYSREG32(lpi, ICC_DIR_EL1);

Given that LPIs are always guest IRQs, and given that we support EOImode = 1, shouldn't we just do ICC_EOIR1_EL1 here (no ICC_DIR_EL1)?

xen/arch/arm/gic-its.c static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d) · line 97

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+    hlpip = gic_find_host_lpi(lpi, NULL);
+    if ( !hlpip )
+        return;
+
+    hlpi.data = hlpip->data;

Why can't we just reference hlpip directly? Why do we need hlpi?

Quoted 55 lines
xen/arch/arm/gic-its.c +13 -0
+    if ( !hlpi.virt_lpi )
+        return;
+
+    d = get_domain_by_id(hlpi.dom_id);
+    if ( !d )
+        return;
+
+    if ( hlpi.vcpu_id >= d->max_vcpus )
+        return;
+
+    vgic_vcpu_inject_irq(d->vcpu[hlpi.vcpu_id], hlpi.virt_lpi);
+}
+
 #define ITS_COMMAND_SIZE        32
 
 static int its_send_command(struct host_its *hw_its, void *its_cmd)
xen/arch/arm/gic.c +4 -2
index ebe4035..2fad2f1 100644
@@ -697,8 +697,10 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
             local_irq_enable();
             do_IRQ(regs, irq, is_fiq);
             local_irq_disable();
-        }
-        else if (unlikely(irq < 16))
+        } else if ( irq >= 8192 )
+        {
+            do_LPI(irq);
+        } else if ( unlikely(irq < 16) )
         {
             do_sgi(regs, irq);
         }
xen/include/asm-arm/irq.h +8 -0
index 8f7a167..ee47de8 100644
@@ -34,6 +34,14 @@ struct irq_desc *__irq_to_desc(int irq);
 
 void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq);
 
+#ifdef CONFIG_HAS_ITS
+void do_LPI(unsigned int irq);
+#else
+static inline void do_LPI(unsigned int irq)
+{
+}
+#endif
+
 #define domain_pirq_to_irq(d, pirq) (pirq)
 
 bool_t is_assignable_irq(unsigned int irq);
Signature · 1 line
2.9.0

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

Reviewed-by on v1 10/24
#58 lore raw

Re: [RFC PATCH 10/24] ARM: GICv3: enable ITS and LPIs on the host

In reply to Andre Przywara
Quoted 6 lines · Andre Przywara

Now that the host part of the ITS code is in place, we can enable the ITS and also LPIs on each redistributor to get the show rolling. At this point there would be no LPIs mapped, as guests don't know about the ITS yet.

Quoted 59 lines
 xen/arch/arm/gic-its.c |  4 ++++
 xen/arch/arm/gic-v3.c  | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
xen/arch/arm/gic-its.c +4 -0
index b7aa918..6bac422 100644
@@ -449,6 +449,10 @@ int gicv3_its_init(struct host_its *hw_its)
     its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
     its_send_cmd_sync(hw_its, smp_processor_id());
 
+    /* Now enable interrupt translation on that ITS. */
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg | GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
     return 0;
 }
 
xen/arch/arm/gic-v3.c +19 -0
index b9387a3..57009c6 100644
@@ -642,6 +642,21 @@ static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
     gicv3_its_setup_collection(smp_processor_id());
 }
 
+/* Enable LPIs on this redistributor (only useful when the host has an ITS. */
+static bool gicv3_enable_lpis(void)
+{
+    uint32_t val;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_TYPER);
+    if ( !(val & GICR_TYPER_PLPIS) )
+        return false;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_CTLR);
+    writel_relaxed(val | GICR_CTLR_ENABLE_LPIS, GICD_RDIST_BASE + GICR_CTLR);
+
+    return true;
+}
+
 static int __init gicv3_populate_rdist(void)
 {
     int i;
@@ -741,6 +756,10 @@ static int gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+    /* If the host has any ITSes, enable LPIs now. */
+    if ( !list_empty(&host_its_list) )
+        gicv3_enable_lpis();
+
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
                 GIC_PRI_IPI);
Signature · 1 line
2.9.0

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

#63 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara
Quoted 144 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +83 -9
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #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 <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

map_guest_pages and unmap_guest_pages are not ARM specific and should live somewhere in xen/common, maybe xen/common/vmap.c.

xen/arch/arm/vgic-v3.c read_unknown: · line 423

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */

No need: the caller is already doing that, right?

xen/arch/arm/vgic-v3.c read_unknown: · line 435

Quoted 11 lines
xen/arch/arm/vgic-v3.c +11 -0
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }

How often do you think only 1 page will be used? If the answer is not often, I would get rid of the onepage optimization.

xen/arch/arm/vgic-v3.c read_unknown: · line 440

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);

Don't you need to call page_to_mfn (or gfn_to_mfn) to get the mfn?

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

It is possible for vmap to fail and return NULL. Maybe because the guest intentionally tried to break the hypervisor by passing an array of pages that ends in an MMIO region. We need to check vmap errors and handle them.

Quoted 34 lines
xen/arch/arm/vgic-v3.c +25 -0
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     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 516

Quoted 8 lines
xen/arch/arm/vgic-v3.c +2 -3
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;

unsigned int

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

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Why not use vgic_reg64_check_access?

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

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;

Why? If there is a good reason for this, it is probably worth writing it in an in-code comment.

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

Quoted 7 lines
xen/arch/arm/vgic-v3.c +6 -0
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);

Do we need to set an upper limit on nr_pages? We don't really want to allow (2^0x1f)/4096 pages, right?

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

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);

I am pretty sure I am reading the right spec now and it should be GENMASK(51, 12). Also, don't we need to sanitize the table too before using it?

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

Quoted 6 lines
xen/arch/arm/vgic-v3.c +3 -2
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Why not use vgic_reg64_check_access?

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

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+	reg = v->arch.vgic.rdist_pendbase;
+	vgic_reg64_update(&reg, r, info);
+	reg = sanitize_pendbaser(reg);
+	v->arch.vgic.rdist_pendbase = reg;
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);
+	v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);

Indentation.
We need to make sure the vmap mapping is correct. Also we need to sanitize this table too before using it.

Quoted 9 lines
xen/arch/arm/vgic-v3.c +1 -0
+	return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +0 -0
index b961551..4d9304f 100644

xen/arch/arm/vgic.c struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi, · line 493

Quoted 6 lines
xen/arch/arm/vgic.c +3 -0
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);

Where is the GIC_IRQ_GUEST_ENABLED unset?

xen/include/asm-arm/domain.h struct arch_domain · line 113

Quoted 13 lines
xen/arch/arm/vgic.c +0 -0
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +2 -0
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;

Do we need to keep both rdist_propbase and proptable? It is easy to go from proptable to rdist_propbase and I guess it is not an operation that is done often? If so, we could save some memory and remove it.

xen/include/asm-arm/domain.h struct arch_vcpu · line 255

Quoted 12 lines
xen/include/asm-arm/domain.h +4 -1
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;

Same here.

xen/include/asm-arm/gic-its.h int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 142

Quoted 13 lines
xen/include/asm-arm/domain.h +0 -0
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +1 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;

Please #define 0xfc. Do we need to check for lpi overflows? As in lpi numbers larger than proptable size?

Quoted 23 lines
xen/include/asm-arm/gic-its.h +8 -0
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +0 -0
index 4e29ba6..2b216cc 100644

xen/include/asm-arm/vgic.h VGIC_REG_HELPERS(32, 0x3); · line 293

Quoted 9 lines
xen/include/asm-arm/vgic.h +3 -0
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*

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

#3 lore raw

Re: [RFC PATCH 01/24] ARM: GICv3 ITS: parse and store ITS subnodes from hardware DT

In reply to Andre Przywara

Hi Andre,

Quoted 21 lines · Andre Przywara

Parse the DT GIC subnodes to find every ITS MSI controller the hardware offers. Store that information in a list to both propagate all of them later to Dom0, but also to be able to iterate over all ITSes. This introduces an ITS Kconfig option.

---
 xen/arch/arm/Kconfig          |  5 ++++
 xen/arch/arm/Makefile         |  1 +
 xen/arch/arm/gic-its.c        | 67 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  6 ++++
 xen/include/asm-arm/gic-its.h | 57 ++++++++++++++++++++++++++++++++++++
 5 files changed, 136 insertions(+)
 create mode 100644 xen/arch/arm/gic-its.c
 create mode 100644 xen/include/asm-arm/gic-its.h
xen/arch/arm/Kconfig +0 -0
index 797c91f..9fe3b8e 100644

xen/arch/arm/Kconfig config ACPI · line 50

Quoted 6 lines
xen/arch/arm/Kconfig +3 -0
@@ -45,6 +45,11 @@ config ACPI
 config HAS_GICV3
 	bool
 
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on ARM_64

HAS_GICV3 will only be selected for 64-bit. It would need some rework to be supported on 32-bit. So I would drop this dependency.

xen/arch/arm/Kconfig config ACPI · line 52

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+        depends on HAS_GICV3
+

I am not convinced that we should (currently) let the user selecting the ITS support. It increases the test coverage (we have to test with and without). Do we expect people using GICv3 without ITS?

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 02/24] ARM: GICv3: allocate LPI pending and property table

In reply to Andre Przywara

Hi Andre,

Quoted 24 lines · Andre Przywara

The ARM GICv3 ITS provides a new kind of interrupt called LPIs. The pending bits and the configuration data (priority, enable bits) for those LPIs are stored in tables in normal memory, which software has to provide to the hardware.
Allocate the required memory, initialize it and hand it over to each ITS. We limit the number of LPIs we use with a compile time constant to avoid wasting memory.

---
 xen/arch/arm/Kconfig              |  6 ++++
 xen/arch/arm/efi/efi-boot.h       |  1 -
 xen/arch/arm/gic-its.c            | 76 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             | 27 ++++++++++++++
 xen/include/asm-arm/cache.h       |  4 +++
 xen/include/asm-arm/gic-its.h     | 22 +++++++++++-
 xen/include/asm-arm/gic_v3_defs.h | 48 ++++++++++++++++++++++++-
 7 files changed, 181 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index 9fe3b8e..66e2bb8 100644

xen/arch/arm/Kconfig config HAS_ITS · line 58

Quoted 9 lines
xen/arch/arm/Kconfig +6 -0
@@ -50,6 +50,12 @@ config HAS_ITS
         depends on ARM_64
         depends on HAS_GICV3
 
+config HOST_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+

This would be better to be defined as a parameter command line. So the user does not need to rebuild Xen in order to increase the number of bits supported. It would also be useful to get a rational behind the default number in the commit message.

xen/arch/arm/gic-its.c

Quoted 19 lines
xen/arch/arm/Kconfig +0 -0
 config ALTERNATIVE
 	bool
 
xen/arch/arm/efi/efi-boot.h +0 -1
index 045d6ce..dc64aec 100644
@@ -10,7 +10,6 @@
 #include "efi-dom0.h"
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
-void __flush_dcache_area(const void *vaddr, unsigned long size);
 
 #define DEVICE_TREE_GUID \
 {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}
xen/arch/arm/gic-its.c +0 -0
index 0f42a77..b52dff3 100644

Please rename this file gic-v3-its to make clear ITS is only GICv3.

xen/arch/arm/gic-its.c line 23

Quoted 5 lines
xen/arch/arm/gic-its.c +1 -0
@@ -20,10 +20,86 @@
 #include <xen/lib.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
+#include <asm/p2m.h>

Why did you include p2m.h? This header contains stage-2 page table functions but I don't see any use of them within this patch.

xen/arch/arm/gic-its.c line 31

Quoted 8 lines
xen/arch/arm/gic-its.c +4 -0
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
 
+/* Global state */
+static struct {
+    uint8_t *lpi_property;
+    int host_lpi_bits;

Please use unsigned int.

xen/arch/arm/gic-its.c line 38

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+} lpi_data;
+
+/* Pending table for each redistributor */
+static DEFINE_PER_CPU(void *, pending_table);
+
+#define MAX_HOST_LPI_BITS                                                \
+        min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)

Why don't you directly initialize host_lpi_bits to the correct value? This would avoid to compute the min every time you use MAX_HOST_LPI_BITS and save few instructions.

xen/arch/arm/gic-its.c line 39

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+#define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)

I know that we don't support ITS for 32-bits, but I would rather avoid to use BIT as this macro is working on unsigned long. I would prefer if you introduce BIT_ULL or open-code.

xen/arch/arm/gic-its.c line 48

Quoted 9 lines
xen/arch/arm/gic-its.c +9 -0
+
+uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    uint64_t reg, attr;
+    void *pendtable;
+
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;

From the spec (8.11.18 in ARM IHI 0069C) the cacheability and shareability could be fixed (though it marked as deprecated). Should we check whether the value stick?

Also the variable attr sounds pointless as you will directly assign the value to reg with no more computation.

xen/arch/arm/gic-its.c line 59

Quoted 11 lines
xen/arch/arm/gic-its.c +11 -0
+
+    /*
+     * The pending table holds one bit per LPI, so we need three bits less
+     * than the number of LPI_BITs. But the alignment requirement from the
+     * ITS is 64K, so make order at least 16 (-12).
+     */
+    pendtable = alloc_xenheap_pages(MAX(lpi_data.host_lpi_bits - 3, 16) - 12, 0);
+    if ( !pendtable )
+        return 0;
+
+    memset(pendtable, 0, BIT(lpi_data.host_lpi_bits - 3));

Same remark for BIT here.

xen/arch/arm/gic-its.c line 63

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+    this_cpu(pending_table) = pendtable;
+
+    reg  = attr | GICR_PENDBASER_PTZ;
+    reg |= virt_to_maddr(pendtable) & GENMASK(51, 16);

I don't think the mask is useful and would need to be changed if the physical address bits increased as it was done in ARMv8.2.

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+
+    return reg;
+}
+
+uint64_t gicv3_lpi_get_proptable()
+{

xen/arch/arm/gic-its.c line 79

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    uint64_t attr;
+    static uint64_t reg = 0;
+
+    /* The property table is shared across all redistributors. */
+    if ( reg )
+        return reg;
+
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;

Same question for the cacheability and shareability.

Also the variable attr sounds pointless as you will directly assign the value to reg with no more computation.

xen/arch/arm/gic-its.c line 89

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+    lpi_data.lpi_property = alloc_xenheap_pages(MAX_HOST_LPI_BITS - 12, 0);
+    if ( !lpi_data.lpi_property )
+        return 0;
+
+    memset(lpi_data.lpi_property, GIC_PRI_IRQ | LPI_PROP_RES1, MAX_HOST_LPIS);
+    __flush_dcache_area(lpi_data.lpi_property, MAX_HOST_LPIS);
+
+    reg  = attr | ((MAX_HOST_LPI_BITS - 1) << 0);
+    reg |= virt_to_maddr(lpi_data.lpi_property) & GENMASK(51, 12);

Same remark for the mask here.

xen/arch/arm/gic-its.c line 94

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+
+    return reg;
+}
+
+int gicv3_lpi_init_host_lpis(int lpi_bits)

Please use unsigned int for lpi_bits.

Also this function should probably be in the section __init.

xen/arch/arm/gic-its.c line 98

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+{
+    lpi_data.host_lpi_bits = lpi_bits;
+
+    printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);

%lu.

Quoted 22 lines
xen/arch/arm/gic-its.c +4 -0
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/gic-v3.c +3 -0
index 238da84..2534aa5 100644
@@ -546,6 +546,9 @@ static void __init gicv3_dist_init(void)
     type = readl_relaxed(GICD + GICD_TYPER);
     nr_lines = 32 * ((type & GICD_TYPE_LINES) + 1);
 
+    if ( type & GICD_TYPE_LPIS )
+        gicv3_lpi_init_host_lpis(((type >> GICD_TYPE_ID_BITS_SHIFT) & 0x1f) + 1);
+
     printk("GICv3: %d lines, (IID %8.8x).\n",
            nr_lines, readl_relaxed(GICD + GICD_IIDR));
 

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 620

Quoted 3 lines
xen/arch/arm/gic-v3.c +0 -0
@@ -615,6 +618,26 @@ static int gicv3_enable_redist(void)
 
     return 0;
 }

Missing blank line here.

Quoted 5 lines
xen/arch/arm/gic-v3.c +5 -0
+static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 635

Quoted 10 lines
xen/arch/arm/gic-v3.c +10 -0
+    if ( list_empty(&host_its_list) )
+        return;
+
+    /* Make sure LPIs are disabled before setting up the BASERs. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    writel_relaxed(reg & ~GICR_CTLR_ENABLE_LPIS, rdist_base + GICR_CTLR);
+
+    table_reg = gicv3_lpi_allocate_pendtable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);

Is it fine to continue silently if gicv3_lpi_allocate_pendtable has failed?

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 639

Quoted 4 lines
xen/arch/arm/gic-v3.c +4 -0
+
+    table_reg = gicv3_lpi_get_proptable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);

Ditto.

Quoted 20 lines
xen/arch/arm/gic-v3.c +5 -0
+}
 
 static int __init gicv3_populate_rdist(void)
 {
@@ -658,6 +681,10 @@ static int __init gicv3_populate_rdist(void)
             if ( (typer >> 32) == aff )
             {
                 this_cpu(rbase) = ptr;
+
+                if ( typer & GICR_TYPER_PLPIS )
+                    gicv3_rdist_init_lpis(ptr);
+
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
                 return 0;
xen/include/asm-arm/cache.h +0 -0
index 2de6564..af96eee 100644

xen/include/asm-arm/cache.h line 13

Quoted 7 lines
xen/include/asm-arm/cache.h +4 -0
@@ -7,6 +7,10 @@
 #define L1_CACHE_SHIFT  (CONFIG_ARM_L1_CACHE_SHIFT)
 #define L1_CACHE_BYTES  (1 << L1_CACHE_SHIFT)
 
+#ifndef __ASSEMBLY__
+void __flush_dcache_area(const void *vaddr, unsigned long size);
+#endif
+

Please move this change in a separate patch.

Quoted 16 lines
xen/include/asm-arm/cache.h +0 -0
 #define __read_mostly __section(".data.read_mostly")
 
 #endif
xen/include/asm-arm/gic-its.h +5 -0
index 2f5c51c..48c6c78 100644
@@ -36,12 +36,32 @@ extern struct list_head host_its_list;
 /* Parse the host DT and pick up all host ITSes. */
 void gicv3_its_dt_init(const struct dt_device_node *node);
 
+/* Allocate and initialize tables for each host redistributor.
+ * Returns the respective {PROP,PEND}BASER register value.
+ */
+uint64_t gicv3_lpi_get_proptable(void);
+uint64_t gicv3_lpi_allocate_pendtable(void);

xen/include/asm-arm/gic-its.h extern struct list_head host_its_list; · line 44

Quoted 10 lines
xen/include/asm-arm/gic-its.h +4 -1
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(int nr_lpis);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
 {
 }
-

Please add a newline here.

xen/include/asm-arm/gic-its.h extern struct list_head host_its_list; · line 56

Quoted 4 lines
xen/include/asm-arm/gic-its.h +4 -0
+static inline uint64_t gicv3_lpi_get_proptable(void)
+{
+    return 0;
+}

Ditto

xen/include/asm-arm/gic-its.h extern struct list_head host_its_list; · line 60

Quoted 4 lines
xen/include/asm-arm/gic-its.h +4 -0
+static inline uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    return 0;
+}

Ditto

xen/include/asm-arm/gic-its.h extern struct list_head host_its_list; · line 64

Quoted 4 lines
xen/include/asm-arm/gic-its.h +4 -0
+static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
+{
+    return 0;
+}

Ditto

Quoted 8 lines
xen/include/asm-arm/gic-its.h +0 -0
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */
xen/include/asm-arm/gic_v3_defs.h +0 -0
index 6bd25a5..da5fb77 100644

xen/include/asm-arm/gic_v3_defs.h line 48

Quoted 6 lines
xen/include/asm-arm/gic_v3_defs.h +2 -1
@@ -44,7 +44,8 @@
 #define GICC_SRE_EL2_ENEL1           (1UL << 3)
 
 /* Additional bits in GICD_TYPER defined by GICv3 */
-#define GICD_TYPE_ID_BITS_SHIFT 19
+#define GICD_TYPE_ID_BITS_SHIFT      19
+#define GICD_TYPE_LPIS               (1U << 17)

I was about to say that this should be named GICD_TYPER... but it looks like we already defined and use GIC_TYPE_ID_BITS_SHIFTS. So it is up to you if you rename it to get the correct register name.

xen/include/asm-arm/gic_v3_defs.h line 99

Quoted 8 lines
xen/include/asm-arm/gic_v3_defs.h +1 -0
 #define GICD_CTLR_RWP                (1UL << 31)
 #define GICD_CTLR_ARE_NS             (1U << 4)
@@ -95,12 +96,57 @@
 #define GICR_IGRPMODR0               (0x0D00)
 #define GICR_NSACR                   (0x0E00)
 
+#define GICR_CTLR_ENABLE_LPIS        (1U << 0)

Please add a new line here to separate definition for GICR_CTLR and GICR_TYPER.

xen/include/asm-arm/gic_v3_defs.h line 105

Quoted 6 lines
xen/include/asm-arm/gic_v3_defs.h +2 -0
 #define GICR_TYPER_PLPIS             (1U << 0)
 #define GICR_TYPER_VLPIS             (1U << 1)
 #define GICR_TYPER_LAST              (1U << 4)
 
+#define GIC_BASER_CACHE_nCnB         0ULL
+#define GIC_BASER_CACHE_SameAsInner  0ULL

I think this would require some description in the code as it is not clear wheather nCnB apply for Outer or Inner. From my understanding it is only the later.

xen/include/asm-arm/gic_v3_defs.h line 106

Quoted 8 lines
xen/include/asm-arm/gic_v3_defs.h +8 -0
+#define GIC_BASER_CACHE_nC           1ULL
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

New line here please and maybe a comment to say this is shareability definition.

xen/include/asm-arm/gic_v3_defs.h

Quoted 10 lines
xen/include/asm-arm/gic_v3_defs.h +9 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

It might be better to define GIC_BASER_SHAREABILITY_MASK rather than open-coding 3UL. Also technically 3UL should be 3ULL.

xen/include/asm-arm/gic_v3_defs.h

Quoted 2 lines
xen/include/asm-arm/gic_v3_defs.h +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Same remark here.

xen/include/asm-arm/gic_v3_defs.h

Quoted 2 lines
xen/include/asm-arm/gic_v3_defs.h +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Ditto

xen/include/asm-arm/gic_v3_defs.h

Quoted 1 line
xen/include/asm-arm/gic_v3_defs.h +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

I would probably rename this field GICR_PROPBASER_RES0_MASK.

xen/include/asm-arm/gic_v3_defs.h

Quoted 7 lines
xen/include/asm-arm/gic_v3_defs.h +7 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

See my remark above.

30 lines not shown · raw message

Parts of this message are not shown here. View the raw message.

#21 lore raw

Re: [RFC PATCH 03/24] ARM: GICv3 ITS: allocate device and collection table

In reply to Stefano Stabellini

Hi Stefano,

Quoted 10 lines · Stefano Stabellini
Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
Quoted 8 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 74

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+     * supports.
+     */

Is this really the best way to do it? Can't we assume ITS supports 4K, given that Xen requires 4K pages at the moment? Is it actually possible to find hardware that supports 4K but with an ITS that only support 64K or 16K pages? It seems insane to me. Otherwise can't we probe the page size somehow?

By reading the spec (8.19.1 in IHI 0069C):

"If the GIC implementation supports only a single, fixed page size, this field might be RO. When this register has an architecturally-defined reset value, if this field is implemented as an RW field, it resets to a value that is architecturally UNKNOWN."

As the reset value is architecturally unknown the only way to find out the correct page size is to try them one by one.

The GIC is a separate component of the platform and will be programed using physical address (and not virtual one). It would be fine to have a BASE registers supporting only 64K to save few lines in the GIC.

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 03/24] ARM: GICv3 ITS: allocate device and collection table

In reply to Andre Przywara

Hi Andre,

Quoted 28 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (usually the PCI b/d/f triplet) and an EventID (the MSI payload or interrupt ID) to a pair of LPI number and collection ID, which points to the target CPU.
This mapping is stored in the device and collection tables, which software has to provide for the ITS to use.
Allocate the required memory and hand it the ITS.
We limit the number of devices to cover 4 PCI busses for now.

---
 xen/arch/arm/gic-its.c        | 114 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   5 ++
 xen/include/asm-arm/gic-its.h |  49 +++++++++++++++++-
 3 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +1 -0
index b52dff3..40238a2 100644
@@ -21,6 +21,7 @@
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 46

Quoted 8 lines
xen/arch/arm/gic-its.c +5 -0
@@ -38,6 +39,119 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define BASER_ATTR_MASK                                           \
+        ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
+         (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
+         (0x7UL << GITS_BASER_INNER_CACHEABILITY_SHIFT))
+#define BASER_RO_MASK   (GENMASK(52, 48) | GENMASK(58, 56))

I did not notice in the previous patch. But I would rather introduce GENMASK_ULL to avoid any issue the day we decide to port on 32-bits (for similar reason as BIT).

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 48

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+
+static uint64_t encode_phys_addr(paddr_t addr, int page_bits)

Please make page_bits unsigned int.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 52

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+{
+    uint64_t ret;
+
+    if ( page_bits < 16)

Coding style.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 59

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+        return (uint64_t)addr & GENMASK(47, page_bits);
+
+    ret = addr & GENMASK(47, 16);
+    return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
+}
+
+static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)

unsigned int nr_items

Also can you find a better name for regc and/or reg. The naming is very similar and could be error-prone.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 62

Quoted 3 lines
xen/arch/arm/gic-its.c +3 -0
+{
+    uint64_t attr;
+    int entry_size = (regc >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f;

unsigned int here.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 63

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+    int pagesz;

Same.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 64

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+    int order;

Same.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 75

Quoted 11 lines
xen/arch/arm/gic-its.c +11 -0
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */
+    for (pagesz = 0; pagesz < 3; pagesz++)

Coding style: for ( ... )

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 78

Quoted 3 lines
xen/arch/arm/gic-its.c +3 -0
+    {
+        uint64_t reg;
+        int nr_bytes;

unsigned int nr_bytes.

Quoted 8 lines
xen/arch/arm/gic-its.c +8 -0
+
+        nr_bytes = ROUNDUP(nr_items * entry_size, BIT(pagesz * 2 + 12));
+        order = get_order_from_bytes(nr_bytes);
+
+        if ( !buffer )
+            buffer = alloc_xenheap_pages(order, 0);
+        if ( !buffer )
+            return -ENOMEM;

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 96

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+        reg  = attr;
+        reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+        reg |= nr_bytes >> (pagesz * 2 + 12);
+        reg |= regc & BASER_RO_MASK;
+        reg |= GITS_BASER_VALID;
+        reg |= encode_phys_addr(virt_to_maddr(buffer), pagesz * 2 + 12);
+
+        writeq_relaxed(reg, basereg);
+        regc = readl_relaxed(basereg);

Should not it be readq_relaxed?

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 100

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+
+        /* The host didn't like our attributes, just use what it returned. */
+        if ( (regc & BASER_ATTR_MASK) != attr )
+            attr = regc & BASER_ATTR_MASK;

Should we flush the cache if the GIC does not support shareability?

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 103

Quoted 3 lines
xen/arch/arm/gic-its.c +3 -0
+
+        /* If the host accepted our page size, we are done. */
+        if ( (reg & (3UL << GITS_BASER_PAGE_SIZE_SHIFT)) == pagesz )

You probably want a define for 3UL << GITS_BASER_PAGE_SIZE_SHIFT.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 107

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+            return 0;
+
+        /* Check whether our buffer is aligned to the next page size already. */
+        if ( !(virt_to_maddr(buffer) & (BIT(pagesz * 2 + 12 + 2) - 1)) )

Well the buffer could be aligned to the next page size but the size not page aligned. So you would provide the GIC memory that it cannot touch.

So you have to always reallocate the buffer from scratch to avoid this issue.

Quoted 12 lines
xen/arch/arm/gic-its.c +12 -0
+        {
+            free_xenheap_pages(buffer, order);
+            buffer = NULL;
+        }
+    }
+
+    if ( buffer )
+        free_xenheap_pages(buffer, order);
+
+    return -EINVAL;
+}
+

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 129

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+int gicv3_its_init(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    for (i = 0; i < 8; i++)

Coding style: for ( ... )

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 142

Quoted 13 lines
xen/arch/arm/gic-its.c +13 -0
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
+        int type;
+
+        reg = readq_relaxed(basereg);
+        type = (reg >> 56) & 0x7;
+        switch ( type )
+        {
+        case GITS_BASER_TYPE_NONE:
+            continue;
+        case GITS_BASER_TYPE_DEVICE:
+            /* TODO: find some better way of limiting the number of devices */
+            gicv3_map_baser(basereg, reg, 1024);

Flat table may use a huge amount of memory on some platform (depending how many device are present and how the ID space is sparsed). It would probably worth to add a TODO regarding the support of one/two-level table.

Also, the limitation could be dropped if we use the one/two-level table as the amount of memory pre-allocated will be smaller.

Quoted 117 lines
xen/arch/arm/gic-its.c +12 -0
+            break;
+        case GITS_BASER_TYPE_COLLECTION:
+            gicv3_map_baser(basereg, reg, NR_CPUS);
+            break;
+        default:
+            continue;
+        }
+    }
+
+    return 0;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
xen/arch/arm/gic-v3.c +5 -0
index 2534aa5..5cf4618 100644
@@ -29,6 +29,7 @@
 #include <xen/irq.h>
 #include <xen/iocap.h>
 #include <xen/sched.h>
+#include <xen/err.h>
 #include <xen/errno.h>
 #include <xen/delay.h>
 #include <xen/device_tree.h>
@@ -1548,6 +1549,7 @@ static int __init gicv3_init(void)
 {
     int res, i;
     uint32_t reg;
+    struct host_its *hw_its;
 
     if ( !cpu_has_gicv3 )
     {
@@ -1603,6 +1605,9 @@ static int __init gicv3_init(void)
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 
+    list_for_each_entry(hw_its, &host_its_list, entry)
+        gicv3_its_init(hw_its);
+
     spin_unlock(&gicv3.lock);
 
     return res;
xen/include/asm-arm/gic-its.h +44 -1
index 48c6c78..589b889 100644
@@ -18,6 +18,47 @@
 #ifndef __ASM_ARM_ITS_H__
 #define __ASM_ARM_ITS_H__
 
+#define LPI_OFFSET      8192
+
+#define GITS_CTLR       (0x000)
+#define GITS_IIDR       (0x004)
+#define GITS_TYPER      (0x008)
+#define GITS_CBASER     (0x080)
+#define GITS_CWRITER    (0x088)
+#define GITS_CREADR     (0x090)
+#define GITS_BASER0     (0x100)
+#define GITS_BASER1     (0x108)
+#define GITS_BASER2     (0x110)
+#define GITS_BASER3     (0x118)
+#define GITS_BASER4     (0x120)
+#define GITS_BASER5     (0x128)
+#define GITS_BASER6     (0x130)
+#define GITS_BASER7     (0x138)
+
+/* Register bits */
+#define GITS_CTLR_ENABLE     0x1
+#define GITS_IIDR_VALUE      0x34c
+
+#define GITS_BASER_VALID                BIT(63)
+#define GITS_BASER_INDIRECT             BIT(62)
+#define GITS_BASER_INNER_CACHEABILITY_SHIFT        59
+#define GITS_BASER_TYPE_SHIFT           56
+#define GITS_BASER_OUTER_CACHEABILITY_SHIFT        53
+#define GITS_BASER_TYPE_NONE            0UL
+#define GITS_BASER_TYPE_DEVICE          1UL
+#define GITS_BASER_TYPE_VCPU            2UL
+#define GITS_BASER_TYPE_CPU             3UL
+#define GITS_BASER_TYPE_COLLECTION      4UL
+#define GITS_BASER_TYPE_RESERVED5       5UL
+#define GITS_BASER_TYPE_RESERVED6       6UL
+#define GITS_BASER_TYPE_RESERVED7       7UL
+#define GITS_BASER_ENTRY_SIZE_SHIFT     48
+#define GITS_BASER_SHAREABILITY_SHIFT   10
+#define GITS_BASER_PAGE_SIZE_SHIFT      8
+#define GITS_BASER_RO_MASK              ((7UL << GITS_BASER_TYPE_SHIFT) | \
+                                        (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
+                                        GITS_BASER_INDIRECT)
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
@@ -27,6 +68,7 @@ struct host_its {
     const struct dt_device_node *dt_node;
     paddr_t addr;
     paddr_t size;
+    void __iomem *its_base;
 };
 
 extern struct list_head host_its_list;
@@ -42,8 +84,9 @@ void gicv3_its_dt_init(const struct dt_device_node *node);
 uint64_t gicv3_lpi_get_proptable(void);
 uint64_t gicv3_lpi_allocate_pendtable(void);
 
-/* Initialize the host structures for LPIs. */
+/* Initialize the host structures for LPIs and the host ITSes. */
 int gicv3_lpi_init_host_lpis(int nr_lpis);
+int gicv3_its_init(struct host_its *hw_its);
 
 #else
 

xen/include/asm-arm/gic-its.h static inline int gicv3_lpi_init_host_lpis(int nr_lpis) · line 107

Quoted 3 lines
xen/include/asm-arm/gic-its.h +0 -0
@@ -62,6 +105,10 @@ static inline int gicv3_lpi_init_host_lpis(int nr_lpis)
 {
     return 0;
 }

Please add a newline here and ...

xen/include/asm-arm/gic-its.h static inline int gicv3_lpi_init_host_lpis(int nr_lpis) · line 111

Quoted 4 lines
xen/include/asm-arm/gic-its.h +4 -0
+static inline int gicv3_its_init(struct host_its *hw_its)
+{
+    return 0;
+}

here.

xen/include/asm-arm/gic-its.h static inline int gicv3_lpi_init_host_lpis(int nr_lpis) · line 114

Quoted 4 lines
xen/include/asm-arm/gic-its.h +0 -0
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 04/24] ARM: GICv3 ITS: map ITS command buffer

In reply to Andre Przywara

Hello Andre,

Quoted 27 lines · Andre Przywara

Instead of directly manipulating the tables in memory, an ITS driver sends commands via a ring buffer to the ITS h/w to create or alter the LPI mappings.
Allocate memory for that buffer and tell the ITS about it to be able to send ITS commands.

---
 xen/arch/arm/gic-its.c        | 25 +++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  1 +
 2 files changed, 26 insertions(+)
xen/arch/arm/gic-its.c +1 -0
index 40238a2..c8a7a7e 100644
@@ -18,6 +18,7 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/err.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
@@ -56,6 +57,26 @@ static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
     return ret | (addr & GENMASK(51, 48)) >> (48 - 12);
 }

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 68

Quoted 10 lines
xen/arch/arm/gic-its.c +9 -0
 
+static void *gicv3_map_cbaser(void __iomem *cbasereg)
+{
+    uint64_t attr, reg;
+    void *buffer;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+

You could directly use 'reg' here rather than have a temporary variable.

Also what if the shareability/cacheability has been fixed by the hardware?

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 69

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+    buffer = alloc_xenheap_pages(0, 0);

Please document how you decide to use only a 4K page (is there potentially a drawback)? Also I would prefer if you add a define for the size of the command queue. This will be more readable.

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 74

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+    if ( !buffer )
+        return ERR_PTR(-ENOMEM);
+
+    /* We use exactly one 4K page, so the "Size" field is 0. */
+    reg = attr | BIT(63) | (virt_to_maddr(buffer) & GENMASK(51, 12));

Please introduce a define for bit 63. Also masking the address is not useful.

xen/arch/arm/gic-its.c static uint64_t encode_phys_addr(paddr_t addr, int page_bits) · line 75

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+    writeq_relaxed(reg, cbasereg);

Should not we initialize GITS_CWRITER to 0? From the spec the field is reset to an UNKNOWN value (see 8.19.5)?

Quoted 24 lines
xen/arch/arm/gic-its.c +8 -0
+
+    return buffer;
+}
+
 static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
 {
     uint64_t attr;
@@ -149,6 +170,10 @@ int gicv3_its_init(struct host_its *hw_its)
         }
     }
 
+    hw_its->cmd_buf = gicv3_map_cbaser(hw_its->its_base + GITS_CBASER);
+    if ( IS_ERR(hw_its->cmd_buf) )
+        return PTR_ERR(hw_its->cmd_buf);
+
     return 0;
 }
 
xen/include/asm-arm/gic-its.h +0 -0
index 589b889..b2a003f 100644

xen/include/asm-arm/gic-its.h struct host_its { · line 75

Quoted 7 lines
xen/include/asm-arm/gic-its.h +1 -0
@@ -69,6 +69,7 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    void *cmd_buf;
 };
 
 extern struct list_head host_its_list;

Regards,

Signature · 6 lines
Julien Grall

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

Hi,

Quoted 8 lines · Andre Przywara

Andre Przywara (24):
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 host LPI array
ARM: GICv3 ITS: introduce device mapping

Andre and I talked IRL about GICv3 ITS host driver, I will summarize here for the other.

Whilst reviewing the host driver, I was wondering if we could share the driver with Linux. I remember that Vijay's managed to do it in his series [1] and I quite liked the idea. It makes easier to track bugs and errata as we would rely on Linux.

We already did that in quite few place in Xen (e.g SMMUv2).

Any opinions?

Regards,

[1]
https://lists.xenproject.org/archives/html/xen-devel/2016-02/msg00047.html

Quoted 3 lines

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

--
Julien Grall
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

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

#34 lore raw

Re: [RFC PATCH 05/24] ARM: GICv3 ITS: introduce ITS command handling

In reply to Andre Przywara

Hi Andre,

Quoted 18 lines · Andre Przywara

To be able to easily send commands to the ITS, create the respective wrapper functions, which take care of the ring buffer.
The first two commands we implement provide methods to map a collection to a redistributor (aka host core) and to flush the command queue (SYNC). Start using these commands for mapping one collection to each host CPU.

---
 xen/arch/arm/gic-its.c        | 101 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  17 +++++++
 xen/include/asm-arm/gic-its.h |  32 +++++++++++++
 3 files changed, 150 insertions(+)
xen/arch/arm/gic-its.c +0 -0
index c8a7a7e..88397bc 100644

xen/arch/arm/gic-its.c static struct { · line 37

Quoted 5 lines
xen/arch/arm/gic-its.c +2 -0
@@ -33,6 +33,10 @@ static struct {
     int host_lpi_bits;
 } lpi_data;
 
+/* Physical redistributor address */
+static DEFINE_PER_CPU(uint64_t, rdist_addr);

The type should be paddr_t.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 49

Quoted 12 lines
xen/arch/arm/gic-its.c +5 -0
+/* Redistributor ID */
+static DEFINE_PER_CPU(uint64_t, rdist_id);
 /* Pending table for each redistributor */
 static DEFINE_PER_CPU(void *, pending_table);
 
@@ -40,6 +44,86 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define ITS_COMMAND_SIZE        32
+
+static int its_send_command(struct host_its *hw_its, void *its_cmd)

The its_cmd could be const as you don't modify it.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 51

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+{
+    int readp, writep;

Please use uint32_t (or maybe uint64_t) here.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 56

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+
+    spin_lock(&hw_its->cmd_lock);
+
+    readp = readl_relaxed(hw_its->its_base + GITS_CREADR) & GENMASK(19, 5);
+    writep = readl_relaxed(hw_its->its_base + GITS_CWRITER) & GENMASK(19, 5);

Please introduce a define for the GENMASK(19, 5) rather than hardcoding it in multiple place.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 65

Quoted 9 lines
xen/arch/arm/gic-its.c +9 -0
+
+    if ( ((writep + ITS_COMMAND_SIZE) % PAGE_SIZE) == readp )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        return -EBUSY;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_COMMAND_SIZE);
+    __flush_dcache_area(hw_its->cmd_buf + writep, ITS_COMMAND_SIZE);

Why the flush here? From patch #4, the GIC has been configured to be able to snoop the cache. So a dsb(ish) would be enough here.

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 72

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+    writep = (writep + ITS_COMMAND_SIZE) % PAGE_SIZE;
+
+    writeq_relaxed(writep & GENMASK(19, 5), hw_its->its_base + GITS_CWRITER);
+
+    spin_unlock(&hw_its->cmd_lock);
+
+    return 0;

This function return either -EBUSY or 0. Would not it be better to return a bool instead?

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 82

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, int cpu, uint64_t reg)
+{
+    reg &= ~GENMASK(51, 16);
+
+    if ( hw_its->pta )
+        reg |= per_cpu(rdist_addr, cpu) & GENMASK(51, 16);
+    else
+        reg |= per_cpu(rdist_id, cpu) << 16;

I would prefer if we setup the target address at initialize per-cpu rather than doing it every time we send a sync command (or else).

Quoted 31 lines
xen/arch/arm/gic-its.c +31 -0
+
+    return reg;
+}
+
+static int its_send_cmd_sync(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_SYNC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, 0x0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, (collection_id & GENMASK(15, 0)) | BIT(63));
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+void gicv3_its_setup_collection(int cpu)
+{

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 123

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    struct host_its *its;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        /* Only send commands to ITS that have been initialized already. */
+        if ( !its->cmd_buf )
+            continue;
+
+        its_send_cmd_mapc(its, cpu, cpu);
+        its_send_cmd_sync(its, cpu);

Looking at the implementation of its_send_cmd_*, the functions may return an error if the command queue is full. However you don't check the return, and continue as it was fine. We will get in trouble much later.

Furthermore, sending the SYNC command does not meaning the ITS has executed the command. You have to ensure that GITS_CREADR == GITS_CWRITER and I didn't find this code within this series.

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 236

Quoted 13 lines
xen/arch/arm/gic-its.c +6 -0
+    }
+}
+
 #define BASER_ATTR_MASK                                           \
         ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
          (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
@@ -147,6 +231,13 @@ int gicv3_its_init(struct host_its *hw_its)
     if ( !hw_its->its_base )
         return -ENOMEM;
 
+    /* Make sure the ITS is disabled before programming the BASE registers. */
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg & ~GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);

The spec (6.2.1 in IHI 0069C) requires the ITS to be disabled and quiescent before programming the BASE registers. So I don't think this check is enough here.

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 269

Quoted 13 lines
xen/arch/arm/gic-its.c +6 -0
+
+    reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
+    hw_its->pta = reg & GITS_TYPER_PTA;
+
     for (i = 0; i < 8; i++)
     {
         void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
@@ -174,9 +265,18 @@ int gicv3_its_init(struct host_its *hw_its)
     if ( IS_ERR(hw_its->cmd_buf) )
         return PTR_ERR(hw_its->cmd_buf);
 
+    its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
+    its_send_cmd_sync(hw_its, smp_processor_id());

See my comments on the previous its_send_* functions

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 274

Quoted 5 lines
xen/arch/arm/gic-its.c +2 -0
+
     return 0;
 }
 
+void gicv3_set_redist_addr(paddr_t address, int redist_id)

The second parameter should probably be unsigned, maybe uint64_t?

Quoted 30 lines
xen/arch/arm/gic-its.c +6 -0
+{
+    this_cpu(rdist_addr) = address;
+    this_cpu(rdist_id) = redist_id;
+}
+
 uint64_t gicv3_lpi_allocate_pendtable(void)
 {
     uint64_t reg, attr;
@@ -265,6 +365,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->addr = addr;
         its_data->size = size;
         its_data->dt_node = its;
+        spin_lock_init(&its_data->cmd_lock);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/arch/arm/gic-v3.c +2 -0
index 5cf4618..b9387a3 100644
@@ -638,6 +638,8 @@ static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
     table_reg = gicv3_lpi_get_proptable();
     if ( table_reg )
         writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);
+
+    gicv3_its_setup_collection(smp_processor_id());
 }
 
 static int __init gicv3_populate_rdist(void)

xen/arch/arm/gic-v3.c static int __init gicv3_populate_rdist(void) · line 695

Quoted 10 lines
xen/arch/arm/gic-v3.c +7 -0
@@ -684,7 +686,22 @@ static int __init gicv3_populate_rdist(void)
                 this_cpu(rbase) = ptr;
 
                 if ( typer & GICR_TYPER_PLPIS )
+                {
+                    paddr_t rdist_addr;
+
+                    rdist_addr = gicv3.rdist_regions[i].base;
+                    rdist_addr += ptr - gicv3.rdist_regions[i].map_base;
+
+                    /* The ITS refers to redistributors either by their physical

Coding style:

/*
* Foo

xen/arch/arm/gic-v3.c static int __init gicv3_populate_rdist(void) · line 701

Quoted 6 lines
xen/arch/arm/gic-v3.c +6 -0
+                     * address or by their ID. Determine those two values and
+                     * let the ITS code store them in per host CPU variables to
+                     * later be able to address those redistributors.
+                     */
+                    gicv3_set_redist_addr(rdist_addr,
+                                          (typer >> 8) & GENMASK(15, 0));

Please avoid hardcoding mask and use a define.

Quoted 65 lines
xen/arch/arm/gic-v3.c +2 -0
+
                     gicv3_rdist_init_lpis(ptr);
+                }
 
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
xen/include/asm-arm/gic-its.h +25 -0
index b2a003f..b49d274 100644
@@ -37,6 +37,7 @@
 
 /* Register bits */
 #define GITS_CTLR_ENABLE     0x1
+#define GITS_TYPER_PTA       BIT(19)
 #define GITS_IIDR_VALUE      0x34c
 
 #define GITS_BASER_VALID                BIT(63)
@@ -59,6 +60,22 @@
                                         (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
                                         GITS_BASER_INDIRECT)
 
+/* ITS command definitions */
+#define ITS_CMD_SIZE                    32
+
+#define GITS_CMD_MOVI                   0x01
+#define GITS_CMD_INT                    0x03
+#define GITS_CMD_CLEAR                  0x04
+#define GITS_CMD_SYNC                   0x05
+#define GITS_CMD_MAPD                   0x08
+#define GITS_CMD_MAPC                   0x09
+#define GITS_CMD_MAPTI                  0x0a
+#define GITS_CMD_MAPI                   0x0b
+#define GITS_CMD_INV                    0x0c
+#define GITS_CMD_INVALL                 0x0d
+#define GITS_CMD_MOVALL                 0x0e
+#define GITS_CMD_DISCARD                0x0f
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
@@ -69,7 +86,9 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;
     void *cmd_buf;
+    bool pta;
 };
 
 extern struct list_head host_its_list;
@@ -89,6 +108,12 @@ uint64_t gicv3_lpi_allocate_pendtable(void);
 int gicv3_lpi_init_host_lpis(int nr_lpis);
 int gicv3_its_init(struct host_its *hw_its);
 
+/* Set the physical address and ID for each redistributor as read from DT. */
+void gicv3_set_redist_addr(paddr_t address, int redist_id);
+
+/* Map a collection for this host CPU to each host ITS. */
+void gicv3_its_setup_collection(int cpu);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)

xen/include/asm-arm/gic-its.h static inline int gicv3_its_init(struct host_its *hw_its) · line 137

Quoted 3 lines
xen/include/asm-arm/gic-its.h +0 -0
@@ -110,6 +135,13 @@ static inline int gicv3_its_init(struct host_its *hw_its)
 {
     return 0;
 }

Newline here

xen/include/asm-arm/gic-its.h static inline int gicv3_its_init(struct host_its *hw_its) · line 140

Quoted 3 lines
xen/include/asm-arm/gic-its.h +3 -0
+static inline void gicv3_set_redist_addr(paddr_t address, int redist_id)
+{
+}

Ditto

xen/include/asm-arm/gic-its.h static inline int gicv3_its_init(struct host_its *hw_its) · line 147

Quoted 8 lines
xen/include/asm-arm/gic-its.h +4 -0
+static inline void gicv3_its_setup_collection(int cpu)
+{
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif /* __ASSEMBLY__ */

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 06/24] ARM: GICv3 ITS: introduce host LPI array

In reply to Stefano Stabellini

Hi,

Quoted 70 lines · Stefano Stabellini
Quoted 39 lines · Andre Przywara

The number of LPIs on a host can be potentially huge (millions), although in practise will be mostly reasonable. So prematurely allocating an array of struct irq_desc's for each LPI is not an option. However Xen itself does not care about LPIs, as every LPI will be injected into a guest (Dom0 for now).
Create a dense data structure (8 Bytes) for each LPI which holds just enough information to determine the virtual IRQ number and the VCPU into which the LPI needs to be injected.
Also to not artificially limit the number of LPIs, we create a 2-level table for holding those structures.
This patch introduces functions to initialize these tables and to create, lookup and destroy entries for a given LPI.
We allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-its.c        | 154 ++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  18 +++++
 2 files changed, 172 insertions(+)
xen/arch/arm/gic-its.c +2 -0
index 88397bc..2140e4a 100644
@@ -18,18 +18,31 @@
 
 #include <xen/config.h>
 #include <xen/lib.h>
+#include <xen/sched.h>
 #include <xen/err.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/domain.h>
 #include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>

xen/arch/arm/gic-its.c line 40

Quoted 10 lines
xen/arch/arm/gic-its.c +9 -0
 
+/* LPIs on the host always go to a guest, so no struct irq_desc for them. */
+union host_lpi {
+    uint64_t data;
+    struct {
+        uint64_t virt_lpi:32;
+        uint64_t dom_id:16;
+        uint64_t vcpu_id:16;
+    };
+};

Why not the following?

  union host_lpi {
      uint64_t data;
      struct {
          uint32_t virt_lpi;
          uint16_t dom_id;
          uint16_t vcpu_id;
      };
  };
Quoted 7 lines
xen/arch/arm/gic-its.c +1 -0
 /* Global state */
 static struct {
     uint8_t *lpi_property;
+    union host_lpi **host_lpis;
     int host_lpi_bits;
 } lpi_data;
 
Quoted 10 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 61

Quoted 7 lines
xen/arch/arm/gic-its.c +3 -0
@@ -43,6 +56,26 @@ static DEFINE_PER_CPU(void *, pending_table);
 #define MAX_HOST_LPI_BITS                                                \
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
+#define HOST_LPIS_PER_PAGE      (PAGE_SIZE / sizeof(union host_lpi))
+
+static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d)

I take "lpi" is the physical lpi here. Maybe we would rename it to "plpi" for clarity.

+1 here. We tend to use the prefix 'p' for physical and 'v' for virtual (e.g virq/pirq, vcpu/pcpu). I'd like to see the same for the LPIs.

While we are here, I think the function should be named gic_find_plpi.

Quoted 24 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 72

Quoted 13 lines
xen/arch/arm/gic-its.c +11 -0
+{
+    union host_lpi *hlpi;
+
+    if ( lpi < 8192 || lpi >= MAX_HOST_LPIS + 8192 )
+        return NULL;
+
+    lpi -= 8192;
+    if ( !lpi_data.host_lpis[lpi / HOST_LPIS_PER_PAGE] )
+        return NULL;
+
+    hlpi = &lpi_data.host_lpis[lpi / HOST_LPIS_PER_PAGE][lpi % HOST_LPIS_PER_PAGE];

I realize I am sometimes obsessive about this, but division operations are expensive and this is on the hot path, so I would do:

#define HOST_LPIS_PER_PAGE (PAGE_SIZE >> 3)

unsigned int table = lpi / HOST_LPIS_PER_PAGE;

then use table throughout this function.

Quoted 6 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 75

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+    if ( d && hlpi->dom_id != d->domain_id )
+        return NULL;

I think this function is very useful so I would avoid making any domain checks here: one day we might want to retrieve hlpi even if hlpi->dom_id != d->domain_id. I would move the domain check outside.

+1.

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 02/24] ARM: GICv3: allocate LPI pending and property table

In reply to Vijay Kilari

On 24/10/16 15:28, Vijay Kilari wrote:

Hi Vijay,

thanks for having a look!

Quoted 84 lines · Andre Przywara
Quoted 83 lines

The ARM GICv3 ITS provides a new kind of interrupt called LPIs. The pending bits and the configuration data (priority, enable bits) for those LPIs are stored in tables in normal memory, which software has to provide to the hardware.
Allocate the required memory, initialize it and hand it over to each ITS. We limit the number of LPIs we use with a compile time constant to avoid wasting memory.

---
 xen/arch/arm/Kconfig              |  6 ++++
 xen/arch/arm/efi/efi-boot.h       |  1 -
 xen/arch/arm/gic-its.c            | 76 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             | 27 ++++++++++++++
 xen/include/asm-arm/cache.h       |  4 +++
 xen/include/asm-arm/gic-its.h     | 22 +++++++++++-
 xen/include/asm-arm/gic_v3_defs.h | 48 ++++++++++++++++++++++++-
 7 files changed, 181 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +6 -0
index 9fe3b8e..66e2bb8 100644
@@ -50,6 +50,12 @@ config HAS_ITS
         depends on ARM_64
         depends on HAS_GICV3
 
+config HOST_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+
 config ALTERNATIVE
        bool
 
xen/arch/arm/efi/efi-boot.h +0 -1
index 045d6ce..dc64aec 100644
@@ -10,7 +10,6 @@
 #include "efi-dom0.h"
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
-void __flush_dcache_area(const void *vaddr, unsigned long size);
 
 #define DEVICE_TREE_GUID \
 {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}
xen/arch/arm/gic-its.c +23 -0
index 0f42a77..b52dff3 100644
@@ -20,10 +20,86 @@
 #include <xen/lib.h>
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
+#include <asm/p2m.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
 
+/* Global state */
+static struct {
+    uint8_t *lpi_property;
+    int host_lpi_bits;
+} lpi_data;
+
+/* Pending table for each redistributor */
+static DEFINE_PER_CPU(void *, pending_table);
+
+#define MAX_HOST_LPI_BITS                                                \
+        min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
+#define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
+
+uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    uint64_t reg, attr;
+    void *pendtable;
+
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;
+
Quoted 8 lines

xen/arch/arm/gic-its.c line 55

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+    /*
+     * The pending table holds one bit per LPI, so we need three bits less
+     * than the number of LPI_BITs. But the alignment requirement from the
+     * ITS is 64K, so make order at least 16 (-12).
+     */
+    pendtable = alloc_xenheap_pages(MAX(lpi_data.host_lpi_bits - 3, 16) - 12, 0);

The pend table size allocated is differ from proptable size?

According to the spec the pending table always covers all LPIs that the ITS advertises, consequently GICR_PENDBASER has no field to indicate the size of the table.

Quoted 6 lines

xen/arch/arm/gic-its.c line 59

Quoted 5 lines
xen/arch/arm/gic-its.c +4 -0
+    if ( !pendtable )
+        return 0;
+
+    memset(pendtable, 0, BIT(lpi_data.host_lpi_bits - 3));

memset size is different from allocated size?

We just clean what the ITS needs. A potentially bigger allocation above is just to match the alignment requirement. I didn't find a nice function to allocate pages with a specific alignment beyond a single page.

Quoted 7 lines

flushing zeroed pendtable?

xen/arch/arm/gic-its.c line 63

Quoted 4 lines
xen/arch/arm/gic-its.c +4 -0
+    this_cpu(pending_table) = pendtable;
+
+    reg  = attr | GICR_PENDBASER_PTZ;
+    reg |= virt_to_maddr(pendtable) & GENMASK(51, 16);

can use __pa instead of virt_to_maddr()
Isn't GENMASK(47, 12) here?

Please download the newest revision (issue C) of the spec. Issue B extended physical address space to cover 52 bits in many places.

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+    return reg;
+}
+
+uint64_t gicv3_lpi_get_proptable()
+{
+    uint64_t attr;
+    static uint64_t reg = 0;
+
+    /* The property table is shared across all redistributors. */
Quoted 8 lines

xen/arch/arm/gic-its.c line 79

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+    if ( reg )
+        return reg;
+
+    attr  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;

using PENDBASER definitions for PROPBASER?

Good catch.

Quoted 57 lines

xen/arch/arm/gic-its.c line 89

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+    lpi_data.lpi_property = alloc_xenheap_pages(MAX_HOST_LPI_BITS - 12, 0);
+    if ( !lpi_data.lpi_property )
+        return 0;
+
+    memset(lpi_data.lpi_property, GIC_PRI_IRQ | LPI_PROP_RES1, MAX_HOST_LPIS);
+    __flush_dcache_area(lpi_data.lpi_property, MAX_HOST_LPIS);
+
+    reg  = attr | ((MAX_HOST_LPI_BITS - 1) << 0);
+    reg |= virt_to_maddr(lpi_data.lpi_property) & GENMASK(51, 12);

Isn't GENMASK(47, 12)?

Quoted 46 lines
xen/arch/arm/gic-its.c +13 -0
+
+    return reg;
+}
+
+int gicv3_lpi_init_host_lpis(int lpi_bits)
+{
+    lpi_data.host_lpi_bits = lpi_bits;
+
+    printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/gic-v3.c +15 -0
index 238da84..2534aa5 100644
@@ -546,6 +546,9 @@ static void __init gicv3_dist_init(void)
     type = readl_relaxed(GICD + GICD_TYPER);
     nr_lines = 32 * ((type & GICD_TYPE_LINES) + 1);
 
+    if ( type & GICD_TYPE_LPIS )
+        gicv3_lpi_init_host_lpis(((type >> GICD_TYPE_ID_BITS_SHIFT) & 0x1f) + 1);
+
     printk("GICv3: %d lines, (IID %8.8x).\n",
            nr_lines, readl_relaxed(GICD + GICD_IIDR));
 
@@ -615,6 +618,26 @@ static int gicv3_enable_redist(void)
 
     return 0;
 }
+static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+
+    if ( list_empty(&host_its_list) )
+        return;
+
+    /* Make sure LPIs are disabled before setting up the BASERs. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    writel_relaxed(reg & ~GICR_CTLR_ENABLE_LPIS, rdist_base + GICR_CTLR);
+
Quoted 8 lines

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 637

Quoted 5 lines
xen/arch/arm/gic-v3.c +5 -0
+    table_reg = gicv3_lpi_allocate_pendtable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);
+
+    table_reg = gicv3_lpi_get_proptable();

Here LPI property table is allocated per cpu. One property table
should be enough and can be shared by all cpus.

The function is called _get_ and not _allocate_, multiple calls to it returns the same pointer, allocated on the first incarnation by the magic of a function-local, static variable. See the above:

	if ( reg ) return reg;
Quoted 5 lines

xen/arch/arm/gic-v3.c static int gicv3_enable_redist(void) · line 639

Quoted 2 lines
xen/arch/arm/gic-v3.c +2 -0
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PROPBASER);

After updating GICR_PENDBASER and GICR_PROPBASER regs
shouldn't we read back and check if sharability bits are support by HW or not like it is done in linux driver?

Possibly.

Cheers,
Andre.

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

#65 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara

Hi Andre,

Quoted 39 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +2 -1
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #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 <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */

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

Quoted 10 lines
xen/arch/arm/vgic-v3.c +5 -3
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);

The field PTZ read as 0.

Quoted 17 lines
xen/arch/arm/vgic-v3.c +1 -5
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;

xen/arch/arm/vgic-v3.c read_unknown: · line 338

Quoted 10 lines
xen/arch/arm/vgic-v3.c +7 -0
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;

Newline here please.

xen/arch/arm/vgic-v3.c read_unknown: · line 351

Quoted 13 lines
xen/arch/arm/vgic-v3.c +13 -0
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}

I am not sure to understand why we need to sanitise the value here. From my understanding of the spec (see 8.11.18 in IHI 0069C) we should support any shareability/cacheability, correct?

Quoted 29 lines
xen/arch/arm/vgic-v3.c +29 -0
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,

xen/arch/arm/vgic-v3.c read_unknown: · line 390

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);

Why do you mask the bits 51:48. There is no restriction in Xen about the size of the IPA (though 52 bits support is part of ARMv8.2), so we should avoid to open-code mask everywhere in the code. Otherwise it will be more painful to extend the number of bits supported.

FWIW, all the p2m code is checking whether the IPA is supported.

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,

xen/arch/arm/vgic-v3.c read_unknown: · line 407

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);

Ditto.

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

Please pass a gfn_t rather than paddr_t.

xen/arch/arm/vgic-v3.c read_unknown: · line 419

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;

s/pages/mfns/

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;

xen/arch/arm/vgic-v3.c read_unknown: · line 439

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);

get_page_from_gfn can fail if you try to get a page on memory that is not baked by a RAM region. Also get_page_from_gfn will work on foreign mapping, we don't want the guest using foreing memory (e.g memory belonging to another domain) for the ITS internal memory.

Also, please try to pay attention for error path whilst you write code. It is a pain to handle them after the code has been written. I will try to point them when I spot it.

xen/arch/arm/vgic-v3.c read_unknown: · line 440

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);

You cannot assume a 1:1 mapping between the IPA and the PA. Please use the struct page_info returned by get_page_from_gfn

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem to use
it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

Quoted 35 lines
xen/arch/arm/vgic-v3.c +26 -0
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     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 515

Quoted 7 lines
xen/arch/arm/vgic-v3.c +1 -3
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {

Coding style, the { should be on a line.

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

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        int nr_pages;

unsigned int

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

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Newline here for clarity. Also please use vgic_reg64_check_access rather than open-coding it.

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

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )

From my understanding VGIC_V3_LPIS_ENABLED is set when the guest enable LPIs on this re-distributor. However, this check is not safe as GICR_CTLR.Enable_LPIs may be set concurrently (the re-distributors are accessible from any vCPU).

Also, when ITS is not available we should avoid to handle the register (i.e treating as write ignore). My rational here is we should limit the amount of emulation exposed to the guest whenever it is possible.

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

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+            return 1;

I think we should at least print warning as writing to GICR_PROPBASER when GICR_CTLR.Enable_LPIs is set is unpredictable. IHMO, I would even crash the guest.

The code below likely needs locking as the property table is common to all re-distributor, hence could be modified concurrently. Also, I would like to see a comment on top of emulation of GICR_TYPER to mention that all re-distributor shares the same common property table (GICR_TYPER.CommonLPIAff = 0).

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

Quoted 7 lines
xen/arch/arm/vgic-v3.c +6 -0
+
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;

The spec (see 8.11.19): "If the value of this field is larger than the value of GICD_TYPER.IDbits, the GICD_TYPER.IDbits value applies). We don't want to map more than necessary uin

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

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);

This looks wrong to me. A guest could specify a size different from the previous write.

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

Quoted 9 lines
xen/arch/arm/vgic-v3.c +6 -2
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Newline + vgic_reg64_check_access

Also, you don't check whether the LPIs have been enabled here.

All my comments above stands. Furthermore, the code is not correctly indented (you are using hard tab).

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

Quoted 8 lines
xen/arch/arm/vgic-v3.c +8 -0
+	reg = v->arch.vgic.rdist_pendbase;
+	vgic_reg64_update(&reg, r, info);
+	reg = sanitize_pendbaser(reg);
+	v->arch.vgic.rdist_pendbase = reg;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);
+	v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);

The pending table is never touched by Xen. So I would avoid to mapping it.

Quoted 33 lines
xen/arch/arm/vgic-v3.c +1 -0
+	return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +4 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +2 -0
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
 #endif
     } vgic;
 

xen/include/asm-arm/domain.h struct arch_vcpu · line 252

Quoted 5 lines
xen/include/asm-arm/domain.h +1 -1
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */

Please avoid spurious change. We don't require in Xen to have all the constant aligned. This also makes harder to got through the changes.

xen/include/asm-arm/domain.h struct arch_vcpu · line 253

Quoted 1 line
xen/include/asm-arm/domain.h +1 -0
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)

Please document the purpose of this bit.

xen/include/asm-arm/gic-its.h int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 140

Quoted 12 lines
xen/include/asm-arm/domain.h +2 -0
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +0 -0
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)

s/lpi/vlpi/ to make clear this is a function deal with virtual LPIs.

xen/include/asm-arm/gic-its.h int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 142

Quoted 3 lines
xen/include/asm-arm/gic-its.h +1 -1
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;

I think this is the best place to ask this question, I don't see any code within this series to check that the guest effectively initialized proptable and the size is correct (you don't check that the guest provided enough memory compare to the vLPI suggested).

FWIW, I have only already made those comments back Vijay sent his patch series. It might be worth for you to look at what he did regarding all the sanity checks.

xen/include/asm-arm/gic-its.h int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 143

Quoted 1 line
xen/include/asm-arm/gic-its.h +1 -0
+}

Newline here for clarity.

xen/include/asm-arm/gic-its.h int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 144

Quoted 1 line
xen/include/asm-arm/gic-its.h +1 -0
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)

Ditto for the naming.

xen/include/asm-arm/gic-its.h static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi) · line 191

Quoted 9 lines
xen/include/asm-arm/gic-its.h +2 -0
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }

Newline here for clarity.

Quoted 13 lines
xen/include/asm-arm/gic-its.h +4 -0
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +0 -0
index 4e29ba6..2b216cc 100644

xen/include/asm-arm/vgic.h VGIC_REG_HELPERS(32, 0x3); · line 293

Quoted 9 lines
xen/include/asm-arm/vgic.h +3 -0
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Quoted 11 lines · Julien Grall

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem
to use it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

I don't like the idea of using the vmap for this either, but the problem with the copy_*_guest approach is that it only maps one page at a time. It is unable to map multiple pages contiguously, which seems to be required here.

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

#19 lore raw

Re: [RFC PATCH 03/24] ARM: GICv3 ITS: allocate device and collection table

In reply to Vijay Kilari

Hi,

Quoted 10 lines · Vijay Kilari
Quoted 7 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (usually the PCI b/d/f triplet) and an EventID (the MSI payload or interrupt ID) to a pair of LPI number and collection ID, which points to the target CPU.
This mapping is stored in the device and collection tables, which software has to provide for the ITS to use.
Allocate the required memory and hand it the ITS.
We limit the number of devices to cover 4 PCI busses for now.

Thunderx has more than 4 PCI busses

Yeah, I am thinking about a proper solution for that hack. We may use a default of 4 buses and allow platform to override this. Or make this a configuration value.
Or copy the Linux behaviour by limiting the number of pages to some sensible value (16MB, if I got this correctly).
Anyway I think we need indirect table support to save Thunder from allocating too much memory for this.

Quoted 33 lines
Quoted 33 lines
---
 xen/arch/arm/gic-its.c        | 114 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |   5 ++
 xen/include/asm-arm/gic-its.h |  49 +++++++++++++++++-
 3 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +10 -0
index b52dff3..40238a2 100644
@@ -21,6 +21,7 @@
 #include <xen/device_tree.h>
 #include <xen/libfdt/libfdt.h>
 #include <asm/p2m.h>
+#include <asm/io.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic-its.h>
@@ -38,6 +39,119 @@ static DEFINE_PER_CPU(void *, pending_table);
         min_t(unsigned int, lpi_data.host_lpi_bits, CONFIG_HOST_LPI_BITS)
 #define MAX_HOST_LPIS   (BIT(MAX_HOST_LPI_BITS) - 8192)
 
+#define BASER_ATTR_MASK                                           \
+        ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
+         (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
+         (0x7UL << GITS_BASER_INNER_CACHEABILITY_SHIFT))
+#define BASER_RO_MASK   (GENMASK(52, 48) | GENMASK(58, 56))
+
+static uint64_t encode_phys_addr(paddr_t addr, int page_bits)
+{
+    uint64_t ret;
Quoted 8 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 56

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+
+    if ( page_bits < 16)
+        return (uint64_t)addr & GENMASK(47, page_bits);
+
+    ret = addr & GENMASK(47, 16);
+    return ret | (addr & GENMASK(51, 48)) >> (48 - 12);

why this mask and shift for?.

According to the (current issue of the) spec bits 48-51 of the address are stored in bits 12-15 of the register.

Quoted 22 lines
Quoted 22 lines
xen/arch/arm/gic-its.c +22 -0
+}
+
+static int gicv3_map_baser(void __iomem *basereg, uint64_t regc, int nr_items)
+{
+    uint64_t attr;
+    int entry_size = (regc >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f;
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */
+    for (pagesz = 0; pagesz < 3; pagesz++)
+    {
+        uint64_t reg;
+        int nr_bytes;
Quoted 8 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 84

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+
+        nr_bytes = ROUNDUP(nr_items * entry_size, BIT(pagesz * 2 + 12));
+        order = get_order_from_bytes(nr_bytes);
+
+        if ( !buffer )
+            buffer = alloc_xenheap_pages(order, 0);

Don't we need to reset to zero all the pages before handing
memory to ITS hw?

True. Will fix it.

Quoted 45 lines
Quoted 45 lines
xen/arch/arm/gic-its.c +45 -0
+        if ( !buffer )
+            return -ENOMEM;
+
+        reg  = attr;
+        reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+        reg |= nr_bytes >> (pagesz * 2 + 12);
+        reg |= regc & BASER_RO_MASK;
+        reg |= GITS_BASER_VALID;
+        reg |= encode_phys_addr(virt_to_maddr(buffer), pagesz * 2 + 12);
+
+        writeq_relaxed(reg, basereg);
+        regc = readl_relaxed(basereg);
+
+        /* The host didn't like our attributes, just use what it returned. */
+        if ( (regc & BASER_ATTR_MASK) != attr )
+            attr = regc & BASER_ATTR_MASK;
+
+        /* If the host accepted our page size, we are done. */
+        if ( (reg & (3UL << GITS_BASER_PAGE_SIZE_SHIFT)) == pagesz )
+            return 0;
+
+        /* Check whether our buffer is aligned to the next page size already. */
+        if ( !(virt_to_maddr(buffer) & (BIT(pagesz * 2 + 12 + 2) - 1)) )
+        {
+            free_xenheap_pages(buffer, order);
+            buffer = NULL;
+        }
+    }
+
+    if ( buffer )
+        free_xenheap_pages(buffer, order);
+
+    return -EINVAL;
+}
+
+int gicv3_its_init(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    for (i = 0; i < 8; i++)
Quoted 8 lines

xen/arch/arm/gic-its.c static DEFINE_PER_CPU(void *, pending_table); · line 135

Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;
+        int type;
+
+        reg = readq_relaxed(basereg);
+        type = (reg >> 56) & 0x7;

define a macro for these constants

Sure.

Cheers,
Andre.

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

#67 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Stefano Stabellini

Hi Stefano,

Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Julien Grall

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem
to use it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

Quoted 7 lines
Quoted 2 lines

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

I don't like the idea of using the vmap for this either, but the problem with the copy_*_guest approach is that it only maps one page at a time. It is unable to map multiple pages contiguously, which seems to be required here.

We will get into trouble very quickly with the vmap solution. The memory provisioned by the ITS can be quite big. Each GITS_BASER can hold up to 16MB (if I computed correctly) of memory.

The question is why do we need to map the page contiguously? Can we do in a different way?

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Quoted 21 lines · Julien Grall

Hi Stefano,

Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Julien Grall

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem
to use it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

Quoted 7 lines
Quoted 2 lines

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

I don't like the idea of using the vmap for this either, but the problem with the copy_*_guest approach is that it only maps one page at a time. It is unable to map multiple pages contiguously, which seems to be required here.

Quoted 6 lines

We will get into trouble very quickly with the vmap solution. The memory provisioned by the ITS can be quite big. Each GITS_BASER can hold up to 16MB (if I computed correctly) of memory.

The question is why do we need to map the page contiguously? Can we do in a different way?

I agree with Julien: if we can get away with mapping one page at a time without performance penalties, we should do it. Keep in mind that on ARM64 Xen doesn't actually need to map anything because the whole physical memory is already mapped.

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

#78 lore raw

Re: [RFC PATCH 12/24] 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).

The ITS is a complex piece so I think it would be good to describe more in the commit message how this will work. Also a documentation in the tree would be very good to help understanding the code.

Quoted 62 lines
---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +34 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#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-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>

xen/arch/arm/vgic-its.c line 44

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;

CWRITER and CREADR are registers so they need to be described in term of number of bits. Also, while the top word of CREADR/CWRITER is RES0. I would much prefer to see uint64_t rather than uint32_t as this is the real size of the register.

xen/arch/arm/vgic-its.c line 46

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;

Please describe what contains baser0 and baser1. If I understand correctly the code, baser0 will be store Device information whilst baser1 the collection.

xen/arch/arm/vgic-its.c line 47

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    uint16_t *coll_table;

What is the layout of the device table?

xen/arch/arm/vgic-its.c line 48

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    int max_collections;

unsigned int

xen/arch/arm/vgic-its.c line 49

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    uint64_t *dev_table;

What is the layout of the device table?

xen/arch/arm/vgic-its.c line 50

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    int max_devices;

unsigned int.

xen/arch/arm/vgic-its.c line 54

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+    bool enabled;
+};
+
+/* An Interrupt Translation Table Entry: this is indexed by a

Coding style:

/*
* Foo

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{

xen/arch/arm/vgic-its.c line 68

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,

Please make this function inline.

xen/arch/arm/vgic-its.c line 69

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+                                   int word, int shift, int size)

unsigned for all those parameters.

xen/arch/arm/vgic-its.c line 71

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);

It is probably better to use BIT_ULL (see my explanation on previous patches).

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+}
+
+#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)

xen/arch/arm/vgic-its.c line 86

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+#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 here.

xen/arch/arm/vgic-its.c line 94

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;

You return an error value but the caller does not check it. Should not the caller do a different action when the return -1? If not, it should be documented.

xen/arch/arm/vgic-its.c line 96

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+
+    spin_lock(&its->vcmd_lock);

I am quite concerned about this locking.

xen/arch/arm/vgic-its.c line 101

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))

Coding style: switch ( ... )

xen/arch/arm/vgic-its.c line 105

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

The indentation is wrong.

xen/arch/arm/vgic-its.c line 107

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",

gdprintk will happen XENLOG_GUEST, so you can use XENLOG_WARNING here.

Also s/%ld/%lu/

xen/arch/arm/vgic-its.c line 108

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+                   its_cmd_get_command(cmdptr));

Should not we report the error to the default, or crash it? We tend to do the latter on Xen for constrained unpredictable behavior.

xen/arch/arm/vgic-its.c line 116

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;

I think its->cwriter should be updated before the loop. So another vCPU could read the correct CWRITER whilst this vCPU is executing the commands.

xen/arch/arm/vgic-its.c line 127

Quoted 11 lines
xen/arch/arm/vgic-its.c new file +11 -0
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/* The physical address is encoded slightly differently depending on

Coding style:

/*
* foo

xen/arch/arm/vgic-its.c line 133

Quoted 6 lines
xen/arch/arm/vgic-its.c new file +6 -0
+ * 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) )

Please document what is bit 9.

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+

xen/arch/arm/vgic-its.c line 148

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+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);

Please use a define for BIT(31). Also, technically the ITS is not quiescent when command are executed (GITS_CTLR could be read from another vCPU).

xen/arch/arm/vgic-its.c line 155

Quoted 7 lines
xen/arch/arm/vgic-its.c new file +7 -0
+	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;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 156

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        *r = vgic_reg64_extract(0x1eff1, info);

Please document the value and add defines. Vijay's mentioned about the number of device IDs, but the number of collection likely needs to be dynamic as it depends on the number of vCPUs.

xen/arch/arm/vgic-its.c line 159

Quoted 3 lines
xen/arch/arm/vgic-its.c new file +3 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 163

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 167

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 171

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 175

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

310 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#76 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Vijay Kilari

Hi,

Quoted 182 lines · Vijay Kilari
Quoted 181 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-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +149 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#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-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    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
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * 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;
Quoted 8 lines

xen/arch/arm/vgic-its.c line 156

Quoted 7 lines
xen/arch/arm/vgic-its.c new file +7 -0
+    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);

GITS_TYPER.HCC is not set. Should be max vcpus of the domain

HCC is clear on purpose. We want the guest to provide memory for everything that it allocates, to avoid it to hog Xen with allocations.

Quoted 1 line

GITS_TYPER.ID_bits are also just set to 15.

Yeah, I guess it should match what the hardware ITS provides, to not impose an artificial limit here.

Quoted 29 lines
Quoted 19 lines
xen/arch/arm/vgic-its.c new file +19 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

xen/arch/arm/vgic-its.c line 185

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        *r = vgic_reg64_extract(its->baser1, info);
+        break;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0, info);
+        break;
+    case VREG32(GICD_PIDR2):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GICV3_GICD_PIDR2, info);
+        break;
Quoted 7 lines

missing default

xen/arch/arm/vgic-its.c line 190

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+    }
+
+    return 1;
+
+bad_width:

print would be helpful

Yes.

Quoted 63 lines
Quoted 63 lines
xen/arch/arm/vgic-its.c new file +63 -0
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+/******************************
+ * ITS registers write access *
+ ******************************/
+
+static int its_baser_table_size(uint64_t baser)
+{
+    int page_size = 0;
+
+    switch ( (baser >> 8) & 3 )
+    {
+    case 0: page_size = SZ_4K; break;
+    case 1: page_size = SZ_16K; break;
+    case 2:
+    case 3: page_size = SZ_64K; break;
+    }
+
+    return page_size * ((baser & GENMASK(7, 0)) + 1);
+}
+
+static int its_baser_nr_entries(uint64_t baser)
+{
+    int entry_size = ((baser & GENMASK(52, 48)) >> 48) + 1;
+
+    return its_baser_table_size(baser) / entry_size;
+}
+
+static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,
+                                  register_t r, void *priv)
+{
+    struct domain *d = v->domain;
+    struct virt_its *its = priv;
+    uint64_t reg;
+    uint32_t ctlr;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        ctlr = its->enabled ? GITS_CTLR_ENABLE : 0;
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+       vgic_reg32_update(&ctlr, r, info);
+       its->enabled = ctlr & GITS_CTLR_ENABLE;
+       /* TODO: trigger something ... */
+        return 1;
+    case VREG32(GITS_IIDR):
+        goto write_ignore_32;
+    case VREG32(GITS_TYPER):
+        goto write_ignore_32;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->cbaser;
+        vgic_reg64_update(&reg, r, info);
+        /* TODO: sanitise! */
+        its->cbaser = reg;
Quoted 8 lines

xen/arch/arm/vgic-its.c line 257

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+
+        if ( reg & BIT(63) )
+        {
+            its->cmdbuf = map_guest_pages(d, reg & GENMASK(51, 12), 1);

Only one page of guest cmd queue is mapped. After cwriter
moving beyond 1 page,
panic is observed. Map all the guest pages

Right, good catch.

Cheers,
Andre.

Quoted 9 lines

xen/arch/arm/vgic-its.c line 261

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->cmdbuf, 1);

Same here.

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+            its->cmdbuf = NULL;
+        }
+
+       return 1;

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

#44 lore raw

Re: [RFC PATCH 07/24] ARM: GICv3 ITS: introduce device mapping

In reply to Vijay Kilari

Hi,

Quoted 47 lines · Vijay Kilari
Quoted 46 lines · Andre Przywara

The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host.
For this we have to map each device that Dom0 may request to a host ITS device with the same identifier.
Allocate the respective memory and enter each device into a list to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-its.c        | 90 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h | 16 ++++++++
 2 files changed, 106 insertions(+)
xen/arch/arm/gic-its.c +25 -0
index 2140e4a..bf1f5b5 100644
@@ -168,6 +168,94 @@ static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             int size, uint64_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = size & GENMASK(4, 0);
+    cmd[2] = itt_addr & GENMASK(51, 8);
+    if ( valid )
+        cmd[2] |= BIT(63);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid)
+{
+    void *itt_addr = NULL;
+    struct its_devices *dev, *temp;
+    bool reuse_dev = false;
+
+    list_for_each_entry_safe(dev, temp, &hw_its->its_devices, entry)
+    {
+        if ( (dev->d->domain_id != d->domain_id) || (dev->devid != devid) )
Quoted 8 lines

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 202

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+            continue;
+
+        its_send_cmd_mapd(hw_its, dev->devid, 0, 0, false);
+        xfree(dev->itt_addr);
+        if ( !valid )
+        {
+            xfree(dev);

xfree() should be done after list_del()

Oh, indeed.

Quoted 24 lines
Quoted 24 lines
xen/arch/arm/gic-its.c +24 -0
+            list_del(&dev->entry);
+
+            return 0;
+        }
+
+        reuse_dev = true;
+        break;
+    }
+
+    if ( !valid )
+        return 0;
+
+    itt_addr = _xmalloc(BIT(bits) * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        return -ENOMEM;
+
+    if ( !reuse_dev )
+    {
+        dev = xmalloc(struct its_devices);
+        if ( !dev )
+            return -ENOMEM;
+
+        list_add_tail(&dev->entry, &hw_its->its_devices);
+    }
Quoted 8 lines

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 233

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+
+    dev->itt_addr = itt_addr;
+    dev->d = d;
+    dev->devid = devid;
+
+    return its_send_cmd_mapd(hw_its, devid, bits - 1,
+                             itt_addr ? virt_to_maddr(itt_addr) : 0, true);

check on itt_addr is redundant

Ah, yes, this is an artifact of an earlier version. Thanks for spotting this.

Quoted 21 lines
Quoted 9 lines
xen/arch/arm/gic-its.c +8 -0
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+int its_remove_domain(struct domain *d)
+{
+    struct host_its *its;
+    struct its_devices *dev, *temp;
+

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 251

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        list_for_each_entry_safe(dev, temp, &its->its_devices, entry)
+        {
+            if ( dev->d->domain_id != d->domain_id )
+                continue;
+
+            its_send_cmd_mapd(its, dev->devid, 0, 0, false);
+            xfree(dev->itt_addr);
+            xfree(dev);

xfree() should be done after list_del(

Quoted 3 lines

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 253

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+            list_del(&dev->entry);
+        }

This code is same as above. Can be moved to a separate function?

Probably. Will take a look.

Thanks,
Andre.

Quoted 70 lines

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 388

Quoted 13 lines
xen/arch/arm/gic-its.c +6 -0
+    }
+
+    return 0;
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 void gicv3_its_setup_collection(int cpu)
 {
@@ -297,6 +385,7 @@ int gicv3_its_init(struct host_its *hw_its)
 
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     hw_its->pta = reg & GITS_TYPER_PTA;
+    hw_its->itte_size = ((reg >> 4) & 0xf) + 1;

can define a macro for these constants

Quoted 56 lines
xen/arch/arm/gic-its.c +1 -0
     for (i = 0; i < 8; i++)
     {
@@ -520,6 +609,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->size = size;
         its_data->dt_node = its;
         spin_lock_init(&its_data->cmd_lock);
+        INIT_LIST_HEAD(&its_data->its_devices);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/include/asm-arm/gic-its.h +16 -1
index 512a388..4e9841a 100644
@@ -79,6 +79,13 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
+struct its_devices {
+    struct list_head entry;
+    struct domain *d;
+    void *itt_addr;
+    int devid;
+};
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -88,6 +95,8 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    struct list_head its_devices;
+    int itte_size;
     bool pta;
 };
 
@@ -114,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* 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.
+ */
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid);
+
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
--

2.9.0

Quoted 5 lines
Quoted 4 lines

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

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

#48 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Vijay Kilari

Hi,

Quoted 21 lines · Vijay Kilari
Quoted 20 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
Quoted 11 lines

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 510

Quoted 10 lines
xen/arch/arm/gic.c +2 -0
index 63c744a..ebe4035 100644
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )

Can define something line is_lpi(irq) and use it everywhere

Yes, that was on my list anyway.

Quoted 85 lines
Quoted 70 lines
xen/arch/arm/gic.c +1 -0
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +19 -2
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
+    if ( virq >= 8192 )
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    /* TODO: locking! */
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

With this approach of allocating pending_irq on demand, if the
pending_lpi_list
is at n position then it iterates for long time to find pending_irq entry. This will increase LPI injection time to domain.

Quoted 1 line

Why can't we use btree?

That's an optimization. You will find that the actual number of interrupts on a VCPU at any given time is very low, especially if we look at LPIs only. So my hunch is that it's either 0, 1 or 2, not more. So for simplicity I'd keep it as a list for now. If we see issues, we can amend this at any time.

Quoted 60 lines
Quoted 60 lines
xen/arch/arm/vgic.c +24 -1
+
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);
+
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
Quoted 14 lines

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 142

Quoted 13 lines
xen/include/asm-arm/domain.h +0 -0
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +4 -0
index 4e9841a..1f881c0 100644
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;

Why lpi priority is fixed?. can't we use domain set lpi priority?

Mmh, looks like a rebase artifact. It is fixed if the ITS isn't configured, but should just be the prototype if not.
The final file has it right, so I guess this gets amended in some later patch. Thanks for spotting this.

Cheers,
Andre.

Quoted 49 lines
Quoted 49 lines
xen/include/asm-arm/gic-its.h +6 -0
+}
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -1
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
--

2.9.0

Quoted 5 lines
Quoted 4 lines

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

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

#61 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Vijay Kilari

Hi,

Quoted 148 lines · Vijay Kilari
Quoted 147 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +86 -9
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #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 <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
Quoted 8 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

I think this file is not right place to put this generic function

Yeah, possibly.

Quoted 17 lines
Quoted 17 lines
xen/arch/arm/vgic-v3.c +17 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
Quoted 8 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 439

Quoted 6 lines
xen/arch/arm/vgic-v3.c +6 -0
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);

check return value of this function

Yes.

Quoted 13 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 451

Quoted 12 lines
xen/arch/arm/vgic-v3.c +12 -0
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);
+    }
+
+    ptr = vmap(pages, nr_pages);
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)

same here. Can be put in generic file p2m.c?

Quoted 10 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 460

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);

can use _pa()

Do you mean __pa()? Which is defined to be exactly virt_to_maddr()? I prefer the more verbose version, which is more readable, IMHO.

Quoted 36 lines
Quoted 36 lines
xen/arch/arm/vgic-v3.c +20 -3
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;
+
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;
+
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
Quoted 2 lines

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

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;

should be validated against HOST_LPIS?

I don't think so. The actual LPI numbers are totally independent between host and Dom0.
So why and how should this be matched?

Quoted 12 lines

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

Quoted 11 lines
xen/arch/arm/vgic-v3.c +8 -2
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
Quoted 1 line

check on VGIC_V3_LPIS_ENABLED is required

Right, forgot this.

Quoted 7 lines

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

Quoted 6 lines
xen/arch/arm/vgic-v3.c +6 -0
+       reg = v->arch.vgic.rdist_pendbase;
+       vgic_reg64_update(&reg, r, info);
+       reg = sanitize_pendbaser(reg);
+       v->arch.vgic.rdist_pendbase = reg;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);

why only 16 pages are unmapped?

Well, it matches the allocation below, but I agree that this should match the advertised number of LPIs in GICD_TYPER.

Cheers,
Andre.

Quoted 92 lines
Quoted 92 lines
xen/arch/arm/vgic-v3.c +3 -0
+       v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);
+       return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +4 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +6 -1
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +9 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +3 -1
index 4e29ba6..2b216cc 100644
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*
--

2.9.0

Quoted 5 lines
Quoted 4 lines

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

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

#100 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Vijay Kilari

Hi,

Quoted 74 lines · Vijay Kilari
Quoted 73 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +45 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
Quoted 8 lines

xen/arch/arm/gic-its.c uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id) · line 720

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }

AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

Cheers,
Andre.

Quoted 76 lines
Quoted 76 lines
xen/arch/arm/gic-its.c +8 -0
+
+    /* Tell all ITSes that they should update the property table for CPU 0,
+     * which is where we map all LPIs to.
+     */
+    list_for_each_entry(its, &host_its_list, entry)
+        its_send_cmd_invall(its, 0);
+}
+
 void gicv3_lpi_set_enable(struct host_its *its,
                           uint32_t deviceid, uint32_t eventid,
                           uint32_t host_lpi, bool enabled)
xen/arch/arm/vgic-its.c +30 -0
index 74da8fc..1e429b7 100644
@@ -294,6 +294,33 @@ out_unlock:
     return ret;
 }
 
+/* INVALL updates the per-LPI configuration status for every LPI mapped to
+ * this redistributor. For the guest side we don't need to update anything,
+ * as we always refer to the actual table for the enabled bit and the
+ * priority.
+ * Enabling or disabling a virtual LPI however needs to be propagated to
+ * the respective host LPI. Instead of iterating over all mapped LPIs in our
+ * emulated GIC (which is expensive due to the required on-demand mapping),
+ * we iterate over all mapped _host_ LPIs and filter for those which are
+ * forwarded to this virtual redistributor.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !vcpu )
+        return -1;
+
+    gicv3_lpi_update_configurations(vcpu, its->d->arch.vgic.proptable);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -515,6 +542,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;
xen/include/asm-arm/gic-its.h +2 -1
index 2cdb3e1..ba6b2d5 100644
@@ -146,6 +146,8 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
 
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable);
+
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
--

2.9.0

Quoted 5 lines
Quoted 4 lines

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

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

#62 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara

Hi,

Quoted 8 lines · Andre Przywara
Quoted 4 lines · Vijay Kilari

xen/arch/arm/vgic-v3.c read_unknown: · line 460

Quoted 2 lines · Andre Przywara
xen/arch/arm/vgic-v3.c +2 -0
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);

can use _pa()

Do you mean __pa()? Which is defined to be exactly virt_to_maddr()? I prefer the more verbose version, which is more readable, IMHO.

FWIW, __pa tends to be more used than virt_to_maddr within the source base.

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara

Hello Andre,

Quoted 15 lines · Andre Przywara
Quoted 14 lines · Vijay Kilari
Quoted 13 lines · Andre Przywara
xen/arch/arm/vgic-its.c new file +13 -0
+    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):
+        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);
Quoted 4 lines
Quoted 1 line

GITS_TYPER.HCC is not set. Should be max vcpus of the domain

HCC is clear on purpose. We want the guest to provide memory for everything that it allocates, to avoid it to hog Xen with allocations.

Whilst I agree that we want to limit the memory allocated by Xen itself,
each collection entry is just 16-bit. So unless we want to support a very big number of collection, I don't see any reason to request the guest to provision memory.

This makes the code more complex and you also have to validate the collection every time.

I remembered minimum number of collection an implementation has to support is "max_vcpus + 1" but I can't find again the statement in the spec.

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara

Hi Andre,

Quoted 18 lines · Andre Przywara
xen/arch/arm/vgic.c +18 -0
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    /* TODO: locking! */
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 482

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
+
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);

xzalloc can return NULL if we fail to allocate memory.

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 493

Quoted 11 lines
xen/arch/arm/vgic.c +11 -0
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+

Regards,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 13/24] ARM: vITS: handle CLEAR command

In reply to Andre Przywara

Hi Andre,

Quoted 17 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.

In addition this patch introduces the lookup function which translates a given DeviceID/EventID pair into a pointer to our vITTE structure.

---
 xen/arch/arm/vgic-its.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
xen/arch/arm/vgic-its.c +0 -0
index 875b992..99d9e9c 100644

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

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

This comment is a call to have an ASSERT in the function.

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

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

Please use unsigned int.

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

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

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+
+    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))

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-its.c struct vits_itte · line 92

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+#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;

Please use INVALID_PADDR here.

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

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

Ditto.

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

Quoted 7 lines
xen/arch/arm/vgic-its.c +7 -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

Coding style:

/*
* Foo

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

Quoted 4 lines
xen/arch/arm/vgic-its.c +4 -0
+ * 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-its.c struct vits_itte · line 109

Quoted 3 lines
xen/arch/arm/vgic-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-its.c struct vits_itte · line 114

Quoted 5 lines
xen/arch/arm/vgic-its.c +5 -0
+{
+    paddr_t addr = get_itte_address(its, devid, evid);
+    struct vits_itte *itte;
+
+    if (addr == ~0)

Coding style: if ( ... )

And return INVALID_PADDR.

Quoted 51 lines
xen/arch/arm/vgic-its.c +44 -0
+        return NULL;
+
+    /* TODO: check locking for map_guest_pages() */
+    itte = map_guest_pages(its->d, addr & PAGE_MASK, 1);
+    if ( !itte )
+        return NULL;
+
+    return itte + (addr & ~PAGE_MASK) / sizeof(struct vits_itte);
+}
+
+/* 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);
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/
@@ -80,6 +147,51 @@ 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 vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vcpu = get_vcpu_from_collection(its, itte->collection);
+    if ( !vcpu )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vlpi = itte->vlpi;
+
+    put_devid_evid(its, itte);

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

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    spin_unlock(&its->its_lock);
+
+    /* 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. */

NIT: s/availabe/available/

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

Quoted 2 lines
xen/arch/arm/vgic-its.c +2 -0
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;

This code should be in a separate helper. It will be helpful to make the structure available again easily without open coding it.

Quoted 9 lines
xen/arch/arm/vgic-its.c +5 -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,

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

Quoted 5 lines
xen/arch/arm/vgic-its.c +2 -0
@@ -100,6 +212,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-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 220

Quoted 5 lines
xen/arch/arm/vgic-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
#79 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara
Quoted 128 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-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +96 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 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; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ */
+
+#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-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    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
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * 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);

xen/arch/arm/vgic-its.c line 106

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+
+    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;

indentation

Quoted 22 lines
xen/arch/arm/vgic-its.c new file +22 -0
+        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

xen/arch/arm/vgic-its.c line 137

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+ * 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);
+}

I would simplify the code by supporting only one page size, maybe 4K.

xen/arch/arm/vgic-its.c line 149

Quoted 12 lines
xen/arch/arm/vgic-its.c new file +12 -0
+
+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;

indentation

xen/arch/arm/vgic-its.c line 156

Quoted 7 lines
xen/arch/arm/vgic-its.c new file +7 -0
+    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 #define 0x1eff1

Quoted 15 lines
xen/arch/arm/vgic-its.c new file +15 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

xen/arch/arm/vgic-its.c line 181

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->baser1, info);
+        break;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0, info);
+        break;

I notice that this patch lacks the code to initialize the vits registers to sensible defaults. For example, who initializes the entry size (52:48) of GITS_BASER?

Quoted 46 lines
xen/arch/arm/vgic-its.c new file +46 -0
+    case VREG32(GICD_PIDR2):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GICV3_GICD_PIDR2, info);
+        break;
+    }
+
+    return 1;
+
+bad_width:
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+/******************************
+ * ITS registers write access *
+ ******************************/
+
+static int its_baser_table_size(uint64_t baser)
+{
+    int page_size = 0;
+
+    switch ( (baser >> 8) & 3 )
+    {
+    case 0: page_size = SZ_4K; break;
+    case 1: page_size = SZ_16K; break;
+    case 2:
+    case 3: page_size = SZ_64K; break;
+    }
+
+    return page_size * ((baser & GENMASK(7, 0)) + 1);
+}
+
+static int its_baser_nr_entries(uint64_t baser)
+{
+    int entry_size = ((baser & GENMASK(52, 48)) >> 48) + 1;
+
+    return its_baser_table_size(baser) / entry_size;
+}
+
+static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,
+                                  register_t r, void *priv)
+{
+    struct domain *d = v->domain;
+    struct virt_its *its = priv;
+    uint64_t reg;

xen/arch/arm/vgic-its.c line 237

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+    uint32_t ctlr;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        ctlr = its->enabled ? GITS_CTLR_ENABLE : 0;
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+	vgic_reg32_update(&ctlr, r, info);
+	its->enabled = ctlr & GITS_CTLR_ENABLE;
+	/* TODO: trigger something ... */

indentation

xen/arch/arm/vgic-its.c line 247

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        return 1;
+    case VREG32(GITS_IIDR):
+        goto write_ignore_32;
+    case VREG32(GITS_TYPER):
+        goto write_ignore_32;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )

It is worth printing an error (gdprintk).

xen/arch/arm/vgic-its.c line 252

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+            return 1;
+
+        reg = its->cbaser;
+        vgic_reg64_update(&reg, r, info);
+        /* TODO: sanitise! */

Yeah, we really need to do that :-)

xen/arch/arm/vgic-its.c line 257

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+        its->cbaser = reg;
+
+        if ( reg & BIT(63) )
+        {
+            its->cmdbuf = map_guest_pages(d, reg & GENMASK(51, 12), 1);

This is only one page, there is no need to use the vmap.

xen/arch/arm/vgic-its.c line 265

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->cmdbuf, 1);
+            its->cmdbuf = NULL;
+        }
+
+	return 1;

indentation

xen/arch/arm/vgic-its.c line 278

Quoted 13 lines
xen/arch/arm/vgic-its.c new file +13 -0
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        reg = its->cwriter;
+        vgic_reg64_update(&reg, r, info);
+        vgic_its_handle_cmds(d, its, reg);
+        return 1;
+    case VREG64(GITS_CREADR):
+        goto write_ignore_64;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )

please add a warning

xen/arch/arm/vgic-its.c line 286

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+            return 1;
+
+        reg = its->baser0;
+        vgic_reg64_update(&reg, r, info);
+
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint64_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT;

Why not | with its->baser0?

xen/arch/arm/vgic-its.c line 288

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        /* TODO: sanitise! */

Indeed

xen/arch/arm/vgic-its.c line 288

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        /* TODO: locking(?) */

vITS stuff can be modified concurrently by two or more vCPUs, so anything that changes a shared state accessible by multiple vCPUs need a lock.

154 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#82 lore raw

Re: [RFC PATCH 13/24] ARM: vITS: handle CLEAR command

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

In addition this patch introduces the lookup function which translates a given DeviceID/EventID pair into a pointer to our vITTE structure.

---
 xen/arch/arm/vgic-its.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
xen/arch/arm/vgic-its.c +19 -0
index 875b992..99d9e9c 100644
@@ -61,6 +61,73 @@ struct vits_itte
     uint64_t collection:16;
 };
 
+#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))

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

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+#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;

Please #define the error

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

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

same here

Quoted 14 lines
xen/arch/arm/vgic-its.c +14 -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)
+{

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

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    paddr_t addr = get_itte_address(its, devid, evid);
+    struct vits_itte *itte;
+
+    if (addr == ~0)
+        return NULL;
+
+    /* TODO: check locking for map_guest_pages() */
+    itte = map_guest_pages(its->d, addr & PAGE_MASK, 1);
+    if ( !itte )
+        return NULL;

No need to use the vmap to map 1 page

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

Quoted 1 line
xen/arch/arm/vgic-its.c +1 -0
+    return itte + (addr & ~PAGE_MASK) / sizeof(struct vits_itte);

Please use () around the div operation for clarity

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

Quoted 6 lines
xen/arch/arm/vgic-its.c +6 -0
+}
+
+/* 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);

No need for this, once you use __pa instead of the vmap

Quoted 30 lines
xen/arch/arm/vgic-its.c +23 -0
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/
@@ -80,6 +147,51 @@ 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 vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vcpu = get_vcpu_from_collection(its, itte->collection);
+    if ( !vcpu )
+    {

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

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vlpi = itte->vlpi;
+
+    put_devid_evid(its, itte);
+    spin_unlock(&its->its_lock);
+
+    /* Remove a pending, but not yet injected guest IRQ. */

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 30 lines
xen/arch/arm/vgic-its.c +17 -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;
+    }
+
+    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,
@@ -100,6 +212,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

#85 lore raw

Re: [RFC PATCH 14/24] ARM: vITS: handle INT command

In reply to Andre Przywara
Quoted 22 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-its.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
xen/arch/arm/vgic-its.c +6 -0
index 99d9e9c..7072753 100644
@@ -192,6 +192,37 @@ 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 vits_itte *itte;
+    struct vcpu *vcpu;

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

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    int ret = -1;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    vcpu = its->d->vcpu[itte->collection];

We need to check that itte->collection is a valid vcpu before using as array index.

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

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    vlpi = itte->vlpi;
+
+    ret = 0;
+
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+
+    if ( !ret)

code style

Quoted 21 lines
xen/arch/arm/vgic-its.c +8 -0
+        vgic_vcpu_inject_irq(vcpu, vlpi);
+
+    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,
@@ -215,6 +246,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 · 1 line
2.9.0

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