All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h
@ 2011-09-11 19:43 Eli Billauer
  2011-09-11 19:43 ` [PATCH 2/3] microblaze: Added DMA sync operations Eli Billauer
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Eli Billauer @ 2011-09-11 19:43 UTC (permalink / raw)
  To: linux-kernel; +Cc: monstr, Eli Billauer

__dma_sync_page() was replaced by __dma_sync(), and parameters of calls to
the new function were adjusted to match __dma_sync()'s format.

Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
---
 arch/microblaze/include/asm/dma-mapping.h |   20 ++++++++++++++++++--
 arch/microblaze/kernel/dma.c              |   22 +++-------------------
 2 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/arch/microblaze/include/asm/dma-mapping.h b/arch/microblaze/include/asm/dma-mapping.h
index 8fbb0ec..cddeca5 100644
--- a/arch/microblaze/include/asm/dma-mapping.h
+++ b/arch/microblaze/include/asm/dma-mapping.h
@@ -28,12 +28,12 @@
 #include <linux/dma-attrs.h>
 #include <asm/io.h>
 #include <asm-generic/dma-coherent.h>
+#include <asm/cacheflush.h>
 
 #define DMA_ERROR_CODE		(~(dma_addr_t)0x0)
 
 #define __dma_alloc_coherent(dev, gfp, size, handle)	NULL
 #define __dma_free_coherent(size, addr)		((void)0)
-#define __dma_sync(addr, size, rw)		((void)0)
 
 static inline unsigned long device_to_mask(struct device *dev)
 {
@@ -95,6 +95,22 @@ static inline int dma_set_mask(struct device *dev, u64 dma_mask)
 
 #include <asm-generic/dma-mapping-common.h>
 
+static inline void __dma_sync(unsigned long paddr,
+			      size_t size, enum dma_data_direction direction)
+{
+	switch (direction) {
+	case DMA_TO_DEVICE:
+	case DMA_BIDIRECTIONAL:
+		flush_dcache_range(paddr, paddr + size);
+		break;
+	case DMA_FROM_DEVICE:
+		invalidate_dcache_range(paddr, paddr + size);
+		break;
+	default:
+		BUG();
+	}
+}
+
 static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
 {
 	struct dma_map_ops *ops = get_dma_ops(dev);
@@ -135,7 +151,7 @@ static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
 		enum dma_data_direction direction)
 {
 	BUG_ON(direction == DMA_NONE);
-	__dma_sync(vaddr, size, (int)direction);
+	__dma_sync(virt_to_phys(vaddr), size, (int)direction);
 }
 
 #endif	/* _ASM_MICROBLAZE_DMA_MAPPING_H */
diff --git a/arch/microblaze/kernel/dma.c b/arch/microblaze/kernel/dma.c
index 393e6b2..595130b 100644
--- a/arch/microblaze/kernel/dma.c
+++ b/arch/microblaze/kernel/dma.c
@@ -11,7 +11,6 @@
 #include <linux/gfp.h>
 #include <linux/dma-debug.h>
 #include <asm/bug.h>
-#include <asm/cacheflush.h>
 
 /*
  * Generic direct DMA implementation
@@ -21,21 +20,6 @@
  * can set archdata.dma_data to an unsigned long holding the offset. By
  * default the offset is PCI_DRAM_OFFSET.
  */
-static inline void __dma_sync_page(unsigned long paddr, unsigned long offset,
-				size_t size, enum dma_data_direction direction)
-{
-	switch (direction) {
-	case DMA_TO_DEVICE:
-	case DMA_BIDIRECTIONAL:
-		flush_dcache_range(paddr + offset, paddr + offset + size);
-		break;
-	case DMA_FROM_DEVICE:
-		invalidate_dcache_range(paddr + offset, paddr + offset + size);
-		break;
-	default:
-		BUG();
-	}
-}
 
 static unsigned long get_dma_direct_offset(struct device *dev)
 {
@@ -91,7 +75,7 @@ static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
 	/* FIXME this part of code is untested */
 	for_each_sg(sgl, sg, nents, i) {
 		sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
-		__dma_sync_page(page_to_phys(sg_page(sg)), sg->offset,
+		__dma_sync(page_to_phys(sg_page(sg)) + sg->offset,
 							sg->length, direction);
 	}
 
@@ -116,7 +100,7 @@ static inline dma_addr_t dma_direct_map_page(struct device *dev,
 					     enum dma_data_direction direction,
 					     struct dma_attrs *attrs)
 {
-	__dma_sync_page(page_to_phys(page), offset, size, direction);
+	__dma_sync(page_to_phys(page) + offset, size, direction);
 	return page_to_phys(page) + offset + get_dma_direct_offset(dev);
 }
 
@@ -131,7 +115,7 @@ static inline void dma_direct_unmap_page(struct device *dev,
  * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
  * dma_address is physical address
  */
-	__dma_sync_page(dma_address, 0 , size, direction);
+	__dma_sync(dma_address, size, direction);
 }
 
 struct dma_map_ops dma_direct_ops = {
-- 
1.7.2.3


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

* [PATCH 2/3] microblaze: Added DMA sync operations
  2011-09-11 19:43 [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h Eli Billauer
@ 2011-09-11 19:43 ` Eli Billauer
  2011-09-13 11:26   ` Michal Simek
  2011-09-11 19:43 ` [PATCH 3/3] microblaze: Refinements on cache flushes and invalidations (dma.c) Eli Billauer
  2011-09-13 11:26 ` [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h Michal Simek
  2 siblings, 1 reply; 6+ messages in thread
From: Eli Billauer @ 2011-09-11 19:43 UTC (permalink / raw)
  To: linux-kernel; +Cc: monstr, Eli Billauer

Added support gor dma_direct_sync_single_for_*() and dma_direct_sync_sg_for_*()

Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
---
 arch/microblaze/kernel/dma.c |   60 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/arch/microblaze/kernel/dma.c b/arch/microblaze/kernel/dma.c
index 595130b..e06ddad 100644
--- a/arch/microblaze/kernel/dma.c
+++ b/arch/microblaze/kernel/dma.c
@@ -118,6 +118,62 @@ static inline void dma_direct_unmap_page(struct device *dev,
 	__dma_sync(dma_address, size, direction);
 }
 
+static inline void
+dma_direct_sync_single_for_cpu(struct device *dev,
+			       dma_addr_t dma_handle, size_t size,
+			       enum dma_data_direction direction)
+{
+	/*
+	 *  It's pointless to flush the cache as the memory segment
+	 *  is given to the CPU
+	 */
+
+	if (direction == DMA_FROM_DEVICE)
+		__dma_sync(dma_handle, size, direction);
+}
+
+static inline void
+dma_direct_sync_single_for_device(struct device *dev,
+				  dma_addr_t dma_handle, size_t size,
+				  enum dma_data_direction direction)
+{
+	/*
+	 *  It's pointless to invalidate the cache if the device isn't
+	 *  supposed to write to the relevant region
+	 */
+
+	if (direction == DMA_TO_DEVICE)
+		__dma_sync(dma_handle, size, direction);
+}
+
+static inline void
+dma_direct_sync_sg_for_cpu(struct device *dev,
+			   struct scatterlist *sgl, int nents,
+			   enum dma_data_direction direction)
+{
+	struct scatterlist *sg;
+	int i;
+
+	/* FIXME this part of code is untested */
+	if (direction == DMA_FROM_DEVICE)
+		for_each_sg(sgl, sg, nents, i)
+			__dma_sync(sg->dma_address, sg->length, direction);
+}
+
+static inline void
+dma_direct_sync_sg_for_device(struct device *dev,
+			      struct scatterlist *sgl, int nents,
+			      enum dma_data_direction direction)
+{
+	struct scatterlist *sg;
+	int i;
+
+	/* FIXME this part of code is untested */
+	if (direction == DMA_TO_DEVICE)
+		for_each_sg(sgl, sg, nents, i)
+			__dma_sync(sg->dma_address, sg->length, direction);
+}
+
 struct dma_map_ops dma_direct_ops = {
 	.alloc_coherent	= dma_direct_alloc_coherent,
 	.free_coherent	= dma_direct_free_coherent,
@@ -126,6 +182,10 @@ struct dma_map_ops dma_direct_ops = {
 	.dma_supported	= dma_direct_dma_supported,
 	.map_page	= dma_direct_map_page,
 	.unmap_page	= dma_direct_unmap_page,
+	.sync_single_for_cpu		= dma_direct_sync_single_for_cpu,
+	.sync_single_for_device		= dma_direct_sync_single_for_device,
+	.sync_sg_for_cpu		= dma_direct_sync_sg_for_cpu,
+	.sync_sg_for_device		= dma_direct_sync_sg_for_device,
 };
 EXPORT_SYMBOL(dma_direct_ops);
 
-- 
1.7.2.3


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

* [PATCH 3/3] microblaze: Refinements on cache flushes and invalidations (dma.c)
  2011-09-11 19:43 [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h Eli Billauer
  2011-09-11 19:43 ` [PATCH 2/3] microblaze: Added DMA sync operations Eli Billauer
