Skip to contentSkip to conversation

iommu/tegra-smmu: Add PCI support

PATCH Superseded Nicolin Chen 3 participants 46 messages Oct 2, 2020 – Oct 9, 2020

Subsystems (3)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover iommu/tegra-smmu: Add PCI support

This series is to add PCI support in tegra-smmu driver.

Changelog (Detail in each patch)
v3->v4
* Dropped helper function
* Found another way to get smmu pointer
v2->v3
* Replaced with devm_tegra_get_memory_controller
* Updated changes by following Dmitry's comments
v1->v2
* Added PATCH-1 suggested by Dmitry
* Reworked PATCH-2 to unify certain code

Nicolin Chen (3):
iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev iommu/tegra-smmu: Rework tegra_smmu_probe_device() iommu/tegra-smmu: Add PCI support

 drivers/iommu/tegra-smmu.c | 177 ++++++++++++-------------------------
 1 file changed, 56 insertions(+), 121 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/3 iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

In tegra_smmu_(de)attach_dev() functions, we poll DTB for each client's iommus property to get swgroup ID in order to prepare "as" and enable smmu. Actually tegra_smmu_configure() prepared an fwspec for each client, and added to the fwspec all swgroup IDs of client DT node in DTB.

So this patch uses fwspec in tegra_smmu_(de)attach_dev() so as to replace the redundant DT polling code.

---

Changelog
v3->v4:
* Seperated the change, as a cleanup, from the rework patch
v1->v3:
* N/A

 drivers/iommu/tegra-smmu.c | 50 +++++++++++++++-----------------------
 1 file changed, 20 insertions(+), 30 deletions(-)
drivers/iommu/tegra-smmu.c +20 -30
index 6a3ecc334481..a573a5151c69 100644
@@ -484,60 +484,50 @@ static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
 static int tegra_smmu_attach_dev(struct iommu_domain *domain,
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;
 
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return -ENOENT;
 
+	for (index = 0; index < fwspec->num_ids; index++) {
 		err = tegra_smmu_as_prepare(smmu, as);
-		if (err < 0)
-			return err;
+		if (err)
+			goto disable;
 
-		tegra_smmu_enable(smmu, swgroup, as->id);
-		index++;
+		tegra_smmu_enable(smmu, fwspec->ids[index], as->id);
 	}
 
 	if (index == 0)
 		return -ENODEV;
 
 	return 0;
+
+disable:
+	while (index--) {
+		tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
+		tegra_smmu_as_unprepare(smmu, as);
+	}
+
+	return err;
 }
 
 static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
 	struct tegra_smmu *smmu = as->smmu;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return;
 
-		tegra_smmu_disable(smmu, swgroup, as->id);
+	for (index = 0; index < fwspec->num_ids; index++) {
+		tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
 		tegra_smmu_as_unprepare(smmu, as);
-		index++;
 	}
 }
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 491

Quoted 10 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -2
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;

Looks like there is no need to initialize 'index' and 'err' variables anymore.

#3 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 15 lines

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 491

Quoted 10 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -2
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;

Looks like there is no need to initialize 'index' and 'err' variables anymore.

Same for tegra_smmu_detach_dev().

#4 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

Quoted 17 lines · Dmitry Osipenko

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 15 lines

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 491

Quoted 10 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -2
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;

Looks like there is no need to initialize 'index' and 'err' variables anymore.

Quoted 1 line

Same for tegra_smmu_detach_dev().

Can remove them.

#5 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

In reply to Nicolin Chen

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 11 lines

static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev) {

drivers/iommu/tegra-smmu.c +1 -4
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
 	struct tegra_smmu *smmu = as->smmu;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 526

Quoted 10 lines
drivers/iommu/tegra-smmu.c +2 -8
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return;

When !fwspec could be true?

#6 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

In reply to Nicolin Chen

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 13 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -4
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;
 
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 494

Quoted 10 lines
drivers/iommu/tegra-smmu.c +2 -8
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return -ENOENT;

Could the !fwspec ever be true here as well?

#7 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

Quoted 12 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 11 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -2
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;
Quoted 14 lines

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 494

Quoted 12 lines
drivers/iommu/tegra-smmu.c +2 -10
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return -ENOENT;

Could the !fwspec ever be true here as well?

There are multiple callers of this function. It's really not that straightforward to track every one of them. So I'd rather have it here as other iommu drivers do. We are human beings, so we could have missed something somewhere, especially callers are not from tegra-* drivers.

#8 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

02.10.2020 22:45, Nicolin Chen пишет:

Quoted 28 lines · Dmitry Osipenko
Quoted 12 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 11 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -2
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;
Quoted 14 lines

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 494

Quoted 12 lines
drivers/iommu/tegra-smmu.c +2 -10
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return -ENOENT;

Could the !fwspec ever be true here as well?

Quoted 6 lines

There are multiple callers of this function. It's really not that straightforward to track every one of them. So I'd rather have it here as other iommu drivers do. We are human beings, so we could have missed something somewhere, especially callers are not from tegra-* drivers.

I'm looking at the IOMMU core and it requires device to be in IOMMU group before attach_dev() could be called.

The group can't be assigned to device without the fwspec, see tegra_smmu_device_group().

Seems majority of IOMMU drivers are checking dev_iommu_priv_get() for NULL in attach_dev(), some not checking anything, some check both and only arm-smmu checks the fwspec.

#9 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

Quoted 39 lines · Dmitry Osipenko

02.10.2020 22:45, Nicolin Chen пишет:

Quoted 28 lines · Dmitry Osipenko
Quoted 12 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 11 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -2
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;
Quoted 14 lines

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 494

Quoted 12 lines
drivers/iommu/tegra-smmu.c +2 -10
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return -ENOENT;

Could the !fwspec ever be true here as well?

Quoted 6 lines

There are multiple callers of this function. It's really not that straightforward to track every one of them. So I'd rather have it here as other iommu drivers do. We are human beings, so we could have missed something somewhere, especially callers are not from tegra-* drivers.

I'm looking at the IOMMU core and it requires device to be in IOMMU group before attach_dev() could be called.

Quoted 6 lines

The group can't be assigned to device without the fwspec, see tegra_smmu_device_group().

Seems majority of IOMMU drivers are checking dev_iommu_priv_get() for NULL in attach_dev(), some not checking anything, some check both and only arm-smmu checks the fwspec.

As I said a couple of days ago, I don't like to assume that the callers won't change. And this time, it's from open code. So I don't want to assume that there won't be a change.

If you are confident that there is no need to add such a check, please send patches to remove those checks in those drivers to see if others would agree. I would be willing to remove it after that. Otherwise, I'd like to keep this.

Thanks for the review.

#10 lore raw

Re: [PATCH v4 1/3] iommu/tegra-smmu: Use fwspec in tegra_smmu_(de)attach_dev

03.10.2020 02:53, Nicolin Chen пишет:

Quoted 51 lines · Dmitry Osipenko
Quoted 39 lines

02.10.2020 22:45, Nicolin Chen пишет:

Quoted 28 lines · Dmitry Osipenko
Quoted 12 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 11 lines

static int tegra_smmu_attach_dev(struct iommu_domain *domain,

drivers/iommu/tegra-smmu.c +1 -2
 				 struct device *dev)
 {
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
 	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 	struct tegra_smmu_as *as = to_smmu_as(domain);
-	struct device_node *np = dev->of_node;
-	struct of_phandle_args args;
 	unsigned int index = 0;
 	int err = 0;
Quoted 14 lines

drivers/iommu/tegra-smmu.c static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu, · line 494

Quoted 12 lines
drivers/iommu/tegra-smmu.c +2 -10
-	while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					   &args)) {
-		unsigned int swgroup = args.args[0];
-
-		if (args.np != smmu->dev->of_node) {
-			of_node_put(args.np);
-			continue;
-		}
-
-		of_node_put(args.np);
+	if (!fwspec)
+		return -ENOENT;

Could the !fwspec ever be true here as well?

Quoted 6 lines

There are multiple callers of this function. It's really not that straightforward to track every one of them. So I'd rather have it here as other iommu drivers do. We are human beings, so we could have missed something somewhere, especially callers are not from tegra-* drivers.

I'm looking at the IOMMU core and it requires device to be in IOMMU group before attach_dev() could be called.

Quoted 6 lines

The group can't be assigned to device without the fwspec, see tegra_smmu_device_group().

Seems majority of IOMMU drivers are checking dev_iommu_priv_get() for NULL in attach_dev(), some not checking anything, some check both and only arm-smmu checks the fwspec.

As I said a couple of days ago, I don't like to assume that the callers won't change. And this time, it's from open code. So I don't want to assume that there won't be a change.

Quoted 7 lines

If you are confident that there is no need to add such a check, please send patches to remove those checks in those drivers to see if others would agree. I would be willing to remove it after that. Otherwise, I'd like to keep this.

Thanks for the review.

I haven't tried to check every code path very thoroughly, expecting you to do it since you're making this patch. Maybe there is a real reason why majority of drivers do the checks and it would be good to know why. Although, it's not critical in this particular case and indeed the checks could be improved later on.

It looks to me that at least will be a bit better/cleaner to check the dev_iommu_priv_get() for NULL instead of fwspec because the private variable depends on the fwspec presence and there is a similar check in probe_device, hence checks will be more consistent.

#11 lore raw

PATCH v4 2/3 iommu/tegra-smmu: Rework tegra_smmu_probe_device()

In reply to Nicolin Chen

The bus_set_iommu() in tegra_smmu_probe() enumerates all clients to call in tegra_smmu_probe_device() where each client searches its DT node for smmu pointer and swgroup ID, so as to configure an fwspec. But this requires a valid smmu pointer even before mc and smmu drivers are probed. So in tegra_smmu_probe() we added a line of code to fill mc->smmu, marking "a bit of a hack".

This works for most of clients in the DTB, however, doesn't work for a client that doesn't exist in DTB, a PCI device for example.

Actually, if we return ERR_PTR(-ENODEV) in ->probe_device() when it's called from bus_set_iommu(), iommu core will let everything carry on. Then when a client gets probed, of_iommu_configure() in iommu core will search DTB for swgroup ID and call ->of_xlate() to prepare an fwspec, similar to tegra_smmu_probe_device() and tegra_smmu_configure(). Then it'll call tegra_smmu_probe_device() again, and this time we shall return smmu->iommu pointer properly.

So we can get rid of tegra_smmu_find() and tegra_smmu_configure() along with DT polling code by letting the iommu core handle every thing, except a problem that we search iommus property in DTB not only for swgroup ID but also for mc node to get mc->smmu pointer to call dev_iommu_priv_set() and return the smmu->iommu pointer. So we'll need to find another way to get smmu pointer.

Referencing the implementation of sun50i-iommu driver, of_xlate() has client's dev pointer, mc node and swgroup ID. This means that we can call dev_iommu_priv_set() in of_xlate() instead, so we can simply get smmu pointer in ->probe_device().

This patch reworks tegra_smmu_probe_device() by:
1) Removing mc->smmu hack in tegra_smmu_probe() so as to return ERR_PTR(-ENODEV) in tegra_smmu_probe_device() during stage of tegra_smmu_probe/tegra_mc_probe().
2) Moving dev_iommu_priv_set() to of_xlate() so we can get smmu pointer in tegra_smmu_probe_device() to replace DTB polling.
3) Removing tegra_smmu_configure() accordingly since iommu core takes care of it.

