Skip to contentSkip to conversation

SMMU handling for PCIe Passthrough on ARM

PATCH Superseded Mykyta Poturai 4 participants 37 messages Mar 14, 2025 – Apr 29, 2025

v9 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v9 cover SMMU handling for PCIe Passthrough on ARM

This series introduces SMMU handling for PCIe passthrough on ARM. These patches should be able to be upstreamed independently from the vPCI series [1]. See [2] for notes about test cases.

[1] https://lists.xenproject.org/archives/html/xen-devel/2023-10/msg00660.html
[2] https://lists.xenproject.org/archives/html/xen-devel/2023-06/msg01135.html

v8->v9:
* see individual patches

v7->v8:
* no changes

v6->v7:
* drop ("xen/arm: don't pass iommu properties to hwdom for iommu-map")

v5->v6:
* don't revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: enable dom0 to use PCI devices with pci-passthrough=no")

v4->v5:
* drop ("xen/arm: Improve readability of check for registered devices")
* drop ("xen/arm: Move is_protected flag to struct device")
* add ("xen/arm: don't pass iommu properties to hwdom for iommu-map")
* add ("xen/arm: Fix mapping for PCI bridge mmio region")
* revert ("xen/arm: Add cmdline boot option "pci-passthrough = <boolean>"")
* add ("xen/arm: Map ITS doorbell register to IOMMU page tables.")
* fix test case #1 with PCI device in dom0

v3->v4:
* split a change from ("xen/arm: Move is_protected flag to struct device") into a new separate patch
* see individual patches for further details

v2->v3:
* drop "pci/arm: Use iommu_add_dt_pci_device()"
* drop "RFC: pci/arm: don't do iommu call for phantom functions"
* move invocation of sideband ID mapping function to add_device() platform_ops/iommu_ops hook

Oleksandr Andrushchenko (1):
xen/arm: smmuv2: Add PCI devices support for SMMUv2

Oleksandr Tyshchenko (2):
iommu/arm: Add iommu_dt_xlate()
iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

Rahul Singh (3):
xen/arm: smmuv3: Add PCI devices support for SMMUv3 xen/arm: Fix mapping for PCI bridge mmio region xen/arm: Map ITS doorbell register to IOMMU page tables

Stewart Hildebrand (2):
iommu/arm: iommu_add_dt_pci_sideband_ids phantom handling xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

 xen/arch/arm/device.c                 |   2 +-
 xen/arch/arm/domain_build.c           |   2 +-
 xen/arch/arm/include/asm/pci.h        |   5 +-
 xen/arch/arm/pci/pci.c                |  11 +-
 xen/arch/arm/vgic-v3-its.c            |  20 +++
 xen/arch/x86/include/asm/pci.h        |   2 +-
 xen/common/device-tree/device-tree.c  |  91 ++++++++++++
 xen/drivers/passthrough/arm/smmu-v3.c | 117 ++++++++++++++--
 xen/drivers/passthrough/arm/smmu.c    | 190 ++++++++++++++++++++------
 xen/drivers/passthrough/device_tree.c |  91 ++++++++++--
 xen/drivers/passthrough/iommu.c       |  15 ++
 xen/drivers/pci/physdev.c             |   4 +-
 xen/include/xen/device_tree.h         |  23 ++++
 xen/include/xen/iommu.h               |  40 +++++-
 xen/include/xen/pci.h                 |   2 +-
 15 files changed, 534 insertions(+), 81 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v9 1/8 iommu/arm: Add iommu_dt_xlate()

From: Oleksandr Tyshchenko <[email protected]>

Move code for processing DT IOMMU specifier to a separate helper. This helper will be re-used for adding PCI devices by the subsequent patches as we will need exact the same actions for processing DT PCI-IOMMU specifier.

---
v8->v9:
* remove DT_NO_IOMMU

v7->v8:
* explain NO_IOMMU better and rename to DT_NO_IOMMU

v6->v7:
* explained NO_IOMMU in comments

v5->v6:
* pass ops parameter to iommu_dt_xlate()
* add Julien's R-b

v4->v5:
* rebase on top of "dynamic node programming using overlay dtbo" series
* move #define NO_IOMMU 1 to header
* s/these/this/ inside comment

v3->v4:
* make dt_phandle_args *iommu_spec const
* move !ops->add_device check to helper

v2->v3:
* no change

v1->v2:
* no change

downstream->v1:
* trivial rebase
* s/dt_iommu_xlate/iommu_dt_xlate/

(cherry picked from commit c26bab0415ca303df86aba1d06ef8edc713734d3 from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)

---
 xen/drivers/passthrough/device_tree.c | 42 +++++++++++++++++----------
 1 file changed, 27 insertions(+), 15 deletions(-)
xen/drivers/passthrough/device_tree.c +27 -15
index 075fb25a37..4a1971c3fc 100644
@@ -137,6 +137,30 @@ int iommu_release_dt_devices(struct domain *d)
     return 0;
 }
 
+static int iommu_dt_xlate(struct device *dev,
+                          const struct dt_phandle_args *iommu_spec,
+                          const struct iommu_ops *ops)
+{
+    int rc;
+
+    if ( !ops->dt_xlate )
+        return -EINVAL;
+
+    if ( !dt_device_is_available(iommu_spec->np) )
+        return 1;
+
+    rc = iommu_fwspec_init(dev, &iommu_spec->np->dev);
+    if ( rc )
+        return rc;
+
+    /*
+     * Provide DT IOMMU specifier which describes the IOMMU master
+     * interfaces of that device (device IDs, etc) to the driver.
+     * The driver is responsible to decide how to interpret them.
+     */
+    return ops->dt_xlate(dev, iommu_spec);
+}
+
 int iommu_remove_dt_device(struct dt_device_node *np)
 {
     const struct iommu_ops *ops = iommu_get_ops();
@@ -215,27 +239,15 @@ int iommu_add_dt_device(struct dt_device_node *np)
     {
         /*
          * The driver which supports generic IOMMU DT bindings must have
-         * these callback implemented.
+         * this callback implemented.
          */
-        if ( !ops->add_device || !ops->dt_xlate )
+        if ( !ops->add_device )
         {
             rc = -EINVAL;
             goto fail;
         }
 
-        if ( !dt_device_is_available(iommu_spec.np) )
-            break;
-
-        rc = iommu_fwspec_init(dev, &iommu_spec.np->dev);
-        if ( rc )
-            break;
-
-        /*
-         * Provide DT IOMMU specifier which describes the IOMMU master
-         * interfaces of that device (device IDs, etc) to the driver.
-         * The driver is responsible to decide how to interpret them.
-         */
-        rc = ops->dt_xlate(dev, &iommu_spec);
+        rc = iommu_dt_xlate(dev, &iommu_spec, ops);
         if ( rc )
             break;
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v9 1/8] iommu/arm: Add iommu_dt_xlate()

Quoted 11 lines · Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

Move code for processing DT IOMMU specifier to a separate helper. This helper will be re-used for adding PCI devices by the subsequent patches as we will need exact the same actions for processing DT PCI-IOMMU specifier.

Committed

#3 lore raw

PATCH v9 2/8 iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

In reply to Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.

This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.

The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root.

v8->v9:
* replace DT_NO_IOMMU with 1
* guard iommu_add_pci_sideband_ids with CONFIG_ARM

v7->v8:
* ENOSYS->EOPNOTSUPP
* move iommu_add_pci_sideband_ids to iommu.c to fix x86 build
* simplify iommu_add_pci_sideband_ids
* add docstrings to iommu_add_{dt_}pci_sideband_ids

v6->v7:
* put iommu_add_pci_sideband_ids under ifdef
* remove ifdef CONFIG_APCI
* style: add newline for symmetry

v5->v6:
* pass ops to iommu_dt_xlate()

v4->v5:
* style: add newlines after variable declarations and before return in iommu.h
* drop device_is_protected() check in iommu_add_dt_pci_sideband_ids()
* rebase on top of ("dynamic node programming using overlay dtbo") series
* fix typo in commit message
* remove #ifdef around dt_map_id() prototype
* move dt_map_id() to xen/common/device_tree.c
* add function name in error prints
* use dprintk for debug prints
* use GENMASK and #include <xen/bitops.h>
* fix typo in comment
* remove unnecessary (int) cast in loop condition
* assign *id_out and return success in case of no translation in dt_map_id()
* don't initialize local variable unnecessarily
* return error in case of ACPI/no DT in iommu_add_{dt_}pci_sideband_ids()

v3->v4:
* wrap #include <asm/acpi.h> and if ( acpi_disabled ) in #ifdef CONFIG_ACPI
* fix Michal's remarks about style, parenthesis, and print formats
* remove !ops->dt_xlate check since it is already in iommu_dt_xlate helper
* rename s/iommu_dt_pci_map_id/dt_map_id/ because it is generic, not specific to iommu
* update commit description

v2->v3:
* new patch title (was: iommu/arm: Introduce iommu_add_dt_pci_device API)
* renamed function
from: iommu_add_dt_pci_device
to: iommu_add_dt_pci_sideband_ids
* removed stale ops->add_device check
* iommu.h: add empty stub iommu_add_dt_pci_sideband_ids for !HAS_DEVICE_TREE
* iommu.h: add iommu_add_pci_sideband_ids helper
* iommu.h: don't wrap prototype in #ifdef CONFIG_HAS_PCI
* s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/

v1->v2:
* remove extra devfn parameter since pdev fully describes the device
* remove ops->add_device() call from iommu_add_dt_pci_device(). Instead, rely on the existing iommu call in iommu_add_device().
* move the ops->add_device and ops->dt_xlate checks earlier

downstream->v1:
* rebase
* add const qualifier to struct dt_device_node *np arg in dt_map_id()
* add const qualifier to struct dt_device_node *np declaration in iommu_add_pci_device()
* use stdint.h types instead of u8/u32/etc...
* rename functions:
s/dt_iommu_xlate/iommu_dt_xlate/
s/dt_map_id/iommu_dt_pci_map_id/ s/iommu_add_pci_device/iommu_add_dt_pci_device/
* add device_is_protected check in iommu_add_dt_pci_device
* wrap prototypes in CONFIG_HAS_PCI

(cherry picked from commit 734e3bf6ee77e7947667ab8fa96c25b349c2e1da from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)

---
 xen/common/device-tree/device-tree.c  | 91 +++++++++++++++++++++++++++
 xen/drivers/passthrough/device_tree.c | 42 +++++++++++++
 xen/drivers/passthrough/iommu.c       | 15 +++++
 xen/include/xen/device_tree.h         | 23 +++++++
 xen/include/xen/iommu.h               | 40 +++++++++++-
 5 files changed, 210 insertions(+), 1 deletion(-)
xen/common/device-tree/device-tree.c +91 -0
index d0528c5825..3de7858df6 100644
@@ -10,6 +10,7 @@
  * published by the Free Software Foundation.
  */
 
+#include <xen/bitops.h>
 #include <xen/types.h>
 #include <xen/init.h>
 #include <xen/guest_access.h>
@@ -2243,6 +2244,96 @@ int dt_get_pci_domain_nr(struct dt_device_node *node)
     return (u16)domain;
 }
 
+int dt_map_id(const struct dt_device_node *np, uint32_t id,
+              const char *map_name, const char *map_mask_name,
+              struct dt_device_node **target, uint32_t *id_out)
+{
+    uint32_t map_mask, masked_id, map_len;
+    const __be32 *map = NULL;
+
+    if ( !np || !map_name || (!target && !id_out) )
+        return -EINVAL;
+
+    map = dt_get_property(np, map_name, &map_len);
+    if ( !map )
+    {
+        if ( target )
+            return -ENODEV;
+
+        /* Otherwise, no map implies no translation */
+        *id_out = id;
+        return 0;
+    }
+
+    if ( !map_len || (map_len % (4 * sizeof(*map))) )
+    {
+        printk(XENLOG_ERR "%s(): %s: Error: Bad %s length: %u\n", __func__,
+               np->full_name, map_name, map_len);
+        return -EINVAL;
+    }
+
+    /* The default is to select all bits. */
+    map_mask = GENMASK(31, 0);
+
+    /*
+     * Can be overridden by "{iommu,msi}-map-mask" property.
+     * If dt_property_read_u32() fails, the default is used.
+     */
+    if ( map_mask_name )
+        dt_property_read_u32(np, map_mask_name, &map_mask);
+
+    masked_id = map_mask & id;
+    for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4 )
+    {
+        struct dt_device_node *phandle_node;
+        uint32_t id_base = be32_to_cpup(map + 0);
+        uint32_t phandle = be32_to_cpup(map + 1);
+        uint32_t out_base = be32_to_cpup(map + 2);
+        uint32_t id_len = be32_to_cpup(map + 3);
+
+        if ( id_base & ~map_mask )
+        {
+            printk(XENLOG_ERR "%s(): %s: Invalid %s translation - %s-mask (0x%"PRIx32") ignores id-base (0x%"PRIx32")\n",
+                   __func__, np->full_name, map_name, map_name, map_mask,
+                   id_base);
+            return -EFAULT;
+        }
+
+        if ( (masked_id < id_base) || (masked_id >= (id_base + id_len)) )
+            continue;
+
+        phandle_node = dt_find_node_by_phandle(phandle);
+        if ( !phandle_node )
+            return -ENODEV;
+
+        if ( target )
+        {
+            if ( !*target )
+                *target = phandle_node;
+
+            if ( *target != phandle_node )
+                continue;
+        }
+
+        if ( id_out )
+            *id_out = masked_id - id_base + out_base;
+
+        dprintk(XENLOG_DEBUG, "%s: %s, using mask %08"PRIx32", id-base: %08"PRIx32", out-base: %08"PRIx32", length: %08"PRIx32", id: %08"PRIx32" -> %08"PRIx32"\n",
+               np->full_name, map_name, map_mask, id_base, out_base, id_len, id,
+               masked_id - id_base + out_base);
+        return 0;
+    }
+
+    dprintk(XENLOG_DEBUG, "%s: no %s translation for id 0x%"PRIx32" on %s\n",
+           np->full_name, map_name, id,
+           (target && *target) ? (*target)->full_name : NULL);
+
+    if ( id_out )
+        *id_out = id;
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/drivers/passthrough/device_tree.c +42 -0

48 lines not on this page · raw message

xen/drivers/passthrough/iommu.c +15 -0
index 9e74a1fc72..dfaca67302 100644
@@ -20,6 +20,7 @@
 #include <xen/param.h>
 #include <xen/softirq.h>
 #include <xen/keyhandler.h>
+#include <xen/acpi.h>
 #include <xsm/xsm.h>
 
 #ifdef CONFIG_X86
@@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func,
     return 0;
 }
 
+#ifdef CONFIG_ARM
+int iommu_add_pci_sideband_ids(struct pci_dev *pdev)
+{
+    int ret = -EOPNOTSUPP;
+
+#ifdef CONFIG_HAS_PCI
+    if ( acpi_disabled )
+        ret = iommu_add_dt_pci_sideband_ids(pdev);
+#endif
+
+    return ret;
+}
+#endif
+
 /*
  * Local variables:
  * mode: C
xen/include/xen/device_tree.h +23 -0
index 5ff763bb80..9254204af6 100644
@@ -946,6 +946,29 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,
  */
 int dt_get_pci_domain_nr(struct dt_device_node *node);
 
+/**
+ * dt_map_id - Translate an ID through a downstream mapping.
+ * @np: root complex device node.
+ * @id: device ID to map.
+ * @map_name: property name of the map to use.
+ * @map_mask_name: optional property name of the mask to use.
+ * @target: optional pointer to a target device node.
+ * @id_out: optional pointer to receive the translated ID.
+ *
+ * Given a device ID, look up the appropriate implementation-defined
+ * platform ID and/or the target device which receives transactions on that
+ * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
+ * @id_out may be NULL if only the other is required. If @target points to
+ * a non-NULL device node pointer, only entries targeting that node will be
+ * matched; if it points to a NULL value, it will receive the device node of
+ * the first matching target phandle, with a reference held.
+ *
+ * Return: 0 on success or a standard error code on failure.
+ */
+int dt_map_id(const struct dt_device_node *np, uint32_t id,
+              const char *map_name, const char *map_mask_name,
+              struct dt_device_node **target, uint32_t *id_out);
+
 struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle);
 
 #ifdef CONFIG_DEVICE_TREE_DEBUG
xen/include/xen/iommu.h +39 -1
index b928c67e19..82319016a1 100644
@@ -215,7 +215,8 @@ int iommu_dt_domain_init(struct domain *d);
 int iommu_release_dt_devices(struct domain *d);
 
 /*
- * Helper to add master device to the IOMMU using generic IOMMU DT bindings.
+ * Helpers to add master device to the IOMMU using generic (PCI-)IOMMU
+ * DT bindings.
  *
  * Return values:
  *  0 : device is protected by an IOMMU
@@ -225,6 +226,19 @@ int iommu_release_dt_devices(struct domain *d);
  */
 int iommu_add_dt_device(struct dt_device_node *np);
 
+/*
+ * Fills out the device's IOMMU fwspec with IOMMU ids from the DT.
+ * Ids are specified in the iommu-map property in the host bridge node.
+ * More information on the iommu-map property format can be found in
+ * Documentation/devicetree/bindings/pci/pci-iommu.txt from Linux.
+ *
+ * Return values:
+ *  0 : iommu_fwspec is filled out successfully.
+ * <0 : error while filling out the iommu_fwspec.
+ * >0 : IOMMU is not enabled/present or device is not connected to it.
+ */
+int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev);
+
 int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,
                        XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl);
 
@@ -238,8 +252,32 @@ int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,
  */
 int iommu_remove_dt_device(struct dt_device_node *np);
 
+#else /* !HAS_DEVICE_TREE */
+static inline int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)
+{
+    return -EOPNOTSUPP;
+}
+
 #endif /* HAS_DEVICE_TREE */
 
+/*
+ * This function is not strictly ARM-specific, but it is only used by ARM
+ * as of now. So wrap it with ifdef to avoid creating dead code on other
+ * architectures. When usage is extended to other architectures, this ifdef
+ * should be removed.
+ */
+#ifdef CONFIG_ARM
+/*
+ * Fills out the device's IOMMU fwspec with IOMMU ids.
+ *
+ * Return values:
+ *  0 : iommu_fwspec is filled out successfully.
+ * <0 : error while filling out the iommu_fwspec.
+ * >0 : IOMMU is not enabled/present or device is not connected to it.
+ */
+int iommu_add_pci_sideband_ids(struct pci_dev *pdev);
+#endif
+
 struct page_info;
 
 /*
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

Quoted 28 lines · Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.

This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.

The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root.

Quoted 3 lines

v8->v9:
* replace DT_NO_IOMMU with 1
* guard iommu_add_pci_sideband_ids with CONFIG_ARM

I fear I'm confused: Isn't this contradicting ...

Quoted 3 lines

v7->v8:
* ENOSYS->EOPNOTSUPP
* move iommu_add_pci_sideband_ids to iommu.c to fix x86 build

... this earlier change? Really, with there being no caller, I can't see why there could be any build issue here affecting only x86. Except for Misra complaining about unreachable code being introduced, which I'm sure I said before should be avoided.

Quoted 11 lines
xen/drivers/passthrough/iommu.c +1 -0
@@ -20,6 +20,7 @@
 #include <xen/param.h>
 #include <xen/softirq.h>
 #include <xen/keyhandler.h>
+#include <xen/acpi.h>
 #include <xsm/xsm.h>
 
 #ifdef CONFIG_X86

xen/drivers/passthrough/iommu.c int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, · line 748

Quoted 4 lines
xen/drivers/passthrough/iommu.c +1 -0
@@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func,
     return 0;
 }
 
+#ifdef CONFIG_ARM

I realize we have CONFIG_X86 here as well (visible even in context of the earlier hunk. Yet then the goal ought to be to reduce these anomalies, not add new ones. Since I don't have a clear picture of what's wanted, I'm also in trouble suggesting any alternative, I'm afraid.

Quoted 6 lines
xen/drivers/passthrough/iommu.c +6 -0
+int iommu_add_pci_sideband_ids(struct pci_dev *pdev)
+{
+    int ret = -EOPNOTSUPP;
+
+#ifdef CONFIG_HAS_PCI
+    if ( acpi_disabled )

xen/drivers/passthrough/iommu.c int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, · line 764

Quoted 10 lines
xen/drivers/passthrough/iommu.c +7 -0
+        ret = iommu_add_dt_pci_sideband_ids(pdev);
+#endif
+
+    return ret;
+}
+#endif
+
 /*
  * Local variables:
  * mode: C

Having reached the end of the changes to this file, it's also not quite clear why xen/acpi.h would need including uniformly. Can't this live inside the outer #ifdef?

Further, since #ifdef-ary is used here already anyway, why ...

xen/drivers/passthrough/iommu.c int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d, · line 261

Quoted 11 lines
xen/drivers/passthrough/iommu.c +6 -0
@@ -238,8 +252,32 @@ int iommu_do_dt_domctl(struct xen_domctl *domctl, struct domain *d,
  */
 int iommu_remove_dt_device(struct dt_device_node *np);
 
+#else /* !HAS_DEVICE_TREE */
+static inline int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)
+{
+    return -EOPNOTSUPP;
+}
+
 #endif /* HAS_DEVICE_TREE */

... resort to a stub when the inner #ifdef could simple be extended?

Jan

#5 lore raw

Re: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

Quoted 35 lines · Jan Beulich
Quoted 28 lines · Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.

This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.

The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root.

Quoted 3 lines

v8->v9:
* replace DT_NO_IOMMU with 1
* guard iommu_add_pci_sideband_ids with CONFIG_ARM

I fear I'm confused: Isn't this contradicting ...

Quoted 8 lines
Quoted 3 lines

v7->v8:
* ENOSYS->EOPNOTSUPP
* move iommu_add_pci_sideband_ids to iommu.c to fix x86 build

... this earlier change? Really, with there being no caller, I can't see why there could be any build issue here affecting only x86. Except for Misra complaining about unreachable code being introduced, which I'm sure I said before should be avoided.

The original reason for moving this function was the conflicting ACPI and EFI headers, I described it in V8 comments here[1].

Quoted 15 lines
Quoted 15 lines
xen/drivers/passthrough/iommu.c +1 -0
@@ -20,6 +20,7 @@
  #include <xen/param.h>
  #include <xen/softirq.h>
  #include <xen/keyhandler.h>
+#include <xen/acpi.h>
  #include <xsm/xsm.h>
 
  #ifdef CONFIG_X86
@@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func,
      return 0;
  }
 
Quoted 8 lines

xen/drivers/passthrough/iommu.c int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, · line 748

Quoted 1 line
xen/drivers/passthrough/iommu.c +1 -0
+#ifdef CONFIG_ARM

I realize we have CONFIG_X86 here as well (visible even in context of the earlier hunk. Yet then the goal ought to be to reduce these anomalies, not add new ones. Since I don't have a clear picture of what's wanted, I'm also in trouble suggesting any alternative, I'm afraid.

Jan

Here is a short summary:

The main problem is that we need this function somewhere, but there is no good place for it. It is only called on ARM for now but it's not ARM-specific by nature and can be eventually used on other platforms as well. It can't be just dropped because of the effort to support the co-existence of DT and ACPI. It also can't be declared as a static function because it requires the inclusion of <xen/acpi.h> for acpi_disabled define, which leads to build errors[1]. And without ifdef guards it would be a MISRA violation.

[1]
https://patchew.org/Xen/[email protected]/67b9bd138c12c0df35e5c4b3137c30ad345097d5.1739182214.git.mykyta._5Fpoturai@epam.com/#[email protected]
--
Mykyta

#6 lore raw

Re: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

Quoted 72 lines · Mykyta Poturai
Quoted 35 lines · Jan Beulich
Quoted 28 lines · Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.

This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.

The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root.

Quoted 3 lines

v8->v9:
* replace DT_NO_IOMMU with 1
* guard iommu_add_pci_sideband_ids with CONFIG_ARM

I fear I'm confused: Isn't this contradicting ...

Quoted 8 lines
Quoted 3 lines

v7->v8:
* ENOSYS->EOPNOTSUPP
* move iommu_add_pci_sideband_ids to iommu.c to fix x86 build

... this earlier change? Really, with there being no caller, I can't see why there could be any build issue here affecting only x86. Except for Misra complaining about unreachable code being introduced, which I'm sure I said before should be avoided.

The original reason for moving this function was the conflicting ACPI and EFI headers, I described it in V8 comments here[1].

Quoted 15 lines
Quoted 15 lines
xen/drivers/passthrough/iommu.c +1 -0
@@ -20,6 +20,7 @@
  #include <xen/param.h>
  #include <xen/softirq.h>
  #include <xen/keyhandler.h>
+#include <xen/acpi.h>
  #include <xsm/xsm.h>
 
  #ifdef CONFIG_X86
@@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func,
      return 0;
  }
 
Quoted 6 lines

xen/drivers/passthrough/iommu.c int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, · line 748

Quoted 1 line
xen/drivers/passthrough/iommu.c +1 -0
+#ifdef CONFIG_ARM

I realize we have CONFIG_X86 here as well (visible even in context of the earlier hunk. Yet then the goal ought to be to reduce these anomalies, not add new ones. Since I don't have a clear picture of what's wanted, I'm also in trouble suggesting any alternative, I'm afraid.

Here is a short summary:

Quoted 8 lines

The main problem is that we need this function somewhere, but there is no good place for it. It is only called on ARM for now but it's not ARM-specific by nature and can be eventually used on other platforms as well. It can't be just dropped because of the effort to support the co-existence of DT and ACPI. It also can't be declared as a static function because it requires the inclusion of <xen/acpi.h> for acpi_disabled define, which leads to build errors[1]. And without ifdef guards it would be a MISRA violation.

An abridged version of this ought to go in the patch description, I think. This is special, so it needs calling out.

As to the placement - would making an entirely new .c file possibly help? (Then, instead of in the patch description, maybe the special aspect could be put in a code comment at the top of the file.)

Jan

#7 lore raw

Re: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

Quoted 82 lines · Jan Beulich
Quoted 72 lines · Mykyta Poturai
Quoted 35 lines · Jan Beulich
Quoted 28 lines · Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.

This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.

The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root.

Quoted 3 lines

v8->v9:
* replace DT_NO_IOMMU with 1
* guard iommu_add_pci_sideband_ids with CONFIG_ARM

I fear I'm confused: Isn't this contradicting ...

Quoted 8 lines
Quoted 3 lines

v7->v8:
* ENOSYS->EOPNOTSUPP
* move iommu_add_pci_sideband_ids to iommu.c to fix x86 build

... this earlier change? Really, with there being no caller, I can't see why there could be any build issue here affecting only x86. Except for Misra complaining about unreachable code being introduced, which I'm sure I said before should be avoided.

The original reason for moving this function was the conflicting ACPI and EFI headers, I described it in V8 comments here[1].

Quoted 15 lines
Quoted 15 lines
xen/drivers/passthrough/iommu.c +1 -0
@@ -20,6 +20,7 @@
   #include <xen/param.h>
   #include <xen/softirq.h>
   #include <xen/keyhandler.h>
+#include <xen/acpi.h>
   #include <xsm/xsm.h>
 
   #ifdef CONFIG_X86
@@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func,
       return 0;
   }
 
Quoted 6 lines

xen/drivers/passthrough/iommu.c int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, · line 748

Quoted 1 line
xen/drivers/passthrough/iommu.c +1 -0
+#ifdef CONFIG_ARM

I realize we have CONFIG_X86 here as well (visible even in context of the earlier hunk. Yet then the goal ought to be to reduce these anomalies, not add new ones. Since I don't have a clear picture of what's wanted, I'm also in trouble suggesting any alternative, I'm afraid.

Here is a short summary:

Quoted 8 lines

The main problem is that we need this function somewhere, but there is no good place for it. It is only called on ARM for now but it's not ARM-specific by nature and can be eventually used on other platforms as well. It can't be just dropped because of the effort to support the co-existence of DT and ACPI. It also can't be declared as a static function because it requires the inclusion of <xen/acpi.h> for acpi_disabled define, which leads to build errors[1]. And without ifdef guards it would be a MISRA violation.

Quoted 8 lines

An abridged version of this ought to go in the patch description, I think. This is special, so it needs calling out.

As to the placement - would making an entirely new .c file possibly help? (Then, instead of in the patch description, maybe the special aspect could be put in a code comment at the top of the file.)

Jan

It seems to me creating a new file would be overkill for one small function. I considered moving it to xen/drivers/passthrough/arm/iommu.c to reduce ifdefs but I feared it would suggest that it is arch-specific a bit too strongly. So maybe move it there after all if you think it would be better?

Signature · 1 line
Mykyta
#8 lore raw

Re: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

Quoted 90 lines · Mykyta Poturai
Quoted 82 lines · Jan Beulich
Quoted 72 lines · Mykyta Poturai
Quoted 35 lines · Jan Beulich
Quoted 28 lines · Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.

This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.

The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root.

Quoted 3 lines

v8->v9:
* replace DT_NO_IOMMU with 1
* guard iommu_add_pci_sideband_ids with CONFIG_ARM

I fear I'm confused: Isn't this contradicting ...

Quoted 8 lines
Quoted 3 lines

v7->v8:
* ENOSYS->EOPNOTSUPP
* move iommu_add_pci_sideband_ids to iommu.c to fix x86 build

... this earlier change? Really, with there being no caller, I can't see why there could be any build issue here affecting only x86. Except for Misra complaining about unreachable code being introduced, which I'm sure I said before should be avoided.

The original reason for moving this function was the conflicting ACPI and EFI headers, I described it in V8 comments here[1].

Quoted 15 lines
Quoted 15 lines
xen/drivers/passthrough/iommu.c +1 -0
@@ -20,6 +20,7 @@
   #include <xen/param.h>
   #include <xen/softirq.h>
   #include <xen/keyhandler.h>
+#include <xen/acpi.h>
   #include <xsm/xsm.h>
 
   #ifdef CONFIG_X86
@@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func,
       return 0;
   }
 
Quoted 6 lines

xen/drivers/passthrough/iommu.c int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, · line 748

Quoted 1 line
xen/drivers/passthrough/iommu.c +1 -0
+#ifdef CONFIG_ARM