@ 2011-09-11 19:43 ` Eli Billauer
  2011-09-13 11:31   ` Michal Simek
  2011-09-13 11:26 ` [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h Michal Simek
  2 siblings, 1 reply; 6+ messages in thread
From: Eli Billauer @ 2011-09-11 19:43 UTC (permalink / raw)
  To: linux-kernel; +Cc: monstr, Eli Billauer

Mapping and unmapping methods' cache syncronization calls were refined to
avoid unnecessary synchronizations of the cache. Flushing of the cache is
now made even when mapping memory as DMA_FROM_DEVICE, so that a possible
dirty cache lines don't get flushed later, and overwrite data written by
device.

Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
---
 arch/microblaze/kernel/dma.c |   36 ++++++++++++++++++++++++++++--------
 1 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/arch/microblaze/kernel/dma.c b/arch/microblaze/kernel/dma.c
index e06ddad..2f231ee 100644
--- a/arch/microblaze/kernel/dma.c
+++ b/arch/microblaze/kernel/dma.c
@@ -76,16 +76,23 @@ static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
 	for_each_sg(sgl, sg, nents, i) {
 		sg->dma_address = sg_phys(sg) + get_dma_direct_offset(dev);
 		__dma_sync(page_to_phys(sg_page(sg)) + sg->offset,
-							sg->length, direction);
+			   sg->length, DMA_TO_DEVICE);
 	}
 
 	return nents;
 }
 
-static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
+static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
 				int nents, enum dma_data_direction direction,
 				struct dma_attrs *attrs)
 {
+	struct scatterlist *sg;
+	int i;
+
+	/* FIXME this part of code is untested */
+	if (direction == DMA_FROM_DEVICE)
+		for_each_sg(sgl, sg, nents, i)
+			__dma_sync(sg->dma_address, sg->length, direction);
 }
 
 static int dma_direct_dma_supported(struct device *dev, u64 mask)
@@ -100,7 +107,16 @@ static inline dma_addr_t dma_direct_map_page(struct device *dev,
 					     enum dma_data_direction direction,
 					     struct dma_attrs *attrs)
 {
-	__dma_sync(page_to_phys(page) + offset, size, direction);
+	/*
+	 * We're before the DMA transfer, so cache invalidation makes no
+	 * sense in the case of DMA_FROM_DEVICE. Flushing is necessary
+	 * in either case, or an unflushed cache line may overwrite
+	 * data written by device, in the event of that line being allocated
+	 * for other use. Calling __dma_sync with DMA_TO_DEVICE makes this
+	 * flush.
+	 */
+
+	__dma_sync(page_to_phys(page) + offset, size, DMA_TO_DEVICE);
 	return page_to_phys(page) + offset + get_dma_direct_offset(dev);
 }
 
@@ -110,12 +126,16 @@ static inline void dma_direct_unmap_page(struct device *dev,
 					 enum dma_data_direction direction,
 					 struct dma_attrs *attrs)
 {
-/* There is not necessary to do cache cleanup
- *
- * phys_to_virt is here because in __dma_sync_page is __virt_to_phys and
- * dma_address is physical address
+
+/*
+ * On a DMA to the device, the data has already been flushed and read by
+ * the device at the point unmapping is done. No point doing anything.
+ * In the other direction, unmapping may be used just before accessing the
+ *  data on the CPU, so cache invalidation is necessary.
  */
-	__dma_sync(dma_address, size, direction);
+
+	if (direction == DMA_FROM_DEVICE)
+		__dma_sync(dma_address, size, direction);
 }
 
 static inline void 
-- 
1.7.2.3


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

* Re: [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h
  2011-09-11 19:43 [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h Eli Billauer
  2011-09-11 19:43 ` [PATCH 2/3] microblaze: Added DMA sync operations Eli Billauer
  2011-09-11 19:43 ` [PATCH 3/3] microblaze: Refinements on cache flushes and invalidations (dma.c) Eli Billauer
@ 2011-09-13 11:26 ` Michal Simek
  2 siblings, 0 replies; 6+ messages in thread
