devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thierry Reding <thierry.reding@gmail.com>
To: Joerg Roedel <joro@8bytes.org>, Rob Herring <robh+dt@kernel.org>
Cc: Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	Nicolin Chen <nicolinc@nvidia.com>,
	Krishna Reddy <vdumpa@nvidia.com>,
	Dmitry Osipenko <digetx@gmail.com>,
	devicetree@vger.kernel.org, iommu@lists.linux-foundation.org,
	linux-tegra@vger.kernel.org,
	Frank Rowand <frowand.list@gmail.com>,
	Rob Herring <robh@kernel.org>
Subject: [PATCH v2 2/5] iommu: Implement of_iommu_get_resv_regions()
Date: Fri, 23 Apr 2021 18:32:31 +0200	[thread overview]
Message-ID: <20210423163234.3651547-3-thierry.reding@gmail.com> (raw)
In-Reply-To: <20210423163234.3651547-1-thierry.reding@gmail.com>

From: Thierry Reding <treding@nvidia.com>

This is an implementation that IOMMU drivers can use to obtain reserved
memory regions from a device tree node. It uses the reserved-memory DT
bindings to find the regions associated with a given device. If these
regions are marked accordingly, identity mappings will be created for
them in the IOMMU domain that the devices will be attached to.

Cc: Frank Rowand <frowand.list@gmail.com>
Cc: devicetree@vger.kernel.org
Reviewed-by: Rob Herring <robh@kernel.org>
Signed-off-by: Thierry Reding <treding@nvidia.com>
---
Changes in v3:
- change "active" property to identity mapping flag that is part of the
  memory region specifier (as defined by #memory-region-cells) to allow
  per-reference flags to be used

Changes in v2:
- use "active" property to determine whether direct mappings are needed
---
 drivers/iommu/of_iommu.c | 54 ++++++++++++++++++++++++++++++++++++++++
 include/linux/of_iommu.h |  8 ++++++
 2 files changed, 62 insertions(+)

diff --git a/drivers/iommu/of_iommu.c b/drivers/iommu/of_iommu.c
index a9d2df001149..321ebd5fdaba 100644
--- a/drivers/iommu/of_iommu.c
+++ b/drivers/iommu/of_iommu.c
@@ -11,12 +11,15 @@
 #include <linux/module.h>
 #include <linux/msi.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/of_iommu.h>
 #include <linux/of_pci.h>
 #include <linux/pci.h>
 #include <linux/slab.h>
 #include <linux/fsl/mc.h>
 
+#include <dt-bindings/reserved-memory.h>
+
 #define NO_IOMMU	1
 
 /**
@@ -240,3 +243,54 @@ const struct iommu_ops *of_iommu_configure(struct device *dev,
 
 	return ops;
 }
+
+/**
+ * of_iommu_get_resv_regions - reserved region driver helper for device tree
+ * @dev: device for which to get reserved regions
+ * @list: reserved region list
+ *
+ * IOMMU drivers can use this to implement their .get_resv_regions() callback
+ * for memory regions attached to a device tree node. See the reserved-memory
+ * device tree bindings on how to use these:
+ *
+ *   Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
+ */
+void of_iommu_get_resv_regions(struct device *dev, struct list_head *list)
+{
+	struct of_phandle_iterator it;
+	int err;
+
+	of_for_each_phandle(&it, err, dev->of_node, "memory-region", "#memory-region-cells", 0) {
+		struct iommu_resv_region *region;
+		struct of_phandle_args args;
+		struct resource res;
+
+		args.args_count = of_phandle_iterator_args(&it, args.args, MAX_PHANDLE_ARGS);
+
+		err = of_address_to_resource(it.node, 0, &res);
+		if (err < 0) {
+			dev_err(dev, "failed to parse memory region %pOF: %d\n",
+				it.node, err);
+			continue;
+		}
+
+		if (args.args_count > 0) {
+			/*
+			 * Active memory regions are expected to be accessed by hardware during
+			 * boot and must therefore have an identity mapping created prior to the
+			 * driver taking control of the hardware. This ensures that non-quiescent
+			 * hardware doesn't cause IOMMU faults during boot.
+			 */
+			if (args.args[0] & MEMORY_REGION_IDENTITY_MAPPING) {
+				region = iommu_alloc_resv_region(res.start, resource_size(&res),
+								 IOMMU_READ | IOMMU_WRITE,
+								 IOMMU_RESV_DIRECT_RELAXABLE);
+				if (!region)
+					continue;
+
+				list_add_tail(&region->list, list);
+			}
+		}
+	}
+}
+EXPORT_SYMBOL(of_iommu_get_resv_regions);
diff --git a/include/linux/of_iommu.h b/include/linux/of_iommu.h
index 16f4b3e87f20..8412437acaac 100644
--- a/include/linux/of_iommu.h
+++ b/include/linux/of_iommu.h
@@ -16,6 +16,9 @@ extern const struct iommu_ops *of_iommu_configure(struct device *dev,
 					struct device_node *master_np,
 					const u32 *id);
 
+extern void of_iommu_get_resv_regions(struct device *dev,
+				      struct list_head *list);
+
 #else
 
 static inline int of_get_dma_window(struct device_node *dn, const char *prefix,
@@ -32,6 +35,11 @@ static inline const struct iommu_ops *of_iommu_configure(struct device *dev,
 	return NULL;
 }
 
+static inline void of_iommu_get_resv_regions(struct device *dev,
+					     struct list_head *list)
+{
+}
+
 #endif	/* CONFIG_OF_IOMMU */
 
 #endif /* __OF_IOMMU_H */
-- 
2.30.2


  parent reply	other threads:[~2021-04-23 16:31 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-23 16:32 [PATCH v2 0/5] iommu: Support identity mappings of reserved-memory regions Thierry Reding
2021-04-23 16:32 ` [PATCH v2 1/5] dt-bindings: reserved-memory: Document memory region specifier Thierry Reding
2021-05-20 22:03   ` Rob Herring
2021-05-28 16:54     ` Thierry Reding
2021-06-08 16:51       ` Thierry Reding
2021-07-01 18:14         ` Thierry Reding
2021-07-02 14:16           ` Dmitry Osipenko
2021-09-01 14:13             ` Thierry Reding
2021-09-03 13:20               ` Rob Herring
2021-09-03 13:52                 ` Thierry Reding
2021-09-03 14:36                   ` Rob Herring
2021-09-03 15:35                     ` Thierry Reding
2021-09-07 15:33                       ` Rob Herring
2021-09-07 17:44                         ` Thierry Reding
2021-09-15 15:19                           ` Thierry Reding
2022-02-06 22:27                             ` Janne Grunau
2022-02-09 16:31                               ` Thierry Reding
2022-02-10 23:15                                 ` Janne Grunau
2022-03-31 16:25                                   ` Thierry Reding
2022-04-01 17:08                                     ` Janne Grunau
2021-04-23 16:32 ` Thierry Reding [this message]
2021-07-02 14:05   ` [PATCH v2 2/5] iommu: Implement of_iommu_get_resv_regions() Dmitry Osipenko
2021-07-16 14:41     ` Rob Herring
2021-07-17 11:07       ` Dmitry Osipenko
2021-07-30 12:18         ` Will Deacon
2021-04-23 16:32 ` [PATCH v2 3/5] iommu: dma: Use of_iommu_get_resv_regions() Thierry Reding
2021-04-23 16:32 ` [PATCH v2 4/5] iommu/tegra-smmu: Add support for reserved regions Thierry Reding
2021-04-23 16:32 ` [PATCH v2 5/5] iommu/tegra-smmu: Support managed domains Thierry Reding
2021-10-11 23:25   ` Dmitry Osipenko
2021-04-24  7:26 ` [PATCH v2 0/5] iommu: Support identity mappings of reserved-memory regions Dmitry Osipenko
2021-04-27 18:30   ` Krishna Reddy
2021-04-28  5:44     ` Dmitry Osipenko
2021-04-29  5:51       ` Krishna Reddy
2021-04-29 12:43         ` Dmitry Osipenko
2021-04-28  5:51 ` Dmitry Osipenko
2021-04-28  5:57   ` Mikko Perttunen
2021-04-28  7:55     ` Dmitry Osipenko
2021-04-28  5:59 ` Dmitry Osipenko
2021-10-03  1:09 ` Dmitry Osipenko
2021-10-04 19:23   ` Thierry Reding
2021-10-04 20:32     ` Dmitry Osipenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210423163234.3651547-3-thierry.reding@gmail.com \
    --to=thierry.reding@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=digetx@gmail.com \
    --cc=frowand.list@gmail.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=joro@8bytes.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=nicolinc@nvidia.com \
    --cc=robh+dt@kernel.org \
    --cc=robh@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=vdumpa@nvidia.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).