[PATCH] dma: move dma_map/unmap_single from ARM to common code

Ahmad Fatoum ahmad at a3f.at
Wed Mar 3 16:12:47 GMT 2021


There's nothing ARM specific about these functions. Move them to a
common location, so other arches can use them as well.

This also fixes a bug on ARM MMU=n configurations:
Previously these two functions these functions only respected
dma-ranges translation when compiled with MMU support. DMA address
translation is applicable regardless of CPU use of MMU though.
Now, dma-ranges should be respected unconditionally.

Signed-off-by: Ahmad Fatoum <ahmad at a3f.at>
---
Sascha, please replace the original patch with this one.
---
 arch/arm/cpu/mmu-common.c      | 41 +--------------------------------
 arch/arm/include/asm/dma.h     | 11 ---------
 arch/sandbox/include/asm/dma.h | 11 ---------
 drivers/dma/Makefile           |  1 +
 drivers/dma/map.c              | 42 ++++++++++++++++++++++++++++++++++
 5 files changed, 44 insertions(+), 62 deletions(-)
 create mode 100644 drivers/dma/map.c

diff --git a/arch/arm/cpu/mmu-common.c b/arch/arm/cpu/mmu-common.c
index 287622b20399..5cc5138cfa3d 100644
--- a/arch/arm/cpu/mmu-common.c
+++ b/arch/arm/cpu/mmu-common.c
@@ -10,27 +10,6 @@
 #include <memory.h>
 #include "mmu.h"
 
-
-static inline dma_addr_t cpu_to_dma(struct device_d *dev, unsigned long cpu_addr)
-{
-	dma_addr_t dma_addr = cpu_addr;
-
-	if (dev)
-		dma_addr -= dev->dma_offset;
-
-	return dma_addr;
-}
-
-static inline unsigned long dma_to_cpu(struct device_d *dev, dma_addr_t addr)
-{
-	unsigned long cpu_addr = addr;
-
-	if (dev)
-		cpu_addr += dev->dma_offset;
-
-	return cpu_addr;
-}
-
 void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
 			     enum dma_data_direction dir)
 {
@@ -41,24 +20,6 @@ void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
 		dma_inv_range((void *)address, size);
 }
 
-dma_addr_t dma_map_single(struct device_d *dev, void *ptr, size_t size,
-			  enum dma_data_direction dir)
-{
-	unsigned long addr = (unsigned long)ptr;
-
-	dma_sync_single_for_device(addr, size, dir);
-
-	return cpu_to_dma(dev, addr);
-}
-
-void dma_unmap_single(struct device_d *dev, dma_addr_t dma_addr, size_t size,
-		      enum dma_data_direction dir)
-{
-	unsigned long addr = dma_to_cpu(dev, dma_addr);
-
-	dma_sync_single_for_cpu(addr, size, dir);
-}
-
 void *dma_alloc_map(size_t size, dma_addr_t *dma_handle, unsigned flags)
 {
 	void *ret;
@@ -108,4 +69,4 @@ static int mmu_init(void)
 
 	return 0;
 }
-mmu_initcall(mmu_init);
\ No newline at end of file
+mmu_initcall(mmu_init);
diff --git a/arch/arm/include/asm/dma.h b/arch/arm/include/asm/dma.h
index 08a9fc43b7fc..226b1c146479 100644
--- a/arch/arm/include/asm/dma.h
+++ b/arch/arm/include/asm/dma.h
@@ -32,17 +32,6 @@ static inline void dma_free_coherent(void *mem, dma_addr_t dma_handle,
 	free(mem);
 }
 
-static inline dma_addr_t dma_map_single(struct device_d *dev, void *ptr, size_t size,
-					enum dma_data_direction dir)
-{
-	return (dma_addr_t)ptr;
-}
-
-static inline void dma_unmap_single(struct device_d *dev, dma_addr_t addr, size_t size,
-				    enum dma_data_direction dir)
-{
-}
-
 static inline void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
 					   enum dma_data_direction dir)
 {
diff --git a/arch/sandbox/include/asm/dma.h b/arch/sandbox/include/asm/dma.h
index 5e72d8e7df2d..34c0fc5190f8 100644
--- a/arch/sandbox/include/asm/dma.h
+++ b/arch/sandbox/include/asm/dma.h
@@ -40,17 +40,6 @@ static inline void dma_free_coherent(void *mem, dma_addr_t dma_handle,
 	free(mem);
 }
 
-static inline dma_addr_t dma_map_single(struct device_d *dev, void *ptr, size_t size,
-					enum dma_data_direction dir)
-{
-	return (dma_addr_t)ptr;
-}
-
-static inline void dma_unmap_single(struct device_d *dev, dma_addr_t addr, size_t size,
-				    enum dma_data_direction dir)
-{
-}
-
 static inline void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
 					   enum dma_data_direction dir)
 {
diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile
index 7a3a3b2bd85d..49d6d6573f4f 100644
--- a/drivers/dma/Makefile
+++ b/drivers/dma/Makefile
@@ -1 +1,2 @@
 obj-$(CONFIG_MXS_APBH_DMA)	+= apbh_dma.o
+obj-$(CONFIG_HAS_DMA)		+= map.o
diff --git a/drivers/dma/map.c b/drivers/dma/map.c
new file mode 100644
index 000000000000..a3e1b3b5b527
--- /dev/null
+++ b/drivers/dma/map.c
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* SPDX-FileCopyrightText: 2012 Marc Kleine-Budde <mkl at pengutronix.de> */
+
+#include <dma.h>
+
+static inline dma_addr_t cpu_to_dma(struct device_d *dev, unsigned long cpu_addr)
+{
+	dma_addr_t dma_addr = cpu_addr;
+
+	if (dev)
+		dma_addr -= dev->dma_offset;
+
+	return dma_addr;
+}
+
+static inline unsigned long dma_to_cpu(struct device_d *dev, dma_addr_t addr)
+{
+	unsigned long cpu_addr = addr;
+
+	if (dev)
+		cpu_addr += dev->dma_offset;
+
+	return cpu_addr;
+}
+
+dma_addr_t dma_map_single(struct device_d *dev, void *ptr, size_t size,
+			  enum dma_data_direction dir)
+{
+	unsigned long addr = (unsigned long)ptr;
+
+	dma_sync_single_for_device(addr, size, dir);
+
+	return cpu_to_dma(dev, addr);
+}
+
+void dma_unmap_single(struct device_d *dev, dma_addr_t dma_addr, size_t size,
+		      enum dma_data_direction dir)
+{
+	unsigned long addr = dma_to_cpu(dev, dma_addr);
+
+	dma_sync_single_for_cpu(addr, size, dir);
+}
-- 
2.30.0




More information about the barebox mailing list