From: Michal Simek @ 2011-09-13 11:26 UTC (permalink / raw)
  To: Eli Billauer; +Cc: linux-kernel

Eli Billauer wrote:
> __dma_sync_page() was replaced by __dma_sync(), and parameters of calls to
> the new function were adjusted to match __dma_sync()'s format.
> 
> Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
> ---
>  arch/microblaze/include/asm/dma-mapping.h |   20 ++++++++++++++++++--
>  arch/microblaze/kernel/dma.c              |   22 +++-------------------
>  2 files changed, 21 insertions(+), 21 deletions(-)

Applied.

Thanks,
Michal

-- 
Michal Simek, Ing. (M.Eng)
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel 2.6 Microblaze Linux - http://www.monstr.eu/fdt/
Microblaze U-BOOT custodian

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

* Re: [PATCH 2/3] microblaze: Added DMA sync operations
  2011-09-11 19:43 ` [PATCH 2/3] microblaze: Added DMA sync operations Eli Billauer
@ 2011-09-13 11:26   ` Michal Simek
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Simek @ 2011-09-13 11:26 UTC (permalink / raw)
  To: Eli Billauer; +Cc: linux-kernel

Eli Billauer wrote:
> Added support gor dma_direct_sync_single_for_*() and dma_direct_sync_sg_for_*()
> 
> Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
> ---
>  arch/microblaze/kernel/dma.c |   60 ++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 60 insertions(+), 0 deletions(-)

Applied.

Thanks,
Michal


-- 
Michal Simek, Ing. (M.Eng)
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel 2.6 Microblaze Linux - http://www.monstr.eu/fdt/
Microblaze U-BOOT custodian

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

* Re: [PATCH 3/3] microblaze: Refinements on cache flushes and invalidations (dma.c)
  2011-09-11 19:43 ` [PATCH 3/3] microblaze: Refinements on cache flushes and invalidations (dma.c) Eli Billauer
@ 2011-09-13 11:31   ` Michal Simek
  0 siblings, 0 replies; 6+ messages in thread
From: Michal Simek @ 2011-09-13 11:31 UTC (permalink / raw)
  To: Eli Billauer; +Cc: linux-kernel

Eli Billauer wrote:
> Mapping and unmapping methods' cache syncronization calls were refined to
> avoid unnecessary synchronizations of the cache. Flushing of the cache is
> now made even when mapping memory as DMA_FROM_DEVICE, so that a possible
> dirty cache lines don't get flushed later, and overwrite data written by
> device.
> 
> Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
> ---
>  arch/microblaze/kernel/dma.c |   36 ++++++++++++++++++++++++++++--------
>  1 files changed, 28 insertions(+), 8 deletions(-)

Keep it in my branch for testing.

Thanks,
Michal

-- 
Michal Simek, Ing. (M.Eng)
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel 2.6 Microblaze Linux - http://www.monstr.eu/fdt/
Microblaze U-BOOT custodian

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

end of thread, other threads:[~2011-09-13 11:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-11 19:43 [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h Eli Billauer
2011-09-11 19:43 ` [PATCH 2/3] microblaze: Added DMA sync operations Eli Billauer
2011-09-13 11:26   ` Michal Simek
2011-09-11 19:43 ` [PATCH 3/3] microblaze: Refinements on cache flushes and invalidations (dma.c) Eli Billauer
2011-09-13 11:31   ` Michal Simek
2011-09-13 11:26 ` [PATCH 1/3] microblaze: Moved __dma_sync() to dma-mapping.h Michal Simek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.