mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [RFC 0/2] dma: clean up streaming DMA API
@ 2023-02-28 10:32 Denis Orlov
  2023-02-28 10:32 ` [RFC 1/2] dma: rework dma_sync_single interface Denis Orlov
  2023-02-28 10:32 ` [RFC 2/2] dma: update dma_sync_single uses in drivers Denis Orlov
  0 siblings, 2 replies; 5+ messages in thread
From: Denis Orlov @ 2023-02-28 10:32 UTC (permalink / raw)
  To: barebox; +Cc: Denis Orlov, Ahmad Fatoum

This fixes some incoherence between dma_map/free_coherent() and
dma_sync_single_for_*() functions. While the former do appropriate
cpu/dma conversions, the latter do not. This also fixes streaming DMA
for MIPS CPUs and cleans up the interface somewhat.

I want to get some thoughts on whether those are the appropriate changes
to make and if I missed something important.

The changes are split in two parts for now so it would be easier to
review them. The first commit is the main part. It contains interface
changes and arch-specific code adjustments. The second one just updates
the functions usage in drivers.

Denis Orlov (2):
  dma: rework dma_sync_single interface
  dma: update dma_sync_single uses in drivers

 arch/arm/cpu/mmu-common.c            |   9 +--
 arch/arm/cpu/mmu.c                   |  17 +++--
 arch/arm/cpu/mmu_64.c                |  13 ++--
 arch/arm/include/asm/dma.h           |  12 ++--
 arch/arm/mach-bcm283x/mbox.c         |   4 +-
 arch/kvx/lib/dma-default.c           |  16 +++--
 arch/mips/lib/dma-default.c          |  12 ++--
 arch/riscv/cpu/dma.c                 |  23 +++---
 arch/riscv/include/asm/dma.h         |   4 +-
 arch/sandbox/include/asm/dma.h       |  12 ++--
 arch/x86/include/asm/dma.h           |  12 ++--
 drivers/crypto/caam/caam-blobgen.c   |  28 ++++----
 drivers/crypto/caam/caamrng.c        |   6 +-
 drivers/crypto/caam/rng_self_test.c  |   6 +-
 drivers/dma/Makefile                 |   1 -
 drivers/dma/map.c                    |  39 -----------
 drivers/mci/dove-sdhci.c             |   8 +--
 drivers/mci/stm32_sdmmc2.c           |   8 +--
 drivers/mci/tegra-sdmmc.c            |   8 +--
 drivers/net/ag71xx.c                 |   8 +--
 drivers/net/arc_emac.c               |  10 +--
 drivers/net/at91_ether.c             |   8 +--
 drivers/net/bcmgenet.c               |   4 +-
 drivers/net/cpsw.c                   |   8 +--
 drivers/net/davinci_emac.c           |   8 +--
 drivers/net/designware.c             |  10 +--
 drivers/net/designware_eqos.c        |   4 +-
 drivers/net/e1000/main.c             |   4 +-
 drivers/net/fec_imx.c                |   4 +-
 drivers/net/fsl-fman.c               |   7 +-
 drivers/net/macb.c                   |  20 +++---
 drivers/net/mvneta.c                 |   8 +--
 drivers/net/orion-gbe.c              |   8 +--
 drivers/net/rtl8169.c                |  12 ++--
 drivers/soc/sifive/sifive_l2_cache.c |   2 +-
 drivers/soc/starfive/jh7100_dma.c    |   2 +-
 drivers/usb/gadget/fsl_udc.c         |   4 +-
 drivers/usb/host/ohci-hcd.c          |   6 +-
 include/dma.h                        | 101 +++++++++++++++++++--------
 39 files changed, 240 insertions(+), 236 deletions(-)
 delete mode 100644 drivers/dma/map.c

-- 
2.30.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC 1/2] dma: rework dma_sync_single interface
  2023-02-28 10:32 [RFC 0/2] dma: clean up streaming DMA API Denis Orlov
