Commit 449fa54d authored by Fugang Duan's avatar Fugang Duan Committed by Christoph Hellwig

dma-direct: correct the physical addr in dma_direct_sync_sg_for_cpu/device

dma_map_sg() may use swiotlb buffer when the kernel command line includes
"swiotlb=force" or the dma_addr is out of dev->dma_mask range.  After
DMA complete the memory moving from device to memory, then user call
dma_sync_sg_for_cpu() to sync with DMA buffer, and copy the original
virtual buffer to other space.

So dma_direct_sync_sg_for_cpu() should use swiotlb physical addr, not
the original physical addr from sg_phys(sg).

dma_direct_sync_sg_for_device() also has the same issue, correct it as
well.

Fixes: 55897af6("dma-direct: merge swiotlb_dma_ops into the dma_direct code")
Signed-off-by: default avatarFugang Duan <fugang.duan@nxp.com>
Reviewed-by: default avatarRobin Murphy <robin.murphy@arm.com>
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
parent a5008b59
...@@ -234,12 +234,14 @@ void dma_direct_sync_sg_for_device(struct device *dev, ...@@ -234,12 +234,14 @@ void dma_direct_sync_sg_for_device(struct device *dev,
int i; int i;
for_each_sg(sgl, sg, nents, i) { for_each_sg(sgl, sg, nents, i) {
if (unlikely(is_swiotlb_buffer(sg_phys(sg)))) phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length,
if (unlikely(is_swiotlb_buffer(paddr)))
swiotlb_tbl_sync_single(dev, paddr, sg->length,
dir, SYNC_FOR_DEVICE); dir, SYNC_FOR_DEVICE);
if (!dev_is_dma_coherent(dev)) if (!dev_is_dma_coherent(dev))
arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, arch_sync_dma_for_device(dev, paddr, sg->length,
dir); dir);
} }
} }
...@@ -271,11 +273,13 @@ void dma_direct_sync_sg_for_cpu(struct device *dev, ...@@ -271,11 +273,13 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
int i; int i;
for_each_sg(sgl, sg, nents, i) { for_each_sg(sgl, sg, nents, i) {
phys_addr_t paddr = dma_to_phys(dev, sg_dma_address(sg));
if (!dev_is_dma_coherent(dev)) if (!dev_is_dma_coherent(dev))
arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir); arch_sync_dma_for_cpu(dev, paddr, sg->length, dir);
if (unlikely(is_swiotlb_buffer(sg_phys(sg)))) if (unlikely(is_swiotlb_buffer(paddr)))
swiotlb_tbl_sync_single(dev, sg_phys(sg), sg->length, dir, swiotlb_tbl_sync_single(dev, paddr, sg->length, dir,
SYNC_FOR_CPU); SYNC_FOR_CPU);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment