Skip to contentSkip to conversation

arm64: Dom0 ITS emulation

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

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

cover arm64: Dom0 ITS emulation

Hi,

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

This series introduces ARM GICv3 ITS emulation, for now restricted to Dom0 only. The ITS is an interrupt controller widget providing a sophisticated way to deal with MSIs in a scalable manner.
For hardware which relies on the ITS to provide interrupts for its peripherals this code is needed to get a machine booted into Dom0 at all. ITS emulation for DomUs is only really useful with PCI passthrough, which is not yet available for ARM. It is expected that this feature will be co-developed with the ITS DomU code.

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

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

* On the guest side we (later will) have to deal with malicious guests trying to hog Xen with mapping requests for a lot of LPIs, for instance. As the ITS actually uses system memory for storing status information, we use this memory (which the guest has to provide) to naturally limit a guest. For those tables which are page sized (devices, collections (CPUs), LPI properties) we map those pages into Xen, so we can easily access them from the virtual GIC code.
Unfortunately the actual interrupt mapping tables are not necessarily page aligned, also can be much smaller than a page, so mapping all of them permanently is fiddly. As ITS commands in need to iterate those tables are pretty rare after all, we for now map them on demand upon emulating a virtual ITS command.

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

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

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

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

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

Cheers,
Andre

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

 xen/arch/arm/Kconfig              |  11 +
 xen/arch/arm/Makefile             |   2 +
 xen/arch/arm/efi/efi-boot.h       |   1 -
 xen/arch/arm/gic-its.c            | 861 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             |  78 +++-
 xen/arch/arm/gic.c                |   9 +-
 xen/arch/arm/vgic-its.c           | 861 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            | 240 +++++++++--
 xen/arch/arm/vgic.c               |  60 ++-
 xen/include/asm-arm/cache.h       |   4 +
 xen/include/asm-arm/domain.h      |   9 +-
 xen/include/asm-arm/gic-its.h     | 246 +++++++++++
 xen/include/asm-arm/gic_v3_defs.h |  67 ++-
 xen/include/asm-arm/irq.h         |   8 +
 xen/include/asm-arm/vgic.h        |  12 +
 15 files changed, 2432 insertions(+), 37 deletions(-)
 create mode 100644 xen/arch/arm/gic-its.c
 create mode 100644 xen/arch/arm/vgic-its.c
 create mode 100644 xen/include/asm-arm/gic-its.h
Signature · 7 lines
2.9.0


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

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

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

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

67 lines not on this page · raw message

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

57 lines not on this page · raw message

Signature · 7 lines
2.9.0


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

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

Quoted 6 lines · Andre Przywara

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

Looks good to me

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

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

#3 lore raw

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

In reply to Andre Przywara

Hi Andre,

Quoted 21 lines · Andre Przywara

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

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

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

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

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

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

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

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

Regards,

Signature · 6 lines
Julien Grall

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

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

Hi,

Quoted 28 lines · Julien Grall

Hi Andre,

Quoted 25 lines · Andre Przywara

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

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

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

Quoted 3 lines
xen/arch/arm/Kconfig +3 -0
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on ARM_64

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

OK, makes sense.

Quoted 6 lines

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

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

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

My concern was more that if it breaks something, people can just disable it. But I have to go through the patches again to see if disabling it really brings us something (because thinking about it I don't think so).

So given the test coverage argument I think we should at least enable it by default for ARM64. Is there some "expert options" group somewhere where we could insert the option to turn it off?

Cheers,
Andre.

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

#5 lore raw

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

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 43 lines · Julien Grall
Quoted 26 lines · Andre Przywara
Quoted 25 lines

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

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

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

Quoted 3 lines
xen/arch/arm/Kconfig +3 -0
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on ARM_64

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

OK, makes sense.

Quoted 6 lines

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

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

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

Quoted 7 lines

My concern was more that if it breaks something, people can just disable it. But I have to go through the patches again to see if disabling it really brings us something (because thinking about it I don't think so).

So given the test coverage argument I think we should at least enable it by default for ARM64. Is there some "expert options" group somewhere where we could insert the option to turn it off?

You can use "if EXPERT=y", see how we handle ACPI for instance.

Thinking a bit more about this, I would like to see ITS as a technical preview at the beginning. This would let us a bit of time to stabilize the code. Any opinions?

Regards,

Signature · 6 lines
Julien Grall

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

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

In reply to Andre Przywara

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

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

86 lines not on this page · raw message

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

66 lines not on this page · raw message

Signature · 7 lines
2.9.0


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

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

Quoted 79 lines · Andre Przywara

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

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

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

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

The pend table size allocated is differ from proptable size?

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

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

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

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

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

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

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

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

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

using PENDBASER definitions for PROPBASER?

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

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

Isn't GENMASK(47, 12)?

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

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

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

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

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

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

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

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

2.9.0

Quoted 4 lines

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

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

#8 lore raw

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

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

Hi Vijay,

thanks for having a look!

Quoted 84 lines · Andre Przywara
Quoted 83 lines

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

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

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

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

The pend table size allocated is differ from proptable size?

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

Quoted 6 lines

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

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

memset size is different from allocated size?

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

Quoted 7 lines

flushing zeroed pendtable?

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

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

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

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

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

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

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

using PENDBASER definitions for PROPBASER?

Good catch.

Quoted 57 lines

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

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

Isn't GENMASK(47, 12)?

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

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

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

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

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

	if ( reg ) return reg;
Quoted 5 lines

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

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

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

Possibly.

Cheers,
Andre.

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

#9 lore raw

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

In reply to Andre Przywara

Hi Andre,

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

Quoted 61 lines · Andre Przywara

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

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

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

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

To avoid confusion, I would call this MAX_PHYS_LPI_BITS

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

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

And this MAX_PHYS_LPIS

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

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

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

xen/arch/arm/gic-its.c

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

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

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

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

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

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

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

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

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

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

flush_dcache?

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

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

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

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

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

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

Please add a comment on how the order is calculated.

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

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

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

Please #define a mask instead of using 0x1f

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

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

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

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

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

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

#10 lore raw

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

Hi,

Quoted 4 lines · Stefano Stabellini

Hi Andre,

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

No worries and thanks for the thorough review, much appreciated. As you can see I took my time to respond as well ;-)

Quoted 77 lines · Andre Przywara
Quoted 61 lines

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

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

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

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

To avoid confusion, I would call this MAX_PHYS_LPI_BITS

Quoted 4 lines

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

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

And this MAX_PHYS_LPIS

Done.

Quoted 6 lines

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

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

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

Can do. So I return back this value then, though this should never happen.

Quoted 10 lines

xen/arch/arm/gic-its.c

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

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

3 bits as in 2 << 3 = 8 = BITS_PER_BYTES. We need to divide by that, which is shift by 3, which is ORDER - 3. Does that make sense? But this mayhem goes away anyway with _xmalloc.

Quoted 6 lines

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

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

Does it need to be 64K aligned or does it need to be at least 64K in size?

The first.

Quoted 2 lines

That makes a big difference. If it just needs to be 64K aligned, you can do that with xmalloc.

Well, not xmalloc (since I don't have a data structure of that size), but _xmalloc. I just saw that this is exported as well (I dismissed this before because of the leading underscore).
Also "alloc pages" sounded more like what I had in mind, but I guess aligning it to 64K serves the same purpose.

Quoted 5 lines

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

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

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

I was under the impression that the redistributors expect the pending table to cover every possible LPI as reported in GICD_TYPER (because in contrast to PROPBASER the PENDBASER register lacks a size field). But thinking about this again this seems to be insane, since 32 bit worth of LPIs would lead to a 0.5GB pending table. But as the LPI numbers are under the control of software, we can go with allocating less - up to our internal limit - which is also what Linux does.

Quoted 7 lines

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

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

flush_dcache?

Uhm, yes.

Quoted 12 lines
Quoted 12 lines
xen/arch/arm/gic-its.c +11 -0
+    this_cpu(pending_table) = pendtable;
+
+    reg  = attr | GICR_PENDBASER_PTZ;
+    reg |= virt_to_maddr(pendtable) & GENMASK(51, 16);
+
+    return reg;
+}
+
+uint64_t gicv3_lpi_get_proptable()
+{
+    uint64_t attr;
Quoted 8 lines

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

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

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

Seems like a good idea actually. We have to reconstruct the register content, but that seems doable.

Quoted 7 lines

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

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

Please add a comment on how the order is calculated.

Does " ... - PAGE_SHIFT" suffice?

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

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

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

Please #define a mask instead of using 0x1f

Quoted 17 lines
xen/arch/arm/gic-v3.c +10 -0
+
     printk("GICv3: %d lines, (IID %8.8x).\n",
            nr_lines, readl_relaxed(GICD + GICD_IIDR));
 
@@ -615,6 +618,26 @@ static int gicv3_enable_redist(void)
 
     return 0;
 }
+static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+
+    if ( list_empty(&host_its_list) )
+        return;
+
+    /* Make sure LPIs are disabled before setting up the BASERs. */
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/gic-v3.c +6 -0
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    writel_relaxed(reg & ~GICR_CTLR_ENABLE_LPIS, rdist_base + GICR_CTLR);
+
+    table_reg = gicv3_lpi_allocate_pendtable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);

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

I guess so. I just wonder what we would do in this case? Panic? Theoretically we could just proceed without enabling LPIs on this redistributor, but that's probably not what a user would expect.

Cheers,
Andre.

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

#11 lore raw

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

Quoted 113 lines · Andre Przywara

Hi,

Quoted 4 lines · Stefano Stabellini

Hi Andre,

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

No worries and thanks for the thorough review, much appreciated. As you can see I took my time to respond as well ;-)

Quoted 77 lines · Andre Przywara
Quoted 61 lines

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

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

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

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

To avoid confusion, I would call this MAX_PHYS_LPI_BITS

Quoted 4 lines

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

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

And this MAX_PHYS_LPIS

Done.

Quoted 6 lines

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

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

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

Can do. So I return back this value then, though this should never happen.

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

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

3 bits as in 2 << 3 = 8 = BITS_PER_BYTES. We need to divide by that, which is shift by 3, which is ORDER - 3. Does that make sense? But this mayhem goes away anyway with _xmalloc.

Please add info to the in code comment (if it will still be there).

Quoted 72 lines
Quoted 6 lines

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

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

Does it need to be 64K aligned or does it need to be at least 64K in size?

The first.

Quoted 2 lines

That makes a big difference. If it just needs to be 64K aligned, you can do that with xmalloc.

Well, not xmalloc (since I don't have a data structure of that size), but _xmalloc. I just saw that this is exported as well (I dismissed this before because of the leading underscore).
Also "alloc pages" sounded more like what I had in mind, but I guess aligning it to 64K serves the same purpose.

Quoted 5 lines

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

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

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

I was under the impression that the redistributors expect the pending table to cover every possible LPI as reported in GICD_TYPER (because in contrast to PROPBASER the PENDBASER register lacks a size field). But thinking about this again this seems to be insane, since 32 bit worth of LPIs would lead to a 0.5GB pending table. But as the LPI numbers are under the control of software, we can go with allocating less - up to our internal limit - which is also what Linux does.

Quoted 7 lines

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

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

flush_dcache?

Uhm, yes.

Quoted 12 lines
Quoted 12 lines
xen/arch/arm/gic-its.c +11 -0
+    this_cpu(pending_table) = pendtable;
+
+    reg  = attr | GICR_PENDBASER_PTZ;
+    reg |= virt_to_maddr(pendtable) & GENMASK(51, 16);
+
+    return reg;
+}
+
+uint64_t gicv3_lpi_get_proptable()
+{
+    uint64_t attr;
Quoted 8 lines

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

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

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

Seems like a good idea actually. We have to reconstruct the register content, but that seems doable.

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

Please add a comment on how the order is calculated.

Does " ... - PAGE_SHIFT" suffice?

Yes, that would work.

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

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

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

Please #define a mask instead of using 0x1f

Quoted 17 lines
xen/arch/arm/gic-v3.c +10 -0
+
     printk("GICv3: %d lines, (IID %8.8x).\n",
            nr_lines, readl_relaxed(GICD + GICD_IIDR));
 
@@ -615,6 +618,26 @@ static int gicv3_enable_redist(void)
 
     return 0;
 }
+static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+
+    if ( list_empty(&host_its_list) )
+        return;
+
+    /* Make sure LPIs are disabled before setting up the BASERs. */
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/gic-v3.c +6 -0
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    writel_relaxed(reg & ~GICR_CTLR_ENABLE_LPIS, rdist_base + GICR_CTLR);
+
+    table_reg = gicv3_lpi_allocate_pendtable();
+    if ( table_reg )
+        writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);

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

Quoted 3 lines

I guess so. I just wonder what we would do in this case? Panic? Theoretically we could just proceed without enabling LPIs on this redistributor, but that's probably not what a user would expect.

Print an error and/or panic are good options.

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

#12 lore raw

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

In reply to Andre Przywara

Hi Andre,

Quoted 24 lines · Andre Przywara

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

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

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

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

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

xen/arch/arm/gic-its.c

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

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

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

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

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

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

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

Please use unsigned int.

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

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

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

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

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

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

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

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

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

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

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

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

Same remark for BIT here.

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

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

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

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

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

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

Same question for the cacheability and shareability.

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

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

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

Same remark for the mask here.

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

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

Please use unsigned int for lpi_bits.

Also this function should probably be in the section __init.

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

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

%lu.

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

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

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

Missing blank line here.

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

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

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

Is it fine to continue silently if gicv3_lpi_allocate_pendtable has failed?

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

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

Ditto.

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

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

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

Please move this change in a separate patch.

Quoted 18 lines
xen/include/asm-arm/cache.h +0 -0
 #define __read_mostly __section(".data.read_mostly")
 
 #endif
xen/include/asm-arm/gic-its.h +4 -1
index 2f5c51c..48c6c78 100644

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

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

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

Quoted 8 lines
xen/include/asm-arm/gic-its.h +5 -0
@@ -36,12 +36,32 @@ extern struct list_head host_its_list;
 /* Parse the host DT and pick up all host ITSes. */
 void gicv3_its_dt_init(const struct dt_device_node *node);
 
+/* Allocate and initialize tables for each host redistributor.
+ * Returns the respective {PROP,PEND}BASER register value.
+ */
+uint64_t gicv3_lpi_get_proptable(void);
+uint64_t gicv3_lpi_allocate_pendtable(void);
[… more lines in the raw message]

Please add a newline here.

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

Quoted 4 lines
xen/include/asm-arm/gic-its.h +4 -0

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

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

Ditto

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

Quoted 4 lines
xen/include/asm-arm/gic-its.h +4 -0

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

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

Ditto

102 lines not shown · raw message

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

#13 lore raw

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

Hi Julien,

Quoted 31 lines · Julien Grall

Hi Andre,

Quoted 28 lines · Andre Przywara

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

---
 xen/arch/arm/Kconfig              |  6 ++++
 xen/arch/arm/efi/efi-boot.h       |  1 -
 xen/arch/arm/gic-its.c            | 76
+++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             | 27 ++++++++++++++
 xen/include/asm-arm/cache.h       |  4 +++
 xen/include/asm-arm/gic-its.h     | 22 +++++++++++-
 xen/include/asm-arm/gic_v3_defs.h | 48 ++++++++++++++++++++++++-
 7 files changed, 181 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index 9fe3b8e..66e2bb8 100644
@@ -50,6 +50,12 @@ config HAS_ITS
         depends on ARM_64
         depends on HAS_GICV3
 
Quoted 11 lines

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

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

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

Yeah, a command line option sounds useful, though I have to check whether this changes compile-time computation into actual runtime one.

The number is made-up, based on some reasoning on the possible memory consumption and the number of LPIs provided. 8 MB and 1 million LPIs sounded like a sweet spot.
If I got this correctly, Linux atm doesn't use more than 65536 LPIs.

Quoted 13 lines
Quoted 13 lines
xen/arch/arm/Kconfig +0 -0
 config ALTERNATIVE
     bool
 
xen/arch/arm/efi/efi-boot.h +0 -1
index 045d6ce..dc64aec 100644
@@ -10,7 +10,6 @@
 #include "efi-dom0.h"
 
 void noreturn efi_xen_start(void *fdt_ptr, uint32_t fdt_size);
-void __flush_dcache_area(const void *vaddr, unsigned long size);
 
Quoted 9 lines

xen/arch/arm/gic-its.c

Quoted 7 lines
xen/arch/arm/efi/efi-boot.h +0 -0
 #define DEVICE_TREE_GUID \
 {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69,

0xaa, 0xe0}}

xen/arch/arm/gic-its.c +0 -0
index 0f42a77..b52dff3 100644

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

OK.

Quoted 8 lines

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

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

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

This may be a rebase artifact introduced when shuffling patches around.

Quoted 11 lines

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

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

Please use unsigned int.

Quoted 13 lines

xen/arch/arm/gic-its.c

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

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

Mmmh, probably because of forest and trees and stuff ;-) Looks indeed like being pointless.

Quoted 5 lines

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

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

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

Sure.

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/gic-its.c +7 -0
+
+uint64_t gicv3_lpi_allocate_pendtable(void)
+{
+    uint64_t reg, attr;
+    void *pendtable;
+
+    attr  = GIC_BASER_CACHE_RaWaWb <<
 GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
Quoted 8 lines
Quoted 4 lines
xen/arch/arm/gic-its.c +1 -0
+    attr |= GIC_BASER_CACHE_SameAsInner <<

GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+ attr |= GIC_BASER_InnerShareable <<
GICR_PENDBASER_SHAREABILITY_SHIFT;

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

Yeah, I guess we have to. I was just not sure what to do when it doesn't. By design we don't touch the pending table after having it handed in, so technically we wouldn't even need to map it. But we have to make sure nobody steps on it and that it stays reserved.

Quoted 2 lines

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

Yes, this is probably copy & pasted from a place where it needed a loop. I just kept it because of readability. Can surely clean this up.

Quoted 17 lines
Quoted 10 lines
xen/arch/arm/gic-its.c +3 -0
+
+    /*
+     * The pending table holds one bit per LPI, so we need three bits

less
+ * than the number of LPI_BITs. But the alignment requirement
from the

xen/arch/arm/gic-its.c +3 -0
+     * ITS is 64K, so make order at least 16 (-12).
+     */
+    pendtable = alloc_xenheap_pages(MAX(lpi_data.host_lpi_bits - 3,

16) - 12, 0);

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

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

Same remark for BIT here.

Quoted 7 lines

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

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

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

Mmmh, not so sure we can extend the mask to cover the region that it RES0 at the moment. We need some mask anyway (to not clobber the upper bits), so I figured we just use what the spec says.

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

GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+ attr |= GIC_BASER_CACHE_SameAsInner <<
GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+ attr |= GIC_BASER_InnerShareable <<
GICR_PENDBASER_SHAREABILITY_SHIFT;

Same question for the cacheability and shareability.

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

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

Quoted 12 lines
xen/arch/arm/gic-its.c +2 -0
+
+    lpi_data.lpi_property = alloc_xenheap_pages(MAX_HOST_LPI_BITS -

12, 0);

xen/arch/arm/gic-its.c +4 -0
+    if ( !lpi_data.lpi_property )
+        return 0;
+
+    memset(lpi_data.lpi_property, GIC_PRI_IRQ | LPI_PROP_RES1,

MAX_HOST_LPIS);

xen/arch/arm/gic-its.c +4 -0
+    __flush_dcache_area(lpi_data.lpi_property, MAX_HOST_LPIS);
+
+    reg  = attr | ((MAX_HOST_LPI_BITS - 1) << 0);
+    reg |= virt_to_maddr(lpi_data.lpi_property) & GENMASK(51, 12);

51 lines not shown · raw message

Quoted 8 lines

7 lines not shown · raw message

237 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#14 lore raw

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

Hi Andre,

Quoted 42 lines · Andre Przywara
Quoted 29 lines · Julien Grall
Quoted 28 lines · Andre Przywara

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

---
 xen/arch/arm/Kconfig              |  6 ++++
 xen/arch/arm/efi/efi-boot.h       |  1 -
 xen/arch/arm/gic-its.c            | 76
+++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c             | 27 ++++++++++++++
 xen/include/asm-arm/cache.h       |  4 +++
 xen/include/asm-arm/gic-its.h     | 22 +++++++++++-
 xen/include/asm-arm/gic_v3_defs.h | 48 ++++++++++++++++++++++++-
 7 files changed, 181 insertions(+), 3 deletions(-)
xen/arch/arm/Kconfig +0 -0
index 9fe3b8e..66e2bb8 100644
@@ -50,6 +50,12 @@ config HAS_ITS
         depends on ARM_64
         depends on HAS_GICV3
 
Quoted 11 lines

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

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

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

Quoted 7 lines

Yeah, a command line option sounds useful, though I have to check whether this changes compile-time computation into actual runtime one.

The number is made-up, based on some reasoning on the possible memory consumption and the number of LPIs provided. 8 MB and 1 million LPIs sounded like a sweet spot.
If I got this correctly, Linux atm doesn't use more than 65536 LPIs.

You know my answer here ;). Linux is not the only guests OS supports on Xen, so we should avoid making assumptions on the best behavior based on this.

[...]

Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
xen/arch/arm/Kconfig +3 -0
+
+    /*
+     * The pending table holds one bit per LPI, so we need three bits

less
+ * than the number of LPI_BITs. But the alignment requirement
from the

xen/arch/arm/Kconfig +3 -0
+     * ITS is 64K, so make order at least 16 (-12).
+     */
+    pendtable = alloc_xenheap_pages(MAX(lpi_data.host_lpi_bits - 3,

16) - 12, 0);

xen/arch/arm/Kconfig

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

Same remark for BIT here.

Quoted 4 lines
xen/arch/arm/Kconfig +4 -0
+    this_cpu(pending_table) = pendtable;
+
+    reg  = attr | GICR_PENDBASER_PTZ;
+    reg |= virt_to_maddr(pendtable) & GENMASK(51, 16);
Quoted 6 lines
Quoted 2 lines

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

Mmmh, not so sure we can extend the mask to cover the region that it RES0 at the moment. We need some mask anyway (to not clobber the upper bits), so I figured we just use what the spec says.

The masked PA should always be equal to the PA. Otherwise we would program the wrong address into the register and who knows what can happen. So this mask is pointless.

[..]

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines

#endif /* CONFIG_HAS_ITS */

#endif /* __ASSEMBLY__ */

xen/include/asm-arm/gic_v3_defs.h +0 -0
index 6bd25a5..da5fb77 100644
@@ -44,7 +44,8 @@
 #define GICC_SRE_EL2_ENEL1           (1UL << 3)
 
Quoted 8 lines

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

Quoted 4 lines
xen/include/asm-arm/gic_v3_defs.h +2 -1
 /* Additional bits in GICD_TYPER defined by GICv3 */
-#define GICD_TYPE_ID_BITS_SHIFT 19
+#define GICD_TYPE_ID_BITS_SHIFT      19
+#define GICD_TYPE_LPIS               (1U << 17)

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

Quoted 4 lines

Yeah, I was unsure about this as well. My hunch is we should avoid the churn and keep existing names around. Experience shows that those simple renames tend to introduce nasty rebase issues, especially with a long-standing series like this.

Clean up are always welcomed ;). I am fine if it comes after.

Regards,

Signature · 6 lines
Julien Grall

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

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

In reply to Andre Przywara

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

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

74 lines not on this page · raw message

Signature · 7 lines
2.9.0


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

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

Hi Andre,

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

Quoted 122 lines · Andre Przywara

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

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

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

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

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

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

2.9.0

Quoted 4 lines

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

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

#17 lore raw

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

Hi,

Quoted 4 lines · Vijay Kilari

Hi Andre,

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

MAPD commands from Dom0, you mean?

And thanks for giving it a try!

Quoted 129 lines · Andre Przywara
Quoted 128 lines

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

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

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

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

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

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

Quoted 2 lines

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

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

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

Cheers,
Andre.

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

#18 lore raw

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

In reply to Andre Przywara
Quoted 7 lines · Andre Przywara

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

Thunderx has more than 4 PCI busses

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

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

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

why this mask and shift for?.

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

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

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

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

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

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

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

define a macro for these constants

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

2.9.0

Quoted 4 lines

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

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

#19 lore raw

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

Hi,

Quoted 10 lines · Vijay Kilari
Quoted 7 lines · Andre Przywara

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

Thunderx has more than 4 PCI busses

Yeah, I am thinking about a proper solution for that hack. We may use a default of 4 buses and allow platform to override this. Or make this a configuration value.
Or copy the Linux behaviour by limiting the number of pages to some sensible value (16MB, if I got this correctly).
Anyway I think we need indirect table support to save Thunder from allocating too much memory for this.

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

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

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

why this mask and shift for?.

According to the (current issue of the) spec bits 48-51 of the address are stored in bits 12-15 of the register.

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

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

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

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

True. Will fix it.

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

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

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

define a macro for these constants

Sure.

Cheers,
Andre.

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

#20 lore raw

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

In reply to Andre Przywara
Quoted 39 lines · Andre Przywara

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

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

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

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

Shouldn't this be called its_map_baser?

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

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

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

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

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

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

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

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

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

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

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

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

Please #define 56 and 0x7

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

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

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

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

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

#21 lore raw

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

Hi Stefano,

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

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

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

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

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

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

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

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

Regards,

Signature · 6 lines
Julien Grall

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

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

In reply to Stefano Stabellini

Hi,

Quoted 44 lines · Stefano Stabellini
Quoted 43 lines · Andre Przywara

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

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

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

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

Shouldn't this be called its_map_baser?

Yes, the BASER registers are an ITS property.

Quoted 6 lines

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

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

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

Mmh, looks so. I guess it worked because the number gets dwarfed by the page size round up below.

Quoted 7 lines
Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
Quoted 8 lines

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

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */

Is this really the best way to do it? Can't we assume ITS supports 4K, given that Xen requires 4K pages at the moment?

The ITS pages are totally independent from the core's MMU page size. So the spec says: "If the GIC implementation supports only a single, fixed page size, this field might be RO."
I take it that this means that the only implemented page size could be 64K, for instance. And in fact the KVM ITS emulation advertises exactly this to a guest.

Quoted 4 lines

Is it actually possible
to find hardware that supports 4K but with an ITS that only support 64K or 16K pages? It seems insane to me. Otherwise can't we probe the page size somehow?

We can probe by writing and seeing if it sticks - that's what the code does. Is it really so horrible? I agree it's nasty, but isn't it basically a loop around the code needed anyway?

Yes to the rest of the comments.

Cheers,
Andre.

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

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

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

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

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

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

Please #define 56 and 0x7

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

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

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

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

#23 lore raw

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

Quoted 91 lines · Andre Przywara

Hi,

Quoted 44 lines · Stefano Stabellini
Quoted 43 lines · Andre Przywara

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

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

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

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

Shouldn't this be called its_map_baser?

Yes, the BASER registers are an ITS property.

Quoted 6 lines

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

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

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

Mmh, looks so. I guess it worked because the number gets dwarfed by the page size round up below.

Quoted 7 lines
Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+    int pagesz;
+    int order;
+    void *buffer = NULL;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
Quoted 8 lines

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

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+
+    /*
+     * Loop over the page sizes (4K, 16K, 64K) to find out what the host
+     * supports.
+     */

Is this really the best way to do it? Can't we assume ITS supports 4K, given that Xen requires 4K pages at the moment?

The ITS pages are totally independent from the core's MMU page size. So the spec says: "If the GIC implementation supports only a single, fixed page size, this field might be RO."
I take it that this means that the only implemented page size could be 64K, for instance. And in fact the KVM ITS emulation advertises exactly this to a guest.

Quoted 8 lines
Quoted 4 lines

Is it actually possible
to find hardware that supports 4K but with an ITS that only support 64K or 16K pages? It seems insane to me. Otherwise can't we probe the page size somehow?

We can probe by writing and seeing if it sticks - that's what the code does. Is it really so horrible? I agree it's nasty, but isn't it basically a loop around the code needed anyway?

It looks very strange that there isn't a better way to find that info. It looks a bit like an hack. It is also bad from a software point of view being forced to cope with all three possible page granularities.

But oh well, sometimes we just have to deal with whatever the hardware offers us.

Quoted 86 lines

Yes to the rest of the comments.

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

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

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

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

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

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

Please #define 56 and 0x7

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

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

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

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

#24 lore raw

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

In reply to Andre Przywara

Hi Andre,

Quoted 28 lines · Andre Przywara

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

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

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

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

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

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

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

Please make page_bits unsigned int.

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

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

Coding style.

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

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

unsigned int nr_items

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

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

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

unsigned int here.

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

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

Same.

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

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

Same.

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

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

Coding style: for ( ... )

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

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

unsigned int nr_bytes.

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

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

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

Should not it be readq_relaxed?

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

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

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

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

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

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

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

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

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

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

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

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

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

Coding style: for ( ... )

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

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

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

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

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

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

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

Please add a newline here and ...

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

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

here.

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

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

Regards,

Signature · 6 lines
Julien Grall

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

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

In reply to Andre Przywara

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

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


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

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

Quoted 33 lines · Andre Przywara

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

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

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

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

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

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

#27 lore raw

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

In reply to Andre Przywara
Quoted 25 lines · Andre Przywara

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

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

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

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

Shouldn't it be its_map_cbaser?

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

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

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

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

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

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

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

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

#28 lore raw

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

Hi,

Quoted 29 lines · Stefano Stabellini
Quoted 28 lines · Andre Przywara

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

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

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

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+static void *gicv3_map_cbaser(void __iomem *cbasereg)

Shouldn't it be its_map_cbaser?

Yes.

Quoted 6 lines
Quoted 6 lines
xen/arch/arm/gic-its.c +6 -0
+{
+    uint64_t attr, reg;
+    void *buffer;
+
+    attr  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    attr |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
Quoted 8 lines

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

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+    attr |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    buffer = alloc_xenheap_pages(0, 0);
+    if ( !buffer )
+        return ERR_PTR(-ENOMEM);

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

In this case I agree, though "we haven't uses it much" isn't really a good argument ;-)

Cheers,
Andre.

Quoted 38 lines

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

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

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

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

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

#29 lore raw

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

In reply to Andre Przywara

Hello Andre,

Quoted 27 lines · Andre Przywara

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Regards,

Signature · 6 lines
Julien Grall

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

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

In reply to Andre Przywara

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

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


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

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

Quoted 29 lines · Andre Przywara

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

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

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

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

uint64_t

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

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

It might be worth to

  #define ITS_CMD_RING_SIZE PAGE_SIZE

for clarity

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

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

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

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

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

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

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

To avoid problems:

  pta = !!(reg & GITS_TYPER_PTA);

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

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

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

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

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

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

Please #define the 8

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

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

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

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

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

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

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

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

#32 lore raw

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

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

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

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

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

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

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

#33 lore raw

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

In reply to Stefano Stabellini

Hi,

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

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

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

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

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+#define ITS_COMMAND_SIZE        32
+
+static int its_send_command(struct host_its *hw_its, void *its_cmd)
+{
+    int readp, writep;

uint64_t

A bit overkill, but probably right type-wise.

Quoted 11 lines

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

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

It might be worth to

  #define ITS_CMD_RING_SIZE PAGE_SIZE

for clarity

Or revisit this to allow bigger queues than one page.

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

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

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

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

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

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

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

To avoid problems:

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

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

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+    its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
+    its_send_cmd_sync(hw_its, smp_processor_id());

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

gicv3_its_setup_collection() gets called for each redistributor. On the first CPU this is done _before_ we actually have set up the ITS, so we can't send any commands at this time. The function actually checks for an initialised command buffer, eventually doing nothing on the first core. However this function _here_ is only called once (on core 0). So we send the commands for core 0 now, actually at the earliest possible time.

Cheers,
Andre.

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

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

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

Please #define the 8

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

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

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

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

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

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

Quoted 1 line
xen/include/asm-arm/gic-its.h +0 -0
 #endif /* __ASSEMBLY__ */

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

#34 lore raw

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

In reply to Andre Przywara

Hi Andre,

Quoted 18 lines · Andre Przywara

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

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

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

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

The type should be paddr_t.

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

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

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

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

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

Please use uint32_t (or maybe uint64_t) here.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

See my comments on the previous its_send_* functions

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

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

The second parameter should probably be unsigned, maybe uint64_t?

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

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

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

Coding style:

/*
* Foo

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

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

Please avoid hardcoding mask and use a define.

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

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

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

Newline here

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

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

Ditto

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

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

Regards,

Signature · 6 lines
Julien Grall

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

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

Hi Julien,

(forgot to hit the Send button yesterday ...)

just going over the review comments again and found some leftovers. I fixed/addressed all comments of yours that I don't explicitly refer to here.

...

Quoted 75 lines · Julien Grall

Hi Andre,

Quoted 19 lines · Andre Przywara

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

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

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

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

The type should be paddr_t.

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

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

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

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

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

Please use uint32_t (or maybe uint64_t) here.

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

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

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

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

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

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

Quoted 11 lines

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

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

GITS_CWRITER);

xen/arch/arm/gic-its.c +4 -0
+
+    spin_unlock(&hw_its->cmd_lock);
+
+    return 0;

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

I'd rather keep this in line with the general UNIX/Linux way of returning an int, with 0 on success and a negative error number on failure. This allows easier fixing when we introduce more error handling in the future (SErrors?).
So callers just pass on the return value and it's up to the leaf functions to come up with the proper error value.

Quoted 6 lines
Quoted 6 lines
xen/arch/arm/gic-its.c +3 -0
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, int cpu,

uint64_t reg)

xen/arch/arm/gic-its.c +2 -0
+{
+    reg &= ~GENMASK(51, 16);
Quoted 8 lines

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

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+
+    if ( hw_its->pta )
+        reg |= per_cpu(rdist_addr, cpu) & GENMASK(51, 16);
+    else
+        reg |= per_cpu(rdist_id, cpu) << 16;

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

I believe we can't do easily, because the PTA bit is per ITS, not per redistributor. I see that it's rather unlikely that we have ITSes with different PTA bit settings in one system, but architecturally it's possible.

Quoted 49 lines
Quoted 39 lines
xen/arch/arm/gic-its.c +17 -0
+
+    return reg;
+}
+
+static int its_send_cmd_sync(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_SYNC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, 0x0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+static int its_send_cmd_mapc(struct host_its *its, int collection_id,

int cpu)

xen/arch/arm/gic-its.c +6 -0
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPC;
+    cmd[1] = 0x00;
+    cmd[2] = encode_rdbase(its, cpu, (collection_id & GENMASK(15, 0))

| BIT(63));

xen/arch/arm/gic-its.c +13 -0
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+void gicv3_its_setup_collection(int cpu)
+{
+    struct host_its *its;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        /* Only send commands to ITS that have been initialized

already. */

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

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+        if ( !its->cmd_buf )
+            continue;
+
+        its_send_cmd_mapc(its, cpu, cpu);
+        its_send_cmd_sync(its, cpu);

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

Quoted 3 lines

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

Originally I didn't care so much about handling ITS command queue errors, because traditionally we couldn't do too much about them, since there is no good way of communicating failure back to the guest. But since we now only issue commands outside of a non-Dom0 guest context, I added the error handling you suggested and pass any error up the call chain.

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

registers. */

xen/arch/arm/gic-its.c +2 -0
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg & ~GITS_CTLR_ENABLE, hw_its->its_base +
Quoted 1 line

GITS_CTLR);

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

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

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

See my comments on the previous its_send_* functions

Quoted 7 lines

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

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

The second parameter should probably be unsigned, maybe uint64_t?

Well, the processor ID is a 16-bit value and nicely aligns with Xen's VCPUIDs, which are declared as "int" in struct vcpu.
So I'd rather keep it as int here.

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

static int __init gicv3_populate_rdist(void)

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

their physical

Coding style:

Quoted 2 lines

/*
* Foo

Oh, right, I think I copied this commenting style wrongly from some other Xen VGIC code. Fixed that now.

Cheers,
Andre.

Quoted 100 lines
Quoted 9 lines

+ * address or by their ID. Determine those two
values and
+ * let the ITS code store them in per host CPU
variables to

xen/arch/arm/gic-v3.c +4 -0
+                     * later be able to address those redistributors.
+                     */
+                    gicv3_set_redist_addr(rdist_addr,
+                                          (typer >> 8) & GENMASK(15,

0));

Please avoid hardcoding mask and use a define.

Quoted 69 lines
xen/arch/arm/gic-v3.c +2 -0
+
                     gicv3_rdist_init_lpis(ptr);
+                }
 
                 printk("GICv3: CPU%d: Found redistributor in region

%d @%p\n",

                         smp_processor_id(), i, ptr);
