Skip to contentSkip to conversation

Add support parse PCI iommu bindings for Xen SMMU

PATCH In review Wei Chen 3 participants 8 messages Jun 30, 2017 – Jul 7, 2017

lore
Share & tools
lore

Wei Chen

author
#0 lore raw

cover Add support parse PCI iommu bindings for Xen SMMU

The legacy IOMMU bindings will be deprecated in future. Instead, device tree provide generic IOMMU bindings and PCI IOMMU bindings. Currently, the PCI support hasn't been enabled on ARM Xen. So in this series, we just add the support of parsing PCI IOMMU bindings.

Wei Chen (2):
xen: devicetree: Introduce a helper to translate PCI requester ID xen/arm: smmu: Parse generic iommu binding for PCI devices

 xen/common/device_tree.c           | 89 ++++++++++++++++++++++++++++++++++++++
 xen/drivers/passthrough/arm/smmu.c | 73 ++++++++++++++++++++++++++++++-
 xen/include/xen/device_tree.h      | 23 ++++++++++
 3 files changed, 183 insertions(+), 2 deletions(-)
Signature · 7 lines
2.7.4


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

PATCH 1/2 xen: devicetree: Introduce a helper to translate PCI requester ID

Each PCI(e) device under a root complex is uniquely identified by its Requester ID (AKA RID). A Requester ID is a triplet of a Bus number, Device number, and Function number. IOMMUs may distinguish PCI devices through sideband data derived from the Requester ID. While a given PCI device can only master through one IOMMU, a root complex may split masters across a set of IOMMUs.

The generic 'iommus' property is using to describe this relationship. This helper will be used to parse and map PCI Requester ID to IOMMU match ID in later patches.

This patch is based on Linux of_pci.c:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/of/of_pci.c
The commit id is: 987068fcbdb7a085bb11151b91dc6f4c956c4a1b

---
 xen/common/device_tree.c      | 89 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/device_tree.h | 23 +++++++++++
 2 files changed, 112 insertions(+)
xen/common/device_tree.c +89 -0
index 7b009ea..bf95cda 100644
@@ -1663,6 +1663,95 @@ int dt_parse_phandle_with_args(const struct dt_device_node *np,
                                         index, out_args);
 }
 
