Skip to contentSkip to conversation

New VGIC(-v2) implementation

RFC PATCH In review Andre Przywara 3 participants 154 messages Feb 9, 2018 – Mar 2, 2018 Page 1 of 2

lore
Share & tools
lore
#0 lore raw

cover New VGIC(-v2) implementation

tl;dr: More preparatory patches from patch 07, actual new VGIC starting at patch 20.
=============

During development of the Dom0 ITS MSI support last year we realised that the existing GIC interrupt controller emulation has some shortcomings. After some tries to fix those in the existing code, it was agreed upon that the problems are fundamental and a new implementation based on the "new VGIC" in KVM is the best choice.
This is the first drop of this new VGIC implementation. It lives in the xen/arch/arm/vgic/ directory and is written to be a compile time option, so people can choose whether to use the new VGIC or the existing implementation. This is just for a transitional period, the old VGIC is expected to be removed after confidence in the new implementation has grown.

This series starts with some GICv3 redistributor cleanup, which I posted before. I need to incorporate the comments from the list, but for now I left those patches as it from the previous post.

Starting with patch 07 there are some more cleanups and preparations for the existing VGIC/GIC code. A big part of those patches are preparations to properly support level triggered interrupts. This is one of the biggest problems in the existing VGIC, which only correctly emulates edge triggered IRQs. This affects both arch code and some users like the timer and the event channel.

Starting with patch 20 we plumb in the new VGIC then. This is done in a new directory, with all the files actually not wired into the build system until the very last patch. The idea is to split the series into reviewable chunks without resorting to nasty hacks to keep bisectability. The code was forked from Linux' virt/kvm/arm/vgic/, as of 4.14-rc7, plus some recent changes to improve support for level triggered and hardware mapped interrupts, which is what we use heavily in Dom0. The code was heavily adapted to fit into Xen, starting with using the Xen coding style and using Xen structure and variable names (struct domain instead of struct kvm, for instance). Where interfacing functions were similar enough, they were changed over to the existing Xen name and prototypes (for instance kvm_vgic_create() was renamed to domain_vgic_register()). As far as possible the code layout and split was re-used from KVM, so patches in Linux should be relatively easy to port into Xen. Due to the mentioned changes this can not be done easily in an automatic way, but it should be not too complicated to extract the gist of the patch and re-apply this to our code base.

The actual VGIC code splits into several parts:
- The core is the struct vgic_irq, which holds every information about a virtual IRQ, including a per-IRQ lock. Also there is on (ordered) per-VCPU list (ap_list), which links the interrupts to be considered by a VCPU. There are functions to deal with queuing and removing IRQs from those lists safely, obeying the locking order. (patches 20-23)
- There are functions to push vIRQs on a VCPU list to the list registers, and handle their state changes. (patches 24-26)
- The distributor MMIO emulation is using separate functions per register, also having read and write split. (patches 27-37)
- There are functions to deal with Xen specialities. (patches 38-43)
- The data structures and the wiring of the emulation into the hypervisor and the guests are done in vgic-init.c. (patches 44-47)
- Finally patch 49 enables the build of the new VGIC. This requires to increase the size limit for struct vcpu in patch 48.

Please consider this series the first post that it is. It was briefly tested on a Juno and a Midway (with and without CONFIG_NEW_VGIC). It does not cover GICv3 yet, though supporting this should be relatively straightforward. In fact I have most of the code here already, but I wanted to wait for initial feedback before applying similar transformations to the *-v3.c files. Also this does not include ITS support, though the code itself is more ready for that than the old VGIC ever was.
I did some semi-automatic translation of coding style, so I am sure there are quite some leftovers that slipped through, including identifiers with kvm_ in their name ;-)

I would appreciate if somehow could have a look at those patches, patch 20 and following are surely the most interesting ones to have a look at.

Cheers,
Andre

Andre Przywara (49):
tools: ARM: vGICv3: avoid inserting optional DT properties
ARM: vGICv3: drop GUEST_GICV3_RDIST_REGIONS symbol
ARM: GICv3: use hardware GICv3 redistributor regions for Dom0
ARM: GICv3: simplify GICv3 redistributor stride handling
ARM: vGICv3: always use architected redist stride
ARM: vGICv3: remove rdist_stride from VGIC structure
ARM: VGIC: move gic_remove_from_lr_pending() prototype
ARM: VGIC: move max_vcpus VGIC limit to struct arch_domain
ARM: VGIC: change to level-IRQ compatible IRQ injection interface
ARM: VGIC: carve out struct vgic_cpu and struct vgic_dist
ARM: VGIC: reorder prototypes in vgic.h
ARM: VGIC: introduce gic_get_nr_lrs()
ARM: VGIC: Add hypervisor base address to vgic_v2_setup_hw()
ARM: VGIC: extend GIC CPU interface definitions
ARM: GIC: Allow tweaking the active state of an IRQ
ARM: GIC: allow reading pending state of a hardware IRQ
ARM: timer: Handle level triggered IRQs correctly
ARM: evtchn: Handle level triggered IRQs correctly
ARM: vPL011: Use the VGIC's level triggered IRQs handling if available
ARM: new VGIC: Add data structure definitions
ARM: new VGIC: Add acccessor to new struct vgic_irq instance
ARM: new VGIC: Implement virtual IRQ injection
ARM: new VGIC: Add IRQ sorting
ARM: new VGIC: Add IRQ sync/flush framework
ARM: new VGIC: Add GICv2 world switch backend
ARM: new VGIC: Implement vgic_vcpu_pending_irq
ARM: new VGIC: Add MMIO handling framework
ARM: new VGIC: Add GICv2 MMIO handling framework
ARM: new VGIC: Add CTLR, TYPER and IIDR handlers
ARM: new VGIC: Add ENABLE registers handlers
ARM: new VGIC: Add PENDING registers handlers
ARM: new VGIC: Add ACTIVE registers handlers
ARM: new VGIC: Add PRIORITY registers handlers
ARM: new VGIC: Add CONFIG registers handlers
ARM: new VGIC: Add TARGET registers handlers
ARM: new VGIC: Add SGIR register handler
ARM: new VGIC: Add SGIPENDR register handlers
ARM: new VGIC: handle hardware mapped IRQs
ARM: new VGIC: Add event channel IRQ handling
ARM: new VGIC: Handle virtual IRQ allocation/reservation
ARM: new VGIC: dump virtual IRQ info
ARM: new VGIC: provide system register emulation stub
ARM: new VGIC: Add preliminary stub implementations
ARM: new VGIC: vgic-init: register VGIC
ARM: new VGIC: vgic-init: implement vgic_init
ARM: new VGIC: vgic-init: implement map_resources
ARM: new VGIC: Add vgic_v2_enable
ARM: allocate two pages for struct vcpu
ARM: VGIC: wire new VGIC(-v2) files into Xen build system

 tools/libxl/libxl_arm.c           |   8 -
 xen/arch/arm/Kconfig              |   6 +-
 xen/arch/arm/Makefile             |  10 +-
 xen/arch/arm/domain.c             |  25 +-
 xen/arch/arm/gic-v2.c             |  20 +-
 xen/arch/arm/gic-v3-lpi.c         |   2 +-
 xen/arch/arm/gic-v3.c             |  72 ++--
 xen/arch/arm/gic-vgic.c           |  10 +-
 xen/arch/arm/gic.c                |  15 +
 xen/arch/arm/irq.c                |   2 +-
 xen/arch/arm/time.c               |  36 +-
 xen/arch/arm/traps.c              |   2 +
 xen/arch/arm/vgic-v2.c            |   3 +-
 xen/arch/arm/vgic-v3.c            |  45 +-
 xen/arch/arm/vgic.c               |  46 +-
 xen/arch/arm/vgic/vgic-init.c     | 259 ++++++++++++
 xen/arch/arm/vgic/vgic-mmio-v2.c  | 297 +++++++++++++
 xen/arch/arm/vgic/vgic-mmio.c     | 589 ++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic-mmio.h     | 194 +++++++++
 xen/arch/arm/vgic/vgic-v2.c       | 339 +++++++++++++++
 xen/arch/arm/vgic/vgic.c          | 871 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.h          |  69 +++
 xen/arch/arm/vpl011.c             |   6 +-
 xen/arch/arm/vtimer.c             |   4 +-
 xen/common/Makefile               |   1 +
 xen/common/list_sort.c            | 170 ++++++++
 xen/include/asm-arm/arm_vgic.h    | 269 ++++++++++++
 xen/include/asm-arm/domain.h      |  92 +---
 xen/include/asm-arm/event.h       |   1 +
 xen/include/asm-arm/gic.h         |  31 +-
 xen/include/asm-arm/gic_v3_defs.h |   5 +
 xen/include/asm-arm/vgic.h        | 170 ++++++--
 xen/include/public/arch-arm.h     |   3 -
 xen/include/xen/list_sort.h       |  11 +
 xen/include/xen/timer.h           |   2 +
 35 files changed, 3456 insertions(+), 229 deletions(-)
 create mode 100644 xen/arch/arm/vgic/vgic-init.c
 create mode 100644 xen/arch/arm/vgic/vgic-mmio-v2.c
 create mode 100644 xen/arch/arm/vgic/vgic-mmio.c
 create mode 100644 xen/arch/arm/vgic/vgic-mmio.h
 create mode 100644 xen/arch/arm/vgic/vgic-v2.c
 create mode 100644 xen/arch/arm/vgic/vgic.c
 create mode 100644 xen/arch/arm/vgic/vgic.h
 create mode 100644 xen/common/list_sort.c
 create mode 100644 xen/include/asm-arm/arm_vgic.h
 create mode 100644 xen/include/xen/list_sort.h
Signature · 7 lines
2.14.1


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

RFC PATCH 1/49 tools: ARM: vGICv3: avoid inserting optional DT properties

When creating a GICv3 devicetree node, we currently insert the redistributor-stride and #redistributor-regions properties, with fixed values which are actually the architected ones. But those properties are optional and only needed to cover for broken platforms, where the values differ from the architected one. This will never be the case for the constructed DomU memory map.
So we drop those properties altogether and provide a clean and architected GICv3 DT node for DomUs.

---
 tools/libxl/libxl_arm.c | 8 --------
 1 file changed, 8 deletions(-)
tools/libxl/libxl_arm.c +0 -8
index 3e46554301..b5bba3cd33 100644
@@ -524,14 +524,6 @@ static int make_gicv3_node(libxl__gc *gc, void *fdt)
     res = fdt_property(fdt, "interrupt-controller", NULL, 0);
     if (res) return res;
 
-    res = fdt_property_cell(fdt, "redistributor-stride",
-                            GUEST_GICV3_RDIST_STRIDE);
-    if (res) return res;
-
-    res = fdt_property_cell(fdt, "#redistributor-regions",
-                            GUEST_GICV3_RDIST_REGIONS);
-    if (res) return res;
-
     res = fdt_property_regs(gc, fdt, ROOT_ADDRESS_CELLS, ROOT_SIZE_CELLS,
                             2,
                             gicd_base, gicd_size,
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 01/49] tools: ARM: vGICv3: avoid inserting optional DT properties

Hi,

AFAICT, patch #1-#6 were sent separately and were reviewed. I will skip them for now, but please address the comments on the next version.

Cheers,

Quoted 34 lines · Andre Przywara

When creating a GICv3 devicetree node, we currently insert the redistributor-stride and #redistributor-regions properties, with fixed values which are actually the architected ones. But those properties are optional and only needed to cover for broken platforms, where the values differ from the architected one. This will never be the case for the constructed DomU memory map.
So we drop those properties altogether and provide a clean and architected GICv3 DT node for DomUs.

---
  tools/libxl/libxl_arm.c | 8 --------
  1 file changed, 8 deletions(-)
tools/libxl/libxl_arm.c +0 -8
index 3e46554301..b5bba3cd33 100644
@@ -524,14 +524,6 @@ static int make_gicv3_node(libxl__gc *gc, void *fdt)
      res = fdt_property(fdt, "interrupt-controller", NULL, 0);
      if (res) return res;
 
-    res = fdt_property_cell(fdt, "redistributor-stride",
-                            GUEST_GICV3_RDIST_STRIDE);
-    if (res) return res;
-
-    res = fdt_property_cell(fdt, "#redistributor-regions",
-                            GUEST_GICV3_RDIST_REGIONS);
-    if (res) return res;
-
      res = fdt_property_regs(gc, fdt, ROOT_ADDRESS_CELLS, ROOT_SIZE_CELLS,
                              2,
                              gicd_base, gicd_size,
Signature · 6 lines
Julien Grall

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

RFC PATCH 2/49 ARM: vGICv3: drop GUEST_GICV3_RDIST_REGIONS symbol

In reply to Andre Przywara

Architecturally there is only one GICv3 redistributor region. Drop the symbol which suggested that was a delibarate choice for Xen guests, instead hard code the "1" in the appropriate places, along with a comment to explain the reasons.

---
 xen/arch/arm/vgic-v3.c        | 17 ++++++++++++-----
 xen/include/public/arch-arm.h |  1 -
 2 files changed, 12 insertions(+), 6 deletions(-)
xen/arch/arm/vgic-v3.c +12 -5
index 2ad8a6be62..12338c6b21 100644
@@ -1632,8 +1632,18 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
 
 static inline unsigned int vgic_v3_rdist_count(struct domain *d)
 {
-    return is_hardware_domain(d) ? vgic_v3_hw.nr_rdist_regions :
-               GUEST_GICV3_RDIST_REGIONS;
+    /*
+     * Architecturally there is only one GICv3 redistributor region.
+     * The GICv3 DT binding provisions for multiple regions, since there are
+     * platforms out there which break this architectural assumption.
+     * ACPI does not support this workaround at all.
+     * For Dom0 we have to live with the MMIO layout the hardware provides,
+     * so we have to copy the multiple regions - as the first region may not
+     * provide enough space to hold all redistributors we need.
+     * However DomU get a constructed memory map, so we can go with
+     * the architected single redistributor region.
+     */
+    return is_hardware_domain(d) ? vgic_v3_hw.nr_rdist_regions : 1;
 }
 
 static int vgic_v3_domain_init(struct domain *d)
@@ -1692,9 +1702,6 @@ static int vgic_v3_domain_init(struct domain *d)
     {
         d->arch.vgic.dbase = GUEST_GICV3_GICD_BASE;
 
-        /* XXX: Only one Re-distributor region mapped for the guest */
-        BUILD_BUG_ON(GUEST_GICV3_RDIST_REGIONS != 1);
-
         d->arch.vgic.rdist_stride = GUEST_GICV3_RDIST_STRIDE;
 
         /* The first redistributor should contain enough space for all CPUs */
xen/include/public/arch-arm.h +0 -1
index 05fd11ca38..ca79ab6284 100644
@@ -402,7 +402,6 @@ typedef uint64_t xen_callback_t;
 #define GUEST_GICV3_GICD_SIZE      xen_mk_ullong(0x00010000)
 
 #define GUEST_GICV3_RDIST_STRIDE   xen_mk_ullong(0x00020000)
-#define GUEST_GICV3_RDIST_REGIONS  1
 
 #define GUEST_GICV3_GICR0_BASE     xen_mk_ullong(0x03020000) /* vCPU0..127 */
 #define GUEST_GICV3_GICR0_SIZE     xen_mk_ullong(0x01000000)
Signature · 7 lines
2.14.1


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

RFC PATCH 3/49 ARM: GICv3: use hardware GICv3 redistributor regions for Dom0

In reply to Andre Przywara

The code to generate the DT node or MADT table for Dom0 reaches into the domain's VGIC structure to learn the number of redistributor regions and their base addresses.
Since those values are copied from the hardware, we can as well use those hardware values directly when setting up the hardware domain.

This avoids the hardware GIC code to reference vGIC data structures, making this variable VGIC internal.

---
 xen/arch/arm/gic-v3.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
xen/arch/arm/gic-v3.c +6 -8
index 25c30bb9ea..bdca77417c 100644
@@ -1167,8 +1167,7 @@ static int gicv3_make_hwdom_dt_node(const struct domain *d,
     if ( res )
         return res;
 
-    res = fdt_property_cell(fdt, "#redistributor-regions",
-                            d->arch.vgic.nr_regions);
+    res = fdt_property_cell(fdt, "#redistributor-regions", gicv3.rdist_count);
     if ( res )
         return res;
 
@@ -1178,7 +1177,7 @@ static int gicv3_make_hwdom_dt_node(const struct domain *d,
      * CPU interface and virtual cpu interfaces accessesed as System registers
      * So cells are created only for Distributor and rdist regions
      */
-    new_len = new_len * (d->arch.vgic.nr_regions + 1);
+    new_len = new_len * (gicv3.rdist_count + 1);
 
     hw_reg = dt_get_property(gic, "reg", &len);
     if ( !hw_reg )
@@ -1406,13 +1405,13 @@ static int gicv3_make_hwdom_madt(const struct domain *d, u32 offset)
 
     /* Add Generic Redistributor */
     size = sizeof(struct acpi_madt_generic_redistributor);
-    for ( i = 0; i < d->arch.vgic.nr_regions; i++ )
+    for ( i = 0; i < gicv3.rdist_count; i++ )
     {
         gicr = (struct acpi_madt_generic_redistributor *)(base_ptr + table_len);
         gicr->header.type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
         gicr->header.length = size;
-        gicr->base_address = d->arch.vgic.rdist_regions[i].base;
-        gicr->length = d->arch.vgic.rdist_regions[i].size;
+        gicr->base_address = gicv3.rdist_regions[i].base;
+        gicr->length = gicv3.rdist_regions[i].size;
         table_len += size;
     }
 
@@ -1425,8 +1424,7 @@ static unsigned long gicv3_get_hwdom_extra_madt_size(const struct domain *d)
 {
     unsigned long size;
 
-    size = sizeof(struct acpi_madt_generic_redistributor)
-           * d->arch.vgic.nr_regions;
+    size = sizeof(struct acpi_madt_generic_redistributor) * gicv3.rdist_count;
 
     size += sizeof(struct acpi_madt_generic_translator)
             * vgic_v3_its_count(d);
Signature · 7 lines
2.14.1


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

RFC PATCH 4/49 ARM: GICv3: simplify GICv3 redistributor stride handling

In reply to Andre Przywara

Instead of hard coding the architected redistributor stride into the code, lets use a clear #define to the two values for GICv3 and GICv4 and clarify the algorithm to determine the needed stride value.

---
 xen/arch/arm/gic-v3.c             | 18 ++++++++++--------
 xen/include/asm-arm/gic_v3_defs.h |  5 +++++
 2 files changed, 15 insertions(+), 8 deletions(-)
xen/arch/arm/gic-v3.c +10 -8
index bdca77417c..7837d93dc1 100644
@@ -690,6 +690,15 @@ static int __init gicv3_populate_rdist(void)
         do {
             typer = readq_relaxed(ptr + GICR_TYPER);
 
+            /* Set the architectural redist size if not overridden by DT. */
+            if ( !gicv3.rdist_stride )
+            {
+                if ( typer & GICR_TYPER_VLPIS )
+                    gicv3.rdist_stride = GICV4_GICR_SIZE;
+                else
+                    gicv3.rdist_stride = GICV3_GICR_SIZE;
+            }
+
             if ( (typer >> 32) == aff )
             {
                 this_cpu(rbase) = ptr;
@@ -732,14 +741,7 @@ static int __init gicv3_populate_rdist(void)
             if ( gicv3.rdist_regions[i].single_rdist )
                 break;
 
-            if ( gicv3.rdist_stride )
-                ptr += gicv3.rdist_stride;
-            else
-            {
-                ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
-                if ( typer & GICR_TYPER_VLPIS )
-                    ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
-            }
+            ptr += gicv3.rdist_stride;
 
         } while ( !(typer & GICR_TYPER_LAST) );
     }
xen/include/asm-arm/gic_v3_defs.h +5 -0
index 65c9dc47cf..412e41afed 100644
@@ -18,6 +18,8 @@
 #ifndef __ASM_ARM_GIC_V3_DEFS_H__
 #define __ASM_ARM_GIC_V3_DEFS_H__
 
+#include <xen/sizes.h>
+
 /*
  * Additional registers defined in GIC v3.
  * Common GICD registers are defined in gic.h
@@ -68,6 +70,9 @@
 #define GICV3_GICD_IIDR_VAL          0x34c
 #define GICV3_GICR_IIDR_VAL          GICV3_GICD_IIDR_VAL
 
+#define GICV3_GICR_SIZE              (2 * SZ_64K)
+#define GICV4_GICR_SIZE              (4 * SZ_64K)
+
 #define GICR_CTLR                    (0x0000)
 #define GICR_IIDR                    (0x0004)
 #define GICR_TYPER                   (0x0008)
Signature · 7 lines
2.14.1


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

RFC PATCH 5/49 ARM: vGICv3: always use architected redist stride

In reply to Andre Przywara

The redistributor-stride property in a GICv3 DT node is only there to cover broken platforms where this value deviates from the architected one. Since we emulate the GICv3 distributor even for Dom0, we don't need to copy the broken behaviour. All the special handling for Dom0s using GICv3 is just for using the hardware's memory map, which is unaffected by the redistributor stride - it can never be smaller than the architected two pages.
Remove the redistributor-stride property from Dom0's DT node and also remove the code that tried to reuse the hardware value for Dom0's GICv3 emulation.

---
 xen/arch/arm/gic-v3.c  |  5 -----
 xen/arch/arm/vgic-v3.c | 14 ++++++--------
 2 files changed, 6 insertions(+), 13 deletions(-)
xen/arch/arm/gic-v3.c +0 -5
index 7837d93dc1..02c85e4c0c 100644
@@ -1164,11 +1164,6 @@ static int gicv3_make_hwdom_dt_node(const struct domain *d,
     if ( res )
         return res;
 
-    res = fdt_property_cell(fdt, "redistributor-stride",
-                            d->arch.vgic.rdist_stride);
-    if ( res )
-        return res;
-
     res = fdt_property_cell(fdt, "#redistributor-regions", gicv3.rdist_count);
     if ( res )
         return res;
xen/arch/arm/vgic-v3.c +6 -8
index 12338c6b21..e45bbc6dcf 100644
@@ -1024,10 +1024,9 @@ static struct vcpu *get_vcpu_from_rdist(struct domain *d,
     paddr_t gpa, uint32_t *offset)
 {
     struct vcpu *v;
-    uint32_t stride = d->arch.vgic.rdist_stride;
     unsigned int vcpu_id;
 
-    vcpu_id = region->first_cpu + ((gpa - region->base) / stride);
+    vcpu_id = region->first_cpu + ((gpa - region->base) / GICV3_GICR_SIZE);
     if ( unlikely(vcpu_id >= d->max_vcpus) )
         return NULL;
 
@@ -1586,7 +1585,6 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
 
     /* Convenient alias */
     struct domain *d = v->domain;
-    uint32_t rdist_stride = d->arch.vgic.rdist_stride;
 
     /*
      * Find the region where the re-distributor lives. For this purpose,
@@ -1602,11 +1600,11 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
 
     /* Get the base address of the redistributor */
     rdist_base = region->base;
-    rdist_base += (v->vcpu_id - region->first_cpu) * rdist_stride;
+    rdist_base += (v->vcpu_id - region->first_cpu) * GICV3_GICR_SIZE;
 
     /* Check if a valid region was found for the re-distributor */
     if ( (rdist_base < region->base) ||
-         ((rdist_base + rdist_stride) > (region->base + region->size)) )
+         ((rdist_base + GICV3_GICR_SIZE) > (region->base + region->size)) )
     {
         dprintk(XENLOG_ERR,
                 "d%u: Unable to find a re-distributor for VCPU %u\n",
@@ -1622,7 +1620,7 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
      * VGIC_V3_RDIST_LAST flags.
      * Note that we are assuming max_vcpus will never change.
      */
-    last_cpu = (region->size / rdist_stride) + region->first_cpu - 1;
+    last_cpu = (region->size / GICV3_GICR_SIZE) + region->first_cpu - 1;
 
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
@@ -1693,7 +1691,7 @@ static int vgic_v3_domain_init(struct domain *d)
             /* Set the first CPU handled by this region */
             d->arch.vgic.rdist_regions[i].first_cpu = first_cpu;
 
-            first_cpu += size / d->arch.vgic.rdist_stride;
+            first_cpu += size / GICV3_GICR_SIZE;
         }
 
         d->arch.vgic.intid_bits = vgic_v3_hw.intid_bits;
@@ -1705,7 +1703,7 @@ static int vgic_v3_domain_init(struct domain *d)
         d->arch.vgic.rdist_stride = GUEST_GICV3_RDIST_STRIDE;
 
         /* The first redistributor should contain enough space for all CPUs */
-        BUILD_BUG_ON((GUEST_GICV3_GICR0_SIZE / GUEST_GICV3_RDIST_STRIDE) < MAX_VIRT_CPUS);
+        BUILD_BUG_ON((GUEST_GICV3_GICR0_SIZE / GICV3_GICR_SIZE) < MAX_VIRT_CPUS);
         d->arch.vgic.rdist_regions[0].base = GUEST_GICV3_GICR0_BASE;
         d->arch.vgic.rdist_regions[0].size = GUEST_GICV3_GICR0_SIZE;
         d->arch.vgic.rdist_regions[0].first_cpu = 0;
Signature · 7 lines
2.14.1


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

RFC PATCH 6/49 ARM: vGICv3: remove rdist_stride from VGIC structure

In reply to Andre Przywara

The last patch removed the usage of the hardware's redistributor-stride value from our (Dom0) GICv3 emulation. This means we no longer need to store this value in the VGIC data structure.
Remove that variable and every code snippet that handled that, instead simply always use the architected value.

---
 xen/arch/arm/gic-v3.c         |  3 +--
 xen/arch/arm/vgic-v3.c        | 14 --------------
 xen/include/asm-arm/domain.h  |  1 -
 xen/include/asm-arm/vgic.h    |  1 -
 xen/include/public/arch-arm.h |  2 --
 5 files changed, 1 insertion(+), 20 deletions(-)
xen/arch/arm/gic-v3.c +1 -2
index 02c85e4c0c..ea14ab4028 100644
@@ -1682,8 +1682,7 @@ static int __init gicv3_init(void)
     reg = readl_relaxed(GICD + GICD_TYPER);
     intid_bits = GICD_TYPE_ID_BITS(reg);
 
-    vgic_v3_setup_hw(dbase, gicv3.rdist_count, gicv3.rdist_regions,
-                     gicv3.rdist_stride, intid_bits);
+    vgic_v3_setup_hw(dbase, gicv3.rdist_count, gicv3.rdist_regions, intid_bits);
     gicv3_init_v2();
 
     spin_lock_init(&gicv3.lock);
xen/arch/arm/vgic-v3.c +0 -14
index e45bbc6dcf..9b1b62744c 100644
@@ -58,21 +58,18 @@ static struct {
     /* Re-distributor regions */
     unsigned int nr_rdist_regions;
     const struct rdist_region *regions;
-    uint32_t rdist_stride; /* Re-distributor stride */
     unsigned int intid_bits;  /* Number of interrupt ID bits */
 } vgic_v3_hw;
 
 void vgic_v3_setup_hw(paddr_t dbase,
                       unsigned int nr_rdist_regions,
                       const struct rdist_region *regions,
-                      uint32_t rdist_stride,
                       unsigned int intid_bits)
 {
     vgic_v3_hw.enabled = true;
     vgic_v3_hw.dbase = dbase;
     vgic_v3_hw.nr_rdist_regions = nr_rdist_regions;
     vgic_v3_hw.regions = regions;
-    vgic_v3_hw.rdist_stride = rdist_stride;
     vgic_v3_hw.intid_bits = intid_bits;
 }
 
@@ -1672,15 +1669,6 @@ static int vgic_v3_domain_init(struct domain *d)
 
         d->arch.vgic.dbase = vgic_v3_hw.dbase;
 
-        d->arch.vgic.rdist_stride = vgic_v3_hw.rdist_stride;
-        /*
-         * If the stride is not set, the default stride for GICv3 is 2 * 64K:
-         *     - first 64k page for Control and Physical LPIs
-         *     - second 64k page for Control and Generation of SGIs
-         */
-        if ( !d->arch.vgic.rdist_stride )
-            d->arch.vgic.rdist_stride = 2 * SZ_64K;
-
         for ( i = 0; i < vgic_v3_hw.nr_rdist_regions; i++ )
         {
             paddr_t size = vgic_v3_hw.regions[i].size;
@@ -1700,8 +1688,6 @@ static int vgic_v3_domain_init(struct domain *d)
     {
         d->arch.vgic.dbase = GUEST_GICV3_GICD_BASE;
 
-        d->arch.vgic.rdist_stride = GUEST_GICV3_RDIST_STRIDE;
-
         /* The first redistributor should contain enough space for all CPUs */
         BUILD_BUG_ON((GUEST_GICV3_GICR0_SIZE / GICV3_GICR_SIZE) < MAX_VIRT_CPUS);
         d->arch.vgic.rdist_regions[0].base = GUEST_GICV3_GICR0_BASE;
xen/include/asm-arm/domain.h +0 -1
index 4fe189b1c3..3eda7196ff 100644
@@ -108,7 +108,6 @@ struct arch_domain
             unsigned int first_cpu;         /* First CPU handled */
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
-        uint32_t rdist_stride;              /* Re-Distributor stride */
         unsigned long int nr_lpis;
         uint64_t rdist_propbase;
         struct rb_root its_devices;         /* Devices mapped to an ITS */
xen/include/asm-arm/vgic.h +0 -1
index 6ea9f140a7..d61b54867b 100644
@@ -261,7 +261,6 @@ struct rdist_region;
 void vgic_v3_setup_hw(paddr_t dbase,
                       unsigned int nr_rdist_regions,
                       const struct rdist_region *regions,
-                      uint32_t rdist_stride,
                       unsigned int intid_bits);
 #endif
 
xen/include/public/arch-arm.h +0 -2
index ca79ab6284..3bca165fbf 100644
@@ -401,8 +401,6 @@ typedef uint64_t xen_callback_t;
 #define GUEST_GICV3_GICD_BASE      xen_mk_ullong(0x03001000)
 #define GUEST_GICV3_GICD_SIZE      xen_mk_ullong(0x00010000)
 
-#define GUEST_GICV3_RDIST_STRIDE   xen_mk_ullong(0x00020000)
-
 #define GUEST_GICV3_GICR0_BASE     xen_mk_ullong(0x03020000) /* vCPU0..127 */
 #define GUEST_GICV3_GICR0_SIZE     xen_mk_ullong(0x01000000)
 
Signature · 7 lines
2.14.1


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

RFC PATCH 7/49 ARM: VGIC: move gic_remove_from_lr_pending() prototype

In reply to Andre Przywara

The prototype for gic_remove_from_lr_pending() is the last function in gic.h which references a VGIC data structure.
Move it over to vgic.h, so that we can remove the inclusion of vgic.h from gic.h. We add it to asm/domain.h instead, where it is actually needed.

---
 xen/include/asm-arm/domain.h | 1 +
 xen/include/asm-arm/gic.h    | 2 --
 xen/include/asm-arm/vgic.h   | 1 +
 3 files changed, 2 insertions(+), 2 deletions(-)
xen/include/asm-arm/domain.h +1 -0
index 3eda7196ff..1dd9683d25 100644
@@ -8,6 +8,7 @@
 #include <asm/vfp.h>
 #include <asm/mmio.h>
 #include <asm/gic.h>
+#include <asm/vgic.h>
 #include <public/hvm/params.h>
 #include <xen/serial.h>
 #include <xen/rbtree.h>
xen/include/asm-arm/gic.h +0 -2
index 497f195bc1..1d382b0ade 100644
@@ -156,7 +156,6 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 #include <xen/irq.h>
-#include <asm-arm/vgic.h>
 
 #define DT_COMPAT_GIC_CORTEX_A15 "arm,cortex-a15-gic"
 
@@ -245,7 +244,6 @@ extern void init_maintenance_interrupt(void);
 extern void gic_raise_guest_irq(struct vcpu *v, unsigned int irq,
         unsigned int priority);
 extern void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq);
-extern void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p);
 
 /* Accept an interrupt from the GIC and dispatch its handler */
 extern void gic_interrupt(struct cpu_user_regs *regs, int is_fiq);
xen/include/asm-arm/vgic.h +1 -0
index d61b54867b..d03298e12c 100644
@@ -205,6 +205,7 @@ 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_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p);
+extern void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p);
 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);
Signature · 7 lines
2.14.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 7/49
#9 lore raw

Re: [RFC PATCH 07/49] ARM: VGIC: move gic_remove_from_lr_pending() prototype

Hi,

Quoted 7 lines · Andre Przywara

The prototype for gic_remove_from_lr_pending() is the last function in gic.h which references a VGIC data structure.
Move it over to vgic.h, so that we can remove the inclusion of vgic.h from gic.h. We add it to asm/domain.h instead, where it is actually needed.

Quoted 44 lines
---
  xen/include/asm-arm/domain.h | 1 +
  xen/include/asm-arm/gic.h    | 2 --
  xen/include/asm-arm/vgic.h   | 1 +
  3 files changed, 2 insertions(+), 2 deletions(-)
xen/include/asm-arm/domain.h +1 -0
index 3eda7196ff..1dd9683d25 100644
@@ -8,6 +8,7 @@
  #include <asm/vfp.h>
  #include <asm/mmio.h>
  #include <asm/gic.h>
+#include <asm/vgic.h>
  #include <public/hvm/params.h>
  #include <xen/serial.h>
  #include <xen/rbtree.h>
xen/include/asm-arm/gic.h +0 -2
index 497f195bc1..1d382b0ade 100644
@@ -156,7 +156,6 @@
  #ifndef __ASSEMBLY__
  #include <xen/device_tree.h>
  #include <xen/irq.h>
-#include <asm-arm/vgic.h>
 
  #define DT_COMPAT_GIC_CORTEX_A15 "arm,cortex-a15-gic"
 
@@ -245,7 +244,6 @@ extern void init_maintenance_interrupt(void);
  extern void gic_raise_guest_irq(struct vcpu *v, unsigned int irq,
          unsigned int priority);
  extern void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq);
-extern void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p);
 
  /* Accept an interrupt from the GIC and dispatch its handler */
  extern void gic_interrupt(struct cpu_user_regs *regs, int is_fiq);
xen/include/asm-arm/vgic.h +0 -0
index d61b54867b..d03298e12c 100644

xen/include/asm-arm/vgic.h extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq); · line 211

Quoted 7 lines
xen/include/asm-arm/vgic.h +1 -0
@@ -205,6 +205,7 @@ 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_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p);
+extern void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p);
  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);

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 8/49 ARM: VGIC: move max_vcpus VGIC limit to struct arch_domain

In reply to Andre Przywara

The VGIC model used for a domain (GICv2 or GICv3) determines the maximum number of VCPUs for that guest, as GICv2 can only handle 8 processors. In the moment we carry this per-VGIC-model limit in the vgic_ops, alongside the model specific functions. That makes some sense, but exposes some current VGIC implementation details to generic Xen code. Add a new arch specific field in our domain structure to hold this vcpu limit, and initialize it when we set the ops. This allows us to plug in the new VGIC later without also needing to carry some ops structure.

---
 xen/arch/arm/domain.c        | 5 ++---
 xen/arch/arm/vgic.c          | 3 ++-
 xen/include/asm-arm/domain.h | 1 +
 3 files changed, 5 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +2 -3
index a010443bfd..9ad4cd0a6e 100644
@@ -975,11 +975,10 @@ unsigned int domain_max_vcpus(const struct domain *d)
      * allocation when the vgic_ops haven't been initialised yet,
      * we return MAX_VIRT_CPUS if d->arch.vgic.handler is null.
      */
-    if ( !d->arch.vgic.handler )
+    if ( !d->arch.max_vcpus )
         return MAX_VIRT_CPUS;
     else
-        return min_t(unsigned int, MAX_VIRT_CPUS,
-                     d->arch.vgic.handler->max_vcpus);
+        return d->arch.max_vcpus;
 }
 
 /*
xen/arch/arm/vgic.c +2 -1
index 9921769b15..5f47aa84a9 100644
@@ -166,7 +166,8 @@ int domain_vgic_init(struct domain *d, unsigned int nr_spis)
 
 void register_vgic_ops(struct domain *d, const struct vgic_ops *ops)
 {
-   d->arch.vgic.handler = ops;
+    d->arch.vgic.handler = ops;
+    d->arch.max_vcpus = ops->max_vcpus;
 }
 
 void domain_vgic_free(struct domain *d)
xen/include/asm-arm/domain.h +1 -0
index 1dd9683d25..2fef32eaee 100644
@@ -149,6 +149,7 @@ struct arch_domain
 #ifdef CONFIG_SBSA_VUART_CONSOLE
     struct vpl011 vpl011;
 #endif
+    unsigned int max_vcpus;
 
 }  __cacheline_aligned;
 
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 08/49] ARM: VGIC: move max_vcpus VGIC limit to struct arch_domain

Hi,

Quoted 53 lines · Andre Przywara

The VGIC model used for a domain (GICv2 or GICv3) determines the maximum number of VCPUs for that guest, as GICv2 can only handle 8 processors. In the moment we carry this per-VGIC-model limit in the vgic_ops, alongside the model specific functions. That makes some sense, but exposes some current VGIC implementation details to generic Xen code. Add a new arch specific field in our domain structure to hold this vcpu limit, and initialize it when we set the ops. This allows us to plug in the new VGIC later without also needing to carry some ops structure.

---
  xen/arch/arm/domain.c        | 5 ++---
  xen/arch/arm/vgic.c          | 3 ++-
  xen/include/asm-arm/domain.h | 1 +
  3 files changed, 5 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +2 -3
index a010443bfd..9ad4cd0a6e 100644
@@ -975,11 +975,10 @@ unsigned int domain_max_vcpus(const struct domain *d)
       * allocation when the vgic_ops haven't been initialised yet,
       * we return MAX_VIRT_CPUS if d->arch.vgic.handler is null.
       */
-    if ( !d->arch.vgic.handler )
+    if ( !d->arch.max_vcpus )
          return MAX_VIRT_CPUS;
      else
-        return min_t(unsigned int, MAX_VIRT_CPUS,
-                     d->arch.vgic.handler->max_vcpus);
+        return d->arch.max_vcpus;
  }
 
  /*
xen/arch/arm/vgic.c +2 -1
index 9921769b15..5f47aa84a9 100644
@@ -166,7 +166,8 @@ int domain_vgic_init(struct domain *d, unsigned int nr_spis)
 
  void register_vgic_ops(struct domain *d, const struct vgic_ops *ops)
  {
-   d->arch.vgic.handler = ops;
+    d->arch.vgic.handler = ops;
+    d->arch.max_vcpus = ops->max_vcpus;
  }
 
  void domain_vgic_free(struct domain *d)
xen/include/asm-arm/domain.h +0 -0
index 1dd9683d25..2fef32eaee 100644

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

Quoted 4 lines
xen/include/asm-arm/domain.h +1 -0
@@ -149,6 +149,7 @@ struct arch_domain
  #ifdef CONFIG_SBSA_VUART_CONSOLE
      struct vpl011 vpl011;
  #endif
+    unsigned int max_vcpus;

Now, you have max_vcpus defined in both arch_domain and domain. Which makes the code very confusing to read. This is becoming apparent in the check if (d->arch.max_vcpus > d->max_vcpus).

If you plan to ditch the ops, then I would prefer a check on the vGIC version. Even if it means carrying vGIC specific implementation in generic Xen code.

This would also avoid to grow the struct domain just for a "constant field" used mostly at guest creation.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 08/49] ARM: VGIC: move max_vcpus VGIC limit to struct arch_domain

Hi,

Quoted 56 lines · Julien Grall

Hi,

Quoted 53 lines · Andre Przywara

The VGIC model used for a domain (GICv2 or GICv3) determines the maximum number of VCPUs for that guest, as GICv2 can only handle 8 processors. In the moment we carry this per-VGIC-model limit in the vgic_ops, alongside the model specific functions. That makes some sense, but exposes some current VGIC implementation details to generic Xen code. Add a new arch specific field in our domain structure to hold this vcpu limit,
and initialize it when we set the ops. This allows us to plug in the new VGIC later without also needing to carry some ops structure.

---
  xen/arch/arm/domain.c        | 5 ++---
  xen/arch/arm/vgic.c          | 3 ++-
  xen/include/asm-arm/domain.h | 1 +

  3 files changed, 5 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +1 -1
index a010443bfd..9ad4cd0a6e 100644
@@ -975,11 +975,10 @@ unsigned int domain_max_vcpus(const struct
 domain *d)
        * allocation when the vgic_ops haven't been initialised yet,
        * we return MAX_VIRT_CPUS if d->arch.vgic.handler is null.
        */
-    if ( !d->arch.vgic.handler )
+    if ( !d->arch.max_vcpus )
           return MAX_VIRT_CPUS;

      else
-        return min_t(unsigned int, MAX_VIRT_CPUS, -                     d->arch.vgic.handler->max_vcpus); +        return d->arch.max_vcpus;
  }
    /*

xen/arch/arm/vgic.c +2 -1
index 9921769b15..5f47aa84a9 100644
@@ -166,7 +166,8 @@ int domain_vgic_init(struct domain *d, unsigned
 int nr_spis)
     void register_vgic_ops(struct domain *d, const struct vgic_ops *ops)
   {
-   d->arch.vgic.handler = ops;
+    d->arch.vgic.handler = ops;
+    d->arch.max_vcpus = ops->max_vcpus;
   }
     void domain_vgic_free(struct domain *d)
xen/include/asm-arm/domain.h +0 -0
index 1dd9683d25..2fef32eaee 100644
@@ -149,6 +149,7 @@ struct arch_domain
Quoted 8 lines

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

Quoted 4 lines
xen/include/asm-arm/domain.h +1 -0
   #ifdef CONFIG_SBSA_VUART_CONSOLE
       struct vpl011 vpl011;
   #endif
+    unsigned int max_vcpus;

Now, you have max_vcpus defined in both arch_domain and domain. Which makes the code very confusing to read. This is becoming apparent in the check if (d->arch.max_vcpus > d->max_vcpus).

True, though I was just copying the name from the vgic_ops ;-) I could suggest arch.vgic_vcpu_limit instead, but...

Quoted 3 lines

If you plan to ditch the ops, then I would prefer a check on the vGIC version. Even if it means carrying vGIC specific implementation in generic Xen code.

So what about just providing per-VGIC implementations of domain_max_vcpus()? That seems to be the only user of this information, AFAICS? So I move this from xen/arch/arm/domain.c to xen/arch/arm{/vgic,}/vgic.c, where we can do all kind of internal VGIC tricks to learn this bit of information.

Quoted 2 lines

This would also avoid to grow the struct domain just for a "constant field" used mostly at guest creation.

"Here’s a nickel, kid. Get yourself some memory." ;-)

Cheers,
Andre.

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

#13 lore raw

Re: [RFC PATCH 08/49] ARM: VGIC: move max_vcpus VGIC limit to struct arch_domain

Hi,

Quoted 75 lines · Andre Przywara

Hi,

Quoted 56 lines · Julien Grall

Hi,

Quoted 53 lines · Andre Przywara

The VGIC model used for a domain (GICv2 or GICv3) determines the maximum number of VCPUs for that guest, as GICv2 can only handle 8 processors. In the moment we carry this per-VGIC-model limit in the vgic_ops, alongside the model specific functions. That makes some sense, but exposes some current VGIC implementation details to generic Xen code. Add a new arch specific field in our domain structure to hold this vcpu limit,
and initialize it when we set the ops. This allows us to plug in the new VGIC later without also needing to carry some ops structure.

---
   xen/arch/arm/domain.c        | 5 ++---
   xen/arch/arm/vgic.c          | 3 ++-
   xen/include/asm-arm/domain.h | 1 +
   3 files changed, 5 insertions(+), 4 deletions(-)
xen/arch/arm/domain.c +2 -3
index a010443bfd..9ad4cd0a6e 100644
@@ -975,11 +975,10 @@ unsigned int domain_max_vcpus(const struct
 domain *d)
        * allocation when the vgic_ops haven't been initialised yet,
        * we return MAX_VIRT_CPUS if d->arch.vgic.handler is null.
        */
-    if ( !d->arch.vgic.handler )
+    if ( !d->arch.max_vcpus )
           return MAX_VIRT_CPUS;
       else
-        return min_t(unsigned int, MAX_VIRT_CPUS,
-                     d->arch.vgic.handler->max_vcpus);
+        return d->arch.max_vcpus;
   }
     /*
xen/arch/arm/vgic.c +2 -1
index 9921769b15..5f47aa84a9 100644
@@ -166,7 +166,8 @@ int domain_vgic_init(struct domain *d, unsigned
 int nr_spis)
     void register_vgic_ops(struct domain *d, const struct vgic_ops *ops)
   {
-   d->arch.vgic.handler = ops;
+    d->arch.vgic.handler = ops;
+    d->arch.max_vcpus = ops->max_vcpus;
   }
     void domain_vgic_free(struct domain *d)
xen/include/asm-arm/domain.h +0 -0
index 1dd9683d25..2fef32eaee 100644
@@ -149,6 +149,7 @@ struct arch_domain
Quoted 8 lines

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

Quoted 4 lines
xen/include/asm-arm/domain.h +1 -0
   #ifdef CONFIG_SBSA_VUART_CONSOLE
       struct vpl011 vpl011;
   #endif
+    unsigned int max_vcpus;

Now, you have max_vcpus defined in both arch_domain and domain. Which makes the code very confusing to read. This is becoming apparent in the check if (d->arch.max_vcpus > d->max_vcpus).

True, though I was just copying the name from the vgic_ops ;-) I could suggest arch.vgic_vcpu_limit instead, but...

Quoted 3 lines

If you plan to ditch the ops, then I would prefer a check on the vGIC version. Even if it means carrying vGIC specific implementation in generic Xen code.

Quoted 5 lines

So what about just providing per-VGIC implementations of domain_max_vcpus()? That seems to be the only user of this information, AFAICS? So I move this from xen/arch/arm/domain.c to xen/arch/arm{/vgic,}/vgic.c, where we can do all kind of internal VGIC tricks to learn this bit of information.

Sounds good to me.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 9/49 ARM: VGIC: change to level-IRQ compatible IRQ injection interface

In reply to Andre Przywara

At the moment vgic_vcpu_inject_irq() is the interface for Xen internal code and virtual devices to inject IRQs into a guest. This interface has two shortcomings:
1) It requires a VCPU pointer, which we may not know (and don't need!) for shared interrupts. A second function (vgic_vcpu_inject_spi()), was there to work around this issue.
2) This interface only really supports edge triggered IRQs, which is what the Xen VGIC emulates only anyway. However this needs to and will change, so we need to add the desired level (high or low) to the interface.
This replaces the existing injection call (taking a VCPU and and IRQ parameter) with a new one, taking domain, VCPU, IRQ and level parameters. The VCPU can be NULL in case we don't know and don't care.
We change all call sites to use this new interface. This still doesn't give us the missing level IRQ handling, but at least prepares the callers to do the right thing later automatically.

---
 xen/arch/arm/domain.c      |  4 ++--
 xen/arch/arm/gic-v3-lpi.c  |  2 +-
 xen/arch/arm/irq.c         |  2 +-
 xen/arch/arm/time.c        |  2 +-
 xen/arch/arm/vgic.c        | 43 +++++++++++++++++++++++++------------------
 xen/arch/arm/vpl011.c      |  2 +-
 xen/arch/arm/vtimer.c      |  4 ++--
 xen/include/asm-arm/vgic.h |  4 ++--
 8 files changed, 35 insertions(+), 28 deletions(-)
xen/arch/arm/domain.c +2 -2
index 9ad4cd0a6e..e76cfdfe83 100644
@@ -952,14 +952,14 @@ void vcpu_mark_events_pending(struct vcpu *v)
     if ( already_pending )
         return;
 
-    vgic_vcpu_inject_irq(v, v->domain->arch.evtchn_irq);
+    vgic_inject_irq(v->domain, v, v->domain->arch.evtchn_irq, true);
 }
 
 /* The ARM spec declares that even if local irqs are masked in
  * the CPSR register, an irq should wake up a cpu from WFI anyway.
  * For this reason we need to check for irqs that need delivery,
  * ignoring the CPSR register, *after* calling SCHEDOP_block to
- * avoid races with vgic_vcpu_inject_irq.
+ * avoid races with vgic_inject_irq.
  */
 void vcpu_block_unless_event_pending(struct vcpu *v)
 {
xen/arch/arm/gic-v3-lpi.c +1 -1
index 84582157b8..efd5cd62fb 100644
@@ -153,7 +153,7 @@ void vgic_vcpu_inject_lpi(struct domain *d, unsigned int virq)
     if ( vcpu_id >= d->max_vcpus )
           return;
 
-    vgic_vcpu_inject_irq(d->vcpu[vcpu_id], virq);
+    vgic_inject_irq(d, d->vcpu[vcpu_id], virq, true);
 }
 
 /*
xen/arch/arm/irq.c +1 -1
index 29af10e82c..aa4e832cae 100644
@@ -225,7 +225,7 @@ void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq)
          * The irq cannot be a PPI, we only support delivery of SPIs to
          * guests.
 	 */
-        vgic_vcpu_inject_spi(info->d, info->virq);
+        vgic_inject_irq(info->d, NULL, info->virq, true);
         goto out_no_end;
     }
 
xen/arch/arm/time.c +1 -1
index 36f640f0c1..c11fcfeadd 100644
@@ -260,7 +260,7 @@ static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
 
     current->arch.virt_timer.ctl = READ_SYSREG32(CNTV_CTL_EL0);
     WRITE_SYSREG32(current->arch.virt_timer.ctl | CNTx_CTL_MASK, CNTV_CTL_EL0);
-    vgic_vcpu_inject_irq(current, current->arch.virt_timer.irq);
+    vgic_inject_irq(current->domain, current, current->arch.virt_timer.irq, true);
 }
 
 /*
xen/arch/arm/vgic.c +25 -18

100 lines not on this page · raw message

xen/arch/arm/vpl011.c +1 -1
index 7788c2fc32..5dcf4bec18 100644
@@ -68,7 +68,7 @@ static void vpl011_update_interrupt_status(struct domain *d)
      * status bit has been set since the last time.
      */
     if ( uartmis & ~vpl011->shadow_uartmis )
-        vgic_vcpu_inject_spi(d, GUEST_VPL011_SPI);
+        vgic_inject_irq(d, NULL, GUEST_VPL011_SPI, true);
 
     vpl011->shadow_uartmis = uartmis;
 }
xen/arch/arm/vtimer.c +2 -2
index f52a723a5f..8164f6c7f1 100644
@@ -46,7 +46,7 @@ static void phys_timer_expired(void *data)
     if ( !(t->ctl & CNTx_CTL_MASK) )
     {
         perfc_incr(vtimer_phys_inject);
-        vgic_vcpu_inject_irq(t->v, t->irq);
+        vgic_inject_irq(t->v->domain, t->v, t->irq, true);
     }
     else
         perfc_incr(vtimer_phys_masked);
@@ -56,7 +56,7 @@ static void virt_timer_expired(void *data)
 {
     struct vtimer *t = data;
     t->ctl |= CNTx_CTL_MASK;
-    vgic_vcpu_inject_irq(t->v, t->irq);
+    vgic_inject_irq(t->v->domain, t->v, t->irq, true);
     perfc_incr(vtimer_virt_inject);
 }
 
xen/include/asm-arm/vgic.h +2 -2
index d03298e12c..b75fdeb068 100644
@@ -202,8 +202,8 @@ extern int domain_vgic_init(struct domain *d, unsigned int nr_spis);
 extern void domain_vgic_free(struct domain *d);
 extern int vcpu_vgic_init(struct vcpu *v);
 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 int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                           bool level);
 extern void vgic_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p);
 extern void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 09/49] ARM: VGIC: change to level-IRQ compatible IRQ injection interface

Hi Andre,

Quoted 38 lines · Andre Przywara
xen/arch/arm/vgic.c +4 -4
index 5f47aa84a9..2fc6e19625 100644
@@ -285,7 +285,7 @@ bool vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq)
          vgic_remove_irq_from_queues(old, p);
          irq_set_affinity(p->desc, cpumask_of(new->processor));
          spin_unlock_irqrestore(&old->arch.vgic.lock, flags);
-        vgic_vcpu_inject_irq(new, irq);
+        vgic_inject_irq(new->domain, new, irq, true);
          return true;
      }
      /* if the IRQ is in a GICH_LR register, set GIC_IRQ_GUEST_MIGRATING
@@ -444,7 +444,7 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
                          sgir, target->list);
                  continue;
              }
-            vgic_vcpu_inject_irq(d->vcpu[vcpuid], virq);
+            vgic_inject_irq(d, d->vcpu[vcpuid], virq, true);
          }
          break;
      case SGI_TARGET_OTHERS:
@@ -453,12 +453,12 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
          {
              if ( i != current->vcpu_id && d->vcpu[i] != NULL &&
                   is_vcpu_online(d->vcpu[i]) )
-                vgic_vcpu_inject_irq(d->vcpu[i], virq);
+                vgic_inject_irq(d, d->vcpu[i], virq, true);
          }
          break;
      case SGI_TARGET_SELF:
          perfc_incr(vgic_sgi_self);
-        vgic_vcpu_inject_irq(d->vcpu[current->vcpu_id], virq);
+        vgic_inject_irq(d, current, virq, true);
          break;
      default:
          gprintk(XENLOG_WARNING,

xen/arch/arm/vgic.c void vgic_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p) · line 522

Quoted 6 lines
xen/arch/arm/vgic.c +2 -1
@@ -518,13 +518,29 @@ void vgic_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p)
      gic_remove_from_lr_pending(v, p);
  }
 
-void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
+int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                    bool level)

Looking at the code after the series has been applied, no one is caring about the return value of vgic_inject_irq. So what is the rationale behind changing the return type from void to int?

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 09/49] ARM: VGIC: change to level-IRQ compatible IRQ injection interface

Hi,

Quoted 46 lines · Julien Grall

Hi Andre,

Quoted 43 lines · Andre Przywara
xen/arch/arm/vgic.c +1 -1
index 5f47aa84a9..2fc6e19625 100644
@@ -285,7 +285,7 @@ bool vgic_migrate_irq(struct vcpu *old, struct
 vcpu *new, unsigned int irq)
           vgic_remove_irq_from_queues(old, p);
           irq_set_affinity(p->desc, cpumask_of(new->processor));
           spin_unlock_irqrestore(&old->arch.vgic.lock, flags);
-        vgic_vcpu_inject_irq(new, irq);
+        vgic_inject_irq(new->domain, new, irq, true);
           return true;
       }

      /* if the IRQ is in a GICH_LR register, set GIC_IRQ_GUEST_MIGRATING

xen/arch/arm/vgic.c +1 -1
@@ -444,7 +444,7 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir,
 enum gic_sgi_mode irqmode,
                           sgir, target->list);
                   continue;
               }
-            vgic_vcpu_inject_irq(d->vcpu[vcpuid], virq);
+            vgic_inject_irq(d, d->vcpu[vcpuid], virq, true);
           }
           break;

      case SGI_TARGET_OTHERS:

xen/arch/arm/vgic.c +0 -0
@@ -453,12 +453,12 @@ bool vgic_to_sgi(struct vcpu *v, register_t
 sgir, enum gic_sgi_mode irqmode,
           {

              if ( i != current->vcpu_id && d->vcpu[i] != NULL && is_vcpu_online(d->vcpu[i]) )
-                vgic_vcpu_inject_irq(d->vcpu[i], virq); +                vgic_inject_irq(d, d->vcpu[i], virq, true); }
          break;
      case SGI_TARGET_SELF:
          perfc_incr(vgic_sgi_self);
-        vgic_vcpu_inject_irq(d->vcpu[current->vcpu_id], virq); +        vgic_inject_irq(d, current, virq, true); break;
      default:
          gprintk(XENLOG_WARNING,

xen/arch/arm/vgic.c +0 -0
@@ -518,13 +518,29 @@ void vgic_remove_irq_from_queues(struct vcpu *v,
 struct pending_irq *p)
       gic_remove_from_lr_pending(v, p);
Quoted 8 lines

xen/arch/arm/vgic.c void vgic_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p) · line 522

Quoted 4 lines
xen/arch/arm/vgic.c +2 -0
   }
   -void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
+int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                    bool level)

Looking at the code after the series has been applied, no one is caring about the return value of vgic_inject_irq. So what is the rationale behind changing the return type from void to int?

The KVM version returns an error value, in particular when:
- the VGIC has not been initialized yet
- we can't determine the VCPU for a private interrupt
- the interrupt ID is invalid (SPI beyond limit, not mapped LPI) In the moment it's not very useful for Xen: the first two conditions don't really happen, consequently I removed those checks. But the third check may become interesting once we get LPIs. Also since Xen currently uses a void prototype for injection, *this* patch *now* doesn't exploit the newly gained possibility of properly handling errors. From briefly checking all the users, all of them seem to be in void functions, so indeed an error return is not very useful.
The reasons I kept it in was to allow introduction of checks later. I think having a function returning an error where some users ignore this is better than the other way round.

So of course I can easily make this void, but I wonder what we do in those cases where the SPI is not valid, for instance? Shall we print some (rate-limited) warning?

Cheers,
Andre.

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

#17 lore raw

Re: [RFC PATCH 09/49] ARM: VGIC: change to level-IRQ compatible IRQ injection interface

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 56 lines · Julien Grall
Quoted 46 lines

Hi Andre,

Quoted 43 lines · Andre Przywara
xen/arch/arm/vgic.c +1 -1
index 5f47aa84a9..2fc6e19625 100644
@@ -285,7 +285,7 @@ bool vgic_migrate_irq(struct vcpu *old, struct
 vcpu *new, unsigned int irq)
           vgic_remove_irq_from_queues(old, p);
           irq_set_affinity(p->desc, cpumask_of(new->processor));
           spin_unlock_irqrestore(&old->arch.vgic.lock, flags);
-        vgic_vcpu_inject_irq(new, irq);
+        vgic_inject_irq(new->domain, new, irq, true);
           return true;
       }

      /* if the IRQ is in a GICH_LR register, set GIC_IRQ_GUEST_MIGRATING

xen/arch/arm/vgic.c +1 -1
@@ -444,7 +444,7 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir,
 enum gic_sgi_mode irqmode,
                           sgir, target->list);
                   continue;
               }
-            vgic_vcpu_inject_irq(d->vcpu[vcpuid], virq);
+            vgic_inject_irq(d, d->vcpu[vcpuid], virq, true);
           }
           break;

      case SGI_TARGET_OTHERS:

xen/arch/arm/vgic.c +2 -2
@@ -453,12 +453,12 @@ bool vgic_to_sgi(struct vcpu *v, register_t
 sgir, enum gic_sgi_mode irqmode,
           {
               if ( i != current->vcpu_id && d->vcpu[i] != NULL &&
                    is_vcpu_online(d->vcpu[i]) )
-                vgic_vcpu_inject_irq(d->vcpu[i], virq);
+                vgic_inject_irq(d, d->vcpu[i], virq, true);
           }
           break;
       case SGI_TARGET_SELF:
           perfc_incr(vgic_sgi_self);
-        vgic_vcpu_inject_irq(d->vcpu[current->vcpu_id], virq);
+        vgic_inject_irq(d, current, virq, true);
           break;
       default:

          gprintk(XENLOG_WARNING,

xen/arch/arm/vgic.c +0 -0
@@ -518,13 +518,29 @@ void vgic_remove_irq_from_queues(struct vcpu *v,
 struct pending_irq *p)
       gic_remove_from_lr_pending(v, p);
Quoted 8 lines

xen/arch/arm/vgic.c void vgic_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p) · line 522

Quoted 4 lines
xen/arch/arm/vgic.c +2 -0
   }
   -void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
+int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                    bool level)

Looking at the code after the series has been applied, no one is caring about the return value of vgic_inject_irq. So what is the rationale behind changing the return type from void to int?

Quoted 14 lines

The KVM version returns an error value, in particular when:
- the VGIC has not been initialized yet
- we can't determine the VCPU for a private interrupt
- the interrupt ID is invalid (SPI beyond limit, not mapped LPI) In the moment it's not very useful for Xen: the first two conditions don't really happen, consequently I removed those checks. But the third check may become interesting once we get LPIs. Also since Xen currently uses a void prototype for injection, *this* patch *now* doesn't exploit the newly gained possibility of properly handling errors. From briefly checking all the users, all of them seem to be in void functions, so indeed an error return is not very useful.
The reasons I kept it in was to allow introduction of checks later. I think having a function returning an error where some users ignore this is better than the other way round.

I don't think it is much better. This is a way to expose yet another security issue because the return is not correctly checked (see XSA-246 for instance). Any return value should be checked or have a comment explaining why it is fine.

Quoted 4 lines

So of course I can easily make this void, but I wonder what we do in those cases where the SPI is not valid, for instance? Shall we print some (rate-limited) warning?

I can understand why KVM needs such interface as the interrupt controller may be emulated QEMU. But I can't see why a SPI would not be valid in Xen context (except programming error). So could give an example?

What would you expect the caller to do on error? Except printing an error message?

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 09/49] ARM: VGIC: change to level-IRQ compatible IRQ injection interface

Hi,

Quoted 86 lines · Julien Grall
Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 58 lines · Julien Grall
Quoted 48 lines

Hi Andre,

Quoted 45 lines · Andre Przywara
xen/arch/arm/vgic.c +1 -1
index 5f47aa84a9..2fc6e19625 100644
@@ -285,7 +285,7 @@ bool vgic_migrate_irq(struct vcpu *old, struct
 vcpu *new, unsigned int irq)
            vgic_remove_irq_from_queues(old, p);
            irq_set_affinity(p->desc, cpumask_of(new->processor));
            spin_unlock_irqrestore(&old->arch.vgic.lock, flags);
-        vgic_vcpu_inject_irq(new, irq);
+        vgic_inject_irq(new->domain, new, irq, true);
            return true;
        }

       /* if the IRQ is in a GICH_LR register, set GIC_IRQ_GUEST_MIGRATING

xen/arch/arm/vgic.c +1 -1
@@ -444,7 +444,7 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir,
 enum gic_sgi_mode irqmode,
                            sgir, target->list);
                    continue;
                }
-            vgic_vcpu_inject_irq(d->vcpu[vcpuid], virq);
+            vgic_inject_irq(d, d->vcpu[vcpuid], virq, true);
            }
            break;

       case SGI_TARGET_OTHERS:

xen/arch/arm/vgic.c +0 -0
@@ -453,12 +453,12 @@ bool vgic_to_sgi(struct vcpu *v, register_t
 sgir, enum gic_sgi_mode irqmode,
            {

               if ( i != current->vcpu_id && d->vcpu[i] != NULL && is_vcpu_online(d->vcpu[i]) )
-                vgic_vcpu_inject_irq(d->vcpu[i], virq); +                vgic_inject_irq(d, d->vcpu[i], virq, true); }
           break;
       case SGI_TARGET_SELF:
           perfc_incr(vgic_sgi_self);
-        vgic_vcpu_inject_irq(d->vcpu[current->vcpu_id], virq); +        vgic_inject_irq(d, current, virq, true); break;
       default:
           gprintk(XENLOG_WARNING,

xen/arch/arm/vgic.c +0 -0
@@ -518,13 +518,29 @@ void vgic_remove_irq_from_queues(struct vcpu *v,
 struct pending_irq *p)
        gic_remove_from_lr_pending(v, p);
    }
Quoted 8 lines

xen/arch/arm/vgic.c void vgic_remove_irq_from_queues(struct vcpu *v, · line 524

Quoted 4 lines
xen/arch/arm/vgic.c +2 -0
    -void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
+int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int
 virq,
+                    bool level)

Looking at the code after the series has been applied, no one is caring about the return value of vgic_inject_irq. So what is the rationale behind changing the return type from void to int?

Quoted 14 lines

The KVM version returns an error value, in particular when:
- the VGIC has not been initialized yet
- we can't determine the VCPU for a private interrupt
- the interrupt ID is invalid (SPI beyond limit, not mapped LPI) In the moment it's not very useful for Xen: the first two conditions don't really happen, consequently I removed those checks. But the third check may become interesting once we get LPIs. Also since Xen currently uses a void prototype for injection, *this* patch *now* doesn't exploit the newly gained possibility of properly handling errors. From briefly checking all the users, all of them seem to be in void functions, so indeed an error return is not very useful.
The reasons I kept it in was to allow introduction of checks later. I think having a function returning an error where some users ignore this is better than the other way round.

I don't think it is much better. This is a way to expose yet another security issue because the return is not correctly checked (see XSA-246 for instance). Any return value should be checked or have a comment explaining why it is fine.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

So of course I can easily make this void, but I wonder what we do in those cases where the SPI is not valid, for instance? Shall we print some (rate-limited) warning?

I can understand why KVM needs such interface as the interrupt controller may be emulated QEMU.

It is also that interrupts from emulated devices are injected from QEMU in userland, and the ioctl used for that can and will return an error. So we can propagate this condition to the device. If and what the devices does with that information, is another question, though, but out of scope for KVM.

Quoted 2 lines

But I can't see why a SPI would not be
valid in Xen context (except programming error). So could give an example?

In KVM the KVM_IRQ_LINE ioctl allows to inject an arbitrary number, so checking this and returning an error is natural and mandatory.

Quoted 2 lines

What would you expect the caller to do on error? Except printing an error message?

I don't know either. Comparing this to hardware, an IRQ is usually fire-and-forget (separating the interrupt line from the interrupt state here), so a device doesn't really handle the case when an IRQ does not make it through (it can't know easily anyway). However the whole state machine might get busted in the process (if no one lowers the line, for instance).
So looking at this printing a message looks like the best choice.

I checked all users of vgic_inject_irq(), at the moment all IRQ numbers passed in look safe: they are either hardcoded (timer, evtchn) or validated before (hardware IRQs, when they are tied to a virtual IRQ). So indeed we *should* never see an invalid IRQ number, at the moment. I need to check how this changes with the ITS, though.

So we could change the prototype (back) to void, but print some error message if the vgic_get_irq() call fails within vgic_inject_irq().

Cheers,
Andre.

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

#19 lore raw

Re: [RFC PATCH 09/49] ARM: VGIC: change to level-IRQ compatible IRQ injection interface

Hi,

Quoted 11 lines · Andre Przywara
Quoted 2 lines

What would you expect the caller to do on error? Except printing an error message?

I don't know either. Comparing this to hardware, an IRQ is usually fire-and-forget (separating the interrupt line from the interrupt state here), so a device doesn't really handle the case when an IRQ does not make it through (it can't know easily anyway). However the whole state machine might get busted in the process (if no one lowers the line, for instance).
So looking at this printing a message looks like the best choice.

Quoted 8 lines

I checked all users of vgic_inject_irq(), at the moment all IRQ numbers passed in look safe: they are either hardcoded (timer, evtchn) or validated before (hardware IRQs, when they are tied to a virtual IRQ). So indeed we *should* never see an invalid IRQ number, at the moment. I need to check how this changes with the ITS, though.

So we could change the prototype (back) to void, but print some error message if the vgic_get_irq() call fails within vgic_inject_irq().

Sounds good to me. Make sure to have those message using the log level guest debug message. I might even be tempt to use dgprintk(...) here so they get dropped in non-debug build.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 10/49 ARM: VGIC: carve out struct vgic_cpu and struct vgic_dist

In reply to Andre Przywara

Currently we describe the VGIC specific fields in an structure *embedded* in struct arch_domain and struct arch_vcpu. These members there are however related to the current VGIC implementation, and will be substantially different in the future.
To allow coexistence of two implementations, move the definition of these embedded structures into vgic.h, and just use the opaque type in the arch specific structures.
This allows easy switching between different implementations later.

---
 xen/include/asm-arm/domain.h | 85 +-----------------------------------------
 xen/include/asm-arm/vgic.h   | 88 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 83 deletions(-)
xen/include/asm-arm/domain.h +2 -83
index 2fef32eaee..968ffb0c81 100644
@@ -74,57 +74,7 @@ struct arch_domain
         uint64_t offset;
     } virt_timer_base;
 
-    struct {
-        /* Version of the vGIC */
-        enum gic_version version;
-        /* GIC HW version specific vGIC driver handler */
-        const struct vgic_ops *handler;
-        /*
-         * Covers access to other members of this struct _except_ for
-         * shared_irqs where each member contains its own locking.
-         *
-         * If both class of lock is required then this lock must be
-         * taken first. If multiple rank locks are required (including
-         * the per-vcpu private_irqs rank) then they must be taken in
-         * rank order.
-         */
-        spinlock_t lock;
-        uint32_t ctlr;
-        int nr_spis; /* Number of SPIs */
-        unsigned long *allocated_irqs; /* bitmap of IRQs allocated */
-        struct vgic_irq_rank *shared_irqs;
-        /*
-         * SPIs are domain global, SGIs and PPIs are per-VCPU and stored in
-         * struct arch_vcpu.
-         */
-        struct pending_irq *pending_irqs;
-        /* Base address for guest GIC */
-        paddr_t dbase; /* Distributor base address */
-#ifdef CONFIG_HAS_GICV3
-        /* GIC V3 addressing */
-        /* List of contiguous occupied by the redistributors */
-        struct vgic_rdist_region {
-            paddr_t base;                   /* Base address */
-            paddr_t size;                   /* Size */
-            unsigned int first_cpu;         /* First CPU handled */
-        } *rdist_regions;
-        int nr_regions;                     /* Number of rdist regions */
-        unsigned long int nr_lpis;
-        uint64_t rdist_propbase;
-        struct rb_root its_devices;         /* Devices mapped to an ITS */
-        spinlock_t its_devices_lock;        /* Protects the its_devices tree */
-        struct radix_tree_root pend_lpi_tree; /* Stores struct pending_irq's */
-        rwlock_t pend_lpi_tree_lock;        /* Protects the pend_lpi_tree */
-        struct list_head vits_list;         /* List of virtual ITSes */
-        unsigned int intid_bits;
-        /*
-         * TODO: if there are more bool's being added below, consider
-         * a flags variable instead.
-         */
-        bool rdists_enabled;                /* Is any redistributor enabled? */
-        bool has_its;
-#endif
-    } vgic;
+    struct vgic_dist vgic;
 
     struct vuart {
 #define VUART_BUF_SIZE 128
@@ -248,38 +198,7 @@ struct arch_vcpu
     union gic_state_data gic;
     uint64_t lr_mask;
 
-    struct {
-        /*
-         * SGIs and PPIs are per-VCPU, SPIs are domain global and in
-         * struct arch_domain.
-         */
-        struct pending_irq pending_irqs[32];
-        struct vgic_irq_rank *private_irqs;
-
-        /* This list is ordered by IRQ priority and it is used to keep
-         * track of the IRQs that the VGIC injected into the guest.
-         * Depending on the availability of LR registers, the IRQs might
-         * actually be in an LR, and therefore injected into the guest,
-         * or queued in gic.lr_pending.
-         * As soon as an IRQ is EOI'd by the guest and removed from the
-         * corresponding LR it is also removed from this list. */
-        struct list_head inflight_irqs;
-        /* lr_pending is used to queue IRQs (struct pending_irq) that the
-         * vgic tried to inject in the guest (calling gic_set_guest_irq) but
-         * no LRs were available at the time.
-         * As soon as an LR is freed we remove the first IRQ from this
-         * list and write it to the LR register.
-         * lr_pending is a subset of vgic.inflight_irqs. */
-        struct list_head lr_pending;
-        spinlock_t lock;
-
-        /* GICv3: redistributor base and flags for this vCPU */
-        paddr_t rdist_base;
-        uint64_t rdist_pendbase;
-#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
-#define VGIC_V3_LPIS_ENABLED    (1 << 1)
-        uint8_t flags;
-    } vgic;
+    struct vgic_cpu vgic;
 
     /* Timer registers  */
     uint32_t cntkctl;
xen/include/asm-arm/vgic.h +88 -0

100 lines not on this page · raw message

Signature · 7 lines
2.14.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 10/49
#21 lore raw

Re: [RFC PATCH 10/49] ARM: VGIC: carve out struct vgic_cpu and struct vgic_dist

Hi Andre,

Quoted 10 lines · Andre Przywara

Currently we describe the VGIC specific fields in an structure *embedded* in struct arch_domain and struct arch_vcpu. These members there are however related to the current VGIC implementation, and will be substantially different in the future.
To allow coexistence of two implementations, move the definition of these embedded structures into vgic.h, and just use the opaque type in the arch specific structures.
This allows easy switching between different implementations later.

Cheers,

Quoted 215 lines
---
  xen/include/asm-arm/domain.h | 85 +-----------------------------------------
  xen/include/asm-arm/vgic.h   | 88 ++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 90 insertions(+), 83 deletions(-)
xen/include/asm-arm/domain.h +2 -83
index 2fef32eaee..968ffb0c81 100644
@@ -74,57 +74,7 @@ struct arch_domain
          uint64_t offset;
      } virt_timer_base;
 
-    struct {
-        /* Version of the vGIC */
-        enum gic_version version;
-        /* GIC HW version specific vGIC driver handler */
-        const struct vgic_ops *handler;
-        /*
-         * Covers access to other members of this struct _except_ for
-         * shared_irqs where each member contains its own locking.
-         *
-         * If both class of lock is required then this lock must be
-         * taken first. If multiple rank locks are required (including
-         * the per-vcpu private_irqs rank) then they must be taken in
-         * rank order.
-         */
-        spinlock_t lock;
-        uint32_t ctlr;
-        int nr_spis; /* Number of SPIs */
-        unsigned long *allocated_irqs; /* bitmap of IRQs allocated */
-        struct vgic_irq_rank *shared_irqs;
-        /*
-         * SPIs are domain global, SGIs and PPIs are per-VCPU and stored in
-         * struct arch_vcpu.
-         */
-        struct pending_irq *pending_irqs;
-        /* Base address for guest GIC */
-        paddr_t dbase; /* Distributor base address */
-#ifdef CONFIG_HAS_GICV3
-        /* GIC V3 addressing */
-        /* List of contiguous occupied by the redistributors */
-        struct vgic_rdist_region {
-            paddr_t base;                   /* Base address */
-            paddr_t size;                   /* Size */
-            unsigned int first_cpu;         /* First CPU handled */
-        } *rdist_regions;
-        int nr_regions;                     /* Number of rdist regions */
-        unsigned long int nr_lpis;
-        uint64_t rdist_propbase;
-        struct rb_root its_devices;         /* Devices mapped to an ITS */
-        spinlock_t its_devices_lock;        /* Protects the its_devices tree */
-        struct radix_tree_root pend_lpi_tree; /* Stores struct pending_irq's */
-        rwlock_t pend_lpi_tree_lock;        /* Protects the pend_lpi_tree */
-        struct list_head vits_list;         /* List of virtual ITSes */
-        unsigned int intid_bits;
-        /*
-         * TODO: if there are more bool's being added below, consider
-         * a flags variable instead.
-         */
-        bool rdists_enabled;                /* Is any redistributor enabled? */
-        bool has_its;
-#endif
-    } vgic;
+    struct vgic_dist vgic;
 
      struct vuart {
  #define VUART_BUF_SIZE 128
@@ -248,38 +198,7 @@ struct arch_vcpu
      union gic_state_data gic;
      uint64_t lr_mask;
 
-    struct {
-        /*
-         * SGIs and PPIs are per-VCPU, SPIs are domain global and in
-         * struct arch_domain.
-         */
-        struct pending_irq pending_irqs[32];
-        struct vgic_irq_rank *private_irqs;
-
-        /* This list is ordered by IRQ priority and it is used to keep
-         * track of the IRQs that the VGIC injected into the guest.
-         * Depending on the availability of LR registers, the IRQs might
-         * actually be in an LR, and therefore injected into the guest,
-         * or queued in gic.lr_pending.
-         * As soon as an IRQ is EOI'd by the guest and removed from the
-         * corresponding LR it is also removed from this list. */
-        struct list_head inflight_irqs;
-        /* lr_pending is used to queue IRQs (struct pending_irq) that the
-         * vgic tried to inject in the guest (calling gic_set_guest_irq) but
-         * no LRs were available at the time.
-         * As soon as an LR is freed we remove the first IRQ from this
-         * list and write it to the LR register.
-         * lr_pending is a subset of vgic.inflight_irqs. */
-        struct list_head lr_pending;
-        spinlock_t lock;
-
-        /* GICv3: redistributor base and flags for this vCPU */
-        paddr_t rdist_base;
-        uint64_t rdist_pendbase;
-#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
-#define VGIC_V3_LPIS_ENABLED    (1 << 1)
-        uint8_t flags;
-    } vgic;
+    struct vgic_cpu vgic;
 
      /* Timer registers  */
      uint32_t cntkctl;
xen/include/asm-arm/vgic.h +88 -0
index b75fdeb068..4e1c37f091 100644
@@ -19,6 +19,9 @@
  #define __ASM_ARM_VGIC_H__
 
  #include <xen/bitops.h>
+#include <xen/radix-tree.h>
+#include <xen/rbtree.h>
+#include <asm/gic.h>
  #include <asm/mmio.h>
  #include <asm/vreg.h>
 
@@ -123,6 +126,91 @@ struct vgic_irq_rank {
      uint8_t vcpu[32];
  };
 
+struct vgic_dist {
+    /* Version of the vGIC */
+    enum gic_version version;
+    /* GIC HW version specific vGIC driver handler */
+    const struct vgic_ops *handler;
+    /*
+     * Covers access to other members of this struct _except_ for
+     * shared_irqs where each member contains its own locking.
+     *
+     * If both class of lock is required then this lock must be
+     * taken first. If multiple rank locks are required (including
+     * the per-vcpu private_irqs rank) then they must be taken in
+     * rank order.
+     */
+    spinlock_t lock;
+    uint32_t ctlr;
+    int nr_spis; /* Number of SPIs */
+    unsigned long *allocated_irqs; /* bitmap of IRQs allocated */
+    struct vgic_irq_rank *shared_irqs;
+    /*
+     * SPIs are domain global, SGIs and PPIs are per-VCPU and stored in
+     * struct arch_vcpu.
+     */
+    struct pending_irq *pending_irqs;
+    /* Base address for guest GIC */
+    paddr_t dbase; /* Distributor base address */
+#ifdef CONFIG_HAS_GICV3
+    /* GIC V3 addressing */
+    /* List of contiguous occupied by the redistributors */
+    struct vgic_rdist_region {
+        paddr_t base;                   /* Base address */
+        paddr_t size;                   /* Size */
+        unsigned int first_cpu;         /* First CPU handled */
+    } *rdist_regions;
+    int nr_regions;                     /* Number of rdist regions */
+    unsigned long int nr_lpis;
+    uint64_t rdist_propbase;
+    struct rb_root its_devices;         /* Devices mapped to an ITS */
+    spinlock_t its_devices_lock;        /* Protects the its_devices tree */
+    struct radix_tree_root pend_lpi_tree; /* Stores struct pending_irq's */
+    rwlock_t pend_lpi_tree_lock;        /* Protects the pend_lpi_tree */
+    struct list_head vits_list;         /* List of virtual ITSes */
+    unsigned int intid_bits;
+    /*
+     * TODO: if there are more bool's being added below, consider
+     * a flags variable instead.
+     */
+    bool rdists_enabled;                /* Is any redistributor enabled? */
+    bool has_its;
+#endif
+};
+
+struct vgic_cpu {
+    /*
+     * SGIs and PPIs are per-VCPU, SPIs are domain global and in
+     * struct arch_domain.
+     */
+    struct pending_irq pending_irqs[32];
+    struct vgic_irq_rank *private_irqs;
+
+    /* This list is ordered by IRQ priority and it is used to keep
+     * track of the IRQs that the VGIC injected into the guest.
+     * Depending on the availability of LR registers, the IRQs might
+     * actually be in an LR, and therefore injected into the guest,
+     * or queued in gic.lr_pending.
+     * As soon as an IRQ is EOI'd by the guest and removed from the
+     * corresponding LR it is also removed from this list. */
+    struct list_head inflight_irqs;
+    /* lr_pending is used to queue IRQs (struct pending_irq) that the
+     * vgic tried to inject in the guest (calling gic_set_guest_irq) but
+     * no LRs were available at the time.
+     * As soon as an LR is freed we remove the first IRQ from this
+     * list and write it to the LR register.
+     * lr_pending is a subset of vgic.inflight_irqs. */
+    struct list_head lr_pending;
+    spinlock_t lock;
+
+    /* GICv3: redistributor base and flags for this vCPU */
+    paddr_t rdist_base;
+    uint64_t rdist_pendbase;
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+    uint8_t flags;
+};
+
  struct sgi_target {
      uint8_t aff1;
      uint16_t list;
Signature · 6 lines
Julien Grall

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

RFC PATCH 11/49 ARM: VGIC: reorder prototypes in vgic.h

In reply to Andre Przywara

Currently vgic.h both contains prototypes used by Xen arch code outside of the actual VGIC (for instance vgic_vcpu_inject_irq()), and prototypes for functions used by the VGIC internally.
Group them to later allow an easy split with one #ifdef.

---
 xen/include/asm-arm/vgic.h | 73 ++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 35 deletions(-)
xen/include/asm-arm/vgic.h +38 -35
index 4e1c37f091..8c39ff1402 100644
@@ -279,19 +279,8 @@ enum gic_sgi_mode;
  */
 #define REG_RANK_INDEX(b, n, s) ((((n) >> s) & ((b)-1)) % 32)
 
-/*
- * In the moment vgic_num_irqs() just covers SPIs and the private IRQs,
- * as it's mostly used for allocating the pending_irq and irq_desc array,
- * in which LPIs don't participate.
- */
-#define vgic_num_irqs(d)        ((d)->arch.vgic.nr_spis + 32)
 
-extern int domain_vgic_init(struct domain *d, unsigned int nr_spis);
-extern void domain_vgic_free(struct domain *d);
-extern int vcpu_vgic_init(struct vcpu *v);
 extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
-extern int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
-                           bool level);
 extern void vgic_remove_irq_from_queues(struct vcpu *v, struct pending_irq *p);
 extern void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
@@ -307,29 +296,40 @@ extern void register_vgic_ops(struct domain *d, const struct vgic_ops *ops);
 int vgic_v2_init(struct domain *d, int *mmio_count);
 int vgic_v3_init(struct domain *d, int *mmio_count);
 
-bool vgic_evtchn_irq_pending(struct vcpu *v);
-struct irq_desc *vgic_get_hw_irq_desc(struct domain *d, struct vcpu *v,
-                                      unsigned int virq);
-int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq,
-                        struct irq_desc *desc, bool connect);
-
-extern int domain_vgic_register(struct domain *d, int *mmio_count);
-extern int vcpu_vgic_free(struct vcpu *v);
 extern bool vgic_to_sgi(struct vcpu *v, register_t sgir,
                         enum gic_sgi_mode irqmode, int virq,
                         const struct sgi_target *target);
 extern bool vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq);
 
-/* Reserve a specific guest vIRQ */
-extern bool vgic_reserve_virq(struct domain *d, unsigned int virq);
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+                      paddr_t vbase, uint32_t aliased_offset);
+
+#ifdef CONFIG_HAS_GICV3
+struct rdist_region;
+void vgic_v3_setup_hw(paddr_t dbase,
+                      unsigned int nr_rdist_regions,
+                      const struct rdist_region *regions,
+                      unsigned int intid_bits);
+#endif
+
+/*** Common VGIC functions used by Xen arch code ****/
 
 /*
- * Allocate a guest VIRQ
- *  - spi == 0 => allocate a PPI. It will be the same on every vCPU
- *  - spi == 1 => allocate an SPI
+ * In the moment vgic_num_irqs() just covers SPIs and the private IRQs,
+ * as it's mostly used for allocating the pending_irq and irq_desc array,
+ * in which LPIs don't participate.
  */
-extern int vgic_allocate_virq(struct domain *d, bool spi);
+#define vgic_num_irqs(d)        ((d)->arch.vgic.nr_spis + 32)
 
+/*
+ * Allocate a guest VIRQ
+ *  - is_spi == 0 => allocate a PPI. It will be the same on every vCPU
+ *  - is_spi == 1 => allocate an SPI
+ */
+extern int vgic_allocate_virq(struct domain *d, bool is_spi);
+/* Reserve a specific guest vIRQ */
+extern bool vgic_reserve_virq(struct domain *d, unsigned int virq);
+extern void vgic_free_virq(struct domain *d, unsigned int virq);
 static inline int vgic_allocate_ppi(struct domain *d)
 {
     return vgic_allocate_virq(d, false /* ppi */);
@@ -340,18 +340,21 @@ static inline int vgic_allocate_spi(struct domain *d)
     return vgic_allocate_virq(d, true /* spi */);
 }
 
-extern void vgic_free_virq(struct domain *d, unsigned int virq);
+struct irq_desc *vgic_get_hw_irq_desc(struct domain *d, struct vcpu *v,
+                                      unsigned int virq);
+int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                        struct irq_desc *desc, bool connect);
 
-void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
-                      paddr_t vbase, uint32_t aliased_offset);
+bool vgic_evtchn_irq_pending(struct vcpu *v);
 
-#ifdef CONFIG_HAS_GICV3
-struct rdist_region;
-void vgic_v3_setup_hw(paddr_t dbase,
-                      unsigned int nr_rdist_regions,
-                      const struct rdist_region *regions,
-                      unsigned int intid_bits);
-#endif
+int domain_vgic_register(struct domain *d, int *mmio_count);
+int domain_vgic_init(struct domain *d, unsigned int nr_spis);
+void domain_vgic_free(struct domain *d);
+int vcpu_vgic_init(struct vcpu *vcpu);
+int vcpu_vgic_free(struct vcpu *vcpu);
+
+int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                    bool level);
 
 #endif /* __ASM_ARM_VGIC_H__ */
 
Signature · 7 lines
2.14.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 11/49
#23 lore raw

Re: [RFC PATCH 11/49] ARM: VGIC: reorder prototypes in vgic.h

Hi Andre,

Quoted 10 lines · Andre Przywara

/*
- * Allocate a guest VIRQ
- * - spi == 0 => allocate a PPI. It will be the same on every vCPU
- * - spi == 1 => allocate an SPI
+ * In the moment vgic_num_irqs() just covers SPIs and the private IRQs,
+ * as it's mostly used for allocating the pending_irq and irq_desc array,
+ * in which LPIs don't participate.
*/

xen/include/asm-arm/vgic.h +1 -1
-extern int vgic_allocate_virq(struct domain *d, bool spi);
+#define vgic_num_irqs(d)        ((d)->arch.vgic.nr_spis + 32)

xen/include/asm-arm/vgic.h extern void register_vgic_ops(struct domain *d, const struct vgic_ops *ops); · line 332

Quoted 10 lines
xen/include/asm-arm/vgic.h +9 -0
 
+/*
+ * Allocate a guest VIRQ
+ *  - is_spi == 0 => allocate a PPI. It will be the same on every vCPU
+ *  - is_spi == 1 => allocate an SPI
+ */
+extern int vgic_allocate_virq(struct domain *d, bool is_spi);
+/* Reserve a specific guest vIRQ */
+extern bool vgic_reserve_virq(struct domain *d, unsigned int virq);
+extern void vgic_free_virq(struct domain *d, unsigned int virq);

newline here please.

Otherwise the split looks good:

Cheers,

Quoted 36 lines
  static inline int vgic_allocate_ppi(struct domain *d)
  {
      return vgic_allocate_virq(d, false /* ppi */);
xen/include/asm-arm/vgic.h +13 -10
@@ -340,18 +340,21 @@ static inline int vgic_allocate_spi(struct domain *d)
      return vgic_allocate_virq(d, true /* spi */);
  }
 
-extern void vgic_free_virq(struct domain *d, unsigned int virq);
+struct irq_desc *vgic_get_hw_irq_desc(struct domain *d, struct vcpu *v,
+                                      unsigned int virq);
+int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                        struct irq_desc *desc, bool connect);
 
-void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
-                      paddr_t vbase, uint32_t aliased_offset);
+bool vgic_evtchn_irq_pending(struct vcpu *v);
 
-#ifdef CONFIG_HAS_GICV3
-struct rdist_region;
-void vgic_v3_setup_hw(paddr_t dbase,
-                      unsigned int nr_rdist_regions,
-                      const struct rdist_region *regions,
-                      unsigned int intid_bits);
-#endif
+int domain_vgic_register(struct domain *d, int *mmio_count);
+int domain_vgic_init(struct domain *d, unsigned int nr_spis);
+void domain_vgic_free(struct domain *d);
+int vcpu_vgic_init(struct vcpu *vcpu);
+int vcpu_vgic_free(struct vcpu *vcpu);
+
+int vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
+                    bool level);
 
  #endif /* __ASM_ARM_VGIC_H__ */
 
Signature · 6 lines
Julien Grall

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

RFC PATCH 12/49 ARM: VGIC: introduce gic_get_nr_lrs()

In reply to Andre Przywara

So far the number of list registers (LRs) a GIC implements is only needed in the hardware facing side of the VGIC code (gic-vgic.c). The new VGIC will need this information in more and multiple places, so export a function that returns the number.

---
 xen/arch/arm/gic-vgic.c   | 10 +++++-----
 xen/arch/arm/gic.c        |  5 +++++
 xen/include/asm-arm/gic.h |  1 +
 3 files changed, 11 insertions(+), 5 deletions(-)
xen/arch/arm/gic-vgic.c +5 -5
index d273863556..c92626e4ee 100644
@@ -25,7 +25,7 @@
 #include <asm/gic.h>
 #include <asm/vgic.h>
 
-#define lr_all_full() (this_cpu(lr_mask) == ((1 << gic_hw_ops->info->nr_lrs) - 1))
+#define lr_all_full() (this_cpu(lr_mask) == ((1 << gic_get_nr_lrs()) - 1))
 
 #undef GIC_DEBUG
 
@@ -110,7 +110,7 @@ static unsigned int gic_find_unused_lr(struct vcpu *v,
                                        struct pending_irq *p,
                                        unsigned int lr)
 {
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
     unsigned long *lr_mask = (unsigned long *) &this_cpu(lr_mask);
     struct gic_lr lr_val;
 
@@ -137,7 +137,7 @@ void gic_raise_guest_irq(struct vcpu *v, unsigned int virtual_irq,
         unsigned int priority)
 {
     int i;
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
     struct pending_irq *p = irq_to_pending(v, virtual_irq);
 
     ASSERT(spin_is_locked(&v->arch.vgic.lock));
@@ -251,7 +251,7 @@ void gic_clear_lrs(struct vcpu *v)
 {
     int i = 0;
     unsigned long flags;
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
 
     /* The idle domain has no LRs to be cleared. Since gic_restore_state
      * doesn't write any LR registers for the idle domain they could be
@@ -278,7 +278,7 @@ static void gic_restore_pending_irqs(struct vcpu *v)
     struct pending_irq *p, *t, *p_r;
     struct list_head *inflight_r;
     unsigned long flags;
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
     int lrs = nr_lrs;
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
xen/arch/arm/gic.c +5 -0
index 968e46fabb..89873c1df4 100644
@@ -47,6 +47,11 @@ void register_gic_ops(const struct gic_hw_operations *ops)
     gic_hw_ops = ops;
 }
 
+int gic_get_nr_lrs(void)
+{
+    return gic_hw_ops->info->nr_lrs;
+}
+
 static void clear_cpu_lr_mask(void)
 {
     this_cpu(lr_mask) = 0ULL;
xen/include/asm-arm/gic.h +1 -0
index 1d382b0ade..c1f027d703 100644
@@ -222,6 +222,7 @@ enum gic_version {
 DECLARE_PER_CPU(uint64_t, lr_mask);
 
 extern enum gic_version gic_hw_version(void);
+extern int gic_get_nr_lrs(void);
 
 /* Program the IRQ type into the GIC */
 void gic_set_irq_type(struct irq_desc *desc, unsigned int type);
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 12/49] ARM: VGIC: introduce gic_get_nr_lrs()

Hi Andre,

Quoted 66 lines · Andre Przywara

So far the number of list registers (LRs) a GIC implements is only needed in the hardware facing side of the VGIC code (gic-vgic.c). The new VGIC will need this information in more and multiple places, so export a function that returns the number.

---
  xen/arch/arm/gic-vgic.c   | 10 +++++-----
  xen/arch/arm/gic.c        |  5 +++++
  xen/include/asm-arm/gic.h |  1 +
  3 files changed, 11 insertions(+), 5 deletions(-)
xen/arch/arm/gic-vgic.c +5 -5
index d273863556..c92626e4ee 100644
@@ -25,7 +25,7 @@
  #include <asm/gic.h>
  #include <asm/vgic.h>
 
-#define lr_all_full() (this_cpu(lr_mask) == ((1 << gic_hw_ops->info->nr_lrs) - 1))
+#define lr_all_full() (this_cpu(lr_mask) == ((1 << gic_get_nr_lrs()) - 1))
 
  #undef GIC_DEBUG
 
@@ -110,7 +110,7 @@ static unsigned int gic_find_unused_lr(struct vcpu *v,
                                         struct pending_irq *p,
                                         unsigned int lr)
  {
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
      unsigned long *lr_mask = (unsigned long *) &this_cpu(lr_mask);
      struct gic_lr lr_val;
 
@@ -137,7 +137,7 @@ void gic_raise_guest_irq(struct vcpu *v, unsigned int virtual_irq,
          unsigned int priority)
  {
      int i;
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
      struct pending_irq *p = irq_to_pending(v, virtual_irq);
 
      ASSERT(spin_is_locked(&v->arch.vgic.lock));
@@ -251,7 +251,7 @@ void gic_clear_lrs(struct vcpu *v)
  {
      int i = 0;
      unsigned long flags;
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
 
      /* The idle domain has no LRs to be cleared. Since gic_restore_state
       * doesn't write any LR registers for the idle domain they could be
@@ -278,7 +278,7 @@ static void gic_restore_pending_irqs(struct vcpu *v)
      struct pending_irq *p, *t, *p_r;
      struct list_head *inflight_r;
      unsigned long flags;
-    unsigned int nr_lrs = gic_hw_ops->info->nr_lrs;
+    unsigned int nr_lrs = gic_get_nr_lrs();
      int lrs = nr_lrs;
 
      spin_lock_irqsave(&v->arch.vgic.lock, flags);
xen/arch/arm/gic.c +0 -0
index 968e46fabb..89873c1df4 100644

xen/arch/arm/gic.c void register_gic_ops(const struct gic_hw_operations *ops) · line 50

Quoted 4 lines
xen/arch/arm/gic.c +1 -0
@@ -47,6 +47,11 @@ void register_gic_ops(const struct gic_hw_operations *ops)
      gic_hw_ops = ops;
  }
 
+int gic_get_nr_lrs(void)

unsigned int here please.

Also, given that gic_hw_ops is exported in gic.h, it would make sense to make that helper static inline in gic.h.

Quoted 13 lines
xen/arch/arm/gic.c +4 -0
+{
+    return gic_hw_ops->info->nr_lrs;
+}
+
  static void clear_cpu_lr_mask(void)
  {
      this_cpu(lr_mask) = 0ULL;
xen/include/asm-arm/gic.h +0 -0
index 1d382b0ade..c1f027d703 100644

xen/include/asm-arm/gic.h enum gic_version { · line 228

Quoted 7 lines
xen/include/asm-arm/gic.h +1 -0
@@ -222,6 +222,7 @@ enum gic_version {
  DECLARE_PER_CPU(uint64_t, lr_mask);
 
  extern enum gic_version gic_hw_version(void);
+extern int gic_get_nr_lrs(void);
 
  /* Program the IRQ type into the GIC */
  void gic_set_irq_type(struct irq_desc *desc, unsigned int type);

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 13/49 ARM: VGIC: Add hypervisor base address to vgic_v2_setup_hw()

In reply to Andre Przywara

The new VGIC will need to know the hypervisor base address at some point, which is private to the hardware facing part of the VGIC so far. Add a parameter to vgic_v2_setup_hw() to pass this address on, so a VGIC implementation can make use of it.
The current VGIC ignores this new parameter.

TODO: add proper value for GICv2 on GICv3 emulation!

---
 xen/arch/arm/gic-v2.c      | 3 ++-
 xen/arch/arm/gic-v3.c      | 3 ++-
 xen/arch/arm/vgic-v2.c     | 3 ++-
 xen/include/asm-arm/vgic.h | 3 ++-
 4 files changed, 8 insertions(+), 4 deletions(-)
xen/arch/arm/gic-v2.c +2 -1
index 2b271ba322..7a18abecfa 100644
@@ -1207,7 +1207,8 @@ static int __init gicv2_init(void)
     if ( !gicv2.map_hbase )
         panic("GICv2: Failed to ioremap for GIC Virtual interface\n");
 
-    vgic_v2_setup_hw(dbase, cbase, csize, vbase, aliased_offset);
+    vgic_v2_setup_hw(dbase, cbase, csize, vbase, gicv2.map_hbase,
+                     aliased_offset);
 
     /* Global settings: interrupt distributor */
     spin_lock_init(&gicv2.lock);
xen/arch/arm/gic-v3.c +2 -1
index ea14ab4028..08d4703687 100644
@@ -1238,7 +1238,8 @@ static void __init gicv3_init_v2(void)
     printk("GICv3 compatible with GICv2 cbase %#"PRIpaddr" vbase %#"PRIpaddr"\n",
            cbase, vbase);
 
-    vgic_v2_setup_hw(dbase, cbase, csize, vbase, 0);
+    /* TODO: provide the proper HBASE address! */
+    vgic_v2_setup_hw(dbase, cbase, csize, vbase, NULL, 0);
 }
 
 static void __init gicv3_ioremap_distributor(paddr_t dist_paddr)
xen/arch/arm/vgic-v2.c +2 -1
index 646d1f3d12..96d543c005 100644
@@ -47,7 +47,8 @@ static struct {
 } vgic_v2_hw;
 
 void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
-                      paddr_t vbase, uint32_t aliased_offset)
+                      paddr_t vbase, void __iomem *hbase,
+                      uint32_t aliased_offset)
 {
     vgic_v2_hw.enabled = true;
     vgic_v2_hw.dbase = dbase;
xen/include/asm-arm/vgic.h +2 -1
index 8c39ff1402..85ad2aca79 100644
@@ -302,7 +302,8 @@ extern bool vgic_to_sgi(struct vcpu *v, register_t sgir,
 extern bool vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq);
 
 void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
-                      paddr_t vbase, uint32_t aliased_offset);
+                      paddr_t vbase, void __iomem *hbase,
+                      uint32_t aliased_offset);
 
 #ifdef CONFIG_HAS_GICV3
 struct rdist_region;
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 13/49] ARM: VGIC: Add hypervisor base address to vgic_v2_setup_hw()

Hi Andre,

Quoted 35 lines · Andre Przywara

The new VGIC will need to know the hypervisor base address at some point, which is private to the hardware facing part of the VGIC so far. Add a parameter to vgic_v2_setup_hw() to pass this address on, so a VGIC implementation can make use of it.
The current VGIC ignores this new parameter.

TODO: add proper value for GICv2 on GICv3 emulation!

---
  xen/arch/arm/gic-v2.c      | 3 ++-
  xen/arch/arm/gic-v3.c      | 3 ++-
  xen/arch/arm/vgic-v2.c     | 3 ++-
  xen/include/asm-arm/vgic.h | 3 ++-
  4 files changed, 8 insertions(+), 4 deletions(-)
xen/arch/arm/gic-v2.c +2 -1
index 2b271ba322..7a18abecfa 100644
@@ -1207,7 +1207,8 @@ static int __init gicv2_init(void)
      if ( !gicv2.map_hbase )
          panic("GICv2: Failed to ioremap for GIC Virtual interface\n");
 
-    vgic_v2_setup_hw(dbase, cbase, csize, vbase, aliased_offset);
+    vgic_v2_setup_hw(dbase, cbase, csize, vbase, gicv2.map_hbase,
+                     aliased_offset);
 
      /* Global settings: interrupt distributor */
      spin_lock_init(&gicv2.lock);
xen/arch/arm/gic-v3.c +0 -0
index ea14ab4028..08d4703687 100644

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

Quoted 5 lines
xen/arch/arm/gic-v3.c +1 -1
@@ -1238,7 +1238,8 @@ static void __init gicv3_init_v2(void)
      printk("GICv3 compatible with GICv2 cbase %#"PRIpaddr" vbase %#"PRIpaddr"\n",
             cbase, vbase);
 
-    vgic_v2_setup_hw(dbase, cbase, csize, vbase, 0);
+    /* TODO: provide the proper HBASE address! */

Well, on GICv3 the hypervisor interface will be configured using system registers. So I am not sure how your new interface is going to work with GICv3.

But IHMO, this is breaking the spirit of this interface. The goal is to tell "we can support a virtual GICv2". How the LRs (or anything touching the hardware) should be done using gic_hw_ops.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 14/49 ARM: VGIC: extend GIC CPU interface definitions

In reply to Andre Przywara

The new VGIC will shortly use more bits of the GICC_CTLR register, so add the respective definitions from the manual.
Also add a missing definition for GICV_PMR_PRIORITY_MASK.

---
 xen/arch/arm/gic-v2.c     |  2 +-
 xen/include/asm-arm/gic.h | 18 ++++++++++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)
xen/arch/arm/gic-v2.c +1 -1
index 7a18abecfa..2e35892881 100644
@@ -358,7 +358,7 @@ static void gicv2_cpu_init(void)
     /* Finest granularity of priority */
     writel_gicc(0x0, GICC_BPR);
     /* Turn on delivery */
-    writel_gicc(GICC_CTL_ENABLE|GICC_CTL_EOI, GICC_CTLR);
+    writel_gicc(GICC_CTL_ENABLE0|GICC_CTL_EOI, GICC_CTLR);
 }
 
 static void gicv2_cpu_disable(void)
xen/include/asm-arm/gic.h +16 -2
index c1f027d703..c4c68c7770 100644
@@ -77,6 +77,7 @@
 #define GICC_EOIR       (0x0010)
 #define GICC_RPR        (0x0014)
 #define GICC_HPPIR      (0x0018)
+#define GICC_ABPR       (0x001c)
 #define GICC_APR        (0x00D0)
 #define GICC_NSAPR      (0x00E0)
 #define GICC_IIDR       (0x00FC)
@@ -102,8 +103,18 @@
 #define GICD_TYPE_SEC   0x400
 #define GICD_TYPER_DVIS (1U << 18)
 
-#define GICC_CTL_ENABLE 0x1
-#define GICC_CTL_EOI    (0x1 << 9)
+#define GICC_CTL_ENABLE0_SHIFT  0
+#define GICC_CTL_ENABLE0        (1U << GICC_CTL_ENABLE0_SHIFT)
+#define GICC_CTL_ENABLE1_SHIFT  1
+#define GICC_CTL_ENABLE1        (1U << GICC_CTL_ENABLE1)
+#define GICC_CTL_AC_SHIFT       2
+#define GICC_CTL_AC             (1U << GICC_CTL_AC_SHIFT)
+#define GICC_CTL_FIQEN_SHIFT    3
+#define GICC_CTL_FIQEN          (1U << GICC_CTL_FIQEN_SHIFT)
+#define GICC_CTL_CBPR_SHIFT     4
+#define GICC_CTL_CBPR           (1U << GICC_CTL_CBPR_SHIFT)
+#define GICC_CTL_EOI_SHIFT      9
+#define GICC_CTL_EOI            (1U << GICC_CTL_EOI_SHIFT)
 
 #define GICC_IA_IRQ       0x03ff
 #define GICC_IA_CPU_MASK  0x1c00
@@ -127,6 +138,9 @@
 #define GICH_MISR_VGRP1E  (1 << 6)
 #define GICH_MISR_VGRP1D  (1 << 7)
 
+#define GICV_PMR_PRIORITY_SHIFT		3
+#define GICV_PMR_PRIORITY_MASK		(0x1f << GICV_PMR_PRIORITY_SHIFT)
+
 /*
  * The minimum GICC_BPR is required to be in the range 0-3. We set
  * GICC_BPR to 0 but we must expect that it might be 3. This means we
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 14/49] ARM: VGIC: extend GIC CPU interface definitions

Hi,

This patch seem to modify the GICv2 CPU interface definitions. If so, please make it clear in the commit message/title.

Quoted 3 lines · Andre Przywara

The new VGIC will shortly use more bits of the GICC_CTLR register, so add the respective definitions from the manual.
Also add a missing definition for GICV_PMR_PRIORITY_MASK.

You also add GICC_ABPR here.

Quoted 33 lines
---
  xen/arch/arm/gic-v2.c     |  2 +-
  xen/include/asm-arm/gic.h | 18 ++++++++++++++++--
  2 files changed, 17 insertions(+), 3 deletions(-)
xen/arch/arm/gic-v2.c +1 -1
index 7a18abecfa..2e35892881 100644
@@ -358,7 +358,7 @@ static void gicv2_cpu_init(void)
      /* Finest granularity of priority */
      writel_gicc(0x0, GICC_BPR);
      /* Turn on delivery */
-    writel_gicc(GICC_CTL_ENABLE|GICC_CTL_EOI, GICC_CTLR);
+    writel_gicc(GICC_CTL_ENABLE0|GICC_CTL_EOI, GICC_CTLR);
  }
 
  static void gicv2_cpu_disable(void)
xen/include/asm-arm/gic.h +1 -0
index c1f027d703..c4c68c7770 100644
@@ -77,6 +77,7 @@
  #define GICC_EOIR       (0x0010)
  #define GICC_RPR        (0x0014)
  #define GICC_HPPIR      (0x0018)
+#define GICC_ABPR       (0x001c)
  #define GICC_APR        (0x00D0)
  #define GICC_NSAPR      (0x00E0)
  #define GICC_IIDR       (0x00FC)

xen/include/asm-arm/gic.h line 107

Quoted 7 lines
xen/include/asm-arm/gic.h +2 -2
@@ -102,8 +103,18 @@
  #define GICD_TYPE_SEC   0x400
  #define GICD_TYPER_DVIS (1U << 18)
 
-#define GICC_CTL_ENABLE 0x1
-#define GICC_CTL_EOI    (0x1 << 9)
+#define GICC_CTL_ENABLE0_SHIFT  0
+#define GICC_CTL_ENABLE0        (1U << GICC_CTL_ENABLE0_SHIFT)

I guess GICCC_CTLR_ENABLE is renamed to GICC_CTL_ENABLE0 to match the spec. If so, please mention it in the commit message.

Quoted 15 lines
xen/include/asm-arm/gic.h +10 -0
+#define GICC_CTL_ENABLE1_SHIFT  1
+#define GICC_CTL_ENABLE1        (1U << GICC_CTL_ENABLE1)
+#define GICC_CTL_AC_SHIFT       2
+#define GICC_CTL_AC             (1U << GICC_CTL_AC_SHIFT)
+#define GICC_CTL_FIQEN_SHIFT    3
+#define GICC_CTL_FIQEN          (1U << GICC_CTL_FIQEN_SHIFT)
+#define GICC_CTL_CBPR_SHIFT     4
+#define GICC_CTL_CBPR           (1U << GICC_CTL_CBPR_SHIFT)
+#define GICC_CTL_EOI_SHIFT      9
+#define GICC_CTL_EOI            (1U << GICC_CTL_EOI_SHIFT)
 
  #define GICC_IA_IRQ       0x03ff
  #define GICC_IA_CPU_MASK  0x1c00

xen/include/asm-arm/gic.h line 146

Quoted 9 lines
xen/include/asm-arm/gic.h +3 -0
@@ -127,6 +138,9 @@
  #define GICH_MISR_VGRP1E  (1 << 6)
  #define GICH_MISR_VGRP1D  (1 << 7)
 
+#define GICV_PMR_PRIORITY_SHIFT		3
+#define GICV_PMR_PRIORITY_MASK		(0x1f << GICV_PMR_PRIORITY_SHIFT)
+
  /*
   * The minimum GICC_BPR is required to be in the range 0-3. We set
   * GICC_BPR to 0 but we must expect that it might be 3. This means we

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 15/49 ARM: GIC: Allow tweaking the active state of an IRQ

In reply to Andre Przywara

When playing around with hardware mapped, level triggered virtual IRQs, there is the need to explicitly set the active state of an interrupt at some point in time.
To prepare the GIC for that, we introduce a set_active_state() function to let the VGIC manipulate the state of an associated hardware IRQ.

---
 xen/arch/arm/gic-v2.c     |  9 +++++++++
 xen/arch/arm/gic-v3.c     | 16 ++++++++++++++++
 xen/arch/arm/gic.c        |  5 +++++
 xen/include/asm-arm/gic.h |  5 +++++
 4 files changed, 35 insertions(+)
xen/arch/arm/gic-v2.c +9 -0
index 2e35892881..5339f69fbc 100644
@@ -235,6 +235,14 @@ static unsigned int gicv2_read_irq(void)
     return (readl_gicc(GICC_IAR) & GICC_IA_IRQ);
 }
 
+static void gicv2_set_active_state(int irq, bool active)
+{
+    if (active)
+        writel_gicd(1U << (irq % 32), GICD_ISACTIVER + (irq / 32) * 4);
+    else
+        writel_gicd(1U << (irq % 32), GICD_ICACTIVER + (irq / 32) * 4);
+}
+
 static void gicv2_set_irq_type(struct irq_desc *desc, unsigned int type)
 {
     uint32_t cfg, actual, edgebit;
@@ -1241,6 +1249,7 @@ const static struct gic_hw_operations gicv2_ops = {
     .eoi_irq             = gicv2_eoi_irq,
     .deactivate_irq      = gicv2_dir_irq,
     .read_irq            = gicv2_read_irq,
+    .set_active_state    = gicv2_set_active_state,
     .set_irq_type        = gicv2_set_irq_type,
     .set_irq_priority    = gicv2_set_irq_priority,
     .send_SGI            = gicv2_send_SGI,
xen/arch/arm/gic-v3.c +16 -0
index 08d4703687..595eaef43a 100644
@@ -475,6 +475,21 @@ static unsigned int gicv3_read_irq(void)
     return irq;
 }
 
+static void gicv3_set_active_state(int irq, bool active)
+{
+    void __iomem *base;
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
+        base = GICD_RDIST_SGI_BASE;
+
+    if ( active )
+        writel(1U << (irq % 32), base + GICD_ISACTIVER);
+    else
+        writel(1U << (irq % 32), base + GICD_ICACTIVER);
+}
+
 static inline uint64_t gicv3_mpidr_to_affinity(int cpu)
 {
      uint64_t mpidr = cpu_logical_map(cpu);
@@ -1722,6 +1737,7 @@ static const struct gic_hw_operations gicv3_ops = {
     .eoi_irq             = gicv3_eoi_irq,
     .deactivate_irq      = gicv3_dir_irq,
     .read_irq            = gicv3_read_irq,
+    .set_active_state    = gicv3_set_active_state,
     .set_irq_type        = gicv3_set_irq_type,
     .set_irq_priority    = gicv3_set_irq_priority,
     .send_SGI            = gicv3_send_sgi,
xen/arch/arm/gic.c +5 -0
index 89873c1df4..dfc2108c4d 100644
@@ -92,6 +92,11 @@ void gic_restore_state(struct vcpu *v)
     isb();
 }
 
+void gic_set_active_state(int irq, bool state)
+{
+    gic_hw_ops->set_active_state(irq, state);
+}
+
 /* desc->irq needs to be disabled before calling this function */
 void gic_set_irq_type(struct irq_desc *desc, unsigned int type)
 {
xen/include/asm-arm/gic.h +5 -0
index c4c68c7770..d330860580 100644
@@ -238,6 +238,9 @@ DECLARE_PER_CPU(uint64_t, lr_mask);
 extern enum gic_version gic_hw_version(void);
 extern int gic_get_nr_lrs(void);
 
+/* Force the state of an IRQ to active. */
+void gic_set_active_state(int irq, bool state);
+
 /* Program the IRQ type into the GIC */
 void gic_set_irq_type(struct irq_desc *desc, unsigned int type);
 
@@ -347,6 +350,8 @@ struct gic_hw_operations {
     void (*deactivate_irq)(struct irq_desc *irqd);
     /* Read IRQ id and Ack */
     unsigned int (*read_irq)(void);
+    /* Force the state of an IRQ to active */
+    void (*set_active_state)(int irq, bool state);
     /* Set IRQ type */
     void (*set_irq_type)(struct irq_desc *desc, unsigned int type);
     /* Set IRQ priority */
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 15/49] ARM: GIC: Allow tweaking the active state of an IRQ

Hi Andre,

Quoted 19 lines · Andre Przywara

When playing around with hardware mapped, level triggered virtual IRQs, there is the need to explicitly set the active state of an interrupt at some point in time.
To prepare the GIC for that, we introduce a set_active_state() function to let the VGIC manipulate the state of an associated hardware IRQ.

---
  xen/arch/arm/gic-v2.c     |  9 +++++++++
  xen/arch/arm/gic-v3.c     | 16 ++++++++++++++++
  xen/arch/arm/gic.c        |  5 +++++
  xen/include/asm-arm/gic.h |  5 +++++
  4 files changed, 35 insertions(+)
xen/arch/arm/gic-v2.c +0 -0
index 2e35892881..5339f69fbc 100644

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_irq(void) · line 238

Quoted 4 lines
xen/arch/arm/gic-v2.c +1 -0
@@ -235,6 +235,14 @@ static unsigned int gicv2_read_irq(void)
      return (readl_gicc(GICC_IAR) & GICC_IA_IRQ);
  }
 
+static void gicv2_set_active_state(int irq, bool active)

I would much prefer to have an irq_desc in parameter. This is matching the other interface and you could update the flags such as _IRQ_INPROGRESS which you don't do at the moment.

Also, who is preventing two CPUs to clear the active bit at the same time?

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_irq(void) · line 243

Quoted 5 lines
xen/arch/arm/gic-v2.c +5 -0
+{
+    if (active)
+        writel_gicd(1U << (irq % 32), GICD_ISACTIVER + (irq / 32) * 4);
+    else
+        writel_gicd(1U << (irq % 32), GICD_ICACTIVER + (irq / 32) * 4);

You will have a few places in the code usually similar construct. It would make sense to introduce a helper poke as we have in the GICv3 code.

Quoted 24 lines
xen/arch/arm/gic-v2.c +3 -0
+}
+
  static void gicv2_set_irq_type(struct irq_desc *desc, unsigned int type)
  {
      uint32_t cfg, actual, edgebit;
@@ -1241,6 +1249,7 @@ const static struct gic_hw_operations gicv2_ops = {
      .eoi_irq             = gicv2_eoi_irq,
      .deactivate_irq      = gicv2_dir_irq,
      .read_irq            = gicv2_read_irq,
+    .set_active_state    = gicv2_set_active_state,
      .set_irq_type        = gicv2_set_irq_type,
      .set_irq_priority    = gicv2_set_irq_priority,
      .send_SGI            = gicv2_send_SGI,
xen/arch/arm/gic-v3.c +3 -0
index 08d4703687..595eaef43a 100644
@@ -475,6 +475,21 @@ static unsigned int gicv3_read_irq(void)
      return irq;
  }
 
+static void gicv3_set_active_state(int irq, bool active)
+{
+    void __iomem *base;

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 10 lines
xen/arch/arm/gic-v3.c +10 -0
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
+        base = GICD_RDIST_SGI_BASE;
+
+    if ( active )
+        writel(1U << (irq % 32), base + GICD_ISACTIVER);
+    else
+        writel(1U << (irq % 32), base + GICD_ICACTIVER);

Shouldn't you wait until RWP bits is cleared here?

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 491

Quoted 1 line
xen/arch/arm/gic-v3.c +1 -0
+}

Why don't you use the function poke?

Quoted 44 lines
xen/arch/arm/gic-v3.c +2 -0
+
  static inline uint64_t gicv3_mpidr_to_affinity(int cpu)
  {
       uint64_t mpidr = cpu_logical_map(cpu);
@@ -1722,6 +1737,7 @@ static const struct gic_hw_operations gicv3_ops = {
      .eoi_irq             = gicv3_eoi_irq,
      .deactivate_irq      = gicv3_dir_irq,
      .read_irq            = gicv3_read_irq,
+    .set_active_state    = gicv3_set_active_state,
      .set_irq_type        = gicv3_set_irq_type,
      .set_irq_priority    = gicv3_set_irq_priority,
      .send_SGI            = gicv3_send_sgi,
xen/arch/arm/gic.c +5 -0
index 89873c1df4..dfc2108c4d 100644
@@ -92,6 +92,11 @@ void gic_restore_state(struct vcpu *v)
      isb();
  }
 
+void gic_set_active_state(int irq, bool state)
+{
+    gic_hw_ops->set_active_state(irq, state);
+}
+
  /* desc->irq needs to be disabled before calling this function */
  void gic_set_irq_type(struct irq_desc *desc, unsigned int type)
  {
xen/include/asm-arm/gic.h +3 -0
index c4c68c7770..d330860580 100644
@@ -238,6 +238,9 @@ DECLARE_PER_CPU(uint64_t, lr_mask);
  extern enum gic_version gic_hw_version(void);
  extern int gic_get_nr_lrs(void);
 
+/* Force the state of an IRQ to active. */
+void gic_set_active_state(int irq, bool state);
+
  /* Program the IRQ type into the GIC */
  void gic_set_irq_type(struct irq_desc *desc, unsigned int type);
 

xen/include/asm-arm/gic.h struct gic_hw_operations { · line 357

Quoted 8 lines
xen/include/asm-arm/gic.h +2 -0
@@ -347,6 +350,8 @@ struct gic_hw_operations {
      void (*deactivate_irq)(struct irq_desc *irqd);
      /* Read IRQ id and Ack */
      unsigned int (*read_irq)(void);
+    /* Force the state of an IRQ to active */
+    void (*set_active_state)(int irq, bool state);
      /* Set IRQ type */
      void (*set_irq_type)(struct irq_desc *desc, unsigned int type);
      /* Set IRQ priority */

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 15/49] ARM: GIC: Allow tweaking the active state of an IRQ

Hi,

Quoted 17 lines · Julien Grall

Hi Andre,

Quoted 14 lines · Andre Przywara

When playing around with hardware mapped, level triggered virtual IRQs, there is the need to explicitly set the active state of an interrupt at some point in time.
To prepare the GIC for that, we introduce a set_active_state() function to let the VGIC manipulate the state of an associated hardware IRQ.

---
  xen/arch/arm/gic-v2.c     |  9 +++++++++
  xen/arch/arm/gic-v3.c     | 16 ++++++++++++++++
  xen/arch/arm/gic.c        |  5 +++++
  xen/include/asm-arm/gic.h |  5 +++++

  4 files changed, 35 insertions(+)
Quoted 11 lines

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_irq(void) · line 237

Quoted 8 lines
xen/arch/arm/gic-v2.c +0 -0
index 2e35892881..5339f69fbc 100644
@@ -235,6 +235,14 @@ static unsigned int gicv2_read_irq(void)
       return (readl_gicc(GICC_IAR) & GICC_IA_IRQ);
   }
   +static void gicv2_set_active_state(int irq, bool active)

I would much prefer to have an irq_desc in parameter. This is matching the other interface

... and that's why I had it just like this in my first version. However this proved to be nasty because I now need to get this irq_desc pointer first, as the caller doesn't have it already. Since all we have and need is the actual hardware IRQ number, I found it more straight-forward to just use that number directly instead of going via the pointer and back (h/w intid => irq_desc => irq).

Quoted 2 lines

and you could update the flags such as
_IRQ_INPROGRESS which you don't do at the moment.

Mmh, interesting point. I guess I should also clear this bit in the new VGIC. At least once I wrapped my head around what this flag is *actually* for (in conjunction with _IRQ_GUEST).
Anyway I guess this bit would still be set in our case.

Quoted 1 line

Also, who is preventing two CPUs to clear the active bit at the same time?

A certain hardware IRQ is assigned to one virtual IRQ on one VCPU at one time only. Besides, GICD_ICACTIVERn has wired NAND semantics, so that's naturally race free (as it was designed to be).
Unless I miss something here (happy to be pointed to an example where it causes problems).

Quoted 8 lines

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_irq(void) · line 243

Quoted 5 lines
xen/arch/arm/gic-v2.c +5 -0
+{
+    if (active)
+        writel_gicd(1U << (irq % 32), GICD_ISACTIVER + (irq / 32) * 4);
+    else
+        writel_gicd(1U << (irq % 32), GICD_ICACTIVER + (irq / 32) * 4);

You will have a few places in the code usually similar construct. It would make sense to introduce a helper poke as we have in the GICv3 code.

Sure.

Quoted 28 lines
Quoted 28 lines
xen/arch/arm/gic-v2.c +2 -0
+}
+

  static void gicv2_set_irq_type(struct irq_desc *desc, unsigned int type)
  {
      uint32_t cfg, actual, edgebit;

xen/arch/arm/gic-v2.c +1 -0
@@ -1241,6 +1249,7 @@ const static struct gic_hw_operations gicv2_ops = {
       .eoi_irq             = gicv2_eoi_irq,
       .deactivate_irq      = gicv2_dir_irq,
       .read_irq            = gicv2_read_irq,
+    .set_active_state    = gicv2_set_active_state,
       .set_irq_type        = gicv2_set_irq_type,
       .set_irq_priority    = gicv2_set_irq_priority,
       .send_SGI            = gicv2_send_SGI,
xen/arch/arm/gic-v3.c +6 -0
index 08d4703687..595eaef43a 100644
@@ -475,6 +475,21 @@ static unsigned int gicv3_read_irq(void)
       return irq;
   }
   +static void gicv3_set_active_state(int irq, bool active)
+{
+    void __iomem *base;
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
Quoted 8 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 6 lines
xen/arch/arm/gic-v3.c +6 -0
+        base = GICD_RDIST_SGI_BASE;
+
+    if ( active )
+        writel(1U << (irq % 32), base + GICD_ISACTIVER);
+    else
+        writel(1U << (irq % 32), base + GICD_ICACTIVER);

Shouldn't you wait until RWP bits is cleared here?

I don't see why. I think this action has some posted semantics anyway, so no need for any synchronisation. And also RWP does not track I[SC]ACTIVER, only ICENABLER and some CTLR bits (ARM IHI 0069D, 8.9.4:
RWP[31]).

Quoted 4 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 2 lines
xen/arch/arm/gic-v3.c +1 -0
+}

Why don't you use the function poke?

Ah, I didn't see this. But then this now does this quite costly RWP dance now. We could add a check in there to only do this if we change the affected registers or pass an explicit "bool wait_for_rwp" in there.

Thanks for staying awake on this ;-)

Cheers,
Andre.

Quoted 43 lines
Quoted 43 lines
xen/arch/arm/gic-v3.c +2 -0
+
   static inline uint64_t gicv3_mpidr_to_affinity(int cpu)
   {
        uint64_t mpidr = cpu_logical_map(cpu);
@@ -1722,6 +1737,7 @@ static const struct gic_hw_operations gicv3_ops = {
       .eoi_irq             = gicv3_eoi_irq,
       .deactivate_irq      = gicv3_dir_irq,
       .read_irq            = gicv3_read_irq,
+    .set_active_state    = gicv3_set_active_state,
       .set_irq_type        = gicv3_set_irq_type,
       .set_irq_priority    = gicv3_set_irq_priority,
       .send_SGI            = gicv3_send_sgi,
xen/arch/arm/gic.c +4 -0
index 89873c1df4..dfc2108c4d 100644
@@ -92,6 +92,11 @@ void gic_restore_state(struct vcpu *v)
       isb();
   }
   +void gic_set_active_state(int irq, bool state)
+{
+    gic_hw_ops->set_active_state(irq, state);
+}
+
   /* desc->irq needs to be disabled before calling this function */
   void gic_set_irq_type(struct irq_desc *desc, unsigned int type)
   {
xen/include/asm-arm/gic.h +0 -0
index c4c68c7770..d330860580 100644
@@ -238,6 +238,9 @@ DECLARE_PER_CPU(uint64_t, lr_mask);
   extern enum gic_version gic_hw_version(void);
   extern int gic_get_nr_lrs(void);

  +/* Force the state of an IRQ to active. */

xen/include/asm-arm/gic.h +2 -0
+void gic_set_active_state(int irq, bool state);
+

  /* Program the IRQ type into the GIC */
  void gic_set_irq_type(struct irq_desc *desc, unsigned int type); @@ -347,6 +350,8 @@ struct gic_hw_operations {
      void (*deactivate_irq)(struct irq_desc *irqd);
      /* Read IRQ id and Ack */
      unsigned int (*read_irq)(void);

Quoted 9 lines
Quoted 6 lines

+    /* Force the state of an IRQ to active */
+    void (*set_active_state)(int irq, bool state);
      /* Set IRQ type */
      void (*set_irq_type)(struct irq_desc *desc, unsigned int type); /* Set IRQ priority */

Cheers,

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

#33 lore raw

Re: [RFC PATCH 15/49] ARM: GIC: Allow tweaking the active state of an IRQ

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 37 lines · Julien Grall
Quoted 17 lines

Hi Andre,

Quoted 14 lines · Andre Przywara

When playing around with hardware mapped, level triggered virtual IRQs, there is the need to explicitly set the active state of an interrupt at some point in time.
To prepare the GIC for that, we introduce a set_active_state() function to let the VGIC manipulate the state of an associated hardware IRQ.

---
   xen/arch/arm/gic-v2.c     |  9 +++++++++
   xen/arch/arm/gic-v3.c     | 16 ++++++++++++++++
   xen/arch/arm/gic.c        |  5 +++++
   xen/include/asm-arm/gic.h |  5 +++++
   4 files changed, 35 insertions(+)
Quoted 11 lines

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_irq(void) · line 237

Quoted 8 lines
xen/arch/arm/gic-v2.c +0 -0
index 2e35892881..5339f69fbc 100644
@@ -235,6 +235,14 @@ static unsigned int gicv2_read_irq(void)
       return (readl_gicc(GICC_IAR) & GICC_IA_IRQ);
   }
   +static void gicv2_set_active_state(int irq, bool active)

I would much prefer to have an irq_desc in parameter. This is matching the other interface

... and that's why I had it just like this in my first version. However this proved to be nasty because I now need to get this irq_desc pointer first, as the caller doesn't have it already. Since all we have and need is the actual hardware IRQ number, I found it more straight-forward to just use that number directly instead of going via the pointer and back (h/w intid => irq_desc => irq).

Quoted 7 lines
Quoted 2 lines

and you could update the flags such as
_IRQ_INPROGRESS which you don't do at the moment.

Mmh, interesting point. I guess I should also clear this bit in the new VGIC. At least once I wrapped my head around what this flag is *actually* for (in conjunction with _IRQ_GUEST).
Anyway I guess this bit would still be set in our case.

For IRQ routed to the guest, the flag is used to know whether you need to EOI the interrupt on domain destruction.

In general, I would like to keep desc->status in sync for the guest IRQ. This is useful for debugging and potentially some ratelimit on interrupt (I am thinking for ITS).

Quoted 8 lines
Quoted 2 lines

Also, who is preventing two CPUs to clear the active bit at the same time?

A certain hardware IRQ is assigned to one virtual IRQ on one VCPU at one time only. Besides, GICD_ICACTIVERn has wired NAND semantics, so that's naturally race free (as it was designed to be).
Unless I miss something here (happy to be pointed to an example where it causes problems).

You could potentially have a race between ICACTIVER an ISACTIVER. This is very similar to the enable/disable part. This matters a lot when updating desc->status.

Quoted 43 lines
Quoted 28 lines
Quoted 28 lines
xen/arch/arm/gic-v2.c +3 -0
+}
+
   static void gicv2_set_irq_type(struct irq_desc *desc, unsigned int
 type)
   {
       uint32_t cfg, actual, edgebit;
@@ -1241,6 +1249,7 @@ const static struct gic_hw_operations gicv2_ops = {
       .eoi_irq             = gicv2_eoi_irq,
       .deactivate_irq      = gicv2_dir_irq,
       .read_irq            = gicv2_read_irq,
+    .set_active_state    = gicv2_set_active_state,
       .set_irq_type        = gicv2_set_irq_type,
       .set_irq_priority    = gicv2_set_irq_priority,
       .send_SGI            = gicv2_send_SGI,
xen/arch/arm/gic-v3.c +6 -0
index 08d4703687..595eaef43a 100644
@@ -475,6 +475,21 @@ static unsigned int gicv3_read_irq(void)
       return irq;
   }
   +static void gicv3_set_active_state(int irq, bool active)
+{
+    void __iomem *base;
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
Quoted 8 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 6 lines
xen/arch/arm/gic-v3.c +6 -0
+        base = GICD_RDIST_SGI_BASE;
+
+    if ( active )
+        writel(1U << (irq % 32), base + GICD_ISACTIVER);
+    else
+        writel(1U << (irq % 32), base + GICD_ICACTIVER);

Shouldn't you wait until RWP bits is cleared here?

I don't see why. I think this action has some posted semantics anyway, so no need for any synchronisation. And also RWP does not track I[SC]ACTIVER, only ICENABLER and some CTLR bits (ARM IHI 0069D, 8.9.4:
RWP[31]).

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 1 line
xen/arch/arm/gic-v3.c +1 -0
+}

Why don't you use the function poke?

Ah, I didn't see this. But then this now does this quite costly RWP dance now. We could add a check in there to only do this if we change the affected registers or pass an explicit "bool wait_for_rwp" in there.

I guess this would be useful even for the current code. If I understand correctly the RWP semantics, it should not be necessary to wait when write to ISENABLER also.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 15/49] ARM: GIC: Allow tweaking the active state of an IRQ

Hi,

Quoted 45 lines · Julien Grall
Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 40 lines · Julien Grall
Quoted 17 lines

Hi Andre,

Quoted 14 lines · Andre Przywara

When playing around with hardware mapped, level triggered virtual IRQs, there is the need to explicitly set the active state of an interrupt at some point in time.
To prepare the GIC for that, we introduce a set_active_state() function to let the VGIC manipulate the state of an associated hardware IRQ.

---
   xen/arch/arm/gic-v2.c     |  9 +++++++++ xen/arch/arm/gic-v3.c     | 16 ++++++++++++++++ xen/arch/arm/gic.c        |  5 +++++ xen/include/asm-arm/gic.h |  5 +++++

   4 files changed, 35 insertions(+)
Quoted 11 lines

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_irq(void) · line 237

Quoted 8 lines
xen/arch/arm/gic-v2.c +0 -0
index 2e35892881..5339f69fbc 100644
@@ -235,6 +235,14 @@ static unsigned int gicv2_read_irq(void)
        return (readl_gicc(GICC_IAR) & GICC_IA_IRQ);
    }
    +static void gicv2_set_active_state(int irq, bool active)

I would much prefer to have an irq_desc in parameter. This is matching the other interface

... and that's why I had it just like this in my first version. However this proved to be nasty because I now need to get this irq_desc pointer first, as the caller doesn't have it already. Since all we have and need is the actual hardware IRQ number, I found it more straight-forward to just use that number directly instead of going via the pointer and back (h/w intid => irq_desc => irq).

Quoted 2 lines

and you could update the flags such as
_IRQ_INPROGRESS which you don't do at the moment.

Quoted 7 lines
Quoted 4 lines

Mmh, interesting point. I guess I should also clear this bit in the new VGIC. At least once I wrapped my head around what this flag is *actually* for (in conjunction with _IRQ_GUEST).
Anyway I guess this bit would still be set in our case.

For IRQ routed to the guest, the flag is used to know whether you need to EOI the interrupt on domain destruction.

Yeah, I found that. In general I am a bit suspicious of replicating and tracking the hardware IRQ state in software.

Quoted 8 lines

In general, I would like to keep desc->status in sync for the guest IRQ. This is useful for debugging and potentially some ratelimit on interrupt (I am thinking for ITS).

Quoted 4 lines
Quoted 3 lines

Also, who is preventing two CPUs to clear the active bit at the same time?

Quoted 7 lines
Quoted 5 lines

A certain hardware IRQ is assigned to one virtual IRQ on one VCPU at one time only. Besides, GICD_ICACTIVERn has wired NAND semantics, so that's naturally race free (as it was designed to be).
Unless I miss something here (happy to be pointed to an example where it causes problems).

You could potentially have a race between ICACTIVER an ISACTIVER.

I don't see why this would be a problem:
Either you activate the IRQ or you deactivate it. The wired-OR/wired-NAND semantics makes sure this never gets inconsistent on the hardware side. If you issue two conflicting requests at the same time, that's a benign race, which you either don't care about or handle via locking in the code which triggers these requests.

Besides, we only do one direction in the code at the moment anyway. And this should be *clearing* the active state, and not setting it, which is a bug I discovered yesterday.

Quoted 2 lines

is very similar to the enable/disable part. This matters a lot when updating desc->status.

Which is one reason why I am suspicious of this whole state replication. But the desc lock should take care of this in general, no?

Quoted 48 lines
Quoted 48 lines
Quoted 29 lines
Quoted 29 lines
xen/arch/arm/gic-v2.c +2 -0
+}
+

   static void gicv2_set_irq_type(struct irq_desc *desc, unsigned int type)
   {
       uint32_t cfg, actual, edgebit;

xen/arch/arm/gic-v2.c +1 -0
@@ -1241,6 +1249,7 @@ const static struct gic_hw_operations
 gicv2_ops = {
        .eoi_irq             = gicv2_eoi_irq,
        .deactivate_irq      = gicv2_dir_irq,
        .read_irq            = gicv2_read_irq,
+    .set_active_state    = gicv2_set_active_state,
        .set_irq_type        = gicv2_set_irq_type,
        .set_irq_priority    = gicv2_set_irq_priority,

       .send_SGI            = gicv2_send_SGI,

xen/arch/arm/gic-v3.c +6 -0
index 08d4703687..595eaef43a 100644
@@ -475,6 +475,21 @@ static unsigned int gicv3_read_irq(void)
        return irq;
    }
    +static void gicv3_set_active_state(int irq, bool active)
+{
+    void __iomem *base;
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
Quoted 8 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 6 lines
xen/arch/arm/gic-v3.c +6 -0
+        base = GICD_RDIST_SGI_BASE;
+
+    if ( active )
+        writel(1U << (irq % 32), base + GICD_ISACTIVER);
+    else
+        writel(1U << (irq % 32), base + GICD_ICACTIVER);

Shouldn't you wait until RWP bits is cleared here?

I don't see why. I think this action has some posted semantics anyway, so no need for any synchronisation. And also RWP does not track I[SC]ACTIVER, only ICENABLER and some CTLR bits (ARM IHI 0069D, 8.9.4:
RWP[31]).

Quoted 4 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 2 lines
xen/arch/arm/gic-v3.c +1 -0
+}

Why don't you use the function poke?

Quoted 7 lines
Quoted 3 lines

Ah, I didn't see this. But then this now does this quite costly RWP dance now. We could add a check in there to only do this if we change the affected registers or pass an explicit "bool wait_for_rwp" in there.

I guess this would be useful even for the current code. If I understand correctly the RWP semantics, it should not be necessary to wait when write to ISENABLER also.

Exactly. I changed poke_irq() to do:

    if ( offset == GICD_ICENABLER )
        gicv3_wait_for_rwp(irqd->irq);

Does that sound acceptable?

I also just added poke_irq()/peek_irq() to gic-v2.c as well.

Cheers,
Andre.

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

#35 lore raw

Re: [RFC PATCH 15/49] ARM: GIC: Allow tweaking the active state of an IRQ

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 51 lines · Julien Grall
Quoted 50 lines · Andre Przywara
Quoted 1 line

Hi,

Hi Andre,

Quoted 44 lines · Julien Grall
Quoted 17 lines

Hi Andre,

Quoted 14 lines · Andre Przywara

When playing around with hardware mapped, level triggered virtual IRQs, there is the need to explicitly set the active state of an interrupt at some point in time.
To prepare the GIC for that, we introduce a set_active_state() function to let the VGIC manipulate the state of an associated hardware IRQ.

---
    xen/arch/arm/gic-v2.c     |  9 +++++++++
    xen/arch/arm/gic-v3.c     | 16 ++++++++++++++++
    xen/arch/arm/gic.c        |  5 +++++
    xen/include/asm-arm/gic.h |  5 +++++
    4 files changed, 35 insertions(+)
Quoted 11 lines

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_irq(void) · line 237

Quoted 8 lines
xen/arch/arm/gic-v2.c +0 -0
index 2e35892881..5339f69fbc 100644
@@ -235,6 +235,14 @@ static unsigned int gicv2_read_irq(void)
        return (readl_gicc(GICC_IAR) & GICC_IA_IRQ);
    }
    +static void gicv2_set_active_state(int irq, bool active)

I would much prefer to have an irq_desc in parameter. This is matching the other interface

... and that's why I had it just like this in my first version. However this proved to be nasty because I now need to get this irq_desc pointer first, as the caller doesn't have it already. Since all we have and need is the actual hardware IRQ number, I found it more straight-forward to just use that number directly instead of going via the pointer and back (h/w intid => irq_desc => irq).

Quoted 2 lines

and you could update the flags such as
_IRQ_INPROGRESS which you don't do at the moment.

Mmh, interesting point. I guess I should also clear this bit in the new VGIC. At least once I wrapped my head around what this flag is *actually* for (in conjunction with _IRQ_GUEST).
Anyway I guess this bit would still be set in our case.

Quoted 5 lines
Quoted 2 lines

For IRQ routed to the guest, the flag is used to know whether you need to EOI the interrupt on domain destruction.

Yeah, I found that. In general I am a bit suspicious of replicating and tracking the hardware IRQ state in software.

This is how Xen has been designed and I am pretty sure Linux does that same. This makes easier if you want in the future to share interrupts between multiple domains (think legacy PCI interrupt) and even abstract the hardware from the virtual GIC.

Quoted 24 lines
Quoted 9 lines

In general, I would like to keep desc->status in sync for the guest IRQ. This is useful for debugging and potentially some ratelimit on interrupt (I am thinking for ITS).

Quoted 4 lines
Quoted 3 lines

Also, who is preventing two CPUs to clear the active bit at the same time?

Quoted 7 lines
Quoted 5 lines

A certain hardware IRQ is assigned to one virtual IRQ on one VCPU at one time only. Besides, GICD_ICACTIVERn has wired NAND semantics, so that's naturally race free (as it was designed to be).
Unless I miss something here (happy to be pointed to an example where it causes problems).

You could potentially have a race between ICACTIVER an ISACTIVER.

I don't see why this would be a problem:
Either you activate the IRQ or you deactivate it. The wired-OR/wired-NAND semantics makes sure this never gets inconsistent on the hardware side. If you issue two conflicting requests at the same time, that's a benign race, which you either don't care about or handle via locking in the code which triggers these requests.

Quoted 3 lines

Besides, we only do one direction in the code at the moment anyway. And this should be *clearing* the active state, and not setting it, which is a bug I discovered yesterday.

If the code do handle only one direction, then you should add an ASSERT to check the state value rather than making believe the code is safe. It is only safe for hardware interrupt assigned to a guest (thanks to locking). It would not be for Xen.

Quoted 6 lines
Quoted 3 lines

is very similar to the enable/disable part. This matters a lot when updating desc->status.

Which is one reason why I am suspicious of this whole state replication. But the desc lock should take care of this in general, no?

Yes if you take it.

Quoted 57 lines
Quoted 49 lines
Quoted 49 lines
Quoted 30 lines
Quoted 30 lines
xen/arch/arm/gic-v2.c +3 -0
+}
+
    static void gicv2_set_irq_type(struct irq_desc *desc, unsigned int
 type)
    {
        uint32_t cfg, actual, edgebit;
@@ -1241,6 +1249,7 @@ const static struct gic_hw_operations
 gicv2_ops = {
        .eoi_irq             = gicv2_eoi_irq,
        .deactivate_irq      = gicv2_dir_irq,
        .read_irq            = gicv2_read_irq,
+    .set_active_state    = gicv2_set_active_state,
        .set_irq_type        = gicv2_set_irq_type,
        .set_irq_priority    = gicv2_set_irq_priority,

       .send_SGI            = gicv2_send_SGI,

xen/arch/arm/gic-v3.c +6 -0
index 08d4703687..595eaef43a 100644
@@ -475,6 +475,21 @@ static unsigned int gicv3_read_irq(void)
        return irq;
    }
    +static void gicv3_set_active_state(int irq, bool active)
+{
+    void __iomem *base;
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
Quoted 8 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 6 lines
xen/arch/arm/gic-v3.c +6 -0
+        base = GICD_RDIST_SGI_BASE;
+
+    if ( active )
+        writel(1U << (irq % 32), base + GICD_ISACTIVER);
+    else
+        writel(1U << (irq % 32), base + GICD_ICACTIVER);

Shouldn't you wait until RWP bits is cleared here?

I don't see why. I think this action has some posted semantics anyway, so no need for any synchronisation. And also RWP does not track I[SC]ACTIVER, only ICENABLER and some CTLR bits (ARM IHI 0069D, 8.9.4:
RWP[31]).

Quoted 4 lines

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_irq(void) · line 490

Quoted 2 lines
xen/arch/arm/gic-v3.c +1 -0
+}

Why don't you use the function poke?

Quoted 7 lines
Quoted 3 lines

Ah, I didn't see this. But then this now does this quite costly RWP dance now. We could add a check in there to only do this if we change the affected registers or pass an explicit "bool wait_for_rwp" in there.

I guess this would be useful even for the current code. If I understand correctly the RWP semantics, it should not be necessary to wait when write to ISENABLER also.

Quoted 5 lines

Exactly. I changed poke_irq() to do:

     if ( offset == GICD_ICENABLER )
         gicv3_wait_for_rwp(irqd->irq);

Does that sound acceptable?

I would prefer the prototype to be updated with another parameter that will tell whether we want to wait or not.

Quoted 6 lines

I also just added poke_irq()/peek_irq() to gic-v2.c as well.

Cheers,
Andre.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 16/49 ARM: GIC: allow reading pending state of a hardware IRQ

In reply to Andre Przywara

To synchronize level triggered interrupts which are mapped into a guest, we need to update the virtual line level at certain points in time. For a hardware mapped interrupt the GIC is the only place where we can easily access this information.
Implement a gic_hw_operations member to return the pending state of a particular interrupt. Due to hardware limitations this only works for private interrupts of the current CPU, so there is not CPU field in the prototype.

---
 xen/arch/arm/gic-v2.c     |  6 ++++++
 xen/arch/arm/gic-v3.c     | 13 +++++++++++++
 xen/arch/arm/gic.c        |  5 +++++
 xen/include/asm-arm/gic.h |  5 +++++
 4 files changed, 29 insertions(+)
xen/arch/arm/gic-v2.c +6 -0
index 5339f69fbc..30081640ac 100644
@@ -514,6 +514,11 @@ static unsigned int gicv2_read_apr(int apr_reg)
    return readl_gich(GICH_APR);
 }
 
+bool gicv2_read_pending_state(int irq)
+{
+    return readl_gicd(GICD_ISPENDR + (irq / 32) * 4) & (1U << (irq % 32));
+}
+
 static void gicv2_irq_enable(struct irq_desc *desc)
 {
     unsigned long flags;
@@ -1261,6 +1266,7 @@ const static struct gic_hw_operations gicv2_ops = {
     .write_lr            = gicv2_write_lr,
     .read_vmcr_priority  = gicv2_read_vmcr_priority,
     .read_apr            = gicv2_read_apr,
+    .read_pending_state  = gicv2_read_pending_state,
     .make_hwdom_dt_node  = gicv2_make_hwdom_dt_node,
     .make_hwdom_madt     = gicv2_make_hwdom_madt,
     .get_hwdom_extra_madt_size = gicv2_get_hwdom_extra_madt_size,
xen/arch/arm/gic-v3.c +13 -0
index 595eaef43a..2cbfeb8e03 100644
@@ -1081,6 +1081,18 @@ static unsigned int gicv3_read_apr(int apr_reg)
     }
 }
 
+static bool gicv3_read_pending_state(int irq)
+{
+    void __iomem *base;
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
+        base = GICD_RDIST_SGI_BASE;
+
+    return readl(base + GICD_ISPENDR) & (1U << (irq % 32));
+}
+
 static void gicv3_irq_enable(struct irq_desc *desc)
 {
     unsigned long flags;
@@ -1749,6 +1761,7 @@ static const struct gic_hw_operations gicv3_ops = {
     .write_lr            = gicv3_write_lr,
     .read_vmcr_priority  = gicv3_read_vmcr_priority,
     .read_apr            = gicv3_read_apr,
+    .read_pending_state  = gicv3_read_pending_state,
     .secondary_init      = gicv3_secondary_cpu_init,
     .make_hwdom_dt_node  = gicv3_make_hwdom_dt_node,
     .make_hwdom_madt     = gicv3_make_hwdom_madt,
xen/arch/arm/gic.c +5 -0
index dfc2108c4d..ce9ab2367e 100644
@@ -116,6 +116,11 @@ static void gic_set_irq_priority(struct irq_desc *desc, unsigned int priority)
     gic_hw_ops->set_irq_priority(desc, priority);
 }
 
+bool gic_read_pending_state(int irq)
+{
+    return gic_hw_ops->read_pending_state(irq);
+}
+
 /* Program the GIC to route an interrupt to the host (i.e. Xen)
  * - needs to be called with desc.lock held
  */
xen/include/asm-arm/gic.h +5 -0
index d330860580..d7fd18fd47 100644
@@ -244,6 +244,9 @@ void gic_set_active_state(int irq, bool state);
 /* Program the IRQ type into the GIC */
 void gic_set_irq_type(struct irq_desc *desc, unsigned int type);
 
+/* Read the pending state of an interrupt from the distributor. */
+bool gic_read_pending_state(int irq);
+
 /* Program the GIC to route an interrupt */
 extern void gic_route_irq_to_xen(struct irq_desc *desc, unsigned int priority);
 extern int gic_route_irq_to_guest(struct domain *, unsigned int virq,
@@ -376,6 +379,8 @@ struct gic_hw_operations {
     unsigned int (*read_vmcr_priority)(void);
     /* Read APRn register */
     unsigned int (*read_apr)(int apr_reg);
+    /* Query the pending state of an interrupt at the distributor level. */
+    bool (*read_pending_state)(int irq);
     /* Secondary CPU init */
     int (*secondary_init)(void);
     /* Create GIC node for the hardware domain */
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 16/49] ARM: GIC: allow reading pending state of a hardware IRQ

Hi Andre,

Quoted 22 lines · Andre Przywara

To synchronize level triggered interrupts which are mapped into a guest, we need to update the virtual line level at certain points in time. For a hardware mapped interrupt the GIC is the only place where we can easily access this information.
Implement a gic_hw_operations member to return the pending state of a particular interrupt. Due to hardware limitations this only works for private interrupts of the current CPU, so there is not CPU field in the prototype.

---
  xen/arch/arm/gic-v2.c     |  6 ++++++
  xen/arch/arm/gic-v3.c     | 13 +++++++++++++
  xen/arch/arm/gic.c        |  5 +++++
  xen/include/asm-arm/gic.h |  5 +++++
  4 files changed, 29 insertions(+)
xen/arch/arm/gic-v2.c +0 -0
index 5339f69fbc..30081640ac 100644

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_apr(int apr_reg) · line 517

Quoted 4 lines
xen/arch/arm/gic-v2.c +1 -0
@@ -514,6 +514,11 @@ static unsigned int gicv2_read_apr(int apr_reg)
     return readl_gich(GICH_APR);
  }
 
+bool gicv2_read_pending_state(int irq)

static

Also, I would like to see the irq turned into irq_desc to match the other interface.

xen/arch/arm/gic-v2.c static unsigned int gicv2_read_apr(int apr_reg) · line 519

Quoted 2 lines
xen/arch/arm/gic-v2.c +2 -0
+{
+    return readl_gicd(GICD_ISPENDR + (irq / 32) * 4) & (1U << (irq % 32));

See my remark in the previous patch. You might want to introduce an helper peek.

Quoted 21 lines
xen/arch/arm/gic-v2.c +3 -0
+}
+
  static void gicv2_irq_enable(struct irq_desc *desc)
  {
      unsigned long flags;
@@ -1261,6 +1266,7 @@ const static struct gic_hw_operations gicv2_ops = {
      .write_lr            = gicv2_write_lr,
      .read_vmcr_priority  = gicv2_read_vmcr_priority,
      .read_apr            = gicv2_read_apr,
+    .read_pending_state  = gicv2_read_pending_state,
      .make_hwdom_dt_node  = gicv2_make_hwdom_dt_node,
      .make_hwdom_madt     = gicv2_make_hwdom_madt,
      .get_hwdom_extra_madt_size = gicv2_get_hwdom_extra_madt_size,
xen/arch/arm/gic-v3.c +0 -0
index 595eaef43a..2cbfeb8e03 100644
@@ -1081,6 +1081,18 @@ static unsigned int gicv3_read_apr(int apr_reg)
      }
  }
 

xen/arch/arm/gic-v3.c static unsigned int gicv3_read_apr(int apr_reg) · line 1093

Quoted 10 lines
xen/arch/arm/gic-v3.c +10 -0
+static bool gicv3_read_pending_state(int irq)
+{
+    void __iomem *base;
+
+    if ( irq >= NR_GIC_LOCAL_IRQS)
+        base = GICD + (irq / 32) * 4;
+    else
+        base = GICD_RDIST_SGI_BASE;
+
+    return readl(base + GICD_ISPENDR) & (1U << (irq % 32));

Looking at the GICv3 code we don't have a peek helper. It is probably worth to add one.

Quoted 45 lines
xen/arch/arm/gic-v3.c +3 -0
+}
+
  static void gicv3_irq_enable(struct irq_desc *desc)
  {
      unsigned long flags;
@@ -1749,6 +1761,7 @@ static const struct gic_hw_operations gicv3_ops = {
      .write_lr            = gicv3_write_lr,
      .read_vmcr_priority  = gicv3_read_vmcr_priority,
      .read_apr            = gicv3_read_apr,
+    .read_pending_state  = gicv3_read_pending_state,
      .secondary_init      = gicv3_secondary_cpu_init,
      .make_hwdom_dt_node  = gicv3_make_hwdom_dt_node,
      .make_hwdom_madt     = gicv3_make_hwdom_madt,
xen/arch/arm/gic.c +5 -0
index dfc2108c4d..ce9ab2367e 100644
@@ -116,6 +116,11 @@ static void gic_set_irq_priority(struct irq_desc *desc, unsigned int priority)
      gic_hw_ops->set_irq_priority(desc, priority);
  }
 
+bool gic_read_pending_state(int irq)
+{
+    return gic_hw_ops->read_pending_state(irq);
+}
+
  /* Program the GIC to route an interrupt to the host (i.e. Xen)
   * - needs to be called with desc.lock held
   */
xen/include/asm-arm/gic.h +3 -0
index d330860580..d7fd18fd47 100644
@@ -244,6 +244,9 @@ void gic_set_active_state(int irq, bool state);
  /* Program the IRQ type into the GIC */
  void gic_set_irq_type(struct irq_desc *desc, unsigned int type);
 
+/* Read the pending state of an interrupt from the distributor. */
+bool gic_read_pending_state(int irq);
+
  /* Program the GIC to route an interrupt */
  extern void gic_route_irq_to_xen(struct irq_desc *desc, unsigned int priority);
  extern int gic_route_irq_to_guest(struct domain *, unsigned int virq,

xen/include/asm-arm/gic.h struct gic_hw_operations { · line 386

Quoted 8 lines
xen/include/asm-arm/gic.h +2 -0
@@ -376,6 +379,8 @@ struct gic_hw_operations {
      unsigned int (*read_vmcr_priority)(void);
      /* Read APRn register */
      unsigned int (*read_apr)(int apr_reg);
+    /* Query the pending state of an interrupt at the distributor level. */
+    bool (*read_pending_state)(int irq);
      /* Secondary CPU init */
      int (*secondary_init)(void);
      /* Create GIC node for the hardware domain */

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 17/49 ARM: timer: Handle level triggered IRQs correctly

In reply to Andre Przywara

The ARM Generic Timer uses a level-sensitive interrupt semantic. We easily catch when the line goes high, as this triggers the hardware IRQ. However we have to sync the state of the interrupt condition at certain points to catch when the line goes low and we can remove the vtimer vIRQ from the vGIC (and the LR).
The VGIC in Xen so far only implemented edge triggered vIRQs, really, so we need to add new functionality to re-sample the interrupt state.

---
 xen/arch/arm/time.c     | 34 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/traps.c    |  1 +
 xen/include/xen/timer.h |  2 ++
 3 files changed, 37 insertions(+)
xen/arch/arm/time.c +34 -0
index c11fcfeadd..98ebb4305d 100644
@@ -263,6 +263,40 @@ static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
     vgic_inject_irq(current->domain, current, current->arch.virt_timer.irq, true);
 }
 
+/**
+ * vtimer_sync() - update the state of the virtual timer after a guest run
+ * @vcpu: The VCPU to sync the arch timer state
+ *
+ * After returning from a guest, update the state of the virtual interrupt
+ * line, to model the level triggered interrupt correctly.
+ * If the guest has handled a timer interrupt, the virtual interrupt line
+ * needs to be lowered explicitly. vgic_inject_irq() takes care of that.
+ */
+void vtimer_sync(struct vcpu *vcpu)
+{
+    struct vtimer *vtimer = &vcpu->arch.virt_timer;
+    bool level;
+
+    vtimer->ctl = READ_SYSREG32(CNTV_CTL_EL0);
+    vtimer->cval = READ_SYSREG64(CNTV_CVAL_EL0);
+
+    /*
+     * Technically we should mask with 0x7 here, to catch if the timer
+     * interrupt is masked. However Xen always masks the timer upon entering
+     * the hypervisor, leaving it up to the guest to un-mask it.
+     * So we would always read a "low" level, despite the condition being
+     * actually "high". Igoring the mask bit solves this (for now).
+     * Another possible check would be to compare the value of CNTVCT_EL0
+     * against vtimer->cval and derive the interrupt state from that.
+     *
+     * TODO: The proper fix for this is to make vtimer vIRQ hardware mapped,
+     * but this requires reworking the arch timer to implement this.
+     */
+    level = (vtimer->ctl & 0x5) == (CNTx_CTL_ENABLE | CNTx_CTL_PENDING);
+
+    vgic_inject_irq(vcpu->domain, vcpu, vtimer->irq, level);
+}
+
 /*
  * Arch timer interrupt really ought to be level triggered, since the
  * design of the timer/comparator mechanism is based around that
xen/arch/arm/traps.c +1 -0
index 1cba7e584d..2d770a14a5 100644
@@ -2024,6 +2024,7 @@ static void enter_hypervisor_head(struct cpu_user_regs *regs)
         if ( current->arch.hcr_el2 & HCR_VA )
             current->arch.hcr_el2 = READ_SYSREG(HCR_EL2);
 
+        vtimer_sync(current);
         gic_clear_lrs(current);
     }
 }
xen/include/xen/timer.h +2 -0
index 4513260b0d..eddbbf3903 100644
@@ -94,6 +94,8 @@ DECLARE_PER_CPU(s_time_t, timer_deadline);
 /* Arch-defined function to reprogram timer hardware for new deadline. */
 int reprogram_timer(s_time_t timeout);
 
+void vtimer_sync(struct vcpu *vcpu);
+
 /* Calculate the aligned first tick time for a given periodic timer. */
 s_time_t align_timer(s_time_t firsttick, uint64_t period);
 
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 17/49] ARM: timer: Handle level triggered IRQs correctly

Hi Andre,

Quoted 7 lines · Andre Przywara

The ARM Generic Timer uses a level-sensitive interrupt semantic. We easily catch when the line goes high, as this triggers the hardware IRQ. However we have to sync the state of the interrupt condition at certain points to catch when the line goes low and we can remove the vtimer vIRQ from the vGIC (and the LR).
The VGIC in Xen so far only implemented edge triggered vIRQs, really, so we need to add new functionality to re-sample the interrupt state.

You might want to make a summary of the discussion we had with Marc Z. today here. This would help the other to understand why sample the interrupt state is necessary :).

Also do we need to do that for the emulated physical timer?

Quoted 13 lines
---
  xen/arch/arm/time.c     | 34 ++++++++++++++++++++++++++++++++++
  xen/arch/arm/traps.c    |  1 +
  xen/include/xen/timer.h |  2 ++
  3 files changed, 37 insertions(+)
xen/arch/arm/time.c +0 -0
index c11fcfeadd..98ebb4305d 100644

xen/arch/arm/time.c static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 266

Quoted 4 lines
xen/arch/arm/time.c +1 -0
@@ -263,6 +263,40 @@ static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
      vgic_inject_irq(current->domain, current, current->arch.virt_timer.irq, true);
  }
 
+/**

One * is enough.

Quoted 5 lines
xen/arch/arm/time.c +5 -0
+ * vtimer_sync() - update the state of the virtual timer after a guest run
+ * @vcpu: The VCPU to sync the arch timer state
+ *
+ * After returning from a guest, update the state of the virtual interrupt
+ * line, to model the level triggered interrupt correctly.

xen/arch/arm/time.c static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 281

Quoted 10 lines
xen/arch/arm/time.c +10 -0
+ * If the guest has handled a timer interrupt, the virtual interrupt line
+ * needs to be lowered explicitly. vgic_inject_irq() takes care of that.
+ */
+void vtimer_sync(struct vcpu *vcpu)
+{
+    struct vtimer *vtimer = &vcpu->arch.virt_timer;
+    bool level;
+
+    vtimer->ctl = READ_SYSREG32(CNTV_CTL_EL0);
+    vtimer->cval = READ_SYSREG64(CNTV_CVAL_EL0);

Why do you need to save cval?

xen/arch/arm/time.c static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 288

Quoted 7 lines
xen/arch/arm/time.c +7 -0
+
+    /*
+     * Technically we should mask with 0x7 here, to catch if the timer
+     * interrupt is masked. However Xen always masks the timer upon entering
+     * the hypervisor, leaving it up to the guest to un-mask it.
+     * So we would always read a "low" level, despite the condition being
+     * actually "high". Igoring the mask bit solves this (for now).

s/Igoring/Ignoring/

xen/arch/arm/time.c static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 293

Quoted 5 lines
xen/arch/arm/time.c +5 -0
+     * Another possible check would be to compare the value of CNTVCT_EL0
+     * against vtimer->cval and derive the interrupt state from that.
+     *
+     * TODO: The proper fix for this is to make vtimer vIRQ hardware mapped,
+     * but this requires reworking the arch timer to implement this.

That something we should look at it once the vGIC is done :).

xen/arch/arm/time.c static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 295

Quoted 2 lines
xen/arch/arm/time.c +2 -0
+     */
+    level = (vtimer->ctl & 0x5) == (CNTx_CTL_ENABLE | CNTx_CTL_PENDING);

Can you please use the proper define rather than plain value?

Quoted 12 lines
xen/arch/arm/time.c +4 -0
+
+    vgic_inject_irq(vcpu->domain, vcpu, vtimer->irq, level);
+}
+
  /*
   * Arch timer interrupt really ought to be level triggered, since the
   * design of the timer/comparator mechanism is based around that
xen/arch/arm/traps.c +0 -0
index 1cba7e584d..2d770a14a5 100644

xen/arch/arm/traps.c static void enter_hypervisor_head(struct cpu_user_regs *regs) · line 2026

Quoted 3 lines
xen/arch/arm/traps.c +0 -0
@@ -2024,6 +2024,7 @@ static void enter_hypervisor_head(struct cpu_user_regs *regs)
          if ( current->arch.hcr_el2 & HCR_VA )
              current->arch.hcr_el2 = READ_SYSREG(HCR_EL2);
 

You need to sample the virtual timer before clearing the LRs, right? If so, you likely want to add a comment here to avoid reshuffling the code.

xen/arch/arm/traps.c static void enter_hypervisor_head(struct cpu_user_regs *regs) · line 2027

Quoted 1 line
xen/arch/arm/traps.c +1 -0
+        vtimer_sync(current);

I am a bit worry about re-sampling the virtual interrupt state at every traps. It might be worth thinking to do the re-sample when syncing the LRs (as you do for HW level interrupt in patch #25). Probably once we get the new vGIC merged.

Quoted 9 lines
xen/arch/arm/traps.c +0 -0
          gic_clear_lrs(current);
      }
  }
xen/include/xen/timer.h +0 -0
index 4513260b0d..eddbbf3903 100644

xen/include/xen/timer.h DECLARE_PER_CPU(s_time_t, timer_deadline); · line 101

Quoted 8 lines
xen/include/xen/timer.h +2 -0
@@ -94,6 +94,8 @@ DECLARE_PER_CPU(s_time_t, timer_deadline);
  /* Arch-defined function to reprogram timer hardware for new deadline. */
  int reprogram_timer(s_time_t timeout);
 
+void vtimer_sync(struct vcpu *vcpu);
+
  /* Calculate the aligned first tick time for a given periodic timer. */
  s_time_t align_timer(s_time_t firsttick, uint64_t period);
 

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 17/49] ARM: timer: Handle level triggered IRQs correctly

Hi,

Quoted 11 lines · Julien Grall

Hi Andre,

Quoted 7 lines · Andre Przywara

The ARM Generic Timer uses a level-sensitive interrupt semantic. We easily catch when the line goes high, as this triggers the hardware IRQ. However we have to sync the state of the interrupt condition at certain points to catch when the line goes low and we can remove the vtimer vIRQ from the vGIC (and the LR).
The VGIC in Xen so far only implemented edge triggered vIRQs, really, so we need to add new functionality to re-sample the interrupt state.

Quoted 3 lines

You might want to make a summary of the discussion we had with Marc Z. today here. This would help the other to understand why sample the interrupt state is necessary :).

Yes, I just saw that I somehow missed copying the elaborate comment from Christoffer. Fixed now, indeed without this background it's next to impossible to understand this ;-)

Quoted 1 line

Also do we need to do that for the emulated physical timer?

Mmh, good question. I believe this whole timer story needs a good think again.

Quoted 8 lines
Quoted 8 lines

---
  xen/arch/arm/time.c     | 34 ++++++++++++++++++++++++++++++++++ xen/arch/arm/traps.c    |  1 + xen/include/xen/timer.h |  2 ++

  3 files changed, 37 insertions(+)
Quoted 12 lines
Quoted 10 lines
xen/arch/arm/time.c +0 -0
index c11fcfeadd..98ebb4305d 100644
@@ -263,6 +263,40 @@ static void vtimer_interrupt(int irq, void
 *dev_id, struct cpu_user_regs *regs)
       vgic_inject_irq(current->domain, current,
 current->arch.virt_timer.irq, true);
   }

  +/**

One * is enough.

That's kernel-doc comment style:
https://www.kernel.org/doc/html/v4.9/kernel-documentation.html#writing-kernel-doc-comments

That allows tools to scan the tree and extract function documentation in an automated way. A bit like markdown: still perfectly readable by humans, but parse-able by scripts as well.

I was hoping that it wouldn't hurt to have this in Xen as well, as I copied this already in other parts of this code.

Quoted 12 lines
Quoted 12 lines

+ * vtimer_sync() - update the state of the virtual timer after a guest run
+ * @vcpu: The VCPU to sync the arch timer state
+ *
+ * After returning from a guest, update the state of the virtual interrupt
+ * line, to model the level triggered interrupt correctly.
+ * If the guest has handled a timer interrupt, the virtual interrupt line
+ * needs to be lowered explicitly. vgic_inject_irq() takes care of that.
+ */
+void vtimer_sync(struct vcpu *vcpu)

Quoted 8 lines

xen/arch/arm/time.c static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 281

Quoted 6 lines
xen/arch/arm/time.c +6 -0
+{
+    struct vtimer *vtimer = &vcpu->arch.virt_timer;
+    bool level;
+
+    vtimer->ctl = READ_SYSREG32(CNTV_CTL_EL0);
+    vtimer->cval = READ_SYSREG64(CNTV_CVAL_EL0);

Why do you need to save cval?

Originally I was copying KVM code which checked the actual IRQ condition (by comparing the counter with CVAL). So this might be a leftover from there. Need to check whether we actually need an up-to-date value of this.

Quoted 20 lines
Quoted 9 lines

+
+    /*
+     * Technically we should mask with 0x7 here, to catch if the timer +     * interrupt is masked. However Xen always masks the timer upon entering
+     * the hypervisor, leaving it up to the guest to un-mask it. +     * So we would always read a "low" level, despite the condition being
+     * actually "high". Igoring the mask bit solves this (for now).

s/Igoring/Ignoring/

Quoted 7 lines

+     * Another possible check would be to compare the value of CNTVCT_EL0
+     * against vtimer->cval and derive the interrupt state from that. +     *
+     * TODO: The proper fix for this is to make vtimer vIRQ hardware mapped,
+     * but this requires reworking the arch timer to implement this.

Quoted 1 line

That something we should look at it once the vGIC is done :).

Indeed, looking forward to it (well, somewhat ... ) ;-)

Quoted 4 lines
Quoted 2 lines

+     */
+    level = (vtimer->ctl & 0x5) == (CNTx_CTL_ENABLE | CNTx_CTL_PENDING);

Can you please use the proper define rather than plain value?

Ah, right, that was a leftover from experimentation. Also a test to see if reviewers are really reading this ;-)

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/time.c +4 -0
+
+    vgic_inject_irq(vcpu->domain, vcpu, vtimer->irq, level);
+}
+

  /*
   * Arch timer interrupt really ought to be level triggered, since the
   * design of the timer/comparator mechanism is based around that

xen/arch/arm/traps.c +0 -0
index 1cba7e584d..2d770a14a5 100644
Quoted 8 lines
Quoted 5 lines
xen/arch/arm/traps.c +0 -0
@@ -2024,6 +2024,7 @@ static void enter_hypervisor_head(struct
 cpu_user_regs *regs)
           if ( current->arch.hcr_el2 & HCR_VA )
               current->arch.hcr_el2 = READ_SYSREG(HCR_EL2);

You need to sample the virtual timer before clearing the LRs, right? If so, you likely want to add a comment here to avoid reshuffling the code.

Yes, good point.

Quoted 6 lines
Quoted 1 line

+        vtimer_sync(current);

I am a bit worry about re-sampling the virtual interrupt state at every traps. It might be worth thinking to do the re-sample when syncing the LRs (as you do for HW level interrupt in patch #25). Probably once we get the new vGIC merged.

I share your concerns, but didn't dare to optimise this yet. But indeed it is something worth to think about.

Cheers,
Andre.

Quoted 10 lines
Quoted 10 lines

          gic_clear_lrs(current);
      }
  }

xen/include/xen/timer.h +0 -0
index 4513260b0d..eddbbf3903 100644
@@ -94,6 +94,8 @@ DECLARE_PER_CPU(s_time_t, timer_deadline);
   /* Arch-defined function to reprogram timer hardware for new

deadline. */

Quoted 9 lines
Quoted 6 lines

  int reprogram_timer(s_time_t timeout);
  +void vtimer_sync(struct vcpu *vcpu);
+
  /* Calculate the aligned first tick time for a given periodic timer. */ s_time_t align_timer(s_time_t firsttick, uint64_t period);

Cheers,

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

#41 lore raw

Re: [RFC PATCH 17/49] ARM: timer: Handle level triggered IRQs correctly

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 49 lines · Julien Grall
Quoted 11 lines

Hi Andre,

Quoted 7 lines · Andre Przywara

The ARM Generic Timer uses a level-sensitive interrupt semantic. We easily catch when the line goes high, as this triggers the hardware IRQ. However we have to sync the state of the interrupt condition at certain points to catch when the line goes low and we can remove the vtimer vIRQ from the vGIC (and the LR).
The VGIC in Xen so far only implemented edge triggered vIRQs, really, so we need to add new functionality to re-sample the interrupt state.

Quoted 3 lines

You might want to make a summary of the discussion we had with Marc Z. today here. This would help the other to understand why sample the interrupt state is necessary :).

Yes, I just saw that I somehow missed copying the elaborate comment from Christoffer. Fixed now, indeed without this background it's next to impossible to understand this ;-)

Quoted 1 line

Also do we need to do that for the emulated physical timer?

Mmh, good question. I believe this whole timer story needs a good think again.

Quoted 8 lines
Quoted 8 lines
---
   xen/arch/arm/time.c     | 34 ++++++++++++++++++++++++++++++++++
   xen/arch/arm/traps.c    |  1 +
   xen/include/xen/timer.h |  2 ++
   3 files changed, 37 insertions(+)
Quoted 12 lines

xen/arch/arm/time.c static void vtimer_interrupt(int irq, void · line 267

Quoted 10 lines
xen/arch/arm/time.c +0 -0
index c11fcfeadd..98ebb4305d 100644
@@ -263,6 +263,40 @@ static void vtimer_interrupt(int irq, void
 *dev_id, struct cpu_user_regs *regs)
       vgic_inject_irq(current->domain, current,
 current->arch.virt_timer.irq, true);
   }
   +/**

One * is enough.

That's kernel-doc comment style:
https://www.kernel.org/doc/html/v4.9/kernel-documentation.html#writing-kernel-doc-comments

Quoted 6 lines

That allows tools to scan the tree and extract function documentation in an automated way. A bit like markdown: still perfectly readable by humans, but parse-able by scripts as well.

I was hoping that it wouldn't hurt to have this in Xen as well, as I copied this already in other parts of this code.

I was blindly following the CODING_STYLE requirements :). But let's keep /** if it helps script.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 18/49 ARM: evtchn: Handle level triggered IRQs correctly

In reply to Andre Przywara

The event channel IRQ has level triggered semantics, however the current VGIC treats everything as edge triggered.
To correctly process those IRQs, we have to lower the (virtual) IRQ line at some point in time, depending on whether ther interrupt condition still prevails.
Check the per-VCPU evtchn_upcall_pending variable to make the interrupt line match its status, and call this function upon every hypervisor entry.

---
 xen/arch/arm/domain.c       | 7 +++++++
 xen/arch/arm/traps.c        | 1 +
 xen/include/asm-arm/event.h | 1 +
 3 files changed, 9 insertions(+)
xen/arch/arm/domain.c +7 -0
index e76cfdfe83..87bd493924 100644
@@ -955,6 +955,13 @@ void vcpu_mark_events_pending(struct vcpu *v)
     vgic_inject_irq(v->domain, v, v->domain->arch.evtchn_irq, true);
 }
 
+void vcpu_update_evtchn_irq(struct vcpu *v)
+{
+    bool pending = vcpu_info(v, evtchn_upcall_pending);
+
+    vgic_inject_irq(v->domain, v, v->domain->arch.evtchn_irq, pending);
+}
+
 /* The ARM spec declares that even if local irqs are masked in
  * the CPSR register, an irq should wake up a cpu from WFI anyway.
  * For this reason we need to check for irqs that need delivery,
xen/arch/arm/traps.c +1 -0
index 2d770a14a5..f57ef2141a 100644
@@ -2025,6 +2025,7 @@ static void enter_hypervisor_head(struct cpu_user_regs *regs)
             current->arch.hcr_el2 = READ_SYSREG(HCR_EL2);
 
         vtimer_sync(current);
+        vcpu_update_evtchn_irq(current);
         gic_clear_lrs(current);
     }
 }
xen/include/asm-arm/event.h +1 -0
index e8c2a6cb44..87ef76e3d5 100644
@@ -6,6 +6,7 @@
 
 void vcpu_kick(struct vcpu *v);
 void vcpu_mark_events_pending(struct vcpu *v);
+void vcpu_update_evtchn_irq(struct vcpu *v);
 void vcpu_block_unless_event_pending(struct vcpu *v);
 
 static inline int vcpu_event_delivery_is_enabled(struct vcpu *v)
Signature · 7 lines
2.14.1


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

RFC PATCH 19/49 ARM: vPL011: Use the VGIC's level triggered IRQs handling if available

In reply to Andre Przywara

The emulated ARM SBSA UART is using level triggered IRQ semantics, however the current VGIC can only handle edge triggered IRQs, really. Disable the existing workaround for this problem in case we have the new VGIC in place, which can properly handle level triggered IRQs.

---
 xen/arch/arm/vpl011.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/arm/vpl011.c +4 -0
index 5dcf4bec18..197ece8873 100644
@@ -54,6 +54,7 @@ static void vpl011_update_interrupt_status(struct domain *d)
      */
     ASSERT(spin_is_locked(&vpl011->lock));
 
+#ifndef CONFIG_NEW_VGIC
     /*
      * TODO: PL011 interrupts are level triggered which means
      * that interrupt needs to be set/clear instead of being
@@ -71,6 +72,9 @@ static void vpl011_update_interrupt_status(struct domain *d)
         vgic_inject_irq(d, NULL, GUEST_VPL011_SPI, true);
 
     vpl011->shadow_uartmis = uartmis;
+#else
+    vgic_inject_irq(d, NULL, GUEST_VPL011_SPI, !!uartmis);
+#endif
 }
 
 static uint8_t vpl011_read_data(struct domain *d)
Signature · 7 lines
2.14.1


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

RFC PATCH 20/49 ARM: new VGIC: Add data structure definitions

In reply to Andre Przywara

Add a new header file for the new and improved GIC implementation. The big change is that we now have a struct vgic_irq per IRQ instead of spreading all the information over various bitmaps in the ranks.

We include this new header conditionally from within the old header file for the time being to avoid touching all the users.

This is based on Linux commit b18b57787f5e, written by Christoffer Dall.

---
 xen/include/asm-arm/arm_vgic.h | 269 +++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/domain.h   |   4 +
 xen/include/asm-arm/vgic.h     |   6 +
 3 files changed, 279 insertions(+)
 create mode 100644 xen/include/asm-arm/arm_vgic.h
xen/include/asm-arm/arm_vgic.h new file +269 -0
new file mode 100644
index 0000000000..865e9ee5bc
@@ -0,0 +1,269 @@
+/*
+ * Copyright (C) 2015, 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 version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __KVM_ARM_VGIC_H
+#define __KVM_ARM_VGIC_H
+
+#include <asm/atomic.h>
+#include <asm/mmio.h>
+#include <xen/spinlock.h>
+#include <xen/list.h>
+
+#define VGIC_V3_MAX_CPUS        255
+#define VGIC_V2_MAX_CPUS        8
+#define VGIC_NR_IRQS_LEGACY     256
+#define VGIC_NR_SGIS            16
+#define VGIC_NR_PPIS            16
+#define VGIC_NR_PRIVATE_IRQS    (VGIC_NR_SGIS + VGIC_NR_PPIS)
+#define VGIC_MAX_PRIVATE        (VGIC_NR_PRIVATE_IRQS - 1)
+#define VGIC_MAX_SPI            1019
+#define VGIC_MAX_RESERVED       1023
+#define VGIC_MIN_LPI            8192
+
+#define irq_is_ppi(irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS)
+#define irq_is_spi(irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \
+                         (irq) <= VGIC_MAX_SPI)
+
+enum vgic_type {
+    VGIC_V2,        /* Good ol' GICv2 */
+    VGIC_V3,        /* New fancy GICv3 */
+};
+
+#define VGIC_V2_MAX_LRS         (1 << 6)
+#define VGIC_V3_MAX_LRS         16
+#define VGIC_V3_LR_INDEX(lr)    (VGIC_V3_MAX_LRS - 1 - lr)
+
+enum vgic_irq_config {
+    VGIC_CONFIG_EDGE = 0,
+    VGIC_CONFIG_LEVEL
+};
+
+struct vgic_irq {
+    spinlock_t irq_lock;        /* Protects the content of the struct */
+    struct list_head lpi_list;  /* Used to link all LPIs together */
+    struct list_head ap_list;
+
+    struct vcpu *vcpu;          /*
+                                 * SGIs and PPIs: The VCPU
+                                 * SPIs and LPIs: The VCPU whose ap_list
+                                 * this is queued on.
+                                 */
+
+    struct vcpu *target_vcpu;   /*
+                                 * The VCPU that this interrupt should
+                                 * be sent to, as a result of the
+                                 * targets reg (v2) or the affinity reg (v3).
+                                 */
+
+    u32 intid;                  /* Guest visible INTID */
+    bool line_level;            /* Level only */
+    bool pending_latch;         /*
+                                 * The pending latch state used to
+                                 * calculate the pending state for both
+                                 * level and edge triggered IRQs.
+                                 */
+    bool active;                /* not used for LPIs */
+    bool enabled;
+    bool hw;                    /* Tied to HW IRQ */
+    atomic_t refcount;          /* Used for LPIs */
+    u32 hwintid;                /* HW INTID number */
+    union
+    {
+        u8 targets;             /* GICv2 target VCPUs mask */
+        u32 mpidr;              /* GICv3 target VCPU */
+    };
+    u8 source;                  /* GICv2 SGIs only */
+    u8 priority;
+    enum vgic_irq_config config;    /* Level or edge */
+};
+
+struct vgic_register_region;
+struct vgic_its;
+
+enum iodev_type {
+    IODEV_CPUIF,
+    IODEV_DIST,
+    IODEV_REDIST,
+    IODEV_ITS
+};
+
+struct vgic_io_device {
+    paddr_t base_addr;
+    union
+    {
+        struct vcpu *redist_vcpu;
+        struct vgic_its *its;
+    };
+    const struct vgic_register_region *regions;
+    enum iodev_type iodev_type;
+    int nr_regions;
+};
+
+struct vgic_its {
+    /* The base address of the ITS control register frame */
+    paddr_t     vgic_its_base;
+
+    bool        enabled;
+    struct vgic_io_device       iodev;
+
+    /* These registers correspond to GITS_BASER{0,1} */
+    u64         baser_device_table;
+    u64         baser_coll_table;
+
+    /* Protects the command queue */
+    spinlock_t  cmd_lock;
+    u64         cbaser;
+    u32         creadr;
+    u32         cwriter;
+
+    /* migration ABI revision in use */
+    u32         abi_rev;
+
+    /* Protects the device and collection lists */
+    spinlock_t  its_lock;
+    struct list_head            device_list;
+    struct list_head            collection_list;
+};
+
+struct vgic_dist {
+    bool                ready;
+    bool                initialized;
+
+    /* vGIC model the kernel emulates for the guest (GICv2 or GICv3) */
+    u32                 version;
+
+    /* Do injected MSIs require an additional device ID? */
+    bool                msis_require_devid;
+
+    int                 nr_spis;
+
+    /* base addresses in guest physical address space: */
+    paddr_t             vgic_dist_base;     /* distributor */
+    union
+    {
+        /* either a GICv2 CPU interface */
+        paddr_t         vgic_cpu_base;
+        /* or a number of GICv3 redistributor regions */
+        struct
+        {
+            paddr_t     vgic_redist_base;
+            paddr_t     vgic_redist_free_offset;
+        };
+    };
+
+    /* distributor enabled */
+    bool                enabled;
+
+    struct vgic_irq     *spis;
+    unsigned long       *allocated_irqs; /* bitmap of IRQs allocated */
+
+    struct vgic_io_device   dist_iodev;
+
+    bool                has_its;
+
+    /*
+     * Contains the attributes and gpa of the LPI configuration table.
+     * Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share
+     * one address across all redistributors.
+     * GICv3 spec: 6.1.2 "LPI Configuration tables"
+     */
+    u64                 propbaser;
+
+    /* Protects the lpi_list and the count value below. */
+    spinlock_t          lpi_list_lock;
+    struct list_head    lpi_list_head;
+    int                 lpi_list_count;
+};
+
+struct vgic_v2_cpu_if {
+    u32     vgic_hcr;
+    u32     vgic_vmcr;
+    u64     vgic_elrsr; /* Saved only */
+    u32     vgic_apr;
+    u32     vgic_lr[VGIC_V2_MAX_LRS];
+};
+
+struct vgic_v3_cpu_if {
+    u32     vgic_hcr;
+    u32     vgic_vmcr;
+    u32     vgic_sre;   /* Restored only, change ignored */
+    u32     vgic_elrsr; /* Saved only */
+    u32     vgic_ap0r[4];
+    u32     vgic_ap1r[4];
+    u64     vgic_lr[VGIC_V3_MAX_LRS];
+};
+
+struct vgic_cpu {
+    /* CPU vif control registers for world switch */
+    union
+    {
+        struct vgic_v2_cpu_if   vgic_v2;
+        struct vgic_v3_cpu_if   vgic_v3;
+    };
+
+    unsigned int used_lrs;
+    struct vgic_irq private_irqs[VGIC_NR_PRIVATE_IRQS];
+
+    spinlock_t ap_list_lock;    /* Protects the ap_list */
+
+    /*
+     * List of IRQs that this VCPU should consider because they are either
+     * Active or Pending (hence the name; AP list), or because they recently
+     * were one of the two and need to be migrated off this list to another
+     * VCPU.
+     */
+    struct list_head ap_list_head;
+
+    /*
+     * Members below are used with GICv3 emulation only and represent
+     * parts of the redistributor.
+     */
+    struct vgic_io_device   rd_iodev;
+    struct vgic_io_device   sgi_iodev;
+
+    /* Contains the attributes and gpa of the LPI pending tables. */
+    u64 pendbaser;
+
+    bool lpis_enabled;
+
+    /* Cache guest priority bits */
+    u32 num_pri_bits;
+
+    /* Cache guest interrupt ID bits */
+    u32 num_id_bits;
+};
+
+extern struct static_key_false vgic_v2_cpuif_trap;
+extern struct static_key_false vgic_v3_cpuif_trap;
+
+#define vgic_initialized(k) ((k)->arch.vgic.initialized)
+#define vgic_ready(k)       ((k)->arch.vgic.ready)
+#define vgic_valid_spi(k, i)    (((i) >= VGIC_NR_PRIVATE_IRQS) && \
+            ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS))
+
+bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
+bool vgic_v3_emulate(struct cpu_user_regs *regs, union hsr hsr);
+
+void vgic_clear_pending_irqs(struct vcpu *v);
+
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+                      paddr_t vbase, void __iomem *hbase,
+                      uint32_t aliased_offset);
+struct rdist_region;
+void vgic_v3_setup_hw(paddr_t dbase, unsigned int nr_rdist_regions,
+                      const struct rdist_region *regions,
+                      unsigned int intid_bits);
+
+#endif /* __KVM_ARM_VGIC_H */
xen/include/asm-arm/domain.h +4 -0
index 968ffb0c81..e8f2917140 100644
@@ -198,7 +198,11 @@ struct arch_vcpu
     union gic_state_data gic;
     uint64_t lr_mask;
 
+#ifdef CONFIG_NEW_VGIC
+    struct vgic_cpu vgic_cpu;
+#else
     struct vgic_cpu vgic;
+#endif
 
     /* Timer registers  */
     uint32_t cntkctl;
xen/include/asm-arm/vgic.h +6 -0
index 85ad2aca79..96b99f5c85 100644
@@ -18,6 +18,10 @@
 #ifndef __ASM_ARM_VGIC_H__
 #define __ASM_ARM_VGIC_H__
 
+#ifdef CONFIG_NEW_VGIC
+#include <asm/arm_vgic.h>
+#else
+
 #include <xen/bitops.h>
 #include <xen/radix-tree.h>
 #include <xen/rbtree.h>
@@ -313,6 +317,8 @@ void vgic_v3_setup_hw(paddr_t dbase,
                       unsigned int intid_bits);
 #endif
 
+#endif /* !CONFIG_NEW_VGIC */
+
 /*** Common VGIC functions used by Xen arch code ****/
 
 /*
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 20/49] ARM: new VGIC: Add data structure definitions

Hi Andre,

xen/include/asm-arm/arm_vgic.h

Quoted 22 lines · Andre Przywara

Add a new header file for the new and improved GIC implementation. The big change is that we now have a struct vgic_irq per IRQ instead of spreading all the information over various bitmaps in the ranks.

We include this new header conditionally from within the old header file for the time being to avoid touching all the users.

This is based on Linux commit b18b57787f5e, written by Christoffer Dall.

---
  xen/include/asm-arm/arm_vgic.h | 269 +++++++++++++++++++++++++++++++++++++++++
  xen/include/asm-arm/domain.h   |   4 +
  xen/include/asm-arm/vgic.h     |   6 +
  3 files changed, 279 insertions(+)

create mode 100644 xen/include/asm-arm/arm_vgic.h

xen/include/asm-arm/arm_vgic.h new file +0 -0
new file mode 100644
index 0000000000..865e9ee5bc

arm_vgic.h is a confusing name. Can we name it vgic-new.h?

Quoted 8 lines
xen/include/asm-arm/arm_vgic.h new file +7 -0
@@ -0,0 +1,269 @@
+/*
+ * Copyright (C) 2015, 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 version 2 as
+ * published by the Free Software Foundation.
+ *

xen/include/asm-arm/arm_vgic.h line 17

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __KVM_ARM_VGIC_H
+#define __KVM_ARM_VGIC_H

Please modify the guards based on the file name.

Quoted 22 lines
xen/include/asm-arm/arm_vgic.h new file +22 -0
+
+#include <asm/atomic.h>
+#include <asm/mmio.h>
+#include <xen/spinlock.h>
+#include <xen/list.h>
+
+#define VGIC_V3_MAX_CPUS        255
+#define VGIC_V2_MAX_CPUS        8
+#define VGIC_NR_IRQS_LEGACY     256
+#define VGIC_NR_SGIS            16
+#define VGIC_NR_PPIS            16
+#define VGIC_NR_PRIVATE_IRQS    (VGIC_NR_SGIS + VGIC_NR_PPIS)
+#define VGIC_MAX_PRIVATE        (VGIC_NR_PRIVATE_IRQS - 1)
+#define VGIC_MAX_SPI            1019
+#define VGIC_MAX_RESERVED       1023
+#define VGIC_MIN_LPI            8192
+
+#define irq_is_ppi(irq) ((irq) >= VGIC_NR_SGIS && (irq) < VGIC_NR_PRIVATE_IRQS)
+#define irq_is_spi(irq) ((irq) >= VGIC_NR_PRIVATE_IRQS && \
+                         (irq) <= VGIC_MAX_SPI)
+
+enum vgic_type {

xen/include/asm-arm/arm_vgic.h line 49

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+    VGIC_V2,        /* Good ol' GICv2 */
+    VGIC_V3,        /* New fancy GICv3 */
+};
+
+#define VGIC_V2_MAX_LRS         (1 << 6)
+#define VGIC_V3_MAX_LRS         16
+#define VGIC_V3_LR_INDEX(lr)    (VGIC_V3_MAX_LRS - 1 - lr)
+
+enum vgic_irq_config {
+    VGIC_CONFIG_EDGE = 0,

I don't think it is necessary to set 0 here as IIRC an enum always start at 0 if not specified.

Quoted 11 lines
xen/include/asm-arm/arm_vgic.h new file +11 -0
+    VGIC_CONFIG_LEVEL
+};
+
+struct vgic_irq {
+    spinlock_t irq_lock;        /* Protects the content of the struct */
+    struct list_head lpi_list;  /* Used to link all LPIs together */
+    struct list_head ap_list;
+
+    struct vcpu *vcpu;          /*
+                                 * SGIs and PPIs: The VCPU
+                                 * SPIs and LPIs: The VCPU whose ap_list

xen/include/asm-arm/arm_vgic.h line 70

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+                                 * this is queued on.
+                                 */
+
+    struct vcpu *target_vcpu;   /*
+                                 * The VCPU that this interrupt should
+                                 * be sent to, as a result of the
+                                 * targets reg (v2) or the affinity reg (v3).
+                                 */
+
+    u32 intid;                  /* Guest visible INTID */

As we are using Xen coding style, please replace all u* with uint_*.

Quoted 12 lines
xen/include/asm-arm/arm_vgic.h new file +12 -0
+    bool line_level;            /* Level only */
+    bool pending_latch;         /*
+                                 * The pending latch state used to
+                                 * calculate the pending state for both
+                                 * level and edge triggered IRQs.
+                                 */
+    bool active;                /* not used for LPIs */
+    bool enabled;
+    bool hw;                    /* Tied to HW IRQ */
+    atomic_t refcount;          /* Used for LPIs */
+    u32 hwintid;                /* HW INTID number */
+    union

xen/include/asm-arm/arm_vgic.h line 92

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+    {
+        u8 targets;             /* GICv2 target VCPUs mask */
+        u32 mpidr;              /* GICv3 target VCPU */
+    };
+    u8 source;                  /* GICv2 SGIs only */
+    u8 priority;
+    enum vgic_irq_config config;    /* Level or edge */
+};
+
+struct vgic_register_region;

Do we really need the forward declaration here?

xen/include/asm-arm/arm_vgic.h line 96

Quoted 4 lines
xen/include/asm-arm/arm_vgic.h new file +4 -0
+struct vgic_its;
+
+enum iodev_type {
+    IODEV_CPUIF,

I don't think this one is necessary.

Quoted 5 lines
xen/include/asm-arm/arm_vgic.h new file +5 -0
+    IODEV_DIST,
+    IODEV_REDIST,
+    IODEV_ITS
+};
+

xen/include/asm-arm/arm_vgic.h line 111

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+struct vgic_io_device {
+    paddr_t base_addr;
+    union
+    {
+        struct vcpu *redist_vcpu;
+        struct vgic_its *its;
+    };
+    const struct vgic_register_region *regions;
+    enum iodev_type iodev_type;
+    int nr_regions;

unsigned int please.

Quoted 29 lines
xen/include/asm-arm/arm_vgic.h new file +29 -0
+};
+
+struct vgic_its {
+    /* The base address of the ITS control register frame */
+    paddr_t     vgic_its_base;
+
+    bool        enabled;
+    struct vgic_io_device       iodev;
+
+    /* These registers correspond to GITS_BASER{0,1} */
+    u64         baser_device_table;
+    u64         baser_coll_table;
+
+    /* Protects the command queue */
+    spinlock_t  cmd_lock;
+    u64         cbaser;
+    u32         creadr;
+    u32         cwriter;
+
+    /* migration ABI revision in use */
+    u32         abi_rev;
+
+    /* Protects the device and collection lists */
+    spinlock_t  its_lock;
+    struct list_head            device_list;
+    struct list_head            collection_list;
+};
+
+struct vgic_dist {

xen/include/asm-arm/arm_vgic.h line 150

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+    bool                ready;
+    bool                initialized;
+
+    /* vGIC model the kernel emulates for the guest (GICv2 or GICv3) */
+    u32                 version;
+
+    /* Do injected MSIs require an additional device ID? */
+    bool                msis_require_devid;
+
+    int                 nr_spis;

Ditto.

Quoted 27 lines
xen/include/asm-arm/arm_vgic.h new file +27 -0
+
+    /* base addresses in guest physical address space: */
+    paddr_t             vgic_dist_base;     /* distributor */
+    union
+    {
+        /* either a GICv2 CPU interface */
+        paddr_t         vgic_cpu_base;
+        /* or a number of GICv3 redistributor regions */
+        struct
+        {
+            paddr_t     vgic_redist_base;
+            paddr_t     vgic_redist_free_offset;
+        };
+    };
+
+    /* distributor enabled */
+    bool                enabled;
+
+    struct vgic_irq     *spis;
+    unsigned long       *allocated_irqs; /* bitmap of IRQs allocated */
+
+    struct vgic_io_device   dist_iodev;
+
+    bool                has_its;
+
+    /*
+     * Contains the attributes and gpa of the LPI configuration table.

xen/include/asm-arm/arm_vgic.h line 187

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+     * Since we report GICR_TYPER.CommonLPIAff as 0b00, we can share
+     * one address across all redistributors.
+     * GICv3 spec: 6.1.2 "LPI Configuration tables"
+     */
+    u64                 propbaser;
+
+    /* Protects the lpi_list and the count value below. */
+    spinlock_t          lpi_list_lock;
+    struct list_head    lpi_list_head;
+    int                 lpi_list_count;

Ditto.

Quoted 52 lines
xen/include/asm-arm/arm_vgic.h new file +52 -0
+};
+
+struct vgic_v2_cpu_if {
+    u32     vgic_hcr;
+    u32     vgic_vmcr;
+    u64     vgic_elrsr; /* Saved only */
+    u32     vgic_apr;
+    u32     vgic_lr[VGIC_V2_MAX_LRS];
+};
+
+struct vgic_v3_cpu_if {
+    u32     vgic_hcr;
+    u32     vgic_vmcr;
+    u32     vgic_sre;   /* Restored only, change ignored */
+    u32     vgic_elrsr; /* Saved only */
+    u32     vgic_ap0r[4];
+    u32     vgic_ap1r[4];
+    u64     vgic_lr[VGIC_V3_MAX_LRS];
+};
+
+struct vgic_cpu {
+    /* CPU vif control registers for world switch */
+    union
+    {
+        struct vgic_v2_cpu_if   vgic_v2;
+        struct vgic_v3_cpu_if   vgic_v3;
+    };
+
+    unsigned int used_lrs;
+    struct vgic_irq private_irqs[VGIC_NR_PRIVATE_IRQS];
+
+    spinlock_t ap_list_lock;    /* Protects the ap_list */
+
+    /*
+     * List of IRQs that this VCPU should consider because they are either
+     * Active or Pending (hence the name; AP list), or because they recently
+     * were one of the two and need to be migrated off this list to another
+     * VCPU.
+     */
+    struct list_head ap_list_head;
+
+    /*
+     * Members below are used with GICv3 emulation only and represent
+     * parts of the redistributor.
+     */
+    struct vgic_io_device   rd_iodev;
+    struct vgic_io_device   sgi_iodev;
+
+    /* Contains the attributes and gpa of the LPI pending tables. */
+    u64 pendbaser;
+
+    bool lpis_enabled;

xen/include/asm-arm/arm_vgic.h line 249

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+
+    /* Cache guest priority bits */
+    u32 num_pri_bits;
+
+    /* Cache guest interrupt ID bits */
+    u32 num_id_bits;
+};
+
+extern struct static_key_false vgic_v2_cpuif_trap;
+extern struct static_key_false vgic_v3_cpuif_trap;

static_key_false does not exist on Xen.

xen/include/asm-arm/arm_vgic.h line 257

Quoted 8 lines
xen/include/asm-arm/arm_vgic.h new file +8 -0
+
+#define vgic_initialized(k) ((k)->arch.vgic.initialized)
+#define vgic_ready(k)       ((k)->arch.vgic.ready)
+#define vgic_valid_spi(k, i)    (((i) >= VGIC_NR_PRIVATE_IRQS) && \
+            ((i) < (k)->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS))
+
+bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
+bool vgic_v3_emulate(struct cpu_user_regs *regs, union hsr hsr);

While I am ok with the structure been defined up front, I would rather prefer to see the prototype defined with the actual implementation. This is much easier to see if you miss a prototype. And technically, this patch only add "data structure definitions".

xen/include/asm-arm/arm_vgic.h line 267

Quoted 10 lines
xen/include/asm-arm/arm_vgic.h new file +10 -0
+
+void vgic_clear_pending_irqs(struct vcpu *v);
+
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+                      paddr_t vbase, void __iomem *hbase,
+                      uint32_t aliased_offset);
+struct rdist_region;
+void vgic_v3_setup_hw(paddr_t dbase, unsigned int nr_rdist_regions,
+                      const struct rdist_region *regions,
+                      unsigned int intid_bits);

Please don't re-define prototypes existing between the two implementation.

xen/include/asm-arm/arm_vgic.h line 269

Quoted 2 lines
xen/include/asm-arm/arm_vgic.h new file +2 -0
+
+#endif /* __KVM_ARM_VGIC_H */

Please fix the name here.

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

Quoted 10 lines
xen/include/asm-arm/domain.h +2 -0
index 968ffb0c81..e8f2917140 100644
@@ -198,7 +198,11 @@ struct arch_vcpu
      union gic_state_data gic;
      uint64_t lr_mask;
 
+#ifdef CONFIG_NEW_VGIC
+    struct vgic_cpu vgic_cpu;

Hmmm. Is there any point of the naming? You are in arch_vcpu so likely it will describe the vGIC CPU interface.

If you real want to do the renaming, then do it for the current vGIC as well.

Quoted 23 lines
xen/include/asm-arm/domain.h +2 -0
+#else
      struct vgic_cpu vgic;
+#endif
 
      /* Timer registers  */
      uint32_t cntkctl;
xen/include/asm-arm/vgic.h +4 -0
index 85ad2aca79..96b99f5c85 100644
@@ -18,6 +18,10 @@
  #ifndef __ASM_ARM_VGIC_H__
  #define __ASM_ARM_VGIC_H__
 
+#ifdef CONFIG_NEW_VGIC
+#include <asm/arm_vgic.h>
+#else
+
  #include <xen/bitops.h>
  #include <xen/radix-tree.h>
  #include <xen/rbtree.h>

xen/include/asm-arm/vgic.h void vgic_v3_setup_hw(paddr_t dbase, · line 324

Quoted 8 lines
xen/include/asm-arm/vgic.h +2 -0
@@ -313,6 +317,8 @@ void vgic_v3_setup_hw(paddr_t dbase,
                        unsigned int intid_bits);
  #endif
 
+#endif /* !CONFIG_NEW_VGIC */
+
  /*** Common VGIC functions used by Xen arch code ****/
 
  /*

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 21/49 ARM: new VGIC: Add acccessor to new struct vgic_irq instance

In reply to Andre Przywara

The new VGIC implementation centers around a struct vgic_irq instance per virtual IRQ.
Provide a function to retrieve the right instance for a given IRQ number and (in case of private interrupts) the right VCPU. This also includes the corresponding put function, which does nothing for private interrupts and SPIs, but handles the ref-counting for LPIs.

This is based on Linux commit 64a959d66e47, written by Christoffer Dall.

---
 xen/arch/arm/vgic/vgic.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.h |  32 ++++++++++++++
 2 files changed, 139 insertions(+)
 create mode 100644 xen/arch/arm/vgic/vgic.c
 create mode 100644 xen/arch/arm/vgic/vgic.h
xen/arch/arm/vgic/vgic.c new file +107 -0
new file mode 100644
index 0000000000..3075091caa
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm/bug.h>
+#include <xen/sched.h>
+
+#include <asm/arm_vgic.h>
+#include "vgic.h"
+
+/*
+ * Iterate over the VM's list of mapped LPIs to find the one with a
+ * matching interrupt ID and return a reference to the IRQ structure.
+ */
+static struct vgic_irq *vgic_get_lpi(struct domain *d, u32 intid)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
+    struct vgic_irq *irq = NULL;
+
+    spin_lock(&dist->lpi_list_lock);
+
+    list_for_each_entry( irq, &dist->lpi_list_head, lpi_list )
+    {
+        if ( irq->intid != intid )
+            continue;
+
+        /*
+         * This increases the refcount, the caller is expected to
+         * call vgic_put_irq() later once it's finished with the IRQ.
+         */
+        vgic_get_irq_kref(irq);
+        goto out_unlock;
+    }
+    irq = NULL;
+
+out_unlock:
+    spin_unlock(&dist->lpi_list_lock);
+
+    return irq;
+}
+
+/*
+ * This looks up the virtual interrupt ID to get the corresponding
+ * struct vgic_irq. It also increases the refcount, so any caller is expected
+ * to call vgic_put_irq() once it's finished with this IRQ.
+ */
+struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
+                  u32 intid)
+{
+    /* SGIs and PPIs */
+    if ( intid <= VGIC_MAX_PRIVATE )
+        return &vcpu->arch.vgic_cpu.private_irqs[intid];
+
+    /* SPIs */
+    if ( intid <= VGIC_MAX_SPI )
+        return &d->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
+
+    /* LPIs */
+    if ( intid >= VGIC_MIN_LPI )
+        return vgic_get_lpi(d, intid);
+
+    WARN();
+    return NULL;
+}
+
+void vgic_put_irq(struct domain *d, struct vgic_irq *irq)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
+
+    if ( irq->intid < VGIC_MIN_LPI )
+        return;
+
+    spin_lock(&dist->lpi_list_lock);
+    if ( !atomic_dec_and_test(&irq->refcount) )
+    {
+        spin_unlock(&dist->lpi_list_lock);
+        return;
+    };
+
+    list_del(&irq->lpi_list);
+    dist->lpi_list_count--;
+    spin_unlock(&dist->lpi_list_lock);
+
+    xfree(irq);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic.h new file +32 -0
new file mode 100644
index 0000000000..7a15cfdd79
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __XEN_ARM_VGIC_NEW_H__
+#define __XEN_ARM_VGIC_NEW_H__
+
+struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
+                              u32 intid);
+void vgic_put_irq(struct domain *d, struct vgic_irq *irq);
+
+static inline void vgic_get_irq_kref(struct vgic_irq *irq)
+{
+    if ( irq->intid < VGIC_MIN_LPI )
+        return;
+
+    atomic_inc(&irq->refcount);
+}
+
+#endif
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 21/49] ARM: new VGIC: Add acccessor to new struct vgic_irq instance

Hi Andre,

Quoted 35 lines · Andre Przywara

The new VGIC implementation centers around a struct vgic_irq instance per virtual IRQ.
Provide a function to retrieve the right instance for a given IRQ number and (in case of private interrupts) the right VCPU. This also includes the corresponding put function, which does nothing for private interrupts and SPIs, but handles the ref-counting for LPIs.

This is based on Linux commit 64a959d66e47, written by Christoffer Dall.

---
  xen/arch/arm/vgic/vgic.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.h |  32 ++++++++++++++
  2 files changed, 139 insertions(+)

create mode 100644 xen/arch/arm/vgic/vgic.c
create mode 100644 xen/arch/arm/vgic/vgic.h

xen/arch/arm/vgic/vgic.c new file +12 -0
new file mode 100644
index 0000000000..3075091caa
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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.

xen/arch/arm/vgic/vgic.c line 22

Quoted 10 lines
xen/arch/arm/vgic/vgic.c new file +10 -0
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm/bug.h>
+#include <xen/sched.h>
+
+#include <asm/arm_vgic.h>
+#include "vgic.h"

Please order the include alphabetically.

xen/arch/arm/vgic/vgic.c line 35

Quoted 13 lines
xen/arch/arm/vgic/vgic.c new file +13 -0
+
+/*
+ * Iterate over the VM's list of mapped LPIs to find the one with a
+ * matching interrupt ID and return a reference to the IRQ structure.
+ */
+static struct vgic_irq *vgic_get_lpi(struct domain *d, u32 intid)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
+    struct vgic_irq *irq = NULL;
+
+    spin_lock(&dist->lpi_list_lock);
+
+    list_for_each_entry( irq, &dist->lpi_list_head, lpi_list )

I think it would be worth thinking of a different data structure here. The number of LPIs can be quite high for the hardware domain.

Quoted 16 lines
xen/arch/arm/vgic/vgic.c new file +16 -0
+    {
+        if ( irq->intid != intid )
+            continue;
+
+        /*
+         * This increases the refcount, the caller is expected to
+         * call vgic_put_irq() later once it's finished with the IRQ.
+         */
+        vgic_get_irq_kref(irq);
+        goto out_unlock;
+    }
+    irq = NULL;
+
+out_unlock:
+    spin_unlock(&dist->lpi_list_lock);
+

xen/arch/arm/vgic/vgic.c line 61

Quoted 10 lines
xen/arch/arm/vgic/vgic.c new file +10 -0
+    return irq;
+}
+
+/*
+ * This looks up the virtual interrupt ID to get the corresponding
+ * struct vgic_irq. It also increases the refcount, so any caller is expected
+ * to call vgic_put_irq() once it's finished with this IRQ.
+ */
+struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
+                  u32 intid)

Indentation.

Quoted 4 lines
xen/arch/arm/vgic/vgic.c new file +4 -0
+{
+    /* SGIs and PPIs */
+    if ( intid <= VGIC_MAX_PRIVATE )
+        return &vcpu->arch.vgic_cpu.private_irqs[intid];

xen/arch/arm/vgic/vgic.c line 75

Quoted 10 lines
xen/arch/arm/vgic/vgic.c new file +10 -0
+
+    /* SPIs */
+    if ( intid <= VGIC_MAX_SPI )
+        return &d->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
+
+    /* LPIs */
+    if ( intid >= VGIC_MIN_LPI )
+        return vgic_get_lpi(d, intid);
+
+    WARN();

Newline here please.

I would turn into an ASSERT_UNREACHABLE() so it is only happening in debug build and avoid to worry about a guest exploiting that :).

Quoted 8 lines
xen/arch/arm/vgic/vgic.c new file +8 -0
+    return NULL;
+}
+
+void vgic_put_irq(struct domain *d, struct vgic_irq *irq)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
+
+    if ( irq->intid < VGIC_MIN_LPI )

xen/arch/arm/vgic/vgic.c line 93

Quoted 10 lines
xen/arch/arm/vgic/vgic.c new file +10 -0
+        return;
+
+    spin_lock(&dist->lpi_list_lock);
+    if ( !atomic_dec_and_test(&irq->refcount) )
+    {
+        spin_unlock(&dist->lpi_list_lock);
+        return;
+    };
+
+    list_del(&irq->lpi_list);

I would add

ASSERT(lpi_list_count >= 1);

But it is a bit hard to know whether this code is valid given you don't have any implementation of ITS so far.

xen/arch/arm/vgic/vgic.h

Quoted 19 lines
xen/arch/arm/vgic/vgic.c new file +14 -0
+    dist->lpi_list_count--;
+    spin_unlock(&dist->lpi_list_lock);
+
+    xfree(irq);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic.h new file +0 -0
new file mode 100644
index 0000000000..7a15cfdd79

To be honest, I am not a big fan of headers defined in the code bits. So I would need a reason for that to be there and not in the include you defined in the previous patch.

Quoted 9 lines
xen/arch/arm/vgic/vgic.h new file +8 -0
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *

xen/arch/arm/vgic/vgic.h line 18

Quoted 10 lines
xen/arch/arm/vgic/vgic.h new file +10 -0
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __XEN_ARM_VGIC_NEW_H__
+#define __XEN_ARM_VGIC_NEW_H__

This does not match the filename/path.

Quoted 4 lines
xen/arch/arm/vgic/vgic.h new file +4 -0
+
+struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
+                              u32 intid);
+void vgic_put_irq(struct domain *d, struct vgic_irq *irq);

xen/arch/arm/vgic/vgic.h line 32

Quoted 10 lines
xen/arch/arm/vgic/vgic.h new file +10 -0
+
+static inline void vgic_get_irq_kref(struct vgic_irq *irq)
+{
+    if ( irq->intid < VGIC_MIN_LPI )
+        return;
+
+    atomic_inc(&irq->refcount);
+}
+
+#endif

Missing emacs magic.

Quoted 1 line

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 21/49] ARM: new VGIC: Add acccessor to new struct vgic_irq instance

Hi,

Quoted 43 lines · Julien Grall

Hi Andre,

Quoted 40 lines · Andre Przywara

The new VGIC implementation centers around a struct vgic_irq instance per virtual IRQ.
Provide a function to retrieve the right instance for a given IRQ number and (in case of private interrupts) the right VCPU. This also includes the corresponding put function, which does nothing for private interrupts and SPIs, but handles the ref-counting for LPIs.

This is based on Linux commit 64a959d66e47, written by Christoffer Dall.

---
  xen/arch/arm/vgic/vgic.c | 107
+++++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.h |  32 ++++++++++++++

  2 files changed, 139 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic.c
  create mode 100644 xen/arch/arm/vgic/vgic.h

xen/arch/arm/vgic/vgic.c new file +16 -0
new file mode 100644
index 0000000000..3075091caa
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
Quoted 8 lines

xen/arch/arm/vgic/vgic.c line 22

Quoted 6 lines
xen/arch/arm/vgic/vgic.c new file +6 -0
+
+#include <asm/bug.h>
+#include <xen/sched.h>
+
+#include <asm/arm_vgic.h>
+#include "vgic.h"

Please order the include alphabetically.

Sure.

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/vgic/vgic.c new file +8 -0
+
+/*
+ * Iterate over the VM's list of mapped LPIs to find the one with a
+ * matching interrupt ID and return a reference to the IRQ structure.
+ */
+static struct vgic_irq *vgic_get_lpi(struct domain *d, u32 intid)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
Quoted 8 lines

xen/arch/arm/vgic/vgic.c line 35

Quoted 5 lines
xen/arch/arm/vgic/vgic.c new file +5 -0
+    struct vgic_irq *irq = NULL;
+
+    spin_lock(&dist->lpi_list_lock);
+
+    list_for_each_entry( irq, &dist->lpi_list_head, lpi_list )

I think it would be worth thinking of a different data structure here. The number of LPIs can be quite high for the hardware domain.

Probably true. I just didn't want to waste time on this yet, as we don't have LPIs at the moment. Having a list has the big advantage of being easy to understand and to implement, so I consider this an optimization that we can have later.

Quoted 47 lines
Quoted 22 lines
xen/arch/arm/vgic/vgic.c new file +22 -0
+    {
+        if ( irq->intid != intid )
+            continue;
+
+        /*
+         * This increases the refcount, the caller is expected to
+         * call vgic_put_irq() later once it's finished with the IRQ.
+         */
+        vgic_get_irq_kref(irq);
+        goto out_unlock;
+    }
+    irq = NULL;
+
+out_unlock:
+    spin_unlock(&dist->lpi_list_lock);
+
+    return irq;
+}
+
+/*
+ * This looks up the virtual interrupt ID to get the corresponding
+ * struct vgic_irq. It also increases the refcount, so any caller is
Quoted 5 lines

expected
+ * to call vgic_put_irq() once it's finished with this IRQ.
+ */
+struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu, +                  u32 intid)

Indentation.

Quoted 4 lines
xen/arch/arm/vgic/vgic.c new file +4 -0
+{
+    /* SGIs and PPIs */
+    if ( intid <= VGIC_MAX_PRIVATE )
+        return &vcpu->arch.vgic_cpu.private_irqs[intid];

xen/arch/arm/vgic/vgic.c line 75

Quoted 10 lines
xen/arch/arm/vgic/vgic.c new file +10 -0
+
+    /* SPIs */
+    if ( intid <= VGIC_MAX_SPI )
+        return &d->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
+
+    /* LPIs */
+    if ( intid >= VGIC_MIN_LPI )
+        return vgic_get_lpi(d, intid);
+
+    WARN();

Newline here please.

Quoted 2 lines

I would turn into an ASSERT_UNREACHABLE() so it is only happening in debug build and avoid to worry about a guest exploiting that :).

Good point - and easy to fix :-D

Quoted 21 lines
Quoted 8 lines

+    return NULL;

xen/arch/arm/vgic/vgic.c new file +7 -0
+}
+
+void vgic_put_irq(struct domain *d, struct vgic_irq *irq)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
+
+    if ( irq->intid < VGIC_MIN_LPI )

xen/arch/arm/vgic/vgic.c line 93

Quoted 10 lines
xen/arch/arm/vgic/vgic.c new file +10 -0
+        return;
+
+    spin_lock(&dist->lpi_list_lock);
+    if ( !atomic_dec_and_test(&irq->refcount) )
+    {
+        spin_unlock(&dist->lpi_list_lock);
+        return;
+    };
+
+    list_del(&irq->lpi_list);

I would add

Quoted 4 lines

ASSERT(lpi_list_count >= 1);

But it is a bit hard to know whether this code is valid given you don't have any implementation of ITS so far.

Is it? You should not need the actual ITS code to validate this function. In fact there are only very few users in vgic-its.c. The main point here is that you have textbook ref-counting: *Every* time you take a pointer to an IRQ (vgic_get_irq), you have to tell the code when you are done with it (vgic_put_irq).
So we decided to have this ref-counting done properly even though it's pointless for SPIs, PPIs and SGIs, as it makes the code very clear to read and verify.
Mostly you have get and put in one function, but sometimes there is more time between them: for instance if an interrupt goes to the ap_list. We "get" it, add it to the list, then return. When the guest has actually handled this interrupt, we remove it from the list and only then "put" it again.

Quoted 15 lines
Quoted 15 lines

+    dist->lpi_list_count--;
+    spin_unlock(&dist->lpi_list_lock);

xen/arch/arm/vgic/vgic.c new file +12 -0
+
+    xfree(irq);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic.h +0 -0
Quoted 8 lines

xen/arch/arm/vgic/vgic.h

Quoted 4 lines

new file mode 100644
index 0000000000..7a15cfdd79

xen/arch/arm/vgic/vgic.h new file +0 -0

To be honest, I am not a big fan of headers defined in the code bits. So I would need a reason for that to be there and not in the include you defined in the previous patch.

What is the problem with that?
The rationale here is to gather all definitions and prototypes that are actually VGIC *internal*. No code outside of the actual VGIC (xen/arch/arm/vgic/) should be concerned with it, and so I consider this good style to keep this header file local. This makes it very clear that no generic or arch code should ever tinker with anything defined in it.

Think about it like we could actually glue all those files in this new directory together into one glorious new-vgic.c. Then we would not need this header. But it's terrible to read and review, so we have this nice split-up into vgic-mmio.c and vgic.c, for instance. And now we need this header file to link those files together, to allow the MMIO emulation to manipulate the state of an interrupt and queue it to a VCPU, for instance.

It's totally possible that there are definitions and prototypes in here which don't belong here. TBH I didn't review this file here very carefully for what we still need and what not, so I am happy to take advice on what's wrong here.

Cheers,
Andre.

Quoted 42 lines
Quoted 10 lines
xen/arch/arm/vgic/vgic.h new file +8 -0
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *

xen/arch/arm/vgic/vgic.h line 18

Quoted 10 lines
xen/arch/arm/vgic/vgic.h new file +10 -0
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __XEN_ARM_VGIC_NEW_H__
+#define __XEN_ARM_VGIC_NEW_H__

This does not match the filename/path.

Quoted 4 lines
xen/arch/arm/vgic/vgic.h new file +4 -0
+
+struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
+                              u32 intid);
+void vgic_put_irq(struct domain *d, struct vgic_irq *irq);

xen/arch/arm/vgic/vgic.h line 32

Quoted 10 lines
xen/arch/arm/vgic/vgic.h new file +10 -0
+
+static inline void vgic_get_irq_kref(struct vgic_irq *irq)
+{
+    if ( irq->intid < VGIC_MIN_LPI )
+        return;
+
+    atomic_inc(&irq->refcount);
+}
+
+#endif

Missing emacs magic.

Quoted 1 line
Quoted 2 lines

Cheers,

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

#49 lore raw

Re: [RFC PATCH 21/49] ARM: new VGIC: Add acccessor to new struct vgic_irq instance

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 73 lines · Julien Grall
Quoted 43 lines

Hi Andre,

Quoted 40 lines · Andre Przywara

The new VGIC implementation centers around a struct vgic_irq instance per virtual IRQ.
Provide a function to retrieve the right instance for a given IRQ number and (in case of private interrupts) the right VCPU. This also includes the corresponding put function, which does nothing for private interrupts and SPIs, but handles the ref-counting for LPIs.

This is based on Linux commit 64a959d66e47, written by Christoffer Dall.

---
   xen/arch/arm/vgic/vgic.c | 107
+++++++++++++++++++++++++++++++++++++++++++++++
   xen/arch/arm/vgic/vgic.h |  32 ++++++++++++++
   2 files changed, 139 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic.c
  create mode 100644 xen/arch/arm/vgic/vgic.h

xen/arch/arm/vgic/vgic.c new file +16 -0
new file mode 100644
index 0000000000..3075091caa
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
Quoted 8 lines

xen/arch/arm/vgic/vgic.c line 22

Quoted 6 lines
xen/arch/arm/vgic/vgic.c new file +6 -0
+
+#include <asm/bug.h>
+#include <xen/sched.h>
+
+#include <asm/arm_vgic.h>
+#include "vgic.h"

Please order the include alphabetically.

Sure.

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/vgic/vgic.c new file +8 -0
+
+/*
+ * Iterate over the VM's list of mapped LPIs to find the one with a
+ * matching interrupt ID and return a reference to the IRQ structure.
+ */
+static struct vgic_irq *vgic_get_lpi(struct domain *d, u32 intid)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
Quoted 8 lines

xen/arch/arm/vgic/vgic.c line 35

Quoted 5 lines
xen/arch/arm/vgic/vgic.c new file +5 -0
+    struct vgic_irq *irq = NULL;
+
+    spin_lock(&dist->lpi_list_lock);
+
+    list_for_each_entry( irq, &dist->lpi_list_head, lpi_list )

I think it would be worth thinking of a different data structure here. The number of LPIs can be quite high for the hardware domain.

Quoted 4 lines

Probably true. I just didn't want to waste time on this yet, as we don't have LPIs at the moment. Having a list has the big advantage of being easy to understand and to implement, so I consider this an optimization that we can have later.

I am not a big fan of adding code that will never be used as it is and just too slow. That's going to have an impact on platform such as Thunder-X.

Quoted 26 lines
Quoted 21 lines
Quoted 8 lines
xen/arch/arm/vgic/vgic.c new file +8 -0
+    return NULL;
+}
+
+void vgic_put_irq(struct domain *d, struct vgic_irq *irq)
+{
+    struct vgic_dist *dist = &d->arch.vgic;
+
+    if ( irq->intid < VGIC_MIN_LPI )

xen/arch/arm/vgic/vgic.c line 93

Quoted 10 lines
xen/arch/arm/vgic/vgic.c new file +10 -0
+        return;
+
+    spin_lock(&dist->lpi_list_lock);
+    if ( !atomic_dec_and_test(&irq->refcount) )
+    {
+        spin_unlock(&dist->lpi_list_lock);
+        return;
+    };
+
+    list_del(&irq->lpi_list);

I would add

Quoted 4 lines

ASSERT(lpi_list_count >= 1);

But it is a bit hard to know whether this code is valid given you don't have any implementation of ITS so far.

Quoted 13 lines

Is it? You should not need the actual ITS code to validate this function. In fact there are only very few users in vgic-its.c. The main point here is that you have textbook ref-counting: *Every* time you take a pointer to an IRQ (vgic_get_irq), you have to tell the code when you are done with it (vgic_put_irq).
So we decided to have this ref-counting done properly even though it's pointless for SPIs, PPIs and SGIs, as it makes the code very clear to read and verify.
Mostly you have get and put in one function, but sometimes there is more time between them: for instance if an interrupt goes to the ap_list. We "get" it, add it to the list, then return. When the guest has actually handled this interrupt, we remove it from the list and only then "put" it again.

When I read only this series, I can't tell why refcounting is necessary for LPIs only. This is neither said in the commit message nor in the cover letter. Note that I know the answer, I doubt the other may know it.

So yes, the code is trivial. But without the full logic/explanation, it is hard to tell how the ITS is going to fit in and whether what you do know looks sensible.

Quoted 39 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/arm/vgic/vgic.c new file +14 -0
+    dist->lpi_list_count--;
+    spin_unlock(&dist->lpi_list_lock);
+
+    xfree(irq);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic.h +0 -0
Quoted 8 lines

xen/arch/arm/vgic/vgic.h

Quoted 4 lines

new file mode 100644
index 0000000000..7a15cfdd79

xen/arch/arm/vgic/vgic.h new file +0 -0

To be honest, I am not a big fan of headers defined in the code bits. So I would need a reason for that to be there and not in the include you defined in the previous patch.

What is the problem with that?
The rationale here is to gather all definitions and prototypes that are actually VGIC *internal*. No code outside of the actual VGIC (xen/arch/arm/vgic/) should be concerned with it, and so I consider this good style to keep this header file local. This makes it very clear that no generic or arch code should ever tinker with anything defined in it.

Think about it like we could actually glue all those files in this new directory together into one glorious new-vgic.c. Then we would not need this header. But it's terrible to read and review, so we have this nice split-up into vgic-mmio.c and vgic.c, for instance. And now we need this header file to link those files together, to allow the MMIO emulation to manipulate the state of an interrupt and queue it to a VCPU, for instance.

Quoted 4 lines

It's totally possible that there are definitions and prototypes in here which don't belong here. TBH I didn't review this file here very carefully for what we still need and what not, so I am happy to take advice on what's wrong here.

Fair enough.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 22/49 ARM: new VGIC: Implement virtual IRQ injection

In reply to Andre Przywara

Provide a vgic_queue_irq_unlock() function which decides whether a given IRQ needs to be queued to a VCPU's ap_list.
This should be called whenever an IRQ becomes pending or enabled, either as a result of a hardware IRQ injection, from devices emulated by Xen (like the architected timer) or from MMIO accesses to the distributor emulation.
Also provides the necessary functions to allow to inject an IRQ to a guest. Since this is the first code that starts using our locking mechanism, we add some (hopefully) clear documentation of our locking strategy and requirements along with this patch.

This is based on Linux commit 81eeb95ddbab, written by Christoffer Dall.

---
 xen/arch/arm/vgic/vgic.c | 224 +++++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.h |  10 +++
 2 files changed, 234 insertions(+)
xen/arch/arm/vgic/vgic.c +224 -0
index 3075091caa..f517df6d00 100644
@@ -21,6 +21,32 @@
 #include <asm/arm_vgic.h>
 #include "vgic.h"
 
+/*
+ * Locking order is always:
+ * kvm->lock (mutex)
+ *   its->cmd_lock (mutex)
+ *     its->its_lock (mutex)
+ *       vgic_cpu->ap_list_lock
+ *         kvm->lpi_list_lock
+ *           vgic_irq->irq_lock
+ *
+ * If you need to take multiple locks, always take the upper lock first,
+ * then the lower ones, e.g. first take the its_lock, then the irq_lock.
+ * If you are already holding a lock and need to take a higher one, you
+ * have to drop the lower ranking lock first and re-aquire it after having
+ * taken the upper one.
+ *
+ * When taking more than one ap_list_lock at the same time, always take the
+ * lowest numbered VCPU's ap_list_lock first, so:
+ *   vcpuX->vcpu_id < vcpuY->vcpu_id:
+ *     spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
+ *     spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
+ *
+ * Since the VGIC must support injecting virtual interrupts from ISRs, we have
+ * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
+ * spinlocks for any lock that may be taken while injecting an interrupt.
+ */
+
 /*
  * Iterate over the VM's list of mapped LPIs to find the one with a
  * matching interrupt ID and return a reference to the IRQ structure.
@@ -97,6 +123,204 @@ void vgic_put_irq(struct domain *d, struct vgic_irq *irq)
     xfree(irq);
 }
 
+/**
+ * vgic_target_oracle - compute the target vcpu for an irq
+ *
+ * @irq:    The irq to route. Must be already locked.
+ *
+ * Based on the current state of the interrupt (enabled, pending,
+ * active, vcpu and target_vcpu), compute the next vcpu this should be
+ * given to. Return NULL if this shouldn't be injected at all.
+ *
+ * Requires the IRQ lock to be held.
+ */
+static struct vcpu *vgic_target_oracle(struct vgic_irq *irq)
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+
+    /* If the interrupt is active, it must stay on the current vcpu */
+    if ( irq->active )
+        return irq->vcpu ? : irq->target_vcpu;
+
+    /*
+     * If the IRQ is not active but enabled and pending, we should direct
+     * it to its configured target VCPU.
+     * If the distributor is disabled, pending interrupts shouldn't be
+     * forwarded.
+     */
+    if ( irq->enabled && irq_is_pending(irq) )
+    {
+        if ( unlikely(irq->target_vcpu &&
+                 !irq->target_vcpu->domain->arch.vgic.enabled) )
+            return NULL;
+
+        return irq->target_vcpu;
+    }
+
+    /* If neither active nor pending and enabled, then this IRQ should not
+     * be queued to any VCPU.
+     */
+    return NULL;
+}
+
+/*
+ * Only valid injection if changing level for level-triggered IRQs or for a
+ * rising edge.
+ */
+static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
+{
+    switch (irq->config)
+    {
+    case VGIC_CONFIG_LEVEL:
+        return irq->line_level != level;
+    case VGIC_CONFIG_EDGE:
+        return level;
+    }
+
+    return false;
+}
+
+/*
+ * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
+ * Do the queuing if necessary, taking the right locks in the right order.
+ * Returns true when the IRQ was queued, false otherwise.
+ *
+ * Needs to be entered with the IRQ lock already held, but will return
+ * with all locks dropped.
+ */
+bool vgic_queue_irq_unlock(struct domain *d, struct vgic_irq *irq,
+               unsigned long flags)
+{
+    struct vcpu *vcpu;
+    bool running;
+
+    ASSERT(spin_is_locked(&irq->irq_lock));
+
+retry:
+    vcpu = vgic_target_oracle(irq);
+    if ( irq->vcpu || !vcpu )
+    {
+        /*
+         * If this IRQ is already on a VCPU's ap_list, then it
+         * cannot be moved or modified and there is no more work for
+         * us to do.
+         *
+         * Otherwise, if the irq is not pending and enabled, it does
+         * not need to be inserted into an ap_list and there is also
+         * no more work for us to do.
+         */
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+
+        /*
+         * We have to kick the VCPU here, because we could be
+         * queueing an edge-triggered interrupt for which we
+         * get no EOI maintenance interrupt. In that case,
+         * while the IRQ is already on the VCPU's AP list, the
+         * VCPU could have EOI'ed the original interrupt and
+         * won't see this one until it exits for some other
+         * reason.
+         */
+        if ( vcpu )
+            vcpu_unblock(vcpu);
+        return false;
+    }
+
+    /*
+     * We must unlock the irq lock to take the ap_list_lock where
+     * we are going to insert this new pending interrupt.
+     */
+    spin_unlock_irqrestore(&irq->irq_lock, flags);
+
+    /* someone can do stuff here, which we re-check below */
+
+    spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+    spin_lock(&irq->irq_lock);
+
+    /*
+     * Did something change behind our backs?
+     *
+     * There are two cases:
+     * 1) The irq lost its pending state or was disabled behind our
+     *    backs and/or it was queued to another VCPU's ap_list.
+     * 2) Someone changed the affinity on this irq behind our
+     *    backs and we are now holding the wrong ap_list_lock.
+     *
+     * In both cases, drop the locks and retry.
+     */
+
+    if ( unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq)) )
+    {
+        spin_unlock(&irq->irq_lock);
+        spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+
+        spin_lock_irqsave(&irq->irq_lock, flags);
+        goto retry;
+    }
+
+    /*
+     * Grab a reference to the irq to reflect the fact that it is
+     * now in the ap_list.
+     */
+    vgic_get_irq_kref(irq);
+    list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);
+    irq->vcpu = vcpu;
+
+    spin_unlock(&irq->irq_lock);
+    spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+
+    running = vcpu->is_running;
+    vcpu_unblock(vcpu);
+    if ( running && vcpu != current )
+        smp_send_event_check_mask(cpumask_of(vcpu->processor));
+
+    return true;
+}
+
+/**
+ * vgic_inject_irq - Inject an IRQ from a device to the vgic
+ * @d:       The domain pointer
+ * @vcpu:    The vCPU for PPIs
+ * @intid:   The INTID to inject a new state to.
+ * @level:   Edge-triggered:  true:  to trigger the interrupt
+ *                false: to ignore the call
+ *       Level-sensitive  true:  raise the input signal
+ *                false: lower the input signal
+ *
+ * The VGIC is not concerned with devices being active-LOW or active-HIGH for
+ * level-sensitive interrupts.  You can think of the level parameter as 1
+ * being HIGH and 0 being LOW and all devices being active-HIGH.
+ */
+int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid,
+            bool level)
+{
+    struct vgic_irq *irq;
+    unsigned long flags;
+
+    irq = vgic_get_irq(d, vcpu, intid);
+    if ( !irq )
+        return -EINVAL;
+
+    spin_lock_irqsave(&irq->irq_lock, flags);
+
+    if ( !vgic_validate_injection(irq, level) )
+    {
+        /* Nothing to see here, move along... */
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+        vgic_put_irq(d, irq);
+        return 0;
+    }
+
+    if ( irq->config == VGIC_CONFIG_LEVEL )
+        irq->line_level = level;
+    else
+        irq->pending_latch = true;
+
+    vgic_queue_irq_unlock(d, irq, flags);
+    vgic_put_irq(d, irq);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic/vgic.h +10 -0
index 7a15cfdd79..5127739f0f 100644
@@ -17,9 +17,19 @@
 #ifndef __XEN_ARM_VGIC_NEW_H__
 #define __XEN_ARM_VGIC_NEW_H__
 
+static inline bool irq_is_pending(struct vgic_irq *irq)
+{
+    if ( irq->config == VGIC_CONFIG_EDGE )
+        return irq->pending_latch;
+    else
+        return irq->pending_latch || irq->line_level;
+}
+
 struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
                               u32 intid);
 void vgic_put_irq(struct domain *d, struct vgic_irq *irq);
+bool vgic_queue_irq_unlock(struct domain *d, struct vgic_irq *irq,
+               unsigned long flags);
 
 static inline void vgic_get_irq_kref(struct vgic_irq *irq)
 {
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 22/49] ARM: new VGIC: Implement virtual IRQ injection

Hi Andre,

Quoted 24 lines · Andre Przywara

Provide a vgic_queue_irq_unlock() function which decides whether a given IRQ needs to be queued to a VCPU's ap_list.
This should be called whenever an IRQ becomes pending or enabled, either as a result of a hardware IRQ injection, from devices emulated by Xen (like the architected timer) or from MMIO accesses to the distributor emulation.
Also provides the necessary functions to allow to inject an IRQ to a guest. Since this is the first code that starts using our locking mechanism, we add some (hopefully) clear documentation of our locking strategy and requirements along with this patch.

This is based on Linux commit 81eeb95ddbab, written by Christoffer Dall.

---
  xen/arch/arm/vgic/vgic.c | 224 +++++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.h |  10 +++
  2 files changed, 234 insertions(+)
xen/arch/arm/vgic/vgic.c +0 -0
index 3075091caa..f517df6d00 100644

xen/arch/arm/vgic/vgic.c line 26

Quoted 6 lines
xen/arch/arm/vgic/vgic.c +3 -0
@@ -21,6 +21,32 @@
  #include <asm/arm_vgic.h>
  #include "vgic.h"
 
+/*
+ * Locking order is always:
+ * kvm->lock (mutex)

You probably want to update the locking order to match Xen one. In that case, I am not sure if we need to take the domain lock in the code?

xen/arch/arm/vgic/vgic.c line 36

Quoted 11 lines
xen/arch/arm/vgic/vgic.c +10 -0
+ *   its->cmd_lock (mutex)
+ *     its->its_lock (mutex)
 
+ *       vgic_cpu->ap_list_lock
+ *         kvm->lpi_list_lock
+ *           vgic_irq->irq_lock
+ *
+ * If you need to take multiple locks, always take the upper lock first,
+ * then the lower ones, e.g. first take the its_lock, then the irq_lock.
+ * If you are already holding a lock and need to take a higher one, you
+ * have to drop the lower ranking lock first and re-aquire it after having

s/re-aquite/acquire/

xen/arch/arm/vgic/vgic.c line 47

Quoted 11 lines
xen/arch/arm/vgic/vgic.c +11 -0
+ * taken the upper one.
+ *
+ * When taking more than one ap_list_lock at the same time, always take the
+ * lowest numbered VCPU's ap_list_lock first, so:
+ *   vcpuX->vcpu_id < vcpuY->vcpu_id:
+ *     spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
+ *     spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
+ *
+ * Since the VGIC must support injecting virtual interrupts from ISRs, we have
+ * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
+ * spinlocks for any lock that may be taken while injecting an interrupt.

It is quite nice to see the locking explained in the file and in general a lot of explanation within the code :).

Quoted 17 lines
xen/arch/arm/vgic/vgic.c +2 -0
+ */
+
  /*
   * Iterate over the VM's list of mapped LPIs to find the one with a

* matching interrupt ID and return a reference to the IRQ structure.

xen/arch/arm/vgic/vgic.c +8 -0
@@ -97,6 +123,204 @@ void vgic_put_irq(struct domain *d, struct vgic_irq *irq)
      xfree(irq);
  }
 
+/**
+ * vgic_target_oracle - compute the target vcpu for an irq
+ *
+ * @irq:    The irq to route. Must be already locked.
+ *
+ * Based on the current state of the interrupt (enabled, pending,
+ * active, vcpu and target_vcpu), compute the next vcpu this should be
+ * given to. Return NULL if this shouldn't be injected at all.

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 143

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+ *
+ * Requires the IRQ lock to be held.
+ */
+static struct vcpu *vgic_target_oracle(struct vgic_irq *irq)
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+
+    /* If the interrupt is active, it must stay on the current vcpu */
+    if ( irq->active )
+        return irq->vcpu ? : irq->target_vcpu;

I am not sure to understand why you check whether irq->vcpu is NULL. If the interrupt is active, then irq->vcpu should be NULL. Did I miss anything?

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 154

Quoted 11 lines
xen/arch/arm/vgic/vgic.c +11 -0
+
+    /*
+     * If the IRQ is not active but enabled and pending, we should direct
+     * it to its configured target VCPU.
+     * If the distributor is disabled, pending interrupts shouldn't be
+     * forwarded.
+     */
+    if ( irq->enabled && irq_is_pending(irq) )
+    {
+        if ( unlikely(irq->target_vcpu &&
+                 !irq->target_vcpu->domain->arch.vgic.enabled) )

The indentation looks wrong here.

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 160

Quoted 6 lines
xen/arch/arm/vgic/vgic.c +6 -0
+            return NULL;
+
+        return irq->target_vcpu;
+    }
+
+    /* If neither active nor pending and enabled, then this IRQ should not

Comment style:

/*
* ...

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 172

Quoted 12 lines
xen/arch/arm/vgic/vgic.c +12 -0
+     * be queued to any VCPU.
+     */
+    return NULL;
+}
+
+/*
+ * Only valid injection if changing level for level-triggered IRQs or for a
+ * rising edge.
+ */
+static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
+{
+    switch (irq->config)

switch ( ... )

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 179

Quoted 7 lines
xen/arch/arm/vgic/vgic.c +7 -0
+    {
+    case VGIC_CONFIG_LEVEL:
+        return irq->line_level != level;
+    case VGIC_CONFIG_EDGE:
+        return level;
+    }
+

I would add an ASSERT_UNREACHABLE().

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 192

Quoted 13 lines
xen/arch/arm/vgic/vgic.c +13 -0
+    return false;
+}
+
+/*
+ * Check whether an IRQ needs to (and can) be queued to a VCPU's ap list.
+ * Do the queuing if necessary, taking the right locks in the right order.
+ * Returns true when the IRQ was queued, false otherwise.
+ *
+ * Needs to be entered with the IRQ lock already held, but will return
+ * with all locks dropped.
+ */
+bool vgic_queue_irq_unlock(struct domain *d, struct vgic_irq *irq,
+               unsigned long flags)

Indentation. Also same remark as from vgic_inject_irq. No-one seems to care about the return (even in KVM :)).

Quoted 22 lines
xen/arch/arm/vgic/vgic.c +22 -0
+{
+    struct vcpu *vcpu;
+    bool running;
+
+    ASSERT(spin_is_locked(&irq->irq_lock));
+
+retry:
+    vcpu = vgic_target_oracle(irq);
+    if ( irq->vcpu || !vcpu )
+    {
+        /*
+         * If this IRQ is already on a VCPU's ap_list, then it
+         * cannot be moved or modified and there is no more work for
+         * us to do.
+         *
+         * Otherwise, if the irq is not pending and enabled, it does
+         * not need to be inserted into an ap_list and there is also
+         * no more work for us to do.
+         */
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+
+        /*

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 224

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+         * We have to kick the VCPU here, because we could be
+         * queueing an edge-triggered interrupt for which we
+         * get no EOI maintenance interrupt. In that case,
+         * while the IRQ is already on the VCPU's AP list, the
+         * VCPU could have EOI'ed the original interrupt and
+         * won't see this one until it exits for some other
+         * reason.
+         */
+        if ( vcpu )
+            vcpu_unblock(vcpu);

vcpu_unblock will only "unblock" a vCPU that is blocked. It won't notify a running vCPU. So you want to have something similar to:

vcpu_unblock(vcpu);
if ( running && vcpu != current )
smp_send_event_check_mask(...);

It is probably worth to introduce an helper for that.

Quoted 31 lines
xen/arch/arm/vgic/vgic.c +31 -0
+        return false;
+    }
+
+    /*
+     * We must unlock the irq lock to take the ap_list_lock where
+     * we are going to insert this new pending interrupt.
+     */
+    spin_unlock_irqrestore(&irq->irq_lock, flags);
+
+    /* someone can do stuff here, which we re-check below */
+
+    spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+    spin_lock(&irq->irq_lock);
+
+    /*
+     * Did something change behind our backs?
+     *
+     * There are two cases:
+     * 1) The irq lost its pending state or was disabled behind our
+     *    backs and/or it was queued to another VCPU's ap_list.
+     * 2) Someone changed the affinity on this irq behind our
+     *    backs and we are now holding the wrong ap_list_lock.
+     *
+     * In both cases, drop the locks and retry.
+     */
+
+    if ( unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq)) )
+    {
+        spin_unlock(&irq->irq_lock);
+        spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 265

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+        spin_lock_irqsave(&irq->irq_lock, flags);
+        goto retry;
+    }
+
+    /*
+     * Grab a reference to the irq to reflect the fact that it is
+     * now in the ap_list.
+     */
+    vgic_get_irq_kref(irq);
+    list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);

I was expecting the list to be sorted here. But you seem to do it only in vgic_flush_lr_state() which is quite interesting.

I can foresee quite a few issues with this choice on Xen:
1) You compute the size of ap list in vgic_flush_lr_state() and take
lock on every IRQ one by one. A guest could be nasty and make that list quite big by make IRQs pending but never "active" them (i.e read IAR).
2) This might be an issue while checking whether you need to deliver an
interrupt (vgic_vcpu_pending_irq) because the list is not sorted.

Quoted 19 lines
xen/arch/arm/vgic/vgic.c +19 -0
+    irq->vcpu = vcpu;
+
+    spin_unlock(&irq->irq_lock);
+    spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+
+    running = vcpu->is_running;
+    vcpu_unblock(vcpu);
+    if ( running && vcpu != current )
+        smp_send_event_check_mask(cpumask_of(vcpu->processor));
+
+    return true;
+}
+
+/**
+ * vgic_inject_irq - Inject an IRQ from a device to the vgic
+ * @d:       The domain pointer
+ * @vcpu:    The vCPU for PPIs
+ * @intid:   The INTID to inject a new state to.
+ * @level:   Edge-triggered:  true:  to trigger the interrupt

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 294

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+ *                false: to ignore the call
+ *       Level-sensitive  true:  raise the input signal
+ *                false: lower the input signal
+ *
+ * The VGIC is not concerned with devices being active-LOW or active-HIGH for
+ * level-sensitive interrupts.  You can think of the level parameter as 1
+ * being HIGH and 0 being LOW and all devices being active-HIGH.
+ */
+int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid,
+            bool level)

Indentation.

Quoted 47 lines
xen/arch/arm/vgic/vgic.c +29 -0
+{
+    struct vgic_irq *irq;
+    unsigned long flags;
+
+    irq = vgic_get_irq(d, vcpu, intid);
+    if ( !irq )
+        return -EINVAL;
+
+    spin_lock_irqsave(&irq->irq_lock, flags);
+
+    if ( !vgic_validate_injection(irq, level) )
+    {
+        /* Nothing to see here, move along... */
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+        vgic_put_irq(d, irq);
+        return 0;
+    }
+
+    if ( irq->config == VGIC_CONFIG_LEVEL )
+        irq->line_level = level;
+    else
+        irq->pending_latch = true;
+
+    vgic_queue_irq_unlock(d, irq, flags);
+    vgic_put_irq(d, irq);
+
+    return 0;
+}
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/vgic/vgic.h +6 -0
index 7a15cfdd79..5127739f0f 100644
@@ -17,9 +17,19 @@
  #ifndef __XEN_ARM_VGIC_NEW_H__
  #define __XEN_ARM_VGIC_NEW_H__
 
+static inline bool irq_is_pending(struct vgic_irq *irq)
+{
+    if ( irq->config == VGIC_CONFIG_EDGE )
+        return irq->pending_latch;
+    else
+        return irq->pending_latch || irq->line_level;

xen/arch/arm/vgic/vgic.h line 35

Quoted 10 lines
xen/arch/arm/vgic/vgic.h +4 -0
+}
+
  struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
                                u32 intid);
  void vgic_put_irq(struct domain *d, struct vgic_irq *irq);
+bool vgic_queue_irq_unlock(struct domain *d, struct vgic_irq *irq,
+               unsigned long flags);
 
  static inline void vgic_get_irq_kref(struct vgic_irq *irq)
  {

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 22/49] ARM: new VGIC: Implement virtual IRQ injection

Hi,

Quoted 93 lines · Julien Grall

Hi Andre,

Quoted 28 lines · Andre Przywara

Provide a vgic_queue_irq_unlock() function which decides whether a given IRQ needs to be queued to a VCPU's ap_list.
This should be called whenever an IRQ becomes pending or enabled, either as a result of a hardware IRQ injection, from devices emulated by Xen (like the architected timer) or from MMIO accesses to the distributor emulation.
Also provides the necessary functions to allow to inject an IRQ to a guest.
Since this is the first code that starts using our locking mechanism, we add some (hopefully) clear documentation of our locking strategy and requirements along with this patch.

This is based on Linux commit 81eeb95ddbab, written by Christoffer Dall.

---
  xen/arch/arm/vgic/vgic.c | 224
+++++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.h |  10 +++

  2 files changed, 234 insertions(+)
xen/arch/arm/vgic/vgic.c +0 -0
index 3075091caa..f517df6d00 100644
@@ -21,6 +21,32 @@
   #include <asm/arm_vgic.h>
   #include "vgic.h"
Quoted 3 lines

  +/*
+ * Locking order is always:
+ * kvm->lock (mutex)

You probably want to update the locking order to match Xen one. In that case, I am not sure if we need to take the domain lock in the code?

Quoted 12 lines

+ *   its->cmd_lock (mutex)
+ *     its->its_lock (mutex)

+ *       vgic_cpu->ap_list_lock
+ *         kvm->lpi_list_lock
+ *           vgic_irq->irq_lock
+ *
+ * If you need to take multiple locks, always take the upper lock first,
+ * then the lower ones, e.g. first take the its_lock, then the irq_lock.
+ * If you are already holding a lock and need to take a higher one, you
+ * have to drop the lower ranking lock first and re-aquire it after having

s/re-aquite/acquire/

Quoted 6 lines

+ * taken the upper one.
+ *
+ * When taking more than one ap_list_lock at the same time, always take the
+ * lowest numbered VCPU's ap_list_lock first, so:
+ *   vcpuX->vcpu_id < vcpuY->vcpu_id:

Quoted 8 lines

+ *     spin_lock(vcpuX->arch.vgic_cpu.ap_list_lock);
+ *     spin_lock(vcpuY->arch.vgic_cpu.ap_list_lock);
+ *
+ * Since the VGIC must support injecting virtual interrupts from ISRs, we have
+ * to use the spin_lock_irqsave/spin_unlock_irqrestore versions of outer
+ * spinlocks for any lock that may be taken while injecting an interrupt.

It is quite nice to see the locking explained in the file and in general a lot of explanation within the code :).

Quoted 21 lines

+ */
+
  /*
   * Iterate over the VM's list of mapped LPIs to find the one with a
   * matching interrupt ID and return a reference to the IRQ structure.

xen/arch/arm/vgic/vgic.c +0 -0
@@ -97,6 +123,204 @@ void vgic_put_irq(struct domain *d, struct
 vgic_irq *irq)
       xfree(irq);
   }

  +/**
+ * vgic_target_oracle - compute the target vcpu for an irq
+ *
+ * @irq:    The irq to route. Must be already locked.
+ *
+ * Based on the current state of the interrupt (enabled, pending,
+ * active, vcpu and target_vcpu), compute the next vcpu this should be
+ * given to. Return NULL if this shouldn't be injected at all.
+ *
+ * Requires the IRQ lock to be held.
+ */
+static struct vcpu *vgic_target_oracle(struct vgic_irq *irq)

Quoted 8 lines

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 143

Quoted 6 lines
xen/arch/arm/vgic/vgic.c +6 -0
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+
+    /* If the interrupt is active, it must stay on the current vcpu */
+    if ( irq->active )
+        return irq->vcpu ? : irq->target_vcpu;

I am not sure to understand why you check whether irq->vcpu is NULL. If the interrupt is active, then irq->vcpu should be NULL. Did I miss

^
not you mean?

Quoted 1 line

anything?

Not if it has been explicitly activated via ISACTIVER. This is not implemented in Xen at the moment, but would be in the future. So I like to keep this in.

Quoted 152 lines
Quoted 4 lines

+
+    /*
+     * If the IRQ is not active but enabled and pending, we should direct

Quoted 8 lines

+     * it to its configured target VCPU.
+     * If the distributor is disabled, pending interrupts shouldn't be +     * forwarded.
+     */
+    if ( irq->enabled && irq_is_pending(irq) )
+    {
+        if ( unlikely(irq->target_vcpu &&
+                 !irq->target_vcpu->domain->arch.vgic.enabled) )

The indentation looks wrong here.

Quoted 7 lines

+            return NULL;

xen/arch/arm/vgic/vgic.c +5 -0
+
+        return irq->target_vcpu;
+    }
+
+    /* If neither active nor pending and enabled, then this IRQ

should not

Comment style:

/*
 * ...

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 172

Quoted 13 lines

+     * be queued to any VCPU.
+     */
+    return NULL;

xen/arch/arm/vgic/vgic.c +4 -0
+}
+
+/*
+ * Only valid injection if changing level for level-triggered IRQs or

for a
+ * rising edge.
+ */

xen/arch/arm/vgic/vgic.c +3 -0
+static bool vgic_validate_injection(struct vgic_irq *irq, bool level)
+{
+    switch (irq->config)

switch ( ... )

Quoted 7 lines

+    {
+    case VGIC_CONFIG_LEVEL:
+        return irq->line_level != level;
+    case VGIC_CONFIG_EDGE:
+        return level;
+    }
+

I would add an ASSERT_UNREACHABLE().

Quoted 7 lines

+    return false;

xen/arch/arm/vgic/vgic.c +4 -0
+}
+
+/*
+ * Check whether an IRQ needs to (and can) be queued to a VCPU's ap

list.
+ * Do the queuing if necessary, taking the right locks in the right

Quoted 8 lines

order.
+ * Returns true when the IRQ was queued, false otherwise.
+ *
+ * Needs to be entered with the IRQ lock already held, but will return
+ * with all locks dropped.
+ */
+bool vgic_queue_irq_unlock(struct domain *d, struct vgic_irq *irq, +               unsigned long flags)

Indentation. Also same remark as from vgic_inject_irq. No-one seems to care about the return (even in KVM :)).

Quoted 22 lines
xen/arch/arm/vgic/vgic.c +22 -0
+{
+    struct vcpu *vcpu;
+    bool running;
+
+    ASSERT(spin_is_locked(&irq->irq_lock));
+
+retry:
+    vcpu = vgic_target_oracle(irq);
+    if ( irq->vcpu || !vcpu )
+    {
+        /*
+         * If this IRQ is already on a VCPU's ap_list, then it
+         * cannot be moved or modified and there is no more work for
+         * us to do.
+         *
+         * Otherwise, if the irq is not pending and enabled, it does
+         * not need to be inserted into an ap_list and there is also
+         * no more work for us to do.
+         */
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+
+        /*

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 224

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+         * We have to kick the VCPU here, because we could be
+         * queueing an edge-triggered interrupt for which we
+         * get no EOI maintenance interrupt. In that case,
+         * while the IRQ is already on the VCPU's AP list, the
+         * VCPU could have EOI'ed the original interrupt and
+         * won't see this one until it exits for some other
+         * reason.
+         */
+        if ( vcpu )
+            vcpu_unblock(vcpu);

vcpu_unblock will only "unblock" a vCPU that is blocked. It won't notify a running vCPU. So you want to have something similar to:

vcpu_unblock(vcpu);
if ( running && vcpu != current )
  smp_send_event_check_mask(...);

It is probably worth to introduce an helper for that.

Quoted 37 lines

+        return false;
+    }

xen/arch/arm/vgic/vgic.c +28 -0
+
+    /*
+     * We must unlock the irq lock to take the ap_list_lock where
+     * we are going to insert this new pending interrupt.
+     */
+    spin_unlock_irqrestore(&irq->irq_lock, flags);
+
+    /* someone can do stuff here, which we re-check below */
+
+    spin_lock_irqsave(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+    spin_lock(&irq->irq_lock);
+
+    /*
+     * Did something change behind our backs?
+     *
+     * There are two cases:
+     * 1) The irq lost its pending state or was disabled behind our
+     *    backs and/or it was queued to another VCPU's ap_list.
+     * 2) Someone changed the affinity on this irq behind our
+     *    backs and we are now holding the wrong ap_list_lock.
+     *
+     * In both cases, drop the locks and retry.
+     */
+
+    if ( unlikely(irq->vcpu || vcpu != vgic_target_oracle(irq)) )
+    {
+        spin_unlock(&irq->irq_lock);
+        spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock,

flags);

xen/arch/arm/vgic/vgic.c +6 -0
+
+        spin_lock_irqsave(&irq->irq_lock, flags);
+        goto retry;
+    }
+
+    /*
Quoted 8 lines
Quoted 5 lines

+     * Grab a reference to the irq to reflect the fact that it is +     * now in the ap_list.
+     */
+    vgic_get_irq_kref(irq);
+    list_add_tail(&irq->ap_list, &vcpu->arch.vgic_cpu.ap_list_head);

I was expecting the list to be sorted here. But you seem to do it only in vgic_flush_lr_state() which is quite interesting.

The list is not sorted on insertion because this is not necessary most of the time. In fact the hardware VGIC will do the sorting (kind of) within the LRs. So as long as we don't have more than <number of LRS> IRQs in the list, sorting is a waste of time. Experiments in the past showed that the number of used LRs is less than 4 almost every time. And since 4 is the mostly used number of implemented LRs, we virtually never need the sorting. So we avoid doing that on every insertion, instead doing that only if it's necessary.

Quoted 4 lines

I can foresee quite a few issues with this choice on Xen:
    1) You compute the size of ap list in vgic_flush_lr_state() and take lock on every IRQ one by one. A guest could be nasty and make that list quite big by make IRQs pending but never "active" them (i.e read IAR).

Yeah, we could try to shortcut a bit here.

Quoted 2 lines

    2) This might be an issue while checking whether you need to deliver an interrupt (vgic_vcpu_pending_irq) because the list is not sorted.

Most of the time the list is very short, storing one, two or actually no IRQs. In the function where we check for pending IRQs we bail out as soon as we found the first eligible interrupt. So sorting does not help in the majority of cases.

If you are really concerned about that list growing too long, we could think about mitigations:
1) Try to avoid iterating the whole list while checking whether it needs to be sorted.
2) Store a flag that notes if the list has already been sorted. As long as we don't change anything, we don't need to sort again. Would be good to test whether this is actually helpful. But we would need to keep this flag up-to-date, which sounds a bit fragile to get right.
3) Switch to sort-on-insertion once we reached a certain number of IRQs on the list, to mitigate DOS attacks from the guest. This should avoid list iterations in hot paths, with IRQs disabled.

But all of these sound a bit hackish to me and just would spoil the very clean and robust code we have today. Also I am not sure we can avoid list iterations every time (for instance in prune_ap_list()). There is an upper limit today (number of SPIs), so we might be happy with that for now.
To be honest I would very much dislike changing the code at this point. I believe a patch series afterwards would be better, also to actually have some numbers on the impact of this.

Cheers,
Andre.

Quoted 84 lines
Quoted 24 lines

+    irq->vcpu = vcpu;

xen/arch/arm/vgic/vgic.c +23 -0
+
+    spin_unlock(&irq->irq_lock);
+    spin_unlock_irqrestore(&vcpu->arch.vgic_cpu.ap_list_lock, flags);
+
+    running = vcpu->is_running;
+    vcpu_unblock(vcpu);
+    if ( running && vcpu != current )
+        smp_send_event_check_mask(cpumask_of(vcpu->processor));
+
+    return true;
+}
+
+/**
+ * vgic_inject_irq - Inject an IRQ from a device to the vgic
+ * @d:       The domain pointer
+ * @vcpu:    The vCPU for PPIs
+ * @intid:   The INTID to inject a new state to.
+ * @level:   Edge-triggered:  true:  to trigger the interrupt
+ *                false: to ignore the call
+ *       Level-sensitive  true:  raise the input signal
+ *                false: lower the input signal
+ *
+ * The VGIC is not concerned with devices being active-LOW or
Quoted 8 lines

active-HIGH for
+ * level-sensitive interrupts.  You can think of the level parameter as 1
+ * being HIGH and 0 being LOW and all devices being active-HIGH.
+ */
+int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid,
+            bool level)

Indentation.

Quoted 49 lines
xen/arch/arm/vgic/vgic.c +29 -0
+{
+    struct vgic_irq *irq;
+    unsigned long flags;
+
+    irq = vgic_get_irq(d, vcpu, intid);
+    if ( !irq )
+        return -EINVAL;
+
+    spin_lock_irqsave(&irq->irq_lock, flags);
+
+    if ( !vgic_validate_injection(irq, level) )
+    {
+        /* Nothing to see here, move along... */
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+        vgic_put_irq(d, irq);
+        return 0;
+    }
+
+    if ( irq->config == VGIC_CONFIG_LEVEL )
+        irq->line_level = level;
+    else
+        irq->pending_latch = true;
+
+    vgic_queue_irq_unlock(d, irq, flags);
+    vgic_put_irq(d, irq);
+
+    return 0;
+}
+

  /*
   * Local variables:
   * mode: C

xen/arch/arm/vgic/vgic.h +7 -0
index 7a15cfdd79..5127739f0f 100644
@@ -17,9 +17,19 @@
   #ifndef __XEN_ARM_VGIC_NEW_H__
   #define __XEN_ARM_VGIC_NEW_H__
   +static inline bool irq_is_pending(struct vgic_irq *irq)
+{
+    if ( irq->config == VGIC_CONFIG_EDGE )
+        return irq->pending_latch;
+    else
+        return irq->pending_latch || irq->line_level;
+}
+
   struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
                                 u32 intid);
Quoted 9 lines

xen/arch/arm/vgic/vgic.h

Quoted 6 lines
xen/arch/arm/vgic/vgic.h +2 -0
   void vgic_put_irq(struct domain *d, struct vgic_irq *irq);
+bool vgic_queue_irq_unlock(struct domain *d, struct vgic_irq *irq,
+               unsigned long flags);
     static inline void vgic_get_irq_kref(struct vgic_irq *irq)
   {
 

Cheers,

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

#53 lore raw

Re: [RFC PATCH 22/49] ARM: new VGIC: Implement virtual IRQ injection

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 29 lines · Julien Grall
Quoted 20 lines · Andre Przywara
Quoted 19 lines

  /*
   * Iterate over the VM's list of mapped LPIs to find the one with a
   * matching interrupt ID and return a reference to the IRQ structure.

xen/arch/arm/vgic/vgic.c +11 -0
@@ -97,6 +123,204 @@ void vgic_put_irq(struct domain *d, struct
 vgic_irq *irq)
       xfree(irq);
   }
   +/**
+ * vgic_target_oracle - compute the target vcpu for an irq
+ *
+ * @irq:    The irq to route. Must be already locked.
+ *
+ * Based on the current state of the interrupt (enabled, pending,
+ * active, vcpu and target_vcpu), compute the next vcpu this should be
+ * given to. Return NULL if this shouldn't be injected at all.
+ *
+ * Requires the IRQ lock to be held.
+ */
+static struct vcpu *vgic_target_oracle(struct vgic_irq *irq)
Quoted 8 lines

xen/arch/arm/vgic/vgic.c void vgic_put_irq(struct domain *d, struct vgic_irq *irq) · line 143

Quoted 6 lines
xen/arch/arm/vgic/vgic.c +6 -0
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+
+    /* If the interrupt is active, it must stay on the current vcpu */
+    if ( irq->active )
+        return irq->vcpu ? : irq->target_vcpu;

I am not sure to understand why you check whether irq->vcpu is NULL. If the interrupt is active, then irq->vcpu should be NULL. Did I miss

Quoted 2 lines

^
not you mean?

Yes not NULL.

Quoted 5 lines
Quoted 1 line

anything?

Not if it has been explicitly activated via ISACTIVER. This is not implemented in Xen at the moment, but would be in the future. So I like to keep this in.

Oh, I missed that case. Thank you for the explanation :).

[...]

Quoted 17 lines

The list is not sorted on insertion because this is not necessary most of the time. In fact the hardware VGIC will do the sorting (kind of) within the LRs. So as long as we don't have more than <number of LRS> IRQs in the list, sorting is a waste of time. Experiments in the past showed that the number of used LRs is less than 4 almost every time. And since 4 is the mostly used number of implemented LRs, we virtually never need the sorting. So we avoid doing that on every insertion, instead doing that only if it's necessary.

Quoted 4 lines

I can foresee quite a few issues with this choice on Xen:
    1) You compute the size of ap list in vgic_flush_lr_state() and take
lock on every IRQ one by one. A guest could be nasty and make that list quite big by make IRQs pending but never "active" them (i.e read IAR).

Yeah, we could try to shortcut a bit here.

Quoted 7 lines
Quoted 2 lines

    2) This might be an issue while checking whether you need to deliver
an interrupt (vgic_vcpu_pending_irq) because the list is not sorted.

Most of the time the list is very short, storing one, two or actually no IRQs. In the function where we check for pending IRQs we bail out as soon as we found the first eligible interrupt. So sorting does not help in the majority of cases.

As you say "most of the time". Malicious guest are unusual but just enough to keep busy both the hypervisor and the security team.

Quoted 13 lines

If you are really concerned about that list growing too long, we could think about mitigations:
1) Try to avoid iterating the whole list while checking whether it needs to be sorted.
2) Store a flag that notes if the list has already been sorted. As long as we don't change anything, we don't need to sort again. Would be good to test whether this is actually helpful. But we would need to keep this flag up-to-date, which sounds a bit fragile to get right.
3) Switch to sort-on-insertion once we reached a certain number of IRQs on the list, to mitigate DOS attacks from the guest. This should avoid list iterations in hot paths, with IRQs disabled.

Quoted 8 lines

But all of these sound a bit hackish to me and just would spoil the very clean and robust code we have today. Also I am not sure we can avoid list iterations every time (for instance in prune_ap_list()). There is an upper limit today (number of SPIs), so we might be happy with that for now.
To be honest I would very much dislike changing the code at this point. I believe a patch series afterwards would be better, also to actually have some numbers on the impact of this.

More robust than the current vGIC yes. However, clean code is not an excuse to dismiss valid (but unusual) use case. So even if I quite like the new vGIC, I really don't want to deal with yet another security.

Thankfully passthrough case is not currently security supported (see SUPPORT.md). So we probably can defer, although I would like to keep track of known pitfalls of the new vGIC.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 23/49 ARM: new VGIC: Add IRQ sorting

In reply to Andre Przywara

Adds the sorting function to cover the case where you have more IRQs to consider than you have LRs. We consider their priorities. This pulls in Linux' list_sort.c , which is a merge sort implementation for linked lists.

This is based on Linux commit 8e4447457965, written by Christoffer Dall.

---
 xen/arch/arm/vgic/vgic.c    |  59 +++++++++++++++
 xen/common/list_sort.c      | 170 ++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/list_sort.h |  11 +++
 3 files changed, 240 insertions(+)
 create mode 100644 xen/common/list_sort.c
 create mode 100644 xen/include/xen/list_sort.h
xen/arch/arm/vgic/vgic.c +59 -0
index f517df6d00..a4efd1fd03 100644
@@ -16,6 +16,7 @@
  */
 
 #include <asm/bug.h>
+#include <xen/list_sort.h>
 #include <xen/sched.h>
 
 #include <asm/arm_vgic.h>
@@ -163,6 +164,64 @@ static struct vcpu *vgic_target_oracle(struct vgic_irq *irq)
     return NULL;
 }
 
+/*
+ * The order of items in the ap_lists defines how we'll pack things in LRs as
+ * well, the first items in the list being the first things populated in the
+ * LRs.
+ *
+ * A hard rule is that active interrupts can never be pushed out of the LRs
+ * (and therefore take priority) since we cannot reliably trap on deactivation
+ * of IRQs and therefore they have to be present in the LRs.
+ *
+ * Otherwise things should be sorted by the priority field and the GIC
+ * hardware support will take care of preemption of priority groups etc.
+ *
+ * Return negative if "a" sorts before "b", 0 to preserve order, and positive
+ * to sort "b" before "a".
+ */
+static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
+{
+    struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
+    struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
+    bool penda, pendb;
+    int ret;
+
+    spin_lock(&irqa->irq_lock);
+    spin_lock(&irqb->irq_lock);
+
+    if ( irqa->active || irqb->active )
+    {
+        ret = (int)irqb->active - (int)irqa->active;
+        goto out;
+    }
+
+    penda = irqa->enabled && irq_is_pending(irqa);
+    pendb = irqb->enabled && irq_is_pending(irqb);
+
+    if ( !penda || !pendb )
+    {
+        ret = (int)pendb - (int)penda;
+        goto out;
+    }
+
+    /* Both pending and enabled, sort by priority */
+    ret = irqa->priority - irqb->priority;
+out:
+    spin_unlock(&irqb->irq_lock);
+    spin_unlock(&irqa->irq_lock);
+    return ret;
+}
+
+/* Must be called with the ap_list_lock held */
+static void vgic_sort_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
+}
+
 /*
  * Only valid injection if changing level for level-triggered IRQs or for a
  * rising edge.
xen/common/list_sort.c new file +170 -0

170 lines not on this page · raw message

xen/include/xen/list_sort.h new file +11 -0
new file mode 100644
index 0000000000..a60c589d4b
@@ -0,0 +1,11 @@
+#ifndef _LINUX_LIST_SORT_H
+#define _LINUX_LIST_SORT_H
+
+#include <xen/types.h>
+
+struct list_head;
+
+void list_sort(void *priv, struct list_head *head,
+               int (*cmp)(void *priv, struct list_head *a,
+                          struct list_head *b));
+#endif
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 23/49] ARM: new VGIC: Add IRQ sorting

Hi Andre,

Quoted 5 lines · Andre Przywara

Adds the sorting function to cover the case where you have more IRQs to consider than you have LRs. We consider their priorities. This pulls in Linux' list_sort.c , which is a merge sort implementation for linked lists.

Quoted 7 lines

This is based on Linux commit 8e4447457965, written by Christoffer Dall.

---
  xen/arch/arm/vgic/vgic.c    |  59 +++++++++++++++
  xen/common/list_sort.c      | 170 ++++++++++++++++++++++++++++++++++++++++++++
  xen/include/xen/list_sort.h |  11 +++

You need to CC "THE REST" maintainers for this code. It would also make sense to have a separate patch for adding list_sort.c

Quoted 24 lines
  3 files changed, 240 insertions(+)

create mode 100644 xen/common/list_sort.c
create mode 100644 xen/include/xen/list_sort.h

xen/arch/arm/vgic/vgic.c +5 -0
index f517df6d00..a4efd1fd03 100644
@@ -16,6 +16,7 @@
   */
 
  #include <asm/bug.h>
+#include <xen/list_sort.h>
  #include <xen/sched.h>
 
  #include <asm/arm_vgic.h>
@@ -163,6 +164,64 @@ static struct vcpu *vgic_target_oracle(struct vgic_irq *irq)
      return NULL;
  }
 
+/*
+ * The order of items in the ap_lists defines how we'll pack things in LRs as
+ * well, the first items in the list being the first things populated in the
+ * LRs.

xen/arch/arm/vgic/vgic.c static struct vcpu *vgic_target_oracle(struct vgic_irq *irq) · line 180

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+ *
+ * A hard rule is that active interrupts can never be pushed out of the LRs
+ * (and therefore take priority) since we cannot reliably trap on deactivation
+ * of IRQs and therefore they have to be present in the LRs.
+ *
+ * Otherwise things should be sorted by the priority field and the GIC
+ * hardware support will take care of preemption of priority groups etc.
+ *
+ * Return negative if "a" sorts before "b", 0 to preserve order, and positive
+ * to sort "b" before "a".

Finally a good explanation of the return value of a sort function :). I always get confused what the return is supposed to be.

xen/arch/arm/vgic/vgic.c static struct vcpu *vgic_target_oracle(struct vgic_irq *irq) · line 190

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+ */
+static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)
+{
+    struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
+    struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
+    bool penda, pendb;
+    int ret;
+
+    spin_lock(&irqa->irq_lock);
+    spin_lock(&irqb->irq_lock);

I guess the locking order does not matter here because this is the only place where two IRQs lock have to be taken?

Also, this will be done with irq disabled right? In that case, may I ask for an ASSERT(!local_irq_is_enabled())? Or maybe in vgic_sort_ap_list.

Quoted 43 lines
xen/arch/arm/vgic/vgic.c +34 -0
+
+    if ( irqa->active || irqb->active )
+    {
+        ret = (int)irqb->active - (int)irqa->active;
+        goto out;
+    }
+
+    penda = irqa->enabled && irq_is_pending(irqa);
+    pendb = irqb->enabled && irq_is_pending(irqb);
+
+    if ( !penda || !pendb )
+    {
+        ret = (int)pendb - (int)penda;
+        goto out;
+    }
+
+    /* Both pending and enabled, sort by priority */
+    ret = irqa->priority - irqb->priority;
+out:
+    spin_unlock(&irqb->irq_lock);
+    spin_unlock(&irqa->irq_lock);
+    return ret;
+}
+
+/* Must be called with the ap_list_lock held */
+static void vgic_sort_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
+}
+
  /*
   * Only valid injection if changing level for level-triggered IRQs or for a
   * rising edge.
xen/common/list_sort.c new file +0 -0
new file mode 100644
index 0000000000..9c5cc58e43

xen/common/list_sort.c line 4

Quoted 4 lines
xen/common/list_sort.c new file +4 -0
@@ -0,0 +1,170 @@
+/*
+ * list_sort.c: merge sort implementation for linked lists
+ * Copied from the Linux kernel (lib/list_sort.c)
+ * (without specific copyright notice there)

I can see you moved from Linux to Xen coding style. Is there any other changes made?

Quoted 174 lines
xen/common/list_sort.c new file +166 -0
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <xen/lib.h>
+#include <xen/list.h>
+
+#define MAX_LIST_LENGTH_BITS 20
+
+/*
+ * Returns a list organized in an intermediate format suited
+ * to chaining of merge() calls: null-terminated, no reserved or
+ * sentinel head node, "prev" links not maintained.
+ */
+static struct list_head *merge(void *priv,
+                               int (*cmp)(void *priv, struct list_head *a,
+                                          struct list_head *b),
+                               struct list_head *a, struct list_head *b)
+{
+    struct list_head head, *tail = &head;
+
+    while ( a && b )
+    {
+        /* if equal, take 'a' -- important for sort stability */
+        if ( (*cmp)(priv, a, b) <= 0 )
+        {
+            tail->next = a;
+            a = a->next;
+        }
+        else
+        {
+            tail->next = b;
+            b = b->next;
+        }
+        tail = tail->next;
+    }
+    tail->next = a?:b;
+    return head.next;
+}
+
+/*
+ * Combine final list merge with restoration of standard doubly-linked
+ * list structure.  This approach duplicates code from merge(), but
+ * runs faster than the tidier alternatives of either a separate final
+ * prev-link restoration pass, or maintaining the prev links
+ * throughout.
+ */
+static void merge_and_restore_back_links(void *priv,
+                                         int (*cmp)(void *priv,
+                                                    struct list_head *a,
+                                                    struct list_head *b),
+                                         struct list_head *head,
+                                         struct list_head *a,
+                                         struct list_head *b)
+{
+    struct list_head *tail = head;
+    u8 count = 0;
+
+    while ( a && b )
+    {
+        /* if equal, take 'a' -- important for sort stability */
+        if ( (*cmp)(priv, a, b) <= 0 )
+        {
+            tail->next = a;
+            a->prev = tail;
+            a = a->next;
+        }
+        else
+        {
+            tail->next = b;
+            b->prev = tail;
+            b = b->next;
+        }
+        tail = tail->next;
+    }
+    tail->next = a ? : b;
+
+    do
+    {
+        /*
+         * In worst cases this loop may run many iterations.
+         * Continue callbacks to the client even though no
+         * element comparison is needed, so the client's cmp()
+         * routine can invoke cond_resched() periodically.
+         */
+        if ( unlikely(!(++count)) )
+            (*cmp)(priv, tail->next, tail->next);
+
+        tail->next->prev = tail;
+        tail = tail->next;
+    } while ( tail->next );
+
+    tail->next = head;
+    head->prev = tail;
+}
+
+/**
+ * list_sort - sort a list
+ * @priv: private data, opaque to list_sort(), passed to @cmp
+ * @head: the list to sort
+ * @cmp: the elements comparison function
+ *
+ * This function implements "merge sort", which has O(nlog(n))
+ * complexity.
+ *
+ * The comparison function @cmp must return a negative value if @a
+ * should sort before @b, and a positive value if @a should sort after
+ * @b. If @a and @b are equivalent, and their original relative
+ * ordering is to be preserved, @cmp must return 0.
+ */
+void list_sort(void *priv, struct list_head *head,
+               int (*cmp)(void *priv, struct list_head *a, struct list_head *b))
+{
+    struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
+                        -- last slot is a sentinel */
+    int lev;  /* index into part[] */
+    int max_lev = 0;
+    struct list_head *list;
+
+    if ( list_empty(head) )
+        return;
+
+    memset(part, 0, sizeof(part));
+
+    head->prev->next = NULL;
+    list = head->next;
+
+    while ( list )
+    {
+        struct list_head *cur = list;
+        list = list->next;
+        cur->next = NULL;
+
+        for ( lev = 0; part[lev]; lev++ )
+        {
+            cur = merge(priv, cmp, part[lev], cur);
+            part[lev] = NULL;
+        }
+        if ( lev > max_lev )
+        {
+            if ( unlikely(lev >= ARRAY_SIZE(part)-1) )
+            {
+                dprintk(XENLOG_DEBUG, "list too long for efficiency\n");
+                lev--;
+            }
+            max_lev = lev;
+        }
+        part[lev] = cur;
+    }
+
+    for ( lev = 0; lev < max_lev; lev++ )
+        if ( part[lev] )
+            list = merge(priv, cmp, part[lev], list);
+
+    merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
+}
+EXPORT_SYMBOL(list_sort);
xen/include/xen/list_sort.h new file +1 -0
new file mode 100644
index 0000000000..a60c589d4b
@@ -0,0 +1,11 @@
+#ifndef _LINUX_LIST_SORT_H

xen/include/xen/list_sort.h line 11

Quoted 10 lines
xen/include/xen/list_sort.h new file +10 -0
+#define _LINUX_LIST_SORT_H
+
+#include <xen/types.h>
+
+struct list_head;
+
+void list_sort(void *priv, struct list_head *head,
+               int (*cmp)(void *priv, struct list_head *a,
+                          struct list_head *b));
+#endif

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 23/49] ARM: new VGIC: Add IRQ sorting

Hi,

Christoffer, Eric, Marc,
a question about locking order between multiple IRQs below. Could you have a brief look, please?

Quoted 17 lines · Julien Grall

Hi Andre,

Quoted 5 lines · Andre Przywara

Adds the sorting function to cover the case where you have more IRQs to consider than you have LRs. We consider their priorities. This pulls in Linux' list_sort.c , which is a merge sort implementation for linked lists.

Quoted 8 lines

This is based on Linux commit 8e4447457965, written by Christoffer Dall.

---
  xen/arch/arm/vgic/vgic.c    |  59 +++++++++++++++ xen/common/list_sort.c      | 170
++++++++++++++++++++++++++++++++++++++++++++
  xen/include/xen/list_sort.h |  11 +++

Quoted 2 lines

You need to CC "THE REST" maintainers for this code. It would also make sense to have a separate patch for adding list_sort.c

Yeah, will do.

Quoted 41 lines
Quoted 29 lines
  3 files changed, 240 insertions(+)

  create mode 100644 xen/common/list_sort.c
  create mode 100644 xen/include/xen/list_sort.h

xen/arch/arm/vgic/vgic.c +1 -0
index f517df6d00..a4efd1fd03 100644
@@ -16,6 +16,7 @@
    */
     #include <asm/bug.h>
+#include <xen/list_sort.h>
   #include <xen/sched.h>
     #include <asm/arm_vgic.h>
@@ -163,6 +164,64 @@ static struct vcpu *vgic_target_oracle(struct
 vgic_irq *irq)
       return NULL;
   }

  +/*
+ * The order of items in the ap_lists defines how we'll pack things in LRs as
+ * well, the first items in the list being the first things populated in the
+ * LRs.
+ *
+ * A hard rule is that active interrupts can never be pushed out of the LRs
+ * (and therefore take priority) since we cannot reliably trap on deactivation

Quoted 8 lines

+ * of IRQs and therefore they have to be present in the LRs.
+ *
+ * Otherwise things should be sorted by the priority field and the GIC
+ * hardware support will take care of preemption of priority groups etc.
+ *
+ * Return negative if "a" sorts before "b", 0 to preserve order, and positive
+ * to sort "b" before "a".

Finally a good explanation of the return value of a sort function :). I always get confused what the return is supposed to be.

Quoted 14 lines

xen/arch/arm/vgic/vgic.c static struct vcpu *vgic_target_oracle(struct vgic_irq *irq) · line 190

Quoted 11 lines

+ */
+static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)

xen/arch/arm/vgic/vgic.c +8 -0
+{
+    struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
+    struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
+    bool penda, pendb;
+    int ret;
+
+    spin_lock(&irqa->irq_lock);
+    spin_lock(&irqb->irq_lock);

I guess the locking order does not matter here because this is the only place where two IRQs lock have to be taken?

Mmh, good question. I guess indeed in practice this will not be a problem:
- As you mentioned this should be the only(?) place where we take multiple IRQ locks, but that sounds fragile.
- A certain IRQ should only be on one VCPU list at a given point in time. So there would be no race with two instances of this compare function trying to lock the same IRQ.

But that sounds a bit dodgy to rely on. It should be relatively straight forward to fix this with a simple comparison, shouldn't it? CC:ing Christoffer, Marc and Eric here to see if we should add this (in KVM as well).

Quoted 2 lines

Also, this will be done with irq disabled right? In that case, may I ask for an ASSERT(!local_irq_is_enabled())? Or maybe in vgic_sort_ap_list.

OK.

Quoted 43 lines
Quoted 43 lines
xen/arch/arm/vgic/vgic.c +34 -0
+
+    if ( irqa->active || irqb->active )
+    {
+        ret = (int)irqb->active - (int)irqa->active;
+        goto out;
+    }
+
+    penda = irqa->enabled && irq_is_pending(irqa);
+    pendb = irqb->enabled && irq_is_pending(irqb);
+
+    if ( !penda || !pendb )
+    {
+        ret = (int)pendb - (int)penda;
+        goto out;
+    }
+
+    /* Both pending and enabled, sort by priority */
+    ret = irqa->priority - irqb->priority;
+out:
+    spin_unlock(&irqb->irq_lock);
+    spin_unlock(&irqa->irq_lock);
+    return ret;
+}
+
+/* Must be called with the ap_list_lock held */
+static void vgic_sort_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    list_sort(NULL, &vgic_cpu->ap_list_head, vgic_irq_cmp);
+}
+

  /*
   * Only valid injection if changing level for level-triggered IRQs or for a
   * rising edge.

xen/common/list_sort.c new file +0 -0
new file mode 100644
index 0000000000..9c5cc58e43
Quoted 8 lines

xen/common/list_sort.c line 4

Quoted 5 lines
xen/common/list_sort.c new file +4 -0
@@ -0,0 +1,170 @@
+/*
+ * list_sort.c: merge sort implementation for linked lists
+ * Copied from the Linux kernel (lib/list_sort.c)
+ * (without specific copyright notice there)

I can see you moved from Linux to Xen coding style. Is there any other changes made?

Just the list of include files, but I didn't touch any actual code. Will mention this in the commit message for this separate patch.

Cheers,
Andre.

Quoted 187 lines
Quoted 187 lines
xen/common/list_sort.c new file +2 -0
+ *
+ * This program is free software; you can redistribute it and/or

modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; If not, see <http://www.gnu.org/licenses/>.
+ */

xen/common/list_sort.c new file +12 -0
+#include <xen/lib.h>
+#include <xen/list.h>
+
+#define MAX_LIST_LENGTH_BITS 20
+
+/*
+ * Returns a list organized in an intermediate format suited
+ * to chaining of merge() calls: null-terminated, no reserved or
+ * sentinel head node, "prev" links not maintained.
+ */
+static struct list_head *merge(void *priv,
+                               int (*cmp)(void *priv, struct

list_head *a,
+                                          struct list_head *b), +                               struct list_head *a, struct list_head *b)

xen/common/list_sort.c new file +33 -0
+{
+    struct list_head head, *tail = &head;
+
+    while ( a && b )
+    {
+        /* if equal, take 'a' -- important for sort stability */
+        if ( (*cmp)(priv, a, b) <= 0 )
+        {
+            tail->next = a;
+            a = a->next;
+        }
+        else
+        {
+            tail->next = b;
+            b = b->next;
+        }
+        tail = tail->next;
+    }
+    tail->next = a?:b;
+    return head.next;
+}
+
+/*
+ * Combine final list merge with restoration of standard doubly-linked
+ * list structure.  This approach duplicates code from merge(), but
+ * runs faster than the tidier alternatives of either a separate final
+ * prev-link restoration pass, or maintaining the prev links
+ * throughout.
+ */
+static void merge_and_restore_back_links(void *priv,
+                                         int (*cmp)(void *priv,
+                                                    struct list_head *a,
+                                                    struct list_head

*b),
+                                         struct list_head *head, +                                         struct list_head *a, +                                         struct list_head *b)

xen/common/list_sort.c new file +58 -0
+{
+    struct list_head *tail = head;
+    u8 count = 0;
+
+    while ( a && b )
+    {
+        /* if equal, take 'a' -- important for sort stability */
+        if ( (*cmp)(priv, a, b) <= 0 )
+        {
+            tail->next = a;
+            a->prev = tail;
+            a = a->next;
+        }
+        else
+        {
+            tail->next = b;
+            b->prev = tail;
+            b = b->next;
+        }
+        tail = tail->next;
+    }
+    tail->next = a ? : b;
+
+    do
+    {
+        /*
+         * In worst cases this loop may run many iterations.
+         * Continue callbacks to the client even though no
+         * element comparison is needed, so the client's cmp()
+         * routine can invoke cond_resched() periodically.
+         */
+        if ( unlikely(!(++count)) )
+            (*cmp)(priv, tail->next, tail->next);
+
+        tail->next->prev = tail;
+        tail = tail->next;
+    } while ( tail->next );
+
+    tail->next = head;
+    head->prev = tail;
+}
+
+/**
+ * list_sort - sort a list
+ * @priv: private data, opaque to list_sort(), passed to @cmp
+ * @head: the list to sort
+ * @cmp: the elements comparison function
+ *
+ * This function implements "merge sort", which has O(nlog(n))
+ * complexity.
+ *
+ * The comparison function @cmp must return a negative value if @a
+ * should sort before @b, and a positive value if @a should sort after
+ * @b. If @a and @b are equivalent, and their original relative
+ * ordering is to be preserved, @cmp must return 0.
+ */
+void list_sort(void *priv, struct list_head *head,
+               int (*cmp)(void *priv, struct list_head *a, struct

list_head *b))
+{
+    struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
+                        -- last slot is a sentinel */
+    int lev;  /* index into part[] */
+    int max_lev = 0;
+    struct list_head *list;

xen/common/list_sort.c new file +39 -0
+
+    if ( list_empty(head) )
+        return;
+
+    memset(part, 0, sizeof(part));
+
+    head->prev->next = NULL;
+    list = head->next;
+
+    while ( list )
+    {
+        struct list_head *cur = list;
+        list = list->next;
+        cur->next = NULL;
+
+        for ( lev = 0; part[lev]; lev++ )
+        {
+            cur = merge(priv, cmp, part[lev], cur);
+            part[lev] = NULL;
+        }
+        if ( lev > max_lev )
+        {
+            if ( unlikely(lev >= ARRAY_SIZE(part)-1) )
+            {
+                dprintk(XENLOG_DEBUG, "list too long for efficiency\n");
+                lev--;
+            }
+            max_lev = lev;
+        }
+        part[lev] = cur;
+    }
+
+    for ( lev = 0; lev < max_lev; lev++ )
+        if ( part[lev] )
+            list = merge(priv, cmp, part[lev], list);
+
+    merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
+}
+EXPORT_SYMBOL(list_sort);
xen/include/xen/list_sort.h new file +6 -0
new file mode 100644
index 0000000000..a60c589d4b
@@ -0,0 +1,11 @@
+#ifndef _LINUX_LIST_SORT_H
+#define _LINUX_LIST_SORT_H
+
+#include <xen/types.h>
+
+struct list_head;
Quoted 9 lines

xen/include/xen/list_sort.h line 11

Quoted 6 lines
xen/include/xen/list_sort.h new file +5 -0
+
+void list_sort(void *priv, struct list_head *head,
+               int (*cmp)(void *priv, struct list_head *a,
+                          struct list_head *b));
+#endif

Cheers,

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

#57 lore raw

Re: [RFC PATCH 23/49] ARM: new VGIC: Add IRQ sorting

Quoted 14 lines · Andre Przywara
Quoted 5 lines
Quoted 5 lines
xen/common/list_sort.c new file +0 -0
new file mode 100644
index 0000000000..9c5cc58e43
Quoted 8 lines

xen/common/list_sort.c line 4

Quoted 5 lines
xen/common/list_sort.c new file +4 -0
@@ -0,0 +1,170 @@
+/*
+ * list_sort.c: merge sort implementation for linked lists
+ * Copied from the Linux kernel (lib/list_sort.c)
+ * (without specific copyright notice there)

I can see you moved from Linux to Xen coding style. Is there any other changes made?

Quoted 2 lines

Just the list of include files, but I didn't touch any actual code. Will mention this in the commit message for this separate patch.

Can you keep the list coding style in that case please?

Thank you.

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 23/49] ARM: new VGIC: Add IRQ sorting

In reply to Andre Przywara
Quoted 92 lines · Andre Przywara

Hi,

Christoffer, Eric, Marc,
a question about locking order between multiple IRQs below. Could you have a brief look, please?

Quoted 17 lines · Julien Grall

Hi Andre,

Quoted 5 lines · Andre Przywara

Adds the sorting function to cover the case where you have more IRQs to consider than you have LRs. We consider their priorities. This pulls in Linux' list_sort.c , which is a merge sort implementation for linked lists.

Quoted 8 lines

This is based on Linux commit 8e4447457965, written by Christoffer Dall.

---
  xen/arch/arm/vgic/vgic.c    |  59 +++++++++++++++
  xen/common/list_sort.c      | 170
++++++++++++++++++++++++++++++++++++++++++++
  xen/include/xen/list_sort.h |  11 +++
Quoted 2 lines

You need to CC "THE REST" maintainers for this code. It would also make sense to have a separate patch for adding list_sort.c

Yeah, will do.

Quoted 41 lines
Quoted 29 lines
  3 files changed, 240 insertions(+)

create mode 100644 xen/common/list_sort.c
create mode 100644 xen/include/xen/list_sort.h

xen/arch/arm/vgic/vgic.c +2 -0
index f517df6d00..a4efd1fd03 100644
@@ -16,6 +16,7 @@
   */
    #include <asm/bug.h>
+#include <xen/list_sort.h>
  #include <xen/sched.h>
    #include <asm/arm_vgic.h>
@@ -163,6 +164,64 @@ static struct vcpu *vgic_target_oracle(struct
 vgic_irq *irq)
      return NULL;
  }
  +/*
+ * The order of items in the ap_lists defines how we'll pack things

in LRs as
+ * well, the first items in the list being the first things populated in the
+ * LRs.
+ *
+ * A hard rule is that active interrupts can never be pushed out of the LRs
+ * (and therefore take priority) since we cannot reliably trap on deactivation

Quoted 8 lines

+ * of IRQs and therefore they have to be present in the LRs.
+ *
+ * Otherwise things should be sorted by the priority field and the GIC
+ * hardware support will take care of preemption of priority groups etc.
+ *
+ * Return negative if "a" sorts before "b", 0 to preserve order, and positive
+ * to sort "b" before "a".

Finally a good explanation of the return value of a sort function :). I always get confused what the return is supposed to be.

Quoted 14 lines

xen/arch/arm/vgic/vgic.c static struct vcpu *vgic_target_oracle(struct vgic_irq *irq) · line 190

Quoted 11 lines

+ */
+static int vgic_irq_cmp(void *priv, struct list_head *a, struct list_head *b)

xen/arch/arm/vgic/vgic.c +8 -0
+{
+    struct vgic_irq *irqa = container_of(a, struct vgic_irq, ap_list);
+    struct vgic_irq *irqb = container_of(b, struct vgic_irq, ap_list);
+    bool penda, pendb;
+    int ret;
+
+    spin_lock(&irqa->irq_lock);
+    spin_lock(&irqb->irq_lock);

I guess the locking order does not matter here because this is the only place where two IRQs lock have to be taken?

Mmh, good question. I guess indeed in practice this will not be a problem:
- As you mentioned this should be the only(?) place where we take multiple IRQ locks, but that sounds fragile.
- A certain IRQ should only be on one VCPU list at a given point in time. So there would be no race with two instances of this compare function trying to lock the same IRQ.

Quoted 5 lines

But that sounds a bit dodgy to rely on. It should be relatively straight forward to fix this with a simple comparison, shouldn't it? CC:ing Christoffer, Marc and Eric here to see if we should add this (in KVM as well).

The only concern about holding two locks at the same time is the risk of another thread attempting to hold a number of locks at the same time in a different order, leading to a deadlock (either directly or via a circular dependency).

As you point out, the only place where we take two irq locks at the same time is in vgic_irq_cmp(). Now, the concern can be reduced to calling this function more than once in parallel, operating on the same set of struct irqs.

An IRQ can only be on a single AP list at any time, and we call vgic_irq_cmp() from exactly one place in the KVM code, which holds the ap_list_lock, and our locking order defines that the ap_list_lock must be taken before irq locks. This means that vgic_irq_cmp() can only execute in parallel on different AP lists and therefore not operate on the same set of struct irqs.

There is no need to change anything in the implementation.

Thanks,
-Christoffer

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

#59 lore raw

RFC PATCH 24/49 ARM: new VGIC: Add IRQ sync/flush framework

In reply to Andre Przywara

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

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

---
 xen/arch/arm/vgic/vgic.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.h |   2 +
 2 files changed, 248 insertions(+)
xen/arch/arm/vgic/vgic.c +246 -0
index a4efd1fd03..a1f77130d4 100644
@@ -380,6 +380,252 @@ int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid,
     return 0;
 }
 
+/**
+ * vgic_prune_ap_list - Remove non-relevant interrupts from the list
+ *
+ * @vcpu: The VCPU pointer
+ *
+ * Go over the list of "interesting" interrupts, and prune those that we
+ * won't have to consider in the near future.
+ */
+static void vgic_prune_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq, *tmp;
+    unsigned long flags;
+
+retry:
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry_safe( irq, tmp, &vgic_cpu->ap_list_head, ap_list )
+    {
+        struct vcpu *target_vcpu, *vcpuA, *vcpuB;
+
+        spin_lock(&irq->irq_lock);
+
+        BUG_ON(vcpu != irq->vcpu);
+
+        target_vcpu = vgic_target_oracle(irq);
+
+        if ( !target_vcpu )
+        {
+            /*
+             * We don't need to process this interrupt any
+             * further, move it off the list.
+             */
+            list_del(&irq->ap_list);
+            irq->vcpu = NULL;
+            spin_unlock(&irq->irq_lock);
+
+            /*
+             * This vgic_put_irq call matches the
+             * vgic_get_irq_kref in vgic_queue_irq_unlock,
+             * where we added the LPI to the ap_list. As
+             * we remove the irq from the list, we drop
+             * also drop the refcount.
+             */
+            vgic_put_irq(vcpu->domain, irq);
+            continue;
+        }
+
+        if ( target_vcpu == vcpu )
+        {
+            /* We're on the right CPU */
+            spin_unlock(&irq->irq_lock);
+            continue;
+        }
+
+        /* This interrupt looks like it has to be migrated. */
+
+        spin_unlock(&irq->irq_lock);
+        spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+
+        /*
+         * Ensure locking order by always locking the smallest
+         * ID first.
+         */
+        if ( vcpu->vcpu_id < target_vcpu->vcpu_id )
+        {
+            vcpuA = vcpu;
+            vcpuB = target_vcpu;
+        }
+        else
+        {
+            vcpuA = target_vcpu;
+            vcpuB = vcpu;
+        }
+
+        spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
+        spin_lock(&vcpuB->arch.vgic_cpu.ap_list_lock);
+        spin_lock(&irq->irq_lock);
+
+        /*
+         * If the affinity has been preserved, move the
+         * interrupt around. Otherwise, it means things have
+         * changed while the interrupt was unlocked, and we
+         * need to replay this.
+         *
+         * In all cases, we cannot trust the list not to have
+         * changed, so we restart from the beginning.
+         */
+        if ( target_vcpu == vgic_target_oracle(irq) )
+        {
+            struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
+
+            list_del(&irq->ap_list);
+            irq->vcpu = target_vcpu;
+            list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
+        }
+
+        spin_unlock(&irq->irq_lock);
+        spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
+        spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
+        goto retry;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+}
+
+static inline void vgic_fold_lr_state(struct vcpu *vcpu)
+{
+}
+
+/* Requires the irq_lock to be held. */
+static inline void vgic_populate_lr(struct vcpu *vcpu,
+                                    struct vgic_irq *irq, int lr)
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+}
+
+static inline void vgic_clear_lr(struct vcpu *vcpu, int lr)
+{
+}
+
+static inline void vgic_set_underflow(struct vcpu *vcpu)
+{
+}
+
+/* Requires the ap_list_lock to be held. */
+static int compute_ap_list_depth(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        /* GICv2 SGIs can count for more than one... */
+        if ( vgic_irq_is_sgi(irq->intid) && irq->source )
+            count += hweight8(irq->source);
+        else
+            count++;
+        spin_unlock(&irq->irq_lock);
+    }
+    return count;
+}
+
+/* Requires the VCPU's ap_list_lock to be held. */
+static void vgic_flush_lr_state(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    if ( compute_ap_list_depth(vcpu) > gic_get_nr_lrs() )
+        vgic_sort_ap_list(vcpu);
+
+    list_for_each_entry( irq, &vgic_cpu->ap_list_head, ap_list )
+    {
+        spin_lock(&irq->irq_lock);
+
+        if ( unlikely(vgic_target_oracle(irq) != vcpu) )
+            goto next;
+
+        /*
+         * If we get an SGI with multiple sources, try to get
+         * them in all at once.
+         */
+        do
+        {
+            vgic_populate_lr(vcpu, irq, count++);
+        } while ( irq->source && count < gic_get_nr_lrs() );
+
+next:
+        spin_unlock(&irq->irq_lock);
+
+        if ( count == gic_get_nr_lrs() )
+        {
+            if ( !list_is_last(&irq->ap_list, &vgic_cpu->ap_list_head) )
+                vgic_set_underflow(vcpu);
+            break;
+        }
+    }
+
+    vcpu->arch.vgic_cpu.used_lrs = count;
+
+    /* Nuke remaining LRs */
+    for ( ; count < gic_get_nr_lrs(); count++)
+        vgic_clear_lr(vcpu, count);
+}
+
+/*
+ * gic_clear_lrs() - Update the VGIC state from hardware after a guest's run.
+ * @vcpu: the VCPU.
+ *
+ * Sync back the hardware VGIC state after the guest has run, into our
+ * VGIC emulation structures, It reads the LRs and updates the respective
+ * struct vgic_irq, taking level/edge into account.
+ * This is the high level function which takes care of the conditions,
+ * also bails out early if there were no interrupts queued.
+ * Was: kvm_vgic_sync_hwstate()
+ */
+void gic_clear_lrs(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+
+    /* An empty ap_list_head implies used_lrs == 0 */
+    if ( list_empty(&vcpu->arch.vgic_cpu.ap_list_head) )
+        return;
+
+    if ( vgic_cpu->used_lrs )
+        vgic_fold_lr_state(vcpu);
+    vgic_prune_ap_list(vcpu);
+}
+
+/*
+ * gic_inject() - flush the emulation state into the hardware on guest entry
+ *
+ * Before we enter a guest, we have to translate the virtual GIC state of a
+ * VCPU into the GIC virtualization hardware registers, namely the LRs.
+ * This is the high level function which takes care about the conditions
+ * and the locking, also bails out early if there are no interrupts queued.
+ * Was: kvm_vgic_flush_hwstate()
+ */
+void gic_inject(void)
+{
+    /*
+     * If there are no virtual interrupts active or pending for this
+     * VCPU, then there is no work to do and we can bail out without
+     * taking any lock.  There is a potential race with someone injecting
+     * interrupts to the VCPU, but it is a benign race as the VCPU will
+     * either observe the new interrupt before or after doing this check,
+     * and introducing additional synchronization mechanism doesn't change
+     * this.
+     */
+    if ( list_empty(&current->arch.vgic_cpu.ap_list_head) )
+        return;
+
+    ASSERT(!local_irq_is_enabled());
+
+    spin_lock(&current->arch.vgic_cpu.ap_list_lock);
+    vgic_flush_lr_state(current);
+    spin_unlock(&current->arch.vgic_cpu.ap_list_lock);
+}
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic/vgic.h +2 -0
index 5127739f0f..47fc58b81e 100644
@@ -17,6 +17,8 @@
 #ifndef __XEN_ARM_VGIC_NEW_H__
 #define __XEN_ARM_VGIC_NEW_H__
 
+#define vgic_irq_is_sgi(intid) ((intid) < VGIC_NR_SGIS)
+
 static inline bool irq_is_pending(struct vgic_irq *irq)
 {
     if ( irq->config == VGIC_CONFIG_EDGE )
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 24/49] ARM: new VGIC: Add IRQ sync/flush framework

Hi Andre,

Quoted 3 lines · Andre Przywara

Implement the framework for syncing IRQs between our emulation and the list registers, which represent the guest's view of IRQs. This is done in kvm_vgic_flush_hwstate and kvm_vgic_sync_hwstate, which

You probably want to update the names here.

Quoted 28 lines

gets called on guest entry and exit.
The code talking to the actual GICv2/v3 hardware is added in the following patches.

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

---
  xen/arch/arm/vgic/vgic.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.h |   2 +
  2 files changed, 248 insertions(+)
xen/arch/arm/vgic/vgic.c +8 -0
index a4efd1fd03..a1f77130d4 100644
@@ -380,6 +380,252 @@ int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid,
      return 0;
  }
 
+/**
+ * vgic_prune_ap_list - Remove non-relevant interrupts from the list
+ *
+ * @vcpu: The VCPU pointer
+ *
+ * Go over the list of "interesting" interrupts, and prune those that we
+ * won't have to consider in the near future.
+ */

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid, · line 400

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+static void vgic_prune_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq, *tmp;
+    unsigned long flags;
+
+retry:
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry_safe( irq, tmp, &vgic_cpu->ap_list_head, ap_list )

See my comment on patch #22, this is where I am worry about going through the list every time we enter to the hypervisor from the guest.

Quoted 107 lines
xen/arch/arm/vgic/vgic.c +107 -0
+    {
+        struct vcpu *target_vcpu, *vcpuA, *vcpuB;
+
+        spin_lock(&irq->irq_lock);
+
+        BUG_ON(vcpu != irq->vcpu);
+
+        target_vcpu = vgic_target_oracle(irq);
+
+        if ( !target_vcpu )
+        {
+            /*
+             * We don't need to process this interrupt any
+             * further, move it off the list.
+             */
+            list_del(&irq->ap_list);
+            irq->vcpu = NULL;
+            spin_unlock(&irq->irq_lock);
+
+            /*
+             * This vgic_put_irq call matches the
+             * vgic_get_irq_kref in vgic_queue_irq_unlock,
+             * where we added the LPI to the ap_list. As
+             * we remove the irq from the list, we drop
+             * also drop the refcount.
+             */
+            vgic_put_irq(vcpu->domain, irq);
+            continue;
+        }
+
+        if ( target_vcpu == vcpu )
+        {
+            /* We're on the right CPU */
+            spin_unlock(&irq->irq_lock);
+            continue;
+        }
+
+        /* This interrupt looks like it has to be migrated. */
+
+        spin_unlock(&irq->irq_lock);
+        spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+
+        /*
+         * Ensure locking order by always locking the smallest
+         * ID first.
+         */
+        if ( vcpu->vcpu_id < target_vcpu->vcpu_id )
+        {
+            vcpuA = vcpu;
+            vcpuB = target_vcpu;
+        }
+        else
+        {
+            vcpuA = target_vcpu;
+            vcpuB = vcpu;
+        }
+
+        spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
+        spin_lock(&vcpuB->arch.vgic_cpu.ap_list_lock);
+        spin_lock(&irq->irq_lock);
+
+        /*
+         * If the affinity has been preserved, move the
+         * interrupt around. Otherwise, it means things have
+         * changed while the interrupt was unlocked, and we
+         * need to replay this.
+         *
+         * In all cases, we cannot trust the list not to have
+         * changed, so we restart from the beginning.
+         */
+        if ( target_vcpu == vgic_target_oracle(irq) )
+        {
+            struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
+
+            list_del(&irq->ap_list);
+            irq->vcpu = target_vcpu;
+            list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
+        }
+
+        spin_unlock(&irq->irq_lock);
+        spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
+        spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
+        goto retry;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+}
+
+static inline void vgic_fold_lr_state(struct vcpu *vcpu)
+{
+}
+
+/* Requires the irq_lock to be held. */
+static inline void vgic_populate_lr(struct vcpu *vcpu,
+                                    struct vgic_irq *irq, int lr)
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+}
+
+static inline void vgic_clear_lr(struct vcpu *vcpu, int lr)
+{
+}
+
+static inline void vgic_set_underflow(struct vcpu *vcpu)
+{
+}
+

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid, · line 517

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+/* Requires the ap_list_lock to be held. */
+static int compute_ap_list_depth(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)

Here another example.

Quoted 60 lines
xen/arch/arm/vgic/vgic.c +60 -0
+    {
+        spin_lock(&irq->irq_lock);
+        /* GICv2 SGIs can count for more than one... */
+        if ( vgic_irq_is_sgi(irq->intid) && irq->source )
+            count += hweight8(irq->source);
+        else
+            count++;
+        spin_unlock(&irq->irq_lock);
+    }
+    return count;
+}
+
+/* Requires the VCPU's ap_list_lock to be held. */
+static void vgic_flush_lr_state(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    if ( compute_ap_list_depth(vcpu) > gic_get_nr_lrs() )
+        vgic_sort_ap_list(vcpu);
+
+    list_for_each_entry( irq, &vgic_cpu->ap_list_head, ap_list )
+    {
+        spin_lock(&irq->irq_lock);
+
+        if ( unlikely(vgic_target_oracle(irq) != vcpu) )
+            goto next;
+
+        /*
+         * If we get an SGI with multiple sources, try to get
+         * them in all at once.
+         */
+        do
+        {
+            vgic_populate_lr(vcpu, irq, count++);
+        } while ( irq->source && count < gic_get_nr_lrs() );
+
+next:
+        spin_unlock(&irq->irq_lock);
+
+        if ( count == gic_get_nr_lrs() )
+        {
+            if ( !list_is_last(&irq->ap_list, &vgic_cpu->ap_list_head) )
+                vgic_set_underflow(vcpu);
+            break;
+        }
+    }
+
+    vcpu->arch.vgic_cpu.used_lrs = count;
+
+    /* Nuke remaining LRs */
+    for ( ; count < gic_get_nr_lrs(); count++)
+        vgic_clear_lr(vcpu, count);
+}
+
+/*
+ * gic_clear_lrs() - Update the VGIC state from hardware after a guest's run.

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid, · line 587

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+ * @vcpu: the VCPU.
+ *
+ * Sync back the hardware VGIC state after the guest has run, into our
+ * VGIC emulation structures, It reads the LRs and updates the respective
+ * struct vgic_irq, taking level/edge into account.
+ * This is the high level function which takes care of the conditions,
+ * also bails out early if there were no interrupts queued.
+ * Was: kvm_vgic_sync_hwstate()
+ */
+void gic_clear_lrs(struct vcpu *vcpu)

I think I would prefer if we stick with the KVM name.

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+
+    /* An empty ap_list_head implies used_lrs == 0 */
+    if ( list_empty(&vcpu->arch.vgic_cpu.ap_list_head) )
+        return;
+
+    if ( vgic_cpu->used_lrs )
+        vgic_fold_lr_state(vcpu);
+    vgic_prune_ap_list(vcpu);

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid, · line 607

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+}
+
+/*
+ * gic_inject() - flush the emulation state into the hardware on guest entry
+ *
+ * Before we enter a guest, we have to translate the virtual GIC state of a
+ * VCPU into the GIC virtualization hardware registers, namely the LRs.
+ * This is the high level function which takes care about the conditions
+ * and the locking, also bails out early if there are no interrupts queued.
+ * Was: kvm_vgic_flush_hwstate()

Ditto.

Quoted 30 lines
xen/arch/arm/vgic/vgic.c +21 -0
+ */
+void gic_inject(void)
+{
+    /*
+     * If there are no virtual interrupts active or pending for this
+     * VCPU, then there is no work to do and we can bail out without
+     * taking any lock.  There is a potential race with someone injecting
+     * interrupts to the VCPU, but it is a benign race as the VCPU will
+     * either observe the new interrupt before or after doing this check,
+     * and introducing additional synchronization mechanism doesn't change
+     * this.
+     */
+    if ( list_empty(&current->arch.vgic_cpu.ap_list_head) )
+        return;
+
+    ASSERT(!local_irq_is_enabled());
+
+    spin_lock(&current->arch.vgic_cpu.ap_list_lock);
+    vgic_flush_lr_state(current);
+    spin_unlock(&current->arch.vgic_cpu.ap_list_lock);
+}
  /*
   * Local variables:
   * mode: C
xen/arch/arm/vgic/vgic.h +0 -0
index 5127739f0f..47fc58b81e 100644

xen/arch/arm/vgic/vgic.h line 24

Quoted 8 lines
xen/arch/arm/vgic/vgic.h +2 -0
@@ -17,6 +17,8 @@
  #ifndef __XEN_ARM_VGIC_NEW_H__
  #define __XEN_ARM_VGIC_NEW_H__
 
+#define vgic_irq_is_sgi(intid) ((intid) < VGIC_NR_SGIS)
+
  static inline bool irq_is_pending(struct vgic_irq *irq)
  {
      if ( irq->config == VGIC_CONFIG_EDGE )

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 24/49] ARM: new VGIC: Add IRQ sync/flush framework

Hi,

Quoted 8 lines · Julien Grall

Hi Andre,

Quoted 3 lines · Andre Przywara

Implement the framework for syncing IRQs between our emulation and the list registers, which represent the guest's view of IRQs. This is done in kvm_vgic_flush_hwstate and kvm_vgic_sync_hwstate, which

You probably want to update the names here.

Sure.

Quoted 35 lines
Quoted 35 lines

gets called on guest entry and exit.
The code talking to the actual GICv2/v3 hardware is added in the following patches.

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

---
  xen/arch/arm/vgic/vgic.c | 246
+++++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.h |   2 +

  2 files changed, 248 insertions(+)
xen/arch/arm/vgic/vgic.c +0 -0
index a4efd1fd03..a1f77130d4 100644
@@ -380,6 +380,252 @@ int vgic_inject_irq(struct domain *d, struct
 vcpu *vcpu, unsigned int intid,
       return 0;
   }

  +/**
+ * vgic_prune_ap_list - Remove non-relevant interrupts from the list
+ *
+ * @vcpu: The VCPU pointer
+ *
+ * Go over the list of "interesting" interrupts, and prune those that we
+ * won't have to consider in the near future.
+ */

xen/arch/arm/vgic/vgic.c +6 -0
+static void vgic_prune_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq, *tmp;
+    unsigned long flags;
+
Quoted 8 lines
Quoted 5 lines
xen/arch/arm/vgic/vgic.c +4 -0
+retry:
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry_safe( irq, tmp, &vgic_cpu->ap_list_head,

ap_list )

See my comment on patch #22, this is where I am worry about going through the list every time we enter to the hypervisor from the guest.

I am not sure we can avoid this here, as this function is crucial to the VGIC state machine.
We might later look into if we can avoid iterating through the whole list or if we can shortcut some interrupts somehow, but I really would be careful tinkering with this function too much.

Quoted 112 lines
Quoted 112 lines

+    {
+        struct vcpu *target_vcpu, *vcpuA, *vcpuB;

xen/arch/arm/vgic/vgic.c +80 -0
+
+        spin_lock(&irq->irq_lock);
+
+        BUG_ON(vcpu != irq->vcpu);
+
+        target_vcpu = vgic_target_oracle(irq);
+
+        if ( !target_vcpu )
+        {
+            /*
+             * We don't need to process this interrupt any
+             * further, move it off the list.
+             */
+            list_del(&irq->ap_list);
+            irq->vcpu = NULL;
+            spin_unlock(&irq->irq_lock);
+
+            /*
+             * This vgic_put_irq call matches the
+             * vgic_get_irq_kref in vgic_queue_irq_unlock,
+             * where we added the LPI to the ap_list. As
+             * we remove the irq from the list, we drop
+             * also drop the refcount.
+             */
+            vgic_put_irq(vcpu->domain, irq);
+            continue;
+        }
+
+        if ( target_vcpu == vcpu )
+        {
+            /* We're on the right CPU */
+            spin_unlock(&irq->irq_lock);
+            continue;
+        }
+
+        /* This interrupt looks like it has to be migrated. */
+
+        spin_unlock(&irq->irq_lock);
+        spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+
+        /*
+         * Ensure locking order by always locking the smallest
+         * ID first.
+         */
+        if ( vcpu->vcpu_id < target_vcpu->vcpu_id )
+        {
+            vcpuA = vcpu;
+            vcpuB = target_vcpu;
+        }
+        else
+        {
+            vcpuA = target_vcpu;
+            vcpuB = vcpu;
+        }
+
+        spin_lock_irqsave(&vcpuA->arch.vgic_cpu.ap_list_lock, flags);
+        spin_lock(&vcpuB->arch.vgic_cpu.ap_list_lock);
+        spin_lock(&irq->irq_lock);
+
+        /*
+         * If the affinity has been preserved, move the
+         * interrupt around. Otherwise, it means things have
+         * changed while the interrupt was unlocked, and we
+         * need to replay this.
+         *
+         * In all cases, we cannot trust the list not to have
+         * changed, so we restart from the beginning.
+         */
+        if ( target_vcpu == vgic_target_oracle(irq) )
+        {
+            struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu;
+
+            list_del(&irq->ap_list);
+            irq->vcpu = target_vcpu;
+            list_add_tail(&irq->ap_list, &new_cpu->ap_list_head);
+        }
+
+        spin_unlock(&irq->irq_lock);
+        spin_unlock(&vcpuB->arch.vgic_cpu.ap_list_lock);
+        spin_unlock_irqrestore(&vcpuA->arch.vgic_cpu.ap_list_lock,

flags);
+        goto retry;
+    }

xen/arch/arm/vgic/vgic.c +27 -0
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+}
+
+static inline void vgic_fold_lr_state(struct vcpu *vcpu)
+{
+}
+
+/* Requires the irq_lock to be held. */
+static inline void vgic_populate_lr(struct vcpu *vcpu,
+                                    struct vgic_irq *irq, int lr)
+{
+    ASSERT(spin_is_locked(&irq->irq_lock));
+}
+
+static inline void vgic_clear_lr(struct vcpu *vcpu, int lr)
+{
+}
+
+static inline void vgic_set_underflow(struct vcpu *vcpu)
+{
+}
+
+/* Requires the ap_list_lock to be held. */
+static int compute_ap_list_depth(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Quoted 8 lines

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid, · line 517

Quoted 6 lines

+    struct vgic_irq *irq;
+    int count = 0;

xen/arch/arm/vgic/vgic.c +4 -0
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)

Here another example.

This can be short cut, indeed. The only place we call this function is where we compare it against the number of LRs below.
So we don't need to know the actual number of elements, but could bail out once we reached the number of LRs.
This function here would then return a bool, comparing internally against the number of LRs already.

Quoted 66 lines
Quoted 66 lines

+    {
+        spin_lock(&irq->irq_lock);
+        /* GICv2 SGIs can count for more than one... */ +        if ( vgic_irq_is_sgi(irq->intid) && irq->source ) +            count += hweight8(irq->source);
+        else
+            count++;
+        spin_unlock(&irq->irq_lock);
+    }
+    return count;

xen/arch/arm/vgic/vgic.c +50 -0
+}
+
+/* Requires the VCPU's ap_list_lock to be held. */
+static void vgic_flush_lr_state(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    if ( compute_ap_list_depth(vcpu) > gic_get_nr_lrs() )
+        vgic_sort_ap_list(vcpu);
+
+    list_for_each_entry( irq, &vgic_cpu->ap_list_head, ap_list )
+    {
+        spin_lock(&irq->irq_lock);
+
+        if ( unlikely(vgic_target_oracle(irq) != vcpu) )
+            goto next;
+
+        /*
+         * If we get an SGI with multiple sources, try to get
+         * them in all at once.
+         */
+        do
+        {
+            vgic_populate_lr(vcpu, irq, count++);
+        } while ( irq->source && count < gic_get_nr_lrs() );
+
+next:
+        spin_unlock(&irq->irq_lock);
+
+        if ( count == gic_get_nr_lrs() )
+        {
+            if ( !list_is_last(&irq->ap_list, &vgic_cpu->ap_list_head) )
+                vgic_set_underflow(vcpu);
+            break;
+        }
+    }
+
+    vcpu->arch.vgic_cpu.used_lrs = count;
+
+    /* Nuke remaining LRs */
+    for ( ; count < gic_get_nr_lrs(); count++)
+        vgic_clear_lr(vcpu, count);
+}
+
+/*
+ * gic_clear_lrs() - Update the VGIC state from hardware after a

guest's run.
+ * @vcpu: the VCPU.
+ *
+ * Sync back the hardware VGIC state after the guest has run, into our
+ * VGIC emulation structures, It reads the LRs and updates the respective

Quoted 8 lines
Quoted 6 lines

+ * struct vgic_irq, taking level/edge into account.
+ * This is the high level function which takes care of the conditions,
+ * also bails out early if there were no interrupts queued.
+ * Was: kvm_vgic_sync_hwstate()
+ */
+void gic_clear_lrs(struct vcpu *vcpu)

I think I would prefer if we stick with the KVM name.

Yes, please! ;-)
I found those names always confusing, especially gic_inject(). To be honest I never know which is which in the KVM case as well (sync vs. flush), so I was wondering if we call both the entry and the exit handler "sync", but denote the direction, like:
vgic_sync_from_lrs() and vgic_sync_to_lrs().

Cheers,
Andre.

Quoted 60 lines
Quoted 15 lines
xen/arch/arm/vgic/vgic.c +14 -0
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+
+    /* An empty ap_list_head implies used_lrs == 0 */
+    if ( list_empty(&vcpu->arch.vgic_cpu.ap_list_head) )
+        return;
+
+    if ( vgic_cpu->used_lrs )
+        vgic_fold_lr_state(vcpu);
+    vgic_prune_ap_list(vcpu);
+}
+
+/*
+ * gic_inject() - flush the emulation state into the hardware on

guest entry

Quoted 8 lines

+ *
+ * Before we enter a guest, we have to translate the virtual GIC state of a
+ * VCPU into the GIC virtualization hardware registers, namely the LRs.
+ * This is the high level function which takes care about the conditions
+ * and the locking, also bails out early if there are no interrupts queued.
+ * Was: kvm_vgic_flush_hwstate()

Ditto.

Quoted 34 lines

+ */

xen/arch/arm/vgic/vgic.c +6 -0
+void gic_inject(void)
+{
+    /*
+     * If there are no virtual interrupts active or pending for this
+     * VCPU, then there is no work to do and we can bail out without
+     * taking any lock.  There is a potential race with someone

injecting
+     * interrupts to the VCPU, but it is a benign race as the VCPU will +     * either observe the new interrupt before or after doing this check,
+     * and introducing additional synchronization mechanism doesn't change
+     * this.
+     */
+    if ( list_empty(&current->arch.vgic_cpu.ap_list_head) ) +        return;

xen/arch/arm/vgic/vgic.c +7 -0
+
+    ASSERT(!local_irq_is_enabled());
+
+    spin_lock(&current->arch.vgic_cpu.ap_list_lock);
+    vgic_flush_lr_state(current);
+    spin_unlock(&current->arch.vgic_cpu.ap_list_lock);
+}

  /*
   * Local variables:
   * mode: C

xen/arch/arm/vgic/vgic.h +0 -0
index 5127739f0f..47fc58b81e 100644
@@ -17,6 +17,8 @@
   #ifndef __XEN_ARM_VGIC_NEW_H__
   #define __XEN_ARM_VGIC_NEW_H__
Quoted 9 lines

xen/arch/arm/vgic/vgic.h line 24

Quoted 6 lines
xen/arch/arm/vgic/vgic.h +1 -0
   +#define vgic_irq_is_sgi(intid) ((intid) < VGIC_NR_SGIS)
+
   static inline bool irq_is_pending(struct vgic_irq *irq)
   {
       if ( irq->config == VGIC_CONFIG_EDGE )

Cheers,

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

#62 lore raw

Re: [RFC PATCH 24/49] ARM: new VGIC: Add IRQ sync/flush framework

Quoted 50 lines · Andre Przywara

Hi,

Quoted 38 lines · Julien Grall

Hi Andre,

Quoted 35 lines · Andre Przywara

gets called on guest entry and exit.
The code talking to the actual GICv2/v3 hardware is added in the following patches.

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

---
   xen/arch/arm/vgic/vgic.c | 246
+++++++++++++++++++++++++++++++++++++++++++++++
   xen/arch/arm/vgic/vgic.h |   2 +
   2 files changed, 248 insertions(+)
xen/arch/arm/vgic/vgic.c +13 -0
index a4efd1fd03..a1f77130d4 100644
@@ -380,6 +380,252 @@ int vgic_inject_irq(struct domain *d, struct
 vcpu *vcpu, unsigned int intid,
       return 0;
   }
   +/**
+ * vgic_prune_ap_list - Remove non-relevant interrupts from the list
+ *
+ * @vcpu: The VCPU pointer
+ *
+ * Go over the list of "interesting" interrupts, and prune those that we
+ * won't have to consider in the near future.
+ */
+static void vgic_prune_ap_list(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq, *tmp;
+    unsigned long flags;
+
Quoted 8 lines

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid,

Quoted 5 lines
xen/arch/arm/vgic/vgic.c +4 -0
+retry:
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry_safe( irq, tmp, &vgic_cpu->ap_list_head,
 ap_list )

See my comment on patch #22, this is where I am worry about going through the list every time we enter to the hypervisor from the guest.

Quoted 5 lines

I am not sure we can avoid this here, as this function is crucial to the VGIC state machine.
We might later look into if we can avoid iterating through the whole list or if we can shortcut some interrupts somehow, but I really would be careful tinkering with this function too much.

My biggest concern is how big the list can be. If the list is quite small, then this function is ok to call when entering to the hypervisor. If you tell me it can be bigger, then I am quite reluctant to see this code in Xen. Obviously this could be solved afterwards but clearly before we make this a default option in Xen.

To be clear, I am asking how much a guest can control the size of the list.

[...]

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/arm/vgic/vgic.c +4 -0
+/* Requires the ap_list_lock to be held. */
+static int compute_ap_list_depth(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
Quoted 8 lines

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid, · line 517

Quoted 6 lines
xen/arch/arm/vgic/vgic.c +6 -0
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)

Here another example.

Quoted 6 lines

This can be short cut, indeed. The only place we call this function is where we compare it against the number of LRs below.
So we don't need to know the actual number of elements, but could bail out once we reached the number of LRs.
This function here would then return a bool, comparing internally against the number of LRs already.

This would only solve one part of the problem. Then you go sorting the list if you have more vIRQ in the list than the number of LRs.

See above for my thoughts on the list.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
xen/arch/arm/vgic/vgic.c +1 -0
+ * gic_clear_lrs() - Update the VGIC state from hardware after a

guest's run.
+ * @vcpu: the VCPU.
+ *
+ * Sync back the hardware VGIC state after the guest has run, into our
+ * VGIC emulation structures, It reads the LRs and updates the respective
+ * struct vgic_irq, taking level/edge into account.
+ * This is the high level function which takes care of the conditions,
+ * also bails out early if there were no interrupts queued.
+ * Was: kvm_vgic_sync_hwstate()
+ */
+void gic_clear_lrs(struct vcpu *vcpu)

Quoted 8 lines
Quoted 1 line

I think I would prefer if we stick with the KVM name.

Yes, please! ;-)
I found those names always confusing, especially gic_inject(). To be honest I never know which is which in the KVM case as well (sync vs. flush), so I was wondering if we call both the entry and the exit handler "sync", but denote the direction, like:
vgic_sync_from_lrs() and vgic_sync_to_lrs().

+1 on the suggested naming :).

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 24/49] ARM: new VGIC: Add IRQ sync/flush framework

In reply to Andre Przywara

Hi Andre,

Quoted 34 lines · Andre Przywara
xen/arch/arm/vgic/vgic.c +34 -0
+/* Requires the VCPU's ap_list_lock to be held. */
+static void vgic_flush_lr_state(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    if ( compute_ap_list_depth(vcpu) > gic_get_nr_lrs() )
+        vgic_sort_ap_list(vcpu);
+
+    list_for_each_entry( irq, &vgic_cpu->ap_list_head, ap_list )
+    {
+        spin_lock(&irq->irq_lock);
+
+        if ( unlikely(vgic_target_oracle(irq) != vcpu) )
+            goto next;
+
+        /*
+         * If we get an SGI with multiple sources, try to get
+         * them in all at once.
+         */
+        do
+        {
+            vgic_populate_lr(vcpu, irq, count++);
+        } while ( irq->source && count < gic_get_nr_lrs() );
+
+next:
+        spin_unlock(&irq->irq_lock);
+
+        if ( count == gic_get_nr_lrs() )
+        {
+            if ( !list_is_last(&irq->ap_list, &vgic_cpu->ap_list_head) )

xen/arch/arm/vgic/vgic.c int vgic_inject_irq(struct domain *d, struct vcpu *vcpu, unsigned int intid, · line 573

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+                vgic_set_underflow(vcpu);
+            break;
+        }
+    }
+
+    vcpu->arch.vgic_cpu.used_lrs = count;
+
+    /* Nuke remaining LRs */
+    for ( ; count < gic_get_nr_lrs(); count++)
+        vgic_clear_lr(vcpu, count);

Why do you need to nuke the LRs here, don't you always zero them when clearing it?

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 24/49] ARM: new VGIC: Add IRQ sync/flush framework

Hi,

Quoted 42 lines · Julien Grall

Hi Andre,

Quoted 39 lines · Andre Przywara
xen/arch/arm/vgic/vgic.c +39 -0
+/* Requires the VCPU's ap_list_lock to be held. */
+static void vgic_flush_lr_state(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    if ( compute_ap_list_depth(vcpu) > gic_get_nr_lrs() )
+        vgic_sort_ap_list(vcpu);
+
+    list_for_each_entry( irq, &vgic_cpu->ap_list_head, ap_list )
+    {
+        spin_lock(&irq->irq_lock);
+
+        if ( unlikely(vgic_target_oracle(irq) != vcpu) )
+            goto next;
+
+        /*
+         * If we get an SGI with multiple sources, try to get
+         * them in all at once.
+         */
+        do
+        {
+            vgic_populate_lr(vcpu, irq, count++);
+        } while ( irq->source && count < gic_get_nr_lrs() );
+
+next:
+        spin_unlock(&irq->irq_lock);
+
+        if ( count == gic_get_nr_lrs() )
+        {
+            if ( !list_is_last(&irq->ap_list, &vgic_cpu->ap_list_head) )
+                vgic_set_underflow(vcpu);
+            break;
+        }
+    }
+
Quoted 8 lines
Quoted 5 lines

+    vcpu->arch.vgic_cpu.used_lrs = count;
+
+    /* Nuke remaining LRs */
+    for ( ; count < gic_get_nr_lrs(); count++)
+        vgic_clear_lr(vcpu, count);

Why do you need to nuke the LRs here, don't you always zero them when clearing it?

We nuke our internal LR copies in here.
It might be interesting to see if we can get rid of those in Xen, because we can always write to the LRs directly. But this is an optimization I am not too keen on addressing too early, because this deviates from the KVM VGIC architecture.

Cheers,
Andre.

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

#65 lore raw

Re: [RFC PATCH 24/49] ARM: new VGIC: Add IRQ sync/flush framework

Quoted 54 lines · Andre Przywara

Hi,

Quoted 42 lines · Julien Grall

Hi Andre,

Quoted 39 lines · Andre Przywara
xen/arch/arm/vgic/vgic.c +39 -0
+/* Requires the VCPU's ap_list_lock to be held. */
+static void vgic_flush_lr_state(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    int count = 0;
+
+    ASSERT(spin_is_locked(&vgic_cpu->ap_list_lock));
+
+    if ( compute_ap_list_depth(vcpu) > gic_get_nr_lrs() )
+        vgic_sort_ap_list(vcpu);
+
+    list_for_each_entry( irq, &vgic_cpu->ap_list_head, ap_list )
+    {
+        spin_lock(&irq->irq_lock);
+
+        if ( unlikely(vgic_target_oracle(irq) != vcpu) )
+            goto next;
+
+        /*
+         * If we get an SGI with multiple sources, try to get
+         * them in all at once.
+         */
+        do
+        {
+            vgic_populate_lr(vcpu, irq, count++);
+        } while ( irq->source && count < gic_get_nr_lrs() );
+
+next:
+        spin_unlock(&irq->irq_lock);
+
+        if ( count == gic_get_nr_lrs() )
+        {
+            if ( !list_is_last(&irq->ap_list, &vgic_cpu->ap_list_head) )
+                vgic_set_underflow(vcpu);
+            break;
+        }
+    }
+
Quoted 8 lines
Quoted 5 lines

+    vcpu->arch.vgic_cpu.used_lrs = count;
+
+    /* Nuke remaining LRs */
+    for ( ; count < gic_get_nr_lrs(); count++)
+        vgic_clear_lr(vcpu, count);

Why do you need to nuke the LRs here, don't you always zero them when clearing it?

Quoted 5 lines

We nuke our internal LR copies in here.
It might be interesting to see if we can get rid of those in Xen, because we can always write to the LRs directly. But this is an optimization I am not too keen on addressing too early, because this deviates from the KVM VGIC architecture.

Oh, I thought you were writing back in the hardware when clearing. My mistake.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 25/49 ARM: new VGIC: Add GICv2 world switch backend

In reply to Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
 xen/arch/arm/vgic/vgic-v2.c | 261 ++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.c    |  20 ++++
 xen/arch/arm/vgic/vgic.h    |   8 ++
 3 files changed, 289 insertions(+)
 create mode 100644 xen/arch/arm/vgic/vgic-v2.c
xen/arch/arm/vgic/vgic-v2.c new file +261 -0
new file mode 100644
index 0000000000..10fc467ffa
@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm/arm_vgic.h>
+#include <asm/bug.h>
+#include <asm/io.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+
+#include "vgic.h"
+
+#define GICH_ELRSR0                     0x30
+#define GICH_ELRSR1                     0x34
+#define GICH_LR0                        0x100
+
+#define GICH_LR_VIRTUALID               (0x3ff << 0)
+#define GICH_LR_PHYSID_CPUID_SHIFT      (10)
+#define GICH_LR_PHYSID_CPUID            (0x3ff << GICH_LR_PHYSID_CPUID_SHIFT)
+#define GICH_LR_PRIORITY_SHIFT          23
+#define GICH_LR_STATE                   (3 << 28)
+#define GICH_LR_PENDING_BIT             (1 << 28)
+#define GICH_LR_ACTIVE_BIT              (1 << 29)
+#define GICH_LR_EOI                     (1 << 19)
+#define GICH_LR_HW                      (1 << 31)
+
+static struct {
+    bool enabled;
+    paddr_t dbase;          /* Distributor interface address */
+    paddr_t cbase;          /* CPU interface address & size */
+    paddr_t csize;
+    paddr_t vbase;          /* Virtual CPU interface address */
+    void __iomem *hbase;        /* Hypervisor control interface */
+
+    /* Offset to add to get an 8kB contiguous region if GIC is aliased */
+    uint32_t aliased_offset;
+} gic_v2_hw_data;
+
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+              paddr_t vbase, void __iomem *hbase,
+              uint32_t aliased_offset)
+{
+    gic_v2_hw_data.enabled = true;
+    gic_v2_hw_data.dbase = dbase;
+    gic_v2_hw_data.cbase = cbase;
+    gic_v2_hw_data.csize = csize;
+    gic_v2_hw_data.vbase = vbase;
+    gic_v2_hw_data.hbase = hbase;
+    gic_v2_hw_data.aliased_offset = aliased_offset;
+}
+
+void vgic_v2_set_underflow(struct vcpu *vcpu)
+{
+    gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 1);
+}
+
+/*
+ * transfer the content of the LRs back into the corresponding ap_list:
+ * - active bit is transferred as is
+ * - pending bit is
+ *   - transferred as is in case of edge sensitive IRQs
+ *   - set to the line-level (resample time) for level sensitive IRQs
+ */
+void vgic_v2_fold_lr_state(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_v2_cpu_if *cpuif = &vgic_cpu->vgic_v2;
+    int lr;
+    unsigned long flags;
+
+    cpuif->vgic_hcr &= ~GICH_HCR_UIE;
+
+    for ( lr = 0; lr < vgic_cpu->used_lrs; lr++ )
+    {
+        u32 val = cpuif->vgic_lr[lr];
+        u32 intid = val & GICH_LR_VIRTUALID;
+        struct vgic_irq *irq;
+
+        irq = vgic_get_irq(vcpu->domain, vcpu, intid);
+
+        spin_lock_irqsave(&irq->irq_lock, flags);
+
+        /* Always preserve the active bit */
+        irq->active = !!(val & GICH_LR_ACTIVE_BIT);
+
+        /* Edge is the only case where we preserve the pending bit */
+        if ( irq->config == VGIC_CONFIG_EDGE && (val & GICH_LR_PENDING_BIT) )
+        {
+            irq->pending_latch = true;
+
+            if ( vgic_irq_is_sgi(intid) )
+            {
+                u32 cpuid = val & GICH_LR_PHYSID_CPUID;
+
+                cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
+                irq->source |= (1 << cpuid);
+            }
+        }
+
+        if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL &&
+            (val & GICH_LR_PENDING_BIT) )
+        {
+            irq->line_level = gic_read_pending_state(irq->hwintid);
+
+            if ( !irq->line_level )
+                            gic_set_active_state(irq->hwintid, true);
+        }
+
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+        vgic_put_irq(vcpu->domain, irq);
+    }
+
+    vgic_cpu->used_lrs = 0;
+}
+
+/*
+ * Populates the particular LR with the state of a given IRQ:
+ * - for an edge sensitive IRQ the pending state is cleared in struct vgic_irq
+ * - for a level sensitive IRQ the pending state value is unchanged;
+ *   it is dictated directly by the input level
+ *
+ * If @irq describes an SGI with multiple sources, we choose the
+ * lowest-numbered source VCPU and clear that bit in the source bitmap.
+ *
+ * The irq_lock must be held by the caller.
+ */
+void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr)
+{
+    u32 val = irq->intid;
+
+    if ( irq_is_pending(irq) )
+    {
+        val |= GICH_LR_PENDING_BIT;
+
+        if ( irq->config == VGIC_CONFIG_EDGE )
+            irq->pending_latch = false;
+
+        if ( vgic_irq_is_sgi(irq->intid) )
+        {
+            u32 src = ffs(irq->source);
+
+            BUG_ON(!src);
+            val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
+            irq->source &= ~(1 << (src - 1));
+            if ( irq->source )
+                irq->pending_latch = true;
+        }
+    }
+
+    if ( irq->active )
+        val |= GICH_LR_ACTIVE_BIT;
+
+    if ( irq->hw )
+    {
+        val |= GICH_LR_HW;
+        val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
+        /*
+         * Never set pending+active on a HW interrupt, as the
+         * pending state is kept at the physical distributor
+         * level.
+         */
+        if ( irq->active && irq_is_pending(irq) )
+            val &= ~GICH_LR_PENDING_BIT;
+    }
+    else
+    {
+        if ( irq->config == VGIC_CONFIG_LEVEL )
+            val |= GICH_LR_EOI;
+    }
+
+    /*
+     * Level-triggered mapped IRQs are special because we only observe
+     * rising edges as input to the VGIC.  We therefore lower the line
+     * level here, so that we can take new virtual IRQs.  See
+     * vgic_v2_fold_lr_state for more info.
+     */
+    if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL &&
+        (val & GICH_LR_PENDING_BIT) )
+        irq->line_level = false;
+
+    /* The GICv2 LR only holds five bits of priority. */
+    val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
+
+    vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val;
+}
+
+void vgic_v2_clear_lr(struct vcpu *vcpu, int lr)
+{
+    vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0;
+}
+
+static void save_lrs(struct vcpu *vcpu, void __iomem *base)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+    u64 elrsr;
+    int i;
+
+    elrsr = readl_relaxed(base + GICH_ELRSR0);
+    if ( unlikely(used_lrs > 32) )
+        elrsr |= ((u64)readl_relaxed(base + GICH_ELRSR1)) << 32;
+
+    for ( i = 0; i < used_lrs; i++ )
+    {
+        if ( elrsr & (1UL << i) )
+            cpu_if->vgic_lr[i] &= ~GICH_LR_STATE;
+        else
+            cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i * 4));
+
+        writel_relaxed(0, base + GICH_LR0 + (i * 4));
+    }
+}
+
+void vgic_v2_save_state(struct vcpu *vcpu)
+{
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+
+    if ( used_lrs )
+    {
+        save_lrs(vcpu, gic_v2_hw_data.hbase);
+        writel_relaxed(0, gic_v2_hw_data.hbase + GICH_HCR);
+    }
+}
+
+void vgic_v2_restore_state(struct vcpu *vcpu)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+    int i;
+
+    if ( used_lrs )
+    {
+        writel_relaxed(cpu_if->vgic_hcr,
+                       gic_v2_hw_data.hbase + GICH_HCR);
+        for ( i = 0; i < used_lrs; i++ )
+            writel_relaxed(cpu_if->vgic_lr[i],
+                           gic_v2_hw_data.hbase + GICH_LR0 + (i * 4));
+    }
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic.c +20 -0
index a1f77130d4..f4f2a04a60 100644
@@ -488,6 +488,7 @@ retry:
 
 static inline void vgic_fold_lr_state(struct vcpu *vcpu)
 {
+    vgic_v2_fold_lr_state(vcpu);
 }
 
 /* Requires the irq_lock to be held. */
@@ -495,14 +496,18 @@ static inline void vgic_populate_lr(struct vcpu *vcpu,
                                     struct vgic_irq *irq, int lr)
 {
     ASSERT(spin_is_locked(&irq->irq_lock));
+
+    vgic_v2_populate_lr(vcpu, irq, lr);
 }
 
 static inline void vgic_clear_lr(struct vcpu *vcpu, int lr)
 {
+    vgic_v2_clear_lr(vcpu, lr);
 }
 
 static inline void vgic_set_underflow(struct vcpu *vcpu)
 {
+    vgic_v2_set_underflow(vcpu);
 }
 
 /* Requires the ap_list_lock to be held. */
@@ -573,6 +578,11 @@ next:
         vgic_clear_lr(vcpu, count);
 }
 
+static inline void vgic_save_state(struct vcpu *vcpu)
+{
+    vgic_v2_save_state(vcpu);
+}
+
 /*
  * gic_clear_lrs() - Update the VGIC state from hardware after a guest's run.
  * @vcpu: the VCPU.
@@ -592,11 +602,18 @@ void gic_clear_lrs(struct vcpu *vcpu)
     if ( list_empty(&vcpu->arch.vgic_cpu.ap_list_head) )
         return;
 
+    vgic_save_state(vcpu);
+
     if ( vgic_cpu->used_lrs )
         vgic_fold_lr_state(vcpu);
     vgic_prune_ap_list(vcpu);
 }
 
+static inline void vgic_restore_state(struct vcpu *vcpu)
+{
+    vgic_v2_restore_state(vcpu);
+}
+
 /*
  * gic_inject() - flush the emulation state into the hardware on guest entry
  *
@@ -625,7 +642,10 @@ void gic_inject(void)
     spin_lock(&current->arch.vgic_cpu.ap_list_lock);
     vgic_flush_lr_state(current);
     spin_unlock(&current->arch.vgic_cpu.ap_list_lock);
+
+    vgic_restore_state(current);
 }
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic/vgic.h +8 -0
index 47fc58b81e..771ca6f046 100644
@@ -41,4 +41,12 @@ static inline void vgic_get_irq_kref(struct vgic_irq *irq)
     atomic_inc(&irq->refcount);
 }
 
+void vgic_v2_fold_lr_state(struct vcpu *vcpu);
+void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr);
+void vgic_v2_clear_lr(struct vcpu *vcpu, int lr);
+void vgic_v2_set_underflow(struct vcpu *vcpu);
+
+void vgic_v2_save_state(struct vcpu *vcpu);
+void vgic_v2_restore_state(struct vcpu *vcpu);
+
 #endif
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend

Hi,

Quoted 51 lines · Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
  xen/arch/arm/vgic/vgic-v2.c | 261 ++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.c    |  20 ++++
  xen/arch/arm/vgic/vgic.h    |   8 ++
  3 files changed, 289 insertions(+)

create mode 100644 xen/arch/arm/vgic/vgic-v2.c

xen/arch/arm/vgic/vgic-v2.c new file +28 -0
new file mode 100644
index 0000000000..10fc467ffa
@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm/arm_vgic.h>
+#include <asm/bug.h>
+#include <asm/io.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+
+#include "vgic.h"
+
+#define GICH_ELRSR0                     0x30
+#define GICH_ELRSR1                     0x34
+#define GICH_LR0                        0x100

xen/arch/arm/vgic/vgic-v2.c line 38

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+
+#define GICH_LR_VIRTUALID               (0x3ff << 0)
+#define GICH_LR_PHYSID_CPUID_SHIFT      (10)
+#define GICH_LR_PHYSID_CPUID            (0x3ff << GICH_LR_PHYSID_CPUID_SHIFT)
+#define GICH_LR_PRIORITY_SHIFT          23
+#define GICH_LR_STATE                   (3 << 28)
+#define GICH_LR_PENDING_BIT             (1 << 28)
+#define GICH_LR_ACTIVE_BIT              (1 << 29)
+#define GICH_LR_EOI                     (1 << 19)
+#define GICH_LR_HW                      (1 << 31)

Can we define them in either in gic.h or a new header gic-v2.h?

Quoted 29 lines
xen/arch/arm/vgic/vgic-v2.c new file +29 -0
+
+static struct {
+    bool enabled;
+    paddr_t dbase;          /* Distributor interface address */
+    paddr_t cbase;          /* CPU interface address & size */
+    paddr_t csize;
+    paddr_t vbase;          /* Virtual CPU interface address */
+    void __iomem *hbase;        /* Hypervisor control interface */
+
+    /* Offset to add to get an 8kB contiguous region if GIC is aliased */
+    uint32_t aliased_offset;
+} gic_v2_hw_data;
+
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+              paddr_t vbase, void __iomem *hbase,
+              uint32_t aliased_offset)
+{
+    gic_v2_hw_data.enabled = true;
+    gic_v2_hw_data.dbase = dbase;
+    gic_v2_hw_data.cbase = cbase;
+    gic_v2_hw_data.csize = csize;
+    gic_v2_hw_data.vbase = vbase;
+    gic_v2_hw_data.hbase = hbase;
+    gic_v2_hw_data.aliased_offset = aliased_offset;
+}
+
+void vgic_v2_set_underflow(struct vcpu *vcpu)
+{
+    gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 1);

xen/arch/arm/vgic/vgic-v2.c line 77

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+}
+
+/*
+ * transfer the content of the LRs back into the corresponding ap_list:
+ * - active bit is transferred as is
+ * - pending bit is
+ *   - transferred as is in case of edge sensitive IRQs
+ *   - set to the line-level (resample time) for level sensitive IRQs
+ */
+void vgic_v2_fold_lr_state(struct vcpu *vcpu)

I am wondering how much we could share this code with vgic_v3_fold_lr_state.

xen/arch/arm/vgic/vgic-v2.c line 81

Quoted 4 lines
xen/arch/arm/vgic/vgic-v2.c new file +4 -0
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_v2_cpu_if *cpuif = &vgic_cpu->vgic_v2;
+    int lr;

unsigned please.

Quoted 21 lines
xen/arch/arm/vgic/vgic-v2.c new file +21 -0
+    unsigned long flags;
+
+    cpuif->vgic_hcr &= ~GICH_HCR_UIE;
+
+    for ( lr = 0; lr < vgic_cpu->used_lrs; lr++ )
+    {
+        u32 val = cpuif->vgic_lr[lr];
+        u32 intid = val & GICH_LR_VIRTUALID;
+        struct vgic_irq *irq;
+
+        irq = vgic_get_irq(vcpu->domain, vcpu, intid);
+
+        spin_lock_irqsave(&irq->irq_lock, flags);
+
+        /* Always preserve the active bit */
+        irq->active = !!(val & GICH_LR_ACTIVE_BIT);
+
+        /* Edge is the only case where we preserve the pending bit */
+        if ( irq->config == VGIC_CONFIG_EDGE && (val & GICH_LR_PENDING_BIT) )
+        {
+            irq->pending_latch = true;

xen/arch/arm/vgic/vgic-v2.c line 112

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+
+            if ( vgic_irq_is_sgi(intid) )
+            {
+                u32 cpuid = val & GICH_LR_PHYSID_CPUID;
+
+                cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT;
+                irq->source |= (1 << cpuid);
+            }
+        }
+

May I ask to keep the big comments from KVM around? It looks quite useful to have it.

xen/arch/arm/vgic/vgic-v2.c line 113

Quoted 1 line
xen/arch/arm/vgic/vgic-v2.c new file +1 -0
+        if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL &&

You probably want to have the helper vgic_irq_is_mapped_level(...) as in KVM.

Quoted 17 lines
xen/arch/arm/vgic/vgic-v2.c new file +17 -0
+            (val & GICH_LR_PENDING_BIT) )
+        {
+            irq->line_level = gic_read_pending_state(irq->hwintid);
+
+            if ( !irq->line_level )
+                            gic_set_active_state(irq->hwintid, true);
+        }
+
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+        vgic_put_irq(vcpu->domain, irq);
+    }
+
+    vgic_cpu->used_lrs = 0;
+}
+
+/*
+ * Populates the particular LR with the state of a given IRQ:

xen/arch/arm/vgic/vgic-v2.c line 140

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+ * - for an edge sensitive IRQ the pending state is cleared in struct vgic_irq
+ * - for a level sensitive IRQ the pending state value is unchanged;
+ *   it is dictated directly by the input level
+ *
+ * If @irq describes an SGI with multiple sources, we choose the
+ * lowest-numbered source VCPU and clear that bit in the source bitmap.
+ *
+ * The irq_lock must be held by the caller.
+ */
+void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr)

I am wondering how much we could share this code with vgic_v3_populate_lr.

Quoted 40 lines
xen/arch/arm/vgic/vgic-v2.c new file +40 -0
+{
+    u32 val = irq->intid;
+
+    if ( irq_is_pending(irq) )
+    {
+        val |= GICH_LR_PENDING_BIT;
+
+        if ( irq->config == VGIC_CONFIG_EDGE )
+            irq->pending_latch = false;
+
+        if ( vgic_irq_is_sgi(irq->intid) )
+        {
+            u32 src = ffs(irq->source);
+
+            BUG_ON(!src);
+            val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
+            irq->source &= ~(1 << (src - 1));
+            if ( irq->source )
+                irq->pending_latch = true;
+        }
+    }
+
+    if ( irq->active )
+        val |= GICH_LR_ACTIVE_BIT;
+
+    if ( irq->hw )
+    {
+        val |= GICH_LR_HW;
+        val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
+        /*
+         * Never set pending+active on a HW interrupt, as the
+         * pending state is kept at the physical distributor
+         * level.
+         */
+        if ( irq->active && irq_is_pending(irq) )
+            val &= ~GICH_LR_PENDING_BIT;
+    }
+    else
+    {
+        if ( irq->config == VGIC_CONFIG_LEVEL )

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

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+            val |= GICH_LR_EOI;
+    }
+
+    /*
+     * Level-triggered mapped IRQs are special because we only observe
+     * rising edges as input to the VGIC.  We therefore lower the line
+     * level here, so that we can take new virtual IRQs.  See
+     * vgic_v2_fold_lr_state for more info.
+     */
+    if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL &&

Same remark for the helper.

Quoted 36 lines
xen/arch/arm/vgic/vgic-v2.c new file +36 -0
+        (val & GICH_LR_PENDING_BIT) )
+        irq->line_level = false;
+
+    /* The GICv2 LR only holds five bits of priority. */
+    val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
+
+    vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val;
+}
+
+void vgic_v2_clear_lr(struct vcpu *vcpu, int lr)
+{
+    vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0;
+}
+
+static void save_lrs(struct vcpu *vcpu, void __iomem *base)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+    u64 elrsr;
+    int i;
+
+    elrsr = readl_relaxed(base + GICH_ELRSR0);
+    if ( unlikely(used_lrs > 32) )
+        elrsr |= ((u64)readl_relaxed(base + GICH_ELRSR1)) << 32;
+
+    for ( i = 0; i < used_lrs; i++ )
+    {
+        if ( elrsr & (1UL << i) )
+            cpu_if->vgic_lr[i] &= ~GICH_LR_STATE;
+        else
+            cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i * 4));
+
+        writel_relaxed(0, base + GICH_LR0 + (i * 4));
+    }
+}
+

xen/arch/arm/vgic/vgic-v2.c line 236

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+void vgic_v2_save_state(struct vcpu *vcpu)
+{
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+
+    if ( used_lrs )
+    {
+        save_lrs(vcpu, gic_v2_hw_data.hbase);
+        writel_relaxed(0, gic_v2_hw_data.hbase + GICH_HCR);
+    }
+}

I am not entirely convinced that have a separate function to save the LRs is necessary. This could be done in fold_lr_state().

Quoted 5 lines
xen/arch/arm/vgic/vgic-v2.c new file +5 -0
+
+void vgic_v2_restore_state(struct vcpu *vcpu)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;

xen/arch/arm/vgic/vgic-v2.c line 251

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+    int i;
+
+    if ( used_lrs )
+    {
+        writel_relaxed(cpu_if->vgic_hcr,
+                       gic_v2_hw_data.hbase + GICH_HCR);
+        for ( i = 0; i < used_lrs; i++ )
+            writel_relaxed(cpu_if->vgic_lr[i],
+                           gic_v2_hw_data.hbase + GICH_LR0 + (i * 4));
+    }

Same here but with populate_lr_state(). This would make the code easier to follow and also avoid a lot ifery in the vgic.c code.

Quoted 91 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic.c +20 -0
index a1f77130d4..f4f2a04a60 100644
@@ -488,6 +488,7 @@ retry:
 
  static inline void vgic_fold_lr_state(struct vcpu *vcpu)
  {
+    vgic_v2_fold_lr_state(vcpu);
  }
 
  /* Requires the irq_lock to be held. */
@@ -495,14 +496,18 @@ static inline void vgic_populate_lr(struct vcpu *vcpu,
                                      struct vgic_irq *irq, int lr)
  {
      ASSERT(spin_is_locked(&irq->irq_lock));
+
+    vgic_v2_populate_lr(vcpu, irq, lr);
  }
 
  static inline void vgic_clear_lr(struct vcpu *vcpu, int lr)
  {
+    vgic_v2_clear_lr(vcpu, lr);
  }
 
  static inline void vgic_set_underflow(struct vcpu *vcpu)
  {
+    vgic_v2_set_underflow(vcpu);
  }
 
  /* Requires the ap_list_lock to be held. */
@@ -573,6 +578,11 @@ next:
          vgic_clear_lr(vcpu, count);
  }
 
+static inline void vgic_save_state(struct vcpu *vcpu)
+{
+    vgic_v2_save_state(vcpu);
+}
+
  /*
   * gic_clear_lrs() - Update the VGIC state from hardware after a guest's run.
   * @vcpu: the VCPU.
@@ -592,11 +602,18 @@ void gic_clear_lrs(struct vcpu *vcpu)
      if ( list_empty(&vcpu->arch.vgic_cpu.ap_list_head) )
          return;
 
+    vgic_save_state(vcpu);
+
      if ( vgic_cpu->used_lrs )
          vgic_fold_lr_state(vcpu);
      vgic_prune_ap_list(vcpu);
  }
 
+static inline void vgic_restore_state(struct vcpu *vcpu)
+{
+    vgic_v2_restore_state(vcpu);
+}
+
  /*
   * gic_inject() - flush the emulation state into the hardware on guest entry
   *
@@ -625,7 +642,10 @@ void gic_inject(void)
      spin_lock(&current->arch.vgic_cpu.ap_list_lock);
      vgic_flush_lr_state(current);
      spin_unlock(&current->arch.vgic_cpu.ap_list_lock);
+
+    vgic_restore_state(current);
  }
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/vgic/vgic.h +0 -0
index 47fc58b81e..771ca6f046 100644
@@ -41,4 +41,12 @@ static inline void vgic_get_irq_kref(struct vgic_irq *irq)
      atomic_inc(&irq->refcount);
  }

xen/arch/arm/vgic/vgic.h static inline void vgic_get_irq_kref(struct vgic_irq *irq) · line 52

Quoted 10 lines
xen/arch/arm/vgic/vgic.h +8 -0
 
+void vgic_v2_fold_lr_state(struct vcpu *vcpu);
+void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr);
+void vgic_v2_clear_lr(struct vcpu *vcpu, int lr);
+void vgic_v2_set_underflow(struct vcpu *vcpu);
+
+void vgic_v2_save_state(struct vcpu *vcpu);
+void vgic_v2_restore_state(struct vcpu *vcpu);
+
  #endif

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend

Hi,

Quoted 60 lines · Julien Grall

Hi,

Quoted 57 lines · Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
  xen/arch/arm/vgic/vgic-v2.c | 261
++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.c    |  20 ++++
  xen/arch/arm/vgic/vgic.h    |   8 ++

  3 files changed, 289 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic-v2.c

xen/arch/arm/vgic/vgic-v2.c new file +32 -0
new file mode 100644
index 0000000000..10fc467ffa
@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm/arm_vgic.h>
+#include <asm/bug.h>
+#include <asm/io.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+
+#include "vgic.h"
+
+#define GICH_ELRSR0                     0x30
+#define GICH_ELRSR1                     0x34
+#define GICH_LR0                        0x100
+
+#define GICH_LR_VIRTUALID               (0x3ff << 0)
+#define GICH_LR_PHYSID_CPUID_SHIFT      (10)
+#define GICH_LR_PHYSID_CPUID            (0x3ff <<

GICH_LR_PHYSID_CPUID_SHIFT)

Quoted 8 lines

xen/arch/arm/vgic/vgic-v2.c line 38

Quoted 6 lines
xen/arch/arm/vgic/vgic-v2.c new file +6 -0
+#define GICH_LR_PRIORITY_SHIFT          23
+#define GICH_LR_STATE                   (3 << 28)
+#define GICH_LR_PENDING_BIT             (1 << 28)
+#define GICH_LR_ACTIVE_BIT              (1 << 29)
+#define GICH_LR_EOI                     (1 << 19)
+#define GICH_LR_HW                      (1 << 31)

Can we define them in either in gic.h or a new header gic-v2.h?

Yes, but they clash with some ill-named GICv3 LR bits. So expect another patch which renames GICH_LR_STATE_SHIFT to ICH_LR_STATE_SHIFT. Which is the actual spec name for that system register in GICv3, there is no GICH_LR_ with the GICv3 bit positions.

Quoted 35 lines
Quoted 35 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+
+static struct {
+    bool enabled;
+    paddr_t dbase;          /* Distributor interface address */
+    paddr_t cbase;          /* CPU interface address & size */
+    paddr_t csize;
+    paddr_t vbase;          /* Virtual CPU interface address */
+    void __iomem *hbase;        /* Hypervisor control interface */
+
+    /* Offset to add to get an 8kB contiguous region if GIC is

aliased */
+    uint32_t aliased_offset;

xen/arch/arm/vgic/vgic-v2.c new file +23 -0
+} gic_v2_hw_data;
+
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+              paddr_t vbase, void __iomem *hbase,
+              uint32_t aliased_offset)
+{
+    gic_v2_hw_data.enabled = true;
+    gic_v2_hw_data.dbase = dbase;
+    gic_v2_hw_data.cbase = cbase;
+    gic_v2_hw_data.csize = csize;
+    gic_v2_hw_data.vbase = vbase;
+    gic_v2_hw_data.hbase = hbase;
+    gic_v2_hw_data.aliased_offset = aliased_offset;
+}
+
+void vgic_v2_set_underflow(struct vcpu *vcpu)
+{
+    gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 1);
+}
+
+/*
+ * transfer the content of the LRs back into the corresponding ap_list:
+ * - active bit is transferred as is
Quoted 8 lines
Quoted 5 lines

+ * - pending bit is
+ *   - transferred as is in case of edge sensitive IRQs
+ *   - set to the line-level (resample time) for level sensitive IRQs
+ */
+void vgic_v2_fold_lr_state(struct vcpu *vcpu)

I am wondering how much we could share this code with vgic_v3_fold_lr_state.

I think we discussed this and dismissed the idea:
- The actual LR encoding is much different between GICv3 and GICv2, up to the point where we have some fields in one which are not in the other. That really clutters the code.
- Originally this function was much shorter and didn't have that many special cases. So the code duplication was really minimal.

I see your point, but don't really want to go there now for two reasons:
- It is probably nasty to implement, since we always have to check which GIC we are running on when masking the LR value.
- It would deviate further from the KVM implementation, in a core function. For any bugs introduced we are on our own here.

I will try to bring this up with the KVM people, to see whether it's worth to revisit this decision. There is indeed quite some code duplication these days.
But this may come as an optimization later.

Quoted 34 lines
Quoted 4 lines

+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +    struct vgic_v2_cpu_if *cpuif = &vgic_cpu->vgic_v2; +    int lr;

unsigned please.

Quoted 27 lines

+    unsigned long flags;

xen/arch/arm/vgic/vgic-v2.c new file +18 -0
+
+    cpuif->vgic_hcr &= ~GICH_HCR_UIE;
+
+    for ( lr = 0; lr < vgic_cpu->used_lrs; lr++ )
+    {
+        u32 val = cpuif->vgic_lr[lr];
+        u32 intid = val & GICH_LR_VIRTUALID;
+        struct vgic_irq *irq;
+
+        irq = vgic_get_irq(vcpu->domain, vcpu, intid);
+
+        spin_lock_irqsave(&irq->irq_lock, flags);
+
+        /* Always preserve the active bit */
+        irq->active = !!(val & GICH_LR_ACTIVE_BIT);
+
+        /* Edge is the only case where we preserve the pending bit */
+        if ( irq->config == VGIC_CONFIG_EDGE && (val &

GICH_LR_PENDING_BIT) )
+        {
+            irq->pending_latch = true;

xen/arch/arm/vgic/vgic-v2.c new file +5 -0
+
+            if ( vgic_irq_is_sgi(intid) )
+            {
+                u32 cpuid = val & GICH_LR_PHYSID_CPUID;
+
Quoted 8 lines
Quoted 5 lines

+                cpuid >>= GICH_LR_PHYSID_CPUID_SHIFT; +                irq->source |= (1 << cpuid); +            }
+        }
+

May I ask to keep the big comments from KVM around? It looks quite useful to have it.

Indeed I somehow lost that. Added now.

Quoted 4 lines
Quoted 1 line

+        if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL &&

You probably want to have the helper vgic_irq_is_mapped_level(...) as in KVM.

Yes.

Cheers,
Andre

Quoted 246 lines
Quoted 21 lines

+            (val & GICH_LR_PENDING_BIT) )
+        {
+            irq->line_level = gic_read_pending_state(irq->hwintid);

xen/arch/arm/vgic/vgic-v2.c new file +15 -0
+
+            if ( !irq->line_level )
+                            gic_set_active_state(irq->hwintid, true);
+        }
+
+        spin_unlock_irqrestore(&irq->irq_lock, flags);
+        vgic_put_irq(vcpu->domain, irq);
+    }
+
+    vgic_cpu->used_lrs = 0;
+}
+
+/*
+ * Populates the particular LR with the state of a given IRQ:
+ * - for an edge sensitive IRQ the pending state is cleared in struct

vgic_irq
+ * - for a level sensitive IRQ the pending state value is unchanged;
+ *   it is dictated directly by the input level

Quoted 8 lines

+ *
+ * If @irq describes an SGI with multiple sources, we choose the
+ * lowest-numbered source VCPU and clear that bit in the source bitmap.
+ *
+ * The irq_lock must be held by the caller.
+ */
+void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr)

I am wondering how much we could share this code with vgic_v3_populate_lr.

Quoted 40 lines
xen/arch/arm/vgic/vgic-v2.c new file +40 -0
+{
+    u32 val = irq->intid;
+
+    if ( irq_is_pending(irq) )
+    {
+        val |= GICH_LR_PENDING_BIT;
+
+        if ( irq->config == VGIC_CONFIG_EDGE )
+            irq->pending_latch = false;
+
+        if ( vgic_irq_is_sgi(irq->intid) )
+        {
+            u32 src = ffs(irq->source);
+
+            BUG_ON(!src);
+            val |= (src - 1) << GICH_LR_PHYSID_CPUID_SHIFT;
+            irq->source &= ~(1 << (src - 1));
+            if ( irq->source )
+                irq->pending_latch = true;
+        }
+    }
+
+    if ( irq->active )
+        val |= GICH_LR_ACTIVE_BIT;
+
+    if ( irq->hw )
+    {
+        val |= GICH_LR_HW;
+        val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
+        /*
+         * Never set pending+active on a HW interrupt, as the
+         * pending state is kept at the physical distributor
+         * level.
+         */
+        if ( irq->active && irq_is_pending(irq) )
+            val &= ~GICH_LR_PENDING_BIT;
+    }
+    else
+    {
+        if ( irq->config == VGIC_CONFIG_LEVEL )

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

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+            val |= GICH_LR_EOI;
+    }
+
+    /*
+     * Level-triggered mapped IRQs are special because we only observe
+     * rising edges as input to the VGIC.  We therefore lower the line
+     * level here, so that we can take new virtual IRQs.  See
+     * vgic_v2_fold_lr_state for more info.
+     */
+    if ( irq->hw && irq->config == VGIC_CONFIG_LEVEL &&

Same remark for the helper.

Quoted 37 lines

+        (val & GICH_LR_PENDING_BIT) )
+        irq->line_level = false;

xen/arch/arm/vgic/vgic-v2.c new file +29 -0
+
+    /* The GICv2 LR only holds five bits of priority. */
+    val |= (irq->priority >> 3) << GICH_LR_PRIORITY_SHIFT;
+
+    vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = val;
+}
+
+void vgic_v2_clear_lr(struct vcpu *vcpu, int lr)
+{
+    vcpu->arch.vgic_cpu.vgic_v2.vgic_lr[lr] = 0;
+}
+
+static void save_lrs(struct vcpu *vcpu, void __iomem *base)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+    u64 elrsr;
+    int i;
+
+    elrsr = readl_relaxed(base + GICH_ELRSR0);
+    if ( unlikely(used_lrs > 32) )
+        elrsr |= ((u64)readl_relaxed(base + GICH_ELRSR1)) << 32;
+
+    for ( i = 0; i < used_lrs; i++ )
+    {
+        if ( elrsr & (1UL << i) )
+            cpu_if->vgic_lr[i] &= ~GICH_LR_STATE;
+        else
+            cpu_if->vgic_lr[i] = readl_relaxed(base + GICH_LR0 + (i *

4));

xen/arch/arm/vgic/vgic-v2.c new file +5 -0
+
+        writel_relaxed(0, base + GICH_LR0 + (i * 4));
+    }
+}
+

xen/arch/arm/vgic/vgic-v2.c line 236

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+void vgic_v2_save_state(struct vcpu *vcpu)
+{
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+
+    if ( used_lrs )
+    {
+        save_lrs(vcpu, gic_v2_hw_data.hbase);
+        writel_relaxed(0, gic_v2_hw_data.hbase + GICH_HCR);
+    }
+}

I am not entirely convinced that have a separate function to save the LRs is necessary. This could be done in fold_lr_state().

Quoted 5 lines
xen/arch/arm/vgic/vgic-v2.c new file +5 -0
+
+void vgic_v2_restore_state(struct vcpu *vcpu)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;

xen/arch/arm/vgic/vgic-v2.c line 251

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+    int i;
+
+    if ( used_lrs )
+    {
+        writel_relaxed(cpu_if->vgic_hcr,
+                       gic_v2_hw_data.hbase + GICH_HCR);
+        for ( i = 0; i < used_lrs; i++ )
+            writel_relaxed(cpu_if->vgic_lr[i],
+                           gic_v2_hw_data.hbase + GICH_LR0 + (i * 4));
+    }

Same here but with populate_lr_state(). This would make the code easier to follow and also avoid a lot ifery in the vgic.c code.

Quoted 91 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic.c +9 -0
index a1f77130d4..f4f2a04a60 100644
@@ -488,6 +488,7 @@ retry:
     static inline void vgic_fold_lr_state(struct vcpu *vcpu)
   {
+    vgic_v2_fold_lr_state(vcpu);
   }
     /* Requires the irq_lock to be held. */
@@ -495,14 +496,18 @@ static inline void vgic_populate_lr(struct vcpu
 *vcpu,
                                       struct vgic_irq *irq, int lr)
   {
       ASSERT(spin_is_locked(&irq->irq_lock));
+
+    vgic_v2_populate_lr(vcpu, irq, lr);
   }
     static inline void vgic_clear_lr(struct vcpu *vcpu, int lr)
   {
+    vgic_v2_clear_lr(vcpu, lr);
   }
     static inline void vgic_set_underflow(struct vcpu *vcpu)
   {
+    vgic_v2_set_underflow(vcpu);
   }
     /* Requires the ap_list_lock to be held. */
@@ -573,6 +578,11 @@ next:
           vgic_clear_lr(vcpu, count);
   }
   +static inline void vgic_save_state(struct vcpu *vcpu)
+{
+    vgic_v2_save_state(vcpu);
+}
+
   /*
    * gic_clear_lrs() - Update the VGIC state from hardware after a

guest's run.
   * @vcpu: the VCPU.

xen/arch/arm/vgic/vgic.c +5 -0
@@ -592,11 +602,18 @@ void gic_clear_lrs(struct vcpu *vcpu)
       if ( list_empty(&vcpu->arch.vgic_cpu.ap_list_head) )
           return;
   +    vgic_save_state(vcpu);
+
       if ( vgic_cpu->used_lrs )
           vgic_fold_lr_state(vcpu);
       vgic_prune_ap_list(vcpu);
   }
   +static inline void vgic_restore_state(struct vcpu *vcpu)
+{
+    vgic_v2_restore_state(vcpu);
+}
+
   /*
    * gic_inject() - flush the emulation state into the hardware on

guest entry
   *

xen/arch/arm/vgic/vgic.c +3 -0
@@ -625,7 +642,10 @@ void gic_inject(void)
       spin_lock(&current->arch.vgic_cpu.ap_list_lock);
       vgic_flush_lr_state(current);
       spin_unlock(&current->arch.vgic_cpu.ap_list_lock);
+
+    vgic_restore_state(current);
   }
+
   /*
    * Local variables:
    * mode: C
xen/arch/arm/vgic/vgic.h +1 -0
index 47fc58b81e..771ca6f046 100644
@@ -41,4 +41,12 @@ static inline void vgic_get_irq_kref(struct
 vgic_irq *irq)
       atomic_inc(&irq->refcount);
   }
   +void vgic_v2_fold_lr_state(struct vcpu *vcpu);
+void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int

lr);

xen/arch/arm/vgic/vgic.h +2 -0
+void vgic_v2_clear_lr(struct vcpu *vcpu, int lr);
+void vgic_v2_set_underflow(struct vcpu *vcpu);
Quoted 9 lines
Quoted 6 lines
xen/arch/arm/vgic/vgic.h +4 -0
+
+void vgic_v2_save_state(struct vcpu *vcpu);
+void vgic_v2_restore_state(struct vcpu *vcpu);
+

  #endif

Cheers,

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

#69 lore raw

Re: [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend

Hi Andre,

Quoted 72 lines · Andre Przywara

Hi,

Quoted 60 lines · Julien Grall

Hi,

Quoted 57 lines · Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
   xen/arch/arm/vgic/vgic-v2.c | 261
++++++++++++++++++++++++++++++++++++++++++++
   xen/arch/arm/vgic/vgic.c    |  20 ++++
   xen/arch/arm/vgic/vgic.h    |   8 ++
   3 files changed, 289 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic-v2.c

xen/arch/arm/vgic/vgic-v2.c new file +32 -0
new file mode 100644
index 0000000000..10fc467ffa
@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm/arm_vgic.h>
+#include <asm/bug.h>
+#include <asm/io.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+
+#include "vgic.h"
+
+#define GICH_ELRSR0                     0x30
+#define GICH_ELRSR1                     0x34
+#define GICH_LR0                        0x100
+
+#define GICH_LR_VIRTUALID               (0x3ff << 0)
+#define GICH_LR_PHYSID_CPUID_SHIFT      (10)
+#define GICH_LR_PHYSID_CPUID            (0x3ff <<

GICH_LR_PHYSID_CPUID_SHIFT)

Quoted 8 lines

xen/arch/arm/vgic/vgic-v2.c line 38

Quoted 6 lines
xen/arch/arm/vgic/vgic-v2.c new file +6 -0
+#define GICH_LR_PRIORITY_SHIFT          23
+#define GICH_LR_STATE                   (3 << 28)
+#define GICH_LR_PENDING_BIT             (1 << 28)
+#define GICH_LR_ACTIVE_BIT              (1 << 29)
+#define GICH_LR_EOI                     (1 << 19)
+#define GICH_LR_HW                      (1 << 31)

Can we define them in either in gic.h or a new header gic-v2.h?

Quoted 4 lines

Yes, but they clash with some ill-named GICv3 LR bits. So expect another patch which renames GICH_LR_STATE_SHIFT to ICH_LR_STATE_SHIFT. Which is the actual spec name for that system register in GICv3, there is no GICH_LR_ with the GICv3 bit positions.

While this would be a nice clean-up. Wouldn't create a new gic-v2.h sufficient?

Quoted 59 lines
Quoted 37 lines
Quoted 37 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+
+static struct {
+    bool enabled;
+    paddr_t dbase;          /* Distributor interface address */
+    paddr_t cbase;          /* CPU interface address & size */
+    paddr_t csize;
+    paddr_t vbase;          /* Virtual CPU interface address */
+    void __iomem *hbase;        /* Hypervisor control interface */
+
+    /* Offset to add to get an 8kB contiguous region if GIC is

aliased */
+    uint32_t aliased_offset;

xen/arch/arm/vgic/vgic-v2.c new file +23 -0
+} gic_v2_hw_data;
+
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+              paddr_t vbase, void __iomem *hbase,
+              uint32_t aliased_offset)
+{
+    gic_v2_hw_data.enabled = true;
+    gic_v2_hw_data.dbase = dbase;
+    gic_v2_hw_data.cbase = cbase;
+    gic_v2_hw_data.csize = csize;
+    gic_v2_hw_data.vbase = vbase;
+    gic_v2_hw_data.hbase = hbase;
+    gic_v2_hw_data.aliased_offset = aliased_offset;
+}
+
+void vgic_v2_set_underflow(struct vcpu *vcpu)
+{
+    gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 1);
+}
+
+/*
+ * transfer the content of the LRs back into the corresponding ap_list:
+ * - active bit is transferred as is
Quoted 8 lines
Quoted 5 lines

+ * - pending bit is
+ *   - transferred as is in case of edge sensitive IRQs
+ *   - set to the line-level (resample time) for level sensitive IRQs
+ */
+void vgic_v2_fold_lr_state(struct vcpu *vcpu)

I am wondering how much we could share this code with vgic_v3_fold_lr_state.

I think we discussed this and dismissed the idea:
- The actual LR encoding is much different between GICv3 and GICv2, up to the point where we have some fields in one which are not in the other. That really clutters the code.
- Originally this function was much shorter and didn't have that many special cases. So the code duplication was really minimal.

I see your point, but don't really want to go there now for two reasons:
- It is probably nasty to implement, since we always have to check which GIC we are running on when masking the LR value.
- It would deviate further from the KVM implementation, in a core function. For any bugs introduced we are on our own here.

Quoted 4 lines

I will try to bring this up with the KVM people, to see whether it's worth to revisit this decision. There is indeed quite some code duplication these days.
But this may come as an optimization later.

Fine with me. It was mostly to avoid having to review twice the same hairy code.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend

Hi,

Quoted 77 lines · Julien Grall

Hi Andre,

Quoted 74 lines · Andre Przywara

Hi,

Quoted 70 lines · Julien Grall

Hi,

Quoted 59 lines · Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
   xen/arch/arm/vgic/vgic-v2.c | 261
++++++++++++++++++++++++++++++++++++++++++++
   xen/arch/arm/vgic/vgic.c    |  20 ++++
   xen/arch/arm/vgic/vgic.h    |   8 ++

   3 files changed, 289 insertions(+)

   create mode 100644 xen/arch/arm/vgic/vgic-v2.c

xen/arch/arm/vgic/vgic-v2.c new file +5 -0
new file mode 100644
index 0000000000..10fc467ffa
@@ -0,0 +1,261 @@
+/*
+ * Copyright (C) 2015, 2016 ARM Ltd.
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or

modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see
<http://www.gnu.org/licenses/>.
+ */

xen/arch/arm/vgic/vgic-v2.c new file +16 -0
+
+#include <asm/arm_vgic.h>
+#include <asm/bug.h>
+#include <asm/io.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+
+#include "vgic.h"
+
+#define GICH_ELRSR0                     0x30
+#define GICH_ELRSR1                     0x34
+#define GICH_LR0                        0x100
+
+#define GICH_LR_VIRTUALID               (0x3ff << 0)
+#define GICH_LR_PHYSID_CPUID_SHIFT      (10)
+#define GICH_LR_PHYSID_CPUID            (0x3ff <<

GICH_LR_PHYSID_CPUID_SHIFT)

xen/arch/arm/vgic/vgic-v2.c line 38

Quoted 6 lines
xen/arch/arm/vgic/vgic-v2.c new file +6 -0
+#define GICH_LR_PRIORITY_SHIFT          23
+#define GICH_LR_STATE                   (3 << 28)
+#define GICH_LR_PENDING_BIT             (1 << 28)
+#define GICH_LR_ACTIVE_BIT              (1 << 29)
+#define GICH_LR_EOI                     (1 << 19)
+#define GICH_LR_HW                      (1 << 31)

Can we define them in either in gic.h or a new header gic-v2.h?

Quoted 7 lines
Quoted 4 lines

Yes, but they clash with some ill-named GICv3 LR bits. So expect another patch which renames GICH_LR_STATE_SHIFT to ICH_LR_STATE_SHIFT. Which is the actual spec name for that system register in GICv3, there is no GICH_LR_ with the GICv3 bit positions.

While this would be a nice clean-up. Wouldn't create a new gic-v2.h sufficient?

I don't think that would be right. We actually already have some GICH_ definitions in xen/include/asm-arm/gic.h, so just adding the missing ones there sounds natural. I now remember that I just didn't do this initially because of the clash and and at this time I just wanted to make it compile ;-)

And since assigning GICH_ names to GICv3 ICH_ register bits sounds wrong in the first place, I consider this a good opportunity to fix this.

Cheers,
Andre.

Quoted 66 lines
Quoted 61 lines
Quoted 39 lines
Quoted 39 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+
+static struct {
+    bool enabled;
+    paddr_t dbase;          /* Distributor interface address */
+    paddr_t cbase;          /* CPU interface address & size */
+    paddr_t csize;
+    paddr_t vbase;          /* Virtual CPU interface address */
+    void __iomem *hbase;        /* Hypervisor control interface */
+
+    /* Offset to add to get an 8kB contiguous region if GIC is

aliased */
+    uint32_t aliased_offset;

xen/arch/arm/vgic/vgic-v2.c new file +22 -0
+} gic_v2_hw_data;
+
+void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
+              paddr_t vbase, void __iomem *hbase,
+              uint32_t aliased_offset)
+{
+    gic_v2_hw_data.enabled = true;
+    gic_v2_hw_data.dbase = dbase;
+    gic_v2_hw_data.cbase = cbase;
+    gic_v2_hw_data.csize = csize;
+    gic_v2_hw_data.vbase = vbase;
+    gic_v2_hw_data.hbase = hbase;
+    gic_v2_hw_data.aliased_offset = aliased_offset;
+}
+
+void vgic_v2_set_underflow(struct vcpu *vcpu)
+{
+    gic_hw_ops->update_hcr_status(GICH_HCR_UIE, 1);
+}
+
+/*
+ * transfer the content of the LRs back into the corresponding

ap_list:
+ * - active bit is transferred as is

Quoted 8 lines
Quoted 5 lines

+ * - pending bit is
+ *   - transferred as is in case of edge sensitive IRQs
+ *   - set to the line-level (resample time) for level sensitive IRQs
+ */
+void vgic_v2_fold_lr_state(struct vcpu *vcpu)

I am wondering how much we could share this code with vgic_v3_fold_lr_state.

I think we discussed this and dismissed the idea:
- The actual LR encoding is much different between GICv3 and GICv2, up to the point where we have some fields in one which are not in the other. That really clutters the code.
- Originally this function was much shorter and didn't have that many special cases. So the code duplication was really minimal.

I see your point, but don't really want to go there now for two reasons:
- It is probably nasty to implement, since we always have to check which GIC we are running on when masking the LR value.
- It would deviate further from the KVM implementation, in a core function. For any bugs introduced we are on our own here.

Quoted 4 lines

I will try to bring this up with the KVM people, to see whether it's worth to revisit this decision. There is indeed quite some code duplication these days.
But this may come as an optimization later.

Quoted 5 lines

Fine with me. It was mostly to avoid having to review twice the same hairy code.

Cheers,

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

#71 lore raw

Re: [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend

In reply to Julien Grall

Hi,

forgot to mention:

Quoted 26 lines · Julien Grall

Hi,

Quoted 23 lines · Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
  xen/arch/arm/vgic/vgic-v2.c | 261
++++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.c    |  20 ++++
  xen/arch/arm/vgic/vgic.h    |   8 ++

  3 files changed, 289 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic-v2.c

xen/arch/arm/vgic/vgic-v2.c new file +0 -0
new file mode 100644
index 0000000000..10fc467ffa

....

Quoted 24 lines

xen/arch/arm/vgic/vgic-v2.c line 236

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+void vgic_v2_save_state(struct vcpu *vcpu)
+{
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+
+    if ( used_lrs )
+    {
+        save_lrs(vcpu, gic_v2_hw_data.hbase);
+        writel_relaxed(0, gic_v2_hw_data.hbase + GICH_HCR);
+    }
+}

I am not entirely convinced that have a separate function to save the LRs is necessary. This could be done in fold_lr_state().

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+
+void vgic_v2_restore_state(struct vcpu *vcpu)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+    int i;
+
+    if ( used_lrs )
+    {
+        writel_relaxed(cpu_if->vgic_hcr,
Quoted 8 lines
Quoted 5 lines

+                       gic_v2_hw_data.hbase + GICH_HCR);
+        for ( i = 0; i < used_lrs; i++ )
+            writel_relaxed(cpu_if->vgic_lr[i],
+                           gic_v2_hw_data.hbase + GICH_LR0 + (i * 4)); +    }

Same here but with populate_lr_state(). This would make the code easier to follow and also avoid a lot ifery in the vgic.c code.

This is mostly due to KVM's inability to directly access the GICv3 LRs when running in EL1. I will take a look whether what it would take to merge this. Sounds tempting, but there might be side effects.

Cheers,
Andre.

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

#72 lore raw

Re: [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend

Quoted 1 line · Andre Przywara

Hi,

Hi,

Quoted 65 lines

forgot to mention:

Quoted 26 lines · Julien Grall

Hi,

Quoted 23 lines · Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
   xen/arch/arm/vgic/vgic-v2.c | 261
++++++++++++++++++++++++++++++++++++++++++++
   xen/arch/arm/vgic/vgic.c    |  20 ++++
   xen/arch/arm/vgic/vgic.h    |   8 ++
   3 files changed, 289 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic-v2.c

xen/arch/arm/vgic/vgic-v2.c new file +0 -0
new file mode 100644
index 0000000000..10fc467ffa

....

Quoted 24 lines

xen/arch/arm/vgic/vgic-v2.c line 236

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+void vgic_v2_save_state(struct vcpu *vcpu)
+{
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+
+    if ( used_lrs )
+    {
+        save_lrs(vcpu, gic_v2_hw_data.hbase);
+        writel_relaxed(0, gic_v2_hw_data.hbase + GICH_HCR);
+    }
+}

I am not entirely convinced that have a separate function to save the LRs is necessary. This could be done in fold_lr_state().

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+
+void vgic_v2_restore_state(struct vcpu *vcpu)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;,
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+    int i;
+
+    if ( used_lrs )
+    {
+        writel_relaxed(cpu_if->vgic_hcr,
Quoted 8 lines
Quoted 5 lines

+                       gic_v2_hw_data.hbase + GICH_HCR);
+        for ( i = 0; i < used_lrs; i++ )
+            writel_relaxed(cpu_if->vgic_lr[i],
+                           gic_v2_hw_data.hbase + GICH_LR0 + (i * 4)); +    }

Same here but with populate_lr_state(). This would make the code easier to follow and also avoid a lot ifery in the vgic.c code.

Quoted 3 lines

This is mostly due to KVM's inability to directly access the GICv3 LRs when running in EL1. I will take a look whether what it would take to merge this. Sounds tempting, but there might be side effects.

I am not sure what would be the side effects. You basically
call save_state and right after fold_lr_state. This would streamline a bit more the code.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 25/49] ARM: new VGIC: Add GICv2 world switch backend

Hi,

Quoted 72 lines · Julien Grall
Quoted 1 line · Andre Przywara

Hi,

Hi,

Quoted 65 lines

forgot to mention:

Quoted 26 lines · Julien Grall

Hi,

Quoted 23 lines · Andre Przywara

Processing maintenance interrupts and accessing the list registers are dependent on the host's GIC version.
Introduce vgic-v2.c to contain GICv2 specific functions. Implement the GICv2 specific code for syncing the emulation state into the VGIC registers.
This also adds the hook to let Xen setup the host GIC addresses.

This is based on Linux commit 140b086dd197, written by Marc Zyngier.

---
   xen/arch/arm/vgic/vgic-v2.c | 261
++++++++++++++++++++++++++++++++++++++++++++
   xen/arch/arm/vgic/vgic.c    |  20 ++++
   xen/arch/arm/vgic/vgic.h    |   8 ++

   3 files changed, 289 insertions(+)

   create mode 100644 xen/arch/arm/vgic/vgic-v2.c

xen/arch/arm/vgic/vgic-v2.c new file +0 -0
new file mode 100644
index 0000000000..10fc467ffa

....

Quoted 32 lines

xen/arch/arm/vgic/vgic-v2.c line 236

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+void vgic_v2_save_state(struct vcpu *vcpu)
+{
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;
+
+    if ( used_lrs )
+    {
+        save_lrs(vcpu, gic_v2_hw_data.hbase);
+        writel_relaxed(0, gic_v2_hw_data.hbase + GICH_HCR);
+    }
+}

I am not entirely convinced that have a separate function to save the LRs is necessary. This could be done in fold_lr_state().

Quoted 5 lines
xen/arch/arm/vgic/vgic-v2.c new file +5 -0
+
+void vgic_v2_restore_state(struct vcpu *vcpu)
+{
+    struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2;,
+    u64 used_lrs = vcpu->arch.vgic_cpu.used_lrs;

xen/arch/arm/vgic/vgic-v2.c line 251

Quoted 10 lines
xen/arch/arm/vgic/vgic-v2.c new file +10 -0
+    int i;
+
+    if ( used_lrs )
+    {
+        writel_relaxed(cpu_if->vgic_hcr,
+                       gic_v2_hw_data.hbase + GICH_HCR);
+        for ( i = 0; i < used_lrs; i++ )
+            writel_relaxed(cpu_if->vgic_lr[i],
+                           gic_v2_hw_data.hbase + GICH_LR0 + (i * 4));
+    }

Same here but with populate_lr_state(). This would make the code easier to follow and also avoid a lot ifery in the vgic.c code.

Quoted 7 lines
Quoted 3 lines

This is mostly due to KVM's inability to directly access the GICv3 LRs when running in EL1. I will take a look whether what it would take to merge this. Sounds tempting, but there might be side effects.

I am not sure what would be the side effects. You basically call save_state and right after fold_lr_state. This would streamline a bit more the code.

The possible side effects are that we actually now have a shadow copy of the LRs in our data structures. I have the gut feeling we don't need this in Xen, but need to check more thoroughly.

Cheers,
Andre.

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

#74 lore raw

RFC PATCH 26/49 ARM: new VGIC: Implement vgic_vcpu_pending_irq

In reply to Andre Przywara

Tell Xen whether a particular VCPU has an IRQ that needs handling in the guest. This is used to decide whether a VCPU is runnable.

This is based on Linux commit 90eee56c5f90, written by Eric Auger.

---
 xen/arch/arm/vgic/vgic.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
xen/arch/arm/vgic/vgic.c +32 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
     vgic_restore_state(current);
 }
 
+static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)
+{
+    return vgic_vcpu_pending_irq(current);
+}
+
 /*
  * Local variables:
  * mode: C
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

Hi,

Quoted 36 lines · Andre Przywara

Tell Xen whether a particular VCPU has an IRQ that needs handling in the guest. This is used to decide whether a VCPU is runnable.

This is based on Linux commit 90eee56c5f90, written by Eric Auger.

---
  xen/arch/arm/vgic/vgic.c | 32 ++++++++++++++++++++++++++++++++
  1 file changed, 32 insertions(+)
xen/arch/arm/vgic/vgic.c +18 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
      vgic_restore_state(current);
  }
 
+static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 676

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

Quoted 5 lines · Julien Grall

Hi,

Quoted 2 lines · Andre Przywara

Tell Xen whether a particular VCPU has an IRQ that needs handling in the guest. This is used to decide whether a VCPU is runnable.

I forgot to mention one thing. This is not the main usage of this function in Xen. That function will mostly be used to check whether we need to preempt an hypercall to run interrupt.

Please update the commit message accordingly.

Quoted 48 lines
Quoted 33 lines

This is based on Linux commit 90eee56c5f90, written by Eric Auger.

---
  xen/arch/arm/vgic/vgic.c | 32 ++++++++++++++++++++++++++++++++

  1 file changed, 32 insertions(+)
xen/arch/arm/vgic/vgic.c +18 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
       vgic_restore_state(current);
   }
+static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 675

Quoted 10 lines
xen/arch/arm/vgic/vgic.c +10 -0
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

In reply to Julien Grall

Hi,

Quoted 43 lines · Julien Grall

Hi,

Quoted 40 lines · Andre Przywara

Tell Xen whether a particular VCPU has an IRQ that needs handling in the guest. This is used to decide whether a VCPU is runnable.

This is based on Linux commit 90eee56c5f90, written by Eric Auger.

---
  xen/arch/arm/vgic/vgic.c | 32 ++++++++++++++++++++++++++++++++

  1 file changed, 32 insertions(+)
xen/arch/arm/vgic/vgic.c +22 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
       vgic_restore_state(current);
   }
   +static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
Quoted 8 lines

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 676

Quoted 5 lines
xen/arch/arm/vgic/vgic.c +5 -0
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Rename to what? I need both functions: vgic_vcpu_pending_irq() is also called by vgic_kick_vcpus() (later in the series).
And gic_events_need_delivery(void) is the interface that the arch code expects. Shall I rename this there? To what?

Cheers,
Andre.

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

#78 lore raw

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

Hi,

Quoted 40 lines · Andre Przywara
Quoted 30 lines · Julien Grall
Quoted 30 lines
xen/arch/arm/vgic/vgic.c +22 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
       vgic_restore_state(current);
   }
   +static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
Quoted 8 lines

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 676

Quoted 5 lines
xen/arch/arm/vgic/vgic.c +5 -0
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Quoted 4 lines

Rename to what? I need both functions: vgic_vcpu_pending_irq() is also called by vgic_kick_vcpus() (later in the series).
And gic_events_need_delivery(void) is the interface that the arch code expects. Shall I rename this there? To what?

Let me start with it is a bit odd to have a function name 'gic_*' in the virtual GIC code. So at least renaming to vgic_events_need_delivery would be an improvement.

Regarding the interface itself, it is ARM specific and not set in stone. It would not be too bad to use vgic_vcpu_pending_irq(current). Is there any reason for not doing that?

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

Hi,

Quoted 48 lines · Julien Grall

Hi,

Quoted 40 lines · Andre Przywara
Quoted 30 lines · Julien Grall
Quoted 30 lines
xen/arch/arm/vgic/vgic.c +22 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
        vgic_restore_state(current);
    }
    +static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
Quoted 8 lines

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 676

Quoted 5 lines
xen/arch/arm/vgic/vgic.c +5 -0
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Quoted 4 lines

Rename to what? I need both functions: vgic_vcpu_pending_irq() is also called by vgic_kick_vcpus() (later in the series).
And gic_events_need_delivery(void) is the interface that the arch code expects. Shall I rename this there? To what?

Quoted 7 lines

Let me start with it is a bit odd to have a function name 'gic_*' in the virtual GIC code. So at least renaming to vgic_events_need_delivery would be an improvement.

Regarding the interface itself, it is ARM specific and not set in stone. It would not be too bad to use vgic_vcpu_pending_irq(current). Is there any reason for not doing that?

Not really, but I am a bit reluctant to change too much original Xen code, don't want to step on anyone's toes ;-)

But if that's fine with you, I am OK with the renaming - though it adds yet another patch ;-)

Cheers,
Andre.

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

#80 lore raw

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

Quoted 55 lines · Andre Przywara

Hi,

Quoted 52 lines · Julien Grall

Hi,

Quoted 40 lines · Andre Przywara
Quoted 30 lines · Julien Grall
Quoted 30 lines
xen/arch/arm/vgic/vgic.c +22 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
        vgic_restore_state(current);
    }
    +static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
Quoted 8 lines

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 676

Quoted 5 lines
xen/arch/arm/vgic/vgic.c +5 -0
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Quoted 4 lines

Rename to what? I need both functions: vgic_vcpu_pending_irq() is also called by vgic_kick_vcpus() (later in the series).
And gic_events_need_delivery(void) is the interface that the arch code expects. Shall I rename this there? To what?

Let me start with it is a bit odd to have a function name 'gic_*' in the virtual GIC code. So at least renaming to vgic_events_need_delivery would be an improvement.

Quoted 6 lines
Quoted 3 lines

Regarding the interface itself, it is ARM specific and not set in stone. It would not be too bad to use vgic_vcpu_pending_irq(current). Is there any reason for not doing that?

Not really, but I am a bit reluctant to change too much original Xen code, don't want to step on anyone's toes ;-)

The original code is going to get kill at some point. So better use name that makes sense in the new context. It is quite similar to the gic_inject change.

Quoted 3 lines

But if that's fine with you, I am OK with the renaming - though it adds yet another patch ;-)

The end goal is a better world, so the number of patches does not matter here :).

If you put them at the beginning, we can merge them right away.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

Hi,

Quoted 54 lines · Julien Grall
Quoted 51 lines · Andre Przywara

Hi,

Quoted 48 lines · Julien Grall

Hi,

Quoted 44 lines · Andre Przywara
Quoted 30 lines · Julien Grall
Quoted 30 lines
xen/arch/arm/vgic/vgic.c +22 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
         vgic_restore_state(current);
     }
     +static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
Quoted 8 lines

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 676

Quoted 5 lines
xen/arch/arm/vgic/vgic.c +5 -0
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Rename to what? I need both functions: vgic_vcpu_pending_irq() is also called by vgic_kick_vcpus() (later in the series).
And gic_events_need_delivery(void) is the interface that the arch code expects. Shall I rename this there? To what?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Let me start with it is a bit odd to have a function name 'gic_*' in the virtual GIC code. So at least renaming to vgic_events_need_delivery would be an improvement.

Regarding the interface itself, it is ARM specific and not set in stone. It would not be too bad to use vgic_vcpu_pending_irq(current). Is there any reason for not doing that?

The two interfaces used for that purpose are different in the two VGICs:
- The old VGIC only works on the current VCPU, since it peeks into the GICH_ register to learn the priority (regardless of whether this is really needed or useful).
- The new VGIC can use this function for any VCPU, and we need this functionality later on (when we iterate over all VCPUs).
So we can't use a function hardwiring "current", that would break vgic_kick_vcpus() in the new VGIC. And we can't pass a VCPU parameter, that would not work for the old VGIC.
So I believe having this small wrapper here is the easiest solution. I will add a patch to rename this function to vgic_pending_irq(), though, so this one here looks like:
int vgic_pending_irq(void)

{
    return vgic_vcpu_pending_irq(current);
}

We can clean this up when the old VGIC gets removed.

Cheers,
Andre.

Quoted 11 lines
Quoted 2 lines

Not really, but I am a bit reluctant to change too much original Xen code, don't want to step on anyone's toes ;-)

The original code is going to get kill at some point. So better use name that makes sense in the new context. It is quite similar to the gic_inject change.

Quoted 3 lines

But if that's fine with you, I am OK with the renaming - though it adds yet another patch ;-)

Quoted 7 lines

The end goal is a better world, so the number of patches does not matter here :).

If you put them at the beginning, we can merge them right away.

Cheers,

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

#82 lore raw

Re: [RFC PATCH 26/49] ARM: new VGIC: Implement vgic_vcpu_pending_irq

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 76 lines · Julien Grall
Quoted 54 lines · Andre Przywara
Quoted 51 lines

Hi,

Quoted 48 lines · Julien Grall

Hi,

Quoted 44 lines · Andre Przywara
Quoted 30 lines · Julien Grall
Quoted 30 lines
xen/arch/arm/vgic/vgic.c +22 -0
index f4f2a04a60..9e7fb1edcb 100644
@@ -646,6 +646,38 @@ void gic_inject(void)
         vgic_restore_state(current);
     }
     +static int vgic_vcpu_pending_irq(struct vcpu *vcpu)
+{
+    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+    struct vgic_irq *irq;
+    bool pending = false;
+    unsigned long flags;
+
+    if ( !vcpu->domain->arch.vgic.enabled )
+        return false;
+
+    spin_lock_irqsave(&vgic_cpu->ap_list_lock, flags);
+
+    list_for_each_entry(irq, &vgic_cpu->ap_list_head, ap_list)
+    {
+        spin_lock(&irq->irq_lock);
+        pending = irq_is_pending(irq) && irq->enabled;
+        spin_unlock(&irq->irq_lock);
+
+        if ( pending )
+            break;
+    }
+
+    spin_unlock_irqrestore(&vgic_cpu->ap_list_lock, flags);
Quoted 8 lines

xen/arch/arm/vgic/vgic.c void gic_inject(void) · line 675

Quoted 5 lines
xen/arch/arm/vgic/vgic.c +5 -0
+
+    return pending;
+}
+
+int gic_events_need_delivery(void)

You probably want to rename that function or just expose vgic_vcpu_pending_irq().

Rename to what? I need both functions: vgic_vcpu_pending_irq() is also called by vgic_kick_vcpus() (later in the series).
And gic_events_need_delivery(void) is the interface that the arch code expects. Shall I rename this there? To what?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Let me start with it is a bit odd to have a function name 'gic_*' in the virtual GIC code. So at least renaming to vgic_events_need_delivery would be an improvement.

Regarding the interface itself, it is ARM specific and not set in stone. It would not be too bad to use vgic_vcpu_pending_irq(current). Is there any reason for not doing that?

The two interfaces used for that purpose are different in the two VGICs:
- The old VGIC only works on the current VCPU, since it peeks into the GICH_ register to learn the priority (regardless of whether this is really needed or useful).
- The new VGIC can use this function for any VCPU, and we need this functionality later on (when we iterate over all VCPUs).
So we can't use a function hardwiring "current", that would break vgic_kick_vcpus() in the new VGIC. And we can't pass a VCPU parameter, that would not work for the old VGIC.
So I believe having this small wrapper here is the easiest solution. I will add a patch to rename this function to vgic_pending_irq(), though, so this one here looks like:
int vgic_pending_irq(void)

Quoted 5 lines
{
     return vgic_vcpu_pending_irq(current);
}

We can clean this up when the old VGIC gets removed.

Likely no-one in the old vGIC are going to call that function with v != current. This would not be the only place in Xen where a vCPU is taken in parameter but effectively v can only be current. That's where ASSERT(v == current) comes into place.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 27/49 ARM: new VGIC: Add MMIO handling framework

In reply to Andre Przywara

Add an MMIO handling framework to the VGIC emulation:
Each register is described by its offset, size (or number of bits per IRQ, if applicable) and the read/write handler functions. We provide initialization macros to describe each GIC register later easily.

Separate dispatch functions for read and write accesses are connected to Xen's MMIO handling framework and binary-search for the responsible register handler based on the offset address within the region.

The register handler prototype are courtesy of Christoffer Dall.

This is based on Linux commit 4493b1c4866a, written by Marc Zyngier.

---
 xen/arch/arm/vgic/vgic-mmio.c | 192 ++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic-mmio.h | 145 +++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic.h      |   4 +
 3 files changed, 341 insertions(+)
 create mode 100644 xen/arch/arm/vgic/vgic-mmio.c
 create mode 100644 xen/arch/arm/vgic/vgic-mmio.h
xen/arch/arm/vgic/vgic-mmio.c new file +192 -0
new file mode 100644
index 0000000000..3c70945466
@@ -0,0 +1,192 @@
+/*
+ * VGIC MMIO handling functions
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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/lib.h>
+#include <xen/sched.h>
+#include <asm/arm_vgic.h>
+#include <asm/byteorder.h>
+
+#include "vgic.h"
+#include "vgic-mmio.h"
+
+unsigned long vgic_mmio_read_raz(struct vcpu *vcpu,
+                 paddr_t addr, unsigned int len)
+{
+    return 0;
+}
+
+unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
+                 paddr_t addr, unsigned int len)
+{
+    return -1UL;
+}
+
+void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
+            unsigned int len, unsigned long val)
+{
+    /* Ignore */
+}
+
+static int match_region(const void *key, const void *elt)
+{
+    const unsigned int offset = (unsigned long)key;
+    const struct vgic_register_region *region = elt;
+
+    if ( offset < region->reg_offset )
+        return -1;
+
+    if ( offset >= region->reg_offset + region->len )
+        return 1;
+
+    return 0;
+}
+
+const struct vgic_register_region *
+vgic_find_mmio_region(const struct vgic_register_region *regions,
+              int nr_regions, unsigned int offset)
+{
+    return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
+               sizeof(regions[0]), match_region);
+}
+
+static bool check_region(const struct domain *d,
+             const struct vgic_register_region *region,
+             paddr_t addr, int len)
+{
+    int flags, nr_irqs = d->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
+
+    switch (len)
+    {
+    case sizeof(u8):
+        flags = VGIC_ACCESS_8bit;
+        break;
+    case sizeof(u32):
+        flags = VGIC_ACCESS_32bit;
+        break;
+    case sizeof(u64):
+        flags = VGIC_ACCESS_64bit;
+        break;
+    default:
+        return false;
+    }
+
+    if ( (region->access_flags & flags) && IS_ALIGNED(addr, len) )
+    {
+        if ( !region->bits_per_irq )
+            return true;
+
+        /* Do we access a non-allocated IRQ? */
+        return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
+    }
+
+    return false;
+}
+
+const struct vgic_register_region *
+vgic_get_mmio_region(struct vcpu *vcpu, struct vgic_io_device *iodev,
+             paddr_t addr, int len)
+{
+    const struct vgic_register_region *region;
+
+    region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
+                       addr - iodev->base_addr);
+    if ( !region || !check_region(vcpu->domain, region, addr, len) )
+        return NULL;
+
+    return region;
+}
+
+static int dispatch_mmio_read(struct vcpu *vcpu, mmio_info_t *info,
+                  register_t *r, void *priv)
+{
+    struct vgic_io_device *iodev = priv;
+    const struct vgic_register_region *region;
+    unsigned long data = 0;
+    paddr_t addr = info->gpa;
+    int len = 1U << info->dabt.size;
+
+    region = vgic_get_mmio_region(vcpu, iodev, addr, len);
+    if ( !region )
+    {
+        memset(r, 0, len);
+        return 0;
+    }
+
+    switch (iodev->iodev_type)
+    {
+    case IODEV_CPUIF:
+        data = region->read(vcpu, addr, len);
+        break;
+    case IODEV_DIST:
+        data = region->read(vcpu, addr, len);
+        break;
+    case IODEV_REDIST:
+        data = region->read(iodev->redist_vcpu, addr, len);
+        break;
+    case IODEV_ITS:
+        data = region->its_read(vcpu->domain, iodev->its, addr, len);
+        break;
+    }
+
+    memcpy(r, &data, len);
+
+    return 1;
+}
+
+static int dispatch_mmio_write(struct vcpu *vcpu, mmio_info_t *info,
+                   register_t r, void *priv)
+{
+    struct vgic_io_device *iodev = priv;
+    const struct vgic_register_region *region;
+    unsigned long data = r;
+    paddr_t addr = info->gpa;
+    int len = 1U << info->dabt.size;
+
+    region = vgic_get_mmio_region(vcpu, iodev, addr, len);
+    if ( !region )
+        return 0;
+
+    switch (iodev->iodev_type)
+    {
+    case IODEV_CPUIF:
+        region->write(vcpu, addr, len, data);
+        break;
+    case IODEV_DIST:
+        region->write(vcpu, addr, len, data);
+        break;
+    case IODEV_REDIST:
+        region->write(iodev->redist_vcpu, addr, len, data);
+        break;
+    case IODEV_ITS:
+        region->its_write(vcpu->domain, iodev->its, addr, len, data);
+        break;
+    }
+
+    return 1;
+}
+
+struct mmio_handler_ops xen_io_gic_ops = {
+    .read = dispatch_mmio_read,
+    .write = dispatch_mmio_write,
+};
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic-mmio.h new file +145 -0

145 lines not on this page · raw message

xen/arch/arm/vgic/vgic.h +4 -0
index 771ca6f046..426b34d0ce 100644
@@ -27,6 +27,10 @@ static inline bool irq_is_pending(struct vgic_irq *irq)
         return irq->pending_latch || irq->line_level;
 }
 
+const struct vgic_register_region *
+vgic_get_mmio_region(struct vcpu *vcpu, struct vgic_io_device *iodev,
+             paddr_t addr, int len);
+
 struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
                               u32 intid);
 void vgic_put_irq(struct domain *d, struct vgic_irq *irq);
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 27/49] ARM: new VGIC: Add MMIO handling framework

Hi Andre,7

Quoted 43 lines · Andre Przywara

Add an MMIO handling framework to the VGIC emulation:
Each register is described by its offset, size (or number of bits per IRQ, if applicable) and the read/write handler functions. We provide initialization macros to describe each GIC register later easily.

Separate dispatch functions for read and write accesses are connected to Xen's MMIO handling framework and binary-search for the responsible register handler based on the offset address within the region.

The register handler prototype are courtesy of Christoffer Dall.

This is based on Linux commit 4493b1c4866a, written by Marc Zyngier.

---
  xen/arch/arm/vgic/vgic-mmio.c | 192 ++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic-mmio.h | 145 +++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic.h      |   4 +
  3 files changed, 341 insertions(+)

create mode 100644 xen/arch/arm/vgic/vgic-mmio.c create mode 100644 xen/arch/arm/vgic/vgic-mmio.h

xen/arch/arm/vgic/vgic-mmio.c new file +15 -0
new file mode 100644
index 0000000000..3c70945466
@@ -0,0 +1,192 @@
+/*
+ * VGIC MMIO handling functions
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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>

xen/arch/arm/vgic/vgic-mmio.c line 25

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <asm/arm_vgic.h>
+#include <asm/byteorder.h>
+
+#include "vgic.h"
+#include "vgic-mmio.h"
+
+unsigned long vgic_mmio_read_raz(struct vcpu *vcpu,
+                 paddr_t addr, unsigned int len)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c line 31

Quoted 6 lines
xen/arch/arm/vgic/vgic-mmio.c new file +6 -0
+{
+    return 0;
+}
+
+unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
+                 paddr_t addr, unsigned int len)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c line 37

Quoted 6 lines
xen/arch/arm/vgic/vgic-mmio.c new file +6 -0
+{
+    return -1UL;
+}
+
+void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
+            unsigned int len, unsigned long val)

Indentation.

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+{
+    /* Ignore */
+}
+
+static int match_region(const void *key, const void *elt)
+{
+    const unsigned int offset = (unsigned long)key;
+    const struct vgic_register_region *region = elt;
+
+    if ( offset < region->reg_offset )

xen/arch/arm/vgic/vgic-mmio.c line 57

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+        return -1;
+
+    if ( offset >= region->reg_offset + region->len )
+        return 1;
+
+    return 0;
+}
+
+const struct vgic_register_region *
+vgic_find_mmio_region(const struct vgic_register_region *regions,

Any reason to export this?

xen/arch/arm/vgic/vgic-mmio.c line 58

Quoted 1 line
xen/arch/arm/vgic/vgic-mmio.c new file +1 -0
+              int nr_regions, unsigned int offset)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c line 66

Quoted 8 lines
xen/arch/arm/vgic/vgic-mmio.c new file +8 -0
+{
+    return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
+               sizeof(regions[0]), match_region);
+}
+
+static bool check_region(const struct domain *d,
+             const struct vgic_register_region *region,
+             paddr_t addr, int len)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c

Quoted 3 lines
xen/arch/arm/vgic/vgic-mmio.c new file +3 -0
+{
+    int flags, nr_irqs = d->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
+ > +    switch (len)

switch ( ... )

xen/arch/arm/vgic/vgic-mmio.c line 71

Quoted 2 lines
xen/arch/arm/vgic/vgic-mmio.c new file +2 -0
+    {
+    case sizeof(u8):

s/u8/uint8_t/ here an below.

Quoted 16 lines
xen/arch/arm/vgic/vgic-mmio.c new file +16 -0
+        flags = VGIC_ACCESS_8bit;
+        break;
+    case sizeof(u32):
+        flags = VGIC_ACCESS_32bit;
+        break;
+    case sizeof(u64):
+        flags = VGIC_ACCESS_64bit;
+        break;
+    default:
+        return false;
+    }
+
+    if ( (region->access_flags & flags) && IS_ALIGNED(addr, len) )
+    {
+        if ( !region->bits_per_irq )
+            return true;

xen/arch/arm/vgic/vgic-mmio.c line 98

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+
+        /* Do we access a non-allocated IRQ? */
+        return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
+    }
+
+    return false;
+}
+
+const struct vgic_register_region *
+vgic_get_mmio_region(struct vcpu *vcpu, struct vgic_io_device *iodev,

Any reason to export this?

xen/arch/arm/vgic/vgic-mmio.c line 98

Quoted 1 line
xen/arch/arm/vgic/vgic-mmio.c new file +1 -0
+             paddr_t addr, int len)

Indentation and unsigned int please.

xen/arch/arm/vgic/vgic-mmio.c line 112

Quoted 13 lines
xen/arch/arm/vgic/vgic-mmio.c new file +13 -0
+{
+    const struct vgic_register_region *region;
+
+    region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
+                       addr - iodev->base_addr);
+    if ( !region || !check_region(vcpu->domain, region, addr, len) )
+        return NULL;
+
+    return region;
+}
+
+static int dispatch_mmio_read(struct vcpu *vcpu, mmio_info_t *info,
+                  register_t *r, void *priv)

Indentation.

Quoted 58 lines
xen/arch/arm/vgic/vgic-mmio.c new file +58 -0
+{
+    struct vgic_io_device *iodev = priv;
+    const struct vgic_register_region *region;
+    unsigned long data = 0;
+    paddr_t addr = info->gpa;
+    int len = 1U << info->dabt.size;
+
+    region = vgic_get_mmio_region(vcpu, iodev, addr, len);
+    if ( !region )
+    {
+        memset(r, 0, len);
+        return 0;
+    }
+
+    switch (iodev->iodev_type)
+    {
+    case IODEV_CPUIF:
+        data = region->read(vcpu, addr, len);
+        break;
+    case IODEV_DIST:
+        data = region->read(vcpu, addr, len);
+        break;
+    case IODEV_REDIST:
+        data = region->read(iodev->redist_vcpu, addr, len);
+        break;
+    case IODEV_ITS:
+        data = region->its_read(vcpu->domain, iodev->its, addr, len);
+        break;
+    }
+
+    memcpy(r, &data, len);
+
+    return 1;
+}
+
+static int dispatch_mmio_write(struct vcpu *vcpu, mmio_info_t *info,
+                   register_t r, void *priv)
+{
+    struct vgic_io_device *iodev = priv;
+    const struct vgic_register_region *region;
+    unsigned long data = r;
+    paddr_t addr = info->gpa;
+    int len = 1U << info->dabt.size;
+
+    region = vgic_get_mmio_region(vcpu, iodev, addr, len);
+    if ( !region )
+        return 0;
+
+    switch (iodev->iodev_type)
+    {
+    case IODEV_CPUIF:
+        region->write(vcpu, addr, len, data);
+        break;
+    case IODEV_DIST:
+        region->write(vcpu, addr, len, data);
+        break;
+    case IODEV_REDIST:
+        region->write(iodev->redist_vcpu, addr, len, data);

xen/arch/arm/vgic/vgic-mmio.c line 180

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+        break;
+    case IODEV_ITS:
+        region->its_write(vcpu->domain, iodev->its, addr, len, data);
+        break;
+    }
+
+    return 1;
+}
+
+struct mmio_handler_ops xen_io_gic_ops = {

I would rename to vgic_io_ops.

Quoted 25 lines
xen/arch/arm/vgic/vgic-mmio.c new file +12 -0
+    .read = dispatch_mmio_read,
+    .write = dispatch_mmio_write,
+};
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic-mmio.h new file +7 -0
new file mode 100644
index 0000000000..375b70561d
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2015, 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 version 2 as
+ * published by the Free Software Foundation.
+ *

xen/arch/arm/vgic/vgic-mmio.h line 17

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.h new file +10 -0
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __KVM_ARM_VGIC_MMIO_H__
+#define __KVM_ARM_VGIC_MMIO_H__

Please use update the guard.

Quoted 21 lines
xen/arch/arm/vgic/vgic-mmio.h new file +21 -0
+
+struct vgic_register_region {
+    unsigned int reg_offset;
+    unsigned int len;
+    unsigned int bits_per_irq;
+    unsigned int access_flags;
+    union
+    {
+        unsigned long (*read)(struct vcpu *vcpu, paddr_t addr,
+                              unsigned int len);
+        unsigned long (*its_read)(struct domain *d, struct vgic_its *its,
+                                  paddr_t addr, unsigned int len);
+    };
+    union
+    {
+        void (*write)(struct vcpu *vcpu, paddr_t addr,
+                      unsigned int len, unsigned long val);
+        void (*its_write)(struct domain *d, struct vgic_its *its,
+                          paddr_t addr, unsigned int len,
+                          unsigned long val);
+    };

xen/arch/arm/vgic/vgic-mmio.h line 48

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.h new file +10 -0
+    unsigned long (*uaccess_read)(struct vcpu *vcpu, paddr_t addr,
+                                  unsigned int len);
+    union
+    {
+        void (*uaccess_write)(struct vcpu *vcpu, paddr_t addr,
+                              unsigned int len, unsigned long val);
+        int (*uaccess_its_write)(struct domain *d, struct vgic_its *its,
+                                 paddr_t addr, unsigned int len,
+                                 unsigned long val);
+    };

I don't think uaccess helpers makes sense for Xen.

Quoted 15 lines
xen/arch/arm/vgic/vgic-mmio.h new file +15 -0
+};
+
+extern struct mmio_handler_ops xen_io_gic_ops;
+
+#define VGIC_ACCESS_8bit    1
+#define VGIC_ACCESS_32bit   2
+#define VGIC_ACCESS_64bit   4
+
+/*
+ * Generate a mask that covers the number of bytes required to address
+ * up to 1024 interrupts, each represented by <bits> bits. This assumes
+ * that <bits> is a power of two.
+ */
+#define VGIC_ADDR_IRQ_MASK(bits) (((bits) * 1024 / 8) - 1)
+

xen/arch/arm/vgic/vgic-mmio.h line 73

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.h new file +10 -0
+/*
+ * (addr & mask) gives us the _byte_ offset for the INT ID.
+ * We multiply this by 8 the get the _bit_ offset, then divide this by
+ * the number of bits to learn the actual INT ID.
+ * But instead of a division (which requires a "long long div" implementation),
+ * we shift by the binary logarithm of <bits>.
+ * This assumes that <bits> is a power of two.
+ */
+#define VGIC_ADDR_TO_INTID(addr, bits)  (((addr) & VGIC_ADDR_IRQ_MASK(bits)) * \
+                                         8 >> LOG_2(bits))

We are going to switch to ilog2 (see Sameer's patch "xen/bitops: Rename LOG_2 to ilog2").

Quoted 35 lines
xen/arch/arm/vgic/vgic-mmio.h new file +35 -0
+
+/*
+ * Some VGIC registers store per-IRQ information, with a different number
+ * of bits per IRQ. For those registers this macro is used.
+ * The _WITH_LENGTH version instantiates registers with a fixed length
+ * and is mutually exclusive with the _PER_IRQ version.
+ */
+#define REGISTER_DESC_WITH_BITS_PER_IRQ(off, rd, wr, ur, uw, bpi, acc)  \
+    {                           \
+        .reg_offset = off,      \
+        .bits_per_irq = bpi,    \
+        .len = bpi * 1024 / 8,  \
+        .access_flags = acc,    \
+        .read = rd,             \
+        .write = wr,            \
+        .uaccess_read = ur,     \
+        .uaccess_write = uw,    \
+    }
+
+#define REGISTER_DESC_WITH_LENGTH(off, rd, wr, length, acc)     \
+    {                           \
+        .reg_offset = off,      \
+        .bits_per_irq = 0,      \
+        .len = length,          \
+        .access_flags = acc,    \
+        .read = rd,             \
+        .write = wr,            \
+    }
+
+#define REGISTER_DESC_WITH_LENGTH_UACCESS(off, rd, wr, urd, uwr, length, acc) \
+    {                           \
+        .reg_offset = off,      \
+        .bits_per_irq = 0,      \
+        .len = length,          \
+        .access_flags = acc,    \

xen/arch/arm/vgic/vgic-mmio.h line 118

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.h new file +10 -0
+        .read = rd,             \
+        .write = wr,            \
+        .uaccess_read = urd,    \
+        .uaccess_write = uwr,   \
+    }
+
+int kvm_vgic_register_mmio_region(struct domain *d, struct vcpu *vcpu,
+                                  struct vgic_register_region *reg_desc,
+                                  struct vgic_io_device *region,
+                                  int nr_irqs, bool offset_private);

You want to do some clean-up in the prototype below. Only the one used in the patch should be added. The other should either move in there corresponding patch or dropped if not used.

Quoted 32 lines
xen/arch/arm/vgic/vgic-mmio.h new file +27 -0
+
+unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len);
+
+void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
+                                unsigned long data);
+
+unsigned long extract_bytes(u64 data, unsigned int offset,
+                            unsigned int num);
+
+u64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len,
+                     unsigned long val);
+
+unsigned long vgic_mmio_read_raz(struct vcpu *vcpu,
+                                 paddr_t addr, unsigned int len);
+
+unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
+                                 paddr_t addr, unsigned int len);
+
+void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
+                        unsigned int len, unsigned long val);
+
+/* Find the proper register handler entry given a certain address offset */
+const struct vgic_register_region *
+vgic_find_mmio_region(const struct vgic_register_region *regions,
+                      int nr_regions, unsigned int offset);
+
+#endif
xen/arch/arm/vgic/vgic.h +0 -0
index 771ca6f046..426b34d0ce 100644

xen/arch/arm/vgic/vgic.h static inline bool irq_is_pending(struct vgic_irq *irq) · line 33

Quoted 7 lines
xen/arch/arm/vgic/vgic.h +4 -0
@@ -27,6 +27,10 @@ static inline bool irq_is_pending(struct vgic_irq *irq)
          return irq->pending_latch || irq->line_level;
  }
 
+const struct vgic_register_region *
+vgic_get_mmio_region(struct vcpu *vcpu, struct vgic_io_device *iodev,
+             paddr_t addr, int len);
+

Why this one is added in vgic.h and not kept in vgic-mmio.h?

xen/arch/arm/vgic/vgic.h static inline bool irq_is_pending(struct vgic_irq *irq) · line 36

Quoted 4 lines
xen/arch/arm/vgic/vgic.h +0 -0
  struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
                                u32 intid);
  void vgic_put_irq(struct domain *d, struct vgic_irq *irq);

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 27/49] ARM: new VGIC: Add MMIO handling framework

Hi,

Quoted 93 lines · Julien Grall

Hi Andre,7

Quoted 45 lines · Andre Przywara

Add an MMIO handling framework to the VGIC emulation:
Each register is described by its offset, size (or number of bits per IRQ, if applicable) and the read/write handler functions. We provide initialization macros to describe each GIC register later easily.

Separate dispatch functions for read and write accesses are connected to Xen's MMIO handling framework and binary-search for the responsible register handler based on the offset address within the region.

The register handler prototype are courtesy of Christoffer Dall.

This is based on Linux commit 4493b1c4866a, written by Marc Zyngier.

---
  xen/arch/arm/vgic/vgic-mmio.c | 192
++++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic-mmio.h | 145 +++++++++++++++++++++++++++++++ xen/arch/arm/vgic/vgic.h      |   4 +

  3 files changed, 341 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic-mmio.c create mode 100644 xen/arch/arm/vgic/vgic-mmio.h

xen/arch/arm/vgic/vgic-mmio.c new file +15 -0
new file mode 100644
index 0000000000..3c70945466
@@ -0,0 +1,192 @@
+/*
+ * VGIC MMIO handling functions
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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>

xen/arch/arm/vgic/vgic-mmio.c line 25

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+#include <xen/lib.h>
+#include <xen/sched.h>
+#include <asm/arm_vgic.h>
+#include <asm/byteorder.h>
+
+#include "vgic.h"
+#include "vgic-mmio.h"
+
+unsigned long vgic_mmio_read_raz(struct vcpu *vcpu,
+                 paddr_t addr, unsigned int len)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c line 31

Quoted 6 lines
xen/arch/arm/vgic/vgic-mmio.c new file +6 -0
+{
+    return 0;
+}
+
+unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
+                 paddr_t addr, unsigned int len)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c line 37

Quoted 6 lines
xen/arch/arm/vgic/vgic-mmio.c new file +6 -0
+{
+    return -1UL;
+}
+
+void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
+            unsigned int len, unsigned long val)

Indentation.

Quoted 14 lines
xen/arch/arm/vgic/vgic-mmio.c new file +14 -0
+{
+    /* Ignore */
+}
+
+static int match_region(const void *key, const void *elt)
+{
+    const unsigned int offset = (unsigned long)key;
+    const struct vgic_register_region *region = elt;
+
+    if ( offset < region->reg_offset )
+        return -1;
+
+    if ( offset >= region->reg_offset + region->len )
+        return 1;
Quoted 8 lines

xen/arch/arm/vgic/vgic-mmio.c line 57

Quoted 6 lines
xen/arch/arm/vgic/vgic-mmio.c new file +6 -0
+
+    return 0;
+}
+
+const struct vgic_register_region *
+vgic_find_mmio_region(const struct vgic_register_region *regions,

Any reason to export this?

Good catch, this is needed in KVM to do the user space access, where we re-use these functions to call into the MMIO handlers.
So I can make them static and then loose the prototype down below as well.

Quoted 213 lines

xen/arch/arm/vgic/vgic-mmio.c line 58

Quoted 2 lines
xen/arch/arm/vgic/vgic-mmio.c new file +1 -0
+              int nr_regions, unsigned int offset)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c line 66

Quoted 8 lines
xen/arch/arm/vgic/vgic-mmio.c new file +8 -0
+{
+    return bsearch((void *)(uintptr_t)offset, regions, nr_regions,
+               sizeof(regions[0]), match_region);
+}
+
+static bool check_region(const struct domain *d,
+             const struct vgic_register_region *region,
+             paddr_t addr, int len)

Indentation.

xen/arch/arm/vgic/vgic-mmio.c

Quoted 3 lines
xen/arch/arm/vgic/vgic-mmio.c new file +3 -0
+{
+    int flags, nr_irqs = d->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
+ > +    switch (len)

switch ( ... )

xen/arch/arm/vgic/vgic-mmio.c line 71

Quoted 2 lines
xen/arch/arm/vgic/vgic-mmio.c new file +2 -0
+    {
+    case sizeof(u8):

s/u8/uint8_t/ here an below.

Quoted 16 lines
xen/arch/arm/vgic/vgic-mmio.c new file +16 -0
+        flags = VGIC_ACCESS_8bit;
+        break;
+    case sizeof(u32):
+        flags = VGIC_ACCESS_32bit;
+        break;
+    case sizeof(u64):
+        flags = VGIC_ACCESS_64bit;
+        break;
+    default:
+        return false;
+    }
+
+    if ( (region->access_flags & flags) && IS_ALIGNED(addr, len) )
+    {
+        if ( !region->bits_per_irq )
+            return true;

xen/arch/arm/vgic/vgic-mmio.c line 98

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+
+        /* Do we access a non-allocated IRQ? */
+        return VGIC_ADDR_TO_INTID(addr, region->bits_per_irq) < nr_irqs;
+    }
+
+    return false;
+}
+
+const struct vgic_register_region *
+vgic_get_mmio_region(struct vcpu *vcpu, struct vgic_io_device *iodev,

Any reason to export this?

xen/arch/arm/vgic/vgic-mmio.c line 98

Quoted 1 line
xen/arch/arm/vgic/vgic-mmio.c new file +1 -0
+             paddr_t addr, int len)

Indentation and unsigned int please.

xen/arch/arm/vgic/vgic-mmio.c line 112

Quoted 13 lines
xen/arch/arm/vgic/vgic-mmio.c new file +13 -0
+{
+    const struct vgic_register_region *region;
+
+    region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
+                       addr - iodev->base_addr);
+    if ( !region || !check_region(vcpu->domain, region, addr, len) )
+        return NULL;
+
+    return region;
+}
+
+static int dispatch_mmio_read(struct vcpu *vcpu, mmio_info_t *info,
+                  register_t *r, void *priv)

Indentation.

Quoted 58 lines
xen/arch/arm/vgic/vgic-mmio.c new file +58 -0
+{
+    struct vgic_io_device *iodev = priv;
+    const struct vgic_register_region *region;
+    unsigned long data = 0;
+    paddr_t addr = info->gpa;
+    int len = 1U << info->dabt.size;
+
+    region = vgic_get_mmio_region(vcpu, iodev, addr, len);
+    if ( !region )
+    {
+        memset(r, 0, len);
+        return 0;
+    }
+
+    switch (iodev->iodev_type)
+    {
+    case IODEV_CPUIF:
+        data = region->read(vcpu, addr, len);
+        break;
+    case IODEV_DIST:
+        data = region->read(vcpu, addr, len);
+        break;
+    case IODEV_REDIST:
+        data = region->read(iodev->redist_vcpu, addr, len);
+        break;
+    case IODEV_ITS:
+        data = region->its_read(vcpu->domain, iodev->its, addr, len);
+        break;
+    }
+
+    memcpy(r, &data, len);
+
+    return 1;
+}
+
+static int dispatch_mmio_write(struct vcpu *vcpu, mmio_info_t *info,
+                   register_t r, void *priv)
+{
+    struct vgic_io_device *iodev = priv;
+    const struct vgic_register_region *region;
+    unsigned long data = r;
+    paddr_t addr = info->gpa;
+    int len = 1U << info->dabt.size;
+
+    region = vgic_get_mmio_region(vcpu, iodev, addr, len);
+    if ( !region )
+        return 0;
+
+    switch (iodev->iodev_type)
+    {
+    case IODEV_CPUIF:
+        region->write(vcpu, addr, len, data);
+        break;
+    case IODEV_DIST:
+        region->write(vcpu, addr, len, data);
+        break;
+    case IODEV_REDIST:
+        region->write(iodev->redist_vcpu, addr, len, data);

xen/arch/arm/vgic/vgic-mmio.c line 180

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.c new file +10 -0
+        break;
+    case IODEV_ITS:
+        region->its_write(vcpu->domain, iodev->its, addr, len, data);
+        break;
+    }
+
+    return 1;
+}
+
+struct mmio_handler_ops xen_io_gic_ops = {

I would rename to vgic_io_ops.

Quoted 26 lines
xen/arch/arm/vgic/vgic-mmio.c new file +12 -0
+    .read = dispatch_mmio_read,
+    .write = dispatch_mmio_write,
+};
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic-mmio.h new file +7 -0
new file mode 100644
index 0000000000..375b70561d
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2015, 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 version 2 as
+ * published by the Free Software Foundation.
+ *

xen/arch/arm/vgic/vgic-mmio.h line 17

Quoted 10 lines
xen/arch/arm/vgic/vgic-mmio.h new file +10 -0
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef __KVM_ARM_VGIC_MMIO_H__
+#define __KVM_ARM_VGIC_MMIO_H__

Please use update the guard.

Quoted 26 lines
xen/arch/arm/vgic/vgic-mmio.h new file +11 -0
+
+struct vgic_register_region {
+    unsigned int reg_offset;
+    unsigned int len;
+    unsigned int bits_per_irq;
+    unsigned int access_flags;
+    union
+    {
+        unsigned long (*read)(struct vcpu *vcpu, paddr_t addr,
+                              unsigned int len);
+        unsigned long (*its_read)(struct domain *d, struct vgic_its

*its,
+                                  paddr_t addr, unsigned int len); +    };
+    union
+    {
+        void (*write)(struct vcpu *vcpu, paddr_t addr, +                      unsigned int len, unsigned long val); +        void (*its_write)(struct domain *d, struct vgic_its *its, +                          paddr_t addr, unsigned int len, +                          unsigned long val);
+    };
+    unsigned long (*uaccess_read)(struct vcpu *vcpu, paddr_t addr, +                                  unsigned int len);
+    union
+    {

Quoted 8 lines
Quoted 6 lines

+        void (*uaccess_write)(struct vcpu *vcpu, paddr_t addr, +                              unsigned int len, unsigned long val); +        int (*uaccess_its_write)(struct domain *d, struct vgic_its *its, +                                 paddr_t addr, unsigned int len, +                                 unsigned long val);
+    };

I don't think uaccess helpers makes sense for Xen.

True, I was unsure about whether to keep them. I have the gut feeling we need it later when we want to suspend/resume the VGIC, so removing everything and then simplifying the code afterwards might bite us in the future.
So as long as it doesn't really hurt, I am tempted to keep that code in, which also keeps it closer the the KVM implementation.

But if you insist: deleting this is easy ;-)

Quoted 22 lines
Quoted 22 lines
xen/arch/arm/vgic/vgic-mmio.h new file +20 -0
+};
+
+extern struct mmio_handler_ops xen_io_gic_ops;
+
+#define VGIC_ACCESS_8bit    1
+#define VGIC_ACCESS_32bit   2
+#define VGIC_ACCESS_64bit   4
+
+/*
+ * Generate a mask that covers the number of bytes required to address
+ * up to 1024 interrupts, each represented by <bits> bits. This assumes
+ * that <bits> is a power of two.
+ */
+#define VGIC_ADDR_IRQ_MASK(bits) (((bits) * 1024 / 8) - 1)
+
+/*
+ * (addr & mask) gives us the _byte_ offset for the INT ID.
+ * We multiply this by 8 the get the _bit_ offset, then divide this by
+ * the number of bits to learn the actual INT ID.
+ * But instead of a division (which requires a "long long div"

implementation),
+ * we shift by the binary logarithm of <bits>.

Quoted 8 lines
Quoted 5 lines

+ * This assumes that <bits> is a power of two.
+ */
+#define VGIC_ADDR_TO_INTID(addr, bits)  (((addr) & VGIC_ADDR_IRQ_MASK(bits)) * \
+                                         8 >> LOG_2(bits))

We are going to switch to ilog2 (see Sameer's patch "xen/bitops: Rename LOG_2 to ilog2").

\o/

Quoted 45 lines
Quoted 45 lines
xen/arch/arm/vgic/vgic-mmio.h new file +3 -0
+
+/*
+ * Some VGIC registers store per-IRQ information, with a different

number
+ * of bits per IRQ. For those registers this macro is used.
+ * The _WITH_LENGTH version instantiates registers with a fixed length
+ * and is mutually exclusive with the _PER_IRQ version.
+ */
+#define REGISTER_DESC_WITH_BITS_PER_IRQ(off, rd, wr, ur, uw, bpi, acc)  \
+    {                           \
+        .reg_offset = off,      \
+        .bits_per_irq = bpi,    \
+        .len = bpi * 1024 / 8,  \
+        .access_flags = acc,    \
+        .read = rd,             \
+        .write = wr,            \
+        .uaccess_read = ur,     \
+        .uaccess_write = uw,    \
+    }

xen/arch/arm/vgic/vgic-mmio.h new file +12 -0
+
+#define REGISTER_DESC_WITH_LENGTH(off, rd, wr, length, acc)     \
+    {                           \
+        .reg_offset = off,      \
+        .bits_per_irq = 0,      \
+        .len = length,          \
+        .access_flags = acc,    \
+        .read = rd,             \
+        .write = wr,            \
+    }
+
+#define REGISTER_DESC_WITH_LENGTH_UACCESS(off, rd, wr, urd, uwr,

length, acc) \
+    {                           \
+        .reg_offset = off,      \
+        .bits_per_irq = 0,      \
+        .len = length,          \
+        .access_flags = acc,    \
+        .read = rd,             \
+        .write = wr,            \
+        .uaccess_read = urd,    \
+        .uaccess_write = uwr,   \
+    }
+

Quoted 8 lines
Quoted 4 lines

+int kvm_vgic_register_mmio_region(struct domain *d, struct vcpu *vcpu, +                                  struct vgic_register_region *reg_desc, +                                  struct vgic_io_device *region, +                                  int nr_irqs, bool offset_private);

You want to do some clean-up in the prototype below. Only the one used in the patch should be added. The other should either move in there corresponding patch or dropped if not used.

Thanks for the heads up. In general I didn't spend much time on the prototypes in header files, so there might indeed by some leftovers from KVM.

Consider the rest fixed.

Cheers,
Andre.

Quoted 44 lines
Quoted 38 lines
xen/arch/arm/vgic/vgic-mmio.h new file +2 -0
+
+unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned

int len);

xen/arch/arm/vgic/vgic-mmio.h new file +20 -0
+
+void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
+                                unsigned long data);
+
+unsigned long extract_bytes(u64 data, unsigned int offset,
+                            unsigned int num);
+
+u64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len,
+                     unsigned long val);
+
+unsigned long vgic_mmio_read_raz(struct vcpu *vcpu,
+                                 paddr_t addr, unsigned int len);
+
+unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
+                                 paddr_t addr, unsigned int len);
+
+void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
+                        unsigned int len, unsigned long val);
+
+/* Find the proper register handler entry given a certain address

offset */

xen/arch/arm/vgic/vgic-mmio.h new file +5 -0
+const struct vgic_register_region *
+vgic_find_mmio_region(const struct vgic_register_region *regions,
+                      int nr_regions, unsigned int offset);
+
+#endif
xen/arch/arm/vgic/vgic.h +0 -0
index 771ca6f046..426b34d0ce 100644
@@ -27,6 +27,10 @@ static inline bool irq_is_pending(struct vgic_irq
 *irq)
           return irq->pending_latch || irq->line_level;
   }

  +const struct vgic_register_region *

xen/arch/arm/vgic/vgic.h static inline bool irq_is_pending(struct vgic_irq *irq) · line 33

Quoted 3 lines
xen/arch/arm/vgic/vgic.h +3 -0
+vgic_get_mmio_region(struct vcpu *vcpu, struct vgic_io_device *iodev,
+             paddr_t addr, int len);
+

Why this one is added in vgic.h and not kept in vgic-mmio.h?

Quoted 7 lines
Quoted 4 lines

  struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu, u32 intid);
  void vgic_put_irq(struct domain *d, struct vgic_irq *irq);

Cheers,

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

#86 lore raw

Re: [RFC PATCH 27/49] ARM: new VGIC: Add MMIO handling framework

Hi Andre,

Quoted 33 lines · Andre Przywara
Quoted 32 lines · Julien Grall
Quoted 31 lines

+struct vgic_register_region {
+    unsigned int reg_offset;
+    unsigned int len;
+    unsigned int bits_per_irq;
+    unsigned int access_flags;
+    union
+    {
+        unsigned long (*read)(struct vcpu *vcpu, paddr_t addr, +                              unsigned int len);
+        unsigned long (*its_read)(struct domain *d, struct vgic_its *its,
+                                  paddr_t addr, unsigned int len); +    };
+    union
+    {
+        void (*write)(struct vcpu *vcpu, paddr_t addr,
+                      unsigned int len, unsigned long val); +        void (*its_write)(struct domain *d, struct vgic_its *its, +                          paddr_t addr, unsigned int len,
+                          unsigned long val);
+    };
+    unsigned long (*uaccess_read)(struct vcpu *vcpu, paddr_t addr, +                                  unsigned int len);
+    union
+    {
+        void (*uaccess_write)(struct vcpu *vcpu, paddr_t addr, +                              unsigned int len, unsigned long val); +        int (*uaccess_its_write)(struct domain *d, struct vgic_its *its, +                                 paddr_t addr, unsigned int len, +                                 unsigned long val);
+    };

Quoted 8 lines
Quoted 1 line

I don't think uaccess helpers makes sense for Xen.

True, I was unsure about whether to keep them. I have the gut feeling we need it later when we want to suspend/resume the VGIC, so removing everything and then simplifying the code afterwards might bite us in the future.
So as long as it doesn't really hurt, I am tempted to keep that code in, which also keeps it closer the the KVM implementation.

I don't want to see code that is going to potentially rot. If we really need it, we can add it afterwards.

Cheers,

Signature · 6 lines
Julien Grall

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

RFC PATCH 28/49 ARM: new VGIC: Add GICv2 MMIO handling framework

In reply to Andre Przywara

Create vgic-mmio-v2.c to describe GICv2 emulation specific handlers using the initializer macros provided by the VGIC MMIO framework. Provide a function to register the GICv2 distributor registers to the Xen MMIO framework.
The actual handler functions are still stubs in this patch.

This is based on Linux commit fb848db39661, written by Andre Przywara.

---
 xen/arch/arm/vgic/vgic-mmio-v2.c | 83 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic/vgic-mmio.c    | 26 +++++++++++++
 xen/arch/arm/vgic/vgic-mmio.h    |  2 +
 xen/arch/arm/vgic/vgic.h         |  2 +
 4 files changed, 113 insertions(+)
 create mode 100644 xen/arch/arm/vgic/vgic-mmio-v2.c
xen/arch/arm/vgic/vgic-mmio-v2.c new file +83 -0
new file mode 100644
index 0000000000..ee685a5a07
@@ -0,0 +1,83 @@
+/*
+ * VGICv2 MMIO handling functions
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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/sched.h>
+#include <xen/sizes.h>
+#include <asm/arm_vgic.h>
+
+#include "vgic.h"
+#include "vgic-mmio.h"
+
+static const struct vgic_register_region vgic_v2_dist_registers[] = {
+    REGISTER_DESC_WITH_LENGTH(GICD_CTLR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 12,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_IGROUPR,
+        vgic_mmio_read_rao, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISENABLER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICENABLER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISPENDR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICPENDR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISACTIVER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICACTIVER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_IPRIORITYR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ITARGETSR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICFGR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 2,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_SGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_CPENDSGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_SPENDSGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+};
+
+unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev)
+{
+    dev->regions = vgic_v2_dist_registers;
+    dev->nr_regions = ARRAY_SIZE(vgic_v2_dist_registers);
+
+    return SZ_4K;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic-mmio.c +26 -0
index 3c70945466..59703a6909 100644
@@ -182,6 +182,32 @@ struct mmio_handler_ops xen_io_gic_ops = {
     .write = dispatch_mmio_write,
 };
 
+int vgic_register_dist_iodev(struct domain *d, paddr_t dist_base_address,
+                 enum vgic_type type)
+{
+    struct vgic_io_device *io_device = &d->arch.vgic.dist_iodev;
+    int ret = 0;
+    unsigned int len;
+
+    switch (type)
+    {
+    case VGIC_V2:
+        len = vgic_v2_init_dist_iodev(io_device);
+        break;
+    default:
+        BUG_ON(1);
+    }
+
+    io_device->base_addr = dist_base_address;
+    io_device->iodev_type = IODEV_DIST;
+    io_device->redist_vcpu = NULL;
+
+    register_mmio_handler(d, &xen_io_gic_ops, dist_base_address, len,
+                  io_device);
+
+    return ret;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic/vgic-mmio.h +2 -0
index 375b70561d..10ac682296 100644
@@ -137,6 +137,8 @@ unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
 void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
                         unsigned int len, unsigned long val);
 
+unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev);
+
 /* Find the proper register handler entry given a certain address offset */
 const struct vgic_register_region *
 vgic_find_mmio_region(const struct vgic_register_region *regions,
xen/arch/arm/vgic/vgic.h +2 -0
index 426b34d0ce..7747d3f3e0 100644
@@ -49,6 +49,8 @@ void vgic_v2_fold_lr_state(struct vcpu *vcpu);
 void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr);
 void vgic_v2_clear_lr(struct vcpu *vcpu, int lr);
 void vgic_v2_set_underflow(struct vcpu *vcpu);
+int vgic_register_dist_iodev(struct domain *d, paddr_t dist_base_address,
+                 enum vgic_type);
 
 void vgic_v2_save_state(struct vcpu *vcpu);
 void vgic_v2_restore_state(struct vcpu *vcpu);
Signature · 7 lines
2.14.1


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

Re: [RFC PATCH 28/49] ARM: new VGIC: Add GICv2 MMIO handling framework

Hi Andre,

Quoted 111 lines · Andre Przywara

Create vgic-mmio-v2.c to describe GICv2 emulation specific handlers using the initializer macros provided by the VGIC MMIO framework. Provide a function to register the GICv2 distributor registers to the Xen MMIO framework.
The actual handler functions are still stubs in this patch.

This is based on Linux commit fb848db39661, written by Andre Przywara.

---
  xen/arch/arm/vgic/vgic-mmio-v2.c | 83 ++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic-mmio.c    | 26 +++++++++++++
  xen/arch/arm/vgic/vgic-mmio.h    |  2 +
  xen/arch/arm/vgic/vgic.h         |  2 +
  4 files changed, 113 insertions(+)

create mode 100644 xen/arch/arm/vgic/vgic-mmio-v2.c

xen/arch/arm/vgic/vgic-mmio-v2.c new file +83 -0
new file mode 100644
index 0000000000..ee685a5a07
@@ -0,0 +1,83 @@
+/*
+ * VGICv2 MMIO handling functions
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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/sched.h>
+#include <xen/sizes.h>
+#include <asm/arm_vgic.h>
+
+#include "vgic.h"
+#include "vgic-mmio.h"
+
+static const struct vgic_register_region vgic_v2_dist_registers[] = {
+    REGISTER_DESC_WITH_LENGTH(GICD_CTLR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 12,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_IGROUPR,
+        vgic_mmio_read_rao, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISENABLER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICENABLER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISPENDR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICPENDR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISACTIVER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICACTIVER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_IPRIORITYR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ITARGETSR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICFGR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 2,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_SGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_CPENDSGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_SPENDSGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+};
+
+unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev)
+{
+    dev->regions = vgic_v2_dist_registers;
+    dev->nr_regions = ARRAY_SIZE(vgic_v2_dist_registers);
+
+    return SZ_4K;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic-mmio.c +0 -0
index 3c70945466..59703a6909 100644

xen/arch/arm/vgic/vgic-mmio.c struct mmio_handler_ops xen_io_gic_ops = { · line 185

Quoted 4 lines
xen/arch/arm/vgic/vgic-mmio.c +1 -0
@@ -182,6 +182,32 @@ struct mmio_handler_ops xen_io_gic_ops = {
      .write = dispatch_mmio_write,
  };
 
+int vgic_register_dist_iodev(struct domain *d, paddr_t dist_base_address,

I would rather prefer to use gfn_t over paddr_t. The former deal with frame only is safer to use.

xen/arch/arm/vgic/vgic-mmio.c struct mmio_handler_ops xen_io_gic_ops = { · line 189

Quoted 4 lines
xen/arch/arm/vgic/vgic-mmio.c +4 -0
+                 enum vgic_type type)
+{
+    struct vgic_io_device *io_device = &d->arch.vgic.dist_iodev;
+    int ret = 0;

This variable is pointless. You never set it after so always return 0.

xen/arch/arm/vgic/vgic-mmio.c struct mmio_handler_ops xen_io_gic_ops = { · line 192

Quoted 3 lines
xen/arch/arm/vgic/vgic-mmio.c +3 -0
+    unsigned int len;
+
+    switch (type)

switch ( ... )

xen/arch/arm/vgic/vgic-mmio.c struct mmio_handler_ops xen_io_gic_ops = { · line 198

Quoted 6 lines
xen/arch/arm/vgic/vgic-mmio.c +6 -0
+    {
+    case VGIC_V2:
+        len = vgic_v2_init_dist_iodev(io_device);
+        break;
+    default:
+        BUG_ON(1);

Please use BUG() here. But have you checked we will never reach here with the wrong vgic_type?

xen/arch/arm/vgic/vgic-mmio.c struct mmio_handler_ops xen_io_gic_ops = { · line 201

Quoted 3 lines
xen/arch/arm/vgic/vgic-mmio.c +3 -0
+    }
+
+    io_device->base_addr = dist_base_address;

Also base_addr & co would only contain frame.

Quoted 31 lines
xen/arch/arm/vgic/vgic-mmio.c +9 -0
+    io_device->iodev_type = IODEV_DIST;
+    io_device->redist_vcpu = NULL;
+
+    register_mmio_handler(d, &xen_io_gic_ops, dist_base_address, len,
+                  io_device);
+
+    return ret;
+}
+
  /*
   * Local variables:
   * mode: C
xen/arch/arm/vgic/vgic-mmio.h +2 -0
index 375b70561d..10ac682296 100644
@@ -137,6 +137,8 @@ unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
  void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
                          unsigned int len, unsigned long val);
 
+unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev);
+
  /* Find the proper register handler entry given a certain address offset */
  const struct vgic_register_region *
  vgic_find_mmio_region(const struct vgic_register_region *regions,
xen/arch/arm/vgic/vgic.h +0 -0
index 426b34d0ce..7747d3f3e0 100644

xen/arch/arm/vgic/vgic.h void vgic_v2_fold_lr_state(struct vcpu *vcpu); · line 56

Quoted 8 lines
xen/arch/arm/vgic/vgic.h +2 -0
@@ -49,6 +49,8 @@ void vgic_v2_fold_lr_state(struct vcpu *vcpu);
  void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq, int lr);
  void vgic_v2_clear_lr(struct vcpu *vcpu, int lr);
  void vgic_v2_set_underflow(struct vcpu *vcpu);
+int vgic_register_dist_iodev(struct domain *d, paddr_t dist_base_address,
+                 enum vgic_type);
 
  void vgic_v2_save_state(struct vcpu *vcpu);
  void vgic_v2_restore_state(struct vcpu *vcpu);

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [RFC PATCH 28/49] ARM: new VGIC: Add GICv2 MMIO handling framework

Hi,

Quoted 116 lines · Julien Grall

Hi Andre,

Quoted 113 lines · Andre Przywara

Create vgic-mmio-v2.c to describe GICv2 emulation specific handlers using the initializer macros provided by the VGIC MMIO framework. Provide a function to register the GICv2 distributor registers to the Xen MMIO framework.
The actual handler functions are still stubs in this patch.

This is based on Linux commit fb848db39661, written by Andre Przywara.

---
  xen/arch/arm/vgic/vgic-mmio-v2.c | 83
++++++++++++++++++++++++++++++++++++++++
  xen/arch/arm/vgic/vgic-mmio.c    | 26 +++++++++++++ xen/arch/arm/vgic/vgic-mmio.h    |  2 + xen/arch/arm/vgic/vgic.h         |  2 +

  4 files changed, 113 insertions(+)

  create mode 100644 xen/arch/arm/vgic/vgic-mmio-v2.c

xen/arch/arm/vgic/vgic-mmio-v2.c new file +83 -0
new file mode 100644
index 0000000000..ee685a5a07
@@ -0,0 +1,83 @@
+/*
+ * VGICv2 MMIO handling functions
+ * Imported from Linux ("new" KVM VGIC) and heavily adapted to Xen.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * 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/sched.h>
+#include <xen/sizes.h>
+#include <asm/arm_vgic.h>
+
+#include "vgic.h"
+#include "vgic-mmio.h"
+
+static const struct vgic_register_region vgic_v2_dist_registers[] = {
+    REGISTER_DESC_WITH_LENGTH(GICD_CTLR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 12,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_IGROUPR,
+        vgic_mmio_read_rao, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISENABLER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICENABLER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISPENDR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICPENDR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ISACTIVER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICACTIVER,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 1,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_IPRIORITYR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ITARGETSR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 8,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_ICFGR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, NULL, NULL, 2,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_SGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 4,
+        VGIC_ACCESS_32bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_CPENDSGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+    REGISTER_DESC_WITH_LENGTH(GICD_SPENDSGIR,
+        vgic_mmio_read_raz, vgic_mmio_write_wi, 16,
+        VGIC_ACCESS_32bit | VGIC_ACCESS_8bit),
+};
+
+unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev)
+{
+    dev->regions = vgic_v2_dist_registers;
+    dev->nr_regions = ARRAY_SIZE(vgic_v2_dist_registers);
+
+    return SZ_4K;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic/vgic-mmio.c +0 -0
index 3c70945466..59703a6909 100644
Quoted 8 lines
Quoted 5 lines
xen/arch/arm/vgic/vgic-mmio.c +0 -0
@@ -182,6 +182,32 @@ struct mmio_handler_ops xen_io_gic_ops = {
       .write = dispatch_mmio_write,
   };

  +int vgic_register_dist_iodev(struct domain *d, paddr_t dist_base_address,

I would rather prefer to use gfn_t over paddr_t. The former deal with frame only is safer to use.

OK.

Quoted 6 lines
Quoted 4 lines

+                 enum vgic_type type)
+{
+    struct vgic_io_device *io_device = &d->arch.vgic.dist_iodev; +    int ret = 0;

This variable is pointless. You never set it after so always return 0.

True. The KVM version of register_mmio_handler() returns an error value, which is held in this variable (since it is called within a lock). Will remove it.

Quoted 13 lines
Quoted 3 lines

+    unsigned int len;
+
+    switch (type)

switch ( ... )

Quoted 6 lines

+    {
+    case VGIC_V2:
+        len = vgic_v2_init_dist_iodev(io_device); +        break;
+    default:
+        BUG_ON(1);

Quoted 2 lines

Please use BUG() here. But have you checked we will never reach here with the wrong vgic_type?

This is just a placeholder for now, the proper vGICv3 function will plug in later. Actually I don't expect this line to be ever part of running code. Even if, at the moment no one sets the type to anything other than VGIC_V2, so this should be safe.

Cheers,
Andre.

Quoted 41 lines
Quoted 3 lines

+    }
+
+    io_device->base_addr = dist_base_address;

Also base_addr & co would only contain frame.

Quoted 35 lines

+    io_device->iodev_type = IODEV_DIST;
+    io_device->redist_vcpu = NULL;

xen/arch/arm/vgic/vgic-mmio.c +7 -0
+
+    register_mmio_handler(d, &xen_io_gic_ops, dist_base_address, len,
+                  io_device);
+
+    return ret;
+}
+

  /*
   * Local variables:
   * mode: C

xen/arch/arm/vgic/vgic-mmio.h +1 -0
index 375b70561d..10ac682296 100644
@@ -137,6 +137,8 @@ unsigned long vgic_mmio_read_rao(struct vcpu *vcpu,
   void vgic_mmio_write_wi(struct vcpu *vcpu, paddr_t addr,
                           unsigned int len, unsigned long val);
   +unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev);
+
   /* Find the proper register handler entry given a certain address

offset */
  const struct vgic_register_region * vgic_find_mmio_region(const struct vgic_register_region *regions,

xen/arch/arm/vgic/vgic.h +0 -0
index 426b34d0ce..7747d3f3e0 100644
@@ -49,6 +49,8 @@ void vgic_v2_fold_lr_state(struct vcpu *vcpu);
   void vgic_v2_populate_lr(struct vcpu *vcpu, struct vgic_irq *irq,
 int lr);
   void vgic_v2_clear_lr(struct vcpu *vcpu, int lr);
   void vgic_v2_set_underflow(struct vcpu *vcpu);
Quoted 9 lines
Quoted 6 lines
xen/arch/arm/vgic/vgic.h +2 -0
+int vgic_register_dist_iodev(struct domain *d, paddr_t
 dist_base_address,
+                 enum vgic_type);
     void vgic_v2_save_state(struct vcpu *vcpu);

  void vgic_v2_restore_state(struct vcpu *vcpu);

Cheers,

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

#90 lore raw

RFC PATCH 29/49 ARM: new VGIC: Add CTLR, TYPER and IIDR handlers

In reply to Andre Przywara

Those three registers are v2 emulation specific, so their implementation lives entirely in vgic-mmio-v2.c. Also they are handled in one function, as their implementation is pretty simple.
When the guest enables the distributor, we kick all VCPUs to get potentially pending interrupts serviced.

This is based on Linux commit 2b0cda878965, written by Marc Zyngier.

---
 xen/arch/arm/vgic/vgic-mmio-v2.c | 48 +++++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/vgic/vgic.c         | 15 +++++++++++++
 xen/arch/arm/vgic/vgic.h         |  4 ++++
 3 files changed, 66 insertions(+), 1 deletion(-)
xen/arch/arm/vgic/vgic-mmio-v2.c +47 -1
index ee685a5a07..0926b3243e 100644
@@ -20,9 +20,55 @@
 #include "vgic.h"
 #include "vgic-mmio.h"
 
+static unsigned long vgic_mmio_read_v2_misc(struct vcpu *vcpu,
+                        paddr_t addr, unsigned int len)
+{
+    u32 value;
+
+    switch (addr & 0x0c)
+    {
+    case GICD_CTLR:
+        value = vcpu->domain->arch.vgic.enabled ? GICD_CTL_ENABLE : 0;
+        break;
+    case GICD_TYPER:
+        value = vcpu->domain->arch.vgic.nr_spis + VGIC_NR_PRIVATE_IRQS;
+        value = (value >> 5) - 1;
+        value |= (vcpu->domain->max_vcpus - 1) << 5;
+        break;
+    case GICD_IIDR:
+        value = (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0);
+        break;
+    default:
+        return 0;
+    }
+
+    return value;
+}
+
+static void vgic_mmio_write_v2_misc(struct vcpu *vcpu,
+                    paddr_t addr, unsigned int len,
+                    unsigned long val)
+{
+    struct vgic_dist *dist = &vcpu->domain->arch.vgic;
+    bool was_enabled = dist->enabled;
+
+    switch (addr & 0x0c)
+    {
+    case GICD_CTLR:
+        dist->enabled = val & GICD_CTL_ENABLE;
+        if ( !was_enabled && dist->enabled )
+            vgic_kick_vcpus(vcpu->domain);
+        break;
+    case GICD_TYPER:
+    case GICD_IIDR:
+        /* Nothing to do */
+        return;
+    }
+}
+
 static const struct vgic_register_region vgic_v2_dist_registers[] = {
     REGISTER_DESC_WITH_LENGTH(GICD_CTLR,
-        vgic_mmio_read_raz, vgic_mmio_write_wi, 12,
+        vgic_mmio_read_v2_misc, vgic_mmio_write_v2_misc, 12,
         VGIC_ACCESS_32bit),
     REGISTER_DESC_WITH_BITS_PER_IRQ(GICD_IGROUPR,
         vgic_mmio_read_rao, vgic_mmio_write_wi, NULL, NULL, 1,
xen/arch/arm/vgic/vgic.c +15 -0
index 9e7fb1edcb..dc5e011fa3 100644
@@ -678,6 +678,21 @@ int gic_events_need_delivery(void)
     return vgic_vcpu_pending_irq(current);
 }
 
+void vgic_kick_vcpus(struct domain *d)
+{
+    struct vcpu *vcpu;
+
+    /*
+     * We've injected an interrupt, time to find out who deserves
+     * a good kick...
+     */
+    for_each_vcpu( d, vcpu )
+    {
+        if ( vgic_vcpu_pending_irq(vcpu) )
+            vcpu_unblock(vcpu);
+    }
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic/vgic.h +4 -0
index 7747d3f3e0..82fe902e26 100644
@@ -17,6 +17,9 @@
 #ifndef __XEN_ARM_VGIC_NEW_H__
 #define __XEN_ARM_VGIC_NEW_H__
 
+#define PRODUCT_ID_KVM      0x4b    /* ASCII code K */
+#define IMPLEMENTER_ARM     0x43b
+
 #define vgic_irq_is_sgi(intid) ((intid) < VGIC_NR_SGIS)
 
 static inline bool irq_is_pending(struct vgic_irq *irq)
@@ -36,6 +39,7 @@ struct vgic_irq *vgic_get_irq(struct domain *d, struct vcpu *vcpu,
 void vgic_put_irq(struct domain *d, struct vgic_irq *irq);
 bool vgic_queue_irq_unlock(struct domain *d, struct vgic_irq *irq,
                unsigned long flags);
+void vgic_kick_vcpus(struct domain *d);
 
 static inline void vgic_get_irq_kref(struct vgic_irq *irq)
 {
Signature · 7 lines
2.14.1


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