xen/include/asm-arm/gic-its.h +20 -0
index b2a003f..b49d274 100644
@@ -37,6 +37,7 @@
 
 /* Register bits */
 #define GITS_CTLR_ENABLE     0x1
+#define GITS_TYPER_PTA       BIT(19)
 #define GITS_IIDR_VALUE      0x34c
 
 #define GITS_BASER_VALID                BIT(63)
@@ -59,6 +60,22 @@
                                         (31UL <<
 GITS_BASER_ENTRY_SIZE_SHIFT) |\
                                         GITS_BASER_INDIRECT)
 
+/* ITS command definitions */
+#define ITS_CMD_SIZE                    32
+
+#define GITS_CMD_MOVI                   0x01
+#define GITS_CMD_INT                    0x03
+#define GITS_CMD_CLEAR                  0x04
+#define GITS_CMD_SYNC                   0x05
+#define GITS_CMD_MAPD                   0x08
+#define GITS_CMD_MAPC                   0x09
+#define GITS_CMD_MAPTI                  0x0a
+#define GITS_CMD_MAPI                   0x0b
+#define GITS_CMD_INV                    0x0c
+#define GITS_CMD_INVALL                 0x0d
+#define GITS_CMD_MOVALL                 0x0e
+#define GITS_CMD_DISCARD                0x0f
+
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
@@ -69,7 +86,9 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;
     void *cmd_buf;
+    bool pta;
 };
 
 extern struct list_head host_its_list;
@@ -89,6 +108,12 @@ uint64_t gicv3_lpi_allocate_pendtable(void);
 int gicv3_lpi_init_host_lpis(int nr_lpis);
 int gicv3_its_init(struct host_its *hw_its);
 
+/* Set the physical address and ID for each redistributor as read

from DT. */

xen/include/asm-arm/gic-its.h +5 -0
+void gicv3_set_redist_addr(paddr_t address, int redist_id);
+
+/* Map a collection for this host CPU to each host ITS. */
+void gicv3_its_setup_collection(int cpu);
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)

xen/include/asm-arm/gic-its.h static inline int gicv3_its_init(struct host_its · line 138

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

Newline here

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

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

Ditto

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

#endif /* __ASSEMBLY__ */

Regards,

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

#36 lore raw

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

Hi Andre,

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

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

Quoted 11 lines · Andre Przywara
xen/arch/arm/gic-its.c +3 -0
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, int cpu,

uint64_t reg)

xen/arch/arm/gic-its.c +7 -0
+{
+    reg &= ~GENMASK(51, 16);
+
+    if ( hw_its->pta )
+        reg |= per_cpu(rdist_addr, cpu) & GENMASK(51, 16);
+    else
+        reg |= per_cpu(rdist_id, cpu) << 16;

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

Quoted 3 lines

I believe we can't do easily, because the PTA bit is per ITS, not per redistributor. I see that it's rather unlikely that we have ITSes with different PTA bit settings in one system, but architecturally it's possible.

How about storing the value per ITS then?

[...]

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+void gicv3_set_redist_addr(paddr_t address, int redist_id)

The second parameter should probably be unsigned, maybe uint64_t?

Well, the processor ID is a 16-bit value and nicely aligns with Xen's VCPUIDs, which are declared as "int" in struct vcpu.
So I'd rather keep it as int here.

I am not sure why you speak about the vCPUIDS where this code only deal with pCPUIDs. Anyway, the ID should never be signed and even though it has been defined as int in the structure, on ARM we are trying to use unsigned number as it makes much more sense.

Furthermore, the per-cpu value rdist_id has been defined as uint64_t, hence my request to use uint64_t.

The 2 type need to match for consistency. I am fine if you decide to use unsigned int for the per-cpu value.

Regards,

Signature · 6 lines
Julien Grall

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

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

In reply to Andre Przywara

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

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


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

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

Quoted 39 lines · Andre Przywara

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

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

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

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

Why not the following?

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

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

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

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

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

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

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

#define HOST_LPIS_PER_PAGE (PAGE_SIZE >> 3)

unsigned int table = lpi / HOST_LPIS_PER_PAGE;

then use table throughout this function.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

^ allocated

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

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

code style

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

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

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

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

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

#39 lore raw

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

Hi,

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

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

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

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

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

Why not the following?

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

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

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

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

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

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

Quoted 24 lines

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

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

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

#define HOST_LPIS_PER_PAGE (PAGE_SIZE >> 3)

unsigned int table = lpi / HOST_LPIS_PER_PAGE;

then use table throughout this function.

Quoted 6 lines

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

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

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

+1.

Regards,

Signature · 6 lines
Julien Grall

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

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

In reply to Stefano Stabellini

Hi,

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

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

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

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

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

Why not the following?

Quoted 8 lines
  union host_lpi {
      uint64_t data;
      struct {
          uint32_t virt_lpi;
          uint16_t dom_id;
          uint16_t vcpu_id;
      };
  };

I am not sure that gives me a guarantee of stuffing everything into a u64 (as per the C standard). It probably will on arm64 with gcc, but I thought better safe than sorry.

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

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

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

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

Indeed.

Quoted 16 lines

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

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

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

Quoted 1 line

#define HOST_LPIS_PER_PAGE (PAGE_SIZE >> 3)

to replace
#define HOST_LPIS_PER_PAGE (PAGE_SIZE / sizeof(union host_lpi))?

This should be computed by the compiler, as it's constant.

Quoted 1 line

unsigned int table = lpi / HOST_LPIS_PER_PAGE;

So I'd rather replace this by ">> (PAGE_SIZE - 3)".
But again the compiler would do this for us, as replacing "constant divisions by power-of-two" with "right shifts" are a text book example of easy optimization, if I remember this compiler class at uni correctly ;-)

Quoted 1 line

then use table throughout this function.

I see your point (though this is ARMv8, which always has udiv). But to prove your paranoia wrong: I don't see any divisions in the disassembly, but a lsr #3 and a lsr #9 and various other clever and cheap ARMv8 instructions ;-)
Compilers have really come a long way in 2016 ...

Quoted 7 lines

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

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

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

That's why I have "d && ..." in front. If you pass in NULL for the domain, it will skip this check. That saves us from coding the check in every caller.
Is that not good enough?

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

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

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

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

LPIs can be allocated sparsely. Each LPI uses 8 Bytes, chances are we never use more than a few dozen on a real system, so we just use two pages with this scheme.

Allocating memory for all 2 << 20 (the default) takes 8 MB (probably for nothing), extending this to 24 bits uses 128 MB already.
The problem is that Xen cannot know how many LPIs Dom0 will use, so I'd rather make this number generous here - hence the allocation scheme.

Not sure if this is actually overkill or paranoid and we would get away with a much smaller single level allocation, driven by a config option or runtime parameter, though.

Quoted 22 lines
Quoted 22 lines
xen/arch/arm/gic-its.c +17 -0
     printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);
 
     return 0;
 }
 
+/* Allocates a new host LPI to be injected as "virt_lpi" into the specified
+ * VCPU. Returns the host LPI ID or a negative error value.
+ */
+int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                uint32_t devid, uint32_t eventid,
+                                struct vcpu *v, int virt_lpi)
+{
+    int chunk, i;
+    union host_lpi hlpi, *new_chunk;
+
+    /* TODO: handle some kind of preassigned LPI mapping for DomUs */
+    if ( !its )
+        return -EPERM;
+
+    /* TODO: This could be optimized by storing some "next available" hint and
+     * only iterate if this one doesn't work. But this function should be
+     * called rarely.
Quoted 8 lines

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

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+     */

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

I can try, though keep in mind that this code is really only called on allocating a host LPI, which would only happen when an LPI gets mapped. And this is done only upon a Dom0 driver initializing a device. Normally you wouldn't expect this during the actual guest runtime.

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

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

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

^ allocated

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

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

code style

Quoted 7 lines

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

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

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

Yes, admittedly I was papering over this for the RFC (as I am afraid there's more than that).
Will look at this.

Cheers,
Andre.

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

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

#41 lore raw

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

Quoted 65 lines · Andre Przywara

Hi,

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

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

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

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

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

Why not the following?

Quoted 8 lines
  union host_lpi {
      uint64_t data;
      struct {
          uint32_t virt_lpi;
          uint16_t dom_id;
          uint16_t vcpu_id;
      };
  };
Quoted 3 lines

I am not sure that gives me a guarantee of stuffing everything into a u64 (as per the C standard). It probably will on arm64 with gcc, but I thought better safe than sorry.

I am pretty sure that it is covered by the standard, also see IHI0055A_aapcs64. Additionally I don't think the union with "data" is actually required either.

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

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

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

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

Indeed.

Quoted 16 lines

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

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

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

Quoted 1 line

#define HOST_LPIS_PER_PAGE (PAGE_SIZE >> 3)

Quoted 8 lines

to replace
#define HOST_LPIS_PER_PAGE (PAGE_SIZE / sizeof(union host_lpi))?

This should be computed by the compiler, as it's constant.

Quoted 1 line

unsigned int table = lpi / HOST_LPIS_PER_PAGE;

So I'd rather replace this by ">> (PAGE_SIZE - 3)".

This is actually what I meant, thanks.

Quoted 3 lines

But again the compiler would do this for us, as replacing "constant divisions by power-of-two" with "right shifts" are a text book example of easy optimization, if I remember this compiler class at uni correctly ;-)

Yet, we found instanced where this didn't happen in the common Xen scheduler code on x86.

Quoted 7 lines
Quoted 1 line

then use table throughout this function.

I see your point (though this is ARMv8, which always has udiv). But to prove your paranoia wrong: I don't see any divisions in the disassembly, but a lsr #3 and a lsr #9 and various other clever and cheap ARMv8 instructions ;-)
Compilers have really come a long way in 2016 ...

Fair enough, thanks for checking. That is enough for me.

Quoted 11 lines
Quoted 6 lines

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

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

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

That's why I have "d && ..." in front. If you pass in NULL for the domain, it will skip this check. That saves us from coding the check in every caller.
Is that not good enough?

There is a simple solution to this: write two functions, one without the check and a wrapper to it with the check.

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

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

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

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

LPIs can be allocated sparsely. Each LPI uses 8 Bytes, chances are we never use more than a few dozen on a real system, so we just use two pages with this scheme.

Quoted 8 lines

Allocating memory for all 2 << 20 (the default) takes 8 MB (probably for nothing), extending this to 24 bits uses 128 MB already.
The problem is that Xen cannot know how many LPIs Dom0 will use, so I'd rather make this number generous here - hence the allocation scheme.

Not sure if this is actually overkill or paranoid and we would get away with a much smaller single level allocation, driven by a config option or runtime parameter, though.

All right. Please write an in-code comment explaining this reasoning with a sample number of LPIs used by Dom0 on a real case scenario.

Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
xen/arch/arm/gic-its.c +17 -0
     printk("GICv3: using at most %ld LPIs on the host.\n", MAX_HOST_LPIS);
 
     return 0;
 }
 
+/* Allocates a new host LPI to be injected as "virt_lpi" into the specified
+ * VCPU. Returns the host LPI ID or a negative error value.
+ */
+int gicv3_lpi_allocate_host_lpi(struct host_its *its,
+                                uint32_t devid, uint32_t eventid,
+                                struct vcpu *v, int virt_lpi)
+{
+    int chunk, i;
+    union host_lpi hlpi, *new_chunk;
+
+    /* TODO: handle some kind of preassigned LPI mapping for DomUs */
+    if ( !its )
+        return -EPERM;
+
+    /* TODO: This could be optimized by storing some "next available" hint and
+     * only iterate if this one doesn't work. But this function should be
+     * called rarely.
Quoted 8 lines

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

Quoted 1 line
xen/arch/arm/gic-its.c +1 -0
+     */

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

Quoted 4 lines

I can try, though keep in mind that this code is really only called on allocating a host LPI, which would only happen when an LPI gets mapped. And this is done only upon a Dom0 driver initializing a device. Normally you wouldn't expect this during the actual guest runtime.

It would happen during actual guest runtime with device assignment, wouldn't?

A simple pointer to last would be a good start, and in-code comment about how many times we loop to find an empty plpi on a normal case (no device assignment).

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

#42 lore raw

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

In reply to Andre Przywara

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

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


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

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

Quoted 43 lines · Andre Przywara

The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host.
For this we have to map each device that Dom0 may request to a host ITS device with the same identifier.
Allocate the respective memory and enter each device into a list to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-its.c        | 90 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h | 16 ++++++++
 2 files changed, 106 insertions(+)
xen/arch/arm/gic-its.c +22 -0
index 2140e4a..bf1f5b5 100644
@@ -168,6 +168,94 @@ static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             int size, uint64_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = size & GENMASK(4, 0);
+    cmd[2] = itt_addr & GENMASK(51, 8);
+    if ( valid )
+        cmd[2] |= BIT(63);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid)
+{
+    void *itt_addr = NULL;
+    struct its_devices *dev, *temp;
+    bool reuse_dev = false;
+

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 202

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    list_for_each_entry_safe(dev, temp, &hw_its->its_devices, entry)
+    {
+        if ( (dev->d->domain_id != d->domain_id) || (dev->devid != devid) )
+            continue;
+
+        its_send_cmd_mapd(hw_its, dev->devid, 0, 0, false);
+        xfree(dev->itt_addr);
+        if ( !valid )
+        {
+            xfree(dev);

xfree() should be done after list_del()

Quoted 21 lines
xen/arch/arm/gic-its.c +21 -0
+            list_del(&dev->entry);
+
+            return 0;
+        }
+
+        reuse_dev = true;
+        break;
+    }
+
+    if ( !valid )
+        return 0;
+
+    itt_addr = _xmalloc(BIT(bits) * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        return -ENOMEM;
+
+    if ( !reuse_dev )
+    {
+        dev = xmalloc(struct its_devices);
+        if ( !dev )
+            return -ENOMEM;

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 233

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+
+        list_add_tail(&dev->entry, &hw_its->its_devices);
+    }
+
+    dev->itt_addr = itt_addr;
+    dev->d = d;
+    dev->devid = devid;
+
+    return its_send_cmd_mapd(hw_its, devid, bits - 1,
+                             itt_addr ? virt_to_maddr(itt_addr) : 0, true);

check on itt_addr is redundant

Quoted 8 lines
xen/arch/arm/gic-its.c +8 -0
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+int its_remove_domain(struct domain *d)
+{
+    struct host_its *its;
+    struct its_devices *dev, *temp;
+

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 251

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        list_for_each_entry_safe(dev, temp, &its->its_devices, entry)
+        {
+            if ( dev->d->domain_id != d->domain_id )
+                continue;
+
+            its_send_cmd_mapd(its, dev->devid, 0, 0, false);
+            xfree(dev->itt_addr);
+            xfree(dev);

xfree() should be done after list_del(

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 253

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+            list_del(&dev->entry);
+        }

This code is same as above. Can be moved to a separate function?

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 388

Quoted 13 lines
xen/arch/arm/gic-its.c +6 -0
+    }
+
+    return 0;
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 void gicv3_its_setup_collection(int cpu)
 {
@@ -297,6 +385,7 @@ int gicv3_its_init(struct host_its *hw_its)
 
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     hw_its->pta = reg & GITS_TYPER_PTA;
+    hw_its->itte_size = ((reg >> 4) & 0xf) + 1;

can define a macro for these constants

Quoted 56 lines
xen/arch/arm/gic-its.c +1 -0
     for (i = 0; i < 8; i++)
     {
@@ -520,6 +609,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->size = size;
         its_data->dt_node = its;
         spin_lock_init(&its_data->cmd_lock);
+        INIT_LIST_HEAD(&its_data->its_devices);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/include/asm-arm/gic-its.h +16 -1
index 512a388..4e9841a 100644
@@ -79,6 +79,13 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
+struct its_devices {
+    struct list_head entry;
+    struct domain *d;
+    void *itt_addr;
+    int devid;
+};
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -88,6 +95,8 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    struct list_head its_devices;
+    int itte_size;
     bool pta;
 };
 
@@ -114,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* Map a device on the host by allocating an ITT on the host (ITS).
+ * "bits" specifies how many events (interrupts) this device will need.
+ * Setting "valid" to false deallocates the device.
+ */
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid);
+
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
--

2.9.0

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#44 lore raw

Re: [RFC PATCH 07/24] ARM: GICv3 ITS: introduce device mapping

Hi,

Quoted 47 lines · Vijay Kilari
Quoted 46 lines · Andre Przywara

The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host.
For this we have to map each device that Dom0 may request to a host ITS device with the same identifier.
Allocate the respective memory and enter each device into a list to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-its.c        | 90 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h | 16 ++++++++
 2 files changed, 106 insertions(+)
xen/arch/arm/gic-its.c +25 -0
index 2140e4a..bf1f5b5 100644
@@ -168,6 +168,94 @@ static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             int size, uint64_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = size & GENMASK(4, 0);
+    cmd[2] = itt_addr & GENMASK(51, 8);
+    if ( valid )
+        cmd[2] |= BIT(63);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid)
+{
+    void *itt_addr = NULL;
+    struct its_devices *dev, *temp;
+    bool reuse_dev = false;
+
+    list_for_each_entry_safe(dev, temp, &hw_its->its_devices, entry)
+    {
+        if ( (dev->d->domain_id != d->domain_id) || (dev->devid != devid) )
Quoted 8 lines

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 202

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+            continue;
+
+        its_send_cmd_mapd(hw_its, dev->devid, 0, 0, false);
+        xfree(dev->itt_addr);
+        if ( !valid )
+        {
+            xfree(dev);

xfree() should be done after list_del()

Oh, indeed.

Quoted 24 lines
Quoted 24 lines
xen/arch/arm/gic-its.c +24 -0
+            list_del(&dev->entry);
+
+            return 0;
+        }
+
+        reuse_dev = true;
+        break;
+    }
+
+    if ( !valid )
+        return 0;
+
+    itt_addr = _xmalloc(BIT(bits) * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        return -ENOMEM;
+
+    if ( !reuse_dev )
+    {
+        dev = xmalloc(struct its_devices);
+        if ( !dev )
+            return -ENOMEM;
+
+        list_add_tail(&dev->entry, &hw_its->its_devices);
+    }
Quoted 8 lines

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 233

Quoted 7 lines
xen/arch/arm/gic-its.c +7 -0
+
+    dev->itt_addr = itt_addr;
+    dev->d = d;
+    dev->devid = devid;
+
+    return its_send_cmd_mapd(hw_its, devid, bits - 1,
+                             itt_addr ? virt_to_maddr(itt_addr) : 0, true);

check on itt_addr is redundant

Ah, yes, this is an artifact of an earlier version. Thanks for spotting this.

Quoted 21 lines
Quoted 9 lines
xen/arch/arm/gic-its.c +8 -0
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+int its_remove_domain(struct domain *d)
+{
+    struct host_its *its;
+    struct its_devices *dev, *temp;
+

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 251

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        list_for_each_entry_safe(dev, temp, &its->its_devices, entry)
+        {
+            if ( dev->d->domain_id != d->domain_id )
+                continue;
+
+            its_send_cmd_mapd(its, dev->devid, 0, 0, false);
+            xfree(dev->itt_addr);
+            xfree(dev);

xfree() should be done after list_del(

Quoted 3 lines

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 253

Quoted 2 lines
xen/arch/arm/gic-its.c +2 -0
+            list_del(&dev->entry);
+        }

This code is same as above. Can be moved to a separate function?

Probably. Will take a look.

Thanks,
Andre.

Quoted 70 lines

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 388

Quoted 13 lines
xen/arch/arm/gic-its.c +6 -0
+    }
+
+    return 0;
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 void gicv3_its_setup_collection(int cpu)
 {
@@ -297,6 +385,7 @@ int gicv3_its_init(struct host_its *hw_its)
 
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     hw_its->pta = reg & GITS_TYPER_PTA;
+    hw_its->itte_size = ((reg >> 4) & 0xf) + 1;

can define a macro for these constants

Quoted 56 lines
xen/arch/arm/gic-its.c +1 -0
     for (i = 0; i < 8; i++)
     {
@@ -520,6 +609,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->size = size;
         its_data->dt_node = its;
         spin_lock_init(&its_data->cmd_lock);
+        INIT_LIST_HEAD(&its_data->its_devices);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/include/asm-arm/gic-its.h +16 -1
index 512a388..4e9841a 100644
@@ -79,6 +79,13 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
+struct its_devices {
+    struct list_head entry;
+    struct domain *d;
+    void *itt_addr;
+    int devid;
+};
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -88,6 +95,8 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    struct list_head its_devices;
+    int itte_size;
     bool pta;
 };
 
@@ -114,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* Map a device on the host by allocating an ITT on the host (ITS).
+ * "bits" specifies how many events (interrupts) this device will need.
+ * Setting "valid" to false deallocates the device.
+ */
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid);
+
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
--

2.9.0

Quoted 5 lines
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#45 lore raw

Re: [RFC PATCH 07/24] ARM: GICv3 ITS: introduce device mapping

In reply to Andre Przywara
Quoted 51 lines · Andre Przywara

The ITS uses device IDs to map LPIs to a device. Dom0 will later use those IDs, which we directly pass on to the host.
For this we have to map each device that Dom0 may request to a host ITS device with the same identifier.
Allocate the respective memory and enter each device into a list to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-its.c        | 90 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h | 16 ++++++++
 2 files changed, 106 insertions(+)
xen/arch/arm/gic-its.c +30 -0
index 2140e4a..bf1f5b5 100644
@@ -168,6 +168,94 @@ static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu)
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             int size, uint64_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = size & GENMASK(4, 0);
+    cmd[2] = itt_addr & GENMASK(51, 8);
+    if ( valid )
+        cmd[2] |= BIT(63);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid)
+{
+    void *itt_addr = NULL;
+    struct its_devices *dev, *temp;
+    bool reuse_dev = false;
+
+    list_for_each_entry_safe(dev, temp, &hw_its->its_devices, entry)
+    {
+        if ( (dev->d->domain_id != d->domain_id) || (dev->devid != devid) )
+            continue;
+
+        its_send_cmd_mapd(hw_its, dev->devid, 0, 0, false);
+        xfree(dev->itt_addr);
+        if ( !valid )

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 210

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+        {
+            xfree(dev);
+            list_del(&dev->entry);
+
+            return 0;
+        }
+
+        reuse_dev = true;
+        break;
+    }

I don't think we want to go through the whole list every time this function is called. Devices can be thousands. I would split it in two:
one to retrieve existing mappings and another to allocate new ones. We need to make sure we don't call the function to retrieve existing mappings often.

We can also consider using a rbtree instead of a list, or if devids are a dense numeric group, we could use an array and direct access.

Quoted 33 lines
xen/arch/arm/gic-its.c +33 -0
+    if ( !valid )
+        return 0;
+
+    itt_addr = _xmalloc(BIT(bits) * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        return -ENOMEM;
+
+    if ( !reuse_dev )
+    {
+        dev = xmalloc(struct its_devices);
+        if ( !dev )
+            return -ENOMEM;
+
+        list_add_tail(&dev->entry, &hw_its->its_devices);
+    }
+
+    dev->itt_addr = itt_addr;
+    dev->d = d;
+    dev->devid = devid;
+
+    return its_send_cmd_mapd(hw_its, devid, bits - 1,
+                             itt_addr ? virt_to_maddr(itt_addr) : 0, true);
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+int its_remove_domain(struct domain *d)
+{
+    struct host_its *its;
+    struct its_devices *dev, *temp;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        list_for_each_entry_safe(dev, temp, &its->its_devices, entry)

xen/arch/arm/gic-its.c static int its_send_cmd_mapc(struct host_its *its, int collection_id, int cpu) · line 254

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+        {
+            if ( dev->d->domain_id != d->domain_id )
+                continue;
+
+            its_send_cmd_mapd(its, dev->devid, 0, 0, false);
+            xfree(dev->itt_addr);
+            xfree(dev);
+            list_del(&dev->entry);
+        }
+    }

Again scanning the full list on every domain destruction is not good. This is easy to work around, even without completely reworking the data structures, because we could add a second per-domain list to link all devices that belong to the same domain.

xen/arch/arm/gic-its.c int gicv3_its_init(struct host_its *hw_its) · line 388

Quoted 11 lines
xen/arch/arm/gic-its.c +4 -0
+    return 0;
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 void gicv3_its_setup_collection(int cpu)
 {
@@ -297,6 +385,7 @@ int gicv3_its_init(struct host_its *hw_its)
 
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     hw_its->pta = reg & GITS_TYPER_PTA;
+    hw_its->itte_size = ((reg >> 4) & 0xf) + 1;

Please add some #define

Quoted 54 lines
xen/arch/arm/gic-its.c +1 -0
     for (i = 0; i < 8; i++)
     {
@@ -520,6 +609,7 @@ void gicv3_its_dt_init(const struct dt_device_node *node)
         its_data->size = size;
         its_data->dt_node = its;
         spin_lock_init(&its_data->cmd_lock);
+        INIT_LIST_HEAD(&its_data->its_devices);
 
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/include/asm-arm/gic-its.h +16 -0
index 512a388..4e9841a 100644
@@ -79,6 +79,13 @@
 #ifndef __ASSEMBLY__
 #include <xen/device_tree.h>
 
+struct its_devices {
+    struct list_head entry;
+    struct domain *d;
+    void *itt_addr;
+    int devid;
+};
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -88,6 +95,8 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    struct list_head its_devices;
+    int itte_size;
     bool pta;
 };
 
@@ -114,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* Map a device on the host by allocating an ITT on the host (ITS).
+ * "bits" specifies how many events (interrupts) this device will need.
+ * Setting "valid" to false deallocates the device.
+ */
+int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
+                         int devid, int bits, bool valid);
+
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#46 lore raw

RFC PATCH 8/24 ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +3 -0
index 63c744a..ebe4035 100644
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +53 -3

102 lines not on this page · raw message

xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +10 -0
index 4e9841a..1f881c0 100644
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -0
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#47 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 25 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +0 -0
index 63c744a..ebe4035 100644

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 510

Quoted 5 lines
xen/arch/arm/gic.c +2 -0
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )

Can define something line is_lpi(irq) and use it everywhere

Quoted 70 lines
xen/arch/arm/gic.c +1 -0
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +19 -2
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
+    if ( virq >= 8192 )
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    /* TODO: locking! */
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

With this approach of allocating pending_irq on demand, if the
pending_lpi_list
is at n position then it iterates for long time to find pending_irq entry. This will increase LPI injection time to domain.

Why can't we use btree?

Quoted 66 lines
xen/arch/arm/vgic.c +24 -1
+
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);
+
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +0 -0
index 4e9841a..1f881c0 100644

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 142

Quoted 7 lines
xen/include/asm-arm/gic-its.h +4 -0
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;

Why lpi priority is fixed?. can't we use domain set lpi priority?

Quoted 49 lines
xen/include/asm-arm/gic-its.h +6 -0
+}
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -1
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
--

2.9.0

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#48 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi,

Quoted 21 lines · Vijay Kilari
Quoted 20 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
Quoted 11 lines

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 510

Quoted 10 lines
xen/arch/arm/gic.c +2 -0
index 63c744a..ebe4035 100644
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )

Can define something line is_lpi(irq) and use it everywhere

Yes, that was on my list anyway.

Quoted 85 lines
Quoted 70 lines
xen/arch/arm/gic.c +1 -0
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +19 -2
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
+    if ( virq >= 8192 )
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    /* TODO: locking! */
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

With this approach of allocating pending_irq on demand, if the
pending_lpi_list
is at n position then it iterates for long time to find pending_irq entry. This will increase LPI injection time to domain.

Quoted 1 line

Why can't we use btree?

That's an optimization. You will find that the actual number of interrupts on a VCPU at any given time is very low, especially if we look at LPIs only. So my hunch is that it's either 0, 1 or 2, not more. So for simplicity I'd keep it as a list for now. If we see issues, we can amend this at any time.

Quoted 60 lines
Quoted 60 lines
xen/arch/arm/vgic.c +24 -1
+
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);
+
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
Quoted 14 lines

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 142

Quoted 13 lines
xen/include/asm-arm/domain.h +0 -0
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +4 -0
index 4e9841a..1f881c0 100644
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;

Why lpi priority is fixed?. can't we use domain set lpi priority?

Mmh, looks like a rebase artifact. It is fixed if the ITS isn't configured, but should just be the prototype if not.
The final file has it right, so I guess this gets amended in some later patch. Thanks for spotting this.

Cheers,
Andre.

Quoted 49 lines
Quoted 49 lines
xen/include/asm-arm/gic-its.h +6 -0
+}
+
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -1
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
--

2.9.0

Quoted 5 lines
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#49 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara
Quoted 25 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +0 -0
index 63c744a..ebe4035 100644

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 511

Quoted 6 lines
xen/arch/arm/gic.c +3 -0
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )
+                p->irq = 0;

I believe that 0 is a valid irq number, we need to come up with a different invalid_irq value, and we should #define it. We could also consider checking if the irq is inflight (linked to the inflight list) instead of using irq == 0 to understand if it is reusable.

Quoted 39 lines
xen/arch/arm/gic.c +0 -0
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +3 -1
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);

xen/arch/arm/vgic.c struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq) · line 253

Quoted 9 lines
xen/arch/arm/vgic.c +2 -1
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
+    if ( virq >= 8192 )

Please introduce a convenience static inline function such as:

bool is_lpi(unsigned int irq)

Quoted 13 lines
xen/arch/arm/vgic.c +6 -0
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 467

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    /* TODO: locking! */

Yeah, this needs to be fixed in v1 :-)

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 8 lines
xen/arch/arm/vgic.c +8 -0
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

