Skip to contentSkip to conversation

arm64: Dom0 ITS emulation

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

v2 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v2 cover arm64: Dom0 ITS emulation

Hi,

a revised version of the series to emulate an ARM GICv3 ITS interrupt controller, for Dom0 only at the moment.
The ITS is an interrupt controller widget providing a sophisticated way of dealing with MSIs in a scalable manner. For more explanations, see below.
Some remarks on this version:
- I kept the command line parameters and Kconfig options in for now. I believe we have to impose _some_ kind of limit on the number of supported LPIs and devices, Linux does certainly do as well. Until we know some one-fits-all numbers and have confirmation from several platforms, I'd like to keep this easily configurable.
I am happy to remove this from the final drop should people still have concerns.
- Two-level tables for devices are missing from this version. This shouldn't be hard to implement, though, I just didn't find the time yet. I suspect it will be part of the next drop.
- To allow mapping devices with non-trivial device ID mappings (platform devices, for instance), we deviate from our premature allocation scheme and move to mapping devices on demand upon Dom0's MAPD calls. I am not a fan of this approach, but this should solve the immediate issues we see on actual hardware. Advantage is that we know the device ID and the number of MSIs very precisely.
I hope that this approach can go away once we have a proper way of dealing with that (DT/IORT parsing in Xen), but it definitely should stay restricted to Dom0.
So I removed the code which created device mapping on the PCI physops calls, however have the patch still around here in case we need it.
- The teardown path needs some more love. AFAICT Dom0 does not take part in the usual domain destroy procedure, so we can't really test this. For now I kept one function in to show what needs to be done, but this is not plugged anywhere. So to avoid untested and possibly buggy code to sneak in, we might think about removing it.
- I tried to exterminate any signed ints and also get rid of any unattended (as in not-named) constants, but I am sure I missed some. So let your OCD kick in and find all of them! Also I am sure the massive rebasing and patch squashing created some new funny bugs and artifacts.

For other changes, see the changelog below.
----------------------------------
This series adds support for emulation of an ARM GICv3 ITS interrupt controller. For hardware which relies on the ITS to provide interrupts for its peripherals this code is needed to get a machine booted into Dom0 at all. ITS emulation for DomUs is only really useful with PCI passthrough, which is not yet available for ARM. It is expected that this feature will be co-developed with the ITS DomU code. However this code drop here considered DomU emulation already, to keep later architectural changes to a minimum.

Some generic design principles:

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

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

* An obvious approach to handling some guest ITS commands would be to propagate them to the host, for instance to map devices and LPIs and to enable or disable LPIs.
However this (later with DomU support) will create an attack vector, as a malicious guest could try to fill the host command queue with propagated commands.
So we try to avoid this situation: Dom0 sending a device mapping (MAPD) command is the only time we allow queuing commands to the host ITS command queue, as this seems to be the only reliable way of getting the required information at the moment. However at the same time we map all events to LPIs already, also enable them. This avoids sending commands later at runtime, as we can deal with mappings and LPI enabling/disabling internally.

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

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

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

Cheers,
Andre

Changelog v1 .. v2:
- clean up header file inclusion
- rework host ITS table allocation: observe attributes, many fixes
- remove patch 1 to export __flush_dcache_area, use existing function instead
- use number of LPIs internally instead of number of bits
- keep host_its_list as private as possible
- keep struct its_devices private
- rework gicv3_its_map_guest_devices
- fix rbtree issues
- more error handling and propagation
- cope with GICv4 implementations (but no virtual LPI features!)
- abstract host and guest ITSes by using doorbell addresses
- join per-redistributor variables into one per-CPU structure
- fix data types (unsigned int)
- many minor bug fixes

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

*** BLURB HERE ***

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

 docs/misc/xen-command-line.markdown |  17 +
 xen/arch/arm/Kconfig                |  33 ++
 xen/arch/arm/Makefile               |   3 +
 xen/arch/arm/gic-v3-its.c           | 925 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3-lpi.c           | 491 +++++++++++++++++++
 xen/arch/arm/gic-v3.c               |  70 ++-
 xen/arch/arm/gic.c                  |   9 +-
 xen/arch/arm/vgic-v3-its.c          | 953 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c              | 347 +++++++++++--
 xen/arch/arm/vgic.c                 |  68 ++-
 xen/include/asm-arm/atomic.h        |   6 +-
 xen/include/asm-arm/bitops.h        |   1 +
 xen/include/asm-arm/domain.h        |  14 +-
 xen/include/asm-arm/gic.h           |   7 +
 xen/include/asm-arm/gic_v3_defs.h   |  73 ++-
 xen/include/asm-arm/gic_v3_its.h    | 250 ++++++++++
 xen/include/asm-arm/irq.h           |   8 +
 xen/include/asm-arm/vgic.h          |  34 ++
 18 files changed, 3263 insertions(+), 46 deletions(-)
 create mode 100644 xen/arch/arm/gic-v3-its.c
 create mode 100644 xen/arch/arm/gic-v3-lpi.c
 create mode 100644 xen/arch/arm/vgic-v3-its.c
 create mode 100644 xen/include/asm-arm/gic_v3_its.h
Signature · 7 lines
2.9.0


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

PATCH v2 1/27 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             |  4 +++
 xen/arch/arm/Makefile            |  1 +
 xen/arch/arm/gic-v3-its.c        | 73 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c            | 10 +++---
 xen/include/asm-arm/gic_v3_its.h | 67 ++++++++++++++++++++++++++++++++++++
 5 files changed, 151 insertions(+), 4 deletions(-)
 create mode 100644 xen/arch/arm/gic-v3-its.c
 create mode 100644 xen/include/asm-arm/gic_v3_its.h
xen/arch/arm/Kconfig +4 -0
index 2e023d1..bf64c61 100644
@@ -45,6 +45,10 @@ config ACPI
 config HAS_GICV3
 	bool
 
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on HAS_GICV3
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/Makefile +1 -0
index 7afb8a3..54860e0 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-v3-its.o
 obj-y += guestcopy.o
 obj-y += hvm.o
 obj-y += io.o
xen/arch/arm/gic-v3-its.c new file +73 -0

73 lines not on this page · raw message

xen/arch/arm/gic-v3.c +6 -4
index 955591b..1512521 100644
@@ -43,6 +43,7 @@
 #include <asm/device.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
 #include <asm/cpufeature.h>
 #include <asm/acpi.h>
 
@@ -1228,11 +1229,12 @@ static void __init gicv3_dt_init(void)
      */
     res = dt_device_get_address(node, 1 + gicv3.rdist_count,
                                 &cbase, &csize);
-    if ( res )
-        return;
+    if ( !res )
+        dt_device_get_address(node, 1 + gicv3.rdist_count + 2,
+                              &vbase, &vsize);
 
-    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_v3_its.h new file +67 -0

67 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: [PATCH v2 01/27] ARM: GICv3 ITS: parse and store ITS subnodes from hardware DT

Hi Andre,

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

Quoted 11 lines · Andre Przywara
xen/arch/arm/Kconfig +3 -0
index 2e023d1..bf64c61 100644
@@ -45,6 +45,10 @@ config ACPI
 config HAS_GICV3
 	bool
 
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on HAS_GICV3

IIRC, we discussed that GICv3 ITS will be a technical preview for Xen 4.9. I think case I think it should depends on EXPERT mode to avoid been shipped by default.

Quoted 4 lines
xen/arch/arm/Kconfig +1 -0
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"

[...]

Quoted 21 lines
xen/arch/arm/gic-v3-its.c new file +15 -0
new file mode 100644
index 0000000..4056e5b
@@ -0,0 +1,73 @@
+/*
+ * xen/arch/arm/gic-v3-its.c
+ *
+ * ARM GICv3 Interrupt Translation Service (ITS) support
+ *
+ * Copyright (C) 2016,2017 - ARM Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.

xen/arch/arm/gic-v3-its.c line 25

Quoted 10 lines
xen/arch/arm/gic-v3-its.c new file +10 -0
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <xen/lib.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+
+LIST_HEAD(host_its_list);

On the previous version we agreed that host_its_list should be confined to gic-v3-its.c. So why is it still exported?

[...]

Quoted 6 lines
xen/include/asm-arm/gic_v3_its.h new file +0 -0
new file mode 100644
index 0000000..765a655
@@ -0,0 +1,67 @@

[...]

xen/include/asm-arm/gic_v3_its.h line 45

Quoted 3 lines
xen/include/asm-arm/gic_v3_its.h new file +3 -0
+#else
+
+static LIST_HEAD(host_its_list);

This is quite ugly and could really be avoided if the host_its_list was confined as we agreed in the previous version.

Cheers,

Signature · 6 lines
Julien Grall

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

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

Hi,

Quoted 52 lines · Julien Grall

Hi Andre,

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

Quoted 11 lines · Andre Przywara
xen/arch/arm/Kconfig +3 -0
index 2e023d1..bf64c61 100644
@@ -45,6 +45,10 @@ config ACPI
 config HAS_GICV3
     bool
 
+config HAS_ITS
+        bool "GICv3 ITS MSI controller support"
+        depends on HAS_GICV3

IIRC, we discussed that GICv3 ITS will be a technical preview for Xen 4.9. I think case I think it should depends on EXPERT mode to avoid been shipped by default.

Quoted 4 lines
xen/arch/arm/Kconfig +1 -0
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"

[...]

Quoted 26 lines
xen/arch/arm/gic-v3-its.c new file +20 -0
new file mode 100644
index 0000000..4056e5b
@@ -0,0 +1,73 @@
+/*
+ * xen/arch/arm/gic-v3-its.c
+ *
+ * ARM GICv3 Interrupt Translation Service (ITS) support
+ *
+ * Copyright (C) 2016,2017 - ARM Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
Quoted 8 lines

xen/arch/arm/gic-v3-its.c line 25

Quoted 5 lines
xen/arch/arm/gic-v3-its.c new file +5 -0
+#include <xen/lib.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+
+LIST_HEAD(host_its_list);

On the previous version we agreed that host_its_list should be confined to gic-v3-its.c. So why is it still exported?

Because I need it later on in vgic-v3.c, when creating virtual ITSes that mimic the host ITSes, see patch 25/27. I haven't found a neat solution for that yet.

For the rest of the code you will see that every other reference to the host_its_list has been removed, so it is *mostly* confined to gic-v3-its.c by now.

Quoted 14 lines

[...]

Quoted 7 lines
xen/include/asm-arm/gic_v3_its.h new file +0 -0
new file mode 100644
index 0000000..765a655
@@ -0,0 +1,67 @@

[...]

Quoted 6 lines

xen/include/asm-arm/gic_v3_its.h line 45

Quoted 3 lines
xen/include/asm-arm/gic_v3_its.h new file +3 -0
+#else
+
+static LIST_HEAD(host_its_list);

This is quite ugly and could really be avoided if the host_its_list was confined as we agreed in the previous version.

I think you said that you are fine with an export if it cannot easily be avoided.

Cheers,
Andre.

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

#4 lore raw

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

Hi Andre,

Quoted 39 lines · Andre Przywara
Quoted 29 lines · Julien Grall
Quoted 28 lines · Andre Przywara
xen/arch/arm/Kconfig +0 -0
index 2e023d1..bf64c61 100644
xen/arch/arm/gic-v3-its.c new file +20 -0
new file mode 100644
index 0000000..4056e5b
@@ -0,0 +1,73 @@
+/*
+ * xen/arch/arm/gic-v3-its.c
+ *
+ * ARM GICv3 Interrupt Translation Service (ITS) support
+ *
+ * Copyright (C) 2016,2017 - ARM Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
Quoted 8 lines

xen/arch/arm/gic-v3-its.c line 25

Quoted 5 lines
xen/arch/arm/gic-v3-its.c new file +5 -0
+#include <xen/lib.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+
+LIST_HEAD(host_its_list);

On the previous version we agreed that host_its_list should be confined to gic-v3-its.c. So why is it still exported?

Quoted 3 lines

Because I need it later on in vgic-v3.c, when creating virtual ITSes that mimic the host ITSes, see patch 25/27. I haven't found a neat solution for that yet.

I will have a look.

Cheers,

Signature · 6 lines
Julien Grall

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

PATCH v2 2/27 ARM: GICv3: allocate LPI pending and property table

In reply to Andre Przywara

The ARM GICv3 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 redistributor. The maximum number of LPIs to be used can be adjusted with the command line option "max_lpi_bits", which defaults to a compile time constant exposed in Kconfig.

---
 docs/misc/xen-command-line.markdown |   9 ++
 xen/arch/arm/Kconfig                |  15 +++
 xen/arch/arm/Makefile               |   1 +
 xen/arch/arm/gic-v3-lpi.c           | 201 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c               |  17 +++
 xen/include/asm-arm/bitops.h        |   1 +
 xen/include/asm-arm/gic.h           |   2 +
 xen/include/asm-arm/gic_v3_defs.h   |  52 +++++++++-
 xen/include/asm-arm/gic_v3_its.h    |  14 +++
 9 files changed, 311 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/gic-v3-lpi.c
docs/misc/xen-command-line.markdown +9 -0
index a11fdf9..619016d 100644
@@ -1158,6 +1158,15 @@ based interrupts. Any higher IRQs will be available for use via PCI MSI.
 ### maxcpus
 > `= <integer>`
 
+### max\_lpi\_bits
+> `= <integer>`
+
+Specifies the number of ARM GICv3 LPI interrupts to allocate on the host,
+presented as the number of bits needed to encode it. This must be at least
+14 and not exceed 32, and each LPI requires one byte (configuration) and
+one pending bit to be allocated.
+Defaults to 20 bits (to cover at most 1048576 interrupts).
+
 ### mce
 > `= <integer>`
 
xen/arch/arm/Kconfig +15 -0
index bf64c61..86f7b53 100644
@@ -49,6 +49,21 @@ config HAS_ITS
         bool "GICv3 ITS MSI controller support"
         depends on HAS_GICV3
 
+config MAX_PHYS_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+        help
+          Specifies the maximum number of LPIs (in bits) Xen should take
+          care of. The host ITS may provide support for a very large number
+          of supported LPIs, for all of which we may not want to allocate
+          memory, so this number here allows to limit this.
+          Xen itself does not know how many LPIs domains will ever need
+          beforehand.
+          This can be overridden on the command line with the max_lpi_bits
+          parameter.
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/Makefile +1 -0
index 54860e0..02a8737 100644
@@ -19,6 +19,7 @@ obj-y += gic.o
 obj-y += gic-v2.o
 obj-$(CONFIG_HAS_GICV3) += gic-v3.o
 obj-$(CONFIG_HAS_ITS) += gic-v3-its.o
+obj-$(CONFIG_HAS_ITS) += gic-v3-lpi.o
 obj-y += guestcopy.o
 obj-y += hvm.o
 obj-y += io.o
xen/arch/arm/gic-v3-lpi.c new file +201 -0

201 lines not on this page · raw message

xen/arch/arm/gic-v3.c +17 -0
index 1512521..ed78363 100644
@@ -548,6 +548,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));
 
@@ -660,6 +663,20 @@ static int __init gicv3_populate_rdist(void)
             if ( (typer >> 32) == aff )
             {
                 this_cpu(rbase) = ptr;
+
+                if ( typer & GICR_TYPER_PLPIS )
+                {
+                    int ret;
+
+                    ret = gicv3_lpi_init_rdist(ptr);
+                    if ( ret && ret != -ENODEV )
+                    {
+                        printk("GICv3: CPU%d: Cannot initialize LPIs: %d\n",
+                               smp_processor_id(), ret);
+                        break;
+                    }
+                }
+
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
                 return 0;
xen/include/asm-arm/bitops.h +1 -0
index bda8898..1cbfb9e 100644
@@ -24,6 +24,7 @@
 #define BIT(nr)                 (1UL << (nr))
 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_WORD))
 #define BIT_WORD(nr)            ((nr) / BITS_PER_WORD)
+#define BIT_ULL(nr)             (1ULL << (nr))
 #define BITS_PER_BYTE           8
 
 #define ADDR (*(volatile int *) addr)
xen/include/asm-arm/gic.h +2 -0
index 836a103..12bd155 100644
@@ -220,6 +220,8 @@ enum gic_version {
     GIC_V3,
 };
 
+#define LPI_OFFSET      8192
+
 extern enum gic_version gic_hw_version(void);
 
 /* Program the IRQ type into the GIC */
xen/include/asm-arm/gic_v3_defs.h +51 -1

70 lines not on this page · raw message

xen/include/asm-arm/gic_v3_its.h +14 -0
index 765a655..219d109 100644
@@ -40,6 +40,11 @@ void gicv3_its_dt_init(const struct dt_device_node *node);
 
 bool gicv3_its_host_has_its(void);
 
+int gicv3_lpi_init_rdist(void __iomem * rdist_base);
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);
+
 #else
 
 static LIST_HEAD(host_its_list);
@@ -53,6 +58,15 @@ static inline bool gicv3_its_host_has_its(void)
     return false;
 }
 
+static inline int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    return -ENODEV;
+}
+
+static inline int gicv3_lpi_init_host_lpis(unsigned int nr_lpis)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif
Signature · 7 lines
2.9.0


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

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Quoted 194 lines · Andre Przywara

The ARM GICv3 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 redistributor. The maximum number of LPIs to be used can be adjusted with the command line option "max_lpi_bits", which defaults to a compile time constant exposed in Kconfig.

---
 docs/misc/xen-command-line.markdown |   9 ++
 xen/arch/arm/Kconfig                |  15 +++
 xen/arch/arm/Makefile               |   1 +
 xen/arch/arm/gic-v3-lpi.c           | 201 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c               |  17 +++
 xen/include/asm-arm/bitops.h        |   1 +
 xen/include/asm-arm/gic.h           |   2 +
 xen/include/asm-arm/gic_v3_defs.h   |  52 +++++++++-
 xen/include/asm-arm/gic_v3_its.h    |  14 +++
 9 files changed, 311 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/arm/gic-v3-lpi.c
docs/misc/xen-command-line.markdown +9 -0
index a11fdf9..619016d 100644
@@ -1158,6 +1158,15 @@ based interrupts. Any higher IRQs will be available for use via PCI MSI.
 ### maxcpus
 > `= <integer>`
 
+### max\_lpi\_bits
+> `= <integer>`
+
+Specifies the number of ARM GICv3 LPI interrupts to allocate on the host,
+presented as the number of bits needed to encode it. This must be at least
+14 and not exceed 32, and each LPI requires one byte (configuration) and
+one pending bit to be allocated.
+Defaults to 20 bits (to cover at most 1048576 interrupts).
+
 ### mce
 > `= <integer>`
 
xen/arch/arm/Kconfig +15 -0
index bf64c61..86f7b53 100644
@@ -49,6 +49,21 @@ config HAS_ITS
         bool "GICv3 ITS MSI controller support"
         depends on HAS_GICV3
 
+config MAX_PHYS_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+        help
+          Specifies the maximum number of LPIs (in bits) Xen should take
+          care of. The host ITS may provide support for a very large number
+          of supported LPIs, for all of which we may not want to allocate
+          memory, so this number here allows to limit this.
+          Xen itself does not know how many LPIs domains will ever need
+          beforehand.
+          This can be overridden on the command line with the max_lpi_bits
+          parameter.
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/Makefile +1 -0
index 54860e0..02a8737 100644
@@ -19,6 +19,7 @@ obj-y += gic.o
 obj-y += gic-v2.o
 obj-$(CONFIG_HAS_GICV3) += gic-v3.o
 obj-$(CONFIG_HAS_ITS) += gic-v3-its.o
+obj-$(CONFIG_HAS_ITS) += gic-v3-lpi.o
 obj-y += guestcopy.o
 obj-y += hvm.o
 obj-y += io.o
xen/arch/arm/gic-v3-lpi.c new file +107 -0
new file mode 100644
index 0000000..4f8414b
@@ -0,0 +1,201 @@
+/*
+ * xen/arch/arm/gic-v3-lpi.c
+ *
+ * ARM GICv3 Locality-specific Peripheral Interrupts (LPI) support
+ *
+ * Copyright (C) 2016,2017 - ARM Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <xen/lib.h>
+#include <xen/mm.h>
+#include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+#include <asm/io.h>
+#include <asm/page.h>
+
+#define LPI_PROPTABLE_NEEDS_FLUSHING    (1U << 0)
+/* Global state */
+static struct {
+    /* The global LPI property table, shared by all redistributors. */
+    uint8_t *lpi_property;
+    /*
+     * Number of physical LPIs the host supports. This is a property of
+     * the GIC hardware. We depart from the habit of naming these things
+     * "physical" in Xen, as the GICv3/4 spec uses the term "physical LPI"
+     * in a different context to differentiate them from "virtual LPIs".
+     */
+    unsigned long int nr_host_lpis;
+    unsigned int flags;
+} lpi_data;
+
+struct lpi_redist_data {
+    void                *pending_table;
+};
+
+static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
+
+#define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)
+
+static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
+{
+    uint64_t val;
+    unsigned int order;
+    void *pendtable;
+
+    if ( this_cpu(lpi_redist).pending_table )
+        return -EBUSY;
+
+    val  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    val |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    val |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;
+
+    /*
+     * The pending table holds one bit per LPI and even covers bits for
+     * interrupt IDs below 8192, so we allocate the full range.
+     * The GICv3 imposes a 64KB alignment requirement, also requires
+     * physically contigious memory.
+     */
+    order = max(get_order_from_bytes(lpi_data.nr_host_lpis / 8), 4U);
+    pendtable = alloc_xenheap_pages(order, 0);
+    if ( !pendtable )
+        return -ENOMEM;
+
+    /* Make sure the physical address can be encoded in the register. */
+    if ( (virt_to_maddr(pendtable) & ~GENMASK(51, 16)) )
+    {
+        free_xenheap_pages(pendtable, 0);
+        return -ERANGE;
+    }
+    memset(pendtable, 0, lpi_data.nr_host_lpis / 8);
+    clean_and_invalidate_dcache_va_range(pendtable,
+                                         lpi_data.nr_host_lpis / 8);
+
+    this_cpu(lpi_redist).pending_table = pendtable;
+
+    val |= GICR_PENDBASER_PTZ;
+
+    val |= virt_to_maddr(pendtable);
+
+    *reg = val;
+
+    return 0;
+}
+
+/*
+ * Tell a redistributor about the (shared) property table, allocating one
+ * if not already done.
+ */
+static int gicv3_lpi_set_proptable(void __iomem * rdist_base)
+{
+    uint64_t reg;
+
+    reg  = GIC_BASER_CACHE_RaWaWb << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_InnerShareable << GICR_PROPBASER_SHAREABILITY_SHIFT;

xen/arch/arm/gic-v3-lpi.c line 117

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+
+    /*
+     * The property table is shared across all redistributors, so allocate
+     * this only once, but return the same value on subsequent calls.
+     */
+    if ( !lpi_data.lpi_property )
+    {
+        /* The property table holds one byte per LPI. */
+        unsigned int order = get_order_from_bytes(lpi_data.nr_host_lpis);
+        void *table = alloc_xenheap_pages(order, 0);

you investigated the possibility of using _xmalloc to 64K align it?

Quoted 254 lines
xen/arch/arm/gic-v3-lpi.c new file +83 -0
+        if ( !table )
+            return -ENOMEM;
+
+        /* Make sure the physical address can be encoded in the register. */
+        if ( (virt_to_maddr(table) & ~GENMASK(51, 12)) )
+        {
+            free_xenheap_pages(table, 0);
+            return -ERANGE;
+        }
+        memset(table, GIC_PRI_IRQ | LPI_PROP_RES1, MAX_PHYS_LPIS);
+        clean_and_invalidate_dcache_va_range(table, MAX_PHYS_LPIS);
+        lpi_data.lpi_property = table;
+    }
+
+    /* Encode the number of bits needed, minus one */
+    reg |= ((fls(lpi_data.nr_host_lpis - 1) - 1) << 0);
+
+    reg |= virt_to_maddr(lpi_data.lpi_property);
+
+    writeq_relaxed(reg, rdist_base + GICR_PROPBASER);
+    reg = readq_relaxed(rdist_base + GICR_PROPBASER);
+
+    /* If we can't do shareable, we have to drop cacheability as well. */
+    if ( !(reg & GICR_PROPBASER_SHAREABILITY_MASK) )
+    {
+        reg &= ~GICR_PROPBASER_INNER_CACHEABILITY_MASK;
+        reg |= GIC_BASER_CACHE_nC << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+    }
+
+    /* Remember that we have to flush the property table if non-cacheable. */
+    if ( (reg & GICR_PROPBASER_INNER_CACHEABILITY_MASK) <= GIC_BASER_CACHE_nC )
+    {
+        lpi_data.flags |= LPI_PROPTABLE_NEEDS_FLUSHING;
+        /* Update the redistributors knowledge about the attributes. */
+        writeq_relaxed(reg, rdist_base + GICR_PROPBASER);
+    }
+
+    return 0;
+}
+
+int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+    int ret;
+
+    /* We don't support LPIs without an ITS. */
+    if ( !gicv3_its_host_has_its() )
+        return -ENODEV;
+
+    /* Make sure LPIs are disabled before setting up the tables. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    if ( reg & GICR_CTLR_ENABLE_LPIS )
+        return -EBUSY;
+
+    ret = gicv3_lpi_allocate_pendtable(&table_reg);
+    if (ret)
+        return ret;
+    writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);
+
+    return gicv3_lpi_set_proptable(rdist_base);
+}
+
+static unsigned int max_lpi_bits = CONFIG_MAX_PHYS_LPI_BITS;
+integer_param("max_lpi_bits", max_lpi_bits);
+
+int gicv3_lpi_init_host_lpis(unsigned int hw_lpi_bits)
+{
+    lpi_data.nr_host_lpis = BIT_ULL(min(hw_lpi_bits, max_lpi_bits));
+
+    printk("GICv3: using at most %ld LPIs on the host.\n", MAX_PHYS_LPIS);
+
+    return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/gic-v3.c +17 -0
index 1512521..ed78363 100644
@@ -548,6 +548,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));
 
@@ -660,6 +663,20 @@ static int __init gicv3_populate_rdist(void)
             if ( (typer >> 32) == aff )
             {
                 this_cpu(rbase) = ptr;
+
+                if ( typer & GICR_TYPER_PLPIS )
+                {
+                    int ret;
+
+                    ret = gicv3_lpi_init_rdist(ptr);
+                    if ( ret && ret != -ENODEV )
+                    {
+                        printk("GICv3: CPU%d: Cannot initialize LPIs: %d\n",
+                               smp_processor_id(), ret);
+                        break;
+                    }
+                }
+
                 printk("GICv3: CPU%d: Found redistributor in region %d @%p\n",
                         smp_processor_id(), i, ptr);
                 return 0;
xen/include/asm-arm/bitops.h +1 -0
index bda8898..1cbfb9e 100644
@@ -24,6 +24,7 @@
 #define BIT(nr)                 (1UL << (nr))
 #define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_WORD))
 #define BIT_WORD(nr)            ((nr) / BITS_PER_WORD)
+#define BIT_ULL(nr)             (1ULL << (nr))
 #define BITS_PER_BYTE           8
 
 #define ADDR (*(volatile int *) addr)
xen/include/asm-arm/gic.h +2 -0
index 836a103..12bd155 100644
@@ -220,6 +220,8 @@ enum gic_version {
     GIC_V3,
 };
 
+#define LPI_OFFSET      8192
+
 extern enum gic_version gic_hw_version(void);
 
 /* Program the IRQ type into the GIC */
xen/include/asm-arm/gic_v3_defs.h +51 -1
index 6bd25a5..b307322 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,61 @@
 #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)
 
+/* For specifying the inner cacheability type only */
+#define GIC_BASER_CACHE_nCnB         0ULL
+/* For specifying the outer cacheability type only */
+#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 GICR_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 GICR_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_PRIO_MASK           0xfc
+#define LPI_PROP_RES1                (1 << 1)
+#define LPI_PROP_ENABLED             (1 << 0)
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
xen/include/asm-arm/gic_v3_its.h +14 -0
index 765a655..219d109 100644
@@ -40,6 +40,11 @@ void gicv3_its_dt_init(const struct dt_device_node *node);
 
 bool gicv3_its_host_has_its(void);
 
+int gicv3_lpi_init_rdist(void __iomem * rdist_base);
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);
+
 #else
 
 static LIST_HEAD(host_its_list);
@@ -53,6 +58,15 @@ static inline bool gicv3_its_host_has_its(void)
     return false;
 }
 
+static inline int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    return -ENODEV;
+}
+
+static inline int gicv3_lpi_init_host_lpis(unsigned int nr_lpis)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif
Signature · 1 line
2.9.0

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

#7 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Quoted 11 lines · Stefano Stabellini
Quoted 10 lines · Andre Przywara

The ARM GICv3 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 redistributor. The maximum number of LPIs to be used can be adjusted with the command line option "max_lpi_bits", which defaults to a compile time constant exposed in Kconfig.

....

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c new file +0 -0
new file mode 100644
index 0000000..4f8414b

....

Quoted 15 lines
Quoted 15 lines
xen/arch/arm/gic-v3-lpi.c new file +15 -0
+/*
+ * Tell a redistributor about the (shared) property table, allocating one
+ * if not already done.
+ */
+static int gicv3_lpi_set_proptable(void __iomem * rdist_base)
+{
+    uint64_t reg;
+
+    reg  = GIC_BASER_CACHE_RaWaWb << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_InnerShareable << GICR_PROPBASER_SHAREABILITY_SHIFT;
+
+    /*
+     * The property table is shared across all redistributors, so allocate
+     * this only once, but return the same value on subsequent calls.
Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c line 117

Quoted 6 lines
xen/arch/arm/gic-v3-lpi.c new file +6 -0
+     */
+    if ( !lpi_data.lpi_property )
+    {
+        /* The property table holds one byte per LPI. */
+        unsigned int order = get_order_from_bytes(lpi_data.nr_host_lpis);
+        void *table = alloc_xenheap_pages(order, 0);

you investigated the possibility of using _xmalloc to 64K align it?

Yes, _xmalloc gives me the alignment, but AFAICT not the physical contiguous memory guarantee the table needs. If there are other ways to satisfy those requirements, I am all ears. Otherwise I'd just add a comment to motivate this.

Cheers,
Andre.

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

#8 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Quoted 47 lines · André Przywara
Quoted 11 lines · Stefano Stabellini
Quoted 10 lines · Andre Przywara

The ARM GICv3 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 redistributor. The maximum number of LPIs to be used can be adjusted with the command line option "max_lpi_bits", which defaults to a compile time constant exposed in Kconfig.

....

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c new file +0 -0
new file mode 100644
index 0000000..4f8414b

....

Quoted 15 lines
Quoted 15 lines
xen/arch/arm/gic-v3-lpi.c new file +15 -0
+/*
+ * Tell a redistributor about the (shared) property table, allocating one
+ * if not already done.
+ */
+static int gicv3_lpi_set_proptable(void __iomem * rdist_base)
+{
+    uint64_t reg;
+
+    reg  = GIC_BASER_CACHE_RaWaWb << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_InnerShareable << GICR_PROPBASER_SHAREABILITY_SHIFT;
+
+    /*
+     * The property table is shared across all redistributors, so allocate
+     * this only once, but return the same value on subsequent calls.
Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c line 117

Quoted 6 lines
xen/arch/arm/gic-v3-lpi.c new file +6 -0
+     */
+    if ( !lpi_data.lpi_property )
+    {
+        /* The property table holds one byte per LPI. */
+        unsigned int order = get_order_from_bytes(lpi_data.nr_host_lpis);
+        void *table = alloc_xenheap_pages(order, 0);

you investigated the possibility of using _xmalloc to 64K align it?

Quoted 4 lines

Yes, _xmalloc gives me the alignment, but AFAICT not the physical contiguous memory guarantee the table needs. If there are other ways to satisfy those requirements, I am all ears. Otherwise I'd just add a comment to motivate this.

OK, but the problem is that I am not sure whether alloc_xenheap_pages makes any guarantees on the alignment of the allocated pages. Is it possible to allocate 1<<4 pages that are not 64K aligned with it?

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

#9 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Hi,

Quoted 48 lines · Stefano Stabellini
Quoted 47 lines · André Przywara
Quoted 11 lines · Stefano Stabellini
Quoted 10 lines · Andre Przywara

The ARM GICv3 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 redistributor. The maximum number of LPIs to be used can be adjusted with the command line option "max_lpi_bits", which defaults to a compile time constant exposed in Kconfig.

....

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c new file +0 -0
new file mode 100644
index 0000000..4f8414b

....

Quoted 23 lines
Quoted 11 lines
xen/arch/arm/gic-v3-lpi.c new file +11 -0
+/*
+ * Tell a redistributor about the (shared) property table, allocating one
+ * if not already done.
+ */
+static int gicv3_lpi_set_proptable(void __iomem * rdist_base)
+{
+    uint64_t reg;
+
+    reg  = GIC_BASER_CACHE_RaWaWb << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_InnerShareable << GICR_PROPBASER_SHAREABILITY_SHIFT;

xen/arch/arm/gic-v3-lpi.c line 117

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+
+    /*
+     * The property table is shared across all redistributors, so allocate
+     * this only once, but return the same value on subsequent calls.
+     */
+    if ( !lpi_data.lpi_property )
+    {
+        /* The property table holds one byte per LPI. */
+        unsigned int order = get_order_from_bytes(lpi_data.nr_host_lpis);
+        void *table = alloc_xenheap_pages(order, 0);

you investigated the possibility of using _xmalloc to 64K align it?

Quoted 8 lines
Quoted 4 lines

Yes, _xmalloc gives me the alignment, but AFAICT not the physical contiguous memory guarantee the table needs. If there are other ways to satisfy those requirements, I am all ears. Otherwise I'd just add a comment to motivate this.

OK, but the problem is that I am not sure whether alloc_xenheap_pages makes any guarantees on the alignment of the allocated pages. Is it possible to allocate 1<<4 pages that are not 64K aligned with it?

My understanding of the code is that it naturally aligns the allocation to the order you requested: So calling it with (4, 0) would give you (4K << 4) = 64K alignment (because it's taken from the 64K bucket).

But anyway the property table needs only to be 4K aligned, isn't it?

I know that the GICv3 is amazing in this aspect: every table has slightly different requirements when it comes to alignment, support of physical address space, page sizes and so on...

Cheers,
Andre.

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

#10 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 49 lines · Stefano Stabellini
Quoted 48 lines · André Przywara
Quoted 47 lines · Stefano Stabellini
Quoted 11 lines · Andre Przywara
Quoted 10 lines

The ARM GICv3 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 redistributor. The maximum number of LPIs to be used can be adjusted with the command line option "max_lpi_bits", which defaults to a compile time constant exposed in Kconfig.

....

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c new file +0 -0
new file mode 100644
index 0000000..4f8414b

....

Quoted 23 lines
Quoted 11 lines
xen/arch/arm/gic-v3-lpi.c new file +11 -0
+/*
+ * Tell a redistributor about the (shared) property table, allocating one
+ * if not already done.
+ */
+static int gicv3_lpi_set_proptable(void __iomem * rdist_base)
+{
+    uint64_t reg;
+
+    reg  = GIC_BASER_CACHE_RaWaWb << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_InnerShareable << GICR_PROPBASER_SHAREABILITY_SHIFT;

xen/arch/arm/gic-v3-lpi.c line 117

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+
+    /*
+     * The property table is shared across all redistributors, so allocate
+     * this only once, but return the same value on subsequent calls.
+     */
+    if ( !lpi_data.lpi_property )
+    {
+        /* The property table holds one byte per LPI. */
+        unsigned int order = get_order_from_bytes(lpi_data.nr_host_lpis);
+        void *table = alloc_xenheap_pages(order, 0);

you investigated the possibility of using _xmalloc to 64K align it?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Yes, _xmalloc gives me the alignment, but AFAICT not the physical contiguous memory guarantee the table needs. If there are other ways to satisfy those requirements, I am all ears. Otherwise I'd just add a comment to motivate this.

As already said numerous times, _x*alloc will guarantee contiguous physical memory. The same as kzalloc.

Quoted 9 lines
Quoted 4 lines

OK, but the problem is that I am not sure whether alloc_xenheap_pages makes any guarantees on the alignment of the allocated pages. Is it possible to allocate 1<<4 pages that are not 64K aligned with it?

My understanding of the code is that it naturally aligns the allocation to the order you requested: So calling it with (4, 0) would give you (4K << 4) = 64K alignment (because it's taken from the 64K bucket).

Quoted 5 lines

But anyway the property table needs only to be 4K aligned, isn't it?

I know that the GICv3 is amazing in this aspect: every table has slightly different requirements when it comes to alignment, support of physical address space, page sizes and so on...

alloc_xenheap_pages will guarantee allocation aligned to the (1 << order) * PAGE_SIZE.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

In reply to Andre Przywara

Hi Andre,

Quoted 12 lines · Andre Przywara
xen/arch/arm/Kconfig +4 -0
index bf64c61..86f7b53 100644
@@ -49,6 +49,21 @@ config HAS_ITS
         bool "GICv3 ITS MSI controller support"
         depends on HAS_GICV3
 
+config MAX_PHYS_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32

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

Quoted 10 lines
xen/arch/arm/Kconfig +10 -0
+        default "20"
+        help
+          Specifies the maximum number of LPIs (in bits) Xen should take
+          care of. The host ITS may provide support for a very large number
+          of supported LPIs, for all of which we may not want to allocate
+          memory, so this number here allows to limit this.
+          Xen itself does not know how many LPIs domains will ever need
+          beforehand.
+          This can be overridden on the command line with the max_lpi_bits
+          parameter.

I continue to disagree on this Kconfig option. There is no sensible default value and I don't see how a distribution will be able to pick-up one value here.

If the number of LPIs have to be restricted, this should be done via the command line or a per-platform option.

I have already made my point on previous e-mails so I am not going to argue more.

Quoted 41 lines
xen/arch/arm/Kconfig +1 -0
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/Makefile +1 -0
index 54860e0..02a8737 100644
@@ -19,6 +19,7 @@ obj-y += gic.o
 obj-y += gic-v2.o
 obj-$(CONFIG_HAS_GICV3) += gic-v3.o
 obj-$(CONFIG_HAS_ITS) += gic-v3-its.o
+obj-$(CONFIG_HAS_ITS) += gic-v3-lpi.o
 obj-y += guestcopy.o
 obj-y += hvm.o
 obj-y += io.o
xen/arch/arm/gic-v3-lpi.c new file +19 -0
new file mode 100644
index 0000000..4f8414b
@@ -0,0 +1,201 @@
+/*
+ * xen/arch/arm/gic-v3-lpi.c
+ *
+ * ARM GICv3 Locality-specific Peripheral Interrupts (LPI) support
+ *
+ * Copyright (C) 2016,2017 - ARM Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */

xen/arch/arm/gic-v3-lpi.c line 29

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+
+#include <xen/lib.h>
+#include <xen/mm.h>
+#include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+#include <asm/io.h>
+#include <asm/page.h>
+
+#define LPI_PROPTABLE_NEEDS_FLUSHING    (1U << 0)

NIT: Newline here please.

xen/arch/arm/gic-v3-lpi.c line 40

Quoted 11 lines
xen/arch/arm/gic-v3-lpi.c new file +11 -0
+/* Global state */
+static struct {
+    /* The global LPI property table, shared by all redistributors. */
+    uint8_t *lpi_property;
+    /*
+     * Number of physical LPIs the host supports. This is a property of
+     * the GIC hardware. We depart from the habit of naming these things
+     * "physical" in Xen, as the GICv3/4 spec uses the term "physical LPI"
+     * in a different context to differentiate them from "virtual LPIs".
+     */
+    unsigned long int nr_host_lpis;

Why unsigned long?

xen/arch/arm/gic-v3-lpi.c line 50

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+    unsigned int flags;
+} lpi_data;
+
+struct lpi_redist_data {
+    void                *pending_table;
+};
+
+static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
+
+#define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)

This is fairly confusing. When I read "nr_host_lpis" I understand that you store the number of LPIs. But in fact you store the maximum LPI ID.

Also it is very unclear the difference between MAX_PHYS_LPIS and nr_host_lpis and will likely cause trouble in the future.

So it looks like to me that nr_host_lpis should be named max_host_lpis_ids and MAX_PHYS_LPIS should be MAX_NR_PHYS_LPIS.

Also, please stay consistent with the naming. If you use host then use host everywhere and not a mix between phys and host for the same purpose.

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c new file +9 -0
+
+static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
+{
+    uint64_t val;
+    unsigned int order;
+    void *pendtable;
+
+    if ( this_cpu(lpi_redist).pending_table )
+        return -EBUSY;

xen/arch/arm/gic-v3-lpi.c line 69

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+
+    val  = GIC_BASER_CACHE_RaWaWb << GICR_PENDBASER_INNER_CACHEABILITY_SHIFT;
+    val |= GIC_BASER_CACHE_SameAsInner << GICR_PENDBASER_OUTER_CACHEABILITY_SHIFT;
+    val |= GIC_BASER_InnerShareable << GICR_PENDBASER_SHAREABILITY_SHIFT;
+
+    /*
+     * The pending table holds one bit per LPI and even covers bits for
+     * interrupt IDs below 8192, so we allocate the full range.
+     * The GICv3 imposes a 64KB alignment requirement, also requires
+     * physically contigious memory.

The bit after the comma seems quite obvious. Both xalloc and alloc_xenheap_pages will ensure that the region will be contiguous in the physical address space.

[...]

Quoted 24 lines
xen/arch/arm/gic-v3-lpi.c new file +24 -0
+static int gicv3_lpi_set_proptable(void __iomem * rdist_base)
+{
+    uint64_t reg;
+
+    reg  = GIC_BASER_CACHE_RaWaWb << GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_InnerShareable << GICR_PROPBASER_SHAREABILITY_SHIFT;
+
+    /*
+     * The property table is shared across all redistributors, so allocate
+     * this only once, but return the same value on subsequent calls.
+     */
+    if ( !lpi_data.lpi_property )
+    {
+        /* The property table holds one byte per LPI. */
+        unsigned int order = get_order_from_bytes(lpi_data.nr_host_lpis);
+        void *table = alloc_xenheap_pages(order, 0);
+
+        if ( !table )
+            return -ENOMEM;
+
+        /* Make sure the physical address can be encoded in the register. */
+        if ( (virt_to_maddr(table) & ~GENMASK(51, 12)) )
+        {

xen/arch/arm/gic-v3-lpi.c line 134

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+            free_xenheap_pages(table, 0);
+            return -ERANGE;
+        }
+        memset(table, GIC_PRI_IRQ | LPI_PROP_RES1, MAX_PHYS_LPIS);
+        clean_and_invalidate_dcache_va_range(table, MAX_PHYS_LPIS);
+        lpi_data.lpi_property = table;
+    }
+
+    /* Encode the number of bits needed, minus one */
+    reg |= ((fls(lpi_data.nr_host_lpis - 1) - 1) << 0);

As said on the previous version, please avoid hardcoded shift.

[...]

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c new file +9 -0
+int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+    int ret;
+
+    /* We don't support LPIs without an ITS. */
+    if ( !gicv3_its_host_has_its() )
+        return -ENODEV;

xen/arch/arm/gic-v3-lpi.c line 177

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+
+    /* Make sure LPIs are disabled before setting up the tables. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    if ( reg & GICR_CTLR_ENABLE_LPIS )
+        return -EBUSY;
+
+    ret = gicv3_lpi_allocate_pendtable(&table_reg);
+    if (ret)
+        return ret;
+    writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);

Same question as on the previous version. The cacheability and shareability may not stick. This was a bug fix in Linux (see commit 241a386) and I am struggling to understand why Xen would not be affected?

xen/arch/arm/gic-v3-lpi.c line 186

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c new file +9 -0
+
+    return gicv3_lpi_set_proptable(rdist_base);
+}
+
+static unsigned int max_lpi_bits = CONFIG_MAX_PHYS_LPI_BITS;
+integer_param("max_lpi_bits", max_lpi_bits);
+
+int gicv3_lpi_init_host_lpis(unsigned int hw_lpi_bits)
+{

Please add a comment to explain why you don't sanitize the value from the command line.

xen/arch/arm/gic-v3-lpi.c line 187

Quoted 1 line
xen/arch/arm/gic-v3-lpi.c new file +1 -0
+    lpi_data.nr_host_lpis = BIT_ULL(min(hw_lpi_bits, max_lpi_bits));

nr_host_lpis is "unsigned long" so why are you using BIT_ULL?

xen/arch/arm/gic-v3-lpi.c line 134

Quoted 2 lines
xen/arch/arm/gic-v3-lpi.c new file +2 -0
+
+    printk("GICv3: using at most %ld LPIs on the host.\n", MAX_PHYS_LPIS);

s/%ld/%lu/

[...]

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

Quoted 10 lines
xen/arch/arm/gic-v3.c +2 -0
index 1512521..ed78363 100644
@@ -548,6 +548,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);

Again, a macro has been suggested on in the last 2 versions to avoid hardcoded value. You said you will fix it and it is not done. Please do it.

[...]

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

Quoted 9 lines
xen/include/asm-arm/gic.h +1 -0
index 836a103..12bd155 100644
@@ -220,6 +220,8 @@ enum gic_version {
     GIC_V3,
 };
 
+#define LPI_OFFSET      8192

My comments on the previous version about LPI_OFFSET are not addressed. And one question was left unanswered.

Quoted 4 lines
xen/include/asm-arm/gic.h +1 -0
+
 extern enum gic_version gic_hw_version(void);
 
 /* Program the IRQ type into the GIC */

[...]

xen/include/asm-arm/gic_v3_its.h void gicv3_its_dt_init(const struct dt_device_node *node); · line 46

Quoted 12 lines
xen/include/asm-arm/gic_v3_its.h +4 -0
index 765a655..219d109 100644
@@ -40,6 +40,11 @@ void gicv3_its_dt_init(const struct dt_device_node *node);
 
 bool gicv3_its_host_has_its(void);
 
+int gicv3_lpi_init_rdist(void __iomem * rdist_base);
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);

In the implementation, the parameter is called "hw_lpi_bits". Please stay consistent.

Quoted 5 lines
xen/include/asm-arm/gic_v3_its.h +1 -0
+
 #else
 
 static LIST_HEAD(host_its_list);

xen/include/asm-arm/gic_v3_its.h static inline bool gicv3_its_host_has_its(void) · line 66

Quoted 9 lines
xen/include/asm-arm/gic_v3_its.h +6 -0
@@ -53,6 +58,15 @@ static inline bool gicv3_its_host_has_its(void)
     return false;
 }
 
+static inline int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    return -ENODEV;
+}
+
+static inline int gicv3_lpi_init_host_lpis(unsigned int nr_lpis)

Ditto.

Quoted 7 lines
xen/include/asm-arm/gic_v3_its.h +3 -0
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif
Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Hi,

Quoted 33 lines · Julien Grall

Hi Andre,

Quoted 23 lines · Andre Przywara
xen/arch/arm/Kconfig +8 -0
index bf64c61..86f7b53 100644
@@ -49,6 +49,21 @@ config HAS_ITS
         bool "GICv3 ITS MSI controller support"
         depends on HAS_GICV3
 
+config MAX_PHYS_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+        help
+          Specifies the maximum number of LPIs (in bits) Xen should take
+          care of. The host ITS may provide support for a very large

number
+ of supported LPIs, for all of which we may not want to
allocate

xen/arch/arm/Kconfig +4 -0
+          memory, so this number here allows to limit this.
+          Xen itself does not know how many LPIs domains will ever need
+          beforehand.
+          This can be overridden on the command line with the
Quoted 2 lines

max_lpi_bits
+ parameter.

I continue to disagree on this Kconfig option. There is no sensible default value and I don't see how a distribution will be able to pick-up one value here.

Quoted 2 lines

If the number of LPIs have to be restricted, this should be done via the command line or a per-platform option.

So are you opting for taking the hardware provided value, unless there is a command line option or a per-platform limit?

Please keep in mind that the situation here is different from the device table:
- There is no indirect table option for the property and pending table. Any redistributor supporting 32 bits worth of LPIs would lead to a 4GB property table and 512MB pending table allocation.
- An OS like Linux can make sensible assumptions about the number of LPIs needed and only allocate as much LPIs as required. Linux for instance uses at most 65536. Xen cannot easily make this decision. So we would need to go as high as possible, but not too high to not waste memory. I see different tradeoffs here between a distribution targeting servers or another one aiming at some embedded devices, for instance. So I would expect exactly this decision to be covered by a Kconfig option.
- In contrast to the device ID, which is guest provided, the host LPI number is chosen by Xen. We allocate them in chunks of 32, starting at the beginning and just counting up. So we can limit them without loosing too much flexibility, because it's not that sparse as device IDs, for instance.

Quoted 2 lines

I have already made my point on previous e-mails so I am not going to argue more.

So as I mentioned before, I am happy to loose the Kconfig option, but then we need some sensible default value. In our case we could be cheeky here for now and just use the Linux value, because a Linux Dom0 would be the only user. But that doesn't sound very future proof, though this may not matter for 4.9.

Quoted 54 lines
Quoted 41 lines
xen/arch/arm/Kconfig +1 -0
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/Makefile +1 -0
index 54860e0..02a8737 100644
@@ -19,6 +19,7 @@ obj-y += gic.o
 obj-y += gic-v2.o
 obj-$(CONFIG_HAS_GICV3) += gic-v3.o
 obj-$(CONFIG_HAS_ITS) += gic-v3-its.o
+obj-$(CONFIG_HAS_ITS) += gic-v3-lpi.o
 obj-y += guestcopy.o
 obj-y += hvm.o
 obj-y += io.o
xen/arch/arm/gic-v3-lpi.c new file +19 -0
new file mode 100644
index 0000000..4f8414b
@@ -0,0 +1,201 @@
+/*
+ * xen/arch/arm/gic-v3-lpi.c
+ *
+ * ARM GICv3 Locality-specific Peripheral Interrupts (LPI) support
+ *
+ * Copyright (C) 2016,2017 - ARM Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */

xen/arch/arm/gic-v3-lpi.c line 29

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+
+#include <xen/lib.h>
+#include <xen/mm.h>
+#include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
+#include <asm/io.h>
+#include <asm/page.h>
+
+#define LPI_PROPTABLE_NEEDS_FLUSHING    (1U << 0)

NIT: Newline here please.

Quoted 14 lines

xen/arch/arm/gic-v3-lpi.c line 40

Quoted 12 lines
xen/arch/arm/gic-v3-lpi.c new file +8 -0
+/* Global state */
+static struct {
+    /* The global LPI property table, shared by all redistributors. */
+    uint8_t *lpi_property;
+    /*
+     * Number of physical LPIs the host supports. This is a property of
+     * the GIC hardware. We depart from the habit of naming these things
+     * "physical" in Xen, as the GICv3/4 spec uses the term "physical

LPI"

xen/arch/arm/gic-v3-lpi.c new file +3 -0
+     * in a different context to differentiate them from "virtual LPIs".
+     */
+    unsigned long int nr_host_lpis;

Why unsigned long?

Because someone requested that I store the actual number of LPIs, not the number of bits required to encode them. So I need to cover the case of exactly 32 bits worth of LPIs, which u32 cannot hold.

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c new file +5 -0
+    unsigned int flags;
+} lpi_data;
+
+struct lpi_redist_data {
+    void                *pending_table;
Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c line 50

Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c new file +5 -0
+};
+
+static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
+
+#define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)

This is fairly confusing. When I read "nr_host_lpis" I understand that you store the number of LPIs. But in fact you store the maximum LPI ID.

One of the reasons I argued for storing the number of bits ....

Quoted 8 lines

Also it is very unclear the difference between MAX_PHYS_LPIS and nr_host_lpis and will likely cause trouble in the future.

So it looks like to me that nr_host_lpis should be named max_host_lpis_ids and MAX_PHYS_LPIS should be MAX_NR_PHYS_LPIS.

Also, please stay consistent with the naming. If you use host then use host everywhere and not a mix between phys and host for the same purpose.

OK, can do.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/gic-v3-lpi.c new file +11 -0
+
+static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
+{
+    uint64_t val;
+    unsigned int order;
+    void *pendtable;
+
+    if ( this_cpu(lpi_redist).pending_table )
+        return -EBUSY;
+
+    val  = GIC_BASER_CACHE_RaWaWb <<

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

xen/arch/arm/gic-v3-lpi.c new file +2 -0
+
+    /*
Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c line 69

Quoted 4 lines
xen/arch/arm/gic-v3-lpi.c new file +4 -0
+     * The pending table holds one bit per LPI and even covers bits for
+     * interrupt IDs below 8192, so we allocate the full range.
+     * The GICv3 imposes a 64KB alignment requirement, also requires
+     * physically contigious memory.

The bit after the comma seems quite obvious. Both xalloc and alloc_xenheap_pages will ensure that the region will be contiguous in the physical address space.

Mmh, somehow I missed that when I tried to decide which allocation function is the right one. But indeed xmalloc() eventually calls alloc_xenheap_pages() with the order of alignment, so we get the same guarantees.

Quoted 63 lines

[...]

Quoted 30 lines
xen/arch/arm/gic-v3-lpi.c new file +5 -0
+static int gicv3_lpi_set_proptable(void __iomem * rdist_base)
+{
+    uint64_t reg;
+
+    reg  = GIC_BASER_CACHE_RaWaWb <<

GICR_PROPBASER_INNER_CACHEABILITY_SHIFT;
+ reg |= GIC_BASER_CACHE_SameAsInner <<
GICR_PROPBASER_OUTER_CACHEABILITY_SHIFT;
+ reg |= GIC_BASER_InnerShareable <<
GICR_PROPBASER_SHAREABILITY_SHIFT;

xen/arch/arm/gic-v3-lpi.c new file +3 -0
+
+    /*
+     * The property table is shared across all redistributors, so

allocate

xen/arch/arm/gic-v3-lpi.c new file +6 -0
+     * this only once, but return the same value on subsequent calls.
+     */
+    if ( !lpi_data.lpi_property )
+    {
+        /* The property table holds one byte per LPI. */
+        unsigned int order =

get_order_from_bytes(lpi_data.nr_host_lpis);

xen/arch/arm/gic-v3-lpi.c new file +6 -0
+        void *table = alloc_xenheap_pages(order, 0);
+
+        if ( !table )
+            return -ENOMEM;
+
+        /* Make sure the physical address can be encoded in the

register. */

xen/arch/arm/gic-v3-lpi.c new file +2 -0
+        if ( (virt_to_maddr(table) & ~GENMASK(51, 12)) )
+        {

xen/arch/arm/gic-v3-lpi.c line 134

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c new file +10 -0
+            free_xenheap_pages(table, 0);
+            return -ERANGE;
+        }
+        memset(table, GIC_PRI_IRQ | LPI_PROP_RES1, MAX_PHYS_LPIS);
+        clean_and_invalidate_dcache_va_range(table, MAX_PHYS_LPIS);
+        lpi_data.lpi_property = table;
+    }
+
+    /* Encode the number of bits needed, minus one */
+    reg |= ((fls(lpi_data.nr_host_lpis - 1) - 1) << 0);

As said on the previous version, please avoid hardcoded shift.

[...]

Quoted 15 lines
xen/arch/arm/gic-v3-lpi.c new file +15 -0
+int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+    int ret;
+
+    /* We don't support LPIs without an ITS. */
+    if ( !gicv3_its_host_has_its() )
+        return -ENODEV;
+
+    /* Make sure LPIs are disabled before setting up the tables. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    if ( reg & GICR_CTLR_ENABLE_LPIS )
+        return -EBUSY;
+
Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c line 177

Quoted 4 lines
xen/arch/arm/gic-v3-lpi.c new file +4 -0
+    ret = gicv3_lpi_allocate_pendtable(&table_reg);
+    if (ret)
+        return ret;
+    writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);

Same question as on the previous version. The cacheability and shareability may not stick. This was a bug fix in Linux (see commit 241a386) and I am struggling to understand why Xen would not be affected?

Oh, OK, we need to remove cacheability if shareability is denied, though that sounds like a hardware bug to me if this is not observed by the redistributor.
The reason I didn't care about it here was that software doesn't use the pending table, so there would be nothing on the code side to be done in case the redistributor shoots down one of our requests.
The mentioned Linux patch covers all ITS tables and the property table as well.

Quoted 14 lines

xen/arch/arm/gic-v3-lpi.c line 186

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c new file +9 -0
+
+    return gicv3_lpi_set_proptable(rdist_base);
+}
+
+static unsigned int max_lpi_bits = CONFIG_MAX_PHYS_LPI_BITS;
+integer_param("max_lpi_bits", max_lpi_bits);
+
+int gicv3_lpi_init_host_lpis(unsigned int hw_lpi_bits)
+{

Please add a comment to explain why you don't sanitize the value from the command line.

Quoted 3 lines
Quoted 1 line

+ lpi_data.nr_host_lpis = BIT_ULL(min(hw_lpi_bits, max_lpi_bits));

nr_host_lpis is "unsigned long" so why are you using BIT_ULL?

Right, creeping over from Linux where the ITS is supported by ARM(32) as well.

Quoted 16 lines
Quoted 3 lines
xen/arch/arm/gic-v3-lpi.c new file +2 -0
+
+    printk("GICv3: using at most %ld LPIs on the host.\n",

MAX_PHYS_LPIS);

s/%ld/%lu/

[...]

Quoted 8 lines
xen/arch/arm/gic-v3.c +0 -0
index 1512521..ed78363 100644
@@ -548,6 +548,9 @@ static void __init gicv3_dist_init(void)
     type = readl_relaxed(GICD + GICD_TYPER);
     nr_lines = 32 * ((type & GICD_TYPE_LINES) + 1);
 
Quoted 6 lines

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

Quoted 3 lines
xen/arch/arm/gic-v3.c +2 -0
+    if ( type & GICD_TYPE_LPIS )
+        gicv3_lpi_init_host_lpis(((type >> GICD_TYPE_ID_BITS_SHIFT) &
 0x1f) + 1);

Again, a macro has been suggested on in the last 2 versions to avoid hardcoded value. You said you will fix it and it is not done. Please do it.

Sorry for that, I did this for GITS_TYPER_DEVICE_ID_BITS, so mentally ticked this one off as well.
Will be fixed.

Quoted 11 lines

[...]

Quoted 8 lines
xen/include/asm-arm/gic.h +0 -0
index 836a103..12bd155 100644
@@ -220,6 +220,8 @@ enum gic_version {
     GIC_V3,
 };
 
Quoted 4 lines

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

Quoted 1 line
xen/include/asm-arm/gic.h +1 -0
+#define LPI_OFFSET      8192

My comments on the previous version about LPI_OFFSET are not addressed. And one question was left unanswered.

Do you mean this?

Quoted 2 lines
Quoted 2 lines

It would make much sense to have this definition moved in irq.h close to NR_IRQS.

Admittedly I missed that over the next question...
I think I wasn't convinced in the first place to put this GIC specific detail into the generic irq.h, but I see that it makes sense since we have the do_LPI() prototype in there as well.

Quoted 2 lines
Quoted 2 lines

Also, I am a bit surprised that NR_IRQS & co has not been modified. Is there any reason for that?

But I answered on this one.
Is there anything else you want to have answered?

Quoted 18 lines
Quoted 5 lines

+
extern enum gic_version gic_hw_version(void);

/* Program the IRQ type into the GIC */

[...]

Quoted 10 lines
xen/include/asm-arm/gic_v3_its.h +0 -0
index 765a655..219d109 100644
@@ -40,6 +40,11 @@ void gicv3_its_dt_init(const struct dt_device_node
 *node);
 
 bool gicv3_its_host_has_its(void);
 
Quoted 7 lines

xen/include/asm-arm/gic_v3_its.h void gicv3_its_dt_init(const struct dt_device_node *node); · line 46

Quoted 4 lines
xen/include/asm-arm/gic_v3_its.h +4 -0
+int gicv3_lpi_init_rdist(void __iomem * rdist_base);
+
+/* Initialize the host structures for LPIs. */
+int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);

In the implementation, the parameter is called "hw_lpi_bits". Please stay consistent.

Yeah, this header file is a constant source of merge conflicts when squashing fixes and reworks, which makes it a pain to work with. Sorry for that.

Cheers,
Andre.

Quoted 17 lines
Quoted 5 lines
xen/include/asm-arm/gic_v3_its.h +1 -0
+
 #else
 

static LIST_HEAD(host_its_list);

xen/include/asm-arm/gic_v3_its.h static inline bool gicv3_its_host_has_its(void) · line 66

Quoted 9 lines
xen/include/asm-arm/gic_v3_its.h +6 -0
@@ -53,6 +58,15 @@ static inline bool gicv3_its_host_has_its(void)
     return false;
 }
 
+static inline int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    return -ENODEV;
+}
+
+static inline int gicv3_lpi_init_host_lpis(unsigned int nr_lpis)

Ditto.

Quoted 8 lines

xen/include/asm-arm/gic_v3_its.h static inline bool gicv3_its_host_has_its(void) · line 72

Quoted 7 lines
xen/include/asm-arm/gic_v3_its.h +3 -0
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif

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

#13 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 41 lines · Julien Grall
Quoted 33 lines

Hi Andre,

Quoted 23 lines · Andre Przywara
xen/arch/arm/Kconfig +8 -0
index bf64c61..86f7b53 100644
@@ -49,6 +49,21 @@ config HAS_ITS
         bool "GICv3 ITS MSI controller support"
         depends on HAS_GICV3
 
+config MAX_PHYS_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+        help
+          Specifies the maximum number of LPIs (in bits) Xen should take
+          care of. The host ITS may provide support for a very large

number
+ of supported LPIs, for all of which we may not want to
allocate

xen/arch/arm/Kconfig +4 -0
+          memory, so this number here allows to limit this.
+          Xen itself does not know how many LPIs domains will ever need
+          beforehand.
+          This can be overridden on the command line with the
Quoted 2 lines

max_lpi_bits
+ parameter.

I continue to disagree on this Kconfig option. There is no sensible default value and I don't see how a distribution will be able to pick-up one value here.

Quoted 2 lines

If the number of LPIs have to be restricted, this should be done via the command line or a per-platform option.

So are you opting for taking the hardware provided value, unless there is a command line option or a per-platform limit?

Quoted 13 lines

Please keep in mind that the situation here is different from the device table:
- There is no indirect table option for the property and pending table. Any redistributor supporting 32 bits worth of LPIs would lead to a 4GB property table and 512MB pending table allocation.
- An OS like Linux can make sensible assumptions about the number of LPIs needed and only allocate as much LPIs as required. Linux for instance uses at most 65536. Xen cannot easily make this decision. So we would need to go as high as possible, but not too high to not waste memory. I see different tradeoffs here between a distribution targeting servers or another one aiming at some embedded devices, for instance. So I would expect exactly this decision to be covered by a Kconfig option.

Hence why a parameter on the command line is the better place for that. The decision is left to the user.

Quoted 8 lines
Quoted 2 lines

I have already made my point on previous e-mails so I am not going to argue more.

So as I mentioned before, I am happy to loose the Kconfig option, but then we need some sensible default value. In our case we could be cheeky here for now and just use the Linux value, because a Linux Dom0 would be the only user. But that doesn't sound very future proof, though this may not matter for 4.9.

I don't think we need a sensible default value and IHMO there is none. I would left the user to decide the exact number.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
xen/arch/arm/Kconfig +6 -0
+/* Global state */
+static struct {
+    /* The global LPI property table, shared by all redistributors. */
+    uint8_t *lpi_property;
+    /*
+     * Number of physical LPIs the host supports. This is a property of
Quoted 8 lines

xen/arch/arm/Kconfig

Quoted 6 lines
xen/arch/arm/Kconfig +2 -0
+     * the GIC hardware. We depart from the habit of naming these things
+     * "physical" in Xen, as the GICv3/4 spec uses the term "physical

LPI"

xen/arch/arm/Kconfig +3 -0
+     * in a different context to differentiate them from "virtual LPIs".
+     */
+    unsigned long int nr_host_lpis;

Why unsigned long?

Quoted 3 lines

Because someone requested that I store the actual number of LPIs, not the number of bits required to encode them. So I need to cover the case of exactly 32 bits worth of LPIs, which u32 cannot hold.

Fair enough.

[...]

Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
xen/arch/arm/Kconfig +15 -0
+int gicv3_lpi_init_rdist(void __iomem * rdist_base)
+{
+    uint32_t reg;
+    uint64_t table_reg;
+    int ret;
+
+    /* We don't support LPIs without an ITS. */
+    if ( !gicv3_its_host_has_its() )
+        return -ENODEV;
+
+    /* Make sure LPIs are disabled before setting up the tables. */
+    reg = readl_relaxed(rdist_base + GICR_CTLR);
+    if ( reg & GICR_CTLR_ENABLE_LPIS )
+        return -EBUSY;
+
Quoted 8 lines

xen/arch/arm/Kconfig

Quoted 4 lines
xen/arch/arm/Kconfig +4 -0
+    ret = gicv3_lpi_allocate_pendtable(&table_reg);
+    if (ret)
+        return ret;
+    writeq_relaxed(table_reg, rdist_base + GICR_PENDBASER);

Same question as on the previous version. The cacheability and shareability may not stick. This was a bug fix in Linux (see commit 241a386) and I am struggling to understand why Xen would not be affected?

Quoted 8 lines

Oh, OK, we need to remove cacheability if shareability is denied, though that sounds like a hardware bug to me if this is not observed by the redistributor.
The reason I didn't care about it here was that software doesn't use the pending table, so there would be nothing on the code side to be done in case the redistributor shoots down one of our requests.
The mentioned Linux patch covers all ITS tables and the property table as well.

The patch was sent by Marc and I trust him to do the right things. Looking at the commit message, it is to avoid cacheable traffic. Unless you give me a good reason for not doing that, please do the change.

I don't want to end-up digging into weird bug later on because we decided that "the hardware is not supposed to do that".

[...]

Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
xen/include/asm-arm/gic.h +0 -0
index 836a103..12bd155 100644
@@ -220,6 +220,8 @@ enum gic_version {
     GIC_V3,
 };
 
Quoted 4 lines

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

Quoted 1 line
xen/include/asm-arm/gic.h +1 -0
+#define LPI_OFFSET      8192

My comments on the previous version about LPI_OFFSET are not addressed. And one question was left unanswered.

Do you mean this?

Quoted 2 lines
Quoted 2 lines

It would make much sense to have this definition moved in irq.h close to NR_IRQS.

Admittedly I missed that over the next question...
I think I wasn't convinced in the first place to put this GIC specific detail into the generic irq.h, but I see that it makes sense since we have the do_LPI() prototype in there as well.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Also, I am a bit surprised that NR_IRQS & co has not been modified. Is there any reason for that?

But I answered on this one.
Is there anything else you want to have answered?

Yes, the question in <[email protected]>.

"Can we have a comment on top of NR_IRQS explaining why LPIs are not taken into account?"

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

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

Hi,

Hi Andre,

Quoted 42 lines · Julien Grall
Quoted 34 lines

Hi Andre,

Quoted 24 lines · Andre Przywara
xen/arch/arm/Kconfig +7 -0
index bf64c61..86f7b53 100644
@@ -49,6 +49,21 @@ config HAS_ITS
         bool "GICv3 ITS MSI controller support"
         depends on HAS_GICV3
 
+config MAX_PHYS_LPI_BITS
+        depends on HAS_ITS
+        int "Maximum bits for GICv3 host LPIs (14-32)"
+        range 14 32
+        default "20"
+        help
+          Specifies the maximum number of LPIs (in bits) Xen should

take
+ care of. The host ITS may provide support for a very large
number
+ of supported LPIs, for all of which we may not want to
allocate

xen/arch/arm/Kconfig +4 -0
+          memory, so this number here allows to limit this.
+          Xen itself does not know how many LPIs domains will ever need
+          beforehand.
+          This can be overridden on the command line with the
Quoted 2 lines

max_lpi_bits
+ parameter.

I continue to disagree on this Kconfig option. There is no sensible default value and I don't see how a distribution will be able to pick-up one value here.

Quoted 2 lines

If the number of LPIs have to be restricted, this should be done via the command line or a per-platform option.

So are you opting for taking the hardware provided value, unless there is a command line option or a per-platform limit?

Quoted 13 lines

Please keep in mind that the situation here is different from the device table:
- There is no indirect table option for the property and pending table. Any redistributor supporting 32 bits worth of LPIs would lead to a 4GB property table and 512MB pending table allocation.
- An OS like Linux can make sensible assumptions about the number of LPIs needed and only allocate as much LPIs as required. Linux for instance uses at most 65536. Xen cannot easily make this decision. So we would need to go as high as possible, but not too high to not waste memory. I see different tradeoffs here between a distribution targeting servers or another one aiming at some embedded devices, for instance. So I would expect exactly this decision to be covered by a Kconfig option.

Hence why a parameter on the command line is the better place for that. The decision is left to the user.

Quoted 3 lines
Quoted 2 lines

I have already made my point on previous e-mails so I am not going to argue more.

Quoted 8 lines
Quoted 5 lines

So as I mentioned before, I am happy to loose the Kconfig option, but then we need some sensible default value. In our case we could be cheeky here for now and just use the Linux value, because a Linux Dom0 would be the only user. But that doesn't sound very future proof, though this may not matter for 4.9.

I don't think we need a sensible default value and IHMO there is none. I would left the user to decide the exact number.

In that case, the command line parameter becomes mandatory: we need to force the user to specify it as we do for dom0_mem today.

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

#15 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Hi Stefano,

Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · Julien Grall
Quoted 5 lines

So as I mentioned before, I am happy to loose the Kconfig option, but then we need some sensible default value. In our case we could be cheeky here for now and just use the Linux value, because a Linux Dom0 would be the only user. But that doesn't sound very future proof, though this may not matter for 4.9.

Quoted 5 lines
Quoted 2 lines

I don't think we need a sensible default value and IHMO there is none. I would left the user to decide the exact number.

In that case, the command line parameter becomes mandatory: we need to force the user to specify it as we do for dom0_mem today.

Not really. We should use the hardware value by default. If a user thinks the number allocated is too big for his use case, then it can limit using the command line.

Anyway, I will not oppose to make this command option mandatory when ITS is in use.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Quoted 17 lines · Julien Grall

Hi Stefano,

Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · Julien Grall
Quoted 5 lines

So as I mentioned before, I am happy to loose the Kconfig option, but then we need some sensible default value. In our case we could be cheeky here for now and just use the Linux value, because a Linux Dom0 would be the only user. But that doesn't sound very future proof, though this may not matter for 4.9.

Quoted 6 lines
Quoted 3 lines

I don't think we need a sensible default value and IHMO there is none. I would
left the user to decide the exact number.

In that case, the command line parameter becomes mandatory: we need to force the user to specify it as we do for dom0_mem today.

Quoted 6 lines

Not really. We should use the hardware value by default. If a user thinks the number allocated is too big for his use case, then it can limit using the command line.

Anyway, I will not oppose to make this command option mandatory when ITS is in use.

Andre wrote:

Any redistributor supporting 32 bits worth of LPIs would lead to a 4GB property table and 512MB pending table allocation.

Let's assume that such scenario is realistic (if it is not, then this discussion is fruitless), in this case the user most surely is not going to want to use the hardware provided value. But how can she knows it? How can she find out that she is wasting too much memory on her system? Is there an easy and obvious way to know?

She could find out if Xen printed a big warning such as:

USING 4G OF MEMORY FOR ITS PROPTABLE, CONSIDER PASSING max_lpi_bits to XEN

but to do that, we need a threshold value in Xen, above which the hypervisor prints the warning. But if we have a threshold value in Xen, then we might as well consider making it the default ceiling: Xen uses the hardware provided value, unless it's greater than threshold, in that case it uses threshold and prints a warning, for example:

LIMITING ITS PROPTABLE MEMORY TO 1G, CHANGE IT WITH max_lpi_bits PARAMETER

Does it make sense?

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

#17 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Hi,

Quoted 46 lines · Stefano Stabellini
Quoted 17 lines · Julien Grall

Hi Stefano,

Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · Julien Grall
Quoted 5 lines

So as I mentioned before, I am happy to loose the Kconfig option, but then we need some sensible default value. In our case we could be cheeky here for now and just use the Linux value, because a Linux Dom0 would be the only user. But that doesn't sound very future proof, though this may not matter for 4.9.

Quoted 6 lines
Quoted 3 lines

I don't think we need a sensible default value and IHMO there is none. I would
left the user to decide the exact number.

In that case, the command line parameter becomes mandatory: we need to force the user to specify it as we do for dom0_mem today.

Quoted 6 lines

Not really. We should use the hardware value by default. If a user thinks the number allocated is too big for his use case, then it can limit using the command line.

Anyway, I will not oppose to make this command option mandatory when ITS is in use.

Andre wrote:

Any redistributor supporting 32 bits worth of LPIs would lead to a 4GB property table and 512MB pending table allocation.

Let's assume that such scenario is realistic (if it is not, then this discussion is fruitless), in this case the user most surely is not going to want to use the hardware provided value. But how can she knows it? How can she find out that she is wasting too much memory on her system? Is there an easy and obvious way to know?

She could find out if Xen printed a big warning such as:

USING 4G OF MEMORY FOR ITS PROPTABLE, CONSIDER PASSING max_lpi_bits to XEN

but to do that, we need a threshold value in Xen, above which the hypervisor prints the warning. But if we have a threshold value in Xen, then we might as well consider making it the default ceiling: Xen uses the hardware provided value, unless it's greater than threshold, in that case it uses threshold and prints a warning, for example:

Quoted 4 lines

LIMITING ITS PROPTABLE MEMORY TO 1G, CHANGE IT WITH max_lpi_bits PARAMETER

Does it make sense?

Yes, that is exactly what I was after.
In contrast to the number of device IDs I think the number of LPI bits is _not_ a value that software should use directly, it's more a limit, actually a GICv3 implementation choice (how wide the LPI ID fields internally are, for instance).

So do we want to limit to 1GB and warn starting at 256MB?

Cheers,
Andre.

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

#18 lore raw

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Hi Andre,

Quoted 54 lines · Andre Przywara

Hi,

Quoted 46 lines · Stefano Stabellini
Quoted 17 lines · Julien Grall

Hi Stefano,

Quoted 7 lines · Stefano Stabellini
Quoted 6 lines · Julien Grall
Quoted 5 lines

So as I mentioned before, I am happy to loose the Kconfig option, but then we need some sensible default value. In our case we could be cheeky here for now and just use the Linux value, because a Linux Dom0 would be the only user. But that doesn't sound very future proof, though this may not matter for 4.9.

Quoted 6 lines
Quoted 3 lines

I don't think we need a sensible default value and IHMO there is none. I would
left the user to decide the exact number.

In that case, the command line parameter becomes mandatory: we need to force the user to specify it as we do for dom0_mem today.

Quoted 6 lines

Not really. We should use the hardware value by default. If a user thinks the number allocated is too big for his use case, then it can limit using the command line.

Anyway, I will not oppose to make this command option mandatory when ITS is in use.

Andre wrote:

Any redistributor supporting 32 bits worth of LPIs would lead to a 4GB property table and 512MB pending table allocation.

Let's assume that such scenario is realistic (if it is not, then this discussion is fruitless), in this case the user most surely is not going to want to use the hardware provided value. But how can she knows it? How can she find out that she is wasting too much memory on her system? Is there an easy and obvious way to know?

She could find out if Xen printed a big warning such as:

USING 4G OF MEMORY FOR ITS PROPTABLE, CONSIDER PASSING max_lpi_bits to XEN

but to do that, we need a threshold value in Xen, above which the hypervisor prints the warning. But if we have a threshold value in Xen, then we might as well consider making it the default ceiling: Xen uses the hardware provided value, unless it's greater than threshold, in that case it uses threshold and prints a warning, for example:

Quoted 4 lines

LIMITING ITS PROPTABLE MEMORY TO 1G, CHANGE IT WITH max_lpi_bits PARAMETER

Does it make sense?

Quoted 7 lines

Yes, that is exactly what I was after.
In contrast to the number of device IDs I think the number of LPI bits is _not_ a value that software should use directly, it's more a limit, actually a GICv3 implementation choice (how wide the LPI ID fields internally are, for instance).

So do we want to limit to 1GB and warn starting at 256MB?

I think Stefano was suggesting to make the command line mandatory.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 02/27] ARM: GICv3: allocate LPI pending and property table

Quoted 73 lines · Julien Grall

Hi Andre,

Quoted 70 lines · Andre Przywara

Hi,

Quoted 55 lines · Stefano Stabellini
Quoted 28 lines · Julien Grall

Hi Stefano,

Quoted 11 lines · Stefano Stabellini
Quoted 10 lines · Julien Grall
Quoted 9 lines

So as I mentioned before, I am happy to loose the Kconfig option, but
then we need some sensible default value. In our case we could be cheeky
here for now and just use the Linux value, because a Linux Dom0 would be
the only user. But that doesn't sound very future proof, though this may
not matter for 4.9.

Quoted 7 lines
Quoted 4 lines

I don't think we need a sensible default value and IHMO there is none. I
would
left the user to decide the exact number.

In that case, the command line parameter becomes mandatory: we need to force the user to specify it as we do for dom0_mem today.

Not really. We should use the hardware value by default. If a user thinks the
number allocated is too big for his use case, then it can limit using the
command line.

Quoted 3 lines

Anyway, I will not oppose to make this command option mandatory when ITS is in
use.

Andre wrote:

Any redistributor supporting 32 bits worth of LPIs would lead to a 4GB property table and 512MB pending table allocation.

Let's assume that such scenario is realistic (if it is not, then this discussion is fruitless), in this case the user most surely is not going to want to use the hardware provided value. But how can she knows it? How can she find out that she is wasting too much memory on her system? Is there an easy and obvious way to know?

She could find out if Xen printed a big warning such as:

USING 4G OF MEMORY FOR ITS PROPTABLE, CONSIDER PASSING max_lpi_bits to
XEN

but to do that, we need a threshold value in Xen, above which the hypervisor prints the warning. But if we have a threshold value in Xen, then we might as well consider making it the default ceiling: Xen uses the hardware provided value, unless it's greater than threshold, in that case it uses threshold and prints a warning, for example:

Quoted 5 lines

LIMITING ITS PROPTABLE MEMORY TO 1G, CHANGE IT WITH max_lpi_bits
PARAMETER

Does it make sense?

Yes, that is exactly what I was after.
In contrast to the number of device IDs I think the number of LPI bits is _not_ a value that software should use directly, it's more a limit, actually a GICv3 implementation choice (how wide the LPI ID fields internally are, for instance).

Quoted 3 lines
Quoted 1 line

So do we want to limit to 1GB and warn starting at 256MB?

I think Stefano was suggesting to make the command line mandatory.

Either way works, but the idea is that the user should not be surprised by the amount of memory utilized because either

1) she chose max_lpi_bits
2) Xen printed a WARNING at boot impossible to miss

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

#20 lore raw

PATCH v2 3/27 ARM: GICv3 ITS: allocate device and collection table

In reply to Andre Przywara

Each ITS maps a pair of a DeviceID (for instance derived from a 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 to the ITS.
The maximum number of devices is limited to a compile-time constant exposed in Kconfig.

---
 docs/misc/xen-command-line.markdown |   8 ++
 xen/arch/arm/Kconfig                |  14 ++++
 xen/arch/arm/gic-v3-its.c           | 163 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c               |   3 +
 xen/include/asm-arm/gic_v3_its.h    |  63 +++++++++++++-
 5 files changed, 250 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +8 -0
index 619016d..068d116 100644
@@ -1158,6 +1158,14 @@ based interrupts. Any higher IRQs will be available for use via PCI MSI.
 ### maxcpus
 > `= <integer>`
 
+### max\_its\_device\_bits
+> `= <integer>`
+
+Specifies the maximum number of devices using MSIs on the ARM GICv3 ITS
+controller to allocate table entries for. Each table entry uses a hardware
+specific size, typically 8 or 16 bytes.
+Defaults to 10 bits (to cover at most 1024 devices).
+
 ### max\_lpi\_bits
 > `= <integer>`
 
xen/arch/arm/Kconfig +14 -0
index 86f7b53..0d50156 100644
@@ -64,6 +64,20 @@ config MAX_PHYS_LPI_BITS
           This can be overridden on the command line with the max_lpi_bits
           parameter.
 
+config MAX_PHYS_ITS_DEVICE_BITS
+        depends on HAS_ITS
+        int "Number of device bits the ITS supports"
+        range 1 32
+        default "10"
+        help
+          Specifies the maximum number of devices which want to use the ITS.
+          Xen needs to allocates memory for the whole range very early.
+          The allocation scheme may be sparse, so a much larger number must
+          be supported to cover devices with a high bus number or those on
+          separate bus segments.
+          This can be overridden on the command line with the
+          max_its_device_bits parameter.
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/gic-v3-its.c +163 -0

177 lines not on this page · raw message

xen/arch/arm/gic-v3.c +3 -0
index ed78363..cc1e219 100644
@@ -1590,6 +1590,9 @@ static int __init gicv3_init(void)
     spin_lock(&gicv3.lock);
 
     gicv3_dist_init();
+    res = gicv3_its_init();
+    if ( res )
+        printk(XENLOG_WARNING "GICv3: ITS: initialization failed: %d\n", res);
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 
xen/include/asm-arm/gic_v3_its.h +62 -1

88 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
#21 lore raw

Re: [PATCH v2 03/27] ARM: GICv3 ITS: allocate device and collection table

Quoted 202 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (for instance derived from a 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 to the ITS.
The maximum number of devices is limited to a compile-time constant exposed in Kconfig.

---
 docs/misc/xen-command-line.markdown |   8 ++
 xen/arch/arm/Kconfig                |  14 ++++
 xen/arch/arm/gic-v3-its.c           | 163 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c               |   3 +
 xen/include/asm-arm/gic_v3_its.h    |  63 +++++++++++++-
 5 files changed, 250 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +8 -0
index 619016d..068d116 100644
@@ -1158,6 +1158,14 @@ based interrupts. Any higher IRQs will be available for use via PCI MSI.
 ### maxcpus
 > `= <integer>`
 
+### max\_its\_device\_bits
+> `= <integer>`
+
+Specifies the maximum number of devices using MSIs on the ARM GICv3 ITS
+controller to allocate table entries for. Each table entry uses a hardware
+specific size, typically 8 or 16 bytes.
+Defaults to 10 bits (to cover at most 1024 devices).
+
 ### max\_lpi\_bits
 > `= <integer>`
 
xen/arch/arm/Kconfig +14 -0
index 86f7b53..0d50156 100644
@@ -64,6 +64,20 @@ config MAX_PHYS_LPI_BITS
           This can be overridden on the command line with the max_lpi_bits
           parameter.
 
+config MAX_PHYS_ITS_DEVICE_BITS
+        depends on HAS_ITS
+        int "Number of device bits the ITS supports"
+        range 1 32
+        default "10"
+        help
+          Specifies the maximum number of devices which want to use the ITS.
+          Xen needs to allocates memory for the whole range very early.
+          The allocation scheme may be sparse, so a much larger number must
+          be supported to cover devices with a high bus number or those on
+          separate bus segments.
+          This can be overridden on the command line with the
+          max_its_device_bits parameter.
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/gic-v3-its.c +123 -0
index 4056e5b..9982fe9 100644
@@ -19,8 +19,10 @@
  */
 
 #include <xen/lib.h>
+#include <xen/mm.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
+#include <asm/io.h>
 
 LIST_HEAD(host_its_list);
 
@@ -29,6 +31,167 @@ bool gicv3_its_host_has_its(void)
     return !list_empty(&host_its_list);
 }
 
+#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(58, 56) | GENMASK(52, 48))
+
+/* Check that the physical address can be encoded in the PROPBASER register. */
+static bool check_propbaser_phys_addr(void *vaddr, unsigned int page_bits)
+{
+    paddr_t paddr = virt_to_maddr(vaddr);
+
+    return (!(paddr & ~GENMASK(page_bits < 16 ? 47 : 51, page_bits)));
+}
+
+static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits)
+{
+    uint64_t ret = addr & GENMASK(47, page_bits);
+
+    if ( page_bits < 16 )
+        return ret;
+
+    /* For 64K pages address bits 51-48 are encoded in bits 15-12. */
+    return ret | ((addr & GENMASK(51, 48)) >> (48 - 12));
+}
+
+/* The ITS BASE registers work with page sizes of 4K, 16K or 64K. */
+#define BASER_PAGE_BITS(sz) ((sz) * 2 + 12)
+
+static int its_map_baser(void __iomem *basereg, uint64_t regc,
+                         unsigned int nr_items)
+{
+    uint64_t attr, reg;
+    unsigned int entry_size = GITS_BASER_ENTRY_SIZE(regc);
+    unsigned int pagesz = 2, order, table_size;
+    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;
+
+    /*
+     * Setup the BASE register with the attributes that we like. Then read
+     * it back and see what sticks (page size, cacheability and shareability
+     * attributes), retrying if necessary.
+     */
+retry:
+    table_size = ROUNDUP(nr_items * entry_size, BIT(BASER_PAGE_BITS(pagesz)));
+    /* The BASE registers support at most 256 pages. */
+    table_size = min(table_size, 256U << BASER_PAGE_BITS(pagesz));
+    /* The memory block must be aligned to the requested page size. */
+    order = max(get_order_from_bytes(table_size), pagesz * 2);
+
+    buffer = alloc_xenheap_pages(order, 0);
+    if ( !buffer )
+        return -ENOMEM;
+
+    if ( !check_propbaser_phys_addr(buffer, BASER_PAGE_BITS(pagesz)) )
+    {
+        free_xenheap_pages(buffer, 0);
+        return -ERANGE;
+    }
+    memset(buffer, 0, table_size);
+
+    reg  = attr;
+    reg |= (pagesz << GITS_BASER_PAGE_SIZE_SHIFT);
+    reg |= (table_size >> BASER_PAGE_BITS(pagesz)) - 1;
+    reg |= regc & BASER_RO_MASK;
+    reg |= GITS_VALID_BIT;
+    reg |= encode_propbaser_phys_addr(virt_to_maddr(buffer),
+                                      BASER_PAGE_BITS(pagesz));
+
+    writeq_relaxed(reg, basereg);
+    regc = readq_relaxed(basereg);
+
+    /* The host didn't like our attributes, just use what it returned. */
+    if ( (regc & BASER_ATTR_MASK) != attr )
+    {
+        /* If we can't map it shareable, drop cacheability as well. */
+        if ( (regc & GITS_BASER_SHAREABILITY_MASK) == GIC_BASER_NonShareable )
+        {
+            regc &= ~GITS_BASER_INNER_CACHEABILITY_MASK;
+            writeq_relaxed(regc, basereg);
+        }
+        attr = regc & BASER_ATTR_MASK;
+    }
+    if ( (regc & GITS_BASER_INNER_CACHEABILITY_MASK) <= GIC_BASER_CACHE_nC )
+        clean_and_invalidate_dcache_va_range(buffer, table_size);
+
+    /* If the host accepted our page size, we are done. */
+    if ( ((regc >> GITS_BASER_PAGE_SIZE_SHIFT) & 0x3UL) == pagesz )
+        return 0;
+
+    free_xenheap_pages(buffer, order);
+
+    if ( pagesz-- > 0 )
+        goto retry;
+
+    /* None of the page sizes was accepted, give up */
+    return -EINVAL;
+}
+
+static unsigned int max_its_device_bits = CONFIG_MAX_PHYS_ITS_DEVICE_BITS;
+integer_param("max_its_device_bits", max_its_device_bits);
+
+static int gicv3_its_init_single_its(struct host_its *hw_its)
+{
+    uint64_t reg;
+    int i;
+    unsigned int devid_bits;
+
+    hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
+    if ( !hw_its->its_base )
+        return -ENOMEM;
+
+    reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
+    devid_bits = GITS_TYPER_DEVICE_ID_BITS(reg);
+    devid_bits = min(devid_bits, max_its_device_bits);
+
+    for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
+    {
+        void __iomem *basereg = hw_its->its_base + GITS_BASER0 + i * 8;

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 164

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+        unsigned int type;
+
+        reg = readq_relaxed(basereg);
+        type = (reg & GITS_BASER_TYPE_MASK) >> GITS_BASER_TYPE_SHIFT;
+        switch ( type )
+        {
+        case GITS_BASER_TYPE_NONE:
+            continue;
+        case GITS_BASER_TYPE_DEVICE:
+            its_map_baser(basereg, reg, BIT(devid_bits));

Check return value of its_map_baser?

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 167

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +3 -0
+            break;
 
+        case GITS_BASER_TYPE_COLLECTION:
+            its_map_baser(basereg, reg, NR_CPUS);

We should use num_possible_cpus() instead of NR_CPUS here

Quoted 143 lines
xen/arch/arm/gic-v3-its.c +27 -0
+            break;
+        /* In case this is a GICv4, provide a (dummy) vPE table as well. */
+        case GITS_BASER_TYPE_VCPU:
+            its_map_baser(basereg, reg, 1);
+            break;
+        default:
+            continue;
+        }
+    }
+
+    return 0;
+}
+
+int gicv3_its_init(void)
+{
+    struct host_its *hw_its;
+    int ret;
+
+    list_for_each_entry(hw_its, &host_its_list, entry) {
+        ret = gicv3_its_init_single_its(hw_its);
+        if ( ret )
+            return ret;
+    }
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)

{

xen/arch/arm/gic-v3.c +3 -0
index ed78363..cc1e219 100644
@@ -1590,6 +1590,9 @@ static int __init gicv3_init(void)
     spin_lock(&gicv3.lock);
 
     gicv3_dist_init();
+    res = gicv3_its_init();
+    if ( res )
+        printk(XENLOG_WARNING "GICv3: ITS: initialization failed: %d\n", res);
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 
xen/include/asm-arm/gic_v3_its.h +62 -1
index 219d109..a6c0acc 100644
@@ -20,6 +20,60 @@
 #ifndef __ASM_ARM_ITS_H__
 #define __ASM_ARM_ITS_H__
 
+#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_BASER_NR_REGS              8
+#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_VALID_BIT                  BIT_ULL(63)
+
+#define GITS_CTLR_QUIESCENT             BIT(31)
+#define GITS_CTLR_ENABLE                BIT(0)
+
+#define GITS_TYPER_DEVIDS_SHIFT         13
+#define GITS_TYPER_DEVIDS_MASK          (0x1fUL << GITS_TYPER_DEVIDS_SHIFT)
+#define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
+                                               GITS_TYPER_DEVIDS_SHIFT) + 1)
+
+#define GITS_IIDR_VALUE                 0x34c
+
+#define GITS_BASER_INDIRECT             BIT_ULL(62)
+#define GITS_BASER_INNER_CACHEABILITY_SHIFT        59
+#define GITS_BASER_TYPE_SHIFT           56
+#define GITS_BASER_TYPE_MASK            (7ULL << GITS_BASER_TYPE_SHIFT)
+#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_ENTRY_SIZE(reg)                                       \
+                        (((reg >> GITS_BASER_ENTRY_SIZE_SHIFT) & 0x1f) + 1)
+#define GITS_BASER_SHAREABILITY_SHIFT   10
+#define GITS_BASER_PAGE_SIZE_SHIFT      8
+#define GITS_BASER_RO_MASK              (GITS_BASER_TYPE_MASK | \
+                                        (31UL << GITS_BASER_ENTRY_SIZE_SHIFT) |\
+                                        GITS_BASER_INDIRECT)
+#define GITS_BASER_SHAREABILITY_MASK   (0x3ULL << GITS_BASER_SHAREABILITY_SHIFT)
+#define GITS_BASER_OUTER_CACHEABILITY_MASK   (0x7ULL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)
+#define GITS_BASER_INNER_CACHEABILITY_MASK   (0x7ULL << GITS_BASER_INNER_CACHEABILITY_SHIFT)
+
 #include <xen/device_tree.h>
 
 /* data structure for each hardware ITS */
@@ -28,6 +82,7 @@ struct host_its {
     const struct dt_device_node *dt_node;
     paddr_t addr;
     paddr_t size;
+    void __iomem *its_base;
 };
 
 
@@ -42,8 +97,9 @@ bool gicv3_its_host_has_its(void);
 
 int gicv3_lpi_init_rdist(void __iomem * rdist_base);
 
-/* Initialize the host structures for LPIs. */
+/* Initialize the host structures for LPIs and the host ITSes. */
 int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);
+int gicv3_its_init(void);
 
 #else
 
@@ -67,6 +123,11 @@ static inline int gicv3_lpi_init_host_lpis(unsigned int nr_lpis)
 {
     return 0;
 }
+
+static inline int gicv3_its_init(void)
+{
+    return 0;
+}
 #endif /* CONFIG_HAS_ITS */
 
 #endif
Signature · 1 line
2.9.0

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

#22 lore raw

Re: [PATCH v2 03/27] ARM: GICv3 ITS: allocate device and collection table

In reply to Andre Przywara

Hi Andre,

Quoted 23 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (for instance derived from a 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 to the ITS.
The maximum number of devices is limited to a compile-time constant exposed in Kconfig.

---
 docs/misc/xen-command-line.markdown |   8 ++
 xen/arch/arm/Kconfig                |  14 ++++
 xen/arch/arm/gic-v3-its.c           | 163 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c               |   3 +
 xen/include/asm-arm/gic_v3_its.h    |  63 +++++++++++++-
 5 files changed, 250 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +0 -0
index 619016d..068d116 100644

docs/misc/xen-command-line.markdown based interrupts. Any higher IRQs will be available for use via PCI MSI. · line 1167

Quoted 10 lines
docs/misc/xen-command-line.markdown +7 -0
@@ -1158,6 +1158,14 @@ based interrupts. Any higher IRQs will be available for use via PCI MSI.
 ### maxcpus
 > `= <integer>`
 
+### max\_its\_device\_bits
+> `= <integer>`
+
+Specifies the maximum number of devices using MSIs on the ARM GICv3 ITS
+controller to allocate table entries for. Each table entry uses a hardware
+specific size, typically 8 or 16 bytes.
+Defaults to 10 bits (to cover at most 1024 devices).

The description is misleading. Even if the platform has less than 1024 devices, 10 may not be enough if the Device ID are not contiguous.

However, I don't think this is useful. A user may not know the DevIDs in use of the platform and hence will not be able to choose a sensible value.

I still think that we should allocate what the hardware asked us and if it is necessary to limit this should be done per-platform.

Quoted 15 lines
docs/misc/xen-command-line.markdown +1 -0
+
 ### max\_lpi\_bits
 > `= <integer>`
 
xen/arch/arm/Kconfig +3 -0
index 86f7b53..0d50156 100644
@@ -64,6 +64,20 @@ config MAX_PHYS_LPI_BITS
           This can be overridden on the command line with the max_lpi_bits
           parameter.
 
+config MAX_PHYS_ITS_DEVICE_BITS
+        depends on HAS_ITS
+        int "Number of device bits the ITS supports"

xen/arch/arm/Kconfig config MAX_PHYS_LPI_BITS · line 79

Quoted 10 lines
xen/arch/arm/Kconfig +10 -0
+        range 1 32
+        default "10"
+        help
+          Specifies the maximum number of devices which want to use the ITS.
+          Xen needs to allocates memory for the whole range very early.
+          The allocation scheme may be sparse, so a much larger number must
+          be supported to cover devices with a high bus number or those on
+          separate bus segments.
+          This can be overridden on the command line with the
+          max_its_device_bits parameter.

From what I understand the default you suggested fit neither Cavium nor Qualcomm platform. It is also hard to see how a Distribution will be able to choose a sensible value here.

Quoted 8 lines
xen/arch/arm/Kconfig +1 -0
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/gic-v3-its.c +0 -0
index 4056e5b..9982fe9 100644

[...]

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 67

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+static int its_map_baser(void __iomem *basereg, uint64_t regc,
+                         unsigned int nr_items)
+{
+    uint64_t attr, reg;
+    unsigned int entry_size = GITS_BASER_ENTRY_SIZE(regc);
+    unsigned int pagesz = 2, order, table_size;

Please document what mean 2.

Quoted 9 lines
xen/arch/arm/gic-v3-its.c +9 -0
+    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;
+
+    /*
+     * Setup the BASE register with the attributes that we like. Then read
+     * it back and see what sticks (page size, cacheability and shareability

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 86

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+     * attributes), retrying if necessary.
+     */
+retry:
+    table_size = ROUNDUP(nr_items * entry_size, BIT(BASER_PAGE_BITS(pagesz)));
+    /* The BASE registers support at most 256 pages. */
+    table_size = min(table_size, 256U << BASER_PAGE_BITS(pagesz));
+    /* The memory block must be aligned to the requested page size. */
+    order = max(get_order_from_bytes(table_size), pagesz * 2);
+
+    buffer = alloc_xenheap_pages(order, 0);

Why don't you use _zalloc(...)? This would avoid to compute the order and drop few lines.

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 90

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    if ( !buffer )
+        return -ENOMEM;
+
+    if ( !check_propbaser_phys_addr(buffer, BASER_PAGE_BITS(pagesz)) )

The name of the function does not make sense. You check an address for the register BASER and not PROPBASER.

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    {
+        free_xenheap_pages(buffer, 0);
+        return -ERANGE;
+    }
+    memset(buffer, 0, table_size);

[...]

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 186

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+int gicv3_its_init(void)
+{
+    struct host_its *hw_its;
+    int ret;
+
+    list_for_each_entry(hw_its, &host_its_list, entry) {

Coding style:

list_for_each_entry(....)
{

Quoted 16 lines
xen/arch/arm/gic-v3-its.c +8 -0
+        ret = gicv3_its_init_single_its(hw_its);
+        if ( ret )
+            return ret;
+    }
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
xen/arch/arm/gic-v3.c +0 -0
index ed78363..cc1e219 100644

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

Quoted 6 lines
xen/arch/arm/gic-v3.c +3 -0
@@ -1590,6 +1590,9 @@ static int __init gicv3_init(void)
     spin_lock(&gicv3.lock);
 
     gicv3_dist_init();
+    res = gicv3_its_init();
+    if ( res )
+        printk(XENLOG_WARNING "GICv3: ITS: initialization failed: %d\n", res);

I would have expect a panic here because the ITS subsystem could be half initialized and it is not safe to continue.

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

Quoted 3 lines
xen/arch/arm/gic-v3.c +0 -0
     res = gicv3_cpu_init();
     gicv3_hyp_init();
 

Cheers,

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

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

#23 lore raw

Re: [PATCH v2 03/27] ARM: GICv3 ITS: allocate device and collection table

Hi,

Quoted 33 lines · Julien Grall

Hi Andre,

Quoted 30 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (for instance derived from a 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 to the ITS.
The maximum number of devices is limited to a compile-time constant exposed in Kconfig.

---
 docs/misc/xen-command-line.markdown |   8 ++
 xen/arch/arm/Kconfig                |  14 ++++
 xen/arch/arm/gic-v3-its.c           | 163
++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c               |   3 +
 xen/include/asm-arm/gic_v3_its.h    |  63 +++++++++++++-
 5 files changed, 250 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +0 -0
index 619016d..068d116 100644
@@ -1158,6 +1158,14 @@ based interrupts. Any higher IRQs will be

available for use via PCI MSI.
### maxcpus

Quoted 1 line

= <integer>

Quoted 11 lines

docs/misc/xen-command-line.markdown based interrupts. Any higher IRQs will be available for use via PCI MSI. · line 1167

Quoted 8 lines
docs/misc/xen-command-line.markdown +5 -0
+### max\_its\_device\_bits
+> `= <integer>`
+
+Specifies the maximum number of devices using MSIs on the ARM GICv3 ITS
+controller to allocate table entries for. Each table entry uses a

hardware

docs/misc/xen-command-line.markdown +2 -0
+specific size, typically 8 or 16 bytes.
+Defaults to 10 bits (to cover at most 1024 devices).

The description is misleading. Even if the platform has less than 1024 devices, 10 may not be enough if the Device ID are not contiguous.

Right.

Quoted 5 lines

However, I don't think this is useful. A user may not know the DevIDs in use of the platform and hence will not be able to choose a sensible value.

I still think that we should allocate what the hardware asked us and if it is necessary to limit this should be done per-platform.

I think you are right, a configurable limit does not make much sense. I was wondering if we should have a hard coded *memory* limit instead, to prevent ridiculously high allocations, say entry_size=8 and 32 bits of device IDs, which would result in 32GB of memory for a flat device table.

Or if we don't want to arbitrarily limit this, we always print the actual allocated size, maybe with a extra WARNING if it exceeds sane levels.

Quoted 25 lines
Quoted 25 lines
docs/misc/xen-command-line.markdown +1 -0
+
 ### max\_lpi\_bits
 > `= <integer>`
xen/arch/arm/Kconfig +0 -0
index 86f7b53..0d50156 100644
@@ -64,6 +64,20 @@ config MAX_PHYS_LPI_BITS
           This can be overridden on the command line with the

max_lpi_bits
parameter.

xen/arch/arm/Kconfig +7 -0
+config MAX_PHYS_ITS_DEVICE_BITS
+        depends on HAS_ITS
+        int "Number of device bits the ITS supports"
+        range 1 32
+        default "10"
+        help
+          Specifies the maximum number of devices which want to use

the ITS.

xen/arch/arm/Kconfig +2 -0
+          Xen needs to allocates memory for the whole range very early.
+          The allocation scheme may be sparse, so a much larger

number must
+ be supported to cover devices with a high bus number or

Quoted 8 lines

xen/arch/arm/Kconfig config MAX_PHYS_LPI_BITS · line 79

Quoted 4 lines

those on

xen/arch/arm/Kconfig +3 -0
+          separate bus segments.
+          This can be overridden on the command line with the
+          max_its_device_bits parameter.

From what I understand the default you suggested fit neither Cavium nor Qualcomm platform. It is also hard to see how a Distribution will be able to choose a sensible value here.

But it works for me (TM) ;-)
I was hoping that people would scream and suggest usable values instead (because I don't know their limits). But indeed lets just remove that in favor of something more future proof.

Quoted 39 lines
Quoted 8 lines
xen/arch/arm/Kconfig +1 -0
+
 endmenu
 
 menu "ARM errata workaround via the alternative framework"
xen/arch/arm/gic-v3-its.c +0 -0
index 4056e5b..9982fe9 100644

[...]

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 67

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+static int its_map_baser(void __iomem *basereg, uint64_t regc,
+                         unsigned int nr_items)
+{
+    uint64_t attr, reg;
+    unsigned int entry_size = GITS_BASER_ENTRY_SIZE(regc);
+    unsigned int pagesz = 2, order, table_size;

Please document what mean 2.

Quoted 19 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    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;

xen/arch/arm/gic-v3-its.c +3 -0
+
+    /*
+     * Setup the BASE register with the attributes that we like. Then

read
+ * it back and see what sticks (page size, cacheability and
shareability

xen/arch/arm/gic-v3-its.c +4 -0
+     * attributes), retrying if necessary.
+     */
+retry:
+    table_size = ROUNDUP(nr_items * entry_size,

BIT(BASER_PAGE_BITS(pagesz)));
+ /* The BASE registers support at most 256 pages. */

Quoted 8 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 86

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    table_size = min(table_size, 256U << BASER_PAGE_BITS(pagesz));
+    /* The memory block must be aligned to the requested page size. */
+    order = max(get_order_from_bytes(table_size), pagesz * 2);
+
+    buffer = alloc_xenheap_pages(order, 0);

Why don't you use _zalloc(...)? This would avoid to compute the order and drop few lines.

Because they need to be physically contiguous.
Please correct me if I am wrong here, but the normal *alloc functions do not guarantee this (from checking the implementation).

Quoted 7 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 90

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    if ( !buffer )
+        return -ENOMEM;
+
+    if ( !check_propbaser_phys_addr(buffer, BASER_PAGE_BITS(pagesz)) )

The name of the function does not make sense. You check an address for the register BASER and not PROPBASER.

Indeed.

Quoted 17 lines
Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    {
+        free_xenheap_pages(buffer, 0);
+        return -ERANGE;
+    }
+    memset(buffer, 0, table_size);

[...]

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 186

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+int gicv3_its_init(void)
+{
+    struct host_its *hw_its;
+    int ret;
+
+    list_for_each_entry(hw_its, &host_its_list, entry) {

Coding style:

Quoted 2 lines

list_for_each_entry(....)
{

Yeah, I was looking at the Linux driver at the same time ;-)

Quoted 19 lines
Quoted 19 lines
xen/arch/arm/gic-v3-its.c +8 -0
+        ret = gicv3_its_init_single_its(hw_its);
+        if ( ret )
+            return ret;
+    }
+
+    return 0;
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out

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

xen/arch/arm/gic-v3.c +0 -0
index ed78363..cc1e219 100644
@@ -1590,6 +1590,9 @@ static int __init gicv3_init(void)
     spin_lock(&gicv3.lock);
 
Quoted 8 lines

xen/arch/arm/gic-v3.c static int __init gicv3_init(void)

Quoted 5 lines
xen/arch/arm/gic-v3.c +3 -0
     gicv3_dist_init();
+    res = gicv3_its_init();
+    if ( res )
+        printk(XENLOG_WARNING "GICv3: ITS: initialization failed:
 %d\n", res);

I would have expect a panic here because the ITS subsystem could be half initialized and it is not safe to continue.

OK, let me check what actually happens here if there is no ITS ;-)

Cheers,
Andre

Quoted 7 lines

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

Quoted 4 lines
xen/arch/arm/gic-v3.c +0 -0
     res = gicv3_cpu_init();
     gicv3_hyp_init();

Cheers,

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

#24 lore raw

Re: [PATCH v2 03/27] ARM: GICv3 ITS: allocate device and collection table

Quoted 1 line · André Przywara

Hi,

Hi Andre,

Quoted 55 lines · Julien Grall
Quoted 33 lines

Hi Andre,

Quoted 30 lines · Andre Przywara

Each ITS maps a pair of a DeviceID (for instance derived from a 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 to the ITS.
The maximum number of devices is limited to a compile-time constant exposed in Kconfig.

---
 docs/misc/xen-command-line.markdown |   8 ++
 xen/arch/arm/Kconfig                |  14 ++++
 xen/arch/arm/gic-v3-its.c           | 163
++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c               |   3 +
 xen/include/asm-arm/gic_v3_its.h    |  63 +++++++++++++-
 5 files changed, 250 insertions(+), 1 deletion(-)
docs/misc/xen-command-line.markdown +0 -0
index 619016d..068d116 100644
@@ -1158,6 +1158,14 @@ based interrupts. Any higher IRQs will be

available for use via PCI MSI.
### maxcpus

Quoted 1 line

= <integer>

Quoted 11 lines

docs/misc/xen-command-line.markdown based interrupts. Any higher IRQs will be available for use via PCI MSI. · line 1167

Quoted 8 lines
docs/misc/xen-command-line.markdown +5 -0
+### max\_its\_device\_bits
+> `= <integer>`
+
+Specifies the maximum number of devices using MSIs on the ARM GICv3 ITS
+controller to allocate table entries for. Each table entry uses a

hardware

docs/misc/xen-command-line.markdown +2 -0
+specific size, typically 8 or 16 bytes.
+Defaults to 10 bits (to cover at most 1024 devices).

The description is misleading. Even if the platform has less than 1024 devices, 10 may not be enough if the Device ID are not contiguous.

Right.

Quoted 5 lines

However, I don't think this is useful. A user may not know the DevIDs in use of the platform and hence will not be able to choose a sensible value.

I still think that we should allocate what the hardware asked us and if it is necessary to limit this should be done per-platform.

Quoted 7 lines

I think you are right, a configurable limit does not make much sense. I was wondering if we should have a hard coded *memory* limit instead, to prevent ridiculously high allocations, say entry_size=8 and 32 bits of device IDs, which would result in 32GB of memory for a flat device table.

Or if we don't want to arbitrarily limit this, we always print the actual allocated size, maybe with a extra WARNING if it exceeds sane levels.

But what is a sane level? To be fair, I don't think this is the business of the common code to care about high allocations. If the platform ask 32bits and not able to cope, then it should be a workaround for the platform.

[...]

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
docs/misc/xen-command-line.markdown +4 -0
+    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;

docs/misc/xen-command-line.markdown +3 -0
+
+    /*
+     * Setup the BASE register with the attributes that we like. Then

read
+ * it back and see what sticks (page size, cacheability and
shareability

docs/misc/xen-command-line.markdown +4 -0
+     * attributes), retrying if necessary.
+     */
+retry:
+    table_size = ROUNDUP(nr_items * entry_size,

BIT(BASER_PAGE_BITS(pagesz)));
+ /* The BASE registers support at most 256 pages. */

Quoted 8 lines

docs/misc/xen-command-line.markdown

Quoted 5 lines
docs/misc/xen-command-line.markdown +5 -0
+    table_size = min(table_size, 256U << BASER_PAGE_BITS(pagesz));
+    /* The memory block must be aligned to the requested page size. */
+    order = max(get_order_from_bytes(table_size), pagesz * 2);
+
+    buffer = alloc_xenheap_pages(order, 0);

Why don't you use _zalloc(...)? This would avoid to compute the order and drop few lines.

Quoted 3 lines

Because they need to be physically contiguous.
Please correct me if I am wrong here, but the normal *alloc functions do not guarantee this (from checking the implementation).

_x*alloc will work the same way as k*alloc in Linux. The memory will be contiguous. Regarding the implementation details, _x*alloc is an overlay of alloc_xenheap_pages. For small size (i.e < PAGE_SIZE), it will coalesce in the same page. For bigger size, alloc_xenheap_pages will be used. Although, the resulting memory usage will differ because _x*alloc will free unused pages.

This is because alloc_xenheap_pages is working in term of order. So if you request 12K, alloc_xenheap_pages will allocate 16K. The implementation of _x*alloc will free the last 4K to avoid wasting space.

So effectively, _x*alloc will result in lower memory usage.

Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
     gicv3_dist_init();
docs/misc/xen-command-line.markdown +3 -0
+    res = gicv3_its_init();
+    if ( res )
+        printk(XENLOG_WARNING "GICv3: ITS: initialization failed:

%d\n", res);

I would have expect a panic here because the ITS subsystem could be half initialized and it is not safe to continue.

OK, let me check what actually happens here if there is no ITS ;-)

Technically, this message should not happen when there is no ITS because it is not mandatory to have one on the platform.

So this would be an coding error for me.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 03/27] ARM: GICv3 ITS: allocate device and collection table

Hi,

On 22/03/17 16:33, Julien Grall wrote:
[ ... ]

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
     gicv3_dist_init();
xen/arch/arm/gic-v3.c +3 -0
+    res = gicv3_its_init();
+    if ( res )
+        printk(XENLOG_WARNING "GICv3: ITS: initialization failed:

%d\n", res);

I would have expect a panic here because the ITS subsystem could be half initialized and it is not safe to continue.

Quoted 6 lines
Quoted 1 line

OK, let me check what actually happens here if there is no ITS ;-)

Technically, this message should not happen when there is no ITS because it is not mandatory to have one on the platform.

So this would be an coding error for me.

Having no ITS (node in the DT) would result in a empty host_its_list and a "0" return, so doesn't raise any issues.

Technically we could cope with one or all ITSes to not initialize (by not propagating them to any guests).
But for now I can just panic here, I guess, because it should point to some serious issue.
If there is a use case, we can always add a more relaxed behavior later.

Cheers,
Andre.

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

#26 lore raw

PATCH v2 4/27 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 in normal system memory 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-v3-its.c        | 57 ++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic_v3_its.h |  6 +++++
 2 files changed, 63 insertions(+)
xen/arch/arm/gic-v3-its.c +57 -0
index 9982fe9..e5601ed 100644
@@ -20,10 +20,13 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sizes.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 
+#define ITS_CMD_QUEUE_SZ                SZ_64K
+
 LIST_HEAD(host_its_list);
 
 bool gicv3_its_host_has_its(void)
@@ -56,6 +59,55 @@ static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits)
     return ret | ((addr & GENMASK(51, 48)) >> (48 - 12));
 }
 
+static void *its_map_cbaser(struct host_its *its)
+{
+    void __iomem *cbasereg = its->its_base + GITS_CBASER;
+    uint64_t reg;
+    void *buffer;
+    unsigned int order;
+
+    reg  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /* The ITS command buffer needs to be 64K aligned. */
+    order = max(get_order_from_bytes(ITS_CMD_QUEUE_SZ), 16U - PAGE_SHIFT);
+    buffer = alloc_xenheap_pages(order, 0);
+    if ( !buffer )
+        return NULL;
+
+    if ( virt_to_maddr(buffer) & ~GENMASK(51, 12) )
+    {
+        free_xenheap_pages(buffer, 0);
+        return NULL;
+    }
+    memset(buffer, 0, ITS_CMD_QUEUE_SZ);
+
+    reg |= GITS_VALID_BIT | virt_to_maddr(buffer);
+    reg |= ((ITS_CMD_QUEUE_SZ / SZ_4K) - 1) & GITS_CBASER_SIZE_MASK;
+    writeq_relaxed(reg, cbasereg);
+    reg = readq_relaxed(cbasereg);
+
+    /* If the ITS dropped shareability, drop cacheability as well. */
+    if ( (reg & GITS_BASER_SHAREABILITY_MASK) == 0 )
+    {
+        reg &= ~GITS_BASER_INNER_CACHEABILITY_MASK;
+        writeq_relaxed(reg, cbasereg);
+    }
+
+    /*
+     * If the command queue memory is mapped as uncached, we need to flush
+     * it on every access.
+     */
+    if ( !(reg & GITS_BASER_INNER_CACHEABILITY_MASK) )
+    {
+        its->flags |= HOST_ITS_FLUSH_CMD_QUEUE;
+        dprintk(XENLOG_WARNING, "using non-cacheable ITS command queue\n");
+    }
+
+    return buffer;
+}
+
 /* The ITS BASE registers work with page sizes of 4K, 16K or 64K. */
 #define BASER_PAGE_BITS(sz) ((sz) * 2 + 12)
 
@@ -175,6 +227,11 @@ static int gicv3_its_init_single_its(struct host_its *hw_its)
         }
     }
 
+    hw_its->cmd_buf = its_map_cbaser(hw_its);
+    if ( !hw_its->cmd_buf )
+        return -ENOMEM;
+    writeq_relaxed(0, hw_its->its_base + GITS_CWRITER);
+
     return 0;
 }
 
xen/include/asm-arm/gic_v3_its.h +6 -0
index a6c0acc..d5facf0 100644
@@ -74,8 +74,12 @@
 #define GITS_BASER_OUTER_CACHEABILITY_MASK   (0x7ULL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)
 #define GITS_BASER_INNER_CACHEABILITY_MASK   (0x7ULL << GITS_BASER_INNER_CACHEABILITY_SHIFT)
 
+#define GITS_CBASER_SIZE_MASK           0xff
+
 #include <xen/device_tree.h>
 
+#define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -83,6 +87,8 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    void *cmd_buf;
+    unsigned int flags;
 };
 
 
Signature · 7 lines
2.9.0


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

Re: [PATCH v2 04/27] ARM: GICv3 ITS: map ITS command buffer

Quoted 7 lines · Andre Przywara

Instead of directly manipulating the tables in memory, an ITS driver sends commands via a ring buffer in normal system memory 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.

Quoted 120 lines
---
 xen/arch/arm/gic-v3-its.c        | 57 ++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic_v3_its.h |  6 +++++
 2 files changed, 63 insertions(+)
xen/arch/arm/gic-v3-its.c +57 -0
index 9982fe9..e5601ed 100644
@@ -20,10 +20,13 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sizes.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 
+#define ITS_CMD_QUEUE_SZ                SZ_64K
+
 LIST_HEAD(host_its_list);
 
 bool gicv3_its_host_has_its(void)
@@ -56,6 +59,55 @@ static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits)
     return ret | ((addr & GENMASK(51, 48)) >> (48 - 12));
 }
 
+static void *its_map_cbaser(struct host_its *its)
+{
+    void __iomem *cbasereg = its->its_base + GITS_CBASER;
+    uint64_t reg;
+    void *buffer;
+    unsigned int order;
+
+    reg  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /* The ITS command buffer needs to be 64K aligned. */
+    order = max(get_order_from_bytes(ITS_CMD_QUEUE_SZ), 16U - PAGE_SHIFT);
+    buffer = alloc_xenheap_pages(order, 0);
+    if ( !buffer )
+        return NULL;
+
+    if ( virt_to_maddr(buffer) & ~GENMASK(51, 12) )
+    {
+        free_xenheap_pages(buffer, 0);
+        return NULL;
+    }
+    memset(buffer, 0, ITS_CMD_QUEUE_SZ);
+
+    reg |= GITS_VALID_BIT | virt_to_maddr(buffer);
+    reg |= ((ITS_CMD_QUEUE_SZ / SZ_4K) - 1) & GITS_CBASER_SIZE_MASK;
+    writeq_relaxed(reg, cbasereg);
+    reg = readq_relaxed(cbasereg);
+
+    /* If the ITS dropped shareability, drop cacheability as well. */
+    if ( (reg & GITS_BASER_SHAREABILITY_MASK) == 0 )
+    {
+        reg &= ~GITS_BASER_INNER_CACHEABILITY_MASK;
+        writeq_relaxed(reg, cbasereg);
+    }
+
+    /*
+     * If the command queue memory is mapped as uncached, we need to flush
+     * it on every access.
+     */
+    if ( !(reg & GITS_BASER_INNER_CACHEABILITY_MASK) )
+    {
+        its->flags |= HOST_ITS_FLUSH_CMD_QUEUE;
+        dprintk(XENLOG_WARNING, "using non-cacheable ITS command queue\n");
+    }
+
+    return buffer;
+}
+
 /* The ITS BASE registers work with page sizes of 4K, 16K or 64K. */
 #define BASER_PAGE_BITS(sz) ((sz) * 2 + 12)
 
@@ -175,6 +227,11 @@ static int gicv3_its_init_single_its(struct host_its *hw_its)
         }
     }
 
+    hw_its->cmd_buf = its_map_cbaser(hw_its);
+    if ( !hw_its->cmd_buf )
+        return -ENOMEM;
+    writeq_relaxed(0, hw_its->its_base + GITS_CWRITER);
+
     return 0;
 }
 
xen/include/asm-arm/gic_v3_its.h +6 -0
index a6c0acc..d5facf0 100644
@@ -74,8 +74,12 @@
 #define GITS_BASER_OUTER_CACHEABILITY_MASK   (0x7ULL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)
 #define GITS_BASER_INNER_CACHEABILITY_MASK   (0x7ULL << GITS_BASER_INNER_CACHEABILITY_SHIFT)
 
+#define GITS_CBASER_SIZE_MASK           0xff
+
 #include <xen/device_tree.h>
 
+#define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -83,6 +87,8 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    void *cmd_buf;
+    unsigned int flags;
 };
 
 
Signature · 1 line
2.9.0

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

#28 lore raw

Re: [PATCH v2 04/27] ARM: GICv3 ITS: map ITS command buffer

In reply to Andre Przywara

Hi Andre,

Quoted 17 lines · Andre Przywara

Instead of directly manipulating the tables in memory, an ITS driver sends commands via a ring buffer in normal system memory 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-v3-its.c        | 57 ++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic_v3_its.h |  6 +++++
 2 files changed, 63 insertions(+)
xen/arch/arm/gic-v3-its.c +0 -0
index 9982fe9..e5601ed 100644

xen/arch/arm/gic-v3-its.c line 28

Quoted 9 lines
xen/arch/arm/gic-v3-its.c +2 -0
@@ -20,10 +20,13 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sizes.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 
+#define ITS_CMD_QUEUE_SZ                SZ_64K

I thought you were planning to increase the size to 1MB?

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +3 -0
+
 LIST_HEAD(host_its_list);
 
 bool gicv3_its_host_has_its(void)
@@ -56,6 +59,55 @@ static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits)
     return ret | ((addr & GENMASK(51, 48)) >> (48 - 12));
 }
 
+static void *its_map_cbaser(struct host_its *its)
+{

xen/arch/arm/gic-v3-its.c static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits) · line 73

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    void __iomem *cbasereg = its->its_base + GITS_CBASER;
+    uint64_t reg;
+    void *buffer;
+    unsigned int order;
+
+    reg  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner << GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_RaWaWb << GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /* The ITS command buffer needs to be 64K aligned. */

Looking at the spec, the command buffer does not need to be 64K aligned. On the previous version, you made it 4K aligned. So why this restriction?

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

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+    order = max(get_order_from_bytes(ITS_CMD_QUEUE_SZ), 16U - PAGE_SHIFT);
+    buffer = alloc_xenheap_pages(order, 0);

I am not sure to understand why you move from _zalloc to alloc_xenheap_*. The resulting behavior will be exactly the same but the former result to a simpler code.

Quoted 20 lines
xen/arch/arm/gic-v3-its.c +20 -0
+    if ( !buffer )
+        return NULL;
+
+    if ( virt_to_maddr(buffer) & ~GENMASK(51, 12) )
+    {
+        free_xenheap_pages(buffer, 0);
+        return NULL;
+    }
+    memset(buffer, 0, ITS_CMD_QUEUE_SZ);
+
+    reg |= GITS_VALID_BIT | virt_to_maddr(buffer);
+    reg |= ((ITS_CMD_QUEUE_SZ / SZ_4K) - 1) & GITS_CBASER_SIZE_MASK;
+    writeq_relaxed(reg, cbasereg);
+    reg = readq_relaxed(cbasereg);
+
+    /* If the ITS dropped shareability, drop cacheability as well. */
+    if ( (reg & GITS_BASER_SHAREABILITY_MASK) == 0 )
+    {
+        reg &= ~GITS_BASER_INNER_CACHEABILITY_MASK;
+        writeq_relaxed(reg, cbasereg);

xen/arch/arm/gic-v3-its.c static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits) · line 105

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    }
+
+    /*
+     * If the command queue memory is mapped as uncached, we need to flush
+     * it on every access.
+     */
+    if ( !(reg & GITS_BASER_INNER_CACHEABILITY_MASK) )
+    {
+        its->flags |= HOST_ITS_FLUSH_CMD_QUEUE;
+        dprintk(XENLOG_WARNING, "using non-cacheable ITS command queue\n");

Please use printk, the message is useful even for non-debug build.

xen/arch/arm/gic-v3-its.c static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits) · line 110

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    }
+
+    return buffer;
+}
+

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 04/27] ARM: GICv3 ITS: map ITS command buffer

Quoted 29 lines · Julien Grall

Hi Andre,

Quoted 26 lines · Andre Przywara

Instead of directly manipulating the tables in memory, an ITS driver sends commands via a ring buffer in normal system memory 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-v3-its.c        | 57
++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic_v3_its.h |  6 +++++
 2 files changed, 63 insertions(+)
xen/arch/arm/gic-v3-its.c +1 -0
index 9982fe9..e5601ed 100644
@@ -20,10 +20,13 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sizes.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 
Quoted 3 lines

xen/arch/arm/gic-v3-its.c line 28

Quoted 1 line
xen/arch/arm/gic-v3-its.c +1 -0
+#define ITS_CMD_QUEUE_SZ                SZ_64K

I thought you were planning to increase the size to 1MB?

Good, you noticed ;-) Indeed forgot to change it ...

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
 LIST_HEAD(host_its_list);
 
 bool gicv3_its_host_has_its(void)
@@ -56,6 +59,55 @@ static uint64_t encode_propbaser_phys_addr(paddr_t
 addr, unsigned int page_bits)
     return ret | ((addr & GENMASK(51, 48)) >> (48 - 12));
 }
 
+static void *its_map_cbaser(struct host_its *its)
+{
+    void __iomem *cbasereg = its->its_base + GITS_CBASER;
+    uint64_t reg;
+    void *buffer;
+    unsigned int order;
+
+    reg  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner <<
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static uint64_t encode_propbaser_phys_addr(paddr_t · line 76

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +3 -0
 GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_RaWaWb <<
 GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /* The ITS command buffer needs to be 64K aligned. */

Looking at the spec, the command buffer does not need to be 64K aligned. On the previous version, you made it 4K aligned. So why this restriction?

As you have already learnt, the GIC is more subtle sometimes ;-) Read the description at "Physical_Address, bits [51:12]" in the CBASER paragraph and tell me what you take from it. I decided to read it as "has to be 64K aligned".
Happy to correct this otherwise.

Quoted 7 lines
Quoted 3 lines
xen/arch/arm/gic-v3-its.c +1 -0
+    order = max(get_order_from_bytes(ITS_CMD_QUEUE_SZ), 16U -

PAGE_SHIFT);
+ buffer = alloc_xenheap_pages(order, 0);

I am not sure to understand why you move from _zalloc to alloc_xenheap_*. The resulting behavior will be exactly the same but the former result to a simpler code.

                                                  ^^^^^^^^^^^^^^^^

Really?
I checked and found that alloc_xenheap_pages gives me physically contiguous allocation in the given order, where *alloc just guarantees virtually contiguous pages.

Quoted 27 lines
Quoted 27 lines
xen/arch/arm/gic-v3-its.c +24 -0
+    if ( !buffer )
+        return NULL;
+
+    if ( virt_to_maddr(buffer) & ~GENMASK(51, 12) )
+    {
+        free_xenheap_pages(buffer, 0);
+        return NULL;
+    }
+    memset(buffer, 0, ITS_CMD_QUEUE_SZ);
+
+    reg |= GITS_VALID_BIT | virt_to_maddr(buffer);
+    reg |= ((ITS_CMD_QUEUE_SZ / SZ_4K) - 1) & GITS_CBASER_SIZE_MASK;
+    writeq_relaxed(reg, cbasereg);
+    reg = readq_relaxed(cbasereg);
+
+    /* If the ITS dropped shareability, drop cacheability as well. */
+    if ( (reg & GITS_BASER_SHAREABILITY_MASK) == 0 )
+    {
+        reg &= ~GITS_BASER_INNER_CACHEABILITY_MASK;
+        writeq_relaxed(reg, cbasereg);
+    }
+
+    /*
+     * If the command queue memory is mapped as uncached, we need to

flush
+ * it on every access.

Quoted 8 lines
Quoted 6 lines
xen/arch/arm/gic-v3-its.c +5 -0
+     */
+    if ( !(reg & GITS_BASER_INNER_CACHEABILITY_MASK) )
+    {
+        its->flags |= HOST_ITS_FLUSH_CMD_QUEUE;
+        dprintk(XENLOG_WARNING, "using non-cacheable ITS command

queue\n");

Please use printk, the message is useful even for non-debug build.

OK.

Cheers,
Andre.

Quoted 9 lines

xen/arch/arm/gic-v3-its.c static uint64_t encode_propbaser_phys_addr(paddr_t addr, unsigned int page_bits) · line 110

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    }
+
+    return buffer;
+}
+

Cheers,

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

#30 lore raw

Re: [PATCH v2 04/27] ARM: GICv3 ITS: map ITS command buffer

Hi Andre,

Quoted 29 lines · André Przywara
Quoted 19 lines · Julien Grall
Quoted 18 lines · Andre Przywara
xen/arch/arm/gic-v3-its.c +10 -0
+
 LIST_HEAD(host_its_list);
 
 bool gicv3_its_host_has_its(void)
@@ -56,6 +59,55 @@ static uint64_t encode_propbaser_phys_addr(paddr_t
 addr, unsigned int page_bits)
     return ret | ((addr & GENMASK(51, 48)) >> (48 - 12));
 }
 
+static void *its_map_cbaser(struct host_its *its)
+{
+    void __iomem *cbasereg = its->its_base + GITS_CBASER;
+    uint64_t reg;
+    void *buffer;
+    unsigned int order;
+
+    reg  = GIC_BASER_InnerShareable << GITS_BASER_SHAREABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_SameAsInner <<
Quoted 8 lines

Review static uint64_t encode_propbaser_phys_addr(paddr_t · line 76

Quoted 5 lines
(quoted diff) +3 -0
 GITS_BASER_OUTER_CACHEABILITY_SHIFT;
+    reg |= GIC_BASER_CACHE_RaWaWb <<
 GITS_BASER_INNER_CACHEABILITY_SHIFT;
+
+    /* The ITS command buffer needs to be 64K aligned. */

Looking at the spec, the command buffer does not need to be 64K aligned. On the previous version, you made it 4K aligned. So why this restriction?

Quoted 5 lines

As you have already learnt, the GIC is more subtle sometimes ;-) Read the description at "Physical_Address, bits [51:12]" in the CBASER paragraph and tell me what you take from it. I decided to read it as "has to be 64K aligned".
Happy to correct this otherwise.

Hmmm. I think you are right. This is a little bit weird, but fine. Lets keep like that.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
(quoted diff) +1 -0
+    order = max(get_order_from_bytes(ITS_CMD_QUEUE_SZ), 16U -

PAGE_SHIFT);
+ buffer = alloc_xenheap_pages(order, 0);

Quoted 8 lines
Quoted 3 lines

I am not sure to understand why you move from _zalloc to alloc_xenheap_*. The resulting behavior will be exactly the same but the former result to a simpler code.

                                                  ^^^^^^^^^^^^^^^^

Really?
I checked and found that alloc_xenheap_pages gives me physically contiguous allocation in the given order, where *alloc just guarantees virtually contiguous pages.

Yes. Give a look at my answer on patch #3.

Regards,

Signature · 6 lines
Julien Grall

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

PATCH v2 5/27 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-v3-its.c         | 181 +++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c         |  22 +++++
 xen/arch/arm/gic-v3.c             |  19 +++-
 xen/include/asm-arm/gic_v3_defs.h |   2 +
 xen/include/asm-arm/gic_v3_its.h  |  38 ++++++++
 5 files changed, 260 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +180 -1
index e5601ed..5c11b0d 100644
@@ -19,11 +19,14 @@
  */
 
 #include <xen/lib.h>
+#include <xen/delay.h>
 #include <xen/mm.h>
 #include <xen/sizes.h>
+#include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
+#include <asm/page.h>
 
 #define ITS_CMD_QUEUE_SZ                SZ_64K
 
@@ -34,6 +37,145 @@ bool gicv3_its_host_has_its(void)
     return !list_empty(&host_its_list);
 }
 
+#define BUFPTR_MASK                     GENMASK(19, 5)
+static int its_send_command(struct host_its *hw_its, const void *its_cmd)
+{
+    s_time_t deadline = NOW() + MILLISECS(1);
+    uint64_t readp, writep;
+    int ret = -EBUSY;
+
+    /* No ITS commands from an interrupt handler (at the moment). */
+    ASSERT(!in_irq());
+
+    spin_lock(&hw_its->cmd_lock);
+
+    do {
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+
+        if ( ((writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ) != readp )
+        {
+            ret = 0;
+            break;
+        }
+
+        /*
+         * If the command queue is full, wait for a bit in the hope it drains
+         * before giving up.
+         */
+        spin_unlock(&hw_its->cmd_lock);
+        cpu_relax();
+        udelay(1);
+        spin_lock(&hw_its->cmd_lock);
+    } while ( NOW() <= deadline );
+
+    if ( ret )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        printk(XENLOG_WARNING "ITS: command queue full.\n");
+        return ret;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_CMD_SIZE);
+    if ( hw_its->flags & HOST_ITS_FLUSH_CMD_QUEUE )
+        clean_and_invalidate_dcache_va_range(hw_its->cmd_buf + writep,
+                                             ITS_CMD_SIZE);
+    else
+        dsb(ishst);
+
+    writep = (writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ;
+    writeq_relaxed(writep & BUFPTR_MASK, hw_its->its_base + GITS_CWRITER);
+
+    spin_unlock(&hw_its->cmd_lock);
+
+    return 0;
+}
+
+/* Wait for an ITS to finish processing all commands. */
+static int gicv3_its_wait_commands(struct host_its *hw_its)
+{
+    s_time_t deadline = NOW() + MILLISECS(100);
+    uint64_t readp, writep;
+
+    do {
+        spin_lock(&hw_its->cmd_lock);
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+        spin_unlock(&hw_its->cmd_lock);
+
+        if ( readp == writep )
+            return 0;
+
+        cpu_relax();
+        udelay(1);
+    } while ( NOW() <= deadline );
+
+    return -ETIMEDOUT;
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, unsigned int cpu,
+                              uint64_t reg)
+{
+    reg &= ~GENMASK(51, 16);
+
+    reg |= gicv3_get_redist_address(cpu, hw_its->flags & HOST_ITS_USES_PTA);
+
+    return reg;
+}
+
+static int its_send_cmd_sync(struct host_its *its, unsigned 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, uint32_t collection_id,
+                             unsigned 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)));
+    cmd[2] |= GITS_VALID_BIT;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+int gicv3_its_setup_collection(unsigned int cpu)
+{
+    struct host_its *its;
+    int ret;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        if ( !its->cmd_buf )
+            continue;
+
+        ret = its_send_cmd_mapc(its, cpu, cpu);
+        if ( ret )
+            return ret;
+
+        ret = its_send_cmd_sync(its, cpu);
+        if ( ret )
+            return ret;
+
+        ret = gicv3_its_wait_commands(its);
+        if ( ret )
+            return ret;
+    }
+
+    return 0;
+}
+
 #define BASER_ATTR_MASK                                           \
         ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
          (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
@@ -184,22 +326,59 @@ retry:
     return -EINVAL;
 }
 
+/*
+ * Before an ITS gets initialized, it should be in a quiescent state, where
+ * all outstanding commands and transactions have finished.
+ * So if the ITS is already enabled, turn it off and wait for all outstanding
+ * operations to get processed by polling the QUIESCENT bit.
+ */
+static int gicv3_disable_its(struct host_its *hw_its)
+{
+    uint32_t reg;
+    s_time_t deadline = NOW() + MILLISECS(100);
+
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    if ( (reg & GITS_CTLR_QUIESCENT) && !(reg & GITS_CTLR_ENABLE) )
+        return 0;
+
+    writel_relaxed(reg & ~GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
+    do {
+        reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+        if ( reg & GITS_CTLR_QUIESCENT )
+            return 0;
+
+        cpu_relax();
+        udelay(1);
+    } while ( NOW() <= deadline );
+
+    dprintk(XENLOG_ERR, "ITS not quiescent.\n");
+
+    return -ETIMEDOUT;
+}
+
 static unsigned int max_its_device_bits = CONFIG_MAX_PHYS_ITS_DEVICE_BITS;
 integer_param("max_its_device_bits", max_its_device_bits);
 
 static int gicv3_its_init_single_its(struct host_its *hw_its)
 {
     uint64_t reg;
-    int i;
+    int i ,ret;
     unsigned int devid_bits;
 
     hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
     if ( !hw_its->its_base )
         return -ENOMEM;
 
+    ret = gicv3_disable_its(hw_its);
+    if ( ret )
+        return ret;
+
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     devid_bits = GITS_TYPER_DEVICE_ID_BITS(reg);
     devid_bits = min(devid_bits, max_its_device_bits);
+    if ( reg & GITS_TYPER_PTA )
+        hw_its->flags |= HOST_ITS_USES_PTA;
 
     for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
     {
xen/arch/arm/gic-v3-lpi.c +22 -0
index 4f8414b..51d7425 100644
@@ -42,6 +42,8 @@ static struct {
 } lpi_data;
 
 struct lpi_redist_data {
+    paddr_t             redist_addr;
+    unsigned int        redist_id;
     void                *pending_table;
 };
 
@@ -49,6 +51,26 @@ static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
 
 #define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)
 
+/* Stores this redistributor's physical address and ID in a per-CPU variable */
+void gicv3_set_redist_address(paddr_t address, unsigned int redist_id)
+{
+    this_cpu(lpi_redist).redist_addr = address;
+    this_cpu(lpi_redist).redist_id = redist_id;
+}
+
+/*
+ * Returns a redistributor's ID (either as an address or as an ID).
+ * This must be (and is) called only after it has been setup by the above
+ * function.
+ */
+uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta)
+{
+    if ( use_pta )
+        return per_cpu(lpi_redist, cpu).redist_addr & GENMASK(51, 16);
+    else
+        return per_cpu(lpi_redist, cpu).redist_id << 16;
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/gic-v3.c +18 -1
index cc1e219..38dafe7 100644
@@ -666,7 +666,21 @@ static int __init gicv3_populate_rdist(void)
 
                 if ( typer & GICR_TYPER_PLPIS )
                 {
-                    int ret;
+                    paddr_t rdist_addr;
+                    int procnum, ret;
+
+                    /*
+                     * 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.
+                     */
+                    rdist_addr = gicv3.rdist_regions[i].base;
+                    rdist_addr += ptr - gicv3.rdist_regions[i].map_base;
+                    procnum = (typer & GICR_TYPER_PROC_NUM_MASK);
+                    procnum >>= GICR_TYPER_PROC_NUM_SHIFT;
+
+                    gicv3_set_redist_address(rdist_addr, procnum);
 
                     ret = gicv3_lpi_init_rdist(ptr);
                     if ( ret && ret != -ENODEV )
@@ -715,6 +729,9 @@ static int gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+    if ( gicv3_its_host_has_its() )
+        gicv3_its_setup_collection(smp_processor_id());
+
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
                 GIC_PRI_IPI);
xen/include/asm-arm/gic_v3_defs.h +2 -0
index b307322..878bae2 100644
@@ -101,6 +101,8 @@
 #define GICR_TYPER_PLPIS             (1U << 0)
 #define GICR_TYPER_VLPIS             (1U << 1)
 #define GICR_TYPER_LAST              (1U << 4)
+#define GICR_TYPER_PROC_NUM_SHIFT    8
+#define GICR_TYPER_PROC_NUM_MASK     (0xffff << GICR_TYPER_PROC_NUM_SHIFT)
 
 /* For specifying the inner cacheability type only */
 #define GIC_BASER_CACHE_nCnB         0ULL
xen/include/asm-arm/gic_v3_its.h +38 -0
index d5facf0..8b493fb 100644
@@ -42,10 +42,12 @@
 #define GITS_CTLR_QUIESCENT             BIT(31)
 #define GITS_CTLR_ENABLE                BIT(0)
 
+#define GITS_TYPER_PTA                  BIT_ULL(19)
 #define GITS_TYPER_DEVIDS_SHIFT         13
 #define GITS_TYPER_DEVIDS_MASK          (0x1fUL << GITS_TYPER_DEVIDS_SHIFT)
 #define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
                                                GITS_TYPER_DEVIDS_SHIFT) + 1)
+#define GITS_TYPER_IDBITS_SHIFT         8
 
 #define GITS_IIDR_VALUE                 0x34c
 
@@ -76,9 +78,26 @@
 
 #define GITS_CBASER_SIZE_MASK           0xff
 
+/* 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
+
 #include <xen/device_tree.h>
 
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
+#define HOST_ITS_USES_PTA               (1U << 1)
 
 /* data structure for each hardware ITS */
 struct host_its {
@@ -87,6 +106,7 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;
     void *cmd_buf;
     unsigned int flags;
 };
@@ -107,6 +127,13 @@ int gicv3_lpi_init_rdist(void __iomem * rdist_base);
 int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);
 int gicv3_its_init(void);
 
+/* Store the physical address and ID for each redistributor as read from DT. */
+void gicv3_set_redist_address(paddr_t address, unsigned int redist_id);
+uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
+
+/* Map a collection for this host CPU to each host ITS. */
+int gicv3_its_setup_collection(unsigned int cpu);
+
 #else
 
 static LIST_HEAD(host_its_list);
@@ -134,6 +161,17 @@ static inline int gicv3_its_init(void)
 {
     return 0;
 }
+
+static inline void gicv3_set_redist_address(paddr_t address,
+                                            unsigned int redist_id)
+{
+}
+
+static inline int gicv3_its_setup_collection(unsigned int cpu)
+{
+    return 0;
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif
Signature · 7 lines
2.9.0


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

Re: [PATCH v2 05/27] ARM: GICv3 ITS: introduce ITS command handling

Hi Andre,

Quoted 187 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-v3-its.c         | 181 +++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c         |  22 +++++
 xen/arch/arm/gic-v3.c             |  19 +++-
 xen/include/asm-arm/gic_v3_defs.h |   2 +
 xen/include/asm-arm/gic_v3_its.h  |  38 ++++++++
 5 files changed, 260 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +145 -0
index e5601ed..5c11b0d 100644
@@ -19,11 +19,14 @@
  */
 
 #include <xen/lib.h>
+#include <xen/delay.h>
 #include <xen/mm.h>
 #include <xen/sizes.h>
+#include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
+#include <asm/page.h>
 
 #define ITS_CMD_QUEUE_SZ                SZ_64K
 
@@ -34,6 +37,145 @@ bool gicv3_its_host_has_its(void)
     return !list_empty(&host_its_list);
 }
 
+#define BUFPTR_MASK                     GENMASK(19, 5)
+static int its_send_command(struct host_its *hw_its, const void *its_cmd)
+{
+    s_time_t deadline = NOW() + MILLISECS(1);
+    uint64_t readp, writep;
+    int ret = -EBUSY;
+
+    /* No ITS commands from an interrupt handler (at the moment). */
+    ASSERT(!in_irq());
+
+    spin_lock(&hw_its->cmd_lock);
+
+    do {
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+
+        if ( ((writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ) != readp )
+        {
+            ret = 0;
+            break;
+        }
+
+        /*
+         * If the command queue is full, wait for a bit in the hope it drains
+         * before giving up.
+         */
+        spin_unlock(&hw_its->cmd_lock);
+        cpu_relax();
+        udelay(1);
+        spin_lock(&hw_its->cmd_lock);
+    } while ( NOW() <= deadline );
+
+    if ( ret )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        printk(XENLOG_WARNING "ITS: command queue full.\n");
+        return ret;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_CMD_SIZE);
+    if ( hw_its->flags & HOST_ITS_FLUSH_CMD_QUEUE )
+        clean_and_invalidate_dcache_va_range(hw_its->cmd_buf + writep,
+                                             ITS_CMD_SIZE);
+    else
+        dsb(ishst);
+
+    writep = (writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ;
+    writeq_relaxed(writep & BUFPTR_MASK, hw_its->its_base + GITS_CWRITER);
+
+    spin_unlock(&hw_its->cmd_lock);
+
+    return 0;
+}
+
+/* Wait for an ITS to finish processing all commands. */
+static int gicv3_its_wait_commands(struct host_its *hw_its)
+{
+    s_time_t deadline = NOW() + MILLISECS(100);
+    uint64_t readp, writep;
+
+    do {
+        spin_lock(&hw_its->cmd_lock);
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+        spin_unlock(&hw_its->cmd_lock);
+
+        if ( readp == writep )
+            return 0;
+
+        cpu_relax();
+        udelay(1);
+    } while ( NOW() <= deadline );
+
+    return -ETIMEDOUT;
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, unsigned int cpu,
+                              uint64_t reg)
+{
+    reg &= ~GENMASK(51, 16);
+
+    reg |= gicv3_get_redist_address(cpu, hw_its->flags & HOST_ITS_USES_PTA);
+
+    return reg;
+}
+
+static int its_send_cmd_sync(struct host_its *its, unsigned 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, uint32_t collection_id,
+                             unsigned 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)));
+    cmd[2] |= GITS_VALID_BIT;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+int gicv3_its_setup_collection(unsigned int cpu)
+{
+    struct host_its *its;
+    int ret;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        if ( !its->cmd_buf )
+            continue;
+
+        ret = its_send_cmd_mapc(its, cpu, cpu);
+        if ( ret )
+            return ret;
+
+        ret = its_send_cmd_sync(its, cpu);
+        if ( ret )
+            return ret;
+
+        ret = gicv3_its_wait_commands(its);
+        if ( ret )
+            return ret;
+    }
+
+    return 0;
+}
+
 #define BASER_ATTR_MASK                                           \
         ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
          (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
@@ -184,22 +326,59 @@ retry:
     return -EINVAL;
 }
 
+/*
+ * Before an ITS gets initialized, it should be in a quiescent state, where
+ * all outstanding commands and transactions have finished.

xen/arch/arm/gic-v3-its.c retry: · line 341

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+ * So if the ITS is already enabled, turn it off and wait for all outstanding
+ * operations to get processed by polling the QUIESCENT bit.
+ */
+static int gicv3_disable_its(struct host_its *hw_its)
+{
+    uint32_t reg;
+    s_time_t deadline = NOW() + MILLISECS(100);
+
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    if ( (reg & GITS_CTLR_QUIESCENT) && !(reg & GITS_CTLR_ENABLE) )

nit: I prefer changing to 'if ( !(reg & GITS_CTLR_ENABLE) && (reg & GITS_CTLR_QUIESCENT) ) ' because bit GITS_CTLR_QUIESCENT is not valid if ITS hardware is in enabled state.

Quoted 1 line
xen/arch/arm/gic-v3-its.c +0 -0
 #endif
Signature · 9 lines
Shanker Donthineni
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.


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

Re: [PATCH v2 05/27] ARM: GICv3 ITS: introduce ITS command handling

Hi Shanker,

Quoted 12 lines · Shanker Donthineni

Hi Andre,

Quoted 8 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.

---

...

Quoted 4 lines
Quoted 4 lines
xen/arch/arm/gic-v3-its.c +0 -0
@@ -184,22 +326,59 @@ retry:
     return -EINVAL;
 }
 
Quoted 15 lines

xen/arch/arm/gic-v3-its.c retry: · line 341

Quoted 13 lines
xen/arch/arm/gic-v3-its.c +13 -0
+/*
+ * Before an ITS gets initialized, it should be in a quiescent state, where
+ * all outstanding commands and transactions have finished.
+ * So if the ITS is already enabled, turn it off and wait for all outstanding
+ * operations to get processed by polling the QUIESCENT bit.
+ */
+static int gicv3_disable_its(struct host_its *hw_its)
+{
+    uint32_t reg;
+    s_time_t deadline = NOW() + MILLISECS(100);
+
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    if ( (reg & GITS_CTLR_QUIESCENT) && !(reg & GITS_CTLR_ENABLE) )

nit: I prefer changing to 'if ( !(reg & GITS_CTLR_ENABLE) && (reg & GITS_CTLR_QUIESCENT) ) ' because bit GITS_CTLR_QUIESCENT is not valid if ITS hardware is in enabled state.

Sure, makes sense. I will change this.

Thanks for having a look!

Cheers,
Andre.

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

#34 lore raw

Re: [PATCH v2 05/27] ARM: GICv3 ITS: introduce ITS command handling

In reply to Andre Przywara
Quoted 309 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-v3-its.c         | 181 +++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c         |  22 +++++
 xen/arch/arm/gic-v3.c             |  19 +++-
 xen/include/asm-arm/gic_v3_defs.h |   2 +
 xen/include/asm-arm/gic_v3_its.h  |  38 ++++++++
 5 files changed, 260 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +180 -1
index e5601ed..5c11b0d 100644
@@ -19,11 +19,14 @@
  */
 
 #include <xen/lib.h>
+#include <xen/delay.h>
 #include <xen/mm.h>
 #include <xen/sizes.h>
+#include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
+#include <asm/page.h>
 
 #define ITS_CMD_QUEUE_SZ                SZ_64K
 
@@ -34,6 +37,145 @@ bool gicv3_its_host_has_its(void)
     return !list_empty(&host_its_list);
 }
 
+#define BUFPTR_MASK                     GENMASK(19, 5)
+static int its_send_command(struct host_its *hw_its, const void *its_cmd)
+{
+    s_time_t deadline = NOW() + MILLISECS(1);
+    uint64_t readp, writep;
+    int ret = -EBUSY;
+
+    /* No ITS commands from an interrupt handler (at the moment). */
+    ASSERT(!in_irq());
+
+    spin_lock(&hw_its->cmd_lock);
+
+    do {
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+
+        if ( ((writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ) != readp )
+        {
+            ret = 0;
+            break;
+        }
+
+        /*
+         * If the command queue is full, wait for a bit in the hope it drains
+         * before giving up.
+         */
+        spin_unlock(&hw_its->cmd_lock);
+        cpu_relax();
+        udelay(1);
+        spin_lock(&hw_its->cmd_lock);
+    } while ( NOW() <= deadline );
+
+    if ( ret )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        printk(XENLOG_WARNING "ITS: command queue full.\n");
+        return ret;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_CMD_SIZE);
+    if ( hw_its->flags & HOST_ITS_FLUSH_CMD_QUEUE )
+        clean_and_invalidate_dcache_va_range(hw_its->cmd_buf + writep,
+                                             ITS_CMD_SIZE);
+    else
+        dsb(ishst);
+
+    writep = (writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ;
+    writeq_relaxed(writep & BUFPTR_MASK, hw_its->its_base + GITS_CWRITER);
+
+    spin_unlock(&hw_its->cmd_lock);
+
+    return 0;
+}
+
+/* Wait for an ITS to finish processing all commands. */
+static int gicv3_its_wait_commands(struct host_its *hw_its)
+{
+    s_time_t deadline = NOW() + MILLISECS(100);
+    uint64_t readp, writep;
+
+    do {
+        spin_lock(&hw_its->cmd_lock);
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+        spin_unlock(&hw_its->cmd_lock);
+
+        if ( readp == writep )
+            return 0;
+
+        cpu_relax();
+        udelay(1);
+    } while ( NOW() <= deadline );
+
+    return -ETIMEDOUT;
+}
+
+static uint64_t encode_rdbase(struct host_its *hw_its, unsigned int cpu,
+                              uint64_t reg)
+{
+    reg &= ~GENMASK(51, 16);
+
+    reg |= gicv3_get_redist_address(cpu, hw_its->flags & HOST_ITS_USES_PTA);
+
+    return reg;
+}
+
+static int its_send_cmd_sync(struct host_its *its, unsigned 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, uint32_t collection_id,
+                             unsigned 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)));
+    cmd[2] |= GITS_VALID_BIT;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+int gicv3_its_setup_collection(unsigned int cpu)
+{
+    struct host_its *its;
+    int ret;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        if ( !its->cmd_buf )
+            continue;
+
+        ret = its_send_cmd_mapc(its, cpu, cpu);
+        if ( ret )
+            return ret;
+
+        ret = its_send_cmd_sync(its, cpu);
+        if ( ret )
+            return ret;
+
+        ret = gicv3_its_wait_commands(its);
+        if ( ret )
+            return ret;
+    }
+
+    return 0;
+}
+
 #define BASER_ATTR_MASK                                           \
         ((0x3UL << GITS_BASER_SHAREABILITY_SHIFT)               | \
          (0x7UL << GITS_BASER_OUTER_CACHEABILITY_SHIFT)         | \
@@ -184,22 +326,59 @@ retry:
     return -EINVAL;
 }
 
+/*
+ * Before an ITS gets initialized, it should be in a quiescent state, where
+ * all outstanding commands and transactions have finished.
+ * So if the ITS is already enabled, turn it off and wait for all outstanding
+ * operations to get processed by polling the QUIESCENT bit.
+ */
+static int gicv3_disable_its(struct host_its *hw_its)
+{
+    uint32_t reg;
+    s_time_t deadline = NOW() + MILLISECS(100);
+
+    reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+    if ( (reg & GITS_CTLR_QUIESCENT) && !(reg & GITS_CTLR_ENABLE) )
+        return 0;
+
+    writel_relaxed(reg & ~GITS_CTLR_ENABLE, hw_its->its_base + GITS_CTLR);
+
+    do {
+        reg = readl_relaxed(hw_its->its_base + GITS_CTLR);
+        if ( reg & GITS_CTLR_QUIESCENT )
+            return 0;
+
+        cpu_relax();
+        udelay(1);
+    } while ( NOW() <= deadline );
+
+    dprintk(XENLOG_ERR, "ITS not quiescent.\n");
+
+    return -ETIMEDOUT;
+}
+
 static unsigned int max_its_device_bits = CONFIG_MAX_PHYS_ITS_DEVICE_BITS;
 integer_param("max_its_device_bits", max_its_device_bits);
 
 static int gicv3_its_init_single_its(struct host_its *hw_its)
 {
     uint64_t reg;
-    int i;
+    int i ,ret;
     unsigned int devid_bits;
 
     hw_its->its_base = ioremap_nocache(hw_its->addr, hw_its->size);
     if ( !hw_its->its_base )
         return -ENOMEM;
 
+    ret = gicv3_disable_its(hw_its);
+    if ( ret )
+        return ret;
+
     reg = readq_relaxed(hw_its->its_base + GITS_TYPER);
     devid_bits = GITS_TYPER_DEVICE_ID_BITS(reg);
     devid_bits = min(devid_bits, max_its_device_bits);
+    if ( reg & GITS_TYPER_PTA )
+        hw_its->flags |= HOST_ITS_USES_PTA;
 
     for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
     {
xen/arch/arm/gic-v3-lpi.c +22 -0
index 4f8414b..51d7425 100644
@@ -42,6 +42,8 @@ static struct {
 } lpi_data;
 
 struct lpi_redist_data {
+    paddr_t             redist_addr;
+    unsigned int        redist_id;
     void                *pending_table;
 };
 
@@ -49,6 +51,26 @@ static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
 
 #define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)
 
+/* Stores this redistributor's physical address and ID in a per-CPU variable */
+void gicv3_set_redist_address(paddr_t address, unsigned int redist_id)
+{
+    this_cpu(lpi_redist).redist_addr = address;
+    this_cpu(lpi_redist).redist_id = redist_id;
+}
+
+/*
+ * Returns a redistributor's ID (either as an address or as an ID).
+ * This must be (and is) called only after it has been setup by the above
+ * function.
+ */
+uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta)
+{
+    if ( use_pta )
+        return per_cpu(lpi_redist, cpu).redist_addr & GENMASK(51, 16);
+    else
+        return per_cpu(lpi_redist, cpu).redist_id << 16;
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/gic-v3.c +15 -1
index cc1e219..38dafe7 100644
@@ -666,7 +666,21 @@ static int __init gicv3_populate_rdist(void)
 
                 if ( typer & GICR_TYPER_PLPIS )
                 {
-                    int ret;
+                    paddr_t rdist_addr;
+                    int procnum, ret;
+
+                    /*
+                     * 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.
+                     */
+                    rdist_addr = gicv3.rdist_regions[i].base;
+                    rdist_addr += ptr - gicv3.rdist_regions[i].map_base;
+                    procnum = (typer & GICR_TYPER_PROC_NUM_MASK);
+                    procnum >>= GICR_TYPER_PROC_NUM_SHIFT;
+
+                    gicv3_set_redist_address(rdist_addr, procnum);
 
                     ret = gicv3_lpi_init_rdist(ptr);
                     if ( ret && ret != -ENODEV )

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

Quoted 5 lines
xen/arch/arm/gic-v3.c +2 -0
@@ -715,6 +729,9 @@ static int gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+    if ( gicv3_its_host_has_its() )
+        gicv3_its_setup_collection(smp_processor_id());

Check for return errors?

Quoted 103 lines
xen/arch/arm/gic-v3.c +0 -0
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
                 GIC_PRI_IPI);
xen/include/asm-arm/gic_v3_defs.h +2 -0
index b307322..878bae2 100644
@@ -101,6 +101,8 @@
 #define GICR_TYPER_PLPIS             (1U << 0)
 #define GICR_TYPER_VLPIS             (1U << 1)
 #define GICR_TYPER_LAST              (1U << 4)
+#define GICR_TYPER_PROC_NUM_SHIFT    8
+#define GICR_TYPER_PROC_NUM_MASK     (0xffff << GICR_TYPER_PROC_NUM_SHIFT)
 
 /* For specifying the inner cacheability type only */
 #define GIC_BASER_CACHE_nCnB         0ULL
xen/include/asm-arm/gic_v3_its.h +38 -0
index d5facf0..8b493fb 100644
@@ -42,10 +42,12 @@
 #define GITS_CTLR_QUIESCENT             BIT(31)
 #define GITS_CTLR_ENABLE                BIT(0)
 
+#define GITS_TYPER_PTA                  BIT_ULL(19)
 #define GITS_TYPER_DEVIDS_SHIFT         13
 #define GITS_TYPER_DEVIDS_MASK          (0x1fUL << GITS_TYPER_DEVIDS_SHIFT)
 #define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
                                                GITS_TYPER_DEVIDS_SHIFT) + 1)
+#define GITS_TYPER_IDBITS_SHIFT         8
 
 #define GITS_IIDR_VALUE                 0x34c
 
@@ -76,9 +78,26 @@
 
 #define GITS_CBASER_SIZE_MASK           0xff
 
+/* 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
+
 #include <xen/device_tree.h>
 
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
+#define HOST_ITS_USES_PTA               (1U << 1)
 
 /* data structure for each hardware ITS */
 struct host_its {
@@ -87,6 +106,7 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;
     void *cmd_buf;
     unsigned int flags;
 };
@@ -107,6 +127,13 @@ int gicv3_lpi_init_rdist(void __iomem * rdist_base);
 int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);
 int gicv3_its_init(void);
 
+/* Store the physical address and ID for each redistributor as read from DT. */
+void gicv3_set_redist_address(paddr_t address, unsigned int redist_id);
+uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
+
+/* Map a collection for this host CPU to each host ITS. */
+int gicv3_its_setup_collection(unsigned int cpu);
+
 #else
 
 static LIST_HEAD(host_its_list);
@@ -134,6 +161,17 @@ static inline int gicv3_its_init(void)
 {
     return 0;
 }
+
+static inline void gicv3_set_redist_address(paddr_t address,
+                                            unsigned int redist_id)
+{
+}
+
+static inline int gicv3_its_setup_collection(unsigned int cpu)
+{
+    return 0;
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif
Signature · 1 line
2.9.0

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

#35 lore raw

Re: [PATCH v2 05/27] ARM: GICv3 ITS: introduce ITS command handling

In reply to Andre Przywara

Hi Andre,

Quoted 35 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-v3-its.c         | 181 +++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c         |  22 +++++
 xen/arch/arm/gic-v3.c             |  19 +++-
 xen/include/asm-arm/gic_v3_defs.h |   2 +
 xen/include/asm-arm/gic_v3_its.h  |  38 ++++++++
 5 files changed, 260 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +3 -0
index e5601ed..5c11b0d 100644
@@ -19,11 +19,14 @@
  */
 
 #include <xen/lib.h>
+#include <xen/delay.h>
 #include <xen/mm.h>
 #include <xen/sizes.h>
+#include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
+#include <asm/page.h>
 
 #define ITS_CMD_QUEUE_SZ                SZ_64K
 

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 43

Quoted 7 lines
xen/arch/arm/gic-v3-its.c +4 -0
@@ -34,6 +37,145 @@ bool gicv3_its_host_has_its(void)
     return !list_empty(&host_its_list);
 }
 
+#define BUFPTR_MASK                     GENMASK(19, 5)
+static int its_send_command(struct host_its *hw_its, const void *its_cmd)
+{
+    s_time_t deadline = NOW() + MILLISECS(1);

It would be nice to explain where does this value comes from.

Quoted 22 lines
xen/arch/arm/gic-v3-its.c +22 -0
+    uint64_t readp, writep;
+    int ret = -EBUSY;
+
+    /* No ITS commands from an interrupt handler (at the moment). */
+    ASSERT(!in_irq());
+
+    spin_lock(&hw_its->cmd_lock);
+
+    do {
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+
+        if ( ((writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ) != readp )
+        {
+            ret = 0;
+            break;
+        }
+
+        /*
+         * If the command queue is full, wait for a bit in the hope it drains
+         * before giving up.
+         */

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 75

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+        spin_unlock(&hw_its->cmd_lock);
+        cpu_relax();
+        udelay(1);
+        spin_lock(&hw_its->cmd_lock);
+    } while ( NOW() <= deadline );
+
+    if ( ret )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        printk(XENLOG_WARNING "ITS: command queue full.\n");

This function could be called from a domain. So please ratelimit the message (see printk_ratelimit).

Quoted 12 lines
xen/arch/arm/gic-v3-its.c +12 -0
+        return ret;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_CMD_SIZE);
+    if ( hw_its->flags & HOST_ITS_FLUSH_CMD_QUEUE )
+        clean_and_invalidate_dcache_va_range(hw_its->cmd_buf + writep,
+                                             ITS_CMD_SIZE);
+    else
+        dsb(ishst);
+
+    writep = (writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ;
+    writeq_relaxed(writep & BUFPTR_MASK, hw_its->its_base + GITS_CWRITER);

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 97

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
+    spin_unlock(&hw_its->cmd_lock);
+
+    return 0;
+}
+
+/* Wait for an ITS to finish processing all commands. */
+static int gicv3_its_wait_commands(struct host_its *hw_its)
+{
+    s_time_t deadline = NOW() + MILLISECS(100);

Same remark as above. Why 1ms and 100ms here?

Quoted 17 lines
xen/arch/arm/gic-v3-its.c +17 -0
+    uint64_t readp, writep;
+
+    do {
+        spin_lock(&hw_its->cmd_lock);
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) & BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) & BUFPTR_MASK;
+        spin_unlock(&hw_its->cmd_lock);
+
+        if ( readp == writep )
+            return 0;
+
+        cpu_relax();
+        udelay(1);
+    } while ( NOW() <= deadline );
+
+    return -ETIMEDOUT;
+}

[...]

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 145

Quoted 8 lines
xen/arch/arm/gic-v3-its.c +8 -0
+static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id,
+                             unsigned 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)));

I requested to drop the mask here and I didn't see any answer explaining why you would not do it. So this should be dropped unless you give me a reason to keep it.

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+    cmd[2] |= GITS_VALID_BIT;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 161

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+/* Set up the (1:1) collection mapping for the given host CPU. */
+int gicv3_its_setup_collection(unsigned int cpu)
+{
+    struct host_its *its;
+    int ret;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        if ( !its->cmd_buf )
+            continue;

Why this check?

[...]

xen/arch/arm/gic-v3-its.c retry: · line 338

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+/*
+ * Before an ITS gets initialized, it should be in a quiescent state, where
+ * all outstanding commands and transactions have finished.
+ * So if the ITS is already enabled, turn it off and wait for all outstanding
+ * operations to get processed by polling the QUIESCENT bit.
+ */
+static int gicv3_disable_its(struct host_its *hw_its)
+{
+    uint32_t reg;
+    s_time_t deadline = NOW() + MILLISECS(100);

Why 100ms?

[...]

Quoted 8 lines

static int gicv3_lpi_allocate_pendtable(uint64_t *reg)

 {
     uint64_t val;
xen/arch/arm/gic-v3.c +0 -0
index cc1e219..38dafe7 100644

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

Quoted 6 lines
xen/arch/arm/gic-v3.c +2 -1
@@ -666,7 +666,21 @@ static int __init gicv3_populate_rdist(void)
 
                 if ( typer & GICR_TYPER_PLPIS )
                 {
-                    int ret;
+                    paddr_t rdist_addr;
+                    int procnum, ret;

As mentioned in v1, procnum should be unsigned. If you disagree, please explain.

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

Quoted 7 lines
xen/arch/arm/gic-v3.c +7 -0
+
+                    /*
+                     * 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.
+                     */

Again, this comment does not look useful and is misleading as the code to get/set the redistributor information is living in gic-v3-lpi.c and not gic-v3-its.c.

[...]

Quoted 4 lines
xen/include/asm-arm/gic_v3_its.h +0 -0
index d5facf0..8b493fb 100644

[...]

xen/include/asm-arm/gic_v3_its.h struct host_its { · line 109

Quoted 12 lines

#include <xen/device_tree.h>

#define HOST_ITS_FLUSH_CMD_QUEUE (1U << 0)

xen/include/asm-arm/gic_v3_its.h +2 -0
+#define HOST_ITS_USES_PTA               (1U << 1)
 
 /* data structure for each hardware ITS */
 struct host_its {
@@ -87,6 +106,7 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;

Again, initialization, clean-up of a field should be done in the same that added the field. Otherwise, this is a call to miss a bit of the code and makes more difficult for the reviewer.

So please initialize cmd_lock in this patch and patch #6.

Quoted 19 lines
xen/include/asm-arm/gic_v3_its.h +7 -0
     void *cmd_buf;
     unsigned int flags;
 };
@@ -107,6 +127,13 @@ int gicv3_lpi_init_rdist(void __iomem * rdist_base);
 int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);
 int gicv3_its_init(void);
 
+/* Store the physical address and ID for each redistributor as read from DT. */
+void gicv3_set_redist_address(paddr_t address, unsigned int redist_id);
+uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
+
+/* Map a collection for this host CPU to each host ITS. */
+int gicv3_its_setup_collection(unsigned int cpu);
+
 #else
 
 static LIST_HEAD(host_its_list);
@@ -134,6 +161,17 @@ static inline int gicv3_its_init(void)
 {

xen/include/asm-arm/gic_v3_its.h static inline int gicv3_its_init(void) · line 171

Quoted 10 lines
xen/include/asm-arm/gic_v3_its.h +8 -0
     return 0;
 }
+
+static inline void gicv3_set_redist_address(paddr_t address,
+                                            unsigned int redist_id)
+{
+}
+
+static inline int gicv3_its_setup_collection(unsigned int cpu)
+{

This function should never be called as it is gated by the presence of ITS. I would add a BUG() with a comment to ensure this is the case.

xen/include/asm-arm/gic_v3_its.h static inline int gicv3_its_init(void) · line 177

Quoted 7 lines
xen/include/asm-arm/gic_v3_its.h +3 -0
+    return 0;
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif

Cheers

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 05/27] ARM: GICv3 ITS: introduce ITS command handling

Hi,

Quoted 42 lines · Julien Grall

Hi Andre,

Quoted 39 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-v3-its.c         | 181
+++++++++++++++++++++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c         |  22 +++++
 xen/arch/arm/gic-v3.c             |  19 +++-
 xen/include/asm-arm/gic_v3_defs.h |   2 +
 xen/include/asm-arm/gic_v3_its.h  |  38 ++++++++
 5 files changed, 260 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +3 -0
index e5601ed..5c11b0d 100644
@@ -19,11 +19,14 @@
  */
 
 #include <xen/lib.h>
+#include <xen/delay.h>
 #include <xen/mm.h>
 #include <xen/sizes.h>
+#include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
+#include <asm/page.h>
 
 #define ITS_CMD_QUEUE_SZ                SZ_64K
 
@@ -34,6 +37,145 @@ bool gicv3_its_host_has_its(void)
     return !list_empty(&host_its_list);
 }
 
Quoted 7 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 43

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +4 -0
+#define BUFPTR_MASK                     GENMASK(19, 5)
+static int its_send_command(struct host_its *hw_its, const void
 *its_cmd)
+{
+    s_time_t deadline = NOW() + MILLISECS(1);

It would be nice to explain where does this value comes from.

In lack of any real data on this, one millisecond is just a guess. In v3 I added some comment stating that this is a "short" period to wait for. We don't want to stall for a long time for a single command, but also don't want to give up too easily. The command queue is handled by the ITS, which is hardware, so the only thing it does is handling commands. If we are unlucky enough to hit a full command queue due to a sudden spike in host commands, chances are that we get a free slot very soon afterwards. If not, we are screwed anyway.

Quoted 30 lines
Quoted 30 lines
xen/arch/arm/gic-v3-its.c +20 -0
+    uint64_t readp, writep;
+    int ret = -EBUSY;
+
+    /* No ITS commands from an interrupt handler (at the moment). */
+    ASSERT(!in_irq());
+
+    spin_lock(&hw_its->cmd_lock);
+
+    do {
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) &
 BUFPTR_MASK;
+        writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) &
 BUFPTR_MASK;
+
+        if ( ((writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ) != readp )
+        {
+            ret = 0;
+            break;
+        }
+
+        /*
+         * If the command queue is full, wait for a bit in the hope

it drains

xen/arch/arm/gic-v3-its.c +7 -0
+         * before giving up.
+         */
+        spin_unlock(&hw_its->cmd_lock);
+        cpu_relax();
+        udelay(1);
+        spin_lock(&hw_its->cmd_lock);
+    } while ( NOW() <= deadline );
Quoted 8 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 75

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+
+    if ( ret )
+    {
+        spin_unlock(&hw_its->cmd_lock);
+        printk(XENLOG_WARNING "ITS: command queue full.\n");

This function could be called from a domain. So please ratelimit the message (see printk_ratelimit).

Fixed in v4.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/gic-v3-its.c +12 -0
+        return ret;
+    }
+
+    memcpy(hw_its->cmd_buf + writep, its_cmd, ITS_CMD_SIZE);
+    if ( hw_its->flags & HOST_ITS_FLUSH_CMD_QUEUE )
+        clean_and_invalidate_dcache_va_range(hw_its->cmd_buf + writep,
+                                             ITS_CMD_SIZE);
+    else
+        dsb(ishst);
+
+    writep = (writep + ITS_CMD_SIZE) % ITS_CMD_QUEUE_SZ;
+    writeq_relaxed(writep & BUFPTR_MASK, hw_its->its_base +

GITS_CWRITER);

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

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 97

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+}
+
+/* Wait for an ITS to finish processing all commands. */
+static int gicv3_its_wait_commands(struct host_its *hw_its)
+{
+    s_time_t deadline = NOW() + MILLISECS(100);

Same remark as above. Why 1ms and 100ms here?

I made this period longer, because it covers multiple commands and we are somewhat expected to wait here. Still it should be shorter than the Linux timeout, which is one second.

Quoted 22 lines
Quoted 19 lines
xen/arch/arm/gic-v3-its.c +5 -0
+    uint64_t readp, writep;
+
+    do {
+        spin_lock(&hw_its->cmd_lock);
+        readp = readq_relaxed(hw_its->its_base + GITS_CREADR) &

BUFPTR_MASK;
+ writep = readq_relaxed(hw_its->its_base + GITS_CWRITER) &
BUFPTR_MASK;

xen/arch/arm/gic-v3-its.c +11 -0
+        spin_unlock(&hw_its->cmd_lock);
+
+        if ( readp == writep )
+            return 0;
+
+        cpu_relax();
+        udelay(1);
+    } while ( NOW() <= deadline );
+
+    return -ETIMEDOUT;
+}

[...]

Quoted 13 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 145

Quoted 9 lines

+static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id,

xen/arch/arm/gic-v3-its.c +7 -0
+                             unsigned 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)));

I requested to drop the mask here and I didn't see any answer explaining why you would not do it. So this should be dropped unless you give me a reason to keep it.

Fixed in v3.

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    cmd[2] |= GITS_VALID_BIT;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
+/* Set up the (1:1) collection mapping for the given host CPU. */
+int gicv3_its_setup_collection(unsigned int cpu)
+{
+    struct host_its *its;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 161

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+    int ret;
+
+    list_for_each_entry(its, &host_its_list, entry)
+    {
+        if ( !its->cmd_buf )
+            continue;

Why this check?

Indeed a leftover from the older code. Given the current code flow, we shouldn't get here without the cmd_buf being initialized. Still I liked the idea of having some check here in case the code flow changes. Will drop this is v4.

Quoted 16 lines

[...]

xen/arch/arm/gic-v3-its.c retry: · line 338

Quoted 12 lines

+/*
+ * Before an ITS gets initialized, it should be in a quiescent state, where
+ * all outstanding commands and transactions have finished.
+ * So if the ITS is already enabled, turn it off and wait for all outstanding
+ * operations to get processed by polling the QUIESCENT bit.
+ */

xen/arch/arm/gic-v3-its.c +4 -0
+static int gicv3_disable_its(struct host_its *hw_its)
+{
+    uint32_t reg;
+    s_time_t deadline = NOW() + MILLISECS(100);

Why 100ms?

Same rationale as above: We are dealing with multiple commands here, also are expected to take a longer time.

Quoted 9 lines
Quoted 9 lines

static int gicv3_lpi_allocate_pendtable(uint64_t *reg)

 {
     uint64_t val;
xen/arch/arm/gic-v3.c +0 -0
index cc1e219..38dafe7 100644
@@ -666,7 +666,21 @@ static int __init gicv3_populate_rdist(void)
 
Quoted 8 lines

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

Quoted 5 lines
xen/arch/arm/gic-v3.c +2 -1
                 if ( typer & GICR_TYPER_PLPIS )
                 {
-                    int ret;
+                    paddr_t rdist_addr;
+                    int procnum, ret;

As mentioned in v1, procnum should be unsigned. If you disagree, please explain.

Fixed in v4.

Quoted 11 lines

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

Quoted 10 lines
xen/arch/arm/gic-v3.c +3 -0
+
+                    /*
+                     * 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

xen/arch/arm/gic-v3.c +2 -0
+                     * later be able to address those redistributors.
+                     */
Quoted 3 lines

Again, this comment does not look useful and is misleading as the code to get/set the redistributor information is living in gic-v3-lpi.c and not gic-v3-its.c.

But we need to prepare those values here (and only here) and it does make a lot of sense to explain this, since the ability to deal with both ways of representing a redistributor is not obvious.
And this is one of the connections between the ITS and the (GICv3) redistributors, where the line between them is not easy to draw. So in v4 I now reworded the comment to say that we hand this over to the ITS (instead of spoiling ITS implementation details).
Hope you are OK with this.

Quoted 28 lines

[...]

Quoted 5 lines
xen/include/asm-arm/gic_v3_its.h +0 -0
index d5facf0..8b493fb 100644

[...]

xen/include/asm-arm/gic_v3_its.h struct host_its { · line 109

Quoted 12 lines

#include <xen/device_tree.h>

#define HOST_ITS_FLUSH_CMD_QUEUE (1U << 0)

xen/include/asm-arm/gic_v3_its.h +2 -0
+#define HOST_ITS_USES_PTA               (1U << 1)
 
 /* data structure for each hardware ITS */
 struct host_its {
@@ -87,6 +106,7 @@ struct host_its {
     paddr_t addr;
     paddr_t size;
     void __iomem *its_base;
+    spinlock_t cmd_lock;

Again, initialization, clean-up of a field should be done in the same that added the field. Otherwise, this is a call to miss a bit of the code and makes more difficult for the reviewer.

Quoted 1 line

So please initialize cmd_lock in this patch and patch #6.

Fixed in v4.

Quoted 18 lines
Quoted 18 lines
xen/include/asm-arm/gic_v3_its.h +1 -0
     void *cmd_buf;
     unsigned int flags;
 };
@@ -107,6 +127,13 @@ int gicv3_lpi_init_rdist(void __iomem * rdist_base);
 int gicv3_lpi_init_host_lpis(unsigned int nr_lpis);
 int gicv3_its_init(void);
 
+/* Store the physical address and ID for each redistributor as read

from DT. */

xen/include/asm-arm/gic_v3_its.h +6 -0
+void gicv3_set_redist_address(paddr_t address, unsigned int redist_id);
+uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
+
+/* Map a collection for this host CPU to each host ITS. */
+int gicv3_its_setup_collection(unsigned int cpu);
+
 #else
Quoted 16 lines

xen/include/asm-arm/gic_v3_its.h static inline int gicv3_its_init(void) · line 171

Quoted 13 lines

static LIST_HEAD(host_its_list);

xen/include/asm-arm/gic_v3_its.h +8 -0
@@ -134,6 +161,17 @@ static inline int gicv3_its_init(void)
 {
     return 0;
 }
+
+static inline void gicv3_set_redist_address(paddr_t address,
+                                            unsigned int redist_id)
+{
+}
+
+static inline int gicv3_its_setup_collection(unsigned int cpu)
+{

This function should never be called as it is gated by the presence of ITS. I would add a BUG() with a comment to ensure this is the case.

Right, fixed that in v4.

Cheers,
Andre.

Quoted 11 lines

xen/include/asm-arm/gic_v3_its.h static inline int gicv3_its_init(void) · line 177

Quoted 8 lines
xen/include/asm-arm/gic_v3_its.h +3 -0
+    return 0;
+}
+
 #endif /* CONFIG_HAS_ITS */
 
 #endif

Cheers

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

#37 lore raw

Re: [PATCH v2 05/27] ARM: GICv3 ITS: introduce ITS command handling

Hi Andre,

Quoted 8 lines · Andre Przywara
Quoted 7 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 43

Quoted 5 lines
(quoted diff) +2 -0
+#define BUFPTR_MASK                     GENMASK(19, 5)
+static int its_send_command(struct host_its *hw_its, const void

*its_cmd)

xen/arch/arm/gic-v3-its.c +2 -0
+{
+    s_time_t deadline = NOW() + MILLISECS(1);

It would be nice to explain where does this value comes from.

Quoted 8 lines

In lack of any real data on this, one millisecond is just a guess. In v3 I added some comment stating that this is a "short" period to wait for. We don't want to stall for a long time for a single command, but also don't want to give up too easily. The command queue is handled by the ITS, which is hardware, so the only thing it does is handling commands. If we are unlucky enough to hit a full command queue due to a sudden spike in host commands, chances are that we get a free slot very soon afterwards. If not, we are screwed anyway.

It is not the first time I am saying that. If it is a guess, then this should be spelled out. A reader cannot know and we would have a question asking why "1 ms in Xen and 1 sec in Linux"...

Quoted 10 lines
Quoted 6 lines

xen/arch/arm/gic-v3-its.c bool gicv3_its_host_has_its(void) · line 97

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+/* Wait for an ITS to finish processing all commands. */
+static int gicv3_its_wait_commands(struct host_its *hw_its)
+{
+    s_time_t deadline = NOW() + MILLISECS(100);

Same remark as above. Why 1ms and 100ms here?

I made this period longer, because it covers multiple commands and we are somewhat expected to wait here. Still it should be shorter than the Linux timeout, which is one second.

See above.

[...]

Quoted 16 lines
Quoted 15 lines

xen/arch/arm/gic-v3-its.c retry: · line 338

Quoted 13 lines

+/*
+ * Before an ITS gets initialized, it should be in a quiescent state, where
+ * all outstanding commands and transactions have finished.
+ * So if the ITS is already enabled, turn it off and wait for all outstanding
+ * operations to get processed by polling the QUIESCENT bit.
+ */

xen/arch/arm/gic-v3-its.c +4 -0
+static int gicv3_disable_its(struct host_its *hw_its)
+{
+    uint32_t reg;
+    s_time_t deadline = NOW() + MILLISECS(100);

Why 100ms?

Quoted 2 lines

Same rationale as above: We are dealing with multiple commands here, also are expected to take a longer time.

See above.

[...]

Quoted 15 lines
Quoted 11 lines

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

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +3 -0
+
+                    /*
+                     * 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

xen/arch/arm/gic-v3.c +2 -0
+                     * later be able to address those redistributors.
+                     */
Quoted 3 lines

Again, this comment does not look useful and is misleading as the code to get/set the redistributor information is living in gic-v3-lpi.c and not gic-v3-its.c.

Quoted 8 lines

But we need to prepare those values here (and only here) and it does make a lot of sense to explain this, since the ability to deal with both ways of representing a redistributor is not obvious.
And this is one of the connections between the ITS and the (GICv3) redistributors, where the line between them is not easy to draw. So in v4 I now reworded the comment to say that we hand this over to the ITS (instead of spoiling ITS implementation details).
Hope you are OK with this.

Well, it makes sense to initialize the redistributor address in here and not in another place.

This kind of comments is more useful on the place where you initialize the ITS to explain why it is done there and not before.

Cheers,

Signature · 6 lines
Julien Grall

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

PATCH v2 6/27 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 an rbtree to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-v3-its.c        | 207 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           |   3 +
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/gic_v3_its.h |  18 ++++
 4 files changed, 231 insertions(+)
xen/arch/arm/gic-v3-its.c +207 -0
index 5c11b0d..60b15b5 100644
@@ -21,6 +21,8 @@
 #include <xen/lib.h>
 #include <xen/delay.h>
 #include <xen/mm.h>
+#include <xen/rbtree.h>
+#include <xen/sched.h>
 #include <xen/sizes.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
@@ -32,6 +34,17 @@
 
 LIST_HEAD(host_its_list);
 
+struct its_devices {
+    struct rb_node rbnode;
+    struct host_its *hw_its;
+    void *itt_addr;
+    paddr_t guest_doorbell;
+    uint32_t host_devid;
+    uint32_t guest_devid;
+    uint32_t eventids;
+    uint32_t *host_lpis;
+};
+
 bool gicv3_its_host_has_its(void)
 {
     return !list_empty(&host_its_list);
@@ -149,6 +162,24 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             uint8_t size_bits, paddr_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    if ( valid )
+    {
+        ASSERT(size_bits < 32);
+        ASSERT(!(itt_addr & ~GENMASK(51, 8)));
+    }
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = valid ? size_bits : 0x00;
+    cmd[2] = valid ? (itt_addr | GITS_VALID_BIT) : 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -379,6 +410,7 @@ static int gicv3_its_init_single_its(struct host_its *hw_its)
     devid_bits = min(devid_bits, max_its_device_bits);
     if ( reg & GITS_TYPER_PTA )
         hw_its->flags |= HOST_ITS_USES_PTA;
+    hw_its->itte_size = GITS_TYPER_ITT_SIZE(reg);
 
     for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
     {
@@ -428,6 +460,180 @@ int gicv3_its_init(void)
     return 0;
 }
 
+static int remove_mapped_guest_device(struct its_devices *dev)
+{
+    int ret;
+
+    if ( dev->hw_its )
+    {
+        int ret = its_send_cmd_mapd(dev->hw_its, dev->host_devid, 0, 0, false);
+        if ( ret )
+            return ret;
+    }
+
+    ret = gicv3_its_wait_commands(dev->hw_its);
+    if ( ret )
+        return ret;
+
+    xfree(dev->itt_addr);
+    xfree(dev);
+
+    return 0;
+}
+
+static struct host_its *gicv3_its_find_by_doorbell(paddr_t doorbell_address)
+{
+    struct host_its *hw_its;
+
+    list_for_each_entry(hw_its, &host_its_list, entry)
+    {
+        if ( hw_its->addr + ITS_DOORBELL_OFFSET == doorbell_address )
+            return hw_its;
+    }
+
+    return NULL;
+}
+
+static int compare_its_guest_devices(struct its_devices *dev,
+                                     paddr_t doorbell, uint32_t devid)
+{
+    if ( dev->guest_doorbell < doorbell )
+        return -1;
+
+    if ( dev->guest_doorbell > doorbell )
+        return 1;
+
+    if ( dev->guest_devid < devid )
+        return -1;
+
+    if ( dev->guest_devid > devid )
+        return 1;
+
+    return 0;
+}
+
+/*
+ * Map a hardware device, identified by a certain host ITS and its device ID
+ * to domain d, a guest ITS (identified by its doorbell address) and device ID.
+ * Also provide the number of events (MSIs) needed for that device.
+ * This does not check if this particular hardware device is already mapped
+ * at another domain, it is expected that this would be done by the caller.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell, guest_devid) )
+        {
+            if ( !valid )
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);
+        }
+
+        if ( compare_its_guest_devices(temp, guest_doorbell, guest_devid) > 0 )
+            new = &((*new)->rb_left);
+        else
+            new = &((*new)->rb_right);
+    }
+
+    if ( !valid )
+        goto out_unlock;
+
+    ret = -ENOMEM;
+
+    /* An Interrupt Translation Table needs to be 256-byte aligned. */
+    itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        goto out_unlock;
+
+    dev = xzalloc(struct its_devices);
+    if ( !dev )
+        goto out_unlock;
+
+    ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
+                            virt_to_maddr(itt_addr), true);
+    if ( ret )
+        goto out_unlock;
+
+    dev->itt_addr = itt_addr;
+    dev->hw_its = hw_its;
+    dev->guest_doorbell = guest_doorbell;
+    dev->guest_devid = guest_devid;
+    dev->host_devid = host_devid;
+    dev->eventids = nr_events;
+
+    rb_link_node(&dev->rbnode, parent, new);
+    rb_insert_color(&dev->rbnode, &d->arch.vgic.its_devices);
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+
+    return 0;
+
+out_unlock:
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+    if ( dev )
+        xfree(dev->host_lpis);
+    xfree(itt_addr);
+    xfree(dev);
+    return ret;
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+void gicv3_its_unmap_all_devices(struct domain *d)
+{
+    struct rb_node *victim;
+    struct its_devices *dev;
+
+    /*
+     * This is an easily readable, yet inefficient implementation.
+     * It uses the provided iteration wrapper and erases each node, which
+     * possibly triggers rebalancing.
+     * This seems overkill since we are going to abolish the whole tree, but
+     * avoids an open-coded re-implementation of the traversal functions with
+     * some recursive function calls.
+     * Performance does not matter here, since we are destroying a domain.
+     */
+restart:
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    if ( (victim = rb_first(&d->arch.vgic.its_devices)) )
+    {
+        dev = rb_entry(victim, struct its_devices, rbnode);
+        rb_erase(victim, &d->arch.vgic.its_devices);
+
+        spin_unlock(&d->arch.vgic.its_devices_lock);
+
+        remove_mapped_guest_device(dev);
+
+        goto restart;
+    }
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
@@ -455,6 +661,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/vgic-v3.c +3 -0
index d61479d..1fadb00 100644
@@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d)
     d->arch.vgic.nr_regions = rdist_count;
     d->arch.vgic.rdist_regions = rdist_regions;
 
+    spin_lock_init(&d->arch.vgic.its_devices_lock);
+    d->arch.vgic.its_devices = RB_ROOT;
+
     /*
      * Domain 0 gets the hardware address.
      * Guests get the virtual platform layout.
xen/include/asm-arm/domain.h +3 -0
index 2d6fbb1..00b9c1a 100644
@@ -11,6 +11,7 @@
 #include <asm/gic.h>
 #include <public/hvm/params.h>
 #include <xen/serial.h>
+#include <xen/rbtree.h>
 
 struct hvm_domain
 {
@@ -109,6 +110,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        struct rb_root its_devices;         /* devices mapped to an ITS */
+        spinlock_t its_devices_lock;        /* protects the its_devices tree */
 #endif
     } vgic;
 
xen/include/asm-arm/gic_v3_its.h +18 -0
index 8b493fb..3421ea0 100644
@@ -48,6 +48,10 @@
 #define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
                                                GITS_TYPER_DEVIDS_SHIFT) + 1)
 #define GITS_TYPER_IDBITS_SHIFT         8
+#define GITS_TYPER_ITT_SIZE_SHIFT       4
+#define GITS_TYPER_ITT_SIZE_MASK        (0xfUL << GITS_TYPER_ITT_SIZE_SHIFT)
+#define GITS_TYPER_ITT_SIZE(r)          ((((r) & GITS_TYPER_ITT_SIZE_MASK) >> \
+                                                GITS_TYPER_ITT_SIZE_SHIFT) + 1)
 
 #define GITS_IIDR_VALUE                 0x34c
 
@@ -94,7 +98,10 @@
 #define GITS_CMD_MOVALL                 0x0e
 #define GITS_CMD_DISCARD                0x0f
 
+#define ITS_DOORBELL_OFFSET             0x10040
+
 #include <xen/device_tree.h>
+#include <xen/rbtree.h>
 
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
@@ -108,6 +115,7 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    unsigned int itte_size;
     unsigned int flags;
 };
 
@@ -134,6 +142,16 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
 /* Map a collection for this host CPU to each host ITS. */
 int gicv3_its_setup_collection(unsigned int cpu);
 
+/*
+ * Map a device on the host by allocating an ITT on the host (ITS).
+ * "nr_event" specifies how many events (interrupts) this device will need.
+ * Setting "valid" to false deallocates the device.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid);
+
 #else
 
 static LIST_HEAD(host_its_list);
Signature · 7 lines
2.9.0


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

Re: [PATCH v2 06/27] ARM: GICv3 ITS: introduce device mapping

Hi Andre,

Quoted 29 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 an rbtree to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-v3-its.c        | 207 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           |   3 +
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/gic_v3_its.h |  18 ++++
 4 files changed, 231 insertions(+)
xen/arch/arm/gic-v3-its.c +2 -0
index 5c11b0d..60b15b5 100644
@@ -21,6 +21,8 @@
 #include <xen/lib.h>
 #include <xen/delay.h>
 #include <xen/mm.h>
+#include <xen/rbtree.h>
+#include <xen/sched.h>
 #include <xen/sizes.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>

xen/arch/arm/gic-v3-its.c line 41

Quoted 8 lines
xen/arch/arm/gic-v3-its.c +5 -0
@@ -32,6 +34,17 @@
 
 LIST_HEAD(host_its_list);
 
+struct its_devices {
+    struct rb_node rbnode;
+    struct host_its *hw_its;
+    void *itt_addr;
+    paddr_t guest_doorbell;

I think it would be worth to explain in the commit message why you need the guest_doorbell in the struct its_devices and how you plan to use it.

Quoted 11 lines
xen/arch/arm/gic-v3-its.c +6 -0
+    uint32_t host_devid;
+    uint32_t guest_devid;
+    uint32_t eventids;
+    uint32_t *host_lpis;
+};
+
 bool gicv3_its_host_has_its(void)
 {
     return !list_empty(&host_its_list);
@@ -149,6 +162,24 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id,
     return its_send_command(its, cmd);

xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 172

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +8 -0
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             uint8_t size_bits, paddr_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    if ( valid )
+    {
+        ASSERT(size_bits < 32);

It would be better if you do the check against the real number in hardware (i.e GITS_TYPER.ID_bits).

xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 176

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+        ASSERT(!(itt_addr & ~GENMASK(51, 8)));
+    }
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = valid ? size_bits : 0x00;

This is really confusing. The check was not on the previous version. So why do you need that?

Also, it would have been better to hide the "size - 1" in the helper avoiding to really on the caller to do the right thing.

xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 177

Quoted 1 line
xen/arch/arm/gic-v3-its.c +1 -0
+    cmd[2] = valid ? (itt_addr | GITS_VALID_BIT) : 0x00;

Ditto about "valid? ...".

[...]

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 490

Quoted 7 lines
xen/arch/arm/gic-v3-its.c +7 -0
+static struct host_its *gicv3_its_find_by_doorbell(paddr_t doorbell_address)
+{
+    struct host_its *hw_its;
+
+    list_for_each_entry(hw_its, &host_its_list, entry)
+    {
+        if ( hw_its->addr + ITS_DOORBELL_OFFSET == doorbell_address )

Why not storing the ITS address rather than the doorbell to avoid this check?

[...]

Quoted 20 lines
xen/arch/arm/gic-v3-its.c +20 -0
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 551

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell, guest_devid) )
+        {
+            if ( !valid )
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )

Again, a printk(XENLOG_GUEST...) here would be useful to know which host DeviceID was associated to the guest DeviceID.

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 554

Quoted 3 lines
xen/arch/arm/gic-v3-its.c +3 -0
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);

Just above you removed the device from the RB-tree but this function may fail and never free the memory. This means that memory will be leaked leading to a potential denial of service.

Quoted 13 lines
xen/arch/arm/gic-v3-its.c +13 -0
+        }
+
+        if ( compare_its_guest_devices(temp, guest_doorbell, guest_devid) > 0 )
+            new = &((*new)->rb_left);
+        else
+            new = &((*new)->rb_right);
+    }
+
+    if ( !valid )
+        goto out_unlock;
+
+    ret = -ENOMEM;
+

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 577

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    /* An Interrupt Translation Table needs to be 256-byte aligned. */
+    itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        goto out_unlock;
+
+    dev = xzalloc(struct its_devices);
+    if ( !dev )
+        goto out_unlock;
+
+    ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,

I don't understand why nr_events - 1. Can you explain?

[...]

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+/* Removing any connections a domain had to any ITS in the system. */
+void gicv3_its_unmap_all_devices(struct domain *d)
+{
+    struct rb_node *victim;

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 618

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    struct its_devices *dev;
+
+    /*
+     * This is an easily readable, yet inefficient implementation.
+     * It uses the provided iteration wrapper and erases each node, which
+     * possibly triggers rebalancing.
+     * This seems overkill since we are going to abolish the whole tree, but
+     * avoids an open-coded re-implementation of the traversal functions with
+     * some recursive function calls.
+     * Performance does not matter here, since we are destroying a domain.

Again, this is slightly untrue. Performance matter when destroying a domain as Xen cannot be preempted. So if it takes too long, you will have an impact on the overall system.

However, I think it would be fair to assume that all device will be deassigned before the ITS is destroyed. So I would just drop this function. Note that we have the same assumption in the SMMU driver.

Quoted 22 lines
xen/arch/arm/gic-v3-its.c +18 -0
+     */
+restart:
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    if ( (victim = rb_first(&d->arch.vgic.its_devices)) )
+    {
+        dev = rb_entry(victim, struct its_devices, rbnode);
+        rb_erase(victim, &d->arch.vgic.its_devices);
+
+        spin_unlock(&d->arch.vgic.its_devices_lock);
+
+        remove_mapped_guest_device(dev);
+
+        goto restart;
+    }
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {

xen/arch/arm/gic-v3-its.c void gicv3_its_dt_init(const struct dt_device_node *node) · line 664

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +1 -0
@@ -455,6 +661,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);

This should be in patch #5.

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

Quoted 13 lines
xen/arch/arm/gic-v3-its.c +0 -0
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/arch/arm/vgic-v3.c +2 -0
index d61479d..1fadb00 100644
@@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d)
     d->arch.vgic.nr_regions = rdist_count;
     d->arch.vgic.rdist_regions = rdist_regions;
 
+    spin_lock_init(&d->arch.vgic.its_devices_lock);
+    d->arch.vgic.its_devices = RB_ROOT;

Again, the placement of those 2 lines are likely wrong. This should belong to the vITS and not the vgic-v3.

I think it would make sense to get a patch that introduces a skeleton for the vITS before this patch and start plumbing through.

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

Quoted 4 lines
xen/arch/arm/vgic-v3.c +1 -0
+
     /*
      * Domain 0 gets the hardware address.
      * Guests get the virtual platform layout.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 06/27] ARM: GICv3 ITS: introduce device mapping

Hi,

Quoted 36 lines · Julien Grall

Hi Andre,

Quoted 33 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 an rbtree to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-v3-its.c        | 207
+++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           |   3 +
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/gic_v3_its.h |  18 ++++
 4 files changed, 231 insertions(+)
xen/arch/arm/gic-v3-its.c +2 -0
index 5c11b0d..60b15b5 100644
@@ -21,6 +21,8 @@
 #include <xen/lib.h>
 #include <xen/delay.h>
 #include <xen/mm.h>
+#include <xen/rbtree.h>
+#include <xen/sched.h>
 #include <xen/sizes.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
@@ -32,6 +34,17 @@
 
 LIST_HEAD(host_its_list);
 
Quoted 8 lines

xen/arch/arm/gic-v3-its.c line 41

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+struct its_devices {
+    struct rb_node rbnode;
+    struct host_its *hw_its;
+    void *itt_addr;
+    paddr_t guest_doorbell;

I think it would be worth to explain in the commit message why you need the guest_doorbell in the struct its_devices and how you plan to use it.

In v4 I now also elaborated on the reason in a comment (before the struct), which I deem more useful than something in the commit message.

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/gic-v3-its.c +6 -0
+    uint32_t host_devid;
+    uint32_t guest_devid;
+    uint32_t eventids;
+    uint32_t *host_lpis;
+};
+
 bool gicv3_its_host_has_its(void)
 {
     return !list_empty(&host_its_list);
@@ -149,6 +162,24 @@ static int its_send_cmd_mapc(struct host_its
 *its, uint32_t collection_id,
     return its_send_command(its, cmd);
 }
 
Quoted 12 lines

xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 172

Quoted 9 lines
xen/arch/arm/gic-v3-its.c +8 -0
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             uint8_t size_bits, paddr_t itt_addr,
 bool valid)
+{
+    uint64_t cmd[4];
+
+    if ( valid )
+    {
+        ASSERT(size_bits < 32);

It would be better if you do the check against the real number in hardware (i.e GITS_TYPER.ID_bits).

Added in v4.

Quoted 8 lines

xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 176

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +4 -0
+        ASSERT(!(itt_addr & ~GENMASK(51, 8)));
+    }
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = valid ? size_bits : 0x00;

This is really confusing. The check was not on the previous version. So why do you need that?

Admittedly I was taken away be some intention to check this here properly. But since itt_addr and size are only valid with V=1, I removed this in v3.

Quoted 2 lines

Also, it would have been better to hide the "size - 1" in the helper avoiding to really on the caller to do the right thing.

I tend to agree, but then we have the awkward case where an unmap passes 0 in size, which then gets decremented by one. But you are right that it's still saner this way, so I pass 1 now in the unmap call and do the "-1" encoding in here.

Quoted 3 lines

xen/arch/arm/gic-v3-its.c static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id, · line 177

Quoted 1 line
xen/arch/arm/gic-v3-its.c +1 -0
+    cmd[2] = valid ? (itt_addr | GITS_VALID_BIT) : 0x00;

Ditto about "valid? ...".

Removed in v3.

Quoted 13 lines

[...]

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 490

Quoted 8 lines

+static struct host_its *gicv3_its_find_by_doorbell(paddr_t doorbell_address)

xen/arch/arm/gic-v3-its.c +6 -0
+{
+    struct host_its *hw_its;
+
+    list_for_each_entry(hw_its, &host_its_list, entry)
+    {
+        if ( hw_its->addr + ITS_DOORBELL_OFFSET == doorbell_address )

Why not storing the ITS address rather than the doorbell to avoid this check?

Because the doorbell address is a nice architectural property of MSIs in general. And we need this check anyway, it's just the addition of the doorbell offset that is different.

Quoted 31 lines

[...]

Quoted 29 lines
xen/arch/arm/gic-v3-its.c +2 -0
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t

host_devid,
+ paddr_t guest_doorbell, uint32_t
guest_devid,

xen/arch/arm/gic-v3-its.c +6 -0
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent

= NULL;

xen/arch/arm/gic-v3-its.c +14 -0
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell,

guest_devid) )

xen/arch/arm/gic-v3-its.c +2 -0
+        {
+            if ( !valid )
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 551

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )

Again, a printk(XENLOG_GUEST...) here would be useful to know which host DeviceID was associated to the guest DeviceID.

I added a gprintk(XENLOG_DEBUG, ), which I think is more appropriate (as it may spam the console when some stupid guest is running). Let me know if you want to have a different loglevel.

Quoted 7 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 554

Quoted 3 lines
xen/arch/arm/gic-v3-its.c +3 -0
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);

Just above you removed the device from the RB-tree but this function may fail and never free the memory. This means that memory will be leaked leading to a potential denial of service.

So I fixed this case in v4, though there is still a tiny chance of a
memleak: if the MAPD(V=0) command fails. We can't free the ITT table then, really, because it still belongs to the ITS. I don't think we can do much about it, though.
I free the other allocations of the memory now, anyway.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/gic-v3-its.c +3 -0
+        }
+
+        if ( compare_its_guest_devices(temp, guest_doorbell,

guest_devid) > 0 )

xen/arch/arm/gic-v3-its.c +14 -0
+            new = &((*new)->rb_left);
+        else
+            new = &((*new)->rb_right);
+    }
+
+    if ( !valid )
+        goto out_unlock;
+
+    ret = -ENOMEM;
+
+    /* An Interrupt Translation Table needs to be 256-byte aligned. */
+    itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        goto out_unlock;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 577

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+
+    dev = xzalloc(struct its_devices);
+    if ( !dev )
+        goto out_unlock;
+
+    ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,

I don't understand why nr_events - 1. Can you explain?

Xen lacks an ilog2, so "fls" is the closest I could find. "fls" has this slightly weird semantic (from the Linux source):
"Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32."
I think this translates into: "How many bits do I need to express this number?". For our case the highest event number we need to encode is "nr_events - 1", hence the subtraction.
So is it worth to introduce a:

	static inline int ilog2_64(uint64_t n) ...

in xen/include/asm-arm/bitops.h to document this?

Quoted 17 lines

[...]

Quoted 14 lines
xen/arch/arm/gic-v3-its.c +9 -0
+/* Removing any connections a domain had to any ITS in the system. */
+void gicv3_its_unmap_all_devices(struct domain *d)
+{
+    struct rb_node *victim;
+    struct its_devices *dev;
+
+    /*
+     * This is an easily readable, yet inefficient implementation.
+     * It uses the provided iteration wrapper and erases each node,

which

xen/arch/arm/gic-v3-its.c +2 -0
+     * possibly triggers rebalancing.
+     * This seems overkill since we are going to abolish the whole

tree, but
+ * avoids an open-coded re-implementation of the traversal

Quoted 8 lines
Quoted 4 lines

functions with

xen/arch/arm/gic-v3-its.c +2 -0
+     * some recursive function calls.
+     * Performance does not matter here, since we are destroying a

domain.

Again, this is slightly untrue. Performance matter when destroying a domain as Xen cannot be preempted. So if it takes too long, you will have an impact on the overall system.

I reworded this sentence in v3, since you apparently misunderstood me. By inefficient I meant sub-optimal, but this is not a _critical_ path, so we don't care too much. The execution time is clearly bounded by the number of devices. We simply shouldn't allow gazillion of devices on a DomU if we care about those things.

Quoted 3 lines

However, I think it would be fair to assume that all device will be deassigned before the ITS is destroyed. So I would just drop this function. Note that we have the same assumption in the SMMU driver.

Well, why not keep it as a safeguard, then? If a domain gets destroyed, aren't we supposed to clean up?

Quoted 22 lines
Quoted 22 lines
xen/arch/arm/gic-v3-its.c +18 -0
+     */
+restart:
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    if ( (victim = rb_first(&d->arch.vgic.its_devices)) )
+    {
+        dev = rb_entry(victim, struct its_devices, rbnode);
+        rb_erase(victim, &d->arch.vgic.its_devices);
+
+        spin_unlock(&d->arch.vgic.its_devices_lock);
+
+        remove_mapped_guest_device(dev);
+
+        goto restart;
+    }
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out

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

Quoted 8 lines

xen/arch/arm/gic-v3-its.c void gicv3_its_dt_init(const struct dt_device_node · line 665

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1 -0
@@ -455,6 +661,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);

This should be in patch #5.

Done in v4.

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/gic-v3-its.c +0 -0
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/arch/arm/vgic-v3.c +0 -0
index d61479d..1fadb00 100644
@@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d)
     d->arch.vgic.nr_regions = rdist_count;
     d->arch.vgic.rdist_regions = rdist_regions;
 
Quoted 5 lines

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

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+    spin_lock_init(&d->arch.vgic.its_devices_lock);
+    d->arch.vgic.its_devices = RB_ROOT;

Again, the placement of those 2 lines are likely wrong. This should belong to the vITS and not the vgic-v3.

Well, it's a "domain which has an ITS" thing, as this is not per ITS. So far we only have an per-ITS init function, as we don't iterate over the host ITSes there anymore.
So I refrained from adding a separate function to initialize these simple and generic data structures. Please let me know if you insist on some ITS-per-domain init function.

Cheers,
Andre.

Quoted 10 lines

I think it would make sense to get a patch that introduces a skeleton for the vITS before this patch and start plumbing through.

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

Quoted 4 lines
xen/arch/arm/vgic-v3.c +1 -0
+
     /*
      * Domain 0 gets the hardware address.
      * Guests get the virtual platform layout.

Cheers,

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

#41 lore raw

Re: [PATCH v2 06/27] ARM: GICv3 ITS: introduce device mapping

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 39 lines · Julien Grall
Quoted 29 lines
Quoted 29 lines
(quoted diff) +2 -0
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t

host_devid,
+ paddr_t guest_doorbell, uint32_t
guest_devid,

xen/arch/arm/gic-v3-its.c +6 -0
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent

= NULL;

xen/arch/arm/gic-v3-its.c +14 -0
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell,

guest_devid) )

(quoted diff) +2 -0
+        {
+            if ( !valid )
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 551

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )

Again, a printk(XENLOG_GUEST...) here would be useful to know which host DeviceID was associated to the guest DeviceID.

Quoted 3 lines

I added a gprintk(XENLOG_DEBUG, ), which I think is more appropriate (as it may spam the console when some stupid guest is running). Let me know if you want to have a different loglevel.

I don't think this is more appropriate. gprintk will print the domain ID of the current domain, whilst this function will be called by the toolstack in the future.

Furthemore, if you look at the implementation of gprintk you will notice that it is basically turning into printk(XENLOG_GUEST...) and adding information of the current vCPU.

What matters for ratelimiting is XENLOG_GUEST.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
xen/arch/arm/gic-v3-its.c +3 -0
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);
Quoted 8 lines
Quoted 3 lines

Just above you removed the device from the RB-tree but this function may fail and never free the memory. This means that memory will be leaked leading to a potential denial of service.

So I fixed this case in v4, though there is still a tiny chance of a
memleak: if the MAPD(V=0) command fails. We can't free the ITT table then, really, because it still belongs to the ITS. I don't think we can do much about it, though.

This is a leak and even tiny is quite worrying. How do you plan to address this in the future? What is the best thing to do?

Quoted 36 lines

I free the other allocations of the memory now, anyway.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/gic-v3-its.c +3 -0
+        }
+
+        if ( compare_its_guest_devices(temp, guest_doorbell,

guest_devid) > 0 )

xen/arch/arm/gic-v3-its.c +14 -0
+            new = &((*new)->rb_left);
+        else
+            new = &((*new)->rb_right);
+    }
+
+    if ( !valid )
+        goto out_unlock;
+
+    ret = -ENOMEM;
+
+    /* An Interrupt Translation Table needs to be 256-byte aligned. */
+    itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        goto out_unlock;
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 577

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+
+    dev = xzalloc(struct its_devices);
+    if ( !dev )
+        goto out_unlock;
+
+    ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,

I don't understand why nr_events - 1. Can you explain?

Xen lacks an ilog2, so "fls" is the closest I could find. "fls" has this slightly weird semantic (from the Linux source):
"Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32."
I think this translates into: "How many bits do I need to express this number?". For our case the highest event number we need to encode is "nr_events - 1", hence the subtraction.
So is it worth to introduce a:

Quoted 2 lines
	static inline int ilog2_64(uint64_t n) ...

in xen/include/asm-arm/bitops.h to document this?

This might make easier to read the code.

Quoted 26 lines
Quoted 17 lines

[...]

Quoted 14 lines
xen/arch/arm/gic-v3-its.c +9 -0
+/* Removing any connections a domain had to any ITS in the system. */
+void gicv3_its_unmap_all_devices(struct domain *d)
+{
+    struct rb_node *victim;
+    struct its_devices *dev;
+
+    /*
+     * This is an easily readable, yet inefficient implementation.
+     * It uses the provided iteration wrapper and erases each node,

which

(quoted diff) +2 -0
+     * possibly triggers rebalancing.
+     * This seems overkill since we are going to abolish the whole

tree, but
+ * avoids an open-coded re-implementation of the traversal

Quoted 8 lines
Quoted 4 lines

functions with

(quoted diff) +2 -0
+     * some recursive function calls.
+     * Performance does not matter here, since we are destroying a

domain.

Again, this is slightly untrue. Performance matter when destroying a domain as Xen cannot be preempted. So if it takes too long, you will have an impact on the overall system.

Quoted 5 lines

I reworded this sentence in v3, since you apparently misunderstood me. By inefficient I meant sub-optimal, but this is not a _critical_ path, so we don't care too much. The execution time is clearly bounded by the number of devices. We simply shouldn't allow gazillion of devices on a DomU if we care about those things.

This is a very naive way of thinking how domain destruction is working on Xen. Domain destruction is likely to get time, you have to release all the resources (memory, devices...). So we usually split in smaller pieces to be able to handle interrupts or allowing preemption.

Quoted 7 lines
Quoted 4 lines

However, I think it would be fair to assume that all device will be deassigned before the ITS is destroyed. So I would just drop this function. Note that we have the same assumption in the SMMU driver.

Well, why not keep it as a safeguard, then? If a domain gets destroyed, aren't we supposed to clean up?

See above. We use ASSERT(....) in the SMMU code to check this assumption.

Quoted 17 lines
Quoted 11 lines
Quoted 11 lines
         printk("GICv3: Found ITS @0x%lx\n", addr);
xen/arch/arm/vgic-v3.c +0 -0
index d61479d..1fadb00 100644
@@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d)
     d->arch.vgic.nr_regions = rdist_count;
     d->arch.vgic.rdist_regions = rdist_regions;
 
Quoted 5 lines

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

Quoted 2 lines
xen/arch/arm/vgic-v3.c +2 -0
+    spin_lock_init(&d->arch.vgic.its_devices_lock);
+    d->arch.vgic.its_devices = RB_ROOT;

Again, the placement of those 2 lines are likely wrong. This should belong to the vITS and not the vgic-v3.

Quoted 6 lines

Well, it's a "domain which has an ITS" thing, as this is not per ITS. So far we only have an per-ITS init function, as we don't iterate over the host ITSes there anymore.
So I refrained from adding a separate function to initialize these simple and generic data structures. Please let me know if you insist on some ITS-per-domain init function.

Where is the limit to refrain from creating function then? What matter is logic...

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 06/27] ARM: GICv3 ITS: introduce device mapping

Hi,

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

Hi,

Hi Andre,

Quoted 39 lines · Julien Grall
Quoted 29 lines
Quoted 29 lines
(quoted diff) +2 -0
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t

host_devid,
+ paddr_t guest_doorbell, uint32_t
guest_devid,

xen/arch/arm/gic-v3-its.c +6 -0
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent

= NULL;

xen/arch/arm/gic-v3-its.c +14 -0
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell,

guest_devid) )

(quoted diff) +2 -0
+        {
+            if ( !valid )
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 551

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )

Again, a printk(XENLOG_GUEST...) here would be useful to know which host DeviceID was associated to the guest DeviceID.

Quoted 3 lines

I added a gprintk(XENLOG_DEBUG, ), which I think is more appropriate (as it may spam the console when some stupid guest is running). Let me know if you want to have a different loglevel.

I don't think this is more appropriate. gprintk will print the domain ID of the current domain, whilst this function will be called by the toolstack in the future.

Furthemore, if you look at the implementation of gprintk you will notice that it is basically turning into printk(XENLOG_GUEST...) and adding information of the current vCPU.

What matters for ratelimiting is XENLOG_GUEST.

Quoted 9 lines
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 554

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +3 -0
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);

Just above you removed the device from the RB-tree but this function may fail and never free the memory. This means that memory will be leaked leading to a potential denial of service.

Quoted 7 lines
Quoted 4 lines

So I fixed this case in v4, though there is still a tiny chance of a
memleak: if the MAPD(V=0) command fails. We can't free the ITT table then, really, because it still belongs to the ITS. I don't think we can do much about it, though.

This is a leak and even tiny is quite worrying. How do you plan to address this in the future? What is the best thing to do?

In an ideal world we would probably have a spillover queue for ITS commands. Whenever a command can't be queued to the hardware ITS queue immediately, the guest should be put to wait somehow and the commands recorded, to be executed whenever the hardware command queue gets free slots again.
However I think we can't really do this today, because we don't have a good way of putting a guest to sleep when it trapped on an MMIO access. When in the future device assignment / de-assignment is handled via a hypercall, we can probably address this more easily.

As for the likeliness: I think this is extremely rare. With our large command queue and the ITS running at multiple hundred MHz I don't think we will ever run into this situation, really, especially not with just Dom0.

So for now are we OK with this comment noting the corner case? Maybe a TODO?

Cheers,
Andre.

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

#43 lore raw

Re: [PATCH v2 06/27] ARM: GICv3 ITS: introduce device mapping

In reply to Andre Przywara
Quoted 99 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 an rbtree to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-v3-its.c        | 207 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           |   3 +
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/gic_v3_its.h |  18 ++++
 4 files changed, 231 insertions(+)
xen/arch/arm/gic-v3-its.c +48 -0
index 5c11b0d..60b15b5 100644
@@ -21,6 +21,8 @@
 #include <xen/lib.h>
 #include <xen/delay.h>
 #include <xen/mm.h>
+#include <xen/rbtree.h>
+#include <xen/sched.h>
 #include <xen/sizes.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
@@ -32,6 +34,17 @@
 
 LIST_HEAD(host_its_list);
 
+struct its_devices {
+    struct rb_node rbnode;
+    struct host_its *hw_its;
+    void *itt_addr;
+    paddr_t guest_doorbell;
+    uint32_t host_devid;
+    uint32_t guest_devid;
+    uint32_t eventids;
+    uint32_t *host_lpis;
+};
+
 bool gicv3_its_host_has_its(void)
 {
     return !list_empty(&host_its_list);
@@ -149,6 +162,24 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             uint8_t size_bits, paddr_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    if ( valid )
+    {
+        ASSERT(size_bits < 32);
+        ASSERT(!(itt_addr & ~GENMASK(51, 8)));
+    }
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = valid ? size_bits : 0x00;
+    cmd[2] = valid ? (itt_addr | GITS_VALID_BIT) : 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -379,6 +410,7 @@ static int gicv3_its_init_single_its(struct host_its *hw_its)
     devid_bits = min(devid_bits, max_its_device_bits);
     if ( reg & GITS_TYPER_PTA )
         hw_its->flags |= HOST_ITS_USES_PTA;
+    hw_its->itte_size = GITS_TYPER_ITT_SIZE(reg);
 
     for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
     {
@@ -428,6 +460,180 @@ int gicv3_its_init(void)
     return 0;
 }
 
+static int remove_mapped_guest_device(struct its_devices *dev)
+{
+    int ret;
+
+    if ( dev->hw_its )
+    {
+        int ret = its_send_cmd_mapd(dev->hw_its, dev->host_devid, 0, 0, false);
+        if ( ret )
+            return ret;
+    }
+
+    ret = gicv3_its_wait_commands(dev->hw_its);
+    if ( ret )
+        return ret;
+
+    xfree(dev->itt_addr);

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 488

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    xfree(dev);
+
+    return 0;
+}
+
+static struct host_its *gicv3_its_find_by_doorbell(paddr_t doorbell_address)
+{
+    struct host_its *hw_its;
+
+    list_for_each_entry(hw_its, &host_its_list, entry)

Does this need to take a spinlock to protect host_its_list? I guess not because the list is not modified after boot?

Quoted 58 lines
xen/arch/arm/gic-v3-its.c +58 -0
+    {
+        if ( hw_its->addr + ITS_DOORBELL_OFFSET == doorbell_address )
+            return hw_its;
+    }
+
+    return NULL;
+}
+
+static int compare_its_guest_devices(struct its_devices *dev,
+                                     paddr_t doorbell, uint32_t devid)
+{
+    if ( dev->guest_doorbell < doorbell )
+        return -1;
+
+    if ( dev->guest_doorbell > doorbell )
+        return 1;
+
+    if ( dev->guest_devid < devid )
+        return -1;
+
+    if ( dev->guest_devid > devid )
+        return 1;
+
+    return 0;
+}
+
+/*
+ * Map a hardware device, identified by a certain host ITS and its device ID
+ * to domain d, a guest ITS (identified by its doorbell address) and device ID.
+ * Also provide the number of events (MSIs) needed for that device.
+ * This does not check if this particular hardware device is already mapped
+ * at another domain, it is expected that this would be done by the caller.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell, guest_devid) )
+        {
+            if ( !valid )

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 556

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);
+        }
+

Plese don't run compare_its_guest_devices twice with the same arguments, just store the return value.

Quoted 33 lines
xen/arch/arm/gic-v3-its.c +33 -0
+        if ( compare_its_guest_devices(temp, guest_doorbell, guest_devid) > 0 )
+            new = &((*new)->rb_left);
+        else
+            new = &((*new)->rb_right);
+    }
+
+    if ( !valid )
+        goto out_unlock;
+
+    ret = -ENOMEM;
+
+    /* An Interrupt Translation Table needs to be 256-byte aligned. */
+    itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        goto out_unlock;
+
+    dev = xzalloc(struct its_devices);
+    if ( !dev )
+        goto out_unlock;
+
+    ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
+                            virt_to_maddr(itt_addr), true);
+    if ( ret )
+        goto out_unlock;
+
+    dev->itt_addr = itt_addr;
+    dev->hw_its = hw_its;
+    dev->guest_doorbell = guest_doorbell;
+    dev->guest_devid = guest_devid;
+    dev->host_devid = host_devid;
+    dev->eventids = nr_events;
+
+    rb_link_node(&dev->rbnode, parent, new);

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 599

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    rb_insert_color(&dev->rbnode, &d->arch.vgic.its_devices);
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+
+    return 0;
+
+out_unlock:
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+    if ( dev )
+        xfree(dev->host_lpis);

Where is host_lpis allocated? Why is it freed here?

Quoted 23 lines
xen/arch/arm/gic-v3-its.c +23 -0
+    xfree(itt_addr);
+    xfree(dev);
+    return ret;
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+void gicv3_its_unmap_all_devices(struct domain *d)
+{
+    struct rb_node *victim;
+    struct its_devices *dev;
+
+    /*
+     * This is an easily readable, yet inefficient implementation.
+     * It uses the provided iteration wrapper and erases each node, which
+     * possibly triggers rebalancing.
+     * This seems overkill since we are going to abolish the whole tree, but
+     * avoids an open-coded re-implementation of the traversal functions with
+     * some recursive function calls.
+     * Performance does not matter here, since we are destroying a domain.
+     */
+restart:
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    if ( (victim = rb_first(&d->arch.vgic.its_devices)) )

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 632

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    {
+        dev = rb_entry(victim, struct its_devices, rbnode);
+        rb_erase(victim, &d->arch.vgic.its_devices);
+
+        spin_unlock(&d->arch.vgic.its_devices_lock);
+
+        remove_mapped_guest_device(dev);
+
+        goto restart;
+    }

There is no need to use goto here, a simple while loop should suffice.

xen/arch/arm/gic-v3-its.c void gicv3_its_dt_init(const struct dt_device_node *node) · line 664

Quoted 11 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
@@ -455,6 +661,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);

This change should be in the previous patch

Quoted 64 lines
xen/arch/arm/gic-v3-its.c +0 -0
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/arch/arm/vgic-v3.c +3 -0
index d61479d..1fadb00 100644
@@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d)
     d->arch.vgic.nr_regions = rdist_count;
     d->arch.vgic.rdist_regions = rdist_regions;
 
+    spin_lock_init(&d->arch.vgic.its_devices_lock);
+    d->arch.vgic.its_devices = RB_ROOT;
+
     /*
      * Domain 0 gets the hardware address.
      * Guests get the virtual platform layout.
xen/include/asm-arm/domain.h +3 -0
index 2d6fbb1..00b9c1a 100644
@@ -11,6 +11,7 @@
 #include <asm/gic.h>
 #include <public/hvm/params.h>
 #include <xen/serial.h>
+#include <xen/rbtree.h>
 
 struct hvm_domain
 {
@@ -109,6 +110,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        struct rb_root its_devices;         /* devices mapped to an ITS */
+        spinlock_t its_devices_lock;        /* protects the its_devices tree */
 #endif
     } vgic;
 
xen/include/asm-arm/gic_v3_its.h +7 -0
index 8b493fb..3421ea0 100644
@@ -48,6 +48,10 @@
 #define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
                                                GITS_TYPER_DEVIDS_SHIFT) + 1)
 #define GITS_TYPER_IDBITS_SHIFT         8
+#define GITS_TYPER_ITT_SIZE_SHIFT       4
+#define GITS_TYPER_ITT_SIZE_MASK        (0xfUL << GITS_TYPER_ITT_SIZE_SHIFT)
+#define GITS_TYPER_ITT_SIZE(r)          ((((r) & GITS_TYPER_ITT_SIZE_MASK) >> \
+                                                GITS_TYPER_ITT_SIZE_SHIFT) + 1)
 
 #define GITS_IIDR_VALUE                 0x34c
 
@@ -94,7 +98,10 @@
 #define GITS_CMD_MOVALL                 0x0e
 #define GITS_CMD_DISCARD                0x0f
 
+#define ITS_DOORBELL_OFFSET             0x10040
+
 #include <xen/device_tree.h>
+#include <xen/rbtree.h>
 
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)

xen/include/asm-arm/gic_v3_its.h struct host_its { · line 120

Quoted 6 lines
xen/include/asm-arm/gic_v3_its.h +1 -0
@@ -108,6 +115,7 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    unsigned int itte_size;
     unsigned int flags;
 };

I would move itte_size and its initialization to the patch that introduced struct host_its.

Quoted 20 lines
xen/include/asm-arm/gic_v3_its.h +10 -0
@@ -134,6 +142,16 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
 /* Map a collection for this host CPU to each host ITS. */
 int gicv3_its_setup_collection(unsigned int cpu);
 
+/*
+ * Map a device on the host by allocating an ITT on the host (ITS).
+ * "nr_event" specifies how many events (interrupts) this device will need.
+ * Setting "valid" to false deallocates the device.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid);
+
 #else
 
 static LIST_HEAD(host_its_list);
Signature · 1 line
2.9.0

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

#44 lore raw

Re: [PATCH v2 06/27] ARM: GICv3 ITS: introduce device mapping

Hi,

Quoted 105 lines · Stefano Stabellini
Quoted 104 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 an rbtree to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-v3-its.c        | 207 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           |   3 +
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/gic_v3_its.h |  18 ++++
 4 files changed, 231 insertions(+)
xen/arch/arm/gic-v3-its.c +53 -0
index 5c11b0d..60b15b5 100644
@@ -21,6 +21,8 @@
 #include <xen/lib.h>
 #include <xen/delay.h>
 #include <xen/mm.h>
+#include <xen/rbtree.h>
+#include <xen/sched.h>
 #include <xen/sizes.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
@@ -32,6 +34,17 @@
 
 LIST_HEAD(host_its_list);
 
+struct its_devices {
+    struct rb_node rbnode;
+    struct host_its *hw_its;
+    void *itt_addr;
+    paddr_t guest_doorbell;
+    uint32_t host_devid;
+    uint32_t guest_devid;
+    uint32_t eventids;
+    uint32_t *host_lpis;
+};
+
 bool gicv3_its_host_has_its(void)
 {
     return !list_empty(&host_its_list);
@@ -149,6 +162,24 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             uint8_t size_bits, paddr_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    if ( valid )
+    {
+        ASSERT(size_bits < 32);
+        ASSERT(!(itt_addr & ~GENMASK(51, 8)));
+    }
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = valid ? size_bits : 0x00;
+    cmd[2] = valid ? (itt_addr | GITS_VALID_BIT) : 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -379,6 +410,7 @@ static int gicv3_its_init_single_its(struct host_its *hw_its)
     devid_bits = min(devid_bits, max_its_device_bits);
     if ( reg & GITS_TYPER_PTA )
         hw_its->flags |= HOST_ITS_USES_PTA;
+    hw_its->itte_size = GITS_TYPER_ITT_SIZE(reg);
 
     for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
     {
@@ -428,6 +460,180 @@ int gicv3_its_init(void)
     return 0;
 }
 
+static int remove_mapped_guest_device(struct its_devices *dev)
+{
+    int ret;
+
+    if ( dev->hw_its )
+    {
+        int ret = its_send_cmd_mapd(dev->hw_its, dev->host_devid, 0, 0, false);
+        if ( ret )
+            return ret;
+    }
+
+    ret = gicv3_its_wait_commands(dev->hw_its);
+    if ( ret )
+        return ret;
+
+    xfree(dev->itt_addr);
+    xfree(dev);
+
+    return 0;
+}
+
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 488

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+static struct host_its *gicv3_its_find_by_doorbell(paddr_t doorbell_address)
+{
+    struct host_its *hw_its;
+
+    list_for_each_entry(hw_its, &host_its_list, entry)

Does this need to take a spinlock to protect host_its_list? I guess not because the list is not modified after boot?

Exactly, I added a comment in v4 explaining this.

Quoted 63 lines
Quoted 63 lines
xen/arch/arm/gic-v3-its.c +63 -0
+    {
+        if ( hw_its->addr + ITS_DOORBELL_OFFSET == doorbell_address )
+            return hw_its;
+    }
+
+    return NULL;
+}
+
+static int compare_its_guest_devices(struct its_devices *dev,
+                                     paddr_t doorbell, uint32_t devid)
+{
+    if ( dev->guest_doorbell < doorbell )
+        return -1;
+
+    if ( dev->guest_doorbell > doorbell )
+        return 1;
+
+    if ( dev->guest_devid < devid )
+        return -1;
+
+    if ( dev->guest_devid > devid )
+        return 1;
+
+    return 0;
+}
+
+/*
+ * Map a hardware device, identified by a certain host ITS and its device ID
+ * to domain d, a guest ITS (identified by its doorbell address) and device ID.
+ * Also provide the number of events (MSIs) needed for that device.
+ * This does not check if this particular hardware device is already mapped
+ * at another domain, it is expected that this would be done by the caller.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell, guest_devid) )
+        {
+            if ( !valid )
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 556

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +5 -0
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);
+        }
+

Plese don't run compare_its_guest_devices twice with the same arguments, just store the return value.

Fixed in v3.

Quoted 37 lines
Quoted 37 lines
xen/arch/arm/gic-v3-its.c +37 -0
+        if ( compare_its_guest_devices(temp, guest_doorbell, guest_devid) > 0 )
+            new = &((*new)->rb_left);
+        else
+            new = &((*new)->rb_right);
+    }
+
+    if ( !valid )
+        goto out_unlock;
+
+    ret = -ENOMEM;
+
+    /* An Interrupt Translation Table needs to be 256-byte aligned. */
+    itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256);
+    if ( !itt_addr )
+        goto out_unlock;
+
+    dev = xzalloc(struct its_devices);
+    if ( !dev )
+        goto out_unlock;
+
+    ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
+                            virt_to_maddr(itt_addr), true);
+    if ( ret )
+        goto out_unlock;
+
+    dev->itt_addr = itt_addr;
+    dev->hw_its = hw_its;
+    dev->guest_doorbell = guest_doorbell;
+    dev->guest_devid = guest_devid;
+    dev->host_devid = host_devid;
+    dev->eventids = nr_events;
+
+    rb_link_node(&dev->rbnode, parent, new);
+    rb_insert_color(&dev->rbnode, &d->arch.vgic.its_devices);
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 599

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+    return 0;
+
+out_unlock:
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+    if ( dev )
+        xfree(dev->host_lpis);

Where is host_lpis allocated? Why is it freed here?

So I swapped this patch with the next one in v4, because this solves this issue.

Quoted 27 lines
Quoted 27 lines
xen/arch/arm/gic-v3-its.c +27 -0
+    xfree(itt_addr);
+    xfree(dev);
+    return ret;
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+void gicv3_its_unmap_all_devices(struct domain *d)
+{
+    struct rb_node *victim;
+    struct its_devices *dev;
+
+    /*
+     * This is an easily readable, yet inefficient implementation.
+     * It uses the provided iteration wrapper and erases each node, which
+     * possibly triggers rebalancing.
+     * This seems overkill since we are going to abolish the whole tree, but
+     * avoids an open-coded re-implementation of the traversal functions with
+     * some recursive function calls.
+     * Performance does not matter here, since we are destroying a domain.
+     */
+restart:
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    if ( (victim = rb_first(&d->arch.vgic.its_devices)) )
+    {
+        dev = rb_entry(victim, struct its_devices, rbnode);
+        rb_erase(victim, &d->arch.vgic.its_devices);
+
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 632

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +6 -0
+        spin_unlock(&d->arch.vgic.its_devices_lock);
+
+        remove_mapped_guest_device(dev);
+
+        goto restart;
+    }

There is no need to use goto here, a simple while loop should suffice.

Fixed in v4.

Quoted 5 lines
Quoted 5 lines
xen/arch/arm/gic-v3-its.c +3 -0
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
Quoted 8 lines

xen/arch/arm/gic-v3-its.c void gicv3_its_dt_init(const struct dt_device_node *node) · line 664

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1 -0
 {
@@ -455,6 +661,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);

This change should be in the previous patch

Fixed in v4.

Quoted 61 lines
Quoted 61 lines
xen/arch/arm/gic-v3-its.c +0 -0
         printk("GICv3: Found ITS @0x%lx\n", addr);
 
xen/arch/arm/vgic-v3.c +3 -0
index d61479d..1fadb00 100644
@@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d)
     d->arch.vgic.nr_regions = rdist_count;
     d->arch.vgic.rdist_regions = rdist_regions;
 
+    spin_lock_init(&d->arch.vgic.its_devices_lock);
+    d->arch.vgic.its_devices = RB_ROOT;
+
     /*
      * Domain 0 gets the hardware address.
      * Guests get the virtual platform layout.
xen/include/asm-arm/domain.h +3 -0
index 2d6fbb1..00b9c1a 100644
@@ -11,6 +11,7 @@
 #include <asm/gic.h>
 #include <public/hvm/params.h>
 #include <xen/serial.h>
+#include <xen/rbtree.h>
 
 struct hvm_domain
 {
@@ -109,6 +110,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        struct rb_root its_devices;         /* devices mapped to an ITS */
+        spinlock_t its_devices_lock;        /* protects the its_devices tree */
 #endif
     } vgic;
 
xen/include/asm-arm/gic_v3_its.h +7 -0
index 8b493fb..3421ea0 100644
@@ -48,6 +48,10 @@
 #define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
                                                GITS_TYPER_DEVIDS_SHIFT) + 1)
 #define GITS_TYPER_IDBITS_SHIFT         8
+#define GITS_TYPER_ITT_SIZE_SHIFT       4
+#define GITS_TYPER_ITT_SIZE_MASK        (0xfUL << GITS_TYPER_ITT_SIZE_SHIFT)
+#define GITS_TYPER_ITT_SIZE(r)          ((((r) & GITS_TYPER_ITT_SIZE_MASK) >> \
+                                                GITS_TYPER_ITT_SIZE_SHIFT) + 1)
 
 #define GITS_IIDR_VALUE                 0x34c
 
@@ -94,7 +98,10 @@
 #define GITS_CMD_MOVALL                 0x0e
 #define GITS_CMD_DISCARD                0x0f
 
+#define ITS_DOORBELL_OFFSET             0x10040
+
 #include <xen/device_tree.h>
+#include <xen/rbtree.h>
 
Quoted 12 lines

xen/include/asm-arm/gic_v3_its.h struct host_its { · line 120

Quoted 9 lines
xen/include/asm-arm/gic_v3_its.h +1 -0
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
@@ -108,6 +115,7 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    unsigned int itte_size;
     unsigned int flags;
 };

I would move itte_size and its initialization to the patch that introduced struct host_its.

Puh, OK. This wasn't as innocent as it looks like on the first glance, so I created a new patch to introduce the host ITS init early and separate this from the ITS table init.

Cheers,
Andre.

Quoted 16 lines
Quoted 16 lines
xen/include/asm-arm/gic_v3_its.h +10 -0
@@ -134,6 +142,16 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
 /* Map a collection for this host CPU to each host ITS. */
 int gicv3_its_setup_collection(unsigned int cpu);
 
+/*
+ * Map a device on the host by allocating an ITT on the host (ITS).
+ * "nr_event" specifies how many events (interrupts) this device will need.
+ * Setting "valid" to false deallocates the device.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid);
+
 #else
 
Quoted 4 lines
Quoted 4 lines
xen/include/asm-arm/gic_v3_its.h +0 -0
 static LIST_HEAD(host_its_list);
Signature · 1 line
2.9.0

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

#45 lore raw

Re: [PATCH v2 06/27] ARM: GICv3 ITS: introduce device mapping

In reply to Andre Przywara

Hi Andre,

Quoted 180 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 an rbtree to later be able to iterate over it or to easily teardown guests.

---
 xen/arch/arm/gic-v3-its.c        | 207 +++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c           |   3 +
 xen/include/asm-arm/domain.h     |   3 +
 xen/include/asm-arm/gic_v3_its.h |  18 ++++
 4 files changed, 231 insertions(+)
xen/arch/arm/gic-v3-its.c +129 -0
index 5c11b0d..60b15b5 100644
@@ -21,6 +21,8 @@
 #include <xen/lib.h>
 #include <xen/delay.h>
 #include <xen/mm.h>
+#include <xen/rbtree.h>
+#include <xen/sched.h>
 #include <xen/sizes.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
@@ -32,6 +34,17 @@
 
 LIST_HEAD(host_its_list);
 
+struct its_devices {
+    struct rb_node rbnode;
+    struct host_its *hw_its;
+    void *itt_addr;
+    paddr_t guest_doorbell;
+    uint32_t host_devid;
+    uint32_t guest_devid;
+    uint32_t eventids;
+    uint32_t *host_lpis;
+};
+
 bool gicv3_its_host_has_its(void)
 {
     return !list_empty(&host_its_list);
@@ -149,6 +162,24 @@ static int its_send_cmd_mapc(struct host_its *its, uint32_t collection_id,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
+                             uint8_t size_bits, paddr_t itt_addr, bool valid)
+{
+    uint64_t cmd[4];
+
+    if ( valid )
+    {
+        ASSERT(size_bits < 32);
+        ASSERT(!(itt_addr & ~GENMASK(51, 8)));
+    }
+    cmd[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
+    cmd[1] = valid ? size_bits : 0x00;
+    cmd[2] = valid ? (itt_addr | GITS_VALID_BIT) : 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -379,6 +410,7 @@ static int gicv3_its_init_single_its(struct host_its *hw_its)
     devid_bits = min(devid_bits, max_its_device_bits);
     if ( reg & GITS_TYPER_PTA )
         hw_its->flags |= HOST_ITS_USES_PTA;
+    hw_its->itte_size = GITS_TYPER_ITT_SIZE(reg);
 
     for ( i = 0; i < GITS_BASER_NR_REGS; i++ )
     {
@@ -428,6 +460,180 @@ int gicv3_its_init(void)
     return 0;
 }
 
+static int remove_mapped_guest_device(struct its_devices *dev)
+{
+    int ret;
+
+    if ( dev->hw_its )
+    {
+        int ret = its_send_cmd_mapd(dev->hw_its, dev->host_devid, 0, 0, false);
+        if ( ret )
+            return ret;
+    }
+
+    ret = gicv3_its_wait_commands(dev->hw_its);
+    if ( ret )
+        return ret;
+
+    xfree(dev->itt_addr);
+    xfree(dev);
+
+    return 0;
+}
+
+static struct host_its *gicv3_its_find_by_doorbell(paddr_t doorbell_address)
+{
+    struct host_its *hw_its;
+
+    list_for_each_entry(hw_its, &host_its_list, entry)
+    {
+        if ( hw_its->addr + ITS_DOORBELL_OFFSET == doorbell_address )
+            return hw_its;
+    }
+
+    return NULL;
+}
+
+static int compare_its_guest_devices(struct its_devices *dev,
+                                     paddr_t doorbell, uint32_t devid)
+{
+    if ( dev->guest_doorbell < doorbell )
+        return -1;
+
+    if ( dev->guest_doorbell > doorbell )
+        return 1;
+
+    if ( dev->guest_devid < devid )
+        return -1;
+
+    if ( dev->guest_devid > devid )
+        return 1;
+
+    return 0;
+}
+
+/*
+ * Map a hardware device, identified by a certain host ITS and its device ID
+ * to domain d, a guest ITS (identified by its doorbell address) and device ID.
+ * Also provide the number of events (MSIs) needed for that device.
+ * This does not check if this particular hardware device is already mapped
+ * at another domain, it is expected that this would be done by the caller.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid)
+{
+    void *itt_addr = NULL;
+    struct host_its *hw_its;
+    struct its_devices *dev = NULL, *temp;
+    struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
+    int ret = -ENOENT;
+
+    hw_its = gicv3_its_find_by_doorbell(host_doorbell);
+    if ( !hw_its )
+        return ret;
+
+    /* check for already existing mappings */
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    while ( *new )
+    {
+        temp = rb_entry(*new, struct its_devices, rbnode);
+
+        parent = *new;
+        if ( !compare_its_guest_devices(temp, guest_doorbell, guest_devid) )
+        {
+            if ( !valid )
+                rb_erase(&temp->rbnode, &d->arch.vgic.its_devices);
+
+            spin_unlock(&d->arch.vgic.its_devices_lock);
+
+            if ( valid )
+                return -EBUSY;
+
+            return remove_mapped_guest_device(temp);
+        }
+
+        if ( compare_its_guest_devices(temp, guest_doorbell, guest_devid) > 0 )
+            new = &((*new)->rb_left);
+        else

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 569

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+            new = &((*new)->rb_right);
+    }
+
+    if ( !valid )
+        goto out_unlock;
+
+    ret = -ENOMEM;
+
+    /* An Interrupt Translation Table needs to be 256-byte aligned. */
+    itt_addr = _xzalloc(nr_events * hw_its->itte_size, 256);

Here itt_addr should be calculated based number of IDs mapped with MAPTI. Should be LPI_BLOCK size. Ex: If guest sends MAPD with nr_events 2 and later MAPTI
is sent for Xen for mapping 32 events, causes memory corruption.

xen/arch/arm/gic-v3-its.c int gicv3_its_init(void) · line 578

Quoted 9 lines
xen/arch/arm/gic-v3-its.c +9 -0
+    if ( !itt_addr )
+        goto out_unlock;
+
+    dev = xzalloc(struct its_devices);
+    if ( !dev )
+        goto out_unlock;
+
+    ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
+                            virt_to_maddr(itt_addr), true);

Here MAPD is sent with Size (nr_events) set by domain. However
MAPTI is sent for LPI block (32). ITS HW will throw 0xa05 (MAPVI_ID_OOR) error indicating ID out of range.

Quoted 156 lines
xen/arch/arm/gic-v3-its.c +59 -0
+    if ( ret )
+        goto out_unlock;
+
+    dev->itt_addr = itt_addr;
+    dev->hw_its = hw_its;
+    dev->guest_doorbell = guest_doorbell;
+    dev->guest_devid = guest_devid;
+    dev->host_devid = host_devid;
+    dev->eventids = nr_events;
+
+    rb_link_node(&dev->rbnode, parent, new);
+    rb_insert_color(&dev->rbnode, &d->arch.vgic.its_devices);
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+
+    return 0;
+
+out_unlock:
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+    if ( dev )
+        xfree(dev->host_lpis);
+    xfree(itt_addr);
+    xfree(dev);
+    return ret;
+}
+
+/* Removing any connections a domain had to any ITS in the system. */
+void gicv3_its_unmap_all_devices(struct domain *d)
+{
+    struct rb_node *victim;
+    struct its_devices *dev;
+
+    /*
+     * This is an easily readable, yet inefficient implementation.
+     * It uses the provided iteration wrapper and erases each node, which
+     * possibly triggers rebalancing.
+     * This seems overkill since we are going to abolish the whole tree, but
+     * avoids an open-coded re-implementation of the traversal functions with
+     * some recursive function calls.
+     * Performance does not matter here, since we are destroying a domain.
+     */
+restart:
+    spin_lock(&d->arch.vgic.its_devices_lock);
+    if ( (victim = rb_first(&d->arch.vgic.its_devices)) )
+    {
+        dev = rb_entry(victim, struct its_devices, rbnode);
+        rb_erase(victim, &d->arch.vgic.its_devices);
+
+        spin_unlock(&d->arch.vgic.its_devices_lock);
+
+        remove_mapped_guest_device(dev);
+
+        goto restart;
+    }
+
+    spin_unlock(&d->arch.vgic.its_devices_lock);
+}
+
 /* Scan the DT for any ITS nodes and create a list of host ITSes out of it. */
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
@@ -455,6 +661,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/vgic-v3.c +3 -0
index d61479d..1fadb00 100644
@@ -1450,6 +1450,9 @@ static int vgic_v3_domain_init(struct domain *d)
     d->arch.vgic.nr_regions = rdist_count;
     d->arch.vgic.rdist_regions = rdist_regions;
 
+    spin_lock_init(&d->arch.vgic.its_devices_lock);
+    d->arch.vgic.its_devices = RB_ROOT;
+
     /*
      * Domain 0 gets the hardware address.
      * Guests get the virtual platform layout.
xen/include/asm-arm/domain.h +3 -0
index 2d6fbb1..00b9c1a 100644
@@ -11,6 +11,7 @@
 #include <asm/gic.h>
 #include <public/hvm/params.h>
 #include <xen/serial.h>
+#include <xen/rbtree.h>
 
 struct hvm_domain
 {
@@ -109,6 +110,8 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        struct rb_root its_devices;         /* devices mapped to an ITS */
+        spinlock_t its_devices_lock;        /* protects the its_devices tree */
 #endif
     } vgic;
 
xen/include/asm-arm/gic_v3_its.h +18 -1
index 8b493fb..3421ea0 100644
@@ -48,6 +48,10 @@
 #define GITS_TYPER_DEVICE_ID_BITS(r)    (((r & GITS_TYPER_DEVIDS_MASK) >> \
                                                GITS_TYPER_DEVIDS_SHIFT) + 1)
 #define GITS_TYPER_IDBITS_SHIFT         8
+#define GITS_TYPER_ITT_SIZE_SHIFT       4
+#define GITS_TYPER_ITT_SIZE_MASK        (0xfUL << GITS_TYPER_ITT_SIZE_SHIFT)
+#define GITS_TYPER_ITT_SIZE(r)          ((((r) & GITS_TYPER_ITT_SIZE_MASK) >> \
+                                                GITS_TYPER_ITT_SIZE_SHIFT) + 1)
 
 #define GITS_IIDR_VALUE                 0x34c
 
@@ -94,7 +98,10 @@
 #define GITS_CMD_MOVALL                 0x0e
 #define GITS_CMD_DISCARD                0x0f
 
+#define ITS_DOORBELL_OFFSET             0x10040
+
 #include <xen/device_tree.h>
+#include <xen/rbtree.h>
 
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
@@ -108,6 +115,7 @@ struct host_its {
     void __iomem *its_base;
     spinlock_t cmd_lock;
     void *cmd_buf;
+    unsigned int itte_size;
     unsigned int flags;
 };
 
@@ -134,6 +142,16 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta);
 /* Map a collection for this host CPU to each host ITS. */
 int gicv3_its_setup_collection(unsigned int cpu);
 
+/*
+ * Map a device on the host by allocating an ITT on the host (ITS).
+ * "nr_event" specifies how many events (interrupts) this device will need.
+ * Setting "valid" to false deallocates the device.
+ */
+int gicv3_its_map_guest_device(struct domain *d,
+                               paddr_t host_doorbell, uint32_t host_devid,
+                               paddr_t guest_doorbell, uint32_t guest_devid,
+                               uint32_t nr_events, bool valid);
+
 #else
 
 static LIST_HEAD(host_its_list);
--
Quoted 2 lines

2.9.0

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

#46 lore raw

PATCH v2 7/27 ARM: arm64: activate atomic 64-bit accessors

In reply to Andre Przywara

For some reason (probably because there was no user before) the 64-bit atomic access wrappers were commented out so far.
As we will need them in the next patch, active (and fix) them now.

---
 xen/include/asm-arm/atomic.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/include/asm-arm/atomic.h +3 -3
index 22a5036..df9de6a 100644
@@ -53,9 +53,9 @@ build_atomic_write(write_u16_atomic, "h", WORD, uint16_t, "r")
 build_atomic_write(write_u32_atomic, "",  WORD, uint32_t, "r")
 build_atomic_write(write_int_atomic, "",  WORD, int, "r")
 
-#if 0 /* defined (CONFIG_ARM_64) */
-build_atomic_read(read_u64_atomic, "x", uint64_t, "=r")
-build_atomic_write(write_u64_atomic, "x", uint64_t, "r")
+#if defined (CONFIG_ARM_64)
+build_atomic_read(read_u64_atomic, "", "", uint64_t, "=r")
+build_atomic_write(write_u64_atomic, "", "", uint64_t, "r")
 #endif
 
 build_add_sized(add_u8_sized, "b", BYTE, uint8_t, "ri")
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 7/27
#47 lore raw

Re: [PATCH v2 07/27] ARM: arm64: activate atomic 64-bit accessors

Hi Andre,

Quoted 5 lines · Andre Przywara

For some reason (probably because there was no user before) the 64-bit atomic access wrappers were commented out so far.
As we will need them in the next patch, active (and fix) them now.

Regards,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 07/27] ARM: arm64: activate atomic 64-bit accessors

Quoted 10 lines · Julien Grall

Hi Andre,

Quoted 5 lines · Andre Przywara

For some reason (probably because there was no user before) the 64-bit atomic access wrappers were commented out so far.
As we will need them in the next patch, active (and fix) them now.

I committed this patch.

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

#49 lore raw

PATCH v2 8/27 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.
By using the naturally atomic access guarantee the native uint64_t data type gives us, we allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-v3-its.c        |  90 ++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c        | 188 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic.h        |   5 ++
 xen/include/asm-arm/gic_v3_its.h |  11 +++
 4 files changed, 292 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +88 -2
index 60b15b5..ed14d95 100644
@@ -148,6 +148,20 @@ static int its_send_cmd_sync(struct host_its *its, unsigned int cpu)
     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, uint32_t collection_id,
                              unsigned int cpu)
 {
@@ -180,6 +194,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -462,7 +489,7 @@ int gicv3_its_init(void)
 
 static int remove_mapped_guest_device(struct its_devices *dev)
 {
-    int ret;
+    int ret, i;
 
     if ( dev->hw_its )
     {
@@ -471,11 +498,19 @@ static int remove_mapped_guest_device(struct its_devices *dev)
             return ret;
     }
 
+    /*
+     * The only error the function below would return is -ENOENT, in which
+     * case there is nothing to free here. So we just ignore it.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(dev->eventids, LPI_BLOCK); i++ )
+        gicv3_free_host_lpi_block(dev->hw_its, dev->host_lpis[i]);
+
     ret = gicv3_its_wait_commands(dev->hw_its);
     if ( ret )
         return ret;
 
     xfree(dev->itt_addr);
+    xfree(dev->host_lpis);
     xfree(dev);
 
     return 0;
@@ -513,6 +548,36 @@ static int compare_its_guest_devices(struct its_devices *dev,
 }
 
 /*
+ * On the host ITS @its, map @nr_events consecutive LPIs.
+ * The mapping connects a device @devid and event @eventid pair to LPI @lpi,
+ * increasing both @eventid and @lpi to cover the number of requested LPIs.
+ */
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t devid, uint32_t eventid, uint32_t lpi,
+                              uint32_t nr_events)
+{
+    uint32_t i;
+    int ret;
+
+    for ( i = 0; i < nr_events; i++ )
+    {
+        /* For now we map every host LPI to host CPU 0 */
+        ret = its_send_cmd_mapti(its, devid, eventid + i, lpi + i, 0);
+        if ( ret )
+            return ret;
+        ret = its_send_cmd_inv(its, devid, eventid + i);
+        if ( ret )
+            return ret;
+    }
+
+    ret = its_send_cmd_sync(its, 0);
+    if ( ret )
+        return ret;
+
+    return gicv3_its_wait_commands(its);
+}
+
+/*
  * Map a hardware device, identified by a certain host ITS and its device ID
  * to domain d, a guest ITS (identified by its doorbell address) and device ID.
  * Also provide the number of events (MSIs) needed for that device.
@@ -528,7 +593,7 @@ int gicv3_its_map_guest_device(struct domain *d,
     struct host_its *hw_its;
     struct its_devices *dev = NULL, *temp;
     struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
-    int ret = -ENOENT;
+    int ret = -ENOENT, i;
 
     hw_its = gicv3_its_find_by_doorbell(host_doorbell);
     if ( !hw_its )
@@ -574,6 +639,11 @@ int gicv3_its_map_guest_device(struct domain *d,
     if ( !dev )
         goto out_unlock;
 
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));
+    if ( !dev->host_lpis )
+        goto out_unlock;
+
     ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
                             virt_to_maddr(itt_addr), true);
     if ( ret )
@@ -591,10 +661,26 @@ int gicv3_its_map_guest_device(struct domain *d,
 
     spin_unlock(&d->arch.vgic.its_devices_lock);
 
+    /*
+     * Map all host LPIs within this device already. We can't afford to queue
+     * any host ITS commands later on during the guest's runtime.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(nr_events, LPI_BLOCK); i++ )
+    {
+        ret = gicv3_allocate_host_lpi_block(hw_its, d, host_devid,
+                                            i * LPI_BLOCK);
+        if ( ret < 0 )
+            goto out;
+
+        dev->host_lpis[i] = ret;
+    }
+
     return 0;
 
 out_unlock:
     spin_unlock(&d->arch.vgic.its_devices_lock);
+
+out:
     if ( dev )
         xfree(dev->host_lpis);
     xfree(itt_addr);
xen/arch/arm/gic-v3-lpi.c +188 -0

233 lines not on this page · raw message

xen/include/asm-arm/gic.h +5 -0
index 12bd155..7825575 100644
@@ -220,7 +220,12 @@ enum gic_version {
     GIC_V3,
 };
 
+#define INVALID_LPI     0
 #define LPI_OFFSET      8192
+static inline bool is_lpi(unsigned int irq)
+{
+    return irq >= LPI_OFFSET;
+}
 
 extern enum gic_version gic_hw_version(void);
 
xen/include/asm-arm/gic_v3_its.h +11 -0
index 3421ea0..2387972 100644
@@ -106,6 +106,11 @@
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
 
+#define INVALID_DOMID ((uint16_t)~0)
+
+/* We allocate LPIs on the hosts in chunks of 32 to reduce handling overhead. */
+#define LPI_BLOCK                       32
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -151,6 +156,12 @@ int gicv3_its_map_guest_device(struct domain *d,
                                paddr_t host_doorbell, uint32_t host_devid,
                                paddr_t guest_doorbell, uint32_t guest_devid,
                                uint32_t nr_events, bool valid);
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t host_devid, uint32_t eventid,
+                              uint32_t lpi, uint32_t nrevents);
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+                                  uint32_t host_devid, uint32_t eventid);
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 
 #else
 
Signature · 7 lines
2.9.0


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

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

Quoted 145 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.
By using the naturally atomic access guarantee the native uint64_t data type gives us, we allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-v3-its.c        |  90 ++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c        | 188 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic.h        |   5 ++
 xen/include/asm-arm/gic_v3_its.h |  11 +++
 4 files changed, 292 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +67 -2
index 60b15b5..ed14d95 100644
@@ -148,6 +148,20 @@ static int its_send_cmd_sync(struct host_its *its, unsigned int cpu)
     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, uint32_t collection_id,
                              unsigned int cpu)
 {
@@ -180,6 +194,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -462,7 +489,7 @@ int gicv3_its_init(void)
 
 static int remove_mapped_guest_device(struct its_devices *dev)
 {
-    int ret;
+    int ret, i;
 
     if ( dev->hw_its )
     {
@@ -471,11 +498,19 @@ static int remove_mapped_guest_device(struct its_devices *dev)
             return ret;
     }
 
+    /*
+     * The only error the function below would return is -ENOENT, in which
+     * case there is nothing to free here. So we just ignore it.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(dev->eventids, LPI_BLOCK); i++ )
+        gicv3_free_host_lpi_block(dev->hw_its, dev->host_lpis[i]);
+
     ret = gicv3_its_wait_commands(dev->hw_its);
     if ( ret )
         return ret;
 
     xfree(dev->itt_addr);
+    xfree(dev->host_lpis);
     xfree(dev);
 
     return 0;
@@ -513,6 +548,36 @@ static int compare_its_guest_devices(struct its_devices *dev,
 }
 
 /*
+ * On the host ITS @its, map @nr_events consecutive LPIs.
+ * The mapping connects a device @devid and event @eventid pair to LPI @lpi,
+ * increasing both @eventid and @lpi to cover the number of requested LPIs.
+ */
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t devid, uint32_t eventid, uint32_t lpi,
+                              uint32_t nr_events)
+{
+    uint32_t i;
+    int ret;
+
+    for ( i = 0; i < nr_events; i++ )
+    {
+        /* For now we map every host LPI to host CPU 0 */
+        ret = its_send_cmd_mapti(its, devid, eventid + i, lpi + i, 0);
+        if ( ret )
+            return ret;
+        ret = its_send_cmd_inv(its, devid, eventid + i);
+        if ( ret )
+            return ret;
+    }
+
+    ret = its_send_cmd_sync(its, 0);
+    if ( ret )
+        return ret;
+
+    return gicv3_its_wait_commands(its);
+}
+
+/*
  * Map a hardware device, identified by a certain host ITS and its device ID
  * to domain d, a guest ITS (identified by its doorbell address) and device ID.
  * Also provide the number of events (MSIs) needed for that device.
@@ -528,7 +593,7 @@ int gicv3_its_map_guest_device(struct domain *d,
     struct host_its *hw_its;
     struct its_devices *dev = NULL, *temp;
     struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
-    int ret = -ENOENT;
+    int ret = -ENOENT, i;
 
     hw_its = gicv3_its_find_by_doorbell(host_doorbell);
     if ( !hw_its )

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 645

Quoted 7 lines
xen/arch/arm/gic-v3-its.c +4 -0
@@ -574,6 +639,11 @@ int gicv3_its_map_guest_device(struct domain *d,
     if ( !dev )
         goto out_unlock;
 
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));
+    if ( !dev->host_lpis )
+        goto out_unlock;

The expectation is that we are going to allocate far more than 32 lpis for one device, possibly thousands, right?
If so, dev->host_lpis could be quite large. I am thinking we should consider switching to a sparse data structure to save memory, as in most cases the allocated blocks are going to be consecutive.

I would probably store the first lpi and the number of consecutive lpis, rather than the first lpi in each block. For example, if the allocated blocks are:

0-31, 32-63, 64-95, 96-127, 128-159, 256-287

Now we store: 0, 32, 64, 96, 128, 256
I would store [0, 160], [256, 32]

In the case of a thousand consecutive block allocations, it would shorten the array from 1000/32 to 1 element.

Does it make sense? Is it worth doing? This last question is mostly for the Qualcomm and Cavium guys on the list because it depends on the numbers of events on a given platform.

Quoted 65 lines
xen/arch/arm/gic-v3-its.c +16 -0
     ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
                             virt_to_maddr(itt_addr), true);
     if ( ret )
@@ -591,10 +661,26 @@ int gicv3_its_map_guest_device(struct domain *d,
 
     spin_unlock(&d->arch.vgic.its_devices_lock);
 
+    /*
+     * Map all host LPIs within this device already. We can't afford to queue
+     * any host ITS commands later on during the guest's runtime.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(nr_events, LPI_BLOCK); i++ )
+    {
+        ret = gicv3_allocate_host_lpi_block(hw_its, d, host_devid,
+                                            i * LPI_BLOCK);
+        if ( ret < 0 )
+            goto out;
+
+        dev->host_lpis[i] = ret;
+    }
+
     return 0;
 
 out_unlock:
     spin_unlock(&d->arch.vgic.its_devices_lock);
+
+out:
     if ( dev )
         xfree(dev->host_lpis);
     xfree(itt_addr);
xen/arch/arm/gic-v3-lpi.c +14 -0
index 51d7425..59d3ba4 100644
@@ -20,24 +20,44 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sched.h>
+#include <asm/atomic.h>
+#include <asm/domain.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 #include <asm/page.h>
 
+/* LPIs on the host always go to a guest, so no struct irq_desc for them. */
+union host_lpi {
+    uint64_t data;
+    struct {
+        uint32_t virt_lpi;
+        uint16_t dom_id;
+        uint16_t vcpu_id;
+    };
+};
 
 #define LPI_PROPTABLE_NEEDS_FLUSHING    (1U << 0)
 /* Global state */
 static struct {
     /* The global LPI property table, shared by all redistributors. */
     uint8_t *lpi_property;
     /*
+     * A two-level table to lookup LPIs firing on the host and look up the
+     * domain and virtual LPI number to inject.

xen/arch/arm/gic-v3-lpi.c line 59

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +4 -0
+     */
+    union host_lpi **host_lpis;
+    /*
      * Number of physical LPIs the host supports. This is a property of
      * the GIC hardware. We depart from the habit of naming these things
      * "physical" in Xen, as the GICv3/4 spec uses the term "physical LPI"
      * in a different context to differentiate them from "virtual LPIs".
      */
     unsigned long int nr_host_lpis;
+    /* Protects allocation and deallocation of host LPIs, but not the access */

OK. What does protect the access to host LPIs?

xen/arch/arm/gic-v3-lpi.c struct lpi_redist_data { · line 77

Quoted 13 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
+    spinlock_t host_lpis_lock;
     unsigned int flags;
 } lpi_data;
@@ -50,6 +70,19 @@ struct lpi_redist_data {
 static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
 
 #define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)
+#define HOST_LPIS_PER_PAGE      (PAGE_SIZE / sizeof(union host_lpi))
+
+static union host_lpi *gic_get_host_lpi(uint32_t plpi)
+{
+    if ( !is_lpi(plpi) || plpi >= MAX_PHYS_LPIS + LPI_OFFSET )

Arguably if (plpi >= MAX_PHYS_LPIS + LPI_OFFSET) is not really an lpi. I would add the plpi >= MAX_PHYS_LPIS + LPI_OFFSET check to is_lpi.

xen/arch/arm/gic-v3-lpi.c struct lpi_redist_data { · line 84

Quoted 7 lines
xen/arch/arm/gic-v3-lpi.c +7 -0
+        return NULL;
+
+    plpi -= LPI_OFFSET;
+    if ( !lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE] )
+        return NULL;
+
+    return &lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE][plpi % HOST_LPIS_PER_PAGE];

I am almost sure this line is longer than 80 characters.. Please double check the coding style throughout the series.

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c +2 -0
+}
 
 /* Stores this redistributor's physical address and ID in a per-CPU variable */
 void gicv3_set_redist_address(paddr_t address, unsigned int redist_id)
@@ -204,15 +237,170 @@ int gicv3_lpi_init_rdist(void __iomem * rdist_base)
 static unsigned int max_lpi_bits = CONFIG_MAX_PHYS_LPI_BITS;
 integer_param("max_lpi_bits", max_lpi_bits);
 
+/*

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 250

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +8 -0
+ * 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(unsigned int hw_lpi_bits)
 {
+    int nr_lpi_ptrs;
+
+    /* We rely on the data structure being atomically accessible. */
+    BUILD_BUG_ON(sizeof(union host_lpi) > sizeof(unsigned long));

Technically, I think that arm32 is able to access uint64_t atomically (ldrexd and strexd).

Quoted 113 lines
xen/arch/arm/gic-v3-lpi.c +106 -0
     lpi_data.nr_host_lpis = BIT_ULL(min(hw_lpi_bits, max_lpi_bits));
 
+    spin_lock_init(&lpi_data.host_lpis_lock);
+
+    nr_lpi_ptrs = MAX_PHYS_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_PHYS_LPIS);
 
     return 0;
 }
 
+/* Must be called with host_lpis_lock held. */
+static int find_unused_host_lpi(int start, uint32_t *index)
+{
+    int chunk;
+    uint32_t i = *index;
+
+    for ( chunk = start; chunk < MAX_PHYS_LPIS / HOST_LPIS_PER_PAGE; chunk++ )
+    {
+        /* If we hit an unallocated chunk, use entry 0 in that one. */
+        if ( !lpi_data.host_lpis[chunk] )
+        {
+            *index = 0;
+            return chunk;
+        }
+
+        /* Find an unallocated entry in this chunk. */
+        for ( ; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+        {
+            if ( lpi_data.host_lpis[chunk][i].dom_id == INVALID_DOMID )
+            {
+                *index = i;
+                return chunk;
+            }
+        }
+        i = 0;
+    }
+
+    return -1;
+}
+
+/*
+ * Allocate a block of 32 LPIs on the given host ITS for device "devid",
+ * starting with "eventid". Put them into the respective ITT by issuing a
+ * MAPTI command for each of them.
+ */
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+                                  uint32_t host_devid, uint32_t eventid)
+{
+    static uint32_t next_lpi = 0;
+    uint32_t lpi, lpi_idx = next_lpi % HOST_LPIS_PER_PAGE;
+    int chunk;
+    int i;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+    chunk = find_unused_host_lpi(next_lpi / HOST_LPIS_PER_PAGE, &lpi_idx);
+
+    if ( chunk == - 1 )          /* rescan for a hole from the beginning */
+    {
+        lpi_idx = 0;
+        chunk = find_unused_host_lpi(0, &lpi_idx);
+        if ( chunk == -1 )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
+            return -ENOSPC;
+        }
+    }
+
+    /* If we hit an unallocated chunk, we initialize it and use entry 0. */
+    if ( !lpi_data.host_lpis[chunk] )
+    {
+        union host_lpi *new_chunk;
+
+        new_chunk = alloc_xenheap_pages(0, 0);
+        if ( !new_chunk )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
+            return -ENOMEM;
+        }
+
+        for ( i = 0; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+            new_chunk[i].dom_id = INVALID_DOMID;
+
+        lpi_data.host_lpis[chunk] = new_chunk;
+        lpi_idx = 0;
+    }
+
+    lpi = chunk * HOST_LPIS_PER_PAGE + lpi_idx;
+
+    for ( i = 0; i < LPI_BLOCK; i++ )
+    {
+        union host_lpi hlpi;
+
+        /*
+         * Mark this host LPI as belonging to the domain, but don't assign
+         * any virtual LPI or a VCPU yet.
+         */
+        hlpi.virt_lpi = INVALID_LPI;
+        hlpi.dom_id = d->domain_id;
+        hlpi.vcpu_id = INVALID_DOMID;
+        write_u64_atomic(&lpi_data.host_lpis[chunk][lpi_idx + i].data,
+                         hlpi.data);
+
+        /*
+         * Enable this host LPI, so we don't have to do this during the
+         * guest's runtime.
+         */
+        lpi_data.lpi_property[lpi + i] |= LPI_PROP_ENABLED;
+    }
+

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 374

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+    /*
+     * We have allocated and initialized the host LPI entries, so it's safe
+     * to drop the lock now. Access to the structures can be done concurrently
+     * as it involves only an atomic uint64_t access.
+     */
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    if ( lpi_data.flags & LPI_PROPTABLE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(&lpi_data.lpi_property[lpi],
+                                             LPI_BLOCK);

I would move this right below
lpi_data.lpi_property[lpi + i] |= LPI_PROP_ENABLED;

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 388

Quoted 13 lines
xen/arch/arm/gic-v3-lpi.c +13 -0
+    gicv3_its_map_host_events(its, host_devid, eventid, lpi + LPI_OFFSET,
+                              LPI_BLOCK);
+
+    next_lpi = lpi + LPI_BLOCK;
+    return lpi + LPI_OFFSET;
+}
+
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi)
+{
+    union host_lpi *hlpi, empty_lpi = { .dom_id = INVALID_DOMID };
+    int i;
+
+    hlpi = gic_get_host_lpi(lpi);

I think you need to make sure that "lpi" is the first lpi of a LPI_BLOCK. Either that, or gic_get_host_lpi should make sure to always return the first lpi in a block.

Quoted 40 lines
xen/arch/arm/gic-v3-lpi.c +15 -0
+    if ( !hlpi )
+        return -ENOENT;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+
+    for ( i = 0; i < LPI_BLOCK; i++ )
+        write_u64_atomic(&hlpi[i].data, empty_lpi.data);
+
+    /* TODO: Call a function in gic-v3-its.c to send DISCARDs */
+
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-arm/gic.h +5 -0
index 12bd155..7825575 100644
@@ -220,7 +220,12 @@ enum gic_version {
     GIC_V3,
 };
 
+#define INVALID_LPI     0
 #define LPI_OFFSET      8192
+static inline bool is_lpi(unsigned int irq)
+{
+    return irq >= LPI_OFFSET;
+}
 
 extern enum gic_version gic_hw_version(void);
 
xen/include/asm-arm/gic_v3_its.h +0 -0
index 3421ea0..2387972 100644

xen/include/asm-arm/gic_v3_its.h line 109

Quoted 4 lines
xen/include/asm-arm/gic_v3_its.h +1 -0
@@ -106,6 +106,11 @@
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
 
+#define INVALID_DOMID ((uint16_t)~0)

We already have DOMID_INVALID

Quoted 22 lines
xen/include/asm-arm/gic_v3_its.h +9 -0
+/* We allocate LPIs on the hosts in chunks of 32 to reduce handling overhead. */
+#define LPI_BLOCK                       32
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -151,6 +156,12 @@ int gicv3_its_map_guest_device(struct domain *d,
                                paddr_t host_doorbell, uint32_t host_devid,
                                paddr_t guest_doorbell, uint32_t guest_devid,
                                uint32_t nr_events, bool valid);
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t host_devid, uint32_t eventid,
+                              uint32_t lpi, uint32_t nrevents);
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+                                  uint32_t host_devid, uint32_t eventid);
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 
 #else
 
Signature · 1 line
2.9.0

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

#51 lore raw

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

Hi Stefano,

Quoted 1 line · Stefano Stabellini

On Thu, 16 Mar 2017, Andre Przywara wrote:

[...]

Quoted 20 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 645

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));
+    if ( !dev->host_lpis )
+        goto out_unlock;

The expectation is that we are going to allocate far more than 32 lpis for one device, possibly thousands, right?
If so, dev->host_lpis could be quite large. I am thinking we should consider switching to a sparse data structure to save memory, as in most cases the allocated blocks are going to be consecutive.

I would probably store the first lpi and the number of consecutive lpis, rather than the first lpi in each block. For example, if the allocated blocks are:

0-31, 32-63, 64-95, 96-127, 128-159, 256-287

Now we store: 0, 32, 64, 96, 128, 256
I would store [0, 160], [256, 32]

Quoted 6 lines

In the case of a thousand consecutive block allocations, it would shorten the array from 1000/32 to 1 element.

Does it make sense? Is it worth doing? This last question is mostly for the Qualcomm and Cavium guys on the list because it depends on the numbers of events on a given platform.

Not answering directly to the question. But I think this is an improvement and not necessary for a first version.

I would like to see this series merged in Xen 4.9 as a tech preview, so I would suggest to gather this list of improvement (maybe on Jira?) and defer them for Xen 4.10. They would need to be addressed before making ITS stable. Stefano, does it sound good to you?

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

In reply to Stefano Stabellini

Hi,

Quoted 149 lines · Stefano Stabellini
Quoted 148 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.
By using the naturally atomic access guarantee the native uint64_t data type gives us, we allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-v3-its.c        |  90 ++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c        | 188 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic.h        |   5 ++
 xen/include/asm-arm/gic_v3_its.h |  11 +++
 4 files changed, 292 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +67 -2
index 60b15b5..ed14d95 100644
@@ -148,6 +148,20 @@ static int its_send_cmd_sync(struct host_its *its, unsigned int cpu)
     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, uint32_t collection_id,
                              unsigned int cpu)
 {
@@ -180,6 +194,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -462,7 +489,7 @@ int gicv3_its_init(void)
 
 static int remove_mapped_guest_device(struct its_devices *dev)
 {
-    int ret;
+    int ret, i;
 
     if ( dev->hw_its )
     {
@@ -471,11 +498,19 @@ static int remove_mapped_guest_device(struct its_devices *dev)
             return ret;
     }
 
+    /*
+     * The only error the function below would return is -ENOENT, in which
+     * case there is nothing to free here. So we just ignore it.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(dev->eventids, LPI_BLOCK); i++ )
+        gicv3_free_host_lpi_block(dev->hw_its, dev->host_lpis[i]);
+
     ret = gicv3_its_wait_commands(dev->hw_its);
     if ( ret )
         return ret;
 
     xfree(dev->itt_addr);
+    xfree(dev->host_lpis);
     xfree(dev);
 
     return 0;
@@ -513,6 +548,36 @@ static int compare_its_guest_devices(struct its_devices *dev,
 }
 
 /*
+ * On the host ITS @its, map @nr_events consecutive LPIs.
+ * The mapping connects a device @devid and event @eventid pair to LPI @lpi,
+ * increasing both @eventid and @lpi to cover the number of requested LPIs.
+ */
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t devid, uint32_t eventid, uint32_t lpi,
+                              uint32_t nr_events)
+{
+    uint32_t i;
+    int ret;
+
+    for ( i = 0; i < nr_events; i++ )
+    {
+        /* For now we map every host LPI to host CPU 0 */
+        ret = its_send_cmd_mapti(its, devid, eventid + i, lpi + i, 0);
+        if ( ret )
+            return ret;
+        ret = its_send_cmd_inv(its, devid, eventid + i);
+        if ( ret )
+            return ret;
+    }
+
+    ret = its_send_cmd_sync(its, 0);
+    if ( ret )
+        return ret;
+
+    return gicv3_its_wait_commands(its);
+}
+
+/*
  * Map a hardware device, identified by a certain host ITS and its device ID
  * to domain d, a guest ITS (identified by its doorbell address) and device ID.
  * Also provide the number of events (MSIs) needed for that device.
@@ -528,7 +593,7 @@ int gicv3_its_map_guest_device(struct domain *d,
     struct host_its *hw_its;
     struct its_devices *dev = NULL, *temp;
     struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
-    int ret = -ENOENT;
+    int ret = -ENOENT, i;
 
     hw_its = gicv3_its_find_by_doorbell(host_doorbell);
     if ( !hw_its )
@@ -574,6 +639,11 @@ int gicv3_its_map_guest_device(struct domain *d,
     if ( !dev )
         goto out_unlock;
 
Quoted 7 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 645

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));
+    if ( !dev->host_lpis )
+        goto out_unlock;

The expectation is that we are going to allocate far more than 32 lpis for one device, possibly thousands, right?

Mmh, what makes you think so?
My understanding is that you have one MSI per interrupt "reason" (TX, RX, error, for instance). And even if you have multiple queues, each connected to separate MSIs, that won't go into the thousands. MSI-X for instance is *limited* to 2048 MSIs.

But in reality I dont' see nearly that many MSIs, mostly less than a dozen per device. And since 32 is the old PCI MSI limit, I thought this would be a good starting point. So most of the time we just need one block.

I believe this could add up with virtual functions, but those are separate devices, right?

Cheers,
Andre.

Quoted 366 lines

If so, dev->host_lpis could be quite large. I am thinking we should consider switching to a sparse data structure to save memory, as in most cases the allocated blocks are going to be consecutive.

I would probably store the first lpi and the number of consecutive lpis, rather than the first lpi in each block. For example, if the allocated blocks are:

0-31, 32-63, 64-95, 96-127, 128-159, 256-287

Now we store: 0, 32, 64, 96, 128, 256
I would store [0, 160], [256, 32]

In the case of a thousand consecutive block allocations, it would shorten the array from 1000/32 to 1 element.

Does it make sense? Is it worth doing? This last question is mostly for the Qualcomm and Cavium guys on the list because it depends on the numbers of events on a given platform.

Quoted 65 lines
xen/arch/arm/gic-v3-its.c +16 -0
     ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
                             virt_to_maddr(itt_addr), true);
     if ( ret )
@@ -591,10 +661,26 @@ int gicv3_its_map_guest_device(struct domain *d,
 
     spin_unlock(&d->arch.vgic.its_devices_lock);
 
+    /*
+     * Map all host LPIs within this device already. We can't afford to queue
+     * any host ITS commands later on during the guest's runtime.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(nr_events, LPI_BLOCK); i++ )
+    {
+        ret = gicv3_allocate_host_lpi_block(hw_its, d, host_devid,
+                                            i * LPI_BLOCK);
+        if ( ret < 0 )
+            goto out;
+
+        dev->host_lpis[i] = ret;
+    }
+
     return 0;
 
 out_unlock:
     spin_unlock(&d->arch.vgic.its_devices_lock);
+
+out:
     if ( dev )
         xfree(dev->host_lpis);
     xfree(itt_addr);
xen/arch/arm/gic-v3-lpi.c +14 -0
index 51d7425..59d3ba4 100644
@@ -20,24 +20,44 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sched.h>
+#include <asm/atomic.h>
+#include <asm/domain.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 #include <asm/page.h>
 
+/* LPIs on the host always go to a guest, so no struct irq_desc for them. */
+union host_lpi {
+    uint64_t data;
+    struct {
+        uint32_t virt_lpi;
+        uint16_t dom_id;
+        uint16_t vcpu_id;
+    };
+};
 
 #define LPI_PROPTABLE_NEEDS_FLUSHING    (1U << 0)
 /* Global state */
 static struct {
     /* The global LPI property table, shared by all redistributors. */
     uint8_t *lpi_property;
     /*
+     * A two-level table to lookup LPIs firing on the host and look up the
+     * domain and virtual LPI number to inject.

xen/arch/arm/gic-v3-lpi.c line 59

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +4 -0
+     */
+    union host_lpi **host_lpis;
+    /*
      * Number of physical LPIs the host supports. This is a property of
      * the GIC hardware. We depart from the habit of naming these things
      * "physical" in Xen, as the GICv3/4 spec uses the term "physical LPI"
      * in a different context to differentiate them from "virtual LPIs".
      */
     unsigned long int nr_host_lpis;
+    /* Protects allocation and deallocation of host LPIs, but not the access */

OK. What does protect the access to host LPIs?

xen/arch/arm/gic-v3-lpi.c struct lpi_redist_data { · line 77

Quoted 13 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
+    spinlock_t host_lpis_lock;
     unsigned int flags;
 } lpi_data;
@@ -50,6 +70,19 @@ struct lpi_redist_data {
 static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
 
 #define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)
+#define HOST_LPIS_PER_PAGE      (PAGE_SIZE / sizeof(union host_lpi))
+
+static union host_lpi *gic_get_host_lpi(uint32_t plpi)
+{
+    if ( !is_lpi(plpi) || plpi >= MAX_PHYS_LPIS + LPI_OFFSET )

Arguably if (plpi >= MAX_PHYS_LPIS + LPI_OFFSET) is not really an lpi. I would add the plpi >= MAX_PHYS_LPIS + LPI_OFFSET check to is_lpi.

xen/arch/arm/gic-v3-lpi.c struct lpi_redist_data { · line 84

Quoted 7 lines
xen/arch/arm/gic-v3-lpi.c +7 -0
+        return NULL;
+
+    plpi -= LPI_OFFSET;
+    if ( !lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE] )
+        return NULL;
+
+    return &lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE][plpi % HOST_LPIS_PER_PAGE];

I am almost sure this line is longer than 80 characters.. Please double check the coding style throughout the series.

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c +2 -0
+}
 
 /* Stores this redistributor's physical address and ID in a per-CPU variable */
 void gicv3_set_redist_address(paddr_t address, unsigned int redist_id)
@@ -204,15 +237,170 @@ int gicv3_lpi_init_rdist(void __iomem * rdist_base)
 static unsigned int max_lpi_bits = CONFIG_MAX_PHYS_LPI_BITS;
 integer_param("max_lpi_bits", max_lpi_bits);
 
+/*

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 250

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +8 -0
+ * 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(unsigned int hw_lpi_bits)
 {
+    int nr_lpi_ptrs;
+
+    /* We rely on the data structure being atomically accessible. */
+    BUILD_BUG_ON(sizeof(union host_lpi) > sizeof(unsigned long));

Technically, I think that arm32 is able to access uint64_t atomically (ldrexd and strexd).

Quoted 113 lines
xen/arch/arm/gic-v3-lpi.c +106 -0
     lpi_data.nr_host_lpis = BIT_ULL(min(hw_lpi_bits, max_lpi_bits));
 
+    spin_lock_init(&lpi_data.host_lpis_lock);
+
+    nr_lpi_ptrs = MAX_PHYS_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_PHYS_LPIS);
 
     return 0;
 }
 
+/* Must be called with host_lpis_lock held. */
+static int find_unused_host_lpi(int start, uint32_t *index)
+{
+    int chunk;
+    uint32_t i = *index;
+
+    for ( chunk = start; chunk < MAX_PHYS_LPIS / HOST_LPIS_PER_PAGE; chunk++ )
+    {
+        /* If we hit an unallocated chunk, use entry 0 in that one. */
+        if ( !lpi_data.host_lpis[chunk] )
+        {
+            *index = 0;
+            return chunk;
+        }
+
+        /* Find an unallocated entry in this chunk. */
+        for ( ; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+        {
+            if ( lpi_data.host_lpis[chunk][i].dom_id == INVALID_DOMID )
+            {
+                *index = i;
+                return chunk;
+            }
+        }
+        i = 0;
+    }
+
+    return -1;
+}
+
+/*
+ * Allocate a block of 32 LPIs on the given host ITS for device "devid",
+ * starting with "eventid". Put them into the respective ITT by issuing a
+ * MAPTI command for each of them.
+ */
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+                                  uint32_t host_devid, uint32_t eventid)
+{
+    static uint32_t next_lpi = 0;
+    uint32_t lpi, lpi_idx = next_lpi % HOST_LPIS_PER_PAGE;
+    int chunk;
+    int i;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+    chunk = find_unused_host_lpi(next_lpi / HOST_LPIS_PER_PAGE, &lpi_idx);
+
+    if ( chunk == - 1 )          /* rescan for a hole from the beginning */
+    {
+        lpi_idx = 0;
+        chunk = find_unused_host_lpi(0, &lpi_idx);
+        if ( chunk == -1 )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
+            return -ENOSPC;
+        }
+    }
+
+    /* If we hit an unallocated chunk, we initialize it and use entry 0. */
+    if ( !lpi_data.host_lpis[chunk] )
+    {
+        union host_lpi *new_chunk;
+
+        new_chunk = alloc_xenheap_pages(0, 0);
+        if ( !new_chunk )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
+            return -ENOMEM;
+        }
+
+        for ( i = 0; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+            new_chunk[i].dom_id = INVALID_DOMID;
+
+        lpi_data.host_lpis[chunk] = new_chunk;
+        lpi_idx = 0;
+    }
+
+    lpi = chunk * HOST_LPIS_PER_PAGE + lpi_idx;
+
+    for ( i = 0; i < LPI_BLOCK; i++ )
+    {
+        union host_lpi hlpi;
+
+        /*
+         * Mark this host LPI as belonging to the domain, but don't assign
+         * any virtual LPI or a VCPU yet.
+         */
+        hlpi.virt_lpi = INVALID_LPI;
+        hlpi.dom_id = d->domain_id;
+        hlpi.vcpu_id = INVALID_DOMID;
+        write_u64_atomic(&lpi_data.host_lpis[chunk][lpi_idx + i].data,
+                         hlpi.data);
+
+        /*
+         * Enable this host LPI, so we don't have to do this during the
+         * guest's runtime.
+         */
+        lpi_data.lpi_property[lpi + i] |= LPI_PROP_ENABLED;
+    }
+

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 374

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+    /*
+     * We have allocated and initialized the host LPI entries, so it's safe
+     * to drop the lock now. Access to the structures can be done concurrently
+     * as it involves only an atomic uint64_t access.
+     */
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    if ( lpi_data.flags & LPI_PROPTABLE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(&lpi_data.lpi_property[lpi],
+                                             LPI_BLOCK);

I would move this right below
lpi_data.lpi_property[lpi + i] |= LPI_PROP_ENABLED;

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 388

Quoted 13 lines
xen/arch/arm/gic-v3-lpi.c +13 -0
+    gicv3_its_map_host_events(its, host_devid, eventid, lpi + LPI_OFFSET,
+                              LPI_BLOCK);
+
+    next_lpi = lpi + LPI_BLOCK;
+    return lpi + LPI_OFFSET;
+}
+
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi)
+{
+    union host_lpi *hlpi, empty_lpi = { .dom_id = INVALID_DOMID };
+    int i;
+
+    hlpi = gic_get_host_lpi(lpi);

I think you need to make sure that "lpi" is the first lpi of a LPI_BLOCK. Either that, or gic_get_host_lpi should make sure to always return the first lpi in a block.

Quoted 40 lines
xen/arch/arm/gic-v3-lpi.c +15 -0
+    if ( !hlpi )
+        return -ENOENT;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+
+    for ( i = 0; i < LPI_BLOCK; i++ )
+        write_u64_atomic(&hlpi[i].data, empty_lpi.data);
+
+    /* TODO: Call a function in gic-v3-its.c to send DISCARDs */
+
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-arm/gic.h +5 -0
index 12bd155..7825575 100644
@@ -220,7 +220,12 @@ enum gic_version {
     GIC_V3,
 };
 
+#define INVALID_LPI     0
 #define LPI_OFFSET      8192
+static inline bool is_lpi(unsigned int irq)
+{
+    return irq >= LPI_OFFSET;
+}
 
 extern enum gic_version gic_hw_version(void);
 
xen/include/asm-arm/gic_v3_its.h +0 -0
index 3421ea0..2387972 100644

xen/include/asm-arm/gic_v3_its.h line 109

Quoted 4 lines
xen/include/asm-arm/gic_v3_its.h +1 -0
@@ -106,6 +106,11 @@
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
 
+#define INVALID_DOMID ((uint16_t)~0)

We already have DOMID_INVALID

Quoted 17 lines
xen/include/asm-arm/gic_v3_its.h +9 -0
+/* We allocate LPIs on the hosts in chunks of 32 to reduce handling overhead. */
+#define LPI_BLOCK                       32
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -151,6 +156,12 @@ int gicv3_its_map_guest_device(struct domain *d,
                                paddr_t host_doorbell, uint32_t host_devid,
                                paddr_t guest_doorbell, uint32_t guest_devid,
                                uint32_t nr_events, bool valid);
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t host_devid, uint32_t eventid,
+                              uint32_t lpi, uint32_t nrevents);
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+                                  uint32_t host_devid, uint32_t eventid);
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 
Quoted 5 lines
Quoted 5 lines
xen/include/asm-arm/gic_v3_its.h +0 -0
 #else
 
Signature · 1 line
2.9.0

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

#53 lore raw

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

Quoted 166 lines · Andre Przywara

Hi,

Quoted 149 lines · Stefano Stabellini
Quoted 148 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.
By using the naturally atomic access guarantee the native uint64_t data type gives us, we allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-v3-its.c        |  90 ++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c        | 188 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic.h        |   5 ++
 xen/include/asm-arm/gic_v3_its.h |  11 +++
 4 files changed, 292 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +67 -2
index 60b15b5..ed14d95 100644
@@ -148,6 +148,20 @@ static int its_send_cmd_sync(struct host_its *its, unsigned int cpu)
     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, uint32_t collection_id,
                              unsigned int cpu)
 {
@@ -180,6 +194,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -462,7 +489,7 @@ int gicv3_its_init(void)
 
 static int remove_mapped_guest_device(struct its_devices *dev)
 {
-    int ret;
+    int ret, i;
 
     if ( dev->hw_its )
     {
@@ -471,11 +498,19 @@ static int remove_mapped_guest_device(struct its_devices *dev)
             return ret;
     }
 
+    /*
+     * The only error the function below would return is -ENOENT, in which
+     * case there is nothing to free here. So we just ignore it.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(dev->eventids, LPI_BLOCK); i++ )
+        gicv3_free_host_lpi_block(dev->hw_its, dev->host_lpis[i]);
+
     ret = gicv3_its_wait_commands(dev->hw_its);
     if ( ret )
         return ret;
 
     xfree(dev->itt_addr);
+    xfree(dev->host_lpis);
     xfree(dev);
 
     return 0;
@@ -513,6 +548,36 @@ static int compare_its_guest_devices(struct its_devices *dev,
 }
 
 /*
+ * On the host ITS @its, map @nr_events consecutive LPIs.
+ * The mapping connects a device @devid and event @eventid pair to LPI @lpi,
+ * increasing both @eventid and @lpi to cover the number of requested LPIs.
+ */
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t devid, uint32_t eventid, uint32_t lpi,
+                              uint32_t nr_events)
+{
+    uint32_t i;
+    int ret;
+
+    for ( i = 0; i < nr_events; i++ )
+    {
+        /* For now we map every host LPI to host CPU 0 */
+        ret = its_send_cmd_mapti(its, devid, eventid + i, lpi + i, 0);
+        if ( ret )
+            return ret;
+        ret = its_send_cmd_inv(its, devid, eventid + i);
+        if ( ret )
+            return ret;
+    }
+
+    ret = its_send_cmd_sync(its, 0);
+    if ( ret )
+        return ret;
+
+    return gicv3_its_wait_commands(its);
+}
+
+/*
  * Map a hardware device, identified by a certain host ITS and its device ID
  * to domain d, a guest ITS (identified by its doorbell address) and device ID.
  * Also provide the number of events (MSIs) needed for that device.
@@ -528,7 +593,7 @@ int gicv3_its_map_guest_device(struct domain *d,
     struct host_its *hw_its;
     struct its_devices *dev = NULL, *temp;
     struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
-    int ret = -ENOENT;
+    int ret = -ENOENT, i;
 
     hw_its = gicv3_its_find_by_doorbell(host_doorbell);
     if ( !hw_its )
@@ -574,6 +639,11 @@ int gicv3_its_map_guest_device(struct domain *d,
     if ( !dev )
         goto out_unlock;
 
Quoted 7 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 645

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));
+    if ( !dev->host_lpis )
+        goto out_unlock;

The expectation is that we are going to allocate far more than 32 lpis for one device, possibly thousands, right?

Mmh, what makes you think so?
My understanding is that you have one MSI per interrupt "reason" (TX, RX, error, for instance). And even if you have multiple queues, each connected to separate MSIs, that won't go into the thousands. MSI-X for instance is *limited* to 2048 MSIs.

Quoted 6 lines

But in reality I dont' see nearly that many MSIs, mostly less than a dozen per device. And since 32 is the old PCI MSI limit, I thought this would be a good starting point. So most of the time we just need one block.

I believe this could add up with virtual functions, but those are separate devices, right?

Yes, you are right about that, but even 2048 can make a difference when taken 32 at a time: an array of 64 entries vs. 1 entry (if the allocation is fully contiguous), and that for each device, in the case of SR-IOV they can be hundreds.

Quoted 7 lines · Julien Grall

Not answering directly to the question. But I think this is an improvement and not necessary for a first version.

I would like to see this series merged in Xen 4.9 as a tech preview, so I would suggest to gather this list of improvement (maybe on Jira?) and defer them for Xen 4.10. They would need to be addressed before making ITS stable. Stefano, does it sound good to you?

Yes, I am OK with not having this in 4.9, but we need to keep track (Jira or otherwise) of these things.

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

#54 lore raw

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

Hi Stefano,

Quoted 5 lines · Stefano Stabellini

On Thu, 23 Mar 2017, Andre Przywara wrote:

Quoted 3 lines · Julien Grall

Not answering directly to the question. But I think this is an improvement and not necessary for a first version.

Quoted 7 lines
Quoted 4 lines

I would like to see this series merged in Xen 4.9 as a tech preview, so I would suggest to gather this list of improvement (maybe on Jira?) and defer them for Xen 4.10. They would need to be addressed before making ITS stable. Stefano, does it sound good to you?

Yes, I am OK with not having this in 4.9, but we need to keep track (Jira or otherwise) of these things.

I have created XEN-60 for this item and linked to XEN-2 (the Xen ITS task).

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

In reply to Andre Przywara

Hi Andre,

Quoted 79 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.
By using the naturally atomic access guarantee the native uint64_t data type gives us, we allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-v3-its.c        |  90 ++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c        | 188 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic.h        |   5 ++
 xen/include/asm-arm/gic_v3_its.h |  11 +++
 4 files changed, 292 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +28 -1
index 60b15b5..ed14d95 100644
@@ -148,6 +148,20 @@ static int its_send_cmd_sync(struct host_its *its, unsigned int cpu)
     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, uint32_t collection_id,
                              unsigned int cpu)
 {
@@ -180,6 +194,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)
 {
@@ -462,7 +489,7 @@ int gicv3_its_init(void)
 
 static int remove_mapped_guest_device(struct its_devices *dev)
 {
-    int ret;
+    int ret, i;
 
     if ( dev->hw_its )
     {

xen/arch/arm/gic-v3-its.c static int remove_mapped_guest_device(struct its_devices *dev) · line 504

Quoted 7 lines
xen/arch/arm/gic-v3-its.c +4 -0
@@ -471,11 +498,19 @@ static int remove_mapped_guest_device(struct its_devices *dev)
             return ret;
     }
 
+    /*
+     * The only error the function below would return is -ENOENT, in which
+     * case there is nothing to free here. So we just ignore it.
+     */

The function gicv3_free_host_lpi_block will only be used here. And to be fair, if you try to free something that does not exist then it is not really an error...

So I would prefer to see the function to be void.

xen/arch/arm/gic-v3-its.c static int remove_mapped_guest_device(struct its_devices *dev) · line 506

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+    for ( i = 0; i < DIV_ROUND_UP(dev->eventids, LPI_BLOCK); i++ )
+        gicv3_free_host_lpi_block(dev->hw_its, dev->host_lpis[i]);

Again, without looking at the implementation of gicv3_free_host_lpi_block, I think the usage of the function is very confusing. When I read host_lpis, I expect to see one LPI per event. But instead it be the first LPI of a block. The lack of documentation of the field in its_devices does not help to understand what's going on.

So please add some documentation and probably renaming some fields.

Lastly, you have not answered to my question: should not we discard the LPIs before removing the device? Or does MAPD take care for you?"

xen/arch/arm/gic-v3-its.c static int remove_mapped_guest_device(struct its_devices *dev) · line 510

Quoted 4 lines
xen/arch/arm/gic-v3-its.c +1 -0
+
     ret = gicv3_its_wait_commands(dev->hw_its);
     if ( ret )
         return ret;

I know I asked to wait the command, thank you for addressing it. But now, if the function fail you will end-up to leak memory. This is not better than failing to wait commands.

Quoted 18 lines
xen/arch/arm/gic-v3-its.c +9 -0
     xfree(dev->itt_addr);
+    xfree(dev->host_lpis);
     xfree(dev);
 
     return 0;
@@ -513,6 +548,36 @@ static int compare_its_guest_devices(struct its_devices *dev,
 }
 
 /*
+ * On the host ITS @its, map @nr_events consecutive LPIs.
+ * The mapping connects a device @devid and event @eventid pair to LPI @lpi,
+ * increasing both @eventid and @lpi to cover the number of requested LPIs.
+ */
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t devid, uint32_t eventid, uint32_t lpi,
+                              uint32_t nr_events)
+{

xen/arch/arm/gic-v3-its.c static int compare_its_guest_devices(struct its_devices *dev, · line 568

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    uint32_t i;
+    int ret;
+
+    for ( i = 0; i < nr_events; i++ )
+    {
+        /* For now we map every host LPI to host CPU 0 */
+        ret = its_send_cmd_mapti(its, devid, eventid + i, lpi + i, 0);
+        if ( ret )
+            return ret;
+        ret = its_send_cmd_inv(its, devid, eventid + i);

So the spec allows up to 32KB event per device. As all the LPIs will be routed to CPU0 (e.g collection 0), it would be more efficient to do an INVALL. I would be happy to defer that to post Xen 4.9, but the rest needs to be fixed.

Furthermore, what if the queue is full? AFAIU, you will return an error but it is not propagate. So Xen will think the device has been mapped even if it is not true.

I think we need to have a plan here as this may likely happen if a device has many MSI and/or the queue is nearly full.

Quoted 16 lines
xen/arch/arm/gic-v3-its.c +12 -0
+        if ( ret )
+            return ret;
+    }
+
+    ret = its_send_cmd_sync(its, 0);
+    if ( ret )
+        return ret;
+
+    return gicv3_its_wait_commands(its);
+}
+
+/*
  * Map a hardware device, identified by a certain host ITS and its device ID
  * to domain d, a guest ITS (identified by its doorbell address) and device ID.
  * Also provide the number of events (MSIs) needed for that device.

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 596

Quoted 5 lines
xen/arch/arm/gic-v3-its.c +1 -1
@@ -528,7 +593,7 @@ int gicv3_its_map_guest_device(struct domain *d,
     struct host_its *hw_its;
     struct its_devices *dev = NULL, *temp;
     struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent = NULL;
-    int ret = -ENOENT;
+    int ret = -ENOENT, i;

Again, i should be unsigned.

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 643

Quoted 9 lines
xen/arch/arm/gic-v3-its.c +2 -0
     hw_its = gicv3_its_find_by_doorbell(host_doorbell);
     if ( !hw_its )
@@ -574,6 +639,11 @@ int gicv3_its_map_guest_device(struct domain *d,
     if ( !dev )
         goto out_unlock;
 
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));

Rather than having DIV_ROUND_UP spread everywhere. Would not be easier to round_up nr_events once for all?

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +3 -0
+    if ( !dev->host_lpis )
+        goto out_unlock;
+
     ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
                             virt_to_maddr(itt_addr), true);
     if ( ret )
@@ -591,10 +661,26 @@ int gicv3_its_map_guest_device(struct domain *d,
 
     spin_unlock(&d->arch.vgic.its_devices_lock);
 

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 673

Quoted 10 lines
xen/arch/arm/gic-v3-its.c +10 -0
+    /*
+     * Map all host LPIs within this device already. We can't afford to queue
+     * any host ITS commands later on during the guest's runtime.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(nr_events, LPI_BLOCK); i++ )
+    {
+        ret = gicv3_allocate_host_lpi_block(hw_its, d, host_devid,
+                                            i * LPI_BLOCK);
+        if ( ret < 0 )
+            goto out;

This sounds quite wrong to me. If you fail to allocate a block, you will end-up with half of the device configured. However, you are freeing the memory but not reverting the ITS command. This is going to end up very badly.

Please be really careful with the error path. Most of them are not correct and I don't want to end up dealing with security issue later on.

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 675

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+
+        dev->host_lpis[i] = ret;

Hmmm... as I said on the previous version you should not use the return for both error code and LPIs. LPIs are encoded on 32-bit, sooner or later there will be a clash between the error and the LPI.

Quoted 82 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    }
+
     return 0;
 
 out_unlock:
     spin_unlock(&d->arch.vgic.its_devices_lock);
+
+out:
     if ( dev )
         xfree(dev->host_lpis);
     xfree(itt_addr);
xen/arch/arm/gic-v3-lpi.c +33 -0
index 51d7425..59d3ba4 100644
@@ -20,24 +20,44 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sched.h>
+#include <asm/atomic.h>
+#include <asm/domain.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 #include <asm/page.h>
 
+/* LPIs on the host always go to a guest, so no struct irq_desc for them. */
+union host_lpi {
+    uint64_t data;
+    struct {
+        uint32_t virt_lpi;
+        uint16_t dom_id;
+        uint16_t vcpu_id;
+    };
+};
+
 #define LPI_PROPTABLE_NEEDS_FLUSHING    (1U << 0)
 /* Global state */
 static struct {
     /* The global LPI property table, shared by all redistributors. */
     uint8_t *lpi_property;
     /*
+     * A two-level table to lookup LPIs firing on the host and look up the
+     * domain and virtual LPI number to inject.
+     */
+    union host_lpi **host_lpis;
+    /*
      * Number of physical LPIs the host supports. This is a property of
      * the GIC hardware. We depart from the habit of naming these things
      * "physical" in Xen, as the GICv3/4 spec uses the term "physical LPI"
      * in a different context to differentiate them from "virtual LPIs".
      */
     unsigned long int nr_host_lpis;
+    /* Protects allocation and deallocation of host LPIs, but not the access */
+    spinlock_t host_lpis_lock;
     unsigned int flags;
 } lpi_data;
 
@@ -50,6 +70,19 @@ struct lpi_redist_data {
 static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
 
 #define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)
+#define HOST_LPIS_PER_PAGE      (PAGE_SIZE / sizeof(union host_lpi))
+
+static union host_lpi *gic_get_host_lpi(uint32_t plpi)
+{
+    if ( !is_lpi(plpi) || plpi >= MAX_PHYS_LPIS + LPI_OFFSET )
+        return NULL;
+
+    plpi -= LPI_OFFSET;
+    if ( !lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE] )
+        return NULL;
+
+    return &lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE][plpi % HOST_LPIS_PER_PAGE];
+}
 
 /* Stores this redistributor's physical address and ID in a per-CPU variable */
 void gicv3_set_redist_address(paddr_t address, unsigned int redist_id)
@@ -204,15 +237,170 @@ int gicv3_lpi_init_rdist(void __iomem * rdist_base)
 static unsigned int max_lpi_bits = CONFIG_MAX_PHYS_LPI_BITS;

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 247

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
 integer_param("max_lpi_bits", max_lpi_bits);
 
+/*
+ * 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(unsigned int hw_lpi_bits)
 {
+    int nr_lpi_ptrs;

unsigned int please.

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c +7 -0
+
+    /* We rely on the data structure being atomically accessible. */
+    BUILD_BUG_ON(sizeof(union host_lpi) > sizeof(unsigned long));
+
     lpi_data.nr_host_lpis = BIT_ULL(min(hw_lpi_bits, max_lpi_bits));
 
+    spin_lock_init(&lpi_data.host_lpis_lock);
+
+    nr_lpi_ptrs = MAX_PHYS_LPIS / (PAGE_SIZE / sizeof(union host_lpi));

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 266

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +5 -0
+    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_PHYS_LPIS);
 
     return 0;
 }
 
+/* Must be called with host_lpis_lock held. */

Again, this is a call for adding an ASSERT in the function.

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 267

Quoted 1 line
xen/arch/arm/gic-v3-lpi.c +1 -0
+static int find_unused_host_lpi(int start, uint32_t *index)

start should probably be unsigned.

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 269

Quoted 2 lines
xen/arch/arm/gic-v3-lpi.c +2 -0
+{
+    int chunk;

Ditto.

Quoted 23 lines
xen/arch/arm/gic-v3-lpi.c +23 -0
+    uint32_t i = *index;
+
+    for ( chunk = start; chunk < MAX_PHYS_LPIS / HOST_LPIS_PER_PAGE; chunk++ )
+    {
+        /* If we hit an unallocated chunk, use entry 0 in that one. */
+        if ( !lpi_data.host_lpis[chunk] )
+        {
+            *index = 0;
+            return chunk;
+        }
+
+        /* Find an unallocated entry in this chunk. */
+        for ( ; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+        {
+            if ( lpi_data.host_lpis[chunk][i].dom_id == INVALID_DOMID )
+            {
+                *index = i;
+                return chunk;
+            }
+        }
+        i = 0;
+    }
+

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 302

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+    return -1;
+}
+
+/*
+ * Allocate a block of 32 LPIs on the given host ITS for device "devid",
+ * starting with "eventid". Put them into the respective ITT by issuing a
+ * MAPTI command for each of them.
+ */
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+                                  uint32_t host_devid, uint32_t eventid)

Again, most of the parameters here is just for calling back ITS and the caller should all in hand. So I would much prefer to avoid a call chain ITS -> LPI -> ITS and make the LPI code ITS agnostic.

Furthermore, the return value is both used to return an error and the LPI. Given that an LPI is encoded on 32-bit, sooner or later there will be a clash with between the error and the LPI. So it would be wise to dissociate the error code and the LPIs.

The prototype of this function would like:

int gicv3_allocate_host_lpi_block(struct domain *domain, uint32_t *first_lpi);

Quoted 8 lines
xen/arch/arm/gic-v3-lpi.c +8 -0
+{
+    static uint32_t next_lpi = 0;
+    uint32_t lpi, lpi_idx = next_lpi % HOST_LPIS_PER_PAGE;
+    int chunk;
+    int i;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+    chunk = find_unused_host_lpi(next_lpi / HOST_LPIS_PER_PAGE, &lpi_idx);

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 320

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+
+    if ( chunk == - 1 )          /* rescan for a hole from the beginning */
+    {
+        lpi_idx = 0;
+        chunk = find_unused_host_lpi(0, &lpi_idx);
+        if ( chunk == -1 )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
+            return -ENOSPC;
+        }

The algo does not seem to have changed since the previous version. Looking at it again, my understanding is you will always try to allocate forward. So if the current chunk is full, you will allocate the next one rather than looking whether a previous chunk has space available. This will result to allocate more memory than necessary.

Similarly unused chunk could be freed to save memory.

To be clear, I am asking for confirmation, documentation, and listing as an improvement.

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 328

Quoted 8 lines
xen/arch/arm/gic-v3-lpi.c +8 -0
+    }
+
+    /* If we hit an unallocated chunk, we initialize it and use entry 0. */
+    if ( !lpi_data.host_lpis[chunk] )
+    {
+        union host_lpi *new_chunk;
+
+        new_chunk = alloc_xenheap_pages(0, 0);

As said on the previous version, please use alloc_xenheap_page as you only allocate one page.

Also, when NUMA support will be added we may want to take into account the node associated to the device saving us some time when reading the memory. You don't need to handle that now, but a TODO would be quite helpful.

Quoted 16 lines
xen/arch/arm/gic-v3-lpi.c +16 -0
+        if ( !new_chunk )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
+            return -ENOMEM;
+        }
+
+        for ( i = 0; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+            new_chunk[i].dom_id = INVALID_DOMID;
+
+        lpi_data.host_lpis[chunk] = new_chunk;
+        lpi_idx = 0;
+    }
+
+    lpi = chunk * HOST_LPIS_PER_PAGE + lpi_idx;
+
+    for ( i = 0; i < LPI_BLOCK; i++ )

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 354

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+    {
+        union host_lpi hlpi;
+
+        /*
+         * Mark this host LPI as belonging to the domain, but don't assign
+         * any virtual LPI or a VCPU yet.
+         */
+        hlpi.virt_lpi = INVALID_LPI;
+        hlpi.dom_id = d->domain_id;
+        hlpi.vcpu_id = INVALID_DOMID;

Again, please don't mix vcpu and domain. If INVALID_VCPUID does not exist then it might be worth adding one.

Quoted 13 lines
xen/arch/arm/gic-v3-lpi.c +13 -0
+        write_u64_atomic(&lpi_data.host_lpis[chunk][lpi_idx + i].data,
+                         hlpi.data);
+
+        /*
+         * Enable this host LPI, so we don't have to do this during the
+         * guest's runtime.
+         */
+        lpi_data.lpi_property[lpi + i] |= LPI_PROP_ENABLED;
+    }
+
+    /*
+     * We have allocated and initialized the host LPI entries, so it's safe
+     * to drop the lock now. Access to the structures can be done concurrently

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 377

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+     * as it involves only an atomic uint64_t access.
+     */
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    if ( lpi_data.flags & LPI_PROPTABLE_NEEDS_FLUSHING )
+        clean_and_invalidate_dcache_va_range(&lpi_data.lpi_property[lpi],
+                                             LPI_BLOCK);
+
+    gicv3_its_map_host_events(its, host_devid, eventid, lpi + LPI_OFFSET,
+                              LPI_BLOCK);

See my comment at the top of the function.

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 379

Quoted 2 lines
xen/arch/arm/gic-v3-lpi.c +2 -0
+
+    next_lpi = lpi + LPI_BLOCK;

Newline here please.

Quoted 8 lines
xen/arch/arm/gic-v3-lpi.c +8 -0
+    return lpi + LPI_OFFSET;
+}
+
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi)
+{
+    union host_lpi *hlpi, empty_lpi = { .dom_id = INVALID_DOMID };
+    int i;
+

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 397

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+    hlpi = gic_get_host_lpi(lpi);
+    if ( !hlpi )
+        return -ENOENT;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+
+    for ( i = 0; i < LPI_BLOCK; i++ )
+        write_u64_atomic(&hlpi[i].data, empty_lpi.data);
+
+    /* TODO: Call a function in gic-v3-its.c to send DISCARDs */

It is quite worrying to see a TODO in a code that is supposed to be merged really soon.

I think this should be done by the caller and not here. You also need to disable the interrupts which has not been done here.

Quoted 14 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
+
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-arm/gic.h +0 -0
index 12bd155..7825575 100644

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

Quoted 5 lines
xen/include/asm-arm/gic.h +1 -0
@@ -220,7 +220,12 @@ enum gic_version {
     GIC_V3,
 };
 
+#define INVALID_LPI     0
 #define LPI_OFFSET      8192

Again, newline here please.

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

Quoted 4 lines
xen/include/asm-arm/gic.h +4 -0
+static inline bool is_lpi(unsigned int irq)
+{
+    return irq >= LPI_OFFSET;
+}

Again, I think both INVALID_LPI and is_lpi should be moved in irq.h.

xen/include/asm-arm/gic_v3_its.h line 109

Quoted 12 lines
xen/include/asm-arm/gic.h +0 -0
 extern enum gic_version gic_hw_version(void);
 
xen/include/asm-arm/gic_v3_its.h +1 -0
index 3421ea0..2387972 100644
@@ -106,6 +106,11 @@
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
 
+#define INVALID_DOMID ((uint16_t)~0)

Again, rather than defining your own invalid domid, it would be better to use the one defined in xen.h (see DOMID_INVALID).

Quoted 11 lines
xen/include/asm-arm/gic_v3_its.h +4 -0
+
+/* We allocate LPIs on the hosts in chunks of 32 to reduce handling overhead. */
+#define LPI_BLOCK                       32
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -151,6 +156,12 @@ int gicv3_its_map_guest_device(struct domain *d,
                                paddr_t host_doorbell, uint32_t host_devid,
                                paddr_t guest_doorbell, uint32_t guest_devid,

xen/include/asm-arm/gic_v3_its.h int gicv3_its_map_guest_device(struct domain *d, · line 167

Quoted 10 lines
xen/include/asm-arm/gic_v3_its.h +6 -0
                                uint32_t nr_events, bool valid);
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t host_devid, uint32_t eventid,
+                              uint32_t lpi, uint32_t nrevents);
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+                                  uint32_t host_devid, uint32_t eventid);
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);
 
 #else
 

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

Hi,

Quoted 101 lines · Julien Grall

Hi Andre,

Quoted 91 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.
By using the naturally atomic access guarantee the native uint64_t data type gives us, we allocate and access LPI information in a way that does not require a lock.

---
 xen/arch/arm/gic-v3-its.c        |  90 ++++++++++++++++++-
 xen/arch/arm/gic-v3-lpi.c        | 188
+++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic.h        |   5 ++
 xen/include/asm-arm/gic_v3_its.h |  11 +++
 4 files changed, 292 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-its.c +14 -0
index 60b15b5..ed14d95 100644
@@ -148,6 +148,20 @@ static int its_send_cmd_sync(struct host_its
 *its, unsigned int cpu)
     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, uint32_t
 collection_id,

unsigned int cpu)
{

xen/arch/arm/gic-v3-its.c +13 -0
@@ -180,6 +194,19 @@ static int its_send_cmd_mapd(struct host_its
 *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 /* Set up the (1:1) collection mapping for the given host CPU. */
 int gicv3_its_setup_collection(unsigned int cpu)

{

xen/arch/arm/gic-v3-its.c +3 -1
@@ -462,7 +489,7 @@ int gicv3_its_init(void)
 
 static int remove_mapped_guest_device(struct its_devices *dev)
 {
-    int ret;
+    int ret, i;
 
     if ( dev->hw_its )
     {
@@ -471,11 +498,19 @@ static int remove_mapped_guest_device(struct
 its_devices *dev)
             return ret;
     }
 
+    /*
+     * The only error the function below would return is -ENOENT, in

which

xen/arch/arm/gic-v3-its.c static int remove_mapped_guest_device(struct its_devices *dev) · line 504

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+     * case there is nothing to free here. So we just ignore it.
+     */

The function gicv3_free_host_lpi_block will only be used here. And to be fair, if you try to free something that does not exist then it is not really an error...

Quoted 1 line

So I would prefer to see the function to be void.

Fixed in v3.

Quoted 10 lines

xen/arch/arm/gic-v3-its.c static int remove_mapped_guest_device(struct its_devices *dev) · line 506

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+    for ( i = 0; i < DIV_ROUND_UP(dev->eventids, LPI_BLOCK); i++ )
+        gicv3_free_host_lpi_block(dev->hw_its, dev->host_lpis[i]);

Again, without looking at the implementation of gicv3_free_host_lpi_block, I think the usage of the function is very confusing. When I read host_lpis, I expect to see one LPI per event. But instead it be the first LPI of a block. The lack of documentation of the field in its_devices does not help to understand what's going on.

So please add some documentation and probably renaming some fields.

Fixed in v3.

Quoted 3 lines

Lastly, you have not answered to my question: should not we discard the LPIs before removing the device? Or does MAPD take care for you?"

Yes: "MAPD removes the mapping of the specified DeviceID. and interrupt requests from that device are discarded."

So we don't need to issue individual DISCARDs if the device in unmapped.

Quoted 8 lines
Quoted 4 lines

+

     ret = gicv3_its_wait_commands(dev->hw_its);
     if ( ret )
         return ret;

I know I asked to wait the command, thank you for addressing it. But now, if the function fail you will end-up to leak memory. This is not better than failing to wait commands.

Fixed in v3.

Quoted 30 lines
Quoted 30 lines
     xfree(dev->itt_addr);
xen/arch/arm/gic-v3-its.c +4 -0
+    xfree(dev->host_lpis);
     xfree(dev);
 
     return 0;
@@ -513,6 +548,36 @@ static int compare_its_guest_devices(struct
 its_devices *dev,
 }
 
 /*
+ * On the host ITS @its, map @nr_events consecutive LPIs.
+ * The mapping connects a device @devid and event @eventid pair to
 LPI @lpi,
+ * increasing both @eventid and @lpi to cover the number of requested

LPIs.
+ */

xen/arch/arm/gic-v3-its.c +2 -0
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t devid, uint32_t eventid,

uint32_t lpi,

xen/arch/arm/gic-v3-its.c +10 -0
+                              uint32_t nr_events)
+{
+    uint32_t i;
+    int ret;
+
+    for ( i = 0; i < nr_events; i++ )
+    {
+        /* For now we map every host LPI to host CPU 0 */
+        ret = its_send_cmd_mapti(its, devid, eventid + i, lpi + i, 0);
+        if ( ret )
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static int compare_its_guest_devices(struct its_devices *dev, · line 568

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+            return ret;
+        ret = its_send_cmd_inv(its, devid, eventid + i);

So the spec allows up to 32KB event per device. As all the LPIs will be routed to CPU0 (e.g collection 0), it would be more efficient to do an INVALL. I would be happy to defer that to post Xen 4.9, but the rest needs to be fixed.

I tried INVALL and it didn't work, at least on the model. I can't see why, so I kept the individual INVs in.
I have the patch still lying around, so we can revisit this later.

Quoted 3 lines

Furthermore, what if the queue is full? AFAIU, you will return an error but it is not propagate. So Xen will think the device has been mapped even if it is not true.

In v3 the error is now checked in the caller and gicv3_its_map_guest_device() cleans up and returns an error. In the moment this is called from the MAPD emulation, so there is no good way of communicating this back to the guest, but in the future this can return the error to the hypercall and Dom0 can react accordingly.

Quoted 21 lines

I think we need to have a plan here as this may likely happen if a device has many MSI and/or the queue is nearly full.

Quoted 18 lines
xen/arch/arm/gic-v3-its.c +12 -0
+        if ( ret )
+            return ret;
+    }
+
+    ret = its_send_cmd_sync(its, 0);
+    if ( ret )
+        return ret;
+
+    return gicv3_its_wait_commands(its);
+}
+
+/*
  * Map a hardware device, identified by a certain host ITS and its

device ID
* to domain d, a guest ITS (identified by its doorbell address) and
device ID.
* Also provide the number of events (MSIs) needed for that device.

xen/arch/arm/gic-v3-its.c +0 -0
@@ -528,7 +593,7 @@ int gicv3_its_map_guest_device(struct domain *d,
Quoted 8 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 596

Quoted 6 lines
xen/arch/arm/gic-v3-its.c +1 -1
     struct host_its *hw_its;
     struct its_devices *dev = NULL, *temp;
     struct rb_node **new = &d->arch.vgic.its_devices.rb_node, *parent
 = NULL;
-    int ret = -ENOENT;
+    int ret = -ENOENT, i;

Again, i should be unsigned.

Fixed in v4.

Quoted 7 lines
Quoted 7 lines
xen/arch/arm/gic-v3-its.c +0 -0
     hw_its = gicv3_its_find_by_doorbell(host_doorbell);
     if ( !hw_its )
@@ -574,6 +639,11 @@ int gicv3_its_map_guest_device(struct domain *d,
     if ( !dev )
         goto out_unlock;
 
Quoted 5 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 643

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));

Rather than having DIV_ROUND_UP spread everywhere. Would not be easier to round_up nr_events once for all?

I'd rather keep nr_events as the actual number around. I think we might look into actually limiting the allocation later.

Quoted 11 lines
Quoted 11 lines
xen/arch/arm/gic-v3-its.c +3 -0
+    if ( !dev->host_lpis )
+        goto out_unlock;
+
     ret = its_send_cmd_mapd(hw_its, host_devid, fls(nr_events - 1) - 1,
                             virt_to_maddr(itt_addr), true);
     if ( ret )
@@ -591,10 +661,26 @@ int gicv3_its_map_guest_device(struct domain *d,
 
     spin_unlock(&d->arch.vgic.its_devices_lock);
 
Quoted 16 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 673

Quoted 11 lines
xen/arch/arm/gic-v3-its.c +2 -0
+    /*
+     * Map all host LPIs within this device already. We can't afford

to queue

xen/arch/arm/gic-v3-its.c +8 -0
+     * any host ITS commands later on during the guest's runtime.
+     */
+    for ( i = 0; i < DIV_ROUND_UP(nr_events, LPI_BLOCK); i++ )
+    {
+        ret = gicv3_allocate_host_lpi_block(hw_its, d, host_devid,
+                                            i * LPI_BLOCK);
+        if ( ret < 0 )
+            goto out;

This sounds quite wrong to me. If you fail to allocate a block, you will end-up with half of the device configured. However, you are freeing the memory but not reverting the ITS command. This is going to end up very badly.

I need to check again whether this is really a problem here.

Quoted 9 lines

Please be really careful with the error path. Most of them are not correct and I don't want to end up dealing with security issue later on.

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 675

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+
+        dev->host_lpis[i] = ret;

Hmmm... as I said on the previous version you should not use the return for both error code and LPIs. LPIs are encoded on 32-bit, sooner or later there will be a clash between the error and the LPI.

Fixed in v3.

Quoted 92 lines
Quoted 92 lines
xen/arch/arm/gic-v3-its.c +4 -0
+    }
+
     return 0;
 
 out_unlock:
     spin_unlock(&d->arch.vgic.its_devices_lock);
+
+out:
     if ( dev )
         xfree(dev->host_lpis);
     xfree(itt_addr);
xen/arch/arm/gic-v3-lpi.c +4 -0
index 51d7425..59d3ba4 100644
@@ -20,24 +20,44 @@
 
 #include <xen/lib.h>
 #include <xen/mm.h>
+#include <xen/sched.h>
+#include <asm/atomic.h>
+#include <asm/domain.h>
 #include <asm/gic.h>
 #include <asm/gic_v3_defs.h>
 #include <asm/gic_v3_its.h>
 #include <asm/io.h>
 #include <asm/page.h>
 
+/* LPIs on the host always go to a guest, so no struct irq_desc for

them. */

xen/arch/arm/gic-v3-lpi.c +10 -0
+union host_lpi {
+    uint64_t data;
+    struct {
+        uint32_t virt_lpi;
+        uint16_t dom_id;
+        uint16_t vcpu_id;
+    };
+};
+
 #define LPI_PROPTABLE_NEEDS_FLUSHING    (1U << 0)
 /* Global state */
 static struct {
     /* The global LPI property table, shared by all redistributors. */
     uint8_t *lpi_property;
     /*
+     * A two-level table to lookup LPIs firing on the host and look

up the

xen/arch/arm/gic-v3-lpi.c +4 -0
+     * domain and virtual LPI number to inject.
+     */
+    union host_lpi **host_lpis;
+    /*
      * Number of physical LPIs the host supports. This is a property of
      * the GIC hardware. We depart from the habit of naming these things
      * "physical" in Xen, as the GICv3/4 spec uses the term "physical

LPI"

      * in a different context to differentiate them from "virtual LPIs".
      */
     unsigned long int nr_host_lpis;

+ /* Protects allocation and deallocation of host LPIs, but not the
access */

xen/arch/arm/gic-v3-lpi.c +14 -0
+    spinlock_t host_lpis_lock;
     unsigned int flags;
 } lpi_data;
@@ -50,6 +70,19 @@ struct lpi_redist_data {
 static DEFINE_PER_CPU(struct lpi_redist_data, lpi_redist);
 
 #define MAX_PHYS_LPIS   (lpi_data.nr_host_lpis - LPI_OFFSET)
+#define HOST_LPIS_PER_PAGE      (PAGE_SIZE / sizeof(union host_lpi))
+
+static union host_lpi *gic_get_host_lpi(uint32_t plpi)
+{
+    if ( !is_lpi(plpi) || plpi >= MAX_PHYS_LPIS + LPI_OFFSET )
+        return NULL;
+
+    plpi -= LPI_OFFSET;
+    if ( !lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE] )
+        return NULL;
+
+    return &lpi_data.host_lpis[plpi / HOST_LPIS_PER_PAGE][plpi %
 HOST_LPIS_PER_PAGE];
+}
 
 /* Stores this redistributor's physical address and ID in a per-CPU

variable */
void gicv3_set_redist_address(paddr_t address, unsigned int redist_id)

xen/arch/arm/gic-v3-lpi.c +0 -0
@@ -204,15 +237,170 @@ int gicv3_lpi_init_rdist(void __iomem *
 rdist_base)
 static unsigned int max_lpi_bits = CONFIG_MAX_PHYS_LPI_BITS;
 integer_param("max_lpi_bits", max_lpi_bits);
 
Quoted 10 lines

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 247

Quoted 8 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
+/*
+ * 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(unsigned int hw_lpi_bits)
 {
+    int nr_lpi_ptrs;

unsigned int please.

Fixed in v4.

Quoted 16 lines
Quoted 16 lines
xen/arch/arm/gic-v3-lpi.c +11 -0
+
+    /* We rely on the data structure being atomically accessible. */
+    BUILD_BUG_ON(sizeof(union host_lpi) > sizeof(unsigned long));
+
     lpi_data.nr_host_lpis = BIT_ULL(min(hw_lpi_bits, max_lpi_bits));
 
+    spin_lock_init(&lpi_data.host_lpis_lock);
+
+    nr_lpi_ptrs = MAX_PHYS_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_PHYS_LPIS);
 
Quoted 6 lines

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 266

Quoted 4 lines
xen/arch/arm/gic-v3-lpi.c +1 -0
     return 0;
 }
 
+/* Must be called with host_lpis_lock held. */

Again, this is a call for adding an ASSERT in the function.

This comment is more like lock documentation, to give code writers a guidance how the locking should be handled here.
I am not convinced that having ASSERTS in *static* functions is really useful.

Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 267

Quoted 1 line
xen/arch/arm/gic-v3-lpi.c +1 -0
+static int find_unused_host_lpi(int start, uint32_t *index)

start should probably be unsigned.

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * · line 271

Quoted 2 lines
xen/arch/arm/gic-v3-lpi.c +2 -0
+{
+    int chunk;

Ditto.

Fixed in v3.

Quoted 46 lines
Quoted 30 lines
xen/arch/arm/gic-v3-lpi.c +29 -0
+    uint32_t i = *index;
+
+    for ( chunk = start; chunk < MAX_PHYS_LPIS / HOST_LPIS_PER_PAGE;
 chunk++ )
+    {
+        /* If we hit an unallocated chunk, use entry 0 in that one. */
+        if ( !lpi_data.host_lpis[chunk] )
+        {
+            *index = 0;
+            return chunk;
+        }
+
+        /* Find an unallocated entry in this chunk. */
+        for ( ; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+        {
+            if ( lpi_data.host_lpis[chunk][i].dom_id == INVALID_DOMID )
+            {
+                *index = i;
+                return chunk;
+            }
+        }
+        i = 0;
+    }
+
+    return -1;
+}
+
+/*
+ * Allocate a block of 32 LPIs on the given host ITS for device "devid",
+ * starting with "eventid". Put them into the respective ITT by
Quoted 6 lines

issuing a
+ * MAPTI command for each of them.
+ */
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain *d,
+ uint32_t host_devid, uint32_t eventid)

Again, most of the parameters here is just for calling back ITS and the caller should all in hand. So I would much prefer to avoid a call chain ITS -> LPI -> ITS and make the LPI code ITS agnostic.

Furthermore, the return value is both used to return an error and the LPI. Given that an LPI is encoded on 32-bit, sooner or later there will be a clash with between the error and the LPI. So it would be wise to dissociate the error code and the LPIs.

Quoted 4 lines

The prototype of this function would like:

int gicv3_allocate_host_lpi_block(struct domain *domain, uint32_t *first_lpi);

Fixed in v3.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/gic-v3-lpi.c +8 -0
+{
+    static uint32_t next_lpi = 0;
+    uint32_t lpi, lpi_idx = next_lpi % HOST_LPIS_PER_PAGE;
+    int chunk;
+    int i;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+    chunk = find_unused_host_lpi(next_lpi / HOST_LPIS_PER_PAGE,

&lpi_idx);

xen/arch/arm/gic-v3-lpi.c +2 -0
+
+    if ( chunk == - 1 )          /* rescan for a hole from the

beginning */

xen/arch/arm/gic-v3-lpi.c +6 -0
+    {
+        lpi_idx = 0;
+        chunk = find_unused_host_lpi(0, &lpi_idx);
+        if ( chunk == -1 )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c

Quoted 2 lines
xen/arch/arm/gic-v3-lpi.c +2 -0
+            return -ENOSPC;
+        }

The algo does not seem to have changed since the previous version. Looking at it again, my understanding is you will always try to allocate forward. So if the current chunk is full, you will allocate the next one rather than looking whether a previous chunk has space available. This will result to allocate more memory than necessary.

In v4 I amended the code slightly to move next_lpi outside of the function. When we now free an LPI block, we check if the previous next_lpi was pointing after this block and adjust it in this case.

Quoted 1 line

Similarly unused chunk could be freed to save memory.

No, we can't, because this breaks the lockless property. A user (incoming LPI) would find the first level pointer and go into that block. So we can't never replace this block pointer now. That smells like a case for RCU, but I am not sure if Xen can properly handle this case. But with the above optimization (adjusting next_lpi on freeing a block) I am pretty sure this isn't a problem for now, especially for Dom0.

Quoted 15 lines

To be clear, I am asking for confirmation, documentation, and listing as an improvement.

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 328

Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c +3 -0
+    }
+
+    /* If we hit an unallocated chunk, we initialize it and use entry

0. */

xen/arch/arm/gic-v3-lpi.c +5 -0
+    if ( !lpi_data.host_lpis[chunk] )
+    {
+        union host_lpi *new_chunk;
+
+        new_chunk = alloc_xenheap_pages(0, 0);

As said on the previous version, please use alloc_xenheap_page as you only allocate one page.

Fixed in v3.

Quoted 4 lines

Also, when NUMA support will be added we may want to take into account the node associated to the device saving us some time when reading the memory. You don't need to handle that now, but a TODO would be quite helpful.

TODO added in v3.

Quoted 22 lines
Quoted 22 lines
xen/arch/arm/gic-v3-lpi.c +21 -0
+        if ( !new_chunk )
+        {
+            spin_unlock(&lpi_data.host_lpis_lock);
+            return -ENOMEM;
+        }
+
+        for ( i = 0; i < HOST_LPIS_PER_PAGE; i += LPI_BLOCK )
+            new_chunk[i].dom_id = INVALID_DOMID;
+
+        lpi_data.host_lpis[chunk] = new_chunk;
+        lpi_idx = 0;
+    }
+
+    lpi = chunk * HOST_LPIS_PER_PAGE + lpi_idx;
+
+    for ( i = 0; i < LPI_BLOCK; i++ )
+    {
+        union host_lpi hlpi;
+
+        /*
+         * Mark this host LPI as belonging to the domain, but don't

assign

Quoted 8 lines

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 354

Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c +5 -0
+         * any virtual LPI or a VCPU yet.
+         */
+        hlpi.virt_lpi = INVALID_LPI;
+        hlpi.dom_id = d->domain_id;
+        hlpi.vcpu_id = INVALID_DOMID;

Again, please don't mix vcpu and domain. If INVALID_VCPUID does not exist then it might be worth adding one.

Fixed in v4.

Quoted 21 lines
Quoted 21 lines
xen/arch/arm/gic-v3-lpi.c +12 -0
+        write_u64_atomic(&lpi_data.host_lpis[chunk][lpi_idx + i].data,
+                         hlpi.data);
+
+        /*
+         * Enable this host LPI, so we don't have to do this during the
+         * guest's runtime.
+         */
+        lpi_data.lpi_property[lpi + i] |= LPI_PROP_ENABLED;
+    }
+
+    /*
+     * We have allocated and initialized the host LPI entries, so

it's safe
+ * to drop the lock now. Access to the structures can be done
concurrently

xen/arch/arm/gic-v3-lpi.c +6 -0
+     * as it involves only an atomic uint64_t access.
+     */
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    if ( lpi_data.flags & LPI_PROPTABLE_NEEDS_FLUSHING )
+       
Quoted 8 lines
Quoted 6 lines

clean_and_invalidate_dcache_va_range(&lpi_data.lpi_property[lpi],

xen/arch/arm/gic-v3-lpi.c +3 -0
+                                             LPI_BLOCK);
+
+    gicv3_its_map_host_events(its, host_devid, eventid, lpi +

LPI_OFFSET,
+ LPI_BLOCK);

See my comment at the top of the function.

Fixed in v3 (I believe).

Quoted 4 lines

xen/arch/arm/gic-v3-lpi.c

Quoted 2 lines
xen/arch/arm/gic-v3-lpi.c +2 -0
+
+    next_lpi = lpi + LPI_BLOCK;

Newline here please.

Fixed in v3.

Quoted 23 lines
Quoted 9 lines
xen/arch/arm/gic-v3-lpi.c +8 -0
+    return lpi + LPI_OFFSET;
+}
+
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi)
+{
+    union host_lpi *hlpi, empty_lpi = { .dom_id = INVALID_DOMID };
+    int i;
+

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 397

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+    hlpi = gic_get_host_lpi(lpi);
+    if ( !hlpi )
+        return -ENOENT;
+
+    spin_lock(&lpi_data.host_lpis_lock);
+
+    for ( i = 0; i < LPI_BLOCK; i++ )
+        write_u64_atomic(&hlpi[i].data, empty_lpi.data);
+
+    /* TODO: Call a function in gic-v3-its.c to send DISCARDs */

It is quite worrying to see a TODO in a code that is supposed to be merged really soon.

Quoted 2 lines

I think this should be done by the caller and not here. You also need to disable the interrupts which has not been done here.

I removed the TODO in v3, because it was wrong. We unmap the device before, so no need for individual DISCARDs (as mentioned above).

Quoted 17 lines
Quoted 17 lines
xen/arch/arm/gic-v3-lpi.c +6 -0
+
+    spin_unlock(&lpi_data.host_lpis_lock);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/include/asm-arm/gic.h +0 -0
index 12bd155..7825575 100644
@@ -220,7 +220,12 @@ enum gic_version {
     GIC_V3,
 };
 
Quoted 4 lines

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

Quoted 2 lines
xen/include/asm-arm/gic.h +1 -0
+#define INVALID_LPI     0
 #define LPI_OFFSET      8192

Again, newline here please.

Fixed in v3.

Quoted 6 lines

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

Quoted 4 lines
xen/include/asm-arm/gic.h +4 -0
+static inline bool is_lpi(unsigned int irq)
+{
+    return irq >= LPI_OFFSET;
+}

Again, I think both INVALID_LPI and is_lpi should be moved in irq.h.

Fixed in v3.

Quoted 12 lines
Quoted 12 lines
xen/include/asm-arm/gic.h +0 -0
 extern enum gic_version gic_hw_version(void);
 
xen/include/asm-arm/gic_v3_its.h +0 -0
index 3421ea0..2387972 100644
@@ -106,6 +106,11 @@
 #define HOST_ITS_FLUSH_CMD_QUEUE        (1U << 0)
 #define HOST_ITS_USES_PTA               (1U << 1)
 
Quoted 4 lines

xen/include/asm-arm/gic_v3_its.h line 109

Quoted 1 line
xen/include/asm-arm/gic_v3_its.h +1 -0
+#define INVALID_DOMID ((uint16_t)~0)

Again, rather than defining your own invalid domid, it would be better to use the one defined in xen.h (see DOMID_INVALID).

Fixed in v3.

Cheers,
Andre.

Quoted 23 lines
Quoted 23 lines
xen/include/asm-arm/gic_v3_its.h +2 -0
+
+/* We allocate LPIs on the hosts in chunks of 32 to reduce handling

overhead. */

xen/include/asm-arm/gic_v3_its.h +7 -0
+#define LPI_BLOCK                       32
+
 /* data structure for each hardware ITS */
 struct host_its {
     struct list_head entry;
@@ -151,6 +156,12 @@ int gicv3_its_map_guest_device(struct domain *d,
                                paddr_t host_doorbell, uint32_t
 host_devid,
                                paddr_t guest_doorbell, uint32_t
 guest_devid,
                                uint32_t nr_events, bool valid);
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t host_devid, uint32_t eventid,
+                              uint32_t lpi, uint32_t nrevents);
+int gicv3_allocate_host_lpi_block(struct host_its *its, struct domain
 *d,
+                                  uint32_t host_devid, uint32_t

eventid);
+int gicv3_free_host_lpi_block(struct host_its *its, uint32_t lpi);

Quoted 6 lines
Quoted 3 lines

#else

Cheers,

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

#57 lore raw

Re: [PATCH v2 08/27] ARM: GICv3 ITS: introduce host LPI array

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 30 lines · Julien Grall
Quoted 20 lines
Quoted 20 lines

/*
+ * On the host ITS @its, map @nr_events consecutive LPIs.
+ * The mapping connects a device @devid and event @eventid pair to LPI @lpi,
+ * increasing both @eventid and @lpi to cover the number of requested LPIs.
+ */

(quoted diff) +2 -0
+int gicv3_its_map_host_events(struct host_its *its,
+                              uint32_t devid, uint32_t eventid,

uint32_t lpi,

xen/arch/arm/gic-v3-its.c +10 -0
+                              uint32_t nr_events)
+{
+    uint32_t i;
+    int ret;
+
+    for ( i = 0; i < nr_events; i++ )
+    {
+        /* For now we map every host LPI to host CPU 0 */
+        ret = its_send_cmd_mapti(its, devid, eventid + i, lpi + i, 0);
+        if ( ret )
Quoted 8 lines

xen/arch/arm/gic-v3-its.c static int compare_its_guest_devices(struct its_devices *dev, · line 568

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+            return ret;
+        ret = its_send_cmd_inv(its, devid, eventid + i);

So the spec allows up to 32KB event per device. As all the LPIs will be routed to CPU0 (e.g collection 0), it would be more efficient to do an INVALL. I would be happy to defer that to post Xen 4.9, but the rest needs to be fixed.

Quoted 3 lines

I tried INVALL and it didn't work, at least on the model. I can't see why, so I kept the individual INVs in.
I have the patch still lying around, so we can revisit this later.

Can you add a TODO comment please?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
     hw_its = gicv3_its_find_by_doorbell(host_doorbell);
     if ( !hw_its )
xen/arch/arm/gic-v3-its.c +0 -0
@@ -574,6 +639,11 @@ int gicv3_its_map_guest_device(struct domain *d,
     if ( !dev )
         goto out_unlock;
 
Quoted 8 lines
Quoted 5 lines

xen/arch/arm/gic-v3-its.c int gicv3_its_map_guest_device(struct domain *d, · line 643

Quoted 2 lines
xen/arch/arm/gic-v3-its.c +2 -0
+    dev->host_lpis = xzalloc_array(uint32_t,
+                                   DIV_ROUND_UP(nr_events, LPI_BLOCK));

Rather than having DIV_ROUND_UP spread everywhere. Would not be easier to round_up nr_events once for all?

I'd rather keep nr_events as the actual number around. I think we might look into actually limiting the allocation later.

Why? This number will likely be a multiple of bit because of the ITS works. You would also have to keep around multiple different value that will make the code more complicate to read...

Quoted 8 lines
Quoted 3 lines

xen/arch/arm/gic-v3-lpi.c int gicv3_lpi_init_rdist(void __iomem * rdist_base) · line 266

Quoted 1 line
xen/arch/arm/gic-v3-lpi.c +1 -0
+/* Must be called with host_lpis_lock held. */

Again, this is a call for adding an ASSERT in the function.

This comment is more like lock documentation, to give code writers a guidance how the locking should be handled here.
I am not convinced that having ASSERTS in *static* functions is really useful.

Well, you seem to assume that you will be the only one to modify this code and it is very easy to skip reading a comment by mistake.

So the ASSERT will catch such error. Give me a reason that the ASSERT is a bad idea and I will re-think my position.

[...]

Quoted 10 lines
Quoted 5 lines

The algo does not seem to have changed since the previous version. Looking at it again, my understanding is you will always try to allocate forward. So if the current chunk is full, you will allocate the next one rather than looking whether a previous chunk has space available. This will result to allocate more memory than necessary.

In v4 I amended the code slightly to move next_lpi outside of the function. When we now free an LPI block, we check if the previous next_lpi was pointing after this block and adjust it in this case.

Quoted 8 lines
Quoted 1 line

Similarly unused chunk could be freed to save memory.

No, we can't, because this breaks the lockless property. A user (incoming LPI) would find the first level pointer and go into that block. So we can't never replace this block pointer now. That smells like a case for RCU, but I am not sure if Xen can properly handle this case. But with the above optimization (adjusting next_lpi on freeing a block) I am pretty sure this isn't a problem for now, especially for Dom0.

Then document it, because this is a call to forget to revisit that in the future.

Cheers,

Signature · 6 lines
Julien Grall

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

PATCH v2 9/27 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.
I added some locking around this list here, however my gut feeling is that we don't need one because this a per-VCPU structure anyway. If someone could confirm this, I'd be grateful.
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       |  3 +++
 xen/arch/arm/vgic.c          | 64 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h |  2 ++
 xen/include/asm-arm/vgic.h   | 14 ++++++++++
 5 files changed, 83 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +3 -0
index a5348f2..bd3c032 100644
@@ -509,6 +509,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 ( is_lpi(p->irq) )
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +3 -0
index 1fadb00..b0653c2 100644
@@ -1426,6 +1426,9 @@ 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;
 
+    spin_lock_init(&v->arch.vgic.pending_lpi_list_lock);
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +61 -3

110 lines not on this page · raw message

xen/include/asm-arm/domain.h +2 -0
index 00b9c1a..f44a84b 100644
@@ -257,6 +257,8 @@ 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;
+        spinlock_t pending_lpi_list_lock;   /* protects the pending_lpi_list */
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/vgic.h +14 -0
index 467333c..8f1099c 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,13 +302,21 @@ 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 bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
 extern void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n);
 extern void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n);
+/* placeholder function until the property table gets introduced */
+static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
+{
+    return 0xa;
+}
 extern void register_vgic_ops(struct domain *d, const struct vgic_ops *ops);
 int vgic_v2_init(struct domain *d, int *mmio_count);
 int vgic_v3_init(struct domain *d, int *mmio_count);
Signature · 7 lines
2.9.0


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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 13 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.
I added some locking around this list here, however my gut feeling is that we don't need one because this a per-VCPU structure anyway. If someone could confirm this, I'd be grateful.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

Quoted 1 line

Still all past comments are unaddress ;-(

It makes very hard to continue doing reviews. I am sorry but I'll have to stop reviewing until I see a series with my past comments addressed. I encourage Julien to do the same.

Quoted 211 lines
---
 xen/arch/arm/gic.c           |  3 +++
 xen/arch/arm/vgic-v3.c       |  3 +++
 xen/arch/arm/vgic.c          | 64 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h |  2 ++
 xen/include/asm-arm/vgic.h   | 14 ++++++++++
 5 files changed, 83 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +3 -0
index a5348f2..bd3c032 100644
@@ -509,6 +509,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 ( is_lpi(p->irq) )
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +3 -0
index 1fadb00..b0653c2 100644
@@ -1426,6 +1426,9 @@ 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;
 
+    spin_lock_init(&v->arch.vgic.pending_lpi_list_lock);
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +61 -3
index 364d5f0..e5cfa54 100644
@@ -30,6 +30,8 @@
 
 #include <asm/mmio.h>
 #include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
 #include <asm/vgic.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 ( is_lpi(virq) )
+        return vgic_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,63 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
     return true;
 }
 
+/*
+ * 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;
+
+    spin_lock(&v->arch.vgic.pending_lpi_list_lock);
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+        {
+            spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+            return &lpi_irq->pirq;
+        }
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
+
+    if ( !allocate )
+    {
+        spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+        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;
+    }
+
+    spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+
+    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 ( is_lpi(irq) )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +536,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 running;
 
@@ -488,6 +544,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 +2 -0
index 00b9c1a..f44a84b 100644
@@ -257,6 +257,8 @@ 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;
+        spinlock_t pending_lpi_list_lock;   /* protects the pending_lpi_list */
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/vgic.h +14 -0
index 467333c..8f1099c 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,13 +302,21 @@ 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 bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
 extern void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n);
 extern void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n);
+/* placeholder function until the property table gets introduced */
+static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
+{
+    return 0xa;
+}
 extern void register_vgic_ops(struct domain *d, const struct vgic_ops *ops);
 int vgic_v2_init(struct domain *d, int *mmio_count);
 int vgic_v3_init(struct domain *d, int *mmio_count);
Signature · 1 line
2.9.0

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

#60 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 14 lines · Stefano Stabellini
Quoted 13 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.
I added some locking around this list here, however my gut feeling is that we don't need one because this a per-VCPU structure anyway. If someone could confirm this, I'd be grateful.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

Quoted 3 lines
Quoted 1 line

Still all past comments are unaddress ;-(

Sorry for that. I just see that the reviews for those and later emails were not in the same thread as the other ones, so they were swamped in my inbox and didn't show up in my TODO thread. This might be due to races between direct email and list bounces. Interestingly this seems to affect previous emails as well, which isn't really an excuse, but explains my perceived rudeness in not addressing your comments (simply because I didn't see them).
I will try to sort this mess out in the next few days and send a new version early next week.
If you know from the top of your head any serious architectural change requests you raised, it would be nice if you could repeat them now, so that I can address them early enough.
I will now sort my mails by author name to find any comment I might have missed before.

Sorry again for the mess!

Cheers,
Andre.

Quoted 208 lines

It makes very hard to continue doing reviews. I am sorry but I'll have to stop reviewing until I see a series with my past comments addressed. I encourage Julien to do the same.

Quoted 203 lines
---
 xen/arch/arm/gic.c           |  3 +++
 xen/arch/arm/vgic-v3.c       |  3 +++
 xen/arch/arm/vgic.c          | 64 +++++++++++++++++++++++++++++++++++++++++---
 xen/include/asm-arm/domain.h |  2 ++
 xen/include/asm-arm/vgic.h   | 14 ++++++++++
 5 files changed, 83 insertions(+), 3 deletions(-)
xen/arch/arm/gic.c +3 -0
index a5348f2..bd3c032 100644
@@ -509,6 +509,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 ( is_lpi(p->irq) )
+                p->irq = 0;
         }
     }
 }
xen/arch/arm/vgic-v3.c +3 -0
index 1fadb00..b0653c2 100644
@@ -1426,6 +1426,9 @@ 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;
 
+    spin_lock_init(&v->arch.vgic.pending_lpi_list_lock);
+    INIT_LIST_HEAD(&v->arch.vgic.pending_lpi_list);
+
     return 0;
 }
 
xen/arch/arm/vgic.c +61 -3
index 364d5f0..e5cfa54 100644
@@ -30,6 +30,8 @@
 
 #include <asm/mmio.h>
 #include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>
 #include <asm/vgic.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 ( is_lpi(virq) )
+        return vgic_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,63 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
     return true;
 }
 
+/*
+ * 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;
+
+    spin_lock(&v->arch.vgic.pending_lpi_list_lock);
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+        {
+            spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+            return &lpi_irq->pirq;
+        }
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }
+
+    if ( !allocate )
+    {
+        spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+        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;
+    }
+
+    spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+
+    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 ( is_lpi(irq) )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +536,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 running;
 
@@ -488,6 +544,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 +2 -0
index 00b9c1a..f44a84b 100644
@@ -257,6 +257,8 @@ 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;
+        spinlock_t pending_lpi_list_lock;   /* protects the pending_lpi_list */
     } vgic;
 
     /* Timer registers  */
xen/include/asm-arm/vgic.h +12 -0
index 467333c..8f1099c 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,13 +302,21 @@ 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 bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
 extern void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n);
 extern void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n);
+/* placeholder function until the property table gets introduced */
+static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
+{
Quoted 8 lines
Quoted 8 lines
xen/include/asm-arm/vgic.h +2 -0
+    return 0xa;
+}
 extern void register_vgic_ops(struct domain *d, const struct vgic_ops *ops);
 int vgic_v2_init(struct domain *d, int *mmio_count);
 int vgic_v3_init(struct domain *d, int *mmio_count);
Signature · 1 line
2.9.0

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

#61 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi Andre,

Quoted 19 lines · André Przywara
Quoted 14 lines · Stefano Stabellini
Quoted 13 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.
I added some locking around this list here, however my gut feeling is that we don't need one because this a per-VCPU structure anyway. If someone could confirm this, I'd be grateful.
Teach the existing VGIC functions to find the right pointer when being given a virtual LPI number.

Quoted 3 lines
Quoted 1 line

Still all past comments are unaddress ;-(

Quoted 12 lines

Sorry for that. I just see that the reviews for those and later emails were not in the same thread as the other ones, so they were swamped in my inbox and didn't show up in my TODO thread. This might be due to races between direct email and list bounces. Interestingly this seems to affect previous emails as well, which isn't really an excuse, but explains my perceived rudeness in not addressing your comments (simply because I didn't see them).
I will try to sort this mess out in the next few days and send a new version early next week.
If you know from the top of your head any serious architectural change requests you raised, it would be nice if you could repeat them now, so that I can address them early enough.

I am planning to have a look at the full series today. And will try to point out major architectural changes.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara

Hi Andre

xen/arch/arm/vgic.c line 34

Quoted 10 lines · Andre Przywara
xen/arch/arm/vgic.c +2 -0
index 364d5f0..e5cfa54 100644
@@ -30,6 +30,8 @@
 
 #include <asm/mmio.h>
 #include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>

I really don't want to see gic_v3_* headers included in common code. Why do you need them?

Quoted 13 lines
xen/arch/arm/vgic.c +1 -1
 #include <asm/vgic.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 254

Quoted 10 lines
xen/arch/arm/vgic.c +3 -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 ( is_lpi(virq) )
+        return vgic_lpi_get_priority(v->domain, virq);

This would benefits some comments to explain why LPI pending handling is a different path.

Quoted 6 lines
xen/arch/arm/vgic.c +2 -0
+
+    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);

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

Quoted 10 lines
xen/arch/arm/vgic.c +7 -0
@@ -446,13 +452,63 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,
     return true;
 }
 
+/*
+ * 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.
+ */

I am afraid this will not prevent a guest to use too much Xen memory. Let's imagine the guest is mapping thousands of LPIs but decides to never handle them or is slow. You would allocate pending_irq for each LPI, and never release the memory.

If we use dynamic allocation, we need a way to limit the number of LPIs received by a guest to avoid memory exhaustion. The only idea I have is an artificial limit, but I don't think it is good. Any other ideas?

Quoted 17 lines
xen/arch/arm/vgic.c +17 -0
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    spin_lock(&v->arch.vgic.pending_lpi_list_lock);
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+        {
+            spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+            return &lpi_irq->pirq;
+        }
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

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

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    if ( !allocate )
+    {
+        spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+        return NULL;
+    }
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);

xzalloc will return NULL if memory is exhausted. There is a general lack of error checking within this series. Any missing error could be a potential target from a guest, leading to security issue. Stefano and I already spot some, it does not mean we found all. So Before sending the next version, please go through the series and verify *every* return.

Also, I can't find the code which release LPIs neither in this patch nor in this series. A general rule is too have allocation and free within the same patch. It is much easier to spot missing free.

Quoted 7 lines
xen/arch/arm/vgic.c +7 -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;
+    }

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

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

Spurious change.

Quoted 19 lines
xen/arch/arm/vgic.c +3 -1
     /* 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 ( is_lpi(irq) )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +536,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 running;
 

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

Quoted 4 lines
xen/arch/arm/vgic.c +1 -0
@@ -488,6 +544,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 did you move this code?

Quoted 9 lines
xen/arch/arm/vgic.c +1 -0
+
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +0 -0
index 00b9c1a..f44a84b 100644

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

Quoted 5 lines
xen/include/asm-arm/domain.h +2 -0
@@ -257,6 +257,8 @@ 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;
+        spinlock_t pending_lpi_list_lock;   /* protects the pending_lpi_list */

It would be better to have this structure per-domain limiting the amount of memory allocating. Also, Stefano was suggesting to use a more efficient data structure, such as an hashtable or a tree.

I would be ok to focus on the correctness so far, but this would need to be address before ITS is marked as stable.

xen/include/asm-arm/domain.h

Quoted 10 lines
xen/include/asm-arm/domain.h +0 -0
 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 bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);

extern void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n); extern void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n);

xen/include/asm-arm/domain.h +5 -0
+/* placeholder function until the property table gets introduced */
+static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
+{
+    return 0xa;
+}

To be fair, you can avoid this function by re-ordering the patches. As suggested earlier, I would introduce some bits of the vITS before. This would also make the series easier to read.

Also, looking how it has been implemented later. I would prefer to see a new callback get_priority in vgic_ops which will return the correct priority.

I agree this would introduce a bit more of duplicated code. But it would limit the use of is_lpi in the common code.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi,

Quoted 58 lines · Julien Grall

Hi Andre

xen/arch/arm/vgic.c line 34

Quoted 10 lines · Andre Przywara
xen/arch/arm/vgic.c +2 -0
index 364d5f0..e5cfa54 100644
@@ -30,6 +30,8 @@
 
 #include <asm/mmio.h>
 #include <asm/gic.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic_v3_its.h>

I really don't want to see gic_v3_* headers included in common code. Why do you need them?

Quoted 17 lines
xen/arch/arm/vgic.c +0 -0
 #include <asm/vgic.h>
 
 static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v, int

rank)

xen/arch/arm/vgic.c +1 -1
@@ -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 +0 -0
@@ -244,10 +246,14 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu
 *v, unsigned int virq)

xen/arch/arm/vgic.c struct vcpu *vgic_get_target_vcpu(struct vcpu · line 255

Quoted 10 lines
xen/arch/arm/vgic.c +3 -1
 
 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 ( is_lpi(virq) )
+        return vgic_lpi_get_priority(v->domain, virq);

This would benefits some comments to explain why LPI pending handling is a different path.

Quoted 10 lines
xen/arch/arm/vgic.c +2 -0
+
+    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);
xen/arch/arm/vgic.c +0 -0
@@ -446,13 +452,63 @@ bool vgic_to_sgi(struct vcpu *v, register_t
 sgir, enum gic_sgi_mode irqmode,
     return true;
 }
 
Quoted 16 lines
Quoted 11 lines
xen/arch/arm/vgic.c +2 -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.
+ */

I am afraid this will not prevent a guest to use too much Xen memory. Let's imagine the guest is mapping thousands of LPIs but decides to never handle them or is slow. You would allocate pending_irq for each LPI, and never release the memory.

So what would be the alternative here? In the current GICv2/3 scheme a pending_irq for each (SPI) interrupt is allocated up front. This approach here tries just to be a bit smarter for LPIs, since the expectation is that we by far don't need so many. In the worst case the memory allocation would converge to the upper limit (number of mapped LPIs), which would probably be used otherwise for the static allocation scheme. Which means we would never be worse.
What actually is missing is the freeing the memory upon domain destruction, which can't be tested for Dom0.

Quoted 3 lines

If we use dynamic allocation, we need a way to limit the number of LPIs received by a guest to avoid memory exhaustion. The only idea I have is an artificial limit, but I don't think it is good. Any other ideas?

I think if we are concerned about this, we shouldn't allow *DomUs* to allocate too many LPIs, which are bounded by the DeviceID/EventID combinations. This will be under full control by Dom0, which will communicate the number of MSIs (events) for each passthrough-ed device, so we can easily impose a policy limit upon creation.

And just to be sure: we are at most allocating one structure per event. An MSI interrupt storm would still converge into one pending LPI (struct), so wouldn't trigger any further allocations. Remapping LPIs won't increase that as well, since we only can assign one LPI to a DevID/EvID pair at any given point in time. Remapping requires the pending bit to be cleared. And the structures are not indexed by the LPI number, but just take a free slot.

For Dom0 I don't see any good ways of limiting the number of LPIs, but we shouldn't need any.

Quoted 34 lines
Quoted 17 lines
xen/arch/arm/vgic.c +17 -0
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    spin_lock(&v->arch.vgic.pending_lpi_list_lock);
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+        {
+            spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+            return &lpi_irq->pirq;
+        }
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

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

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    if ( !allocate )
+    {
+        spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+        return NULL;
+    }
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);

xzalloc will return NULL if memory is exhausted. There is a general lack of error checking within this series. Any missing error could be a potential target from a guest, leading to security issue. Stefano and I already spot some, it does not mean we found all. So Before sending the next version, please go through the series and verify *every* return.

Quoted 3 lines

Also, I can't find the code which release LPIs neither in this patch nor in this series. A general rule is too have allocation and free within the same patch. It is much easier to spot missing free.

There is no such code, on purpose. We only grow the number, but never shrink it (to what?, where to stop?, what if we need more again?). As said above, in the worst case this ends up at something where a static allocation would have started with from the beginning.

And as mentioned, I only skipped the "free the whole list upon domain destruction" part.

Cheers,
Andre.

Quoted 88 lines
Quoted 7 lines
xen/arch/arm/vgic.c +7 -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;
+    }

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

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

Spurious change.

Quoted 19 lines
     /* 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];
xen/arch/arm/vgic.c +3 -1
+    else if ( is_lpi(irq) )
+        n = lpi_to_pending(v, irq, true);
     else
         n = &v->domain->arch.vgic.pending_irqs[irq - 32];
     return n;
@@ -480,7 +536,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 running;
 

xen/arch/arm/vgic.c void vgic_vcpu_inject_irq(struct vcpu *v, unsigned · line 548

Quoted 5 lines
xen/arch/arm/vgic.c +1 -0
@@ -488,6 +544,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 did you move this code?

Quoted 14 lines
xen/arch/arm/vgic.c +1 -0
+
     /* vcpu offline */
     if ( test_bit(_VPF_down, &v->pause_flags) )
     {
xen/include/asm-arm/domain.h +2 -0
index 00b9c1a..f44a84b 100644
@@ -257,6 +257,8 @@ 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;
+        spinlock_t pending_lpi_list_lock;   /* protects the
Quoted 1 line

pending_lpi_list */

It would be better to have this structure per-domain limiting the amount of memory allocating. Also, Stefano was suggesting to use a more efficient data structure, such as an hashtable or a tree.

I would be ok to focus on the correctness so far, but this would need to be address before ITS is marked as stable.

xen/include/asm-arm/domain.h

Quoted 12 lines

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 bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr); extern void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n); extern void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n);

xen/include/asm-arm/domain.h +5 -0
+/* placeholder function until the property table gets introduced */
+static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
+{
+    return 0xa;
+}

To be fair, you can avoid this function by re-ordering the patches. As suggested earlier, I would introduce some bits of the vITS before. This would also make the series easier to read.

Quoted 9 lines

Also, looking how it has been implemented later. I would prefer to see a new callback get_priority in vgic_ops which will return the correct priority.

I agree this would introduce a bit more of duplicated code. But it would limit the use of is_lpi in the common code.

Cheers,

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

#64 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi Andre,

Quoted 18 lines · Andre Przywara
Quoted 12 lines · Julien Grall
Quoted 11 lines

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

Quoted 4 lines

I am afraid this will not prevent a guest to use too much Xen memory. Let's imagine the guest is mapping thousands of LPIs but decides to never handle them or is slow. You would allocate pending_irq for each LPI, and never release the memory.

Quoted 7 lines

So what would be the alternative here? In the current GICv2/3 scheme a pending_irq for each (SPI) interrupt is allocated up front. This approach here tries just to be a bit smarter for LPIs, since the expectation is that we by far don't need so many. In the worst case the memory allocation would converge to the upper limit (number of mapped LPIs), which would probably be used otherwise for the static allocation scheme. Which means we would never be worse.

It is getting worse in the current version because you have the list per vCPU. So you may get nLPIs * nvCPUs pending_irq allocated.

Quoted 2 lines

What actually is missing is the freeing the memory upon domain destruction, which can't be tested for Dom0.

As I already said multiple time, forgetting to add the domain destruction is much worse because this is a call to miss something when we will need it. I prefer to see the code now so we can review.

Quoted 10 lines
Quoted 4 lines

If we use dynamic allocation, we need a way to limit the number of LPIs received by a guest to avoid memory exhaustion. The only idea I have is an artificial limit, but I don't think it is good. Any other ideas?

I think if we are concerned about this, we shouldn't allow *DomUs* to allocate too many LPIs, which are bounded by the DeviceID/EventID combinations. This will be under full control by Dom0, which will communicate the number of MSIs (events) for each passthrough-ed device, so we can easily impose a policy limit upon creation.

If DOM0 is able to control the number of DeviceID/EventID, then why do we need to allocate on the fly?

Also, I don't think the spec prevents to populate Device Table for a deviceID that has no Device associated. It could be used by a domain to inject fake LPI. For instance this could replace polling mode for the command queue.

My main concern is memory allocation can fail. Then what will you do? You will penalize a well-behave domain that because someone else was nasty.

And you have no way to report to the guest: "I was not able to allocate memory, please try later" because this is an interrupt.

Quoted 38 lines
Quoted 34 lines
Quoted 17 lines
xen/arch/arm/vgic.c +17 -0
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    spin_lock(&v->arch.vgic.pending_lpi_list_lock);
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+        {
+            spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+            return &lpi_irq->pirq;
+        }
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

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

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    if ( !allocate )
+    {
+        spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+        return NULL;
+    }
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);

xzalloc will return NULL if memory is exhausted. There is a general lack of error checking within this series. Any missing error could be a potential target from a guest, leading to security issue. Stefano and I already spot some, it does not mean we found all. So Before sending the next version, please go through the series and verify *every* return.

Quoted 3 lines

Also, I can't find the code which release LPIs neither in this patch nor in this series. A general rule is too have allocation and free within the same patch. It is much easier to spot missing free.

Quoted 7 lines

There is no such code, on purpose. We only grow the number, but never shrink it (to what?, where to stop?, what if we need more again?). As said above, in the worst case this ends up at something where a static allocation would have started with from the beginning.

And as mentioned, I only skipped the "free the whole list upon domain destruction" part.

A general rule is to explain in the commit message what is done. So you avoid argument on the ML.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Andre Przywara
Quoted 34 lines · Andre Przywara
Quoted 34 lines
Quoted 17 lines
xen/arch/arm/vgic.c +17 -0
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    spin_lock(&v->arch.vgic.pending_lpi_list_lock);
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+        {
+            spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+            return &lpi_irq->pirq;
+        }
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

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

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    if ( !allocate )
+    {
+        spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+        return NULL;
+    }
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);

xzalloc will return NULL if memory is exhausted. There is a general lack of error checking within this series. Any missing error could be a potential target from a guest, leading to security issue. Stefano and I already spot some, it does not mean we found all. So Before sending the next version, please go through the series and verify *every* return.

Quoted 8 lines
Quoted 3 lines

Also, I can't find the code which release LPIs neither in this patch nor in this series. A general rule is too have allocation and free within the same patch. It is much easier to spot missing free.

There is no such code, on purpose. We only grow the number, but never shrink it (to what?, where to stop?, what if we need more again?). As said above, in the worst case this ends up at something where a static allocation would have started with from the beginning.

Dellocate struct pending_irq when the domain is destroyed or when an LPI is disabled?

Quoted 1 line

to what?, where to stop?

We don't stop, why stop? Let's go back to 0 if we can.

Quoted 1 line

what if we need more again?

We allocate them again?

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

#66 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi,

Quoted 39 lines · Stefano Stabellini
Quoted 38 lines · Andre Przywara
Quoted 37 lines
Quoted 17 lines
xen/arch/arm/vgic.c +17 -0
+struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
+                                   bool allocate)
+{
+    struct lpi_pending_irq *lpi_irq, *empty = NULL;
+
+    spin_lock(&v->arch.vgic.pending_lpi_list_lock);
+    list_for_each_entry(lpi_irq, &v->arch.vgic.pending_lpi_list, entry)
+    {
+        if ( lpi_irq->pirq.irq == lpi )
+        {
+            spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+            return &lpi_irq->pirq;
+        }
+
+        if ( lpi_irq->pirq.irq == 0 && !empty )
+            empty = lpi_irq;
+    }

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

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+    if ( !allocate )
+    {
+        spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
+        return NULL;
+    }
+
+    if ( !empty )
+    {
+        empty = xzalloc(struct lpi_pending_irq);

xzalloc will return NULL if memory is exhausted. There is a general lack of error checking within this series. Any missing error could be a potential target from a guest, leading to security issue. Stefano and I already spot some, it does not mean we found all. So Before sending the next version, please go through the series and verify *every* return.

Also, I can't find the code which release LPIs neither in this patch nor in this series. A general rule is too have allocation and free within the same patch. It is much easier to spot missing free.

Quoted 6 lines
Quoted 4 lines

There is no such code, on purpose. We only grow the number, but never shrink it (to what?, where to stop?, what if we need more again?). As said above, in the worst case this ends up at something where a static allocation would have started with from the beginning.

Dellocate struct pending_irq when the domain is destroyed

Sure, I think this is what is missing at the moment.

Quoted 1 line

or when an LPI is disabled?

A certain struct pending_irq is not assigned to a particular LPI, it's more a member of a pool to allocate from when in need.
I consider them like shadow list registers, which can in addition be used as spill-overs.
Very much like an LR the pending_irq is only assigned to a certain LPI for the lifetime of a *pending* LPI on a particular VCPU. As mentioned earlier, under normal conditions this is very short for always edge triggered LPIs which don't have an active state.

Quoted 7 lines
Quoted 1 line

to what?, where to stop?

We don't stop, why stop? Let's go back to 0 if we can.

Quoted 1 line

what if we need more again?

We allocate them again?

I am afraid that this would lead to situations where we needlessly allocate and deallocate pending_irqs. Under normal load I'd expect to have something like zero to three LPIs pending at any given point in time (mostly zero, to be honest).
So this will lead to a situation where *every* LPI that becomes pending triggers a memory allocation - in the hot path. That's why the pool idea. So if we are going to shrink the pool, I'd stop at something like five entries, to not penalize the common case.
Does that sound useful?

Cheers,
Andre.

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

#67 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi,

Quoted 11 lines · Andre Przywara
Quoted 1 line · Stefano Stabellini

On Fri, 24 Mar 2017, Andre Przywara wrote:

I am afraid that this would lead to situations where we needlessly allocate and deallocate pending_irqs. Under normal load I'd expect to have something like zero to three LPIs pending at any given point in time (mostly zero, to be honest).
So this will lead to a situation where *every* LPI that becomes pending triggers a memory allocation - in the hot path. That's why the pool idea. So if we are going to shrink the pool, I'd stop at something like five entries, to not penalize the common case.
Does that sound useful?

Not answering directly to the question here. I will summarize the face to face discussion I had with Andre this morning.

So allocating the pending_irq in the IRQ path is not a solution because memory allocation should not happen in IRQ context, see ASSERT(!in_irq()) in _xmalloc.

Regardless the ASSERT, it will also increase the time to handle and forward an interrupt when there are no pending_irq free because it is necessary to allocate a new one. Lastly, we have no way to tell the guest: "Try again" if it Xen is running out of memory.

The outcome of the discussion is to pre-allocate the pending_irq when a device is assigned to a domain. We know the maximum number of event supported by a device and that 1 event = 1 LPI.

This may allocate more memory (a pending_irq is 56 bytes), but at least we don't need allocation on the fly and can report error.

One could argue that we could allocate on MAPTI to limit the allocation. However, as we are not able to rate-limit/defer the execution of the command queue so far, a guest could potentially flood with MAPTI and monopolize the pCPU for a long time.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 31 lines · Julien Grall

Hi,

Quoted 11 lines · Andre Przywara
Quoted 1 line · Stefano Stabellini

On Fri, 24 Mar 2017, Andre Przywara wrote:

I am afraid that this would lead to situations where we needlessly allocate and deallocate pending_irqs. Under normal load I'd expect to have something like zero to three LPIs pending at any given point in time (mostly zero, to be honest).
So this will lead to a situation where *every* LPI that becomes pending triggers a memory allocation - in the hot path. That's why the pool idea. So if we are going to shrink the pool, I'd stop at something like five entries, to not penalize the common case.
Does that sound useful?

Not answering directly to the question here. I will summarize the face to face discussion I had with Andre this morning.

So allocating the pending_irq in the IRQ path is not a solution because memory allocation should not happen in IRQ context, see ASSERT(!in_irq()) in _xmalloc.

Regardless the ASSERT, it will also increase the time to handle and forward an interrupt when there are no pending_irq free because it is necessary to allocate a new one. Lastly, we have no way to tell the guest: "Try again" if it Xen is running out of memory.

The outcome of the discussion is to pre-allocate the pending_irq when a device is assigned to a domain. We know the maximum number of event supported by a device and that 1 event = 1 LPI.

Quoted 7 lines

This may allocate more memory (a pending_irq is 56 bytes), but at least we don't need allocation on the fly and can report error.

One could argue that we could allocate on MAPTI to limit the allocation. However, as we are not able to rate-limit/defer the execution of the command queue so far, a guest could potentially flood with MAPTI and monopolize the pCPU for a long time.

It makes a lot of sense to keep the allocation out of the irq path. However, I am wondering if the allocation/deallocation of pending_irq structs could be done at the point the vLPIs are enabled/disabled, instead of device assignment time.

In any case, there should be code for the deallocation. If we keep the allocation at device assignment time, there should be code for the deallocation when a device is remove from the guest (even if we cannot test that case well now).

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

#69 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi Stefano,

Quoted 40 lines · Stefano Stabellini
Quoted 31 lines · Julien Grall

Hi,

Quoted 11 lines · Andre Przywara
Quoted 1 line · Stefano Stabellini

On Fri, 24 Mar 2017, Andre Przywara wrote:

I am afraid that this would lead to situations where we needlessly allocate and deallocate pending_irqs. Under normal load I'd expect to have something like zero to three LPIs pending at any given point in time (mostly zero, to be honest).
So this will lead to a situation where *every* LPI that becomes pending triggers a memory allocation - in the hot path. That's why the pool idea. So if we are going to shrink the pool, I'd stop at something like five entries, to not penalize the common case.
Does that sound useful?

Not answering directly to the question here. I will summarize the face to face discussion I had with Andre this morning.

So allocating the pending_irq in the IRQ path is not a solution because memory allocation should not happen in IRQ context, see ASSERT(!in_irq()) in _xmalloc.

Regardless the ASSERT, it will also increase the time to handle and forward an interrupt when there are no pending_irq free because it is necessary to allocate a new one. Lastly, we have no way to tell the guest: "Try again" if it Xen is running out of memory.

The outcome of the discussion is to pre-allocate the pending_irq when a device is assigned to a domain. We know the maximum number of event supported by a device and that 1 event = 1 LPI.

Quoted 7 lines

This may allocate more memory (a pending_irq is 56 bytes), but at least we don't need allocation on the fly and can report error.

One could argue that we could allocate on MAPTI to limit the allocation. However, as we are not able to rate-limit/defer the execution of the command queue so far, a guest could potentially flood with MAPTI and monopolize the pCPU for a long time.

Quoted 4 lines

It makes a lot of sense to keep the allocation out of the irq path. However, I am wondering if the allocation/deallocation of pending_irq structs could be done at the point the vLPIs are enabled/disabled, instead of device assignment time.

I am not sure what you mean by enabling/disabling vLPIS. Do you mean when the guest is enabling/disabling them or the guest will assign a vLPI to a (deviceID, event) via MAPTI.

For both guest could potentially flood us. It would take us a lot of time to allocate/free memory for each vLPIs modified. Hence, why I didn't suggest it and said: "One could argue that we could allocate on MAPTI to limit the allocation...".

Quoted 5 lines

In any case, there should be code for the deallocation. If we keep the allocation at device assignment time, there should be code for the deallocation when a device is remove from the guest (even if we cannot test that case well now).

It was implied in my answered :).

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

CC'ing Andrew, Jan and George to get more feedback on the security impact of this patch.

I'll make a quick summary for you: we need to allocate a 56 bytes struct (called pending_irq) for each potential interrupt injected to guests (dom0 and domUs). With the new ARM interrupt controller there could be thousands.

We could do the allocation upfront, which requires more memory, or we could do the allocation dynamically at run time when each interrupt is enabled, and deallocate the struct when an interrupt is disabled.

However, there is a concern that doing xmalloc/xfree in response to an unprivileged DomU request could end up becoming a potential vector of denial of service attacks. The guest could enable a thousand interrupts, then disable a thousand interrupts and so on, monopolizing the usage of one physical cpu. It only takes the write of 1 bit in memory for a guest to enable/disable an interrupt.

See below.

Quoted 51 lines · Julien Grall

Hi Stefano,

Quoted 48 lines · Stefano Stabellini
Quoted 46 lines · Julien Grall

Hi,

Quoted 11 lines · Andre Przywara
Quoted 1 line · Stefano Stabellini

On Fri, 24 Mar 2017, Andre Przywara wrote:

I am afraid that this would lead to situations where we needlessly allocate and deallocate pending_irqs. Under normal load I'd expect to have something like zero to three LPIs pending at any given point in time (mostly zero, to be honest).
So this will lead to a situation where *every* LPI that becomes pending triggers a memory allocation - in the hot path. That's why the pool idea. So if we are going to shrink the pool, I'd stop at something like five entries, to not penalize the common case.
Does that sound useful?

Not answering directly to the question here. I will summarize the face to face
discussion I had with Andre this morning.

So allocating the pending_irq in the IRQ path is not a solution because memory
allocation should not happen in IRQ context, see ASSERT(!in_irq()) in _xmalloc.

Regardless the ASSERT, it will also increase the time to handle and forward an
interrupt when there are no pending_irq free because it is necessary to allocate a new one. Lastly, we have no way to tell the guest: "Try again" if
it Xen is running out of memory.

The outcome of the discussion is to pre-allocate the pending_irq when a device
is assigned to a domain. We know the maximum number of event supported by a
device and that 1 event = 1 LPI.

This may allocate more memory (a pending_irq is 56 bytes), but at least we don't need allocation on the fly and can report error.

One could argue that we could allocate on MAPTI to limit the allocation. However, as we are not able to rate-limit/defer the execution of the command
queue so far, a guest could potentially flood with MAPTI and monopolize the
pCPU for a long time.

Quoted 8 lines
Quoted 4 lines

It makes a lot of sense to keep the allocation out of the irq path. However, I am wondering if the allocation/deallocation of pending_irq structs could be done at the point the vLPIs are enabled/disabled, instead of device assignment time.

I am not sure what you mean by enabling/disabling vLPIS. Do you mean when the guest is enabling/disabling them or the guest will assign a vLPI to a (deviceID, event) via MAPTI.

I was thinking enable/disable on the LPI Configuration table. However, it would have the same issues you wrote for MAPTI.

Quoted 4 lines

For both guest could potentially flood us. It would take us a lot of time to allocate/free memory for each vLPIs modified. Hence, why I didn't suggest it and said: "One could argue that we could allocate on MAPTI to limit the allocation...".

Given that Xen wouldn't allocate the same pending_irq twice, at most Xen would allocate the same amount of memory and the same number of pending_irq that it would otherwise allocate if we did it at assignment time, right?

Therefore, we are not concerned about memory utilization. We are concerned about the CPU time to do the allocation itself, right?

However, the CPU time to run xmalloc/xfree should be small and in any case the scheduler has always the chance to deschedule the vcpu if it wants to. This is no different than issuing any of the hypercalls that require some work on the hypervisor side.

I don't think we have reasons to be concerned. Any opinions?

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

#71 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi Stefano,

Quoted 14 lines · Stefano Stabellini
Quoted 4 lines · Julien Grall

For both guest could potentially flood us. It would take us a lot of time to allocate/free memory for each vLPIs modified. Hence, why I didn't suggest it and said: "One could argue that we could allocate on MAPTI to limit the allocation...".

Given that Xen wouldn't allocate the same pending_irq twice, at most Xen would allocate the same amount of memory and the same number of pending_irq that it would otherwise allocate if we did it at assignment time, right?

Therefore, we are not concerned about memory utilization. We are concerned about the CPU time to do the allocation itself, right?

Quoted 6 lines

However, the CPU time to run xmalloc/xfree should be small and in any case the scheduler has always the chance to deschedule the vcpu if it wants to. This is no different than issuing any of the hypercalls that require some work on the hypervisor side.

I don't think we have reasons to be concerned. Any opinions?

Well, I have already said on the IRQ version but forgot to mention here. How do you report error if xmalloc has failed? This could happen if memory resource has been exhausted even for a well-behaved guest.

If you do the allocation on enable, this means on a memory trap, you would have to inject a data abort to the guest. If you do on the MAPTI command, it would be a SError with a IMPLEMENTATION DEFINED error code. In both case the guest will likely not able interpret it and crash. This could be a vector attack by exhausting the resource.

Furthermore, in the case of enable/disable vLPI solution, you can disable an LPI that are still inflight. So you would not be able to free here. Furthermore enable/disable can happen often if you want to mask the interrupt temporarily (all MSI are edge-interrupt).

Lastly, in the case of MAPTI it is not possible to preempt the command queue that can contain ~32K of commands. So we may have to do 32K of xmalloc/xfree. Even one MAPTI is fast, likely 32K will be slow and monopolize a pCPU for more than expected.

But can we focus on getting a sensible design which can be easily extended and without any flaw? This already is quite a difficult tasks with the ITS. Asking also for performance from the first draft is making much worse.

If you recall, a lot of ARM subsystems (P2M, vGIC...) has been built in multiple stage. Each stage improved the performance. It sounds quite unfair to me to require the same level of performance as the current vGIC just because the code was merged later one.

This is raising the bar to contribute on Xen on ARM very high and may discourage someone to push something upstream.

To be clear, I do care about performance. But I think this has to come in a second step when it is too complicate to address as a first step. The first step is to be able to use Xen on the hardware without any flaw and allowing us to extend the code easily.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

In reply to Stefano Stabellini
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 27.03.17 at 20:39, <[email protected]> wrote:

CC'ing Andrew, Jan and George to get more feedback on the security impact of this patch.

I'll make a quick summary for you: we need to allocate a 56 bytes struct (called pending_irq) for each potential interrupt injected to guests (dom0 and domUs). With the new ARM interrupt controller there could be thousands.

We could do the allocation upfront, which requires more memory, or we could do the allocation dynamically at run time when each interrupt is enabled, and deallocate the struct when an interrupt is disabled.

Quoted 6 lines

However, there is a concern that doing xmalloc/xfree in response to an unprivileged DomU request could end up becoming a potential vector of denial of service attacks. The guest could enable a thousand interrupts, then disable a thousand interrupts and so on, monopolizing the usage of one physical cpu. It only takes the write of 1 bit in memory for a guest to enable/disable an interrupt.

Well, I think doing the allocations at device assign time would be the least problematic approach: The tool stack could account for the needed memory (ballooning Dom0 if necessary). (We still have the open work item of introducing accounting of guest- associated, but guest-inaccessible memory in the hypervisor.)

What I don't really understand the background of is the pCPU monopolization concern. Is there anything here that's long-running inside the hypervisor? Otherwise, the scheduler should be taking care of everything.

Jan

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

#73 lore raw

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Hi Jan,

Quoted 26 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 27.03.17 at 20:39, <[email protected]> wrote:

CC'ing Andrew, Jan and George to get more feedback on the security impact of this patch.

I'll make a quick summary for you: we need to allocate a 56 bytes struct (called pending_irq) for each potential interrupt injected to guests (dom0 and domUs). With the new ARM interrupt controller there could be thousands.

We could do the allocation upfront, which requires more memory, or we could do the allocation dynamically at run time when each interrupt is enabled, and deallocate the struct when an interrupt is disabled.

Quoted 6 lines

However, there is a concern that doing xmalloc/xfree in response to an unprivileged DomU request could end up becoming a potential vector of denial of service attacks. The guest could enable a thousand interrupts, then disable a thousand interrupts and so on, monopolizing the usage of one physical cpu. It only takes the write of 1 bit in memory for a guest to enable/disable an interrupt.

Well, I think doing the allocations at device assign time would be the least problematic approach: The tool stack could account for the needed memory (ballooning Dom0 if necessary). (We still have the open work item of introducing accounting of guest- associated, but guest-inaccessible memory in the hypervisor.)

Quoted 4 lines

What I don't really understand the background of is the pCPU monopolization concern. Is there anything here that's long-running inside the hypervisor? Otherwise, the scheduler should be taking care of everything.

Let me give you some background before answering to the question. The ITS is an interrupt controller widget which provides a sophisticated way
of dealing with MSIs in a scalable manner. A command queue is used to manage the ITS. It is a memory region provided by the guest can be up to 1MB. Each command is composed of 4 double-word (e.g 32 bytes) which make the possibly for a guest to queue up 32768 commands.

In order to control the command queue there are 2 registers:
- GITS_CWRITER that points to the end of the queue and updated by the
software when a new command is written
- GITS_CREADR that points to the beginning of the queue and updated by
the forware when a new command has been executed.

The ITS will process command until the queue is empty (u.e GITS_CWRITER == GITS_CREADR). A software has 2 solutions to wait the completion of the command:
- Polling on GITS_CREADR
- Adding a command INT which will send a interrupt when executed

Usually the polling mode also includes a timeout in the code.

A guest could potentially queue up to 32768 commands before updating GITS_CWRITER. In the current approach, the command queue will be handled synchronously when the software wrote into GITS_CWRITER and trapped by the hypervisor. This means that we will not return from the hypervisor until the ITS executed the command between GITS_CREADER and GITS_CWRITER.

All the command have to be executed quickly to avoid the guest monopolizing the pCPU by flooding the command queue.

We thought using different alternative such as tasklet or breaking down the command queue in batch. But none of them fit well.

If you have a better idea, I am happy to consider it.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 09/27] ARM: GICv3: introduce separate pending_irq structs for LPIs

Quoted 62 lines
Quoted 1 line
Quoted 1 line · On 28.03.17 at 15:12, <[email protected]> wrote:

Hi Jan,

Quoted 26 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 27.03.17 at 20:39, <[email protected]> wrote:

CC'ing Andrew, Jan and George to get more feedback on the security impact of this patch.

I'll make a quick summary for you: we need to allocate a 56 bytes struct (called pending_irq) for each potential interrupt injected to guests (dom0 and domUs). With the new ARM interrupt controller there could be thousands.

We could do the allocation upfront, which requires more memory, or we could do the allocation dynamically at run time when each interrupt is enabled, and deallocate the struct when an interrupt is disabled.

Quoted 6 lines

However, there is a concern that doing xmalloc/xfree in response to an unprivileged DomU request could end up becoming a potential vector of denial of service attacks. The guest could enable a thousand interrupts, then disable a thousand interrupts and so on, monopolizing the usage of one physical cpu. It only takes the write of 1 bit in memory for a guest to enable/disable an interrupt.

Well, I think doing the allocations at device assign time would be the least problematic approach: The tool stack could account for the needed memory (ballooning Dom0 if necessary). (We still have the open work item of introducing accounting of guest- associated, but guest-inaccessible memory in the hypervisor.)

Quoted 4 lines

What I don't really understand the background of is the pCPU monopolization concern. Is there anything here that's long-running inside the hypervisor? Otherwise, the scheduler should be taking care of everything.

Let me give you some background before answering to the question. The ITS is an interrupt controller widget which provides a sophisticated way of dealing with MSIs in a scalable manner. A command queue is used to manage the ITS. It is a memory region provided by the guest can be up to 1MB. Each command is composed of 4 double-word (e.g 32 bytes) which make the possibly for a guest to queue up 32768 commands.

In order to control the command queue there are 2 registers:
- GITS_CWRITER that points to the end of the queue and updated by the
software when a new command is written
- GITS_CREADR that points to the beginning of the queue and updated by
the forware when a new command has been executed.

The ITS will process command until the queue is empty (u.e GITS_CWRITER == GITS_CREADR). A software has 2 solutions to wait the completion of the command:
- Polling on GITS_CREADR
- Adding a command INT which will send a interrupt when executed

Usually the polling mode also includes a timeout in the code.

A guest could potentially queue up to 32768 commands before updating GITS_CWRITER. In the current approach, the command queue will be handled synchronously when the software wrote into GITS_CWRITER and trapped by the hypervisor. This means that we will not return from the hypervisor until the ITS executed the command between GITS_CREADER and GITS_CWRITER.

Quoted 5 lines

All the command have to be executed quickly to avoid the guest monopolizing the pCPU by flooding the command queue.

We thought using different alternative such as tasklet or breaking down the command queue in batch. But none of them fit well.

I guess you want something continuation-like then? Does the instruction doing the GITS_CWRITER write have any other side effects? If not, wouldn't the approach used on x86 for forwarding requests to qemu work here too? I.e. retry the instruction as long as you're not ready to actually complete it, with a continuation check put in the handling code you describe every so many iterations. Of course there are dependencies here on the cross-CPU visibility of the register - if all guest vCPU-s can access it, things would require more care (as intermediate reads as well as successive writes from multiple parties would need taking into consideration).

Jan

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

#75 lore raw

PATCH v2 10/27 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-v3-lpi.c | 41 +++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic.c        |  6 ++++--
 xen/include/asm-arm/irq.h |  8 ++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-lpi.c +41 -0
index 59d3ba4..0579976 100644
@@ -104,6 +104,47 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta)
         return per_cpu(lpi_redist, cpu).redist_id << 16;
 }
 
+/*
+ * 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;
+    struct vcpu *vcpu;
+
+    WRITE_SYSREG32(lpi, ICC_EOIR1_EL1);
+
+    hlpip = gic_get_host_lpi(lpi);
+    if ( !hlpip )
+        return;
+
+    hlpi.data = read_u64_atomic(&hlpip->data);
+
+    /* We may have mapped more host LPIs than the guest actually asked for. */
+    if ( !hlpi.virt_lpi )
+        return;
+
+    d = get_domain_by_id(hlpi.dom_id);
+    if ( !d )
+        return;
+
+    if ( hlpi.vcpu_id >= d->max_vcpus )
+    {
+        put_domain(d);
+        return;
+    }
+
+    vcpu = d->vcpu[hlpi.vcpu_id];
+
+    put_domain(d);
+
+    vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/gic.c +4 -2
index bd3c032..7286e5d 100644
@@ -700,8 +700,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 ( is_lpi(irq) )
+        {
+            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
#76 lore raw

Re: [PATCH v2 10/27] ARM: GICv3: forward pending LPIs to guests

Hi Andre,

Quoted 32 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-v3-lpi.c | 41 +++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic.c        |  6 ++++--
 xen/include/asm-arm/irq.h |  8 ++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-lpi.c +11 -0
index 59d3ba4..0579976 100644
@@ -104,6 +104,47 @@ uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta)
         return per_cpu(lpi_redist, cpu).redist_id << 16;
 }
 
+/*
+ * 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;
+    struct vcpu *vcpu;

xen/arch/arm/gic-v3-lpi.c uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta) · line 127

Quoted 10 lines
xen/arch/arm/gic-v3-lpi.c +10 -0
+
+    WRITE_SYSREG32(lpi, ICC_EOIR1_EL1);
+
+    hlpip = gic_get_host_lpi(lpi);
+    if ( !hlpip )
+        return;
+
+    hlpi.data = read_u64_atomic(&hlpip->data);
+
+    /* We may have mapped more host LPIs than the guest actually asked for. */

Another way, is the interrupt has been received at the same time the guest is configuring it. What will happen if the interrupt is lost?

xen/arch/arm/gic-v3-lpi.c uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta) · line 131

Quoted 4 lines
xen/arch/arm/gic-v3-lpi.c +4 -0
+    if ( !hlpi.virt_lpi )
+        return;
+
+    d = get_domain_by_id(hlpi.dom_id);

It would be enough to use rcu_lock_domain_by_id here.

xen/arch/arm/gic-v3-lpi.c uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta) · line 135

Quoted 4 lines
xen/arch/arm/gic-v3-lpi.c +4 -0
+    if ( !d )
+        return;
+
+    if ( hlpi.vcpu_id >= d->max_vcpus )

A comment would be certainly useful here to explain why this check.

Quoted 20 lines
xen/arch/arm/gic-v3-lpi.c +12 -0
+    {
+        put_domain(d);
+        return;
+    }
+
+    vcpu = d->vcpu[hlpi.vcpu_id];
+
+    put_domain(d);
+
+    vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/gic.c +0 -0
index bd3c032..7286e5d 100644

xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 705

Quoted 8 lines
xen/arch/arm/gic.c +3 -2
@@ -700,8 +700,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 ( is_lpi(irq) )
+        {
+            do_LPI(irq);

I really don't want to see GICv3 specific code called in common code. Please introduce a specific callback in gic_hw_operations.

xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 706

Quoted 1 line
xen/arch/arm/gic.c +1 -0
+        } else if ( unlikely(irq < 16) )

Coding style:

}
else if (...)
{
}
else if (...)

Quoted 9 lines
xen/arch/arm/gic.c +0 -0
         {
             do_sgi(regs, irq);
         }
xen/include/asm-arm/irq.h +0 -0
index 8f7a167..ee47de8 100644
@@ -34,6 +34,14 @@ struct irq_desc *__irq_to_desc(int irq);
 

xen/include/asm-arm/irq.h struct irq_desc *__irq_to_desc(int irq); · line 44

Quoted 10 lines
xen/include/asm-arm/irq.h +8 -0
 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
+

This would avoid such ugly hack where do_LPI is define in gic-v3-its.c but declared in irq.h.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 10/27] ARM: GICv3: forward pending LPIs to guests

Hi,

Quoted 45 lines · Julien Grall

Hi Andre,

Quoted 42 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-v3-lpi.c | 41 +++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic.c        |  6 ++++--
 xen/include/asm-arm/irq.h |  8 ++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)
xen/arch/arm/gic-v3-lpi.c +2 -0
index 59d3ba4..0579976 100644
@@ -104,6 +104,47 @@ uint64_t gicv3_get_redist_address(unsigned int
 cpu, bool use_pta)
         return per_cpu(lpi_redist, cpu).redist_id << 16;
 }
 
+/*
+ * 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.
+ */

xen/arch/arm/gic-v3-lpi.c +11 -0
+void do_LPI(unsigned int lpi)
+{
+    struct domain *d;
+    union host_lpi *hlpip, hlpi;
+    struct vcpu *vcpu;
+
+    WRITE_SYSREG32(lpi, ICC_EOIR1_EL1);
+
+    hlpip = gic_get_host_lpi(lpi);
+    if ( !hlpip )
+        return;
Quoted 8 lines
Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c +4 -0
+
+    hlpi.data = read_u64_atomic(&hlpip->data);
+
+    /* We may have mapped more host LPIs than the guest actually

asked for. */

Another way, is the interrupt has been received at the same time the guest is configuring it. What will happen if the interrupt is lost?

I don't see how this would be our problem. If the guest is still about to configure an LPI, then any incoming one is pretty clearly a spurious interrupt.
I take it that you mean "mapping an LPI using ITS commands" when you say "configuring" here. If this is not what you mean, please correct me.

I'd expect a guest to first map the LPI, then enable it and send an INV command. Doing that differently will not result in any sane behavior (as
in: a guest cannot expect an LPI to come through), so nothing we need to worry about (from a lost IRQ perspective).

Or what do I miss here?

Quoted 7 lines

xen/arch/arm/gic-v3-lpi.c uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta) · line 131

Quoted 5 lines
xen/arch/arm/gic-v3-lpi.c +4 -0
+    if ( !hlpi.virt_lpi )
+        return;
+
+    d = get_domain_by_id(hlpi.dom_id);

It would be enough to use rcu_lock_domain_by_id here.

Done in v3.

Quoted 6 lines

xen/arch/arm/gic-v3-lpi.c uint64_t gicv3_get_redist_address(unsigned int cpu, bool use_pta) · line 135

Quoted 4 lines
xen/arch/arm/gic-v3-lpi.c +4 -0
+    if ( !d )
+        return;
+
+    if ( hlpi.vcpu_id >= d->max_vcpus )

A comment would be certainly useful here to explain why this check.

Done in v3, though personally I don't see what is unclear about an:

	if ( id >= max )
		return;
Quoted 24 lines
Quoted 24 lines
xen/arch/arm/gic-v3-lpi.c +12 -0
+    {
+        put_domain(d);
+        return;
+    }
+
+    vcpu = d->vcpu[hlpi.vcpu_id];
+
+    put_domain(d);
+
+    vgic_vcpu_inject_irq(vcpu, hlpi.virt_lpi);
+}
+
 static int gicv3_lpi_allocate_pendtable(uint64_t *reg)
 {
     uint64_t val;
xen/arch/arm/gic.c +0 -0
index bd3c032..7286e5d 100644
@@ -700,8 +700,10 @@ void gic_interrupt(struct cpu_user_regs *regs,
 int is_fiq)
             local_irq_enable();
             do_IRQ(regs, irq, is_fiq);
             local_irq_disable();
Quoted 8 lines

xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 705

Quoted 5 lines
xen/arch/arm/gic.c +3 -2
-        }
-        else if (unlikely(irq < 16))
+        } else if ( is_lpi(irq) )
+        {
+            do_LPI(irq);

I really don't want to see GICv3 specific code called in common code. Please introduce a specific callback in gic_hw_operations.

OK, you convinced me by persistence ;-)
I still don't fully believe it, but well ...

Quoted 10 lines

xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 706

Quoted 2 lines
xen/arch/arm/gic.c +1 -0
+        } else if ( unlikely(irq < 16) )

Coding style:

}
else if (...)
{
}
else if (...)

Fixed in v3.

Quoted 12 lines
Quoted 12 lines
xen/arch/arm/gic.c +0 -0
         {
             do_sgi(regs, irq);
         }
xen/include/asm-arm/irq.h +0 -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);
 
Quoted 11 lines

xen/include/asm-arm/irq.h struct irq_desc *__irq_to_desc(int irq); · line 44

Quoted 8 lines
xen/include/asm-arm/irq.h +8 -0
+#ifdef CONFIG_HAS_ITS
+void do_LPI(unsigned int irq);
+#else
+static inline void do_LPI(unsigned int irq)
+{
+}
+#endif
+

This would avoid such ugly hack where do_LPI is define in gic-v3-its.c but declared in irq.h.

I agree that this doesn't look nice, that's why I came up with a neater version in v3.
So doing the indirection comes at the price of additional code, which is hard to chase (because of the function pointer) and probably more costly (because it's harder to speculate on a CPU). But well, as you wish ...

Cheers,
Andre.

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

#78 lore raw

Re: [PATCH v2 10/27] ARM: GICv3: forward pending LPIs to guests

Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 13 lines · Julien Grall
Quoted 5 lines
Quoted 2 lines

+ /* We may have mapped more host LPIs than the guest actually
asked for. */

Another way, is the interrupt has been received at the same time the guest is configuring it. What will happen if the interrupt is lost?

I don't see how this would be our problem. If the guest is still about to configure an LPI, then any incoming one is pretty clearly a spurious interrupt.
I take it that you mean "mapping an LPI using ITS commands" when you say "configuring" here. If this is not what you mean, please correct me.

Quoted 6 lines

I'd expect a guest to first map the LPI, then enable it and send an INV command. Doing that differently will not result in any sane behavior (as
in: a guest cannot expect an LPI to come through), so nothing we need to worry about (from a lost IRQ perspective).

Or what do I miss here?

All host LPIs will be enabled when a device is assigned to a guest. So technically an LPI can come at anytime before the guest is configuring the virtual mapping.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v2 10/27] ARM: GICv3: forward pending LPIs to guests

Hi,

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

Hi,

Hi Andre,

Quoted 18 lines · Julien Grall
Quoted 5 lines
Quoted 2 lines

+ /* We may have mapped more host LPIs than the guest actually
asked for. */

Another way, is the interrupt has been received at the same time the guest is configuring it. What will happen if the interrupt is lost?

I don't see how this would be our problem. If the guest is still about to configure an LPI, then any incoming one is pretty clearly a spurious interrupt.
I take it that you mean "mapping an LPI using ITS commands" when you say "configuring" here. If this is not what you mean, please correct me.

I'd expect a guest to first map the LPI, then enable it and send an INV command. Doing that differently will not result in any sane behavior (as
in: a guest cannot expect an LPI to come through), so nothing we need to worry about (from a lost IRQ perspective).

Quoted 5 lines
Quoted 1 line

Or what do I miss here?

All host LPIs will be enabled when a device is assigned to a guest. So technically an LPI can come at anytime before the guest is configuring the virtual mapping.

As I said: what is the problem? Any LPI coming in without being mapped by a guest would be spurious from a guest point of view, as in not expected and ignored. But a guest will never receive it, since we explicitly check for that and on the host side and bail out early. From do_LPI():

    /* Unmapped events are marked with an invalid LPI ID. */
    if ( hlpi.virt_lpi == INVALID_LPI )
        return;

And we (atomically) update the entry once the guest has configured it, so either it's ignored or delivered. And this is a benign race you have on real hardware too.

Cheers,
Andre.

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

#80 lore raw

Re: [PATCH v2 10/27] ARM: GICv3: forward pending LPIs to guests

Quoted 37 lines · Andre Przywara
Quoted 25 lines · Julien Grall
Quoted 1 line · Andre Przywara

Hi,

Hi Andre,

Quoted 18 lines · Julien Grall
Quoted 5 lines
Quoted 2 lines

+ /* We may have mapped more host LPIs than the guest actually
asked for. */

Another way, is the interrupt has been received at the same time the guest is configuring it. What will happen if the interrupt is lost?

I don't see how this would be our problem. If the guest is still about to configure an LPI, then any incoming one is pretty clearly a spurious interrupt.
I take it that you mean "mapping an LPI using ITS commands" when you say "configuring" here. If this is not what you mean, please correct me.

I'd expect a guest to first map the LPI, then enable it and send an INV command. Doing that differently will not result in any sane behavior (as
in: a guest cannot expect an LPI to come through), so nothing we need to worry about (from a lost IRQ perspective).

Quoted 5 lines
Quoted 1 line

Or what do I miss here?

All host LPIs will be enabled when a device is assigned to a guest. So technically an LPI can come at anytime before the guest is configuring the virtual mapping.

As I said: what is the problem? Any LPI coming in without being mapped by a guest would be spurious from a guest point of view, as in not expected and ignored. But a guest will never receive it, since we explicitly check for that and on the host side and bail out early. From do_LPI():

Quoted 7 lines
    /* Unmapped events are marked with an invalid LPI ID. */
    if ( hlpi.virt_lpi == INVALID_LPI )
        return;

And we (atomically) update the entry once the guest has configured it, so either it's ignored or delivered. And this is a benign race you have on real hardware too.

The document it... if I ask a questions multiple time it is likely the code is not clear enough.

Cheers,

Signature · 6 lines
Julien Grall

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

PATCH v2 11/27 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-v3-its.c |  4 ++++
 xen/arch/arm/gic-v3.c     | 19 +++++++++++++++++++
 2 files changed, 23 insertions(+)
xen/arch/arm/gic-v3-its.c +4 -0
index ed14d95..5a2dbec 100644
@@ -470,6 +470,10 @@ static int gicv3_its_init_single_its(struct host_its *hw_its)
         return -ENOMEM;
     writeq_relaxed(0, hw_its->its_base + GITS_CWRITER);
 
+    /* Now enable interrupt translation and command processing 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 38dafe7..83a839a 100644
@@ -621,6 +621,21 @@ static int gicv3_enable_redist(void)
     return 0;
 }
 
+/* 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;
@@ -729,8 +744,12 @@ static int gicv3_cpu_init(void)
     if ( gicv3_enable_redist() )
         return -ENODEV;
 
+    /* If the host has any ITSes, enable LPIs now. */
     if ( gicv3_its_host_has_its() )
+    {
         gicv3_its_setup_collection(smp_processor_id());
+        gicv3_enable_lpis();
+    }
 
     /* Set priority on PPI and SGI interrupts */
     priority = (GIC_PRI_IPI << 24 | GIC_PRI_IPI << 16 | GIC_PRI_IPI << 8 |
Signature · 7 lines
2.9.0


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

PATCH v2 12/27 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       | 220 +++++++++++++++++++++++++++++++++++++++----
 xen/arch/arm/vgic.c          |   4 +
 xen/include/asm-arm/domain.h |   8 +-
 xen/include/asm-arm/vgic.h   |  24 ++++-
 4 files changed, 233 insertions(+), 23 deletions(-)
xen/arch/arm/vgic-v3.c +203 -17
index b0653c2..c6db2d7 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>
@@ -229,12 +231,15 @@ 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);
+        *r &= ~GICR_PENDBASER_PTZ;       /* WO, reads as 0 */
+        return 1;
 
     case 0x0080:
         goto read_reserved;
@@ -302,11 +307,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;
@@ -331,11 +331,179 @@ 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 &= ~GICR_PROPBASER_RES0_MASK;
+    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 &= ~GICR_PENDBASER_RES0_MASK;
+    return reg;
+}
+
+/*
+ * Mark a given number of guest pages as used (by increasing their refcount),
+ * starting with the given guest address. This needs to be called once before
+ * calling (possibly repeatedly) map_guest_pages().
+ * Before the domain gets destroyed, call put_guest_pages() to drop the
+ * reference.
+ */
+int get_guest_pages(struct domain *d, paddr_t gpa, int nr_pages)
+{
+    int i;
+    struct page_info *page;
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        page = get_page_from_gfn(d, (gpa >> PAGE_SHIFT) + i, NULL, P2M_ALLOC);
+        if ( ! page )
+            return -EINVAL;
+    }
+
+    return 0;
+}
+
+void put_guest_pages(struct domain *d, paddr_t gpa, int nr_pages)
+{
+    mfn_t mfn;
+    int i;
+
+    p2m_read_lock(&d->arch.p2m);
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        mfn = p2m_get_entry(&d->arch.p2m, _gfn((gpa >> PAGE_SHIFT) + i),
+                            NULL, NULL, NULL);
+        if ( mfn_eq(mfn, INVALID_MFN) )
+            continue;
+        put_page(mfn_to_page(mfn_x(mfn)));
+    }
+    p2m_read_unlock(&d->arch.p2m);
+}
+
+/*
+ * Provides easy access to guest memory by "mapping" some parts of it into
+ * Xen's VA space. In fact it relies on the memory being already mapped
+ * and just provides a pointer to it.
+ * This allows the ITS configuration data to be held in guest memory and
+ * avoids using Xen's memory for that.
+ */
+void *map_guest_pages(struct domain *d, paddr_t guest_addr, int nr_pages)
+{
+    int i;
+    void *ptr, *follow;
+
+    ptr = map_domain_page(_mfn(guest_addr >> PAGE_SHIFT));
+
+    /* Make sure subsequent pages are mapped in a virtually contigious way. */
+    for ( i = 1; i < nr_pages; i++ )
+    {
+        follow = map_domain_page(_mfn((guest_addr >> PAGE_SHIFT) + i));
+        if ( follow != ptr + ((long)i << PAGE_SHIFT) )
+            return NULL;
+    }
+
+    return ptr + (guest_addr & ~PAGE_MASK);
+}
+
+/* "Unmap" previously mapped guest pages. Should be optimized away on arm64. */
+void unmap_guest_pages(void *va, int nr_pages)
+{
+    long i;
+
+    for ( i = nr_pages - 1; i >= 0; i-- )
+        unmap_domain_page(((uintptr_t)va & PAGE_MASK) + (i << PAGE_SHIFT));
+}
+
+int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
+{
+    if ( vlpi >= d->arch.vgic.nr_lpis )
+        return GIC_PRI_IRQ;
+
+    return d->arch.vgic.proptable[vlpi - LPI_OFFSET] & LPI_PROP_PRIO_MASK;
+}
+
+bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi)
+{
+    if ( vlpi >= d->arch.vgic.nr_lpis )
+        return false;
+
+    return d->arch.vgic.proptable[vlpi - LPI_OFFSET] & LPI_PROP_ENABLED;
+}
+
 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 )
     {
@@ -366,36 +534,54 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
         goto write_impl_defined;
 
     case VREG64(GICR_SETLPIR):
-        /* LPI is not implemented */
+        /* LPIs without an ITS are not implemented */
         goto write_ignore_64;
 
     case VREG64(GICR_CLRLPIR):
-        /* LPI is not implemented */
+        /* LPIs without an ITS are not implemented */
         goto write_ignore_64;
 
     case 0x0050:
         goto write_reserved;
 
     case VREG64(GICR_PROPBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+
+        /* Writing PROPBASER with LPIs enabled is UNPREDICTABLE. */
+        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;
+        return 1;
 
     case VREG64(GICR_PENDBASER):
-        /* LPI is not implemented */
-        goto write_ignore_64;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
+
+        /* Writing PENDBASER with LPIs enabled is UNPREDICTABLE. */
+        if ( v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED )
+            return 1;
+
+	reg = v->arch.vgic.rdist_pendbase;
+	vgic_reg64_update(&reg, r, info);
+	reg = sanitize_pendbaser(reg);
+	v->arch.vgic.rdist_pendbase = reg;
+	return 1;
 
     case 0x0080:
         goto write_reserved;
 
     case VREG64(GICR_INVLPIR):
-        /* LPI is not implemented */
+        /* LPIs without an ITS are not implemented */
         goto write_ignore_64;
 
     case 0x00A8:
         goto write_reserved;
 
     case VREG64(GICR_INVALLR):
-        /* LPI is not implemented */
+        /* LPIs without an ITS are not implemented */
         goto write_ignore_64;
 
     case 0x00B8:
xen/arch/arm/vgic.c +4 -0
index e5cfa54..47ba8c0 100644
@@ -494,6 +494,10 @@ struct pending_irq *lpi_to_pending(struct vcpu *v, unsigned int lpi,
         empty->pirq.irq = lpi;
     }
 
+    /* Update the enabled status */
+    if ( vgic_lpi_is_enabled(v->domain, lpi) )
+        set_bit(GIC_IRQ_GUEST_ENABLED, &empty->pirq.status);
+
     spin_unlock(&v->arch.vgic.pending_lpi_list_lock);
 
     return &empty->pirq;
xen/include/asm-arm/domain.h +7 -1
index f44a84b..33c1851 100644
@@ -110,6 +110,9 @@ struct arch_domain
         } *rdist_regions;
         int nr_regions;                     /* Number of rdist regions */
         uint32_t rdist_stride;              /* Re-Distributor stride */
+        int nr_lpis;
+        uint64_t rdist_propbase;
+        uint8_t *proptable;
         struct rb_root its_devices;         /* devices mapped to an ITS */
         spinlock_t its_devices_lock;        /* protects the its_devices tree */
 #endif
@@ -255,7 +258,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;
         spinlock_t pending_lpi_list_lock;   /* protects the pending_lpi_list */
xen/include/asm-arm/vgic.h +19 -5
index 8f1099c..f8bccfa 100644
@@ -285,6 +285,11 @@ VGIC_REG_HELPERS(32, 0x3);
 
 #undef VGIC_REG_HELPERS
 
+int get_guest_pages(struct domain *d, paddr_t gpa, int nr_pages);
+void put_guest_pages(struct domain *d, paddr_t gpa, int nr_pages);
+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;
 
 /*
@@ -312,14 +317,23 @@ extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
 extern void vgic_disable_irqs(struct vcpu *v, uint32_t r, int n);
 extern void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n);
-/* placeholder function until the property table gets introduced */
-static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
-{
-    return 0xa;
-}
 extern void register_vgic_ops(struct domain *d, const struct vgic_ops *ops);
 int vgic_v2_init(struct domain *d, int *mmio_count);
 int vgic_v3_init(struct domain *d, int *mmio_count);
+#ifdef CONFIG_HAS_GICV3
+extern int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi);
+extern bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi);
+#else
+static inline int vgic_lpi_get_priority(struct domain *d, uint32_t vlpi)
+{
+    return 0xa0;
+}
+
+static inline bool vgic_lpi_is_enabled(struct domain *d, uint32_t vlpi)
+{
+    return false;
+}
+#endif
 
 extern int domain_vgic_register(struct domain *d, int *mmio_count);
 extern int vcpu_vgic_free(struct vcpu *v);
Signature · 7 lines
2.9.0


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

Re: [PATCH v2 12/27] ARM: vGICv3: handle virtual LPI pending and property tables

Hi Andre,

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

Please address comments from: alpine.DEB.2.10.1610281619240.9978@sstabellini-ThinkPad-X260 and [email protected]

Cheers,

Signature · 6 lines
Julien Grall

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