---

Changelog
v3->v4
* Moved dev_iommu_priv_set() to of_xlate() so we don't need to poll DTB for smmu pointer.
* Removed the hack in tegra_smmu_probe() by returning ERR_PTR( -ENODEV) in tegra_smmu_probe_device() to let iommu core call in again.
* Removed tegra_smmu_find() and tegra_smmu_configure() as iommu core takes care of fwspec.
v2->v3
* Used devm_tegra_get_memory_controller() to get mc pointer
* Replaced IS_ERR_OR_NULL with IS_ERR in .probe_device()
v1->v2
* Replaced in .probe_device() tegra_smmu_find/tegra_smmu_configure() with tegra_get_memory_controller call.
* Dropped the hack in tegra_smmu_probe().

 drivers/iommu/tegra-smmu.c | 90 ++++----------------------------------
 1 file changed, 9 insertions(+), 81 deletions(-)
drivers/iommu/tegra-smmu.c +11 -81
index a573a5151c69..02d02b0c55c4 100644
@@ -797,75 +797,9 @@ static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
 	return SMMU_PFN_PHYS(pfn) + SMMU_OFFSET_IN_PAGE(iova);
 }
 
-static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
-{
-	struct platform_device *pdev;
-	struct tegra_mc *mc;
-
-	pdev = of_find_device_by_node(np);
-	if (!pdev)
-		return NULL;
-
-	mc = platform_get_drvdata(pdev);
-	if (!mc)
-		return NULL;
-
-	return mc->smmu;
-}
-
-static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
-				struct of_phandle_args *args)
-{
-	const struct iommu_ops *ops = smmu->iommu.ops;
-	int err;
-
-	err = iommu_fwspec_init(dev, &dev->of_node->fwnode, ops);
-	if (err < 0) {
-		dev_err(dev, "failed to initialize fwspec: %d\n", err);
-		return err;
-	}
-
-	err = ops->of_xlate(dev, args);
-	if (err < 0) {
-		dev_err(dev, "failed to parse SW group ID: %d\n", err);
-		iommu_fwspec_free(dev);
-		return err;
-	}
-
-	return 0;
-}
-
 static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
 {
-	struct device_node *np = dev->of_node;
-	struct tegra_smmu *smmu = NULL;
-	struct of_phandle_args args;
-	unsigned int index = 0;
-	int err;
-
-	while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					  &args) == 0) {
-		smmu = tegra_smmu_find(args.np);
-		if (smmu) {
-			err = tegra_smmu_configure(smmu, dev, &args);
-			of_node_put(args.np);
-
-			if (err < 0)
-				return ERR_PTR(err);
-
-			/*
-			 * Only a single IOMMU master interface is currently
-			 * supported by the Linux kernel, so abort after the
-			 * first match.
-			 */
-			dev_iommu_priv_set(dev, smmu);
-
-			break;
-		}
-
-		of_node_put(args.np);
-		index++;
-	}
+	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 
 	if (!smmu)
 		return ERR_PTR(-ENODEV);
@@ -873,10 +807,7 @@ static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
 	return &smmu->iommu;
 }
 
-static void tegra_smmu_release_device(struct device *dev)
-{
-	dev_iommu_priv_set(dev, NULL);
-}
+static void tegra_smmu_release_device(struct device *dev) {}
 
 static const struct tegra_smmu_group_soc *
 tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
@@ -953,8 +884,17 @@ static struct iommu_group *tegra_smmu_device_group(struct device *dev)
 static int tegra_smmu_of_xlate(struct device *dev,
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;
+
+	dev_iommu_priv_set(dev, mc->smmu);
+
 	return iommu_fwspec_add_ids(dev, &id, 1);
 }
 