This is another one of those cases where a list is too slow for the hot path. The idea of allocating pending_irq struct on demand is good, but storing them in a linked list would kill performance. Probably the best thing we could do is an hashtable and we should preallocate the initial array of elements. I don't know what the size of the initial array should be, but we can start around 50, and change it in the future once we do tests with real workloads. Of course the other key parameter is the hash function, not sure which one is the right one, but ideally we would never have to allocate new pending_irq struct for LPIs because the preallocated set would suffice.

I could be convinced that a list is sufficient if we do some real benchmarking and it turns out that lpi_to_pending always resolve in less than ~5 steps.

Quoted 11 lines
xen/arch/arm/vgic.c +11 -0
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 497

Quoted 10 lines
xen/arch/arm/vgic.c +7 -0
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+

spurious change

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 502

Quoted 5 lines
xen/arch/arm/vgic.c +1 -0
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )

Use the new static inline

Quoted 14 lines
xen/arch/arm/vgic.c +2 -1
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 

xen/arch/arm/vgic.c void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq) · line 539

Quoted 4 lines
xen/arch/arm/vgic.c +1 -0
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);

Why this change?

Quoted 20 lines
xen/arch/arm/vgic.c +0 -0
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +0 -0
index 4e9841a..1f881c0 100644

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 143

Quoted 8 lines
xen/include/asm-arm/gic-its.h +5 -0
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}

Does it mean that we don't allow changes to LPI priorities?

Quoted 46 lines
xen/include/asm-arm/gic-its.h +4 -0
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -0
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#50 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi Stefano,

as just mentioned in my last reply, I missed that email last time. Sorry for that.

Replying to the comments that still apply to the new drop ...

Quoted 21 lines · Stefano Stabellini
Quoted 20 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
Quoted 16 lines

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 511

Quoted 11 lines
xen/arch/arm/gic.c +3 -0
index 63c744a..ebe4035 100644
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )
+                p->irq = 0;

I believe that 0 is a valid irq number, we need to come up with a different invalid_irq value, and we should #define it. We could also consider checking if the irq is inflight (linked to the inflight list) instead of using irq == 0 to understand if it is reusable.

But those pending_irqs here are used by LPIs only, where everything below 8192 is invalid. So that seemed like an easy and straightforward value to use. The other, statically allocated pending_irqs would never read an IRQ number above 8192. When searching for an empty pending_irq for a new LPI, we would never touch any of the statically allocated structs, so this is safe, isn't it?

Quoted 47 lines
Quoted 47 lines
xen/arch/arm/gic.c +0 -0
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +4 -2
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
Quoted 5 lines

xen/arch/arm/vgic.c struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq) · line 253

Quoted 1 line
xen/arch/arm/vgic.c +1 -0
+    if ( virq >= 8192 )

Please introduce a convenience static inline function such as:

bool is_lpi(unsigned int irq)

Sure.

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/vgic.c +3 -0
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
Quoted 15 lines

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 467

Quoted 13 lines
xen/arch/arm/vgic.c +13 -0
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    /* TODO: locking! */

Yeah, this needs to be fixed in v1 :-)

I fixed that in the RFC v2 post.

Quoted 10 lines

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 9 lines
xen/arch/arm/vgic.c +8 -0
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
Quoted 10 lines

This is another one of those cases where a list is too slow for the hot path. The idea of allocating pending_irq struct on demand is good, but storing them in a linked list would kill performance. Probably the best thing we could do is an hashtable and we should preallocate the initial array of elements. I don't know what the size of the initial array should be, but we can start around 50, and change it in the future once we do tests with real workloads. Of course the other key parameter is the hash function, not sure which one is the right one, but ideally we would never have to allocate new pending_irq struct for LPIs because the preallocated set would suffice.

As I mentioned in the last post, I expect this number to be really low (less than 5). Let's face it: If you have multiple interrupts pending for a significant amount of time you won't make any actual progress in the guest, because it's busy with handling interrupts.
So my picture of LPI handling is:
1) A device triggers an MSI, so the host receives the LPI. Ideally this will be handled by the pCPU where the right VCPU is running atm, so it will exit to EL2. Xen will handle the LPI by assigning one struct pending_irq to it and will inject it into the guest.
2) The VCPU gets to run again and calls the interrupt handler, because the (virtual) LPI is pending.
3) The (Linux) IRQ handler reads the ICC_IAR register to learn the IRQ number, and will get the virtual LPI number.
=> At this point the LPI is done when it comes to the VGIC. The LR state will be set to 0 (neither pending or active). This is independent of the EOI the handler will execute soon (or later).
4) On the next exit the VGIC code will discover that the IRQ is done (LR.state == 0) and will discard the struct pending_irq (set the LPI number to 0 to make it available to the next LPI).

Even if there would be multiple LPIs pending at the same time (because the guest had interrupts disabled, for instance), I believe they can be all handled without exiting. Upon EOIing (priority-dropping, really) the first LPI, the next virtual LPI would fire, calling the interrupt handler again, and so no. Unless the kernel decides to do something that exits (even accessing the hardware normally wouldn't, I believe), we can clear all pending LPIs in one go.

So I have a hard time to imagine how we can really have many LPIs pending and thus struct pending_irqs allocated.
Note that this may differ from SPIs, for instance, because the IRQ life cycle is more complex there (extending till the EOI).

Does that make some sense? Or am I missing something here?

Quoted 3 lines

I could be convinced that a list is sufficient if we do some real benchmarking and it turns out that lpi_to_pending always resolve in less than ~5 steps.

I can try to do this once I get it running on some silicon ...

Quoted 47 lines
Quoted 11 lines
xen/arch/arm/vgic.c +11 -0
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 497

Quoted 10 lines
xen/arch/arm/vgic.c +7 -0
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+

spurious change

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 502

Quoted 5 lines
xen/arch/arm/vgic.c +1 -0
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )

Use the new static inline

Quoted 13 lines
xen/arch/arm/vgic.c +2 -1
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 
Quoted 7 lines

xen/arch/arm/vgic.c void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq) · line 539

Quoted 5 lines
xen/arch/arm/vgic.c +1 -0
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);

Why this change?

Because we now need to hold the lock before calling irq_to_pending(), which now may call lpi_to_pending().

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/vgic.c +0 -0
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
Quoted 16 lines
Quoted 6 lines
xen/include/asm-arm/domain.h +0 -0
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +0 -0
index 4e9841a..1f881c0 100644

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 143

Quoted 8 lines
xen/include/asm-arm/gic-its.h +5 -0
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}

Does it mean that we don't allow changes to LPI priorities?

This is placeholder code for now, until we learn about the virtual property table in patch 11/24 (where this function gets amended). The new code drop gets away without this function here entirely.

Cheers,
Andre.

Quoted 31 lines
Quoted 31 lines
xen/include/asm-arm/gic-its.h +4 -0
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +6 -0
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
Quoted 16 lines
Quoted 15 lines
xen/include/asm-arm/vgic.h +3 -0
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#51 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 46 lines · Andre Przywara

Hi Stefano,

as just mentioned in my last reply, I missed that email last time. Sorry for that.

Replying to the comments that still apply to the new drop ...

Quoted 21 lines · Stefano Stabellini
Quoted 20 lines · Andre Przywara

For the same reason that allocating a struct irq_desc for each possible LPI is not an option, having a struct pending_irq for each LPI is also not feasible. However we actually only need those when an interrupt is on a vCPU (or is about to be injected). Maintain a list of those structs that we can use for the lifecycle of a guest LPI. We allocate new entries if necessary, however reuse pre-owned entries whenever possible.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

---
 xen/arch/arm/gic.c            |  3 +++
 xen/arch/arm/vgic-v3.c        |  2 ++
 xen/arch/arm/vgic.c           | 56 ++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 10 ++++++++
 xen/include/asm-arm/vgic.h    |  9 +++++++
 6 files changed, 78 insertions(+), 3 deletions(-)
Quoted 16 lines

xen/arch/arm/gic.c static void gic_update_one_lr(struct vcpu *v, int i) · line 511

Quoted 11 lines
xen/arch/arm/gic.c +3 -0
index 63c744a..ebe4035 100644
@@ -506,6 +506,9 @@ static void gic_update_one_lr(struct vcpu *v, int i)
                 struct vcpu *v_target = vgic_get_target_vcpu(v, irq);
                 irq_set_affinity(p->desc, cpumask_of(v_target->processor));
             }
+            /* If this was an LPI, mark this struct as available again. */
+            if ( p->irq >= 8192 )
+                p->irq = 0;

I believe that 0 is a valid irq number, we need to come up with a different invalid_irq value, and we should #define it. We could also consider checking if the irq is inflight (linked to the inflight list) instead of using irq == 0 to understand if it is reusable.

Quoted 6 lines

But those pending_irqs here are used by LPIs only, where everything below 8192 is invalid. So that seemed like an easy and straightforward value to use. The other, statically allocated pending_irqs would never read an IRQ number above 8192. When searching for an empty pending_irq for a new LPI, we would never touch any of the statically allocated structs, so this is safe, isn't it?

I think you are right. Still, please #define it.

Quoted 104 lines
Quoted 47 lines
Quoted 47 lines
xen/arch/arm/gic.c +0 -0
         }
     }
 }
xen/arch/arm/vgic-v3.c +2 -0
index ec038a3..e9b6490 100644
@@ -1388,6 +1388,8 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
         v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
 
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +4 -2
index 0965119..b961551 100644
@@ -31,6 +31,8 @@
 #include <asm/mmio.h>
 #include <asm/gic.h>
 #include <asm/vgic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int rank)
 {
@@ -61,7 +63,7 @@ struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq)
     return vgic_get_rank(v, rank);
 }
 
-static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
+void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
 {
     INIT_LIST_HEAD(&p->inflight);
     INIT_LIST_HEAD(&p->lr_queue);
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq)
 
 static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
 {
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    struct vgic_irq_rank *rank;
     unsigned long flags;
     int priority;
 
Quoted 5 lines

xen/arch/arm/vgic.c struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq) · line 253

Quoted 1 line
xen/arch/arm/vgic.c +1 -0
+    if ( virq >= 8192 )

Please introduce a convenience static inline function such as:

bool is_lpi(unsigned int irq)

Sure.

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/vgic.c +3 -0
+        return gicv3_lpi_get_priority(v->domain, virq);
+
+    rank = vgic_rank_irq(v, virq);
     vgic_lock_rank(v, rank, flags);
     priority = rank->priority[virq & INTERRUPT_RANK_MASK];
     vgic_unlock_rank(v, rank, flags);
@@ -446,13 +452,55 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
     return 1;
 }
 
Quoted 15 lines

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 467

Quoted 13 lines
xen/arch/arm/vgic.c +13 -0
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    /* TODO: locking! */

Yeah, this needs to be fixed in v1 :-)

I fixed that in the RFC v2 post.

Quoted 10 lines

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 9 lines
xen/arch/arm/vgic.c +8 -0
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
Quoted 10 lines

This is another one of those cases where a list is too slow for the hot path. The idea of allocating pending_irq struct on demand is good, but storing them in a linked list would kill performance. Probably the best thing we could do is an hashtable and we should preallocate the initial array of elements. I don't know what the size of the initial array should be, but we can start around 50, and change it in the future once we do tests with real workloads. Of course the other key parameter is the hash function, not sure which one is the right one, but ideally we would never have to allocate new pending_irq struct for LPIs because the preallocated set would suffice.

Quoted 2 lines

As I mentioned in the last post, I expect this number to be really low (less than 5).

Are you able to check this assumption in a real scenario? If not you, somebody else?

Quoted 10 lines

Let's face it: If you have multiple interrupts pending
for a significant amount of time you won't make any actual progress in the guest, because it's busy with handling interrupts.
So my picture of LPI handling is:
1) A device triggers an MSI, so the host receives the LPI. Ideally this will be handled by the pCPU where the right VCPU is running atm, so it will exit to EL2. Xen will handle the LPI by assigning one struct pending_irq to it and will inject it into the guest.
2) The VCPU gets to run again and calls the interrupt handler, because the (virtual) LPI is pending.

Quoted 8 lines

3) The (Linux) IRQ handler reads the ICC_IAR register to learn the IRQ number, and will get the virtual LPI number.
=> At this point the LPI is done when it comes to the VGIC. The LR state will be set to 0 (neither pending or active). This is independent of the EOI the handler will execute soon (or later).
4) On the next exit the VGIC code will discover that the IRQ is done (LR.state == 0) and will discard the struct pending_irq (set the LPI number to 0 to make it available to the next LPI).

I am following

Quoted 8 lines

Even if there would be multiple LPIs pending at the same time (because the guest had interrupts disabled, for instance), I believe they can be all handled without exiting. Upon EOIing (priority-dropping, really) the first LPI, the next virtual LPI would fire, calling the interrupt handler again, and so no. Unless the kernel decides to do something that exits (even accessing the hardware normally wouldn't, I believe), we can clear all pending LPIs in one go.

Quoted 6 lines

So I have a hard time to imagine how we can really have many LPIs pending and thus struct pending_irqs allocated.
Note that this may differ from SPIs, for instance, because the IRQ life cycle is more complex there (extending till the EOI).

Does that make some sense? Or am I missing something here?

In my tests with much smaller platforms than the ones existing today, I could easily have 2-3 interrupts pending at the same time without much load and without any SR-IOV NICs or any other fancy PCIE hardware. It would be nice to test on Cavium ThunderX for example. It's also easy to switch to rbtrees.

Quoted 95 lines
Quoted 3 lines

I could be convinced that a list is sufficient if we do some real benchmarking and it turns out that lpi_to_pending always resolve in less than ~5 steps.

I can try to do this once I get it running on some silicon ...

Quoted 47 lines
Quoted 11 lines
xen/arch/arm/vgic.c +11 -0
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 497

Quoted 10 lines
xen/arch/arm/vgic.c +7 -0
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
+

spurious change

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 502

Quoted 5 lines
xen/arch/arm/vgic.c +1 -0
     /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
+    else if ( irq >= 8192 )

Use the new static inline

Quoted 13 lines
xen/arch/arm/vgic.c +2 -1
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +528,7 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct pending_irq *iter, *n = irq_to_pending(v, virq);
+    struct pending_irq *iter, *n;
     unsigned long flags;
     bool_t running;
 
Quoted 7 lines

xen/arch/arm/vgic.c void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq) · line 539

Quoted 5 lines
xen/arch/arm/vgic.c +1 -0
@@ -488,6 +536,8 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
+    n = irq_to_pending(v, virq);

Why this change?

Because we now need to hold the lock before calling irq_to_pending(), which now may call lpi_to_pending().

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/vgic.c +0 -0
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +1 -0
index 9452fcd..ae8a9de 100644
@@ -249,6 +249,7 @@ struct arch_vcpu
         paddr_t rdist_base;
 #define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
         uint8_t flags;
+        struct list_head pending_lpi_list;
     } vgic;
 
Quoted 16 lines
Quoted 6 lines
xen/include/asm-arm/domain.h +0 -0
     /* Timer registers  */
xen/include/asm-arm/gic-its.h +0 -0
index 4e9841a..1f881c0 100644

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 143

Quoted 8 lines
xen/include/asm-arm/gic-its.h +5 -0
@@ -136,6 +136,12 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
+
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}

Does it mean that we don't allow changes to LPI priorities?

Quoted 55 lines

This is placeholder code for now, until we learn about the virtual property table in patch 11/24 (where this function gets amended). The new code drop gets away without this function here entirely.

Cheers,
Andre.

Quoted 47 lines
Quoted 46 lines
xen/include/asm-arm/gic-its.h +4 -0
 #else
 
 static inline void gicv3_its_dt_init(const struct dt_device_node *node)
@@ -175,6 +181,10 @@ static inline int gicv3_lpi_drop_host_lpi(struct host_its *its,
 {
     return 0;
 }
+static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
+{
+    return GIC_PRI_IRQ;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +9 -0
index 300f461..4e29ba6 100644
@@ -83,6 +83,12 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+struct lpi_pending_irq
+{
+    struct list_head entry;
+    struct pending_irq pirq;
+};
+
 #define NR_INTERRUPT_PER_RANK   32
 #define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
 
@@ -296,8 +302,11 @@ extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
+extern void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
 extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
+extern struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int irq,
+                                          bool allocate);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#52 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 1 line · Stefano Stabellini

On Thu, 12 Jan 2017, Andre Przywara wrote:

Hi Stefano,

...

Quoted 62 lines
Quoted 20 lines
Quoted 9 lines

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 8 lines
xen/arch/arm/vgic.c +8 -0
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
Quoted 10 lines

This is another one of those cases where a list is too slow for the hot path. The idea of allocating pending_irq struct on demand is good, but storing them in a linked list would kill performance. Probably the best thing we could do is an hashtable and we should preallocate the initial array of elements. I don't know what the size of the initial array should be, but we can start around 50, and change it in the future once we do tests with real workloads. Of course the other key parameter is the hash function, not sure which one is the right one, but ideally we would never have to allocate new pending_irq struct for LPIs because the preallocated set would suffice.

Quoted 2 lines

As I mentioned in the last post, I expect this number to be really low (less than 5).

Are you able to check this assumption in a real scenario? If not you, somebody else?

Quoted 10 lines

Let's face it: If you have multiple interrupts pending
for a significant amount of time you won't make any actual progress in the guest, because it's busy with handling interrupts.
So my picture of LPI handling is:
1) A device triggers an MSI, so the host receives the LPI. Ideally this will be handled by the pCPU where the right VCPU is running atm, so it will exit to EL2. Xen will handle the LPI by assigning one struct pending_irq to it and will inject it into the guest.
2) The VCPU gets to run again and calls the interrupt handler, because the (virtual) LPI is pending.

Quoted 8 lines

3) The (Linux) IRQ handler reads the ICC_IAR register to learn the IRQ number, and will get the virtual LPI number.
=> At this point the LPI is done when it comes to the VGIC. The LR state will be set to 0 (neither pending or active). This is independent of the EOI the handler will execute soon (or later).
4) On the next exit the VGIC code will discover that the IRQ is done (LR.state == 0) and will discard the struct pending_irq (set the LPI number to 0 to make it available to the next LPI).

I am following

Quoted 13 lines

Even if there would be multiple LPIs pending at the same time (because the guest had interrupts disabled, for instance), I believe they can be all handled without exiting. Upon EOIing (priority-dropping, really) the first LPI, the next virtual LPI would fire, calling the interrupt handler again, and so no. Unless the kernel decides to do something that exits (even accessing the hardware normally wouldn't, I believe), we can clear all pending LPIs in one go.

So I have a hard time to imagine how we can really have many LPIs pending and thus struct pending_irqs allocated.
Note that this may differ from SPIs, for instance, because the IRQ life cycle is more complex there (extending till the EOI).

Quoted 5 lines
Quoted 1 line

Does that make some sense? Or am I missing something here?

In my tests with much smaller platforms than the ones existing today, I could easily have 2-3 interrupts pending at the same time without much load and without any SR-IOV NICs or any other fancy PCIE hardware.

The difference to LPIs is that SPIs can be level triggered (eventually requiring a driver to delete the interrupt condition in the device), also require an explicit deactivation to finish off the IRQ state machine. Both these things will lead to an IRQ to stay much longer in the LRs than one would expect for an always edge triggered LPI lacking an active state.
Also the timer IRQ is a PPI and thus a frequent visitor in the LRs.

Quoted 1 line

It would be nice to test on Cavium ThunderX for example.

Yes, I agree that there is quite some guessing involved, so proving this sounds like a worthwhile task.

Quoted 1 line

It's also easy to switch to rbtrees.

On Friday I looked at rbtrees in Xen, which thankfully seem to be the same as in Linux. So I converted the its_devices list over.

But in this case here I don't believe that rbtrees are the best data structure, since we frequently need to look up entries, but also need to find new, empty ones (when an LPI has fired).
And for allocating new LPIs we don't need a certain slot, just any free would do. We probably want to avoid actually malloc-ing pending_irq structures for that.

So a hash table with open addressing sounds like a better fit here:
- With a clever hash function (taken for instance Linux' LPI allocation scheme into account) we get very quick lookup times for already assigned LPIs.
- Assigning an LPI would use the same hash function, probably finding an unused pending_irq, which we then could easily allocate.

I will try to write something along those lines.

Cheers,
Andre.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#53 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 100 lines · André Przywara
Quoted 1 line · Stefano Stabellini

On Thu, 12 Jan 2017, Andre Przywara wrote:

Hi Stefano,

...

Quoted 62 lines
Quoted 20 lines
Quoted 9 lines

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 475

Quoted 8 lines
xen/arch/arm/vgic.c +8 -0
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
Quoted 10 lines

This is another one of those cases where a list is too slow for the hot path. The idea of allocating pending_irq struct on demand is good, but storing them in a linked list would kill performance. Probably the best thing we could do is an hashtable and we should preallocate the initial array of elements. I don't know what the size of the initial array should be, but we can start around 50, and change it in the future once we do tests with real workloads. Of course the other key parameter is the hash function, not sure which one is the right one, but ideally we would never have to allocate new pending_irq struct for LPIs because the preallocated set would suffice.

Quoted 2 lines

As I mentioned in the last post, I expect this number to be really low (less than 5).

Are you able to check this assumption in a real scenario? If not you, somebody else?

Quoted 10 lines

Let's face it: If you have multiple interrupts pending
for a significant amount of time you won't make any actual progress in the guest, because it's busy with handling interrupts.
So my picture of LPI handling is:
1) A device triggers an MSI, so the host receives the LPI. Ideally this will be handled by the pCPU where the right VCPU is running atm, so it will exit to EL2. Xen will handle the LPI by assigning one struct pending_irq to it and will inject it into the guest.
2) The VCPU gets to run again and calls the interrupt handler, because the (virtual) LPI is pending.

Quoted 8 lines

3) The (Linux) IRQ handler reads the ICC_IAR register to learn the IRQ number, and will get the virtual LPI number.
=> At this point the LPI is done when it comes to the VGIC. The LR state will be set to 0 (neither pending or active). This is independent of the EOI the handler will execute soon (or later).
4) On the next exit the VGIC code will discover that the IRQ is done (LR.state == 0) and will discard the struct pending_irq (set the LPI number to 0 to make it available to the next LPI).

I am following

Quoted 13 lines

Even if there would be multiple LPIs pending at the same time (because the guest had interrupts disabled, for instance), I believe they can be all handled without exiting. Upon EOIing (priority-dropping, really) the first LPI, the next virtual LPI would fire, calling the interrupt handler again, and so no. Unless the kernel decides to do something that exits (even accessing the hardware normally wouldn't, I believe), we can clear all pending LPIs in one go.

So I have a hard time to imagine how we can really have many LPIs pending and thus struct pending_irqs allocated.
Note that this may differ from SPIs, for instance, because the IRQ life cycle is more complex there (extending till the EOI).

Quoted 5 lines
Quoted 1 line

Does that make some sense? Or am I missing something here?

In my tests with much smaller platforms than the ones existing today, I could easily have 2-3 interrupts pending at the same time without much load and without any SR-IOV NICs or any other fancy PCIE hardware.

The difference to LPIs is that SPIs can be level triggered (eventually requiring a driver to delete the interrupt condition in the device), also require an explicit deactivation to finish off the IRQ state machine. Both these things will lead to an IRQ to stay much longer in the LRs than one would expect for an always edge triggered LPI lacking an active state.
Also the timer IRQ is a PPI and thus a frequent visitor in the LRs.

Quoted 1 line

It would be nice to test on Cavium ThunderX for example.

Yes, I agree that there is quite some guessing involved, so proving this sounds like a worthwhile task.

Quoted 1 line

It's also easy to switch to rbtrees.

On Friday I looked at rbtrees in Xen, which thankfully seem to be the same as in Linux. So I converted the its_devices list over.

But in this case here I don't believe that rbtrees are the best data structure, since we frequently need to look up entries, but also need to find new, empty ones (when an LPI has fired).
And for allocating new LPIs we don't need a certain slot, just any free would do. We probably want to avoid actually malloc-ing pending_irq structures for that.

Quoted 8 lines

So a hash table with open addressing sounds like a better fit here:
- With a clever hash function (taken for instance Linux' LPI allocation scheme into account) we get very quick lookup times for already assigned LPIs.
- Assigning an LPI would use the same hash function, probably finding an unused pending_irq, which we then could easily allocate.

I will try to write something along those lines.

Thanks Andre, indeed it sounds like a better option.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#54 lore raw

Re: [RFC PATCH 08/24] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara

Hi Andre,

Quoted 18 lines · Andre Przywara
xen/arch/arm/vgic.c +18 -0
+/*
+ * Holding struct pending_irq's for each possible virtual LPI in each domain
+ * requires too much Xen memory, also a malicious guest could potentially
+ * spam Xen with LPI map requests. We cannot cover those with (guest allocated)
+ * ITS memory, so we use a dynamic scheme of allocating struct pending_irq's
+ * on demand.
+ */
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    /* TODO: locking! */
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+            return &lpi_irq->pirq;
+

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 482

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
+
+    if ( !allocate )
+        return NULL;
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);

xzalloc can return NULL if we fail to allocate memory.

xen/arch/arm/vgic.c int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int · line 493

Quoted 11 lines
xen/arch/arm/vgic.c +11 -0
+        vgic_init_pending_irq(&empty->pirq, lpi);
+        list_add_tail(&empty->entry, &v->arch.vgic.pending_lpi_list);
+    } else
+    {
+        empty->pirq.status = 0;
+        empty->pirq.irq = lpi;
+    }
+
+    return &empty->pirq;
+}
+

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#55 lore raw

RFC PATCH 9/24 ARM: GICv3: forward pending LPIs to guests

In reply to Andre Przywara

Upon receiving an LPI, we need to find the right VCPU and virtual IRQ number to get this IRQ injected.
Iterate our two-level LPI table to find this information quickly when the host takes an LPI. Call the existing injection function to let the GIC emulation deal with this interrupt.

---
 xen/arch/arm/gic-its.c    | 32 ++++++++++++++++++++++++++++++++
 xen/arch/arm/gic.c        |  6 ++++--
 xen/include/asm-arm/irq.h |  8 ++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)
xen/arch/arm/gic-its.c +32 -0
index bf1f5b5..b7aa918 100644
@@ -77,6 +77,38 @@ static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d)
     return hlpi;
 }
 
+/* Handle incoming LPIs, which are a bit special, because they are potentially
+ * numerous and also only get injected into guests. Treat them specially here,
+ * by just looking up their target vCPU and virtual LPI number and hand it
+ * over to the injection function.
+ */
+void do_LPI(unsigned int lpi)
+{
+    struct domain *d;
+    union host_lpi *hlpip, hlpi;
+
+    WRITE_SYSREG32(lpi, ICC_EOIR1_EL1);
+    WRITE_SYSREG32(lpi, ICC_DIR_EL1);
+
+    hlpip = gic_find_host_lpi(lpi, NULL);
+    if ( !hlpip )
+        return;
+
+    hlpi.data = hlpip->data;
+
+    if ( !hlpi.virt_lpi )
+        return;
+
+    d = get_domain_by_id(hlpi.dom_id);
+    if ( !d )
+        return;
+
+    if ( hlpi.vcpu_id >= d->max_vcpus )
+        return;
+
+    vgic_vcpu_inject_irq(d->vcpu[hlpi.vcpu_id], hlpi.virt_lpi);
+}
+
 #define ITS_COMMAND_SIZE        32
 
 static int its_send_command(struct host_its *hw_its, void *its_cmd)
xen/arch/arm/gic.c +4 -2
index ebe4035..2fad2f1 100644
@@ -697,8 +697,10 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
             local_irq_enable();
             do_IRQ(regs, irq, is_fiq);
             local_irq_disable();
-        }
-        else if (unlikely(irq < 16))
+        } else if ( irq >= 8192 )
+        {
+            do_LPI(irq);
+        } else if ( unlikely(irq < 16) )
         {
             do_sgi(regs, irq);
         }
xen/include/asm-arm/irq.h +8 -0
index 8f7a167..ee47de8 100644
@@ -34,6 +34,14 @@ struct irq_desc *__irq_to_desc(int irq);
 
 void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq);
 
+#ifdef CONFIG_HAS_ITS
+void do_LPI(unsigned int irq);
+#else
+static inline void do_LPI(unsigned int irq)
+{
+}
+#endif
+
 #define domain_pirq_to_irq(d, pirq) (pirq)
 
 bool_t is_assignable_irq(unsigned int irq);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#56 lore raw

Re: [RFC PATCH 09/24] ARM: GICv3: forward pending LPIs to guests

Quoted 23 lines · Andre Przywara

Upon receiving an LPI, we need to find the right VCPU and virtual IRQ number to get this IRQ injected.
Iterate our two-level LPI table to find this information quickly when the host takes an LPI. Call the existing injection function to let the GIC emulation deal with this interrupt.

---
 xen/arch/arm/gic-its.c    | 32 ++++++++++++++++++++++++++++++++
 xen/arch/arm/gic.c        |  6 ++++--
 xen/include/asm-arm/irq.h |  8 ++++++++
 3 files changed, 44 insertions(+), 2 deletions(-)
xen/arch/arm/gic-its.c +2 -0
index bf1f5b5..b7aa918 100644
@@ -77,6 +77,38 @@ static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d)
     return hlpi;
 }
 
+/* Handle incoming LPIs, which are a bit special, because they are potentially
+ * numerous and also only get injected into guests. Treat them specially here,

xen/arch/arm/gic-its.c static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d) · line 91

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+ * by just looking up their target vCPU and virtual LPI number and hand it
+ * over to the injection function.
+ */
+void do_LPI(unsigned int lpi)
+{
+    struct domain *d;
+    union host_lpi *hlpip, hlpi;
+
+    WRITE_SYSREG32(lpi, ICC_EOIR1_EL1);
+    WRITE_SYSREG32(lpi, ICC_DIR_EL1);

Given that LPIs are always guest IRQs, and given that we support EOImode = 1, shouldn't we just do ICC_EOIR1_EL1 here (no ICC_DIR_EL1)?

xen/arch/arm/gic-its.c static union host_lpi *gic_find_host_lpi(uint32_t lpi, struct domain *d) · line 97

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+    hlpip = gic_find_host_lpi(lpi, NULL);
+    if ( !hlpip )
+        return;
+
+    hlpi.data = hlpip->data;

Why can't we just reference hlpip directly? Why do we need hlpi?

Quoted 55 lines
xen/arch/arm/gic-its.c +13 -0
+    if ( !hlpi.virt_lpi )
+        return;
+
+    d = get_domain_by_id(hlpi.dom_id);
+    if ( !d )
+        return;
+
+    if ( hlpi.vcpu_id >= d->max_vcpus )
+        return;
+
+    vgic_vcpu_inject_irq(d->vcpu[hlpi.vcpu_id], hlpi.virt_lpi);
+}
+
 #define ITS_COMMAND_SIZE        32
 
 static int its_send_command(struct host_its *hw_its, void *its_cmd)
xen/arch/arm/gic.c +4 -2
index ebe4035..2fad2f1 100644
@@ -697,8 +697,10 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
             local_irq_enable();
             do_IRQ(regs, irq, is_fiq);
             local_irq_disable();
-        }
-        else if (unlikely(irq < 16))
+        } else if ( irq >= 8192 )
+        {
+            do_LPI(irq);
+        } else if ( unlikely(irq < 16) )
         {
             do_sgi(regs, irq);
         }
xen/include/asm-arm/irq.h +8 -0
index 8f7a167..ee47de8 100644
@@ -34,6 +34,14 @@ struct irq_desc *__irq_to_desc(int irq);
 
 void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq);
 
+#ifdef CONFIG_HAS_ITS
+void do_LPI(unsigned int irq);
+#else
+static inline void do_LPI(unsigned int irq)
+{
+}
+#endif
+
 #define domain_pirq_to_irq(d, pirq) (pirq)
 
 bool_t is_assignable_irq(unsigned int irq);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#57 lore raw

RFC PATCH 10/24 ARM: GICv3: enable ITS and LPIs on the host

In reply to Andre Przywara

Now that the host part of the ITS code is in place, we can enable the ITS and also LPIs on each redistributor to get the show rolling. At this point there would be no LPIs mapped, as guests don't know about the ITS yet.

---
 xen/arch/arm/gic-its.c |  4 ++++
 xen/arch/arm/gic-v3.c  | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
xen/arch/arm/gic-its.c +4 -0
index b7aa918..6bac422 100644
@@ -449,6 +449,10 @@ int gicv3_its_init(struct host_its *hw_its)
     its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
     its_send_cmd_sync(hw_its, smp_processor_id());
 
+    /* Now enable interrupt translation on that ITS. */
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg | GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
     return 0;
 }
 
xen/arch/arm/gic-v3.c +19 -0
index b9387a3..57009c6 100644
@@ -642,6 +642,21 @@ static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
     gicv3_its_setup_collection(smp_processor_id());
 }
 
+/* Enable LPIs on this redistributor (only useful when the host has an ITS. */
+static bool gicv3_enable_lpis(void)
+{
+    uint32_t val;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_TYPER);
+    if ( !(val & GICR_TYPER_PLPIS) )
+        return false;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_CTLR);
+    writel_relaxed(val | GICR_CTLR_ENABLE_LPIS, GICD_RDIST_BASE + GICR_CTLR);
+
+    return true;
+}
+
 static int __init gicv3_populate_rdist(void)
 {
     int i;
@@ -741,6 +756,10 @@ static int gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+    /* If the host has any ITSes, enable LPIs now. */
+    if ( !list_empty(&host_its_list) )
+        gicv3_enable_lpis();
+
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
                 GIC_PRI_IPI);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v1 10/24
#58 lore raw

Re: [RFC PATCH 10/24] ARM: GICv3: enable ITS and LPIs on the host

Quoted 6 lines · Andre Przywara

Now that the host part of the ITS code is in place, we can enable the ITS and also LPIs on each redistributor to get the show rolling. At this point there would be no LPIs mapped, as guests don't know about the ITS yet.

Quoted 59 lines
 xen/arch/arm/gic-its.c |  4 ++++
 xen/arch/arm/gic-v3.c  | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
xen/arch/arm/gic-its.c +4 -0
index b7aa918..6bac422 100644
@@ -449,6 +449,10 @@ int gicv3_its_init(struct host_its *hw_its)
     its_send_cmd_mapc(hw_its, smp_processor_id(), smp_processor_id());
     its_send_cmd_sync(hw_its, smp_processor_id());
 
+    /* Now enable interrupt translation on that ITS. */
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    writel_relaxed(reg | GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
     return 0;
 }
 
xen/arch/arm/gic-v3.c +19 -0
index b9387a3..57009c6 100644
@@ -642,6 +642,21 @@ static void gicv3_rdist_init_lpis(void __iomem * rdist_base)
     gicv3_its_setup_collection(smp_processor_id());
 }
 
+/* Enable LPIs on this redistributor (only useful when the host has an ITS. */
+static bool gicv3_enable_lpis(void)
+{
+    uint32_t val;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_TYPER);
+    if ( !(val & GICR_TYPER_PLPIS) )
+        return false;
+
+    val = readl_relaxed(GICD_RDIST_BASE + GICR_CTLR);
+    writel_relaxed(val | GICR_CTLR_ENABLE_LPIS, GICD_RDIST_BASE + GICR_CTLR);
+
+    return true;
+}
+
 static int __init gicv3_populate_rdist(void)
 {
     int i;
@@ -741,6 +756,10 @@ static int gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+    /* If the host has any ITSes, enable LPIs now. */
+    if ( !list_empty(&host_its_list) )
+        gicv3_enable_lpis();
+
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
                 GIC_PRI_IPI);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#59 lore raw

RFC PATCH 11/24 ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +175 -14
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #include <xen/bitops.h>
 #include <xen/config.h>
+#include <xen/domain_page.h>
 #include <xen/lib.h>
 #include <xen/init.h>
 #include <xen/softirq.h>
 #include <xen/irq.h>
 #include <xen/sched.h>
 #include <xen/sizes.h>
+#include <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);
+    }
+
+    ptr = vmap(pages, nr_pages);
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;
+
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;
+
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+	reg = v->arch.vgic.rdist_pendbase;
+	vgic_reg64_update(&reg, r, info);
+	reg = sanitize_pendbaser(reg);
+	v->arch.vgic.rdist_pendbase = reg;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);
+	v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);
+	return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +4 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +6 -1
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +9 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +3 -0
index 4e29ba6..2b216cc 100644
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#60 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Quoted 144 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +83 -9
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #include <xen/bitops.h>
 #include <xen/config.h>
+#include <xen/domain_page.h>
 #include <xen/lib.h>
 #include <xen/init.h>
 #include <xen/softirq.h>
 #include <xen/irq.h>
 #include <xen/sched.h>
 #include <xen/sizes.h>
+#include <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

I think this file is not right place to put this generic function

Quoted 13 lines
xen/arch/arm/vgic-v3.c +13 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;

xen/arch/arm/vgic-v3.c read_unknown: · line 439

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);

check return value of this function

xen/arch/arm/vgic-v3.c read_unknown: · line 451

Quoted 12 lines
xen/arch/arm/vgic-v3.c +12 -0
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);
+    }
+
+    ptr = vmap(pages, nr_pages);
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)

same here. Can be put in generic file p2m.c?

xen/arch/arm/vgic-v3.c read_unknown: · line 460

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);

can use _pa()

Quoted 27 lines
xen/arch/arm/vgic-v3.c +12 -3
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;
+

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 527

Quoted 10 lines
xen/arch/arm/vgic-v3.c +9 -0
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;
+
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;

should be validated against HOST_LPIS?

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 536

Quoted 11 lines
xen/arch/arm/vgic-v3.c +8 -2
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

check on VGIC_V3_LPIS_ENABLED is required

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 542

Quoted 6 lines
xen/arch/arm/vgic-v3.c +6 -0
+       reg = v->arch.vgic.rdist_pendbase;
+       vgic_reg64_update(&reg, r, info);
+       reg = sanitize_pendbaser(reg);
+       v->arch.vgic.rdist_pendbase = reg;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);

why only 16 pages are unmapped?

Quoted 92 lines
xen/arch/arm/vgic-v3.c +3 -0
+       v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);
+       return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +4 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +6 -1
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +9 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +3 -1
index 4e29ba6..2b216cc 100644
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*
--

2.9.0

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#61 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Hi,

Quoted 148 lines · Vijay Kilari
Quoted 147 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +86 -9
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #include <xen/bitops.h>
 #include <xen/config.h>
+#include <xen/domain_page.h>
 #include <xen/lib.h>
 #include <xen/init.h>
 #include <xen/softirq.h>
 #include <xen/irq.h>
 #include <xen/sched.h>
 #include <xen/sizes.h>
+#include <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
Quoted 8 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

I think this file is not right place to put this generic function

Yeah, possibly.

Quoted 17 lines
Quoted 17 lines
xen/arch/arm/vgic-v3.c +17 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
Quoted 8 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 439

Quoted 6 lines
xen/arch/arm/vgic-v3.c +6 -0
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);

check return value of this function

Yes.

Quoted 13 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 451

Quoted 12 lines
xen/arch/arm/vgic-v3.c +12 -0
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);
+    }
+
+    ptr = vmap(pages, nr_pages);
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)

same here. Can be put in generic file p2m.c?

Quoted 10 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 460

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);

can use _pa()

Do you mean __pa()? Which is defined to be exactly virt_to_maddr()? I prefer the more verbose version, which is more readable, IMHO.

Quoted 36 lines
Quoted 36 lines
xen/arch/arm/vgic-v3.c +20 -3
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;
+
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;
+
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
Quoted 2 lines

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 527

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;

should be validated against HOST_LPIS?

I don't think so. The actual LPI numbers are totally independent between host and Dom0.
So why and how should this be matched?

Quoted 12 lines

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 536

Quoted 11 lines
xen/arch/arm/vgic-v3.c +8 -2
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
Quoted 1 line

check on VGIC_V3_LPIS_ENABLED is required

Right, forgot this.

Quoted 7 lines

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 542

Quoted 6 lines
xen/arch/arm/vgic-v3.c +6 -0
+       reg = v->arch.vgic.rdist_pendbase;
+       vgic_reg64_update(&reg, r, info);
+       reg = sanitize_pendbaser(reg);
+       v->arch.vgic.rdist_pendbase = reg;
+
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);

why only 16 pages are unmapped?

Well, it matches the allocation below, but I agree that this should match the advertised number of LPIs in GICD_TYPER.

Cheers,
Andre.

Quoted 92 lines
Quoted 92 lines
xen/arch/arm/vgic-v3.c +3 -0
+       v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);
+       return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +4 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +6 -1
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +9 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +3 -1
index 4e29ba6..2b216cc 100644
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*
--

2.9.0

Quoted 5 lines
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#62 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Hi,

Quoted 8 lines · Andre Przywara
Quoted 4 lines · Vijay Kilari

xen/arch/arm/vgic-v3.c read_unknown: · line 460

Quoted 2 lines · Andre Przywara
xen/arch/arm/vgic-v3.c +2 -0
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);

can use _pa()

Do you mean __pa()? Which is defined to be exactly virt_to_maddr()? I prefer the more verbose version, which is more readable, IMHO.

FWIW, __pa tends to be more used than virt_to_maddr within the source base.

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#63 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara
Quoted 144 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +83 -9
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #include <xen/bitops.h>
 #include <xen/config.h>
+#include <xen/domain_page.h>
 #include <xen/lib.h>
 #include <xen/init.h>
 #include <xen/softirq.h>
 #include <xen/irq.h>
 #include <xen/sched.h>
 #include <xen/sizes.h>
+#include <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    reg &= ~GENMASK(51, 48);
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

map_guest_pages and unmap_guest_pages are not ARM specific and should live somewhere in xen/common, maybe xen/common/vmap.c.

xen/arch/arm/vgic-v3.c read_unknown: · line 423

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */

No need: the caller is already doing that, right?

xen/arch/arm/vgic-v3.c read_unknown: · line 435

Quoted 11 lines
xen/arch/arm/vgic-v3.c +11 -0
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }

How often do you think only 1 page will be used? If the answer is not often, I would get rid of the onepage optimization.

xen/arch/arm/vgic-v3.c read_unknown: · line 440

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);

Don't you need to call page_to_mfn (or gfn_to_mfn) to get the mfn?

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

It is possible for vmap to fail and return NULL. Maybe because the guest intentionally tried to break the hypervisor by passing an array of pages that ends in an MMIO region. We need to check vmap errors and handle them.

Quoted 34 lines
xen/arch/arm/vgic-v3.c +25 -0
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 516

Quoted 8 lines
xen/arch/arm/vgic-v3.c +2 -3
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+    case VREG64(GICR_PROPBASER): {
+        int nr_pages;

unsigned int

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 517

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Why not use vgic_reg64_check_access?

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 519

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;

Why? If there is a good reason for this, it is probably worth writing it in an in-code comment.

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 528

Quoted 7 lines
xen/arch/arm/vgic-v3.c +6 -0
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);

Do we need to set an upper limit on nr_pages? We don't really want to allow (2^0x1f)/4096 pages, right?

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 532

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+        unmap_guest_pages(v->domain->arch.vgic.proptable, nr_pages);
+        v->domain->arch.vgic.proptable = map_guest_pages(v->domain,
+                                                         reg & GENMASK(47, 12),
+                                                         nr_pages);

I am pretty sure I am reading the right spec now and it should be GENMASK(51, 12). Also, don't we need to sanitize the table too before using it?

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 536

Quoted 6 lines
xen/arch/arm/vgic-v3.c +3 -2
+        return 1;
+    }
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Why not use vgic_reg64_check_access?

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 544

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+	reg = v->arch.vgic.rdist_pendbase;
+	vgic_reg64_update(&reg, r, info);
+	reg = sanitize_pendbaser(reg);
+	v->arch.vgic.rdist_pendbase = reg;
+        unmap_guest_pages(v->arch.vgic.pendtable, 16);
+	v->arch.vgic.pendtable = map_guest_pages(v->domain,
+                                                 reg & GENMASK(47, 12), 16);

Indentation.
We need to make sure the vmap mapping is correct. Also we need to sanitize this table too before using it.

Quoted 9 lines
xen/arch/arm/vgic-v3.c +1 -0
+	return 1;
 
     case 0x0080:
         goto write_reserved;
xen/arch/arm/vgic.c +0 -0
index b961551..4d9304f 100644

xen/arch/arm/vgic.c struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi, · line 493

Quoted 6 lines
xen/arch/arm/vgic.c +3 -0
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);

Where is the GIC_IRQ_GUEST_ENABLED unset?

xen/include/asm-arm/domain.h struct arch_domain · line 113

Quoted 13 lines
xen/arch/arm/vgic.c +0 -0
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +2 -0
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;

Do we need to keep both rdist_propbase and proptable? It is easy to go from proptable to rdist_propbase and I guess it is not an operation that is done often? If so, we could save some memory and remove it.

xen/include/asm-arm/domain.h struct arch_vcpu · line 255

Quoted 12 lines
xen/include/asm-arm/domain.h +4 -1
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;

Same here.

xen/include/asm-arm/gic-its.h int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 142

Quoted 13 lines
xen/include/asm-arm/domain.h +0 -0
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +1 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;

Please #define 0xfc. Do we need to check for lpi overflows? As in lpi numbers larger than proptable size?

Quoted 23 lines
xen/include/asm-arm/gic-its.h +8 -0
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +0 -0
index 4e29ba6..2b216cc 100644

xen/include/asm-arm/vgic.h VGIC_REG_HELPERS(32, 0x3); · line 293

Quoted 9 lines
xen/include/asm-arm/vgic.h +3 -0
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#64 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Hi,

Quoted 13 lines · Stefano Stabellini
Quoted 12 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---

xen/arch/arm/vgic-v3.c +0 -0
index e9b6490..8fe8386 100644

....

Quoted 10 lines

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 528

Quoted 7 lines
xen/arch/arm/vgic-v3.c +6 -0
+        reg = v->domain->arch.vgic.rdist_propbase;
+        vgic_reg64_update(&reg, r, info);
+        reg = sanitize_propbaser(reg);
+        v->domain->arch.vgic.rdist_propbase = reg;
 
+        nr_pages = BIT((v->domain->arch.vgic.rdist_propbase & 0x1f) + 1) - 8192;
+        nr_pages = DIV_ROUND_UP(nr_pages, PAGE_SIZE);

Do we need to set an upper limit on nr_pages? We don't really want to allow (2^0x1f)/4096 pages, right?

Why not? This is the virtual property table, and the *guest* provides the memory. We just comply here and map it. I don't see any issue.

[ .... ]

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/vgic.c +0 -0
index b961551..4d9304f 100644
@@ -488,6 +488,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
Quoted 5 lines

xen/arch/arm/vgic.c struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi, · line 493

Quoted 3 lines
xen/arch/arm/vgic.c +3 -0
+    /* Update the enabled status */
+    if ( gicv3_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);

Where is the GIC_IRQ_GUEST_ENABLED unset?

In the patch where the INV command is emulated. This is how enabling/disabling LPI works: Software (the guest here) sets the bit in the property table and issues an ITS command to notify the ITS (emulation) about it.

Quoted 17 lines

xen/include/asm-arm/domain.h struct arch_domain · line 113

Quoted 13 lines
xen/arch/arm/vgic.c +0 -0
     return &empty->pirq;
 }
 
xen/include/asm-arm/domain.h +2 -0
index ae8a9de..0cd3500 100644
@@ -109,6 +109,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        uint64_t rdist_propbase;
+        uint8_t *proptable;

Do we need to keep both rdist_propbase and proptable? It is easy to go from proptable to rdist_propbase and I guess it is not an operation that is done often? If so, we could save some memory and remove it.

The code has changed meanwhile, so this does not apply direclty anymore, but just to make sure:
We need rdist_propbase separately, because a guest can happily set and change it as often as it wants before enabling LPIs. We shouldn't (and we don't) allocate memory now (and so set proptable) until the LPIs get enabled.

Quoted 5 lines
Quoted 5 lines
xen/include/asm-arm/domain.h +0 -0
 #endif
     } vgic;
 
@@ -247,7 +249,10 @@ struct arch_vcpu
 
Quoted 9 lines

xen/include/asm-arm/domain.h struct arch_vcpu · line 255

Quoted 7 lines
xen/include/asm-arm/domain.h +4 -1
         /* GICv3: redistributor base and flags for this vCPU */
         paddr_t rdist_base;
-#define VGIC_V3_RDIST_LAST  (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_RDIST_LAST      (1 << 0)        /* last vCPU of the rdist */
+#define VGIC_V3_LPIS_ENABLED    (1 << 1)
+        uint64_t rdist_pendbase;
+        unsigned long *pendtable;

Same here.

And the same rationale applies here.

Fixed / addresses the rest.

Cheers,
Andre.

Quoted 42 lines

xen/include/asm-arm/gic-its.h int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 142

Quoted 13 lines
xen/include/asm-arm/domain.h +0 -0
         uint8_t flags;
         struct list_head pending_lpi_list;
     } vgic;
xen/include/asm-arm/gic-its.h +1 -1
index 1f881c0..3b2e5c0 100644
@@ -139,7 +139,11 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
 
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
-    return GIC_PRI_IRQ;
+    return d->arch.vgic.proptable[lpi - 8192] & 0xfc;

Please #define 0xfc. Do we need to check for lpi overflows? As in lpi numbers larger than proptable size?

Quoted 24 lines
xen/include/asm-arm/gic-its.h +8 -0
+}
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return d->arch.vgic.proptable[lpi - 8192] & LPI_PROP_ENABLED;
 }
 
 #else
@@ -185,6 +189,10 @@ static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return GIC_PRI_IRQ;
 }