+#define pr_err(fmt, ...) printk(XENLOG_ERR fmt, ## __VA_ARGS__)
+#define pr_info(fmt, ...) printk(XENLOG_INFO fmt, ## __VA_ARGS__)
+#define pr_debug(fmt, ...) printk(XENLOG_DEBUG fmt, ## __VA_ARGS__)
+
+int dt_pci_map_rid(struct dt_device_node *np, u32 rid,
+           const char *map_name, const char *map_mask_name,
+           struct dt_device_node **target, u32 *id_out)
+{
+    u32 map_mask, masked_rid, 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 = rid;
+        return 0;
+    }
+
+    if ( !map_len || map_len % (4 * sizeof(*map)) )
+    {
+        pr_err("%s: Error: Bad %s length: %d\n", np->full_name,
+               map_name, map_len);
+        return -EINVAL;
+    }
+
+    /*
+     * Can be overridden by "{iommu,msi}-map-mask" property.
+     * If of_property_read_u32() fails, the default is used.
+     */
+    if ( !map_mask_name ||
+         !dt_property_read_u32(np, map_mask_name, &map_mask) )
+        /* The default is to select all bits. */
+        map_mask = 0xffffffff;
+
+    masked_rid = map_mask & rid;
+    for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4 )
+    {
+        struct dt_device_node *phandle_node;
+        u32 rid_base = be32_to_cpup(map + 0);
+        u32 phandle = be32_to_cpup(map + 1);
+        u32 out_base = be32_to_cpup(map + 2);
+        u32 rid_len = be32_to_cpup(map + 3);
+
+        if ( rid_base & ~map_mask )
+        {
+            pr_err("%s: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
+                    np->full_name, map_name, map_name,
+                    map_mask, rid_base);
+            return -EFAULT;
+        }
+
+        if ( masked_rid < rid_base || masked_rid >= rid_base + rid_len )
+            continue;
+
+        phandle_node = dt_find_node_by_phandle(phandle);
+        if ( !phandle_node )
+            return -ENODEV;
+
+        if ( target )
+        {
+            if ( *target == NULL )
+                *target = phandle_node;
+
+            if ( *target != phandle_node )
+                continue;
+        }
+
+        if ( id_out )
+            *id_out = masked_rid - rid_base + out_base;
+
+        pr_info("%s: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
+                np->full_name, map_name, map_mask, rid_base, out_base,
+                rid_len, rid, *id_out);
+        return 0;
+    }
+
+    pr_err("%s: Invalid %s translation - no match for rid 0x%x on %s\n",
+           np->full_name, map_name, rid,
+           target && *target ? (*target)->full_name : "any target");
+
+    return -EFAULT;
+}
+
 /**
  * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  * @fdt: The parent device tree blob
xen/include/xen/device_tree.h +23 -0
index 0aecbe0..0bddd7f 100644
@@ -486,6 +486,29 @@ int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
                           struct dt_device_node **node);
 
 /**
+ * dt_pci_map_rid - Translate a requester ID through a downstream mapping.
+ * @np: root complex device node.
+ * @rid: PCI requester 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 PCI requester 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_pci_map_rid(struct dt_device_node *np, u32 rid,
+           const char *map_name, const char *map_mask_name,
+           struct dt_device_node **target, u32 *id_out);
+
+/**
  * dt_get_parent - Get a node's parent if any
  * @node: Node to get parent
  *
Signature · 7 lines
2.7.4


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

Re: [PATCH 1/2] xen: devicetree: Introduce a helper to translate PCI requester ID

Quoted 26 lines · Wei Chen

Each PCI(e) device under a root complex is uniquely identified by its Requester ID (AKA RID). A Requester ID is a triplet of a Bus number, Device number, and Function number. IOMMUs may distinguish PCI devices through sideband data derived from the Requester ID. While a given PCI device can only master through one IOMMU, a root complex may split masters across a set of IOMMUs.

The generic 'iommus' property is using to describe this relationship. This helper will be used to parse and map PCI Requester ID to IOMMU match ID in later patches.

This patch is based on Linux of_pci.c:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/of/of_pci.c
The commit id is: 987068fcbdb7a085bb11151b91dc6f4c956c4a1b

---
 xen/common/device_tree.c      | 89 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/device_tree.h | 23 +++++++++++
 2 files changed, 112 insertions(+)
xen/common/device_tree.c +0 -0
index 7b009ea..bf95cda 100644

xen/common/device_tree.c int dt_parse_phandle_with_args(const struct dt_device_node *np, · line 1668

Quoted 6 lines
xen/common/device_tree.c +3 -0
@@ -1663,6 +1663,95 @@ int dt_parse_phandle_with_args(const struct dt_device_node *np,
                                         index, out_args);
 }
 
+#define pr_err(fmt, ...) printk(XENLOG_ERR fmt, ## __VA_ARGS__)
+#define pr_info(fmt, ...) printk(XENLOG_INFO fmt, ## __VA_ARGS__)
+#define pr_debug(fmt, ...) printk(XENLOG_DEBUG fmt, ## __VA_ARGS__)

I wouldn't define pr_* in device_tree.c just for this function. I would use printk(XENLOG_* directly and dt_dprintk.

Quoted 4 lines
xen/common/device_tree.c +4 -0
+int dt_pci_map_rid(struct dt_device_node *np, u32 rid,
+           const char *map_name, const char *map_mask_name,
+           struct dt_device_node **target, u32 *id_out)
+{

xen/common/device_tree.c int dt_parse_phandle_with_args(const struct dt_device_node *np, · line 1683

Quoted 10 lines
xen/common/device_tree.c +10 -0
+    u32 map_mask, masked_rid, 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)

if ( target )

Quoted 111 lines
xen/common/device_tree.c +71 -0
+            return -ENODEV;
+        /* Otherwise, no map implies no translation */
+        *id_out = rid;
+        return 0;
+    }
+
+    if ( !map_len || map_len % (4 * sizeof(*map)) )
+    {
+        pr_err("%s: Error: Bad %s length: %d\n", np->full_name,
+               map_name, map_len);
+        return -EINVAL;
+    }
+
+    /*
+     * Can be overridden by "{iommu,msi}-map-mask" property.
+     * If of_property_read_u32() fails, the default is used.
+     */
+    if ( !map_mask_name ||
+         !dt_property_read_u32(np, map_mask_name, &map_mask) )
+        /* The default is to select all bits. */
+        map_mask = 0xffffffff;
+
+    masked_rid = map_mask & rid;
+    for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4 )
+    {
+        struct dt_device_node *phandle_node;
+        u32 rid_base = be32_to_cpup(map + 0);
+        u32 phandle = be32_to_cpup(map + 1);
+        u32 out_base = be32_to_cpup(map + 2);
+        u32 rid_len = be32_to_cpup(map + 3);
+
+        if ( rid_base & ~map_mask )
+        {
+            pr_err("%s: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
+                    np->full_name, map_name, map_name,
+                    map_mask, rid_base);
+            return -EFAULT;
+        }
+
+        if ( masked_rid < rid_base || masked_rid >= rid_base + rid_len )
+            continue;
+
+        phandle_node = dt_find_node_by_phandle(phandle);
+        if ( !phandle_node )
+            return -ENODEV;
+
+        if ( target )
+        {
+            if ( *target == NULL )
+                *target = phandle_node;
+
+            if ( *target != phandle_node )
+                continue;
+        }
+
+        if ( id_out )
+            *id_out = masked_rid - rid_base + out_base;
+
+        pr_info("%s: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
+                np->full_name, map_name, map_mask, rid_base, out_base,
+                rid_len, rid, *id_out);
+        return 0;
+    }
+
+    pr_err("%s: Invalid %s translation - no match for rid 0x%x on %s\n",
+           np->full_name, map_name, rid,
+           target && *target ? (*target)->full_name : "any target");
+
+    return -EFAULT;
+}
+
 /**
  * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  * @fdt: The parent device tree blob
xen/include/xen/device_tree.h +23 -0
index 0aecbe0..0bddd7f 100644
@@ -486,6 +486,29 @@ int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
                           struct dt_device_node **node);
 
 /**
+ * dt_pci_map_rid - Translate a requester ID through a downstream mapping.
+ * @np: root complex device node.
+ * @rid: PCI requester 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 PCI requester 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_pci_map_rid(struct dt_device_node *np, u32 rid,
+           const char *map_name, const char *map_mask_name,
+           struct dt_device_node **target, u32 *id_out);
+
+/**
  * dt_get_parent - Get a node's parent if any
  * @node: Node to get parent
  *
Signature · 1 line
2.7.4

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

author
#3 lore raw

Re: [PATCH 1/2] xen: devicetree: Introduce a helper to translate PCI requester ID

Hi Stefano,

Quoted 44 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 2017年7月7日 4:26
To: Wei Chen <[email protected]>
Cc: [email protected]; [email protected]; Julien Grall <[email protected]>; Steve Capper <[email protected]>; Kaly Xin <[email protected]>; nd <[email protected]>
Subject: Re: [PATCH 1/2] xen: devicetree: Introduce a helper to translate PCI requester ID

Quoted 7 lines · Wei Chen

Each PCI(e) device under a root complex is uniquely identified by its Requester ID (AKA RID). A Requester ID is a triplet of a Bus number, Device number, and Function number. IOMMUs may distinguish PCI devices through sideband data derived from the Requester ID. While a given PCI device can only master through one IOMMU, a root complex may split masters across a set of IOMMUs.

Quoted 6 lines

The generic 'iommus' property is using to describe this relationship. This helper will be used to parse and map PCI Requester ID to IOMMU match ID in later patches.

This patch is based on Linux of_pci.c:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/driver
s/of/of_pci.c

Quoted 5 lines

The commit id is: 987068fcbdb7a085bb11151b91dc6f4c956c4a1b

---
 xen/common/device_tree.c      | 89

+++++++++++++++++++++++++++++++++++++++++++

xen/common/device_tree.c int dt_parse_phandle_with_args(const struct

Quoted 8 lines
 xen/include/xen/device_tree.h | 23 +++++++++++
 2 files changed, 112 insertions(+)
xen/common/device_tree.c +0 -0
index 7b009ea..bf95cda 100644
@@ -1663,6 +1663,95 @@ int dt_parse_phandle_with_args(const struct

dt_device_node *np,

Quoted 3 lines
xen/common/device_tree.c +0 -0
                                         index, out_args);
 }
 
Quoted 7 lines

xen/common/device_tree.c int dt_parse_phandle_with_args(const struct dt_device_node *np, · line 1668

Quoted 3 lines
xen/common/device_tree.c +3 -0
+#define pr_err(fmt, ...) printk(XENLOG_ERR fmt, ## __VA_ARGS__)
+#define pr_info(fmt, ...) printk(XENLOG_INFO fmt, ## __VA_ARGS__)
+#define pr_debug(fmt, ...) printk(XENLOG_DEBUG fmt, ## __VA_ARGS__)

I wouldn't define pr_* in device_tree.c just for this function. I would use printk(XENLOG_* directly and dt_dprintk.

Ok, I will cleanup it.

Quoted 17 lines
Quoted 5 lines
xen/common/device_tree.c +4 -0
+int dt_pci_map_rid(struct dt_device_node *np, u32 rid,
+           const char *map_name, const char *map_mask_name,
+           struct dt_device_node **target, u32 *id_out)
+{

xen/common/device_tree.c int dt_parse_phandle_with_args(const struct dt_device_node *np, · line 1683

Quoted 10 lines
xen/common/device_tree.c +10 -0
+    u32 map_mask, masked_rid, 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)

if ( target )

Oh, it's code-style mistake, I will fix it.

Quoted 102 lines
Quoted 26 lines
xen/common/device_tree.c +24 -0
+            return -ENODEV;
+        /* Otherwise, no map implies no translation */
+        *id_out = rid;
+        return 0;
+    }
+
+    if ( !map_len || map_len % (4 * sizeof(*map)) )
+    {
+        pr_err("%s: Error: Bad %s length: %d\n", np->full_name,
+               map_name, map_len);
+        return -EINVAL;
+    }
+
+    /*
+     * Can be overridden by "{iommu,msi}-map-mask" property.
+     * If of_property_read_u32() fails, the default is used.
+     */
+    if ( !map_mask_name ||
+         !dt_property_read_u32(np, map_mask_name, &map_mask) )
+        /* The default is to select all bits. */
+        map_mask = 0xffffffff;
+
+    masked_rid = map_mask & rid;
+    for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4 )