@@ -1079,16 +1017,6 @@ struct tegra_smmu *tegra_smmu_probe(struct device *dev,
 	if (!smmu)
 		return ERR_PTR(-ENOMEM);
 
-	/*
-	 * This is a bit of a hack. Ideally we'd want to simply return this
-	 * value. However the IOMMU registration process will attempt to add
-	 * all devices to the IOMMU when bus_set_iommu() is called. In order
-	 * not to rely on global variables to track the IOMMU instance, we
-	 * set it here so that it can be looked up from the .probe_device()
-	 * callback via the IOMMU device's .drvdata field.
-	 */
-	mc->smmu = smmu;
-
 	size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
 
 	smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#12 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_device *tegra_smmu_probe_device(struct device *dev) · line 810

Quoted 5 lines
drivers/iommu/tegra-smmu.c +1 -4
-static void tegra_smmu_release_device(struct device *dev)
-{
-	dev_iommu_priv_set(dev, NULL);
-}
+static void tegra_smmu_release_device(struct device *dev) {}

Please keep the braces as-is.

#13 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 9 lines

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_device *tegra_smmu_probe_device(struct device *dev) · line 810

Quoted 5 lines
drivers/iommu/tegra-smmu.c +1 -4
-static void tegra_smmu_release_device(struct device *dev)
-{
-	dev_iommu_priv_set(dev, NULL);
-}
+static void tegra_smmu_release_device(struct device *dev) {}

Please keep the braces as-is.

I noticed that you borrowed this style from the sun50i-iommu driver, but this is a bit unusual coding style for the c files. At least to me it's unusual to see header-style function stub in a middle of c file. But maybe it's just me.

#14 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 11 lines · Dmitry Osipenko

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 9 lines

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_device *tegra_smmu_probe_device(struct device *dev) · line 810

Quoted 5 lines
drivers/iommu/tegra-smmu.c +1 -4
-static void tegra_smmu_release_device(struct device *dev)
-{
-	dev_iommu_priv_set(dev, NULL);
-}
+static void tegra_smmu_release_device(struct device *dev) {}

Please keep the braces as-is.

Quoted 4 lines

I noticed that you borrowed this style from the sun50i-iommu driver, but this is a bit unusual coding style for the c files. At least to me it's unusual to see header-style function stub in a middle of c file. But maybe it's just me.

I don't see a rule in ./Documentation/process/coding-style.rst against this, and there're plenty of drivers doing so. If you feel uncomfortable with this style, you may add a rule to that doc so everyone will follow :)

#15 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 17 lines · Nicolin Chen
Quoted 11 lines · Dmitry Osipenko

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 9 lines

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_device *tegra_smmu_probe_device(struct device *dev) · line 810

Quoted 5 lines
drivers/iommu/tegra-smmu.c +1 -4
-static void tegra_smmu_release_device(struct device *dev)
-{
-	dev_iommu_priv_set(dev, NULL);
-}
+static void tegra_smmu_release_device(struct device *dev) {}

Please keep the braces as-is.

Quoted 4 lines

I noticed that you borrowed this style from the sun50i-iommu driver, but this is a bit unusual coding style for the c files. At least to me it's unusual to see header-style function stub in a middle of c file. But maybe it's just me.

Quoted 4 lines

I don't see a rule in ./Documentation/process/coding-style.rst against this, and there're plenty of drivers doing so. If you feel uncomfortable with this style, you may add a rule to that doc so everyone will follow :)

I also prefer braces on separate lines. Even better would be to just drop this entirely and make ->release_device() optional. At least the following drivers could be cleaned up that way:

* fsl-pamu
* msm-iommu
* sun50i-iommu
* tegra-gart
* tegra-smmu

And it looks like mtk-iommu and mtk-iommu-v1 do only iommu_fwspec_free() in their ->release_device() implementations, but that's already done via iommu_release_device().

Thierry

#16 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

In reply to Nicolin Chen

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 894

Quoted 11 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +6 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 896

Quoted 1 line
drivers/iommu/tegra-smmu.c +1 -0
+	dev_iommu_priv_set(dev, mc->smmu);

I think put_device(mc->dev) is missed here, doesn't it?

Why sun50i-iommu driver doesn't have this error-checking? Is it really needed at all?

#17 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 7 lines
Quoted 7 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +2 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
Quoted 6 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 894

Quoted 4 lines
drivers/iommu/tegra-smmu.c +4 -0
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

Actually, platform_get_drvdata(NULL) can't happen. I overlooked this.

#18 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 8 lines · Dmitry Osipenko

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 7 lines
Quoted 7 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +2 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
Quoted 8 lines
Quoted 6 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 894

Quoted 4 lines
drivers/iommu/tegra-smmu.c +4 -0
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

Actually, platform_get_drvdata(NULL) can't happen. I overlooked this.

How so? It's technically possible for the iommus property to reference a device tree node for which no platform device will ever be created, in which case of_find_device_by_node() will return NULL. That's very unlikely and perhaps worth just crashing on to make sure it gets fixed immediately.

Thierry

#19 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

05.10.2020 12:53, Thierry Reding пишет:

Quoted 16 lines · Dmitry Osipenko
Quoted 15 lines

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 13 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 894

Quoted 11 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +6 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

Quoted 7 lines
Quoted 1 line

Actually, platform_get_drvdata(NULL) can't happen. I overlooked this.

How so? It's technically possible for the iommus property to reference a device tree node for which no platform device will ever be created, in which case of_find_device_by_node() will return NULL. That's very unlikely and perhaps worth just crashing on to make sure it gets fixed immediately.

The tegra_smmu_ops are registered from the SMMU driver itself and MC driver sets platform data before SMMU is initialized, hence device is guaranteed to exist and mc can't be NULL.

#20 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 25 lines · Dmitry Osipenko

05.10.2020 12:53, Thierry Reding пишет:

Quoted 16 lines · Dmitry Osipenko
Quoted 15 lines

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 13 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 894

Quoted 11 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +6 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

Quoted 7 lines
Quoted 1 line

Actually, platform_get_drvdata(NULL) can't happen. I overlooked this.

How so? It's technically possible for the iommus property to reference a device tree node for which no platform device will ever be created, in which case of_find_device_by_node() will return NULL. That's very unlikely and perhaps worth just crashing on to make sure it gets fixed immediately.

Quoted 3 lines

The tegra_smmu_ops are registered from the SMMU driver itself and MC driver sets platform data before SMMU is initialized, hence device is guaranteed to exist and mc can't be NULL.

Yes, but that assumes that args->np points to the memory controller's device tree node. It's obviously a mistake to do this, but I don't think anyone will prevent you from doing this:

	iommus = <&{/chosen} 0>;

In that case, since no platform device is created for the /chosen node, iommu_pdev will end up being NULL and platform_get_drvdata() will crash.

That said, I'm fine with not adding a check for that. If anyone really does end up messing this up they deserve the crash.

I'm still a bit undecided about the mc->smmu check because I haven't convinced myself yet that it can't happen.

Thierry

#21 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

05.10.2020 14:15, Thierry Reding пишет:

Quoted 30 lines · Dmitry Osipenko
Quoted 25 lines

05.10.2020 12:53, Thierry Reding пишет:

Quoted 16 lines · Dmitry Osipenko
Quoted 15 lines

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 13 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 894

Quoted 11 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +6 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

Quoted 7 lines
Quoted 1 line

Actually, platform_get_drvdata(NULL) can't happen. I overlooked this.

How so? It's technically possible for the iommus property to reference a device tree node for which no platform device will ever be created, in which case of_find_device_by_node() will return NULL. That's very unlikely and perhaps worth just crashing on to make sure it gets fixed immediately.

Quoted 3 lines

The tegra_smmu_ops are registered from the SMMU driver itself and MC driver sets platform data before SMMU is initialized, hence device is guaranteed to exist and mc can't be NULL.