@ 2023-02-28 10:32 ` Denis Orlov
  2023-03-03 10:06   ` Sascha Hauer
  2023-02-28 10:32 ` [RFC 2/2] dma: update dma_sync_single uses in drivers Denis Orlov
  1 sibling, 1 reply; 5+ messages in thread
From: Denis Orlov @ 2023-02-28 10:32 UTC (permalink / raw)
  To: barebox; +Cc: Denis Orlov, Ahmad Fatoum

Currently, a lot of code is handling dma_addr_t values as if they
unconditionally hold CPU addresses. However, this is not true for every
architecture. For example, MIPS requires an explicit conversion from a
physical address space to a virtual address that corresponds to some
(currently used one in our case) address space segment for this pointer
to be valid and usable. Another issue is that DMA ranges, specified in
device trees, will not work this way. To get from a virtual address to
a dma handle and vice versa we need to add/subtract some offset, which
is calculated from "dma-ranges" property. As it seems, no driver
actually uses this at the moment, but as there exists code that does
this conversions, which is used in dma_map_single(), it seems reasonable
to utilize it for dma_sync_single_for_*() too.

I wonder if there are some actual use cases that would not be properly
covered by this change, for example if there are dma/cpu conversions
that we may have to do in architecture-specific code and which require
virtual addresses. For those, we cannot have dma_sync_single_for_*
functions that just call arch_sync_single_for_* only for the CPU cache
syncronization part, i.e. we would have to do all dma/cpu conversions in
arch-specific code, unlike how it is done in this patch. This would
lead to some code duplication though.

TODO: dma_alloc/free_coherent do need same kind of changes to utilise
cpu/dma conversions properly

Signed-off-by: Denis Orlov <denorl2009@gmail.com>
---
 arch/arm/cpu/mmu-common.c            |   9 +--
 arch/arm/cpu/mmu.c                   |  17 +++--
 arch/arm/cpu/mmu_64.c                |  13 ++--
 arch/arm/include/asm/dma.h           |  12 ++--
 arch/kvx/lib/dma-default.c           |  16 +++--
 arch/mips/lib/dma-default.c          |  12 ++--
 arch/riscv/cpu/dma.c                 |  23 +++---
 arch/riscv/include/asm/dma.h         |   4 +-
 arch/sandbox/include/asm/dma.h       |  12 ++--
 arch/x86/include/asm/dma.h           |  12 ++--
 drivers/dma/Makefile                 |   1 -
 drivers/dma/map.c                    |  39 -----------
 drivers/soc/sifive/sifive_l2_cache.c |   2 +-
 drivers/soc/starfive/jh7100_dma.c    |   2 +-
 include/dma.h                        | 101 +++++++++++++++++++--------
 15 files changed, 138 insertions(+), 137 deletions(-)
 delete mode 100644 drivers/dma/map.c

diff --git a/arch/arm/cpu/mmu-common.c b/arch/arm/cpu/mmu-common.c
index 488a189f1c..f523cd2660 100644
--- a/arch/arm/cpu/mmu-common.c
+++ b/arch/arm/cpu/mmu-common.c
@@ -13,14 +13,11 @@
 #include <memory.h>
 #include "mmu.h"
 
-void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
-			     enum dma_data_direction dir)
+void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+			   enum dma_data_direction dir)
 {
-	/*
-	 * FIXME: This function needs a device argument to support non 1:1 mappings
-	 */
 	if (dir != DMA_TO_DEVICE)
-		dma_inv_range((void *)address, size);
+		dma_inv_range(vaddr, size);
 }
 
 void *dma_alloc_map(size_t size, dma_addr_t *dma_handle, unsigned flags)
diff --git a/arch/arm/cpu/mmu.c b/arch/arm/cpu/mmu.c
index 6388e1bf14..c1615682fc 100644
--- a/arch/arm/cpu/mmu.c
+++ b/arch/arm/cpu/mmu.c
@@ -494,20 +494,19 @@ void *dma_alloc_writecombine(size_t size, dma_addr_t *dma_handle)
 	return dma_alloc_map(size, dma_handle, ARCH_MAP_WRITECOMBINE);
 }
 
-void dma_sync_single_for_device(dma_addr_t address, size_t size,
-				enum dma_data_direction dir)
+void arch_sync_dma_for_device(void *vaddr, size_t size,
+			      enum dma_data_direction dir)
 {
-	/*
-	 * FIXME: This function needs a device argument to support non 1:1 mappings
-	 */
+	unsigned long start = (unsigned long)vaddr;
+	unsigned long end = start + size;
 
 	if (dir == DMA_FROM_DEVICE) {
-		__dma_inv_range(address, address + size);
+		__dma_inv_range(start, end);
 		if (outer_cache.inv_range)
-			outer_cache.inv_range(address, address + size);
+			outer_cache.inv_range(start, end);
 	} else {
-		__dma_clean_range(address, address + size);
+		__dma_clean_range(start, end);
 		if (outer_cache.clean_range)
-			outer_cache.clean_range(address, address + size);
+			outer_cache.clean_range(start, end);
 	}
 }
diff --git a/arch/arm/cpu/mmu_64.c b/arch/arm/cpu/mmu_64.c
index f43ac9a121..e5b0d55e71 100644
--- a/arch/arm/cpu/mmu_64.c
+++ b/arch/arm/cpu/mmu_64.c
@@ -249,15 +249,14 @@ void dma_flush_range(void *ptr, size_t size)
 	v8_flush_dcache_range(start, end);
 }
 
-void dma_sync_single_for_device(dma_addr_t address, size_t size,
-                                enum dma_data_direction dir)
+void arch_sync_dma_for_device(void *vaddr, size_t size,
+			      enum dma_data_direction dir)
 {
-	/*
-	 * FIXME: This function needs a device argument to support non 1:1 mappings
-	 */
+	unsigned long start = (unsigned long)vaddr;
+	unsigned long end = start + size - 1;
 
 	if (dir == DMA_FROM_DEVICE)
-		v8_inv_dcache_range(address, address + size - 1);
+		v8_inv_dcache_range(start, end);
 	else
-		v8_flush_dcache_range(address, address + size - 1);
+		v8_flush_dcache_range(start, end);
 }
diff --git a/arch/arm/include/asm/dma.h b/arch/arm/include/asm/dma.h
index 75a6c1ad86..53953a4863 100644
--- a/arch/arm/include/asm/dma.h
+++ b/arch/arm/include/asm/dma.h
@@ -35,15 +35,15 @@ static inline void dma_free_coherent(void *mem, dma_addr_t dma_handle,
 	free(mem);
 }
 
-#define dma_sync_single_for_cpu dma_sync_single_for_cpu
-static inline void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
-					   enum dma_data_direction dir)
+#define arch_sync_dma_for_cpu arch_sync_dma_for_cpu
+static inline void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+					 enum dma_data_direction dir)
 {
 }
 
-#define dma_sync_single_for_device dma_sync_single_for_device
-static inline void dma_sync_single_for_device(dma_addr_t address, size_t size,
-					      enum dma_data_direction dir)
+#define arch_sync_dma_for_device arch_sync_dma_for_device
+static inline void arch_sync_dma_for_device(void *vaddr, size_t size,
+					    enum dma_data_direction dir)
 {
 }
 #endif
diff --git a/arch/kvx/lib/dma-default.c b/arch/kvx/lib/dma-default.c
index c84a32954e..8eaa1df5f0 100644
--- a/arch/kvx/lib/dma-default.c
+++ b/arch/kvx/lib/dma-default.c
@@ -20,13 +20,15 @@
  * (see https://lkml.org/lkml/2018/5/18/979)
  */
 
-void dma_sync_single_for_device(dma_addr_t addr, size_t size,
-				enum dma_data_direction dir)
+void arch_sync_dma_for_device(void *vaddr, size_t size,
+			      enum dma_data_direction dir)
 {
+	unsigned long address = (unsigned long)vaddr;
+
 	/* dcache is Write-Through: no need to flush to force writeback */
 	switch (dir) {
 	case DMA_FROM_DEVICE:
-		invalidate_dcache_range(addr, addr + size);
+		invalidate_dcache_range(address, address + size);
 		break;
 	case DMA_TO_DEVICE:
 	case DMA_BIDIRECTIONAL:
@@ -38,9 +40,11 @@ void dma_sync_single_for_device(dma_addr_t addr, size_t size,
 	}
 }
 
-void dma_sync_single_for_cpu(dma_addr_t addr, size_t size,
-				enum dma_data_direction dir)
+void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+			   enum dma_data_direction dir)
 {
+	unsigned long address = (unsigned long)vaddr;
+
 	/* CPU does not speculatively prefetches */
 	switch (dir) {
 	case DMA_FROM_DEVICE:
@@ -48,7 +52,7 @@ void dma_sync_single_for_cpu(dma_addr_t addr, size_t size,
 	case DMA_TO_DEVICE:
 		break;
 	case DMA_BIDIRECTIONAL:
-		invalidate_dcache_range(addr, addr + size);
+		invalidate_dcache_range(address, address + size);
 		break;
 	default:
 		BUG();
diff --git a/arch/mips/lib/dma-default.c b/arch/mips/lib/dma-default.c
index 48176e5d28..9392d8ee29 100644
--- a/arch/mips/lib/dma-default.c
+++ b/arch/mips/lib/dma-default.c
@@ -27,14 +27,14 @@ static inline void __dma_sync_mips(unsigned long addr, size_t size,
 	}
 }
 
-void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
-			     enum dma_data_direction dir)
+void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+			   enum dma_data_direction dir)
 {
-	__dma_sync_mips(address, size, dir);
+	__dma_sync_mips((unsigned long)vaddr, size, dir);
 }
 
-void dma_sync_single_for_device(dma_addr_t address, size_t size,
-				enum dma_data_direction dir)
+void arch_sync_dma_for_device(void *vaddr, size_t size,
+			      enum dma_data_direction dir)
 {
-	__dma_sync_mips(address, size, dir);
+	__dma_sync_mips((unsigned long)vaddr, size, dir);
 }
diff --git a/arch/riscv/cpu/dma.c b/arch/riscv/cpu/dma.c
index 5a4d714e5e..511170aaa4 100644
--- a/arch/riscv/cpu/dma.c
+++ b/arch/riscv/cpu/dma.c
@@ -52,23 +52,24 @@ void dma_set_ops(const struct dma_ops *ops)
 	dma_ops = ops;
 }
 
-void dma_sync_single_for_cpu(dma_addr_t address, size_t size, enum dma_data_direction dir)
+void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+			   enum dma_data_direction dir)
 {
-        /*
-         * FIXME: This function needs a device argument to support non 1:1 mappings
-         */
+	unsigned long start = (unsigned long)vaddr;
+	unsigned long end = start + size;
+
         if (dir != DMA_TO_DEVICE)
-                dma_ops->inv_range(address, address + size);
+                dma_ops->inv_range(start, end);
 }
 
-void dma_sync_single_for_device(dma_addr_t address, size_t size, enum dma_data_direction dir)
+void arch_sync_dma_for_device(void *vaddr, size_t size,
+			      enum dma_data_direction dir)
 {
-        /*
-         * FIXME: This function needs a device argument to support non 1:1 mappings
-         */
+	unsigned long start = (unsigned long)vaddr;
+	unsigned long end = start + size;
 
         if (dir == DMA_FROM_DEVICE)
-                dma_ops->inv_range(address, address + size);
+                dma_ops->inv_range(start, end);
         else
-                dma_ops->flush_range(address, address + size);
+                dma_ops->flush_range(start, end);
 }
diff --git a/arch/riscv/include/asm/dma.h b/arch/riscv/include/asm/dma.h
index 56bcf06cc4..db0cd0c735 100644
--- a/arch/riscv/include/asm/dma.h
+++ b/arch/riscv/include/asm/dma.h
@@ -8,8 +8,8 @@ struct dma_ops {
 	void *(*alloc_coherent)(size_t size, dma_addr_t *dma_handle);
 	void (*free_coherent)(void *vaddr, dma_addr_t dma_handle, size_t size);
 
-	void (*flush_range)(dma_addr_t start, dma_addr_t end);
-	void (*inv_range)(dma_addr_t start, dma_addr_t end);
+	void (*flush_range)(unsigned long start, unsigned long end);
+	void (*inv_range)(unsigned long start, unsigned long end);
 };
 
 /* Override for SoCs with cache-incoherent DMA masters */
diff --git a/arch/sandbox/include/asm/dma.h b/arch/sandbox/include/asm/dma.h
index 958d10e2a1..ac8b408aae 100644
--- a/arch/sandbox/include/asm/dma.h
+++ b/arch/sandbox/include/asm/dma.h
@@ -43,15 +43,15 @@ static inline void dma_free_coherent(void *mem, dma_addr_t dma_handle,
 	free(mem);
 }
 
-#define dma_sync_single_for_cpu dma_sync_single_for_cpu
-static inline void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
-					   enum dma_data_direction dir)
+#define arch_sync_dma_for_cpu arch_sync_dma_for_cpu
+static inline void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+					 enum dma_data_direction dir)
 {
 }
 
-#define dma_sync_single_for_device dma_sync_single_for_device
-static inline void dma_sync_single_for_device(dma_addr_t address, size_t size,
-					      enum dma_data_direction dir)
+#define arch_sync_dma_for_device arch_sync_dma_for_device
+static inline void arch_sync_dma_for_device(void *vaddr, size_t size,
+					    enum dma_data_direction dir)
 {
 }
 
diff --git a/arch/x86/include/asm/dma.h b/arch/x86/include/asm/dma.h
index 90791ecf3e..fe486c687f 100644
--- a/arch/x86/include/asm/dma.h
+++ b/arch/x86/include/asm/dma.h
@@ -32,15 +32,15 @@ static inline void dma_free_coherent(void *mem, dma_addr_t dma_handle,
 	free(mem);
 }
 
-#define dma_sync_single_for_cpu dma_sync_single_for_cpu
-static inline void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
-					   enum dma_data_direction dir)
+#define arch_sync_dma_for_cpu arch_sync_dma_for_cpu
+static inline void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+					 enum dma_data_direction dir)
 {
 }
 
-#define dma_sync_single_for_device dma_sync_single_for_device
-static inline void dma_sync_single_for_device(dma_addr_t address, size_t size,
-					      enum dma_data_direction dir)
+#define arch_sync_dma_for_device arch_sync_dma_for_device
+static inline void arch_sync_dma_for_device(void *vaddr, size_t size,
+					    enum dma_data_direction dir)
 {
 }
 
diff --git a/drivers/dma/Makefile b/drivers/dma/Makefile
index 39829cab50..8e1aac9f6f 100644
--- a/drivers/dma/Makefile
+++ b/drivers/dma/Makefile
@@ -1,3 +1,2 @@
 # SPDX-License-Identifier: GPL-2.0-only
 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
deleted file mode 100644
index 114c0f7db3..0000000000
--- a/drivers/dma/map.c
+++ /dev/null
@@ -1,39 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-/* SPDX-FileCopyrightText: 2012 Marc Kleine-Budde <mkl@pengutronix.de> */
-
-#include <dma.h>
-#include <asm/io.h>
-
-static inline dma_addr_t cpu_to_dma(struct device *dev, void *cpu_addr)
-{
-	if (dev && dev->dma_offset)
-		return (unsigned long)cpu_addr - dev->dma_offset;
-
-	return virt_to_phys(cpu_addr);
-}
-
-static inline void *dma_to_cpu(struct device *dev, dma_addr_t addr)
-{
-	if (dev && dev->dma_offset)
-		return (void *)(addr + dev->dma_offset);
-
-	return phys_to_virt(addr);
-}
-
-dma_addr_t dma_map_single(struct device *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, ptr);
-}
-
-void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
-		      enum dma_data_direction dir)
-{
-	unsigned long addr = (unsigned long)dma_to_cpu(dev, dma_addr);
-
-	dma_sync_single_for_cpu(addr, size, dir);
-}
diff --git a/drivers/soc/sifive/sifive_l2_cache.c b/drivers/soc/sifive/sifive_l2_cache.c
index 03945e9bea..b9a8923631 100644
--- a/drivers/soc/sifive/sifive_l2_cache.c
+++ b/drivers/soc/sifive/sifive_l2_cache.c
@@ -68,7 +68,7 @@ static void sifive_l2_config_read(struct device *dev)
 	printf("  #Index of the largest way enabled: %d\n", regval);
 }
 
-void sifive_l2_flush64_range(dma_addr_t start, dma_addr_t end)
+void sifive_l2_flush64_range(unsigned long start, unsigned long end)
 {
 	unsigned long line;
 
diff --git a/drivers/soc/starfive/jh7100_dma.c b/drivers/soc/starfive/jh7100_dma.c
index e3cfc8cf65..f1cc83f25a 100644
--- a/drivers/soc/starfive/jh7100_dma.c
+++ b/drivers/soc/starfive/jh7100_dma.c
@@ -19,7 +19,7 @@ static inline void *jh7100_alloc_coherent(size_t size, dma_addr_t *dma_handle)
 
 	memset(ret, 0, size);
 
-	cpu_base = (dma_addr_t)ret;
+	cpu_base = (unsigned long)ret;
 
 	if (dma_handle)
 		*dma_handle = cpu_base;
diff --git a/include/dma.h b/include/dma.h
index 26c71d812e..382e32acbf 100644
--- a/include/dma.h
+++ b/include/dma.h
@@ -34,61 +34,102 @@ static inline void dma_free(void *mem)
 }
 #endif
 
-dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
-			  enum dma_data_direction dir);
-void dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
-		      enum dma_data_direction dir);
-
-#define DMA_BIT_MASK(n)	(((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
-
-#define DMA_MASK_NONE	0x0ULL
-
-static inline void dma_set_mask(struct device *dev, u64 dma_mask)
+static inline dma_addr_t cpu_to_dma(struct device *dev, void *cpu_addr)
 {
-	dev->dma_mask = dma_mask;
-}
+	if (dev && dev->dma_offset)
+		return (unsigned long)cpu_addr - dev->dma_offset;
 
-#define DMA_ERROR_CODE  (~(dma_addr_t)0)
+	return virt_to_phys(cpu_addr);
+}
 
-static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
+static inline void *dma_to_cpu(struct device *dev, dma_addr_t addr)
 {
-	return dma_addr == DMA_ERROR_CODE ||
-		(dev->dma_mask && dma_addr > dev->dma_mask);
+	if (dev && dev->dma_offset)
+		return (void *)(addr + dev->dma_offset);
+
+	return phys_to_virt(addr);
 }
 
 #ifndef __PBL__
 /* streaming DMA - implement the below calls to support HAS_DMA */
-#ifndef dma_sync_single_for_cpu
-void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
-			     enum dma_data_direction dir);
+#ifndef arch_sync_dma_for_cpu
+void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+			   enum dma_data_direction dir);
 #endif
 
-#ifndef dma_sync_single_for_device
-void dma_sync_single_for_device(dma_addr_t address, size_t size,
-				enum dma_data_direction dir);
+#ifndef arch_sync_dma_for_device
+void arch_sync_dma_for_device(void *vaddr, size_t size,
+			      enum dma_data_direction dir);
 #endif
 #else
-#ifndef dma_sync_single_for_cpu
+#ifndef arch_sync_dma_for_cpu
 /*
  * assumes buffers are in coherent/uncached memory, e.g. because
  * MMU is only enabled in barebox_arm_entry which hasn't run yet.
  */
-static inline void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
-			     enum dma_data_direction dir)
+void arch_sync_dma_for_cpu(void *vaddr, size_t size,
+			   enum dma_data_direction dir)
 {
-	barrier_data((void *)address);
+	barrier_data(vaddr);
 }
 #endif
 
-#ifndef dma_sync_single_for_device
-static inline void dma_sync_single_for_device(dma_addr_t address, size_t size,
-				enum dma_data_direction dir)
+#ifndef arch_sync_dma_for_device
+void arch_sync_dma_for_device(void *vaddr, size_t size,
+			      enum dma_data_direction dir);
 {
-	barrier_data((void *)address);
+	barrier_data(address);
 }
 #endif
 #endif
 
+static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t address,
+					   size_t size, enum dma_data_direction dir)
+{
+	void *ptr = dma_to_cpu(dev, address);
+
+	arch_sync_dma_for_cpu(ptr, size, dir);
+}
+
+static inline void dma_sync_single_for_device(struct device *dev, dma_addr_t address,
+					      size_t size, enum dma_data_direction dir)
+{
+	void *ptr = dma_to_cpu(dev, address);
+
+	arch_sync_dma_for_device(ptr, size, dir);
+}
+
+static inline dma_addr_t dma_map_single(struct device *dev, void *ptr,
+					size_t size, enum dma_data_direction dir)
+{
+	arch_sync_dma_for_device(ptr, size, dir);
+
+	return cpu_to_dma(dev, ptr);
+}
+
+static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
+				    size_t size, enum dma_data_direction dir)
+{
+	dma_sync_single_for_cpu(dev, dma_addr, size, dir);
+}
+
+#define DMA_BIT_MASK(n)	(((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
+
+#define DMA_MASK_NONE	0x0ULL
+
+static inline void dma_set_mask(struct device *dev, u64 dma_mask)
+{
+	dev->dma_mask = dma_mask;
+}
+
+#define DMA_ERROR_CODE  (~(dma_addr_t)0)
+
+static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
+{
+	return dma_addr == DMA_ERROR_CODE ||
+		(dev->dma_mask && dma_addr > dev->dma_mask);
+}
+
 #ifndef dma_alloc_coherent
 void *dma_alloc_coherent(size_t size, dma_addr_t *dma_handle);
 #endif
-- 
2.30.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC 2/2] dma: update dma_sync_single uses in drivers
  2023-02-28 10:32 [RFC 0/2] dma: clean up streaming DMA API Denis Orlov
  2023-02-28 10:32 ` [RFC 1/2] dma: rework dma_sync_single interface Denis Orlov
