178bfc7
From 449fa54d6815be8c2c1f68fa9dbbae9384a7c03e Mon Sep 17 00:00:00 2001
178bfc7
From: Fugang Duan <fugang.duan@nxp.com>
178bfc7
Date: Fri, 19 Jul 2019 17:26:48 +0800
178bfc7
Subject: [PATCH] dma-direct: correct the physical addr in
178bfc7
 dma_direct_sync_sg_for_cpu/device
178bfc7
178bfc7
dma_map_sg() may use swiotlb buffer when the kernel command line includes
178bfc7
"swiotlb=force" or the dma_addr is out of dev->dma_mask range.  After
178bfc7
DMA complete the memory moving from device to memory, then user call
178bfc7
dma_sync_sg_for_cpu() to sync with DMA buffer, and copy the original
178bfc7
virtual buffer to other space.
178bfc7
178bfc7
So dma_direct_sync_sg_for_cpu() should use swiotlb physical addr, not
178bfc7
the original physical addr from sg_phys(sg).
178bfc7
178bfc7
dma_direct_sync_sg_for_device() also has the same issue, correct it as
178bfc7
well.
178bfc7
178bfc7
Fixes: 55897af63091("dma-direct: merge swiotlb_dma_ops into the dma_direct code")
178bfc7
Signed-off-by: Fugang Duan <fugang.duan@nxp.com>
178bfc7
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
178bfc7
Signed-off-by: Christoph Hellwig <hch@lst.de>
178bfc7
---
178bfc7
 kernel/dma/direct.c | 18 +++++++++++-------
178bfc7
 1 file changed, 11 insertions(+), 7 deletions(-)
178bfc7
178bfc7
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
178bfc7
index e269b6f9b444..59bdceea3737 100644
178bfc7
--- a/kernel/dma/direct.c
178bfc7
+++ b/kernel/dma/direct.c
178bfc7
@@ -234,12 +234,14 @@ void dma_direct_sync_sg_for_device(struct device *dev,
178bfc7
 	int i;
178bfc7
 
178bfc7
 	for_each_sg(sgl, sg, nents, i) {
178bfc7
-		if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
178bfc7
-			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length,
178bfc7
+		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
178bfc7
+
178bfc7
+		if (unlikely(is_swiotlb_buffer(paddr)))
178bfc7
+			swiotlb_tbl_sync_single(dev, paddr, sg->length,
178bfc7
 					dir, SYNC_FOR_DEVICE);
178bfc7
 
178bfc7
 		if (!dev_is_dma_coherent(dev))
178bfc7
-			arch_sync_dma_for_device(dev, sg_phys(sg), sg->length,
178bfc7
+			arch_sync_dma_for_device(dev, paddr, sg->length,
178bfc7
 					dir);
178bfc7
 	}
178bfc7
 }
178bfc7
@@ -271,11 +273,13 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
178bfc7
 	int i;
178bfc7
 
178bfc7
 	for_each_sg(sgl, sg, nents, i) {
178bfc7
+		phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
178bfc7
+
178bfc7
 		if (!dev_is_dma_coherent(dev))
178bfc7
-			arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
178bfc7
-	
178bfc7
-		if (unlikely(is_swiotlb_buffer(sg_phys(sg))))
178bfc7
-			swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, dir,
178bfc7
+			arch_sync_dma_for_cpu(dev, paddr, sg->length, dir);
178bfc7
+
178bfc7
+		if (unlikely(is_swiotlb_buffer(paddr)))
178bfc7
+			swiotlb_tbl_sync_single(dev, paddr, sg->length, dir,
178bfc7
 					SYNC_FOR_CPU);
178bfc7
 	}
178bfc7
 
178bfc7
-- 
178bfc7
2.21.0
178bfc7