Quoted 8 lines

Yes, but that assumes that args->np points to the memory controller's device tree node. It's obviously a mistake to do this, but I don't think anyone will prevent you from doing this:

	iommus = <&{/chosen} 0>;

In that case, since no platform device is created for the /chosen node, iommu_pdev will end up being NULL and platform_get_drvdata() will crash.

But then Tegra SMMU isn't associated with the device's IOMMU path, and thus, tegra_smmu_of_xlate() won't be invoked for this device.

Quoted 5 lines

That said, I'm fine with not adding a check for that. If anyone really does end up messing this up they deserve the crash.

I'm still a bit undecided about the mc->smmu check because I haven't convinced myself yet that it can't happen.

For now I can't see any realistic situation where mc->smmu could be NULL.

#22 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 35 lines · Dmitry Osipenko

05.10.2020 14:15, Thierry Reding пишет:

Quoted 34 lines · Dmitry Osipenko
Quoted 25 lines

05.10.2020 12:53, Thierry Reding пишет:

Quoted 16 lines · Dmitry Osipenko
Quoted 15 lines

02.10.2020 17:22, Dmitry Osipenko пишет:

Quoted 13 lines

Review

Quoted 11 lines

static int tegra_smmu_of_xlate(struct device *dev,

(quoted diff) +6 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

Quoted 7 lines
Quoted 1 line

Actually, platform_get_drvdata(NULL) can't happen. I overlooked this.

How so? It's technically possible for the iommus property to reference a device tree node for which no platform device will ever be created, in which case of_find_device_by_node() will return NULL. That's very unlikely and perhaps worth just crashing on to make sure it gets fixed immediately.

Quoted 3 lines

The tegra_smmu_ops are registered from the SMMU driver itself and MC driver sets platform data before SMMU is initialized, hence device is guaranteed to exist and mc can't be NULL.

Yes, but that assumes that args->np points to the memory controller's device tree node. It's obviously a mistake to do this, but I don't think anyone will prevent you from doing this:

Quoted 7 lines
Quoted 4 lines
	iommus = <&{/chosen} 0>;

In that case, since no platform device is created for the /chosen node, iommu_pdev will end up being NULL and platform_get_drvdata() will crash.

But then Tegra SMMU isn't associated with the device's IOMMU path, and thus, tegra_smmu_of_xlate() won't be invoked for this device.

Indeed, you're right! It used to be that ops were assigned to the bus without any knowledge about the specific instances that might exist, but nowadays there's struct iommu_device which properly encapsulates all of that, so yeah, I don't think this can ever be NULL.

Although that makes me wonder why we aren't going one step further and pass struct iommu_device * into ->of_xlate(), which would avoid the need for us to do the look up once more.

Thierry

#23 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

In reply to Dmitry Osipenko
Quoted 15 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 894

Quoted 11 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +6 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);
+
+	if (!mc || !mc->smmu)
+		return -EPROBE_DEFER;

platform_get_drvdata(NULL) will crash.

Quoted 3 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 896

Quoted 1 line
drivers/iommu/tegra-smmu.c +1 -0
+	dev_iommu_priv_set(dev, mc->smmu);

I think put_device(mc->dev) is missed here, doesn't it?

Yeah, I think we'd need that here, otherwise we'd be leaking a reference. Worse, even, mc->dev is the same device that owns the SMMU, so we're basically incrementing our own reference here and never releasing it. We also need that put_device(mc->dev) in the error case above because we already hold the reference there.

Thierry

#24 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

In reply to Nicolin Chen

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 25 lines

static struct iommu_device *tegra_smmu_probe_device(struct device *dev) {

drivers/iommu/tegra-smmu.c +0 -23
-	struct device_node *np = dev->of_node;
-	struct tegra_smmu *smmu = NULL;
-	struct of_phandle_args args;
-	unsigned int index = 0;
-	int err;
-
-	while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					  &args) == 0) {
-		smmu = tegra_smmu_find(args.np);
-		if (smmu) {
-			err = tegra_smmu_configure(smmu, dev, &args);
-			of_node_put(args.np);
-
-			if (err < 0)
-				return ERR_PTR(err);
-
-			/*
-			 * Only a single IOMMU master interface is currently
-			 * supported by the Linux kernel, so abort after the
-			 * first match.
-			 */
-			dev_iommu_priv_set(dev, smmu);
-

drivers/iommu/tegra-smmu.c static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain, · line 805

Quoted 10 lines
drivers/iommu/tegra-smmu.c +1 -6
-			break;
-		}
-
-		of_node_put(args.np);
-		index++;
-	}
+	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
 
 	if (!smmu)
 		return ERR_PTR(-ENODEV);

The !smmu can't ever be true now, isn't it? Then please remove it.

#25 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 34 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 33 lines

static struct iommu_device *tegra_smmu_probe_device(struct device *dev) {

drivers/iommu/tegra-smmu.c +1 -29
-	struct device_node *np = dev->of_node;
-	struct tegra_smmu *smmu = NULL;
-	struct of_phandle_args args;
-	unsigned int index = 0;
-	int err;
-
-	while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					  &args) == 0) {
-		smmu = tegra_smmu_find(args.np);
-		if (smmu) {
-			err = tegra_smmu_configure(smmu, dev, &args);
-			of_node_put(args.np);
-
-			if (err < 0)
-				return ERR_PTR(err);
-
-			/*
-			 * Only a single IOMMU master interface is currently
-			 * supported by the Linux kernel, so abort after the
-			 * first match.
-			 */
-			dev_iommu_priv_set(dev, smmu);
-
-			break;
-		}
-
-		of_node_put(args.np);
-		index++;
-	}
+	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
Quoted 4 lines

drivers/iommu/tegra-smmu.c static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain, · line 805

Quoted 2 lines
drivers/iommu/tegra-smmu.c +0 -0
 	if (!smmu)
 		return ERR_PTR(-ENODEV);

The !smmu can't ever be true now, isn't it? Then please remove it.

How can you be so sure? Have you read my commit message? The whole point of removing the hack in tegra_smmu_probe() is to return the ERR_PTR(-ENODEV) here. The bus_set_iommu() will call this function when mc->smmu is not assigned it, as it's assigned after we return tegra_smmu_probe() while bus_set_iommu() is still in the middle of the tegra_smmu_probe().
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#26 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

02.10.2020 21:01, Nicolin Chen пишет:

Quoted 40 lines · Dmitry Osipenko
Quoted 34 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 33 lines

static struct iommu_device *tegra_smmu_probe_device(struct device *dev) {

drivers/iommu/tegra-smmu.c +1 -29
-	struct device_node *np = dev->of_node;
-	struct tegra_smmu *smmu = NULL;
-	struct of_phandle_args args;
-	unsigned int index = 0;
-	int err;
-
-	while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
-					  &args) == 0) {
-		smmu = tegra_smmu_find(args.np);
-		if (smmu) {
-			err = tegra_smmu_configure(smmu, dev, &args);
-			of_node_put(args.np);
-
-			if (err < 0)
-				return ERR_PTR(err);
-
-			/*
-			 * Only a single IOMMU master interface is currently
-			 * supported by the Linux kernel, so abort after the
-			 * first match.
-			 */
-			dev_iommu_priv_set(dev, smmu);
-
-			break;
-		}
-
-		of_node_put(args.np);
-		index++;
-	}
+	struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
Quoted 4 lines

drivers/iommu/tegra-smmu.c static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain, · line 805

Quoted 2 lines
drivers/iommu/tegra-smmu.c +0 -0
 	if (!smmu)
 		return ERR_PTR(-ENODEV);

