From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] of: platform: port Linux of_dma_is_coherent
Date: Mon, 5 Dec 2022 14:32:27 +0100 [thread overview]
Message-ID: <20221205133227.3010785-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20221205133227.3010785-1-a.fatoum@pengutronix.de>
In order to correctly handle cache-coherent systems on ARM, we need to
be able to determine whether a device instantiated from DT is DMA
coherent. Port the Linux helper that does this check.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
of_dma_is_coherent is still unused, but it will be used in future.
---
arch/riscv/Kconfig | 1 +
drivers/of/Kconfig | 4 ++++
drivers/of/platform.c | 41 +++++++++++++++++++++++++++++++++++++++++
include/of_address.h | 6 ++++++
4 files changed, 52 insertions(+)
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index cd7f4abf5694..afbd55aa3e6f 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -10,6 +10,7 @@ config RISCV
select COMMON_CLK_OF_PROVIDER
select CLKDEV_LOOKUP
select HAS_DMA
+ select OF_DMA_DEFAULT_COHERENT
select HAVE_PBL_IMAGE
select HAVE_PBL_MULTI_IMAGES
select HAVE_IMAGE_COMPRESSION
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index 7283331ba9ce..4dc40b27f442 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -31,6 +31,10 @@ config FEATURE_CONTROLLER_FIXUP
config OF_ADDRESS_PCI
bool
+config OF_DMA_DEFAULT_COHERENT
+ # arches should select this if DMA is coherent by default for OF devices
+ bool
+
config OF_GPIO
depends on GPIOLIB
depends on OFDEVICE
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
index a9a5d4c2daf2..2b5ad08c124b 100644
--- a/drivers/of/platform.c
+++ b/drivers/of/platform.c
@@ -73,6 +73,47 @@ static void of_device_make_bus_id(struct device_d *dev)
}
}
+static struct device_node *of_get_next_dma_parent(const struct device_node *np)
+{
+ struct of_phandle_args args;
+ int ret, index;
+
+ index = of_property_match_string(np, "interconnect-names", "dma-mem");
+ if (index < 0)
+ return of_get_parent(np);
+
+ ret = of_parse_phandle_with_args(np, "interconnects",
+ "#interconnect-cells",
+ index, &args);
+ if (ret < 0)
+ return of_get_parent(np);
+
+ return args.np;
+}
+
+/**
+ * of_dma_is_coherent - Check if device is coherent
+ * @np: device node
+ *
+ * It returns true if "dma-coherent" property was found
+ * for this device in the DT, or if DMA is coherent by
+ * default for OF devices on the current platform and no
+ * "dma-noncoherent" property was found for this device.
+ */
+bool of_dma_is_coherent(struct device_node *node)
+{
+ while (node) {
+ if (of_property_read_bool(node, "dma-coherent"))
+ return true;
+ if (of_property_read_bool(node, "dma-noncoherent"))
+ return false;
+ node = of_get_next_dma_parent(node);
+ }
+
+ return IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT);
+}
+EXPORT_SYMBOL_GPL(of_dma_is_coherent);
+
static void of_dma_configure(struct device_d *dev, struct device_node *np)
{
u64 dma_addr, paddr, size = 0;
diff --git a/include/of_address.h b/include/of_address.h
index 66117b1fa7f8..4e5faf6f7783 100644
--- a/include/of_address.h
+++ b/include/of_address.h
@@ -60,6 +60,8 @@ extern void __iomem *of_iomap(struct device_node *np, int index);
extern int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr,
u64 *size);
+extern bool of_dma_is_coherent(struct device_node *np);
+
#else /* CONFIG_OFTREE */
static inline u64 of_translate_address(struct device_node *dev,
@@ -109,6 +111,10 @@ static inline int of_dma_get_range(struct device_node *np, u64 *dma_addr,
return -ENOSYS;
}
+static inline bool of_dma_is_coherent(struct device_node *np)
+{
+ return false;
+}
#endif /* CONFIG_OFTREE */
#ifdef CONFIG_OF_PCI
--
2.30.2
next prev parent reply other threads:[~2022-12-05 13:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-05 13:32 [PATCH 1/2] of: have of_property_match_string accept pointer to const device_node Ahmad Fatoum
2022-12-05 13:32 ` Ahmad Fatoum [this message]
2022-12-07 6:58 ` Sascha Hauer
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=20221205133227.3010785-2-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.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