I realize we have CONFIG_X86 here as well (visible even in context of the earlier hunk. Yet then the goal ought to be to reduce these anomalies, not add new ones. Since I don't have a clear picture of what's wanted, I'm also in trouble suggesting any alternative, I'm afraid.

Here is a short summary:

Quoted 8 lines

The main problem is that we need this function somewhere, but there is no good place for it. It is only called on ARM for now but it's not ARM-specific by nature and can be eventually used on other platforms as well. It can't be just dropped because of the effort to support the co-existence of DT and ACPI. It also can't be declared as a static function because it requires the inclusion of <xen/acpi.h> for acpi_disabled define, which leads to build errors[1]. And without ifdef guards it would be a MISRA violation.

Quoted 6 lines

An abridged version of this ought to go in the patch description, I think. This is special, so it needs calling out.

As to the placement - would making an entirely new .c file possibly help? (Then, instead of in the patch description, maybe the special aspect could be put in a code comment at the top of the file.)

Quoted 5 lines

It seems to me creating a new file would be overkill for one small function. I considered moving it to xen/drivers/passthrough/arm/iommu.c to reduce ifdefs but I feared it would suggest that it is arch-specific a bit too strongly. So maybe move it there after all if you think it would be better?

Well - with "#ifdef CONFIG_ARM" around it's Arm-specific too, isn't it? IOW - either have a proper (even if simple) abstraction, or perhaps indeed put it there (if that's also okay with the Arm maintainers).

Jan

#9 lore raw

Re: [PATCH v9 2/8] iommu/arm: Introduce iommu_add_dt_pci_sideband_ids API

Quoted 97 lines · Jan Beulich
Quoted 90 lines · Mykyta Poturai
Quoted 82 lines · Jan Beulich
Quoted 72 lines · Mykyta Poturai
Quoted 35 lines · Jan Beulich
Quoted 28 lines · Mykyta Poturai

From: Oleksandr Tyshchenko <[email protected]>

The main purpose of this patch is to add a way to register PCI device (which is behind the IOMMU) using the generic PCI-IOMMU DT bindings [1] before assigning that device to a domain.

This behaves similarly to the existing iommu_add_dt_device API, except it handles PCI devices, and it is to be invoked from the add_device hook in the SMMU driver.

The function dt_map_id to translate an ID through a downstream mapping (which is also suitable for mapping Requester ID) was borrowed from Linux (v5.10-rc6) and updated according to the Xen code base.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
Regarding pci_for_each_dma_alias question: getting host bridge node directly seems like a simpler solution with the same result. AFAIU with pci_for_each_dma_alias in linux we would arrive to the host brige node anyway, but also try to call dt_map_id for each device along the way. I am not sure why exactly it is done this way in linux, as according to the pci-iommu.txt, iommu-map node can only be present in the PCI root.

Quoted 3 lines

v8->v9:
* replace DT_NO_IOMMU with 1
* guard iommu_add_pci_sideband_ids with CONFIG_ARM

I fear I'm confused: Isn't this contradicting ...

Quoted 8 lines
Quoted 3 lines

v7->v8:
* ENOSYS->EOPNOTSUPP
* move iommu_add_pci_sideband_ids to iommu.c to fix x86 build

... this earlier change? Really, with there being no caller, I can't see why there could be any build issue here affecting only x86. Except for Misra complaining about unreachable code being introduced, which I'm sure I said before should be avoided.

The original reason for moving this function was the conflicting ACPI and EFI headers, I described it in V8 comments here[1].

Quoted 15 lines
Quoted 15 lines
xen/drivers/passthrough/iommu.c +1 -0
@@ -20,6 +20,7 @@
   #include <xen/param.h>
   #include <xen/softirq.h>
   #include <xen/keyhandler.h>
+#include <xen/acpi.h>
   #include <xsm/xsm.h>
 
   #ifdef CONFIG_X86
@@ -744,6 +745,20 @@ int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func,
       return 0;
   }
 
Quoted 6 lines

xen/drivers/passthrough/iommu.c int __init iommu_get_extra_reserved_device_memory(iommu_grdm_t *func, · line 748

Quoted 1 line
xen/drivers/passthrough/iommu.c +1 -0
+#ifdef CONFIG_ARM

I realize we have CONFIG_X86 here as well (visible even in context of the earlier hunk. Yet then the goal ought to be to reduce these anomalies, not add new ones. Since I don't have a clear picture of what's wanted, I'm also in trouble suggesting any alternative, I'm afraid.

Here is a short summary:

Quoted 8 lines

The main problem is that we need this function somewhere, but there is no good place for it. It is only called on ARM for now but it's not ARM-specific by nature and can be eventually used on other platforms as well. It can't be just dropped because of the effort to support the co-existence of DT and ACPI. It also can't be declared as a static function because it requires the inclusion of <xen/acpi.h> for acpi_disabled define, which leads to build errors[1]. And without ifdef guards it would be a MISRA violation.

Quoted 6 lines

An abridged version of this ought to go in the patch description, I think. This is special, so it needs calling out.

As to the placement - would making an entirely new .c file possibly help? (Then, instead of in the patch description, maybe the special aspect could be put in a code comment at the top of the file.)

Quoted 5 lines

It seems to me creating a new file would be overkill for one small function. I considered moving it to xen/drivers/passthrough/arm/iommu.c to reduce ifdefs but I feared it would suggest that it is arch-specific a bit too strongly. So maybe move it there after all if you think it would be better?

Quoted 3 lines

Well - with "#ifdef CONFIG_ARM" around it's Arm-specific too, isn't it? IOW - either have a proper (even if simple) abstraction, or perhaps indeed put it there (if that's also okay with the Arm maintainers).

I am OK with that

#10 lore raw

PATCH v9 3/8 iommu/arm: iommu_add_dt_pci_sideband_ids phantom handling

In reply to Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Handle phantom functions in iommu_add_dt_pci_sideband_ids(). Each phantom function will have a unique requestor ID (RID)/BDF. On ARM, we need to map/translate the RID/BDF to an AXI stream ID for each phantom function according to the pci-iommu device tree mapping [1]. The RID/BDF -> AXI stream ID mapping in DT could allow phantom devices (i.e. devices with phantom functions) to use different AXI stream IDs based on the (phantom) function.

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

---
v8->v9:
* replace DT_NO_IOMMU with 1

v7->v8:
* no change

v6->v7:
* no change

v5->v6:
* no change

v4->v5:
* no change

v3->v4:
* s/iommu_dt_pci_map_id/dt_map_id/

v2->v3:
* new patch title (was: iommu/arm: iommu_add_dt_pci_device phantom handling)
* rework loop to reduce duplication
* s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/

v1->v2:
* new patch

---
TODO: investigate Jan's comment [2]
[2] https://lore.kernel.org/xen-devel/[email protected]/

---
 xen/drivers/passthrough/device_tree.c | 33 ++++++++++++++++-----------
 1 file changed, 20 insertions(+), 13 deletions(-)
xen/drivers/passthrough/device_tree.c +20 -13
index 37e1437b65..f5850a2607 100644
@@ -169,6 +169,7 @@ int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)
     struct device *dev = pci_to_dev(pdev);
     const struct dt_device_node *np;
     int rc;
+    unsigned int devfn = pdev->devfn;
 
     if ( !iommu_enabled )
         return 1;
@@ -183,21 +184,27 @@ int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)
     if ( !np )
         return -ENODEV;
 
-    /*
-     * According to the Documentation/devicetree/bindings/pci/pci-iommu.txt
-     * from Linux.
-     */
-    rc = dt_map_id(np, PCI_BDF(pdev->bus, pdev->devfn), "iommu-map",
-                   "iommu-map-mask", &iommu_spec.np, iommu_spec.args);
-    if ( rc )
-        return (rc == -ENODEV) ? 1 : rc;
+    do {
+        /*
+         * According to the Documentation/devicetree/bindings/pci/pci-iommu.txt
+         * from Linux.
+         */
+        rc = dt_map_id(np, PCI_BDF(pdev->bus, devfn), "iommu-map",
+                       "iommu-map-mask", &iommu_spec.np, iommu_spec.args);
+        if ( rc )
+            return (rc == -ENODEV) ? 1 : rc;
 
-    rc = iommu_dt_xlate(dev, &iommu_spec, ops);
-    if ( rc < 0 )
-    {
-        iommu_fwspec_free(dev);
-        return -EINVAL;
+        rc = iommu_dt_xlate(dev, &iommu_spec, ops);
+        if ( rc < 0 )
+        {
+            iommu_fwspec_free(dev);
+            return -EINVAL;
+        }
+
+        devfn += pdev->phantom_stride;
     }
+    while ( (devfn != pdev->devfn) &&
+            (PCI_SLOT(devfn) == PCI_SLOT(pdev->devfn)) );
 
     return rc;
 }
Signature · 1 line
2.34.1
Reviewed-by on v9 3/8
#11 lore raw

Re: [PATCH v9 3/8] iommu/arm: iommu_add_dt_pci_sideband_ids phantom handling

Quoted 9 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Handle phantom functions in iommu_add_dt_pci_sideband_ids(). Each phantom function will have a unique requestor ID (RID)/BDF. On ARM, we need to map/translate the RID/BDF to an AXI stream ID for each phantom function according to the pci-iommu device tree mapping [1]. The RID/BDF -> AXI stream ID mapping in DT could allow phantom devices (i.e. devices with phantom functions) to use different AXI stream IDs based on the (phantom) function.

Quoted 3 lines

[1] https://www.kernel.org/doc/Documentation/devicetree/bindings/pci/pci-iommu.txt

Quoted 90 lines

---
v8->v9:
* replace DT_NO_IOMMU with 1

v7->v8:
* no change

v6->v7:
* no change

v5->v6:
* no change

v4->v5:
* no change

v3->v4:
* s/iommu_dt_pci_map_id/dt_map_id/

v2->v3:
* new patch title (was: iommu/arm: iommu_add_dt_pci_device phantom handling)
* rework loop to reduce duplication
* s/iommu_fwspec_free(pci_to_dev(pdev))/iommu_fwspec_free(dev)/

v1->v2:
* new patch

---
TODO: investigate Jan's comment [2]
[2] https://lore.kernel.org/xen-devel/[email protected]/

---
 xen/drivers/passthrough/device_tree.c | 33 ++++++++++++++++-----------
 1 file changed, 20 insertions(+), 13 deletions(-)
xen/drivers/passthrough/device_tree.c +20 -13
index 37e1437b65..f5850a2607 100644
@@ -169,6 +169,7 @@ int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)
     struct device *dev = pci_to_dev(pdev);
     const struct dt_device_node *np;
     int rc;
+    unsigned int devfn = pdev->devfn;
 
     if ( !iommu_enabled )
         return 1;
@@ -183,21 +184,27 @@ int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)
     if ( !np )
         return -ENODEV;
 
-    /*
-     * According to the Documentation/devicetree/bindings/pci/pci-iommu.txt
-     * from Linux.
-     */
-    rc = dt_map_id(np, PCI_BDF(pdev->bus, pdev->devfn), "iommu-map",
-                   "iommu-map-mask", &iommu_spec.np, iommu_spec.args);
-    if ( rc )
-        return (rc == -ENODEV) ? 1 : rc;
+    do {
+        /*
+         * According to the Documentation/devicetree/bindings/pci/pci-iommu.txt
+         * from Linux.
+         */
+        rc = dt_map_id(np, PCI_BDF(pdev->bus, devfn), "iommu-map",
+                       "iommu-map-mask", &iommu_spec.np, iommu_spec.args);
+        if ( rc )
+            return (rc == -ENODEV) ? 1 : rc;
 
-    rc = iommu_dt_xlate(dev, &iommu_spec, ops);
-    if ( rc < 0 )
-    {
-        iommu_fwspec_free(dev);
-        return -EINVAL;
+        rc = iommu_dt_xlate(dev, &iommu_spec, ops);
+        if ( rc < 0 )
+        {
+            iommu_fwspec_free(dev);
+            return -EINVAL;
+        }
+
+        devfn += pdev->phantom_stride;
     }
+    while ( (devfn != pdev->devfn) &&
+            (PCI_SLOT(devfn) == PCI_SLOT(pdev->devfn)) );
 
     return rc;
 }
Signature · 1 line
2.34.1
#12 lore raw

PATCH v9 4/8 xen/arm: smmuv2: Add PCI devices support for SMMUv2

In reply to Mykyta Poturai

From: Oleksandr Andrushchenko <[email protected]>

Implement support for PCI devices in the SMMU driver. Make arm_smmu_master structure to hold a pointer to the device to allow it to hold PCI devices. Trigger iommu-map parsing when new PCI device is added. Add checks to assign/deassign functions to ensure PCI devices are handled correctly. Implement basic quarantining.

All pci devices are automatically assigned to hardware domain if it exists to ensure it can probe them.

TODO:
Implement scratch page quarantining support.

---
v8->v9:
* no change

v7->v8:
* no change

v6->v7:
* use d->pci_lock in arm_smmu_assign_dev()
* remove !is_hardware_domain and pdev->domain == d checks in assign to support future dom0less use case when dom0 is using vPCI
* remove stale todo in dev_get_dev_node
* don't print ""
* remove redundant dt_device_is_protected check
* remove assign/deassing prints
* change assign logic to remove reassign reimplementation
* check if pdev->domain exists before assigning to it
* explain pdev->devfn check
* make reassign check stricter and update comment

v5->v6:
* check for hardware_domain == NULL (dom0less test case)
* locking: assign pdev->domain before list_add()

v4->v5:
* assign device to pdev->domain (usually dom0) by default in add_device() hook
* deassign from hwdom
* rebase on top of ("dynamic node programming using overlay dtbo") series
* remove TODO in comment about device prints
* add TODO regarding locking
* fixup after dropping ("xen/arm: Move is_protected flag to struct device")

v3->v4:
* add new device_is_protected check in add_device hook to match SMMUv3 and IPMMU-VMSA drivers

v2->v3:
* invoke iommu_add_pci_sideband_ids() from add_device hook

v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions (i.e. devfn != pdev->devfn)

downstream->v1:
* wrap unused function in #ifdef 0
* remove the remove_device() stub since it was submitted separately to the list [XEN][PATCH v6 12/19] xen/smmu: Add remove_device callback for smmu_iommu ops
https://lists.xenproject.org/archives/html/xen-devel/2023-05/msg00204.html
* arm_smmu_(de)assign_dev: return error instead of crashing system
* update condition in arm_smmu_reassign_dev
* style fixup
* add && !is_hardware_domain(d) into condition in arm_smmu_assign_dev()

