History

xen/arm: introduce phys/dma translations in xen_dma_sync_for_*

xen_dma_sync_for_cpu, xen_dma_sync_for_device, xen_arch_need_swiotlb are getting called passing dma addresses. On some platforms dma addresses could be different from physical addresses. Before doing any operations on these addresses we need to convert them back to physical addresses using dma_to_phys. Move the arch_sync_dma_for_cpu and arch_sync_dma_for_device calls from xen_dma_sync_for_cpu/device to swiotlb-xen.c, and add a call dma_to_phys to do address translations there. dma_cache_maint is fixed by the next patch.

Stefano Stabellini committed 6 years ago

Commit 63f06201 parent 91ffe4a3 files · +32 −23Browse files

Unified · Split

3 files
  1. arch/arm/xen/mm.c +6 −11
  2. drivers/xen/swiotlb-xen.c +24 −8
  3. include/xen/swiotlb-xen.h +2 −4
arch/arm/xen/mm.c+6−11
@@ -1,5 +1,6 @@
11 // SPDX-License-Identifier: GPL-2.0-only
22 #include <linux/cpu.h>
3+#include <linux/dma-direct.h>
34 #include <linux/dma-noncoherent.h>
45 #include <linux/gfp.h>
56 #include <linux/highmem.h>
@@ -72,22 +73,16 @@ static void dma_cache_maint(dma_addr_t handle, size_t size, u32 op) · 66 unmodified lines
7273 * dma-direct functions, otherwise we call the Xen specific version.
7374 */
7475 void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
75− phys_addr_t paddr, size_t size,
76− enum dma_data_direction dir)
76+ size_t size, enum dma_data_direction dir)
7777 {
78− if (pfn_valid(PFN_DOWN(handle)))
79− arch_sync_dma_for_cpu(paddr, size, dir);
80− else if (dir != DMA_TO_DEVICE)
78+ if (dir != DMA_TO_DEVICE)
8179 dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);
8280 }
8381
8482 void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
85− phys_addr_t paddr, size_t size,
86− enum dma_data_direction dir)
83+ size_t size, enum dma_data_direction dir)
8784 {
88− if (pfn_valid(PFN_DOWN(handle)))
89− arch_sync_dma_for_device(paddr, size, dir);
90− else if (dir == DMA_FROM_DEVICE)
85+ if (dir == DMA_FROM_DEVICE)
9186 dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);
9287 else
9388 dma_cache_maint(handle, size, GNTTAB_CACHE_CLEAN);
@@ -98,7 +93,7 @@ bool xen_arch_need_swiotlb(struct device *dev, · 4 unmodified lines
9893 dma_addr_t dev_addr)
9994 {
10095 unsigned int xen_pfn = XEN_PFN_DOWN(phys);
101− unsigned int bfn = XEN_PFN_DOWN(dev_addr);
96+ unsigned int bfn = XEN_PFN_DOWN(dma_to_phys(dev, dev_addr));
10297
10398 /*
10499 * The swiotlb buffer should be used if
drivers/xen/swiotlb-xen.c+24−8
@@ -413,8 +413,12 @@ static dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page, · 412 unmodified lines
413413 }
414414
415415 done:
416− if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
417− xen_dma_sync_for_device(dev, dev_addr, phys, size, dir);
416+ if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
417+ if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dev_addr))))
418+ arch_sync_dma_for_device(phys, size, dir);
419+ else
420+ xen_dma_sync_for_device(dev, dev_addr, size, dir);
421+ }
418422 return dev_addr;
419423 }
420424
@@ -433,8 +437,12 @@ static void xen_swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr, · 12 unmodified lines
433437
434438 BUG_ON(dir == DMA_NONE);
435439
436− if (!dev_is_dma_coherent(hwdev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
437− xen_dma_sync_for_cpu(hwdev, dev_addr, paddr, size, dir);
440+ if (!dev_is_dma_coherent(hwdev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
441+ if (pfn_valid(PFN_DOWN(dma_to_phys(hwdev, dev_addr))))
442+ arch_sync_dma_for_cpu(paddr, size, dir);
443+ else
444+ xen_dma_sync_for_cpu(hwdev, dev_addr, size, dir);
445+ }
438446
439447 /* NOTE: We use dev_addr here, not paddr! */
440448 if (is_xen_swiotlb_buffer(hwdev, dev_addr))
@@ -447,8 +455,12 @@ xen_swiotlb_sync_single_for_cpu(struct device *dev, dma_addr_t dma_addr, · 6 unmodified lines
447455 {
448456 phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
449457
450− if (!dev_is_dma_coherent(dev))
451− xen_dma_sync_for_cpu(dev, dma_addr, paddr, size, dir);
458+ if (!dev_is_dma_coherent(dev)) {
459+ if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
460+ arch_sync_dma_for_cpu(paddr, size, dir);
461+ else
462+ xen_dma_sync_for_cpu(dev, dma_addr, size, dir);
463+ }
452464
453465 if (is_xen_swiotlb_buffer(dev, dma_addr))
454466 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU);
@@ -463,8 +475,12 @@ xen_swiotlb_sync_single_for_device(struct device *dev, dma_addr_t dma_addr, · 8 unmodified lines
463475 if (is_xen_swiotlb_buffer(dev, dma_addr))
464476 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE);
465477
466− if (!dev_is_dma_coherent(dev))
467− xen_dma_sync_for_device(dev, dma_addr, paddr, size, dir);
478+ if (!dev_is_dma_coherent(dev)) {
479+ if (pfn_valid(PFN_DOWN(dma_to_phys(dev, dma_addr))))
480+ arch_sync_dma_for_device(paddr, size, dir);
481+ else
482+ xen_dma_sync_for_device(dev, dma_addr, size, dir);
483+ }
468484 }
469485
470486 /*
include/xen/swiotlb-xen.h+2−4
@@ -5,11 +5,9 @@ · 4 unmodified lines
55 #include <linux/swiotlb.h>
66
77 void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
8− phys_addr_t paddr, size_t size,
9− enum dma_data_direction dir);
8+ size_t size, enum dma_data_direction dir);
109 void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
11− phys_addr_t paddr, size_t size,
12− enum dma_data_direction dir);
10+ size_t size, enum dma_data_direction dir);
1311
1412 extern int xen_swiotlb_init(int verbose, bool early);
1513 extern const struct dma_map_ops xen_swiotlb_dma_ops;