@ 2023-02-28 10:32 ` Denis Orlov
  1 sibling, 0 replies; 5+ messages in thread
From: Denis Orlov @ 2023-02-28 10:32 UTC (permalink / raw)
  To: barebox; +Cc: Denis Orlov, Ahmad Fatoum

With the previous commit that changes dma_sync_single_for_* API, every
callsite that uses it needs to be updated. Driver code is now required
to pass a corresponding pointer to device struct as the first argument.
Some drivers don't seem to have an appropriate device handle handy, so
just use NULL for those. This should not make any functional change, as
there is no driver at the moment that requires non 1-to-1 dma mappings,
and thus would actually utilize those device pointers.

Signed-off-by: Denis Orlov <denorl2009@gmail.com>
---
 arch/arm/mach-bcm283x/mbox.c        |  4 ++--
 drivers/crypto/caam/caam-blobgen.c  | 28 ++++++++++++++--------------
 drivers/crypto/caam/caamrng.c       |  6 +++---
 drivers/crypto/caam/rng_self_test.c |  6 +++---
 drivers/mci/dove-sdhci.c            |  8 ++++----
 drivers/mci/stm32_sdmmc2.c          |  8 ++++----
 drivers/mci/tegra-sdmmc.c           |  8 ++++----
 drivers/net/ag71xx.c                |  8 ++++----
 drivers/net/arc_emac.c              | 10 +++++-----
 drivers/net/at91_ether.c            |  8 ++++----
 drivers/net/bcmgenet.c              |  4 ++--
 drivers/net/cpsw.c                  |  8 ++++----
 drivers/net/davinci_emac.c          |  8 ++++----
 drivers/net/designware.c            | 10 +++++-----
 drivers/net/designware_eqos.c       |  4 ++--
 drivers/net/e1000/main.c            |  4 ++--
 drivers/net/fec_imx.c               |  4 ++--
 drivers/net/fsl-fman.c              |  7 ++++---
 drivers/net/macb.c                  | 20 ++++++++++----------
 drivers/net/mvneta.c                |  8 ++++----
 drivers/net/orion-gbe.c             |  8 ++++----
 drivers/net/rtl8169.c               | 12 +++++++-----
 drivers/usb/gadget/fsl_udc.c        |  4 ++--
 drivers/usb/host/ohci-hcd.c         |  6 +++---
 24 files changed, 102 insertions(+), 99 deletions(-)

diff --git a/arch/arm/mach-bcm283x/mbox.c b/arch/arm/mach-bcm283x/mbox.c
index 4959a1a652..c432c1153f 100644
--- a/arch/arm/mach-bcm283x/mbox.c
+++ b/arch/arm/mach-bcm283x/mbox.c
@@ -66,7 +66,7 @@ static int bcm2835_mbox_call_raw(u32 chan, struct bcm2835_mbox_hdr *buffer,
 	/* Send the request */
 	val = BCM2835_MBOX_PACK(chan, send);
 	pr_debug("mbox: TX raw: 0x%08x\n", val);
-	dma_sync_single_for_device((unsigned long)send, buffer->buf_size,
+	dma_sync_single_for_device(NULL, (unsigned long)send, buffer->buf_size,
 				   DMA_BIDIRECTIONAL);
 	writel(val, mbox_base + MAIL1_WRT);
 
@@ -84,7 +84,7 @@ static int bcm2835_mbox_call_raw(u32 chan, struct bcm2835_mbox_hdr *buffer,
 	/* Read the response */
 	val = readl(mbox_base + MAIL0_RD);
 	pr_debug("mbox: RX raw: 0x%08x\n", val);
-	dma_sync_single_for_cpu((unsigned long)send, buffer->buf_size,
+	dma_sync_single_for_cpu(NULL, (unsigned long)send, buffer->buf_size,
 				DMA_BIDIRECTIONAL);
 
 	/* Validate the response */
diff --git a/drivers/crypto/caam/caam-blobgen.c b/drivers/crypto/caam/caam-blobgen.c
index 5ab7ad3a6e..1ce636a716 100644
--- a/drivers/crypto/caam/caam-blobgen.c
+++ b/drivers/crypto/caam/caam-blobgen.c
@@ -129,14 +129,14 @@ static int caam_blob_decrypt(struct blobgen *bg, const char *modifier,
 
 	jr_jobdesc_blob_decap(ctx, modifier_len, blobsize);
 
-	dma_sync_single_for_device((unsigned long)desc, desc_bytes(desc),
+	dma_sync_single_for_device(jrdev, (unsigned long)desc, desc_bytes(desc),
 				   DMA_TO_DEVICE);
 
-	dma_sync_single_for_device((unsigned long)modifier, modifier_len,
+	dma_sync_single_for_device(jrdev, (unsigned long)modifier, modifier_len,
 				   DMA_TO_DEVICE);
-	dma_sync_single_for_device((unsigned long)*plain, *plainsize,
+	dma_sync_single_for_device(jrdev, (unsigned long)*plain, *plainsize,
 				   DMA_FROM_DEVICE);
-	dma_sync_single_for_device((unsigned long)blob, blobsize,
+	dma_sync_single_for_device(jrdev, (unsigned long)blob, blobsize,
 				   DMA_TO_DEVICE);
 
 	testres.err = 0;
@@ -147,11 +147,11 @@ static int caam_blob_decrypt(struct blobgen *bg, const char *modifier,
 
 	ret = testres.err;
 
-	dma_sync_single_for_cpu((unsigned long)modifier, modifier_len,
+	dma_sync_single_for_cpu(jrdev, (unsigned long)modifier, modifier_len,
 				DMA_TO_DEVICE);
-	dma_sync_single_for_cpu((unsigned long)*plain, *plainsize,
+	dma_sync_single_for_cpu(jrdev, (unsigned long)*plain, *plainsize,
 				DMA_FROM_DEVICE);
-	dma_sync_single_for_cpu((unsigned long)blob, blobsize,
+	dma_sync_single_for_cpu(jrdev, (unsigned long)blob, blobsize,
 				DMA_TO_DEVICE);
 
 	return ret;
@@ -178,14 +178,14 @@ static int caam_blob_encrypt(struct blobgen *bg, const char *modifier,
 
 	jr_jobdesc_blob_encap(ctx, modifier_len, plainsize);
 
-	dma_sync_single_for_device((unsigned long)desc, desc_bytes(desc),
+	dma_sync_single_for_device(jrdev, (unsigned long)desc, desc_bytes(desc),
 				   DMA_TO_DEVICE);
 
-	dma_sync_single_for_device((unsigned long)modifier, modifier_len,
+	dma_sync_single_for_device(jrdev, (unsigned long)modifier, modifier_len,
 				   DMA_TO_DEVICE);
-	dma_sync_single_for_device((unsigned long)plain, plainsize,
+	dma_sync_single_for_device(jrdev, (unsigned long)plain, plainsize,
 				   DMA_TO_DEVICE);
-	dma_sync_single_for_device((unsigned long)blob, *blobsize,
+	dma_sync_single_for_device(jrdev, (unsigned long)blob, *blobsize,
 				   DMA_FROM_DEVICE);
 
 	testres.err = 0;
@@ -196,11 +196,11 @@ static int caam_blob_encrypt(struct blobgen *bg, const char *modifier,
 
 	ret = testres.err;
 
-	dma_sync_single_for_cpu((unsigned long)modifier, modifier_len,
+	dma_sync_single_for_cpu(jrdev, (unsigned long)modifier, modifier_len,
 				DMA_TO_DEVICE);
-	dma_sync_single_for_cpu((unsigned long)plain, plainsize,
+	dma_sync_single_for_cpu(jrdev, (unsigned long)plain, plainsize,
 				DMA_TO_DEVICE);
-	dma_sync_single_for_cpu((unsigned long)blob, *blobsize,
+	dma_sync_single_for_cpu(jrdev, (unsigned long)blob, *blobsize,
 				DMA_FROM_DEVICE);
 
 	return ret;
diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c
index 240a440c76..ede35cea7f 100644
--- a/drivers/crypto/caam/caamrng.c
+++ b/drivers/crypto/caam/caamrng.c
@@ -90,7 +90,7 @@ static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context)
 	bd->empty = BUF_NOT_EMPTY;
 
 	/* Buffer refilled, invalidate cache */
-	dma_sync_single_for_cpu(bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE);
+	dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE);
 
 	print_hex_dump_debug("rng refreshed buf@: ", DUMP_PREFIX_OFFSET,
 			     16, 4, bd->buf, RN_BUF_SIZE, 1);
@@ -105,7 +105,7 @@ static inline int submit_job(struct caam_rng_ctx *ctx, int to_current)
 
 	dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf));
 
-	dma_sync_single_for_device((unsigned long)desc, desc_bytes(desc),
+	dma_sync_single_for_device(jrdev, (unsigned long)desc, desc_bytes(desc),
 				   DMA_TO_DEVICE);
 
 	err = caam_jr_enqueue(jrdev, desc, rng_done, ctx);
@@ -183,7 +183,7 @@ static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx)
 
 	ctx->sh_desc_dma = (dma_addr_t)desc;
 
-	dma_sync_single_for_device((unsigned long)desc, desc_bytes(desc),
+	dma_sync_single_for_device(ctx->jrdev, (unsigned long)desc, desc_bytes(desc),
 				   DMA_TO_DEVICE);
 
 	print_hex_dump_debug("rng shdesc@: ", DUMP_PREFIX_OFFSET, 16, 4,
diff --git a/drivers/crypto/caam/rng_self_test.c b/drivers/crypto/caam/rng_self_test.c
index c02f4072df..3b20f8aad2 100644
--- a/drivers/crypto/caam/rng_self_test.c
+++ b/drivers/crypto/caam/rng_self_test.c
@@ -186,9 +186,9 @@ int caam_rng_self_test(struct device *dev, const u8 caam_era, const u8 rngvid,
 
 	construct_rng_self_test_jobdesc(desc, rng_st_dsc, result, desc_size);
 
-	dma_sync_single_for_device((unsigned long)desc,
+	dma_sync_single_for_device(dev, (unsigned long)desc,
 			desc_size * sizeof(*desc), DMA_TO_DEVICE);
-	dma_sync_single_for_device((unsigned long)result,
+	dma_sync_single_for_device(dev, (unsigned long)result,
 			result_size * sizeof(*result), DMA_FROM_DEVICE);
 
 	/* wait for job completion */
@@ -205,7 +205,7 @@ int caam_rng_self_test(struct device *dev, const u8 caam_era, const u8 rngvid,
 		goto err;
 	}
 
-	dma_sync_single_for_cpu((unsigned long)result, result_size * sizeof(*result),
+	dma_sync_single_for_cpu(dev, (unsigned long)result, result_size * sizeof(*result),
 			DMA_FROM_DEVICE);
 
 	if (memcmp(result, exp_result, sizeof(*result) * result_size) != 0) {
diff --git a/drivers/mci/dove-sdhci.c b/drivers/mci/dove-sdhci.c
index ff75bb8059..54def4c0d3 100644
--- a/drivers/mci/dove-sdhci.c
+++ b/drivers/mci/dove-sdhci.c
@@ -97,10 +97,10 @@ static int dove_sdhci_mci_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 
 
 		if (data->flags & MMC_DATA_WRITE)
-			dma_sync_single_for_device((unsigned long)data->src,
+			dma_sync_single_for_device(host->mci.hw_dev, (unsigned long)data->src,
 						    num_bytes, DMA_TO_DEVICE);
 		else
-			dma_sync_single_for_device((unsigned long)data->dest,
+			dma_sync_single_for_device(host->mci.hw_dev, (unsigned long)data->dest,
 						    num_bytes, DMA_FROM_DEVICE);
 	}
 
@@ -126,10 +126,10 @@ static int dove_sdhci_mci_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 
 	if (data) {
 		if (data->flags & MMC_DATA_WRITE)
-			dma_sync_single_for_cpu((unsigned long)data->src,
+			dma_sync_single_for_cpu(host->mci.hw_dev, (unsigned long)data->src,
 						num_bytes, DMA_TO_DEVICE);
 		else
-			dma_sync_single_for_cpu((unsigned long)data->dest,
+			dma_sync_single_for_cpu(host->mci.hw_dev, (unsigned long)data->dest,
 					 num_bytes, DMA_FROM_DEVICE);
 
 		ret = dove_sdhci_wait_for_done(host, SDHCI_INT_XFER_COMPLETE);
diff --git a/drivers/mci/stm32_sdmmc2.c b/drivers/mci/stm32_sdmmc2.c
index b7bf41ee50..90e969a867 100644
--- a/drivers/mci/stm32_sdmmc2.c
+++ b/drivers/mci/stm32_sdmmc2.c
@@ -282,10 +282,10 @@ static void stm32_sdmmc2_start_data(struct stm32_sdmmc2_priv *priv,
 	writel(data_ctrl, priv->base + SDMMC_DCTRL);
 
 	if (data->flags & MMC_DATA_WRITE)
-		dma_sync_single_for_device((unsigned long)idmabase0,
+		dma_sync_single_for_device(priv->dev, (unsigned long)idmabase0,
 					   num_bytes, DMA_TO_DEVICE);
 	else
-		dma_sync_single_for_device((unsigned long)idmabase0,
+		dma_sync_single_for_device(priv->dev, (unsigned long)idmabase0,
 					   num_bytes, DMA_FROM_DEVICE);
 
 	/* Enable internal DMA */
@@ -437,10 +437,10 @@ static int stm32_sdmmc2_end_data(struct stm32_sdmmc2_priv *priv,
 	}
 
 	if (data->flags & MMC_DATA_WRITE)
-		dma_sync_single_for_cpu((unsigned long)data->src,
+		dma_sync_single_for_cpu(priv->dev, (unsigned long)data->src,
 					num_bytes, DMA_TO_DEVICE);
 	else
-		dma_sync_single_for_cpu((unsigned long)data->dest,
+		dma_sync_single_for_cpu(priv->dev, (unsigned long)data->dest,
 					num_bytes, DMA_FROM_DEVICE);
 
 	if (status & SDMMC_STA_DCRCFAIL) {
diff --git a/drivers/mci/tegra-sdmmc.c b/drivers/mci/tegra-sdmmc.c
index d3eb39bb32..1359663a73 100644
--- a/drivers/mci/tegra-sdmmc.c
+++ b/drivers/mci/tegra-sdmmc.c
@@ -113,11 +113,11 @@ static int tegra_sdmmc_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 		num_bytes = data->blocks * data->blocksize;
 
 		if (data->flags & MMC_DATA_WRITE) {
-			dma_sync_single_for_device((unsigned long)data->src,
+			dma_sync_single_for_device(mci->hw_dev, (unsigned long)data->src,
 						   num_bytes, DMA_TO_DEVICE);
 			sdhci_write32(&host->sdhci, SDHCI_DMA_ADDRESS, (u32)data->src);
 		} else {
-			dma_sync_single_for_device((unsigned long)data->dest,
+			dma_sync_single_for_device(mci->hw_dev, (unsigned long)data->dest,
 						   num_bytes, DMA_FROM_DEVICE);
 			sdhci_write32(&host->sdhci, SDHCI_DMA_ADDRESS, (u32)data->dest);
 		}
@@ -220,10 +220,10 @@ static int tegra_sdmmc_send_cmd(struct mci_host *mci, struct mci_cmd *cmd,
 		sdhci_write32(&host->sdhci, SDHCI_INT_STATUS, val);
 
 		if (data->flags & MMC_DATA_WRITE)
-			dma_sync_single_for_cpu((unsigned long)data->src,
+			dma_sync_single_for_cpu(mci->hw_dev, (unsigned long)data->src,
 						num_bytes, DMA_TO_DEVICE);
 		else
-			dma_sync_single_for_cpu((unsigned long)data->dest,
+			dma_sync_single_for_cpu(mci->hw_dev, (unsigned long)data->dest,
 						num_bytes, DMA_FROM_DEVICE);
 	}
 
diff --git a/drivers/net/ag71xx.c b/drivers/net/ag71xx.c
index 13e104d225..5979d974da 100644
--- a/drivers/net/ag71xx.c
+++ b/drivers/net/ag71xx.c
@@ -403,7 +403,7 @@ static int ag71xx_ether_rx(struct eth_device *edev)
 		rx_pkt = priv->rx_pkt[priv->next_rx];
 
 		/* invalidate */
-		dma_sync_single_for_cpu((unsigned long)rx_pkt, pktlen,
+		dma_sync_single_for_cpu(priv->dev, (unsigned long)rx_pkt, pktlen,
 						DMA_FROM_DEVICE);
 
 		net_receive(edev, rx_pkt, pktlen - 4);
@@ -431,7 +431,7 @@ static int ag71xx_ether_send(struct eth_device *edev, void *packet, int length)
 	int ret = 0;
 
 	/* flush */
-	dma_sync_single_for_device((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_device(dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 
 	f->pkt_start_addr = virt_to_phys(packet);
 	f->res1 = 0;
@@ -441,7 +441,7 @@ static int ag71xx_ether_send(struct eth_device *edev, void *packet, int length)
 	ag71xx_wr(priv, AG71XX_REG_TX_CTRL, TX_CTRL_TXE);
 
 	/* flush again?! */
-	dma_sync_single_for_cpu((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 
 	start = get_time_ns();
 	while (!f->is_empty) {
@@ -491,7 +491,7 @@ static int ag71xx_ether_init(struct eth_device *edev)
 		fr->next_desc = virt_to_phys(&priv->fifo_rx[(i + 1) % NO_OF_RX_FIFOS]);
 
 		/* invalidate */
-		dma_sync_single_for_device((unsigned long)rxbuf, MAX_RBUFF_SZ,
+		dma_sync_single_for_device(priv->dev, (unsigned long)rxbuf, MAX_RBUFF_SZ,
 					DMA_FROM_DEVICE);
 
 		rxbuf += MAX_RBUFF_SZ;
diff --git a/drivers/net/arc_emac.c b/drivers/net/arc_emac.c
index 6a68405889..fe9bcf5bb8 100644
--- a/drivers/net/arc_emac.c
+++ b/drivers/net/arc_emac.c
@@ -189,7 +189,7 @@ static int arc_emac_open(struct eth_device *edev)
 		rxbd->data = cpu_to_le32(rxbuf);
 
 		/* Return ownership to EMAC */
-		dma_sync_single_for_device((unsigned long)rxbuf, PKTSIZE,
+		dma_sync_single_for_device(NULL, (unsigned long)rxbuf, PKTSIZE,
 					   DMA_FROM_DEVICE);
 		rxbd->info = cpu_to_le32(FOR_EMAC | PKTSIZE);
 
@@ -240,7 +240,7 @@ static int arc_emac_send(struct eth_device *edev, void *data, int length)
 		length = EMAC_ZLEN;
 	}
 
-	dma_sync_single_for_device((unsigned long)data, length, DMA_TO_DEVICE);
+	dma_sync_single_for_device(NULL, (unsigned long)data, length, DMA_TO_DEVICE);
 
 	bd->data = cpu_to_le32(data);
 	bd->info = cpu_to_le32(FOR_EMAC | FIRST_OR_LAST_MASK | length);
@@ -249,7 +249,7 @@ static int arc_emac_send(struct eth_device *edev, void *data, int length)
 	ret = wait_on_timeout(20 * MSECOND,
 			      (arc_reg_get(priv, R_STATUS) & TXINT_MASK) != 0);
 
-	dma_sync_single_for_cpu((unsigned long)data, length, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(NULL, (unsigned long)data, length, DMA_TO_DEVICE);
 
 	if (ret) {
 		dev_err(&edev->dev, "transmit timeout\n");
@@ -297,12 +297,12 @@ static int arc_emac_recv(struct eth_device *edev)
 
 		pktlen = info & LEN_MASK;
 
-		dma_sync_single_for_cpu((unsigned long)rxbd->data, pktlen,
+		dma_sync_single_for_cpu(NULL, (unsigned long)rxbd->data, pktlen,
 					DMA_FROM_DEVICE);
 
 		net_receive(edev, (unsigned char *)rxbd->data, pktlen);
 
-		dma_sync_single_for_device((unsigned long)rxbd->data, pktlen,
+		dma_sync_single_for_device(NULL, (unsigned long)rxbd->data, pktlen,
 					   DMA_FROM_DEVICE);
 
 		rxbd->info = cpu_to_le32(FOR_EMAC | PKTSIZE);
diff --git a/drivers/net/at91_ether.c b/drivers/net/at91_ether.c
index 6cf700ad90..0cda213922 100644
--- a/drivers/net/at91_ether.c
+++ b/drivers/net/at91_ether.c
@@ -186,7 +186,7 @@ static int at91_ether_send(struct eth_device *edev, void *packet, int length)
 {
 	while (!(at91_emac_read(AT91_EMAC_TSR) & AT91_EMAC_TSR_BNQ));
 
-	dma_sync_single_for_device((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_device(NULL, (unsigned long)packet, length, DMA_TO_DEVICE);
 
 	/* Set address of the data in the Transmit Address register */
 	at91_emac_write(AT91_EMAC_TAR, (unsigned long) packet);
@@ -198,7 +198,7 @@ static int at91_ether_send(struct eth_device *edev, void *packet, int length)
 	at91_emac_write(AT91_EMAC_TSR,
 		at91_emac_read(AT91_EMAC_TSR) | AT91_EMAC_TSR_COMP);
 
-	dma_sync_single_for_cpu((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(NULL, (unsigned long)packet, length, DMA_TO_DEVICE);
 
 	return 0;
 }
@@ -214,10 +214,10 @@ static int at91_ether_rx(struct eth_device *edev)
 
 	size = rbfp->size & RBF_SIZE;
 
-	dma_sync_single_for_cpu((unsigned long)rbfp->addr, size,
+	dma_sync_single_for_cpu(NULL, (unsigned long)rbfp->addr, size,
 				DMA_FROM_DEVICE);
 	net_receive(edev, (unsigned char *)(rbfp->addr & RBF_ADDR), size);
-	dma_sync_single_for_device((unsigned long)rbfp->addr, size,
+	dma_sync_single_for_device(NULL, (unsigned long)rbfp->addr, size,
 				   DMA_FROM_DEVICE);
 
 	rbfp->addr &= ~RBF_OWNER;
diff --git a/drivers/net/bcmgenet.c b/drivers/net/bcmgenet.c
index 3ae341a658..9ec6aaf1e8 100644
--- a/drivers/net/bcmgenet.c
+++ b/drivers/net/bcmgenet.c
@@ -326,7 +326,7 @@ static int bcmgenet_gmac_eth_recv(struct eth_device *edev)
 	addr_hi = readl(desc_base + DMA_DESC_ADDRESS_HI);
 	addr = (u64)addr_hi << 32 | addr_lo;
 
-	dma_sync_single_for_cpu(addr, length, DMA_FROM_DEVICE);
+	dma_sync_single_for_cpu(priv->dev, addr, length, DMA_FROM_DEVICE);
 
 	/* To cater for the IP header alignment the hardware does.
 	 * This would actually not be needed if we don't program
@@ -334,7 +334,7 @@ static int bcmgenet_gmac_eth_recv(struct eth_device *edev)
 	 */
 	net_receive(edev, (void *)addr + RX_BUF_OFFSET, length - RX_BUF_OFFSET);
 
-	dma_sync_single_for_device(addr, length, DMA_FROM_DEVICE);
+	dma_sync_single_for_device(priv->dev, addr, length, DMA_FROM_DEVICE);
 
 	/* Tell the MAC we have consumed that last receive buffer. */
 	priv->c_index = (priv->c_index + 1) & 0xffff;
diff --git a/drivers/net/cpsw.c b/drivers/net/cpsw.c
index 82a1fb974e..0a8e4191dc 100644
--- a/drivers/net/cpsw.c
+++ b/drivers/net/cpsw.c
@@ -1021,10 +1021,10 @@ static int cpsw_send(struct eth_device *edev, void *packet, int length)
 
 	dev_dbg(&slave->dev, "%s: %i bytes @ 0x%p\n", __func__, length, packet);
 
-	dma_sync_single_for_device((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_device(priv->dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 	ret = cpdma_submit(priv, &priv->tx_chan, packet,
 			   length, BIT(slave->slave_num));
-	dma_sync_single_for_cpu((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(priv->dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 
 	return ret;
 }
@@ -1037,10 +1037,10 @@ static int cpsw_recv(struct eth_device *edev)
 	int len;
 
 	while (cpdma_process(slave, &priv->rx_chan, &buffer, &len) >= 0) {
-		dma_sync_single_for_cpu((unsigned long)buffer, len,
+		dma_sync_single_for_cpu(priv->dev, (unsigned long)buffer, len,
 				DMA_FROM_DEVICE);
 		net_receive(edev, buffer, len);
-		dma_sync_single_for_device((unsigned long)buffer, len,
+		dma_sync_single_for_device(priv->dev, (unsigned long)buffer, len,
 				DMA_FROM_DEVICE);
 		cpdma_submit(priv, &priv->rx_chan, buffer, PKTSIZE, 0);
 	}
diff --git a/drivers/net/davinci_emac.c b/drivers/net/davinci_emac.c
index 2415aa7443..72741b4626 100644
--- a/drivers/net/davinci_emac.c
+++ b/drivers/net/davinci_emac.c
@@ -430,7 +430,7 @@ static int davinci_emac_send(struct eth_device *edev, void *packet, int length)
 				    EMAC_CPPI_OWNERSHIP_BIT |
 				    EMAC_CPPI_EOP_BIT),
 		priv->emac_tx_desc + EMAC_DESC_PKT_FLAG_LEN);
-	dma_sync_single_for_device((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_device(priv->dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 	/* Send the packet */
 	writel(BD_TO_HW(priv->emac_tx_desc), priv->adap_emac + EMAC_TX0HDP);
 
@@ -448,7 +448,7 @@ static int davinci_emac_send(struct eth_device *edev, void *packet, int length)
 			break;
 		}
 	}
-	dma_sync_single_for_cpu((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(priv->dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 
 	dev_dbg(priv->dev, "- emac_send (ret_status %i)\n", ret_status);
 	return ret_status;
@@ -480,9 +480,9 @@ static int davinci_emac_recv(struct eth_device *edev)
 		pkt = (unsigned char *)readl(rx_curr_desc + EMAC_DESC_BUFFER);
 		len = readl(rx_curr_desc + EMAC_DESC_BUFF_OFF_LEN) & 0xffff;
 		dev_dbg(priv->dev, "| emac_recv got packet (length %i)\n", len);
-		dma_sync_single_for_cpu((unsigned long)pkt, len, DMA_FROM_DEVICE);
+		dma_sync_single_for_cpu(priv->dev, (unsigned long)pkt, len, DMA_FROM_DEVICE);
 		net_receive(edev, pkt, len);
-		dma_sync_single_for_device((unsigned long)pkt, len, DMA_FROM_DEVICE);
+		dma_sync_single_for_device(priv->dev, (unsigned long)pkt, len, DMA_FROM_DEVICE);
 		ret = len;
 	}
 
diff --git a/drivers/net/designware.c b/drivers/net/designware.c
index e9bc46da00..18ddc22cb7 100644
--- a/drivers/net/designware.c
+++ b/drivers/net/designware.c
@@ -156,7 +156,7 @@ static void rx_descs_init(struct eth_device *dev)
 		else
 			desc_p->dmamac_cntl |= DESC_RXCTRL_RXCHAIN;
 
-		dma_sync_single_for_cpu(desc_p->dmamac_addr,
+		dma_sync_single_for_cpu(NULL, desc_p->dmamac_addr,
 					CONFIG_ETH_BUFSIZE, DMA_FROM_DEVICE);
 		desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
 	}
@@ -286,7 +286,7 @@ static int dwc_ether_send(struct eth_device *dev, void *packet, int length)
 	}
 
 	memcpy(dmamac_addr(desc_p), packet, length);
-	dma_sync_single_for_device(desc_p->dmamac_addr, length,
+	dma_sync_single_for_device(NULL, desc_p->dmamac_addr, length,
 				   DMA_TO_DEVICE);
 
 	if (priv->enh_desc) {
@@ -314,7 +314,7 @@ static int dwc_ether_send(struct eth_device *dev, void *packet, int length)
 
 	/* Start the transmission */
 	writel(POLL_DATA, &dma_p->txpolldemand);
-	dma_sync_single_for_cpu(desc_p->dmamac_addr, length,
+	dma_sync_single_for_cpu(NULL, desc_p->dmamac_addr, length,
 				DMA_TO_DEVICE);
 
 	return 0;
@@ -358,10 +358,10 @@ static int dwc_ether_rx(struct eth_device *dev)
 		length = (status & DESC_RXSTS_FRMLENMSK) >>
 			 DESC_RXSTS_FRMLENSHFT;
 
-		dma_sync_single_for_cpu(desc_p->dmamac_addr,
+		dma_sync_single_for_cpu(NULL, desc_p->dmamac_addr,
 					length, DMA_FROM_DEVICE);
 		net_receive(dev, dmamac_addr(desc_p), length);
-		dma_sync_single_for_device(desc_p->dmamac_addr,
+		dma_sync_single_for_device(NULL, desc_p->dmamac_addr,
 					   length, DMA_FROM_DEVICE);
 		ret = length;
 	}
diff --git a/drivers/net/designware_eqos.c b/drivers/net/designware_eqos.c
index ee5a10a007..17eef7c935 100644
--- a/drivers/net/designware_eqos.c
+++ b/drivers/net/designware_eqos.c
@@ -739,9 +739,9 @@ static int eqos_recv(struct eth_device *edev)
 	frame = phys_to_virt(rx_desc->des0);
 	length = rx_desc->des3 & 0x7fff;
 
-	dma_sync_single_for_cpu((unsigned long)frame, length, DMA_FROM_DEVICE);
+	dma_sync_single_for_cpu(NULL, (unsigned long)frame, length, DMA_FROM_DEVICE);
 	net_receive(edev, frame, length);
-	dma_sync_single_for_device((unsigned long)frame, length, DMA_FROM_DEVICE);
+	dma_sync_single_for_device(NULL, (unsigned long)frame, length, DMA_FROM_DEVICE);
 
 	rx_desc->des0 = (unsigned long)frame;
 	rx_desc->des1 = 0;
diff --git a/drivers/net/e1000/main.c b/drivers/net/e1000/main.c
index c259d72f27..76acea563e 100644
--- a/drivers/net/e1000/main.c
+++ b/drivers/net/e1000/main.c
@@ -3438,12 +3438,12 @@ static int e1000_poll(struct eth_device *edev)
 	if (readb(&rd->status) & E1000_RXD_STAT_DD) {
 		const uint16_t len = readw(&rd->length);
 
-		dma_sync_single_for_cpu(hw->packet_dma, len,
+		dma_sync_single_for_cpu(hw->dev, hw->packet_dma, len,
 					DMA_FROM_DEVICE);
 
 		net_receive(edev, hw->packet, len);
 
-		dma_sync_single_for_device(hw->packet_dma, len,
+		dma_sync_single_for_device(hw->dev, hw->packet_dma, len,
 					   DMA_FROM_DEVICE);
 		e1000_fill_rx(hw);
 		return 1;
diff --git a/drivers/net/fec_imx.c b/drivers/net/fec_imx.c
index c817d6c767..afac0d40ed 100644
--- a/drivers/net/fec_imx.c
+++ b/drivers/net/fec_imx.c
@@ -589,7 +589,7 @@ static int fec_recv(struct eth_device *dev)
 			 * fixup and net_receive below would get
 			 * proper data
 			 */
-			dma_sync_single_for_cpu((unsigned long)frame,
+			dma_sync_single_for_cpu(fec->dev, (unsigned long)frame,
 						data_length,
 						DMA_FROM_DEVICE);
 			if (fec_is_imx28(fec))
@@ -601,7 +601,7 @@ static int fec_recv(struct eth_device *dev)
 			 */
 			len = data_length - 4;
 			net_receive(dev, frame, len);
-			dma_sync_single_for_device((unsigned long)frame,
+			dma_sync_single_for_device(fec->dev, (unsigned long)frame,
 						   data_length,
 						   DMA_FROM_DEVICE);
 		}
diff --git a/drivers/net/fsl-fman.c b/drivers/net/fsl-fman.c
index 0b85283040..1191f7b612 100644
--- a/drivers/net/fsl-fman.c
+++ b/drivers/net/fsl-fman.c
@@ -641,7 +641,8 @@ static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
 					i * MAX_RXBUF_LEN));
 		buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
 					i * MAX_RXBUF_LEN));
-		dma_sync_single_for_device((unsigned long)rx_buf_pool + i * MAX_RXBUF_LEN,
+		dma_sync_single_for_device(fm_eth->dev,
+					   (unsigned long)rx_buf_pool + i * MAX_RXBUF_LEN,
 					   MAX_RXBUF_LEN, DMA_FROM_DEVICE);
 		muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
 		out_be32(&rxbd->buf_ptr_lo, buf_lo);
@@ -911,13 +912,13 @@ static int fm_eth_recv(struct eth_device *edev)
 			data = (u8 *)((unsigned long)(buf_hi << 16) << 16 | buf_lo);
 			len = muram_readw(&rxbd->len);
 
-			dma_sync_single_for_cpu((unsigned long)data,
+			dma_sync_single_for_cpu(fm_eth->dev, (unsigned long)data,
 						len,
 						DMA_FROM_DEVICE);
 
 			net_receive(edev, data, len);
 
-			dma_sync_single_for_device((unsigned long)data,
+			dma_sync_single_for_device(fm_eth->dev, (unsigned long)data,
 						len,
 						DMA_FROM_DEVICE);
 		} else {
diff --git a/drivers/net/macb.c b/drivers/net/macb.c
index 3d7627fed8..2dda5f934b 100644
--- a/drivers/net/macb.c
+++ b/drivers/net/macb.c
@@ -119,7 +119,7 @@ static int macb_send(struct eth_device *edev, void *packet,
 	macb->tx_ring[tx_head].ctrl = ctrl;
 	macb->tx_ring[tx_head].addr = (ulong)packet;
 	barrier();
-	dma_sync_single_for_device((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_device(macb->dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 	macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
 
 	start = get_time_ns();
@@ -132,7 +132,7 @@ static int macb_send(struct eth_device *edev, void *packet,
 			break;
 		}
 	} while (!is_timeout(start, 100 * MSECOND));
-	dma_sync_single_for_cpu((unsigned long)packet, length, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(macb->dev, (unsigned long)packet, length, DMA_TO_DEVICE);
 
 	if (ctrl & MACB_BIT(TX_UNDERRUN))
 		dev_err(macb->dev, "TX underrun\n");
@@ -182,10 +182,10 @@ static int gem_recv(struct eth_device *edev)
 		status = macb->rx_ring[macb->rx_tail].ctrl;
 		length = MACB_BFEXT(RX_FRMLEN, status);
 		buffer = macb->rx_buffer + macb->rx_buffer_size * macb->rx_tail;
-		dma_sync_single_for_cpu((unsigned long)buffer, length,
+		dma_sync_single_for_cpu(macb->dev, (unsigned long)buffer, length,
 					DMA_FROM_DEVICE);
 		net_receive(edev, buffer, length);
-		dma_sync_single_for_device((unsigned long)buffer, length,
+		dma_sync_single_for_device(macb->dev, (unsigned long)buffer, length,
 					   DMA_FROM_DEVICE);
 		macb->rx_ring[macb->rx_tail].addr &= ~MACB_BIT(RX_USED);
 		barrier();
@@ -229,22 +229,22 @@ static int macb_recv(struct eth_device *edev)
 				headlen = macb->rx_buffer_size * (macb->rx_ring_size
 						 - macb->rx_tail);
 				taillen = length - headlen;
-				dma_sync_single_for_cpu((unsigned long)buffer,
+				dma_sync_single_for_cpu(macb->dev, (unsigned long)buffer,
 							headlen, DMA_FROM_DEVICE);
 				memcpy(macb->rx_packet_buf, buffer, headlen);
-				dma_sync_single_for_cpu((unsigned long)macb->rx_buffer,
+				dma_sync_single_for_cpu(macb->dev, (unsigned long)macb->rx_buffer,
 							taillen, DMA_FROM_DEVICE);
 				memcpy(macb->rx_packet_buf + headlen, macb->rx_buffer, taillen);
-				dma_sync_single_for_device((unsigned long)buffer,
+				dma_sync_single_for_device(macb->dev, (unsigned long)buffer,
 							headlen, DMA_FROM_DEVICE);
-				dma_sync_single_for_device((unsigned long)macb->rx_buffer,
+				dma_sync_single_for_device(macb->dev, (unsigned long)macb->rx_buffer,
 							taillen, DMA_FROM_DEVICE);
 				net_receive(edev, macb->rx_packet_buf, length);
 			} else {
-				dma_sync_single_for_cpu((unsigned long)buffer, length,
+				dma_sync_single_for_cpu(macb->dev, (unsigned long)buffer, length,
 							DMA_FROM_DEVICE);
 				net_receive(edev, buffer, length);
-				dma_sync_single_for_device((unsigned long)buffer, length,
+				dma_sync_single_for_device(macb->dev, (unsigned long)buffer, length,
 							DMA_FROM_DEVICE);
 			}
 			barrier();
diff --git a/drivers/net/mvneta.c b/drivers/net/mvneta.c
index 779cc636a0..fc23c0c993 100644
--- a/drivers/net/mvneta.c
+++ b/drivers/net/mvneta.c
@@ -383,7 +383,7 @@ static int mvneta_send(struct eth_device *edev, void *data, int len)
 	int ret, error, last_desc;
 
 	/* Flush transmit data */
-	dma_sync_single_for_device((unsigned long)data, len, DMA_TO_DEVICE);
+	dma_sync_single_for_device(&priv->dev, (unsigned long)data, len, DMA_TO_DEVICE);
 
 	memset(txdesc, 0, sizeof(*txdesc));
 	/* Fill the Tx descriptor */
@@ -400,7 +400,7 @@ static int mvneta_send(struct eth_device *edev, void *data, int len)
 	 * the Tx port status register (PTXS).
 	 */
 	ret = wait_on_timeout(TRANSFER_TIMEOUT, !mvneta_pending_tx(priv));
-	dma_sync_single_for_cpu((unsigned long)data, len, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(&priv->dev, (unsigned long)data, len, DMA_TO_DEVICE);
 	if (ret) {
 		dev_err(&edev->dev, "transmit timeout\n");
 		return ret;
@@ -451,7 +451,7 @@ static int mvneta_recv(struct eth_device *edev)
 	}
 
 	/* invalidate current receive buffer */
-	dma_sync_single_for_cpu((unsigned long)rxdesc->buf_phys_addr,
+	dma_sync_single_for_cpu(&priv->dev, (unsigned long)rxdesc->buf_phys_addr,
 				ALIGN(PKTSIZE, 8), DMA_FROM_DEVICE);
 
 	/* received packet is padded with two null bytes (Marvell header) */
@@ -459,7 +459,7 @@ static int mvneta_recv(struct eth_device *edev)
 			  rxdesc->data_size - MVNETA_MH_SIZE);
 	ret = 0;
 
-	dma_sync_single_for_device((unsigned long)rxdesc->buf_phys_addr,
+	dma_sync_single_for_device(&priv->dev, (unsigned long)rxdesc->buf_phys_addr,
 				   ALIGN(PKTSIZE, 8), DMA_FROM_DEVICE);
 
 recv_err:
diff --git a/drivers/net/orion-gbe.c b/drivers/net/orion-gbe.c
index abd588e013..908e847a76 100644
--- a/drivers/net/orion-gbe.c
+++ b/drivers/net/orion-gbe.c
@@ -228,7 +228,7 @@ static int port_send(struct eth_device *edev, void *data, int len)
 	int ret;
 
 	/* flush transmit data */
-	dma_sync_single_for_device((unsigned long)data, len, DMA_TO_DEVICE);
+	dma_sync_single_for_device(&port->dev, (unsigned long)data, len, DMA_TO_DEVICE);
 
 	txdesc->cmd_sts = TXDESC_OWNED_BY_DMA;
 	txdesc->cmd_sts |= TXDESC_FIRST | TXDESC_LAST;
@@ -243,7 +243,7 @@ static int port_send(struct eth_device *edev, void *data, int len)
 	/* wait for packet transmit completion */
 	ret = wait_on_timeout(TRANSFER_TIMEOUT,
 		      (readl(&txdesc->cmd_sts) & TXDESC_OWNED_BY_DMA) == 0);
-	dma_sync_single_for_cpu((unsigned long)data, len, DMA_TO_DEVICE);
+	dma_sync_single_for_cpu(&port->dev, (unsigned long)data, len, DMA_TO_DEVICE);
 	if (ret) {
 		dev_err(&edev->dev, "transmit timeout\n");
 		return ret;
@@ -287,13 +287,13 @@ static int port_recv(struct eth_device *edev)
 	}
 
 	/* invalidate current receive buffer */
-	dma_sync_single_for_cpu((unsigned long)rxdesc->buf_ptr,
+	dma_sync_single_for_cpu(&port->dev, (unsigned long)rxdesc->buf_ptr,
 				ALIGN(PKTSIZE, 8), DMA_FROM_DEVICE);
 
 	/* received packet is padded with two null bytes */
 	net_receive(edev, rxdesc->buf_ptr + 0x2, rxdesc->byte_cnt - 0x2);
 
-	dma_sync_single_for_device((unsigned long)rxdesc->buf_ptr,
+	dma_sync_single_for_device(&port->dev, (unsigned long)rxdesc->buf_ptr,
 				   ALIGN(PKTSIZE, 8), DMA_FROM_DEVICE);
 
 	ret = 0;
diff --git a/drivers/net/rtl8169.c b/drivers/net/rtl8169.c
index ffc4ef238b..b5adaf1d0f 100644
--- a/drivers/net/rtl8169.c
+++ b/drivers/net/rtl8169.c
@@ -353,6 +353,7 @@ static int rtl8169_eth_send(struct eth_device *edev, void *packet,
 				int packet_length)
 {
 	struct rtl8169_priv *priv = edev->priv;
+	struct device *dev = &priv->pci_dev->dev;
 	unsigned int entry;
 
 	entry = priv->cur_tx % NUM_TX_DESC;
@@ -360,8 +361,8 @@ static int rtl8169_eth_send(struct eth_device *edev, void *packet,
 	if (packet_length < ETH_ZLEN)
 		memset(priv->tx_buf + entry * PKT_BUF_SIZE, 0, ETH_ZLEN);
 	memcpy(priv->tx_buf + entry * PKT_BUF_SIZE, packet, packet_length);
-	dma_sync_single_for_device(priv->tx_buf_phys + entry *
-				   PKT_BUF_SIZE, PKT_BUF_SIZE, DMA_TO_DEVICE);
+	dma_sync_single_for_device(dev, priv->tx_buf_phys + entry * PKT_BUF_SIZE,
+				   PKT_BUF_SIZE, DMA_TO_DEVICE);
 
 	priv->tx_desc[entry].buf_Haddr = 0;
 	priv->tx_desc[entry].buf_addr =
@@ -382,7 +383,7 @@ static int rtl8169_eth_send(struct eth_device *edev, void *packet,
 	while (le32_to_cpu(priv->tx_desc[entry].status) & BD_STAT_OWN)
 		;
 
-	dma_sync_single_for_cpu(priv->tx_buf_phys + entry * PKT_BUF_SIZE,
+	dma_sync_single_for_cpu(dev, priv->tx_buf_phys + entry * PKT_BUF_SIZE,
 				PKT_BUF_SIZE, DMA_TO_DEVICE);
 
 	priv->cur_tx++;
@@ -393,6 +394,7 @@ static int rtl8169_eth_send(struct eth_device *edev, void *packet,
 static int rtl8169_eth_rx(struct eth_device *edev)
 {
 	struct rtl8169_priv *priv = edev->priv;
+	struct device *dev = &priv->pci_dev->dev;
 	unsigned int entry, pkt_size = 0;
 	u8 status;
 
@@ -402,13 +404,13 @@ static int rtl8169_eth_rx(struct eth_device *edev)
 		if (!(le32_to_cpu(priv->rx_desc[entry].status) & BD_STAT_RX_RES)) {
 			pkt_size = (le32_to_cpu(priv->rx_desc[entry].status) & 0x1fff) - 4;
 
-			dma_sync_single_for_cpu(priv->rx_buf_phys + entry * PKT_BUF_SIZE,
+			dma_sync_single_for_cpu(dev, priv->rx_buf_phys + entry * PKT_BUF_SIZE,
 						pkt_size, DMA_FROM_DEVICE);
 
 			net_receive(edev, priv->rx_buf + entry * PKT_BUF_SIZE,
 			            pkt_size);
 
-			dma_sync_single_for_device(priv->rx_buf_phys + entry * PKT_BUF_SIZE,
+			dma_sync_single_for_device(dev, priv->rx_buf_phys + entry * PKT_BUF_SIZE,
 						   pkt_size, DMA_FROM_DEVICE);
 
 			if (entry == NUM_RX_DESC - 1)
diff --git a/drivers/usb/gadget/fsl_udc.c b/drivers/usb/gadget/fsl_udc.c
index 2f2f4caebb..26859458c3 100644
--- a/drivers/usb/gadget/fsl_udc.c
+++ b/drivers/usb/gadget/fsl_udc.c
@@ -197,7 +197,7 @@ static void done(struct fsl_ep *ep, struct fsl_req *req, int status)
 		dma_free_coherent(curr_td, 0, sizeof(struct ep_td_struct));
 	}
 
-	dma_sync_single_for_cpu((unsigned long)req->req.buf, req->req.length,
+	dma_sync_single_for_cpu(NULL, (unsigned long)req->req.buf, req->req.length,
 				DMA_BIDIRECTIONAL);
 
 	if (status && (status != -ESHUTDOWN))
@@ -885,7 +885,7 @@ fsl_ep_queue(struct usb_ep *_ep, struct usb_request *_req)
 
 	req->ep = ep;
 
-	dma_sync_single_for_device((unsigned long)req->req.buf, req->req.length,
+	dma_sync_single_for_device(NULL, (unsigned long)req->req.buf, req->req.length,
 				   DMA_BIDIRECTIONAL);
 
 	req->req.status = -EINPROGRESS;
diff --git a/drivers/usb/host/ohci-hcd.c b/drivers/usb/host/ohci-hcd.c
index fcec5eee5e..39e946d2ef 100644
--- a/drivers/usb/host/ohci-hcd.c
+++ b/drivers/usb/host/ohci-hcd.c
@@ -842,7 +842,7 @@ static void td_fill(struct ohci *ohci, unsigned int info,
 
 	td->hwNextTD = virt_to_phys((void *)m32_swap((unsigned long)td_pt));
 
-	dma_sync_single_for_device((unsigned long)data, len, DMA_BIDIRECTIONAL);
+	dma_sync_single_for_device(NULL, (unsigned long)data, len, DMA_BIDIRECTIONAL);
 
 	/* append to queue */
 	td->ed->hwTailP = td->hwNextTD;
@@ -1078,7 +1078,7 @@ static int dl_done_list(struct ohci *ohci)
 	unsigned long ptdphys = virt_to_phys(ptd);
 	struct td *td_list;
 
-	dma_sync_single_for_device((unsigned long)ptdphys,
+	dma_sync_single_for_device(NULL, (unsigned long)ptdphys,
 				sizeof(struct td) * NUM_TD, DMA_BIDIRECTIONAL);
 
 	td_list = dl_reverse_done_list(ohci);
@@ -1515,7 +1515,7 @@ static int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *b
 	dev->status = stat;
 	dev->act_len = urb->actual_length;
 
-	dma_sync_single_for_cpu((unsigned long)buffer, transfer_len,
+	dma_sync_single_for_cpu(NULL, (unsigned long)buffer, transfer_len,
 				DMA_BIDIRECTIONAL);
 
 	pkt_print(urb, dev, pipe, buffer, transfer_len,
-- 
2.30.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC 1/2] dma: rework dma_sync_single interface
  2023-02-28 10:32 ` [RFC 1/2] dma: rework dma_sync_single interface Denis Orlov
