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 @@
1// SPDX-License-Identifier: GPL-2.0-only1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/cpu.h>2#include <linux/cpu.h>
3#include <linux/dma-direct.h>
3#include <linux/dma-noncoherent.h>4#include <linux/dma-noncoherent.h>
4#include <linux/gfp.h>5#include <linux/gfp.h>
5#include <linux/highmem.h>6#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
72 * dma-direct functions, otherwise we call the Xen specific version.73 * dma-direct functions, otherwise we call the Xen specific version.
73 */74 */
74void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,75void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
75 phys_addr_t paddr, size_t size,76 size_t size, enum dma_data_direction dir)
76 enum dma_data_direction dir)
77{77{
78 if (pfn_valid(PFN_DOWN(handle)))78 if (dir != DMA_TO_DEVICE)
79 arch_sync_dma_for_cpu(paddr, size, dir);
80 else if (dir != DMA_TO_DEVICE)
81 dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);79 dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);
82}80}
83 81
84void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,82void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
85 phys_addr_t paddr, size_t size,83 size_t size, enum dma_data_direction dir)
86 enum dma_data_direction dir)
87{84{
88 if (pfn_valid(PFN_DOWN(handle)))85 if (dir == DMA_FROM_DEVICE)
89 arch_sync_dma_for_device(paddr, size, dir);
90 else if (dir == DMA_FROM_DEVICE)
91 dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);86 dma_cache_maint(handle, size, GNTTAB_CACHE_INVAL);
92 else87 else
93 dma_cache_maint(handle, size, GNTTAB_CACHE_CLEAN);88 dma_cache_maint(handle, size, GNTTAB_CACHE_CLEAN);
@@ -98,7 +93,7 @@ bool xen_arch_need_swiotlb(struct device *dev, · 4 unmodified lines
98 dma_addr_t dev_addr)93 dma_addr_t dev_addr)
99{94{
100 unsigned int xen_pfn = XEN_PFN_DOWN(phys);95 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));
102 97
103 /*98 /*
104 * The swiotlb buffer should be used if99 * 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
413 }413 }
414 414
415done:415done:
416 if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))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);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 }
418 return dev_addr;422 return dev_addr;
419}423}
420 424
@@ -433,8 +437,12 @@ static void xen_swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr, · 12 unmodified lines
433 437
434 BUG_ON(dir == DMA_NONE);438 BUG_ON(dir == DMA_NONE);
435 439
436 if (!dev_is_dma_coherent(hwdev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))440 if (!dev_is_dma_coherent(hwdev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
437 xen_dma_sync_for_cpu(hwdev, dev_addr, paddr, size, dir);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 }
438 446
439 /* NOTE: We use dev_addr here, not paddr! */447 /* NOTE: We use dev_addr here, not paddr! */
440 if (is_xen_swiotlb_buffer(hwdev, dev_addr))448 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
447{455{
448 phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);456 phys_addr_t paddr = xen_dma_to_phys(dev, dma_addr);
449 457
450 if (!dev_is_dma_coherent(dev))458 if (!dev_is_dma_coherent(dev)) {
451 xen_dma_sync_for_cpu(dev, dma_addr, paddr, size, dir);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 }
452 464
453 if (is_xen_swiotlb_buffer(dev, dma_addr))465 if (is_xen_swiotlb_buffer(dev, dma_addr))
454 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU);466 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
463 if (is_xen_swiotlb_buffer(dev, dma_addr))475 if (is_xen_swiotlb_buffer(dev, dma_addr))
464 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE);476 swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE);
465 477
466 if (!dev_is_dma_coherent(dev))478 if (!dev_is_dma_coherent(dev)) {
467 xen_dma_sync_for_device(dev, dma_addr, paddr, size, dir);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 }
468}484}
469 485
470/*486/*
include/xen/swiotlb-xen.h+2−4
@@ -5,11 +5,9 @@ · 4 unmodified lines
5#include <linux/swiotlb.h>5#include <linux/swiotlb.h>
6 6
7void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,7void xen_dma_sync_for_cpu(struct device *dev, dma_addr_t handle,
8 phys_addr_t paddr, size_t size,8 size_t size, enum dma_data_direction dir);
9 enum dma_data_direction dir);
10void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,9void xen_dma_sync_for_device(struct device *dev, dma_addr_t handle,
11 phys_addr_t paddr, size_t size,10 size_t size, enum dma_data_direction dir);
12 enum dma_data_direction dir);
13 11
14extern int xen_swiotlb_init(int verbose, bool early);12extern int xen_swiotlb_init(int verbose, bool early);
15extern const struct dma_map_ops xen_swiotlb_dma_ops;13extern const struct dma_map_ops xen_swiotlb_dma_ops;