Skip to contentSkip to conversation

iommu/omap: Add support for iommu-groups and 'struct iommu_device'

PATCH Superseded Joerg Roedel 2 participants 11 messages Mar 31, 2017 – Apr 7, 2017 IOMMU

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Joerg Roedel

author maintainer
#0 lore raw

cover iommu/omap: Add support for iommu-groups and 'struct iommu_device'

Hi,

here is a small patch-set for the omap-iommu driver to make it use new features of the iommu-core. Please review.

Thanks,

Joerg

Joerg Roedel (5):
iommu/omap: Move data structures to omap-iommu.h iommu/omap: Permanently keep iommu_dev pointer in arch_data iommu/omap: Set dev->archdata.iommu = NULL in omap_iommu_remove_device iommu/omap: Make use of 'struct iommu_device'
iommu/omap: Add iommu-group support

 drivers/iommu/omap-iommu.c               | 137 ++++++++++++++++++++-----------
 drivers/iommu/omap-iommu.h               |  35 ++++++++
 include/linux/platform_data/iommu-omap.h |  15 ----
 3 files changed, 124 insertions(+), 63 deletions(-)
Signature · 1 line
1.9.1
author maintainer
#1 lore raw

PATCH 1/5 iommu/omap: Move data structures to omap-iommu.h

From: Joerg Roedel <[email protected]>

The internal data-structures are scattered over various header and C files. Consolidate them in omap-iommu.h.

---
 drivers/iommu/omap-iommu.c               | 16 ----------------
 drivers/iommu/omap-iommu.h               | 32 ++++++++++++++++++++++++++++++++
 include/linux/platform_data/iommu-omap.h | 15 ---------------
 3 files changed, 32 insertions(+), 31 deletions(-)
drivers/iommu/omap-iommu.c +0 -16
index e2583cc..e9c9b08 100644
@@ -42,22 +42,6 @@
 /* bitmap of the page sizes currently supported */
 #define OMAP_IOMMU_PGSIZES	(SZ_4K | SZ_64K | SZ_1M | SZ_16M)
 
-/**
- * struct omap_iommu_domain - omap iommu domain
- * @pgtable:	the page table
- * @iommu_dev:	an omap iommu device attached to this domain. only a single
- *		iommu device can be attached for now.
- * @dev:	Device using this domain.
- * @lock:	domain lock, should be taken when attaching/detaching
- */
-struct omap_iommu_domain {
-	u32 *pgtable;
-	struct omap_iommu *iommu_dev;
-	struct device *dev;
-	spinlock_t lock;
-	struct iommu_domain domain;
-};
-
 #define MMU_LOCK_BASE_SHIFT	10
 #define MMU_LOCK_BASE_MASK	(0x1f << MMU_LOCK_BASE_SHIFT)
 #define MMU_LOCK_BASE(x)	\
drivers/iommu/omap-iommu.h +32 -0
index 59628e5..3cd414a 100644
@@ -14,6 +14,7 @@
 #define _OMAP_IOMMU_H
 
 #include <linux/bitops.h>