xen/common/device_tree.c int dt_parse_phandle_with_args(const struct dt_device_node *np,

Quoted 10 lines
xen/common/device_tree.c +10 -0
+    {
+        struct dt_device_node *phandle_node;
+        u32 rid_base = be32_to_cpup(map + 0);
+        u32 phandle = be32_to_cpup(map + 1);
+        u32 out_base = be32_to_cpup(map + 2);
+        u32 rid_len = be32_to_cpup(map + 3);
+
+        if ( rid_base & ~map_mask )
+        {
+            pr_err("%s: Invalid %s translation - %s-mask (0x%x) ignores

rid-base (0x%x)\n",

Quoted 15 lines
xen/common/device_tree.c +15 -0
+                    np->full_name, map_name, map_name,
+                    map_mask, rid_base);
+            return -EFAULT;
+        }
+
+        if ( masked_rid < rid_base || masked_rid >= rid_base + rid_len )
+            continue;
+
+        phandle_node = dt_find_node_by_phandle(phandle);
+        if ( !phandle_node )
+            return -ENODEV;
+
+        if ( target )
+        {
+            if ( *target == NULL )

xen/common/device_tree.c int dt_parse_phandle_with_args(const struct dt_device_node *np,

Quoted 10 lines
xen/common/device_tree.c +10 -0
+                *target = phandle_node;
+
+            if ( *target != phandle_node )
+                continue;
+        }
+
+        if ( id_out )
+            *id_out = masked_rid - rid_base + out_base;
+
+        pr_info("%s: %s, using mask %08x, rid-base: %08x, out-base: %08x,

length: %08x, rid: %08x -> %08x\n",

xen/include/xen/device_tree.h int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path,

Quoted 20 lines
xen/common/device_tree.c +12 -0
+                np->full_name, map_name, map_mask, rid_base, out_base,
+                rid_len, rid, *id_out);
+        return 0;
+    }
+
+    pr_err("%s: Invalid %s translation - no match for rid 0x%x on %s\n",
+           np->full_name, map_name, rid,
+           target && *target ? (*target)->full_name : "any target");
+
+    return -EFAULT;
+}
+
 /**
  * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  * @fdt: The parent device tree blob
xen/include/xen/device_tree.h +0 -0
index 0aecbe0..0bddd7f 100644
@@ -486,6 +486,29 @@ int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path,

uint32_t u_plen,

Quoted 7 lines
xen/include/xen/device_tree.h +4 -0
                           struct dt_device_node **node);
 
 /**
+ * dt_pci_map_rid - Translate a requester ID through a downstream mapping.
+ * @np: root complex device node.
+ * @rid: PCI requester ID to map.
+ * @map_name: property name of the map to use.

xen/include/xen/device_tree.h int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,

Quoted 10 lines
xen/include/xen/device_tree.h +10 -0
+ * @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 PCI requester 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

Quoted 15 lines
Quoted 13 lines
xen/include/xen/device_tree.h +9 -1
+ * the first matching target phandle, with a reference held.
+ *
+ * Return: 0 on success or a standard error code on failure.
+ */
+int dt_pci_map_rid(struct dt_device_node *np, u32 rid,
+           const char *map_name, const char *map_mask_name,
+           struct dt_device_node **target, u32 *id_out);
+
+/**
  * dt_get_parent - Get a node's parent if any
  * @node: Node to get parent
  *
--
Quoted 2 lines

2.7.4

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

author
#4 lore raw

PATCH 2/2 xen/arm: smmu: Parse generic iommu binding for PCI devices

In reply to Wei Chen

The legacy smmu binding will be deprecated in favour of the generic "iommus" binding. So we need a new helper to parse generic iommu bindings. When the system detects the SMMU is using generic iommu binding, this helper will be called when this platform device is assiged to a guest.

This patch is based on Linux of_iommu.c:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/iommu/of_iommu.c
The commit id is:
2a0c57545a291f257cd231b1c4b18285b84608d8

---
 xen/drivers/passthrough/arm/smmu.c | 73 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 71 insertions(+), 2 deletions(-)
xen/drivers/passthrough/arm/smmu.c +71 -2
index 25f2207..50ff997 100644
@@ -2750,7 +2750,72 @@ static int arm_smmu_platform_iommu_init(struct device *dev)
 	return 0;
 }
 
-static int arm_smmu_xen_add_device(u8 devfn, struct device*dev)
+/*
+ * Currently, we haven't supported PCI device on ARM. So this is the
+ * temporary function to get device node of pci bridge device for
+ * function verification only
+ */
+static struct device_node *pci_get_bridge_device_node(struct device *dev)
+{
+	struct device_node *dt_dev;
+	const char *type_str;
+
+	dt_for_each_device_node(dt_host, dt_dev) {
+		/* Return the first pci bridge device node simply */
+		if (!dt_property_read_string(dt_dev, "device_type", &type_str) &&
+			!strcmp(type_str, "pci"))
+			return dt_dev;
+	}
+
+	return NULL;
+}
+
+#define PCI_DEVID(bus, devfn)  ((((u16)(bus)) << 8) | (devfn))
+
+static int arm_smmu_pci_iommu_init(struct device *dev, u8 devfn)
+{
+	struct device_node *bridge_np;
+	struct of_phandle_args iommu_spec;
+	struct pci_dev *pdev = to_pci_dev(dev);
+	int ret;
+
+	bridge_np = pci_get_bridge_device_node(dev);
+	if (!bridge_np) {
+		dev_err(dev, "Cloud not find the pci bridge device node!\n");
+		return -ENODEV;
+	}
+
+	/*
+	 * Start by tracing the RID alias down the PCI topology as
+	 * far as the host bridge whose OF node we have...
+	 * (we're not even attempting to handle multi-alias devices yet)
+	 */
+	iommu_spec.args_count = 1;
+	iommu_spec.np = bridge_np;
+	ret = __arm_smmu_get_pci_sid(pdev, PCI_DEVID(pdev->bus, devfn),
+					&iommu_spec.args[0]);
+	if (ret) {
+		dev_err(dev, "Get pci requester ID failed, err=%d!\n", ret);
+		return ret;
+	}
+
+	/*
+	 * ...then find out what that becomes once it escapes the PCI
+	 * bus into the system beyond, and which IOMMU it ends up at.
+	 */
+	iommu_spec.np = NULL;
+	ret = dt_pci_map_rid(bridge_np, iommu_spec.args[0], "iommu-map",
+					"iommu-map-mask", &iommu_spec.np,
+					iommu_spec.args);
+	if (ret) {
+		dev_err(dev, "Do pci map rid failed, err=%d\n", ret);
+		return ret;
+	}
+
+	return arm_smmu_of_xlate(dev, &iommu_spec);
+}
+
+static int arm_smmu_xen_add_device(u8 devfn, struct device *dev)
 {
 	int ret;
 
@@ -2760,7 +2825,11 @@ static int arm_smmu_xen_add_device(u8 devfn, struct device*dev)
 	 * register Master IDs while this function had been invoked.
 	 */
 	if (using_generic_binding) {
-		ret = arm_smmu_platform_iommu_init(dev);
+		if (dev_is_pci(dev))
+			ret = arm_smmu_pci_iommu_init(dev, devfn);
+		else
+			ret = arm_smmu_platform_iommu_init(dev);
+
 		if (ret)
 			return ret;
 	}
Signature · 7 lines
2.7.4


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

Re: [PATCH 2/2] xen/arm: smmu: Parse generic iommu binding for PCI devices

Quoted 21 lines · Wei Chen

The legacy smmu binding will be deprecated in favour of the generic "iommus" binding. So we need a new helper to parse generic iommu bindings. When the system detects the SMMU is using generic iommu binding, this helper will be called when this platform device is assiged to a guest.

This patch is based on Linux of_iommu.c:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/iommu/of_iommu.c
The commit id is:
2a0c57545a291f257cd231b1c4b18285b84608d8

---
 xen/drivers/passthrough/arm/smmu.c | 73 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 71 insertions(+), 2 deletions(-)
xen/drivers/passthrough/arm/smmu.c +0 -0
index 25f2207..50ff997 100644

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

Quoted 6 lines
xen/drivers/passthrough/arm/smmu.c +2 -1
@@ -2750,7 +2750,72 @@ static int arm_smmu_platform_iommu_init(struct device *dev)
 	return 0;
 }
 
-static int arm_smmu_xen_add_device(u8 devfn, struct device*dev)
+/*
+ * Currently, we haven't supported PCI device on ARM. So this is the

"Currently, we don't support PCI devices on ARM."

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

Quoted 2 lines
xen/drivers/passthrough/arm/smmu.c +2 -0
+ * temporary function to get device node of pci bridge device for
+ * function verification only

What do you mean by "for function verification only"?

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

Quoted 11 lines
xen/drivers/passthrough/arm/smmu.c +11 -0
+ */
+static struct device_node *pci_get_bridge_device_node(struct device *dev)
+{
+	struct device_node *dt_dev;
+	const char *type_str;
+
+	dt_for_each_device_node(dt_host, dt_dev) {
+		/* Return the first pci bridge device node simply */
+		if (!dt_property_read_string(dt_dev, "device_type", &type_str) &&
+			!strcmp(type_str, "pci"))
+			return dt_dev;

Not only this is very expensive, but also it doesn't seem to be even checking that it found the right pci bridge before returning it. We cannot just return the first one we find. If we need this function for testing, then please separate it out to a different patch (that won't get applied).

Quoted 70 lines
xen/drivers/passthrough/arm/smmu.c +56 -1
+	}
+
+	return NULL;
+}
+
+#define PCI_DEVID(bus, devfn)  ((((u16)(bus)) << 8) | (devfn))
+
+static int arm_smmu_pci_iommu_init(struct device *dev, u8 devfn)
+{
+	struct device_node *bridge_np;
+	struct of_phandle_args iommu_spec;
+	struct pci_dev *pdev = to_pci_dev(dev);
+	int ret;
+
+	bridge_np = pci_get_bridge_device_node(dev);
+	if (!bridge_np) {
+		dev_err(dev, "Cloud not find the pci bridge device node!\n");
+		return -ENODEV;
+	}
+
+	/*
+	 * Start by tracing the RID alias down the PCI topology as
+	 * far as the host bridge whose OF node we have...
+	 * (we're not even attempting to handle multi-alias devices yet)
+	 */
+	iommu_spec.args_count = 1;
+	iommu_spec.np = bridge_np;
+	ret = __arm_smmu_get_pci_sid(pdev, PCI_DEVID(pdev->bus, devfn),
+					&iommu_spec.args[0]);
+	if (ret) {
+		dev_err(dev, "Get pci requester ID failed, err=%d!\n", ret);
+		return ret;
+	}
+
+	/*
+	 * ...then find out what that becomes once it escapes the PCI
+	 * bus into the system beyond, and which IOMMU it ends up at.
+	 */
+	iommu_spec.np = NULL;
+	ret = dt_pci_map_rid(bridge_np, iommu_spec.args[0], "iommu-map",
+					"iommu-map-mask", &iommu_spec.np,
+					iommu_spec.args);
+	if (ret) {
+		dev_err(dev, "Do pci map rid failed, err=%d\n", ret);
+		return ret;
+	}
+
+	return arm_smmu_of_xlate(dev, &iommu_spec);
+}
+
+static int arm_smmu_xen_add_device(u8 devfn, struct device *dev)
 {
 	int ret;
 
@@ -2760,7 +2825,11 @@ static int arm_smmu_xen_add_device(u8 devfn, struct device*dev)
 	 * register Master IDs while this function had been invoked.
 	 */
 	if (using_generic_binding) {
-		ret = arm_smmu_platform_iommu_init(dev);
+		if (dev_is_pci(dev))
+			ret = arm_smmu_pci_iommu_init(dev, devfn);
+		else
+			ret = arm_smmu_platform_iommu_init(dev);
+
 		if (ret)
 			return ret;
 	}
Signature · 1 line
2.7.4

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

author
#6 lore raw

Re: [PATCH 2/2] xen/arm: smmu: Parse generic iommu binding for PCI devices

Hi Stefano,

Quoted 37 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 2017年7月7日 5:05
To: Wei Chen <[email protected]>
Cc: [email protected]; [email protected]; Julien Grall <[email protected]>; Steve Capper <[email protected]>; Kaly Xin <[email protected]>; nd <[email protected]>
Subject: Re: [PATCH 2/2] xen/arm: smmu: Parse generic iommu binding for PCI devices

Quoted 8 lines · Wei Chen

The legacy smmu binding will be deprecated in favour of the generic "iommus" binding. So we need a new helper to parse generic iommu bindings. When the system detects the SMMU is using generic iommu binding, this helper will be called when this platform device is assiged to a guest.

This patch is based on Linux of_iommu.c:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/driver
s/iommu/of_iommu.c

Quoted 6 lines

The commit id is:
2a0c57545a291f257cd231b1c4b18285b84608d8

---
 xen/drivers/passthrough/arm/smmu.c | 73

++++++++++++++++++++++++++++++++++++--

Review

Quoted 3 lines
 1 file changed, 71 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/xen/drivers/passthrough/arm/smmu.c

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_platform_iommu_init(struct device

Quoted 4 lines

index 25f2207..50ff997 100644

xen/drivers/passthrough/arm/smmu.c +0 -0
@@ -2750,7 +2750,72 @@ static int arm_smmu_platform_iommu_init(struct device

*dev)

Quoted 9 lines

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

Quoted 6 lines
xen/drivers/passthrough/arm/smmu.c +2 -1
 	return 0;
 }
 
-static int arm_smmu_xen_add_device(u8 devfn, struct device*dev)
+/*
+ * Currently, we haven't supported PCI device on ARM. So this is the

"Currently, we don't support PCI devices on ARM."

Ok.

Quoted 6 lines

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

Quoted 3 lines
xen/drivers/passthrough/arm/smmu.c +2 -0
+ * temporary function to get device node of pci bridge device for
+ * function verification only

What do you mean by "for function verification only"?

See comment below.

Quoted 10 lines
Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +9 -0
+ */
+static struct device_node *pci_get_bridge_device_node(struct device *dev)
+{
+	struct device_node *dt_dev;
+	const char *type_str;
+
+	dt_for_each_device_node(dt_host, dt_dev) {
+		/* Return the first pci bridge device node simply */
+		if (!dt_property_read_string(dt_dev, "device_type", &type_str) &&
Quoted 9 lines

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

Quoted 2 lines
xen/drivers/passthrough/arm/smmu.c +2 -0
+			!strcmp(type_str, "pci"))
+			return dt_dev;

Not only this is very expensive, but also it doesn't seem to be even checking that it found the right pci bridge before returning it. We cannot just return the first one we find. If we need this function for testing, then please separate it out to a different patch (that won't get applied).

I just used it to verify the translation of PCI RID on my board. The original intention of placing this patch to this series is to do a reminder:
After we support PCI devices on ARM, we should fix this function to return correct PCI bridge.

But now, it seems the worry is needless, I will separate this patch from this Series.

Quoted 57 lines

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_xen_add_device(u8 devfn, struct

Quoted 56 lines
xen/drivers/passthrough/arm/smmu.c +51 -0
+	}
+
+	return NULL;
+}
+
+#define PCI_DEVID(bus, devfn)  ((((u16)(bus)) << 8) | (devfn))
+
+static int arm_smmu_pci_iommu_init(struct device *dev, u8 devfn)
+{
+	struct device_node *bridge_np;
+	struct of_phandle_args iommu_spec;
+	struct pci_dev *pdev = to_pci_dev(dev);
+	int ret;
+
+	bridge_np = pci_get_bridge_device_node(dev);
+	if (!bridge_np) {
+		dev_err(dev, "Cloud not find the pci bridge device node!\n");
+		return -ENODEV;
+	}
+
+	/*
+	 * Start by tracing the RID alias down the PCI topology as
+	 * far as the host bridge whose OF node we have...
+	 * (we're not even attempting to handle multi-alias devices yet)
+	 */
+	iommu_spec.args_count = 1;
+	iommu_spec.np = bridge_np;
+	ret = __arm_smmu_get_pci_sid(pdev, PCI_DEVID(pdev->bus, devfn),
+					&iommu_spec.args[0]);
+	if (ret) {
+		dev_err(dev, "Get pci requester ID failed, err=%d!\n", ret);
+		return ret;
+	}
+
+	/*
+	 * ...then find out what that becomes once it escapes the PCI
+	 * bus into the system beyond, and which IOMMU it ends up at.
+	 */
+	iommu_spec.np = NULL;
+	ret = dt_pci_map_rid(bridge_np, iommu_spec.args[0], "iommu-map",
+					"iommu-map-mask", &iommu_spec.np,
+					iommu_spec.args);
+	if (ret) {
+		dev_err(dev, "Do pci map rid failed, err=%d\n", ret);
+		return ret;
+	}
+
+	return arm_smmu_of_xlate(dev, &iommu_spec);
+}
+
+static int arm_smmu_xen_add_device(u8 devfn, struct device *dev)
 {
 	int ret;
 
@@ -2760,7 +2825,11 @@ static int arm_smmu_xen_add_device(u8 devfn, struct

device*dev)

Quoted 15 lines
Quoted 13 lines
xen/drivers/passthrough/arm/smmu.c +5 -2
 	 * register Master IDs while this function had been invoked.
 	 */
 	if (using_generic_binding) {
-		ret = arm_smmu_platform_iommu_init(dev);
+		if (dev_is_pci(dev))
+			ret = arm_smmu_pci_iommu_init(dev, devfn);
+		else
+			ret = arm_smmu_platform_iommu_init(dev);
+
 		if (ret)
 			return ret;
 	}
--
Quoted 2 lines

2.7.4

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

#7 lore raw

Re: [PATCH 2/2] xen/arm: smmu: Parse generic iommu binding for PCI devices

Quoted 80 lines · Wei Chen

Hi Stefano,

Quoted 37 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 2017年7月7日 5:05
To: Wei Chen <[email protected]>
Cc: [email protected]; [email protected]; Julien Grall <[email protected]>; Steve Capper <[email protected]>; Kaly Xin <[email protected]>; nd <[email protected]>
Subject: Re: [PATCH 2/2] xen/arm: smmu: Parse generic iommu binding for PCI devices

Quoted 8 lines · Wei Chen

The legacy smmu binding will be deprecated in favour of the generic "iommus" binding. So we need a new helper to parse generic iommu bindings. When the system detects the SMMU is using generic iommu binding, this helper will be called when this platform device is assiged to a guest.

This patch is based on Linux of_iommu.c:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/driver
s/iommu/of_iommu.c

Quoted 6 lines

The commit id is:
2a0c57545a291f257cd231b1c4b18285b84608d8

---
 xen/drivers/passthrough/arm/smmu.c | 73

++++++++++++++++++++++++++++++++++++--

Review

Quoted 3 lines
 1 file changed, 71 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/xen/drivers/passthrough/arm/smmu.c

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_platform_iommu_init(struct device

Quoted 4 lines

index 25f2207..50ff997 100644

xen/drivers/passthrough/arm/smmu.c +0 -0
@@ -2750,7 +2750,72 @@ static int arm_smmu_platform_iommu_init(struct device

*dev)

Quoted 9 lines

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

Quoted 6 lines
xen/drivers/passthrough/arm/smmu.c +2 -1
     return 0;
 }
 
-static int arm_smmu_xen_add_device(u8 devfn, struct device*dev)
+/*
+ * Currently, we haven't supported PCI device on ARM. So this is the

"Currently, we don't support PCI devices on ARM."

Ok.

Quoted 6 lines

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

Quoted 3 lines
xen/drivers/passthrough/arm/smmu.c +2 -0
+ * temporary function to get device node of pci bridge device for
+ * function verification only

What do you mean by "for function verification only"?

See comment below.

Quoted 10 lines
Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +9 -0
+ */
+static struct device_node *pci_get_bridge_device_node(struct device *dev)
+{
+   struct device_node *dt_dev;
+   const char *type_str;
+
+   dt_for_each_device_node(dt_host, dt_dev) {
+           /* Return the first pci bridge device node simply */
+           if (!dt_property_read_string(dt_dev, "device_type", &type_str) &&
Quoted 9 lines

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

Quoted 2 lines
xen/drivers/passthrough/arm/smmu.c +2 -0
+                   !strcmp(type_str, "pci"))
+                   return dt_dev;

Not only this is very expensive, but also it doesn't seem to be even checking that it found the right pci bridge before returning it. We cannot just return the first one we find. If we need this function for testing, then please separate it out to a different patch (that won't get applied).

Quoted 7 lines

I just used it to verify the translation of PCI RID on my board. Theoriginal intention of placing this patch to this series is to do a reminder:
After we support PCI devices on ARM, we should fix this function to return correct PCI bridge.

But now, it seems the worry is needless, I will separate this patch fromthis Series.

Well, I am a little bit concerned to merge a code that we don't know whether it will work when PCI will be fully supported in Xen and until then would just be dead code.

I can see quite a few problems in this code, you assume BDF == RID. This is not true on all the platform. You should at least call pci_for_each_dma_alias. The function to_pci will return NULL, how can you get through the segfault?

But looking at the code, I am not even sure why this code is in ARM SMMU given that likely you will have to do for the same for all the SMMUs. Looking at Linux, they are calling of_iommu_xlate on each PCI devices.

Cheers,

Signature · 6 lines
Julien Grall

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