The !smmu can't ever be true now, isn't it? Then please remove it.

Quoted 7 lines

How can you be so sure? Have you read my commit message? The whole point of removing the hack in tegra_smmu_probe() is to return the ERR_PTR(-ENODEV) here. The bus_set_iommu() will call this function when mc->smmu is not assigned it, as it's assigned after we return tegra_smmu_probe() while bus_set_iommu() is still in the middle of the tegra_smmu_probe().

My bad, I probably missed that was looking at the probe_device(), looks good then.

#27 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

In reply to Nicolin Chen

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 891

Quoted 8 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +3 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

#28 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 8 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 7 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +2 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
Quoted 4 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 891

Quoted 1 line
drivers/iommu/tegra-smmu.c +1 -0
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

Looks like so. Replacing it with put_device(&iommu_pdev->dev);

Thanks

#29 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 9 lines · Nicolin Chen
Quoted 8 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 7 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +2 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
Quoted 6 lines
Quoted 4 lines

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 891

Quoted 1 line
drivers/iommu/tegra-smmu.c +1 -0
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

Looks like so. Replacing it with put_device(&iommu_pdev->dev);

Putting the put_device() here is wrong, though. You need to make sure you keep a reference to it as long as you keep accessing the data that is owned by it.

Like I said earlier, this is a bit weird in this case because we're self-referencing, so iommu_pdev->dev is going to stay around as long as the SMMU is. However, it might be worth to properly track the lifetime anyway just so that the code can serve as a good example of how to do things.

If you decide to go for the shortcut and not track this reference properly, then at least you need to add a comment as to why it is safe to do in this case. This ensures that readers are away of the circumstances and don't copy this bad code into a context where the circumstances are different.

Thierry

#30 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 15 lines · Thierry Reding
Quoted 14 lines · Nicolin Chen
Quoted 12 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 891

Quoted 8 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +3 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

Quoted 5 lines
Quoted 1 line

Looks like so. Replacing it with put_device(&iommu_pdev->dev);

Putting the put_device() here is wrong, though. You need to make sure you keep a reference to it as long as you keep accessing the data that is owned by it.

I am confused. You said in the other reply (to Dmitry) that we do need to put_device(mc->dev), where mc->dev should be the same as iommu_pdev->dev. But here your comments sounds that we should not put_device at all since ->probe_device/group_device/attach_dev() will use it later.

Quoted 5 lines

Like I said earlier, this is a bit weird in this case because we're self-referencing, so iommu_pdev->dev is going to stay around as long as the SMMU is. However, it might be worth to properly track the lifetime anyway just so that the code can serve as a good example of how to do things.

What's this "track-the-lifetime"?

Quoted 5 lines

If you decide to go for the shortcut and not track this reference properly, then at least you need to add a comment as to why it is safe to do in this case. This ensures that readers are away of the circumstances and don't copy this bad code into a context where the circumstances are different.

I don't quite get this "shortcut" here either...mind elaborating?

#31 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 22 lines · Nicolin Chen
Quoted 15 lines · Thierry Reding
Quoted 14 lines · Nicolin Chen
Quoted 12 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 891

Quoted 8 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +3 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

Quoted 5 lines
Quoted 1 line

Looks like so. Replacing it with put_device(&iommu_pdev->dev);

Putting the put_device() here is wrong, though. You need to make sure you keep a reference to it as long as you keep accessing the data that is owned by it.

Quoted 5 lines

I am confused. You said in the other reply (to Dmitry) that we do need to put_device(mc->dev), where mc->dev should be the same as iommu_pdev->dev. But here your comments sounds that we should not put_device at all since ->probe_device/group_device/attach_dev() will use it later.

You need to call put_device() at some point to release the reference that you acquired by calling of_find_device_by_node(). If you don't release it, you're leaking the reference and the kernel isn't going to know when it's safe to delete the device.

So what I'm saying is that we either release it here, which isn't quite right because we do reference data relating to the device later on. And because it isn't quite right there should be a reason to justify it, which is that the SMMU parent device is the same as the MC, so the reference count isn't strictly necessary. But that's not quite obvious, so highlighting it in a comment makes sense.

The other alternative is to not call put_device() here and keep on to the reference as long as you keep using "mc". This might be difficult to implement because it may not be obvious where to release it. I think this is the better alternative, but if it's too complicated to implement it might not be worth it.

Quoted 7 lines
Quoted 5 lines

Like I said earlier, this is a bit weird in this case because we're self-referencing, so iommu_pdev->dev is going to stay around as long as the SMMU is. However, it might be worth to properly track the lifetime anyway just so that the code can serve as a good example of how to do things.

What's this "track-the-lifetime"?

This basically just means that SMMU needs to ensure that MC stays alive (by holding a reference to it) as long as SMMU uses it. If the last reference to MC is dropped, then the mc pointer and potentially anything that it points to will become dangling. If you were to drop the last reference at this point, then on the next line the mc pointer could already be invalid.

That's how it generally works, anyway. What's special about this use- case is that the SMMU and MC are the same device, so it should be safe to omit this additional tracking because the IOMMU tracking should take care of that already.

Quoted 7 lines
Quoted 5 lines

If you decide to go for the shortcut and not track this reference properly, then at least you need to add a comment as to why it is safe to do in this case. This ensures that readers are away of the circumstances and don't copy this bad code into a context where the circumstances are different.

I don't quite get this "shortcut" here either...mind elaborating?

The shortcut is taking advantage of the knowledge that the SMMU and the MC are the same device and therefore not properly track the MC object. Given that their code is located in different locations, this isn't obvious to the casual reader of the code, so they may assume that this is the normal way to do things. To avoid that, the code should have a comment explaining why that is.

Thierry

#32 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 29 lines · Thierry Reding
Quoted 22 lines · Nicolin Chen
Quoted 15 lines · Thierry Reding
Quoted 14 lines · Nicolin Chen
Quoted 12 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

drivers/iommu/tegra-smmu.c static struct iommu_group *tegra_smmu_device_group(struct device *dev) · line 891

Quoted 8 lines