+#include <linux/iommu.h>
 
 #define for_each_iotlb_cr(obj, n, __i, cr)				\
 	for (__i = 0;							\
@@ -27,6 +28,22 @@ struct iotlb_entry {
 	u32 endian, elsz, mixed;
 };
 
+/**
+ * struct omap_iommu_domain - omap iommu domain
+ * @pgtable:	the page table
+ * @iommu_dev:	an omap iommu device attached to this domain. only a single
+ *		iommu device can be attached for now.
+ * @dev:	Device using this domain.
+ * @lock:	domain lock, should be taken when attaching/detaching
+ */
+struct omap_iommu_domain {
+	u32 *pgtable;
+	struct omap_iommu *iommu_dev;
+	struct device *dev;
+	spinlock_t lock;
+	struct iommu_domain domain;
+};
+
 struct omap_iommu {
 	const char	*name;
 	void __iomem	*regbase;
@@ -52,6 +69,21 @@ struct omap_iommu {
 	u32 id;
 };
 
+/**
+ * struct iommu_arch_data - omap iommu private data
+ * @name: name of the iommu device
+ * @iommu_dev: handle of the iommu device
+ *
+ * This is an omap iommu private data object, which binds an iommu user
+ * to its iommu device. This object should be placed at the iommu user's
+ * dev_archdata so generic IOMMU API can be used without having to
+ * utilize omap-specific plumbing anymore.
+ */
+struct omap_iommu_arch_data {
+	const char *name;
+	struct omap_iommu *iommu_dev;
+};
+
 struct cr_regs {
 	u32 cam;
 	u32 ram;
include/linux/platform_data/iommu-omap.h +0 -15
index 0496d17..8c2a77c 100644
@@ -14,21 +14,6 @@
 
 #define MMU_REG_SIZE		256
 
-/**
- * struct iommu_arch_data - omap iommu private data
- * @name: name of the iommu device
- * @iommu_dev: handle of the iommu device
- *
- * This is an omap iommu private data object, which binds an iommu user
- * to its iommu device. This object should be placed at the iommu user's
- * dev_archdata so generic IOMMU API can be used without having to
- * utilize omap-specific plumbing anymore.
- */
-struct omap_iommu_arch_data {
-	const char *name;
-	struct omap_iommu *iommu_dev;
-};
-
 struct iommu_platform_data {
 	const char *name;
 	const char *reset_name;
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 1/5] iommu/omap: Move data structures to omap-iommu.h

Quoted 101 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

The internal data-structures are scattered over various header and C files. Consolidate them in omap-iommu.h.

---
 drivers/iommu/omap-iommu.c               | 16 ----------------
 drivers/iommu/omap-iommu.h               | 32 ++++++++++++++++++++++++++++++++
 include/linux/platform_data/iommu-omap.h | 15 ---------------
 3 files changed, 32 insertions(+), 31 deletions(-)
drivers/iommu/omap-iommu.c +0 -16
index e2583cc..e9c9b08 100644
@@ -42,22 +42,6 @@
 /* bitmap of the page sizes currently supported */
 #define OMAP_IOMMU_PGSIZES	(SZ_4K | SZ_64K | SZ_1M | SZ_16M)
 
-/**
- * struct omap_iommu_domain - omap iommu domain
- * @pgtable:	the page table
- * @iommu_dev:	an omap iommu device attached to this domain. only a single
- *		iommu device can be attached for now.
- * @dev:	Device using this domain.
- * @lock:	domain lock, should be taken when attaching/detaching
- */
-struct omap_iommu_domain {
-	u32 *pgtable;
-	struct omap_iommu *iommu_dev;
-	struct device *dev;
-	spinlock_t lock;
-	struct iommu_domain domain;
-};
-
 #define MMU_LOCK_BASE_SHIFT	10
 #define MMU_LOCK_BASE_MASK	(0x1f << MMU_LOCK_BASE_SHIFT)
 #define MMU_LOCK_BASE(x)	\
drivers/iommu/omap-iommu.h +32 -0
index 59628e5..3cd414a 100644
@@ -14,6 +14,7 @@
 #define _OMAP_IOMMU_H
 
 #include <linux/bitops.h>
+#include <linux/iommu.h>
 
 #define for_each_iotlb_cr(obj, n, __i, cr)				\
 	for (__i = 0;							\
@@ -27,6 +28,22 @@ struct iotlb_entry {
 	u32 endian, elsz, mixed;
 };
 
+/**
+ * struct omap_iommu_domain - omap iommu domain
+ * @pgtable:	the page table
+ * @iommu_dev:	an omap iommu device attached to this domain. only a single
+ *		iommu device can be attached for now.
+ * @dev:	Device using this domain.
+ * @lock:	domain lock, should be taken when attaching/detaching
+ */
+struct omap_iommu_domain {
+	u32 *pgtable;
+	struct omap_iommu *iommu_dev;
+	struct device *dev;
+	spinlock_t lock;
+	struct iommu_domain domain;
+};
+
 struct omap_iommu {
 	const char	*name;
 	void __iomem	*regbase;
@@ -52,6 +69,21 @@ struct omap_iommu {
 	u32 id;
 };
 
+/**
+ * struct iommu_arch_data - omap iommu private data
+ * @name: name of the iommu device
+ * @iommu_dev: handle of the iommu device
+ *
+ * This is an omap iommu private data object, which binds an iommu user
+ * to its iommu device. This object should be placed at the iommu user's
+ * dev_archdata so generic IOMMU API can be used without having to
+ * utilize omap-specific plumbing anymore.
+ */
+struct omap_iommu_arch_data {
+	const char *name;
+	struct omap_iommu *iommu_dev;
+};
+
 struct cr_regs {
 	u32 cam;
 	u32 ram;
include/linux/platform_data/iommu-omap.h +0 -0
index 0496d17..8c2a77c 100644

include/linux/platform_data/iommu-omap.h line 15

Quoted 2 lines
include/linux/platform_data/iommu-omap.h +0 -0
@@ -14,21 +14,6 @@
 
 #define MMU_REG_SIZE		256

This define can be dropped as well since you are already modifying this file, it's already defined in drivers/iommu/omap-iommu.h.

regards
Suman

Quoted 20 lines
include/linux/platform_data/iommu-omap.h +0 -15
-/**
- * struct iommu_arch_data - omap iommu private data
- * @name: name of the iommu device
- * @iommu_dev: handle of the iommu device
- *
- * This is an omap iommu private data object, which binds an iommu user
- * to its iommu device. This object should be placed at the iommu user's
- * dev_archdata so generic IOMMU API can be used without having to
- * utilize omap-specific plumbing anymore.
- */
-struct omap_iommu_arch_data {
-	const char *name;
-	struct omap_iommu *iommu_dev;
-};
-
 struct iommu_platform_data {
 	const char *name;
 	const char *reset_name;
 
author maintainer
#3 lore raw

PATCH 2/5 iommu/omap: Permanently keep iommu_dev pointer in arch_data

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Instead of finding the matching IOMMU for a device using string comparision functions, keep the pointer to the iommu_dev in arch_data permanently populated.

---
 drivers/iommu/omap-iommu.c | 56 ++++++++++++++++++++--------------------------
 drivers/iommu/omap-iommu.h |  2 +-
 2 files changed, 25 insertions(+), 33 deletions(-)
drivers/iommu/omap-iommu.c +24 -32
index e9c9b08..45df5c8 100644
@@ -802,33 +802,14 @@ static irqreturn_t iommu_fault_handler(int irq, void *data)
 	return IRQ_NONE;
 }
 
-static int device_match_by_alias(struct device *dev, void *data)
-{
-	struct omap_iommu *obj = to_iommu(dev);
-	const char *name = data;
-
-	pr_debug("%s: %s %s\n", __func__, obj->name, name);
-
-	return strcmp(obj->name, name) == 0;
-}
-
 /**
  * omap_iommu_attach() - attach iommu device to an iommu domain
- * @name:	name of target omap iommu device
+ * @obj:	target omap iommu device
  * @iopgd:	page table
  **/
-static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
+static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
 {
 	int err;
-	struct device *dev;
-	struct omap_iommu *obj;
-
-	dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
-				 device_match_by_alias);
-	if (!dev)
-		return ERR_PTR(-ENODEV);
-
-	obj = to_iommu(dev);
 
 	spin_lock(&obj->iommu_lock);
 
@@ -841,11 +822,13 @@ static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
 	spin_unlock(&obj->iommu_lock);
 
 	dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
-	return obj;
+
+	return 0;
 
 err_enable:
 	spin_unlock(&obj->iommu_lock);
-	return ERR_PTR(err);
+
+	return err;
 }
 
 /**
@@ -1061,11 +1044,11 @@ static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
 omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
 {
 	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
-	struct omap_iommu *oiommu;
 	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
+	struct omap_iommu *oiommu;
 	int ret = 0;
 
-	if (!arch_data || !arch_data->name) {
+	if (!arch_data || !arch_data->iommu_dev) {
 		dev_err(dev, "device doesn't have an associated iommu\n");
 		return -EINVAL;
 	}
@@ -1079,15 +1062,17 @@ static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
 		goto out;
 	}
 
+	oiommu = arch_data->iommu_dev;
+
 	/* get a handle to and enable the omap iommu */
-	oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
-	if (IS_ERR(oiommu)) {
-		ret = PTR_ERR(oiommu);
+	ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
+	if (ret) {
 		dev_err(dev, "can't get omap iommu: %d\n", ret);
 		goto out;
 	}
 
-	omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
+	arch_data->domain = omap_domain;
+	omap_domain->iommu_dev = oiommu;
 	omap_domain->dev = dev;
 	oiommu->domain = domain;
 
@@ -1112,7 +1097,8 @@ static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
 
 	omap_iommu_detach(oiommu);
 
-	omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
+	omap_domain->iommu_dev = NULL;
+	arch_data->domain = NULL;
 	omap_domain->dev = NULL;
 	oiommu->domain = NULL;
 }
@@ -1216,6 +1202,7 @@ static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
 static int omap_iommu_add_device(struct device *dev)
 {
 	struct omap_iommu_arch_data *arch_data;
+	struct omap_iommu *iommu;
 	struct device_node *np;
 	struct platform_device *pdev;
 
@@ -1238,13 +1225,19 @@ static int omap_iommu_add_device(struct device *dev)
 		return -EINVAL;
 	}
 
+	iommu = platform_get_drvdata(pdev);
+	if (!iommu) {
+		of_node_put(np);
+		return -EINVAL;
+	}
+
 	arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
 	if (!arch_data) {
 		of_node_put(np);
 		return -ENOMEM;
 	}
 
-	arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
+	arch_data->iommu_dev = iommu;
 	dev->archdata.iommu = arch_data;
 
 	of_node_put(np);
@@ -1259,7 +1252,6 @@ static void omap_iommu_remove_device(struct device *dev)
 	if (!dev->of_node || !arch_data)
 		return;
 
-	kfree(arch_data->name);
 	kfree(arch_data);
 }
 
drivers/iommu/omap-iommu.h +1 -1
index 3cd414a..05bd279 100644
@@ -80,8 +80,8 @@ struct omap_iommu {
  * utilize omap-specific plumbing anymore.
  */
 struct omap_iommu_arch_data {
-	const char *name;
 	struct omap_iommu *iommu_dev;
+	struct omap_iommu_domain *domain;
 };
 
 struct cr_regs {
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH 2/5] iommu/omap: Permanently keep iommu_dev pointer in arch_data

Hi Joerg,

Quoted 123 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

Instead of finding the matching IOMMU for a device using string comparision functions, keep the pointer to the iommu_dev in arch_data permanently populated.

---
 drivers/iommu/omap-iommu.c | 56 ++++++++++++++++++++--------------------------
 drivers/iommu/omap-iommu.h |  2 +-
 2 files changed, 25 insertions(+), 33 deletions(-)
drivers/iommu/omap-iommu.c +17 -30
index e9c9b08..45df5c8 100644
@@ -802,33 +802,14 @@ static irqreturn_t iommu_fault_handler(int irq, void *data)
 	return IRQ_NONE;
 }
 
-static int device_match_by_alias(struct device *dev, void *data)
-{
-	struct omap_iommu *obj = to_iommu(dev);
-	const char *name = data;
-
-	pr_debug("%s: %s %s\n", __func__, obj->name, name);
-
-	return strcmp(obj->name, name) == 0;
-}
-
 /**
  * omap_iommu_attach() - attach iommu device to an iommu domain
- * @name:	name of target omap iommu device
+ * @obj:	target omap iommu device
  * @iopgd:	page table
  **/
-static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
+static int omap_iommu_attach(struct omap_iommu *obj, u32 *iopgd)
 {
 	int err;
-	struct device *dev;
-	struct omap_iommu *obj;
-
-	dev = driver_find_device(&omap_iommu_driver.driver, NULL, (void *)name,
-				 device_match_by_alias);
-	if (!dev)
-		return ERR_PTR(-ENODEV);
-
-	obj = to_iommu(dev);
 
 	spin_lock(&obj->iommu_lock);
 
@@ -841,11 +822,13 @@ static struct omap_iommu *omap_iommu_attach(const char *name, u32 *iopgd)
 	spin_unlock(&obj->iommu_lock);
 
 	dev_dbg(obj->dev, "%s: %s\n", __func__, obj->name);
-	return obj;
+
+	return 0;
 
 err_enable:
 	spin_unlock(&obj->iommu_lock);
-	return ERR_PTR(err);
+
+	return err;
 }
 
 /**
@@ -1061,11 +1044,11 @@ static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
 omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
 {
 	struct omap_iommu_domain *omap_domain = to_omap_domain(domain);
-	struct omap_iommu *oiommu;
 	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
+	struct omap_iommu *oiommu;
 	int ret = 0;
 
-	if (!arch_data || !arch_data->name) {
+	if (!arch_data || !arch_data->iommu_dev) {
 		dev_err(dev, "device doesn't have an associated iommu\n");
 		return -EINVAL;
 	}
@@ -1079,15 +1062,17 @@ static size_t omap_iommu_unmap(struct iommu_domain *domain, unsigned long da,
 		goto out;
 	}
 
+	oiommu = arch_data->iommu_dev;
+
 	/* get a handle to and enable the omap iommu */
-	oiommu = omap_iommu_attach(arch_data->name, omap_domain->pgtable);
-	if (IS_ERR(oiommu)) {
-		ret = PTR_ERR(oiommu);
+	ret = omap_iommu_attach(oiommu, omap_domain->pgtable);
+	if (ret) {
 		dev_err(dev, "can't get omap iommu: %d\n", ret);
 		goto out;
 	}
 
-	omap_domain->iommu_dev = arch_data->iommu_dev = oiommu;
+	arch_data->domain = omap_domain;
+	omap_domain->iommu_dev = oiommu;
 	omap_domain->dev = dev;
 	oiommu->domain = domain;
 
@@ -1112,7 +1097,8 @@ static void _omap_iommu_detach_dev(struct omap_iommu_domain *omap_domain,
 
 	omap_iommu_detach(oiommu);
 
-	omap_domain->iommu_dev = arch_data->iommu_dev = NULL;
+	omap_domain->iommu_dev = NULL;
+	arch_data->domain = NULL;
 	omap_domain->dev = NULL;
 	oiommu->domain = NULL;
 }
@@ -1216,6 +1202,7 @@ static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
 static int omap_iommu_add_device(struct device *dev)
 {
 	struct omap_iommu_arch_data *arch_data;
+	struct omap_iommu *iommu;
 	struct device_node *np;
 	struct platform_device *pdev;
 

drivers/iommu/omap-iommu.c static int omap_iommu_add_device(struct device *dev) · line 1232

Quoted 8 lines
drivers/iommu/omap-iommu.c +5 -0
@@ -1238,13 +1225,19 @@ static int omap_iommu_add_device(struct device *dev)
 		return -EINVAL;
 	}
 
+	iommu = platform_get_drvdata(pdev);
+	if (!iommu) {
+		of_node_put(np);
+		return -EINVAL;
+	}

This change is causing the issues. OMAP IOMMU driver is not probed yet, but this gets called during the driver's init function in bus_set_iommu() and bus's iommu_ops gets set to NULL. The add_device today is used to add the linking to an IOMMU device (currently name primarily to support the legacy non-DT mode which is no longer an issue, but a dev pointer can be used instead here, and the real enabling is done during the domain's attach_dev callback.

Quoted 25 lines
drivers/iommu/omap-iommu.c +2 -2
+
 	arch_data = kzalloc(sizeof(*arch_data), GFP_KERNEL);
 	if (!arch_data) {
 		of_node_put(np);
 		return -ENOMEM;
 	}
 
-	arch_data->name = kstrdup(dev_name(&pdev->dev), GFP_KERNEL);
+	arch_data->iommu_dev = iommu;
 	dev->archdata.iommu = arch_data;
 
 	of_node_put(np);
@@ -1259,7 +1252,6 @@ static void omap_iommu_remove_device(struct device *dev)
 	if (!dev->of_node || !arch_data)
 		return;
 
-	kfree(arch_data->name);
 	kfree(arch_data);
 }
 
drivers/iommu/omap-iommu.h +0 -0
index 3cd414a..05bd279 100644

drivers/iommu/omap-iommu.h struct omap_iommu { · line 83

Quoted 4 lines
drivers/iommu/omap-iommu.h +0 -1
@@ -80,8 +80,8 @@ struct omap_iommu {
  * utilize omap-specific plumbing anymore.
  */
 struct omap_iommu_arch_data {
-	const char *name;

Missing the removal from the structure kerneldoc documentation.

drivers/iommu/omap-iommu.h struct omap_iommu { · line 84

Quoted 2 lines
drivers/iommu/omap-iommu.h +1 -0
 	struct omap_iommu *iommu_dev;
+	struct omap_iommu_domain *domain;

This doesn't seem to serve any purpose for now, is there a plan/purpose for this? The iommu_domain is being stored within the omap_iommu structure at the moment and it is stored during attach_dev() callback.

regards
Suman

Quoted 4 lines
drivers/iommu/omap-iommu.h +0 -0
 };
 
 struct cr_regs {
author maintainer
#5 lore raw

Re: [PATCH 2/5] iommu/omap: Permanently keep iommu_dev pointer in arch_data

Hi Suman,

Quoted 6 lines · Suman Anna

drivers/iommu/omap-iommu.c static int omap_iommu_add_device(struct device *dev) · line 1232

Quoted 5 lines
drivers/iommu/omap-iommu.c +5 -0
+	iommu = platform_get_drvdata(pdev);
+	if (!iommu) {
+		of_node_put(np);
+		return -EINVAL;
+	}
Quoted 7 lines

This change is causing the issues. OMAP IOMMU driver is not probed yet, but this gets called during the driver's init function in bus_set_iommu() and bus's iommu_ops gets set to NULL. The add_device today is used to add the linking to an IOMMU device (currently name primarily to support the legacy non-DT mode which is no longer an issue, but a dev pointer can be used instead here, and the real enabling is done during the domain's attach_dev callback.

Yeah, okay. Solving this problem requires more sophisticated handling in the iommu core code, which is not implemented yet.

I drop this patch for now and do the device-linking and group-handling in attach_dev. This makes iommu-groups on omap useless for now, but it is a start.

Joerg

author maintainer
#6 lore raw

PATCH 3/5 iommu/omap: Set dev->archdata.iommu = NULL in omap_iommu_remove_device

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Don't leave a stale pointer in case the device continues to exist for some more time.

---
 drivers/iommu/omap-iommu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/omap-iommu.c +1 -0
index 45df5c8..04b3718 100644
@@ -1252,6 +1252,7 @@ static void omap_iommu_remove_device(struct device *dev)
 	if (!dev->of_node || !arch_data)
 		return;
 
+	dev->archdata.iommu = NULL;
 	kfree(arch_data);
 }
 
Signature · 1 line
1.9.1
#7 lore raw

Re: [PATCH 3/5] iommu/omap: Set dev->archdata.iommu = NULL in omap_iommu_remove_device

Hi Joerg,

Quoted 15 lines · Joerg Roedel

From: Joerg Roedel <[email protected]>

Don't leave a stale pointer in case the device continues to exist for some more time.

---
 drivers/iommu/omap-iommu.c | 1 +
 1 file changed, 1 insertion(+)
drivers/iommu/omap-iommu.c +0 -0
index 45df5c8..04b3718 100644

drivers/iommu/omap-iommu.c static void omap_iommu_remove_device(struct device *dev) · line 1255

Quoted 4 lines
drivers/iommu/omap-iommu.c +1 -0
@@ -1252,6 +1252,7 @@ static void omap_iommu_remove_device(struct device *dev)
 	if (!dev->of_node || !arch_data)
 		return;
 
+	dev->archdata.iommu = NULL;

This can be squashed into Patch 2 to go alongside the matching change in omap_iommu_attach_device().

regards
Suman

Quoted 4 lines
drivers/iommu/omap-iommu.c +0 -0
 	kfree(arch_data);
 }
 
author maintainer
#8 lore raw

PATCH 4/5 iommu/omap: Make use of 'struct iommu_device'

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Modify the driver to register individual iommus and establish links between devices and iommus in sysfs.

---
 drivers/iommu/omap-iommu.c | 25 +++++++++++++++++++++++++
 drivers/iommu/omap-iommu.h |  2 ++
 2 files changed, 27 insertions(+)
drivers/iommu/omap-iommu.c +25 -0
index 04b3718..5e3a946 100644
@@ -36,6 +36,8 @@
 #include "omap-iopgtable.h"
 #include "omap-iommu.h"
 
+static const struct iommu_ops omap_iommu_ops;
+
 #define to_iommu(dev)							\
 	((struct omap_iommu *)platform_get_drvdata(to_platform_device(dev)))
 
@@ -946,6 +948,16 @@ static int omap_iommu_probe(struct platform_device *pdev)
 	pm_runtime_irq_safe(obj->dev);
 	pm_runtime_enable(obj->dev);
 
+	err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name);
+	if (err)
+		return err;
+
+	iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
+
+	err = iommu_device_register(&obj->iommu);
+	if (err)
+		return err;
+
 	omap_iommu_debugfs_add(obj);
 
 	dev_info(&pdev->dev, "%s registered\n", obj->name);
@@ -956,6 +968,9 @@ static int omap_iommu_remove(struct platform_device *pdev)
 {
 	struct omap_iommu *obj = platform_get_drvdata(pdev);
 
+	iommu_device_sysfs_remove(&obj->iommu);
+	iommu_device_unregister(&obj->iommu);
+
 	omap_iommu_debugfs_remove(obj);
 
 	pm_runtime_disable(obj->dev);
@@ -1205,6 +1220,7 @@ static int omap_iommu_add_device(struct device *dev)
 	struct omap_iommu *iommu;
 	struct device_node *np;
 	struct platform_device *pdev;
+	int ret;
 
 	/*
 	 * Allocate the archdata iommu structure for DT-based devices.
@@ -1237,6 +1253,12 @@ static int omap_iommu_add_device(struct device *dev)
 		return -ENOMEM;
 	}
 
+	ret = iommu_device_link(&iommu->iommu, dev);
+	if (ret) {
+		of_node_put(np);
+		return ret;
+	}
+
 	arch_data->iommu_dev = iommu;
 	dev->archdata.iommu = arch_data;
 
@@ -1252,8 +1274,11 @@ static void omap_iommu_remove_device(struct device *dev)
 	if (!dev->of_node || !arch_data)
 		return;
 
+	iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
+
 	dev->archdata.iommu = NULL;
 	kfree(arch_data);
+
 }
 
 static const struct iommu_ops omap_iommu_ops = {
drivers/iommu/omap-iommu.h +2 -0
index 05bd279..07e6ea1 100644
@@ -67,6 +67,8 @@ struct omap_iommu {
 
 	int has_bus_err_back;
 	u32 id;
+
+	struct iommu_device iommu;
 };
 
 /**
Signature · 1 line
1.9.1
author maintainer
#9 lore raw

PATCH 5/5 iommu/omap: Add iommu-group support

In reply to Joerg Roedel

From: Joerg Roedel <[email protected]>

Support for IOMMU groups will become mandatory for drivers, so add it to the omap iommu driver.

---
 drivers/iommu/omap-iommu.c | 43 +++++++++++++++++++++++++++++++++++++++++--
 drivers/iommu/omap-iommu.h |  1 +
 2 files changed, 42 insertions(+), 2 deletions(-)
drivers/iommu/omap-iommu.c +41 -2
index 5e3a946..bd5e014 100644
@@ -948,26 +948,42 @@ static int omap_iommu_probe(struct platform_device *pdev)
 	pm_runtime_irq_safe(obj->dev);
 	pm_runtime_enable(obj->dev);
 
+	obj->group = iommu_group_alloc();
+	if (IS_ERR(obj->group))
+		return PTR_ERR(obj->group);
+
 	err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL, obj->name);
 	if (err)
-		return err;
+		goto out_group;
 
 	iommu_device_set_ops(&obj->iommu, &omap_iommu_ops);
 
 	err = iommu_device_register(&obj->iommu);
 	if (err)
-		return err;
+		goto out_sysfs;
 
 	omap_iommu_debugfs_add(obj);
 
 	dev_info(&pdev->dev, "%s registered\n", obj->name);
+
 	return 0;
+
+out_sysfs:
+	iommu_device_sysfs_remove(&obj->iommu);
+
+out_group:
+	iommu_group_put(obj->group);
+
+	return err;
 }
 
 static int omap_iommu_remove(struct platform_device *pdev)
 {
 	struct omap_iommu *obj = platform_get_drvdata(pdev);
 
+	iommu_group_put(obj->group);
+	obj->group = NULL;
+
 	iommu_device_sysfs_remove(&obj->iommu);
 	iommu_device_unregister(&obj->iommu);
 
@@ -1217,6 +1233,7 @@ static phys_addr_t omap_iommu_iova_to_phys(struct iommu_domain *domain,
 static int omap_iommu_add_device(struct device *dev)
 {
 	struct omap_iommu_arch_data *arch_data;
+	struct iommu_group *group;
 	struct omap_iommu *iommu;
 	struct device_node *np;
 	struct platform_device *pdev;
@@ -1262,6 +1279,15 @@ static int omap_iommu_add_device(struct device *dev)
 	arch_data->iommu_dev = iommu;
 	dev->archdata.iommu = arch_data;
 
+	/*
+	 * IOMMU group initialization calls into omap_device_group, which needs
+	 * a valid dev->archdata.iommu pointer
+	 */
+	group = iommu_group_get_for_dev(dev);
+	if (IS_ERR(group))
+		return PTR_ERR(group);
+	iommu_group_put(group);
+
 	of_node_put(np);
 
 	return 0;
@@ -1275,12 +1301,24 @@ static void omap_iommu_remove_device(struct device *dev)
 		return;
 
 	iommu_device_unlink(&arch_data->iommu_dev->iommu, dev);
+	iommu_group_remove_device(dev);
 
 	dev->archdata.iommu = NULL;
 	kfree(arch_data);
 
 }
 
+static struct iommu_group *omap_device_group(struct device *dev)
+{
+	struct omap_iommu_arch_data *arch_data = dev->archdata.iommu;
+	struct iommu_group *group = NULL;
+
+	if (arch_data->iommu_dev)
+		group = arch_data->iommu_dev->group;
+
+	return group;
+}
+
 static const struct iommu_ops omap_iommu_ops = {
 	.domain_alloc	= omap_iommu_domain_alloc,
 	.domain_free	= omap_iommu_domain_free,
@@ -1292,6 +1330,7 @@ static void omap_iommu_remove_device(struct device *dev)
 	.iova_to_phys	= omap_iommu_iova_to_phys,
 	.add_device	= omap_iommu_add_device,
 	.remove_device	= omap_iommu_remove_device,
+	.device_group	= omap_device_group,
 	.pgsize_bitmap	= OMAP_IOMMU_PGSIZES,
 };
 
drivers/iommu/omap-iommu.h +1 -0
index 07e6ea1..28483c5 100644
@@ -69,6 +69,7 @@ struct omap_iommu {
 	u32 id;
 
 	struct iommu_device iommu;
+	struct iommu_group *group;
 };
 
 /**
Signature · 1 line
1.9.1
#10 lore raw
In reply to Joerg Roedel

Hi Joerg,

Quoted 4 lines · Joerg Roedel

Hi,

here is a small patch-set for the omap-iommu driver to make it use new features of the iommu-core. Please review.

Thanks for the patches - this has been on my TODO list but you beat me in getting there first :). Are these for 4.12?

I have given the series a quick try and these currently break the functionality. I am investigating the failures, so will mostly need some additional changes. Let me know if you want me to take up posting of the v2. Meanwhile, I have added some comments on the patches.

regards
Suman

Quoted 17 lines

Thanks,

Joerg

Joerg Roedel (5):
iommu/omap: Move data structures to omap-iommu.h iommu/omap: Permanently keep iommu_dev pointer in arch_data iommu/omap: Set dev->archdata.iommu = NULL in omap_iommu_remove_device iommu/omap: Make use of 'struct iommu_device'
iommu/omap: Add iommu-group support

 drivers/iommu/omap-iommu.c               | 137 ++++++++++++++++++++-----------
 drivers/iommu/omap-iommu.h               |  35 ++++++++
 include/linux/platform_data/iommu-omap.h |  15 ----
 3 files changed, 124 insertions(+), 63 deletions(-)