+static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
+{
+    return false;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
xen/include/asm-arm/vgic.h +0 -0
index 4e29ba6..2b216cc 100644
@@ -285,6 +285,9 @@ VGIC_REG_HELPERS(32, 0x3);
 
Quoted 8 lines

xen/include/asm-arm/vgic.h VGIC_REG_HELPERS(32, 0x3); · line 293

Quoted 8 lines
xen/include/asm-arm/vgic.h +3 -0
 #undef VGIC_REG_HELPERS
 
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages);
+void unmap_guest_pages(void *va, int nr_pages);
+
 enum gic_sgi_mode;
 
 /*

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#65 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara

Hi Andre,

Quoted 39 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189 ++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +2 -1
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #include <xen/bitops.h>
 #include <xen/config.h>
+#include <xen/domain_page.h>
 #include <xen/lib.h>
 #include <xen/init.h>
 #include <xen/softirq.h>
 #include <xen/irq.h>
 #include <xen/sched.h>
 #include <xen/sizes.h>
+#include <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info, · line 239

Quoted 10 lines
xen/arch/arm/vgic-v3.c +5 -3
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase, info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);

The field PTZ read as 0.

Quoted 17 lines
xen/arch/arm/vgic-v3.c +1 -5
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;

xen/arch/arm/vgic-v3.c read_unknown: · line 338

Quoted 10 lines
xen/arch/arm/vgic-v3.c +7 -0
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;

Newline here please.

xen/arch/arm/vgic-v3.c read_unknown: · line 351

Quoted 13 lines
xen/arch/arm/vgic-v3.c +13 -0
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
+    default:
+        return field;
+    }
+}

I am not sure to understand why we need to sanitise the value here. From my understanding of the spec (see 8.11.18 in IHI 0069C) we should support any shareability/cacheability, correct?

Quoted 29 lines
xen/arch/arm/vgic-v3.c +29 -0
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,

xen/arch/arm/vgic-v3.c read_unknown: · line 390

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);

Why do you mask the bits 51:48. There is no restriction in Xen about the size of the IPA (though 52 bits support is part of ARMv8.2), so we should avoid to open-code mask everywhere in the code. Otherwise it will be more painful to extend the number of bits supported.

FWIW, all the p2m code is checking whether the IPA is supported.

Quoted 7 lines
xen/arch/arm/vgic-v3.c +7 -0
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,

xen/arch/arm/vgic-v3.c read_unknown: · line 407

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);

Ditto.

xen/arch/arm/vgic-v3.c read_unknown: · line 416

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

Please pass a gfn_t rather than paddr_t.

xen/arch/arm/vgic-v3.c read_unknown: · line 419

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;

s/pages/mfns/

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use get-put-put? */
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;

xen/arch/arm/vgic-v3.c read_unknown: · line 439

Quoted 10 lines
xen/arch/arm/vgic-v3.c +10 -0
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);

get_page_from_gfn can fail if you try to get a page on memory that is not baked by a RAM region. Also get_page_from_gfn will work on foreign mapping, we don't want the guest using foreing memory (e.g memory belonging to another domain) for the ITS internal memory.

Also, please try to pay attention for error path whilst you write code. It is a pain to handle them after the code has been written. I will try to point them when I spot it.

xen/arch/arm/vgic-v3.c read_unknown: · line 440

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0
+        pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);

You cannot assume a 1:1 mapping between the IPA and the PA. Please use the struct page_info returned by get_page_from_gfn

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem to use
it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

Quoted 37 lines
xen/arch/arm/vgic-v3.c +27 -1
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
+    uint64_t reg;
 
     switch ( gicr_reg )
     {

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info, · line 379

Quoted 5 lines
xen/arch/arm/vgic-v3.c +0 -2
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
[… more lines in the raw message]

Coding style, the { should be on a line.

xen/arch/arm/vgic-v3.c

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

unsigned int

xen/arch/arm/vgic-v3.c

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Newline here for clarity. Also please use vgic_reg64_check_access rather than open-coding it.

xen/arch/arm/vgic-v3.c

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From my understanding VGIC_V3_LPIS_ENABLED is set when the guest enable LPIs on this re-distributor. However, this check is not safe as GICR_CTLR.Enable_LPIs may be set concurrently (the re-distributors are accessible from any vCPU).

Also, when ITS is not available we should avoid to handle the register (i.e treating as write ignore). My rational here is we should limit the amount of emulation exposed to the guest whenever it is possible.

xen/arch/arm/vgic-v3.c

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

I think we should at least print warning as writing to GICR_PROPBASER when GICR_CTLR.Enable_LPIs is set is unpredictable. IHMO, I would even crash the guest.

The code below likely needs locking as the property table is common to all re-distributor, hence could be modified concurrently. Also, I would like to see a comment on top of emulation of GICR_TYPER to mention that all re-distributor shares the same common property table (GICR_TYPER.CommonLPIAff = 0).

xen/arch/arm/vgic-v3.c

Quoted 7 lines
xen/arch/arm/vgic-v3.c +6 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

The spec (see 8.11.19): "If the value of this field is larger than the value of GICD_TYPER.IDbits, the GICD_TYPER.IDbits value applies). We don't want to map more than necessary uin

xen/arch/arm/vgic-v3.c

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

This looks wrong to me. A guest could specify a size different from the previous write.

123 lines not shown · raw message

Parts of this message are not shown here. View the raw message.

#66 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Quoted 11 lines · Julien Grall

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem
to use it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

I don't like the idea of using the vmap for this either, but the problem with the copy_*_guest approach is that it only maps one page at a time. It is unable to map multiple pages contiguously, which seems to be required here.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#67 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Hi Stefano,

Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Julien Grall

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem
to use it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

Quoted 7 lines
Quoted 2 lines

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

I don't like the idea of using the vmap for this either, but the problem with the copy_*_guest approach is that it only maps one page at a time. It is unable to map multiple pages contiguously, which seems to be required here.

We will get into trouble very quickly with the vmap solution. The memory provisioned by the ITS can be quite big. Each GITS_BASER can hold up to 16MB (if I computed correctly) of memory.

The question is why do we need to map the page contiguously? Can we do in a different way?

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#68 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Quoted 21 lines · Julien Grall

Hi Stefano,

Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Julien Grall

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem
to use it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

Quoted 7 lines
Quoted 2 lines

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

I don't like the idea of using the vmap for this either, but the problem with the copy_*_guest approach is that it only maps one page at a time. It is unable to map multiple pages contiguously, which seems to be required here.

Quoted 6 lines

We will get into trouble very quickly with the vmap solution. The memory provisioned by the ITS can be quite big. Each GITS_BASER can hold up to 16MB (if I computed correctly) of memory.

The question is why do we need to map the page contiguously? Can we do in a different way?

I agree with Julien: if we can get away with mapping one page at a time without performance penalties, we should do it. Keep in mind that on ARM64 Xen doesn't actually need to map anything because the whole physical memory is already mapped.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#69 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Julien Grall

Hi Julien,

(forgot to hit the Send button yesterday ...)

....

Quoted 97 lines · Julien Grall

Hi Andre,

Quoted 42 lines · Andre Przywara

Allow a guest to provide the address and size for the memory regions it has reserved for the GICv3 pending and property tables. We sanitise the various fields of the respective redistributor registers and map those pages into Xen's address space to have easy access.

---
 xen/arch/arm/vgic-v3.c        | 189
++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c           |   4 +
 xen/include/asm-arm/domain.h  |   7 +-
 xen/include/asm-arm/gic-its.h |  10 ++-
 xen/include/asm-arm/vgic.h    |   3 +
 5 files changed, 197 insertions(+), 16 deletions(-)
xen/arch/arm/vgic-v3.c +2 -2
index e9b6490..8fe8386 100644
@@ -20,12 +20,14 @@
 
 #include <xen/bitops.h>
 #include <xen/config.h>
+#include <xen/domain_page.h>
 #include <xen/lib.h>
 #include <xen/init.h>
 #include <xen/softirq.h>
 #include <xen/irq.h>
 #include <xen/sched.h>
 #include <xen/sizes.h>
+#include <xen/vmap.h>
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
@@ -228,12 +230,14 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct
 vcpu *v, mmio_info_t *info,
         goto read_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_read(struct · line 241

Quoted 10 lines
xen/arch/arm/vgic-v3.c +5 -2
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->domain->arch.vgic.rdist_propbase,
 info);
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI's not implemented */
-        goto read_as_zero_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+        *r = vgic_reg64_extract(v->arch.vgic.rdist_pendbase, info);

The field PTZ read as 0.

Quoted 17 lines
xen/arch/arm/vgic-v3.c +1 -0
+        return 1;
 
     case 0x0080:
         goto read_reserved;
xen/arch/arm/vgic-v3.c +0 -5
@@ -301,11 +305,6 @@ bad_width:
     domain_crash_synchronous();
     return 0;
 
-read_as_zero_64:
-    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-    *r = 0;
-    return 1;
-
 read_as_zero_32:
     if ( dabt.size != DABT_WORD ) goto bad_width;
     *r = 0;

xen/arch/arm/vgic-v3.c read_unknown: · line 338

Quoted 10 lines
xen/arch/arm/vgic-v3.c +7 -0
@@ -330,11 +329,149 @@ read_unknown:
     return 1;
 }
 
+static uint64_t vgic_sanitise_field(uint64_t reg, uint64_t field_mask,
+                                    int field_shift,
+                                    uint64_t (*sanitise_fn)(uint64_t))
+{
+    uint64_t field = (reg & field_mask) >> field_shift;
+
+    field = sanitise_fn(field) << field_shift;

Newline here please.

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
Quoted 8 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 351

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+    default:
+        return field;
+    }
+}

I am not sure to understand why we need to sanitise the value here. From my understanding of the spec (see 8.11.18 in IHI 0069C) we should support any shareability/cacheability, correct?

No, actually an ITS is free to support only _one_ of those attributes, up to the point where it is read-only:

"It is IMPLEMENTATION DEFINED whether this field has a fixed value or can be programmed by software. Implementing this field with a fixed value is deprecated."

So we support more than one value, but refuse any really not useful ones. This goes in line with the KVM implementation.

For the rest of the comments regarding the memory tables setup:
I effectively rewrote this in the new series, so I think the majority of the comments don't apply anymore, hopefully the rewrite actually fixed the issues you mentioned. So I refrain from any comments now and look forward to a review of the new approach ;-)

Cheers,
Andre.

Quoted 389 lines
Quoted 31 lines
xen/arch/arm/vgic-v3.c +31 -0
+
+/* Avoid any inner non-cacheable mapping. */
+static uint64_t vgic_sanitise_inner_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_nCnB:
+    case GIC_BASER_CACHE_nC:
+        return GIC_BASER_CACHE_RaWb;
+    default:
+        return field;
+    }
+}
+
+/* Non-cacheable or same-as-inner are OK. */
+static uint64_t vgic_sanitise_outer_cacheability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_CACHE_SameAsInner:
+    case GIC_BASER_CACHE_nC:
+        return field;
+    default:
+        return GIC_BASER_CACHE_nC;
+    }
+}
+
+static uint64_t sanitize_propbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PROPBASER_SHAREABILITY_MASK,
+                              GICR_PROPBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg,

xen/arch/arm/vgic-v3.c read_unknown: · line 390

Quoted 10 lines
xen/arch/arm/vgic-v3.c +8 -0
 GICR_PROPBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg,
 GICR_PROPBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PROPBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);

Why do you mask the bits 51:48. There is no restriction in Xen about the size of the IPA (though 52 bits support is part of ARMv8.2), so we should avoid to open-code mask everywhere in the code. Otherwise it will be more painful to extend the number of bits supported.

FWIW, all the p2m code is checking whether the IPA is supported.

Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+    return reg;
+}
+
+static uint64_t sanitize_pendbaser(uint64_t reg)
+{
+    reg = vgic_sanitise_field(reg, GICR_PENDBASER_SHAREABILITY_MASK,
+                              GICR_PENDBASER_SHAREABILITY_SHIFT,
+                              vgic_sanitise_shareability);
+    reg = vgic_sanitise_field(reg,

xen/arch/arm/vgic-v3.c read_unknown: · line 407

Quoted 10 lines
xen/arch/arm/vgic-v3.c +8 -0
 GICR_PENDBASER_INNER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_INNER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_inner_cacheability);
+    reg = vgic_sanitise_field(reg,
 GICR_PENDBASER_OUTER_CACHEABILITY_MASK,
+                              GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT,
+                              vgic_sanitise_outer_cacheability);
+
+    reg &= ~PENDBASER_RES0_MASK;
+    reg &= ~GENMASK(51, 48);

Ditto.

Quoted 11 lines
xen/arch/arm/vgic-v3.c +5 -0
+    return reg;
+}
+
+/*
+ * Allow mapping some parts of guest memory into Xen's VA space to

have easy
+ * access to it. This is to allow ITS configuration data to be held in
+ * guest memory and avoid using Xen memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)

Please pass a gfn_t rather than paddr_t.

xen/arch/arm/vgic-v3.c read_unknown: · line 419

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+{
+    mfn_t onepage;
+    mfn_t *pages;

s/pages/mfns/

Quoted 21 lines
xen/arch/arm/vgic-v3.c +4 -0
+    int i;
+    void *ptr;
+
+    /* TODO: free previous mapping, change prototype? use

get-put-put? */

xen/arch/arm/vgic-v3.c +16 -0
+
+    guest_addr &= PAGE_MASK;
+
+    if ( nr_pages == 1 )
+    {
+        pages = &onepage;
+    } else
+    {
+        pages = xmalloc_array(mfn_t, nr_pages);
+        if ( !pages )
+            return NULL;
+    }
+
+    for (i = 0; i < nr_pages; i++)
+    {
+        get_page_from_gfn(d, (guest_addr >> PAGE_SHIFT) + i, NULL,
Quoted 1 line

P2M_ALLOC);

get_page_from_gfn can fail if you try to get a page on memory that is not baked by a RAM region. Also get_page_from_gfn will work on foreign mapping, we don't want the guest using foreing memory (e.g memory belonging to another domain) for the ITS internal memory.

Also, please try to pay attention for error path whilst you write code. It is a pain to handle them after the code has been written. I will try to point them when I spot it.

Quoted 1 line

+ pages[i] = _mfn((guest_addr + i * PAGE_SIZE) >> PAGE_SHIFT);

You cannot assume a 1:1 mapping between the IPA and the PA. Please use the struct page_info returned by get_page_from_gfn

xen/arch/arm/vgic-v3.c read_unknown: · line 443

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    }
+
+    ptr = vmap(pages, nr_pages);

I am not a big fan of the vmap solution for various reasons:
- the VMAP area is small (only 1GB) it will not scale (you seem to
use it to map pretty much all memory provisioned for the ITS)
- writing in a register cannot fail, how do you co-op with that?

I think the best approach here is to use a similar approach as copy_*_guests helpers but dealing with IPA rather than guest VA.

Quoted 38 lines
xen/arch/arm/vgic-v3.c +25 -0
+
+    if ( nr_pages > 1 )
+        xfree(pages);
+
+    return ptr;
+}
+
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    paddr_t pa;
+    unsigned long i;
+
+    if ( !va )
+        return;
+
+    va = (void *)((uintptr_t)va & PAGE_MASK);
+    pa = virt_to_maddr(va);
+
+    vunmap(va);
+    for (i = 0; i < nr_pages; i++)
+        put_page(mfn_to_page((pa >> PAGE_SHIFT) + i));
+
+    return;
+}
+
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t

*info,

                                           uint32_t gicr_reg,
                                           register_t r)
 {
     struct hsr_dabt dabt = info->dabt;
xen/arch/arm/vgic-v3.c +2 -1
+    uint64_t reg;
 
     switch ( gicr_reg )
     {

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_write(struct · line 380

Quoted 6 lines
xen/arch/arm/vgic-v3.c +0 -2
@@ -375,13 +512,37 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct
 vcpu *v, mmio_info_t *info,
     case 0x0050:
         goto write_reserved;
 
-    case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
[… more lines in the raw message]

Coding style, the { should be on a line.

xen/arch/arm/vgic-v3.c

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

unsigned int

xen/arch/arm/vgic-v3.c

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Newline here for clarity. Also please use vgic_reg64_check_access rather than open-coding it.

xen/arch/arm/vgic-v3.c

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

From my understanding VGIC_V3_LPIS_ENABLED is set when the guest enable LPIs on this re-distributor. However, this check is not safe as GICR_CTLR.Enable_LPIs may be set concurrently (the re-distributors are accessible from any vCPU).

Also, when ITS is not available we should avoid to handle the register (i.e treating as write ignore). My rational here is we should limit the amount of emulation exposed to the guest whenever it is possible.

xen/arch/arm/vgic-v3.c

Quoted 1 line
xen/arch/arm/vgic-v3.c +1 -0

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

152 lines not shown · raw message

Quoted 5 lines

3 lines not shown · raw message

4 lines not shown · raw message

Parts of this message are not shown here. View the raw message.