static int tegra_smmu_of_xlate(struct device *dev,

drivers/iommu/tegra-smmu.c +3 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

Quoted 5 lines
Quoted 1 line

Looks like so. Replacing it with put_device(&iommu_pdev->dev);

Putting the put_device() here is wrong, though. You need to make sure you keep a reference to it as long as you keep accessing the data that is owned by it.

Quoted 5 lines

I am confused. You said in the other reply (to Dmitry) that we do need to put_device(mc->dev), where mc->dev should be the same as iommu_pdev->dev. But here your comments sounds that we should not put_device at all since ->probe_device/group_device/attach_dev() will use it later.

Quoted 7 lines

You need to call put_device() at some point to release the reference that you acquired by calling of_find_device_by_node(). If you don't release it, you're leaking the reference and the kernel isn't going to know when it's safe to delete the device.

So what I'm saying is that we either release it here, which isn't quite right because we do reference data relating to the device later on. And

I see. A small question here by the way: By looking at other IOMMU drivers that are calling driver_find_device_by_fwnode() function, I found that most of them put_device right after the function call, and dev_get_drvdata() after putting the device..

Feels like they are doing it wrongly?

Quoted 10 lines

because it isn't quite right there should be a reason to justify it, which is that the SMMU parent device is the same as the MC, so the reference count isn't strictly necessary. But that's not quite obvious, so highlighting it in a comment makes sense.

The other alternative is to not call put_device() here and keep on to the reference as long as you keep using "mc". This might be difficult to implement because it may not be obvious where to release it. I think this is the better alternative, but if it's too complicated to implement it might not be worth it.

I feel so too. The dev is got at of_xlate() that does not have an obvious counterpart function. So I'll just remove put_device() and put a line of comments, as you suggested.

Quoted 15 lines
Quoted 7 lines
Quoted 5 lines

Like I said earlier, this is a bit weird in this case because we're self-referencing, so iommu_pdev->dev is going to stay around as long as the SMMU is. However, it might be worth to properly track the lifetime anyway just so that the code can serve as a good example of how to do things.

What's this "track-the-lifetime"?

This basically just means that SMMU needs to ensure that MC stays alive (by holding a reference to it) as long as SMMU uses it. If the last reference to MC is dropped, then the mc pointer and potentially anything that it points to will become dangling. If you were to drop the last reference at this point, then on the next line the mc pointer could already be invalid.

Quoted 4 lines

That's how it generally works, anyway. What's special about this use- case is that the SMMU and MC are the same device, so it should be safe to omit this additional tracking because the IOMMU tracking should take care of that already.

Okay.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

If you decide to go for the shortcut and not track this reference properly, then at least you need to add a comment as to why it is safe to do in this case. This ensures that readers are away of the circumstances and don't copy this bad code into a context where the circumstances are different.

Quoted 8 lines
Quoted 1 line

I don't quite get this "shortcut" here either...mind elaborating?

The shortcut is taking advantage of the knowledge that the SMMU and the MC are the same device and therefore not properly track the MC object. Given that their code is located in different locations, this isn't obvious to the casual reader of the code, so they may assume that this is the normal way to do things. To avoid that, the code should have a comment explaining why that is.

Got it. Thanks!

#33 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 38 lines · Nicolin Chen
Quoted 29 lines · Thierry Reding
Quoted 22 lines · Nicolin Chen
Quoted 15 lines · Thierry Reding
Quoted 14 lines · Nicolin Chen
Quoted 12 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

Review

Quoted 8 lines

static int tegra_smmu_of_xlate(struct device *dev,

(quoted diff) +3 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

Quoted 5 lines
Quoted 1 line

Looks like so. Replacing it with put_device(&iommu_pdev->dev);

Putting the put_device() here is wrong, though. You need to make sure you keep a reference to it as long as you keep accessing the data that is owned by it.

Quoted 5 lines

I am confused. You said in the other reply (to Dmitry) that we do need to put_device(mc->dev), where mc->dev should be the same as iommu_pdev->dev. But here your comments sounds that we should not put_device at all since ->probe_device/group_device/attach_dev() will use it later.

Quoted 7 lines

You need to call put_device() at some point to release the reference that you acquired by calling of_find_device_by_node(). If you don't release it, you're leaking the reference and the kernel isn't going to know when it's safe to delete the device.

So what I'm saying is that we either release it here, which isn't quite right because we do reference data relating to the device later on. And

Quoted 6 lines

I see. A small question here by the way: By looking at other IOMMU drivers that are calling driver_find_device_by_fwnode() function, I found that most of them put_device right after the function call, and dev_get_drvdata() after putting the device..

Feels like they are doing it wrongly?

Well, like I said this is somewhat academic because these are all referencing the IOMMU that by definition still needs to be around when this code is called, and there's locks in place to ensure these don't go away. So it's not like these drivers are doing it wrong, they're just not doing it pedantically right.

Quoted 12 lines
Quoted 11 lines

because it isn't quite right there should be a reason to justify it, which is that the SMMU parent device is the same as the MC, so the reference count isn't strictly necessary. But that's not quite obvious, so highlighting it in a comment makes sense.

The other alternative is to not call put_device() here and keep on to the reference as long as you keep using "mc". This might be difficult to implement because it may not be obvious where to release it. I think this is the better alternative, but if it's too complicated to implement it might not be worth it.

Quoted 3 lines

I feel so too. The dev is got at of_xlate() that does not have an obvious counterpart function. So I'll just remove put_device() and put a line of comments, as you suggested.

I think you misunderstood. Not calling put_device() would be wrong because that leaks a reference to the SMMU that you can't get back. My suggestion was rather to keep put_device() here, but add a comment as to why it's okay to call the put_device() here, even though you keep using its private data later beyond this point, which typically would be wrong to do.

Thierry

#34 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

Quoted 68 lines · Thierry Reding
Quoted 38 lines · Nicolin Chen
Quoted 29 lines · Thierry Reding
Quoted 22 lines · Nicolin Chen
Quoted 15 lines · Thierry Reding
Quoted 14 lines · Nicolin Chen
Quoted 12 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

Review

Quoted 8 lines

static int tegra_smmu_of_xlate(struct device *dev,

(quoted diff) +3 -0
 			       struct of_phandle_args *args)
 {
+	struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
+	struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
 	u32 id = args->args[0];
 
+	of_node_put(args->np);

of_find_device_by_node() takes device reference and not the np reference. This is a bug, please remove of_node_put().

Quoted 5 lines
Quoted 1 line

Looks like so. Replacing it with put_device(&iommu_pdev->dev);

Putting the put_device() here is wrong, though. You need to make sure you keep a reference to it as long as you keep accessing the data that is owned by it.

Quoted 5 lines

I am confused. You said in the other reply (to Dmitry) that we do need to put_device(mc->dev), where mc->dev should be the same as iommu_pdev->dev. But here your comments sounds that we should not put_device at all since ->probe_device/group_device/attach_dev() will use it later.

Quoted 7 lines

You need to call put_device() at some point to release the reference that you acquired by calling of_find_device_by_node(). If you don't release it, you're leaking the reference and the kernel isn't going to know when it's safe to delete the device.

So what I'm saying is that we either release it here, which isn't quite right because we do reference data relating to the device later on. And

Quoted 6 lines

I see. A small question here by the way: By looking at other IOMMU drivers that are calling driver_find_device_by_fwnode() function, I found that most of them put_device right after the function call, and dev_get_drvdata() after putting the device..

Feels like they are doing it wrongly?

Well, like I said this is somewhat academic because these are all referencing the IOMMU that by definition still needs to be around when this code is called, and there's locks in place to ensure these don't go away. So it's not like these drivers are doing it wrong, they're just not doing it pedantically right.

Quoted 12 lines
Quoted 11 lines

because it isn't quite right there should be a reason to justify it, which is that the SMMU parent device is the same as the MC, so the reference count isn't strictly necessary. But that's not quite obvious, so highlighting it in a comment makes sense.

The other alternative is to not call put_device() here and keep on to the reference as long as you keep using "mc". This might be difficult to implement because it may not be obvious where to release it. I think this is the better alternative, but if it's too complicated to implement it might not be worth it.

Quoted 3 lines

I feel so too. The dev is got at of_xlate() that does not have an obvious counterpart function. So I'll just remove put_device() and put a line of comments, as you suggested.

Quoted 6 lines

I think you misunderstood. Not calling put_device() would be wrong because that leaks a reference to the SMMU that you can't get back. My suggestion was rather to keep put_device() here, but add a comment as to why it's okay to call the put_device() here, even though you keep using its private data later beyond this point, which typically would be wrong to do.

I see. Thanks for clarification! Will send v6 soon.

#35 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

In reply to Nicolin Chen

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 5 lines

Then when a client gets probed, of_iommu_configure() in iommu core will search DTB for swgroup ID and call ->of_xlate() to prepare an fwspec, similar to tegra_smmu_probe_device() and tegra_smmu_configure(). Then it'll call tegra_smmu_probe_device() again, and this time we shall return smmu->iommu pointer properly.

I don't quite see where IOMMU core calls of_xlate().

Have tried to at least boot-test this patch?

#36 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

02.10.2020 18:23, Dmitry Osipenko пишет:

Quoted 11 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 5 lines

Then when a client gets probed, of_iommu_configure() in iommu core will search DTB for swgroup ID and call ->of_xlate() to prepare an fwspec, similar to tegra_smmu_probe_device() and tegra_smmu_configure(). Then it'll call tegra_smmu_probe_device() again, and this time we shall return smmu->iommu pointer properly.

I don't quite see where IOMMU core calls of_xlate().

Have tried to at least boot-test this patch?

I don't see how it ever could work because of_xlate() is only invoked from:

fsl_mc_dma_configure()->of_dma_configure_id()->of_iommu_configure()

Looks like the tegra_smmu_configure() is still needed.

I don't know how sun50i driver could work to be honest. Seems IOMMU is broken on sun50i, but maybe I'm missing something.

I added Maxime Ripard to this thread, who is the author of the sun50i-iommu driver.

#37 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

02.10.2020 19:00, Dmitry Osipenko пишет:

Quoted 17 lines

02.10.2020 18:23, Dmitry Osipenko пишет:

Quoted 11 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 5 lines

Then when a client gets probed, of_iommu_configure() in iommu core will search DTB for swgroup ID and call ->of_xlate() to prepare an fwspec, similar to tegra_smmu_probe_device() and tegra_smmu_configure(). Then it'll call tegra_smmu_probe_device() again, and this time we shall return smmu->iommu pointer properly.

I don't quite see where IOMMU core calls of_xlate().

Have tried to at least boot-test this patch?

I don't see how it ever could work because of_xlate() is only invoked from:

fsl_mc_dma_configure()->of_dma_configure_id()->of_iommu_configure()

Quoted 8 lines

Looks like the tegra_smmu_configure() is still needed.

I don't know how sun50i driver could work to be honest. Seems IOMMU is broken on sun50i, but maybe I'm missing something.

I added Maxime Ripard to this thread, who is the author of the sun50i-iommu driver.

Actually, I now see that the other IOMMU drivers (qcom, exynos, etc) do the same. So obviously I'm missing something and it should work..

#38 lore raw

Re: [PATCH v4 2/3] iommu/tegra-smmu: Rework tegra_smmu_probe_device()

02.10.2020 19:37, Dmitry Osipenko пишет:

Quoted 23 lines

02.10.2020 19:00, Dmitry Osipenko пишет:

Quoted 22 lines

02.10.2020 18:23, Dmitry Osipenko пишет:

Quoted 11 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 5 lines

Then when a client gets probed, of_iommu_configure() in iommu core will search DTB for swgroup ID and call ->of_xlate() to prepare an fwspec, similar to tegra_smmu_probe_device() and tegra_smmu_configure(). Then it'll call tegra_smmu_probe_device() again, and this time we shall return smmu->iommu pointer properly.

I don't quite see where IOMMU core calls of_xlate().

Have tried to at least boot-test this patch?

I don't see how it ever could work because of_xlate() is only invoked from:

fsl_mc_dma_configure()->of_dma_configure_id()->of_iommu_configure()

Looks like the tegra_smmu_configure() is still needed.

I don't know how sun50i driver could work to be honest. Seems IOMMU is broken on sun50i, but maybe I'm missing something.

Quoted 7 lines
Quoted 3 lines

I added Maxime Ripard to this thread, who is the author of the sun50i-iommu driver.

Actually, I now see that the other IOMMU drivers (qcom, exynos, etc) do the same. So obviously I'm missing something and it should work..

Okay, somehow I was oblivious to that of_dma_configure() invokes of_dma_configure_id(). Should be good :)

#39 lore raw

PATCH v4 3/3 iommu/tegra-smmu: Add PCI support

In reply to Nicolin Chen

This patch simply adds support for PCI devices.

---

Changelog
v3->v4
* Dropped !iommu_present() check
* Added CONFIG_PCI check in the exit path
v2->v3
* Replaced ternary conditional operator with if-else in .device_group()
* Dropped change in tegra_smmu_remove()
v1->v2
* Added error-out labels in tegra_smmu_probe()
* Dropped pci_request_acs() since IOMMU core would call it.

 drivers/iommu/tegra-smmu.c | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)
drivers/iommu/tegra-smmu.c +25 -10
index 02d02b0c55c4..b701a7b55e84 100644
@@ -10,6 +10,7 @@
 #include <linux/kernel.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/pci.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
@@ -865,7 +866,11 @@ static struct iommu_group *tegra_smmu_device_group(struct device *dev)
 	group->smmu = smmu;
 	group->soc = soc;
 
-	group->group = iommu_group_alloc();
+	if (dev_is_pci(dev))
+		group->group = pci_device_group(dev);
+	else
+		group->group = generic_device_group(dev);
+
 	if (IS_ERR(group->group)) {
 		devm_kfree(smmu->dev, group);
 		mutex_unlock(&smmu->lock);
@@ -1069,22 +1074,32 @@ struct tegra_smmu *tegra_smmu_probe(struct device *dev,
 	iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
 
 	err = iommu_device_register(&smmu->iommu);
-	if (err) {
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err)
+		goto err_sysfs;
 
 	err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
-	if (err < 0) {
-		iommu_device_unregister(&smmu->iommu);
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err < 0)
+		goto err_unregister;
+
+#ifdef CONFIG_PCI
+	err = bus_set_iommu(&pci_bus_type, &tegra_smmu_ops);
+	if (err < 0)
+		goto err_bus_set;
+#endif
 
 	if (IS_ENABLED(CONFIG_DEBUG_FS))
 		tegra_smmu_debugfs_init(smmu);
 
 	return smmu;
+
+err_bus_set: __maybe_unused;
+	bus_set_iommu(&platform_bus_type, NULL);
+err_unregister:
+	iommu_device_unregister(&smmu->iommu);
+err_sysfs:
+	iommu_device_sysfs_remove(&smmu->iommu);
+
+	return ERR_PTR(err);
 }
 
 void tegra_smmu_remove(struct tegra_smmu *smmu)
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#40 lore raw

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 35 lines
drivers/iommu/tegra-smmu.c +12 -10
@@ -865,7 +866,11 @@ static struct iommu_group *tegra_smmu_device_group(struct device *dev)
 	group->smmu = smmu;
 	group->soc = soc;
 
-	group->group = iommu_group_alloc();
+	if (dev_is_pci(dev))
+		group->group = pci_device_group(dev);
+	else
+		group->group = generic_device_group(dev);
+
 	if (IS_ERR(group->group)) {
 		devm_kfree(smmu->dev, group);
 		mutex_unlock(&smmu->lock);
@@ -1069,22 +1074,32 @@ struct tegra_smmu *tegra_smmu_probe(struct device *dev,
 	iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
 
 	err = iommu_device_register(&smmu->iommu);
-	if (err) {
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err)
+		goto err_sysfs;
 
 	err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
-	if (err < 0) {
-		iommu_device_unregister(&smmu->iommu);
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err < 0)
+		goto err_unregister;
+
+#ifdef CONFIG_PCI
+	err = bus_set_iommu(&pci_bus_type, &tegra_smmu_ops);

drivers/iommu/tegra-smmu.c struct tegra_smmu *tegra_smmu_probe(struct device *dev, · line 1095

Quoted 10 lines
drivers/iommu/tegra-smmu.c +5 -0
+	if (err < 0)
+		goto err_bus_set;
+#endif
 
 	if (IS_ENABLED(CONFIG_DEBUG_FS))
 		tegra_smmu_debugfs_init(smmu);
 
 	return smmu;
+
+err_bus_set: __maybe_unused;

__maybe_unused?

#41 lore raw
Quoted 40 lines · Dmitry Osipenko

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 39 lines
drivers/iommu/tegra-smmu.c +15 -10
@@ -865,7 +866,11 @@ static struct iommu_group *tegra_smmu_device_group(struct device *dev)
 	group->smmu = smmu;
 	group->soc = soc;
 
-	group->group = iommu_group_alloc();
+	if (dev_is_pci(dev))
+		group->group = pci_device_group(dev);
+	else
+		group->group = generic_device_group(dev);
+
 	if (IS_ERR(group->group)) {
 		devm_kfree(smmu->dev, group);
 		mutex_unlock(&smmu->lock);
@@ -1069,22 +1074,32 @@ struct tegra_smmu *tegra_smmu_probe(struct device *dev,
 	iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
 
 	err = iommu_device_register(&smmu->iommu);
-	if (err) {
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err)
+		goto err_sysfs;
 
 	err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
-	if (err < 0) {
-		iommu_device_unregister(&smmu->iommu);
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err < 0)
+		goto err_unregister;
+
+#ifdef CONFIG_PCI
+	err = bus_set_iommu(&pci_bus_type, &tegra_smmu_ops);
+	if (err < 0)
+		goto err_bus_set;
+#endif
 
Quoted 8 lines

drivers/iommu/tegra-smmu.c struct tegra_smmu *tegra_smmu_probe(struct device *dev, · line 1095

Quoted 6 lines
drivers/iommu/tegra-smmu.c +2 -0
 	if (IS_ENABLED(CONFIG_DEBUG_FS))
 		tegra_smmu_debugfs_init(smmu);
 
 	return smmu;
+
+err_bus_set: __maybe_unused;

__maybe_unused?

In order to mute a build warning when CONFIG_PCI=n...

#42 lore raw

02.10.2020 20:45, Nicolin Chen пишет:

Quoted 44 lines · Dmitry Osipenko
Quoted 43 lines

02.10.2020 09:08, Nicolin Chen пишет:

Quoted 42 lines
drivers/iommu/tegra-smmu.c +15 -10
@@ -865,7 +866,11 @@ static struct iommu_group *tegra_smmu_device_group(struct device *dev)
 	group->smmu = smmu;
 	group->soc = soc;
 
-	group->group = iommu_group_alloc();
+	if (dev_is_pci(dev))
+		group->group = pci_device_group(dev);
+	else
+		group->group = generic_device_group(dev);
+
 	if (IS_ERR(group->group)) {
 		devm_kfree(smmu->dev, group);
 		mutex_unlock(&smmu->lock);
@@ -1069,22 +1074,32 @@ struct tegra_smmu *tegra_smmu_probe(struct device *dev,
 	iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
 
 	err = iommu_device_register(&smmu->iommu);
-	if (err) {
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err)
+		goto err_sysfs;
 
 	err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
-	if (err < 0) {
-		iommu_device_unregister(&smmu->iommu);
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err < 0)
+		goto err_unregister;
+
+#ifdef CONFIG_PCI
+	err = bus_set_iommu(&pci_bus_type, &tegra_smmu_ops);
+	if (err < 0)
+		goto err_bus_set;
+#endif
 
 	if (IS_ENABLED(CONFIG_DEBUG_FS))
 		tegra_smmu_debugfs_init(smmu);
 
Quoted 8 lines
Quoted 5 lines

drivers/iommu/tegra-smmu.c struct tegra_smmu *tegra_smmu_probe(struct device *dev, · line 1095

Quoted 3 lines
drivers/iommu/tegra-smmu.c +2 -0
 	return smmu;
+
+err_bus_set: __maybe_unused;

__maybe_unused?

In order to mute a build warning when CONFIG_PCI=n...

okay

#44 lore raw
In reply to Nicolin Chen
Quoted 71 lines · Nicolin Chen

This patch simply adds support for PCI devices.

---

Changelog
v3->v4
* Dropped !iommu_present() check
* Added CONFIG_PCI check in the exit path
v2->v3
* Replaced ternary conditional operator with if-else in .device_group()
* Dropped change in tegra_smmu_remove()
v1->v2
* Added error-out labels in tegra_smmu_probe()
* Dropped pci_request_acs() since IOMMU core would call it.

 drivers/iommu/tegra-smmu.c | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)
drivers/iommu/tegra-smmu.c +16 -10
index 02d02b0c55c4..b701a7b55e84 100644
@@ -10,6 +10,7 @@
 #include <linux/kernel.h>
 #include <linux/of.h>
 #include <linux/of_device.h>
+#include <linux/pci.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
@@ -865,7 +866,11 @@ static struct iommu_group *tegra_smmu_device_group(struct device *dev)
 	group->smmu = smmu;
 	group->soc = soc;
 
-	group->group = iommu_group_alloc();
+	if (dev_is_pci(dev))
+		group->group = pci_device_group(dev);
+	else
+		group->group = generic_device_group(dev);
+
 	if (IS_ERR(group->group)) {
 		devm_kfree(smmu->dev, group);
 		mutex_unlock(&smmu->lock);
@@ -1069,22 +1074,32 @@ struct tegra_smmu *tegra_smmu_probe(struct device *dev,
 	iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
 
 	err = iommu_device_register(&smmu->iommu);
-	if (err) {
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err)
+		goto err_sysfs;
 
 	err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
-	if (err < 0) {
-		iommu_device_unregister(&smmu->iommu);
-		iommu_device_sysfs_remove(&smmu->iommu);
-		return ERR_PTR(err);
-	}
+	if (err < 0)
+		goto err_unregister;
+
+#ifdef CONFIG_PCI
+	err = bus_set_iommu(&pci_bus_type, &tegra_smmu_ops);
+	if (err < 0)
+		goto err_bus_set;
+#endif
 
 	if (IS_ENABLED(CONFIG_DEBUG_FS))

drivers/iommu/tegra-smmu.c struct tegra_smmu *tegra_smmu_probe(struct device *dev, · line 1100

Quoted 10 lines
drivers/iommu/tegra-smmu.c +7 -0
 		tegra_smmu_debugfs_init(smmu);
 
 	return smmu;
+
+err_bus_set: __maybe_unused;
+	bus_set_iommu(&platform_bus_type, NULL);
+err_unregister:
+	iommu_device_unregister(&smmu->iommu);
+err_sysfs:
+	iommu_device_sysfs_remove(&smmu->iommu);

Can you please switch to label names that are more consistent with the others in this driver? Notably the ones in tegra_smmu_domain_alloc(). The idea is to describe in the name of the label what's happening at the label. Something like this, for example:

unset_platform_bus:

	bus_set_iommu(&platform_bus_type, NULL);
unregister:
	iommu_device_unregister(&smmu->iommu);
remove_sysfs:
	iommu_device_sysfs_remove(&smmu->iommu);

Thierry

#45 lore raw
Quoted 12 lines · Thierry Reding

drivers/iommu/tegra-smmu.c struct tegra_smmu *tegra_smmu_probe(struct device *dev, · line 1100

Quoted 6 lines
drivers/iommu/tegra-smmu.c +6 -0
+err_bus_set: __maybe_unused;
+	bus_set_iommu(&platform_bus_type, NULL);
+err_unregister:
+	iommu_device_unregister(&smmu->iommu);
+err_sysfs:
+	iommu_device_sysfs_remove(&smmu->iommu);

Can you please switch to label names that are more consistent with the others in this driver? Notably the ones in tegra_smmu_domain_alloc(). The idea is to describe in the name of the label what's happening at the label. Something like this, for example:

Quoted 6 lines

unset_platform_bus:

	bus_set_iommu(&platform_bus_type, NULL);
unregister:
	iommu_device_unregister(&smmu->iommu);
remove_sysfs:
	iommu_device_sysfs_remove(&smmu->iommu);

Okay. Will update in v7.