(cherry picked from commit 0c11a7f65f044c26d87d1e27ac6283ef1f9cfb7a from
the downstream branch spider-master from
https://github.com/xen-troops/xen.git)

---
 xen/drivers/passthrough/arm/smmu.c | 190 ++++++++++++++++++++++-------
 1 file changed, 147 insertions(+), 43 deletions(-)
xen/drivers/passthrough/arm/smmu.c +147 -43
index 03d22bce1e..cfddcbb1ad 100644
@@ -132,11 +132,21 @@ enum irqreturn {
 
 typedef enum irqreturn irqreturn_t;
 
-/* Device logger functions
- * TODO: Handle PCI
- */
-#define dev_print(dev, lvl, fmt, ...)						\
-	 printk(lvl "smmu: %s: " fmt, dt_node_full_name(dev_to_dt(dev)), ## __VA_ARGS__)
+/* Device logger functions */
+#ifndef CONFIG_HAS_PCI
+#define dev_print(dev, lvl, fmt, ...)    \
+    printk(lvl "smmu: %s: " fmt, dev_name(dev), ## __VA_ARGS__)
+#else
+#define dev_print(dev, lvl, fmt, ...) ({                                \
+    if ( !dev_is_pci((dev)) )                                           \
+        printk(lvl "smmu: %s: " fmt, dev_name((dev)), ## __VA_ARGS__);  \
+    else                                                                \
+    {                                                                   \
+        struct pci_dev *pdev = dev_to_pci((dev));                       \
+        printk(lvl "smmu: %pp: " fmt, &pdev->sbdf, ## __VA_ARGS__);     \
+    }                                                                   \
+})
+#endif
 
 #define dev_dbg(dev, fmt, ...) dev_print(dev, XENLOG_DEBUG, fmt, ## __VA_ARGS__)
 #define dev_notice(dev, fmt, ...) dev_print(dev, XENLOG_INFO, fmt, ## __VA_ARGS__)
@@ -188,6 +198,7 @@ static void __iomem *devm_ioremap_resource(struct device *dev,
  * Xen: PCI functions
  * TODO: It should be implemented when PCI will be supported
  */
+#if 0 /* unused */
 #define to_pci_dev(dev)	(NULL)
 static inline int pci_for_each_dma_alias(struct pci_dev *pdev,
 					 int (*fn) (struct pci_dev *pdev,
@@ -197,6 +208,7 @@ static inline int pci_for_each_dma_alias(struct pci_dev *pdev,
 	BUG();
 	return 0;
 }
+#endif
 
 /* Xen: misc */
 #define PHYS_MASK_SHIFT		PADDR_BITS
@@ -631,7 +643,7 @@ struct arm_smmu_master_cfg {
 	for (i = 0; idx = (cfg)->smendx[i], (i) < (num); ++(i))
 
 struct arm_smmu_master {
-	struct device_node		*of_node;
+	struct device			*dev;
 	struct rb_node			node;
 	struct arm_smmu_master_cfg	cfg;
 };
@@ -723,7 +735,7 @@ arm_smmu_get_fwspec(struct arm_smmu_master_cfg *cfg)
 {
 	struct arm_smmu_master *master = container_of(cfg,
 			                                      struct arm_smmu_master, cfg);
-	return dev_iommu_fwspec_get(&master->of_node->dev);
+	return dev_iommu_fwspec_get(master->dev);
 }
 
 static void parse_driver_options(struct arm_smmu_device *smmu)
@@ -742,21 +754,11 @@ static void parse_driver_options(struct arm_smmu_device *smmu)
 
 static struct device_node *dev_get_dev_node(struct device *dev)
 {
-#if 0 /* Xen: TODO: Add support for PCI */
-	if (dev_is_pci(dev)) {
-		struct pci_bus *bus = to_pci_dev(dev)->bus;
-
-		while (!pci_is_root_bus(bus))
-			bus = bus->parent;
-		return bus->bridge->parent->of_node;
-	}
-#endif
-
 	return dev->of_node;
 }
 
 static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
-						struct device_node *dev_node)
+						struct device *dev)
 {
 	struct rb_node *node = smmu->masters.rb_node;
 
@@ -765,9 +767,9 @@ static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
 
 		master = container_of(node, struct arm_smmu_master, node);
 
-		if (dev_node < master->of_node)
+		if (dev < master->dev)
 			node = node->rb_left;
-		else if (dev_node > master->of_node)
+		else if (dev > master->dev)
 			node = node->rb_right;
 		else
 			return master;
@@ -802,9 +804,9 @@ static int insert_smmu_master(struct arm_smmu_device *smmu,
 			= container_of(*new, struct arm_smmu_master, node);
 
 		parent = *new;
-		if (master->of_node < this->of_node)
+		if (master->dev < this->dev)
 			new = &((*new)->rb_left);
-		else if (master->of_node > this->of_node)
+		else if (master->dev > this->dev)
 			new = &((*new)->rb_right);
 		else
 			return -EEXIST;
@@ -836,28 +838,30 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,
 	struct arm_smmu_master *master;
 	struct device_node *dev_node = dev_get_dev_node(dev);
 
-	master = find_smmu_master(smmu, dev_node);
+	master = find_smmu_master(smmu, dev);
 	if (master) {
 		dev_err(dev,
-			"rejecting multiple registrations for master device %s\n",
-			dev_node->name);
+			"rejecting multiple registrations for master device\n");
 		return -EBUSY;
 	}
 
 	master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
 	if (!master)
 		return -ENOMEM;
-	master->of_node = dev_node;
+	master->dev = dev;
 
-	/* Xen: Let Xen know that the device is protected by an SMMU */
-	dt_device_set_protected(dev_node);
+	if ( !dev_is_pci(dev) )
+	{
+		/* Xen: Let Xen know that the device is protected by an SMMU */
+		dt_device_set_protected(dev_node);
+	}
 
 	for (i = 0; i < fwspec->num_ids; ++i) {
 		if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
 		     (fwspec->ids[i] >= smmu->num_mapping_groups)) {
 			dev_err(dev,
-				"stream ID for master device %s greater than maximum allowed (%d)\n",
-				dev_node->name, smmu->num_mapping_groups);
+				"SMMU stream ID %d is greater than maximum allowed (%d)\n",
+				fwspec->ids[i], smmu->num_mapping_groups);
 			return -ERANGE;
 		}
 		master->cfg.smendx[i] = INVALID_SMENDX;
@@ -872,7 +876,7 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,
 	struct device_node *dev_node = dev_get_dev_node(dev);
 	int ret;
 
-	master = find_smmu_master(smmu, dev_node);
+	master = find_smmu_master(smmu, dev);
 	if (master == NULL) {
 		dev_err(dev,
 			"No registrations found for master device %s\n",
@@ -884,8 +888,9 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,
 	if (ret)
 		return ret;
 
-	/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */
-	dev_node->is_protected = false;
+	if ( !dev_is_pci(dev) )
+		/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */
+		dev_node->is_protected = false;
 
 	kfree(master);
 	return 0;
@@ -914,6 +919,12 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
 					     fwspec);
 }
 
+/* Forward declaration */
+static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
+			       struct device *dev, u32 flag);
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn,
+				 struct device *dev);
+
 /*
  * The driver which supports generic IOMMU DT bindings must have this
  * callback implemented.
@@ -938,6 +949,23 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)
 {
 	struct arm_smmu_device *smmu;
 	struct iommu_fwspec *fwspec;
+	int ret;
+
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+		int ret;
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ret = iommu_add_pci_sideband_ids(pdev);
+		if ( ret < 0 )
+			iommu_fwspec_free(dev);
+	}
+#endif
 
 	fwspec = dev_iommu_fwspec_get(dev);
 	if (fwspec == NULL)
@@ -947,7 +975,25 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)
 	if (smmu == NULL)
 		return -ENXIO;
 
-	return arm_smmu_dt_add_device_legacy(smmu, dev, fwspec);
+	ret = arm_smmu_dt_add_device_legacy(smmu, dev, fwspec);
+	if ( ret )
+		return ret;
+
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/*
+		 * During PHYSDEVOP_pci_device_add, Xen does not assign the
+		 * device, so we must do it here.
+		 */
+		if ( pdev->domain )
+			ret = arm_smmu_assign_dev(pdev->domain, devfn, dev, 0);
+	}
+#endif
+
+	return ret;
 }
 
 static int arm_smmu_dt_xlate_generic(struct device *dev,
@@ -970,11 +1016,10 @@ static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
 {
 	struct arm_smmu_device *smmu;
 	struct arm_smmu_master *master = NULL;
-	struct device_node *dev_node = dev_get_dev_node(dev);
 
 	spin_lock(&arm_smmu_devices_lock);
 	list_for_each_entry(smmu, &arm_smmu_devices, list) {
-		master = find_smmu_master(smmu, dev_node);
+		master = find_smmu_master(smmu, dev);
 		if (master)
 			break;
 	}
@@ -2066,6 +2111,7 @@ static bool arm_smmu_capable(enum iommu_cap cap)
 }
 #endif
 
+#if 0 /* Not used */
 static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
 {
 	*((u16 *)data) = alias;
@@ -2076,6 +2122,7 @@ static void __arm_smmu_release_pci_iommudata(void *data)
 {
 	kfree(data);
 }
+#endif
 
 static int arm_smmu_add_device(struct device *dev)
 {
@@ -2083,12 +2130,13 @@ static int arm_smmu_add_device(struct device *dev)
 	struct arm_smmu_master_cfg *cfg;
 	struct iommu_group *group;
 	void (*releasefn)(void *data) = NULL;
-	int ret;
 
 	smmu = find_smmu_for_device(dev);
 	if (!smmu)
 		return -ENODEV;
 
+	/* There is no need to distinguish here, thanks to PCI-IOMMU DT bindings */
+#if 0
 	if (dev_is_pci(dev)) {
 		struct pci_dev *pdev = to_pci_dev(dev);
 		struct iommu_fwspec *fwspec;
@@ -2113,10 +2161,12 @@ static int arm_smmu_add_device(struct device *dev)
 				       &fwspec->ids[0]);
 		releasefn = __arm_smmu_release_pci_iommudata;
 		cfg->smmu = smmu;
-	} else {
+	} else
+#endif
+	{
 		struct arm_smmu_master *master;
 
-		master = find_smmu_master(smmu, dev->of_node);
+		master = find_smmu_master(smmu, dev);
 		if (!master) {
 			return -ENODEV;
 		}
@@ -2784,6 +2834,42 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
 			return -ENOMEM;
 	}
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ASSERT(pcidevs_locked());
+
+		write_lock(&pdev->domain->pci_lock);
+		list_del(&pdev->domain_list);
+		write_unlock(&pdev->domain->pci_lock);
+
+		pdev->domain = d;
+
+		write_lock(&d->pci_lock);
+		list_add(&pdev->domain_list, &d->pdev_list);
+		write_unlock(&d->pci_lock);
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+		{
+			struct iommu_domain *domain = dev_iommu_domain(dev);
+			if ( !iommu_quarantine )
+				return 0;
+
+			if ( domain && domain->priv )
+				arm_smmu_deassign_dev(domain->priv->cfg.domain, devfn, dev);
+
+			return 0;
+		}
+	}
+#endif
+
 	if (!dev_iommu_group(dev)) {
 		ret = arm_smmu_add_device(dev);
 		if (ret)
@@ -2833,11 +2919,27 @@ out:
 	return ret;
 }
 
-static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn,
+				 struct device *dev)
 {
 	struct iommu_domain *domain = dev_iommu_domain(dev);
 	struct arm_smmu_xen_domain *xen_domain;
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+			return 0;
+	}
+#endif
+
 	xen_domain = dom_iommu(d)->arch.priv;
 
 	if (!domain || domain->priv->cfg.domain != d) {
@@ -2864,14 +2966,16 @@ static int arm_smmu_reassign_dev(struct domain *s, struct domain *t,
 {
 	int ret = 0;
 
-	/* Don't allow remapping on other domain than hwdom */
-	if ( t && !is_hardware_domain(t) )
+	/* Don't allow remapping on other domain than hwdom
+	 * or dom_io for PCI devices
+	 */
+	if ( t && !is_hardware_domain(t) && (t != dom_io || !dev_is_pci(dev)) )
 		return -EPERM;
 
 	if (t == s)
 		return 0;
 
-	ret = arm_smmu_deassign_dev(s, dev);
+	ret = arm_smmu_deassign_dev(s, devfn, dev);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH v9 4/8] xen/arm: smmuv2: Add PCI devices support for SMMUv2

Quoted 118 lines · Mykyta Poturai

From: Oleksandr Andrushchenko <[email protected]>

Implement support for PCI devices in the SMMU driver. Make arm_smmu_master structure to hold a pointer to the device to allow it to hold PCI devices. Trigger iommu-map parsing when new PCI device is added. Add checks to assign/deassign functions to ensure PCI devices are handled correctly. Implement basic quarantining.

All pci devices are automatically assigned to hardware domain if it exists to ensure it can probe them.

TODO:
Implement scratch page quarantining support.

---
v8->v9:
* no change

v7->v8:
* no change

v6->v7:
* use d->pci_lock in arm_smmu_assign_dev()
* remove !is_hardware_domain and pdev->domain == d checks in assign to support future dom0less use case when dom0 is using vPCI
* remove stale todo in dev_get_dev_node
* don't print ""
* remove redundant dt_device_is_protected check
* remove assign/deassing prints
* change assign logic to remove reassign reimplementation
* check if pdev->domain exists before assigning to it
* explain pdev->devfn check
* make reassign check stricter and update comment

v5->v6:
* check for hardware_domain == NULL (dom0less test case)
* locking: assign pdev->domain before list_add()

v4->v5:
* assign device to pdev->domain (usually dom0) by default in add_device() hook
* deassign from hwdom
* rebase on top of ("dynamic node programming using overlay dtbo") series
* remove TODO in comment about device prints
* add TODO regarding locking
* fixup after dropping ("xen/arm: Move is_protected flag to struct device")

v3->v4:
* add new device_is_protected check in add_device hook to match SMMUv3 and IPMMU-VMSA drivers

v2->v3:
* invoke iommu_add_pci_sideband_ids() from add_device hook

v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions (i.e. devfn != pdev->devfn)

downstream->v1:
* wrap unused function in #ifdef 0
* remove the remove_device() stub since it was submitted separately to the list [XEN][PATCH v6 12/19] xen/smmu: Add remove_device callback for smmu_iommu ops
https://lists.xenproject.org/archives/html/xen-devel/2023-05/msg00204.html
* arm_smmu_(de)assign_dev: return error instead of crashing system
* update condition in arm_smmu_reassign_dev
* style fixup
* add && !is_hardware_domain(d) into condition in arm_smmu_assign_dev()

(cherry picked from commit 0c11a7f65f044c26d87d1e27ac6283ef1f9cfb7a from
the downstream branch spider-master from
https://github.com/xen-troops/xen.git)

---
 xen/drivers/passthrough/arm/smmu.c | 190 ++++++++++++++++++++++-------
 1 file changed, 147 insertions(+), 43 deletions(-)
xen/drivers/passthrough/arm/smmu.c +16 -5
index 03d22bce1e..cfddcbb1ad 100644
@@ -132,11 +132,21 @@ enum irqreturn {
 
 typedef enum irqreturn irqreturn_t;
 
-/* Device logger functions
- * TODO: Handle PCI
- */
-#define dev_print(dev, lvl, fmt, ...)						\
-	 printk(lvl "smmu: %s: " fmt, dt_node_full_name(dev_to_dt(dev)), ## __VA_ARGS__)
+/* Device logger functions */
+#ifndef CONFIG_HAS_PCI
+#define dev_print(dev, lvl, fmt, ...)    \
+    printk(lvl "smmu: %s: " fmt, dev_name(dev), ## __VA_ARGS__)
+#else
+#define dev_print(dev, lvl, fmt, ...) ({                                \
+    if ( !dev_is_pci((dev)) )                                           \
+        printk(lvl "smmu: %s: " fmt, dev_name((dev)), ## __VA_ARGS__);  \
+    else                                                                \
+    {                                                                   \
+        struct pci_dev *pdev = dev_to_pci((dev));                       \
+        printk(lvl "smmu: %pp: " fmt, &pdev->sbdf, ## __VA_ARGS__);     \
+    }                                                                   \
+})
+#endif
 
 #define dev_dbg(dev, fmt, ...) dev_print(dev, XENLOG_DEBUG, fmt, ## __VA_ARGS__)
 #define dev_notice(dev, fmt, ...) dev_print(dev, XENLOG_INFO, fmt, ## __VA_ARGS__)
@@ -188,6 +198,7 @@ static void __iomem *devm_ioremap_resource(struct device *dev,
  * Xen: PCI functions
  * TODO: It should be implemented when PCI will be supported
  */
+#if 0 /* unused */
 #define to_pci_dev(dev)	(NULL)
 static inline int pci_for_each_dma_alias(struct pci_dev *pdev,
 					 int (*fn) (struct pci_dev *pdev,

xen/drivers/passthrough/arm/smmu.c static inline int pci_for_each_dma_alias(struct pci_dev *pdev, · line 211

Quoted 4 lines
xen/drivers/passthrough/arm/smmu.c +1 -0
@@ -197,6 +208,7 @@ static inline int pci_for_each_dma_alias(struct pci_dev *pdev,
 	BUG();
 	return 0;
 }
+#endif

I think we should remove them if they are not used.

Quoted 149 lines
xen/drivers/passthrough/arm/smmu.c +32 -28
 /* Xen: misc */
 #define PHYS_MASK_SHIFT		PADDR_BITS
@@ -631,7 +643,7 @@ struct arm_smmu_master_cfg {
 	for (i = 0; idx = (cfg)->smendx[i], (i) < (num); ++(i))
 
 struct arm_smmu_master {
-	struct device_node		*of_node;
+	struct device			*dev;
 	struct rb_node			node;
 	struct arm_smmu_master_cfg	cfg;
 };
@@ -723,7 +735,7 @@ arm_smmu_get_fwspec(struct arm_smmu_master_cfg *cfg)
 {
 	struct arm_smmu_master *master = container_of(cfg,
 			                                      struct arm_smmu_master, cfg);
-	return dev_iommu_fwspec_get(&master->of_node->dev);
+	return dev_iommu_fwspec_get(master->dev);
 }
 
 static void parse_driver_options(struct arm_smmu_device *smmu)
@@ -742,21 +754,11 @@ static void parse_driver_options(struct arm_smmu_device *smmu)
 
 static struct device_node *dev_get_dev_node(struct device *dev)
 {
-#if 0 /* Xen: TODO: Add support for PCI */
-	if (dev_is_pci(dev)) {
-		struct pci_bus *bus = to_pci_dev(dev)->bus;
-
-		while (!pci_is_root_bus(bus))
-			bus = bus->parent;
-		return bus->bridge->parent->of_node;
-	}
-#endif
-
 	return dev->of_node;
 }
 
 static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
-						struct device_node *dev_node)
+						struct device *dev)
 {
 	struct rb_node *node = smmu->masters.rb_node;
 
@@ -765,9 +767,9 @@ static struct arm_smmu_master *find_smmu_master(struct arm_smmu_device *smmu,
 
 		master = container_of(node, struct arm_smmu_master, node);
 
-		if (dev_node < master->of_node)
+		if (dev < master->dev)
 			node = node->rb_left;
-		else if (dev_node > master->of_node)
+		else if (dev > master->dev)
 			node = node->rb_right;
 		else
 			return master;
@@ -802,9 +804,9 @@ static int insert_smmu_master(struct arm_smmu_device *smmu,
 			= container_of(*new, struct arm_smmu_master, node);
 
 		parent = *new;
-		if (master->of_node < this->of_node)
+		if (master->dev < this->dev)
 			new = &((*new)->rb_left);
-		else if (master->of_node > this->of_node)
+		else if (master->dev > this->dev)
 			new = &((*new)->rb_right);
 		else
 			return -EEXIST;
@@ -836,28 +838,30 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,
 	struct arm_smmu_master *master;
 	struct device_node *dev_node = dev_get_dev_node(dev);
 
-	master = find_smmu_master(smmu, dev_node);
+	master = find_smmu_master(smmu, dev);
 	if (master) {
 		dev_err(dev,
-			"rejecting multiple registrations for master device %s\n",
-			dev_node->name);
+			"rejecting multiple registrations for master device\n");
 		return -EBUSY;
 	}
 
 	master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
 	if (!master)
 		return -ENOMEM;
-	master->of_node = dev_node;
+	master->dev = dev;
 
-	/* Xen: Let Xen know that the device is protected by an SMMU */
-	dt_device_set_protected(dev_node);
+	if ( !dev_is_pci(dev) )
+	{
+		/* Xen: Let Xen know that the device is protected by an SMMU */
+		dt_device_set_protected(dev_node);
+	}
 
 	for (i = 0; i < fwspec->num_ids; ++i) {
 		if (!(smmu->features & ARM_SMMU_FEAT_STREAM_MATCH) &&
 		     (fwspec->ids[i] >= smmu->num_mapping_groups)) {
 			dev_err(dev,
-				"stream ID for master device %s greater than maximum allowed (%d)\n",
-				dev_node->name, smmu->num_mapping_groups);
+				"SMMU stream ID %d is greater than maximum allowed (%d)\n",
+				fwspec->ids[i], smmu->num_mapping_groups);
 			return -ERANGE;
 		}
 		master->cfg.smendx[i] = INVALID_SMENDX;
@@ -872,7 +876,7 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,
 	struct device_node *dev_node = dev_get_dev_node(dev);
 	int ret;
 
-	master = find_smmu_master(smmu, dev_node);
+	master = find_smmu_master(smmu, dev);
 	if (master == NULL) {
 		dev_err(dev,
 			"No registrations found for master device %s\n",
@@ -884,8 +888,9 @@ static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,
 	if (ret)
 		return ret;
 
-	/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */
-	dev_node->is_protected = false;
+	if ( !dev_is_pci(dev) )
+		/* Protected by dt_host_lock and dtdevs_lock as caller holds these locks. */
+		dev_node->is_protected = false;
 
 	kfree(master);
 	return 0;
@@ -914,6 +919,12 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
 					     fwspec);
 }
 
+/* Forward declaration */
+static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
+			       struct device *dev, u32 flag);
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn,
+				 struct device *dev);
+
 /*
  * The driver which supports generic IOMMU DT bindings must have this
  * callback implemented.
@@ -938,6 +949,23 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)
 {
 	struct arm_smmu_device *smmu;
 	struct iommu_fwspec *fwspec;
+	int ret;
+
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev) · line 966

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +10 -0
+		struct pci_dev *pdev = dev_to_pci(dev);
+		int ret;
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ret = iommu_add_pci_sideband_ids(pdev);
+		if ( ret < 0 )
+			iommu_fwspec_free(dev);

Should we return here in case of errors?

Quoted 62 lines
xen/drivers/passthrough/arm/smmu.c +24 -3
+	}
+#endif
 
 	fwspec = dev_iommu_fwspec_get(dev);
 	if (fwspec == NULL)
@@ -947,7 +975,25 @@ static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)
 	if (smmu == NULL)
 		return -ENXIO;
 
-	return arm_smmu_dt_add_device_legacy(smmu, dev, fwspec);
+	ret = arm_smmu_dt_add_device_legacy(smmu, dev, fwspec);
+	if ( ret )
+		return ret;
+
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/*
+		 * During PHYSDEVOP_pci_device_add, Xen does not assign the
+		 * device, so we must do it here.
+		 */
+		if ( pdev->domain )
+			ret = arm_smmu_assign_dev(pdev->domain, devfn, dev, 0);
+	}
+#endif
+
+	return ret;
 }
 
 static int arm_smmu_dt_xlate_generic(struct device *dev,
@@ -970,11 +1016,10 @@ static struct arm_smmu_device *find_smmu_for_device(struct device *dev)
 {
 	struct arm_smmu_device *smmu;
 	struct arm_smmu_master *master = NULL;
-	struct device_node *dev_node = dev_get_dev_node(dev);
 
 	spin_lock(&arm_smmu_devices_lock);
 	list_for_each_entry(smmu, &arm_smmu_devices, list) {
-		master = find_smmu_master(smmu, dev_node);
+		master = find_smmu_master(smmu, dev);
 		if (master)
 			break;
 	}
@@ -2066,6 +2111,7 @@ static bool arm_smmu_capable(enum iommu_cap cap)
 }
 #endif
 
+#if 0 /* Not used */
 static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
 {
 	*((u16 *)data) = alias;
@@ -2076,6 +2122,7 @@ static void __arm_smmu_release_pci_iommudata(void *data)
 {
 	kfree(data);
 }
+#endif
 
 static int arm_smmu_add_device(struct device *dev)
 {

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_add_device(struct device *dev) · line 2138

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +1 -1
@@ -2083,12 +2130,13 @@ static int arm_smmu_add_device(struct device *dev)
 	struct arm_smmu_master_cfg *cfg;
 	struct iommu_group *group;
 	void (*releasefn)(void *data) = NULL;
-	int ret;
 
 	smmu = find_smmu_for_device(dev);
 	if (!smmu)
 		return -ENODEV;
 
+	/* There is no need to distinguish here, thanks to PCI-IOMMU DT bindings */

Let's remove this code then. This driver deviated too much from the Linux original anyway to worry about the ability to update the code

Quoted 114 lines
xen/drivers/passthrough/arm/smmu.c +63 -6
+#if 0
 	if (dev_is_pci(dev)) {
 		struct pci_dev *pdev = to_pci_dev(dev);
 		struct iommu_fwspec *fwspec;
@@ -2113,10 +2161,12 @@ static int arm_smmu_add_device(struct device *dev)
 				       &fwspec->ids[0]);
 		releasefn = __arm_smmu_release_pci_iommudata;
 		cfg->smmu = smmu;
-	} else {
+	} else
+#endif
+	{
 		struct arm_smmu_master *master;
 
-		master = find_smmu_master(smmu, dev->of_node);
+		master = find_smmu_master(smmu, dev);
 		if (!master) {
 			return -ENODEV;
 		}
@@ -2784,6 +2834,42 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
 			return -ENOMEM;
 	}
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ASSERT(pcidevs_locked());
+
+		write_lock(&pdev->domain->pci_lock);
+		list_del(&pdev->domain_list);
+		write_unlock(&pdev->domain->pci_lock);
+
+		pdev->domain = d;
+
+		write_lock(&d->pci_lock);
+		list_add(&pdev->domain_list, &d->pdev_list);
+		write_unlock(&d->pci_lock);
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+		{
+			struct iommu_domain *domain = dev_iommu_domain(dev);
+			if ( !iommu_quarantine )
+				return 0;
+
+			if ( domain && domain->priv )
+				arm_smmu_deassign_dev(domain->priv->cfg.domain, devfn, dev);
+
+			return 0;
+		}
+	}
+#endif
+
 	if (!dev_iommu_group(dev)) {
 		ret = arm_smmu_add_device(dev);
 		if (ret)
@@ -2833,11 +2919,27 @@ out:
 	return ret;
 }
 
-static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn,
+				 struct device *dev)
 {
 	struct iommu_domain *domain = dev_iommu_domain(dev);
 	struct arm_smmu_xen_domain *xen_domain;
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+			return 0;
+	}
+#endif
+
 	xen_domain = dom_iommu(d)->arch.priv;
 
 	if (!domain || domain->priv->cfg.domain != d) {
@@ -2864,14 +2966,16 @@ static int arm_smmu_reassign_dev(struct domain *s, struct domain *t,
 {
 	int ret = 0;
 
-	/* Don't allow remapping on other domain than hwdom */
-	if ( t && !is_hardware_domain(t) )
+	/* Don't allow remapping on other domain than hwdom
+	 * or dom_io for PCI devices
+	 */
+	if ( t && !is_hardware_domain(t) && (t != dom_io || !dev_is_pci(dev)) )
 		return -EPERM;
 
 	if (t == s)
 		return 0;
 
-	ret = arm_smmu_deassign_dev(s, dev);
+	ret = arm_smmu_deassign_dev(s, devfn, dev);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.34.1
#14 lore raw

PATCH v9 5/8 xen/arm: smmuv3: Add PCI devices support for SMMUv3

In reply to Mykyta Poturai

From: Rahul Singh <[email protected]>

Implement support for PCI devices in the SMMU driver. Trigger iommu-map parsing when new PCI device is added. Add checks to assign/deassign functions to ensure PCI devices are handled correctly. Implement basic quarantining.

All pci devices are automatically assigned to hardware domain if it exists to ensure it can probe them.

TODO:
Implement scratch page quarantining support.

---
v8->v9:
* no change

v7->v8:
* no change

v6->v7:
* address TODO: use d->pci_lock in arm_smmu_assign_dev()
* remove !is_hardware_domain and pdev->domain == d checks in assign to support future dom0less use case when dom0 is using vPCI
* check if pdev->domain exists before assigning to it
* don't print ""
* change assign logic to remove reassign reimplementation
* explain pdev->devfn check
* make reassign check stricter and update comment

v5->v6:
* check for hardware_domain == NULL (dom0less test case)
* locking: assign pdev->domain before list_add()

v4->v5:
* deassign from hwdom
* add TODO regarding locking
* fixup after dropping ("xen/arm: Move is_protected flag to struct device")

v3->v4:
* no change

v2->v3:
* rebase
* invoke iommu_add_pci_sideband_ids() from add_device hook

v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions (i.e. devfn != pdev->devfn)

downstream->v1:
* rebase
* move 2 replacements of s/dt_device_set_protected(dev_to_dt(dev))/device_set_protected(dev)/ from this commit to ("xen/arm: Move is_protected flag to struct device")
so as to not break ability to bisect
* adjust patch title (remove stray space)
* arm_smmu_(de)assign_dev: return error instead of crashing system
* remove arm_smmu_remove_device() stub
* update condition in arm_smmu_reassign_dev
* style fixup

(cherry picked from commit 7ed6c3ab250d899fe6e893a514278e406a2893e8 from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)

---
 xen/drivers/passthrough/arm/smmu-v3.c | 117 +++++++++++++++++++++++---
 1 file changed, 106 insertions(+), 11 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.c +106 -11
index cee5724022..9c7c13f800 100644
@@ -1467,14 +1467,35 @@ static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
 }
 /* Forward declaration */
 static struct arm_smmu_device *arm_smmu_get_by_dev(const struct device *dev);
+static int arm_smmu_assign_dev(struct domain *d, u8 devfn, struct device *dev,
+			       u32 flag);
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn,
+				 struct device *dev);
 
 static int arm_smmu_add_device(u8 devfn, struct device *dev)
 {
 	int i, ret;
 	struct arm_smmu_device *smmu;
 	struct arm_smmu_master *master;
-	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+	struct iommu_fwspec *fwspec;
+
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+		int ret;
+				
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ret = iommu_add_pci_sideband_ids(pdev);
+		if ( ret < 0 )
+			iommu_fwspec_free(dev);
+	}
+#endif
 
+	fwspec = dev_iommu_fwspec_get(dev);
 	if (!fwspec)
 		return -ENODEV;
 
@@ -1519,17 +1540,38 @@ static int arm_smmu_add_device(u8 devfn, struct device *dev)
 	 */
 	arm_smmu_enable_pasid(master);
 
-	if (dt_device_is_protected(dev_to_dt(dev))) {
-		dev_err(dev, "Already added to SMMUv3\n");
-		return -EEXIST;
-	}
+	if ( !dev_is_pci(dev) )
+	{
+		if (dt_device_is_protected(dev_to_dt(dev))) {
+			dev_err(dev, "Already added to SMMUv3\n");
+			return -EEXIST;
+		}
 
-	/* Let Xen know that the master device is protected by an IOMMU. */
-	dt_device_set_protected(dev_to_dt(dev));
+		/* Let Xen know that the master device is protected by an IOMMU. */
+		dt_device_set_protected(dev_to_dt(dev));
+	}
 
 	dev_info(dev, "Added master device (SMMUv3 %s StreamIds %u)\n",
 			dev_name(fwspec->iommu_dev), fwspec->num_ids);
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/*
+		 * During PHYSDEVOP_pci_device_add, Xen does not assign the
+		 * device, so we must do it here.
+		 */
+		if ( pdev->domain )
+		{
+			ret = arm_smmu_assign_dev(pdev->domain, devfn, dev, 0);
+			if (ret)
+				goto err_free_master;
+		}
+	}
+#endif
+
 	return 0;
 
 err_free_master:
@@ -2622,6 +2664,42 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
 	struct arm_smmu_domain *smmu_domain;
 	struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ASSERT(pcidevs_locked());
+
+		write_lock(&pdev->domain->pci_lock);
+		list_del(&pdev->domain_list);
+		write_unlock(&pdev->domain->pci_lock);
+
+		pdev->domain = d;
+
+		write_lock(&d->pci_lock);
+		list_add(&pdev->domain_list, &d->pdev_list);
+		write_unlock(&d->pci_lock);
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+		{
+			struct arm_smmu_master *master = dev_iommu_priv_get(dev);
+			if ( !iommu_quarantine )
+				return 0;
+
+			if ( master && master->domain )
+				arm_smmu_deassign_dev(master->domain->d, devfn, dev);
+
+			return 0;
+		}
+	}
+#endif
+
 	spin_lock(&xen_domain->lock);
 
 	/*
@@ -2655,7 +2733,7 @@ out:
 	return ret;
 }
 
-static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn, struct device *dev)
 {
 	struct iommu_domain *io_domain = arm_smmu_get_domain(d, dev);
 	struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
@@ -2667,6 +2745,21 @@ static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
 		return -ESRCH;
 	}
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+			return 0;
+	}
+#endif
+
 	spin_lock(&xen_domain->lock);
 
 	arm_smmu_detach_dev(master);
@@ -2685,14 +2778,16 @@ static int arm_smmu_reassign_dev(struct domain *s, struct domain *t,
 {
 	int ret = 0;
 
-	/* Don't allow remapping on other domain than hwdom */
-	if ( t && !is_hardware_domain(t) )
+	/* Don't allow remapping on other domain than hwdom
+	 * or dom_io for PCI devices
+	 */
+	if ( t && !is_hardware_domain(t) && (t != dom_io || !dev_is_pci(dev)) )
 		return -EPERM;
 
 	if (t == s)
 		return 0;
 
-	ret = arm_smmu_deassign_dev(s, dev);
+	ret = arm_smmu_deassign_dev(s, devfn, dev);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.34.1
#15 lore raw

Re: [PATCH v9 5/8] xen/arm: smmuv3: Add PCI devices support for SMMUv3

Quoted 95 lines · Mykyta Poturai

From: Rahul Singh <[email protected]>

Implement support for PCI devices in the SMMU driver. Trigger iommu-map parsing when new PCI device is added. Add checks to assign/deassign functions to ensure PCI devices are handled correctly. Implement basic quarantining.

All pci devices are automatically assigned to hardware domain if it exists to ensure it can probe them.

TODO:
Implement scratch page quarantining support.

---
v8->v9:
* no change

v7->v8:
* no change

v6->v7:
* address TODO: use d->pci_lock in arm_smmu_assign_dev()
* remove !is_hardware_domain and pdev->domain == d checks in assign to support future dom0less use case when dom0 is using vPCI
* check if pdev->domain exists before assigning to it
* don't print ""
* change assign logic to remove reassign reimplementation
* explain pdev->devfn check
* make reassign check stricter and update comment

v5->v6:
* check for hardware_domain == NULL (dom0less test case)
* locking: assign pdev->domain before list_add()

v4->v5:
* deassign from hwdom
* add TODO regarding locking
* fixup after dropping ("xen/arm: Move is_protected flag to struct device")

v3->v4:
* no change

v2->v3:
* rebase
* invoke iommu_add_pci_sideband_ids() from add_device hook

v1->v2:
* ignore add_device/assign_device/reassign_device calls for phantom functions (i.e. devfn != pdev->devfn)

downstream->v1:
* rebase
* move 2 replacements of s/dt_device_set_protected(dev_to_dt(dev))/device_set_protected(dev)/ from this commit to ("xen/arm: Move is_protected flag to struct device")
so as to not break ability to bisect
* adjust patch title (remove stray space)
* arm_smmu_(de)assign_dev: return error instead of crashing system
* remove arm_smmu_remove_device() stub
* update condition in arm_smmu_reassign_dev
* style fixup

(cherry picked from commit 7ed6c3ab250d899fe6e893a514278e406a2893e8 from
the downstream branch poc/pci-passthrough from
https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc.git)

---
 xen/drivers/passthrough/arm/smmu-v3.c | 117 +++++++++++++++++++++++---
 1 file changed, 106 insertions(+), 11 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.c +9 -1
index cee5724022..9c7c13f800 100644
@@ -1467,14 +1467,35 @@ static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
 }
 /* Forward declaration */
 static struct arm_smmu_device *arm_smmu_get_by_dev(const struct device *dev);
+static int arm_smmu_assign_dev(struct domain *d, u8 devfn, struct device *dev,
+			       u32 flag);
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn,
+				 struct device *dev);
 
 static int arm_smmu_add_device(u8 devfn, struct device *dev)
 {
 	int i, ret;
 	struct arm_smmu_device *smmu;
 	struct arm_smmu_master *master;
-	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+	struct iommu_fwspec *fwspec;
+
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{

xen/drivers/passthrough/arm/smmu-v3.c static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) · line 1494

Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10 -0
+		struct pci_dev *pdev = dev_to_pci(dev);
+		int ret;
+				
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ret = iommu_add_pci_sideband_ids(pdev);
+		if ( ret < 0 )
+			iommu_fwspec_free(dev);

Do we need to return on error?

Quoted 149 lines
xen/drivers/passthrough/arm/smmu-v3.c +87 -10
+	}
+#endif
 
+	fwspec = dev_iommu_fwspec_get(dev);
 	if (!fwspec)
 		return -ENODEV;
 
@@ -1519,17 +1540,38 @@ static int arm_smmu_add_device(u8 devfn, struct device *dev)
 	 */
 	arm_smmu_enable_pasid(master);
 
-	if (dt_device_is_protected(dev_to_dt(dev))) {
-		dev_err(dev, "Already added to SMMUv3\n");
-		return -EEXIST;
-	}
+	if ( !dev_is_pci(dev) )
+	{
+		if (dt_device_is_protected(dev_to_dt(dev))) {
+			dev_err(dev, "Already added to SMMUv3\n");
+			return -EEXIST;
+		}
 
-	/* Let Xen know that the master device is protected by an IOMMU. */
-	dt_device_set_protected(dev_to_dt(dev));
+		/* Let Xen know that the master device is protected by an IOMMU. */
+		dt_device_set_protected(dev_to_dt(dev));
+	}
 
 	dev_info(dev, "Added master device (SMMUv3 %s StreamIds %u)\n",
 			dev_name(fwspec->iommu_dev), fwspec->num_ids);
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/*
+		 * During PHYSDEVOP_pci_device_add, Xen does not assign the
+		 * device, so we must do it here.
+		 */
+		if ( pdev->domain )
+		{
+			ret = arm_smmu_assign_dev(pdev->domain, devfn, dev, 0);
+			if (ret)
+				goto err_free_master;
+		}
+	}
+#endif
+
 	return 0;
 
 err_free_master:
@@ -2622,6 +2664,42 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
 	struct arm_smmu_domain *smmu_domain;
 	struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		ASSERT(pcidevs_locked());
+
+		write_lock(&pdev->domain->pci_lock);
+		list_del(&pdev->domain_list);
+		write_unlock(&pdev->domain->pci_lock);
+
+		pdev->domain = d;
+
+		write_lock(&d->pci_lock);
+		list_add(&pdev->domain_list, &d->pdev_list);
+		write_unlock(&d->pci_lock);
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+		{
+			struct arm_smmu_master *master = dev_iommu_priv_get(dev);
+			if ( !iommu_quarantine )
+				return 0;
+
+			if ( master && master->domain )
+				arm_smmu_deassign_dev(master->domain->d, devfn, dev);
+
+			return 0;
+		}
+	}
+#endif
+
 	spin_lock(&xen_domain->lock);
 
 	/*
@@ -2655,7 +2733,7 @@ out:
 	return ret;
 }
 
-static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
+static int arm_smmu_deassign_dev(struct domain *d, uint8_t devfn, struct device *dev)
 {
 	struct iommu_domain *io_domain = arm_smmu_get_domain(d, dev);
 	struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
@@ -2667,6 +2745,21 @@ static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
 		return -ESRCH;
 	}
 
+#ifdef CONFIG_HAS_PCI
+	if ( dev_is_pci(dev) )
+	{
+		struct pci_dev *pdev = dev_to_pci(dev);
+
+		/* Ignore calls for phantom functions */
+		if ( devfn != pdev->devfn )
+			return 0;
+
+		/* dom_io is used as a sentinel for quarantined devices */
+		if ( d == dom_io )
+			return 0;
+	}
+#endif
+
 	spin_lock(&xen_domain->lock);
 
 	arm_smmu_detach_dev(master);
@@ -2685,14 +2778,16 @@ static int arm_smmu_reassign_dev(struct domain *s, struct domain *t,
 {
 	int ret = 0;
 
-	/* Don't allow remapping on other domain than hwdom */
-	if ( t && !is_hardware_domain(t) )
+	/* Don't allow remapping on other domain than hwdom
+	 * or dom_io for PCI devices
+	 */
+	if ( t && !is_hardware_domain(t) && (t != dom_io || !dev_is_pci(dev)) )
 		return -EPERM;
 
 	if (t == s)
 		return 0;
 
-	ret = arm_smmu_deassign_dev(s, dev);
+	ret = arm_smmu_deassign_dev(s, devfn, dev);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.34.1
#16 lore raw

PATCH v9 6/8 xen/arm: Fix mapping for PCI bridge mmio region

In reply to Mykyta Poturai

From: Rahul Singh <[email protected]>

Current code skip the mapping for PCI bridge MMIO region to dom0 when pci_passthrough_enabled flag is set. Mapping should be skip when has_vpci(d) is enabled for the domain, as we need to skip the mapping only when VPCI handler are registered for ECAM.

---
This patch was originally picked up from [1]

v8->v9:
* no change

v7->v8:
* no change

v6->v7:
* add Julien's A-b

v5->v6:
* drop unrelated change in xen/arch/arm/domain_build.c:handle_linux_pci_domain()

v4->v5:
* new patch

changes since picking up from [1]:
* rebase on top of "dynamic node programming using overlay dtbo" series
* replace !is_pci_passthrough_enabled() check with !IS_ENABLED(CONFIG_HAS_PCI) instead of removing

[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg00483.html

---
 xen/arch/arm/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/device.c +1 -1
index 5610cddcba..25847d60ee 100644
@@ -268,7 +268,7 @@ int handle_device(struct domain *d, struct dt_device_node *dev, p2m_type_t p2mt,
         .d = d,
         .p2mt = p2mt,
         .skip_mapping = !own_device ||
-                        (is_pci_passthrough_enabled() &&
+                        (has_vpci(d) &&
                         (device_get_class(dev) == DEVICE_PCI_HOSTBRIDGE)),
         .iomem_ranges = iomem_ranges,
         .irq_ranges = irq_ranges
Signature · 1 line
2.34.1
#17 lore raw

PATCH v9 7/8 xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

In reply to Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

Enable pci_init() for initializing Xen's internal PCI subsystem, and allow PHYSDEVOP_pci_device_add when pci-passthrough is disabled.

---
hmm. Since
dec9e02f3190 ("xen: avoid generation of stub <asm/pci.h> header")
Should we also move is_pci_passthrough_enabled() back to xen/arch/arm/include/asm/pci.h ? Not sure if PPC/RISC-V will plan on using this check.

v8-v9:
* move iommu_enabled check inside is_pci_passthrough_enabled()

v7->v8:
* bring back x86 definition of is_pci_passthrough_enabled()

v6->v7:
* remove x86 definition of is_pci_passthrough_enabled()
* update comments
* make pci_physdev_op checks stricter

v5->v6:
* new patch - this effectively replaces
("Revert "xen/arm: Add cmdline boot option "pci-passthrough = <boolean>""")

---
 xen/arch/arm/domain_build.c    |  2 +-
 xen/arch/arm/include/asm/pci.h |  5 +----
 xen/arch/arm/pci/pci.c         | 11 ++++++++++-
 xen/arch/x86/include/asm/pci.h |  2 +-
 xen/drivers/pci/physdev.c      |  4 ++--
 xen/include/xen/pci.h          |  2 +-
 6 files changed, 16 insertions(+), 10 deletions(-)
xen/arch/arm/domain_build.c +1 -1
index 7b47abade1..fbd6db9438 100644
@@ -526,7 +526,7 @@ static int __init handle_linux_pci_domain(struct kernel_info *kinfo,
     uint16_t segment;
     int res;
 
-    if ( !is_pci_passthrough_enabled() )
+    if ( !is_pci_passthrough_enabled(false) )
         return 0;
 
     if ( !dt_device_type_is_equal(node, "pci") )
xen/arch/arm/include/asm/pci.h +1 -4
index 7f77226c9b..3ae85b4666 100644
@@ -111,10 +111,7 @@ pci_find_host_bridge_node(const struct pci_dev *pdev);
 int pci_get_host_bridge_segment(const struct dt_device_node *node,
                                 uint16_t *segment);
 
-static always_inline bool is_pci_passthrough_enabled(void)
-{
-    return pci_passthrough_enabled;
-}
+bool is_pci_passthrough_enabled(bool dom0);
 
 void arch_pci_init_pdev(struct pci_dev *pdev);
 
xen/arch/arm/pci/pci.c +10 -1
index 78b97beaef..79bb8728a4 100644
@@ -16,9 +16,18 @@
 #include <xen/device_tree.h>
 #include <xen/errno.h>
 #include <xen/init.h>
+#include <xen/iommu.h>
 #include <xen/param.h>
 #include <xen/pci.h>
 
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;
+
+    return pci_passthrough_enabled;
+}
+
 /*
  * PIRQ event channels are not supported on Arm, so nothing to do.
  */
@@ -85,7 +94,7 @@ static int __init pci_init(void)
      * Enable PCI passthrough when has been enabled explicitly
      * (pci-passthrough=on).
      */
-    if ( !pci_passthrough_enabled )
+    if ( !is_pci_passthrough_enabled(true) )
         return 0;
 
     pci_segments_init();
xen/arch/x86/include/asm/pci.h +1 -1
index fd5480d67d..bffeaa507d 100644
@@ -50,7 +50,7 @@ extern int pci_mmcfg_config_num;
 extern struct acpi_mcfg_allocation *pci_mmcfg_config;
 
 /* Unlike ARM, PCI passthrough is always enabled for x86. */
-static always_inline bool is_pci_passthrough_enabled(void)
+static always_inline bool is_pci_passthrough_enabled(__maybe_unused bool dom0)
 {
     return true;
 }
xen/drivers/pci/physdev.c +2 -2
index 0161a85e1e..18448b94b3 100644
@@ -19,7 +19,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         struct pci_dev_info pdev_info;
         nodeid_t node = NUMA_NO_NODE;
 
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
             return -EOPNOTSUPP;
 
         ret = -EFAULT;
@@ -57,7 +57,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_pci_device_remove: {
         struct physdev_pci_device dev;
 
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
             return -EOPNOTSUPP;
 
         ret = -EFAULT;
xen/include/xen/pci.h +1 -1
index f784e91160..c4a49cf584 100644
@@ -74,7 +74,7 @@ typedef union {
 
 struct arch_pci_dev { };
 
-static inline bool is_pci_passthrough_enabled(void)
+static inline bool is_pci_passthrough_enabled(__maybe_unused bool dom0)
 {
     return false;
 }
Signature · 1 line
2.34.1
#18 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 4 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +0 -0
@@ -16,9 +16,18 @@
 #include <xen/device_tree.h>

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

Quoted 10 lines
xen/arch/arm/pci/pci.c +5 -0
 #include <xen/errno.h>
 #include <xen/init.h>
+#include <xen/iommu.h>
 #include <xen/param.h>
 #include <xen/pci.h>
 
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

xen/arch/arm/pci/pci.c line 29

Quoted 2 lines
xen/arch/arm/pci/pci.c +2 -0
+    return pci_passthrough_enabled;
+}

Personally I also view it as undesirable that the global pci_passthrough_enabled is evaluated twice in this function. Better might be e.g.

bool is_pci_passthrough_enabled(bool dom0)

{
    if ( pci_passthrough_enabled )
        return true;

    return dom0 && iommu_enabled;
}

Yet then I'm not a maintainer of this code.

xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 97

Quoted 6 lines
xen/arch/arm/pci/pci.c +1 -1
@@ -85,7 +94,7 @@ static int __init pci_init(void)
      * Enable PCI passthrough when has been enabled explicitly
      * (pci-passthrough=on).
      */
-    if ( !pci_passthrough_enabled )
+    if ( !is_pci_passthrough_enabled(true) )

There's no Dom0 in sight anywhere here, is there? How can you pass true as argument for the "dom0" parameter?

xen/arch/x86/include/asm/pci.h extern int pci_mmcfg_config_num; · line 53

Quoted 8 lines
xen/arch/x86/include/asm/pci.h +1 -1
@@ -50,7 +50,7 @@ extern int pci_mmcfg_config_num;
 extern struct acpi_mcfg_allocation *pci_mmcfg_config;
 
 /* Unlike ARM, PCI passthrough is always enabled for x86. */
-static always_inline bool is_pci_passthrough_enabled(void)
+static always_inline bool is_pci_passthrough_enabled(__maybe_unused bool dom0)

Function parmeters don't need such annotation.

xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 23

Quoted 9 lines
xen/drivers/pci/physdev.c +1 -1
@@ -19,7 +19,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         struct pci_dev_info pdev_info;
         nodeid_t node = NUMA_NO_NODE;
 
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
             return -EOPNOTSUPP;

Seeing the function's parameter name, how do you know it's Dom0 calling here?

Jan

#19 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +1 -0
@@ -16,9 +16,18 @@
  #include <xen/device_tree.h>
  #include <xen/errno.h>
  #include <xen/init.h>
+#include <xen/iommu.h>
  #include <xen/param.h>
  #include <xen/pci.h>
 
Quoted 8 lines

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

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

I am afraid I don't understand your concern. It still checks if PCI passthrough is enabled. With just the change that ARM needs some extra logic for Dom0 PCI to work properly. Maybe change the parameter name to something like "for_pci_hwdom"?

Quoted 8 lines

xen/arch/arm/pci/pci.c line 29

Quoted 2 lines
xen/arch/arm/pci/pci.c +2 -0
+    return pci_passthrough_enabled;
+}

Personally I also view it as undesirable that the global pci_passthrough_enabled is evaluated twice in this function. Better might be e.g.

bool is_pci_passthrough_enabled(bool dom0)

Quoted 9 lines
{
     if ( pci_passthrough_enabled )
         return true;

     return dom0 && iommu_enabled;
}

Yet then I'm not a maintainer of this code.

Agree, will change in the next version.

Quoted 10 lines

xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 97

Quoted 6 lines
xen/arch/arm/pci/pci.c +1 -1
@@ -85,7 +94,7 @@ static int __init pci_init(void)
       * Enable PCI passthrough when has been enabled explicitly
       * (pci-passthrough=on).
       */
-    if ( !pci_passthrough_enabled )
+    if ( !is_pci_passthrough_enabled(true) )

There's no Dom0 in sight anywhere here, is there? How can you pass true as argument for the "dom0" parameter?

This should be read as "is pci passthrough enabled for Dom0?" and if it is we definitely need to do a PCI init.

I've also done some investigations on possible ways to remove the Dom0/other domains distinction, but I'm afraid this is the most reasonable way to make PCI functional on Dom0 without explicitly enabling PCI passthrough.

SMMU is configured to trigger a fault on all transactions by default and we can't statically map PCI devices to Dom0, so the only other way is to put PCI in full passthrough mode, which I think is not safe enough. And we also can't drop this patch as it was directly requested by Julien here [1].

Quoted 11 lines

xen/arch/x86/include/asm/pci.h extern int pci_mmcfg_config_num; · line 53

Quoted 8 lines
xen/arch/x86/include/asm/pci.h +1 -1
@@ -50,7 +50,7 @@ extern int pci_mmcfg_config_num;
  extern struct acpi_mcfg_allocation *pci_mmcfg_config;
 
  /* Unlike ARM, PCI passthrough is always enabled for x86. */
-static always_inline bool is_pci_passthrough_enabled(void)
+static always_inline bool is_pci_passthrough_enabled(__maybe_unused bool dom0)

Function parmeters don't need such annotation.

Got it.

Quoted 6 lines
Quoted 6 lines
xen/drivers/pci/physdev.c +0 -0
@@ -19,7 +19,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
          struct pci_dev_info pdev_info;
          nodeid_t node = NUMA_NO_NODE;
 
Quoted 8 lines

xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 23

Quoted 3 lines
xen/drivers/pci/physdev.c +1 -1
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
              return -EOPNOTSUPP;

Seeing the function's parameter name, how do you know it's Dom0 calling here?

Jan

Is this a functional or naming concern? If it is about naming then can it also be solved by renaming the parameter?

Regarding functional issues, I have assumed that only hwdom can make physdev operations, but after checking it, this assumption seems to be correct on x86 but wrong on Arm.
I expected there would be a check in do_arm_physdev_op() or somewhere near it, similar to how it is done in x86, but there are none. I'm not sure if it is intentional or by mistake, I think it needs some clarification by Arm folks.

[1]
https://patchew.org/Xen/[email protected]/#[email protected]

Signature · 1 line
Mykyta
#20 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 21 lines · Mykyta Poturai
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +1 -0
@@ -16,9 +16,18 @@
  #include <xen/device_tree.h>
  #include <xen/errno.h>
  #include <xen/init.h>
+#include <xen/iommu.h>
  #include <xen/param.h>
  #include <xen/pci.h>
 
Quoted 8 lines

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

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

Quoted 3 lines

I am afraid I don't understand your concern. It still checks if PCI passthrough is enabled. With just the change that ARM needs some extra logic for Dom0 PCI to work properly.

Conceptually there's no such thing as "pass through" for Dom0. Hence the name of the function itself isn't correctly reflecting what it's checking for. iommu_enabled is a prereq for pass-through to be enabled, but it doesn't imply that's necessarily the case.

Quoted 2 lines

Maybe change the parameter name to
something like "for_pci_hwdom"?

That may help below, yes. But not here.

Quoted 37 lines
Quoted 9 lines

xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 97

Quoted 6 lines
xen/arch/arm/pci/pci.c +1 -1
 > @@ -85,7 +94,7 @@ static int __init pci_init(void)
       * Enable PCI passthrough when has been enabled explicitly
       * (pci-passthrough=on).
       */
-    if ( !pci_passthrough_enabled )
+    if ( !is_pci_passthrough_enabled(true) )

There's no Dom0 in sight anywhere here, is there? How can you pass true as argument for the "dom0" parameter?

This should be read as "is pci passthrough enabled for Dom0?" and if it is we definitely need to do a PCI init.

I've also done some investigations on possible ways to remove the Dom0/other domains distinction, but I'm afraid this is the most reasonable way to make PCI functional on Dom0 without explicitly enabling PCI passthrough.

SMMU is configured to trigger a fault on all transactions by default and we can't statically map PCI devices to Dom0, so the only other way is to put PCI in full passthrough mode, which I think is not safe enough. And we also can't drop this patch as it was directly requested by Julien here [1].

Quoted 6 lines
Quoted 6 lines
xen/drivers/pci/physdev.c +0 -0
@@ -19,7 +19,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
          struct pci_dev_info pdev_info;
          nodeid_t node = NUMA_NO_NODE;
 
Quoted 6 lines

xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 23

Quoted 3 lines
xen/drivers/pci/physdev.c +1 -1
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
              return -EOPNOTSUPP;

Seeing the function's parameter name, how do you know it's Dom0 calling here?

Quoted 2 lines

Is this a functional or naming concern? If it is about naming then can it also be solved by renaming the parameter?

The renaming suggested above would resolve this, yes. Whether "for_pci_hwdom" or anything alike is a good parameter name is a different question.

Quoted 7 lines

Regarding functional issues, I have assumed that only hwdom can make physdev operations, but after checking it, this assumption seems to be correct on x86 but wrong on Arm.
I expected there would be a check in do_arm_physdev_op() or somewhere near it, similar to how it is done in x86, but there are none. I'm not sure if it is intentional or by mistake, I think it needs some clarification by Arm folks.

Hmm, looking at x86'es do_physdev_op() I fear I can't see such a check there either. And indeed there are certain PHYSDEVOP_* which DomU-s may also make use of.

Jan

#21 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 22 lines · Jan Beulich
Quoted 21 lines · Mykyta Poturai
Quoted 19 lines · Jan Beulich
Quoted 4 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +0 -0
@@ -16,9 +16,18 @@
   #include <xen/device_tree.h>

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

Quoted 10 lines
xen/arch/arm/pci/pci.c +5 -0
   #include <xen/errno.h>
   #include <xen/init.h>
+#include <xen/iommu.h>
   #include <xen/param.h>
   #include <xen/pci.h>
 
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

Quoted 8 lines
Quoted 3 lines

I am afraid I don't understand your concern. It still checks if PCI passthrough is enabled. With just the change that ARM needs some extra logic for Dom0 PCI to work properly.

Conceptually there's no such thing as "pass through" for Dom0. Hence the name of the function itself isn't correctly reflecting what it's checking for. iommu_enabled is a prereq for pass-through to be enabled, but it doesn't imply that's necessarily the case.

Okay, now I think I get it. Yes from that point of view it seems kind of wrong. Maybe use a separate function then, something like "hwdom_has_vpci"?

Quoted 56 lines
Quoted 2 lines

Maybe change the parameter name to
something like "for_pci_hwdom"?

That may help below, yes. But not here.

Quoted 37 lines
Quoted 9 lines

xen/arch/arm/pci/pci.c static int __init pci_init(void) · line 97

Quoted 6 lines
xen/arch/arm/pci/pci.c +1 -1
 > @@ -85,7 +94,7 @@ static int __init pci_init(void)
        * Enable PCI passthrough when has been enabled explicitly
        * (pci-passthrough=on).
        */
-    if ( !pci_passthrough_enabled )
+    if ( !is_pci_passthrough_enabled(true) )

There's no Dom0 in sight anywhere here, is there? How can you pass true as argument for the "dom0" parameter?

This should be read as "is pci passthrough enabled for Dom0?" and if it is we definitely need to do a PCI init.

I've also done some investigations on possible ways to remove the Dom0/other domains distinction, but I'm afraid this is the most reasonable way to make PCI functional on Dom0 without explicitly enabling PCI passthrough.

SMMU is configured to trigger a fault on all transactions by default and we can't statically map PCI devices to Dom0, so the only other way is to put PCI in full passthrough mode, which I think is not safe enough. And we also can't drop this patch as it was directly requested by Julien here [1].

Quoted 6 lines
Quoted 6 lines
xen/drivers/pci/physdev.c +0 -0
@@ -19,7 +19,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
           struct pci_dev_info pdev_info;
           nodeid_t node = NUMA_NO_NODE;
 
Quoted 6 lines

xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 23

Quoted 3 lines
xen/drivers/pci/physdev.c +1 -1
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
               return -EOPNOTSUPP;

Seeing the function's parameter name, how do you know it's Dom0 calling here?

Quoted 2 lines

Is this a functional or naming concern? If it is about naming then can it also be solved by renaming the parameter?

The renaming suggested above would resolve this, yes. Whether "for_pci_hwdom" or anything alike is a good parameter name is a different question.

Quoted 7 lines

Regarding functional issues, I have assumed that only hwdom can make physdev operations, but after checking it, this assumption seems to be correct on x86 but wrong on Arm.
I expected there would be a check in do_arm_physdev_op() or somewhere near it, similar to how it is done in x86, but there are none. I'm not sure if it is intentional or by mistake, I think it needs some clarification by Arm folks.

Quoted 5 lines

Hmm, looking at x86'es do_physdev_op() I fear I can't see such a check there either. And indeed there are certain PHYSDEVOP_* which DomU-s may also make use of.

Jan

It is one level above in hvm_physdev_op()

     case PHYSDEVOP_setup_gsi:
     case PHYSDEVOP_pci_mmcfg_reserved:
     case PHYSDEVOP_pci_device_add:
     case PHYSDEVOP_pci_device_remove:
     case PHYSDEVOP_pci_device_reset:
     case PHYSDEVOP_dbgp_op:
         if ( !is_hardware_domain(currd) )
             return -ENOSYS;
         break;
Signature · 1 line
Mykyta
#22 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 37 lines · Mykyta Poturai
Quoted 30 lines · Jan Beulich
Quoted 15 lines · Mykyta Poturai
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Mykyta Poturai
xen/drivers/pci/physdev.c +0 -0
@@ -19,7 +19,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
           struct pci_dev_info pdev_info;
           nodeid_t node = NUMA_NO_NODE;
 
Quoted 6 lines

xen/drivers/pci/physdev.c ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 23

Quoted 3 lines
xen/drivers/pci/physdev.c +1 -1
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
               return -EOPNOTSUPP;

Seeing the function's parameter name, how do you know it's Dom0 calling here?

Quoted 2 lines

Is this a functional or naming concern? If it is about naming then can it also be solved by renaming the parameter?

The renaming suggested above would resolve this, yes. Whether "for_pci_hwdom" or anything alike is a good parameter name is a different question.

Quoted 7 lines

Regarding functional issues, I have assumed that only hwdom can make physdev operations, but after checking it, this assumption seems to be correct on x86 but wrong on Arm.
I expected there would be a check in do_arm_physdev_op() or somewhere near it, similar to how it is done in x86, but there are none. I'm not sure if it is intentional or by mistake, I think it needs some clarification by Arm folks.

Quoted 3 lines

Hmm, looking at x86'es do_physdev_op() I fear I can't see such a check there either. And indeed there are certain PHYSDEVOP_* which DomU-s may also make use of.

It is one level above in hvm_physdev_op()

Quoted 9 lines
     case PHYSDEVOP_setup_gsi:
     case PHYSDEVOP_pci_mmcfg_reserved:
     case PHYSDEVOP_pci_device_add:
     case PHYSDEVOP_pci_device_remove:
     case PHYSDEVOP_pci_device_reset:
     case PHYSDEVOP_dbgp_op:
         if ( !is_hardware_domain(currd) )
             return -ENOSYS;
         break;

But that's for just HVM guests, and only a functional restriction, not a security one.

Jan

#23 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +1 -0
@@ -16,9 +16,18 @@
  #include <xen/device_tree.h>
  #include <xen/errno.h>
  #include <xen/init.h>
+#include <xen/iommu.h>
  #include <xen/param.h>
  #include <xen/pci.h>
 
Quoted 8 lines

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

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

Hi Jan,

I've spent some more time thinking about how to better deal with this. In the end, I think your earlier suggestion about introducing a new arch specific function is the best approach, but I want to agree on the naming before sending new patches. Would "arch_requires_pci_physdev" be an appropriate name in your opinion?

At the call sites it will look like this:

     case PHYSDEVOP_pci_device_remove: {
         struct physdev_pci_device dev;

         if ( !is_pci_passthrough_enabled() && !arch_requires_pci_physdev())
             return -EOPNOTSUPP;
Signature · 1 line
Mykyta
#24 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 27 lines · Mykyta Poturai
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +1 -0
@@ -16,9 +16,18 @@
  #include <xen/device_tree.h>
  #include <xen/errno.h>
  #include <xen/init.h>
+#include <xen/iommu.h>
  #include <xen/param.h>
  #include <xen/pci.h>
 
Quoted 8 lines

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

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

I've spent some more time thinking about how to better deal with this. In the end, I think your earlier suggestion about introducing a new arch specific function is the best approach, but I want to agree on the naming before sending new patches. Would "arch_requires_pci_physdev" be an appropriate name in your opinion?

Quoted 6 lines

At the call sites it will look like this:

     case PHYSDEVOP_pci_device_remove: {
         struct physdev_pci_device dev;

         if ( !is_pci_passthrough_enabled() && !arch_requires_pci_physdev())
             return -EOPNOTSUPP;

There are several questions that affect naming: Is it really "requires"? Is it really all PCI-related physdevops? Is the ordering of naming elements in line with what we use elsewhere (arch_ first is, but perhaps either pci or physdevop wants to move earlier)?

Jan

#25 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 35 lines · Jan Beulich
Quoted 27 lines · Mykyta Poturai
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +1 -0
@@ -16,9 +16,18 @@
   #include <xen/device_tree.h>
   #include <xen/errno.h>
   #include <xen/init.h>
+#include <xen/iommu.h>
   #include <xen/param.h>
   #include <xen/pci.h>
 
Quoted 8 lines

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

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

I've spent some more time thinking about how to better deal with this. In the end, I think your earlier suggestion about introducing a new arch specific function is the best approach, but I want to agree on the naming before sending new patches. Would "arch_requires_pci_physdev" be an appropriate name in your opinion?

Quoted 6 lines

At the call sites it will look like this:

      case PHYSDEVOP_pci_device_remove: {
          struct physdev_pci_device dev;

          if ( !is_pci_passthrough_enabled() && !arch_requires_pci_physdev())
              return -EOPNOTSUPP;
Quoted 6 lines

There are several questions that affect naming: Is it really "requires"? Is it really all PCI-related physdevops? Is the ordering of naming elements in line with what we use elsewhere (arch_ first is, but perhaps either pci or physdevop wants to move earlier)?

Jan

I understand the issue with the ordering, will "arch_pci_requires_physdev_ops" or "arch_physdev_pci_update_required" be better? Regarding the specific ops, only add/remove are needed, but I am not sure how to elegantly encode this in the name. Maybe you can suggest something better if you have something specific in mind?

Signature · 1 line
Mykyta
#26 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 41 lines · Mykyta Poturai
Quoted 32 lines · Jan Beulich
Quoted 31 lines · Mykyta Poturai
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Mykyta Poturai
xen/arch/arm/pci/pci.c +1 -0
@@ -16,9 +16,18 @@
   #include <xen/device_tree.h>
   #include <xen/errno.h>
   #include <xen/init.h>
+#include <xen/iommu.h>
   #include <xen/param.h>
   #include <xen/pci.h>
 
Quoted 8 lines

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

Quoted 4 lines
xen/arch/arm/pci/pci.c +4 -0
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

As I think I said before - the function's name now no longer expresses what it really checks. That (imo heavily) misleading at the use sites of this function.

I've spent some more time thinking about how to better deal with this. In the end, I think your earlier suggestion about introducing a new arch specific function is the best approach, but I want to agree on the naming before sending new patches. Would "arch_requires_pci_physdev" be an appropriate name in your opinion?

At the call sites it will look like this:

      case PHYSDEVOP_pci_device_remove: {
          struct physdev_pci_device dev;
Quoted 7 lines
Quoted 2 lines
          if ( !is_pci_passthrough_enabled() && !arch_requires_pci_physdev())
              return -EOPNOTSUPP;

There are several questions that affect naming: Is it really "requires"? Is it really all PCI-related physdevops? Is the ordering of naming elements in line with what we use elsewhere (arch_ first is, but perhaps either pci or physdevop wants to move earlier)?

Quoted 5 lines

I understand the issue with the ordering, will "arch_pci_requires_physdev_ops" or "arch_physdev_pci_update_required" be better? Regarding the specific ops, only add/remove are needed, but I am not sure how to elegantly encode this in the name. Maybe you can suggest something better if you have something specific in mind?

Simply arch_pci_device_physdevop()? This would also avoid the question if it's "requires", "wants", or yet something else. (I'm not going to insist that the verb be omitted, though. If it's included, I'd ask though that it match in tense the "enabled" in the other predicate.) And it would cover PHYSDEVOP_pci_device_reset as well, which sooner or later I expect you'll discover you want/need Dom0 to issue, too.

Jan

Reviewed-by on v9 7/8
#27 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

In reply to Mykyta Poturai
Quoted 75 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

Enable pci_init() for initializing Xen's internal PCI subsystem, and allow PHYSDEVOP_pci_device_add when pci-passthrough is disabled.

---
hmm. Since
dec9e02f3190 ("xen: avoid generation of stub <asm/pci.h> header")
Should we also move is_pci_passthrough_enabled() back to xen/arch/arm/include/asm/pci.h ? Not sure if PPC/RISC-V will plan on using this check.

v8-v9:
* move iommu_enabled check inside is_pci_passthrough_enabled()

v7->v8:
* bring back x86 definition of is_pci_passthrough_enabled()

v6->v7:
* remove x86 definition of is_pci_passthrough_enabled()
* update comments
* make pci_physdev_op checks stricter

v5->v6:
* new patch - this effectively replaces
("Revert "xen/arm: Add cmdline boot option "pci-passthrough = <boolean>""")

---
 xen/arch/arm/domain_build.c    |  2 +-
 xen/arch/arm/include/asm/pci.h |  5 +----
 xen/arch/arm/pci/pci.c         | 11 ++++++++++-
 xen/arch/x86/include/asm/pci.h |  2 +-
 xen/drivers/pci/physdev.c      |  4 ++--
 xen/include/xen/pci.h          |  2 +-
 6 files changed, 16 insertions(+), 10 deletions(-)
xen/arch/arm/domain_build.c +1 -1
index 7b47abade1..fbd6db9438 100644
@@ -526,7 +526,7 @@ static int __init handle_linux_pci_domain(struct kernel_info *kinfo,
     uint16_t segment;
     int res;
 
-    if ( !is_pci_passthrough_enabled() )
+    if ( !is_pci_passthrough_enabled(false) )
         return 0;
 
     if ( !dt_device_type_is_equal(node, "pci") )
xen/arch/arm/include/asm/pci.h +1 -4
index 7f77226c9b..3ae85b4666 100644
@@ -111,10 +111,7 @@ pci_find_host_bridge_node(const struct pci_dev *pdev);
 int pci_get_host_bridge_segment(const struct dt_device_node *node,
                                 uint16_t *segment);
 
-static always_inline bool is_pci_passthrough_enabled(void)
-{
-    return pci_passthrough_enabled;
-}
+bool is_pci_passthrough_enabled(bool dom0);
 
 void arch_pci_init_pdev(struct pci_dev *pdev);
 
xen/arch/arm/pci/pci.c +0 -0
index 78b97beaef..79bb8728a4 100644
@@ -16,9 +16,18 @@
 #include <xen/device_tree.h>

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

Quoted 10 lines
xen/arch/arm/pci/pci.c +5 -0
 #include <xen/errno.h>
 #include <xen/init.h>
+#include <xen/iommu.h>
 #include <xen/param.h>
 #include <xen/pci.h>
 
+bool is_pci_passthrough_enabled(bool dom0)
+{
+    if ( dom0 )
+        return pci_passthrough_enabled || iommu_enabled;

Could this be written as:

    if ( is_hardware_domain(current->domain) )
        return pci_passthrough_enabled || iommu_enabled;

If so, then I think it would be better, if not:

Quoted 66 lines
xen/arch/arm/pci/pci.c +4 -1
+    return pci_passthrough_enabled;
+}
+
 /*
  * PIRQ event channels are not supported on Arm, so nothing to do.
  */
@@ -85,7 +94,7 @@ static int __init pci_init(void)
      * Enable PCI passthrough when has been enabled explicitly
      * (pci-passthrough=on).
      */
-    if ( !pci_passthrough_enabled )
+    if ( !is_pci_passthrough_enabled(true) )
         return 0;
 
     pci_segments_init();
xen/arch/x86/include/asm/pci.h +1 -1
index fd5480d67d..bffeaa507d 100644
@@ -50,7 +50,7 @@ extern int pci_mmcfg_config_num;
 extern struct acpi_mcfg_allocation *pci_mmcfg_config;
 
 /* Unlike ARM, PCI passthrough is always enabled for x86. */
-static always_inline bool is_pci_passthrough_enabled(void)
+static always_inline bool is_pci_passthrough_enabled(__maybe_unused bool dom0)
 {
     return true;
 }
xen/drivers/pci/physdev.c +2 -2
index 0161a85e1e..18448b94b3 100644
@@ -19,7 +19,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         struct pci_dev_info pdev_info;
         nodeid_t node = NUMA_NO_NODE;
 
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
             return -EOPNOTSUPP;
 
         ret = -EFAULT;
@@ -57,7 +57,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_pci_device_remove: {
         struct physdev_pci_device dev;
 
-        if ( !is_pci_passthrough_enabled() )
+        if ( !is_pci_passthrough_enabled(true) )
             return -EOPNOTSUPP;
 
         ret = -EFAULT;
xen/include/xen/pci.h +1 -1
index f784e91160..c4a49cf584 100644
@@ -74,7 +74,7 @@ typedef union {
 
 struct arch_pci_dev { };
 
-static inline bool is_pci_passthrough_enabled(void)
+static inline bool is_pci_passthrough_enabled(__maybe_unused bool dom0)
 {
     return false;
 }
Signature · 1 line
2.34.1
#28 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

In reply to Mykyta Poturai

Hi Mykyta,

Quoted 5 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

It would be good to explain why Xen cannot initialize the PCI controller. Asking, because the reason is the PCI controller is too complex, then you will likely need the same approach for PCI passthrough...

Quoted 3 lines

Enable pci_init() for initializing Xen's internal PCI subsystem, and allow PHYSDEVOP_pci_device_add when pci-passthrough is disabled.

Effectively, wouldn't this mean dom0 always *have* to call PHYSDEVOP_pci_device_add? Otherwise, how would dom0 know whether it needs to call PHSYDEVOP_pci_device_add?

Cheers,

Signature · 1 line
Julien Grall
#29 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 5 lines · Julien Grall

Hi Mykyta,

Quoted 2 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Quoted 7 lines
Quoted 3 lines

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

It would be good to explain why Xen cannot initialize the PCI controller. Asking, because the reason is the PCI controller is too complex, then you will likely need the same approach for PCI passthrough...

I think the main reason for this is complexity and the possibility of additional dependencies: there could be external clocks or reset pins that the PCI host depends on for working correctly. I will add this to the commit message. Regarding PCI passthrough, it is already using the same approach (at least on Arm). There are patches for enabling Xen on Arm to perform bus enumeration by itself by Luca Fancellu, but I haven't yet got to test them in a meaningful way.

Quoted 10 lines
Quoted 3 lines

Enable pci_init() for initializing Xen's internal PCI subsystem, and allow PHYSDEVOP_pci_device_add when pci-passthrough is disabled.

Effectively, wouldn't this mean dom0 always *have* to call PHYSDEVOP_pci_device_add? Otherwise, how would dom0 know whether it needs to call PHSYDEVOP_pci_device_add?

Cheers,

Yes, I can't say for every system but with PCI host behind SMMU the PHYSDEVOP_pci_device_add call is required to use DMA.

Signature · 1 line
Mykyta
#30 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Hi,

Quoted 14 lines · Mykyta Poturai
Quoted 5 lines · Julien Grall

Hi Mykyta,

Quoted 2 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Quoted 7 lines
Quoted 3 lines

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

It would be good to explain why Xen cannot initialize the PCI controller. Asking, because the reason is the PCI controller is too complex, then you will likely need the same approach for PCI passthrough...

Quoted 7 lines

I think the main reason for this is complexity and the possibility of additional dependencies: there could be external clocks or reset pins that the PCI host depends on for working correctly. I will add this to the commit message. Regarding PCI passthrough, it is already using the same approach (at least on Arm). There are patches for enabling Xen on Arm to perform bus enumeration by itself by Luca Fancellu, but I haven't yet got to test them in a meaningful way.

Can you provide a link to the series? I would like to make sure we have a coherent approach. In particular, it is not clear to me how Dom0 and Xen will be able to coordinate the access to the PCI controller. Are we going to have a mediator?

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

Enable pci_init() for initializing Xen's internal PCI subsystem, and allow PHYSDEVOP_pci_device_add when pci-passthrough is disabled.

Effectively, wouldn't this mean dom0 always *have* to call PHYSDEVOP_pci_device_add? Otherwise, how would dom0 know whether it needs to call PHSYDEVOP_pci_device_add?

Quoted 5 lines
Quoted 2 lines

Cheers,

Yes, I can't say for every system but with PCI host behind SMMU the PHYSDEVOP_pci_device_add call is required to use DMA.

Dom0 will not be able to know whether a device is protected by an IOMMU. So I guess it means the OS will need to be able to cope with an error (like on x86).

Cheers,

Signature · 1 line
Julien Grall
#31 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 26 lines · Julien Grall

Hi,

Quoted 15 lines · Mykyta Poturai
Quoted 5 lines · Julien Grall

Hi Mykyta,

Quoted 2 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Quoted 8 lines
Quoted 3 lines

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

It would be good to explain why Xen cannot initialize the PCI controller. Asking, because the reason is the PCI controller is too complex, then you will likely need the same approach for PCI passthrough...

Quoted 7 lines

I think the main reason for this is complexity and the possibility of additional dependencies: there could be external clocks or reset pins that the PCI host depends on for working correctly. I will add this to the commit message. Regarding PCI passthrough, it is already using the same approach (at least on Arm). There are patches for enabling Xen on Arm to perform bus enumeration by itself by Luca Fancellu, but I haven't yet got to test them in a meaningful way.

Quoted 5 lines

Can you provide a link to the series? I would like to make sure we have a coherent approach. In particular, it is not clear to me how Dom0 and Xen will be able to coordinate the access to the PCI controller. Are we going to have a mediator?

Here is a link to my WIP branch
https://github.com/Deedone/xen/tree/pci_passthrough_wip
Although it is slightly outdated due to recent review activity, I will updated it soon with all of the recent changes.

Luca's commits begin from c68a5cbb1a7d17907551159c99ab5c87ce0dedf9

I wasn't able to find them sent to any mailing lists, but I plan to also send them after the base case with Dom0 enumeration stabilizes.

There is no mediator, Dom0 configures the host controller, enumerates child devices, and then gives complete access to some of them to Xen. Xen only does "logical" operations with child devices and does not change any of the host controller base settings. To ensure that Dom0 will not mess with the child devices, tools bind them to the stub driver. Theoretically, Dom0 can bind to those devices again and break something, but it can also do a lot of other breaking stuff so I don't think there is a good reason to invent some forms of protection from it.

After some time with pci-scan changes merged it should become possible to make Xen do the enumeration, and only give Dom0 the virtual PCI bus, which would prevent it from accessing non-owned devices.

Signature · 1 line
Mykyta
#32 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Hi Mykyta,

Quoted 38 lines · Mykyta Poturai
Quoted 26 lines · Julien Grall

Hi,

Quoted 15 lines · Mykyta Poturai
Quoted 5 lines · Julien Grall

Hi Mykyta,

Quoted 2 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Quoted 8 lines
Quoted 3 lines

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

It would be good to explain why Xen cannot initialize the PCI controller. Asking, because the reason is the PCI controller is too complex, then you will likely need the same approach for PCI passthrough...

Quoted 7 lines

I think the main reason for this is complexity and the possibility of additional dependencies: there could be external clocks or reset pins that the PCI host depends on for working correctly. I will add this to the commit message. Regarding PCI passthrough, it is already using the same approach (at least on Arm). There are patches for enabling Xen on Arm to perform bus enumeration by itself by Luca Fancellu, but I haven't yet got to test them in a meaningful way.

Quoted 5 lines

Can you provide a link to the series? I would like to make sure we have a coherent approach. In particular, it is not clear to me how Dom0 and Xen will be able to coordinate the access to the PCI controller. Are we going to have a mediator?

Here is a link to my WIP branch
https://github.com/Deedone/xen/tree/pci_passthrough_wip
Although it is slightly outdated due to recent review activity, I will updated it soon with all of the recent changes.

Quoted 4 lines

Luca's commits begin from c68a5cbb1a7d17907551159c99ab5c87ce0dedf9

I wasn't able to find them sent to any mailing lists, but I plan to also send them after the base case with Dom0 enumeration stabilizes.

I don't think you can stabilize one without the other. I am worry the interface may not work properly for PCI passthrough. So until then, I would say this should be marked as unsupported (maybe protected by CONFIG_UNSUPPORTED).

Quoted 5 lines

There is no mediator, Dom0 configures the host controller, enumerates child devices, and then gives complete access to some of them to Xen. Xen only does "logical" operations with child devices and does not change any of the host controller base settings.

I am not sure I fully understanding this. Both dom0 will need to access the configuration space. So you would need to ensure there is only one accessing the configuration space at a give time.

Quoted 5 lines

To ensure that Dom0
will not mess with the child devices, tools bind them to the stub driver. Theoretically, Dom0 can bind to those devices again and break something, but it can also do a lot of other breaking stuff so I don't think there is a good reason to invent some forms of protection from it.

We should not trust dom0 to do the right thing. But reading ...

Quoted 4 lines

After some time with pci-scan changes merged it should become possible to make Xen do the enumeration, and only give Dom0 the virtual PCI bus, which would prevent it from accessing non-owned devices.

... this it sounds like this would be temporary. Do you have patches already on the mailing list?

Cheers,

Signature · 1 line
Julien Grall
#33 lore raw

Re: [PATCH v9 7/8] xen/arm: enable dom0 to use PCI devices with pci-passthrough=no

Quoted 46 lines · Julien Grall

Hi Mykyta,

Quoted 43 lines · Mykyta Poturai
Quoted 29 lines · Julien Grall

Hi,

Quoted 17 lines · Mykyta Poturai
Quoted 11 lines · Julien Grall

Hi Mykyta,

Quoted 7 lines · Mykyta Poturai

From: Stewart Hildebrand <[email protected]>

Enable the use of IOMMU + PCI in dom0 without having to specify "pci-passthrough=yes". We rely on dom0 to initialize the PCI controller
and perform a PHYSDEVOP_pci_device_add call to add each device to SMMU.

Quoted 4 lines

It would be good to explain why Xen cannot initialize the PCI controller. Asking, because the reason is the PCI controller is too complex, then you will likely need the same approach for PCI passthrough...

Quoted 8 lines

I think the main reason for this is complexity and the possibility of additional dependencies: there could be external clocks or reset pins that the PCI host depends on for working correctly. I will add this to the commit message. Regarding PCI passthrough, it is already using the same approach (at least on Arm). There are patches for enabling Xen on Arm to perform bus enumeration by itself by Luca Fancellu, but I haven't
yet got to test them in a meaningful way.

Quoted 5 lines

Can you provide a link to the series? I would like to make sure we have a coherent approach. In particular, it is not clear to me how Dom0 and Xen will be able to coordinate the access to the PCI controller. Are we going to have a mediator?

Here is a link to my WIP branch
https://github.com/Deedone/xen/tree/pci_passthrough_wip
Although it is slightly outdated due to recent review activity, I will updated it soon with all of the recent changes.

Luca's commits begin from c68a5cbb1a7d17907551159c99ab5c87ce0dedf9

Quoted 7 lines
Quoted 2 lines

I wasn't able to find them sent to any mailing lists, but I plan to also send them after the base case with Dom0 enumeration stabilizes.

I don't think you can stabilize one without the other. I am worry the interface may not work properly for PCI passthrough. So until then, I would say this should be marked as unsupported (maybe protected by CONFIG_UNSUPPORTED).

It is currently not possible to enable HAS_PCI on Arm at all. I will add the UNSUPPORTED guard along with the future changes to Kconfig that will make enabling HAS_PCI on Arm possible.

Quoted 10 lines
Quoted 5 lines

There is no mediator, Dom0 configures the host controller, enumerates child devices, and then gives complete access to some of them to Xen. Xen only does "logical" operations with child devices and does not change any of the host controller base settings.

I am not sure I fully understanding this. Both dom0 will need to access the configuration space. So you would need to ensure there is only one accessing the configuration space at a give time.

If a controller doesn't require specific configuration for mapping child bus then Xen should access only child's config space and if Dom0 ignores this device there will be no conflicts. For more complex controllers like the ones on R-Car boards it is possible to implement safer ways of mapping config space via Xen, example patch here[1] as temporary solution.

Quoted 9 lines
Quoted 5 lines

To ensure that Dom0
will not mess with the child devices, tools bind them to the stub driver. Theoretically, Dom0 can bind to those devices again and break something, but it can also do a lot of other breaking stuff so I don't think there is a good reason to invent some forms of protection from it.

We should not trust dom0 to do the right thing. But reading ...

Quoted 1 line
Quoted 8 lines
Quoted 3 lines

After some time with pci-scan changes merged it should become possible to make Xen do the enumeration, and only give Dom0 the virtual PCI bus, which would prevent it from accessing non-owned devices.

... this it sounds like this would be temporary. Do you have patches already on the mailing list?

Quoted 1 line

Cheers,

Not yet, I am planning to start working on them after dealing with MSI support on Arm.

[1]:
https://github.com/xen-troops/meta-xt-prod-devel-rcar-gen4/blob/master/layers/meta-xt-domd-gen4/recipes-kernel/linux/linux-renesas/0005-HACK-pcie-renesas-emulate-reading-from-ECAM-under-Xe.patch

Signature · 1 line
Mykyta
#34 lore raw

PATCH v9 8/8 xen/arm: Map ITS doorbell register to IOMMU page tables

In reply to Mykyta Poturai

From: Rahul Singh <[email protected]>

When ITS is enabled and PCI devices that are behind an SMMU generate an MSI interrupt, SMMU fault will be observed as there is currently no mapping in p2m table for the ITS translation register (GITS_TRANSLATER).

A mapping is required in the iommu page tables so that the device can generate the MSI interrupt writing to the GITS_TRANSLATER register.

The GITS_TRANSLATER register is a 32-bit register, and there is nothing else in a page containing it, so map that page.

---
This patch was originally picked up from [1], and commit description loosely borrowed from [2].

Example SMMUv3 fault (qemu-system-aarch64 virt model), ITS base 0x8080000:

(XEN) SMMUv3: /smmuv3@9050000: event 0x10 received:

(XEN) SMMUv3: /smmuv3@9050000:  0x0000000800000010
(XEN) SMMUv3: /smmuv3@9050000:  0x0000008000000000
(XEN) SMMUv3: /smmuv3@9050000:  0x0000000008090040
(XEN) SMMUv3: /smmuv3@9050000:  0x0000000000000000

Example SMMUv2 fault (AMD/Xilinx Versal), ITS base 0xf9020000:

(XEN) smmu: /axi/smmu@fd800000: Unhandled context fault: fsr=0x402, iova=0xf9030040, fsynr=0x12, cb=0

v8->v9:
* no changes

v7->v8:
* no changes

v6->v7:
* add tlb flush after mapping
* style: update formatting
* revert back to printk with XENLOG_G_ERR

v5->v6:
* switch to iommu_map() interface
* fix page_count argument
* style fixup
* use gprintk instead of printk
* add my Signed-off-by
* move to vgic_v3_its_init_virtual()

v4->v5:
* new patch

[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg00483.html
[2] https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc/-/commit/6232a0d53377009bb7fbc3c3ab81d0153734be6b

---
 xen/arch/arm/vgic-v3-its.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
xen/arch/arm/vgic-v3-its.c +20 -0
index c65c1dbf52..376254f206 100644
@@ -1478,6 +1478,26 @@ static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr,
 
     register_mmio_handler(d, &vgic_its_mmio_handler, guest_addr, SZ_64K, its);
 
+    if ( is_iommu_enabled(its->d) )
+    {
+        mfn_t mfn = maddr_to_mfn(its->doorbell_address);
+        unsigned int flush_flags = 0;
+        int ret = iommu_map(its->d, _dfn(mfn_x(mfn)), mfn, 1, IOMMUF_writable,
+                            &flush_flags);
+
+        if ( ret < 0 )
+        {
+            printk(XENLOG_G_ERR
+                    "GICv3: Map ITS translation register for %pd failed.\n",
+                    its->d);
+            return ret;
+        }
+
+        ret = iommu_iotlb_flush(its->d, _dfn(mfn_x(mfn)), 1, flush_flags);
+        if ( ret < 0 )
+            return ret;
+    }
+
     /* Register the virtual ITS to be able to clean it up later. */
     list_add_tail(&its->vits_list, &d->arch.vgic.vits_list);
 
Signature · 1 line
2.34.1
#35 lore raw

Re: [PATCH v9 8/8] xen/arm: Map ITS doorbell register to IOMMU page tables

Hi Mykyta,

Quoted 64 lines · Mykyta Poturai

From: Rahul Singh <[email protected]>

When ITS is enabled and PCI devices that are behind an SMMU generate an MSI interrupt, SMMU fault will be observed as there is currently no mapping in p2m table for the ITS translation register (GITS_TRANSLATER).

A mapping is required in the iommu page tables so that the device can generate the MSI interrupt writing to the GITS_TRANSLATER register.

The GITS_TRANSLATER register is a 32-bit register, and there is nothing else in a page containing it, so map that page.

---
This patch was originally picked up from [1], and commit description loosely borrowed from [2].

Example SMMUv3 fault (qemu-system-aarch64 virt model), ITS base 0x8080000:

(XEN) SMMUv3: /smmuv3@9050000: event 0x10 received:

(XEN) SMMUv3: /smmuv3@9050000:  0x0000000800000010
(XEN) SMMUv3: /smmuv3@9050000:  0x0000008000000000
(XEN) SMMUv3: /smmuv3@9050000:  0x0000000008090040
(XEN) SMMUv3: /smmuv3@9050000:  0x0000000000000000

Example SMMUv2 fault (AMD/Xilinx Versal), ITS base 0xf9020000:

(XEN) smmu: /axi/smmu@fd800000: Unhandled context fault: fsr=0x402, iova=0xf9030040, fsynr=0x12, cb=0

v8->v9:
* no changes

v7->v8:
* no changes

v6->v7:
* add tlb flush after mapping
* style: update formatting
* revert back to printk with XENLOG_G_ERR

v5->v6:
* switch to iommu_map() interface
* fix page_count argument
* style fixup
* use gprintk instead of printk
* add my Signed-off-by
* move to vgic_v3_its_init_virtual()

v4->v5:
* new patch

[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg00483.html
[2] https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc/-/commit/6232a0d53377009bb7fbc3c3ab81d0153734be6b

---
  xen/arch/arm/vgic-v3-its.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
xen/arch/arm/vgic-v3-its.c +0 -0
index c65c1dbf52..376254f206 100644

xen/arch/arm/vgic-v3-its.c static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr, · line 1486

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +6 -0
@@ -1478,6 +1478,26 @@ static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr,
 
      register_mmio_handler(d, &vgic_its_mmio_handler, guest_addr, SZ_64K, its);
 
+    if ( is_iommu_enabled(its->d) )
+    {
+        mfn_t mfn = maddr_to_mfn(its->doorbell_address);
+        unsigned int flush_flags = 0;
+        int ret = iommu_map(its->d, _dfn(mfn_x(mfn)), mfn, 1, IOMMUF_writable,
+                            &flush_flags);

While looking at your patches for the guest vITS support. I noticed this line is not updated for domU. Regardless of the one vs many vITS discussion, I don't think this wants to be mapped 1:1. Instead, you want to map the guest doorbell page to the host doorbell page.

The flush would need to be updated accordingly.

Cheers,

Quoted 17 lines
xen/arch/arm/vgic-v3-its.c +14 -0
+
+        if ( ret < 0 )
+        {
+            printk(XENLOG_G_ERR
+                    "GICv3: Map ITS translation register for %pd failed.\n",
+                    its->d);
+            return ret;
+        }
+
+        ret = iommu_iotlb_flush(its->d, _dfn(mfn_x(mfn)), 1, flush_flags);
+        if ( ret < 0 )
+            return ret;
+    }
+
      /* Register the virtual ITS to be able to clean it up later. */
      list_add_tail(&its->vits_list, &d->arch.vgic.vits_list);
 
Signature · 1 line
Julien Grall
#36 lore raw

Re: [PATCH v9 8/8] xen/arm: Map ITS doorbell register to IOMMU page tables

In reply to Mykyta Poturai
Quoted 64 lines · Mykyta Poturai

From: Rahul Singh <[email protected]>

When ITS is enabled and PCI devices that are behind an SMMU generate an MSI interrupt, SMMU fault will be observed as there is currently no mapping in p2m table for the ITS translation register (GITS_TRANSLATER).

A mapping is required in the iommu page tables so that the device can generate the MSI interrupt writing to the GITS_TRANSLATER register.

The GITS_TRANSLATER register is a 32-bit register, and there is nothing else in a page containing it, so map that page.

---
This patch was originally picked up from [1], and commit description loosely borrowed from [2].

Example SMMUv3 fault (qemu-system-aarch64 virt model), ITS base 0x8080000:

(XEN) SMMUv3: /smmuv3@9050000: event 0x10 received:

(XEN) SMMUv3: /smmuv3@9050000:  0x0000000800000010
(XEN) SMMUv3: /smmuv3@9050000:  0x0000008000000000
(XEN) SMMUv3: /smmuv3@9050000:  0x0000000008090040
(XEN) SMMUv3: /smmuv3@9050000:  0x0000000000000000

Example SMMUv2 fault (AMD/Xilinx Versal), ITS base 0xf9020000:

(XEN) smmu: /axi/smmu@fd800000: Unhandled context fault: fsr=0x402, iova=0xf9030040, fsynr=0x12, cb=0

v8->v9:
* no changes

v7->v8:
* no changes

v6->v7:
* add tlb flush after mapping
* style: update formatting
* revert back to printk with XENLOG_G_ERR

v5->v6:
* switch to iommu_map() interface
* fix page_count argument
* style fixup
* use gprintk instead of printk
* add my Signed-off-by
* move to vgic_v3_its_init_virtual()

v4->v5:
* new patch

[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg00483.html
[2] https://gitlab.com/xen-project/people/bmarquis/xen-arm-poc/-/commit/6232a0d53377009bb7fbc3c3ab81d0153734be6b

---
 xen/arch/arm/vgic-v3-its.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
xen/arch/arm/vgic-v3-its.c +0 -0
index c65c1dbf52..376254f206 100644

xen/arch/arm/vgic-v3-its.c static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr, · line 1486

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +6 -0
@@ -1478,6 +1478,26 @@ static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr,
 
     register_mmio_handler(d, &vgic_its_mmio_handler, guest_addr, SZ_64K, its);
 
+    if ( is_iommu_enabled(its->d) )
+    {
+        mfn_t mfn = maddr_to_mfn(its->doorbell_address);
+        unsigned int flush_flags = 0;
+        int ret = iommu_map(its->d, _dfn(mfn_x(mfn)), mfn, 1, IOMMUF_writable,
+                            &flush_flags);

Should this be:

        int ret = iommu_map(its->d, _dfn(PFN_DOWN(its->doorbell_address)), mfn, 1, IOMMUF_writable,
                            &flush_flags);

?

xen/arch/arm/vgic-v3-its.c static int vgic_v3_its_init_virtual(struct domain *d, paddr_t guest_addr, · line 1496

Quoted 9 lines
xen/arch/arm/vgic-v3-its.c +9 -0
+        if ( ret < 0 )
+        {
+            printk(XENLOG_G_ERR
+                    "GICv3: Map ITS translation register for %pd failed.\n",
+                    its->d);
+            return ret;
+        }
+
+        ret = iommu_iotlb_flush(its->d, _dfn(mfn_x(mfn)), 1, flush_flags);

And this:

       ret = iommu_iotlb_flush(its->d, _dfn(PFN_DOWN(its->doorbell_address)), 1, flush_flags);

?

The original code in this patch assumes that the mapping is 1:1 but actually its->doorbell_address is set to guest_addr + ITS_DOORBELL_OFFSET and could potentially be not 1:1 ?

Quoted 10 lines
xen/arch/arm/vgic-v3-its.c +4 -0
+        if ( ret < 0 )
+            return ret;
+    }
+
     /* Register the virtual ITS to be able to clean it up later. */
     list_add_tail(&its->vits_list, &d->arch.vgic.vits_list);
 
Signature · 1 line
2.34.1