@ 2023-03-03 10:06   ` Sascha Hauer
  2023-03-06 13:56     ` Denis Orlov
  0 siblings, 1 reply; 5+ messages in thread
From: Sascha Hauer @ 2023-03-03 10:06 UTC (permalink / raw)
  To: Denis Orlov; +Cc: barebox, Ahmad Fatoum

Hi Denis,

On Tue, Feb 28, 2023 at 01:32:47PM +0300, Denis Orlov wrote:
> -#ifndef dma_sync_single_for_device
> -static inline void dma_sync_single_for_device(dma_addr_t address, size_t size,
> -				enum dma_data_direction dir)
> +#ifndef arch_sync_dma_for_device
> +void arch_sync_dma_for_device(void *vaddr, size_t size,
> +			      enum dma_data_direction dir);
>  {
> -	barrier_data((void *)address);
> +	barrier_data(address);
>  }
>  #endif

"static inline" is missing here. Also "address" should be "vaddr".

With that fixed imx_v7_defconfig compiles successfully and from what I
can tell also works.

I haven't looked into this patch yet. Passing a struct device to the
dma_sync functions is a step into the right direction for sure.

Ultimately both patches should be merged to avoid bisecting failures.

Sascha


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC 1/2] dma: rework dma_sync_single interface
  2023-03-03 10:06   ` Sascha Hauer
@ 2023-03-06 13:56     ` Denis Orlov
  0 siblings, 0 replies; 5+ messages in thread
From: Denis Orlov @ 2023-03-06 13:56 UTC (permalink / raw)
  To: barebox

Hi!

On Fri, 3 Mar 2023 at 13:06, Sascha Hauer <s.hauer@pengutronix.de> wrote:
>
> Hi Denis,
>
> On Tue, Feb 28, 2023 at 01:32:47PM +0300, Denis Orlov wrote:
> > -#ifndef dma_sync_single_for_device
> > -static inline void dma_sync_single_for_device(dma_addr_t address, size_t size,
> > -                             enum dma_data_direction dir)
> > +#ifndef arch_sync_dma_for_device
> > +void arch_sync_dma_for_device(void *vaddr, size_t size,
> > +                           enum dma_data_direction dir);
> >  {
> > -     barrier_data((void *)address);
> > +     barrier_data(address);
> >  }
> >  #endif
>
> "static inline" is missing here. Also "address" should be "vaddr".

Ouch, I will be sure to recheck the code before sending v2.

>
> With that fixed imx_v7_defconfig compiles successfully and from what I
> can tell also works.
>
> I haven't looked into this patch yet. Passing a struct device to the
> dma_sync functions is a step into the right direction for sure.

To be short, the gist of the changes corresponds to removing the
discrepancy between
dma_map and dma_sync code that comes from dma_map doing dma/cpu conversions
and dma_sync not doing them. To do this without resorting to code
duplication, carry out
such conversions in common dma_sync functions and call arch-specific
ones for the
syncing part. The common funcs also get a device pointer as the first
argument to be
able to apply this remapping. After the dma/cpu conversion we do get a
virtual address,
so make sure that arch-specific funcs explicitly take pointers as arguments.

>
> Ultimately both patches should be merged to avoid bisecting failures.

Yeah, sure. Was going to do that anyway. I just didn’t want to dump
all of the changes in
one patch while sending this as an RFC.

>
> Sascha
>
>
> --
> Pengutronix e.K.                           |                             |
> Steuerwalder Str. 21                      | http://www.pengutronix.de/  |
> 31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
> Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-06 13:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28 10:32 [RFC 0/2] dma: clean up streaming DMA API Denis Orlov
2023-02-28 10:32 ` [RFC 1/2] dma: rework dma_sync_single interface Denis Orlov
2023-03-03 10:06   ` Sascha Hauer
2023-03-06 13:56     ` Denis Orlov
2023-02-28 10:32 ` [RFC 2/2] dma: update dma_sync_single uses in drivers Denis Orlov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox